blob: 05a870524a67b784fd31c5afe0cb72fa077150c4 [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;
138 struct dentry *debugfs_node_dir;
139 struct dentry *debugfs_run;
140 struct dentry *debugfs_threads;
141 u8 perf_threads;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600142 /* mutex ensures only one set of threads run at once */
143 struct mutex run_mutex;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700144 struct pthr_ctx pthr_ctx[MAX_THREADS];
145 atomic_t tsync;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600146 atomic_t tdone;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700147};
148
149enum {
150 VERSION = 0,
151 MW_SZ_HIGH,
152 MW_SZ_LOW,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700153 MAX_SPAD
154};
155
156static void perf_link_event(void *ctx)
157{
158 struct perf_ctx *perf = ctx;
159
160 if (ntb_link_is_up(perf->ntb, NULL, NULL) == 1)
161 schedule_delayed_work(&perf->link_work, 2*HZ);
162 else
163 schedule_work(&perf->link_cleanup);
164}
165
166static void perf_db_event(void *ctx, int vec)
167{
168 struct perf_ctx *perf = ctx;
169 u64 db_bits, db_mask;
170
171 db_mask = ntb_db_vector_mask(perf->ntb, vec);
172 db_bits = ntb_db_read(perf->ntb);
173
174 dev_dbg(&perf->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
175 vec, db_mask, db_bits);
176}
177
178static const struct ntb_ctx_ops perf_ops = {
179 .link_event = perf_link_event,
180 .db_event = perf_db_event,
181};
182
183static void perf_copy_callback(void *data)
184{
185 struct pthr_ctx *pctx = data;
186
187 atomic_dec(&pctx->dma_sync);
188}
189
Arnd Bergmann1985a882016-01-26 10:31:45 +0100190static ssize_t perf_copy(struct pthr_ctx *pctx, char __iomem *dst,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700191 char *src, size_t size)
192{
193 struct perf_ctx *perf = pctx->perf;
194 struct dma_async_tx_descriptor *txd;
195 struct dma_chan *chan = pctx->dma_chan;
196 struct dma_device *device;
197 struct dmaengine_unmap_data *unmap;
198 dma_cookie_t cookie;
199 size_t src_off, dst_off;
200 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100201 void __iomem *vbase;
202 void __iomem *dst_vaddr;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700203 dma_addr_t dst_phys;
204 int retries = 0;
205
206 if (!use_dma) {
207 memcpy_toio(dst, src, size);
208 return size;
209 }
210
211 if (!chan) {
212 dev_err(&perf->ntb->dev, "DMA engine does not exist\n");
213 return -EINVAL;
214 }
215
216 device = chan->device;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100217 src_off = (uintptr_t)src & ~PAGE_MASK;
218 dst_off = (uintptr_t __force)dst & ~PAGE_MASK;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700219
220 if (!is_dma_copy_aligned(device, src_off, dst_off, size))
221 return -ENODEV;
222
Arnd Bergmann1985a882016-01-26 10:31:45 +0100223 vbase = mw->vbase;
224 dst_vaddr = dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700225 dst_phys = mw->phys_addr + (dst_vaddr - vbase);
226
227 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
228 if (!unmap)
229 return -ENOMEM;
230
231 unmap->len = size;
232 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(src),
233 src_off, size, DMA_TO_DEVICE);
234 if (dma_mapping_error(device->dev, unmap->addr[0]))
235 goto err_get_unmap;
236
237 unmap->to_cnt = 1;
238
239 do {
240 txd = device->device_prep_dma_memcpy(chan, dst_phys,
241 unmap->addr[0],
242 size, DMA_PREP_INTERRUPT);
243 if (!txd) {
244 set_current_state(TASK_INTERRUPTIBLE);
245 schedule_timeout(DMA_OUT_RESOURCE_TO);
246 }
247 } while (!txd && (++retries < DMA_RETRIES));
248
249 if (!txd) {
250 pctx->dma_prep_err++;
251 goto err_get_unmap;
252 }
253
254 txd->callback = perf_copy_callback;
255 txd->callback_param = pctx;
256 dma_set_unmap(txd, unmap);
257
258 cookie = dmaengine_submit(txd);
259 if (dma_submit_error(cookie))
260 goto err_set_unmap;
261
262 atomic_inc(&pctx->dma_sync);
263 dma_async_issue_pending(chan);
264
265 return size;
266
267err_set_unmap:
268 dmaengine_unmap_put(unmap);
269err_get_unmap:
270 dmaengine_unmap_put(unmap);
271 return 0;
272}
273
Arnd Bergmann1985a882016-01-26 10:31:45 +0100274static int perf_move_data(struct pthr_ctx *pctx, char __iomem *dst, char *src,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700275 u64 buf_size, u64 win_size, u64 total)
276{
277 int chunks, total_chunks, i;
278 int copied_chunks = 0;
279 u64 copied = 0, result;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100280 char __iomem *tmp = dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700281 u64 perf, diff_us;
282 ktime_t kstart, kstop, kdiff;
Logan Gunthorpefd2ecd82016-06-20 13:15:04 -0600283 unsigned long last_sleep = jiffies;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700284
285 chunks = div64_u64(win_size, buf_size);
286 total_chunks = div64_u64(total, buf_size);
287 kstart = ktime_get();
288
289 for (i = 0; i < total_chunks; i++) {
290 result = perf_copy(pctx, tmp, src, buf_size);
291 copied += result;
292 copied_chunks++;
293 if (copied_chunks == chunks) {
294 tmp = dst;
295 copied_chunks = 0;
296 } else
297 tmp += buf_size;
298
Logan Gunthorpefd2ecd82016-06-20 13:15:04 -0600299 /* Probably should schedule every 5s to prevent soft hang. */
300 if (unlikely((jiffies - last_sleep) > 5 * HZ)) {
301 last_sleep = jiffies;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700302 set_current_state(TASK_INTERRUPTIBLE);
303 schedule_timeout(1);
304 }
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600305
306 if (unlikely(kthread_should_stop()))
307 break;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700308 }
309
310 if (use_dma) {
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600311 pr_debug("%s: All DMA descriptors submitted\n", current->comm);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600312 while (atomic_read(&pctx->dma_sync) != 0) {
313 if (kthread_should_stop())
314 break;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700315 msleep(20);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600316 }
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700317 }
318
319 kstop = ktime_get();
320 kdiff = ktime_sub(kstop, kstart);
321 diff_us = ktime_to_us(kdiff);
322
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600323 pr_debug("%s: copied %llu bytes\n", current->comm, copied);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700324
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600325 pr_debug("%s: lasted %llu usecs\n", current->comm, diff_us);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700326
327 perf = div64_u64(copied, diff_us);
328
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600329 pr_debug("%s: MBytes/s: %llu\n", current->comm, perf);
330
331 pctx->copied = copied;
332 pctx->diff_us = diff_us;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700333
334 return 0;
335}
336
337static bool perf_dma_filter_fn(struct dma_chan *chan, void *node)
338{
339 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
340}
341
342static int ntb_perf_thread(void *data)
343{
344 struct pthr_ctx *pctx = data;
345 struct perf_ctx *perf = pctx->perf;
346 struct pci_dev *pdev = perf->ntb->pdev;
347 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100348 char __iomem *dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700349 u64 win_size, buf_size, total;
350 void *src;
351 int rc, node, i;
352 struct dma_chan *dma_chan = NULL;
353
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600354 pr_debug("kthread %s starting...\n", current->comm);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700355
356 node = dev_to_node(&pdev->dev);
357
358 if (use_dma && !pctx->dma_chan) {
359 dma_cap_mask_t dma_mask;
360
361 dma_cap_zero(dma_mask);
362 dma_cap_set(DMA_MEMCPY, dma_mask);
363 dma_chan = dma_request_channel(dma_mask, perf_dma_filter_fn,
364 (void *)(unsigned long)node);
365 if (!dma_chan) {
366 pr_warn("%s: cannot acquire DMA channel, quitting\n",
367 current->comm);
368 return -ENODEV;
369 }
370 pctx->dma_chan = dma_chan;
371 }
372
373 for (i = 0; i < MAX_SRCS; i++) {
374 pctx->srcs[i] = kmalloc_node(MAX_TEST_SIZE, GFP_KERNEL, node);
375 if (!pctx->srcs[i]) {
376 rc = -ENOMEM;
377 goto err;
378 }
379 }
380
381 win_size = mw->phys_size;
382 buf_size = 1ULL << seg_order;
383 total = 1ULL << run_order;
384
385 if (buf_size > MAX_TEST_SIZE)
386 buf_size = MAX_TEST_SIZE;
387
Arnd Bergmann1985a882016-01-26 10:31:45 +0100388 dst = (char __iomem *)mw->vbase;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700389
390 atomic_inc(&perf->tsync);
391 while (atomic_read(&perf->tsync) != perf->perf_threads)
392 schedule();
393
394 src = pctx->srcs[pctx->src_idx];
395 pctx->src_idx = (pctx->src_idx + 1) & (MAX_SRCS - 1);
396
397 rc = perf_move_data(pctx, dst, src, buf_size, win_size, total);
398
399 atomic_dec(&perf->tsync);
400
401 if (rc < 0) {
402 pr_err("%s: failed\n", current->comm);
403 rc = -ENXIO;
404 goto err;
405 }
406
407 for (i = 0; i < MAX_SRCS; i++) {
408 kfree(pctx->srcs[i]);
409 pctx->srcs[i] = NULL;
410 }
411
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600412 atomic_inc(&perf->tdone);
413 wake_up(pctx->wq);
414 rc = 0;
415 goto done;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700416
417err:
418 for (i = 0; i < MAX_SRCS; i++) {
419 kfree(pctx->srcs[i]);
420 pctx->srcs[i] = NULL;
421 }
422
423 if (dma_chan) {
424 dma_release_channel(dma_chan);
425 pctx->dma_chan = NULL;
426 }
427
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600428done:
429 /* Wait until we are told to stop */
430 for (;;) {
431 set_current_state(TASK_INTERRUPTIBLE);
432 if (kthread_should_stop())
433 break;
434 schedule();
435 }
436 __set_current_state(TASK_RUNNING);
437
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700438 return rc;
439}
440
441static void perf_free_mw(struct perf_ctx *perf)
442{
443 struct perf_mw *mw = &perf->mw;
444 struct pci_dev *pdev = perf->ntb->pdev;
445
446 if (!mw->virt_addr)
447 return;
448
449 ntb_mw_clear_trans(perf->ntb, 0);
450 dma_free_coherent(&pdev->dev, mw->buf_size,
451 mw->virt_addr, mw->dma_addr);
452 mw->xlat_size = 0;
453 mw->buf_size = 0;
454 mw->virt_addr = NULL;
455}
456
457static int perf_set_mw(struct perf_ctx *perf, resource_size_t size)
458{
459 struct perf_mw *mw = &perf->mw;
460 size_t xlat_size, buf_size;
Dave Jiangee5f7502016-03-07 15:57:25 -0700461 int rc;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700462
463 if (!size)
464 return -EINVAL;
465
466 xlat_size = round_up(size, mw->xlat_align_size);
467 buf_size = round_up(size, mw->xlat_align);
468
469 if (mw->xlat_size == xlat_size)
470 return 0;
471
472 if (mw->buf_size)
473 perf_free_mw(perf);
474
475 mw->xlat_size = xlat_size;
476 mw->buf_size = buf_size;
477
478 mw->virt_addr = dma_alloc_coherent(&perf->ntb->pdev->dev, buf_size,
479 &mw->dma_addr, GFP_KERNEL);
480 if (!mw->virt_addr) {
481 mw->xlat_size = 0;
482 mw->buf_size = 0;
483 }
484
Dave Jiangee5f7502016-03-07 15:57:25 -0700485 rc = ntb_mw_set_trans(perf->ntb, 0, mw->dma_addr, mw->xlat_size);
486 if (rc) {
487 dev_err(&perf->ntb->dev, "Unable to set mw0 translation\n");
488 perf_free_mw(perf);
489 return -EIO;
490 }
491
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700492 return 0;
493}
494
495static void perf_link_work(struct work_struct *work)
496{
497 struct perf_ctx *perf =
498 container_of(work, struct perf_ctx, link_work.work);
499 struct ntb_dev *ndev = perf->ntb;
500 struct pci_dev *pdev = ndev->pdev;
501 u32 val;
502 u64 size;
503 int rc;
504
505 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
506
507 size = perf->mw.phys_size;
Logan Gunthorpe4aae9772016-06-03 14:50:31 -0600508
509 if (max_mw_size && size > max_mw_size)
510 size = max_mw_size;
511
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700512 ntb_peer_spad_write(ndev, MW_SZ_HIGH, upper_32_bits(size));
513 ntb_peer_spad_write(ndev, MW_SZ_LOW, lower_32_bits(size));
514 ntb_peer_spad_write(ndev, VERSION, PERF_VERSION);
515
516 /* now read what peer wrote */
517 val = ntb_spad_read(ndev, VERSION);
518 if (val != PERF_VERSION) {
519 dev_dbg(&pdev->dev, "Remote version = %#x\n", val);
520 goto out;
521 }
522
523 val = ntb_spad_read(ndev, MW_SZ_HIGH);
524 size = (u64)val << 32;
525
526 val = ntb_spad_read(ndev, MW_SZ_LOW);
527 size |= val;
528
529 dev_dbg(&pdev->dev, "Remote MW size = %#llx\n", size);
530
531 rc = perf_set_mw(perf, size);
532 if (rc)
533 goto out1;
534
535 perf->link_is_up = true;
536
537 return;
538
539out1:
540 perf_free_mw(perf);
541
542out:
543 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
544 schedule_delayed_work(&perf->link_work,
545 msecs_to_jiffies(PERF_LINK_DOWN_TIMEOUT));
546}
547
548static void perf_link_cleanup(struct work_struct *work)
549{
550 struct perf_ctx *perf = container_of(work,
551 struct perf_ctx,
552 link_cleanup);
553
554 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
555
556 if (!perf->link_is_up)
557 cancel_delayed_work_sync(&perf->link_work);
558}
559
560static int perf_setup_mw(struct ntb_dev *ntb, struct perf_ctx *perf)
561{
562 struct perf_mw *mw;
563 int rc;
564
565 mw = &perf->mw;
566
567 rc = ntb_mw_get_range(ntb, 0, &mw->phys_addr, &mw->phys_size,
568 &mw->xlat_align, &mw->xlat_align_size);
569 if (rc)
570 return rc;
571
572 perf->mw.vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
573 if (!mw->vbase)
574 return -ENOMEM;
575
576 return 0;
577}
578
579static ssize_t debugfs_run_read(struct file *filp, char __user *ubuf,
580 size_t count, loff_t *offp)
581{
582 struct perf_ctx *perf = filp->private_data;
583 char *buf;
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600584 ssize_t ret, out_off = 0;
585 struct pthr_ctx *pctx;
586 int i;
587 u64 rate;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700588
589 if (!perf)
590 return 0;
591
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600592 buf = kmalloc(1024, GFP_KERNEL);
Sudip Mukherjee2572c7f2016-03-10 17:51:11 +0530593 if (!buf)
594 return -ENOMEM;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600595
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600596 if (mutex_is_locked(&perf->run_mutex)) {
597 out_off = snprintf(buf, 64, "running\n");
598 goto read_from_buf;
599 }
600
601 for (i = 0; i < MAX_THREADS; i++) {
602 pctx = &perf->pthr_ctx[i];
603
604 if (pctx->status == -ENODATA)
605 break;
606
607 if (pctx->status) {
608 out_off += snprintf(buf + out_off, 1024 - out_off,
609 "%d: error %d\n", i,
610 pctx->status);
611 continue;
612 }
613
614 rate = div64_u64(pctx->copied, pctx->diff_us);
615 out_off += snprintf(buf + out_off, 1024 - out_off,
616 "%d: copied %llu bytes in %llu usecs, %llu MBytes/s\n",
617 i, pctx->copied, pctx->diff_us, rate);
618 }
619
620read_from_buf:
621 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_off);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700622 kfree(buf);
623
624 return ret;
625}
626
Dave Jiang838850e2016-03-18 16:39:47 -0700627static void threads_cleanup(struct perf_ctx *perf)
628{
629 struct pthr_ctx *pctx;
630 int i;
631
Dave Jiang838850e2016-03-18 16:39:47 -0700632 for (i = 0; i < MAX_THREADS; i++) {
633 pctx = &perf->pthr_ctx[i];
634 if (pctx->thread) {
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600635 pctx->status = kthread_stop(pctx->thread);
Dave Jiang838850e2016-03-18 16:39:47 -0700636 pctx->thread = NULL;
637 }
638 }
639}
640
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600641static void perf_clear_thread_status(struct perf_ctx *perf)
642{
643 int i;
644
645 for (i = 0; i < MAX_THREADS; i++)
646 perf->pthr_ctx[i].status = -ENODATA;
647}
648
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700649static ssize_t debugfs_run_write(struct file *filp, const char __user *ubuf,
650 size_t count, loff_t *offp)
651{
652 struct perf_ctx *perf = filp->private_data;
653 int node, i;
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600654 DECLARE_WAIT_QUEUE_HEAD(wq);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700655
656 if (!perf->link_is_up)
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600657 return -ENOLINK;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700658
659 if (perf->perf_threads == 0)
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600660 return -EINVAL;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700661
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600662 if (!mutex_trylock(&perf->run_mutex))
663 return -EBUSY;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700664
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600665 perf_clear_thread_status(perf);
666
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600667 if (perf->perf_threads > MAX_THREADS) {
668 perf->perf_threads = MAX_THREADS;
669 pr_info("Reset total threads to: %u\n", MAX_THREADS);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700670 }
671
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600672 /* no greater than 1M */
673 if (seg_order > MAX_SEG_ORDER) {
674 seg_order = MAX_SEG_ORDER;
675 pr_info("Fix seg_order to %u\n", seg_order);
676 }
677
678 if (run_order < seg_order) {
679 run_order = seg_order;
680 pr_info("Fix run_order to %u\n", run_order);
681 }
682
683 node = dev_to_node(&perf->ntb->pdev->dev);
684 atomic_set(&perf->tdone, 0);
685
686 /* launch kernel thread */
687 for (i = 0; i < perf->perf_threads; i++) {
688 struct pthr_ctx *pctx;
689
690 pctx = &perf->pthr_ctx[i];
691 atomic_set(&pctx->dma_sync, 0);
692 pctx->perf = perf;
693 pctx->wq = &wq;
694 pctx->thread =
695 kthread_create_on_node(ntb_perf_thread,
696 (void *)pctx,
697 node, "ntb_perf %d", i);
698 if (IS_ERR(pctx->thread)) {
699 pctx->thread = NULL;
700 goto err;
701 } else {
702 wake_up_process(pctx->thread);
703 }
704 }
705
706 wait_event_interruptible(wq,
707 atomic_read(&perf->tdone) == perf->perf_threads);
708
709 threads_cleanup(perf);
710 mutex_unlock(&perf->run_mutex);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700711 return count;
Dave Jiang838850e2016-03-18 16:39:47 -0700712
713err:
714 threads_cleanup(perf);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600715 mutex_unlock(&perf->run_mutex);
Dave Jiang838850e2016-03-18 16:39:47 -0700716 return -ENXIO;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700717}
718
719static const struct file_operations ntb_perf_debugfs_run = {
720 .owner = THIS_MODULE,
721 .open = simple_open,
722 .read = debugfs_run_read,
723 .write = debugfs_run_write,
724};
725
726static int perf_debugfs_setup(struct perf_ctx *perf)
727{
728 struct pci_dev *pdev = perf->ntb->pdev;
729
730 if (!debugfs_initialized())
731 return -ENODEV;
732
733 if (!perf_debugfs_dir) {
734 perf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
735 if (!perf_debugfs_dir)
736 return -ENODEV;
737 }
738
739 perf->debugfs_node_dir = debugfs_create_dir(pci_name(pdev),
740 perf_debugfs_dir);
741 if (!perf->debugfs_node_dir)
742 return -ENODEV;
743
744 perf->debugfs_run = debugfs_create_file("run", S_IRUSR | S_IWUSR,
745 perf->debugfs_node_dir, perf,
746 &ntb_perf_debugfs_run);
747 if (!perf->debugfs_run)
748 return -ENODEV;
749
750 perf->debugfs_threads = debugfs_create_u8("threads", S_IRUSR | S_IWUSR,
751 perf->debugfs_node_dir,
752 &perf->perf_threads);
753 if (!perf->debugfs_threads)
754 return -ENODEV;
755
756 return 0;
757}
758
759static int perf_probe(struct ntb_client *client, struct ntb_dev *ntb)
760{
761 struct pci_dev *pdev = ntb->pdev;
762 struct perf_ctx *perf;
763 int node;
764 int rc = 0;
765
Logan Gunthorpe19645a02016-06-07 11:20:22 -0600766 if (ntb_spad_count(ntb) < MAX_SPAD) {
767 dev_err(&ntb->dev, "Not enough scratch pad registers for %s",
768 DRIVER_NAME);
769 return -EIO;
770 }
771
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700772 node = dev_to_node(&pdev->dev);
773
774 perf = kzalloc_node(sizeof(*perf), GFP_KERNEL, node);
775 if (!perf) {
776 rc = -ENOMEM;
777 goto err_perf;
778 }
779
780 perf->ntb = ntb;
781 perf->perf_threads = 1;
782 atomic_set(&perf->tsync, 0);
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600783 mutex_init(&perf->run_mutex);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700784 spin_lock_init(&perf->db_lock);
785 perf_setup_mw(ntb, perf);
786 INIT_DELAYED_WORK(&perf->link_work, perf_link_work);
787 INIT_WORK(&perf->link_cleanup, perf_link_cleanup);
788
789 rc = ntb_set_ctx(ntb, perf, &perf_ops);
790 if (rc)
791 goto err_ctx;
792
793 perf->link_is_up = false;
794 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
795 ntb_link_event(ntb);
796
797 rc = perf_debugfs_setup(perf);
798 if (rc)
799 goto err_ctx;
800
Logan Gunthorpe58fd0f32016-06-20 13:15:06 -0600801 perf_clear_thread_status(perf);
802
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700803 return 0;
804
805err_ctx:
806 cancel_delayed_work_sync(&perf->link_work);
807 cancel_work_sync(&perf->link_cleanup);
808 kfree(perf);
809err_perf:
810 return rc;
811}
812
813static void perf_remove(struct ntb_client *client, struct ntb_dev *ntb)
814{
815 struct perf_ctx *perf = ntb->ctx;
816 int i;
817
818 dev_dbg(&perf->ntb->dev, "%s called\n", __func__);
819
Logan Gunthorpeda573ea2016-06-20 13:15:05 -0600820 mutex_lock(&perf->run_mutex);
821
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700822 cancel_delayed_work_sync(&perf->link_work);
823 cancel_work_sync(&perf->link_cleanup);
824
825 ntb_clear_ctx(ntb);
826 ntb_link_disable(ntb);
827
828 debugfs_remove_recursive(perf_debugfs_dir);
829 perf_debugfs_dir = NULL;
830
831 if (use_dma) {
832 for (i = 0; i < MAX_THREADS; i++) {
833 struct pthr_ctx *pctx = &perf->pthr_ctx[i];
834
835 if (pctx->dma_chan)
836 dma_release_channel(pctx->dma_chan);
837 }
838 }
839
840 kfree(perf);
841}
842
843static struct ntb_client perf_client = {
844 .ops = {
845 .probe = perf_probe,
846 .remove = perf_remove,
847 },
848};
849module_ntb_client(perf_client);