0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <errno.h>
0010 #include <fcntl.h>
0011 #include <stdbool.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <sys/ioctl.h>
0015 #include <unistd.h>
0016
0017 #include <linux/pcitest.h>
0018
0019 #define BILLION 1E9
0020
0021 static char *result[] = { "NOT OKAY", "OKAY" };
0022 static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
0023
0024 struct pci_test {
0025 char *device;
0026 char barnum;
0027 bool legacyirq;
0028 unsigned int msinum;
0029 unsigned int msixnum;
0030 int irqtype;
0031 bool set_irqtype;
0032 bool get_irqtype;
0033 bool clear_irq;
0034 bool read;
0035 bool write;
0036 bool copy;
0037 unsigned long size;
0038 bool use_dma;
0039 };
0040
0041 static int run_test(struct pci_test *test)
0042 {
0043 struct pci_endpoint_test_xfer_param param = {};
0044 int ret = -EINVAL;
0045 int fd;
0046
0047 fd = open(test->device, O_RDWR);
0048 if (fd < 0) {
0049 perror("can't open PCI Endpoint Test device");
0050 return -ENODEV;
0051 }
0052
0053 if (test->barnum >= 0 && test->barnum <= 5) {
0054 ret = ioctl(fd, PCITEST_BAR, test->barnum);
0055 fprintf(stdout, "BAR%d:\t\t", test->barnum);
0056 if (ret < 0)
0057 fprintf(stdout, "TEST FAILED\n");
0058 else
0059 fprintf(stdout, "%s\n", result[ret]);
0060 }
0061
0062 if (test->set_irqtype) {
0063 ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
0064 fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
0065 if (ret < 0)
0066 fprintf(stdout, "FAILED\n");
0067 else
0068 fprintf(stdout, "%s\n", result[ret]);
0069 }
0070
0071 if (test->get_irqtype) {
0072 ret = ioctl(fd, PCITEST_GET_IRQTYPE);
0073 fprintf(stdout, "GET IRQ TYPE:\t\t");
0074 if (ret < 0)
0075 fprintf(stdout, "FAILED\n");
0076 else
0077 fprintf(stdout, "%s\n", irq[ret]);
0078 }
0079
0080 if (test->clear_irq) {
0081 ret = ioctl(fd, PCITEST_CLEAR_IRQ);
0082 fprintf(stdout, "CLEAR IRQ:\t\t");
0083 if (ret < 0)
0084 fprintf(stdout, "FAILED\n");
0085 else
0086 fprintf(stdout, "%s\n", result[ret]);
0087 }
0088
0089 if (test->legacyirq) {
0090 ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
0091 fprintf(stdout, "LEGACY IRQ:\t");
0092 if (ret < 0)
0093 fprintf(stdout, "TEST FAILED\n");
0094 else
0095 fprintf(stdout, "%s\n", result[ret]);
0096 }
0097
0098 if (test->msinum > 0 && test->msinum <= 32) {
0099 ret = ioctl(fd, PCITEST_MSI, test->msinum);
0100 fprintf(stdout, "MSI%d:\t\t", test->msinum);
0101 if (ret < 0)
0102 fprintf(stdout, "TEST FAILED\n");
0103 else
0104 fprintf(stdout, "%s\n", result[ret]);
0105 }
0106
0107 if (test->msixnum > 0 && test->msixnum <= 2048) {
0108 ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
0109 fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
0110 if (ret < 0)
0111 fprintf(stdout, "TEST FAILED\n");
0112 else
0113 fprintf(stdout, "%s\n", result[ret]);
0114 }
0115
0116 if (test->write) {
0117 param.size = test->size;
0118 if (test->use_dma)
0119 param.flags = PCITEST_FLAGS_USE_DMA;
0120 ret = ioctl(fd, PCITEST_WRITE, ¶m);
0121 fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
0122 if (ret < 0)
0123 fprintf(stdout, "TEST FAILED\n");
0124 else
0125 fprintf(stdout, "%s\n", result[ret]);
0126 }
0127
0128 if (test->read) {
0129 param.size = test->size;
0130 if (test->use_dma)
0131 param.flags = PCITEST_FLAGS_USE_DMA;
0132 ret = ioctl(fd, PCITEST_READ, ¶m);
0133 fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
0134 if (ret < 0)
0135 fprintf(stdout, "TEST FAILED\n");
0136 else
0137 fprintf(stdout, "%s\n", result[ret]);
0138 }
0139
0140 if (test->copy) {
0141 param.size = test->size;
0142 if (test->use_dma)
0143 param.flags = PCITEST_FLAGS_USE_DMA;
0144 ret = ioctl(fd, PCITEST_COPY, ¶m);
0145 fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
0146 if (ret < 0)
0147 fprintf(stdout, "TEST FAILED\n");
0148 else
0149 fprintf(stdout, "%s\n", result[ret]);
0150 }
0151
0152 fflush(stdout);
0153 close(fd);
0154 return (ret < 0) ? ret : 1 - ret;
0155 }
0156
0157 int main(int argc, char **argv)
0158 {
0159 int c;
0160 struct pci_test *test;
0161
0162 test = calloc(1, sizeof(*test));
0163 if (!test) {
0164 perror("Fail to allocate memory for pci_test\n");
0165 return -ENOMEM;
0166 }
0167
0168
0169 test->barnum = -1;
0170
0171
0172 test->size = 0x19000;
0173
0174
0175 test->device = "/dev/pci-endpoint-test.0";
0176
0177 while ((c = getopt(argc, argv, "D:b:m:x:i:deIlhrwcs:")) != EOF)
0178 switch (c) {
0179 case 'D':
0180 test->device = optarg;
0181 continue;
0182 case 'b':
0183 test->barnum = atoi(optarg);
0184 if (test->barnum < 0 || test->barnum > 5)
0185 goto usage;
0186 continue;
0187 case 'l':
0188 test->legacyirq = true;
0189 continue;
0190 case 'm':
0191 test->msinum = atoi(optarg);
0192 if (test->msinum < 1 || test->msinum > 32)
0193 goto usage;
0194 continue;
0195 case 'x':
0196 test->msixnum = atoi(optarg);
0197 if (test->msixnum < 1 || test->msixnum > 2048)
0198 goto usage;
0199 continue;
0200 case 'i':
0201 test->irqtype = atoi(optarg);
0202 if (test->irqtype < 0 || test->irqtype > 2)
0203 goto usage;
0204 test->set_irqtype = true;
0205 continue;
0206 case 'I':
0207 test->get_irqtype = true;
0208 continue;
0209 case 'r':
0210 test->read = true;
0211 continue;
0212 case 'w':
0213 test->write = true;
0214 continue;
0215 case 'c':
0216 test->copy = true;
0217 continue;
0218 case 'e':
0219 test->clear_irq = true;
0220 continue;
0221 case 's':
0222 test->size = strtoul(optarg, NULL, 0);
0223 continue;
0224 case 'd':
0225 test->use_dma = true;
0226 continue;
0227 case 'h':
0228 default:
0229 usage:
0230 fprintf(stderr,
0231 "usage: %s [options]\n"
0232 "Options:\n"
0233 "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
0234 "\t-b <bar num> BAR test (bar number between 0..5)\n"
0235 "\t-m <msi num> MSI test (msi number between 1..32)\n"
0236 "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
0237 "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
0238 "\t-e Clear IRQ\n"
0239 "\t-I Get current IRQ type configured\n"
0240 "\t-d Use DMA\n"
0241 "\t-l Legacy IRQ test\n"
0242 "\t-r Read buffer test\n"
0243 "\t-w Write buffer test\n"
0244 "\t-c Copy buffer test\n"
0245 "\t-s <size> Size of buffer {default: 100KB}\n"
0246 "\t-h Print this help message\n",
0247 argv[0]);
0248 return -EINVAL;
0249 }
0250
0251 return run_test(test);
0252 }