blob: f0784e50ceb75658967eb7b3570e18ccfa6f2979 [file] [log] [blame]
Dave Jiang8a7b6a72016-01-13 13:29:48 -07001/*
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 Intel 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 * BSD LICENSE
14 *
15 * Copyright(c) 2015 Intel Corporation. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copy
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * * Neither the name of Intel Corporation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 *
43 * PCIe NTB Perf Linux driver
44 */
45
46#include <linux/init.h>
47#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/kthread.h>
50#include <linux/time.h>
51#include <linux/timer.h>
52#include <linux/dma-mapping.h>
53#include <linux/pci.h>
54#include <linux/slab.h>
55#include <linux/spinlock.h>
56#include <linux/debugfs.h>
57#include <linux/dmaengine.h>
58#include <linux/delay.h>
59#include <linux/sizes.h>
60#include <linux/ntb.h>
Logan Gunthorpeda573ea2016-06-20 13:15:05 -060061#include <linux/mutex.h>
Dave Jiang8a7b6a72016-01-13 13:29:48 -070062
63#define DRIVER_NAME "ntb_perf"
64#define DRIVER_DESCRIPTION "PCIe NTB Performance Measurement Tool"
65
66#define DRIVER_LICENSE "Dual BSD/GPL"
67#define DRIVER_VERSION "1.0"
68#define DRIVER_AUTHOR "Dave Jiang <dave.jiang@intel.com>"
69
70#define PERF_LINK_DOWN_TIMEOUT 10
71#define PERF_VERSION 0xffff0001
72#define MAX_THREADS 32
73#define MAX_TEST_SIZE SZ_1M
74#define MAX_SRCS 32
75#define DMA_OUT_RESOURCE_TO 50
76#define DMA_RETRIES 20
77#define SZ_4G (1ULL << 32)
78#define MAX_SEG_ORDER 20 /* no larger than 1M for kmalloc buffer */
79
80MODULE_LICENSE(DRIVER_LICENSE);
81MODULE_VERSION(DRIVER_VERSION);
82MODULE_AUTHOR(DRIVER_AUTHOR);
83MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
84
85static struct dentry *perf_debugfs_dir;
86
Logan Gunthorpe4aae9772016-06-03 14:50:31 -060087static unsigned long max_mw_size;
88module_param(max_mw_size, ulong, 0644);
89MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
90
Dave Jiang8a7b6a72016-01-13 13:29:48 -070091static unsigned int seg_order = 19; /* 512K */
92module_param(seg_order, uint, 0644);
93MODULE_PARM_DESC(seg_order, "size order [n^2] of buffer segment for testing");
94
95static unsigned int run_order = 32; /* 4G */
96module_param(run_order, uint, 0644);
97MODULE_PARM_DESC(run_order, "size order [n^2] of total data to transfer");
98
99static bool use_dma; /* default to 0 */
100module_param(use_dma, bool, 0644);
101MODULE_PARM_DESC(use_dma, "Using DMA engine to measure performance");
102
103struct perf_mw {
104 phys_addr_t phys_addr;
105 resource_size_t phys_size;
106 resource_size_t xlat_align;
107 resource_size_t xlat_align_size;
108 void __iomem *vbase;
109 size_t xlat_size;
110 size_t buf_size;
111 void *virt_addr;
112 dma_addr_t dma_addr;
113};
114
115struct perf_ctx;
116
117struct pthr_ctx {
118 struct task_struct *thread;
119 struct perf_ctx *perf;
120 atomic_t dma_sync;
121 struct dma_chan *dma_chan;
122 int dma_prep_err;
123 int src_idx;
124 void *srcs[MAX_SRCS];
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600125 wait_queue_head_t *wq;
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600126 int status;
127 u64 copied;
128 u64 diff_us;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700129};
130
131struct perf_ctx {
132 struct ntb_dev *ntb;
133 spinlock_t db_lock;
134 struct perf_mw mw;
135 bool link_is_up;
136 struct work_struct link_cleanup;
137 struct delayed_work link_work;
Logan Gunthorpe26dc6382016-06-20 13:15:07 -0600138 wait_queue_head_t link_wq;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700139 struct dentry *debugfs_node_dir;
140 struct dentry *debugfs_run;
141 struct dentry *debugfs_threads;
142 u8 perf_threads;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600143 /* mutex ensures only one set of threads run at once */
144 struct mutex run_mutex;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700145 struct pthr_ctx pthr_ctx[MAX_THREADS];
146 atomic_t tsync;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600147 atomic_t tdone;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700148};
149
150enum {
151 VERSION = 0,
152 MW_SZ_HIGH,
153 MW_SZ_LOW,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700154 MAX_SPAD
155};
156
157static void perf_link_event(void *ctx)
158{
159 struct perf_ctx *perf = ctx;
160
161 if (ntb_link_is_up(perf->ntb, NULL, NULL) == 1)
162 schedule_delayed_work(&perf->link_work, 2*HZ);
163 else
164 schedule_work(&perf->link_cleanup);
165}
166
167static void perf_db_event(void *ctx, int vec)
168{
169 struct perf_ctx *perf = ctx;
170 u64 db_bits, db_mask;
171
172 db_mask = ntb_db_vector_mask(perf->ntb, vec);
173 db_bits = ntb_db_read(perf->ntb);
174
175 dev_dbg(&perf->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
176 vec, db_mask, db_bits);
177}
178
179static const struct ntb_ctx_ops perf_ops = {
180 .link_event = perf_link_event,
181 .db_event = perf_db_event,
182};
183
184static void perf_copy_callback(void *data)
185{
186 struct pthr_ctx *pctx = data;
187
188 atomic_dec(&pctx->dma_sync);
189}
190
Arnd Bergmann1985a882016-01-26 10:31:45 +0100191static ssize_t perf_copy(struct pthr_ctx *pctx, char __iomem *dst,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700192 char *src, size_t size)
193{
194 struct perf_ctx *perf = pctx->perf;
195 struct dma_async_tx_descriptor *txd;
196 struct dma_chan *chan = pctx->dma_chan;
197 struct dma_device *device;
198 struct dmaengine_unmap_data *unmap;
199 dma_cookie_t cookie;
200 size_t src_off, dst_off;
201 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100202 void __iomem *vbase;
203 void __iomem *dst_vaddr;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700204 dma_addr_t dst_phys;
205 int retries = 0;
206
207 if (!use_dma) {
208 memcpy_toio(dst, src, size);
209 return size;
210 }
211
212 if (!chan) {
213 dev_err(&perf->ntb->dev, "DMA engine does not exist\n");
214 return -EINVAL;
215 }
216
217 device = chan->device;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100218 src_off = (uintptr_t)src & ~PAGE_MASK;
219 dst_off = (uintptr_t __force)dst & ~PAGE_MASK;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700220
221 if (!is_dma_copy_aligned(device, src_off, dst_off, size))
222 return -ENODEV;
223
Arnd Bergmann1985a882016-01-26 10:31:45 +0100224 vbase = mw->vbase;
225 dst_vaddr = dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700226 dst_phys = mw->phys_addr + (dst_vaddr - vbase);
227
228 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
229 if (!unmap)
230 return -ENOMEM;
231
232 unmap->len = size;
233 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(src),
234 src_off, size, DMA_TO_DEVICE);
235 if (dma_mapping_error(device->dev, unmap->addr[0]))
236 goto err_get_unmap;
237
238 unmap->to_cnt = 1;
239
240 do {
241 txd = device->device_prep_dma_memcpy(chan, dst_phys,
242 unmap->addr[0],
243 size, DMA_PREP_INTERRUPT);
244 if (!txd) {
245 set_current_state(TASK_INTERRUPTIBLE);
246 schedule_timeout(DMA_OUT_RESOURCE_TO);
247 }
248 } while (!txd && (++retries < DMA_RETRIES));
249
250 if (!txd) {
251 pctx->dma_prep_err++;
252 goto err_get_unmap;
253 }
254
255 txd->callback = perf_copy_callback;
256 txd->callback_param = pctx;
257 dma_set_unmap(txd, unmap);
258
259 cookie = dmaengine_submit(txd);
260 if (dma_submit_error(cookie))
261 goto err_set_unmap;
262
263 atomic_inc(&pctx->dma_sync);
264 dma_async_issue_pending(chan);
265
266 return size;
267
268err_set_unmap:
269 dmaengine_unmap_put(unmap);
270err_get_unmap:
271 dmaengine_unmap_put(unmap);
272 return 0;
273}
274
Arnd Bergmann1985a882016-01-26 10:31:45 +0100275static int perf_move_data(struct pthr_ctx *pctx, char __iomem *dst, char *src,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700276 u64 buf_size, u64 win_size, u64 total)
277{
278 int chunks, total_chunks, i;
279 int copied_chunks = 0;
280 u64 copied = 0, result;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100281 char __iomem *tmp = dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700282 u64 perf, diff_us;
283 ktime_t kstart, kstop, kdiff;
Logan Gunthorpefd2ecd82016-06-20 13:15:04 -0600284 unsigned long last_sleep = jiffies;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700285
286 chunks = div64_u64(win_size, buf_size);
287 total_chunks = div64_u64(total, buf_size);
288 kstart = ktime_get();
289
290 for (i = 0; i < total_chunks; i++) {
291 result = perf_copy(pctx, tmp, src, buf_size);
292 copied += result;
293 copied_chunks++;
294 if (copied_chunks == chunks) {
295 tmp = dst;
296 copied_chunks = 0;
297 } else
298 tmp += buf_size;
299
Logan Gunthorpefd2ecd82016-06-20 13:15:04 -0600300 /* Probably should schedule every 5s to prevent soft hang. */
301 if (unlikely((jiffies - last_sleep) > 5 * HZ)) {
302 last_sleep = jiffies;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700303 set_current_state(TASK_INTERRUPTIBLE);
304 schedule_timeout(1);
305 }
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600306
307 if (unlikely(kthread_should_stop()))
308 break;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700309 }
310
311 if (use_dma) {
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600312 pr_debug("%s: All DMA descriptors submitted\n", current->comm);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600313 while (atomic_read(&pctx->dma_sync) != 0) {
314 if (kthread_should_stop())
315 break;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700316 msleep(20);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600317 }
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700318 }
319
320 kstop = ktime_get();
321 kdiff = ktime_sub(kstop, kstart);
322 diff_us = ktime_to_us(kdiff);
323
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600324 pr_debug("%s: copied %llu bytes\n", current->comm, copied);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700325
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600326 pr_debug("%s: lasted %llu usecs\n", current->comm, diff_us);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700327
328 perf = div64_u64(copied, diff_us);
329
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600330 pr_debug("%s: MBytes/s: %llu\n", current->comm, perf);
331
332 pctx->copied = copied;
333 pctx->diff_us = diff_us;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700334
335 return 0;
336}
337
338static bool perf_dma_filter_fn(struct dma_chan *chan, void *node)
339{
340 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
341}
342
343static int ntb_perf_thread(void *data)
344{
345 struct pthr_ctx *pctx = data;
346 struct perf_ctx *perf = pctx->perf;
347 struct pci_dev *pdev = perf->ntb->pdev;
348 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100349 char __iomem *dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700350 u64 win_size, buf_size, total;
351 void *src;
352 int rc, node, i;
353 struct dma_chan *dma_chan = NULL;
354
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600355 pr_debug("kthread %s starting...\n", current->comm);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700356
357 node = dev_to_node(&pdev->dev);
358
359 if (use_dma && !pctx->dma_chan) {
360 dma_cap_mask_t dma_mask;
361
362 dma_cap_zero(dma_mask);
363 dma_cap_set(DMA_MEMCPY, dma_mask);
364 dma_chan = dma_request_channel(dma_mask, perf_dma_filter_fn,
365 (void *)(unsigned long)node);
366 if (!dma_chan) {
367 pr_warn("%s: cannot acquire DMA channel, quitting\n",
368 current->comm);
369 return -ENODEV;
370 }
371 pctx->dma_chan = dma_chan;
372 }
373
374 for (i = 0; i < MAX_SRCS; i++) {
375 pctx->srcs[i] = kmalloc_node(MAX_TEST_SIZE, GFP_KERNEL, node);
376 if (!pctx->srcs[i]) {
377 rc = -ENOMEM;
378 goto err;
379 }
380 }
381
382 win_size = mw->phys_size;
383 buf_size = 1ULL << seg_order;
384 total = 1ULL << run_order;
385
386 if (buf_size > MAX_TEST_SIZE)
387 buf_size = MAX_TEST_SIZE;
388
Arnd Bergmann1985a882016-01-26 10:31:45 +0100389 dst = (char __iomem *)mw->vbase;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700390
391 atomic_inc(&perf->tsync);
392 while (atomic_read(&perf->tsync) != perf->perf_threads)
393 schedule();
394
395 src = pctx->srcs[pctx->src_idx];
396 pctx->src_idx = (pctx->src_idx + 1) & (MAX_SRCS - 1);
397
398 rc = perf_move_data(pctx, dst, src, buf_size, win_size, total);
399
400 atomic_dec(&perf->tsync);
401
402 if (rc < 0) {
403 pr_err("%s: failed\n", current->comm);
404 rc = -ENXIO;
405 goto err;
406 }
407
408 for (i = 0; i < MAX_SRCS; i++) {
409 kfree(pctx->srcs[i]);
410 pctx->srcs[i] = NULL;
411 }
412
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600413 atomic_inc(&perf->tdone);
414 wake_up(pctx->wq);
415 rc = 0;
416 goto done;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700417
418err:
419 for (i = 0; i < MAX_SRCS; i++) {
420 kfree(pctx->srcs[i]);
421 pctx->srcs[i] = NULL;
422 }
423
424 if (dma_chan) {
425 dma_release_channel(dma_chan);
426 pctx->dma_chan = NULL;
427 }
428
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600429done:
430 /* Wait until we are told to stop */
431 for (;;) {
432 set_current_state(TASK_INTERRUPTIBLE);
433 if (kthread_should_stop())
434 break;
435 schedule();
436 }
437 __set_current_state(TASK_RUNNING);
438
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700439 return rc;
440}
441
442static void perf_free_mw(struct perf_ctx *perf)
443{
444 struct perf_mw *mw = &perf->mw;
445 struct pci_dev *pdev = perf->ntb->pdev;
446
447 if (!mw->virt_addr)
448 return;
449
450 ntb_mw_clear_trans(perf->ntb, 0);
451 dma_free_coherent(&pdev->dev, mw->buf_size,
452 mw->virt_addr, mw->dma_addr);
453 mw->xlat_size = 0;
454 mw->buf_size = 0;
455 mw->virt_addr = NULL;
456}
457
458static int perf_set_mw(struct perf_ctx *perf, resource_size_t size)
459{
460 struct perf_mw *mw = &perf->mw;
461 size_t xlat_size, buf_size;
Dave Jiangee5f7502016-03-07 15:57:25 -0700462 int rc;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700463
464 if (!size)
465 return -EINVAL;
466
467 xlat_size = round_up(size, mw->xlat_align_size);
468 buf_size = round_up(size, mw->xlat_align);
469
470 if (mw->xlat_size == xlat_size)
471 return 0;
472
473 if (mw->buf_size)
474 perf_free_mw(perf);
475
476 mw->xlat_size = xlat_size;
477 mw->buf_size = buf_size;
478
479 mw->virt_addr = dma_alloc_coherent(&perf->ntb->pdev->dev, buf_size,
480 &mw->dma_addr, GFP_KERNEL);
481 if (!mw->virt_addr) {
482 mw->xlat_size = 0;
483 mw->buf_size = 0;
484 }
485
Dave Jiangee5f7502016-03-07 15:57:25 -0700486 rc = ntb_mw_set_trans(perf->ntb, 0, mw->dma_addr, mw->xlat_size);
487 if (rc) {
488 dev_err(&perf->ntb->dev, "Unable to set mw0 translation\n");
489 perf_free_mw(perf);
490 return -EIO;
491 }
492
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700493 return 0;
494}
495
496static void perf_link_work(struct work_struct *work)
497{
498 struct perf_ctx *perf =
499 container_of(work, struct perf_ctx, link_work.work);
500 struct ntb_dev *ndev = perf->ntb;
501 struct pci_dev *pdev = ndev->pdev;
502 u32 val;
503 u64 size;
504 int rc;
505
506 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
507
508 size = perf->mw.phys_size;
Logan Gunthorpe4aae9772016-06-03 14:50:31 -0600509
510 if (max_mw_size && size > max_mw_size)
511 size = max_mw_size;
512
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700513 ntb_peer_spad_write(ndev, MW_SZ_HIGH, upper_32_bits(size));
514 ntb_peer_spad_write(ndev, MW_SZ_LOW, lower_32_bits(size));
515 ntb_peer_spad_write(ndev, VERSION, PERF_VERSION);
516
517 /* now read what peer wrote */
518 val = ntb_spad_read(ndev, VERSION);
519 if (val != PERF_VERSION) {
520 dev_dbg(&pdev->dev, "Remote version = %#x\n", val);
521 goto out;
522 }
523
524 val = ntb_spad_read(ndev, MW_SZ_HIGH);
525 size = (u64)val << 32;
526
527 val = ntb_spad_read(ndev, MW_SZ_LOW);
528 size |= val;
529
530 dev_dbg(&pdev->dev, "Remote MW size = %#llx\n", size);
531
532 rc = perf_set_mw(perf, size);
533 if (rc)
534 goto out1;
535
536 perf->link_is_up = true;
Logan Gunthorpe26dc6382016-06-20 13:15:07 -0600537 wake_up(&perf->link_wq);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700538
539 return;
540
541out1:
542 perf_free_mw(perf);
543
544out:
545 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
546 schedule_delayed_work(&perf->link_work,
547 msecs_to_jiffies(PERF_LINK_DOWN_TIMEOUT));
548}
549
550static void perf_link_cleanup(struct work_struct *work)
551{
552 struct perf_ctx *perf = container_of(work,
553 struct perf_ctx,
554 link_cleanup);
555
556 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
557
558 if (!perf->link_is_up)
559 cancel_delayed_work_sync(&perf->link_work);
560}
561
562static int perf_setup_mw(struct ntb_dev *ntb, struct perf_ctx *perf)
563{
564 struct perf_mw *mw;
565 int rc;
566
567 mw = &perf->mw;
568
569 rc = ntb_mw_get_range(ntb, 0, &mw->phys_addr, &mw->phys_size,
570 &mw->xlat_align, &mw->xlat_align_size);
571 if (rc)
572 return rc;
573
574 perf->mw.vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
575 if (!mw->vbase)
576 return -ENOMEM;
577
578 return 0;
579}
580
581static ssize_t debugfs_run_read(struct file *filp, char __user *ubuf,
582 size_t count, loff_t *offp)
583{
584 struct perf_ctx *perf = filp->private_data;
585 char *buf;
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600586 ssize_t ret, out_off = 0;
587 struct pthr_ctx *pctx;
588 int i;
589 u64 rate;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700590
591 if (!perf)
592 return 0;
593
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600594 buf = kmalloc(1024, GFP_KERNEL);
Sudip Mukherjee2572c7f2016-03-10 17:51:11 +0530595 if (!buf)
596 return -ENOMEM;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600597
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600598 if (mutex_is_locked(&perf->run_mutex)) {
599 out_off = snprintf(buf, 64, "running\n");
600 goto read_from_buf;
601 }
602
603 for (i = 0; i < MAX_THREADS; i++) {
604 pctx = &perf->pthr_ctx[i];
605
606 if (pctx->status == -ENODATA)
607 break;
608
609 if (pctx->status) {
610 out_off += snprintf(buf + out_off, 1024 - out_off,
611 "%d: error %d\n", i,
612 pctx->status);
613 continue;
614 }
615
616 rate = div64_u64(pctx->copied, pctx->diff_us);
617 out_off += snprintf(buf + out_off, 1024 - out_off,
618 "%d: copied %llu bytes in %llu usecs, %llu MBytes/s\n",
619 i, pctx->copied, pctx->diff_us, rate);
620 }
621
622read_from_buf:
623 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_off);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700624 kfree(buf);
625
626 return ret;
627}
628
Dave Jiang838850e2016-03-18 16:39:47 -0700629static void threads_cleanup(struct perf_ctx *perf)
630{
631 struct pthr_ctx *pctx;
632 int i;
633
Dave Jiang838850e2016-03-18 16:39:47 -0700634 for (i = 0; i < MAX_THREADS; i++) {
635 pctx = &perf->pthr_ctx[i];
636 if (pctx->thread) {
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600637 pctx->status = kthread_stop(pctx->thread);
Dave Jiang838850e2016-03-18 16:39:47 -0700638 pctx->thread = NULL;
639 }
640 }
641}
642
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600643static void perf_clear_thread_status(struct perf_ctx *perf)
644{
645 int i;
646
647 for (i = 0; i < MAX_THREADS; i++)
648 perf->pthr_ctx[i].status = -ENODATA;
649}
650
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700651static ssize_t debugfs_run_write(struct file *filp, const char __user *ubuf,
652 size_t count, loff_t *offp)
653{
654 struct perf_ctx *perf = filp->private_data;
655 int node, i;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600656 DECLARE_WAIT_QUEUE_HEAD(wq);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700657
Logan Gunthorpe26dc6382016-06-20 13:15:07 -0600658 if (wait_event_interruptible(perf->link_wq, perf->link_is_up))
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600659 return -ENOLINK;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700660
661 if (perf->perf_threads == 0)
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600662 return -EINVAL;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700663
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600664 if (!mutex_trylock(&perf->run_mutex))
665 return -EBUSY;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700666
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600667 perf_clear_thread_status(perf);
668
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600669 if (perf->perf_threads > MAX_THREADS) {
670 perf->perf_threads = MAX_THREADS;
671 pr_info("Reset total threads to: %u\n", MAX_THREADS);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700672 }
673
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600674 /* no greater than 1M */
675 if (seg_order > MAX_SEG_ORDER) {
676 seg_order = MAX_SEG_ORDER;
677 pr_info("Fix seg_order to %u\n", seg_order);
678 }
679
680 if (run_order < seg_order) {
681 run_order = seg_order;
682 pr_info("Fix run_order to %u\n", run_order);
683 }
684
685 node = dev_to_node(&perf->ntb->pdev->dev);
686 atomic_set(&perf->tdone, 0);
687
688 /* launch kernel thread */
689 for (i = 0; i < perf->perf_threads; i++) {
690 struct pthr_ctx *pctx;
691
692 pctx = &perf->pthr_ctx[i];
693 atomic_set(&pctx->dma_sync, 0);
694 pctx->perf = perf;
695 pctx->wq = &wq;
696 pctx->thread =
697 kthread_create_on_node(ntb_perf_thread,
698 (void *)pctx,
699 node, "ntb_perf %d", i);
700 if (IS_ERR(pctx->thread)) {
701 pctx->thread = NULL;
702 goto err;
703 } else {
704 wake_up_process(pctx->thread);
705 }
706 }
707
708 wait_event_interruptible(wq,
709 atomic_read(&perf->tdone) == perf->perf_threads);
710
711 threads_cleanup(perf);
712 mutex_unlock(&perf->run_mutex);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700713 return count;
Dave Jiang838850e2016-03-18 16:39:47 -0700714
715err:
716 threads_cleanup(perf);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600717 mutex_unlock(&perf->run_mutex);
Dave Jiang838850e2016-03-18 16:39:47 -0700718 return -ENXIO;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700719}
720
721static const struct file_operations ntb_perf_debugfs_run = {
722 .owner = THIS_MODULE,
723 .open = simple_open,
724 .read = debugfs_run_read,
725 .write = debugfs_run_write,
726};
727
728static int perf_debugfs_setup(struct perf_ctx *perf)
729{
730 struct pci_dev *pdev = perf->ntb->pdev;
731
732 if (!debugfs_initialized())
733 return -ENODEV;
734
735 if (!perf_debugfs_dir) {
736 perf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
737 if (!perf_debugfs_dir)
738 return -ENODEV;
739 }
740
741 perf->debugfs_node_dir = debugfs_create_dir(pci_name(pdev),
742 perf_debugfs_dir);
743 if (!perf->debugfs_node_dir)
744 return -ENODEV;
745
746 perf->debugfs_run = debugfs_create_file("run", S_IRUSR | S_IWUSR,
747 perf->debugfs_node_dir, perf,
748 &ntb_perf_debugfs_run);
749 if (!perf->debugfs_run)
750 return -ENODEV;
751
752 perf->debugfs_threads = debugfs_create_u8("threads", S_IRUSR | S_IWUSR,
753 perf->debugfs_node_dir,
754 &perf->perf_threads);
755 if (!perf->debugfs_threads)
756 return -ENODEV;
757
758 return 0;
759}
760
761static int perf_probe(struct ntb_client *client, struct ntb_dev *ntb)
762{
763 struct pci_dev *pdev = ntb->pdev;
764 struct perf_ctx *perf;
765 int node;
766 int rc = 0;
767
Logan Gunthorpe19645a02016-06-07 11:20:22 -0600768 if (ntb_spad_count(ntb) < MAX_SPAD) {
769 dev_err(&ntb->dev, "Not enough scratch pad registers for %s",
770 DRIVER_NAME);
771 return -EIO;
772 }
773
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700774 node = dev_to_node(&pdev->dev);
775
776 perf = kzalloc_node(sizeof(*perf), GFP_KERNEL, node);
777 if (!perf) {
778 rc = -ENOMEM;
779 goto err_perf;
780 }
781
782 perf->ntb = ntb;
783 perf->perf_threads = 1;
784 atomic_set(&perf->tsync, 0);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600785 mutex_init(&perf->run_mutex);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700786 spin_lock_init(&perf->db_lock);
787 perf_setup_mw(ntb, perf);
Logan Gunthorpe26dc6382016-06-20 13:15:07 -0600788 init_waitqueue_head(&perf->link_wq);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700789 INIT_DELAYED_WORK(&perf->link_work, perf_link_work);
790 INIT_WORK(&perf->link_cleanup, perf_link_cleanup);
791
792 rc = ntb_set_ctx(ntb, perf, &perf_ops);
793 if (rc)
794 goto err_ctx;
795
796 perf->link_is_up = false;
797 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
798 ntb_link_event(ntb);
799
800 rc = perf_debugfs_setup(perf);
801 if (rc)
802 goto err_ctx;
803
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600804 perf_clear_thread_status(perf);
805
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700806 return 0;
807
808err_ctx:
809 cancel_delayed_work_sync(&perf->link_work);
810 cancel_work_sync(&perf->link_cleanup);
811 kfree(perf);
812err_perf:
813 return rc;
814}
815
816static void perf_remove(struct ntb_client *client, struct ntb_dev *ntb)
817{
818 struct perf_ctx *perf = ntb->ctx;
819 int i;
820
821 dev_dbg(&perf->ntb->dev, "%s called\n", __func__);
822
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600823 mutex_lock(&perf->run_mutex);
824
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700825 cancel_delayed_work_sync(&perf->link_work);
826 cancel_work_sync(&perf->link_cleanup);
827
828 ntb_clear_ctx(ntb);
829 ntb_link_disable(ntb);
830
831 debugfs_remove_recursive(perf_debugfs_dir);
832 perf_debugfs_dir = NULL;
833
834 if (use_dma) {
835 for (i = 0; i < MAX_THREADS; i++) {
836 struct pthr_ctx *pctx = &perf->pthr_ctx[i];
837
838 if (pctx->dma_chan)
839 dma_release_channel(pctx->dma_chan);
840 }
841 }
842
843 kfree(perf);
844}
845
846static struct ntb_client perf_client = {
847 .ops = {
848 .probe = perf_probe,
849 .remove = perf_remove,
850 },
851};
852module_ntb_client(perf_client);