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