2 * Userspace PCI Endpoint Test Module
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
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>
29 #include <linux/pcitest.h>
33 static char *result
[] = { "NOT OKAY", "OKAY" };
34 static char *irq
[] = { "LEGACY", "MSI", "MSI-X" };
51 static int run_test(struct pci_test
*test
)
55 struct timespec start
, end
;
58 fd
= open(test
->device
, O_RDWR
);
60 perror("can't open PCI Endpoint Test device");
64 if (test
->barnum
>= 0 && test
->barnum
<= 5) {
65 ret
= ioctl(fd
, PCITEST_BAR
, test
->barnum
);
66 fprintf(stdout
, "BAR%d:\t\t", test
->barnum
);
68 fprintf(stdout
, "TEST FAILED\n");
70 fprintf(stdout
, "%s\n", result
[ret
]);
73 if (test
->set_irqtype
) {
74 ret
= ioctl(fd
, PCITEST_SET_IRQTYPE
, test
->irqtype
);
75 fprintf(stdout
, "SET IRQ TYPE TO %s:\t\t", irq
[test
->irqtype
]);
77 fprintf(stdout
, "FAILED\n");
79 fprintf(stdout
, "%s\n", result
[ret
]);
82 if (test
->get_irqtype
) {
83 ret
= ioctl(fd
, PCITEST_GET_IRQTYPE
);
84 fprintf(stdout
, "GET IRQ TYPE:\t\t");
86 fprintf(stdout
, "FAILED\n");
88 fprintf(stdout
, "%s\n", irq
[ret
]);
91 if (test
->legacyirq
) {
92 ret
= ioctl(fd
, PCITEST_LEGACY_IRQ
, 0);
93 fprintf(stdout
, "LEGACY IRQ:\t");
95 fprintf(stdout
, "TEST FAILED\n");
97 fprintf(stdout
, "%s\n", result
[ret
]);
100 if (test
->msinum
> 0 && test
->msinum
<= 32) {
101 ret
= ioctl(fd
, PCITEST_MSI
, test
->msinum
);
102 fprintf(stdout
, "MSI%d:\t\t", test
->msinum
);
104 fprintf(stdout
, "TEST FAILED\n");
106 fprintf(stdout
, "%s\n", result
[ret
]);
109 if (test
->msixnum
> 0 && test
->msixnum
<= 2048) {
110 ret
= ioctl(fd
, PCITEST_MSIX
, test
->msixnum
);
111 fprintf(stdout
, "MSI-X%d:\t\t", test
->msixnum
);
113 fprintf(stdout
, "TEST FAILED\n");
115 fprintf(stdout
, "%s\n", result
[ret
]);
119 ret
= ioctl(fd
, PCITEST_WRITE
, test
->size
);
120 fprintf(stdout
, "WRITE (%7ld bytes):\t\t", test
->size
);
122 fprintf(stdout
, "TEST FAILED\n");
124 fprintf(stdout
, "%s\n", result
[ret
]);
128 ret
= ioctl(fd
, PCITEST_READ
, test
->size
);
129 fprintf(stdout
, "READ (%7ld bytes):\t\t", test
->size
);
131 fprintf(stdout
, "TEST FAILED\n");
133 fprintf(stdout
, "%s\n", result
[ret
]);
137 ret
= ioctl(fd
, PCITEST_COPY
, test
->size
);
138 fprintf(stdout
, "COPY (%7ld bytes):\t\t", test
->size
);
140 fprintf(stdout
, "TEST FAILED\n");
142 fprintf(stdout
, "%s\n", result
[ret
]);
148 int main(int argc
, char **argv
)
151 struct pci_test
*test
;
153 test
= calloc(1, sizeof(*test
));
155 perror("Fail to allocate memory for pci_test\n");
159 /* since '0' is a valid BAR number, initialize it to -1 */
162 /* set default size as 100KB */
163 test
->size
= 0x19000;
165 /* set default endpoint device */
166 test
->device
= "/dev/pci-endpoint-test.0";
168 while ((c
= getopt(argc
, argv
, "D:b:m:x:i:Ilrwcs:")) != EOF
)
171 test
->device
= optarg
;
174 test
->barnum
= atoi(optarg
);
175 if (test
->barnum
< 0 || test
->barnum
> 5)
179 test
->legacyirq
= true;
182 test
->msinum
= atoi(optarg
);
183 if (test
->msinum
< 1 || test
->msinum
> 32)
187 test
->msixnum
= atoi(optarg
);
188 if (test
->msixnum
< 1 || test
->msixnum
> 2048)
192 test
->irqtype
= atoi(optarg
);
193 if (test
->irqtype
< 0 || test
->irqtype
> 2)
195 test
->set_irqtype
= true;
198 test
->get_irqtype
= true;
210 test
->size
= strtoul(optarg
, NULL
, 0);
217 "usage: %s [options]\n"
219 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
220 "\t-b <bar num> BAR test (bar number between 0..5)\n"
221 "\t-m <msi num> MSI test (msi number between 1..32)\n"
222 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
223 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
224 "\t-I Get current IRQ type configured\n"
225 "\t-l Legacy IRQ test\n"
226 "\t-r Read buffer test\n"
227 "\t-w Write buffer test\n"
228 "\t-c Copy buffer test\n"
229 "\t-s <size> Size of buffer {default: 100KB}\n",