blob: 1509b4c1f20482a1a5734e4570f3fcf218171a38 [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.
Logan Gunthorpe717146a2016-06-20 13:15:09 -060082 *
83 * # Check the memory window translation info
84 * cat $DBG_DIR/peer_trans0
85 *
86 * # Setup a 16k memory window buffer
87 * echo 16384 > $DBG_DIR/peer_trans0
88 *
Allen Hubbe578b8812015-05-21 02:51:39 -040089 */
90
91#include <linux/init.h>
92#include <linux/kernel.h>
93#include <linux/module.h>
94
95#include <linux/debugfs.h>
96#include <linux/dma-mapping.h>
97#include <linux/pci.h>
98#include <linux/slab.h>
Logan Gunthorpe8b71d282016-06-03 14:50:33 -060099#include <linux/uaccess.h>
Allen Hubbe578b8812015-05-21 02:51:39 -0400100
101#include <linux/ntb.h>
102
103#define DRIVER_NAME "ntb_tool"
104#define DRIVER_DESCRIPTION "PCIe NTB Debugging Tool"
105
106#define DRIVER_LICENSE "Dual BSD/GPL"
107#define DRIVER_VERSION "1.0"
108#define DRIVER_RELDATE "22 April 2015"
109#define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
110
111MODULE_LICENSE(DRIVER_LICENSE);
112MODULE_VERSION(DRIVER_VERSION);
113MODULE_AUTHOR(DRIVER_AUTHOR);
114MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
115
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600116#define MAX_MWS 16
117
Allen Hubbe578b8812015-05-21 02:51:39 -0400118static struct dentry *tool_dbgfs;
119
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600120struct tool_mw {
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600121 int idx;
122 struct tool_ctx *tc;
123 resource_size_t win_size;
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600124 resource_size_t size;
125 u8 __iomem *local;
126 u8 *peer;
127 dma_addr_t peer_dma;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600128 struct dentry *peer_dbg_file;
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600129};
130
Allen Hubbe578b8812015-05-21 02:51:39 -0400131struct tool_ctx {
132 struct ntb_dev *ntb;
133 struct dentry *dbgfs;
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600134 int mw_count;
135 struct tool_mw mws[MAX_MWS];
Allen Hubbe578b8812015-05-21 02:51:39 -0400136};
137
138#define SPAD_FNAME_SIZE 0x10
139#define INT_PTR(x) ((void *)(unsigned long)x)
140#define PTR_INT(x) ((int)(unsigned long)x)
141
142#define TOOL_FOPS_RDWR(__name, __read, __write) \
143 const struct file_operations __name = { \
144 .owner = THIS_MODULE, \
145 .open = simple_open, \
146 .read = __read, \
147 .write = __write, \
148 }
149
150static void tool_link_event(void *ctx)
151{
152 struct tool_ctx *tc = ctx;
153 enum ntb_speed speed;
154 enum ntb_width width;
155 int up;
156
157 up = ntb_link_is_up(tc->ntb, &speed, &width);
158
159 dev_dbg(&tc->ntb->dev, "link is %s speed %d width %d\n",
160 up ? "up" : "down", speed, width);
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600161
Allen Hubbe578b8812015-05-21 02:51:39 -0400162}
163
164static void tool_db_event(void *ctx, int vec)
165{
166 struct tool_ctx *tc = ctx;
167 u64 db_bits, db_mask;
168
169 db_mask = ntb_db_vector_mask(tc->ntb, vec);
170 db_bits = ntb_db_read(tc->ntb);
171
172 dev_dbg(&tc->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
173 vec, db_mask, db_bits);
174}
175
176static const struct ntb_ctx_ops tool_ops = {
177 .link_event = tool_link_event,
178 .db_event = tool_db_event,
179};
180
181static ssize_t tool_dbfn_read(struct tool_ctx *tc, char __user *ubuf,
182 size_t size, loff_t *offp,
183 u64 (*db_read_fn)(struct ntb_dev *))
184{
185 size_t buf_size;
186 char *buf;
187 ssize_t pos, rc;
188
189 if (!db_read_fn)
190 return -EINVAL;
191
192 buf_size = min_t(size_t, size, 0x20);
193
194 buf = kmalloc(buf_size, GFP_KERNEL);
195 if (!buf)
196 return -ENOMEM;
197
198 pos = scnprintf(buf, buf_size, "%#llx\n",
199 db_read_fn(tc->ntb));
200
201 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
202
203 kfree(buf);
204
205 return rc;
206}
207
208static ssize_t tool_dbfn_write(struct tool_ctx *tc,
209 const char __user *ubuf,
210 size_t size, loff_t *offp,
211 int (*db_set_fn)(struct ntb_dev *, u64),
212 int (*db_clear_fn)(struct ntb_dev *, u64))
213{
214 u64 db_bits;
215 char *buf, cmd;
216 ssize_t rc;
217 int n;
218
219 buf = kmalloc(size + 1, GFP_KERNEL);
220 if (!buf)
221 return -ENOMEM;
222
223 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
224 if (rc < 0) {
225 kfree(buf);
226 return rc;
227 }
228
229 buf[size] = 0;
230
231 n = sscanf(buf, "%c %lli", &cmd, &db_bits);
232
233 kfree(buf);
234
235 if (n != 2) {
236 rc = -EINVAL;
237 } else if (cmd == 's') {
238 if (!db_set_fn)
239 rc = -EINVAL;
240 else
241 rc = db_set_fn(tc->ntb, db_bits);
242 } else if (cmd == 'c') {
243 if (!db_clear_fn)
244 rc = -EINVAL;
245 else
246 rc = db_clear_fn(tc->ntb, db_bits);
247 } else {
248 rc = -EINVAL;
249 }
250
251 return rc ? : size;
252}
253
254static ssize_t tool_spadfn_read(struct tool_ctx *tc, char __user *ubuf,
255 size_t size, loff_t *offp,
256 u32 (*spad_read_fn)(struct ntb_dev *, int))
257{
258 size_t buf_size;
259 char *buf;
260 ssize_t pos, rc;
261 int i, spad_count;
262
263 if (!spad_read_fn)
264 return -EINVAL;
265
Logan Gunthorpe625f0802016-06-20 13:15:08 -0600266 spad_count = ntb_spad_count(tc->ntb);
267
268 /*
269 * We multiply the number of spads by 15 to get the buffer size
270 * this is from 3 for the %d, 10 for the largest hex value
271 * (0x00000000) and 2 for the tab and line feed.
272 */
273 buf_size = min_t(size_t, size, spad_count * 15);
Allen Hubbe578b8812015-05-21 02:51:39 -0400274
275 buf = kmalloc(buf_size, GFP_KERNEL);
276 if (!buf)
277 return -ENOMEM;
278
279 pos = 0;
280
Allen Hubbe578b8812015-05-21 02:51:39 -0400281 for (i = 0; i < spad_count; ++i) {
282 pos += scnprintf(buf + pos, buf_size - pos, "%d\t%#x\n",
283 i, spad_read_fn(tc->ntb, i));
284 }
285
286 rc = simple_read_from_buffer(ubuf, size, offp, buf, pos);
287
288 kfree(buf);
289
290 return rc;
291}
292
293static ssize_t tool_spadfn_write(struct tool_ctx *tc,
294 const char __user *ubuf,
295 size_t size, loff_t *offp,
296 int (*spad_write_fn)(struct ntb_dev *,
297 int, u32))
298{
299 int spad_idx;
300 u32 spad_val;
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600301 char *buf, *buf_ptr;
Allen Hubbe578b8812015-05-21 02:51:39 -0400302 int pos, n;
303 ssize_t rc;
304
305 if (!spad_write_fn) {
306 dev_dbg(&tc->ntb->dev, "no spad write fn\n");
307 return -EINVAL;
308 }
309
310 buf = kmalloc(size + 1, GFP_KERNEL);
311 if (!buf)
312 return -ENOMEM;
313
314 rc = simple_write_to_buffer(buf, size, offp, ubuf, size);
315 if (rc < 0) {
316 kfree(buf);
317 return rc;
318 }
319
320 buf[size] = 0;
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600321 buf_ptr = buf;
322 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
Allen Hubbe578b8812015-05-21 02:51:39 -0400323 while (n == 2) {
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600324 buf_ptr += pos;
Allen Hubbe578b8812015-05-21 02:51:39 -0400325 rc = spad_write_fn(tc->ntb, spad_idx, spad_val);
326 if (rc)
327 break;
328
Logan Gunthorpec792eba2016-05-27 14:38:31 -0600329 n = sscanf(buf_ptr, "%d %i%n", &spad_idx, &spad_val, &pos);
Allen Hubbe578b8812015-05-21 02:51:39 -0400330 }
331
332 if (n < 0)
333 rc = n;
334
335 kfree(buf);
336
337 return rc ? : size;
338}
339
340static ssize_t tool_db_read(struct file *filep, char __user *ubuf,
341 size_t size, loff_t *offp)
342{
343 struct tool_ctx *tc = filep->private_data;
344
345 return tool_dbfn_read(tc, ubuf, size, offp,
346 tc->ntb->ops->db_read);
347}
348
349static ssize_t tool_db_write(struct file *filep, const char __user *ubuf,
350 size_t size, loff_t *offp)
351{
352 struct tool_ctx *tc = filep->private_data;
353
354 return tool_dbfn_write(tc, ubuf, size, offp,
355 tc->ntb->ops->db_set,
356 tc->ntb->ops->db_clear);
357}
358
359static TOOL_FOPS_RDWR(tool_db_fops,
360 tool_db_read,
361 tool_db_write);
362
363static ssize_t tool_mask_read(struct file *filep, char __user *ubuf,
364 size_t size, loff_t *offp)
365{
366 struct tool_ctx *tc = filep->private_data;
367
368 return tool_dbfn_read(tc, ubuf, size, offp,
369 tc->ntb->ops->db_read_mask);
370}
371
372static ssize_t tool_mask_write(struct file *filep, const char __user *ubuf,
373 size_t size, loff_t *offp)
374{
375 struct tool_ctx *tc = filep->private_data;
376
377 return tool_dbfn_write(tc, ubuf, size, offp,
378 tc->ntb->ops->db_set_mask,
379 tc->ntb->ops->db_clear_mask);
380}
381
382static TOOL_FOPS_RDWR(tool_mask_fops,
383 tool_mask_read,
384 tool_mask_write);
385
386static ssize_t tool_peer_db_read(struct file *filep, char __user *ubuf,
387 size_t size, loff_t *offp)
388{
389 struct tool_ctx *tc = filep->private_data;
390
391 return tool_dbfn_read(tc, ubuf, size, offp,
392 tc->ntb->ops->peer_db_read);
393}
394
395static ssize_t tool_peer_db_write(struct file *filep, const char __user *ubuf,
396 size_t size, loff_t *offp)
397{
398 struct tool_ctx *tc = filep->private_data;
399
400 return tool_dbfn_write(tc, ubuf, size, offp,
401 tc->ntb->ops->peer_db_set,
402 tc->ntb->ops->peer_db_clear);
403}
404
405static TOOL_FOPS_RDWR(tool_peer_db_fops,
406 tool_peer_db_read,
407 tool_peer_db_write);
408
409static ssize_t tool_peer_mask_read(struct file *filep, char __user *ubuf,
410 size_t size, loff_t *offp)
411{
412 struct tool_ctx *tc = filep->private_data;
413
414 return tool_dbfn_read(tc, ubuf, size, offp,
415 tc->ntb->ops->peer_db_read_mask);
416}
417
418static ssize_t tool_peer_mask_write(struct file *filep, const char __user *ubuf,
419 size_t size, loff_t *offp)
420{
421 struct tool_ctx *tc = filep->private_data;
422
423 return tool_dbfn_write(tc, ubuf, size, offp,
424 tc->ntb->ops->peer_db_set_mask,
425 tc->ntb->ops->peer_db_clear_mask);
426}
427
428static TOOL_FOPS_RDWR(tool_peer_mask_fops,
429 tool_peer_mask_read,
430 tool_peer_mask_write);
431
432static ssize_t tool_spad_read(struct file *filep, char __user *ubuf,
433 size_t size, loff_t *offp)
434{
435 struct tool_ctx *tc = filep->private_data;
436
437 return tool_spadfn_read(tc, ubuf, size, offp,
438 tc->ntb->ops->spad_read);
439}
440
441static ssize_t tool_spad_write(struct file *filep, const char __user *ubuf,
442 size_t size, loff_t *offp)
443{
444 struct tool_ctx *tc = filep->private_data;
445
446 return tool_spadfn_write(tc, ubuf, size, offp,
447 tc->ntb->ops->spad_write);
448}
449
450static TOOL_FOPS_RDWR(tool_spad_fops,
451 tool_spad_read,
452 tool_spad_write);
453
454static ssize_t tool_peer_spad_read(struct file *filep, char __user *ubuf,
455 size_t size, loff_t *offp)
456{
457 struct tool_ctx *tc = filep->private_data;
458
459 return tool_spadfn_read(tc, ubuf, size, offp,
460 tc->ntb->ops->peer_spad_read);
461}
462
463static ssize_t tool_peer_spad_write(struct file *filep, const char __user *ubuf,
464 size_t size, loff_t *offp)
465{
466 struct tool_ctx *tc = filep->private_data;
467
468 return tool_spadfn_write(tc, ubuf, size, offp,
469 tc->ntb->ops->peer_spad_write);
470}
471
472static TOOL_FOPS_RDWR(tool_peer_spad_fops,
473 tool_peer_spad_read,
474 tool_peer_spad_write);
475
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600476
477static ssize_t tool_mw_read(struct file *filep, char __user *ubuf,
478 size_t size, loff_t *offp)
479{
480 struct tool_mw *mw = filep->private_data;
481 ssize_t rc;
482 loff_t pos = *offp;
483 void *buf;
484
485 if (mw->local == NULL)
486 return -EIO;
487 if (pos < 0)
488 return -EINVAL;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600489 if (pos >= mw->win_size || !size)
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600490 return 0;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600491 if (size > mw->win_size - pos)
492 size = mw->win_size - pos;
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600493
494 buf = kmalloc(size, GFP_KERNEL);
495 if (!buf)
496 return -ENOMEM;
497
498 memcpy_fromio(buf, mw->local + pos, size);
499 rc = copy_to_user(ubuf, buf, size);
500 if (rc == size) {
501 rc = -EFAULT;
502 goto err_free;
503 }
504
505 size -= rc;
506 *offp = pos + size;
507 rc = size;
508
509err_free:
510 kfree(buf);
511
512 return rc;
513}
514
515static ssize_t tool_mw_write(struct file *filep, const char __user *ubuf,
516 size_t size, loff_t *offp)
517{
518 struct tool_mw *mw = filep->private_data;
519 ssize_t rc;
520 loff_t pos = *offp;
521 void *buf;
522
523 if (pos < 0)
524 return -EINVAL;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600525 if (pos >= mw->win_size || !size)
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600526 return 0;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600527 if (size > mw->win_size - pos)
528 size = mw->win_size - pos;
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600529
530 buf = kmalloc(size, GFP_KERNEL);
531 if (!buf)
532 return -ENOMEM;
533
534 rc = copy_from_user(buf, ubuf, size);
535 if (rc == size) {
536 rc = -EFAULT;
537 goto err_free;
538 }
539
540 size -= rc;
541 *offp = pos + size;
542 rc = size;
543
544 memcpy_toio(mw->local + pos, buf, size);
545
546err_free:
547 kfree(buf);
548
549 return rc;
550}
551
552static TOOL_FOPS_RDWR(tool_mw_fops,
553 tool_mw_read,
554 tool_mw_write);
555
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600556static ssize_t tool_peer_mw_read(struct file *filep, char __user *ubuf,
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600557 size_t size, loff_t *offp)
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600558{
559 struct tool_mw *mw = filep->private_data;
560
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600561 if (!mw->peer)
562 return -ENXIO;
563
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600564 return simple_read_from_buffer(ubuf, size, offp, mw->peer, mw->size);
565}
566
567static ssize_t tool_peer_mw_write(struct file *filep, const char __user *ubuf,
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600568 size_t size, loff_t *offp)
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600569{
570 struct tool_mw *mw = filep->private_data;
571
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600572 if (!mw->peer)
573 return -ENXIO;
574
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600575 return simple_write_to_buffer(mw->peer, mw->size, offp, ubuf, size);
576}
577
578static TOOL_FOPS_RDWR(tool_peer_mw_fops,
579 tool_peer_mw_read,
580 tool_peer_mw_write);
581
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600582static int tool_setup_mw(struct tool_ctx *tc, int idx, size_t req_size)
583{
584 int rc;
585 struct tool_mw *mw = &tc->mws[idx];
586 phys_addr_t base;
587 resource_size_t size, align, align_size;
588 char buf[16];
589
590 if (mw->peer)
591 return 0;
592
593 rc = ntb_mw_get_range(tc->ntb, idx, &base, &size, &align,
594 &align_size);
595 if (rc)
596 return rc;
597
598 mw->size = min_t(resource_size_t, req_size, size);
599 mw->size = round_up(mw->size, align);
600 mw->size = round_up(mw->size, align_size);
601 mw->peer = dma_alloc_coherent(&tc->ntb->pdev->dev, mw->size,
602 &mw->peer_dma, GFP_KERNEL);
603
604 if (!mw->peer)
605 return -ENOMEM;
606
607 rc = ntb_mw_set_trans(tc->ntb, idx, mw->peer_dma, mw->size);
608 if (rc)
609 goto err_free_dma;
610
611 snprintf(buf, sizeof(buf), "peer_mw%d", idx);
612 mw->peer_dbg_file = debugfs_create_file(buf, S_IRUSR | S_IWUSR,
613 mw->tc->dbgfs, mw,
614 &tool_peer_mw_fops);
615
616 return 0;
617
618err_free_dma:
619 dma_free_coherent(&tc->ntb->pdev->dev, mw->size,
620 mw->peer,
621 mw->peer_dma);
622 mw->peer = NULL;
623 mw->peer_dma = 0;
624 mw->size = 0;
625
626 return rc;
627}
628
629static void tool_free_mw(struct tool_ctx *tc, int idx)
630{
631 struct tool_mw *mw = &tc->mws[idx];
632
633 if (mw->peer) {
634 ntb_mw_clear_trans(tc->ntb, idx);
635 dma_free_coherent(&tc->ntb->pdev->dev, mw->size,
636 mw->peer,
637 mw->peer_dma);
638 }
639
640 mw->peer = NULL;
641 mw->peer_dma = 0;
642
643 debugfs_remove(mw->peer_dbg_file);
644
645 mw->peer_dbg_file = NULL;
646}
647
648static ssize_t tool_peer_mw_trans_read(struct file *filep,
649 char __user *ubuf,
650 size_t size, loff_t *offp)
651{
652 struct tool_mw *mw = filep->private_data;
653
654 char *buf;
655 size_t buf_size;
656 ssize_t ret, off = 0;
657
658 phys_addr_t base;
659 resource_size_t mw_size;
660 resource_size_t align;
661 resource_size_t align_size;
662
663 buf_size = min_t(size_t, size, 512);
664
665 buf = kmalloc(buf_size, GFP_KERNEL);
666 if (!buf)
667 return -ENOMEM;
668
669 ntb_mw_get_range(mw->tc->ntb, mw->idx,
670 &base, &mw_size, &align, &align_size);
671
672 off += scnprintf(buf + off, buf_size - off,
673 "Peer MW %d Information:\n", mw->idx);
674
675 off += scnprintf(buf + off, buf_size - off,
676 "Physical Address \t%pa[p]\n",
677 &base);
678
679 off += scnprintf(buf + off, buf_size - off,
680 "Window Size \t%lld\n",
681 (unsigned long long)mw_size);
682
683 off += scnprintf(buf + off, buf_size - off,
684 "Alignment \t%lld\n",
685 (unsigned long long)align);
686
687 off += scnprintf(buf + off, buf_size - off,
688 "Size Alignment \t%lld\n",
689 (unsigned long long)align_size);
690
691 off += scnprintf(buf + off, buf_size - off,
692 "Ready \t%c\n",
693 (mw->peer) ? 'Y' : 'N');
694
695 off += scnprintf(buf + off, buf_size - off,
696 "Allocated Size \t%zd\n",
697 (mw->peer) ? (size_t)mw->size : 0);
698
699 ret = simple_read_from_buffer(ubuf, size, offp, buf, off);
700 kfree(buf);
701 return ret;
702}
703
704static ssize_t tool_peer_mw_trans_write(struct file *filep,
705 const char __user *ubuf,
706 size_t size, loff_t *offp)
707{
708 struct tool_mw *mw = filep->private_data;
709
710 char buf[32];
711 size_t buf_size;
712 unsigned long long val;
713 int rc;
714
715 buf_size = min(size, (sizeof(buf) - 1));
716 if (copy_from_user(buf, ubuf, buf_size))
717 return -EFAULT;
718
719 buf[buf_size] = '\0';
720
721 rc = kstrtoull(buf, 0, &val);
722 if (rc)
723 return rc;
724
725 tool_free_mw(mw->tc, mw->idx);
726 if (val)
727 rc = tool_setup_mw(mw->tc, mw->idx, val);
728
729 if (rc)
730 return rc;
731
732 return size;
733}
734
735static TOOL_FOPS_RDWR(tool_peer_mw_trans_fops,
736 tool_peer_mw_trans_read,
737 tool_peer_mw_trans_write);
738
739static int tool_init_mw(struct tool_ctx *tc, int idx)
740{
741 struct tool_mw *mw = &tc->mws[idx];
742 phys_addr_t base;
743 int rc;
744
745 rc = ntb_mw_get_range(tc->ntb, idx, &base, &mw->win_size,
746 NULL, NULL);
747 if (rc)
748 return rc;
749
750 mw->tc = tc;
751 mw->idx = idx;
752 mw->local = ioremap_wc(base, mw->win_size);
753 if (!mw->local)
754 return -EFAULT;
755
756 return 0;
757}
758
759static void tool_free_mws(struct tool_ctx *tc)
760{
761 int i;
762
763 for (i = 0; i < tc->mw_count; i++) {
764 tool_free_mw(tc, i);
765
766 if (tc->mws[i].local)
767 iounmap(tc->mws[i].local);
768
769 tc->mws[i].local = NULL;
770 }
771}
772
Allen Hubbe578b8812015-05-21 02:51:39 -0400773static void tool_setup_dbgfs(struct tool_ctx *tc)
774{
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600775 int i;
776
Allen Hubbe578b8812015-05-21 02:51:39 -0400777 /* This modules is useless without dbgfs... */
778 if (!tool_dbgfs) {
779 tc->dbgfs = NULL;
780 return;
781 }
782
783 tc->dbgfs = debugfs_create_dir(dev_name(&tc->ntb->dev),
784 tool_dbgfs);
785 if (!tc->dbgfs)
786 return;
787
788 debugfs_create_file("db", S_IRUSR | S_IWUSR, tc->dbgfs,
789 tc, &tool_db_fops);
790
791 debugfs_create_file("mask", S_IRUSR | S_IWUSR, tc->dbgfs,
792 tc, &tool_mask_fops);
793
794 debugfs_create_file("peer_db", S_IRUSR | S_IWUSR, tc->dbgfs,
795 tc, &tool_peer_db_fops);
796
797 debugfs_create_file("peer_mask", S_IRUSR | S_IWUSR, tc->dbgfs,
798 tc, &tool_peer_mask_fops);
799
800 debugfs_create_file("spad", S_IRUSR | S_IWUSR, tc->dbgfs,
801 tc, &tool_spad_fops);
802
803 debugfs_create_file("peer_spad", S_IRUSR | S_IWUSR, tc->dbgfs,
804 tc, &tool_peer_spad_fops);
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600805
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600806 for (i = 0; i < tc->mw_count; i++) {
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600807 char buf[30];
808
809 snprintf(buf, sizeof(buf), "mw%d", i);
810 debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
811 &tc->mws[i], &tool_mw_fops);
812
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600813 snprintf(buf, sizeof(buf), "peer_trans%d", i);
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600814 debugfs_create_file(buf, S_IRUSR | S_IWUSR, tc->dbgfs,
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600815 &tc->mws[i], &tool_peer_mw_trans_fops);
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600816 }
Allen Hubbe578b8812015-05-21 02:51:39 -0400817}
818
819static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
820{
821 struct tool_ctx *tc;
822 int rc;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600823 int i;
Allen Hubbe578b8812015-05-21 02:51:39 -0400824
825 if (ntb_db_is_unsafe(ntb))
826 dev_dbg(&ntb->dev, "doorbell is unsafe\n");
827
828 if (ntb_spad_is_unsafe(ntb))
829 dev_dbg(&ntb->dev, "scratchpad is unsafe\n");
830
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600831 tc = kzalloc(sizeof(*tc), GFP_KERNEL);
Allen Hubbe578b8812015-05-21 02:51:39 -0400832 if (!tc) {
833 rc = -ENOMEM;
834 goto err_tc;
835 }
836
837 tc->ntb = ntb;
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600838
839 tc->mw_count = min(ntb_mw_count(tc->ntb), MAX_MWS);
840 for (i = 0; i < tc->mw_count; i++) {
841 rc = tool_init_mw(tc, i);
842 if (rc)
843 goto err_ctx;
844 }
Allen Hubbe578b8812015-05-21 02:51:39 -0400845
846 tool_setup_dbgfs(tc);
847
848 rc = ntb_set_ctx(ntb, tc, &tool_ops);
849 if (rc)
850 goto err_ctx;
851
852 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
853 ntb_link_event(ntb);
854
855 return 0;
856
857err_ctx:
Logan Gunthorpe717146a2016-06-20 13:15:09 -0600858 tool_free_mws(tc);
Allen Hubbe578b8812015-05-21 02:51:39 -0400859 debugfs_remove_recursive(tc->dbgfs);
860 kfree(tc);
861err_tc:
862 return rc;
863}
864
865static void tool_remove(struct ntb_client *self, struct ntb_dev *ntb)
866{
867 struct tool_ctx *tc = ntb->ctx;
868
Logan Gunthorpe8b71d282016-06-03 14:50:33 -0600869 tool_free_mws(tc);
870
Allen Hubbe578b8812015-05-21 02:51:39 -0400871 ntb_clear_ctx(ntb);
872 ntb_link_disable(ntb);
873
874 debugfs_remove_recursive(tc->dbgfs);
875 kfree(tc);
876}
877
878static struct ntb_client tool_client = {
879 .ops = {
880 .probe = tool_probe,
881 .remove = tool_remove,
882 },
883};
884
885static int __init tool_init(void)
886{
887 int rc;
888
889 if (debugfs_initialized())
890 tool_dbgfs = debugfs_create_dir(KBUILD_MODNAME, NULL);
891
892 rc = ntb_register_client(&tool_client);
893 if (rc)
894 goto err_client;
895
896 return 0;
897
898err_client:
899 debugfs_remove_recursive(tool_dbgfs);
900 return rc;
901}
902module_init(tool_init);
903
904static void __exit tool_exit(void)
905{
906 ntb_unregister_client(&tool_client);
907 debugfs_remove_recursive(tool_dbgfs);
908}
909module_exit(tool_exit);