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);
124 static struct dentry *tool_dbgfs;
129 resource_size_t win_size;
130 resource_size_t size;
134 struct dentry *peer_dbg_file;
139 struct dentry *dbgfs;
140 wait_queue_head_t link_wq;
142 struct tool_mw mws[MAX_MWS];
145 #define SPAD_FNAME_SIZE 0x10
146 #define INT_PTR(x) ((void *)(unsigned long)x)
147 #define PTR_INT(x) ((int)(unsigned long)x)
149 #define TOOL_FOPS_RDWR(__name, __read, __write) \
150 const struct file_operations __name = { \
151 .owner = THIS_MODULE, \
152 .open = simple_open, \
157 static void tool_link_event(void *ctx)
159 struct tool_ctx *tc = ctx;
160 enum ntb_speed speed;
161 enum ntb_width width;
164 up = ntb_link_is_up(tc->ntb, &speed, &width);
166 dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
167 up ? "up" : "down", speed, width);
169 wake_up(&tc->link_wq);
172 static void tool_db_event(void *ctx, int vec)
174 struct tool_ctx *tc = ctx;
175 u64 db_bits, db_mask;
177 db_mask = ntb_db_vector_mask(tc->ntb, vec);
178 db_bits = ntb_db_read(tc->ntb);
180 dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
181 vec, db_mask, db_bits);
184 static const struct ntb_ctx_ops tool_ops = {
185 .link_event = tool_link_event,
186 .db_event = tool_db_event,
189 static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf,
190 size_t size, loff_t *offp,
191 u64 (*db_read_fn)(struct ntb_dev *))
200 buf_size = min_t(size_t, size, 0x20);
202 buf = kmalloc(buf_size, GFP_KERNEL);
206 pos = scnprintf(buf, buf_size, "%#llx\n",
207 db_read_fn(tc->ntb));
209 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
216 static ssize_t tool_dbfn_write(struct tool_ctx *tc,
217 const char __user *ubuf,
218 size_t size, loff_t *offp,
219 int (*db_set_fn)(struct ntb_dev *, u64),
220 int (*db_clear_fn)(struct ntb_dev *, u64))
227 buf = kmalloc(size + 1, GFP_KERNEL);
231 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
239 n = sscanf(buf, "%c %lli", &cmd, &db_bits);
245 } else if (cmd == 's') {
249 rc = db_set_fn(tc->ntb, db_bits);
250 } else if (cmd == 'c') {
254 rc = db_clear_fn(tc->ntb, db_bits);
262 static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf,
263 size_t size, loff_t *offp,
264 u32 (*spad_read_fn)(struct ntb_dev *, int))
274 spad_count = ntb_spad_count(tc->ntb);
277 * We multiply the number of spads by 15 to get the buffer size
278 * this is from 3 for the %d, 10 for the largest hex value
279 * (0x00000000) and 2 for the tab and line feed.
281 buf_size = min_t(size_t, size, spad_count * 15);
283 buf = kmalloc(buf_size, GFP_KERNEL);
289 for (i = 0; i < spad_count; ++i) {
290 pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
291 i, spad_read_fn(tc->ntb, i));
294 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
301 static ssize_t tool_spadfn_write(struct tool_ctx *tc,
302 const char __user *ubuf,
303 size_t size, loff_t *offp,
304 int (*spad_write_fn)(struct ntb_dev *,
313 if (!spad_write_fn) {
314 dev_dbg(&tc->ntb->dev, "no spad write fn\n");
318 buf = kmalloc(size + 1, GFP_KERNEL);
322 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
330 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
333 rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
337 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
348 static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
349 size_t size, loff_t *offp)
351 struct tool_ctx *tc = filep->private_data;
353 return tool_dbfn_read(tc, ubuf, size, offp,
354 tc->ntb->ops->db_read);
357 static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
358 size_t size, loff_t *offp)
360 struct tool_ctx *tc = filep->private_data;
362 return tool_dbfn_write(tc, ubuf, size, offp,
363 tc->ntb->ops->db_set,
364 tc->ntb->ops->db_clear);
367 static TOOL_FOPS_RDWR(tool_db_fops,
371 static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
372 size_t size, loff_t *offp)
374 struct tool_ctx *tc = filep->private_data;
376 return tool_dbfn_read(tc, ubuf, size, offp,
377 tc->ntb->ops->db_read_mask);
380 static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
381 size_t size, loff_t *offp)
383 struct tool_ctx *tc = filep->private_data;
385 return tool_dbfn_write(tc, ubuf, size, offp,
386 tc->ntb->ops->db_set_mask,
387 tc->ntb->ops->db_clear_mask);
390 static TOOL_FOPS_RDWR(tool_mask_fops,
394 static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
395 size_t size, loff_t *offp)
397 struct tool_ctx *tc = filep->private_data;
399 return tool_dbfn_read(tc, ubuf, size, offp,
400 tc->ntb->ops->peer_db_read);
403 static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
404 size_t size, loff_t *offp)
406 struct tool_ctx *tc = filep->private_data;
408 return tool_dbfn_write(tc, ubuf, size, offp,
409 tc->ntb->ops->peer_db_set,
410 tc->ntb->ops->peer_db_clear);
413 static TOOL_FOPS_RDWR(tool_peer_db_fops,
417 static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
418 size_t size, loff_t *offp)
420 struct tool_ctx *tc = filep->private_data;
422 return tool_dbfn_read(tc, ubuf, size, offp,
423 tc->ntb->ops->peer_db_read_mask);
426 static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
427 size_t size, loff_t *offp)
429 struct tool_ctx *tc = filep->private_data;
431 return tool_dbfn_write(tc, ubuf, size, offp,
432 tc->ntb->ops->peer_db_set_mask,
433 tc->ntb->ops->peer_db_clear_mask);
436 static TOOL_FOPS_RDWR(tool_peer_mask_fops,
438 tool_peer_mask_write);
440 static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
441 size_t size, loff_t *offp)
443 struct tool_ctx *tc = filep->private_data;
445 return tool_spadfn_read(tc, ubuf, size, offp,
446 tc->ntb->ops->spad_read);
449 static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
450 size_t size, loff_t *offp)
452 struct tool_ctx *tc = filep->private_data;
454 return tool_spadfn_write(tc, ubuf, size, offp,
455 tc->ntb->ops->spad_write);
458 static TOOL_FOPS_RDWR(tool_spad_fops,
462 static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
463 size_t size, loff_t *offp)
465 struct tool_ctx *tc = filep->private_data;
467 return tool_spadfn_read(tc, ubuf, size, offp,
468 tc->ntb->ops->peer_spad_read);
471 static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
472 size_t size, loff_t *offp)
474 struct tool_ctx *tc = filep->private_data;
476 return tool_spadfn_write(tc, ubuf, size, offp,
477 tc->ntb->ops->peer_spad_write);
480 static TOOL_FOPS_RDWR(tool_peer_spad_fops,
482 tool_peer_spad_write);
484 static ssize_t tool_link_read(struct file *filep, char __user *ubuf,
485 size_t size, loff_t *offp)
487 struct tool_ctx *tc = filep->private_data;
490 buf[0] = ntb_link_is_up(tc->ntb, NULL, NULL) ? 'Y' : 'N';
494 return simple_read_from_buffer(ubuf, size, offp, buf, 2);
497 static ssize_t tool_link_write(struct file *filep, const char __user *ubuf,
498 size_t size, loff_t *offp)
500 struct tool_ctx *tc = filep->private_data;
506 buf_size = min(size, (sizeof(buf) - 1));
507 if (copy_from_user(buf, ubuf, buf_size))
510 buf[buf_size] = '\0';
512 rc = strtobool(buf, &val);
517 rc = ntb_link_enable(tc->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
519 rc = ntb_link_disable(tc->ntb);
527 static TOOL_FOPS_RDWR(tool_link_fops,
531 static ssize_t tool_link_event_write(struct file *filep,
532 const char __user *ubuf,
533 size_t size, loff_t *offp)
535 struct tool_ctx *tc = filep->private_data;
541 buf_size = min(size, (sizeof(buf) - 1));
542 if (copy_from_user(buf, ubuf, buf_size))
545 buf[buf_size] = '\0';
547 rc = strtobool(buf, &val);
551 if (wait_event_interruptible(tc->link_wq,
552 ntb_link_is_up(tc->ntb, NULL, NULL) == val))
558 static TOOL_FOPS_RDWR(tool_link_event_fops,
560 tool_link_event_write);
562 static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
563 size_t size, loff_t *offp)
565 struct tool_mw *mw = filep->private_data;
570 if (mw->local == NULL)
574 if (pos >= mw->win_size || !size)
576 if (size > mw->win_size - pos)
577 size = mw->win_size - pos;
579 buf = kmalloc(size, GFP_KERNEL);
583 memcpy_fromio(buf, mw->local + pos, size);
584 rc = copy_to_user(ubuf, buf, size);
600 static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
601 size_t size, loff_t *offp)
603 struct tool_mw *mw = filep->private_data;
610 if (pos >= mw->win_size || !size)
612 if (size > mw->win_size - pos)
613 size = mw->win_size - pos;
615 buf = kmalloc(size, GFP_KERNEL);
619 rc = copy_from_user(buf, ubuf, size);
629 memcpy_toio(mw->local + pos, buf, size);
637 static TOOL_FOPS_RDWR(tool_mw_fops,
641 static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
642 size_t size, loff_t *offp)
644 struct tool_mw *mw = filep->private_data;
649 return simple_read_from_buffer(ubuf, size, offp, mw->peer, mw->size);
652 static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
653 size_t size, loff_t *offp)
655 struct tool_mw *mw = filep->private_data;
660 return simple_write_to_buffer(mw->peer, mw->size, offp, ubuf, size);
663 static TOOL_FOPS_RDWR(tool_peer_mw_fops,
667 static int tool_setup_mw(struct tool_ctx *tc, int idx, size_t req_size)
670 struct tool_mw *mw = &tc->mws[idx];
672 resource_size_t size, align, align_size;
678 rc = ntb_mw_get_range(tc->ntb, idx, &base, &size, &align,
683 mw->size = min_t(resource_size_t, req_size, size);
684 mw->size = round_up(mw->size, align);
685 mw->size = round_up(mw->size, align_size);
686 mw->peer = dma_alloc_coherent(&tc->ntb->pdev->dev, mw->size,
687 &mw->peer_dma, GFP_KERNEL);
692 rc = ntb_mw_set_trans(tc->ntb, idx, mw->peer_dma, mw->size);
696 snprintf(buf, sizeof(buf), "peer_mw%d", idx);
697 mw->peer_dbg_file = debugfs_create_file(buf, S_IRUSR | S_IWUSR,
704 dma_free_coherent(&tc->ntb->pdev->dev, mw->size,
714 static void tool_free_mw(struct tool_ctx *tc, int idx)
716 struct tool_mw *mw = &tc->mws[idx];
719 ntb_mw_clear_trans(tc->ntb, idx);
720 dma_free_coherent(&tc->ntb->pdev->dev, mw->size,
728 debugfs_remove(mw->peer_dbg_file);
730 mw->peer_dbg_file = NULL;
733 static ssize_t tool_peer_mw_trans_read(struct file *filep,
735 size_t size, loff_t *offp)
737 struct tool_mw *mw = filep->private_data;
741 ssize_t ret, off = 0;
744 resource_size_t mw_size;
745 resource_size_t align;
746 resource_size_t align_size;
748 buf_size = min_t(size_t, size, 512);
750 buf = kmalloc(buf_size, GFP_KERNEL);
754 ntb_mw_get_range(mw->tc->ntb, mw->idx,
755 &base, &mw_size, &align, &align_size);
757 off += scnprintf(buf + off, buf_size - off,
758 "Peer MW %d Information:\n", mw->idx);
760 off += scnprintf(buf + off, buf_size - off,
761 "Physical Address \t%pa[p]\n",
764 off += scnprintf(buf + off, buf_size - off,
765 "Window Size \t%lld\n",
766 (unsigned long long)mw_size);
768 off += scnprintf(buf + off, buf_size - off,
769 "Alignment \t%lld\n",
770 (unsigned long long)align);
772 off += scnprintf(buf + off, buf_size - off,
773 "Size Alignment \t%lld\n",
774 (unsigned long long)align_size);
776 off += scnprintf(buf + off, buf_size - off,
778 (mw->peer) ? 'Y' : 'N');
780 off += scnprintf(buf + off, buf_size - off,
781 "Allocated Size \t%zd\n",
782 (mw->peer) ? (size_t)mw->size : 0);
784 ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
789 static ssize_t tool_peer_mw_trans_write(struct file *filep,
790 const char __user *ubuf,
791 size_t size, loff_t *offp)
793 struct tool_mw *mw = filep->private_data;
797 unsigned long long val;
800 buf_size = min(size, (sizeof(buf) - 1));
801 if (copy_from_user(buf, ubuf, buf_size))
804 buf[buf_size] = '\0';
806 rc = kstrtoull(buf, 0, &val);
810 tool_free_mw(mw->tc, mw->idx);
812 rc = tool_setup_mw(mw->tc, mw->idx, val);
820 static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
821 tool_peer_mw_trans_read,
822 tool_peer_mw_trans_write);
824 static int tool_init_mw(struct tool_ctx *tc, int idx)
826 struct tool_mw *mw = &tc->mws[idx];
830 rc = ntb_mw_get_range(tc->ntb, idx, &base, &mw->win_size,
837 mw->local = ioremap_wc(base, mw->win_size);
844 static void tool_free_mws(struct tool_ctx *tc)
848 for (i = 0; i < tc->mw_count; i++) {
851 if (tc->mws[i].local)
852 iounmap(tc->mws[i].local);
854 tc->mws[i].local = NULL;
858 static void tool_setup_dbgfs(struct tool_ctx *tc)
862 /* This modules is useless without dbgfs... */
868 tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
873 debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
876 debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
877 tc, &tool_mask_fops);
879 debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
880 tc, &tool_peer_db_fops);
882 debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
883 tc, &tool_peer_mask_fops);
885 debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
886 tc, &tool_spad_fops);
888 debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
889 tc, &tool_peer_spad_fops);
891 debugfs_create_file("link", S_IRUSR | S_IWUSR, tc->dbgfs,
892 tc, &tool_link_fops);
894 debugfs_create_file("link_event", S_IWUSR, tc->dbgfs,
895 tc, &tool_link_event_fops);
897 for (i = 0; i < tc->mw_count; i++) {
900 snprintf(buf, sizeof(buf), "mw%d", i);
901 debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
902 &tc->mws[i], &tool_mw_fops);
904 snprintf(buf, sizeof(buf), "peer_trans%d", i);
905 debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
906 &tc->mws[i], &tool_peer_mw_trans_fops);
910 static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
916 if (ntb_db_is_unsafe(ntb))
917 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
919 if (ntb_spad_is_unsafe(ntb))
920 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
922 tc = kzalloc(sizeof(*tc), GFP_KERNEL);
929 init_waitqueue_head(&tc->link_wq);
931 tc->mw_count = min(ntb_mw_count(tc->ntb), MAX_MWS);
932 for (i = 0; i < tc->mw_count; i++) {
933 rc = tool_init_mw(tc, i);
938 tool_setup_dbgfs(tc);
940 rc = ntb_set_ctx(ntb, tc, &tool_ops);
944 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
951 debugfs_remove_recursive(tc->dbgfs);
957 static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
959 struct tool_ctx *tc = ntb->ctx;
964 ntb_link_disable(ntb);
966 debugfs_remove_recursive(tc->dbgfs);
970 static struct ntb_client tool_client = {
973 .remove = tool_remove,
977 static int __init tool_init(void)
981 if (debugfs_initialized())
982 tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
984 rc = ntb_register_client(&tool_client);
991 debugfs_remove_recursive(tool_dbgfs);
994 module_init(tool_init);
996 static void __exit tool_exit(void)
998 ntb_unregister_client(&tool_client);
999 debugfs_remove_recursive(tool_dbgfs);
1001 module_exit(tool_exit);