2 * Userspace PCI Endpoint Test Module
4 * Copyright (C) 2017 Texas Instruments
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/ioctl.h>
28 #include <linux/pcitest.h>
32 static char *result[] = { "NOT OKAY", "OKAY" };
33 static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
50 static void run_test(struct pci_test *test)
55 fd = open(test->device, O_RDWR);
57 perror("can't open PCI Endpoint Test device");
61 if (test->barnum >= 0 && test->barnum <= 5) {
62 ret = ioctl(fd, PCITEST_BAR, test->barnum);
63 fprintf(stdout, "BAR%d:\t\t", test->barnum);
65 fprintf(stdout, "TEST FAILED\n");
67 fprintf(stdout, "%s\n", result[ret]);
70 if (test->set_irqtype) {
71 ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
72 fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
74 fprintf(stdout, "FAILED\n");
76 fprintf(stdout, "%s\n", result[ret]);
79 if (test->get_irqtype) {
80 ret = ioctl(fd, PCITEST_GET_IRQTYPE);
81 fprintf(stdout, "GET IRQ TYPE:\t\t");
83 fprintf(stdout, "FAILED\n");
85 fprintf(stdout, "%s\n", irq[ret]);
88 if (test->legacyirq) {
89 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
90 fprintf(stdout, "LEGACY IRQ:\t");
92 fprintf(stdout, "TEST FAILED\n");
94 fprintf(stdout, "%s\n", result[ret]);
97 if (test->msinum > 0 && test->msinum <= 32) {
98 ret = ioctl(fd, PCITEST_MSI, test->msinum);
99 fprintf(stdout, "MSI%d:\t\t", test->msinum);
101 fprintf(stdout, "TEST FAILED\n");
103 fprintf(stdout, "%s\n", result[ret]);
106 if (test->msixnum > 0 && test->msixnum <= 2048) {
107 ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
108 fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
110 fprintf(stdout, "TEST FAILED\n");
112 fprintf(stdout, "%s\n", result[ret]);
116 ret = ioctl(fd, PCITEST_WRITE, test->size);
117 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
119 fprintf(stdout, "TEST FAILED\n");
121 fprintf(stdout, "%s\n", result[ret]);
125 ret = ioctl(fd, PCITEST_READ, test->size);
126 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
128 fprintf(stdout, "TEST FAILED\n");
130 fprintf(stdout, "%s\n", result[ret]);
134 ret = ioctl(fd, PCITEST_COPY, test->size);
135 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
137 fprintf(stdout, "TEST FAILED\n");
139 fprintf(stdout, "%s\n", result[ret]);
145 int main(int argc, char **argv)
148 struct pci_test *test;
150 test = calloc(1, sizeof(*test));
152 perror("Fail to allocate memory for pci_test\n");
156 /* since '0' is a valid BAR number, initialize it to -1 */
159 /* set default size as 100KB */
160 test->size = 0x19000;
162 /* set default endpoint device */
163 test->device = "/dev/pci-endpoint-test.0";
165 while ((c = getopt(argc, argv, "D:b:m:x:i:Ilrwcs:")) != EOF)
168 test->device = optarg;
171 test->barnum = atoi(optarg);
172 if (test->barnum < 0 || test->barnum > 5)
176 test->legacyirq = true;
179 test->msinum = atoi(optarg);
180 if (test->msinum < 1 || test->msinum > 32)
184 test->msixnum = atoi(optarg);
185 if (test->msixnum < 1 || test->msixnum > 2048)
189 test->irqtype = atoi(optarg);
190 if (test->irqtype < 0 || test->irqtype > 2)
192 test->set_irqtype = true;
195 test->get_irqtype = true;
207 test->size = strtoul(optarg, NULL, 0);
214 "usage: %s [options]\n"
216 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
217 "\t-b <bar num> BAR test (bar number between 0..5)\n"
218 "\t-m <msi num> MSI test (msi number between 1..32)\n"
219 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
220 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
221 "\t-I Get current IRQ type configured\n"
222 "\t-l Legacy IRQ test\n"
223 "\t-r Read buffer test\n"
224 "\t-w Write buffer test\n"
225 "\t-c Copy buffer test\n"
226 "\t-s <size> Size of buffer {default: 100KB}\n",