ntb_pingpong: Add a debugfs file to get the ping count
[cascardo/linux.git] / drivers / ntb / test / ntb_perf.c
1 /*
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 #include <linux/mutex.h>
62
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
80 MODULE_LICENSE(DRIVER_LICENSE);
81 MODULE_VERSION(DRIVER_VERSION);
82 MODULE_AUTHOR(DRIVER_AUTHOR);
83 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
84
85 static struct dentry *perf_debugfs_dir;
86
87 static unsigned long max_mw_size;
88 module_param(max_mw_size, ulong, 0644);
89 MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
90
91 static unsigned int seg_order = 19; /* 512K */
92 module_param(seg_order, uint, 0644);
93 MODULE_PARM_DESC(seg_order, "size order [n^2] of buffer segment for testing");
94
95 static unsigned int run_order = 32; /* 4G */
96 module_param(run_order, uint, 0644);
97 MODULE_PARM_DESC(run_order, "size order [n^2] of total data to transfer");
98
99 static bool use_dma; /* default to 0 */
100 module_param(use_dma, bool, 0644);
101 MODULE_PARM_DESC(use_dma, "Using DMA engine to measure performance");
102
103 struct 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
115 struct perf_ctx;
116
117 struct 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];
125         wait_queue_head_t       *wq;
126         int                     status;
127         u64                     copied;
128         u64                     diff_us;
129 };
130
131 struct 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         wait_queue_head_t       link_wq;
139         struct dentry           *debugfs_node_dir;
140         struct dentry           *debugfs_run;
141         struct dentry           *debugfs_threads;
142         u8                      perf_threads;
143         /* mutex ensures only one set of threads run at once */
144         struct mutex            run_mutex;
145         struct pthr_ctx         pthr_ctx[MAX_THREADS];
146         atomic_t                tsync;
147         atomic_t                tdone;
148 };
149
150 enum {
151         VERSION = 0,
152         MW_SZ_HIGH,
153         MW_SZ_LOW,
154         MAX_SPAD
155 };
156
157 static 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
167 static 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
179 static const struct ntb_ctx_ops perf_ops = {
180         .link_event = perf_link_event,
181         .db_event = perf_db_event,
182 };
183
184 static void perf_copy_callback(void *data)
185 {
186         struct pthr_ctx *pctx = data;
187
188         atomic_dec(&pctx->dma_sync);
189 }
190
191 static ssize_t perf_copy(struct pthr_ctx *pctx, char __iomem *dst,
192                          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;
202         void __iomem *vbase;
203         void __iomem *dst_vaddr;
204         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;
218         src_off = (uintptr_t)src & ~PAGE_MASK;
219         dst_off = (uintptr_t __force)dst & ~PAGE_MASK;
220
221         if (!is_dma_copy_aligned(device, src_off, dst_off, size))
222                 return -ENODEV;
223
224         vbase = mw->vbase;
225         dst_vaddr = dst;
226         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
268 err_set_unmap:
269         dmaengine_unmap_put(unmap);
270 err_get_unmap:
271         dmaengine_unmap_put(unmap);
272         return 0;
273 }
274
275 static int perf_move_data(struct pthr_ctx *pctx, char __iomem *dst, char *src,
276                           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;
281         char __iomem *tmp = dst;
282         u64 perf, diff_us;
283         ktime_t kstart, kstop, kdiff;
284         unsigned long last_sleep = jiffies;
285
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
300                 /* Probably should schedule every 5s to prevent soft hang. */
301                 if (unlikely((jiffies - last_sleep) > 5 * HZ)) {
302                         last_sleep = jiffies;
303                         set_current_state(TASK_INTERRUPTIBLE);
304                         schedule_timeout(1);
305                 }
306
307                 if (unlikely(kthread_should_stop()))
308                         break;
309         }
310
311         if (use_dma) {
312                 pr_debug("%s: All DMA descriptors submitted\n", current->comm);
313                 while (atomic_read(&pctx->dma_sync) != 0) {
314                         if (kthread_should_stop())
315                                 break;
316                         msleep(20);
317                 }
318         }
319
320         kstop = ktime_get();
321         kdiff = ktime_sub(kstop, kstart);
322         diff_us = ktime_to_us(kdiff);
323
324         pr_debug("%s: copied %llu bytes\n", current->comm, copied);
325
326         pr_debug("%s: lasted %llu usecs\n", current->comm, diff_us);
327
328         perf = div64_u64(copied, diff_us);
329
330         pr_debug("%s: MBytes/s: %llu\n", current->comm, perf);
331
332         pctx->copied = copied;
333         pctx->diff_us = diff_us;
334
335         return 0;
336 }
337
338 static 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
343 static 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;
349         char __iomem *dst;
350         u64 win_size, buf_size, total;
351         void *src;
352         int rc, node, i;
353         struct dma_chan *dma_chan = NULL;
354
355         pr_debug("kthread %s starting...\n", current->comm);
356
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
389         dst = (char __iomem *)mw->vbase;
390
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
413         atomic_inc(&perf->tdone);
414         wake_up(pctx->wq);
415         rc = 0;
416         goto done;
417
418 err:
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
429 done:
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
439         return rc;
440 }
441
442 static 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
458 static 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;
462         int rc;
463
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
486         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
493         return 0;
494 }
495
496 static 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;
509
510         if (max_mw_size && size > max_mw_size)
511                 size = max_mw_size;
512
513         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;
537         wake_up(&perf->link_wq);
538
539         return;
540
541 out1:
542         perf_free_mw(perf);
543
544 out:
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
550 static 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
562 static 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
581 static 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;
586         ssize_t ret, out_off = 0;
587         struct pthr_ctx *pctx;
588         int i;
589         u64 rate;
590
591         if (!perf)
592                 return 0;
593
594         buf = kmalloc(1024, GFP_KERNEL);
595         if (!buf)
596                 return -ENOMEM;
597
598         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
622 read_from_buf:
623         ret = simple_read_from_buffer(ubuf, count, offp, buf, out_off);
624         kfree(buf);
625
626         return ret;
627 }
628
629 static void threads_cleanup(struct perf_ctx *perf)
630 {
631         struct pthr_ctx *pctx;
632         int i;
633
634         for (i = 0; i < MAX_THREADS; i++) {
635                 pctx = &perf->pthr_ctx[i];
636                 if (pctx->thread) {
637                         pctx->status = kthread_stop(pctx->thread);
638                         pctx->thread = NULL;
639                 }
640         }
641 }
642
643 static 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
651 static 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;
656         DECLARE_WAIT_QUEUE_HEAD(wq);
657
658         if (wait_event_interruptible(perf->link_wq, perf->link_is_up))
659                 return -ENOLINK;
660
661         if (perf->perf_threads == 0)
662                 return -EINVAL;
663
664         if (!mutex_trylock(&perf->run_mutex))
665                 return -EBUSY;
666
667         perf_clear_thread_status(perf);
668
669         if (perf->perf_threads > MAX_THREADS) {
670                 perf->perf_threads = MAX_THREADS;
671                 pr_info("Reset total threads to: %u\n", MAX_THREADS);
672         }
673
674         /* 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);
713         return count;
714
715 err:
716         threads_cleanup(perf);
717         mutex_unlock(&perf->run_mutex);
718         return -ENXIO;
719 }
720
721 static 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
728 static 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
761 static 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
768         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
774         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);
785         mutex_init(&perf->run_mutex);
786         spin_lock_init(&perf->db_lock);
787         perf_setup_mw(ntb, perf);
788         init_waitqueue_head(&perf->link_wq);
789         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
804         perf_clear_thread_status(perf);
805
806         return 0;
807
808 err_ctx:
809         cancel_delayed_work_sync(&perf->link_work);
810         cancel_work_sync(&perf->link_cleanup);
811         kfree(perf);
812 err_perf:
813         return rc;
814 }
815
816 static 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
823         mutex_lock(&perf->run_mutex);
824
825         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
846 static struct ntb_client perf_client = {
847         .ops = {
848                 .probe = perf_probe,
849                 .remove = perf_remove,
850         },
851 };
852 module_ntb_client(perf_client);