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>
21 static char *result[] = { "NOT OKAY", "OKAY" };
22 static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
41 static int run_test(struct pci_test *test)
43 struct pci_endpoint_test_xfer_param param = {};
47 fd = open(test->device, O_RDWR);
49 perror("can't open PCI Endpoint Test device");
53 if (test->barnum >= 0 && test->barnum <= 5) {
54 ret = ioctl(fd, PCITEST_BAR, test->barnum);
55 fprintf(stdout, "BAR%d:\t\t", test->barnum);
57 fprintf(stdout, "TEST FAILED\n");
59 fprintf(stdout, "%s\n", result[ret]);
62 if (test->set_irqtype) {
63 ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
64 fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
66 fprintf(stdout, "FAILED\n");
68 fprintf(stdout, "%s\n", result[ret]);
71 if (test->get_irqtype) {
72 ret = ioctl(fd, PCITEST_GET_IRQTYPE);
73 fprintf(stdout, "GET IRQ TYPE:\t\t");
75 fprintf(stdout, "FAILED\n");
77 fprintf(stdout, "%s\n", irq[ret]);
80 if (test->clear_irq) {
81 ret = ioctl(fd, PCITEST_CLEAR_IRQ);
82 fprintf(stdout, "CLEAR IRQ:\t\t");
84 fprintf(stdout, "FAILED\n");
86 fprintf(stdout, "%s\n", result[ret]);
89 if (test->legacyirq) {
90 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
91 fprintf(stdout, "LEGACY IRQ:\t");
93 fprintf(stdout, "TEST FAILED\n");
95 fprintf(stdout, "%s\n", result[ret]);
98 if (test->msinum > 0 && test->msinum <= 32) {
99 ret = ioctl(fd, PCITEST_MSI, test->msinum);
100 fprintf(stdout, "MSI%d:\t\t", test->msinum);
102 fprintf(stdout, "TEST FAILED\n");
104 fprintf(stdout, "%s\n", result[ret]);
107 if (test->msixnum > 0 && test->msixnum <= 2048) {
108 ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
109 fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
111 fprintf(stdout, "TEST FAILED\n");
113 fprintf(stdout, "%s\n", result[ret]);
117 param.size = test->size;
119 param.flags = PCITEST_FLAGS_USE_DMA;
120 ret = ioctl(fd, PCITEST_WRITE, ¶m);
121 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
123 fprintf(stdout, "TEST FAILED\n");
125 fprintf(stdout, "%s\n", result[ret]);
129 param.size = test->size;
131 param.flags = PCITEST_FLAGS_USE_DMA;
132 ret = ioctl(fd, PCITEST_READ, ¶m);
133 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
135 fprintf(stdout, "TEST FAILED\n");
137 fprintf(stdout, "%s\n", result[ret]);
141 param.size = test->size;
143 param.flags = PCITEST_FLAGS_USE_DMA;
144 ret = ioctl(fd, PCITEST_COPY, ¶m);
145 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
147 fprintf(stdout, "TEST FAILED\n");
149 fprintf(stdout, "%s\n", result[ret]);
154 return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */
157 int main(int argc, char **argv)
160 struct pci_test *test;
162 test = calloc(1, sizeof(*test));
164 perror("Fail to allocate memory for pci_test\n");
168 /* since '0' is a valid BAR number, initialize it to -1 */
171 /* set default size as 100KB */
172 test->size = 0x19000;
174 /* set default endpoint device */
175 test->device = "/dev/pci-endpoint-test.0";
177 while ((c = getopt(argc, argv, "D:b:m:x:i:deIlhrwcs:")) != EOF)
180 test->device = optarg;
183 test->barnum = atoi(optarg);
184 if (test->barnum < 0 || test->barnum > 5)
188 test->legacyirq = true;
191 test->msinum = atoi(optarg);
192 if (test->msinum < 1 || test->msinum > 32)
196 test->msixnum = atoi(optarg);
197 if (test->msixnum < 1 || test->msixnum > 2048)
201 test->irqtype = atoi(optarg);
202 if (test->irqtype < 0 || test->irqtype > 2)
204 test->set_irqtype = true;
207 test->get_irqtype = true;
219 test->clear_irq = true;
222 test->size = strtoul(optarg, NULL, 0);
225 test->use_dma = true;
231 "usage: %s [options]\n"
233 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
234 "\t-b <bar num> BAR test (bar number between 0..5)\n"
235 "\t-m <msi num> MSI test (msi number between 1..32)\n"
236 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
237 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
239 "\t-I Get current IRQ type configured\n"
241 "\t-l Legacy IRQ test\n"
242 "\t-r Read buffer test\n"
243 "\t-w Write buffer test\n"
244 "\t-c Copy buffer test\n"
245 "\t-s <size> Size of buffer {default: 100KB}\n"
246 "\t-h Print this help message\n",
251 return run_test(test);