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.
7 * Copyright(c) 2015 Intel Corporation. All rights reserved.
8 * Copyright(c) 2017 T-Platforms. All Rights Reserved.
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.
16 * Copyright(c) 2015 Intel Corporation. All rights reserved.
17 * Copyright(c) 2017 T-Platforms. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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
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.
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.
45 * PCIe NTB Perf Linux driver
49 * How to use this tool, by example.
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
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
62 * root@self# cat $DBG_DIR/info
63 *-----------------------------------------------------------------------------
64 * Eg: start performance test with peer (index 0) and get the test metrics
66 * root@self# echo 0 > $DBG_DIR/run
67 * root@self# cat $DBG_DIR/run
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>
87 #define DRIVER_NAME "ntb_perf"
88 #define DRIVER_VERSION "2.0"
90 MODULE_LICENSE("Dual BSD/GPL");
91 MODULE_VERSION(DRIVER_VERSION
);
92 MODULE_AUTHOR("Dave Jiang <dave.jiang@intel.com>");
93 MODULE_DESCRIPTION("PCIe NTB Performance Measurement Tool");
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 */
100 #define DMA_TRIES 100
101 #define DMA_MDELAY 10
103 #define MSG_TRIES 500
104 #define MSG_UDELAY_LOW 1000
105 #define MSG_UDELAY_HIGH 2000
107 #define PERF_BUF_LEN 1024
109 static unsigned long max_mw_size
;
110 module_param(max_mw_size
, ulong
, 0644);
111 MODULE_PARM_DESC(max_mw_size
, "Upper limit of memory window size");
113 static unsigned char chunk_order
= 19; /* 512K */
114 module_param(chunk_order
, byte
, 0644);
115 MODULE_PARM_DESC(chunk_order
, "Data chunk order [2^n] to transfer");
117 static unsigned char total_order
= 30; /* 1G */
118 module_param(total_order
, byte
, 0644);
119 MODULE_PARM_DESC(total_order
, "Total data order [2^n] to transfer");
121 static bool use_dma
; /* default to 0 */
122 module_param(use_dma
, bool, 0644);
123 MODULE_PARM_DESC(use_dma
, "Use DMA engine to measure performance");
125 /*==============================================================================
126 * Perf driver data definition
127 *==============================================================================
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 */
144 struct perf_ctx
*perf
;
148 /* Outbound MW params */
150 resource_size_t outbuf_size
;
151 void __iomem
*outbuf
;
153 /* Inbound MW params */
154 dma_addr_t inbuf_xlat
;
155 resource_size_t inbuf_size
;
158 /* NTB connection setup service */
159 struct work_struct service
;
162 struct completion init_comp
;
164 #define to_peer_service(__work) \
165 container_of(__work, struct perf_peer, service)
168 struct perf_ctx
*perf
;
171 /* DMA-based test sync parameters */
173 wait_queue_head_t dma_wait
;
174 struct dma_chan
*dma_chan
;
176 /* Data source and measured statistics */
181 struct work_struct work
;
183 #define to_thread_work(__work) \
184 container_of(__work, struct perf_thread, work)
189 /* Global device index and peers descriptors */
192 struct perf_peer
*peers
;
194 /* Performance measuring work-threads interface */
195 unsigned long busy_flag
;
196 wait_queue_head_t twait
;
199 struct perf_peer
*test_peer
;
200 struct perf_thread threads
[MAX_THREADS_CNT
];
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
,
207 struct dentry
*dbgfs_dir
;
211 * Scratchpads-base commands interface
213 #define PERF_SPAD_CNT(_pcnt) \
215 #define PERF_SPAD_CMD(_gidx) \
217 #define PERF_SPAD_LDATA(_gidx) \
219 #define PERF_SPAD_HDATA(_gidx) \
221 #define PERF_SPAD_NOTIFY(_gidx) \
225 * Messages-base commands interface
227 #define PERF_MSG_CNT 3
228 #define PERF_MSG_CMD 0
229 #define PERF_MSG_LDATA 1
230 #define PERF_MSG_HDATA 2
232 /*==============================================================================
233 * Static data declarations
234 *==============================================================================
237 static struct dentry
*perf_dbgfs_topdir
;
239 static struct workqueue_struct
*perf_wq __read_mostly
;
241 /*==============================================================================
242 * NTB cross-link commands execution service
243 *==============================================================================
246 static void perf_terminate_test(struct perf_ctx
*perf
);
248 static inline bool perf_link_is_up(struct perf_peer
*peer
)
252 link
= ntb_link_is_up(peer
->perf
->ntb
, NULL
, NULL
);
253 return !!(link
& BIT_ULL_MASK(peer
->pidx
));
256 static int perf_spad_cmd_send(struct perf_peer
*peer
, enum perf_cmd cmd
,
259 struct perf_ctx
*perf
= peer
->perf
;
263 dev_dbg(&perf
->ntb
->dev
, "CMD send: %d 0x%llx\n", cmd
, data
);
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.
272 for (try = 0; try < MSG_TRIES
; try++) {
273 if (!perf_link_is_up(peer
))
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
);
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
));
290 ntb_peer_spad_write(perf
->ntb
, peer
->pidx
,
291 PERF_SPAD_CMD(perf
->gidx
),
294 ntb_peer_db_set(perf
->ntb
, PERF_SPAD_NOTIFY(peer
->gidx
));
296 dev_dbg(&perf
->ntb
->dev
, "DB ring peer %#llx\n",
297 PERF_SPAD_NOTIFY(peer
->gidx
));
302 return try < MSG_TRIES
? 0 : -EAGAIN
;
305 static int perf_spad_cmd_recv(struct perf_ctx
*perf
, int *pidx
,
306 enum perf_cmd
*cmd
, u64
*data
)
308 struct perf_peer
*peer
;
311 ntb_db_clear(perf
->ntb
, PERF_SPAD_NOTIFY(perf
->gidx
));
314 * We start scanning all over, since cleared DB may have been set
315 * by any peer. Yes, it makes peer with smaller index being
316 * serviced with greater priority, but it's convenient for spad
317 * and message code unification and simplicity.
319 for (*pidx
= 0; *pidx
< perf
->pcnt
; (*pidx
)++) {
320 peer
= &perf
->peers
[*pidx
];
322 if (!perf_link_is_up(peer
))
325 val
= ntb_spad_read(perf
->ntb
, PERF_SPAD_CMD(peer
->gidx
));
326 if (val
== PERF_CMD_INVAL
)
331 val
= ntb_spad_read(perf
->ntb
, PERF_SPAD_LDATA(peer
->gidx
));
334 val
= ntb_spad_read(perf
->ntb
, PERF_SPAD_HDATA(peer
->gidx
));
335 *data
|= (u64
)val
<< 32;
337 /* Next command can be retrieved from now */
338 ntb_spad_write(perf
->ntb
, PERF_SPAD_CMD(peer
->gidx
),
341 dev_dbg(&perf
->ntb
->dev
, "CMD recv: %d 0x%llx\n", *cmd
, *data
);
349 static int perf_msg_cmd_send(struct perf_peer
*peer
, enum perf_cmd cmd
,
352 struct perf_ctx
*perf
= peer
->perf
;
356 dev_dbg(&perf
->ntb
->dev
, "CMD send: %d 0x%llx\n", cmd
, data
);
359 * Perform predefined number of attempts before give up. Message
360 * registers are free of race-condition problem when accessed
361 * from different ports, so we don't need splitting registers
362 * by global device index. We also won't have local locking,
363 * since the method is used from service work only.
365 outbits
= ntb_msg_outbits(perf
->ntb
);
366 for (try = 0; try < MSG_TRIES
; try++) {
367 if (!perf_link_is_up(peer
))
370 ret
= ntb_msg_clear_sts(perf
->ntb
, outbits
);
374 ntb_peer_msg_write(perf
->ntb
, peer
->pidx
, PERF_MSG_LDATA
,
375 lower_32_bits(data
));
377 if (ntb_msg_read_sts(perf
->ntb
) & outbits
) {
378 usleep_range(MSG_UDELAY_LOW
, MSG_UDELAY_HIGH
);
382 ntb_peer_msg_write(perf
->ntb
, peer
->pidx
, PERF_MSG_HDATA
,
383 upper_32_bits(data
));
386 /* This call shall trigger peer message event */
387 ntb_peer_msg_write(perf
->ntb
, peer
->pidx
, PERF_MSG_CMD
, cmd
);
392 return try < MSG_TRIES
? 0 : -EAGAIN
;
395 static int perf_msg_cmd_recv(struct perf_ctx
*perf
, int *pidx
,
396 enum perf_cmd
*cmd
, u64
*data
)
401 inbits
= ntb_msg_inbits(perf
->ntb
);
403 if (hweight64(ntb_msg_read_sts(perf
->ntb
) & inbits
) < 3)
406 val
= ntb_msg_read(perf
->ntb
, pidx
, PERF_MSG_CMD
);
409 val
= ntb_msg_read(perf
->ntb
, pidx
, PERF_MSG_LDATA
);
412 val
= ntb_msg_read(perf
->ntb
, pidx
, PERF_MSG_HDATA
);
413 *data
|= (u64
)val
<< 32;
415 /* Next command can be retrieved from now */
416 ntb_msg_clear_sts(perf
->ntb
, inbits
);
418 dev_dbg(&perf
->ntb
->dev
, "CMD recv: %d 0x%llx\n", *cmd
, *data
);
423 static int perf_cmd_send(struct perf_peer
*peer
, enum perf_cmd cmd
, u64 data
)
425 struct perf_ctx
*perf
= peer
->perf
;
427 if (cmd
== PERF_CMD_SSIZE
|| cmd
== PERF_CMD_SXLAT
)
428 return perf
->cmd_send(peer
, cmd
, data
);
430 dev_err(&perf
->ntb
->dev
, "Send invalid command\n");
434 static int perf_cmd_exec(struct perf_peer
*peer
, enum perf_cmd cmd
)
444 dev_err(&peer
->perf
->ntb
->dev
, "Exec invalid command\n");
448 /* No need of memory barrier, since bit ops have invernal lock */
449 set_bit(cmd
, &peer
->sts
);
451 dev_dbg(&peer
->perf
->ntb
->dev
, "CMD exec: %d\n", cmd
);
453 (void)queue_work(system_highpri_wq
, &peer
->service
);
458 static int perf_cmd_recv(struct perf_ctx
*perf
)
460 struct perf_peer
*peer
;
464 while (!(ret
= perf
->cmd_recv(perf
, &pidx
, &cmd
, &data
))) {
465 peer
= &perf
->peers
[pidx
];
469 peer
->inbuf_size
= data
;
470 return perf_cmd_exec(peer
, PERF_CMD_RSIZE
);
472 peer
->outbuf_xlat
= data
;
473 return perf_cmd_exec(peer
, PERF_CMD_RXLAT
);
475 dev_err(&perf
->ntb
->dev
, "Recv invalid command\n");
480 /* Return 0 if no data left to process, otherwise an error */
481 return ret
== -ENODATA
? 0 : ret
;
484 static void perf_link_event(void *ctx
)
486 struct perf_ctx
*perf
= ctx
;
487 struct perf_peer
*peer
;
491 for (pidx
= 0; pidx
< perf
->pcnt
; pidx
++) {
492 peer
= &perf
->peers
[pidx
];
494 lnk_up
= perf_link_is_up(peer
);
497 !test_and_set_bit(PERF_STS_LNKUP
, &peer
->sts
)) {
498 perf_cmd_exec(peer
, PERF_CMD_SSIZE
);
499 } else if (!lnk_up
&&
500 test_and_clear_bit(PERF_STS_LNKUP
, &peer
->sts
)) {
501 perf_cmd_exec(peer
, PERF_CMD_CLEAR
);
506 static void perf_db_event(void *ctx
, int vec
)
508 struct perf_ctx
*perf
= ctx
;
510 dev_dbg(&perf
->ntb
->dev
, "DB vec %d mask %#llx bits %#llx\n", vec
,
511 ntb_db_vector_mask(perf
->ntb
, vec
), ntb_db_read(perf
->ntb
));
513 /* Just receive all available commands */
514 (void)perf_cmd_recv(perf
);
517 static void perf_msg_event(void *ctx
)
519 struct perf_ctx
*perf
= ctx
;
521 dev_dbg(&perf
->ntb
->dev
, "Msg status bits %#llx\n",
522 ntb_msg_read_sts(perf
->ntb
));
524 /* Messages are only sent one-by-one */
525 (void)perf_cmd_recv(perf
);
528 static const struct ntb_ctx_ops perf_ops
= {
529 .link_event
= perf_link_event
,
530 .db_event
= perf_db_event
,
531 .msg_event
= perf_msg_event
534 static void perf_free_outbuf(struct perf_peer
*peer
)
536 (void)ntb_peer_mw_clear_trans(peer
->perf
->ntb
, peer
->pidx
, peer
->gidx
);
539 static int perf_setup_outbuf(struct perf_peer
*peer
)
541 struct perf_ctx
*perf
= peer
->perf
;
544 /* Outbuf size can be unaligned due to custom max_mw_size */
545 ret
= ntb_peer_mw_set_trans(perf
->ntb
, peer
->pidx
, peer
->gidx
,
546 peer
->outbuf_xlat
, peer
->outbuf_size
);
548 dev_err(&perf
->ntb
->dev
, "Failed to set outbuf translation\n");
552 /* Initialization is finally done */
553 set_bit(PERF_STS_DONE
, &peer
->sts
);
554 complete_all(&peer
->init_comp
);
559 static void perf_free_inbuf(struct perf_peer
*peer
)
564 (void)ntb_mw_clear_trans(peer
->perf
->ntb
, peer
->pidx
, peer
->gidx
);
565 dma_free_coherent(&peer
->perf
->ntb
->pdev
->dev
, peer
->inbuf_size
,
566 peer
->inbuf
, peer
->inbuf_xlat
);
570 static int perf_setup_inbuf(struct perf_peer
*peer
)
572 resource_size_t xlat_align
, size_align
, size_max
;
573 struct perf_ctx
*perf
= peer
->perf
;
576 /* Get inbound MW parameters */
577 ret
= ntb_mw_get_align(perf
->ntb
, peer
->pidx
, perf
->gidx
,
578 &xlat_align
, &size_align
, &size_max
);
580 dev_err(&perf
->ntb
->dev
, "Couldn't get inbuf restrictions\n");
584 if (peer
->inbuf_size
> size_max
) {
585 dev_err(&perf
->ntb
->dev
, "Too big inbuf size %pa > %pa\n",
586 &peer
->inbuf_size
, &size_max
);
590 peer
->inbuf_size
= round_up(peer
->inbuf_size
, size_align
);
592 perf_free_inbuf(peer
);
594 peer
->inbuf
= dma_alloc_coherent(&perf
->ntb
->pdev
->dev
,
595 peer
->inbuf_size
, &peer
->inbuf_xlat
,
598 dev_err(&perf
->ntb
->dev
, "Failed to alloc inbuf of %pa\n",
602 if (!IS_ALIGNED(peer
->inbuf_xlat
, xlat_align
)) {
603 dev_err(&perf
->ntb
->dev
, "Unaligned inbuf allocated\n");
607 ret
= ntb_mw_set_trans(perf
->ntb
, peer
->pidx
, peer
->gidx
,
608 peer
->inbuf_xlat
, peer
->inbuf_size
);
610 dev_err(&perf
->ntb
->dev
, "Failed to set inbuf translation\n");
615 * We submit inbuf xlat transmission cmd for execution here to follow
616 * the code architecture, even though this method is called from service
617 * work itself so the command will be executed right after it returns.
619 (void)perf_cmd_exec(peer
, PERF_CMD_SXLAT
);
624 perf_free_inbuf(peer
);
629 static void perf_service_work(struct work_struct
*work
)
631 struct perf_peer
*peer
= to_peer_service(work
);
633 if (test_and_clear_bit(PERF_CMD_SSIZE
, &peer
->sts
))
634 perf_cmd_send(peer
, PERF_CMD_SSIZE
, peer
->outbuf_size
);
636 if (test_and_clear_bit(PERF_CMD_RSIZE
, &peer
->sts
))
637 perf_setup_inbuf(peer
);
639 if (test_and_clear_bit(PERF_CMD_SXLAT
, &peer
->sts
))
640 perf_cmd_send(peer
, PERF_CMD_SXLAT
, peer
->inbuf_xlat
);
642 if (test_and_clear_bit(PERF_CMD_RXLAT
, &peer
->sts
))
643 perf_setup_outbuf(peer
);
645 if (test_and_clear_bit(PERF_CMD_CLEAR
, &peer
->sts
)) {
646 init_completion(&peer
->init_comp
);
647 clear_bit(PERF_STS_DONE
, &peer
->sts
);
648 if (test_bit(0, &peer
->perf
->busy_flag
) &&
649 peer
== peer
->perf
->test_peer
) {
650 dev_warn(&peer
->perf
->ntb
->dev
,
651 "Freeing while test on-fly\n");
652 perf_terminate_test(peer
->perf
);
654 perf_free_outbuf(peer
);
655 perf_free_inbuf(peer
);
659 static int perf_init_service(struct perf_ctx
*perf
)
663 if (ntb_peer_mw_count(perf
->ntb
) < perf
->pcnt
) {
664 dev_err(&perf
->ntb
->dev
, "Not enough memory windows\n");
668 if (ntb_msg_count(perf
->ntb
) >= PERF_MSG_CNT
) {
669 perf
->cmd_send
= perf_msg_cmd_send
;
670 perf
->cmd_recv
= perf_msg_cmd_recv
;
672 dev_dbg(&perf
->ntb
->dev
, "Message service initialized\n");
677 dev_dbg(&perf
->ntb
->dev
, "Message service unsupported\n");
679 mask
= GENMASK_ULL(perf
->pcnt
, 0);
680 if (ntb_spad_count(perf
->ntb
) >= PERF_SPAD_CNT(perf
->pcnt
) &&
681 (ntb_db_valid_mask(perf
->ntb
) & mask
) == mask
) {
682 perf
->cmd_send
= perf_spad_cmd_send
;
683 perf
->cmd_recv
= perf_spad_cmd_recv
;
685 dev_dbg(&perf
->ntb
->dev
, "Scratchpad service initialized\n");
690 dev_dbg(&perf
->ntb
->dev
, "Scratchpad service unsupported\n");
692 dev_err(&perf
->ntb
->dev
, "Command services unsupported\n");
697 static int perf_enable_service(struct perf_ctx
*perf
)
702 mask
= ntb_db_valid_mask(perf
->ntb
);
703 (void)ntb_db_set_mask(perf
->ntb
, mask
);
705 ret
= ntb_set_ctx(perf
->ntb
, perf
, &perf_ops
);
709 if (perf
->cmd_send
== perf_msg_cmd_send
) {
712 inbits
= ntb_msg_inbits(perf
->ntb
);
713 outbits
= ntb_msg_outbits(perf
->ntb
);
714 (void)ntb_msg_set_mask(perf
->ntb
, inbits
| outbits
);
716 incmd_bit
= BIT_ULL(__ffs64(inbits
));
717 ret
= ntb_msg_clear_mask(perf
->ntb
, incmd_bit
);
719 dev_dbg(&perf
->ntb
->dev
, "MSG sts unmasked %#llx\n", incmd_bit
);
721 scnt
= ntb_spad_count(perf
->ntb
);
722 for (sidx
= 0; sidx
< scnt
; sidx
++)
723 ntb_spad_write(perf
->ntb
, sidx
, PERF_CMD_INVAL
);
724 incmd_bit
= PERF_SPAD_NOTIFY(perf
->gidx
);
725 ret
= ntb_db_clear_mask(perf
->ntb
, incmd_bit
);
727 dev_dbg(&perf
->ntb
->dev
, "DB bits unmasked %#llx\n", incmd_bit
);
730 ntb_clear_ctx(perf
->ntb
);
734 ntb_link_enable(perf
->ntb
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
735 /* Might be not necessary */
736 ntb_link_event(perf
->ntb
);
741 static void perf_disable_service(struct perf_ctx
*perf
)
745 ntb_link_disable(perf
->ntb
);
747 if (perf
->cmd_send
== perf_msg_cmd_send
) {
750 inbits
= ntb_msg_inbits(perf
->ntb
);
751 (void)ntb_msg_set_mask(perf
->ntb
, inbits
);
753 (void)ntb_db_set_mask(perf
->ntb
, PERF_SPAD_NOTIFY(perf
->gidx
));
756 ntb_clear_ctx(perf
->ntb
);
758 for (pidx
= 0; pidx
< perf
->pcnt
; pidx
++)
759 perf_cmd_exec(&perf
->peers
[pidx
], PERF_CMD_CLEAR
);
761 for (pidx
= 0; pidx
< perf
->pcnt
; pidx
++)
762 flush_work(&perf
->peers
[pidx
].service
);
765 /*==============================================================================
766 * Performance measuring work-thread
767 *==============================================================================
770 static void perf_dma_copy_callback(void *data
)
772 struct perf_thread
*pthr
= data
;
774 atomic_dec(&pthr
->dma_sync
);
775 wake_up(&pthr
->dma_wait
);
778 static int perf_copy_chunk(struct perf_thread
*pthr
,
779 void __iomem
*dst
, void *src
, size_t len
)
781 struct dma_async_tx_descriptor
*tx
;
782 struct dmaengine_unmap_data
*unmap
;
783 struct device
*dma_dev
;
784 int try = 0, ret
= 0;
787 memcpy_toio(dst
, src
, len
);
788 goto ret_check_tsync
;
791 dma_dev
= pthr
->dma_chan
->device
->dev
;
793 if (!is_dma_copy_aligned(pthr
->dma_chan
->device
, offset_in_page(src
),
794 offset_in_page(dst
), len
))
797 unmap
= dmaengine_get_unmap_data(dma_dev
, 2, GFP_NOWAIT
);
802 unmap
->addr
[0] = dma_map_page(dma_dev
, virt_to_page(src
),
803 offset_in_page(src
), len
, DMA_TO_DEVICE
);
804 if (dma_mapping_error(dma_dev
, unmap
->addr
[0])) {
806 goto err_free_resource
;
810 unmap
->addr
[1] = dma_map_page(dma_dev
, virt_to_page(dst
),
811 offset_in_page(dst
), len
, DMA_FROM_DEVICE
);
812 if (dma_mapping_error(dma_dev
, unmap
->addr
[1])) {
814 goto err_free_resource
;
819 tx
= dmaengine_prep_dma_memcpy(pthr
->dma_chan
, unmap
->addr
[1],
820 unmap
->addr
[0], len
, DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
823 } while (!tx
&& (try++ < DMA_TRIES
));
827 goto err_free_resource
;
830 tx
->callback
= perf_dma_copy_callback
;
831 tx
->callback_param
= pthr
;
832 dma_set_unmap(tx
, unmap
);
834 ret
= dma_submit_error(dmaengine_submit(tx
));
836 dmaengine_unmap_put(unmap
);
837 goto err_free_resource
;
840 dmaengine_unmap_put(unmap
);
842 atomic_inc(&pthr
->dma_sync
);
843 dma_async_issue_pending(pthr
->dma_chan
);
846 return likely(atomic_read(&pthr
->perf
->tsync
) > 0) ? 0 : -EINTR
;
849 dmaengine_unmap_put(unmap
);
854 static bool perf_dma_filter(struct dma_chan
*chan
, void *data
)
856 struct perf_ctx
*perf
= data
;
859 node
= dev_to_node(&perf
->ntb
->dev
);
861 return node
== NUMA_NO_NODE
|| node
== dev_to_node(chan
->device
->dev
);
864 static int perf_init_test(struct perf_thread
*pthr
)
866 struct perf_ctx
*perf
= pthr
->perf
;
867 dma_cap_mask_t dma_mask
;
869 pthr
->src
= kmalloc_node(perf
->test_peer
->outbuf_size
, GFP_KERNEL
,
870 dev_to_node(&perf
->ntb
->dev
));
874 get_random_bytes(pthr
->src
, perf
->test_peer
->outbuf_size
);
879 dma_cap_zero(dma_mask
);
880 dma_cap_set(DMA_MEMCPY
, dma_mask
);
881 pthr
->dma_chan
= dma_request_channel(dma_mask
, perf_dma_filter
, perf
);
882 if (!pthr
->dma_chan
) {
883 dev_err(&perf
->ntb
->dev
, "%d: Failed to get DMA channel\n",
885 atomic_dec(&perf
->tsync
);
886 wake_up(&perf
->twait
);
891 atomic_set(&pthr
->dma_sync
, 0);
896 static int perf_run_test(struct perf_thread
*pthr
)
898 struct perf_peer
*peer
= pthr
->perf
->test_peer
;
899 struct perf_ctx
*perf
= pthr
->perf
;
900 void __iomem
*flt_dst
, *bnd_dst
;
901 u64 total_size
, chunk_size
;
905 total_size
= 1ULL << total_order
;
906 chunk_size
= 1ULL << chunk_order
;
907 chunk_size
= min_t(u64
, peer
->outbuf_size
, chunk_size
);
910 bnd_dst
= peer
->outbuf
+ peer
->outbuf_size
;
911 flt_dst
= peer
->outbuf
;
913 pthr
->duration
= ktime_get();
915 /* Copied field is cleared on test launch stage */
916 while (pthr
->copied
< total_size
) {
917 ret
= perf_copy_chunk(pthr
, flt_dst
, flt_src
, chunk_size
);
919 dev_err(&perf
->ntb
->dev
, "%d: Got error %d on test\n",
924 pthr
->copied
+= chunk_size
;
926 flt_dst
+= chunk_size
;
927 flt_src
+= chunk_size
;
928 if (flt_dst
>= bnd_dst
|| flt_dst
< peer
->outbuf
) {
929 flt_dst
= peer
->outbuf
;
933 /* Give up CPU to give a chance for other threads to use it */
940 static int perf_sync_test(struct perf_thread
*pthr
)
942 struct perf_ctx
*perf
= pthr
->perf
;
947 wait_event(pthr
->dma_wait
,
948 (atomic_read(&pthr
->dma_sync
) == 0 ||
949 atomic_read(&perf
->tsync
) < 0));
951 if (atomic_read(&perf
->tsync
) < 0)
955 pthr
->duration
= ktime_sub(ktime_get(), pthr
->duration
);
957 dev_dbg(&perf
->ntb
->dev
, "%d: copied %llu bytes\n",
958 pthr
->tidx
, pthr
->copied
);
960 dev_dbg(&perf
->ntb
->dev
, "%d: lasted %llu usecs\n",
961 pthr
->tidx
, ktime_to_us(pthr
->duration
));
963 dev_dbg(&perf
->ntb
->dev
, "%d: %llu MBytes/s\n", pthr
->tidx
,
964 div64_u64(pthr
->copied
, ktime_to_us(pthr
->duration
)));
969 static void perf_clear_test(struct perf_thread
*pthr
)
971 struct perf_ctx
*perf
= pthr
->perf
;
977 * If test finished without errors, termination isn't needed.
978 * We call it anyway just to be sure of the transfers completion.
980 (void)dmaengine_terminate_sync(pthr
->dma_chan
);
982 dma_release_channel(pthr
->dma_chan
);
985 atomic_dec(&perf
->tsync
);
986 wake_up(&perf
->twait
);
990 static void perf_thread_work(struct work_struct
*work
)
992 struct perf_thread
*pthr
= to_thread_work(work
);
996 * Perform stages in compliance with use_dma flag value.
997 * Test status is changed only if error happened, otherwise
998 * status -ENODATA is kept while test is on-fly. Results
999 * synchronization is performed only if test fininshed
1000 * without an error or interruption.
1002 ret
= perf_init_test(pthr
);
1008 ret
= perf_run_test(pthr
);
1011 goto err_clear_test
;
1014 pthr
->status
= perf_sync_test(pthr
);
1017 perf_clear_test(pthr
);
1020 static int perf_set_tcnt(struct perf_ctx
*perf
, u8 tcnt
)
1022 if (tcnt
== 0 || tcnt
> MAX_THREADS_CNT
)
1025 if (test_and_set_bit_lock(0, &perf
->busy_flag
))
1030 clear_bit_unlock(0, &perf
->busy_flag
);
1035 static void perf_terminate_test(struct perf_ctx
*perf
)
1039 atomic_set(&perf
->tsync
, -1);
1040 wake_up(&perf
->twait
);
1042 for (tidx
= 0; tidx
< MAX_THREADS_CNT
; tidx
++) {
1043 wake_up(&perf
->threads
[tidx
].dma_wait
);
1044 cancel_work_sync(&perf
->threads
[tidx
].work
);
1048 static int perf_submit_test(struct perf_peer
*peer
)
1050 struct perf_ctx
*perf
= peer
->perf
;
1051 struct perf_thread
*pthr
;
1054 ret
= wait_for_completion_interruptible(&peer
->init_comp
);
1058 if (test_and_set_bit_lock(0, &perf
->busy_flag
))
1061 perf
->test_peer
= peer
;
1062 atomic_set(&perf
->tsync
, perf
->tcnt
);
1064 for (tidx
= 0; tidx
< MAX_THREADS_CNT
; tidx
++) {
1065 pthr
= &perf
->threads
[tidx
];
1067 pthr
->status
= -ENODATA
;
1069 pthr
->duration
= ktime_set(0, 0);
1070 if (tidx
< perf
->tcnt
)
1071 (void)queue_work(perf_wq
, &pthr
->work
);
1074 ret
= wait_event_interruptible(perf
->twait
,
1075 atomic_read(&perf
->tsync
) <= 0);
1076 if (ret
== -ERESTARTSYS
) {
1077 perf_terminate_test(perf
);
1081 clear_bit_unlock(0, &perf
->busy_flag
);
1086 static int perf_read_stats(struct perf_ctx
*perf
, char *buf
,
1087 size_t size
, ssize_t
*pos
)
1089 struct perf_thread
*pthr
;
1092 if (test_and_set_bit_lock(0, &perf
->busy_flag
))
1095 (*pos
) += scnprintf(buf
+ *pos
, size
- *pos
,
1096 " Peer %d test statistics:\n", perf
->test_peer
->pidx
);
1098 for (tidx
= 0; tidx
< MAX_THREADS_CNT
; tidx
++) {
1099 pthr
= &perf
->threads
[tidx
];
1101 if (pthr
->status
== -ENODATA
)
1105 (*pos
) += scnprintf(buf
+ *pos
, size
- *pos
,
1106 "%d: error status %d\n", tidx
, pthr
->status
);
1110 (*pos
) += scnprintf(buf
+ *pos
, size
- *pos
,
1111 "%d: copied %llu bytes in %llu usecs, %llu MBytes/s\n",
1112 tidx
, pthr
->copied
, ktime_to_us(pthr
->duration
),
1113 div64_u64(pthr
->copied
, ktime_to_us(pthr
->duration
)));
1116 clear_bit_unlock(0, &perf
->busy_flag
);
1121 static void perf_init_threads(struct perf_ctx
*perf
)
1123 struct perf_thread
*pthr
;
1126 perf
->tcnt
= DEF_THREADS_CNT
;
1127 perf
->test_peer
= &perf
->peers
[0];
1128 init_waitqueue_head(&perf
->twait
);
1130 for (tidx
= 0; tidx
< MAX_THREADS_CNT
; tidx
++) {
1131 pthr
= &perf
->threads
[tidx
];
1135 pthr
->status
= -ENODATA
;
1136 init_waitqueue_head(&pthr
->dma_wait
);
1137 INIT_WORK(&pthr
->work
, perf_thread_work
);
1141 static void perf_clear_threads(struct perf_ctx
*perf
)
1143 perf_terminate_test(perf
);
1146 /*==============================================================================
1148 *==============================================================================
1151 static ssize_t
perf_dbgfs_read_info(struct file
*filep
, char __user
*ubuf
,
1152 size_t size
, loff_t
*offp
)
1154 struct perf_ctx
*perf
= filep
->private_data
;
1155 struct perf_peer
*peer
;
1161 buf_size
= min_t(size_t, size
, 0x1000U
);
1163 buf
= kmalloc(buf_size
, GFP_KERNEL
);
1167 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1168 " Performance measuring tool info:\n\n");
1170 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1171 "Local port %d, Global index %d\n", ntb_port_number(perf
->ntb
),
1173 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
, "Test status: ");
1174 if (test_bit(0, &perf
->busy_flag
)) {
1175 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1176 "on-fly with port %d (%d)\n",
1177 ntb_peer_port_number(perf
->ntb
, perf
->test_peer
->pidx
),
1178 perf
->test_peer
->pidx
);
1180 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
, "idle\n");
1183 for (pidx
= 0; pidx
< perf
->pcnt
; pidx
++) {
1184 peer
= &perf
->peers
[pidx
];
1186 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1187 "Port %d (%d), Global index %d:\n",
1188 ntb_peer_port_number(perf
->ntb
, peer
->pidx
), peer
->pidx
,
1191 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1192 "\tLink status: %s\n",
1193 test_bit(PERF_STS_LNKUP
, &peer
->sts
) ? "up" : "down");
1195 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1196 "\tOut buffer addr 0x%pK\n", peer
->outbuf
);
1198 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1199 "\tOut buffer size %pa\n", &peer
->outbuf_size
);
1201 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1202 "\tOut buffer xlat 0x%016llx[p]\n", peer
->outbuf_xlat
);
1205 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1206 "\tIn buffer addr: unallocated\n");
1210 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1211 "\tIn buffer addr 0x%pK\n", peer
->inbuf
);
1213 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1214 "\tIn buffer size %pa\n", &peer
->inbuf_size
);
1216 pos
+= scnprintf(buf
+ pos
, buf_size
- pos
,
1217 "\tIn buffer xlat %pad[p]\n", &peer
->inbuf_xlat
);
1220 ret
= simple_read_from_buffer(ubuf
, size
, offp
, buf
, pos
);
1226 static const struct file_operations perf_dbgfs_info
= {
1227 .open
= simple_open
,
1228 .read
= perf_dbgfs_read_info
1231 static ssize_t
perf_dbgfs_read_run(struct file
*filep
, char __user
*ubuf
,
1232 size_t size
, loff_t
*offp
)
1234 struct perf_ctx
*perf
= filep
->private_data
;
1235 ssize_t ret
, pos
= 0;
1238 buf
= kmalloc(PERF_BUF_LEN
, GFP_KERNEL
);
1242 ret
= perf_read_stats(perf
, buf
, PERF_BUF_LEN
, &pos
);
1246 ret
= simple_read_from_buffer(ubuf
, size
, offp
, buf
, pos
);
1253 static ssize_t
perf_dbgfs_write_run(struct file
*filep
, const char __user
*ubuf
,
1254 size_t size
, loff_t
*offp
)
1256 struct perf_ctx
*perf
= filep
->private_data
;
1257 struct perf_peer
*peer
;
1260 ret
= kstrtoint_from_user(ubuf
, size
, 0, &pidx
);
1264 if (pidx
< 0 || pidx
>= perf
->pcnt
)
1267 peer
= &perf
->peers
[pidx
];
1269 ret
= perf_submit_test(peer
);
1276 static const struct file_operations perf_dbgfs_run
= {
1277 .open
= simple_open
,
1278 .read
= perf_dbgfs_read_run
,
1279 .write
= perf_dbgfs_write_run
1282 static ssize_t
perf_dbgfs_read_tcnt(struct file
*filep
, char __user
*ubuf
,
1283 size_t size
, loff_t
*offp
)
1285 struct perf_ctx
*perf
= filep
->private_data
;
1289 pos
= scnprintf(buf
, sizeof(buf
), "%hhu\n", perf
->tcnt
);
1291 return simple_read_from_buffer(ubuf
, size
, offp
, buf
, pos
);
1294 static ssize_t
perf_dbgfs_write_tcnt(struct file
*filep
,
1295 const char __user
*ubuf
,
1296 size_t size
, loff_t
*offp
)
1298 struct perf_ctx
*perf
= filep
->private_data
;
1302 ret
= kstrtou8_from_user(ubuf
, size
, 0, &val
);
1306 ret
= perf_set_tcnt(perf
, val
);
1313 static const struct file_operations perf_dbgfs_tcnt
= {
1314 .open
= simple_open
,
1315 .read
= perf_dbgfs_read_tcnt
,
1316 .write
= perf_dbgfs_write_tcnt
1319 static void perf_setup_dbgfs(struct perf_ctx
*perf
)
1321 struct pci_dev
*pdev
= perf
->ntb
->pdev
;
1323 perf
->dbgfs_dir
= debugfs_create_dir(pci_name(pdev
), perf_dbgfs_topdir
);
1324 if (!perf
->dbgfs_dir
) {
1325 dev_warn(&perf
->ntb
->dev
, "DebugFS unsupported\n");
1329 debugfs_create_file("info", 0600, perf
->dbgfs_dir
, perf
,
1332 debugfs_create_file("run", 0600, perf
->dbgfs_dir
, perf
,
1335 debugfs_create_file("threads_count", 0600, perf
->dbgfs_dir
, perf
,
1338 /* They are made read-only for test exec safety and integrity */
1339 debugfs_create_u8("chunk_order", 0500, perf
->dbgfs_dir
, &chunk_order
);
1341 debugfs_create_u8("total_order", 0500, perf
->dbgfs_dir
, &total_order
);
1343 debugfs_create_bool("use_dma", 0500, perf
->dbgfs_dir
, &use_dma
);
1346 static void perf_clear_dbgfs(struct perf_ctx
*perf
)
1348 debugfs_remove_recursive(perf
->dbgfs_dir
);
1351 /*==============================================================================
1352 * Basic driver initialization
1353 *==============================================================================
1356 static struct perf_ctx
*perf_create_data(struct ntb_dev
*ntb
)
1358 struct perf_ctx
*perf
;
1360 perf
= devm_kzalloc(&ntb
->dev
, sizeof(*perf
), GFP_KERNEL
);
1362 return ERR_PTR(-ENOMEM
);
1364 perf
->pcnt
= ntb_peer_port_count(ntb
);
1365 perf
->peers
= devm_kcalloc(&ntb
->dev
, perf
->pcnt
, sizeof(*perf
->peers
),
1368 return ERR_PTR(-ENOMEM
);
1375 static int perf_setup_peer_mw(struct perf_peer
*peer
)
1377 struct perf_ctx
*perf
= peer
->perf
;
1378 phys_addr_t phys_addr
;
1381 /* Get outbound MW parameters and map it */
1382 ret
= ntb_peer_mw_get_addr(perf
->ntb
, perf
->gidx
, &phys_addr
,
1383 &peer
->outbuf_size
);
1387 peer
->outbuf
= devm_ioremap_wc(&perf
->ntb
->dev
, phys_addr
,
1392 if (max_mw_size
&& peer
->outbuf_size
> max_mw_size
) {
1393 peer
->outbuf_size
= max_mw_size
;
1394 dev_warn(&peer
->perf
->ntb
->dev
,
1395 "Peer %d outbuf reduced to %pa\n", peer
->pidx
,
1396 &peer
->outbuf_size
);
1402 static int perf_init_peers(struct perf_ctx
*perf
)
1404 struct perf_peer
*peer
;
1405 int pidx
, lport
, ret
;
1407 lport
= ntb_port_number(perf
->ntb
);
1409 for (pidx
= 0; pidx
< perf
->pcnt
; pidx
++) {
1410 peer
= &perf
->peers
[pidx
];
1414 if (lport
< ntb_peer_port_number(perf
->ntb
, pidx
)) {
1415 if (perf
->gidx
== -1)
1417 peer
->gidx
= pidx
+ 1;
1421 INIT_WORK(&peer
->service
, perf_service_work
);
1422 init_completion(&peer
->init_comp
);
1424 if (perf
->gidx
== -1)
1428 * Hardware with only two ports may not have unique port
1429 * numbers. In this case, the gidxs should all be zero.
1431 if (perf
->pcnt
== 1 && ntb_port_number(perf
->ntb
) == 0 &&
1432 ntb_peer_port_number(perf
->ntb
, 0) == 0) {
1434 perf
->peers
[0].gidx
= 0;
1437 for (pidx
= 0; pidx
< perf
->pcnt
; pidx
++) {
1438 ret
= perf_setup_peer_mw(&perf
->peers
[pidx
]);
1443 dev_dbg(&perf
->ntb
->dev
, "Global port index %d\n", perf
->gidx
);
1448 static int perf_probe(struct ntb_client
*client
, struct ntb_dev
*ntb
)
1450 struct perf_ctx
*perf
;
1453 perf
= perf_create_data(ntb
);
1455 return PTR_ERR(perf
);
1457 ret
= perf_init_peers(perf
);
1461 perf_init_threads(perf
);
1463 ret
= perf_init_service(perf
);
1467 ret
= perf_enable_service(perf
);
1471 perf_setup_dbgfs(perf
);
1476 static void perf_remove(struct ntb_client
*client
, struct ntb_dev
*ntb
)
1478 struct perf_ctx
*perf
= ntb
->ctx
;
1480 perf_clear_dbgfs(perf
);
1482 perf_disable_service(perf
);
1484 perf_clear_threads(perf
);
1487 static struct ntb_client perf_client
= {
1489 .probe
= perf_probe
,
1490 .remove
= perf_remove
1494 static int __init
perf_init(void)
1498 if (chunk_order
> MAX_CHUNK_ORDER
) {
1499 chunk_order
= MAX_CHUNK_ORDER
;
1500 pr_info("Chunk order reduced to %hhu\n", chunk_order
);
1503 if (total_order
< chunk_order
) {
1504 total_order
= chunk_order
;
1505 pr_info("Total data order reduced to %hhu\n", total_order
);
1508 perf_wq
= alloc_workqueue("perf_wq", WQ_UNBOUND
| WQ_SYSFS
, 0);
1512 if (debugfs_initialized())
1513 perf_dbgfs_topdir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
1515 ret
= ntb_register_client(&perf_client
);
1517 debugfs_remove_recursive(perf_dbgfs_topdir
);
1518 destroy_workqueue(perf_wq
);
1523 module_init(perf_init
);
1525 static void __exit
perf_exit(void)
1527 ntb_unregister_client(&perf_client
);
1528 debugfs_remove_recursive(perf_dbgfs_topdir
);
1529 destroy_workqueue(perf_wq
);
1531 module_exit(perf_exit
);