blob: 251fe75798c133e07b0ff8d710cfed9c4079b308 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
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 * Copyright(c) 2017 T-Platforms. All Rights Reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2015 Intel Corporation. All rights reserved.
17 * Copyright(c) 2017 T-Platforms. All Rights Reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
45 * PCIe NTB Perf Linux driver
46 */
47
48/*
49 * How to use this tool, by example.
50 *
51 * Assuming $DBG_DIR is something like:
52 * '/sys/kernel/debug/ntb_perf/0000:00:03.0'
53 * Suppose aside from local device there is at least one remote device
54 * connected to NTB with index 0.
55 *-----------------------------------------------------------------------------
56 * Eg: install driver with specified chunk/total orders and dma-enabled flag
57 *
58 * root@self# insmod ntb_perf.ko chunk_order=19 total_order=28 use_dma
59 *-----------------------------------------------------------------------------
60 * Eg: check NTB ports (index) and MW mapping information
61 *
62 * root@self# cat $DBG_DIR/info
63 *-----------------------------------------------------------------------------
64 * Eg: start performance test with peer (index 0) and get the test metrics
65 *
66 * root@self# echo 0 > $DBG_DIR/run
67 * root@self# cat $DBG_DIR/run
68 */
69
70#include <linux/init.h>
71#include <linux/kernel.h>
72#include <linux/module.h>
73#include <linux/sched.h>
74#include <linux/wait.h>
75#include <linux/dma-mapping.h>
76#include <linux/dmaengine.h>
77#include <linux/pci.h>
78#include <linux/ktime.h>
79#include <linux/slab.h>
80#include <linux/delay.h>
81#include <linux/sizes.h>
82#include <linux/workqueue.h>
83#include <linux/debugfs.h>
84#include <linux/random.h>
85#include <linux/ntb.h>
86
87#define DRIVER_NAME "ntb_perf"
88#define DRIVER_VERSION "2.0"
89
90MODULE_LICENSE("Dual BSD/GPL");
91MODULE_VERSION(DRIVER_VERSION);
92MODULE_AUTHOR("Dave Jiang <dave.jiang@intel.com>");
93MODULE_DESCRIPTION("PCIe NTB Performance Measurement Tool");
94
95#define MAX_THREADS_CNT 32
96#define DEF_THREADS_CNT 1
97#define MAX_CHUNK_SIZE SZ_1M
98#define MAX_CHUNK_ORDER 20 /* no larger than 1M */
99
100#define DMA_TRIES 100
101#define DMA_MDELAY 10
102
David Brazdil0f672f62019-12-10 10:32:29 +0000103#define MSG_TRIES 1000
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104#define MSG_UDELAY_LOW 1000
105#define MSG_UDELAY_HIGH 2000
106
107#define PERF_BUF_LEN 1024
108
109static unsigned long max_mw_size;
110module_param(max_mw_size, ulong, 0644);
111MODULE_PARM_DESC(max_mw_size, "Upper limit of memory window size");
112
113static unsigned char chunk_order = 19; /* 512K */
114module_param(chunk_order, byte, 0644);
115MODULE_PARM_DESC(chunk_order, "Data chunk order [2^n] to transfer");
116
117static unsigned char total_order = 30; /* 1G */
118module_param(total_order, byte, 0644);
119MODULE_PARM_DESC(total_order, "Total data order [2^n] to transfer");
120
121static bool use_dma; /* default to 0 */
122module_param(use_dma, bool, 0644);
123MODULE_PARM_DESC(use_dma, "Use DMA engine to measure performance");
124
125/*==============================================================================
126 * Perf driver data definition
127 *==============================================================================
128 */
129
130enum perf_cmd {
131 PERF_CMD_INVAL = -1,/* invalid spad command */
132 PERF_CMD_SSIZE = 0, /* send out buffer size */
133 PERF_CMD_RSIZE = 1, /* recv in buffer size */
134 PERF_CMD_SXLAT = 2, /* send in buffer xlat */
135 PERF_CMD_RXLAT = 3, /* recv out buffer xlat */
136 PERF_CMD_CLEAR = 4, /* clear allocated memory */
137 PERF_STS_DONE = 5, /* init is done */
138 PERF_STS_LNKUP = 6, /* link up state flag */
139};
140
141struct perf_ctx;
142
143struct perf_peer {
144 struct perf_ctx *perf;
145 int pidx;
146 int gidx;
147
148 /* Outbound MW params */
149 u64 outbuf_xlat;
150 resource_size_t outbuf_size;
151 void __iomem *outbuf;
152
153 /* Inbound MW params */
154 dma_addr_t inbuf_xlat;
155 resource_size_t inbuf_size;
156 void *inbuf;
157
158 /* NTB connection setup service */
159 struct work_struct service;
160 unsigned long sts;
Olivier Deprez0e641232021-09-23 10:07:05 +0200161
162 struct completion init_comp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163};
164#define to_peer_service(__work) \
165 container_of(__work, struct perf_peer, service)
166
167struct perf_thread {
168 struct perf_ctx *perf;
169 int tidx;
170
171 /* DMA-based test sync parameters */
172 atomic_t dma_sync;
173 wait_queue_head_t dma_wait;
174 struct dma_chan *dma_chan;
175
176 /* Data source and measured statistics */
177 void *src;
178 u64 copied;
179 ktime_t duration;
180 int status;
181 struct work_struct work;
182};
183#define to_thread_work(__work) \
184 container_of(__work, struct perf_thread, work)
185
186struct perf_ctx {
187 struct ntb_dev *ntb;
188
189 /* Global device index and peers descriptors */
190 int gidx;
191 int pcnt;
192 struct perf_peer *peers;
193
194 /* Performance measuring work-threads interface */
195 unsigned long busy_flag;
196 wait_queue_head_t twait;
197 atomic_t tsync;
198 u8 tcnt;
199 struct perf_peer *test_peer;
200 struct perf_thread threads[MAX_THREADS_CNT];
201
202 /* Scratchpad/Message IO operations */
203 int (*cmd_send)(struct perf_peer *peer, enum perf_cmd cmd, u64 data);
204 int (*cmd_recv)(struct perf_ctx *perf, int *pidx, enum perf_cmd *cmd,
205 u64 *data);
206
207 struct dentry *dbgfs_dir;
208};
209
210/*
211 * Scratchpads-base commands interface
212 */
213#define PERF_SPAD_CNT(_pcnt) \
214 (3*((_pcnt) + 1))
215#define PERF_SPAD_CMD(_gidx) \
216 (3*(_gidx))
217#define PERF_SPAD_LDATA(_gidx) \
218 (3*(_gidx) + 1)
219#define PERF_SPAD_HDATA(_gidx) \
220 (3*(_gidx) + 2)
221#define PERF_SPAD_NOTIFY(_gidx) \
222 (BIT_ULL(_gidx))
223
224/*
225 * Messages-base commands interface
226 */
227#define PERF_MSG_CNT 3
228#define PERF_MSG_CMD 0
229#define PERF_MSG_LDATA 1
230#define PERF_MSG_HDATA 2
231
232/*==============================================================================
233 * Static data declarations
234 *==============================================================================
235 */
236
237static struct dentry *perf_dbgfs_topdir;
238
239static struct workqueue_struct *perf_wq __read_mostly;
240
241/*==============================================================================
242 * NTB cross-link commands execution service
243 *==============================================================================
244 */
245
246static void perf_terminate_test(struct perf_ctx *perf);
247
248static inline bool perf_link_is_up(struct perf_peer *peer)
249{
250 u64 link;
251
252 link = ntb_link_is_up(peer->perf->ntb, NULL, NULL);
253 return !!(link & BIT_ULL_MASK(peer->pidx));
254}
255
256static int perf_spad_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
257 u64 data)
258{
259 struct perf_ctx *perf = peer->perf;
260 int try;
261 u32 sts;
262
263 dev_dbg(&perf->ntb->dev, "CMD send: %d 0x%llx\n", cmd, data);
264
265 /*
266 * Perform predefined number of attempts before give up.
267 * We are sending the data to the port specific scratchpad, so
268 * to prevent a multi-port access race-condition. Additionally
269 * there is no need in local locking since only thread-safe
270 * service work is using this method.
271 */
272 for (try = 0; try < MSG_TRIES; try++) {
273 if (!perf_link_is_up(peer))
274 return -ENOLINK;
275
276 sts = ntb_peer_spad_read(perf->ntb, peer->pidx,
277 PERF_SPAD_CMD(perf->gidx));
278 if (sts != PERF_CMD_INVAL) {
279 usleep_range(MSG_UDELAY_LOW, MSG_UDELAY_HIGH);
280 continue;
281 }
282
283 ntb_peer_spad_write(perf->ntb, peer->pidx,
284 PERF_SPAD_LDATA(perf->gidx),
285 lower_32_bits(data));
286 ntb_peer_spad_write(perf->ntb, peer->pidx,
287 PERF_SPAD_HDATA(perf->gidx),
288 upper_32_bits(data));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289 ntb_peer_spad_write(perf->ntb, peer->pidx,
290 PERF_SPAD_CMD(perf->gidx),
291 cmd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292 ntb_peer_db_set(perf->ntb, PERF_SPAD_NOTIFY(peer->gidx));
293
294 dev_dbg(&perf->ntb->dev, "DB ring peer %#llx\n",
295 PERF_SPAD_NOTIFY(peer->gidx));
296
297 break;
298 }
299
300 return try < MSG_TRIES ? 0 : -EAGAIN;
301}
302
303static int perf_spad_cmd_recv(struct perf_ctx *perf, int *pidx,
304 enum perf_cmd *cmd, u64 *data)
305{
306 struct perf_peer *peer;
307 u32 val;
308
309 ntb_db_clear(perf->ntb, PERF_SPAD_NOTIFY(perf->gidx));
310
311 /*
312 * We start scanning all over, since cleared DB may have been set
313 * by any peer. Yes, it makes peer with smaller index being
314 * serviced with greater priority, but it's convenient for spad
315 * and message code unification and simplicity.
316 */
317 for (*pidx = 0; *pidx < perf->pcnt; (*pidx)++) {
318 peer = &perf->peers[*pidx];
319
320 if (!perf_link_is_up(peer))
321 continue;
322
323 val = ntb_spad_read(perf->ntb, PERF_SPAD_CMD(peer->gidx));
324 if (val == PERF_CMD_INVAL)
325 continue;
326
327 *cmd = val;
328
329 val = ntb_spad_read(perf->ntb, PERF_SPAD_LDATA(peer->gidx));
330 *data = val;
331
332 val = ntb_spad_read(perf->ntb, PERF_SPAD_HDATA(peer->gidx));
333 *data |= (u64)val << 32;
334
335 /* Next command can be retrieved from now */
336 ntb_spad_write(perf->ntb, PERF_SPAD_CMD(peer->gidx),
337 PERF_CMD_INVAL);
338
339 dev_dbg(&perf->ntb->dev, "CMD recv: %d 0x%llx\n", *cmd, *data);
340
341 return 0;
342 }
343
344 return -ENODATA;
345}
346
347static int perf_msg_cmd_send(struct perf_peer *peer, enum perf_cmd cmd,
348 u64 data)
349{
350 struct perf_ctx *perf = peer->perf;
351 int try, ret;
352 u64 outbits;
353
354 dev_dbg(&perf->ntb->dev, "CMD send: %d 0x%llx\n", cmd, data);
355
356 /*
357 * Perform predefined number of attempts before give up. Message
358 * registers are free of race-condition problem when accessed
359 * from different ports, so we don't need splitting registers
360 * by global device index. We also won't have local locking,
361 * since the method is used from service work only.
362 */
363 outbits = ntb_msg_outbits(perf->ntb);
364 for (try = 0; try < MSG_TRIES; try++) {
365 if (!perf_link_is_up(peer))
366 return -ENOLINK;
367
368 ret = ntb_msg_clear_sts(perf->ntb, outbits);
369 if (ret)
370 return ret;
371
372 ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_LDATA,
373 lower_32_bits(data));
374
375 if (ntb_msg_read_sts(perf->ntb) & outbits) {
376 usleep_range(MSG_UDELAY_LOW, MSG_UDELAY_HIGH);
377 continue;
378 }
379
380 ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_HDATA,
381 upper_32_bits(data));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382
383 /* This call shall trigger peer message event */
384 ntb_peer_msg_write(perf->ntb, peer->pidx, PERF_MSG_CMD, cmd);
385
386 break;
387 }
388
389 return try < MSG_TRIES ? 0 : -EAGAIN;
390}
391
392static int perf_msg_cmd_recv(struct perf_ctx *perf, int *pidx,
393 enum perf_cmd *cmd, u64 *data)
394{
395 u64 inbits;
396 u32 val;
397
398 inbits = ntb_msg_inbits(perf->ntb);
399
400 if (hweight64(ntb_msg_read_sts(perf->ntb) & inbits) < 3)
401 return -ENODATA;
402
403 val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_CMD);
404 *cmd = val;
405
406 val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_LDATA);
407 *data = val;
408
409 val = ntb_msg_read(perf->ntb, pidx, PERF_MSG_HDATA);
410 *data |= (u64)val << 32;
411
412 /* Next command can be retrieved from now */
413 ntb_msg_clear_sts(perf->ntb, inbits);
414
415 dev_dbg(&perf->ntb->dev, "CMD recv: %d 0x%llx\n", *cmd, *data);
416
417 return 0;
418}
419
420static int perf_cmd_send(struct perf_peer *peer, enum perf_cmd cmd, u64 data)
421{
422 struct perf_ctx *perf = peer->perf;
423
424 if (cmd == PERF_CMD_SSIZE || cmd == PERF_CMD_SXLAT)
425 return perf->cmd_send(peer, cmd, data);
426
427 dev_err(&perf->ntb->dev, "Send invalid command\n");
428 return -EINVAL;
429}
430
431static int perf_cmd_exec(struct perf_peer *peer, enum perf_cmd cmd)
432{
433 switch (cmd) {
434 case PERF_CMD_SSIZE:
435 case PERF_CMD_RSIZE:
436 case PERF_CMD_SXLAT:
437 case PERF_CMD_RXLAT:
438 case PERF_CMD_CLEAR:
439 break;
440 default:
441 dev_err(&peer->perf->ntb->dev, "Exec invalid command\n");
442 return -EINVAL;
443 }
444
445 /* No need of memory barrier, since bit ops have invernal lock */
446 set_bit(cmd, &peer->sts);
447
448 dev_dbg(&peer->perf->ntb->dev, "CMD exec: %d\n", cmd);
449
450 (void)queue_work(system_highpri_wq, &peer->service);
451
452 return 0;
453}
454
455static int perf_cmd_recv(struct perf_ctx *perf)
456{
457 struct perf_peer *peer;
458 int ret, pidx, cmd;
459 u64 data;
460
461 while (!(ret = perf->cmd_recv(perf, &pidx, &cmd, &data))) {
462 peer = &perf->peers[pidx];
463
464 switch (cmd) {
465 case PERF_CMD_SSIZE:
466 peer->inbuf_size = data;
467 return perf_cmd_exec(peer, PERF_CMD_RSIZE);
468 case PERF_CMD_SXLAT:
469 peer->outbuf_xlat = data;
470 return perf_cmd_exec(peer, PERF_CMD_RXLAT);
471 default:
472 dev_err(&perf->ntb->dev, "Recv invalid command\n");
473 return -EINVAL;
474 }
475 }
476
477 /* Return 0 if no data left to process, otherwise an error */
478 return ret == -ENODATA ? 0 : ret;
479}
480
481static void perf_link_event(void *ctx)
482{
483 struct perf_ctx *perf = ctx;
484 struct perf_peer *peer;
485 bool lnk_up;
486 int pidx;
487
488 for (pidx = 0; pidx < perf->pcnt; pidx++) {
489 peer = &perf->peers[pidx];
490
491 lnk_up = perf_link_is_up(peer);
492
493 if (lnk_up &&
494 !test_and_set_bit(PERF_STS_LNKUP, &peer->sts)) {
495 perf_cmd_exec(peer, PERF_CMD_SSIZE);
496 } else if (!lnk_up &&
497 test_and_clear_bit(PERF_STS_LNKUP, &peer->sts)) {
498 perf_cmd_exec(peer, PERF_CMD_CLEAR);
499 }
500 }
501}
502
503static void perf_db_event(void *ctx, int vec)
504{
505 struct perf_ctx *perf = ctx;
506
507 dev_dbg(&perf->ntb->dev, "DB vec %d mask %#llx bits %#llx\n", vec,
508 ntb_db_vector_mask(perf->ntb, vec), ntb_db_read(perf->ntb));
509
510 /* Just receive all available commands */
511 (void)perf_cmd_recv(perf);
512}
513
514static void perf_msg_event(void *ctx)
515{
516 struct perf_ctx *perf = ctx;
517
518 dev_dbg(&perf->ntb->dev, "Msg status bits %#llx\n",
519 ntb_msg_read_sts(perf->ntb));
520
521 /* Messages are only sent one-by-one */
522 (void)perf_cmd_recv(perf);
523}
524
525static const struct ntb_ctx_ops perf_ops = {
526 .link_event = perf_link_event,
527 .db_event = perf_db_event,
528 .msg_event = perf_msg_event
529};
530
531static void perf_free_outbuf(struct perf_peer *peer)
532{
533 (void)ntb_peer_mw_clear_trans(peer->perf->ntb, peer->pidx, peer->gidx);
534}
535
536static int perf_setup_outbuf(struct perf_peer *peer)
537{
538 struct perf_ctx *perf = peer->perf;
539 int ret;
540
541 /* Outbuf size can be unaligned due to custom max_mw_size */
542 ret = ntb_peer_mw_set_trans(perf->ntb, peer->pidx, peer->gidx,
543 peer->outbuf_xlat, peer->outbuf_size);
544 if (ret) {
545 dev_err(&perf->ntb->dev, "Failed to set outbuf translation\n");
546 return ret;
547 }
548
549 /* Initialization is finally done */
550 set_bit(PERF_STS_DONE, &peer->sts);
Olivier Deprez0e641232021-09-23 10:07:05 +0200551 complete_all(&peer->init_comp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000552
553 return 0;
554}
555
556static void perf_free_inbuf(struct perf_peer *peer)
557{
558 if (!peer->inbuf)
559 return;
560
561 (void)ntb_mw_clear_trans(peer->perf->ntb, peer->pidx, peer->gidx);
Olivier Deprez0e641232021-09-23 10:07:05 +0200562 dma_free_coherent(&peer->perf->ntb->pdev->dev, peer->inbuf_size,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000563 peer->inbuf, peer->inbuf_xlat);
564 peer->inbuf = NULL;
565}
566
567static int perf_setup_inbuf(struct perf_peer *peer)
568{
569 resource_size_t xlat_align, size_align, size_max;
570 struct perf_ctx *perf = peer->perf;
571 int ret;
572
573 /* Get inbound MW parameters */
574 ret = ntb_mw_get_align(perf->ntb, peer->pidx, perf->gidx,
575 &xlat_align, &size_align, &size_max);
576 if (ret) {
577 dev_err(&perf->ntb->dev, "Couldn't get inbuf restrictions\n");
578 return ret;
579 }
580
581 if (peer->inbuf_size > size_max) {
582 dev_err(&perf->ntb->dev, "Too big inbuf size %pa > %pa\n",
583 &peer->inbuf_size, &size_max);
584 return -EINVAL;
585 }
586
587 peer->inbuf_size = round_up(peer->inbuf_size, size_align);
588
589 perf_free_inbuf(peer);
590
Olivier Deprez0e641232021-09-23 10:07:05 +0200591 peer->inbuf = dma_alloc_coherent(&perf->ntb->pdev->dev,
592 peer->inbuf_size, &peer->inbuf_xlat,
593 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000594 if (!peer->inbuf) {
595 dev_err(&perf->ntb->dev, "Failed to alloc inbuf of %pa\n",
596 &peer->inbuf_size);
597 return -ENOMEM;
598 }
599 if (!IS_ALIGNED(peer->inbuf_xlat, xlat_align)) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200600 ret = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000601 dev_err(&perf->ntb->dev, "Unaligned inbuf allocated\n");
602 goto err_free_inbuf;
603 }
604
605 ret = ntb_mw_set_trans(perf->ntb, peer->pidx, peer->gidx,
606 peer->inbuf_xlat, peer->inbuf_size);
607 if (ret) {
608 dev_err(&perf->ntb->dev, "Failed to set inbuf translation\n");
609 goto err_free_inbuf;
610 }
611
612 /*
613 * We submit inbuf xlat transmission cmd for execution here to follow
614 * the code architecture, even though this method is called from service
615 * work itself so the command will be executed right after it returns.
616 */
617 (void)perf_cmd_exec(peer, PERF_CMD_SXLAT);
618
619 return 0;
620
621err_free_inbuf:
622 perf_free_inbuf(peer);
623
624 return ret;
625}
626
627static void perf_service_work(struct work_struct *work)
628{
629 struct perf_peer *peer = to_peer_service(work);
630
631 if (test_and_clear_bit(PERF_CMD_SSIZE, &peer->sts))
632 perf_cmd_send(peer, PERF_CMD_SSIZE, peer->outbuf_size);
633
634 if (test_and_clear_bit(PERF_CMD_RSIZE, &peer->sts))
635 perf_setup_inbuf(peer);
636
637 if (test_and_clear_bit(PERF_CMD_SXLAT, &peer->sts))
638 perf_cmd_send(peer, PERF_CMD_SXLAT, peer->inbuf_xlat);
639
640 if (test_and_clear_bit(PERF_CMD_RXLAT, &peer->sts))
641 perf_setup_outbuf(peer);
642
643 if (test_and_clear_bit(PERF_CMD_CLEAR, &peer->sts)) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200644 init_completion(&peer->init_comp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645 clear_bit(PERF_STS_DONE, &peer->sts);
646 if (test_bit(0, &peer->perf->busy_flag) &&
647 peer == peer->perf->test_peer) {
648 dev_warn(&peer->perf->ntb->dev,
649 "Freeing while test on-fly\n");
650 perf_terminate_test(peer->perf);
651 }
652 perf_free_outbuf(peer);
653 perf_free_inbuf(peer);
654 }
655}
656
657static int perf_init_service(struct perf_ctx *perf)
658{
659 u64 mask;
660
Olivier Deprez0e641232021-09-23 10:07:05 +0200661 if (ntb_peer_mw_count(perf->ntb) < perf->pcnt) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000662 dev_err(&perf->ntb->dev, "Not enough memory windows\n");
663 return -EINVAL;
664 }
665
666 if (ntb_msg_count(perf->ntb) >= PERF_MSG_CNT) {
667 perf->cmd_send = perf_msg_cmd_send;
668 perf->cmd_recv = perf_msg_cmd_recv;
669
670 dev_dbg(&perf->ntb->dev, "Message service initialized\n");
671
672 return 0;
673 }
674
675 dev_dbg(&perf->ntb->dev, "Message service unsupported\n");
676
677 mask = GENMASK_ULL(perf->pcnt, 0);
678 if (ntb_spad_count(perf->ntb) >= PERF_SPAD_CNT(perf->pcnt) &&
679 (ntb_db_valid_mask(perf->ntb) & mask) == mask) {
680 perf->cmd_send = perf_spad_cmd_send;
681 perf->cmd_recv = perf_spad_cmd_recv;
682
683 dev_dbg(&perf->ntb->dev, "Scratchpad service initialized\n");
684
685 return 0;
686 }
687
688 dev_dbg(&perf->ntb->dev, "Scratchpad service unsupported\n");
689
690 dev_err(&perf->ntb->dev, "Command services unsupported\n");
691
692 return -EINVAL;
693}
694
695static int perf_enable_service(struct perf_ctx *perf)
696{
697 u64 mask, incmd_bit;
698 int ret, sidx, scnt;
699
700 mask = ntb_db_valid_mask(perf->ntb);
701 (void)ntb_db_set_mask(perf->ntb, mask);
702
703 ret = ntb_set_ctx(perf->ntb, perf, &perf_ops);
704 if (ret)
705 return ret;
706
707 if (perf->cmd_send == perf_msg_cmd_send) {
708 u64 inbits, outbits;
709
710 inbits = ntb_msg_inbits(perf->ntb);
711 outbits = ntb_msg_outbits(perf->ntb);
712 (void)ntb_msg_set_mask(perf->ntb, inbits | outbits);
713
714 incmd_bit = BIT_ULL(__ffs64(inbits));
715 ret = ntb_msg_clear_mask(perf->ntb, incmd_bit);
716
717 dev_dbg(&perf->ntb->dev, "MSG sts unmasked %#llx\n", incmd_bit);
718 } else {
719 scnt = ntb_spad_count(perf->ntb);
720 for (sidx = 0; sidx < scnt; sidx++)
721 ntb_spad_write(perf->ntb, sidx, PERF_CMD_INVAL);
722 incmd_bit = PERF_SPAD_NOTIFY(perf->gidx);
723 ret = ntb_db_clear_mask(perf->ntb, incmd_bit);
724
725 dev_dbg(&perf->ntb->dev, "DB bits unmasked %#llx\n", incmd_bit);
726 }
727 if (ret) {
728 ntb_clear_ctx(perf->ntb);
729 return ret;
730 }
731
732 ntb_link_enable(perf->ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
733 /* Might be not necessary */
734 ntb_link_event(perf->ntb);
735
736 return 0;
737}
738
739static void perf_disable_service(struct perf_ctx *perf)
740{
741 int pidx;
742
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000743 if (perf->cmd_send == perf_msg_cmd_send) {
744 u64 inbits;
745
746 inbits = ntb_msg_inbits(perf->ntb);
747 (void)ntb_msg_set_mask(perf->ntb, inbits);
748 } else {
749 (void)ntb_db_set_mask(perf->ntb, PERF_SPAD_NOTIFY(perf->gidx));
750 }
751
752 ntb_clear_ctx(perf->ntb);
753
754 for (pidx = 0; pidx < perf->pcnt; pidx++)
755 perf_cmd_exec(&perf->peers[pidx], PERF_CMD_CLEAR);
756
757 for (pidx = 0; pidx < perf->pcnt; pidx++)
758 flush_work(&perf->peers[pidx].service);
David Brazdil0f672f62019-12-10 10:32:29 +0000759
760 for (pidx = 0; pidx < perf->pcnt; pidx++) {
761 struct perf_peer *peer = &perf->peers[pidx];
762
763 ntb_spad_write(perf->ntb, PERF_SPAD_CMD(peer->gidx), 0);
764 }
765
766 ntb_db_clear(perf->ntb, PERF_SPAD_NOTIFY(perf->gidx));
767
768 ntb_link_disable(perf->ntb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000769}
770
771/*==============================================================================
772 * Performance measuring work-thread
773 *==============================================================================
774 */
775
776static void perf_dma_copy_callback(void *data)
777{
778 struct perf_thread *pthr = data;
779
780 atomic_dec(&pthr->dma_sync);
781 wake_up(&pthr->dma_wait);
782}
783
784static int perf_copy_chunk(struct perf_thread *pthr,
785 void __iomem *dst, void *src, size_t len)
786{
787 struct dma_async_tx_descriptor *tx;
788 struct dmaengine_unmap_data *unmap;
789 struct device *dma_dev;
790 int try = 0, ret = 0;
791
792 if (!use_dma) {
793 memcpy_toio(dst, src, len);
794 goto ret_check_tsync;
795 }
796
797 dma_dev = pthr->dma_chan->device->dev;
798
799 if (!is_dma_copy_aligned(pthr->dma_chan->device, offset_in_page(src),
800 offset_in_page(dst), len))
801 return -EIO;
802
803 unmap = dmaengine_get_unmap_data(dma_dev, 2, GFP_NOWAIT);
804 if (!unmap)
805 return -ENOMEM;
806
807 unmap->len = len;
808 unmap->addr[0] = dma_map_page(dma_dev, virt_to_page(src),
809 offset_in_page(src), len, DMA_TO_DEVICE);
810 if (dma_mapping_error(dma_dev, unmap->addr[0])) {
811 ret = -EIO;
812 goto err_free_resource;
813 }
814 unmap->to_cnt = 1;
815
816 unmap->addr[1] = dma_map_page(dma_dev, virt_to_page(dst),
817 offset_in_page(dst), len, DMA_FROM_DEVICE);
818 if (dma_mapping_error(dma_dev, unmap->addr[1])) {
819 ret = -EIO;
820 goto err_free_resource;
821 }
822 unmap->from_cnt = 1;
823
824 do {
825 tx = dmaengine_prep_dma_memcpy(pthr->dma_chan, unmap->addr[1],
826 unmap->addr[0], len, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
827 if (!tx)
828 msleep(DMA_MDELAY);
829 } while (!tx && (try++ < DMA_TRIES));
830
831 if (!tx) {
832 ret = -EIO;
833 goto err_free_resource;
834 }
835
836 tx->callback = perf_dma_copy_callback;
837 tx->callback_param = pthr;
838 dma_set_unmap(tx, unmap);
839
840 ret = dma_submit_error(dmaengine_submit(tx));
841 if (ret) {
842 dmaengine_unmap_put(unmap);
843 goto err_free_resource;
844 }
845
846 dmaengine_unmap_put(unmap);
847
848 atomic_inc(&pthr->dma_sync);
849 dma_async_issue_pending(pthr->dma_chan);
850
851ret_check_tsync:
852 return likely(atomic_read(&pthr->perf->tsync) > 0) ? 0 : -EINTR;
853
854err_free_resource:
855 dmaengine_unmap_put(unmap);
856
857 return ret;
858}
859
860static bool perf_dma_filter(struct dma_chan *chan, void *data)
861{
862 struct perf_ctx *perf = data;
863 int node;
864
865 node = dev_to_node(&perf->ntb->dev);
866
867 return node == NUMA_NO_NODE || node == dev_to_node(chan->device->dev);
868}
869
870static int perf_init_test(struct perf_thread *pthr)
871{
872 struct perf_ctx *perf = pthr->perf;
873 dma_cap_mask_t dma_mask;
874
875 pthr->src = kmalloc_node(perf->test_peer->outbuf_size, GFP_KERNEL,
876 dev_to_node(&perf->ntb->dev));
877 if (!pthr->src)
878 return -ENOMEM;
879
880 get_random_bytes(pthr->src, perf->test_peer->outbuf_size);
881
882 if (!use_dma)
883 return 0;
884
885 dma_cap_zero(dma_mask);
886 dma_cap_set(DMA_MEMCPY, dma_mask);
887 pthr->dma_chan = dma_request_channel(dma_mask, perf_dma_filter, perf);
888 if (!pthr->dma_chan) {
889 dev_err(&perf->ntb->dev, "%d: Failed to get DMA channel\n",
890 pthr->tidx);
891 atomic_dec(&perf->tsync);
892 wake_up(&perf->twait);
893 kfree(pthr->src);
894 return -ENODEV;
895 }
896
897 atomic_set(&pthr->dma_sync, 0);
898
899 return 0;
900}
901
902static int perf_run_test(struct perf_thread *pthr)
903{
904 struct perf_peer *peer = pthr->perf->test_peer;
905 struct perf_ctx *perf = pthr->perf;
906 void __iomem *flt_dst, *bnd_dst;
907 u64 total_size, chunk_size;
908 void *flt_src;
909 int ret = 0;
910
911 total_size = 1ULL << total_order;
912 chunk_size = 1ULL << chunk_order;
913 chunk_size = min_t(u64, peer->outbuf_size, chunk_size);
914
915 flt_src = pthr->src;
916 bnd_dst = peer->outbuf + peer->outbuf_size;
917 flt_dst = peer->outbuf;
918
919 pthr->duration = ktime_get();
920
921 /* Copied field is cleared on test launch stage */
922 while (pthr->copied < total_size) {
923 ret = perf_copy_chunk(pthr, flt_dst, flt_src, chunk_size);
924 if (ret) {
925 dev_err(&perf->ntb->dev, "%d: Got error %d on test\n",
926 pthr->tidx, ret);
927 return ret;
928 }
929
930 pthr->copied += chunk_size;
931
932 flt_dst += chunk_size;
933 flt_src += chunk_size;
934 if (flt_dst >= bnd_dst || flt_dst < peer->outbuf) {
935 flt_dst = peer->outbuf;
936 flt_src = pthr->src;
937 }
938
939 /* Give up CPU to give a chance for other threads to use it */
940 schedule();
941 }
942
943 return 0;
944}
945
946static int perf_sync_test(struct perf_thread *pthr)
947{
948 struct perf_ctx *perf = pthr->perf;
949
950 if (!use_dma)
951 goto no_dma_ret;
952
953 wait_event(pthr->dma_wait,
954 (atomic_read(&pthr->dma_sync) == 0 ||
955 atomic_read(&perf->tsync) < 0));
956
957 if (atomic_read(&perf->tsync) < 0)
958 return -EINTR;
959
960no_dma_ret:
961 pthr->duration = ktime_sub(ktime_get(), pthr->duration);
962
963 dev_dbg(&perf->ntb->dev, "%d: copied %llu bytes\n",
964 pthr->tidx, pthr->copied);
965
966 dev_dbg(&perf->ntb->dev, "%d: lasted %llu usecs\n",
967 pthr->tidx, ktime_to_us(pthr->duration));
968
969 dev_dbg(&perf->ntb->dev, "%d: %llu MBytes/s\n", pthr->tidx,
970 div64_u64(pthr->copied, ktime_to_us(pthr->duration)));
971
972 return 0;
973}
974
975static void perf_clear_test(struct perf_thread *pthr)
976{
977 struct perf_ctx *perf = pthr->perf;
978
979 if (!use_dma)
980 goto no_dma_notify;
981
982 /*
983 * If test finished without errors, termination isn't needed.
984 * We call it anyway just to be sure of the transfers completion.
985 */
986 (void)dmaengine_terminate_sync(pthr->dma_chan);
987
988 dma_release_channel(pthr->dma_chan);
989
990no_dma_notify:
991 atomic_dec(&perf->tsync);
992 wake_up(&perf->twait);
993 kfree(pthr->src);
994}
995
996static void perf_thread_work(struct work_struct *work)
997{
998 struct perf_thread *pthr = to_thread_work(work);
999 int ret;
1000
1001 /*
1002 * Perform stages in compliance with use_dma flag value.
1003 * Test status is changed only if error happened, otherwise
1004 * status -ENODATA is kept while test is on-fly. Results
1005 * synchronization is performed only if test fininshed
1006 * without an error or interruption.
1007 */
1008 ret = perf_init_test(pthr);
1009 if (ret) {
1010 pthr->status = ret;
1011 return;
1012 }
1013
1014 ret = perf_run_test(pthr);
1015 if (ret) {
1016 pthr->status = ret;
1017 goto err_clear_test;
1018 }
1019
1020 pthr->status = perf_sync_test(pthr);
1021
1022err_clear_test:
1023 perf_clear_test(pthr);
1024}
1025
1026static int perf_set_tcnt(struct perf_ctx *perf, u8 tcnt)
1027{
1028 if (tcnt == 0 || tcnt > MAX_THREADS_CNT)
1029 return -EINVAL;
1030
1031 if (test_and_set_bit_lock(0, &perf->busy_flag))
1032 return -EBUSY;
1033
1034 perf->tcnt = tcnt;
1035
1036 clear_bit_unlock(0, &perf->busy_flag);
1037
1038 return 0;
1039}
1040
1041static void perf_terminate_test(struct perf_ctx *perf)
1042{
1043 int tidx;
1044
1045 atomic_set(&perf->tsync, -1);
1046 wake_up(&perf->twait);
1047
1048 for (tidx = 0; tidx < MAX_THREADS_CNT; tidx++) {
1049 wake_up(&perf->threads[tidx].dma_wait);
1050 cancel_work_sync(&perf->threads[tidx].work);
1051 }
1052}
1053
1054static int perf_submit_test(struct perf_peer *peer)
1055{
1056 struct perf_ctx *perf = peer->perf;
1057 struct perf_thread *pthr;
1058 int tidx, ret;
1059
Olivier Deprez0e641232021-09-23 10:07:05 +02001060 ret = wait_for_completion_interruptible(&peer->init_comp);
1061 if (ret < 0)
1062 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063
1064 if (test_and_set_bit_lock(0, &perf->busy_flag))
1065 return -EBUSY;
1066
1067 perf->test_peer = peer;
1068 atomic_set(&perf->tsync, perf->tcnt);
1069
1070 for (tidx = 0; tidx < MAX_THREADS_CNT; tidx++) {
1071 pthr = &perf->threads[tidx];
1072
1073 pthr->status = -ENODATA;
1074 pthr->copied = 0;
1075 pthr->duration = ktime_set(0, 0);
1076 if (tidx < perf->tcnt)
1077 (void)queue_work(perf_wq, &pthr->work);
1078 }
1079
1080 ret = wait_event_interruptible(perf->twait,
1081 atomic_read(&perf->tsync) <= 0);
1082 if (ret == -ERESTARTSYS) {
1083 perf_terminate_test(perf);
1084 ret = -EINTR;
1085 }
1086
1087 clear_bit_unlock(0, &perf->busy_flag);
1088
1089 return ret;
1090}
1091
1092static int perf_read_stats(struct perf_ctx *perf, char *buf,
1093 size_t size, ssize_t *pos)
1094{
1095 struct perf_thread *pthr;
1096 int tidx;
1097
1098 if (test_and_set_bit_lock(0, &perf->busy_flag))
1099 return -EBUSY;
1100
1101 (*pos) += scnprintf(buf + *pos, size - *pos,
1102 " Peer %d test statistics:\n", perf->test_peer->pidx);
1103
1104 for (tidx = 0; tidx < MAX_THREADS_CNT; tidx++) {
1105 pthr = &perf->threads[tidx];
1106
1107 if (pthr->status == -ENODATA)
1108 continue;
1109
1110 if (pthr->status) {
1111 (*pos) += scnprintf(buf + *pos, size - *pos,
1112 "%d: error status %d\n", tidx, pthr->status);
1113 continue;
1114 }
1115
1116 (*pos) += scnprintf(buf + *pos, size - *pos,
1117 "%d: copied %llu bytes in %llu usecs, %llu MBytes/s\n",
1118 tidx, pthr->copied, ktime_to_us(pthr->duration),
1119 div64_u64(pthr->copied, ktime_to_us(pthr->duration)));
1120 }
1121
1122 clear_bit_unlock(0, &perf->busy_flag);
1123
1124 return 0;
1125}
1126
1127static void perf_init_threads(struct perf_ctx *perf)
1128{
1129 struct perf_thread *pthr;
1130 int tidx;
1131
1132 perf->tcnt = DEF_THREADS_CNT;
1133 perf->test_peer = &perf->peers[0];
1134 init_waitqueue_head(&perf->twait);
1135
1136 for (tidx = 0; tidx < MAX_THREADS_CNT; tidx++) {
1137 pthr = &perf->threads[tidx];
1138
1139 pthr->perf = perf;
1140 pthr->tidx = tidx;
1141 pthr->status = -ENODATA;
1142 init_waitqueue_head(&pthr->dma_wait);
1143 INIT_WORK(&pthr->work, perf_thread_work);
1144 }
1145}
1146
1147static void perf_clear_threads(struct perf_ctx *perf)
1148{
1149 perf_terminate_test(perf);
1150}
1151
1152/*==============================================================================
1153 * DebugFS nodes
1154 *==============================================================================
1155 */
1156
1157static ssize_t perf_dbgfs_read_info(struct file *filep, char __user *ubuf,
1158 size_t size, loff_t *offp)
1159{
1160 struct perf_ctx *perf = filep->private_data;
1161 struct perf_peer *peer;
1162 size_t buf_size;
1163 ssize_t pos = 0;
1164 int ret, pidx;
1165 char *buf;
1166
1167 buf_size = min_t(size_t, size, 0x1000U);
1168
1169 buf = kmalloc(buf_size, GFP_KERNEL);
1170 if (!buf)
1171 return -ENOMEM;
1172
1173 pos += scnprintf(buf + pos, buf_size - pos,
1174 " Performance measuring tool info:\n\n");
1175
1176 pos += scnprintf(buf + pos, buf_size - pos,
1177 "Local port %d, Global index %d\n", ntb_port_number(perf->ntb),
1178 perf->gidx);
1179 pos += scnprintf(buf + pos, buf_size - pos, "Test status: ");
1180 if (test_bit(0, &perf->busy_flag)) {
1181 pos += scnprintf(buf + pos, buf_size - pos,
1182 "on-fly with port %d (%d)\n",
1183 ntb_peer_port_number(perf->ntb, perf->test_peer->pidx),
1184 perf->test_peer->pidx);
1185 } else {
1186 pos += scnprintf(buf + pos, buf_size - pos, "idle\n");
1187 }
1188
1189 for (pidx = 0; pidx < perf->pcnt; pidx++) {
1190 peer = &perf->peers[pidx];
1191
1192 pos += scnprintf(buf + pos, buf_size - pos,
1193 "Port %d (%d), Global index %d:\n",
1194 ntb_peer_port_number(perf->ntb, peer->pidx), peer->pidx,
1195 peer->gidx);
1196
1197 pos += scnprintf(buf + pos, buf_size - pos,
1198 "\tLink status: %s\n",
1199 test_bit(PERF_STS_LNKUP, &peer->sts) ? "up" : "down");
1200
1201 pos += scnprintf(buf + pos, buf_size - pos,
1202 "\tOut buffer addr 0x%pK\n", peer->outbuf);
1203
1204 pos += scnprintf(buf + pos, buf_size - pos,
1205 "\tOut buffer size %pa\n", &peer->outbuf_size);
1206
1207 pos += scnprintf(buf + pos, buf_size - pos,
1208 "\tOut buffer xlat 0x%016llx[p]\n", peer->outbuf_xlat);
1209
1210 if (!peer->inbuf) {
1211 pos += scnprintf(buf + pos, buf_size - pos,
1212 "\tIn buffer addr: unallocated\n");
1213 continue;
1214 }
1215
1216 pos += scnprintf(buf + pos, buf_size - pos,
1217 "\tIn buffer addr 0x%pK\n", peer->inbuf);
1218
1219 pos += scnprintf(buf + pos, buf_size - pos,
1220 "\tIn buffer size %pa\n", &peer->inbuf_size);
1221
1222 pos += scnprintf(buf + pos, buf_size - pos,
1223 "\tIn buffer xlat %pad[p]\n", &peer->inbuf_xlat);
1224 }
1225
1226 ret = simple_read_from_buffer(ubuf, size, offp, buf, pos);
1227 kfree(buf);
1228
1229 return ret;
1230}
1231
1232static const struct file_operations perf_dbgfs_info = {
1233 .open = simple_open,
1234 .read = perf_dbgfs_read_info
1235};
1236
1237static ssize_t perf_dbgfs_read_run(struct file *filep, char __user *ubuf,
1238 size_t size, loff_t *offp)
1239{
1240 struct perf_ctx *perf = filep->private_data;
1241 ssize_t ret, pos = 0;
1242 char *buf;
1243
1244 buf = kmalloc(PERF_BUF_LEN, GFP_KERNEL);
1245 if (!buf)
1246 return -ENOMEM;
1247
1248 ret = perf_read_stats(perf, buf, PERF_BUF_LEN, &pos);
1249 if (ret)
1250 goto err_free;
1251
1252 ret = simple_read_from_buffer(ubuf, size, offp, buf, pos);
1253err_free:
1254 kfree(buf);
1255
1256 return ret;
1257}
1258
1259static ssize_t perf_dbgfs_write_run(struct file *filep, const char __user *ubuf,
1260 size_t size, loff_t *offp)
1261{
1262 struct perf_ctx *perf = filep->private_data;
1263 struct perf_peer *peer;
1264 int pidx, ret;
1265
1266 ret = kstrtoint_from_user(ubuf, size, 0, &pidx);
1267 if (ret)
1268 return ret;
1269
1270 if (pidx < 0 || pidx >= perf->pcnt)
1271 return -EINVAL;
1272
1273 peer = &perf->peers[pidx];
1274
1275 ret = perf_submit_test(peer);
1276 if (ret)
1277 return ret;
1278
1279 return size;
1280}
1281
1282static const struct file_operations perf_dbgfs_run = {
1283 .open = simple_open,
1284 .read = perf_dbgfs_read_run,
1285 .write = perf_dbgfs_write_run
1286};
1287
1288static ssize_t perf_dbgfs_read_tcnt(struct file *filep, char __user *ubuf,
1289 size_t size, loff_t *offp)
1290{
1291 struct perf_ctx *perf = filep->private_data;
1292 char buf[8];
1293 ssize_t pos;
1294
1295 pos = scnprintf(buf, sizeof(buf), "%hhu\n", perf->tcnt);
1296
1297 return simple_read_from_buffer(ubuf, size, offp, buf, pos);
1298}
1299
1300static ssize_t perf_dbgfs_write_tcnt(struct file *filep,
1301 const char __user *ubuf,
1302 size_t size, loff_t *offp)
1303{
1304 struct perf_ctx *perf = filep->private_data;
1305 int ret;
1306 u8 val;
1307
1308 ret = kstrtou8_from_user(ubuf, size, 0, &val);
1309 if (ret)
1310 return ret;
1311
1312 ret = perf_set_tcnt(perf, val);
1313 if (ret)
1314 return ret;
1315
1316 return size;
1317}
1318
1319static const struct file_operations perf_dbgfs_tcnt = {
1320 .open = simple_open,
1321 .read = perf_dbgfs_read_tcnt,
1322 .write = perf_dbgfs_write_tcnt
1323};
1324
1325static void perf_setup_dbgfs(struct perf_ctx *perf)
1326{
1327 struct pci_dev *pdev = perf->ntb->pdev;
1328
1329 perf->dbgfs_dir = debugfs_create_dir(pci_name(pdev), perf_dbgfs_topdir);
1330 if (!perf->dbgfs_dir) {
1331 dev_warn(&perf->ntb->dev, "DebugFS unsupported\n");
1332 return;
1333 }
1334
1335 debugfs_create_file("info", 0600, perf->dbgfs_dir, perf,
1336 &perf_dbgfs_info);
1337
1338 debugfs_create_file("run", 0600, perf->dbgfs_dir, perf,
1339 &perf_dbgfs_run);
1340
1341 debugfs_create_file("threads_count", 0600, perf->dbgfs_dir, perf,
1342 &perf_dbgfs_tcnt);
1343
1344 /* They are made read-only for test exec safety and integrity */
1345 debugfs_create_u8("chunk_order", 0500, perf->dbgfs_dir, &chunk_order);
1346
1347 debugfs_create_u8("total_order", 0500, perf->dbgfs_dir, &total_order);
1348
1349 debugfs_create_bool("use_dma", 0500, perf->dbgfs_dir, &use_dma);
1350}
1351
1352static void perf_clear_dbgfs(struct perf_ctx *perf)
1353{
1354 debugfs_remove_recursive(perf->dbgfs_dir);
1355}
1356
1357/*==============================================================================
1358 * Basic driver initialization
1359 *==============================================================================
1360 */
1361
1362static struct perf_ctx *perf_create_data(struct ntb_dev *ntb)
1363{
1364 struct perf_ctx *perf;
1365
1366 perf = devm_kzalloc(&ntb->dev, sizeof(*perf), GFP_KERNEL);
1367 if (!perf)
1368 return ERR_PTR(-ENOMEM);
1369
1370 perf->pcnt = ntb_peer_port_count(ntb);
1371 perf->peers = devm_kcalloc(&ntb->dev, perf->pcnt, sizeof(*perf->peers),
1372 GFP_KERNEL);
1373 if (!perf->peers)
1374 return ERR_PTR(-ENOMEM);
1375
1376 perf->ntb = ntb;
1377
1378 return perf;
1379}
1380
1381static int perf_setup_peer_mw(struct perf_peer *peer)
1382{
1383 struct perf_ctx *perf = peer->perf;
1384 phys_addr_t phys_addr;
1385 int ret;
1386
1387 /* Get outbound MW parameters and map it */
David Brazdil0f672f62019-12-10 10:32:29 +00001388 ret = ntb_peer_mw_get_addr(perf->ntb, perf->gidx, &phys_addr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001389 &peer->outbuf_size);
1390 if (ret)
1391 return ret;
1392
1393 peer->outbuf = devm_ioremap_wc(&perf->ntb->dev, phys_addr,
1394 peer->outbuf_size);
1395 if (!peer->outbuf)
1396 return -ENOMEM;
1397
1398 if (max_mw_size && peer->outbuf_size > max_mw_size) {
1399 peer->outbuf_size = max_mw_size;
1400 dev_warn(&peer->perf->ntb->dev,
1401 "Peer %d outbuf reduced to %pa\n", peer->pidx,
1402 &peer->outbuf_size);
1403 }
1404
1405 return 0;
1406}
1407
1408static int perf_init_peers(struct perf_ctx *perf)
1409{
1410 struct perf_peer *peer;
1411 int pidx, lport, ret;
1412
1413 lport = ntb_port_number(perf->ntb);
1414 perf->gidx = -1;
1415 for (pidx = 0; pidx < perf->pcnt; pidx++) {
1416 peer = &perf->peers[pidx];
1417
1418 peer->perf = perf;
1419 peer->pidx = pidx;
1420 if (lport < ntb_peer_port_number(perf->ntb, pidx)) {
1421 if (perf->gidx == -1)
1422 perf->gidx = pidx;
1423 peer->gidx = pidx + 1;
1424 } else {
1425 peer->gidx = pidx;
1426 }
1427 INIT_WORK(&peer->service, perf_service_work);
Olivier Deprez0e641232021-09-23 10:07:05 +02001428 init_completion(&peer->init_comp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001429 }
1430 if (perf->gidx == -1)
1431 perf->gidx = pidx;
1432
Olivier Deprez0e641232021-09-23 10:07:05 +02001433 /*
1434 * Hardware with only two ports may not have unique port
1435 * numbers. In this case, the gidxs should all be zero.
1436 */
1437 if (perf->pcnt == 1 && ntb_port_number(perf->ntb) == 0 &&
1438 ntb_peer_port_number(perf->ntb, 0) == 0) {
1439 perf->gidx = 0;
1440 perf->peers[0].gidx = 0;
1441 }
1442
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001443 for (pidx = 0; pidx < perf->pcnt; pidx++) {
1444 ret = perf_setup_peer_mw(&perf->peers[pidx]);
1445 if (ret)
1446 return ret;
1447 }
1448
1449 dev_dbg(&perf->ntb->dev, "Global port index %d\n", perf->gidx);
1450
1451 return 0;
1452}
1453
1454static int perf_probe(struct ntb_client *client, struct ntb_dev *ntb)
1455{
1456 struct perf_ctx *perf;
1457 int ret;
1458
1459 perf = perf_create_data(ntb);
1460 if (IS_ERR(perf))
1461 return PTR_ERR(perf);
1462
1463 ret = perf_init_peers(perf);
1464 if (ret)
1465 return ret;
1466
1467 perf_init_threads(perf);
1468
1469 ret = perf_init_service(perf);
1470 if (ret)
1471 return ret;
1472
1473 ret = perf_enable_service(perf);
1474 if (ret)
1475 return ret;
1476
1477 perf_setup_dbgfs(perf);
1478
1479 return 0;
1480}
1481
1482static void perf_remove(struct ntb_client *client, struct ntb_dev *ntb)
1483{
1484 struct perf_ctx *perf = ntb->ctx;
1485
1486 perf_clear_dbgfs(perf);
1487
1488 perf_disable_service(perf);
1489
1490 perf_clear_threads(perf);
1491}
1492
1493static struct ntb_client perf_client = {
1494 .ops = {
1495 .probe = perf_probe,
1496 .remove = perf_remove
1497 }
1498};
1499
1500static int __init perf_init(void)
1501{
1502 int ret;
1503
1504 if (chunk_order > MAX_CHUNK_ORDER) {
1505 chunk_order = MAX_CHUNK_ORDER;
1506 pr_info("Chunk order reduced to %hhu\n", chunk_order);
1507 }
1508
1509 if (total_order < chunk_order) {
1510 total_order = chunk_order;
1511 pr_info("Total data order reduced to %hhu\n", total_order);
1512 }
1513
1514 perf_wq = alloc_workqueue("perf_wq", WQ_UNBOUND | WQ_SYSFS, 0);
1515 if (!perf_wq)
1516 return -ENOMEM;
1517
1518 if (debugfs_initialized())
1519 perf_dbgfs_topdir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1520
1521 ret = ntb_register_client(&perf_client);
1522 if (ret) {
1523 debugfs_remove_recursive(perf_dbgfs_topdir);
1524 destroy_workqueue(perf_wq);
1525 }
1526
1527 return ret;
1528}
1529module_init(perf_init);
1530
1531static void __exit perf_exit(void)
1532{
1533 ntb_unregister_client(&perf_client);
1534 debugfs_remove_recursive(perf_dbgfs_topdir);
1535 destroy_workqueue(perf_wq);
1536}
1537module_exit(perf_exit);