blob: 4368519da1023f12dced0af42b1f96ee47986091 [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;
276
277 chunks = div64_u64(win_size, buf_size);
278 total_chunks = div64_u64(total, buf_size);
279 kstart = ktime_get();
280
281 for (i = 0; i < total_chunks; i++) {
282 result = perf_copy(pctx, tmp, src, buf_size);
283 copied += result;
284 copied_chunks++;
285 if (copied_chunks == chunks) {
286 tmp = dst;
287 copied_chunks = 0;
288 } else
289 tmp += buf_size;
290
291 /* Probably should schedule every 4GB to prevent soft hang. */
292 if (((copied % SZ_4G) == 0) && !use_dma) {
293 set_current_state(TASK_INTERRUPTIBLE);
294 schedule_timeout(1);
295 }
296 }
297
298 if (use_dma) {
299 pr_info("%s: All DMA descriptors submitted\n", current->comm);
300 while (atomic_read(&pctx->dma_sync) != 0)
301 msleep(20);
302 }
303
304 kstop = ktime_get();
305 kdiff = ktime_sub(kstop, kstart);
306 diff_us = ktime_to_us(kdiff);
307
308 pr_info("%s: copied %llu bytes\n", current->comm, copied);
309
310 pr_info("%s: lasted %llu usecs\n", current->comm, diff_us);
311
312 perf = div64_u64(copied, diff_us);
313
314 pr_info("%s: MBytes/s: %llu\n", current->comm, perf);
315
316 return 0;
317}
318
319static bool perf_dma_filter_fn(struct dma_chan *chan, void *node)
320{
321 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
322}
323
324static int ntb_perf_thread(void *data)
325{
326 struct pthr_ctx *pctx = data;
327 struct perf_ctx *perf = pctx->perf;
328 struct pci_dev *pdev = perf->ntb->pdev;
329 struct perf_mw *mw = &perf->mw;
Arnd Bergmann1985a882016-01-26 10:31:45 +0100330 char __iomem *dst;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700331 u64 win_size, buf_size, total;
332 void *src;
333 int rc, node, i;
334 struct dma_chan *dma_chan = NULL;
335
336 pr_info("kthread %s starting...\n", current->comm);
337
338 node = dev_to_node(&pdev->dev);
339
340 if (use_dma && !pctx->dma_chan) {
341 dma_cap_mask_t dma_mask;
342
343 dma_cap_zero(dma_mask);
344 dma_cap_set(DMA_MEMCPY, dma_mask);
345 dma_chan = dma_request_channel(dma_mask, perf_dma_filter_fn,
346 (void *)(unsigned long)node);
347 if (!dma_chan) {
348 pr_warn("%s: cannot acquire DMA channel, quitting\n",
349 current->comm);
350 return -ENODEV;
351 }
352 pctx->dma_chan = dma_chan;
353 }
354
355 for (i = 0; i < MAX_SRCS; i++) {
356 pctx->srcs[i] = kmalloc_node(MAX_TEST_SIZE, GFP_KERNEL, node);
357 if (!pctx->srcs[i]) {
358 rc = -ENOMEM;
359 goto err;
360 }
361 }
362
363 win_size = mw->phys_size;
364 buf_size = 1ULL << seg_order;
365 total = 1ULL << run_order;
366
367 if (buf_size > MAX_TEST_SIZE)
368 buf_size = MAX_TEST_SIZE;
369
Arnd Bergmann1985a882016-01-26 10:31:45 +0100370 dst = (char __iomem *)mw->vbase;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700371
372 atomic_inc(&perf->tsync);
373 while (atomic_read(&perf->tsync) != perf->perf_threads)
374 schedule();
375
376 src = pctx->srcs[pctx->src_idx];
377 pctx->src_idx = (pctx->src_idx + 1) & (MAX_SRCS - 1);
378
379 rc = perf_move_data(pctx, dst, src, buf_size, win_size, total);
380
381 atomic_dec(&perf->tsync);
382
383 if (rc < 0) {
384 pr_err("%s: failed\n", current->comm);
385 rc = -ENXIO;
386 goto err;
387 }
388
389 for (i = 0; i < MAX_SRCS; i++) {
390 kfree(pctx->srcs[i]);
391 pctx->srcs[i] = NULL;
392 }
393
394 return 0;
395
396err:
397 for (i = 0; i < MAX_SRCS; i++) {
398 kfree(pctx->srcs[i]);
399 pctx->srcs[i] = NULL;
400 }
401
402 if (dma_chan) {
403 dma_release_channel(dma_chan);
404 pctx->dma_chan = NULL;
405 }
406
407 return rc;
408}
409
410static void perf_free_mw(struct perf_ctx *perf)
411{
412 struct perf_mw *mw = &perf->mw;
413 struct pci_dev *pdev = perf->ntb->pdev;
414
415 if (!mw->virt_addr)
416 return;
417
418 ntb_mw_clear_trans(perf->ntb, 0);
419 dma_free_coherent(&pdev->dev, mw->buf_size,
420 mw->virt_addr, mw->dma_addr);
421 mw->xlat_size = 0;
422 mw->buf_size = 0;
423 mw->virt_addr = NULL;
424}
425
426static int perf_set_mw(struct perf_ctx *perf, resource_size_t size)
427{
428 struct perf_mw *mw = &perf->mw;
429 size_t xlat_size, buf_size;
Dave Jiangee5f7502016-03-07 15:57:25 -0700430 int rc;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700431
432 if (!size)
433 return -EINVAL;
434
435 xlat_size = round_up(size, mw->xlat_align_size);
436 buf_size = round_up(size, mw->xlat_align);
437
438 if (mw->xlat_size == xlat_size)
439 return 0;
440
441 if (mw->buf_size)
442 perf_free_mw(perf);
443
444 mw->xlat_size = xlat_size;
445 mw->buf_size = buf_size;
446
447 mw->virt_addr = dma_alloc_coherent(&perf->ntb->pdev->dev, buf_size,
448 &mw->dma_addr, GFP_KERNEL);
449 if (!mw->virt_addr) {
450 mw->xlat_size = 0;
451 mw->buf_size = 0;
452 }
453
Dave Jiangee5f7502016-03-07 15:57:25 -0700454 rc = ntb_mw_set_trans(perf->ntb, 0, mw->dma_addr, mw->xlat_size);
455 if (rc) {
456 dev_err(&perf->ntb->dev, "Unable to set mw0 translation\n");
457 perf_free_mw(perf);
458 return -EIO;
459 }
460
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700461 return 0;
462}
463
464static void perf_link_work(struct work_struct *work)
465{
466 struct perf_ctx *perf =
467 container_of(work, struct perf_ctx, link_work.work);
468 struct ntb_dev *ndev = perf->ntb;
469 struct pci_dev *pdev = ndev->pdev;
470 u32 val;
471 u64 size;
472 int rc;
473
474 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
475
476 size = perf->mw.phys_size;
Logan Gunthorpe4aae9772016-06-03 14:50:31 -0600477
478 if (max_mw_size && size > max_mw_size)
479 size = max_mw_size;
480
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700481 ntb_peer_spad_write(ndev, MW_SZ_HIGH, upper_32_bits(size));
482 ntb_peer_spad_write(ndev, MW_SZ_LOW, lower_32_bits(size));
483 ntb_peer_spad_write(ndev, VERSION, PERF_VERSION);
484
485 /* now read what peer wrote */
486 val = ntb_spad_read(ndev, VERSION);
487 if (val != PERF_VERSION) {
488 dev_dbg(&pdev->dev, "Remote version = %#x\n", val);
489 goto out;
490 }
491
492 val = ntb_spad_read(ndev, MW_SZ_HIGH);
493 size = (u64)val << 32;
494
495 val = ntb_spad_read(ndev, MW_SZ_LOW);
496 size |= val;
497
498 dev_dbg(&pdev->dev, "Remote MW size = %#llx\n", size);
499
500 rc = perf_set_mw(perf, size);
501 if (rc)
502 goto out1;
503
504 perf->link_is_up = true;
505
506 return;
507
508out1:
509 perf_free_mw(perf);
510
511out:
512 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
513 schedule_delayed_work(&perf->link_work,
514 msecs_to_jiffies(PERF_LINK_DOWN_TIMEOUT));
515}
516
517static void perf_link_cleanup(struct work_struct *work)
518{
519 struct perf_ctx *perf = container_of(work,
520 struct perf_ctx,
521 link_cleanup);
522
523 dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
524
525 if (!perf->link_is_up)
526 cancel_delayed_work_sync(&perf->link_work);
527}
528
529static int perf_setup_mw(struct ntb_dev *ntb, struct perf_ctx *perf)
530{
531 struct perf_mw *mw;
532 int rc;
533
534 mw = &perf->mw;
535
536 rc = ntb_mw_get_range(ntb, 0, &mw->phys_addr, &mw->phys_size,
537 &mw->xlat_align, &mw->xlat_align_size);
538 if (rc)
539 return rc;
540
541 perf->mw.vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
542 if (!mw->vbase)
543 return -ENOMEM;
544
545 return 0;
546}
547
548static ssize_t debugfs_run_read(struct file *filp, char __user *ubuf,
549 size_t count, loff_t *offp)
550{
551 struct perf_ctx *perf = filp->private_data;
552 char *buf;
553 ssize_t ret, out_offset;
554
555 if (!perf)
556 return 0;
557
558 buf = kmalloc(64, GFP_KERNEL);
Sudip Mukherjee2572c7f2016-03-10 17:51:11 +0530559 if (!buf)
560 return -ENOMEM;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700561 out_offset = snprintf(buf, 64, "%d\n", perf->run);
562 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
563 kfree(buf);
564
565 return ret;
566}
567
Dave Jiang838850e2016-03-18 16:39:47 -0700568static void threads_cleanup(struct perf_ctx *perf)
569{
570 struct pthr_ctx *pctx;
571 int i;
572
573 perf->run = false;
574 for (i = 0; i < MAX_THREADS; i++) {
575 pctx = &perf->pthr_ctx[i];
576 if (pctx->thread) {
577 kthread_stop(pctx->thread);
578 pctx->thread = NULL;
579 }
580 }
581}
582
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700583static ssize_t debugfs_run_write(struct file *filp, const char __user *ubuf,
584 size_t count, loff_t *offp)
585{
586 struct perf_ctx *perf = filp->private_data;
587 int node, i;
588
589 if (!perf->link_is_up)
590 return 0;
591
592 if (perf->perf_threads == 0)
593 return 0;
594
595 if (atomic_read(&perf->tsync) == 0)
596 perf->run = false;
597
Dave Jiang838850e2016-03-18 16:39:47 -0700598 if (perf->run)
599 threads_cleanup(perf);
600 else {
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700601 perf->run = true;
602
603 if (perf->perf_threads > MAX_THREADS) {
604 perf->perf_threads = MAX_THREADS;
605 pr_info("Reset total threads to: %u\n", MAX_THREADS);
606 }
607
608 /* no greater than 1M */
609 if (seg_order > MAX_SEG_ORDER) {
610 seg_order = MAX_SEG_ORDER;
611 pr_info("Fix seg_order to %u\n", seg_order);
612 }
613
614 if (run_order < seg_order) {
615 run_order = seg_order;
616 pr_info("Fix run_order to %u\n", run_order);
617 }
618
619 node = dev_to_node(&perf->ntb->pdev->dev);
620 /* launch kernel thread */
621 for (i = 0; i < perf->perf_threads; i++) {
622 struct pthr_ctx *pctx;
623
624 pctx = &perf->pthr_ctx[i];
625 atomic_set(&pctx->dma_sync, 0);
626 pctx->perf = perf;
627 pctx->thread =
628 kthread_create_on_node(ntb_perf_thread,
629 (void *)pctx,
630 node, "ntb_perf %d", i);
Dave Jiangddc8f6f2016-03-18 16:39:41 -0700631 if (IS_ERR(pctx->thread)) {
Dave Jiang838850e2016-03-18 16:39:47 -0700632 pctx->thread = NULL;
633 goto err;
Dave Jiangddc8f6f2016-03-18 16:39:41 -0700634 } else
635 wake_up_process(pctx->thread);
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700636
637 if (perf->run == false)
638 return -ENXIO;
639 }
640
641 }
642
643 return count;
Dave Jiang838850e2016-03-18 16:39:47 -0700644
645err:
646 threads_cleanup(perf);
647 return -ENXIO;
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700648}
649
650static const struct file_operations ntb_perf_debugfs_run = {
651 .owner = THIS_MODULE,
652 .open = simple_open,
653 .read = debugfs_run_read,
654 .write = debugfs_run_write,
655};
656
657static int perf_debugfs_setup(struct perf_ctx *perf)
658{
659 struct pci_dev *pdev = perf->ntb->pdev;
660
661 if (!debugfs_initialized())
662 return -ENODEV;
663
664 if (!perf_debugfs_dir) {
665 perf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
666 if (!perf_debugfs_dir)
667 return -ENODEV;
668 }
669
670 perf->debugfs_node_dir = debugfs_create_dir(pci_name(pdev),
671 perf_debugfs_dir);
672 if (!perf->debugfs_node_dir)
673 return -ENODEV;
674
675 perf->debugfs_run = debugfs_create_file("run", S_IRUSR | S_IWUSR,
676 perf->debugfs_node_dir, perf,
677 &ntb_perf_debugfs_run);
678 if (!perf->debugfs_run)
679 return -ENODEV;
680
681 perf->debugfs_threads = debugfs_create_u8("threads", S_IRUSR | S_IWUSR,
682 perf->debugfs_node_dir,
683 &perf->perf_threads);
684 if (!perf->debugfs_threads)
685 return -ENODEV;
686
687 return 0;
688}
689
690static int perf_probe(struct ntb_client *client, struct ntb_dev *ntb)
691{
692 struct pci_dev *pdev = ntb->pdev;
693 struct perf_ctx *perf;
694 int node;
695 int rc = 0;
696
Logan Gunthorpe19645a02016-06-07 11:20:22 -0600697 if (ntb_spad_count(ntb) < MAX_SPAD) {
698 dev_err(&ntb->dev, "Not enough scratch pad registers for %s",
699 DRIVER_NAME);
700 return -EIO;
701 }
702
Dave Jiang8a7b6a72016-01-13 13:29:48 -0700703 node = dev_to_node(&pdev->dev);
704
705 perf = kzalloc_node(sizeof(*perf), GFP_KERNEL, node);
706 if (!perf) {
707 rc = -ENOMEM;
708 goto err_perf;
709 }
710
711 perf->ntb = ntb;
712 perf->perf_threads = 1;
713 atomic_set(&perf->tsync, 0);
714 perf->run = false;
715 spin_lock_init(&perf->db_lock);
716 perf_setup_mw(ntb, perf);
717 INIT_DELAYED_WORK(&perf->link_work, perf_link_work);
718 INIT_WORK(&perf->link_cleanup, perf_link_cleanup);
719
720 rc = ntb_set_ctx(ntb, perf, &perf_ops);
721 if (rc)
722 goto err_ctx;
723
724 perf->link_is_up = false;
725 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
726 ntb_link_event(ntb);
727
728 rc = perf_debugfs_setup(perf);
729 if (rc)
730 goto err_ctx;
731
732 return 0;
733
734err_ctx:
735 cancel_delayed_work_sync(&perf->link_work);
736 cancel_work_sync(&perf->link_cleanup);
737 kfree(perf);
738err_perf:
739 return rc;
740}
741
742static void perf_remove(struct ntb_client *client, struct ntb_dev *ntb)
743{
744 struct perf_ctx *perf = ntb->ctx;
745 int i;
746
747 dev_dbg(&perf->ntb->dev, "%s called\n", __func__);
748
749 cancel_delayed_work_sync(&perf->link_work);
750 cancel_work_sync(&perf->link_cleanup);
751
752 ntb_clear_ctx(ntb);
753 ntb_link_disable(ntb);
754
755 debugfs_remove_recursive(perf_debugfs_dir);
756 perf_debugfs_dir = NULL;
757
758 if (use_dma) {
759 for (i = 0; i < MAX_THREADS; i++) {
760 struct pthr_ctx *pctx = &perf->pthr_ctx[i];
761
762 if (pctx->dma_chan)
763 dma_release_channel(pctx->dma_chan);
764 }
765 }
766
767 kfree(perf);
768}
769
770static struct ntb_client perf_client = {
771 .ops = {
772 .probe = perf_probe,
773 .remove = perf_remove,
774 },
775};
776module_ntb_client(perf_client);