blob: 5008ccf600a959756ddf63dd0b708d3d0229597b [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>
61
62#define DRIVER_NAME "ntb_perf"
63#define DRIVER_DESCRIPTION "PCIe NTB Performance Measurement Tool"
64
65#define DRIVER_LICENSE "Dual BSD/GPL"
66#define DRIVER_VERSION "1.0"
67#define DRIVER_AUTHOR "Dave Jiang <dave.jiang@intel.com>"
68
69#define PERF_LINK_DOWN_TIMEOUT 10
70#define PERF_VERSION 0xffff0001
71#define MAX_THREADS 32
72#define MAX_TEST_SIZE SZ_1M
73#define MAX_SRCS 32
74#define DMA_OUT_RESOURCE_TO 50
75#define DMA_RETRIES 20
76#define SZ_4G (1ULL << 32)
77#define MAX_SEG_ORDER 20 /* no larger than 1M for kmalloc buffer */
78
79MODULE_LICENSE(DRIVER_LICENSE);
80MODULE_VERSION(DRIVER_VERSION);
81MODULE_AUTHOR(DRIVER_AUTHOR);
82MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
83
84static struct dentry *perf_debugfs_dir;
85
Logan Gunthorpe4aae9772016-06-03 14:50:31 -060086static unsigned long max_mw_size;
87module_param(max_mw_size, ulong, 0644);
88MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
89
Dave Jiang8a7b6a72016-01-13 13:29:48 -070090static unsigned int seg_order = 19; /* 512K */
91module_param(seg_order, uint, 0644);
92MODULE_PARM_DESC(seg_order, "size order [n^2] of buffer segment for testing");
93
94static unsigned int run_order = 32; /* 4G */
95module_param(run_order, uint, 0644);
96MODULE_PARM_DESC(run_order, "size order [n^2] of total data to transfer");
97
98static bool use_dma; /* default to 0 */
99module_param(use_dma, bool, 0644);
100MODULE_PARM_DESC(use_dma, "Using DMA engine to measure performance");
101
102struct perf_mw {
103 phys_addr_t phys_addr;
104 resource_size_t phys_size;
105 resource_size_t xlat_align;
106 resource_size_t xlat_align_size;
107 void __iomem *vbase;
108 size_t xlat_size;
109 size_t buf_size;
110 void *virt_addr;
111 dma_addr_t dma_addr;
112};
113
114struct perf_ctx;
115
116struct pthr_ctx {
117 struct task_struct *thread;
118 struct perf_ctx *perf;
119 atomic_t dma_sync;
120 struct dma_chan *dma_chan;
121 int dma_prep_err;
122 int src_idx;
123 void *srcs[MAX_SRCS];
124};
125
126struct perf_ctx {
127 struct ntb_dev *ntb;
128 spinlock_t db_lock;
129 struct perf_mw mw;
130 bool link_is_up;
131 struct work_struct link_cleanup;
132 struct delayed_work link_work;
133 struct dentry *debugfs_node_dir;
134 struct dentry *debugfs_run;
135 struct dentry *debugfs_threads;
136 u8 perf_threads;
137 bool run;
138 struct pthr_ctx pthr_ctx[MAX_THREADS];
139 atomic_t tsync;
140};
141
142enum {
143 VERSION = 0,
144 MW_SZ_HIGH,
145 MW_SZ_LOW,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700146 MAX_SPAD
147};
148
149static void perf_link_event(void *ctx)
150{
151 struct perf_ctx *perf = ctx;
152
153 if (ntb_link_is_up(perf->ntb, NULL, NULL) == 1)
154 schedule_delayed_work(&perf->link_work, 2*HZ);
155 else
156 schedule_work(&perf->link_cleanup);
157}
158
159static void perf_db_event(void *ctx, int vec)
160{
161 struct perf_ctx *perf = ctx;
162 u64 db_bits, db_mask;
163
164 db_mask = ntb_db_vector_mask(perf->ntb, vec);
165 db_bits = ntb_db_read(perf->ntb);
166
167 dev_dbg(&perf->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
168 vec, db_mask, db_bits);
169}
170
171static const struct ntb_ctx_ops perf_ops = {
172 .link_event = perf_link_event,
173 .db_event = perf_db_event,
174};
175
176static void perf_copy_callback(void *data)
177{
178 struct pthr_ctx *pctx = data;
179
180 atomic_dec(&pctx->dma_sync);
181}
182
Arnd Bergmann1985a882016-01-26 10:31:45 +0100183static ssize_t perf_copy(struct pthr_ctx *pctx, char __iomem *dst,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700184 char *src, size_t size)
185{
186 struct perf_ctx *perf = pctx->perf;
187 struct dma_async_tx_descriptor *txd;
188 struct dma_chan *chan = pctx->dma_chan;
189 struct dma_device *device;
190 struct dmaengine_unmap_data *unmap;
191 dma_cookie_t cookie;
192 size_t src_off, dst_off;
193 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100194 void __iomem *vbase;
195 void __iomem *dst_vaddr;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700196 dma_addr_t dst_phys;
197 int retries = 0;
198
199 if (!use_dma) {
200 memcpy_toio(dst, src, size);
201 return size;
202 }
203
204 if (!chan) {
205 dev_err(&perf->ntb->dev, "DMA engine does not exist\n");
206 return -EINVAL;
207 }
208
209 device = chan->device;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100210 src_off = (uintptr_t)src & ~PAGE_MASK;
211 dst_off = (uintptr_t __force)dst & ~PAGE_MASK;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700212
213 if (!is_dma_copy_aligned(device, src_off, dst_off, size))
214 return -ENODEV;
215
Arnd Bergmann1985a882016-01-26 10:31:45 +0100216 vbase = mw->vbase;
217 dst_vaddr = dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700218 dst_phys = mw->phys_addr + (dst_vaddr - vbase);
219
220 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
221 if (!unmap)
222 return -ENOMEM;
223
224 unmap->len = size;
225 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(src),
226 src_off, size, DMA_TO_DEVICE);
227 if (dma_mapping_error(device->dev, unmap->addr[0]))
228 goto err_get_unmap;
229
230 unmap->to_cnt = 1;
231
232 do {
233 txd = device->device_prep_dma_memcpy(chan, dst_phys,
234 unmap->addr[0],
235 size, DMA_PREP_INTERRUPT);
236 if (!txd) {
237 set_current_state(TASK_INTERRUPTIBLE);
238 schedule_timeout(DMA_OUT_RESOURCE_TO);
239 }
240 } while (!txd && (++retries < DMA_RETRIES));
241
242 if (!txd) {
243 pctx->dma_prep_err++;
244 goto err_get_unmap;
245 }
246
247 txd->callback = perf_copy_callback;
248 txd->callback_param = pctx;
249 dma_set_unmap(txd, unmap);
250
251 cookie = dmaengine_submit(txd);
252 if (dma_submit_error(cookie))
253 goto err_set_unmap;
254
255 atomic_inc(&pctx->dma_sync);
256 dma_async_issue_pending(chan);
257
258 return size;
259
260err_set_unmap:
261 dmaengine_unmap_put(unmap);
262err_get_unmap:
263 dmaengine_unmap_put(unmap);
264 return 0;
265}
266
Arnd Bergmann1985a882016-01-26 10:31:45 +0100267static int perf_move_data(struct pthr_ctx *pctx, char __iomem *dst, char *src,
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700268 u64 buf_size, u64 win_size, u64 total)
269{
270 int chunks, total_chunks, i;
271 int copied_chunks = 0;
272 u64 copied = 0, result;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100273 char __iomem *tmp = dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700274 u64 perf, diff_us;
275 ktime_t kstart, kstop, kdiff;
Logan Gunthorpefd2ecd82016-06-20 13:15:04 -0600276 unsigned long last_sleep = jiffies;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700277
278 chunks = div64_u64(win_size, buf_size);
279 total_chunks = div64_u64(total, buf_size);
280 kstart = ktime_get();
281
282 for (i = 0; i < total_chunks; i++) {
283 result = perf_copy(pctx, tmp, src, buf_size);
284 copied += result;
285 copied_chunks++;
286 if (copied_chunks == chunks) {
287 tmp = dst;
288 copied_chunks = 0;
289 } else
290 tmp += buf_size;
291
Logan Gunthorpefd2ecd82016-06-20 13:15:04 -0600292 /* Probably should schedule every 5s to prevent soft hang. */
293 if (unlikely((jiffies - last_sleep) > 5 * HZ)) {
294 last_sleep = jiffies;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700295 set_current_state(TASK_INTERRUPTIBLE);
296 schedule_timeout(1);
297 }
298 }
299
300 if (use_dma) {
301 pr_info("%s: All DMA descriptors submitted\n", current->comm);
302 while (atomic_read(&pctx->dma_sync) != 0)
303 msleep(20);
304 }
305
306 kstop = ktime_get();
307 kdiff = ktime_sub(kstop, kstart);
308 diff_us = ktime_to_us(kdiff);
309
310 pr_info("%s: copied %llu bytes\n", current->comm, copied);
311
312 pr_info("%s: lasted %llu usecs\n", current->comm, diff_us);
313
314 perf = div64_u64(copied, diff_us);
315
316 pr_info("%s: MBytes/s: %llu\n", current->comm, perf);
317
318 return 0;
319}
320
321static bool perf_dma_filter_fn(struct dma_chan *chan, void *node)
322{
323 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
324}
325
326static int ntb_perf_thread(void *data)
327{
328 struct pthr_ctx *pctx = data;
329 struct perf_ctx *perf = pctx->perf;
330 struct pci_dev *pdev = perf->ntb->pdev;
331 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100332 char __iomem *dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700333 u64 win_size, buf_size, total;
334 void *src;
335 int rc, node, i;
336 struct dma_chan *dma_chan = NULL;
337
338 pr_info("kthread %s starting...\n", current->comm);
339
340 node = dev_to_node(&pdev->dev);
341
342 if (use_dma && !pctx->dma_chan) {
343 dma_cap_mask_t dma_mask;
344
345 dma_cap_zero(dma_mask);
346 dma_cap_set(DMA_MEMCPY, dma_mask);
347 dma_chan = dma_request_channel(dma_mask, perf_dma_filter_fn,
348 (void *)(unsigned long)node);
349 if (!dma_chan) {
350 pr_warn("%s: cannot acquire DMA channel, quitting\n",
351 current->comm);
352 return -ENODEV;
353 }
354 pctx->dma_chan = dma_chan;
355 }
356
357 for (i = 0; i < MAX_SRCS; i++) {
358 pctx->srcs[i] = kmalloc_node(MAX_TEST_SIZE, GFP_KERNEL, node);
359 if (!pctx->srcs[i]) {
360 rc = -ENOMEM;
361 goto err;
362 }
363 }
364
365 win_size = mw->phys_size;
366 buf_size = 1ULL << seg_order;
367 total = 1ULL << run_order;
368
369 if (buf_size > MAX_TEST_SIZE)
370 buf_size = MAX_TEST_SIZE;
371
Arnd Bergmann1985a882016-01-26 10:31:45 +0100372 dst = (char __iomem *)mw->vbase;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700373
374 atomic_inc(&perf->tsync);
375 while (atomic_read(&perf->tsync) != perf->perf_threads)
376 schedule();
377
378 src = pctx->srcs[pctx->src_idx];
379 pctx->src_idx = (pctx->src_idx + 1) & (MAX_SRCS - 1);
380
381 rc = perf_move_data(pctx, dst, src, buf_size, win_size, total);
382
383 atomic_dec(&perf->tsync);
384
385 if (rc < 0) {
386 pr_err("%s: failed\n", current->comm);
387 rc = -ENXIO;
388 goto err;
389 }
390
391 for (i = 0; i < MAX_SRCS; i++) {
392 kfree(pctx->srcs[i]);
393 pctx->srcs[i] = NULL;
394 }
395
396 return 0;
397
398err:
399 for (i = 0; i < MAX_SRCS; i++) {
400 kfree(pctx->srcs[i]);
401 pctx->srcs[i] = NULL;
402 }
403
404 if (dma_chan) {
405 dma_release_channel(dma_chan);
406 pctx->dma_chan = NULL;
407 }
408
409 return rc;
410}
411
412static void perf_free_mw(struct perf_ctx *perf)
413{
414 struct perf_mw *mw = &perf->mw;
415 struct pci_dev *pdev = perf->ntb->pdev;
416
417 if (!mw->virt_addr)
418 return;
419
420 ntb_mw_clear_trans(perf->ntb, 0);
421 dma_free_coherent(&pdev->dev, mw->buf_size,
422 mw->virt_addr, mw->dma_addr);
423 mw->xlat_size = 0;
424 mw->buf_size = 0;
425 mw->virt_addr = NULL;
426}
427
428static int perf_set_mw(struct perf_ctx *perf, resource_size_t size)
429{
430 struct perf_mw *mw = &perf->mw;
431 size_t xlat_size, buf_size;
Dave Jiangee5f7502016-03-07 15:57:25 -0700432 int rc;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700433
434 if (!size)
435 return -EINVAL;
436
437 xlat_size = round_up(size, mw->xlat_align_size);
438 buf_size = round_up(size, mw->xlat_align);
439
440 if (mw->xlat_size == xlat_size)
441 return 0;
442
443 if (mw->buf_size)
444 perf_free_mw(perf);
445
446 mw->xlat_size = xlat_size;
447 mw->buf_size = buf_size;
448
449 mw->virt_addr = dma_alloc_coherent(&perf->ntb->pdev->dev, buf_size,
450 &mw->dma_addr, GFP_KERNEL);
451 if (!mw->virt_addr) {
452 mw->xlat_size = 0;
453 mw->buf_size = 0;
454 }
455
Dave Jiangee5f7502016-03-07 15:57:25 -0700456 rc = ntb_mw_set_trans(perf->ntb, 0, mw->dma_addr, mw->xlat_size);
457 if (rc) {
458 dev_err(&perf->ntb->dev, "Unable to set mw0 translation\n");
459 perf_free_mw(perf);
460 return -EIO;
461 }
462
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700463 return 0;
464}
465
466static void perf_link_work(struct work_struct *work)
467{
468 struct perf_ctx *perf =
469 container_of(work, struct perf_ctx, link_work.work);
470 struct ntb_dev *ndev = perf->ntb;
471 struct pci_dev *pdev = ndev->pdev;
472 u32 val;
473 u64 size;
474 int rc;
475
476 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
477
478 size = perf->mw.phys_size;
Logan Gunthorpe4aae9772016-06-03 14:50:31 -0600479
480 if (max_mw_size && size > max_mw_size)
481 size = max_mw_size;
482
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700483 ntb_peer_spad_write(ndev, MW_SZ_HIGH, upper_32_bits(size));
484 ntb_peer_spad_write(ndev, MW_SZ_LOW, lower_32_bits(size));
485 ntb_peer_spad_write(ndev, VERSION, PERF_VERSION);
486
487 /* now read what peer wrote */
488 val = ntb_spad_read(ndev, VERSION);
489 if (val != PERF_VERSION) {
490 dev_dbg(&pdev->dev, "Remote version = %#x\n", val);
491 goto out;
492 }
493
494 val = ntb_spad_read(ndev, MW_SZ_HIGH);
495 size = (u64)val << 32;
496
497 val = ntb_spad_read(ndev, MW_SZ_LOW);
498 size |= val;
499
500 dev_dbg(&pdev->dev, "Remote MW size = %#llx\n", size);
501
502 rc = perf_set_mw(perf, size);
503 if (rc)
504 goto out1;
505
506 perf->link_is_up = true;
507
508 return;
509
510out1:
511 perf_free_mw(perf);
512
513out:
514 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
515 schedule_delayed_work(&perf->link_work,
516 msecs_to_jiffies(PERF_LINK_DOWN_TIMEOUT));
517}
518
519static void perf_link_cleanup(struct work_struct *work)
520{
521 struct perf_ctx *perf = container_of(work,
522 struct perf_ctx,
523 link_cleanup);
524
525 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
526
527 if (!perf->link_is_up)
528 cancel_delayed_work_sync(&perf->link_work);
529}
530
531static int perf_setup_mw(struct ntb_dev *ntb, struct perf_ctx *perf)
532{
533 struct perf_mw *mw;
534 int rc;
535
536 mw = &perf->mw;
537
538 rc = ntb_mw_get_range(ntb, 0, &mw->phys_addr, &mw->phys_size,
539 &mw->xlat_align, &mw->xlat_align_size);
540 if (rc)
541 return rc;
542
543 perf->mw.vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
544 if (!mw->vbase)
545 return -ENOMEM;
546
547 return 0;
548}
549
550static ssize_t debugfs_run_read(struct file *filp, char __user *ubuf,
551 size_t count, loff_t *offp)
552{
553 struct perf_ctx *perf = filp->private_data;
554 char *buf;
555 ssize_t ret, out_offset;
556
557 if (!perf)
558 return 0;
559
560 buf = kmalloc(64, GFP_KERNEL);
Sudip Mukherjee2572c7f2016-03-10 17:51:11 +0530561 if (!buf)
562 return -ENOMEM;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700563 out_offset = snprintf(buf, 64, "%d\n", perf->run);
564 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
565 kfree(buf);
566
567 return ret;
568}
569
Dave Jiang838850e2016-03-18 16:39:47 -0700570static void threads_cleanup(struct perf_ctx *perf)
571{
572 struct pthr_ctx *pctx;
573 int i;
574
575 perf->run = false;
576 for (i = 0; i < MAX_THREADS; i++) {
577 pctx = &perf->pthr_ctx[i];
578 if (pctx->thread) {
579 kthread_stop(pctx->thread);
580 pctx->thread = NULL;
581 }
582 }
583}
584
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700585static ssize_t debugfs_run_write(struct file *filp, const char __user *ubuf,
586 size_t count, loff_t *offp)
587{
588 struct perf_ctx *perf = filp->private_data;
589 int node, i;
590
591 if (!perf->link_is_up)
592 return 0;
593
594 if (perf->perf_threads == 0)
595 return 0;
596
597 if (atomic_read(&perf->tsync) == 0)
598 perf->run = false;
599
Dave Jiang838850e2016-03-18 16:39:47 -0700600 if (perf->run)
601 threads_cleanup(perf);
602 else {
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700603 perf->run = true;
604
605 if (perf->perf_threads > MAX_THREADS) {
606 perf->perf_threads = MAX_THREADS;
607 pr_info("Reset total threads to: %u\n", MAX_THREADS);
608 }
609
610 /* no greater than 1M */
611 if (seg_order > MAX_SEG_ORDER) {
612 seg_order = MAX_SEG_ORDER;
613 pr_info("Fix seg_order to %u\n", seg_order);
614 }
615
616 if (run_order < seg_order) {
617 run_order = seg_order;
618 pr_info("Fix run_order to %u\n", run_order);
619 }
620
621 node = dev_to_node(&perf->ntb->pdev->dev);
622 /* launch kernel thread */
623 for (i = 0; i < perf->perf_threads; i++) {
624 struct pthr_ctx *pctx;
625
626 pctx = &perf->pthr_ctx[i];
627 atomic_set(&pctx->dma_sync, 0);
628 pctx->perf = perf;
629 pctx->thread =
630 kthread_create_on_node(ntb_perf_thread,
631 (void *)pctx,
632 node, "ntb_perf %d", i);
Dave Jiangddc8f6f2016-03-18 16:39:41 -0700633 if (IS_ERR(pctx->thread)) {
Dave Jiang838850e2016-03-18 16:39:47 -0700634 pctx->thread = NULL;
635 goto err;
Dave Jiangddc8f6f2016-03-18 16:39:41 -0700636 } else
637 wake_up_process(pctx->thread);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700638
639 if (perf->run == false)
640 return -ENXIO;
641 }
642
643 }
644
645 return count;
Dave Jiang838850e2016-03-18 16:39:47 -0700646
647err:
648 threads_cleanup(perf);
649 return -ENXIO;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700650}
651
652static const struct file_operations ntb_perf_debugfs_run = {
653 .owner = THIS_MODULE,
654 .open = simple_open,
655 .read = debugfs_run_read,
656 .write = debugfs_run_write,
657};
658
659static int perf_debugfs_setup(struct perf_ctx *perf)
660{
661 struct pci_dev *pdev = perf->ntb->pdev;
662
663 if (!debugfs_initialized())
664 return -ENODEV;
665
666 if (!perf_debugfs_dir) {
667 perf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
668 if (!perf_debugfs_dir)
669 return -ENODEV;
670 }
671
672 perf->debugfs_node_dir = debugfs_create_dir(pci_name(pdev),
673 perf_debugfs_dir);
674 if (!perf->debugfs_node_dir)
675 return -ENODEV;
676
677 perf->debugfs_run = debugfs_create_file("run", S_IRUSR | S_IWUSR,
678 perf->debugfs_node_dir, perf,
679 &ntb_perf_debugfs_run);
680 if (!perf->debugfs_run)
681 return -ENODEV;
682
683 perf->debugfs_threads = debugfs_create_u8("threads", S_IRUSR | S_IWUSR,
684 perf->debugfs_node_dir,
685 &perf->perf_threads);
686 if (!perf->debugfs_threads)
687 return -ENODEV;
688
689 return 0;
690}
691
692static int perf_probe(struct ntb_client *client, struct ntb_dev *ntb)
693{
694 struct pci_dev *pdev = ntb->pdev;
695 struct perf_ctx *perf;
696 int node;
697 int rc = 0;
698
Logan Gunthorpe19645a02016-06-07 11:20:22 -0600699 if (ntb_spad_count(ntb) < MAX_SPAD) {
700 dev_err(&ntb->dev, "Not enough scratch pad registers for %s",
701 DRIVER_NAME);
702 return -EIO;
703 }
704
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700705 node = dev_to_node(&pdev->dev);
706
707 perf = kzalloc_node(sizeof(*perf), GFP_KERNEL, node);
708 if (!perf) {
709 rc = -ENOMEM;
710 goto err_perf;
711 }
712
713 perf->ntb = ntb;
714 perf->perf_threads = 1;
715 atomic_set(&perf->tsync, 0);
716 perf->run = false;
717 spin_lock_init(&perf->db_lock);
718 perf_setup_mw(ntb, perf);
719 INIT_DELAYED_WORK(&perf->link_work, perf_link_work);
720 INIT_WORK(&perf->link_cleanup, perf_link_cleanup);
721
722 rc = ntb_set_ctx(ntb, perf, &perf_ops);
723 if (rc)
724 goto err_ctx;
725
726 perf->link_is_up = false;
727 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
728 ntb_link_event(ntb);
729
730 rc = perf_debugfs_setup(perf);
731 if (rc)
732 goto err_ctx;
733
734 return 0;
735
736err_ctx:
737 cancel_delayed_work_sync(&perf->link_work);
738 cancel_work_sync(&perf->link_cleanup);
739 kfree(perf);
740err_perf:
741 return rc;
742}
743
744static void perf_remove(struct ntb_client *client, struct ntb_dev *ntb)
745{
746 struct perf_ctx *perf = ntb->ctx;
747 int i;
748
749 dev_dbg(&perf->ntb->dev, "%s called\n", __func__);
750
751 cancel_delayed_work_sync(&perf->link_work);
752 cancel_work_sync(&perf->link_cleanup);
753
754 ntb_clear_ctx(ntb);
755 ntb_link_disable(ntb);
756
757 debugfs_remove_recursive(perf_debugfs_dir);
758 perf_debugfs_dir = NULL;
759
760 if (use_dma) {
761 for (i = 0; i < MAX_THREADS; i++) {
762 struct pthr_ctx *pctx = &perf->pthr_ctx[i];
763
764 if (pctx->dma_chan)
765 dma_release_channel(pctx->dma_chan);
766 }
767 }
768
769 kfree(perf);
770}
771
772static struct ntb_client perf_client = {
773 .ops = {
774 .probe = perf_probe,
775 .remove = perf_remove,
776 },
777};
778module_ntb_client(perf_client);