pcitest.c 5.5 KB

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