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.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
26 * * Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * * Redistributions in binary form must reproduce the above copy
29 * notice, this list of conditions and the following disclaimer in
30 * the documentation and/or other materials provided with the
32 * * Neither the name of Intel Corporation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 * PCIe NTB Debugging Tool Linux driver
50 * Contact Information:
51 * Allen Hubbe <Allen.Hubbe@emc.com>
55 * How to use this tool, by example.
57 * Assuming $DBG_DIR is something like:
58 * '/sys/kernel/debug/ntb_tool/0000:00:03.0'
60 * Eg: check if clearing the doorbell mask generates an interrupt.
62 * # Check the link status
63 * root@self# cat $DBG_DIR/link
65 * # Block until the link is up
66 * root@self# echo Y > $DBG_DIR/link_event
68 * # Set the doorbell mask
69 * root@self# echo 's 1' > $DBG_DIR/mask
71 * # Ring the doorbell from the peer
72 * root@peer# echo 's 1' > $DBG_DIR/peer_db
74 * # Clear the doorbell mask
75 * root@self# echo 'c 1' > $DBG_DIR/mask
77 * Observe debugging output in dmesg or your console. You should see a
78 * doorbell event triggered by clearing the mask. If not, this may indicate an
79 * issue with the hardware that needs to be worked around in the driver.
81 * Eg: read and write scratchpad registers
83 * root@peer# echo '0 0x01010101 1 0x7f7f7f7f' > $DBG_DIR/peer_spad
85 * root@self# cat $DBG_DIR/spad
87 * Observe that spad 0 and 1 have the values set by the peer.
89 * # Check the memory window translation info
90 * cat $DBG_DIR/peer_trans0
92 * # Setup a 16k memory window buffer
93 * echo 16384 > $DBG_DIR/peer_trans0
97 #include <linux/init.h>
98 #include <linux/kernel.h>
99 #include <linux/module.h>
101 #include <linux/debugfs.h>
102 #include <linux/dma-mapping.h>
103 #include <linux/pci.h>
104 #include <linux/slab.h>
105 #include <linux/uaccess.h>
107 #include <linux/ntb.h>
109 #define DRIVER_NAME "ntb_tool"
110 #define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
112 #define DRIVER_LICENSE "Dual BSD/GPL"
113 #define DRIVER_VERSION "1.0"
114 #define DRIVER_RELDATE "22 April 2015"
115 #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
117 MODULE_LICENSE(DRIVER_LICENSE
);
118 MODULE_VERSION(DRIVER_VERSION
);
119 MODULE_AUTHOR(DRIVER_AUTHOR
);
120 MODULE_DESCRIPTION(DRIVER_DESCRIPTION
);
122 /* It is rare to have hadrware with greater than six MWs */
124 /* Only two-ports devices are supported */
125 #define PIDX NTB_DEF_PEER_IDX
127 static struct dentry
*tool_dbgfs
;
132 resource_size_t win_size
;
133 resource_size_t size
;
137 struct dentry
*peer_dbg_file
;
142 struct dentry
*dbgfs
;
143 wait_queue_head_t link_wq
;
145 struct tool_mw mws
[MAX_MWS
];
148 #define SPAD_FNAME_SIZE 0x10
149 #define INT_PTR(x) ((void *)(unsigned long)x)
150 #define PTR_INT(x) ((int)(unsigned long)x)
152 #define TOOL_FOPS_RDWR(__name, __read, __write) \
153 const struct file_operations __name = { \
154 .owner = THIS_MODULE, \
155 .open = simple_open, \
160 static void tool_link_event(void *ctx
)
162 struct tool_ctx
*tc
= ctx
;
163 enum ntb_speed speed
;
164 enum ntb_width width
;
167 up
= ntb_link_is_up(tc
->ntb
, &speed
, &width
);
169 dev_dbg(&tc
->ntb
->dev
, "link is %s speed %d width %d\n",
170 up
? "up" : "down", speed
, width
);
172 wake_up(&tc
->link_wq
);
175 static void tool_db_event(void *ctx
, int vec
)
177 struct tool_ctx
*tc
= ctx
;
178 u64 db_bits
, db_mask
;
180 db_mask
= ntb_db_vector_mask(tc
->ntb
, vec
);
181 db_bits
= ntb_db_read(tc
->ntb
);
183 dev_dbg(&tc
->ntb
->dev
, "doorbell vec %d mask %#llx bits %#llx\n",
184 vec
, db_mask
, db_bits
);
187 static const struct ntb_ctx_ops tool_ops
= {
188 .link_event
= tool_link_event
,
189 .db_event
= tool_db_event
,
192 static ssize_t
tool_dbfn_read(struct tool_ctx
*tc
, char __user
*ubuf
,
193 size_t size
, loff_t
*offp
,
194 u64 (*db_read_fn
)(struct ntb_dev
*))
203 buf_size
= min_t(size_t, size
, 0x20);
205 buf
= kmalloc(buf_size
, GFP_KERNEL
);
209 pos
= scnprintf(buf
, buf_size
, "%#llx\n",
210 db_read_fn(tc
->ntb
));
212 rc
= simple_read_from_buffer(ubuf
, size
, offp
, buf
, pos
);
219 static ssize_t
tool_dbfn_write(struct tool_ctx
*tc
,
220 const char __user
*ubuf
,
221 size_t size
, loff_t
*offp
,
222 int (*db_set_fn
)(struct ntb_dev
*, u64
),
223 int (*db_clear_fn
)(struct ntb_dev
*, u64
))
230 buf
= kmalloc(size
+ 1, GFP_KERNEL
);
234 rc
= simple_write_to_buffer(buf
, size
, offp
, ubuf
, size
);
242 n
= sscanf(buf
, "%c %lli", &cmd
, &db_bits
);
248 } else if (cmd
== 's') {
252 rc
= db_set_fn(tc
->ntb
, db_bits
);
253 } else if (cmd
== 'c') {
257 rc
= db_clear_fn(tc
->ntb
, db_bits
);
265 static ssize_t
tool_spadfn_read(struct tool_ctx
*tc
, char __user
*ubuf
,
266 size_t size
, loff_t
*offp
,
267 u32 (*spad_read_fn
)(struct ntb_dev
*, int))
277 spad_count
= ntb_spad_count(tc
->ntb
);
280 * We multiply the number of spads by 15 to get the buffer size
281 * this is from 3 for the %d, 10 for the largest hex value
282 * (0x00000000) and 2 for the tab and line feed.
284 buf_size
= min_t(size_t, size
, spad_count
* 15);
286 buf
= kmalloc(buf_size
, GFP_KERNEL
);
292 for (i
= 0; i
< spad_count
; ++i
) {
293 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
, "%d\t%#x\n",
294 i
, spad_read_fn(tc
->ntb
, i
));
297 rc
= simple_read_from_buffer(ubuf
, size
, offp
, buf
, pos
);
304 static ssize_t
tool_spadfn_write(struct tool_ctx
*tc
,
305 const char __user
*ubuf
,
306 size_t size
, loff_t
*offp
,
307 int (*spad_write_fn
)(struct ntb_dev
*,
316 if (!spad_write_fn
) {
317 dev_dbg(&tc
->ntb
->dev
, "no spad write fn\n");
321 buf
= kmalloc(size
+ 1, GFP_KERNEL
);
325 rc
= simple_write_to_buffer(buf
, size
, offp
, ubuf
, size
);
333 n
= sscanf(buf_ptr
, "%d %i%n", &spad_idx
, &spad_val
, &pos
);
336 rc
= spad_write_fn(tc
->ntb
, spad_idx
, spad_val
);
340 n
= sscanf(buf_ptr
, "%d %i%n", &spad_idx
, &spad_val
, &pos
);
351 static ssize_t
tool_db_read(struct file
*filep
, char __user
*ubuf
,
352 size_t size
, loff_t
*offp
)
354 struct tool_ctx
*tc
= filep
->private_data
;
356 return tool_dbfn_read(tc
, ubuf
, size
, offp
,
357 tc
->ntb
->ops
->db_read
);
360 static ssize_t
tool_db_write(struct file
*filep
, const char __user
*ubuf
,
361 size_t size
, loff_t
*offp
)
363 struct tool_ctx
*tc
= filep
->private_data
;
365 return tool_dbfn_write(tc
, ubuf
, size
, offp
,
366 tc
->ntb
->ops
->db_set
,
367 tc
->ntb
->ops
->db_clear
);
370 static TOOL_FOPS_RDWR(tool_db_fops
,
374 static ssize_t
tool_mask_read(struct file
*filep
, char __user
*ubuf
,
375 size_t size
, loff_t
*offp
)
377 struct tool_ctx
*tc
= filep
->private_data
;
379 return tool_dbfn_read(tc
, ubuf
, size
, offp
,
380 tc
->ntb
->ops
->db_read_mask
);
383 static ssize_t
tool_mask_write(struct file
*filep
, const char __user
*ubuf
,
384 size_t size
, loff_t
*offp
)
386 struct tool_ctx
*tc
= filep
->private_data
;
388 return tool_dbfn_write(tc
, ubuf
, size
, offp
,
389 tc
->ntb
->ops
->db_set_mask
,
390 tc
->ntb
->ops
->db_clear_mask
);
393 static TOOL_FOPS_RDWR(tool_mask_fops
,
397 static ssize_t
tool_peer_db_read(struct file
*filep
, char __user
*ubuf
,
398 size_t size
, loff_t
*offp
)
400 struct tool_ctx
*tc
= filep
->private_data
;
402 return tool_dbfn_read(tc
, ubuf
, size
, offp
,
403 tc
->ntb
->ops
->peer_db_read
);
406 static ssize_t
tool_peer_db_write(struct file
*filep
, const char __user
*ubuf
,
407 size_t size
, loff_t
*offp
)
409 struct tool_ctx
*tc
= filep
->private_data
;
411 return tool_dbfn_write(tc
, ubuf
, size
, offp
,
412 tc
->ntb
->ops
->peer_db_set
,
413 tc
->ntb
->ops
->peer_db_clear
);
416 static TOOL_FOPS_RDWR(tool_peer_db_fops
,
420 static ssize_t
tool_peer_mask_read(struct file
*filep
, char __user
*ubuf
,
421 size_t size
, loff_t
*offp
)
423 struct tool_ctx
*tc
= filep
->private_data
;
425 return tool_dbfn_read(tc
, ubuf
, size
, offp
,
426 tc
->ntb
->ops
->peer_db_read_mask
);
429 static ssize_t
tool_peer_mask_write(struct file
*filep
, const char __user
*ubuf
,
430 size_t size
, loff_t
*offp
)
432 struct tool_ctx
*tc
= filep
->private_data
;
434 return tool_dbfn_write(tc
, ubuf
, size
, offp
,
435 tc
->ntb
->ops
->peer_db_set_mask
,
436 tc
->ntb
->ops
->peer_db_clear_mask
);
439 static TOOL_FOPS_RDWR(tool_peer_mask_fops
,
441 tool_peer_mask_write
);
443 static ssize_t
tool_spad_read(struct file
*filep
, char __user
*ubuf
,
444 size_t size
, loff_t
*offp
)
446 struct tool_ctx
*tc
= filep
->private_data
;
448 return tool_spadfn_read(tc
, ubuf
, size
, offp
,
449 tc
->ntb
->ops
->spad_read
);
452 static ssize_t
tool_spad_write(struct file
*filep
, const char __user
*ubuf
,
453 size_t size
, loff_t
*offp
)
455 struct tool_ctx
*tc
= filep
->private_data
;
457 return tool_spadfn_write(tc
, ubuf
, size
, offp
,
458 tc
->ntb
->ops
->spad_write
);
461 static TOOL_FOPS_RDWR(tool_spad_fops
,
465 static u32
ntb_tool_peer_spad_read(struct ntb_dev
*ntb
, int sidx
)
467 return ntb_peer_spad_read(ntb
, PIDX
, sidx
);
470 static ssize_t
tool_peer_spad_read(struct file
*filep
, char __user
*ubuf
,
471 size_t size
, loff_t
*offp
)
473 struct tool_ctx
*tc
= filep
->private_data
;
475 return tool_spadfn_read(tc
, ubuf
, size
, offp
, ntb_tool_peer_spad_read
);
478 static int ntb_tool_peer_spad_write(struct ntb_dev
*ntb
, int sidx
, u32 val
)
480 return ntb_peer_spad_write(ntb
, PIDX
, sidx
, val
);
483 static ssize_t
tool_peer_spad_write(struct file
*filep
, const char __user
*ubuf
,
484 size_t size
, loff_t
*offp
)
486 struct tool_ctx
*tc
= filep
->private_data
;
488 return tool_spadfn_write(tc
, ubuf
, size
, offp
,
489 ntb_tool_peer_spad_write
);
492 static TOOL_FOPS_RDWR(tool_peer_spad_fops
,
494 tool_peer_spad_write
);
496 static ssize_t
tool_link_read(struct file
*filep
, char __user
*ubuf
,
497 size_t size
, loff_t
*offp
)
499 struct tool_ctx
*tc
= filep
->private_data
;
502 buf
[0] = ntb_link_is_up(tc
->ntb
, NULL
, NULL
) ? 'Y' : 'N';
506 return simple_read_from_buffer(ubuf
, size
, offp
, buf
, 2);
509 static ssize_t
tool_link_write(struct file
*filep
, const char __user
*ubuf
,
510 size_t size
, loff_t
*offp
)
512 struct tool_ctx
*tc
= filep
->private_data
;
518 buf_size
= min(size
, (sizeof(buf
) - 1));
519 if (copy_from_user(buf
, ubuf
, buf_size
))
522 buf
[buf_size
] = '\0';
524 rc
= strtobool(buf
, &val
);
529 rc
= ntb_link_enable(tc
->ntb
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
531 rc
= ntb_link_disable(tc
->ntb
);
539 static TOOL_FOPS_RDWR(tool_link_fops
,
543 static ssize_t
tool_link_event_write(struct file
*filep
,
544 const char __user
*ubuf
,
545 size_t size
, loff_t
*offp
)
547 struct tool_ctx
*tc
= filep
->private_data
;
553 buf_size
= min(size
, (sizeof(buf
) - 1));
554 if (copy_from_user(buf
, ubuf
, buf_size
))
557 buf
[buf_size
] = '\0';
559 rc
= strtobool(buf
, &val
);
563 if (wait_event_interruptible(tc
->link_wq
,
564 ntb_link_is_up(tc
->ntb
, NULL
, NULL
) == val
))
570 static TOOL_FOPS_RDWR(tool_link_event_fops
,
572 tool_link_event_write
);
574 static ssize_t
tool_mw_read(struct file
*filep
, char __user
*ubuf
,
575 size_t size
, loff_t
*offp
)
577 struct tool_mw
*mw
= filep
->private_data
;
582 if (mw
->local
== NULL
)
586 if (pos
>= mw
->win_size
|| !size
)
588 if (size
> mw
->win_size
- pos
)
589 size
= mw
->win_size
- pos
;
591 buf
= kmalloc(size
, GFP_KERNEL
);
595 memcpy_fromio(buf
, mw
->local
+ pos
, size
);
596 rc
= copy_to_user(ubuf
, buf
, size
);
612 static ssize_t
tool_mw_write(struct file
*filep
, const char __user
*ubuf
,
613 size_t size
, loff_t
*offp
)
615 struct tool_mw
*mw
= filep
->private_data
;
622 if (pos
>= mw
->win_size
|| !size
)
624 if (size
> mw
->win_size
- pos
)
625 size
= mw
->win_size
- pos
;
627 buf
= kmalloc(size
, GFP_KERNEL
);
631 rc
= copy_from_user(buf
, ubuf
, size
);
641 memcpy_toio(mw
->local
+ pos
, buf
, size
);
649 static TOOL_FOPS_RDWR(tool_mw_fops
,
653 static ssize_t
tool_peer_mw_read(struct file
*filep
, char __user
*ubuf
,
654 size_t size
, loff_t
*offp
)
656 struct tool_mw
*mw
= filep
->private_data
;
661 return simple_read_from_buffer(ubuf
, size
, offp
, mw
->peer
, mw
->size
);
664 static ssize_t
tool_peer_mw_write(struct file
*filep
, const char __user
*ubuf
,
665 size_t size
, loff_t
*offp
)
667 struct tool_mw
*mw
= filep
->private_data
;
672 return simple_write_to_buffer(mw
->peer
, mw
->size
, offp
, ubuf
, size
);
675 static TOOL_FOPS_RDWR(tool_peer_mw_fops
,
679 static int tool_setup_mw(struct tool_ctx
*tc
, int idx
, size_t req_size
)
682 struct tool_mw
*mw
= &tc
->mws
[idx
];
683 resource_size_t size
, align_addr
, align_size
;
689 rc
= ntb_mw_get_align(tc
->ntb
, PIDX
, idx
, &align_addr
,
694 mw
->size
= min_t(resource_size_t
, req_size
, size
);
695 mw
->size
= round_up(mw
->size
, align_addr
);
696 mw
->size
= round_up(mw
->size
, align_size
);
697 mw
->peer
= dma_alloc_coherent(&tc
->ntb
->pdev
->dev
, mw
->size
,
698 &mw
->peer_dma
, GFP_KERNEL
);
700 if (!mw
->peer
|| !IS_ALIGNED(mw
->peer_dma
, align_addr
))
703 rc
= ntb_mw_set_trans(tc
->ntb
, PIDX
, idx
, mw
->peer_dma
, mw
->size
);
707 snprintf(buf
, sizeof(buf
), "peer_mw%d", idx
);
708 mw
->peer_dbg_file
= debugfs_create_file(buf
, S_IRUSR
| S_IWUSR
,
715 dma_free_coherent(&tc
->ntb
->pdev
->dev
, mw
->size
,
725 static void tool_free_mw(struct tool_ctx
*tc
, int idx
)
727 struct tool_mw
*mw
= &tc
->mws
[idx
];
730 ntb_mw_clear_trans(tc
->ntb
, PIDX
, idx
);
731 dma_free_coherent(&tc
->ntb
->pdev
->dev
, mw
->size
,
739 debugfs_remove(mw
->peer_dbg_file
);
741 mw
->peer_dbg_file
= NULL
;
744 static ssize_t
tool_peer_mw_trans_read(struct file
*filep
,
746 size_t size
, loff_t
*offp
)
748 struct tool_mw
*mw
= filep
->private_data
;
752 ssize_t ret
, off
= 0;
755 resource_size_t mw_size
;
756 resource_size_t align_addr
= 0;
757 resource_size_t align_size
= 0;
758 resource_size_t max_size
= 0;
760 buf_size
= min_t(size_t, size
, 512);
762 buf
= kmalloc(buf_size
, GFP_KERNEL
);
766 ntb_mw_get_align(mw
->tc
->ntb
, PIDX
, mw
->idx
,
767 &align_addr
, &align_size
, &max_size
);
768 ntb_peer_mw_get_addr(mw
->tc
->ntb
, mw
->idx
, &base
, &mw_size
);
770 off
+= scnprintf(buf
+ off
, buf_size
- off
,
771 "Peer MW %d Information:\n", mw
->idx
);
773 off
+= scnprintf(buf
+ off
, buf_size
- off
,
774 "Physical Address \t%pa[p]\n",
777 off
+= scnprintf(buf
+ off
, buf_size
- off
,
778 "Window Size \t%lld\n",
779 (unsigned long long)mw_size
);
781 off
+= scnprintf(buf
+ off
, buf_size
- off
,
782 "Alignment \t%lld\n",
783 (unsigned long long)align_addr
);
785 off
+= scnprintf(buf
+ off
, buf_size
- off
,
786 "Size Alignment \t%lld\n",
787 (unsigned long long)align_size
);
789 off
+= scnprintf(buf
+ off
, buf_size
- off
,
791 (unsigned long long)max_size
);
793 off
+= scnprintf(buf
+ off
, buf_size
- off
,
795 (mw
->peer
) ? 'Y' : 'N');
797 off
+= scnprintf(buf
+ off
, buf_size
- off
,
798 "Allocated Size \t%zd\n",
799 (mw
->peer
) ? (size_t)mw
->size
: 0);
801 ret
= simple_read_from_buffer(ubuf
, size
, offp
, buf
, off
);
806 static ssize_t
tool_peer_mw_trans_write(struct file
*filep
,
807 const char __user
*ubuf
,
808 size_t size
, loff_t
*offp
)
810 struct tool_mw
*mw
= filep
->private_data
;
814 unsigned long long val
;
817 buf_size
= min(size
, (sizeof(buf
) - 1));
818 if (copy_from_user(buf
, ubuf
, buf_size
))
821 buf
[buf_size
] = '\0';
823 rc
= kstrtoull(buf
, 0, &val
);
827 tool_free_mw(mw
->tc
, mw
->idx
);
829 rc
= tool_setup_mw(mw
->tc
, mw
->idx
, val
);
837 static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops
,
838 tool_peer_mw_trans_read
,
839 tool_peer_mw_trans_write
);
841 static int tool_init_mw(struct tool_ctx
*tc
, int idx
)
843 struct tool_mw
*mw
= &tc
->mws
[idx
];
847 rc
= ntb_peer_mw_get_addr(tc
->ntb
, idx
, &base
, &mw
->win_size
);
853 mw
->local
= ioremap_wc(base
, mw
->win_size
);
860 static void tool_free_mws(struct tool_ctx
*tc
)
864 for (i
= 0; i
< tc
->mw_count
; i
++) {
867 if (tc
->mws
[i
].local
)
868 iounmap(tc
->mws
[i
].local
);
870 tc
->mws
[i
].local
= NULL
;
874 static void tool_setup_dbgfs(struct tool_ctx
*tc
)
878 /* This modules is useless without dbgfs... */
884 tc
->dbgfs
= debugfs_create_dir(dev_name(&tc
->ntb
->dev
),
889 debugfs_create_file("db", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
892 debugfs_create_file("mask", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
893 tc
, &tool_mask_fops
);
895 debugfs_create_file("peer_db", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
896 tc
, &tool_peer_db_fops
);
898 debugfs_create_file("peer_mask", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
899 tc
, &tool_peer_mask_fops
);
901 debugfs_create_file("spad", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
902 tc
, &tool_spad_fops
);
904 debugfs_create_file("peer_spad", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
905 tc
, &tool_peer_spad_fops
);
907 debugfs_create_file("link", S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
908 tc
, &tool_link_fops
);
910 debugfs_create_file("link_event", S_IWUSR
, tc
->dbgfs
,
911 tc
, &tool_link_event_fops
);
913 for (i
= 0; i
< tc
->mw_count
; i
++) {
916 snprintf(buf
, sizeof(buf
), "mw%d", i
);
917 debugfs_create_file(buf
, S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
918 &tc
->mws
[i
], &tool_mw_fops
);
920 snprintf(buf
, sizeof(buf
), "peer_trans%d", i
);
921 debugfs_create_file(buf
, S_IRUSR
| S_IWUSR
, tc
->dbgfs
,
922 &tc
->mws
[i
], &tool_peer_mw_trans_fops
);
926 static int tool_probe(struct ntb_client
*self
, struct ntb_dev
*ntb
)
932 if (!ntb
->ops
->mw_set_trans
) {
933 dev_dbg(&ntb
->dev
, "need inbound MW based NTB API\n");
938 if (ntb_spad_count(ntb
) < 1) {
939 dev_dbg(&ntb
->dev
, "no enough scratchpads\n");
944 if (ntb_db_is_unsafe(ntb
))
945 dev_dbg(&ntb
->dev
, "doorbell is unsafe\n");
947 if (ntb_spad_is_unsafe(ntb
))
948 dev_dbg(&ntb
->dev
, "scratchpad is unsafe\n");
950 if (ntb_peer_port_count(ntb
) != NTB_DEF_PEER_CNT
)
951 dev_warn(&ntb
->dev
, "multi-port NTB is unsupported\n");
953 tc
= kzalloc(sizeof(*tc
), GFP_KERNEL
);
960 init_waitqueue_head(&tc
->link_wq
);
962 tc
->mw_count
= min(ntb_peer_mw_count(tc
->ntb
), MAX_MWS
);
963 for (i
= 0; i
< tc
->mw_count
; i
++) {
964 rc
= tool_init_mw(tc
, i
);
969 tool_setup_dbgfs(tc
);
971 rc
= ntb_set_ctx(ntb
, tc
, &tool_ops
);
975 ntb_link_enable(ntb
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
982 debugfs_remove_recursive(tc
->dbgfs
);
988 static void tool_remove(struct ntb_client
*self
, struct ntb_dev
*ntb
)
990 struct tool_ctx
*tc
= ntb
->ctx
;
995 ntb_link_disable(ntb
);
997 debugfs_remove_recursive(tc
->dbgfs
);
1001 static struct ntb_client tool_client
= {
1003 .probe
= tool_probe
,
1004 .remove
= tool_remove
,
1008 static int __init
tool_init(void)
1012 if (debugfs_initialized())
1013 tool_dbgfs
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
1015 rc
= ntb_register_client(&tool_client
);
1022 debugfs_remove_recursive(tool_dbgfs
);
1025 module_init(tool_init
);
1027 static void __exit
tool_exit(void)
1029 ntb_unregister_client(&tool_client
);
1030 debugfs_remove_recursive(tool_dbgfs
);
1032 module_exit(tool_exit
);