Commit | Line | Data |
---|---|---|
3f2ed813 KVA |
1 | /** |
2 | * Userspace PCI Endpoint Test Module | |
3 | * | |
4 | * Copyright (C) 2017 Texas Instruments | |
5 | * Author: Kishon Vijay Abraham I <kishon@ti.com> | |
6 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
20 | #include <errno.h> | |
21 | #include <fcntl.h> | |
22 | #include <stdbool.h> | |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <sys/ioctl.h> | |
3f2ed813 KVA |
26 | #include <unistd.h> |
27 | ||
28 | #include <linux/pcitest.h> | |
29 | ||
30 | #define BILLION 1E9 | |
31 | ||
32 | static char *result[] = { "NOT OKAY", "OKAY" }; | |
0653217c | 33 | static char *irq[] = { "LEGACY", "MSI", "MSI-X" }; |
3f2ed813 KVA |
34 | |
35 | struct pci_test { | |
36 | char *device; | |
37 | char barnum; | |
38 | bool legacyirq; | |
39 | unsigned int msinum; | |
0653217c GP |
40 | unsigned int msixnum; |
41 | int irqtype; | |
42 | bool set_irqtype; | |
43 | bool get_irqtype; | |
3f2ed813 KVA |
44 | bool read; |
45 | bool write; | |
46 | bool copy; | |
47 | unsigned long size; | |
48 | }; | |
49 | ||
fef31eca | 50 | static void run_test(struct pci_test *test) |
3f2ed813 KVA |
51 | { |
52 | long ret; | |
53 | int fd; | |
3f2ed813 KVA |
54 | |
55 | fd = open(test->device, O_RDWR); | |
56 | if (fd < 0) { | |
57 | perror("can't open PCI Endpoint Test device"); | |
fef31eca | 58 | return; |
3f2ed813 KVA |
59 | } |
60 | ||
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); | |
64 | if (ret < 0) | |
65 | fprintf(stdout, "TEST FAILED\n"); | |
66 | else | |
67 | fprintf(stdout, "%s\n", result[ret]); | |
68 | } | |
69 | ||
0653217c GP |
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]); | |
73 | if (ret < 0) | |
74 | fprintf(stdout, "FAILED\n"); | |
75 | else | |
76 | fprintf(stdout, "%s\n", result[ret]); | |
77 | } | |
78 | ||
79 | if (test->get_irqtype) { | |
80 | ret = ioctl(fd, PCITEST_GET_IRQTYPE); | |
81 | fprintf(stdout, "GET IRQ TYPE:\t\t"); | |
82 | if (ret < 0) | |
83 | fprintf(stdout, "FAILED\n"); | |
84 | else | |
85 | fprintf(stdout, "%s\n", irq[ret]); | |
86 | } | |
87 | ||
3f2ed813 KVA |
88 | if (test->legacyirq) { |
89 | ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0); | |
90 | fprintf(stdout, "LEGACY IRQ:\t"); | |
91 | if (ret < 0) | |
92 | fprintf(stdout, "TEST FAILED\n"); | |
93 | else | |
94 | fprintf(stdout, "%s\n", result[ret]); | |
95 | } | |
96 | ||
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); | |
100 | if (ret < 0) | |
101 | fprintf(stdout, "TEST FAILED\n"); | |
102 | else | |
103 | fprintf(stdout, "%s\n", result[ret]); | |
104 | } | |
105 | ||
0653217c GP |
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); | |
109 | if (ret < 0) | |
110 | fprintf(stdout, "TEST FAILED\n"); | |
111 | else | |
112 | fprintf(stdout, "%s\n", result[ret]); | |
113 | } | |
114 | ||
3f2ed813 KVA |
115 | if (test->write) { |
116 | ret = ioctl(fd, PCITEST_WRITE, test->size); | |
117 | fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size); | |
118 | if (ret < 0) | |
119 | fprintf(stdout, "TEST FAILED\n"); | |
120 | else | |
121 | fprintf(stdout, "%s\n", result[ret]); | |
122 | } | |
123 | ||
124 | if (test->read) { | |
125 | ret = ioctl(fd, PCITEST_READ, test->size); | |
126 | fprintf(stdout, "READ (%7ld bytes):\t\t", test->size); | |
127 | if (ret < 0) | |
128 | fprintf(stdout, "TEST FAILED\n"); | |
129 | else | |
130 | fprintf(stdout, "%s\n", result[ret]); | |
131 | } | |
132 | ||
133 | if (test->copy) { | |
134 | ret = ioctl(fd, PCITEST_COPY, test->size); | |
135 | fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size); | |
136 | if (ret < 0) | |
137 | fprintf(stdout, "TEST FAILED\n"); | |
138 | else | |
139 | fprintf(stdout, "%s\n", result[ret]); | |
140 | } | |
141 | ||
142 | fflush(stdout); | |
b71f0a0b | 143 | return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */ |
3f2ed813 KVA |
144 | } |
145 | ||
146 | int main(int argc, char **argv) | |
147 | { | |
148 | int c; | |
149 | struct pci_test *test; | |
150 | ||
151 | test = calloc(1, sizeof(*test)); | |
152 | if (!test) { | |
153 | perror("Fail to allocate memory for pci_test\n"); | |
154 | return -ENOMEM; | |
155 | } | |
156 | ||
157 | /* since '0' is a valid BAR number, initialize it to -1 */ | |
158 | test->barnum = -1; | |
159 | ||
160 | /* set default size as 100KB */ | |
161 | test->size = 0x19000; | |
162 | ||
163 | /* set default endpoint device */ | |
164 | test->device = "/dev/pci-endpoint-test.0"; | |
165 | ||
fbca0b28 | 166 | while ((c = getopt(argc, argv, "D:b:m:x:i:Ilhrwcs:")) != EOF) |
3f2ed813 KVA |
167 | switch (c) { |
168 | case 'D': | |
169 | test->device = optarg; | |
170 | continue; | |
171 | case 'b': | |
172 | test->barnum = atoi(optarg); | |
173 | if (test->barnum < 0 || test->barnum > 5) | |
174 | goto usage; | |
175 | continue; | |
176 | case 'l': | |
177 | test->legacyirq = true; | |
178 | continue; | |
179 | case 'm': | |
180 | test->msinum = atoi(optarg); | |
181 | if (test->msinum < 1 || test->msinum > 32) | |
182 | goto usage; | |
183 | continue; | |
0653217c GP |
184 | case 'x': |
185 | test->msixnum = atoi(optarg); | |
186 | if (test->msixnum < 1 || test->msixnum > 2048) | |
187 | goto usage; | |
188 | continue; | |
189 | case 'i': | |
190 | test->irqtype = atoi(optarg); | |
191 | if (test->irqtype < 0 || test->irqtype > 2) | |
192 | goto usage; | |
193 | test->set_irqtype = true; | |
194 | continue; | |
195 | case 'I': | |
196 | test->get_irqtype = true; | |
197 | continue; | |
3f2ed813 KVA |
198 | case 'r': |
199 | test->read = true; | |
200 | continue; | |
201 | case 'w': | |
202 | test->write = true; | |
203 | continue; | |
204 | case 'c': | |
205 | test->copy = true; | |
206 | continue; | |
207 | case 's': | |
208 | test->size = strtoul(optarg, NULL, 0); | |
209 | continue; | |
3f2ed813 KVA |
210 | case 'h': |
211 | default: | |
212 | usage: | |
213 | fprintf(stderr, | |
214 | "usage: %s [options]\n" | |
215 | "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" | |
0653217c GP |
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" | |
e4758422 | 222 | "\t-l Legacy IRQ test\n" |
3f2ed813 KVA |
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", | |
fbca0b28 | 227 | "\t-h Print this help message\n", |
3f2ed813 KVA |
228 | argv[0]); |
229 | return -EINVAL; | |
230 | } | |
231 | ||
b71f0a0b | 232 | return run_test(test); |
3f2ed813 | 233 | } |