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>
93 #include <linux/ntb.h>
95 #define DRIVER_NAME "ntb_tool"
96 #define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
98 #define DRIVER_LICENSE "Dual BSD/GPL"
99 #define DRIVER_VERSION "1.0"
100 #define DRIVER_RELDATE "22 April 2015"
101 #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
103 MODULE_LICENSE(DRIVER_LICENSE);
104 MODULE_VERSION(DRIVER_VERSION);
105 MODULE_AUTHOR(DRIVER_AUTHOR);
106 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
108 static struct dentry *tool_dbgfs;
112 struct dentry *dbgfs;
115 #define SPAD_FNAME_SIZE 0x10
116 #define INT_PTR(x) ((void *)(unsigned long)x)
117 #define PTR_INT(x) ((int)(unsigned long)x)
119 #define TOOL_FOPS_RDWR(__name, __read, __write) \
120 const struct file_operations __name = { \
121 .owner = THIS_MODULE, \
122 .open = simple_open, \
127 static void tool_link_event(void *ctx)
129 struct tool_ctx *tc = ctx;
130 enum ntb_speed speed;
131 enum ntb_width width;
134 up = ntb_link_is_up(tc->ntb, &speed, &width);
136 dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
137 up ? "up" : "down", speed, width);
140 static void tool_db_event(void *ctx, int vec)
142 struct tool_ctx *tc = ctx;
143 u64 db_bits, db_mask;
145 db_mask = ntb_db_vector_mask(tc->ntb, vec);
146 db_bits = ntb_db_read(tc->ntb);
148 dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
149 vec, db_mask, db_bits);
152 static const struct ntb_ctx_ops tool_ops = {
153 .link_event = tool_link_event,
154 .db_event = tool_db_event,
157 static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf,
158 size_t size, loff_t *offp,
159 u64 (*db_read_fn)(struct ntb_dev *))
168 buf_size = min_t(size_t, size, 0x20);
170 buf = kmalloc(buf_size, GFP_KERNEL);
174 pos = scnprintf(buf, buf_size, "%#llx\n",
175 db_read_fn(tc->ntb));
177 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
184 static ssize_t tool_dbfn_write(struct tool_ctx *tc,
185 const char __user *ubuf,
186 size_t size, loff_t *offp,
187 int (*db_set_fn)(struct ntb_dev *, u64),
188 int (*db_clear_fn)(struct ntb_dev *, u64))
195 buf = kmalloc(size + 1, GFP_KERNEL);
199 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
207 n = sscanf(buf, "%c %lli", &cmd, &db_bits);
213 } else if (cmd == 's') {
217 rc = db_set_fn(tc->ntb, db_bits);
218 } else if (cmd == 'c') {
222 rc = db_clear_fn(tc->ntb, db_bits);
230 static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf,
231 size_t size, loff_t *offp,
232 u32 (*spad_read_fn)(struct ntb_dev *, int))
242 buf_size = min_t(size_t, size, 0x100);
244 buf = kmalloc(buf_size, GFP_KERNEL);
250 spad_count = ntb_spad_count(tc->ntb);
251 for (i = 0; i < spad_count; ++i) {
252 pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
253 i, spad_read_fn(tc->ntb, i));
256 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
263 static ssize_t tool_spadfn_write(struct tool_ctx *tc,
264 const char __user *ubuf,
265 size_t size, loff_t *offp,
266 int (*spad_write_fn)(struct ntb_dev *,
275 if (!spad_write_fn) {
276 dev_dbg(&tc->ntb->dev, "no spad write fn\n");
280 buf = kmalloc(size + 1, GFP_KERNEL);
284 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
292 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
295 rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
299 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
310 static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
311 size_t size, loff_t *offp)
313 struct tool_ctx *tc = filep->private_data;
315 return tool_dbfn_read(tc, ubuf, size, offp,
316 tc->ntb->ops->db_read);
319 static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
320 size_t size, loff_t *offp)
322 struct tool_ctx *tc = filep->private_data;
324 return tool_dbfn_write(tc, ubuf, size, offp,
325 tc->ntb->ops->db_set,
326 tc->ntb->ops->db_clear);
329 static TOOL_FOPS_RDWR(tool_db_fops,
333 static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
334 size_t size, loff_t *offp)
336 struct tool_ctx *tc = filep->private_data;
338 return tool_dbfn_read(tc, ubuf, size, offp,
339 tc->ntb->ops->db_read_mask);
342 static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
343 size_t size, loff_t *offp)
345 struct tool_ctx *tc = filep->private_data;
347 return tool_dbfn_write(tc, ubuf, size, offp,
348 tc->ntb->ops->db_set_mask,
349 tc->ntb->ops->db_clear_mask);
352 static TOOL_FOPS_RDWR(tool_mask_fops,
356 static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
357 size_t size, loff_t *offp)
359 struct tool_ctx *tc = filep->private_data;
361 return tool_dbfn_read(tc, ubuf, size, offp,
362 tc->ntb->ops->peer_db_read);
365 static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
366 size_t size, loff_t *offp)
368 struct tool_ctx *tc = filep->private_data;
370 return tool_dbfn_write(tc, ubuf, size, offp,
371 tc->ntb->ops->peer_db_set,
372 tc->ntb->ops->peer_db_clear);
375 static TOOL_FOPS_RDWR(tool_peer_db_fops,
379 static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
380 size_t size, loff_t *offp)
382 struct tool_ctx *tc = filep->private_data;
384 return tool_dbfn_read(tc, ubuf, size, offp,
385 tc->ntb->ops->peer_db_read_mask);
388 static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
389 size_t size, loff_t *offp)
391 struct tool_ctx *tc = filep->private_data;
393 return tool_dbfn_write(tc, ubuf, size, offp,
394 tc->ntb->ops->peer_db_set_mask,
395 tc->ntb->ops->peer_db_clear_mask);
398 static TOOL_FOPS_RDWR(tool_peer_mask_fops,
400 tool_peer_mask_write);
402 static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
403 size_t size, loff_t *offp)
405 struct tool_ctx *tc = filep->private_data;
407 return tool_spadfn_read(tc, ubuf, size, offp,
408 tc->ntb->ops->spad_read);
411 static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
412 size_t size, loff_t *offp)
414 struct tool_ctx *tc = filep->private_data;
416 return tool_spadfn_write(tc, ubuf, size, offp,
417 tc->ntb->ops->spad_write);
420 static TOOL_FOPS_RDWR(tool_spad_fops,
424 static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
425 size_t size, loff_t *offp)
427 struct tool_ctx *tc = filep->private_data;
429 return tool_spadfn_read(tc, ubuf, size, offp,
430 tc->ntb->ops->peer_spad_read);
433 static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
434 size_t size, loff_t *offp)
436 struct tool_ctx *tc = filep->private_data;
438 return tool_spadfn_write(tc, ubuf, size, offp,
439 tc->ntb->ops->peer_spad_write);
442 static TOOL_FOPS_RDWR(tool_peer_spad_fops,
444 tool_peer_spad_write);
446 static void tool_setup_dbgfs(struct tool_ctx *tc)
448 /* This modules is useless without dbgfs... */
454 tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
459 debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
462 debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
463 tc, &tool_mask_fops);
465 debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
466 tc, &tool_peer_db_fops);
468 debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
469 tc, &tool_peer_mask_fops);
471 debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
472 tc, &tool_spad_fops);
474 debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
475 tc, &tool_peer_spad_fops);
478 static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
483 if (ntb_db_is_unsafe(ntb))
484 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
486 if (ntb_spad_is_unsafe(ntb))
487 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
489 tc = kmalloc(sizeof(*tc), GFP_KERNEL);
497 tool_setup_dbgfs(tc);
499 rc = ntb_set_ctx(ntb, tc, &tool_ops);
503 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
509 debugfs_remove_recursive(tc->dbgfs);
515 static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
517 struct tool_ctx *tc = ntb->ctx;
520 ntb_link_disable(ntb);
522 debugfs_remove_recursive(tc->dbgfs);
526 static struct ntb_client tool_client = {
529 .remove = tool_remove,
533 static int __init tool_init(void)
537 if (debugfs_initialized())
538 tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
540 rc = ntb_register_client(&tool_client);
547 debugfs_remove_recursive(tool_dbgfs);
550 module_init(tool_init);
552 static void __exit tool_exit(void)
554 ntb_unregister_client(&tool_client);
555 debugfs_remove_recursive(tool_dbgfs);
557 module_exit(tool_exit);