pcitest.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * Userspace PCI Endpoint Test Module
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <[email protected]>
  7. */
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <stdbool.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/ioctl.h>
  14. #include <unistd.h>
  15. #include <linux/pcitest.h>
  16. #define BILLION 1E9
  17. static char *result[] = { "NOT OKAY", "OKAY" };
  18. static char *irq[] = { "LEGACY", "MSI", "MSI-X" };
  19. struct pci_test {
  20. char *device;
  21. char barnum;
  22. bool legacyirq;
  23. unsigned int msinum;
  24. unsigned int msixnum;
  25. int irqtype;
  26. bool set_irqtype;
  27. bool get_irqtype;
  28. bool clear_irq;
  29. bool read;
  30. bool write;
  31. bool copy;
  32. unsigned long size;
  33. bool use_dma;
  34. };
  35. static int run_test(struct pci_test *test)
  36. {
  37. struct pci_endpoint_test_xfer_param param = {};
  38. int ret = -EINVAL;
  39. int fd;
  40. fd = open(test->device, O_RDWR);
  41. if (fd < 0) {
  42. perror("can't open PCI Endpoint Test device");
  43. return -ENODEV;
  44. }
  45. if (test->barnum >= 0 && test->barnum <= 5) {
  46. ret = ioctl(fd, PCITEST_BAR, test->barnum);
  47. fprintf(stdout, "BAR%d:\t\t", test->barnum);
  48. if (ret < 0)
  49. fprintf(stdout, "TEST FAILED\n");
  50. else
  51. fprintf(stdout, "%s\n", result[ret]);
  52. }
  53. if (test->set_irqtype) {
  54. ret = ioctl(fd, PCITEST_SET_IRQTYPE, test->irqtype);
  55. fprintf(stdout, "SET IRQ TYPE TO %s:\t\t", irq[test->irqtype]);
  56. if (ret < 0)
  57. fprintf(stdout, "FAILED\n");
  58. else
  59. fprintf(stdout, "%s\n", result[ret]);
  60. }
  61. if (test->get_irqtype) {
  62. ret = ioctl(fd, PCITEST_GET_IRQTYPE);
  63. fprintf(stdout, "GET IRQ TYPE:\t\t");
  64. if (ret < 0)
  65. fprintf(stdout, "FAILED\n");
  66. else
  67. fprintf(stdout, "%s\n", irq[ret]);
  68. }
  69. if (test->clear_irq) {
  70. ret = ioctl(fd, PCITEST_CLEAR_IRQ);
  71. fprintf(stdout, "CLEAR IRQ:\t\t");
  72. if (ret < 0)
  73. fprintf(stdout, "FAILED\n");
  74. else
  75. fprintf(stdout, "%s\n", result[ret]);
  76. }
  77. if (test->legacyirq) {
  78. ret = ioctl(fd, PCITEST_LEGACY_IRQ, 0);
  79. fprintf(stdout, "LEGACY IRQ:\t");
  80. if (ret < 0)
  81. fprintf(stdout, "TEST FAILED\n");
  82. else
  83. fprintf(stdout, "%s\n", result[ret]);
  84. }
  85. if (test->msinum > 0 && test->msinum <= 32) {
  86. ret = ioctl(fd, PCITEST_MSI, test->msinum);
  87. fprintf(stdout, "MSI%d:\t\t", test->msinum);
  88. if (ret < 0)
  89. fprintf(stdout, "TEST FAILED\n");
  90. else
  91. fprintf(stdout, "%s\n", result[ret]);
  92. }
  93. if (test->msixnum > 0 && test->msixnum <= 2048) {
  94. ret = ioctl(fd, PCITEST_MSIX, test->msixnum);
  95. fprintf(stdout, "MSI-X%d:\t\t", test->msixnum);
  96. if (ret < 0)
  97. fprintf(stdout, "TEST FAILED\n");
  98. else
  99. fprintf(stdout, "%s\n", result[ret]);
  100. }
  101. if (test->write) {
  102. param.size = test->size;
  103. if (test->use_dma)
  104. param.flags = PCITEST_FLAGS_USE_DMA;
  105. ret = ioctl(fd, PCITEST_WRITE, &param);
  106. fprintf(stdout, "WRITE (%7ld bytes):\t\t", test->size);
  107. if (ret < 0)
  108. fprintf(stdout, "TEST FAILED\n");
  109. else
  110. fprintf(stdout, "%s\n", result[ret]);
  111. }
  112. if (test->read) {
  113. param.size = test->size;
  114. if (test->use_dma)
  115. param.flags = PCITEST_FLAGS_USE_DMA;
  116. ret = ioctl(fd, PCITEST_READ, &param);
  117. fprintf(stdout, "READ (%7ld bytes):\t\t", test->size);
  118. if (ret < 0)
  119. fprintf(stdout, "TEST FAILED\n");
  120. else
  121. fprintf(stdout, "%s\n", result[ret]);
  122. }
  123. if (test->copy) {
  124. param.size = test->size;
  125. if (test->use_dma)
  126. param.flags = PCITEST_FLAGS_USE_DMA;
  127. ret = ioctl(fd, PCITEST_COPY, &param);
  128. fprintf(stdout, "COPY (%7ld bytes):\t\t", test->size);
  129. if (ret < 0)
  130. fprintf(stdout, "TEST FAILED\n");
  131. else
  132. fprintf(stdout, "%s\n", result[ret]);
  133. }
  134. fflush(stdout);
  135. close(fd);
  136. return (ret < 0) ? ret : 1 - ret; /* return 0 if test succeeded */
  137. }
  138. int main(int argc, char **argv)
  139. {
  140. int c;
  141. struct pci_test *test;
  142. test = calloc(1, sizeof(*test));
  143. if (!test) {
  144. perror("Fail to allocate memory for pci_test\n");
  145. return -ENOMEM;
  146. }
  147. /* since '0' is a valid BAR number, initialize it to -1 */
  148. test->barnum = -1;
  149. /* set default size as 100KB */
  150. test->size = 0x19000;
  151. /* set default endpoint device */
  152. test->device = "/dev/pci-endpoint-test.0";
  153. while ((c = getopt(argc, argv, "D:b:m:x:i:deIlhrwcs:")) != EOF)
  154. switch (c) {
  155. case 'D':
  156. test->device = optarg;
  157. continue;
  158. case 'b':
  159. test->barnum = atoi(optarg);
  160. if (test->barnum < 0 || test->barnum > 5)
  161. goto usage;
  162. continue;
  163. case 'l':
  164. test->legacyirq = true;
  165. continue;
  166. case 'm':
  167. test->msinum = atoi(optarg);
  168. if (test->msinum < 1 || test->msinum > 32)
  169. goto usage;
  170. continue;
  171. case 'x':
  172. test->msixnum = atoi(optarg);
  173. if (test->msixnum < 1 || test->msixnum > 2048)
  174. goto usage;
  175. continue;
  176. case 'i':
  177. test->irqtype = atoi(optarg);
  178. if (test->irqtype < 0 || test->irqtype > 2)
  179. goto usage;
  180. test->set_irqtype = true;
  181. continue;
  182. case 'I':
  183. test->get_irqtype = true;
  184. continue;
  185. case 'r':
  186. test->read = true;
  187. continue;
  188. case 'w':
  189. test->write = true;
  190. continue;
  191. case 'c':
  192. test->copy = true;
  193. continue;
  194. case 'e':
  195. test->clear_irq = true;
  196. continue;
  197. case 's':
  198. test->size = strtoul(optarg, NULL, 0);
  199. continue;
  200. case 'd':
  201. test->use_dma = true;
  202. continue;
  203. case 'h':
  204. default:
  205. usage:
  206. fprintf(stderr,
  207. "usage: %s [options]\n"
  208. "Options:\n"
  209. "\t-D <dev> PCI endpoint test device {default: /dev/pci-endpoint-test.0}\n"
  210. "\t-b <bar num> BAR test (bar number between 0..5)\n"
  211. "\t-m <msi num> MSI test (msi number between 1..32)\n"
  212. "\t-x <msix num> \tMSI-X test (msix number between 1..2048)\n"
  213. "\t-i <irq type> \tSet IRQ type (0 - Legacy, 1 - MSI, 2 - MSI-X)\n"
  214. "\t-e Clear IRQ\n"
  215. "\t-I Get current IRQ type configured\n"
  216. "\t-d Use DMA\n"
  217. "\t-l Legacy IRQ test\n"
  218. "\t-r Read buffer test\n"
  219. "\t-w Write buffer test\n"
  220. "\t-c Copy buffer test\n"
  221. "\t-s <size> Size of buffer {default: 100KB}\n"
  222. "\t-h Print this help message\n",
  223. argv[0]);
  224. return -EINVAL;
  225. }
  226. return run_test(test);
  227. }