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 = kmalloc(size + 1, GFP_KERNEL);
377 if (copy_from_user(buf, ubuf, size)) {
384 n = sscanf(buf, "%c %lli", &cmd, &bits);
390 } else if (cmd == 's') {
394 ret = fn_set(tc->ntb, bits);
395 } else if (cmd == 'c') {
399 ret = fn_clear(tc->ntb, bits);
407 /*==============================================================================
408 * Port read/write methods
409 *==============================================================================
412 static ssize_t tool_port_read(struct file *filep, char __user *ubuf,
413 size_t size, loff_t *offp)
415 struct tool_ctx *tc = filep->private_data;
416 char buf[TOOL_BUF_LEN];
419 pos = scnprintf(buf, sizeof(buf), "%d\n", ntb_port_number(tc->ntb));
421 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
424 static TOOL_FOPS_RDWR(tool_port_fops,
428 static ssize_t tool_peer_port_read(struct file *filep, char __user *ubuf,
429 size_t size, loff_t *offp)
431 struct tool_peer *peer = filep->private_data;
432 struct tool_ctx *tc = peer->tc;
433 char buf[TOOL_BUF_LEN];
436 pos = scnprintf(buf, sizeof(buf), "%d\n",
437 ntb_peer_port_number(tc->ntb, peer->pidx));
439 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
442 static TOOL_FOPS_RDWR(tool_peer_port_fops,
446 static int tool_init_peers(struct tool_ctx *tc)
450 tc->peer_cnt = ntb_peer_port_count(tc->ntb);
451 tc->peers = devm_kcalloc(&tc->ntb->dev, tc->peer_cnt,
452 sizeof(*tc->peers), GFP_KERNEL);
453 if (tc->peers == NULL)
456 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
457 tc->peers[pidx].pidx = pidx;
458 tc->peers[pidx].tc = tc;
464 /*==============================================================================
465 * Link state read/write methods
466 *==============================================================================
469 static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
470 size_t size, loff_t *offp)
472 struct tool_ctx *tc = filep->private_data;
476 ret = kstrtobool_from_user(ubuf, size, &val);
481 ret = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
483 ret = ntb_link_disable(tc->ntb);
491 static TOOL_FOPS_RDWR(tool_link_fops,
495 static ssize_t tool_peer_link_read(struct file *filep, char __user *ubuf,
496 size_t size, loff_t *offp)
498 struct tool_peer *peer = filep->private_data;
499 struct tool_ctx *tc = peer->tc;
502 if (ntb_link_is_up(tc->ntb, NULL, NULL) & BIT(peer->pidx))
509 return simple_read_from_buffer(ubuf, size, offp, buf, 2);
512 static TOOL_FOPS_RDWR(tool_peer_link_fops,
516 static ssize_t tool_peer_link_event_write(struct file *filep,
517 const char __user *ubuf,
518 size_t size, loff_t *offp)
520 struct tool_peer *peer = filep->private_data;
521 struct tool_ctx *tc = peer->tc;
526 ret = kstrtobool_from_user(ubuf, size, &val);
530 link_msk = BIT_ULL_MASK(peer->pidx);
532 if (wait_event_interruptible(tc->link_wq,
533 !!(ntb_link_is_up(tc->ntb, NULL, NULL) & link_msk) == val))
539 static TOOL_FOPS_RDWR(tool_peer_link_event_fops,
541 tool_peer_link_event_write);
543 /*==============================================================================
544 * Memory windows read/write/setting methods
545 *==============================================================================
548 static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
549 size_t size, loff_t *offp)
551 struct tool_mw *inmw = filep->private_data;
553 if (inmw->mm_base == NULL)
556 return simple_read_from_buffer(ubuf, size, offp,
557 inmw->mm_base, inmw->size);
560 static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
561 size_t size, loff_t *offp)
563 struct tool_mw *inmw = filep->private_data;
565 if (inmw->mm_base == NULL)
568 return simple_write_to_buffer(inmw->mm_base, inmw->size, offp,
572 static TOOL_FOPS_RDWR(tool_mw_fops,
576 static int tool_setup_mw(struct tool_ctx *tc, int pidx, int widx,
579 resource_size_t size, addr_align, size_align;
580 struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
581 char buf[TOOL_BUF_LEN];
584 if (inmw->mm_base != NULL)
587 ret = ntb_mw_get_align(tc->ntb, pidx, widx, &addr_align,
592 inmw->size = min_t(resource_size_t, req_size, size);
593 inmw->size = round_up(inmw->size, addr_align);
594 inmw->size = round_up(inmw->size, size_align);
595 inmw->mm_base = dma_alloc_coherent(&tc->ntb->pdev->dev, inmw->size,
596 &inmw->dma_base, GFP_KERNEL);
600 if (!IS_ALIGNED(inmw->dma_base, addr_align)) {
605 ret = ntb_mw_set_trans(tc->ntb, pidx, widx, inmw->dma_base, inmw->size);
609 snprintf(buf, sizeof(buf), "mw%d", widx);
610 inmw->dbgfs_file = debugfs_create_file(buf, 0600,
611 tc->peers[pidx].dbgfs_dir, inmw,
617 dma_free_coherent(&tc->ntb->pdev->dev, inmw->size, inmw->mm_base,
619 inmw->mm_base = NULL;
626 static void tool_free_mw(struct tool_ctx *tc, int pidx, int widx)
628 struct tool_mw *inmw = &tc->peers[pidx].inmws[widx];
630 debugfs_remove(inmw->dbgfs_file);
632 if (inmw->mm_base != NULL) {
633 ntb_mw_clear_trans(tc->ntb, pidx, widx);
634 dma_free_coherent(&tc->ntb->pdev->dev, inmw->size,
635 inmw->mm_base, inmw->dma_base);
638 inmw->mm_base = NULL;
641 inmw->dbgfs_file = NULL;
644 static ssize_t tool_mw_trans_read(struct file *filep, char __user *ubuf,
645 size_t size, loff_t *offp)
647 struct tool_mw *inmw = filep->private_data;
648 resource_size_t addr_align;
649 resource_size_t size_align;
650 resource_size_t size_max;
651 ssize_t ret, off = 0;
655 buf_size = min_t(size_t, size, 512);
657 buf = kmalloc(buf_size, GFP_KERNEL);
661 ret = ntb_mw_get_align(inmw->tc->ntb, inmw->pidx, inmw->widx,
662 &addr_align, &size_align, &size_max);
666 off += scnprintf(buf + off, buf_size - off,
670 off += scnprintf(buf + off, buf_size - off,
672 ntb_peer_port_number(inmw->tc->ntb, inmw->pidx),
675 off += scnprintf(buf + off, buf_size - off,
676 "Window Address \t0x%pK\n", inmw->mm_base);
678 off += scnprintf(buf + off, buf_size - off,
679 "DMA Address \t%pad\n",
682 off += scnprintf(buf + off, buf_size - off,
683 "Window Size \t%pap\n",
686 off += scnprintf(buf + off, buf_size - off,
687 "Alignment \t%pap\n",
690 off += scnprintf(buf + off, buf_size - off,
691 "Size Alignment \t%pap\n",
694 off += scnprintf(buf + off, buf_size - off,
698 ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
706 static ssize_t tool_mw_trans_write(struct file *filep, const char __user *ubuf,
707 size_t size, loff_t *offp)
709 struct tool_mw *inmw = filep->private_data;
713 ret = kstrtouint_from_user(ubuf, size, 0, &val);
717 tool_free_mw(inmw->tc, inmw->pidx, inmw->widx);
719 ret = tool_setup_mw(inmw->tc, inmw->pidx, inmw->widx, val);
727 static TOOL_FOPS_RDWR(tool_mw_trans_fops,
729 tool_mw_trans_write);
731 static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
732 size_t size, loff_t *offp)
734 struct tool_mw *outmw = filep->private_data;
739 if (outmw->io_base == NULL)
742 if (pos >= outmw->size || !size)
745 if (size > outmw->size - pos)
746 size = outmw->size - pos;
748 buf = kmalloc(size, GFP_KERNEL);
752 memcpy_fromio(buf, outmw->io_base + pos, size);
753 ret = copy_to_user(ubuf, buf, size);
769 static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
770 size_t size, loff_t *offp)
772 struct tool_mw *outmw = filep->private_data;
777 if (outmw->io_base == NULL)
780 if (pos >= outmw->size || !size)
782 if (size > outmw->size - pos)
783 size = outmw->size - pos;
785 buf = kmalloc(size, GFP_KERNEL);
789 ret = copy_from_user(buf, ubuf, size);
799 memcpy_toio(outmw->io_base + pos, buf, size);
807 static TOOL_FOPS_RDWR(tool_peer_mw_fops,
811 static int tool_setup_peer_mw(struct tool_ctx *tc, int pidx, int widx,
812 u64 req_addr, size_t req_size)
814 struct tool_mw *outmw = &tc->outmws[widx];
815 resource_size_t map_size;
816 phys_addr_t map_base;
817 char buf[TOOL_BUF_LEN];
820 if (outmw->io_base != NULL)
823 ret = ntb_peer_mw_get_addr(tc->ntb, widx, &map_base, &map_size);
827 ret = ntb_peer_mw_set_trans(tc->ntb, pidx, widx, req_addr, req_size);
831 outmw->io_base = ioremap_wc(map_base, map_size);
832 if (outmw->io_base == NULL) {
834 goto err_clear_trans;
837 outmw->tr_base = req_addr;
838 outmw->size = req_size;
841 snprintf(buf, sizeof(buf), "peer_mw%d", widx);
842 outmw->dbgfs_file = debugfs_create_file(buf, 0600,
843 tc->peers[pidx].dbgfs_dir, outmw,
849 ntb_peer_mw_clear_trans(tc->ntb, pidx, widx);
854 static void tool_free_peer_mw(struct tool_ctx *tc, int widx)
856 struct tool_mw *outmw = &tc->outmws[widx];
858 debugfs_remove(outmw->dbgfs_file);
860 if (outmw->io_base != NULL) {
861 iounmap(tc->outmws[widx].io_base);
862 ntb_peer_mw_clear_trans(tc->ntb, outmw->pidx, widx);
865 outmw->io_base = NULL;
869 outmw->dbgfs_file = NULL;
872 static ssize_t tool_peer_mw_trans_read(struct file *filep, char __user *ubuf,
873 size_t size, loff_t *offp)
875 struct tool_mw_wrap *outmw_wrap = filep->private_data;
876 struct tool_mw *outmw = outmw_wrap->mw;
877 resource_size_t map_size;
878 phys_addr_t map_base;
884 ret = ntb_peer_mw_get_addr(outmw->tc->ntb, outmw->widx,
885 &map_base, &map_size);
889 buf_size = min_t(size_t, size, 512);
891 buf = kmalloc(buf_size, GFP_KERNEL);
895 off += scnprintf(buf + off, buf_size - off,
896 "Outbound MW: \t%d\n", outmw->widx);
898 if (outmw->io_base != NULL) {
899 off += scnprintf(buf + off, buf_size - off,
900 "Port attached \t%d (%d)\n",
901 ntb_peer_port_number(outmw->tc->ntb, outmw->pidx),
904 off += scnprintf(buf + off, buf_size - off,
905 "Port attached \t-1 (-1)\n");
908 off += scnprintf(buf + off, buf_size - off,
909 "Virtual address \t0x%pK\n", outmw->io_base);
911 off += scnprintf(buf + off, buf_size - off,
912 "Phys Address \t%pap\n", &map_base);
914 off += scnprintf(buf + off, buf_size - off,
915 "Mapping Size \t%pap\n", &map_size);
917 off += scnprintf(buf + off, buf_size - off,
918 "Translation Address \t0x%016llx\n", outmw->tr_base);
920 off += scnprintf(buf + off, buf_size - off,
921 "Window Size \t%pap\n", &outmw->size);
923 ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
929 static ssize_t tool_peer_mw_trans_write(struct file *filep,
930 const char __user *ubuf,
931 size_t size, loff_t *offp)
933 struct tool_mw_wrap *outmw_wrap = filep->private_data;
934 struct tool_mw *outmw = outmw_wrap->mw;
935 size_t buf_size, wsize;
936 char buf[TOOL_BUF_LEN];
940 buf_size = min(size, (sizeof(buf) - 1));
941 if (copy_from_user(buf, ubuf, buf_size))
944 buf[buf_size] = '\0';
946 n = sscanf(buf, "%lli:%zi", &addr, &wsize);
950 tool_free_peer_mw(outmw->tc, outmw->widx);
952 ret = tool_setup_peer_mw(outmw->tc, outmw_wrap->pidx,
953 outmw->widx, addr, wsize);
961 static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
962 tool_peer_mw_trans_read,
963 tool_peer_mw_trans_write);
965 static int tool_init_mws(struct tool_ctx *tc)
969 /* Initialize outbound memory windows */
970 tc->outmw_cnt = ntb_peer_mw_count(tc->ntb);
971 tc->outmws = devm_kcalloc(&tc->ntb->dev, tc->outmw_cnt,
972 sizeof(*tc->outmws), GFP_KERNEL);
973 if (tc->outmws == NULL)
976 for (widx = 0; widx < tc->outmw_cnt; widx++) {
977 tc->outmws[widx].widx = widx;
978 tc->outmws[widx].pidx = -1;
979 tc->outmws[widx].tc = tc;
982 /* Initialize inbound memory windows and outbound MWs wrapper */
983 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
984 tc->peers[pidx].inmw_cnt = ntb_mw_count(tc->ntb, pidx);
985 tc->peers[pidx].inmws =
986 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].inmw_cnt,
987 sizeof(*tc->peers[pidx].inmws), GFP_KERNEL);
988 if (tc->peers[pidx].inmws == NULL)
991 for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
992 tc->peers[pidx].inmws[widx].widx = widx;
993 tc->peers[pidx].inmws[widx].pidx = pidx;
994 tc->peers[pidx].inmws[widx].tc = tc;
997 tc->peers[pidx].outmw_cnt = ntb_peer_mw_count(tc->ntb);
998 tc->peers[pidx].outmws =
999 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmw_cnt,
1000 sizeof(*tc->peers[pidx].outmws), GFP_KERNEL);
1002 for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
1003 tc->peers[pidx].outmws[widx].pidx = pidx;
1004 tc->peers[pidx].outmws[widx].mw = &tc->outmws[widx];
1011 static void tool_clear_mws(struct tool_ctx *tc)
1015 /* Free outbound memory windows */
1016 for (widx = 0; widx < tc->outmw_cnt; widx++)
1017 tool_free_peer_mw(tc, widx);
1019 /* Free outbound memory windows */
1020 for (pidx = 0; pidx < tc->peer_cnt; pidx++)
1021 for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
1022 tool_free_mw(tc, pidx, widx);
1025 /*==============================================================================
1026 * Doorbell read/write methods
1027 *==============================================================================
1030 static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
1031 size_t size, loff_t *offp)
1033 struct tool_ctx *tc = filep->private_data;
1035 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read);
1038 static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
1039 size_t size, loff_t *offp)
1041 struct tool_ctx *tc = filep->private_data;
1043 return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set,
1044 tc->ntb->ops->db_clear);
1047 static TOOL_FOPS_RDWR(tool_db_fops,
1051 static ssize_t tool_db_valid_mask_read(struct file *filep, char __user *ubuf,
1052 size_t size, loff_t *offp)
1054 struct tool_ctx *tc = filep->private_data;
1056 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_valid_mask);
1059 static TOOL_FOPS_RDWR(tool_db_valid_mask_fops,
1060 tool_db_valid_mask_read,
1063 static ssize_t tool_db_mask_read(struct file *filep, char __user *ubuf,
1064 size_t size, loff_t *offp)
1066 struct tool_ctx *tc = filep->private_data;
1068 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->db_read_mask);
1071 static ssize_t tool_db_mask_write(struct file *filep, const char __user *ubuf,
1072 size_t size, loff_t *offp)
1074 struct tool_ctx *tc = filep->private_data;
1076 return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->db_set_mask,
1077 tc->ntb->ops->db_clear_mask);
1080 static TOOL_FOPS_RDWR(tool_db_mask_fops,
1082 tool_db_mask_write);
1084 static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
1085 size_t size, loff_t *offp)
1087 struct tool_ctx *tc = filep->private_data;
1089 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->peer_db_read);
1092 static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
1093 size_t size, loff_t *offp)
1095 struct tool_ctx *tc = filep->private_data;
1097 return tool_fn_write(tc, ubuf, size, offp, tc->ntb->ops->peer_db_set,
1098 tc->ntb->ops->peer_db_clear);
1101 static TOOL_FOPS_RDWR(tool_peer_db_fops,
1103 tool_peer_db_write);
1105 static ssize_t tool_peer_db_mask_read(struct file *filep, char __user *ubuf,
1106 size_t size, loff_t *offp)
1108 struct tool_ctx *tc = filep->private_data;
1110 return tool_fn_read(tc, ubuf, size, offp,
1111 tc->ntb->ops->peer_db_read_mask);
1114 static ssize_t tool_peer_db_mask_write(struct file *filep,
1115 const char __user *ubuf,
1116 size_t size, loff_t *offp)
1118 struct tool_ctx *tc = filep->private_data;
1120 return tool_fn_write(tc, ubuf, size, offp,
1121 tc->ntb->ops->peer_db_set_mask,
1122 tc->ntb->ops->peer_db_clear_mask);
1125 static TOOL_FOPS_RDWR(tool_peer_db_mask_fops,
1126 tool_peer_db_mask_read,
1127 tool_peer_db_mask_write);
1129 static ssize_t tool_db_event_write(struct file *filep,
1130 const char __user *ubuf,
1131 size_t size, loff_t *offp)
1133 struct tool_ctx *tc = filep->private_data;
1137 ret = kstrtou64_from_user(ubuf, size, 0, &val);
1141 if (wait_event_interruptible(tc->db_wq, ntb_db_read(tc->ntb) == val))
1147 static TOOL_FOPS_RDWR(tool_db_event_fops,
1149 tool_db_event_write);
1151 /*==============================================================================
1152 * Scratchpads read/write methods
1153 *==============================================================================
1156 static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
1157 size_t size, loff_t *offp)
1159 struct tool_spad *spad = filep->private_data;
1160 char buf[TOOL_BUF_LEN];
1163 if (!spad->tc->ntb->ops->spad_read)
1166 pos = scnprintf(buf, sizeof(buf), "%#x\n",
1167 ntb_spad_read(spad->tc->ntb, spad->sidx));
1169 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1172 static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
1173 size_t size, loff_t *offp)
1175 struct tool_spad *spad = filep->private_data;
1179 if (!spad->tc->ntb->ops->spad_write) {
1180 dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
1184 ret = kstrtou32_from_user(ubuf, size, 0, &val);
1188 ret = ntb_spad_write(spad->tc->ntb, spad->sidx, val);
1193 static TOOL_FOPS_RDWR(tool_spad_fops,
1197 static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
1198 size_t size, loff_t *offp)
1200 struct tool_spad *spad = filep->private_data;
1201 char buf[TOOL_BUF_LEN];
1204 if (!spad->tc->ntb->ops->peer_spad_read)
1207 pos = scnprintf(buf, sizeof(buf), "%#x\n",
1208 ntb_peer_spad_read(spad->tc->ntb, spad->pidx, spad->sidx));
1210 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1213 static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
1214 size_t size, loff_t *offp)
1216 struct tool_spad *spad = filep->private_data;
1220 if (!spad->tc->ntb->ops->peer_spad_write) {
1221 dev_dbg(&spad->tc->ntb->dev, "no spad write fn\n");
1225 ret = kstrtou32_from_user(ubuf, size, 0, &val);
1229 ret = ntb_peer_spad_write(spad->tc->ntb, spad->pidx, spad->sidx, val);
1234 static TOOL_FOPS_RDWR(tool_peer_spad_fops,
1235 tool_peer_spad_read,
1236 tool_peer_spad_write);
1238 static int tool_init_spads(struct tool_ctx *tc)
1242 /* Initialize inbound scratchpad structures */
1243 tc->inspad_cnt = ntb_spad_count(tc->ntb);
1244 tc->inspads = devm_kcalloc(&tc->ntb->dev, tc->inspad_cnt,
1245 sizeof(*tc->inspads), GFP_KERNEL);
1246 if (tc->inspads == NULL)
1249 for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
1250 tc->inspads[sidx].sidx = sidx;
1251 tc->inspads[sidx].pidx = -1;
1252 tc->inspads[sidx].tc = tc;
1255 /* Initialize outbound scratchpad structures */
1256 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
1257 tc->peers[pidx].outspad_cnt = ntb_spad_count(tc->ntb);
1258 tc->peers[pidx].outspads =
1259 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outspad_cnt,
1260 sizeof(*tc->peers[pidx].outspads), GFP_KERNEL);
1261 if (tc->peers[pidx].outspads == NULL)
1264 for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
1265 tc->peers[pidx].outspads[sidx].sidx = sidx;
1266 tc->peers[pidx].outspads[sidx].pidx = pidx;
1267 tc->peers[pidx].outspads[sidx].tc = tc;
1274 /*==============================================================================
1275 * Messages read/write methods
1276 *==============================================================================
1279 static ssize_t tool_inmsg_read(struct file *filep, char __user *ubuf,
1280 size_t size, loff_t *offp)
1282 struct tool_msg *msg = filep->private_data;
1283 char buf[TOOL_BUF_LEN];
1288 data = ntb_msg_read(msg->tc->ntb, &pidx, msg->midx);
1290 pos = scnprintf(buf, sizeof(buf), "0x%08x<-%d\n", data, pidx);
1292 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1295 static TOOL_FOPS_RDWR(tool_inmsg_fops,
1299 static ssize_t tool_outmsg_write(struct file *filep,
1300 const char __user *ubuf,
1301 size_t size, loff_t *offp)
1303 struct tool_msg *msg = filep->private_data;
1307 ret = kstrtou32_from_user(ubuf, size, 0, &val);
1311 ret = ntb_peer_msg_write(msg->tc->ntb, msg->pidx, msg->midx, val);
1313 return ret ? : size;
1316 static TOOL_FOPS_RDWR(tool_outmsg_fops,
1320 static ssize_t tool_msg_sts_read(struct file *filep, char __user *ubuf,
1321 size_t size, loff_t *offp)
1323 struct tool_ctx *tc = filep->private_data;
1325 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_read_sts);
1328 static ssize_t tool_msg_sts_write(struct file *filep, const char __user *ubuf,
1329 size_t size, loff_t *offp)
1331 struct tool_ctx *tc = filep->private_data;
1333 return tool_fn_write(tc, ubuf, size, offp, NULL,
1334 tc->ntb->ops->msg_clear_sts);
1337 static TOOL_FOPS_RDWR(tool_msg_sts_fops,
1339 tool_msg_sts_write);
1341 static ssize_t tool_msg_inbits_read(struct file *filep, char __user *ubuf,
1342 size_t size, loff_t *offp)
1344 struct tool_ctx *tc = filep->private_data;
1346 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_inbits);
1349 static TOOL_FOPS_RDWR(tool_msg_inbits_fops,
1350 tool_msg_inbits_read,
1353 static ssize_t tool_msg_outbits_read(struct file *filep, char __user *ubuf,
1354 size_t size, loff_t *offp)
1356 struct tool_ctx *tc = filep->private_data;
1358 return tool_fn_read(tc, ubuf, size, offp, tc->ntb->ops->msg_outbits);
1361 static TOOL_FOPS_RDWR(tool_msg_outbits_fops,
1362 tool_msg_outbits_read,
1365 static ssize_t tool_msg_mask_write(struct file *filep, const char __user *ubuf,
1366 size_t size, loff_t *offp)
1368 struct tool_ctx *tc = filep->private_data;
1370 return tool_fn_write(tc, ubuf, size, offp,
1371 tc->ntb->ops->msg_set_mask,
1372 tc->ntb->ops->msg_clear_mask);
1375 static TOOL_FOPS_RDWR(tool_msg_mask_fops,
1377 tool_msg_mask_write);
1379 static ssize_t tool_msg_event_write(struct file *filep,
1380 const char __user *ubuf,
1381 size_t size, loff_t *offp)
1383 struct tool_ctx *tc = filep->private_data;
1387 ret = kstrtou64_from_user(ubuf, size, 0, &val);
1391 if (wait_event_interruptible(tc->msg_wq,
1392 ntb_msg_read_sts(tc->ntb) == val))
1398 static TOOL_FOPS_RDWR(tool_msg_event_fops,
1400 tool_msg_event_write);
1402 static int tool_init_msgs(struct tool_ctx *tc)
1406 /* Initialize inbound message structures */
1407 tc->inmsg_cnt = ntb_msg_count(tc->ntb);
1408 tc->inmsgs = devm_kcalloc(&tc->ntb->dev, tc->inmsg_cnt,
1409 sizeof(*tc->inmsgs), GFP_KERNEL);
1410 if (tc->inmsgs == NULL)
1413 for (midx = 0; midx < tc->inmsg_cnt; midx++) {
1414 tc->inmsgs[midx].midx = midx;
1415 tc->inmsgs[midx].pidx = -1;
1416 tc->inmsgs[midx].tc = tc;
1419 /* Initialize outbound message structures */
1420 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
1421 tc->peers[pidx].outmsg_cnt = ntb_msg_count(tc->ntb);
1422 tc->peers[pidx].outmsgs =
1423 devm_kcalloc(&tc->ntb->dev, tc->peers[pidx].outmsg_cnt,
1424 sizeof(*tc->peers[pidx].outmsgs), GFP_KERNEL);
1425 if (tc->peers[pidx].outmsgs == NULL)
1428 for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
1429 tc->peers[pidx].outmsgs[midx].midx = midx;
1430 tc->peers[pidx].outmsgs[midx].pidx = pidx;
1431 tc->peers[pidx].outmsgs[midx].tc = tc;
1438 /*==============================================================================
1439 * Initialization methods
1440 *==============================================================================
1443 static struct tool_ctx *tool_create_data(struct ntb_dev *ntb)
1445 struct tool_ctx *tc;
1447 tc = devm_kzalloc(&ntb->dev, sizeof(*tc), GFP_KERNEL);
1449 return ERR_PTR(-ENOMEM);
1452 init_waitqueue_head(&tc->link_wq);
1453 init_waitqueue_head(&tc->db_wq);
1454 init_waitqueue_head(&tc->msg_wq);
1456 if (ntb_db_is_unsafe(ntb))
1457 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
1459 if (ntb_spad_is_unsafe(ntb))
1460 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
1465 static void tool_clear_data(struct tool_ctx *tc)
1467 wake_up(&tc->link_wq);
1468 wake_up(&tc->db_wq);
1469 wake_up(&tc->msg_wq);
1472 static int tool_init_ntb(struct tool_ctx *tc)
1474 return ntb_set_ctx(tc->ntb, tc, &tool_ops);
1477 static void tool_clear_ntb(struct tool_ctx *tc)
1479 ntb_clear_ctx(tc->ntb);
1480 ntb_link_disable(tc->ntb);
1483 static void tool_setup_dbgfs(struct tool_ctx *tc)
1485 int pidx, widx, sidx, midx;
1486 char buf[TOOL_BUF_LEN];
1488 /* This modules is useless without dbgfs... */
1489 if (!tool_dbgfs_topdir) {
1490 tc->dbgfs_dir = NULL;
1494 tc->dbgfs_dir = debugfs_create_dir(dev_name(&tc->ntb->dev),
1499 debugfs_create_file("port", 0600, tc->dbgfs_dir,
1500 tc, &tool_port_fops);
1502 debugfs_create_file("link", 0600, tc->dbgfs_dir,
1503 tc, &tool_link_fops);
1505 debugfs_create_file("db", 0600, tc->dbgfs_dir,
1508 debugfs_create_file("db_valid_mask", 0600, tc->dbgfs_dir,
1509 tc, &tool_db_valid_mask_fops);
1511 debugfs_create_file("db_mask", 0600, tc->dbgfs_dir,
1512 tc, &tool_db_mask_fops);
1514 debugfs_create_file("db_event", 0600, tc->dbgfs_dir,
1515 tc, &tool_db_event_fops);
1517 debugfs_create_file("peer_db", 0600, tc->dbgfs_dir,
1518 tc, &tool_peer_db_fops);
1520 debugfs_create_file("peer_db_mask", 0600, tc->dbgfs_dir,
1521 tc, &tool_peer_db_mask_fops);
1523 if (tc->inspad_cnt != 0) {
1524 for (sidx = 0; sidx < tc->inspad_cnt; sidx++) {
1525 snprintf(buf, sizeof(buf), "spad%d", sidx);
1527 debugfs_create_file(buf, 0600, tc->dbgfs_dir,
1528 &tc->inspads[sidx], &tool_spad_fops);
1532 if (tc->inmsg_cnt != 0) {
1533 for (midx = 0; midx < tc->inmsg_cnt; midx++) {
1534 snprintf(buf, sizeof(buf), "msg%d", midx);
1535 debugfs_create_file(buf, 0600, tc->dbgfs_dir,
1536 &tc->inmsgs[midx], &tool_inmsg_fops);
1539 debugfs_create_file("msg_sts", 0600, tc->dbgfs_dir,
1540 tc, &tool_msg_sts_fops);
1542 debugfs_create_file("msg_inbits", 0600, tc->dbgfs_dir,
1543 tc, &tool_msg_inbits_fops);
1545 debugfs_create_file("msg_outbits", 0600, tc->dbgfs_dir,
1546 tc, &tool_msg_outbits_fops);
1548 debugfs_create_file("msg_mask", 0600, tc->dbgfs_dir,
1549 tc, &tool_msg_mask_fops);
1551 debugfs_create_file("msg_event", 0600, tc->dbgfs_dir,
1552 tc, &tool_msg_event_fops);
1555 for (pidx = 0; pidx < tc->peer_cnt; pidx++) {
1556 snprintf(buf, sizeof(buf), "peer%d", pidx);
1557 tc->peers[pidx].dbgfs_dir =
1558 debugfs_create_dir(buf, tc->dbgfs_dir);
1560 debugfs_create_file("port", 0600,
1561 tc->peers[pidx].dbgfs_dir,
1562 &tc->peers[pidx], &tool_peer_port_fops);
1564 debugfs_create_file("link", 0200,
1565 tc->peers[pidx].dbgfs_dir,
1566 &tc->peers[pidx], &tool_peer_link_fops);
1568 debugfs_create_file("link_event", 0200,
1569 tc->peers[pidx].dbgfs_dir,
1570 &tc->peers[pidx], &tool_peer_link_event_fops);
1572 for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++) {
1573 snprintf(buf, sizeof(buf), "mw_trans%d", widx);
1574 debugfs_create_file(buf, 0600,
1575 tc->peers[pidx].dbgfs_dir,
1576 &tc->peers[pidx].inmws[widx],
1577 &tool_mw_trans_fops);
1580 for (widx = 0; widx < tc->peers[pidx].outmw_cnt; widx++) {
1581 snprintf(buf, sizeof(buf), "peer_mw_trans%d", widx);
1582 debugfs_create_file(buf, 0600,
1583 tc->peers[pidx].dbgfs_dir,
1584 &tc->peers[pidx].outmws[widx],
1585 &tool_peer_mw_trans_fops);
1588 for (sidx = 0; sidx < tc->peers[pidx].outspad_cnt; sidx++) {
1589 snprintf(buf, sizeof(buf), "spad%d", sidx);
1591 debugfs_create_file(buf, 0600,
1592 tc->peers[pidx].dbgfs_dir,
1593 &tc->peers[pidx].outspads[sidx],
1594 &tool_peer_spad_fops);
1597 for (midx = 0; midx < tc->peers[pidx].outmsg_cnt; midx++) {
1598 snprintf(buf, sizeof(buf), "msg%d", midx);
1599 debugfs_create_file(buf, 0600,
1600 tc->peers[pidx].dbgfs_dir,
1601 &tc->peers[pidx].outmsgs[midx],
1607 static void tool_clear_dbgfs(struct tool_ctx *tc)
1609 debugfs_remove_recursive(tc->dbgfs_dir);
1612 static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
1614 struct tool_ctx *tc;
1617 tc = tool_create_data(ntb);
1621 ret = tool_init_peers(tc);
1623 goto err_clear_data;
1625 ret = tool_init_mws(tc);
1627 goto err_clear_data;
1629 ret = tool_init_spads(tc);
1633 ret = tool_init_msgs(tc);
1637 ret = tool_init_ntb(tc);
1641 tool_setup_dbgfs(tc);
1649 tool_clear_data(tc);
1654 static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
1656 struct tool_ctx *tc = ntb->ctx;
1658 tool_clear_dbgfs(tc);
1664 tool_clear_data(tc);
1667 static struct ntb_client tool_client = {
1669 .probe = tool_probe,
1670 .remove = tool_remove,
1674 static int __init tool_init(void)
1678 if (debugfs_initialized())
1679 tool_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1681 ret = ntb_register_client(&tool_client);
1683 debugfs_remove_recursive(tool_dbgfs_topdir);
1687 module_init(tool_init);
1689 static void __exit tool_exit(void)
1691 ntb_unregister_client(&tool_client);
1692 debugfs_remove_recursive(tool_dbgfs_topdir);
1694 module_exit(tool_exit);