pcitest.c 5.5 KB

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