blob: 441b542346354a58a31b86707e58244136968f04 [file] [log] [blame]
Thomas Gleixner6b1baef2019-05-29 16:57:55 -07001// SPDX-License-Identifier: GPL-2.0-only
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +05302/**
3 * Userspace PCI Endpoint Test Module
4 *
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +05307 */
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>
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053015#include <unistd.h>
16
17#include <linux/pcitest.h>
18
19#define BILLION 1E9
20
21static char *result[] = { "NOT OKAY", "OKAY" };
Gustavo Pimentel06532172018-07-19 10:32:21 +020022static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053023
24struct pci_test {
25 char *device;
26 char barnum;
27 bool legacyirq;
28 unsigned int msinum;
Gustavo Pimentel06532172018-07-19 10:32:21 +020029 unsigned int msixnum;
30 int irqtype;
31 bool set_irqtype;
32 bool get_irqtype;
Kishon Vijay Abraham I74d60b22020-03-17 15:31:56 +053033 bool clear_irq;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053034 bool read;
35 bool write;
36 bool copy;
37 unsigned long size;
Kishon Vijay Abraham I73c57622020-03-16 16:54:23 +053038 bool use_dma;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053039};
40
Alan Mikhak8a5e0af2019-05-23 14:18:00 -070041static int run_test(struct pci_test *test)
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053042{
Shunyong Yang224d8032021-07-14 21:23:31 +080043 struct pci_endpoint_test_xfer_param param = {};
Alan Mikhak8a5e0af2019-05-23 14:18:00 -070044 int ret = -EINVAL;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053045 int fd;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053046
47 fd = open(test->device, O_RDWR);
48 if (fd < 0) {
49 perror("can't open PCI Endpoint Test device");
Alan Mikhak8a5e0af2019-05-23 14:18:00 -070050 return -ENODEV;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053051 }
52
53 if (test->barnum >= 0 && test->barnum <= 5) {
54 ret = ioctl(fd, PCITEST_BAR, test->barnum);
55 fprintf(stdout, "BAR%d:\t\t", test->barnum);
56 if (ret < 0)
57 fprintf(stdout, "TEST FAILED\n");
58 else
59 fprintf(stdout, "%s\n", result[ret]);
60 }
61
Gustavo Pimentel06532172018-07-19 10:32:21 +020062 if (test->set_irqtype) {
63 ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
64 fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
65 if (ret < 0)
66 fprintf(stdout, "FAILED\n");
67 else
68 fprintf(stdout, "%s\n", result[ret]);
69 }
70
71 if (test->get_irqtype) {
72 ret = ioctl(fd, PCITEST_GET_IRQTYPE);
73 fprintf(stdout, "GET IRQ TYPE:\t\t");
74 if (ret < 0)
75 fprintf(stdout, "FAILED\n");
76 else
77 fprintf(stdout, "%s\n", irq[ret]);
78 }
79
Kishon Vijay Abraham I74d60b22020-03-17 15:31:56 +053080 if (test->clear_irq) {
81 ret = ioctl(fd, PCITEST_CLEAR_IRQ);
82 fprintf(stdout, "CLEAR IRQ:\t\t");
83 if (ret < 0)
84 fprintf(stdout, "FAILED\n");
85 else
86 fprintf(stdout, "%s\n", result[ret]);
87 }
88
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +053089 if (test->legacyirq) {
90 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
91 fprintf(stdout, "LEGACY IRQ:\t");
92 if (ret < 0)
93 fprintf(stdout, "TEST FAILED\n");
94 else
95 fprintf(stdout, "%s\n", result[ret]);
96 }
97
98 if (test->msinum > 0 && test->msinum <= 32) {
99 ret = ioctl(fd, PCITEST_MSI, test->msinum);
100 fprintf(stdout, "MSI%d:\t\t", test->msinum);
101 if (ret < 0)
102 fprintf(stdout, "TEST FAILED\n");
103 else
104 fprintf(stdout, "%s\n", result[ret]);
105 }
106
Gustavo Pimentel06532172018-07-19 10:32:21 +0200107 if (test->msixnum > 0 && test->msixnum <= 2048) {
108 ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
109 fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
110 if (ret < 0)
111 fprintf(stdout, "TEST FAILED\n");
112 else
113 fprintf(stdout, "%s\n", result[ret]);
114 }
115
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530116 if (test->write) {
Kishon Vijay Abraham I73c57622020-03-16 16:54:23 +0530117 param.size = test->size;
118 if (test->use_dma)
119 param.flags = PCITEST_FLAGS_USE_DMA;
120 ret = ioctl(fd, PCITEST_WRITE, &param);
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530121 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
122 if (ret < 0)
123 fprintf(stdout, "TEST FAILED\n");
124 else
125 fprintf(stdout, "%s\n", result[ret]);
126 }
127
128 if (test->read) {
Kishon Vijay Abraham I73c57622020-03-16 16:54:23 +0530129 param.size = test->size;
130 if (test->use_dma)
131 param.flags = PCITEST_FLAGS_USE_DMA;
132 ret = ioctl(fd, PCITEST_READ, &param);
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530133 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
134 if (ret < 0)
135 fprintf(stdout, "TEST FAILED\n");
136 else
137 fprintf(stdout, "%s\n", result[ret]);
138 }
139
140 if (test->copy) {
Kishon Vijay Abraham I73c57622020-03-16 16:54:23 +0530141 param.size = test->size;
142 if (test->use_dma)
143 param.flags = PCITEST_FLAGS_USE_DMA;
144 ret = ioctl(fd, PCITEST_COPY, &param);
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530145 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
146 if (ret < 0)
147 fprintf(stdout, "TEST FAILED\n");
148 else
149 fprintf(stdout, "%s\n", result[ret]);
150 }
151
152 fflush(stdout);
Hewenliang3c379a592019-10-25 21:35:55 -0400153 close(fd);
Jean-Jacques Hiblotb71f0a02018-09-20 17:02:53 +0200154 return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530155}
156
157int main(int argc, char **argv)
158{
159 int c;
160 struct pci_test *test;
161
162 test = calloc(1, sizeof(*test));
163 if (!test) {
164 perror("Fail to allocate memory for pci_test\n");
165 return -ENOMEM;
166 }
167
168 /* since '0' is a valid BAR number, initialize it to -1 */
169 test->barnum = -1;
170
171 /* set default size as 100KB */
172 test->size = 0x19000;
173
174 /* set default endpoint device */
175 test->device = "/dev/pci-endpoint-test.0";
176
Kishon Vijay Abraham I74d60b22020-03-17 15:31:56 +0530177 while ((c = getopt(argc, argv, "D:b:m:x:i:deIlhrwcs:")) != EOF)
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530178 switch (c) {
179 case 'D':
180 test->device = optarg;
181 continue;
182 case 'b':
183 test->barnum = atoi(optarg);
184 if (test->barnum < 0 || test->barnum > 5)
185 goto usage;
186 continue;
187 case 'l':
188 test->legacyirq = true;
189 continue;
190 case 'm':
191 test->msinum = atoi(optarg);
192 if (test->msinum < 1 || test->msinum > 32)
193 goto usage;
194 continue;
Gustavo Pimentel06532172018-07-19 10:32:21 +0200195 case 'x':
196 test->msixnum = atoi(optarg);
197 if (test->msixnum < 1 || test->msixnum > 2048)
198 goto usage;
199 continue;
200 case 'i':
201 test->irqtype = atoi(optarg);
202 if (test->irqtype < 0 || test->irqtype > 2)
203 goto usage;
204 test->set_irqtype = true;
205 continue;
206 case 'I':
207 test->get_irqtype = true;
208 continue;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530209 case 'r':
210 test->read = true;
211 continue;
212 case 'w':
213 test->write = true;
214 continue;
215 case 'c':
216 test->copy = true;
217 continue;
Kishon Vijay Abraham I74d60b22020-03-17 15:31:56 +0530218 case 'e':
219 test->clear_irq = true;
220 continue;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530221 case 's':
222 test->size = strtoul(optarg, NULL, 0);
223 continue;
Kishon Vijay Abraham I73c57622020-03-16 16:54:23 +0530224 case 'd':
225 test->use_dma = true;
226 continue;
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530227 case 'h':
228 default:
229usage:
230 fprintf(stderr,
231 "usage: %s [options]\n"
232 "Options:\n"
233 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
234 "\t-b <bar num> BAR test (bar number between 0..5)\n"
235 "\t-m <msi num> MSI test (msi number between 1..32)\n"
Gustavo Pimentel06532172018-07-19 10:32:21 +0200236 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
237 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
Kishon Vijay Abraham I74d60b22020-03-17 15:31:56 +0530238 "\t-e Clear IRQ\n"
Gustavo Pimentel06532172018-07-19 10:32:21 +0200239 "\t-I Get current IRQ type configured\n"
Kishon Vijay Abraham I73c57622020-03-16 16:54:23 +0530240 "\t-d Use DMA\n"
Stan Drozde4758422017-08-18 20:28:10 +0530241 "\t-l Legacy IRQ test\n"
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530242 "\t-r Read buffer test\n"
243 "\t-w Write buffer test\n"
244 "\t-c Copy buffer test\n"
Alan Mikhak81cb4202019-05-23 14:18:01 -0700245 "\t-s <size> Size of buffer {default: 100KB}\n"
Kishon Vijay Abraham Ifbca0b22019-04-04 16:36:08 +0530246 "\t-h Print this help message\n",
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530247 argv[0]);
248 return -EINVAL;
249 }
250
Jean-Jacques Hiblotb71f0a02018-09-20 17:02:53 +0200251 return run_test(test);
Kishon Vijay Abraham I3f2ed812017-03-27 15:15:16 +0530252}