pcitest.c 5.8 KB

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