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" };
39 static int run_test(struct pci_test *test)
44 fd = open(test->device, O_RDWR);
46 perror("can't open PCI Endpoint Test device");
50 if (test->barnum >= 0 && test->barnum <= 5) {
51 ret = ioctl(fd, PCITEST_BAR, test->barnum);
52 fprintf(stdout, "BAR%d:\t\t", test->barnum);
54 fprintf(stdout, "TEST FAILED\n");
56 fprintf(stdout, "%s\n", result[ret]);
59 if (test->set_irqtype) {
60 ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
61 fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
63 fprintf(stdout, "FAILED\n");
65 fprintf(stdout, "%s\n", result[ret]);
68 if (test->get_irqtype) {
69 ret = ioctl(fd, PCITEST_GET_IRQTYPE);
70 fprintf(stdout, "GET IRQ TYPE:\t\t");
72 fprintf(stdout, "FAILED\n");
74 fprintf(stdout, "%s\n", irq[ret]);
77 if (test->legacyirq) {
78 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
79 fprintf(stdout, "LEGACY IRQ:\t");
81 fprintf(stdout, "TEST FAILED\n");
83 fprintf(stdout, "%s\n", result[ret]);
86 if (test->msinum > 0 && test->msinum <= 32) {
87 ret = ioctl(fd, PCITEST_MSI, test->msinum);
88 fprintf(stdout, "MSI%d:\t\t", test->msinum);
90 fprintf(stdout, "TEST FAILED\n");
92 fprintf(stdout, "%s\n", result[ret]);
95 if (test->msixnum > 0 && test->msixnum <= 2048) {
96 ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
97 fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
99 fprintf(stdout, "TEST FAILED\n");
101 fprintf(stdout, "%s\n", result[ret]);
105 ret = ioctl(fd, PCITEST_WRITE, test->size);
106 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
108 fprintf(stdout, "TEST FAILED\n");
110 fprintf(stdout, "%s\n", result[ret]);
114 ret = ioctl(fd, PCITEST_READ, test->size);
115 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
117 fprintf(stdout, "TEST FAILED\n");
119 fprintf(stdout, "%s\n", result[ret]);
123 ret = ioctl(fd, PCITEST_COPY, test->size);
124 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
126 fprintf(stdout, "TEST FAILED\n");
128 fprintf(stdout, "%s\n", result[ret]);
133 return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */
136 int main(int argc, char **argv)
139 struct pci_test *test;
141 test = calloc(1, sizeof(*test));
143 perror("Fail to allocate memory for pci_test\n");
147 /* since '0' is a valid BAR number, initialize it to -1 */
150 /* set default size as 100KB */
151 test->size = 0x19000;
153 /* set default endpoint device */
154 test->device = "/dev/pci-endpoint-test.0";
156 while ((c = getopt(argc, argv, "D:b:m:x:i:Ilhrwcs:")) != EOF)
159 test->device = optarg;
162 test->barnum = atoi(optarg);
163 if (test->barnum < 0 || test->barnum > 5)
167 test->legacyirq = true;
170 test->msinum = atoi(optarg);
171 if (test->msinum < 1 || test->msinum > 32)
175 test->msixnum = atoi(optarg);
176 if (test->msixnum < 1 || test->msixnum > 2048)
180 test->irqtype = atoi(optarg);
181 if (test->irqtype < 0 || test->irqtype > 2)
183 test->set_irqtype = true;
186 test->get_irqtype = true;
198 test->size = strtoul(optarg, NULL, 0);
204 "usage: %s [options]\n"
206 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
207 "\t-b <bar num> BAR test (bar number between 0..5)\n"
208 "\t-m <msi num> MSI test (msi number between 1..32)\n"
209 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
210 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
211 "\t-I Get current IRQ type configured\n"
212 "\t-l Legacy IRQ test\n"
213 "\t-r Read buffer test\n"
214 "\t-w Write buffer test\n"
215 "\t-c Copy buffer test\n"
216 "\t-s <size> Size of buffer {default: 100KB}\n"
217 "\t-h Print this help message\n",
222 return run_test(test);