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" };
40 static int run_test(struct pci_test *test)
42 struct pci_endpoint_test_xfer_param param;
46 fd = open(test->device, O_RDWR);
48 perror("can't open PCI Endpoint Test device");
52 if (test->barnum >= 0 && test->barnum <= 5) {
53 ret = ioctl(fd, PCITEST_BAR, test->barnum);
54 fprintf(stdout, "BAR%d:\t\t", test->barnum);
56 fprintf(stdout, "TEST FAILED\n");
58 fprintf(stdout, "%s\n", result[ret]);
61 if (test->set_irqtype) {
62 ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
63 fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
65 fprintf(stdout, "FAILED\n");
67 fprintf(stdout, "%s\n", result[ret]);
70 if (test->get_irqtype) {
71 ret = ioctl(fd, PCITEST_GET_IRQTYPE);
72 fprintf(stdout, "GET IRQ TYPE:\t\t");
74 fprintf(stdout, "FAILED\n");
76 fprintf(stdout, "%s\n", irq[ret]);
79 if (test->legacyirq) {
80 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
81 fprintf(stdout, "LEGACY IRQ:\t");
83 fprintf(stdout, "TEST FAILED\n");
85 fprintf(stdout, "%s\n", result[ret]);
88 if (test->msinum > 0 && test->msinum <= 32) {
89 ret = ioctl(fd, PCITEST_MSI, test->msinum);
90 fprintf(stdout, "MSI%d:\t\t", test->msinum);
92 fprintf(stdout, "TEST FAILED\n");
94 fprintf(stdout, "%s\n", result[ret]);
97 if (test->msixnum > 0 && test->msixnum <= 2048) {
98 ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
99 fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
101 fprintf(stdout, "TEST FAILED\n");
103 fprintf(stdout, "%s\n", result[ret]);
107 param.size = test->size;
109 param.flags = PCITEST_FLAGS_USE_DMA;
110 ret = ioctl(fd, PCITEST_WRITE, ¶m);
111 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
113 fprintf(stdout, "TEST FAILED\n");
115 fprintf(stdout, "%s\n", result[ret]);
119 param.size = test->size;
121 param.flags = PCITEST_FLAGS_USE_DMA;
122 ret = ioctl(fd, PCITEST_READ, ¶m);
123 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
125 fprintf(stdout, "TEST FAILED\n");
127 fprintf(stdout, "%s\n", result[ret]);
131 param.size = test->size;
133 param.flags = PCITEST_FLAGS_USE_DMA;
134 ret = ioctl(fd, PCITEST_COPY, ¶m);
135 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
137 fprintf(stdout, "TEST FAILED\n");
139 fprintf(stdout, "%s\n", result[ret]);
144 return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */
147 int main(int argc, char **argv)
150 struct pci_test *test;
152 test = calloc(1, sizeof(*test));
154 perror("Fail to allocate memory for pci_test\n");
158 /* since '0' is a valid BAR number, initialize it to -1 */
161 /* set default size as 100KB */
162 test->size = 0x19000;
164 /* set default endpoint device */
165 test->device = "/dev/pci-endpoint-test.0";
167 while ((c = getopt(argc, argv, "D:b:m:x:i:dIlhrwcs:")) != EOF)
170 test->device = optarg;
173 test->barnum = atoi(optarg);
174 if (test->barnum < 0 || test->barnum > 5)
178 test->legacyirq = true;
181 test->msinum = atoi(optarg);
182 if (test->msinum < 1 || test->msinum > 32)
186 test->msixnum = atoi(optarg);
187 if (test->msixnum < 1 || test->msixnum > 2048)
191 test->irqtype = atoi(optarg);
192 if (test->irqtype < 0 || test->irqtype > 2)
194 test->set_irqtype = true;
197 test->get_irqtype = true;
209 test->size = strtoul(optarg, NULL, 0);
212 test->use_dma = true;
218 "usage: %s [options]\n"
220 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
221 "\t-b <bar num> BAR test (bar number between 0..5)\n"
222 "\t-m <msi num> MSI test (msi number between 1..32)\n"
223 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
224 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
225 "\t-I Get current IRQ type configured\n"
227 "\t-l Legacy IRQ test\n"
228 "\t-r Read buffer test\n"
229 "\t-w Write buffer test\n"
230 "\t-c Copy buffer test\n"
231 "\t-s <size> Size of buffer {default: 100KB}\n"
232 "\t-h Print this help message\n",
237 return run_test(test);