1 // SPDX-License-Identifier: GPL-2.0-only
3 * Userspace PCI Endpoint Test Module
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
14 #include <sys/ioctl.h>
17 #include <linux/pcitest.h>
19 static char *result
[] = { "NOT OKAY", "OKAY" };
20 static char *irq
[] = { "LEGACY", "MSI", "MSI-X" };
39 static int run_test(struct pci_test
*test
)
41 struct pci_endpoint_test_xfer_param param
= {};
45 fd
= open(test
->device
, O_RDWR
);
47 perror("can't open PCI Endpoint Test device");
51 if (test
->barnum
>= 0 && test
->barnum
<= 5) {
52 ret
= ioctl(fd
, PCITEST_BAR
, test
->barnum
);
53 fprintf(stdout
, "BAR%d:\t\t", test
->barnum
);
55 fprintf(stdout
, "TEST FAILED\n");
57 fprintf(stdout
, "%s\n", result
[ret
]);
60 if (test
->set_irqtype
) {
61 ret
= ioctl(fd
, PCITEST_SET_IRQTYPE
, test
->irqtype
);
62 fprintf(stdout
, "SET IRQ TYPE TO %s:\t\t", irq
[test
->irqtype
]);
64 fprintf(stdout
, "FAILED\n");
66 fprintf(stdout
, "%s\n", result
[ret
]);
69 if (test
->get_irqtype
) {
70 ret
= ioctl(fd
, PCITEST_GET_IRQTYPE
);
71 fprintf(stdout
, "GET IRQ TYPE:\t\t");
73 fprintf(stdout
, "FAILED\n");
75 fprintf(stdout
, "%s\n", irq
[ret
]);
78 if (test
->clear_irq
) {
79 ret
= ioctl(fd
, PCITEST_CLEAR_IRQ
);
80 fprintf(stdout
, "CLEAR IRQ:\t\t");
82 fprintf(stdout
, "FAILED\n");
84 fprintf(stdout
, "%s\n", result
[ret
]);
87 if (test
->legacyirq
) {
88 ret
= ioctl(fd
, PCITEST_LEGACY_IRQ
, 0);
89 fprintf(stdout
, "LEGACY IRQ:\t");
91 fprintf(stdout
, "TEST FAILED\n");
93 fprintf(stdout
, "%s\n", result
[ret
]);
96 if (test
->msinum
> 0 && test
->msinum
<= 32) {
97 ret
= ioctl(fd
, PCITEST_MSI
, test
->msinum
);
98 fprintf(stdout
, "MSI%u:\t\t", test
->msinum
);
100 fprintf(stdout
, "TEST FAILED\n");
102 fprintf(stdout
, "%s\n", result
[ret
]);
105 if (test
->msixnum
> 0 && test
->msixnum
<= 2048) {
106 ret
= ioctl(fd
, PCITEST_MSIX
, test
->msixnum
);
107 fprintf(stdout
, "MSI-X%u:\t\t", test
->msixnum
);
109 fprintf(stdout
, "TEST FAILED\n");
111 fprintf(stdout
, "%s\n", result
[ret
]);
115 param
.size
= test
->size
;
117 param
.flags
= PCITEST_FLAGS_USE_DMA
;
118 ret
= ioctl(fd
, PCITEST_WRITE
, ¶m
);
119 fprintf(stdout
, "WRITE (%7lu bytes):\t\t", test
->size
);
121 fprintf(stdout
, "TEST FAILED\n");
123 fprintf(stdout
, "%s\n", result
[ret
]);
127 param
.size
= test
->size
;
129 param
.flags
= PCITEST_FLAGS_USE_DMA
;
130 ret
= ioctl(fd
, PCITEST_READ
, ¶m
);
131 fprintf(stdout
, "READ (%7lu bytes):\t\t", test
->size
);
133 fprintf(stdout
, "TEST FAILED\n");
135 fprintf(stdout
, "%s\n", result
[ret
]);
139 param
.size
= test
->size
;
141 param
.flags
= PCITEST_FLAGS_USE_DMA
;
142 ret
= ioctl(fd
, PCITEST_COPY
, ¶m
);
143 fprintf(stdout
, "COPY (%7lu bytes):\t\t", test
->size
);
145 fprintf(stdout
, "TEST FAILED\n");
147 fprintf(stdout
, "%s\n", result
[ret
]);
152 return (ret
< 0) ? ret
: 1 - ret
; /* return 0 if test succeeded */
155 int main(int argc
, char **argv
)
158 struct pci_test
*test
;
160 test
= calloc(1, sizeof(*test
));
162 perror("Fail to allocate memory for pci_test\n");
166 /* since '0' is a valid BAR number, initialize it to -1 */
169 /* set default size as 100KB */
170 test
->size
= 0x19000;
172 /* set default endpoint device */
173 test
->device
= "/dev/pci-endpoint-test.0";
175 while ((c
= getopt(argc
, argv
, "D:b:m:x:i:deIlhrwcs:")) != EOF
)
178 test
->device
= optarg
;
181 test
->barnum
= atoi(optarg
);
182 if (test
->barnum
< 0 || test
->barnum
> 5)
186 test
->legacyirq
= true;
189 test
->msinum
= atoi(optarg
);
190 if (test
->msinum
< 1 || test
->msinum
> 32)
194 test
->msixnum
= atoi(optarg
);
195 if (test
->msixnum
< 1 || test
->msixnum
> 2048)
199 test
->irqtype
= atoi(optarg
);
200 if (test
->irqtype
< 0 || test
->irqtype
> 2)
202 test
->set_irqtype
= true;
205 test
->get_irqtype
= true;
217 test
->clear_irq
= true;
220 test
->size
= strtoul(optarg
, NULL
, 0);
223 test
->use_dma
= true;
229 "usage: %s [options]\n"
231 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
232 "\t-b <bar num> BAR test (bar number between 0..5)\n"
233 "\t-m <msi num> MSI test (msi number between 1..32)\n"
234 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
235 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
237 "\t-I Get current IRQ type configured\n"
239 "\t-l Legacy IRQ test\n"
240 "\t-r Read buffer test\n"
241 "\t-w Write buffer test\n"
242 "\t-c Copy buffer test\n"
243 "\t-s <size> Size of buffer {default: 100KB}\n"
244 "\t-h Print this help message\n",
249 return run_test(test
);