blob: a0ead31cd79946e72beadce0d3cf05b44c860a9d [file] [log] [blame]
Allen Hubbe578b8812015-05-21 02:51:39 -04001/*
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.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8 *
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.
12 *
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.
17 *
18 * BSD LICENSE
19 *
20 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 *
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
31 * distribution.
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.
35 *
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.
47 *
48 * PCIe NTB Debugging Tool Linux driver
49 *
50 * Contact Information:
51 * Allen Hubbe <Allen.Hubbe@emc.com>
52 */
53
54/*
55 * How to use this tool, by example.
56 *
57 * Assuming $DBG_DIR is something like:
58 * '/sys/kernel/debug/ntb_tool/0000:00:03.0'
59 *
60 * Eg: check if clearing the doorbell mask generates an interrupt.
61 *
62 * # Set the doorbell mask
63 * root@self# echo 's 1' > $DBG_DIR/mask
64 *
65 * # Ring the doorbell from the peer
66 * root@peer# echo 's 1' > $DBG_DIR/peer_db
67 *
68 * # Clear the doorbell mask
69 * root@self# echo 'c 1' > $DBG_DIR/mask
70 *
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.
74 *
75 * Eg: read and write scratchpad registers
76 *
77 * root@peer# echo '0 0x01010101 1 0x7f7f7f7f' > $DBG_DIR/peer_spad
78 *
79 * root@self# cat $DBG_DIR/spad
80 *
81 * Observe that spad 0 and 1 have the values set by the peer.
82 */
83
84#include <linux/init.h>
85#include <linux/kernel.h>
86#include <linux/module.h>
87
88#include <linux/debugfs.h>
89#include <linux/dma-mapping.h>
90#include <linux/pci.h>
91#include <linux/slab.h>
92
93#include <linux/ntb.h>
94
95#define DRIVER_NAME "ntb_tool"
96#define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
97
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>"
102
103MODULE_LICENSE(DRIVER_LICENSE);
104MODULE_VERSION(DRIVER_VERSION);
105MODULE_AUTHOR(DRIVER_AUTHOR);
106MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
107
108static struct dentry *tool_dbgfs;
109
110struct tool_ctx {
111 struct ntb_dev *ntb;
112 struct dentry *dbgfs;
113};
114
115#define SPAD_FNAME_SIZE 0x10
116#define INT_PTR(x) ((void *)(unsigned long)x)
117#define PTR_INT(x) ((int)(unsigned long)x)
118
119#define TOOL_FOPS_RDWR(__name, __read, __write) \
120 const struct file_operations __name = { \
121 .owner = THIS_MODULE, \
122 .open = simple_open, \
123 .read = __read, \
124 .write = __write, \
125 }
126
127static void tool_link_event(void *ctx)
128{
129 struct tool_ctx *tc = ctx;
130 enum ntb_speed speed;
131 enum ntb_width width;
132 int up;
133
134 up = ntb_link_is_up(tc->ntb, &speed, &width);
135
136 dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
137 up ? "up" : "down", speed, width);
138}
139
140static void tool_db_event(void *ctx, int vec)
141{
142 struct tool_ctx *tc = ctx;
143 u64 db_bits, db_mask;
144
145 db_mask = ntb_db_vector_mask(tc->ntb, vec);
146 db_bits = ntb_db_read(tc->ntb);
147
148 dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
149 vec, db_mask, db_bits);
150}
151
152static const struct ntb_ctx_ops tool_ops = {
153 .link_event = tool_link_event,
154 .db_event = tool_db_event,
155};
156
157static 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 *))
160{
161 size_t buf_size;
162 char *buf;
163 ssize_t pos, rc;
164
165 if (!db_read_fn)
166 return -EINVAL;
167
168 buf_size = min_t(size_t, size, 0x20);
169
170 buf = kmalloc(buf_size, GFP_KERNEL);
171 if (!buf)
172 return -ENOMEM;
173
174 pos = scnprintf(buf, buf_size, "%#llx\n",
175 db_read_fn(tc->ntb));
176
177 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
178
179 kfree(buf);
180
181 return rc;
182}
183
184static 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))
189{
190 u64 db_bits;
191 char *buf, cmd;
192 ssize_t rc;
193 int n;
194
195 buf = kmalloc(size + 1, GFP_KERNEL);
196 if (!buf)
197 return -ENOMEM;
198
199 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
200 if (rc < 0) {
201 kfree(buf);
202 return rc;
203 }
204
205 buf[size] = 0;
206
207 n = sscanf(buf, "%c %lli", &cmd, &db_bits);
208
209 kfree(buf);
210
211 if (n != 2) {
212 rc = -EINVAL;
213 } else if (cmd == 's') {
214 if (!db_set_fn)
215 rc = -EINVAL;
216 else
217 rc = db_set_fn(tc->ntb, db_bits);
218 } else if (cmd == 'c') {
219 if (!db_clear_fn)
220 rc = -EINVAL;
221 else
222 rc = db_clear_fn(tc->ntb, db_bits);
223 } else {
224 rc = -EINVAL;
225 }
226
227 return rc ? : size;
228}
229
230static 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))
233{
234 size_t buf_size;
235 char *buf;
236 ssize_t pos, rc;
237 int i, spad_count;
238
239 if (!spad_read_fn)
240 return -EINVAL;
241
Logan Gunthorpe625f0802016-06-20 13:15:08 -0600242 spad_count = ntb_spad_count(tc->ntb);
243
244 /*
245 * We multiply the number of spads by 15 to get the buffer size
246 * this is from 3 for the %d, 10 for the largest hex value
247 * (0x00000000) and 2 for the tab and line feed.
248 */
249 buf_size = min_t(size_t, size, spad_count * 15);
Allen Hubbe578b8812015-05-21 02:51:39 -0400250
251 buf = kmalloc(buf_size, GFP_KERNEL);
252 if (!buf)
253 return -ENOMEM;
254
255 pos = 0;
256
Allen Hubbe578b8812015-05-21 02:51:39 -0400257 for (i = 0; i < spad_count; ++i) {
258 pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
259 i, spad_read_fn(tc->ntb, i));
260 }
261
262 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
263
264 kfree(buf);
265
266 return rc;
267}
268
269static ssize_t tool_spadfn_write(struct tool_ctx *tc,
270 const char __user *ubuf,
271 size_t size, loff_t *offp,
272 int (*spad_write_fn)(struct ntb_dev *,
273 int, u32))
274{
275 int spad_idx;
276 u32 spad_val;
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600277 char *buf, *buf_ptr;
Allen Hubbe578b8812015-05-21 02:51:39 -0400278 int pos, n;
279 ssize_t rc;
280
281 if (!spad_write_fn) {
282 dev_dbg(&tc->ntb->dev, "no spad write fn\n");
283 return -EINVAL;
284 }
285
286 buf = kmalloc(size + 1, GFP_KERNEL);
287 if (!buf)
288 return -ENOMEM;
289
290 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
291 if (rc < 0) {
292 kfree(buf);
293 return rc;
294 }
295
296 buf[size] = 0;
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600297 buf_ptr = buf;
298 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
Allen Hubbe578b8812015-05-21 02:51:39 -0400299 while (n == 2) {
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600300 buf_ptr += pos;
Allen Hubbe578b8812015-05-21 02:51:39 -0400301 rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
302 if (rc)
303 break;
304
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600305 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
Allen Hubbe578b8812015-05-21 02:51:39 -0400306 }
307
308 if (n < 0)
309 rc = n;
310
311 kfree(buf);
312
313 return rc ? : size;
314}
315
316static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
317 size_t size, loff_t *offp)
318{
319 struct tool_ctx *tc = filep->private_data;
320
321 return tool_dbfn_read(tc, ubuf, size, offp,
322 tc->ntb->ops->db_read);
323}
324
325static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
326 size_t size, loff_t *offp)
327{
328 struct tool_ctx *tc = filep->private_data;
329
330 return tool_dbfn_write(tc, ubuf, size, offp,
331 tc->ntb->ops->db_set,
332 tc->ntb->ops->db_clear);
333}
334
335static TOOL_FOPS_RDWR(tool_db_fops,
336 tool_db_read,
337 tool_db_write);
338
339static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
340 size_t size, loff_t *offp)
341{
342 struct tool_ctx *tc = filep->private_data;
343
344 return tool_dbfn_read(tc, ubuf, size, offp,
345 tc->ntb->ops->db_read_mask);
346}
347
348static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
349 size_t size, loff_t *offp)
350{
351 struct tool_ctx *tc = filep->private_data;
352
353 return tool_dbfn_write(tc, ubuf, size, offp,
354 tc->ntb->ops->db_set_mask,
355 tc->ntb->ops->db_clear_mask);
356}
357
358static TOOL_FOPS_RDWR(tool_mask_fops,
359 tool_mask_read,
360 tool_mask_write);
361
362static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
363 size_t size, loff_t *offp)
364{
365 struct tool_ctx *tc = filep->private_data;
366
367 return tool_dbfn_read(tc, ubuf, size, offp,
368 tc->ntb->ops->peer_db_read);
369}
370
371static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
372 size_t size, loff_t *offp)
373{
374 struct tool_ctx *tc = filep->private_data;
375
376 return tool_dbfn_write(tc, ubuf, size, offp,
377 tc->ntb->ops->peer_db_set,
378 tc->ntb->ops->peer_db_clear);
379}
380
381static TOOL_FOPS_RDWR(tool_peer_db_fops,
382 tool_peer_db_read,
383 tool_peer_db_write);
384
385static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
386 size_t size, loff_t *offp)
387{
388 struct tool_ctx *tc = filep->private_data;
389
390 return tool_dbfn_read(tc, ubuf, size, offp,
391 tc->ntb->ops->peer_db_read_mask);
392}
393
394static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
395 size_t size, loff_t *offp)
396{
397 struct tool_ctx *tc = filep->private_data;
398
399 return tool_dbfn_write(tc, ubuf, size, offp,
400 tc->ntb->ops->peer_db_set_mask,
401 tc->ntb->ops->peer_db_clear_mask);
402}
403
404static TOOL_FOPS_RDWR(tool_peer_mask_fops,
405 tool_peer_mask_read,
406 tool_peer_mask_write);
407
408static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
409 size_t size, loff_t *offp)
410{
411 struct tool_ctx *tc = filep->private_data;
412
413 return tool_spadfn_read(tc, ubuf, size, offp,
414 tc->ntb->ops->spad_read);
415}
416
417static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
418 size_t size, loff_t *offp)
419{
420 struct tool_ctx *tc = filep->private_data;
421
422 return tool_spadfn_write(tc, ubuf, size, offp,
423 tc->ntb->ops->spad_write);
424}
425
426static TOOL_FOPS_RDWR(tool_spad_fops,
427 tool_spad_read,
428 tool_spad_write);
429
430static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
431 size_t size, loff_t *offp)
432{
433 struct tool_ctx *tc = filep->private_data;
434
435 return tool_spadfn_read(tc, ubuf, size, offp,
436 tc->ntb->ops->peer_spad_read);
437}
438
439static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
440 size_t size, loff_t *offp)
441{
442 struct tool_ctx *tc = filep->private_data;
443
444 return tool_spadfn_write(tc, ubuf, size, offp,
445 tc->ntb->ops->peer_spad_write);
446}
447
448static TOOL_FOPS_RDWR(tool_peer_spad_fops,
449 tool_peer_spad_read,
450 tool_peer_spad_write);
451
452static void tool_setup_dbgfs(struct tool_ctx *tc)
453{
454 /* This modules is useless without dbgfs... */
455 if (!tool_dbgfs) {
456 tc->dbgfs = NULL;
457 return;
458 }
459
460 tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
461 tool_dbgfs);
462 if (!tc->dbgfs)
463 return;
464
465 debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
466 tc, &tool_db_fops);
467
468 debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
469 tc, &tool_mask_fops);
470
471 debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
472 tc, &tool_peer_db_fops);
473
474 debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
475 tc, &tool_peer_mask_fops);
476
477 debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
478 tc, &tool_spad_fops);
479
480 debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
481 tc, &tool_peer_spad_fops);
482}
483
484static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
485{
486 struct tool_ctx *tc;
487 int rc;
488
489 if (ntb_db_is_unsafe(ntb))
490 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
491
492 if (ntb_spad_is_unsafe(ntb))
493 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
494
495 tc = kmalloc(sizeof(*tc), GFP_KERNEL);
496 if (!tc) {
497 rc = -ENOMEM;
498 goto err_tc;
499 }
500
501 tc->ntb = ntb;
502
503 tool_setup_dbgfs(tc);
504
505 rc = ntb_set_ctx(ntb, tc, &tool_ops);
506 if (rc)
507 goto err_ctx;
508
509 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
510 ntb_link_event(ntb);
511
512 return 0;
513
514err_ctx:
515 debugfs_remove_recursive(tc->dbgfs);
516 kfree(tc);
517err_tc:
518 return rc;
519}
520
521static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
522{
523 struct tool_ctx *tc = ntb->ctx;
524
525 ntb_clear_ctx(ntb);
526 ntb_link_disable(ntb);
527
528 debugfs_remove_recursive(tc->dbgfs);
529 kfree(tc);
530}
531
532static struct ntb_client tool_client = {
533 .ops = {
534 .probe = tool_probe,
535 .remove = tool_remove,
536 },
537};
538
539static int __init tool_init(void)
540{
541 int rc;
542
543 if (debugfs_initialized())
544 tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
545
546 rc = ntb_register_client(&tool_client);
547 if (rc)
548 goto err_client;
549
550 return 0;
551
552err_client:
553 debugfs_remove_recursive(tool_dbgfs);
554 return rc;
555}
556module_init(tool_init);
557
558static void __exit tool_exit(void)
559{
560 ntb_unregister_client(&tool_client);
561 debugfs_remove_recursive(tool_dbgfs);
562}
563module_exit(tool_exit);