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.
7 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8 * Copyright (C) 2017 T-Platforms All Rights Reserved.
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.
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.
21 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22 * Copyright (C) 2017 T-Platforms All Rights Reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
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
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.
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.
50 * PCIe NTB Debugging Tool Linux driver
54 * How to use this tool, by example.
56 * Assuming $DBG_DIR is something like:
57 * '/sys/kernel/debug/ntb_tool/0000:00:03.0'
58 * Suppose aside from local device there is at least one remote device
59 * connected to NTB with index 0.
60 *-----------------------------------------------------------------------------
61 * Eg: check local/peer device information.
63 * # Get local device port number
64 * root@self# cat $DBG_DIR/port
66 * # Check local device functionality
67 * root@self# ls $DBG_DIR
68 * db msg1 msg_sts peer4/ port
69 * db_event msg2 peer0/ peer5/ spad0
70 * db_mask msg3 peer1/ peer_db spad1
71 * link msg_event peer2/ peer_db_mask spad2
72 * msg0 msg_mask peer3/ peer_spad spad3
73 * # As one can see it supports:
74 * # 1) four inbound message registers
75 * # 2) four inbound scratchpads
76 * # 3) up to six peer devices
78 * # Check peer device port number
79 * root@self# cat $DBG_DIR/peer0/port
81 * # Check peer device(s) functionality to be used
82 * root@self# ls $DBG_DIR/peer0
83 * link mw_trans0 mw_trans6 port
84 * link_event mw_trans1 mw_trans7 spad0
85 * msg0 mw_trans2 peer_mw_trans0 spad1
86 * msg1 mw_trans3 peer_mw_trans1 spad2
87 * msg2 mw_trans4 peer_mw_trans2 spad3
88 * msg3 mw_trans5 peer_mw_trans3
89 * # As one can see we got:
90 * # 1) four outbound message registers
91 * # 2) four outbound scratchpads
92 * # 3) eight inbound memory windows
93 * # 4) four outbound memory windows
94 *-----------------------------------------------------------------------------
97 * # Set local link up/down
98 * root@self# echo Y > $DBG_DIR/link
99 * root@self# echo N > $DBG_DIR/link
101 * # Check if link with peer device is up/down:
102 * root@self# cat $DBG_DIR/peer0/link
104 * # Block until the link is up/down
105 * root@self# echo Y > $DBG_DIR/peer0/link_event
106 * root@self# echo N > $DBG_DIR/peer0/link_event
107 *-----------------------------------------------------------------------------
108 * Eg: Doorbell registers tests (some functionality might be absent)
110 * # Set/clear/get local doorbell
111 * root@self# echo 's 1' > $DBG_DIR/db
112 * root@self# echo 'c 1' > $DBG_DIR/db
113 * root@self# cat $DBG_DIR/db
115 * # Set/clear/get local doorbell mask
116 * root@self# echo 's 1' > $DBG_DIR/db_mask
117 * root@self# echo 'c 1' > $DBG_DIR/db_mask
118 * root@self# cat $DBG_DIR/db_mask
120 * # Ring/clear/get peer doorbell
121 * root@peer# echo 's 1' > $DBG_DIR/peer_db
122 * root@peer# echo 'c 1' > $DBG_DIR/peer_db
123 * root@peer# cat $DBG_DIR/peer_db
125 * # Set/clear/get peer doorbell mask
126 * root@self# echo 's 1' > $DBG_DIR/peer_db_mask
127 * root@self# echo 'c 1' > $DBG_DIR/peer_db_mask
128 * root@self# cat $DBG_DIR/peer_db_mask
130 * # Block until local doorbell is set with specified value
131 * root@self# echo 1 > $DBG_DIR/db_event
132 *-----------------------------------------------------------------------------
133 * Eg: Message registers tests (functionality might be absent)
135 * # Set/clear/get in/out message registers status
136 * root@self# echo 's 1' > $DBG_DIR/msg_sts
137 * root@self# echo 'c 1' > $DBG_DIR/msg_sts
138 * root@self# cat $DBG_DIR/msg_sts
140 * # Set/clear in/out message registers mask
141 * root@self# echo 's 1' > $DBG_DIR/msg_mask
142 * root@self# echo 'c 1' > $DBG_DIR/msg_mask
144 * # Get inbound message register #0 value and source of port index
145 * root@self# cat $DBG_DIR/msg0
147 * # Send some data to peer over outbound message register #0
148 * root@self# echo 0x01020304 > $DBG_DIR/peer0/msg0
149 *-----------------------------------------------------------------------------
150 * Eg: Scratchpad registers tests (functionality might be absent)
152 * # Write/read to/from local scratchpad register #0
153 * root@peer# echo 0x01020304 > $DBG_DIR/spad0
154 * root@peer# cat $DBG_DIR/spad0
156 * # Write/read to/from peer scratchpad register #0
157 * root@peer# echo 0x01020304 > $DBG_DIR/peer0/spad0
158 * root@peer# cat $DBG_DIR/peer0/spad0
159 *-----------------------------------------------------------------------------
160 * Eg: Memory windows tests
162 * # Create inbound memory window buffer of specified size/get its base address
163 * root@peer# echo 16384 > $DBG_DIR/peer0/mw_trans0
164 * root@peer# cat $DBG_DIR/peer0/mw_trans0
166 * # Write/read data to/from inbound memory window
167 * root@peer# echo Hello > $DBG_DIR/peer0/mw0
168 * root@peer# head -c 7 $DBG_DIR/peer0/mw0
170 * # Map outbound memory window/check it settings (on peer device)
171 * root@peer# echo 0xADD0BA5E:16384 > $DBG_DIR/peer0/peer_mw_trans0
172 * root@peer# cat $DBG_DIR/peer0/peer_mw_trans0
174 * # Write/read data to/from outbound memory window (on peer device)
175 * root@peer# echo olleH > $DBG_DIR/peer0/peer_mw0
176 * root@peer# head -c 7 $DBG_DIR/peer0/peer_mw0
179 #include <linux/init.h>
180 #include <linux/kernel.h>
181 #include <linux/module.h>
183 #include <linux/debugfs.h>
184 #include <linux/dma-mapping.h>
185 #include <linux/pci.h>
186 #include <linux/slab.h>
187 #include <linux/uaccess.h>
189 #include <linux/ntb.h>
191 #define DRIVER_NAME "ntb_tool"
192 #define DRIVER_VERSION "2.0"
194 MODULE_LICENSE("Dual BSD/GPL");
195 MODULE_VERSION(DRIVER_VERSION);
196 MODULE_AUTHOR("Allen Hubbe <Allen.Hubbe@emc.com>");
197 MODULE_DESCRIPTION("PCIe NTB Debugging Tool");
200 * Inbound and outbound memory windows descriptor. Union members selection
201 * depends on the MW type the structure describes. mm_base/dma_base are the
202 * virtual and DMA address of an inbound MW. io_base/tr_base are the MMIO
203 * mapped virtual and xlat addresses of an outbound MW respectively.
217 resource_size_t size;
218 struct dentry *dbgfs_file;
222 * Wrapper structure is used to distinguish the outbound MW peers reference
223 * within the corresponding DebugFS directory IO operation.
225 struct tool_mw_wrap {
246 struct tool_mw *inmws;
248 struct tool_mw_wrap *outmws;
250 struct tool_msg *outmsgs;
252 struct tool_spad *outspads;
253 struct dentry *dbgfs_dir;
258 wait_queue_head_t link_wq;
259 wait_queue_head_t db_wq;
260 wait_queue_head_t msg_wq;
262 struct tool_mw *outmws;
264 struct tool_peer *peers;
266 struct tool_msg *inmsgs;
268 struct tool_spad *inspads;
269 struct dentry *dbgfs_dir;
272 #define TOOL_FOPS_RDWR(__name, __read, __write) \
273 const struct file_operations __name = { \
274 .owner = THIS_MODULE, \
275 .open = simple_open, \
280 #define TOOL_BUF_LEN 32
282 static struct dentry *tool_dbgfs_topdir;
284 /*==============================================================================
285 * NTB events handlers
286 *==============================================================================
289 static void tool_link_event(void *ctx)
291 struct tool_ctx *tc = ctx;
292 enum ntb_speed speed;
293 enum ntb_width width;
296 up = ntb_link_is_up(tc->ntb, &speed, &width);
298 dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
299 up ? "up" : "down", speed, width);
301 wake_up(&tc->link_wq);
304 static void tool_db_event(void *ctx, int vec)
306 struct tool_ctx *tc = ctx;
307 u64 db_bits, db_mask;
309 db_mask = ntb_db_vector_mask(tc->ntb, vec);
310 db_bits = ntb_db_read(tc->ntb);
312 dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
313 vec, db_mask, db_bits);
318 static void tool_msg_event(void *ctx)
320 struct tool_ctx *tc = ctx;
323 msg_sts = ntb_msg_read_sts(tc->ntb);
325 dev_dbg(&tc->ntb->dev, "message bits %#llx\n", msg_sts);
327 wake_up(&tc->msg_wq);
330 static const struct ntb_ctx_ops tool_ops = {
331 .link_event = tool_link_event,
332 .db_event = tool_db_event,
333 .msg_event = tool_msg_event
336 /*==============================================================================
337 * Common read/write methods
338 *==============================================================================
341 static ssize_t tool_fn_read(struct tool_ctx *tc, char __user *ubuf,
342 size_t size, loff_t *offp,
343 u64 (*fn_read)(struct ntb_dev *))
346 char buf[TOOL_BUF_LEN];
352 buf_size = min(size, sizeof(buf));
354 pos = scnprintf(buf, buf_size, "%#llx\n", fn_read(tc->ntb));
356 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
359 static ssize_t tool_fn_write(struct tool_ctx *tc,
360 const char __user *ubuf,
361 size_t size, loff_t *offp,
362 int (*fn_set)(struct ntb_dev *, u64),
363 int (*fn_clear)(struct ntb_dev *, u64))
373 buf = memdup_user_nul(ubuf, size);
377 n = sscanf(buf, "%c %lli", &cmd, &bits);
383 } else if (cmd == 's') {
387 ret = fn_set(tc->ntb, bits);
388 } else if (cmd == 'c') {
392 ret = fn_clear(tc->ntb, bits);
400 /*==============================================================================
401 * Port read/write methods
402 *==============================================================================
405 static ssize_t tool_port_read(struct file *filep, char __user *ubuf,
406 size_t size, loff_t *offp)
408 struct tool_ctx *tc = filep->private_data;
409 char buf[TOOL_BUF_LEN];
412 pos = scnprintf(buf, sizeof(buf), "%d\n", ntb_port_number(tc->ntb));
414 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
417 static TOOL_FOPS_RDWR(tool_port_fops,
421 static ssize_t tool_peer_port_read(struct file *filep, char __user *ubuf,
422 size_t size, loff_t *offp)
424 struct tool_peer *peer = filep->private_data;
425 struct tool_ctx *tc = peer->tc;
426 char buf[TOOL_BUF_LEN];
429 pos = scnprintf(buf, sizeof(buf), "%d\n",
430 ntb_peer_port_number(tc->ntb, peer->pidx));
432 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
435 static TOOL_FOPS_RDWR(tool_peer_port_fops,
439 static int tool_init_peers(struct tool_ctx *tc)
443 tc->peer_cnt = ntb_peer_port_count(tc->ntb);
444 tc->peers = devm_kcalloc(&tc->ntb->dev, tc->peer_cnt,
445 sizeof(*tc->peers), GFP_KERNEL);
446 if (tc->peers == NULL)
449 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
450 tc->peers[pidx].pidx = pidx;
451 tc->peers[pidx].tc = tc;
457 /*==============================================================================
458 * Link state read/write methods
459 *==============================================================================
462 static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
463 size_t size, loff_t *offp)
465 struct tool_ctx *tc = filep->private_data;
469 ret = kstrtobool_from_user(ubuf, size, &val);
474 ret = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
476 ret = ntb_link_disable(tc->ntb);
484 static TOOL_FOPS_RDWR(tool_link_fops,
488 static ssize_t tool_peer_link_read(struct file *filep, char __user *ubuf,
489 size_t size, loff_t *offp)
491 struct tool_peer *peer = filep->private_data;
492 struct tool_ctx *tc = peer->tc;
495 if (ntb_link_is_up(tc->ntb, NULL, NULL) & BIT(peer->pidx))
502 return simple_read_from_buffer(ubuf, size, offp, buf, 2);
505 static TOOL_FOPS_RDWR(tool_peer_link_fops,
509 static ssize_t tool_peer_link_event_write(struct file *filep,
510 const char __user *ubuf,
511 size_t size, loff_t *offp)
513 struct tool_peer *peer = filep->private_data;
514 struct tool_ctx *tc = peer->tc;
519 ret = kstrtobool_from_user(ubuf, size, &val);
523 link_msk = BIT_ULL_MASK(peer->pidx);
525 if (wait_event_interruptible(tc->link_wq,
526 !!(ntb_link_is_up(tc->ntb, NULL, NULL) & link_msk) == val))
532 static TOOL_FOPS_RDWR(tool_peer_link_event_fops,
534 tool_peer_link_event_write);
536 /*==============================================================================
537 * Memory windows read/write/setting methods
538 *==============================================================================
541 static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
542 size_t size, loff_t *offp)
544 struct tool_mw *inmw = filep->private_data;
546 if (inmw->mm_base == NULL)
549 return simple_read_from_buffer(ubuf, size, offp,
550 inmw->mm_base, inmw->size);
553 static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
554 size_t size, loff_t *offp)
556 struct tool_mw *inmw = filep->private_data;
558 if (inmw->mm_base == NULL)
561 return simple_write_to_buffer(inmw->mm_base, inmw->size, offp,
565 static TOOL_FOPS_RDWR(tool_mw_fops,
569 static int tool_setup_mw(struct tool_ctx *tc, int pidx, int widx,
572 resource_size_t size, addr_align, size_align;
573 struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
574 char buf[TOOL_BUF_LEN];
577 if (inmw->mm_base != NULL)
580 ret = ntb_mw_get_align(tc->ntb, pidx, widx, &addr_align,
585 inmw->size = min_t(resource_size_t, req_size, size);
586 inmw->size = round_up(inmw->size, addr_align);
587 inmw->size = round_up(inmw->size, size_align);
588 inmw->mm_base = dma_alloc_coherent(&tc->ntb->pdev->dev, inmw->size,
589 &inmw->dma_base, GFP_KERNEL);
593 if (!IS_ALIGNED(inmw->dma_base, addr_align)) {
598 ret = ntb_mw_set_trans(tc->ntb, pidx, widx, inmw->dma_base, inmw->size);
602 snprintf(buf, sizeof(buf), "mw%d", widx);
603 inmw->dbgfs_file = debugfs_create_file(buf, 0600,
604 tc->peers[pidx].dbgfs_dir, inmw,
610 dma_free_coherent(&tc->ntb->pdev->dev, inmw->size, inmw->mm_base,
612 inmw->mm_base = NULL;
619 static void tool_free_mw(struct tool_ctx *tc, int pidx, int widx)
621 struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
623 debugfs_remove(inmw->dbgfs_file);
625 if (inmw->mm_base != NULL) {
626 ntb_mw_clear_trans(tc->ntb, pidx, widx);
627 dma_free_coherent(&tc->ntb->pdev->dev, inmw->size,
628 inmw->mm_base, inmw->dma_base);
631 inmw->mm_base = NULL;
634 inmw->dbgfs_file = NULL;
637 static ssize_t tool_mw_trans_read(struct file *filep, char __user *ubuf,
638 size_t size, loff_t *offp)
640 struct tool_mw *inmw = filep->private_data;
641 resource_size_t addr_align;
642 resource_size_t size_align;
643 resource_size_t size_max;
644 ssize_t ret, off = 0;
648 buf_size = min_t(size_t, size, 512);
650 buf = kmalloc(buf_size, GFP_KERNEL);
654 ret = ntb_mw_get_align(inmw->tc->ntb, inmw->pidx, inmw->widx,
655 &addr_align, &size_align, &size_max);
659 off += scnprintf(buf + off, buf_size - off,
663 off += scnprintf(buf + off, buf_size - off,
665 ntb_peer_port_number(inmw->tc->ntb, inmw->pidx),
668 off += scnprintf(buf + off, buf_size - off,
669 "Window Address \t0x%pK\n", inmw->mm_base);
671 off += scnprintf(buf + off, buf_size - off,
672 "DMA Address \t%pad\n",
675 off += scnprintf(buf + off, buf_size - off,
676 "Window Size \t%pap\n",
679 off += scnprintf(buf + off, buf_size - off,
680 "Alignment \t%pap\n",
683 off += scnprintf(buf + off, buf_size - off,
684 "Size Alignment \t%pap\n",
687 off += scnprintf(buf + off, buf_size - off,
691 ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
699 static ssize_t tool_mw_trans_write(struct file *filep, const char __user *ubuf,
700 size_t size, loff_t *offp)
702 struct tool_mw *inmw = filep->private_data;
706 ret = kstrtouint_from_user(ubuf, size, 0, &val);
710 tool_free_mw(inmw->tc, inmw->pidx, inmw->widx);
712 ret = tool_setup_mw(inmw->tc, inmw->pidx, inmw->widx, val);
720 static TOOL_FOPS_RDWR(tool_mw_trans_fops,
722 tool_mw_trans_write);
724 static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
725 size_t size, loff_t *offp)
727 struct tool_mw *outmw = filep->private_data;
732 if (outmw->io_base == NULL)
735 if (pos >= outmw->size || !size)
738 if (size > outmw->size - pos)
739 size = outmw->size - pos;
741 buf = kmalloc(size, GFP_KERNEL);
745 memcpy_fromio(buf, outmw->io_base + pos, size);
746 ret = copy_to_user(ubuf, buf, size);
762 static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
763 size_t size, loff_t *offp)
765 struct tool_mw *outmw = filep->private_data;
770 if (outmw->io_base == NULL)
773 if (pos >= outmw->size || !size)
775 if (size > outmw->size - pos)
776 size = outmw->size - pos;
778 buf = kmalloc(size, GFP_KERNEL);
782 ret = copy_from_user(buf, ubuf, size);
792 memcpy_toio(outmw->io_base + pos, buf, size);
800 static TOOL_FOPS_RDWR(tool_peer_mw_fops,
804 static int tool_setup_peer_mw(struct tool_ctx *tc, int pidx, int widx,
805 u64 req_addr, size_t req_size)
807 struct tool_mw *outmw = &tc->outmws[widx];
808 resource_size_t map_size;
809 phys_addr_t map_base;
810 char buf[TOOL_BUF_LEN];
813 if (outmw->io_base != NULL)
816 ret = ntb_peer_mw_get_addr(tc->ntb, widx, &map_base, &map_size);
820 ret = ntb_peer_mw_set_trans(tc->ntb, pidx, widx, req_addr, req_size);
824 outmw->io_base = ioremap_wc(map_base, map_size);
825 if (outmw->io_base == NULL) {
827 goto err_clear_trans;
830 outmw->tr_base = req_addr;
831 outmw->size = req_size;
834 snprintf(buf, sizeof(buf), "peer_mw%d", widx);
835 outmw->dbgfs_file = debugfs_create_file(buf, 0600,
836 tc->peers[pidx].dbgfs_dir, outmw,
842 ntb_peer_mw_clear_trans(tc->ntb, pidx, widx);
847 static void tool_free_peer_mw(struct tool_ctx *tc, int widx)
849 struct tool_mw *outmw = &tc->outmws[widx];
851 debugfs_remove(outmw->dbgfs_file);
853 if (outmw->io_base != NULL) {
854 iounmap(tc->outmws[widx].io_base);
855 ntb_peer_mw_clear_trans(tc->ntb, outmw->pidx, widx);
858 outmw->io_base = NULL;
862 outmw->dbgfs_file = NULL;
865 static ssize_t tool_peer_mw_trans_read(struct file *filep, char __user *ubuf,
866 size_t size, loff_t *offp)
868 struct tool_mw_wrap *outmw_wrap = filep->private_data;
869 struct tool_mw *outmw = outmw_wrap->mw;
870 resource_size_t map_size;
871 phys_addr_t map_base;
877 ret = ntb_peer_mw_get_addr(outmw->tc->ntb, outmw->widx,
878 &map_base, &map_size);
882 buf_size = min_t(size_t, size, 512);
884 buf = kmalloc(buf_size, GFP_KERNEL);
888 off += scnprintf(buf + off, buf_size - off,
889 "Outbound MW: \t%d\n", outmw->widx);
891 if (outmw->io_base != NULL) {
892 off += scnprintf(buf + off, buf_size - off,
893 "Port attached \t%d (%d)\n",
894 ntb_peer_port_number(outmw->tc->ntb, outmw->pidx),
897 off += scnprintf(buf + off, buf_size - off,
898 "Port attached \t-1 (-1)\n");
901 off += scnprintf(buf + off, buf_size - off,
902 "Virtual address \t0x%pK\n", outmw->io_base);
904 off += scnprintf(buf + off, buf_size - off,
905 "Phys Address \t%pap\n", &map_base);
907 off += scnprintf(buf + off, buf_size - off,
908 "Mapping Size \t%pap\n", &map_size);
910 off += scnprintf(buf + off, buf_size - off,
911 "Translation Address \t0x%016llx\n", outmw->tr_base);
913 off += scnprintf(buf + off, buf_size - off,
914 "Window Size \t%pap\n", &outmw->size);
916 ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
922 static ssize_t tool_peer_mw_trans_write(struct file *filep,
923 const char __user *ubuf,
924 size_t size, loff_t *offp)
926 struct tool_mw_wrap *outmw_wrap = filep->private_data;
927 struct tool_mw *outmw = outmw_wrap->mw;
928 size_t buf_size, wsize;
929 char buf[TOOL_BUF_LEN];
933 buf_size = min(size, (sizeof(buf) - 1));
934 if (copy_from_user(buf, ubuf, buf_size))
937 buf[buf_size] = '\0';
939 n = sscanf(buf, "%lli:%zi", &addr, &wsize);
943 tool_free_peer_mw(outmw->tc, outmw->widx);
945 ret = tool_setup_peer_mw(outmw->tc, outmw_wrap->pidx,
946 outmw->widx, addr, wsize);
954 static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
955 tool_peer_mw_trans_read,
956 tool_peer_mw_trans_write);
958 static int tool_init_mws(struct tool_ctx *tc)
962 /* Initialize outbound memory windows */
963 tc->outmw_cnt = ntb_peer_mw_count(tc->ntb);
964 tc->outmws = devm_kcalloc(&tc->ntb->dev, tc->outmw_cnt,
965 sizeof(*tc->outmws), GFP_KERNEL);
966 if (tc->outmws == NULL)
969 for (widx = 0; widx < tc->outmw_cnt; widx++) {
970 tc->outmws[widx].widx = widx;
971 tc->outmws[widx].pidx = -1;
972 tc->outmws[widx].tc = tc;
975 /* Initialize inbound memory windows and outbound MWs wrapper */
976 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
977 tc->peers[pidx].inmw_cnt = ntb_mw_count(tc->ntb, pidx);
978 tc->peers[pidx].inmws =
979 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].inmw_cnt,
980 sizeof(*tc->peers[pidx].inmws), GFP_KERNEL);
981 if (tc->peers[pidx].inmws == NULL)
984 for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
985 tc->peers[pidx].inmws[widx].widx = widx;
986 tc->peers[pidx].inmws[widx].pidx = pidx;
987 tc->peers[pidx].inmws[widx].tc = tc;
990 tc->peers[pidx].outmw_cnt = ntb_peer_mw_count(tc->ntb);
991 tc->peers[pidx].outmws =
992 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmw_cnt,
993 sizeof(*tc->peers[pidx].outmws), GFP_KERNEL);
994 if (tc->peers[pidx].outmws == NULL)
997 for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
998 tc->peers[pidx].outmws[widx].pidx = pidx;
999 tc->peers[pidx].outmws[widx].mw = &tc->outmws[widx];
1006 static void tool_clear_mws(struct tool_ctx *tc)
1010 /* Free outbound memory windows */
1011 for (widx = 0; widx < tc->outmw_cnt; widx++)
1012 tool_free_peer_mw(tc, widx);
1014 /* Free outbound memory windows */
1015 for (pidx = 0; pidx < tc->peer_cnt; pidx++)
1016 for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
1017 tool_free_mw(tc, pidx, widx);
1020 /*==============================================================================
1021 * Doorbell read/write methods
1022 *==============================================================================
1025 static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
1026 size_t size, loff_t *offp)
1028 struct tool_ctx *tc = filep->private_data;
1030 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read);
1033 static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
1034 size_t size, loff_t *offp)
1036 struct tool_ctx *tc = filep->private_data;
1038 return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set,
1039 tc->ntb->ops->db_clear);
1042 static TOOL_FOPS_RDWR(tool_db_fops,
1046 static ssize_t tool_db_valid_mask_read(struct file *filep, char __user *ubuf,
1047 size_t size, loff_t *offp)
1049 struct tool_ctx *tc = filep->private_data;
1051 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_valid_mask);
1054 static TOOL_FOPS_RDWR(tool_db_valid_mask_fops,
1055 tool_db_valid_mask_read,
1058 static ssize_t tool_db_mask_read(struct file *filep, char __user *ubuf,
1059 size_t size, loff_t *offp)
1061 struct tool_ctx *tc = filep->private_data;
1063 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read_mask);
1066 static ssize_t tool_db_mask_write(struct file *filep, const char __user *ubuf,
1067 size_t size, loff_t *offp)
1069 struct tool_ctx *tc = filep->private_data;
1071 return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set_mask,
1072 tc->ntb->ops->db_clear_mask);
1075 static TOOL_FOPS_RDWR(tool_db_mask_fops,
1077 tool_db_mask_write);
1079 static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
1080 size_t size, loff_t *offp)
1082 struct tool_ctx *tc = filep->private_data;
1084 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->peer_db_read);
1087 static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
1088 size_t size, loff_t *offp)
1090 struct tool_ctx *tc = filep->private_data;
1092 return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->peer_db_set,
1093 tc->ntb->ops->peer_db_clear);
1096 static TOOL_FOPS_RDWR(tool_peer_db_fops,
1098 tool_peer_db_write);
1100 static ssize_t tool_peer_db_mask_read(struct file *filep, char __user *ubuf,
1101 size_t size, loff_t *offp)
1103 struct tool_ctx *tc = filep->private_data;
1105 return tool_fn_read(tc, ubuf, size, offp,
1106 tc->ntb->ops->peer_db_read_mask);
1109 static ssize_t tool_peer_db_mask_write(struct file *filep,
1110 const char __user *ubuf,
1111 size_t size, loff_t *offp)
1113 struct tool_ctx *tc = filep->private_data;
1115 return tool_fn_write(tc, ubuf, size, offp,
1116 tc->ntb->ops->peer_db_set_mask,
1117 tc->ntb->ops->peer_db_clear_mask);
1120 static TOOL_FOPS_RDWR(tool_peer_db_mask_fops,
1121 tool_peer_db_mask_read,
1122 tool_peer_db_mask_write);
1124 static ssize_t tool_db_event_write(struct file *filep,
1125 const char __user *ubuf,
1126 size_t size, loff_t *offp)
1128 struct tool_ctx *tc = filep->private_data;
1132 ret = kstrtou64_from_user(ubuf, size, 0, &val);
1136 if (wait_event_interruptible(tc->db_wq, ntb_db_read(tc->ntb) == val))
1142 static TOOL_FOPS_RDWR(tool_db_event_fops,
1144 tool_db_event_write);
1146 /*==============================================================================
1147 * Scratchpads read/write methods
1148 *==============================================================================
1151 static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
1152 size_t size, loff_t *offp)
1154 struct tool_spad *spad = filep->private_data;
1155 char buf[TOOL_BUF_LEN];
1158 if (!spad->tc->ntb->ops->spad_read)
1161 pos = scnprintf(buf, sizeof(buf), "%#x\n",
1162 ntb_spad_read(spad->tc->ntb, spad->sidx));
1164 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1167 static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
1168 size_t size, loff_t *offp)
1170 struct tool_spad *spad = filep->private_data;
1174 if (!spad->tc->ntb->ops->spad_write) {
1175 dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
1179 ret = kstrtou32_from_user(ubuf, size, 0, &val);
1183 ret = ntb_spad_write(spad->tc->ntb, spad->sidx, val);
1188 static TOOL_FOPS_RDWR(tool_spad_fops,
1192 static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
1193 size_t size, loff_t *offp)
1195 struct tool_spad *spad = filep->private_data;
1196 char buf[TOOL_BUF_LEN];
1199 if (!spad->tc->ntb->ops->peer_spad_read)
1202 pos = scnprintf(buf, sizeof(buf), "%#x\n",
1203 ntb_peer_spad_read(spad->tc->ntb, spad->pidx, spad->sidx));
1205 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1208 static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
1209 size_t size, loff_t *offp)
1211 struct tool_spad *spad = filep->private_data;
1215 if (!spad->tc->ntb->ops->peer_spad_write) {
1216 dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
1220 ret = kstrtou32_from_user(ubuf, size, 0, &val);
1224 ret = ntb_peer_spad_write(spad->tc->ntb, spad->pidx, spad->sidx, val);
1229 static TOOL_FOPS_RDWR(tool_peer_spad_fops,
1230 tool_peer_spad_read,
1231 tool_peer_spad_write);
1233 static int tool_init_spads(struct tool_ctx *tc)
1237 /* Initialize inbound scratchpad structures */
1238 tc->inspad_cnt = ntb_spad_count(tc->ntb);
1239 tc->inspads = devm_kcalloc(&tc->ntb->dev, tc->inspad_cnt,
1240 sizeof(*tc->inspads), GFP_KERNEL);
1241 if (tc->inspads == NULL)
1244 for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
1245 tc->inspads[sidx].sidx = sidx;
1246 tc->inspads[sidx].pidx = -1;
1247 tc->inspads[sidx].tc = tc;
1250 /* Initialize outbound scratchpad structures */
1251 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
1252 tc->peers[pidx].outspad_cnt = ntb_spad_count(tc->ntb);
1253 tc->peers[pidx].outspads =
1254 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outspad_cnt,
1255 sizeof(*tc->peers[pidx].outspads), GFP_KERNEL);
1256 if (tc->peers[pidx].outspads == NULL)
1259 for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
1260 tc->peers[pidx].outspads[sidx].sidx = sidx;
1261 tc->peers[pidx].outspads[sidx].pidx = pidx;
1262 tc->peers[pidx].outspads[sidx].tc = tc;
1269 /*==============================================================================
1270 * Messages read/write methods
1271 *==============================================================================
1274 static ssize_t tool_inmsg_read(struct file *filep, char __user *ubuf,
1275 size_t size, loff_t *offp)
1277 struct tool_msg *msg = filep->private_data;
1278 char buf[TOOL_BUF_LEN];
1283 data = ntb_msg_read(msg->tc->ntb, &pidx, msg->midx);
1285 pos = scnprintf(buf, sizeof(buf), "0x%08x<-%d\n", data, pidx);
1287 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1290 static TOOL_FOPS_RDWR(tool_inmsg_fops,
1294 static ssize_t tool_outmsg_write(struct file *filep,
1295 const char __user *ubuf,
1296 size_t size, loff_t *offp)
1298 struct tool_msg *msg = filep->private_data;
1302 ret = kstrtou32_from_user(ubuf, size, 0, &val);
1306 ret = ntb_peer_msg_write(msg->tc->ntb, msg->pidx, msg->midx, val);
1308 return ret ? : size;
1311 static TOOL_FOPS_RDWR(tool_outmsg_fops,
1315 static ssize_t tool_msg_sts_read(struct file *filep, char __user *ubuf,
1316 size_t size, loff_t *offp)
1318 struct tool_ctx *tc = filep->private_data;
1320 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_read_sts);
1323 static ssize_t tool_msg_sts_write(struct file *filep, const char __user *ubuf,
1324 size_t size, loff_t *offp)
1326 struct tool_ctx *tc = filep->private_data;
1328 return tool_fn_write(tc, ubuf, size, offp, NULL,
1329 tc->ntb->ops->msg_clear_sts);
1332 static TOOL_FOPS_RDWR(tool_msg_sts_fops,
1334 tool_msg_sts_write);
1336 static ssize_t tool_msg_inbits_read(struct file *filep, char __user *ubuf,
1337 size_t size, loff_t *offp)
1339 struct tool_ctx *tc = filep->private_data;
1341 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_inbits);
1344 static TOOL_FOPS_RDWR(tool_msg_inbits_fops,
1345 tool_msg_inbits_read,
1348 static ssize_t tool_msg_outbits_read(struct file *filep, char __user *ubuf,
1349 size_t size, loff_t *offp)
1351 struct tool_ctx *tc = filep->private_data;
1353 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_outbits);
1356 static TOOL_FOPS_RDWR(tool_msg_outbits_fops,
1357 tool_msg_outbits_read,
1360 static ssize_t tool_msg_mask_write(struct file *filep, const char __user *ubuf,
1361 size_t size, loff_t *offp)
1363 struct tool_ctx *tc = filep->private_data;
1365 return tool_fn_write(tc, ubuf, size, offp,
1366 tc->ntb->ops->msg_set_mask,
1367 tc->ntb->ops->msg_clear_mask);
1370 static TOOL_FOPS_RDWR(tool_msg_mask_fops,
1372 tool_msg_mask_write);
1374 static ssize_t tool_msg_event_write(struct file *filep,
1375 const char __user *ubuf,
1376 size_t size, loff_t *offp)
1378 struct tool_ctx *tc = filep->private_data;
1382 ret = kstrtou64_from_user(ubuf, size, 0, &val);
1386 if (wait_event_interruptible(tc->msg_wq,
1387 ntb_msg_read_sts(tc->ntb) == val))
1393 static TOOL_FOPS_RDWR(tool_msg_event_fops,
1395 tool_msg_event_write);
1397 static int tool_init_msgs(struct tool_ctx *tc)
1401 /* Initialize inbound message structures */
1402 tc->inmsg_cnt = ntb_msg_count(tc->ntb);
1403 tc->inmsgs = devm_kcalloc(&tc->ntb->dev, tc->inmsg_cnt,
1404 sizeof(*tc->inmsgs), GFP_KERNEL);
1405 if (tc->inmsgs == NULL)
1408 for (midx = 0; midx < tc->inmsg_cnt; midx++) {
1409 tc->inmsgs[midx].midx = midx;
1410 tc->inmsgs[midx].pidx = -1;
1411 tc->inmsgs[midx].tc = tc;
1414 /* Initialize outbound message structures */
1415 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
1416 tc->peers[pidx].outmsg_cnt = ntb_msg_count(tc->ntb);
1417 tc->peers[pidx].outmsgs =
1418 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmsg_cnt,
1419 sizeof(*tc->peers[pidx].outmsgs), GFP_KERNEL);
1420 if (tc->peers[pidx].outmsgs == NULL)
1423 for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
1424 tc->peers[pidx].outmsgs[midx].midx = midx;
1425 tc->peers[pidx].outmsgs[midx].pidx = pidx;
1426 tc->peers[pidx].outmsgs[midx].tc = tc;
1433 /*==============================================================================
1434 * Initialization methods
1435 *==============================================================================
1438 static struct tool_ctx *tool_create_data(struct ntb_dev *ntb)
1440 struct tool_ctx *tc;
1442 tc = devm_kzalloc(&ntb->dev, sizeof(*tc), GFP_KERNEL);
1444 return ERR_PTR(-ENOMEM);
1447 init_waitqueue_head(&tc->link_wq);
1448 init_waitqueue_head(&tc->db_wq);
1449 init_waitqueue_head(&tc->msg_wq);
1451 if (ntb_db_is_unsafe(ntb))
1452 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
1454 if (ntb_spad_is_unsafe(ntb))
1455 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
1460 static void tool_clear_data(struct tool_ctx *tc)
1462 wake_up(&tc->link_wq);
1463 wake_up(&tc->db_wq);
1464 wake_up(&tc->msg_wq);
1467 static int tool_init_ntb(struct tool_ctx *tc)
1469 return ntb_set_ctx(tc->ntb, tc, &tool_ops);
1472 static void tool_clear_ntb(struct tool_ctx *tc)
1474 ntb_clear_ctx(tc->ntb);
1475 ntb_link_disable(tc->ntb);
1478 static void tool_setup_dbgfs(struct tool_ctx *tc)
1480 int pidx, widx, sidx, midx;
1481 char buf[TOOL_BUF_LEN];
1483 /* This modules is useless without dbgfs... */
1484 if (!tool_dbgfs_topdir) {
1485 tc->dbgfs_dir = NULL;
1489 tc->dbgfs_dir = debugfs_create_dir(dev_name(&tc->ntb->dev),
1492 debugfs_create_file("port", 0600, tc->dbgfs_dir,
1493 tc, &tool_port_fops);
1495 debugfs_create_file("link", 0600, tc->dbgfs_dir,
1496 tc, &tool_link_fops);
1498 debugfs_create_file("db", 0600, tc->dbgfs_dir,
1501 debugfs_create_file("db_valid_mask", 0600, tc->dbgfs_dir,
1502 tc, &tool_db_valid_mask_fops);
1504 debugfs_create_file("db_mask", 0600, tc->dbgfs_dir,
1505 tc, &tool_db_mask_fops);
1507 debugfs_create_file("db_event", 0600, tc->dbgfs_dir,
1508 tc, &tool_db_event_fops);
1510 debugfs_create_file("peer_db", 0600, tc->dbgfs_dir,
1511 tc, &tool_peer_db_fops);
1513 debugfs_create_file("peer_db_mask", 0600, tc->dbgfs_dir,
1514 tc, &tool_peer_db_mask_fops);
1516 if (tc->inspad_cnt != 0) {
1517 for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
1518 snprintf(buf, sizeof(buf), "spad%d", sidx);
1520 debugfs_create_file(buf, 0600, tc->dbgfs_dir,
1521 &tc->inspads[sidx], &tool_spad_fops);
1525 if (tc->inmsg_cnt != 0) {
1526 for (midx = 0; midx < tc->inmsg_cnt; midx++) {
1527 snprintf(buf, sizeof(buf), "msg%d", midx);
1528 debugfs_create_file(buf, 0600, tc->dbgfs_dir,
1529 &tc->inmsgs[midx], &tool_inmsg_fops);
1532 debugfs_create_file("msg_sts", 0600, tc->dbgfs_dir,
1533 tc, &tool_msg_sts_fops);
1535 debugfs_create_file("msg_inbits", 0600, tc->dbgfs_dir,
1536 tc, &tool_msg_inbits_fops);
1538 debugfs_create_file("msg_outbits", 0600, tc->dbgfs_dir,
1539 tc, &tool_msg_outbits_fops);
1541 debugfs_create_file("msg_mask", 0600, tc->dbgfs_dir,
1542 tc, &tool_msg_mask_fops);
1544 debugfs_create_file("msg_event", 0600, tc->dbgfs_dir,
1545 tc, &tool_msg_event_fops);
1548 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
1549 snprintf(buf, sizeof(buf), "peer%d", pidx);
1550 tc->peers[pidx].dbgfs_dir =
1551 debugfs_create_dir(buf, tc->dbgfs_dir);
1553 debugfs_create_file("port", 0600,
1554 tc->peers[pidx].dbgfs_dir,
1555 &tc->peers[pidx], &tool_peer_port_fops);
1557 debugfs_create_file("link", 0200,
1558 tc->peers[pidx].dbgfs_dir,
1559 &tc->peers[pidx], &tool_peer_link_fops);
1561 debugfs_create_file("link_event", 0200,
1562 tc->peers[pidx].dbgfs_dir,
1563 &tc->peers[pidx], &tool_peer_link_event_fops);
1565 for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
1566 snprintf(buf, sizeof(buf), "mw_trans%d", widx);
1567 debugfs_create_file(buf, 0600,
1568 tc->peers[pidx].dbgfs_dir,
1569 &tc->peers[pidx].inmws[widx],
1570 &tool_mw_trans_fops);
1573 for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
1574 snprintf(buf, sizeof(buf), "peer_mw_trans%d", widx);
1575 debugfs_create_file(buf, 0600,
1576 tc->peers[pidx].dbgfs_dir,
1577 &tc->peers[pidx].outmws[widx],
1578 &tool_peer_mw_trans_fops);
1581 for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
1582 snprintf(buf, sizeof(buf), "spad%d", sidx);
1584 debugfs_create_file(buf, 0600,
1585 tc->peers[pidx].dbgfs_dir,
1586 &tc->peers[pidx].outspads[sidx],
1587 &tool_peer_spad_fops);
1590 for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
1591 snprintf(buf, sizeof(buf), "msg%d", midx);
1592 debugfs_create_file(buf, 0600,
1593 tc->peers[pidx].dbgfs_dir,
1594 &tc->peers[pidx].outmsgs[midx],
1600 static void tool_clear_dbgfs(struct tool_ctx *tc)
1602 debugfs_remove_recursive(tc->dbgfs_dir);
1605 static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
1607 struct tool_ctx *tc;
1610 tc = tool_create_data(ntb);
1614 ret = tool_init_peers(tc);
1616 goto err_clear_data;
1618 ret = tool_init_mws(tc);
1620 goto err_clear_data;
1622 ret = tool_init_spads(tc);
1626 ret = tool_init_msgs(tc);
1630 ret = tool_init_ntb(tc);
1634 tool_setup_dbgfs(tc);
1642 tool_clear_data(tc);
1647 static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
1649 struct tool_ctx *tc = ntb->ctx;
1651 tool_clear_dbgfs(tc);
1657 tool_clear_data(tc);
1660 static struct ntb_client tool_client = {
1662 .probe = tool_probe,
1663 .remove = tool_remove,
1667 static int __init tool_init(void)
1671 if (debugfs_initialized())
1672 tool_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1674 ret = ntb_register_client(&tool_client);
1676 debugfs_remove_recursive(tool_dbgfs_topdir);
1680 module_init(tool_init);
1682 static void __exit tool_exit(void)
1684 ntb_unregister_client(&tool_client);
1685 debugfs_remove_recursive(tool_dbgfs_topdir);
1687 module_exit(tool_exit);