1 // SPDX-License-Identifier: GPL-2.0-only
3 * Userspace PCI Endpoint Test Module
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
14 #include <sys/ioctl.h>
17 #include <linux/pcitest.h>
21 static char *result
[] = { "NOT OKAY", "OKAY" };
22 static char *irq
[] = { "LEGACY", "MSI", "MSI-X" };
39 static int run_test(struct pci_test
*test
)
44 fd
= open(test
->device
, O_RDWR
);
46 perror("can't open PCI Endpoint Test device");
50 if (test
->barnum
>= 0 && test
->barnum
<= 5) {
51 ret
= ioctl(fd
, PCITEST_BAR
, test
->barnum
);
52 fprintf(stdout
, "BAR%d:\t\t", test
->barnum
);
54 fprintf(stdout
, "TEST FAILED\n");
56 fprintf(stdout
, "%s\n", result
[ret
]);
59 if (test
->set_irqtype
) {
60 ret
= ioctl(fd
, PCITEST_SET_IRQTYPE
, test
->irqtype
);
61 fprintf(stdout
, "SET IRQ TYPE TO %s:\t\t", irq
[test
->irqtype
]);
63 fprintf(stdout
, "FAILED\n");
65 fprintf(stdout
, "%s\n", result
[ret
]);
68 if (test
->get_irqtype
) {
69 ret
= ioctl(fd
, PCITEST_GET_IRQTYPE
);
70 fprintf(stdout
, "GET IRQ TYPE:\t\t");
72 fprintf(stdout
, "FAILED\n");
74 fprintf(stdout
, "%s\n", irq
[ret
]);
77 if (test
->legacyirq
) {
78 ret
= ioctl(fd
, PCITEST_LEGACY_IRQ
, 0);
79 fprintf(stdout
, "LEGACY IRQ:\t");
81 fprintf(stdout
, "TEST FAILED\n");
83 fprintf(stdout
, "%s\n", result
[ret
]);
86 if (test
->msinum
> 0 && test
->msinum
<= 32) {
87 ret
= ioctl(fd
, PCITEST_MSI
, test
->msinum
);
88 fprintf(stdout
, "MSI%d:\t\t", test
->msinum
);
90 fprintf(stdout
, "TEST FAILED\n");
92 fprintf(stdout
, "%s\n", result
[ret
]);
95 if (test
->msixnum
> 0 && test
->msixnum
<= 2048) {
96 ret
= ioctl(fd
, PCITEST_MSIX
, test
->msixnum
);
97 fprintf(stdout
, "MSI-X%d:\t\t", test
->msixnum
);
99 fprintf(stdout
, "TEST FAILED\n");
101 fprintf(stdout
, "%s\n", result
[ret
]);
105 ret
= ioctl(fd
, PCITEST_WRITE
, test
->size
);
106 fprintf(stdout
, "WRITE (%7ld bytes):\t\t", test
->size
);
108 fprintf(stdout
, "TEST FAILED\n");
110 fprintf(stdout
, "%s\n", result
[ret
]);
114 ret
= ioctl(fd
, PCITEST_READ
, test
->size
);
115 fprintf(stdout
, "READ (%7ld bytes):\t\t", test
->size
);
117 fprintf(stdout
, "TEST FAILED\n");
119 fprintf(stdout
, "%s\n", result
[ret
]);
123 ret
= ioctl(fd
, PCITEST_COPY
, test
->size
);
124 fprintf(stdout
, "COPY (%7ld bytes):\t\t", test
->size
);
126 fprintf(stdout
, "TEST FAILED\n");
128 fprintf(stdout
, "%s\n", result
[ret
]);
132 return (ret
< 0) ? ret
: 1 - ret
; /* return 0 if test succeeded */
135 int main(int argc
, char **argv
)
138 struct pci_test
*test
;
140 test
= calloc(1, sizeof(*test
));
142 perror("Fail to allocate memory for pci_test\n");
146 /* since '0' is a valid BAR number, initialize it to -1 */
149 /* set default size as 100KB */
150 test
->size
= 0x19000;
152 /* set default endpoint device */
153 test
->device
= "/dev/pci-endpoint-test.0";
155 while ((c
= getopt(argc
, argv
, "D:b:m:x:i:Ilhrwcs:")) != EOF
)
158 test
->device
= optarg
;
161 test
->barnum
= atoi(optarg
);
162 if (test
->barnum
< 0 || test
->barnum
> 5)
166 test
->legacyirq
= true;
169 test
->msinum
= atoi(optarg
);
170 if (test
->msinum
< 1 || test
->msinum
> 32)
174 test
->msixnum
= atoi(optarg
);
175 if (test
->msixnum
< 1 || test
->msixnum
> 2048)
179 test
->irqtype
= atoi(optarg
);
180 if (test
->irqtype
< 0 || test
->irqtype
> 2)
182 test
->set_irqtype
= true;
185 test
->get_irqtype
= true;
197 test
->size
= strtoul(optarg
, NULL
, 0);
203 "usage: %s [options]\n"
205 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
206 "\t-b <bar num> BAR test (bar number between 0..5)\n"
207 "\t-m <msi num> MSI test (msi number between 1..32)\n"
208 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
209 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
210 "\t-I Get current IRQ type configured\n"
211 "\t-l Legacy IRQ test\n"
212 "\t-r Read buffer test\n"
213 "\t-w Write buffer test\n"
214 "\t-c Copy buffer test\n"
215 "\t-s <size> Size of buffer {default: 100KB}\n"
216 "\t-h Print this help message\n",
221 return run_test(test
);