blob: 7002df55826f42a097d0e417efc67cf17c3dcb38 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/**
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>
26#include <unistd.h>
27
28#include <linux/pcitest.h>
29
30#define BILLION 1E9
31
32static char *result[] = { "NOT OKAY", "OKAY" };
33
34struct pci_test {
35 char *device;
36 char barnum;
37 bool legacyirq;
38 unsigned int msinum;
39 bool read;
40 bool write;
41 bool copy;
42 unsigned long size;
43};
44
45static int run_test(struct pci_test *test)
46{
47 int ret = -EINVAL;
48 int fd;
49
50 fd = open(test->device, O_RDWR);
51 if (fd < 0) {
52 perror("can't open PCI Endpoint Test device");
53 return -ENODEV;
54 }
55
56 if (test->barnum >= 0 && test->barnum <= 5) {
57 ret = ioctl(fd, PCITEST_BAR, test->barnum);
58 fprintf(stdout, "BAR%d:\t\t", test->barnum);
59 if (ret < 0)
60 fprintf(stdout, "TEST FAILED\n");
61 else
62 fprintf(stdout, "%s\n", result[ret]);
63 }
64
65 if (test->legacyirq) {
66 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
67 fprintf(stdout, "LEGACY IRQ:\t");
68 if (ret < 0)
69 fprintf(stdout, "TEST FAILED\n");
70 else
71 fprintf(stdout, "%s\n", result[ret]);
72 }
73
74 if (test->msinum > 0 && test->msinum <= 32) {
75 ret = ioctl(fd, PCITEST_MSI, test->msinum);
76 fprintf(stdout, "MSI%d:\t\t", test->msinum);
77 if (ret < 0)
78 fprintf(stdout, "TEST FAILED\n");
79 else
80 fprintf(stdout, "%s\n", result[ret]);
81 }
82
83 if (test->write) {
84 ret = ioctl(fd, PCITEST_WRITE, test->size);
85 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
86 if (ret < 0)
87 fprintf(stdout, "TEST FAILED\n");
88 else
89 fprintf(stdout, "%s\n", result[ret]);
90 }
91
92 if (test->read) {
93 ret = ioctl(fd, PCITEST_READ, test->size);
94 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
95 if (ret < 0)
96 fprintf(stdout, "TEST FAILED\n");
97 else
98 fprintf(stdout, "%s\n", result[ret]);
99 }
100
101 if (test->copy) {
102 ret = ioctl(fd, PCITEST_COPY, test->size);
103 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
104 if (ret < 0)
105 fprintf(stdout, "TEST FAILED\n");
106 else
107 fprintf(stdout, "%s\n", result[ret]);
108 }
109
110 fflush(stdout);
111}
112
113int main(int argc, char **argv)
114{
115 int c;
116 struct pci_test *test;
117
118 test = calloc(1, sizeof(*test));
119 if (!test) {
120 perror("Fail to allocate memory for pci_test\n");
121 return -ENOMEM;
122 }
123
124 /* since '0' is a valid BAR number, initialize it to -1 */
125 test->barnum = -1;
126
127 /* set default size as 100KB */
128 test->size = 0x19000;
129
130 /* set default endpoint device */
131 test->device = "/dev/pci-endpoint-test.0";
132
133 while ((c = getopt(argc, argv, "D:b:m:lrwcs:")) != EOF)
134 switch (c) {
135 case 'D':
136 test->device = optarg;
137 continue;
138 case 'b':
139 test->barnum = atoi(optarg);
140 if (test->barnum < 0 || test->barnum > 5)
141 goto usage;
142 continue;
143 case 'l':
144 test->legacyirq = true;
145 continue;
146 case 'm':
147 test->msinum = atoi(optarg);
148 if (test->msinum < 1 || test->msinum > 32)
149 goto usage;
150 continue;
151 case 'r':
152 test->read = true;
153 continue;
154 case 'w':
155 test->write = true;
156 continue;
157 case 'c':
158 test->copy = true;
159 continue;
160 case 's':
161 test->size = strtoul(optarg, NULL, 0);
162 continue;
163 case '?':
164 case 'h':
165 default:
166usage:
167 fprintf(stderr,
168 "usage: %s [options]\n"
169 "Options:\n"
170 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
171 "\t-b <bar num> BAR test (bar number between 0..5)\n"
172 "\t-m <msi num> MSI test (msi number between 1..32)\n"
173 "\t-l Legacy IRQ test\n"
174 "\t-r Read buffer test\n"
175 "\t-w Write buffer test\n"
176 "\t-c Copy buffer test\n"
177 "\t-s <size> Size of buffer {default: 100KB}\n",
178 argv[0]);
179 return -EINVAL;
180 }
181
182 run_test(test);
183 return 0;
184}