ntb_perf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. #include <linux/init.h>
  46. #include <linux/kernel.h>
  47. #include <linux/module.h>
  48. #include <linux/kthread.h>
  49. #include <linux/time.h>
  50. #include <linux/timer.h>
  51. #include <linux/dma-mapping.h>
  52. #include <linux/pci.h>
  53. #include <linux/slab.h>
  54. #include <linux/spinlock.h>
  55. #include <linux/debugfs.h>
  56. #include <linux/dmaengine.h>
  57. #include <linux/delay.h>
  58. #include <linux/sizes.h>
  59. #include <linux/ntb.h>
  60. #define DRIVER_NAME "ntb_perf"
  61. #define DRIVER_DESCRIPTION "PCIe NTB Performance Measurement Tool"
  62. #define DRIVER_LICENSE "Dual BSD/GPL"
  63. #define DRIVER_VERSION "1.0"
  64. #define DRIVER_AUTHOR "Dave Jiang <dave.jiang@intel.com>"
  65. #define PERF_LINK_DOWN_TIMEOUT 10
  66. #define PERF_VERSION 0xffff0001
  67. #define MAX_THREADS 32
  68. #define MAX_TEST_SIZE SZ_1M
  69. #define MAX_SRCS 32
  70. #define DMA_OUT_RESOURCE_TO 50
  71. #define DMA_RETRIES 20
  72. #define SZ_4G (1ULL << 32)
  73. #define MAX_SEG_ORDER 20 /* no larger than 1M for kmalloc buffer */
  74. MODULE_LICENSE(DRIVER_LICENSE);
  75. MODULE_VERSION(DRIVER_VERSION);
  76. MODULE_AUTHOR(DRIVER_AUTHOR);
  77. MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
  78. static struct dentry *perf_debugfs_dir;
  79. static unsigned int seg_order = 19; /* 512K */
  80. module_param(seg_order, uint, 0644);
  81. MODULE_PARM_DESC(seg_order, "size order [n^2] of buffer segment for testing");
  82. static unsigned int run_order = 32; /* 4G */
  83. module_param(run_order, uint, 0644);
  84. MODULE_PARM_DESC(run_order, "size order [n^2] of total data to transfer");
  85. static bool use_dma; /* default to 0 */
  86. module_param(use_dma, bool, 0644);
  87. MODULE_PARM_DESC(use_dma, "Using DMA engine to measure performance");
  88. struct perf_mw {
  89. phys_addr_t phys_addr;
  90. resource_size_t phys_size;
  91. resource_size_t xlat_align;
  92. resource_size_t xlat_align_size;
  93. void __iomem *vbase;
  94. size_t xlat_size;
  95. size_t buf_size;
  96. void *virt_addr;
  97. dma_addr_t dma_addr;
  98. };
  99. struct perf_ctx;
  100. struct pthr_ctx {
  101. struct task_struct *thread;
  102. struct perf_ctx *perf;
  103. atomic_t dma_sync;
  104. struct dma_chan *dma_chan;
  105. int dma_prep_err;
  106. int src_idx;
  107. void *srcs[MAX_SRCS];
  108. };
  109. struct perf_ctx {
  110. struct ntb_dev *ntb;
  111. spinlock_t db_lock;
  112. struct perf_mw mw;
  113. bool link_is_up;
  114. struct work_struct link_cleanup;
  115. struct delayed_work link_work;
  116. struct dentry *debugfs_node_dir;
  117. struct dentry *debugfs_run;
  118. struct dentry *debugfs_threads;
  119. u8 perf_threads;
  120. bool run;
  121. struct pthr_ctx pthr_ctx[MAX_THREADS];
  122. atomic_t tsync;
  123. };
  124. enum {
  125. VERSION = 0,
  126. MW_SZ_HIGH,
  127. MW_SZ_LOW,
  128. SPAD_MSG,
  129. SPAD_ACK,
  130. MAX_SPAD
  131. };
  132. static void perf_link_event(void *ctx)
  133. {
  134. struct perf_ctx *perf = ctx;
  135. if (ntb_link_is_up(perf->ntb, NULL, NULL) == 1)
  136. schedule_delayed_work(&perf->link_work, 2*HZ);
  137. else
  138. schedule_work(&perf->link_cleanup);
  139. }
  140. static void perf_db_event(void *ctx, int vec)
  141. {
  142. struct perf_ctx *perf = ctx;
  143. u64 db_bits, db_mask;
  144. db_mask = ntb_db_vector_mask(perf->ntb, vec);
  145. db_bits = ntb_db_read(perf->ntb);
  146. dev_dbg(&perf->ntb->dev, "doorbell vec %d mask %#llx bits %#llx\n",
  147. vec, db_mask, db_bits);
  148. }
  149. static const struct ntb_ctx_ops perf_ops = {
  150. .link_event = perf_link_event,
  151. .db_event = perf_db_event,
  152. };
  153. static void perf_copy_callback(void *data)
  154. {
  155. struct pthr_ctx *pctx = data;
  156. atomic_dec(&pctx->dma_sync);
  157. }
  158. static ssize_t perf_copy(struct pthr_ctx *pctx, char __iomem *dst,
  159. char *src, size_t size)
  160. {
  161. struct perf_ctx *perf = pctx->perf;
  162. struct dma_async_tx_descriptor *txd;
  163. struct dma_chan *chan = pctx->dma_chan;
  164. struct dma_device *device;
  165. struct dmaengine_unmap_data *unmap;
  166. dma_cookie_t cookie;
  167. size_t src_off, dst_off;
  168. struct perf_mw *mw = &perf->mw;
  169. void __iomem *vbase;
  170. void __iomem *dst_vaddr;
  171. dma_addr_t dst_phys;
  172. int retries = 0;
  173. if (!use_dma) {
  174. memcpy_toio(dst, src, size);
  175. return size;
  176. }
  177. if (!chan) {
  178. dev_err(&perf->ntb->dev, "DMA engine does not exist\n");
  179. return -EINVAL;
  180. }
  181. device = chan->device;
  182. src_off = (uintptr_t)src & ~PAGE_MASK;
  183. dst_off = (uintptr_t __force)dst & ~PAGE_MASK;
  184. if (!is_dma_copy_aligned(device, src_off, dst_off, size))
  185. return -ENODEV;
  186. vbase = mw->vbase;
  187. dst_vaddr = dst;
  188. dst_phys = mw->phys_addr + (dst_vaddr - vbase);
  189. unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
  190. if (!unmap)
  191. return -ENOMEM;
  192. unmap->len = size;
  193. unmap->addr[0] = dma_map_page(device->dev, virt_to_page(src),
  194. src_off, size, DMA_TO_DEVICE);
  195. if (dma_mapping_error(device->dev, unmap->addr[0]))
  196. goto err_get_unmap;
  197. unmap->to_cnt = 1;
  198. do {
  199. txd = device->device_prep_dma_memcpy(chan, dst_phys,
  200. unmap->addr[0],
  201. size, DMA_PREP_INTERRUPT);
  202. if (!txd) {
  203. set_current_state(TASK_INTERRUPTIBLE);
  204. schedule_timeout(DMA_OUT_RESOURCE_TO);
  205. }
  206. } while (!txd && (++retries < DMA_RETRIES));
  207. if (!txd) {
  208. pctx->dma_prep_err++;
  209. goto err_get_unmap;
  210. }
  211. txd->callback = perf_copy_callback;
  212. txd->callback_param = pctx;
  213. dma_set_unmap(txd, unmap);
  214. cookie = dmaengine_submit(txd);
  215. if (dma_submit_error(cookie))
  216. goto err_set_unmap;
  217. atomic_inc(&pctx->dma_sync);
  218. dma_async_issue_pending(chan);
  219. return size;
  220. err_set_unmap:
  221. dmaengine_unmap_put(unmap);
  222. err_get_unmap:
  223. dmaengine_unmap_put(unmap);
  224. return 0;
  225. }
  226. static int perf_move_data(struct pthr_ctx *pctx, char __iomem *dst, char *src,
  227. u64 buf_size, u64 win_size, u64 total)
  228. {
  229. int chunks, total_chunks, i;
  230. int copied_chunks = 0;
  231. u64 copied = 0, result;
  232. char __iomem *tmp = dst;
  233. u64 perf, diff_us;
  234. ktime_t kstart, kstop, kdiff;
  235. chunks = div64_u64(win_size, buf_size);
  236. total_chunks = div64_u64(total, buf_size);
  237. kstart = ktime_get();
  238. for (i = 0; i < total_chunks; i++) {
  239. result = perf_copy(pctx, tmp, src, buf_size);
  240. copied += result;
  241. copied_chunks++;
  242. if (copied_chunks == chunks) {
  243. tmp = dst;
  244. copied_chunks = 0;
  245. } else
  246. tmp += buf_size;
  247. /* Probably should schedule every 4GB to prevent soft hang. */
  248. if (((copied % SZ_4G) == 0) && !use_dma) {
  249. set_current_state(TASK_INTERRUPTIBLE);
  250. schedule_timeout(1);
  251. }
  252. }
  253. if (use_dma) {
  254. pr_info("%s: All DMA descriptors submitted\n", current->comm);
  255. while (atomic_read(&pctx->dma_sync) != 0)
  256. msleep(20);
  257. }
  258. kstop = ktime_get();
  259. kdiff = ktime_sub(kstop, kstart);
  260. diff_us = ktime_to_us(kdiff);
  261. pr_info("%s: copied %llu bytes\n", current->comm, copied);
  262. pr_info("%s: lasted %llu usecs\n", current->comm, diff_us);
  263. perf = div64_u64(copied, diff_us);
  264. pr_info("%s: MBytes/s: %llu\n", current->comm, perf);
  265. return 0;
  266. }
  267. static bool perf_dma_filter_fn(struct dma_chan *chan, void *node)
  268. {
  269. return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
  270. }
  271. static int ntb_perf_thread(void *data)
  272. {
  273. struct pthr_ctx *pctx = data;
  274. struct perf_ctx *perf = pctx->perf;
  275. struct pci_dev *pdev = perf->ntb->pdev;
  276. struct perf_mw *mw = &perf->mw;
  277. char __iomem *dst;
  278. u64 win_size, buf_size, total;
  279. void *src;
  280. int rc, node, i;
  281. struct dma_chan *dma_chan = NULL;
  282. pr_info("kthread %s starting...\n", current->comm);
  283. node = dev_to_node(&pdev->dev);
  284. if (use_dma && !pctx->dma_chan) {
  285. dma_cap_mask_t dma_mask;
  286. dma_cap_zero(dma_mask);
  287. dma_cap_set(DMA_MEMCPY, dma_mask);
  288. dma_chan = dma_request_channel(dma_mask, perf_dma_filter_fn,
  289. (void *)(unsigned long)node);
  290. if (!dma_chan) {
  291. pr_warn("%s: cannot acquire DMA channel, quitting\n",
  292. current->comm);
  293. return -ENODEV;
  294. }
  295. pctx->dma_chan = dma_chan;
  296. }
  297. for (i = 0; i < MAX_SRCS; i++) {
  298. pctx->srcs[i] = kmalloc_node(MAX_TEST_SIZE, GFP_KERNEL, node);
  299. if (!pctx->srcs[i]) {
  300. rc = -ENOMEM;
  301. goto err;
  302. }
  303. }
  304. win_size = mw->phys_size;
  305. buf_size = 1ULL << seg_order;
  306. total = 1ULL << run_order;
  307. if (buf_size > MAX_TEST_SIZE)
  308. buf_size = MAX_TEST_SIZE;
  309. dst = (char __iomem *)mw->vbase;
  310. atomic_inc(&perf->tsync);
  311. while (atomic_read(&perf->tsync) != perf->perf_threads)
  312. schedule();
  313. src = pctx->srcs[pctx->src_idx];
  314. pctx->src_idx = (pctx->src_idx + 1) & (MAX_SRCS - 1);
  315. rc = perf_move_data(pctx, dst, src, buf_size, win_size, total);
  316. atomic_dec(&perf->tsync);
  317. if (rc < 0) {
  318. pr_err("%s: failed\n", current->comm);
  319. rc = -ENXIO;
  320. goto err;
  321. }
  322. for (i = 0; i < MAX_SRCS; i++) {
  323. kfree(pctx->srcs[i]);
  324. pctx->srcs[i] = NULL;
  325. }
  326. return 0;
  327. err:
  328. for (i = 0; i < MAX_SRCS; i++) {
  329. kfree(pctx->srcs[i]);
  330. pctx->srcs[i] = NULL;
  331. }
  332. if (dma_chan) {
  333. dma_release_channel(dma_chan);
  334. pctx->dma_chan = NULL;
  335. }
  336. return rc;
  337. }
  338. static void perf_free_mw(struct perf_ctx *perf)
  339. {
  340. struct perf_mw *mw = &perf->mw;
  341. struct pci_dev *pdev = perf->ntb->pdev;
  342. if (!mw->virt_addr)
  343. return;
  344. ntb_mw_clear_trans(perf->ntb, 0);
  345. dma_free_coherent(&pdev->dev, mw->buf_size,
  346. mw->virt_addr, mw->dma_addr);
  347. mw->xlat_size = 0;
  348. mw->buf_size = 0;
  349. mw->virt_addr = NULL;
  350. }
  351. static int perf_set_mw(struct perf_ctx *perf, resource_size_t size)
  352. {
  353. struct perf_mw *mw = &perf->mw;
  354. size_t xlat_size, buf_size;
  355. int rc;
  356. if (!size)
  357. return -EINVAL;
  358. xlat_size = round_up(size, mw->xlat_align_size);
  359. buf_size = round_up(size, mw->xlat_align);
  360. if (mw->xlat_size == xlat_size)
  361. return 0;
  362. if (mw->buf_size)
  363. perf_free_mw(perf);
  364. mw->xlat_size = xlat_size;
  365. mw->buf_size = buf_size;
  366. mw->virt_addr = dma_alloc_coherent(&perf->ntb->pdev->dev, buf_size,
  367. &mw->dma_addr, GFP_KERNEL);
  368. if (!mw->virt_addr) {
  369. mw->xlat_size = 0;
  370. mw->buf_size = 0;
  371. }
  372. rc = ntb_mw_set_trans(perf->ntb, 0, mw->dma_addr, mw->xlat_size);
  373. if (rc) {
  374. dev_err(&perf->ntb->dev, "Unable to set mw0 translation\n");
  375. perf_free_mw(perf);
  376. return -EIO;
  377. }
  378. return 0;
  379. }
  380. static void perf_link_work(struct work_struct *work)
  381. {
  382. struct perf_ctx *perf =
  383. container_of(work, struct perf_ctx, link_work.work);
  384. struct ntb_dev *ndev = perf->ntb;
  385. struct pci_dev *pdev = ndev->pdev;
  386. u32 val;
  387. u64 size;
  388. int rc;
  389. dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
  390. size = perf->mw.phys_size;
  391. ntb_peer_spad_write(ndev, MW_SZ_HIGH, upper_32_bits(size));
  392. ntb_peer_spad_write(ndev, MW_SZ_LOW, lower_32_bits(size));
  393. ntb_peer_spad_write(ndev, VERSION, PERF_VERSION);
  394. /* now read what peer wrote */
  395. val = ntb_spad_read(ndev, VERSION);
  396. if (val != PERF_VERSION) {
  397. dev_dbg(&pdev->dev, "Remote version = %#x\n", val);
  398. goto out;
  399. }
  400. val = ntb_spad_read(ndev, MW_SZ_HIGH);
  401. size = (u64)val << 32;
  402. val = ntb_spad_read(ndev, MW_SZ_LOW);
  403. size |= val;
  404. dev_dbg(&pdev->dev, "Remote MW size = %#llx\n", size);
  405. rc = perf_set_mw(perf, size);
  406. if (rc)
  407. goto out1;
  408. perf->link_is_up = true;
  409. return;
  410. out1:
  411. perf_free_mw(perf);
  412. out:
  413. if (ntb_link_is_up(ndev, NULL, NULL) == 1)
  414. schedule_delayed_work(&perf->link_work,
  415. msecs_to_jiffies(PERF_LINK_DOWN_TIMEOUT));
  416. }
  417. static void perf_link_cleanup(struct work_struct *work)
  418. {
  419. struct perf_ctx *perf = container_of(work,
  420. struct perf_ctx,
  421. link_cleanup);
  422. dev_dbg(&perf->ntb->pdev->dev, "%s called\n", __func__);
  423. if (!perf->link_is_up)
  424. cancel_delayed_work_sync(&perf->link_work);
  425. }
  426. static int perf_setup_mw(struct ntb_dev *ntb, struct perf_ctx *perf)
  427. {
  428. struct perf_mw *mw;
  429. int rc;
  430. mw = &perf->mw;
  431. rc = ntb_mw_get_range(ntb, 0, &mw->phys_addr, &mw->phys_size,
  432. &mw->xlat_align, &mw->xlat_align_size);
  433. if (rc)
  434. return rc;
  435. perf->mw.vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
  436. if (!mw->vbase)
  437. return -ENOMEM;
  438. return 0;
  439. }
  440. static ssize_t debugfs_run_read(struct file *filp, char __user *ubuf,
  441. size_t count, loff_t *offp)
  442. {
  443. struct perf_ctx *perf = filp->private_data;
  444. char *buf;
  445. ssize_t ret, out_offset;
  446. if (!perf)
  447. return 0;
  448. buf = kmalloc(64, GFP_KERNEL);
  449. if (!buf)
  450. return -ENOMEM;
  451. out_offset = snprintf(buf, 64, "%d\n", perf->run);
  452. ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
  453. kfree(buf);
  454. return ret;
  455. }
  456. static void threads_cleanup(struct perf_ctx *perf)
  457. {
  458. struct pthr_ctx *pctx;
  459. int i;
  460. perf->run = false;
  461. for (i = 0; i < MAX_THREADS; i++) {
  462. pctx = &perf->pthr_ctx[i];
  463. if (pctx->thread) {
  464. kthread_stop(pctx->thread);
  465. pctx->thread = NULL;
  466. }
  467. }
  468. }
  469. static ssize_t debugfs_run_write(struct file *filp, const char __user *ubuf,
  470. size_t count, loff_t *offp)
  471. {
  472. struct perf_ctx *perf = filp->private_data;
  473. int node, i;
  474. if (!perf->link_is_up)
  475. return 0;
  476. if (perf->perf_threads == 0)
  477. return 0;
  478. if (atomic_read(&perf->tsync) == 0)
  479. perf->run = false;
  480. if (perf->run)
  481. threads_cleanup(perf);
  482. else {
  483. perf->run = true;
  484. if (perf->perf_threads > MAX_THREADS) {
  485. perf->perf_threads = MAX_THREADS;
  486. pr_info("Reset total threads to: %u\n", MAX_THREADS);
  487. }
  488. /* no greater than 1M */
  489. if (seg_order > MAX_SEG_ORDER) {
  490. seg_order = MAX_SEG_ORDER;
  491. pr_info("Fix seg_order to %u\n", seg_order);
  492. }
  493. if (run_order < seg_order) {
  494. run_order = seg_order;
  495. pr_info("Fix run_order to %u\n", run_order);
  496. }
  497. node = dev_to_node(&perf->ntb->pdev->dev);
  498. /* launch kernel thread */
  499. for (i = 0; i < perf->perf_threads; i++) {
  500. struct pthr_ctx *pctx;
  501. pctx = &perf->pthr_ctx[i];
  502. atomic_set(&pctx->dma_sync, 0);
  503. pctx->perf = perf;
  504. pctx->thread =
  505. kthread_create_on_node(ntb_perf_thread,
  506. (void *)pctx,
  507. node, "ntb_perf %d", i);
  508. if (IS_ERR(pctx->thread)) {
  509. pctx->thread = NULL;
  510. goto err;
  511. } else
  512. wake_up_process(pctx->thread);
  513. if (perf->run == false)
  514. return -ENXIO;
  515. }
  516. }
  517. return count;
  518. err:
  519. threads_cleanup(perf);
  520. return -ENXIO;
  521. }
  522. static const struct file_operations ntb_perf_debugfs_run = {
  523. .owner = THIS_MODULE,
  524. .open = simple_open,
  525. .read = debugfs_run_read,
  526. .write = debugfs_run_write,
  527. };
  528. static int perf_debugfs_setup(struct perf_ctx *perf)
  529. {
  530. struct pci_dev *pdev = perf->ntb->pdev;
  531. if (!debugfs_initialized())
  532. return -ENODEV;
  533. if (!perf_debugfs_dir) {
  534. perf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  535. if (!perf_debugfs_dir)
  536. return -ENODEV;
  537. }
  538. perf->debugfs_node_dir = debugfs_create_dir(pci_name(pdev),
  539. perf_debugfs_dir);
  540. if (!perf->debugfs_node_dir)
  541. return -ENODEV;
  542. perf->debugfs_run = debugfs_create_file("run", S_IRUSR | S_IWUSR,
  543. perf->debugfs_node_dir, perf,
  544. &ntb_perf_debugfs_run);
  545. if (!perf->debugfs_run)
  546. return -ENODEV;
  547. perf->debugfs_threads = debugfs_create_u8("threads", S_IRUSR | S_IWUSR,
  548. perf->debugfs_node_dir,
  549. &perf->perf_threads);
  550. if (!perf->debugfs_threads)
  551. return -ENODEV;
  552. return 0;
  553. }
  554. static int perf_probe(struct ntb_client *client, struct ntb_dev *ntb)
  555. {
  556. struct pci_dev *pdev = ntb->pdev;
  557. struct perf_ctx *perf;
  558. int node;
  559. int rc = 0;
  560. node = dev_to_node(&pdev->dev);
  561. perf = kzalloc_node(sizeof(*perf), GFP_KERNEL, node);
  562. if (!perf) {
  563. rc = -ENOMEM;
  564. goto err_perf;
  565. }
  566. perf->ntb = ntb;
  567. perf->perf_threads = 1;
  568. atomic_set(&perf->tsync, 0);
  569. perf->run = false;
  570. spin_lock_init(&perf->db_lock);
  571. perf_setup_mw(ntb, perf);
  572. INIT_DELAYED_WORK(&perf->link_work, perf_link_work);
  573. INIT_WORK(&perf->link_cleanup, perf_link_cleanup);
  574. rc = ntb_set_ctx(ntb, perf, &perf_ops);
  575. if (rc)
  576. goto err_ctx;
  577. perf->link_is_up = false;
  578. ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
  579. ntb_link_event(ntb);
  580. rc = perf_debugfs_setup(perf);
  581. if (rc)
  582. goto err_ctx;
  583. return 0;
  584. err_ctx:
  585. cancel_delayed_work_sync(&perf->link_work);
  586. cancel_work_sync(&perf->link_cleanup);
  587. kfree(perf);
  588. err_perf:
  589. return rc;
  590. }
  591. static void perf_remove(struct ntb_client *client, struct ntb_dev *ntb)
  592. {
  593. struct perf_ctx *perf = ntb->ctx;
  594. int i;
  595. dev_dbg(&perf->ntb->dev, "%s called\n", __func__);
  596. cancel_delayed_work_sync(&perf->link_work);
  597. cancel_work_sync(&perf->link_cleanup);
  598. ntb_clear_ctx(ntb);
  599. ntb_link_disable(ntb);
  600. debugfs_remove_recursive(perf_debugfs_dir);
  601. perf_debugfs_dir = NULL;
  602. if (use_dma) {
  603. for (i = 0; i < MAX_THREADS; i++) {
  604. struct pthr_ctx *pctx = &perf->pthr_ctx[i];
  605. if (pctx->dma_chan)
  606. dma_release_channel(pctx->dma_chan);
  607. }
  608. }
  609. kfree(perf);
  610. }
  611. static struct ntb_client perf_client = {
  612. .ops = {
  613. .probe = perf_probe,
  614. .remove = perf_remove,
  615. },
  616. };
  617. module_ntb_client(perf_client);