forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcitest.c
379 lines (338 loc) · 9.42 KB
/
pcitest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/****************************************************************************
* apps/testing/drivers/pcitest/pcitest.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <nuttx/pci/pci_ep_test.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
struct pci_test_s
{
const char *device;
char barnum;
bool legacyirq;
unsigned int msinum;
unsigned int msixnum;
int irqtype;
bool set_irqtype;
bool get_irqtype;
bool clear_irq;
bool read;
bool write;
bool copy;
unsigned long size;
bool use_dma;
};
/****************************************************************************
* Private Data
****************************************************************************/
static const char *g_result[] =
{
"NOT OKAY",
"OKAY"
};
static const char *g_irq[] =
{
"LEGACY",
"MSI",
"MSI-X"
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int run_test(const struct pci_test_s *test)
{
struct pci_ep_test_param_s param;
int ret = -EINVAL;
int fd;
memset(¶m, 0, sizeof(param));
fd = open(test->device, O_RDWR);
if (fd < 0)
{
perror("can't open PCI Endpoint Test device");
return -ENODEV;
}
if (test->barnum >= 0 && test->barnum <= 5)
{
ret = ioctl(fd, PCITEST_BAR, test->barnum);
fprintf(stdout, "BAR%d:\t\t", test->barnum);
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->set_irqtype)
{
ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", g_irq[test->irqtype]);
if (ret < 0)
{
fprintf(stdout, "FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->get_irqtype)
{
ret = ioctl(fd, PCITEST_GET_IRQTYPE);
fprintf(stdout, "GET IRQ TYPE:\t\t");
if (ret < 0)
{
fprintf(stdout, "FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_irq[ret]);
}
}
if (test->clear_irq)
{
ret = ioctl(fd, PCITEST_CLEAR_IRQ);
fprintf(stdout, "CLEAR IRQ:\t\t");
if (ret < 0)
{
fprintf(stdout, "FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->legacyirq)
{
ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
fprintf(stdout, "LEGACY IRQ:\t");
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->msinum > 0 && test->msinum <= 32)
{
ret = ioctl(fd, PCITEST_MSI, test->msinum);
fprintf(stdout, "MSI%d:\t\t", test->msinum);
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->msixnum > 0 && test->msixnum <= 2048)
{
ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->write)
{
param.size = test->size;
if (test->use_dma)
{
param.flags = PCITEST_FLAGS_USE_DMA;
}
ret = ioctl(fd, PCITEST_WRITE, ¶m);
fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->read)
{
param.size = test->size;
if (test->use_dma)
{
param.flags = PCITEST_FLAGS_USE_DMA;
}
ret = ioctl(fd, PCITEST_READ, ¶m);
fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
if (test->copy)
{
param.size = test->size;
if (test->use_dma)
{
param.flags = PCITEST_FLAGS_USE_DMA;
}
ret = ioctl(fd, PCITEST_COPY, ¶m);
fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
if (ret < 0)
{
fprintf(stdout, "TEST FAILED\n");
}
else
{
fprintf(stdout, "%s\n", g_result[ret]);
}
}
fflush(stdout);
close(fd);
return ret < 0 ? ret : 1 - ret; /* return 0 if test succeeded */
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, FAR char *argv[])
{
int c;
struct pci_test_s *test;
test = calloc(1, sizeof(*test));
if (!test)
{
perror("Fail to allocate memory for pci_test\n");
return -ENOMEM;
}
/* Since '0' is a valid BAR number, initialize it to -1 */
test->barnum = -1;
/* Set default size as 100KB */
test->size = 0x19000;
/* Set default endpoint device */
test->device = "/dev/pci-ep-test.0";
while ((c = getopt(argc, argv, "D:b:m:x:i:deIlhrwcs:")) != EOF)
{
switch (c)
{
case 'D':
test->device = optarg;
continue;
case 'b':
test->barnum = atoi(optarg);
if (test->barnum < 0 || test->barnum > 5)
{
goto usage;
}
continue;
case 'l':
test->legacyirq = true;
continue;
case 'm':
test->msinum = atoi(optarg);
if (test->msinum < 1 || test->msinum > 32)
{
goto usage;
}
continue;
case 'x':
test->msixnum = atoi(optarg);
if (test->msixnum < 1 || test->msixnum > 2048)
{
goto usage;
}
continue;
case 'i':
test->irqtype = atoi(optarg);
if (test->irqtype < 0 || test->irqtype > 2)
{
goto usage;
}
test->set_irqtype = true;
continue;
case 'I':
test->get_irqtype = true;
continue;
case 'r':
test->read = true;
continue;
case 'w':
test->write = true;
continue;
case 'c':
test->copy = true;
continue;
case 'e':
test->clear_irq = true;
continue;
case 's':
test->size = strtoul(optarg, NULL, 0);
continue;
case 'd':
test->use_dma = true;
continue;
case 'h':
default:
usage:
fprintf(stderr,
"usage: %s [options]\n"
"Options:\n"
"\t-D <dev> \n"
"\t PCI e test device {default: /dev/pci-ep-test.0}\n"
"\t-b <bar num> BAR test (bar number between 0..5)\n"
"\t-m <msi num> MSI test (msi number between 1..32)\n"
"\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
"\t-i <irq type> \n"
"\t Set IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
"\t-e Clear IRQ\n"
"\t-I Get current IRQ type configured\n"
"\t-d Use DMA\n"
"\t-l Legacy IRQ test\n"
"\t-r Read buffer test\n"
"\t-w Write buffer test\n"
"\t-c Copy buffer test\n"
"\t-s <size> Size of buffer {default: 100KB}\n"
"\t-h Print this help message\n",
argv[0]);
return -EINVAL;
}
}
return run_test(test);
}