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 * # Set the doorbell mask
63 * root@self# echo 's 1' > $DBG_DIR/mask
65 * # Ring the doorbell from the peer
66 * root@peer# echo 's 1' > $DBG_DIR/peer_db
68 * # Clear the doorbell mask
69 * root@self# echo 'c 1' > $DBG_DIR/mask
71 * Observe debugging output in dmesg or your console. You should see a
72 * doorbell event triggered by clearing the mask. If not, this may indicate an
73 * issue with the hardware that needs to be worked around in the driver.
75 * Eg: read and write scratchpad registers
77 * root@peer# echo '0 0x01010101 1 0x7f7f7f7f' > $DBG_DIR/peer_spad
79 * root@self# cat $DBG_DIR/spad
81 * Observe that spad 0 and 1 have the values set by the peer.
84 #include <linux/init.h>
85 #include <linux/kernel.h>
86 #include <linux/module.h>
88 #include <linux/debugfs.h>
89 #include <linux/dma-mapping.h>
90 #include <linux/pci.h>
91 #include <linux/slab.h>
92 #include <linux/uaccess.h>
94 #include <linux/ntb.h>
96 #define DRIVER_NAME "ntb_tool"
97 #define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
99 #define DRIVER_LICENSE "Dual BSD/GPL"
100 #define DRIVER_VERSION "1.0"
101 #define DRIVER_RELDATE "22 April 2015"
102 #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
104 MODULE_LICENSE(DRIVER_LICENSE);
105 MODULE_VERSION(DRIVER_VERSION);
106 MODULE_AUTHOR(DRIVER_AUTHOR);
107 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
111 static unsigned long mw_size = 16;
112 module_param(mw_size, ulong, 0644);
113 MODULE_PARM_DESC(mw_size, "size order [n^2] of the memory window for testing");
115 static struct dentry *tool_dbgfs;
118 resource_size_t size;
126 struct dentry *dbgfs;
127 struct work_struct link_cleanup;
129 struct delayed_work link_work;
131 struct tool_mw mws[MAX_MWS];
134 #define SPAD_FNAME_SIZE 0x10
135 #define INT_PTR(x) ((void *)(unsigned long)x)
136 #define PTR_INT(x) ((int)(unsigned long)x)
138 #define TOOL_FOPS_RDWR(__name, __read, __write) \
139 const struct file_operations __name = { \
140 .owner = THIS_MODULE, \
141 .open = simple_open, \
146 static int tool_setup_mw(struct tool_ctx *tc, int idx)
149 struct tool_mw *mw = &tc->mws[idx];
151 resource_size_t size, align, align_size;
156 rc = ntb_mw_get_range(tc->ntb, idx, &base, &size, &align,
161 mw->size = min_t(resource_size_t, 1 << mw_size, size);
162 mw->size = round_up(mw->size, align);
163 mw->size = round_up(mw->size, align_size);
165 mw->local = ioremap_wc(base, size);
166 if (mw->local == NULL)
169 mw->peer = dma_alloc_coherent(&tc->ntb->pdev->dev, mw->size,
170 &mw->peer_dma, GFP_KERNEL);
172 if (mw->peer == NULL)
175 rc = ntb_mw_set_trans(tc->ntb, idx, mw->peer_dma, mw->size);
182 static void tool_free_mws(struct tool_ctx *tc)
186 for (i = 0; i < tc->mw_count; i++) {
187 if (tc->mws[i].peer) {
188 ntb_mw_clear_trans(tc->ntb, i);
189 dma_free_coherent(&tc->ntb->pdev->dev, tc->mws[i].size,
191 tc->mws[i].peer_dma);
195 tc->mws[i].peer = NULL;
196 tc->mws[i].peer_dma = 0;
198 if (tc->mws[i].local)
199 iounmap(tc->mws[i].local);
201 tc->mws[i].local = NULL;
207 static int tool_setup_mws(struct tool_ctx *tc)
212 tc->mw_count = min(ntb_mw_count(tc->ntb), MAX_MWS);
214 for (i = 0; i < tc->mw_count; i++) {
215 rc = tool_setup_mw(tc, i);
227 static void tool_link_work(struct work_struct *work)
230 struct tool_ctx *tc = container_of(work, struct tool_ctx,
234 rc = tool_setup_mws(tc);
236 dev_err(&tc->ntb->dev,
237 "Error setting up memory windows: %d\n", rc);
239 tc->link_is_up = true;
242 static void tool_link_cleanup(struct work_struct *work)
244 struct tool_ctx *tc = container_of(work, struct tool_ctx,
248 cancel_delayed_work_sync(&tc->link_work);
251 static void tool_link_event(void *ctx)
253 struct tool_ctx *tc = ctx;
254 enum ntb_speed speed;
255 enum ntb_width width;
258 up = ntb_link_is_up(tc->ntb, &speed, &width);
260 dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
261 up ? "up" : "down", speed, width);
264 schedule_delayed_work(&tc->link_work, 2*HZ);
266 schedule_work(&tc->link_cleanup);
269 static void tool_db_event(void *ctx, int vec)
271 struct tool_ctx *tc = ctx;
272 u64 db_bits, db_mask;
274 db_mask = ntb_db_vector_mask(tc->ntb, vec);
275 db_bits = ntb_db_read(tc->ntb);
277 dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
278 vec, db_mask, db_bits);
281 static const struct ntb_ctx_ops tool_ops = {
282 .link_event = tool_link_event,
283 .db_event = tool_db_event,
286 static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf,
287 size_t size, loff_t *offp,
288 u64 (*db_read_fn)(struct ntb_dev *))
297 buf_size = min_t(size_t, size, 0x20);
299 buf = kmalloc(buf_size, GFP_KERNEL);
303 pos = scnprintf(buf, buf_size, "%#llx\n",
304 db_read_fn(tc->ntb));
306 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
313 static ssize_t tool_dbfn_write(struct tool_ctx *tc,
314 const char __user *ubuf,
315 size_t size, loff_t *offp,
316 int (*db_set_fn)(struct ntb_dev *, u64),
317 int (*db_clear_fn)(struct ntb_dev *, u64))
324 buf = kmalloc(size + 1, GFP_KERNEL);
328 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
336 n = sscanf(buf, "%c %lli", &cmd, &db_bits);
342 } else if (cmd == 's') {
346 rc = db_set_fn(tc->ntb, db_bits);
347 } else if (cmd == 'c') {
351 rc = db_clear_fn(tc->ntb, db_bits);
359 static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf,
360 size_t size, loff_t *offp,
361 u32 (*spad_read_fn)(struct ntb_dev *, int))
371 spad_count = ntb_spad_count(tc->ntb);
374 * We multiply the number of spads by 15 to get the buffer size
375 * this is from 3 for the %d, 10 for the largest hex value
376 * (0x00000000) and 2 for the tab and line feed.
378 buf_size = min_t(size_t, size, spad_count * 15);
380 buf = kmalloc(buf_size, GFP_KERNEL);
386 for (i = 0; i < spad_count; ++i) {
387 pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
388 i, spad_read_fn(tc->ntb, i));
391 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
398 static ssize_t tool_spadfn_write(struct tool_ctx *tc,
399 const char __user *ubuf,
400 size_t size, loff_t *offp,
401 int (*spad_write_fn)(struct ntb_dev *,
410 if (!spad_write_fn) {
411 dev_dbg(&tc->ntb->dev, "no spad write fn\n");
415 buf = kmalloc(size + 1, GFP_KERNEL);
419 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
427 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
430 rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
434 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
445 static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
446 size_t size, loff_t *offp)
448 struct tool_ctx *tc = filep->private_data;
450 return tool_dbfn_read(tc, ubuf, size, offp,
451 tc->ntb->ops->db_read);
454 static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
455 size_t size, loff_t *offp)
457 struct tool_ctx *tc = filep->private_data;
459 return tool_dbfn_write(tc, ubuf, size, offp,
460 tc->ntb->ops->db_set,
461 tc->ntb->ops->db_clear);
464 static TOOL_FOPS_RDWR(tool_db_fops,
468 static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
469 size_t size, loff_t *offp)
471 struct tool_ctx *tc = filep->private_data;
473 return tool_dbfn_read(tc, ubuf, size, offp,
474 tc->ntb->ops->db_read_mask);
477 static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
478 size_t size, loff_t *offp)
480 struct tool_ctx *tc = filep->private_data;
482 return tool_dbfn_write(tc, ubuf, size, offp,
483 tc->ntb->ops->db_set_mask,
484 tc->ntb->ops->db_clear_mask);
487 static TOOL_FOPS_RDWR(tool_mask_fops,
491 static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
492 size_t size, loff_t *offp)
494 struct tool_ctx *tc = filep->private_data;
496 return tool_dbfn_read(tc, ubuf, size, offp,
497 tc->ntb->ops->peer_db_read);
500 static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
501 size_t size, loff_t *offp)
503 struct tool_ctx *tc = filep->private_data;
505 return tool_dbfn_write(tc, ubuf, size, offp,
506 tc->ntb->ops->peer_db_set,
507 tc->ntb->ops->peer_db_clear);
510 static TOOL_FOPS_RDWR(tool_peer_db_fops,
514 static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
515 size_t size, loff_t *offp)
517 struct tool_ctx *tc = filep->private_data;
519 return tool_dbfn_read(tc, ubuf, size, offp,
520 tc->ntb->ops->peer_db_read_mask);
523 static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
524 size_t size, loff_t *offp)
526 struct tool_ctx *tc = filep->private_data;
528 return tool_dbfn_write(tc, ubuf, size, offp,
529 tc->ntb->ops->peer_db_set_mask,
530 tc->ntb->ops->peer_db_clear_mask);
533 static TOOL_FOPS_RDWR(tool_peer_mask_fops,
535 tool_peer_mask_write);
537 static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
538 size_t size, loff_t *offp)
540 struct tool_ctx *tc = filep->private_data;
542 return tool_spadfn_read(tc, ubuf, size, offp,
543 tc->ntb->ops->spad_read);
546 static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
547 size_t size, loff_t *offp)
549 struct tool_ctx *tc = filep->private_data;
551 return tool_spadfn_write(tc, ubuf, size, offp,
552 tc->ntb->ops->spad_write);
555 static TOOL_FOPS_RDWR(tool_spad_fops,
559 static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
560 size_t size, loff_t *offp)
562 struct tool_ctx *tc = filep->private_data;
564 return tool_spadfn_read(tc, ubuf, size, offp,
565 tc->ntb->ops->peer_spad_read);
568 static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
569 size_t size, loff_t *offp)
571 struct tool_ctx *tc = filep->private_data;
573 return tool_spadfn_write(tc, ubuf, size, offp,
574 tc->ntb->ops->peer_spad_write);
577 static TOOL_FOPS_RDWR(tool_peer_spad_fops,
579 tool_peer_spad_write);
582 static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
583 size_t size, loff_t *offp)
585 struct tool_mw *mw = filep->private_data;
590 if (mw->local == NULL)
594 if (pos >= mw->size || !size)
596 if (size > mw->size - pos)
597 size = mw->size - pos;
599 buf = kmalloc(size, GFP_KERNEL);
603 memcpy_fromio(buf, mw->local + pos, size);
604 rc = copy_to_user(ubuf, buf, size);
620 static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
621 size_t size, loff_t *offp)
623 struct tool_mw *mw = filep->private_data;
630 if (pos >= mw->size || !size)
632 if (size > mw->size - pos)
633 size = mw->size - pos;
635 buf = kmalloc(size, GFP_KERNEL);
639 rc = copy_from_user(buf, ubuf, size);
649 memcpy_toio(mw->local + pos, buf, size);
657 static TOOL_FOPS_RDWR(tool_mw_fops,
662 static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
663 size_t size, loff_t *offp)
665 struct tool_mw *mw = filep->private_data;
667 return simple_read_from_buffer(ubuf, size, offp, mw->peer, mw->size);
670 static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
671 size_t size, loff_t *offp)
673 struct tool_mw *mw = filep->private_data;
675 return simple_write_to_buffer(mw->peer, mw->size, offp, ubuf, size);
678 static TOOL_FOPS_RDWR(tool_peer_mw_fops,
682 static void tool_setup_dbgfs(struct tool_ctx *tc)
687 /* This modules is useless without dbgfs... */
693 tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
698 debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
701 debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
702 tc, &tool_mask_fops);
704 debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
705 tc, &tool_peer_db_fops);
707 debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
708 tc, &tool_peer_mask_fops);
710 debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
711 tc, &tool_spad_fops);
713 debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
714 tc, &tool_peer_spad_fops);
716 mw_count = min(ntb_mw_count(tc->ntb), MAX_MWS);
717 for (i = 0; i < mw_count; i++) {
720 snprintf(buf, sizeof(buf), "mw%d", i);
721 debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
722 &tc->mws[i], &tool_mw_fops);
724 snprintf(buf, sizeof(buf), "peer_mw%d", i);
725 debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
726 &tc->mws[i], &tool_peer_mw_fops);
731 static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
736 if (ntb_db_is_unsafe(ntb))
737 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
739 if (ntb_spad_is_unsafe(ntb))
740 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
742 tc = kzalloc(sizeof(*tc), GFP_KERNEL);
749 INIT_DELAYED_WORK(&tc->link_work, tool_link_work);
750 INIT_WORK(&tc->link_cleanup, tool_link_cleanup);
752 tool_setup_dbgfs(tc);
754 rc = ntb_set_ctx(ntb, tc, &tool_ops);
758 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
764 debugfs_remove_recursive(tc->dbgfs);
765 cancel_delayed_work_sync(&tc->link_work);
766 cancel_work_sync(&tc->link_cleanup);
772 static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
774 struct tool_ctx *tc = ntb->ctx;
776 cancel_delayed_work_sync(&tc->link_work);
777 cancel_work_sync(&tc->link_cleanup);
782 ntb_link_disable(ntb);
784 debugfs_remove_recursive(tc->dbgfs);
788 static struct ntb_client tool_client = {
791 .remove = tool_remove,
795 static int __init tool_init(void)
799 if (debugfs_initialized())
800 tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
802 rc = ntb_register_client(&tool_client);
809 debugfs_remove_recursive(tool_dbgfs);
812 module_init(tool_init);
814 static void __exit tool_exit(void)
816 ntb_unregister_client(&tool_client);
817 debugfs_remove_recursive(tool_dbgfs);
819 module_exit(tool_exit);