pci_endpoint_test.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /**
  2. * Host side test driver to test endpoint functionality
  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 <linux/crc32.h>
  20. #include <linux/delay.h>
  21. #include <linux/fs.h>
  22. #include <linux/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/module.h>
  27. #include <linux/mutex.h>
  28. #include <linux/random.h>
  29. #include <linux/slab.h>
  30. #include <linux/pci.h>
  31. #include <linux/pci_ids.h>
  32. #include <linux/pci_regs.h>
  33. #include <uapi/linux/pcitest.h>
  34. #define DRV_MODULE_NAME "pci-endpoint-test"
  35. #define IRQ_TYPE_UNDEFINED -1
  36. #define IRQ_TYPE_LEGACY 0
  37. #define IRQ_TYPE_MSI 1
  38. #define IRQ_TYPE_MSIX 2
  39. #define PCI_ENDPOINT_TEST_MAGIC 0x0
  40. #define PCI_ENDPOINT_TEST_COMMAND 0x4
  41. #define COMMAND_RAISE_LEGACY_IRQ BIT(0)
  42. #define COMMAND_RAISE_MSI_IRQ BIT(1)
  43. #define COMMAND_RAISE_MSIX_IRQ BIT(2)
  44. #define COMMAND_READ BIT(3)
  45. #define COMMAND_WRITE BIT(4)
  46. #define COMMAND_COPY BIT(5)
  47. #define PCI_ENDPOINT_TEST_STATUS 0x8
  48. #define STATUS_READ_SUCCESS BIT(0)
  49. #define STATUS_READ_FAIL BIT(1)
  50. #define STATUS_WRITE_SUCCESS BIT(2)
  51. #define STATUS_WRITE_FAIL BIT(3)
  52. #define STATUS_COPY_SUCCESS BIT(4)
  53. #define STATUS_COPY_FAIL BIT(5)
  54. #define STATUS_IRQ_RAISED BIT(6)
  55. #define STATUS_SRC_ADDR_INVALID BIT(7)
  56. #define STATUS_DST_ADDR_INVALID BIT(8)
  57. #define PCI_ENDPOINT_TEST_LOWER_SRC_ADDR 0x0c
  58. #define PCI_ENDPOINT_TEST_UPPER_SRC_ADDR 0x10
  59. #define PCI_ENDPOINT_TEST_LOWER_DST_ADDR 0x14
  60. #define PCI_ENDPOINT_TEST_UPPER_DST_ADDR 0x18
  61. #define PCI_ENDPOINT_TEST_SIZE 0x1c
  62. #define PCI_ENDPOINT_TEST_CHECKSUM 0x20
  63. #define PCI_ENDPOINT_TEST_IRQ_TYPE 0x24
  64. #define PCI_ENDPOINT_TEST_IRQ_NUMBER 0x28
  65. static DEFINE_IDA(pci_endpoint_test_ida);
  66. #define to_endpoint_test(priv) container_of((priv), struct pci_endpoint_test, \
  67. miscdev)
  68. static bool no_msi;
  69. module_param(no_msi, bool, 0444);
  70. MODULE_PARM_DESC(no_msi, "Disable MSI interrupt in pci_endpoint_test");
  71. static int irq_type = IRQ_TYPE_MSI;
  72. module_param(irq_type, int, 0444);
  73. MODULE_PARM_DESC(irq_type, "IRQ mode selection in pci_endpoint_test (0 - Legacy, 1 - MSI, 2 - MSI-X)");
  74. enum pci_barno {
  75. BAR_0,
  76. BAR_1,
  77. BAR_2,
  78. BAR_3,
  79. BAR_4,
  80. BAR_5,
  81. };
  82. struct pci_endpoint_test {
  83. struct pci_dev *pdev;
  84. void __iomem *base;
  85. void __iomem *bar[6];
  86. struct completion irq_raised;
  87. int last_irq;
  88. int num_irqs;
  89. /* mutex to protect the ioctls */
  90. struct mutex mutex;
  91. struct miscdevice miscdev;
  92. enum pci_barno test_reg_bar;
  93. size_t alignment;
  94. };
  95. struct pci_endpoint_test_data {
  96. enum pci_barno test_reg_bar;
  97. size_t alignment;
  98. int irq_type;
  99. };
  100. static inline u32 pci_endpoint_test_readl(struct pci_endpoint_test *test,
  101. u32 offset)
  102. {
  103. return readl(test->base + offset);
  104. }
  105. static inline void pci_endpoint_test_writel(struct pci_endpoint_test *test,
  106. u32 offset, u32 value)
  107. {
  108. writel(value, test->base + offset);
  109. }
  110. static inline u32 pci_endpoint_test_bar_readl(struct pci_endpoint_test *test,
  111. int bar, int offset)
  112. {
  113. return readl(test->bar[bar] + offset);
  114. }
  115. static inline void pci_endpoint_test_bar_writel(struct pci_endpoint_test *test,
  116. int bar, u32 offset, u32 value)
  117. {
  118. writel(value, test->bar[bar] + offset);
  119. }
  120. static irqreturn_t pci_endpoint_test_irqhandler(int irq, void *dev_id)
  121. {
  122. struct pci_endpoint_test *test = dev_id;
  123. u32 reg;
  124. reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
  125. if (reg & STATUS_IRQ_RAISED) {
  126. test->last_irq = irq;
  127. complete(&test->irq_raised);
  128. reg &= ~STATUS_IRQ_RAISED;
  129. }
  130. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_STATUS,
  131. reg);
  132. return IRQ_HANDLED;
  133. }
  134. static void pci_endpoint_test_free_irq_vectors(struct pci_endpoint_test *test)
  135. {
  136. struct pci_dev *pdev = test->pdev;
  137. pci_free_irq_vectors(pdev);
  138. }
  139. static bool pci_endpoint_test_alloc_irq_vectors(struct pci_endpoint_test *test,
  140. int type)
  141. {
  142. int irq = -1;
  143. struct pci_dev *pdev = test->pdev;
  144. struct device *dev = &pdev->dev;
  145. bool res = true;
  146. switch (type) {
  147. case IRQ_TYPE_LEGACY:
  148. irq = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_LEGACY);
  149. if (irq < 0)
  150. dev_err(dev, "Failed to get Legacy interrupt\n");
  151. break;
  152. case IRQ_TYPE_MSI:
  153. irq = pci_alloc_irq_vectors(pdev, 1, 32, PCI_IRQ_MSI);
  154. if (irq < 0)
  155. dev_err(dev, "Failed to get MSI interrupts\n");
  156. break;
  157. case IRQ_TYPE_MSIX:
  158. irq = pci_alloc_irq_vectors(pdev, 1, 2048, PCI_IRQ_MSIX);
  159. if (irq < 0)
  160. dev_err(dev, "Failed to get MSI-X interrupts\n");
  161. break;
  162. default:
  163. dev_err(dev, "Invalid IRQ type selected\n");
  164. }
  165. if (irq < 0) {
  166. irq = 0;
  167. res = false;
  168. }
  169. test->num_irqs = irq;
  170. return res;
  171. }
  172. static void pci_endpoint_test_release_irq(struct pci_endpoint_test *test)
  173. {
  174. int i;
  175. struct pci_dev *pdev = test->pdev;
  176. struct device *dev = &pdev->dev;
  177. for (i = 0; i < test->num_irqs; i++)
  178. devm_free_irq(dev, pci_irq_vector(pdev, i), test);
  179. test->num_irqs = 0;
  180. }
  181. static bool pci_endpoint_test_request_irq(struct pci_endpoint_test *test)
  182. {
  183. int i;
  184. int err;
  185. struct pci_dev *pdev = test->pdev;
  186. struct device *dev = &pdev->dev;
  187. for (i = 0; i < test->num_irqs; i++) {
  188. err = devm_request_irq(dev, pci_irq_vector(pdev, i),
  189. pci_endpoint_test_irqhandler,
  190. IRQF_SHARED, DRV_MODULE_NAME, test);
  191. if (err)
  192. goto fail;
  193. }
  194. return true;
  195. fail:
  196. switch (irq_type) {
  197. case IRQ_TYPE_LEGACY:
  198. dev_err(dev, "Failed to request IRQ %d for Legacy\n",
  199. pci_irq_vector(pdev, i));
  200. break;
  201. case IRQ_TYPE_MSI:
  202. dev_err(dev, "Failed to request IRQ %d for MSI %d\n",
  203. pci_irq_vector(pdev, i),
  204. i + 1);
  205. break;
  206. case IRQ_TYPE_MSIX:
  207. dev_err(dev, "Failed to request IRQ %d for MSI-X %d\n",
  208. pci_irq_vector(pdev, i),
  209. i + 1);
  210. break;
  211. }
  212. return false;
  213. }
  214. static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
  215. enum pci_barno barno)
  216. {
  217. int j;
  218. u32 val;
  219. int size;
  220. struct pci_dev *pdev = test->pdev;
  221. if (!test->bar[barno])
  222. return false;
  223. size = pci_resource_len(pdev, barno);
  224. if (barno == test->test_reg_bar)
  225. size = 0x4;
  226. for (j = 0; j < size; j += 4)
  227. pci_endpoint_test_bar_writel(test, barno, j, 0xA0A0A0A0);
  228. for (j = 0; j < size; j += 4) {
  229. val = pci_endpoint_test_bar_readl(test, barno, j);
  230. if (val != 0xA0A0A0A0)
  231. return false;
  232. }
  233. return true;
  234. }
  235. static bool pci_endpoint_test_legacy_irq(struct pci_endpoint_test *test)
  236. {
  237. u32 val;
  238. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
  239. IRQ_TYPE_LEGACY);
  240. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 0);
  241. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  242. COMMAND_RAISE_LEGACY_IRQ);
  243. val = wait_for_completion_timeout(&test->irq_raised,
  244. msecs_to_jiffies(1000));
  245. if (!val)
  246. return false;
  247. return true;
  248. }
  249. static bool pci_endpoint_test_msi_irq(struct pci_endpoint_test *test,
  250. u16 msi_num, bool msix)
  251. {
  252. u32 val;
  253. struct pci_dev *pdev = test->pdev;
  254. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE,
  255. msix == false ? IRQ_TYPE_MSI :
  256. IRQ_TYPE_MSIX);
  257. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, msi_num);
  258. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  259. msix == false ? COMMAND_RAISE_MSI_IRQ :
  260. COMMAND_RAISE_MSIX_IRQ);
  261. val = wait_for_completion_timeout(&test->irq_raised,
  262. msecs_to_jiffies(1000));
  263. if (!val)
  264. return false;
  265. if (pci_irq_vector(pdev, msi_num - 1) == test->last_irq)
  266. return true;
  267. return false;
  268. }
  269. static bool pci_endpoint_test_copy(struct pci_endpoint_test *test, size_t size)
  270. {
  271. bool ret = false;
  272. void *src_addr;
  273. void *dst_addr;
  274. dma_addr_t src_phys_addr;
  275. dma_addr_t dst_phys_addr;
  276. struct pci_dev *pdev = test->pdev;
  277. struct device *dev = &pdev->dev;
  278. void *orig_src_addr;
  279. dma_addr_t orig_src_phys_addr;
  280. void *orig_dst_addr;
  281. dma_addr_t orig_dst_phys_addr;
  282. size_t offset;
  283. size_t alignment = test->alignment;
  284. u32 src_crc32;
  285. u32 dst_crc32;
  286. if (size > SIZE_MAX - alignment)
  287. goto err;
  288. if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
  289. dev_err(dev, "Invalid IRQ type option\n");
  290. goto err;
  291. }
  292. orig_src_addr = dma_alloc_coherent(dev, size + alignment,
  293. &orig_src_phys_addr, GFP_KERNEL);
  294. if (!orig_src_addr) {
  295. dev_err(dev, "Failed to allocate source buffer\n");
  296. ret = false;
  297. goto err;
  298. }
  299. if (alignment && !IS_ALIGNED(orig_src_phys_addr, alignment)) {
  300. src_phys_addr = PTR_ALIGN(orig_src_phys_addr, alignment);
  301. offset = src_phys_addr - orig_src_phys_addr;
  302. src_addr = orig_src_addr + offset;
  303. } else {
  304. src_phys_addr = orig_src_phys_addr;
  305. src_addr = orig_src_addr;
  306. }
  307. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
  308. lower_32_bits(src_phys_addr));
  309. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
  310. upper_32_bits(src_phys_addr));
  311. get_random_bytes(src_addr, size);
  312. src_crc32 = crc32_le(~0, src_addr, size);
  313. orig_dst_addr = dma_alloc_coherent(dev, size + alignment,
  314. &orig_dst_phys_addr, GFP_KERNEL);
  315. if (!orig_dst_addr) {
  316. dev_err(dev, "Failed to allocate destination address\n");
  317. ret = false;
  318. goto err_orig_src_addr;
  319. }
  320. if (alignment && !IS_ALIGNED(orig_dst_phys_addr, alignment)) {
  321. dst_phys_addr = PTR_ALIGN(orig_dst_phys_addr, alignment);
  322. offset = dst_phys_addr - orig_dst_phys_addr;
  323. dst_addr = orig_dst_addr + offset;
  324. } else {
  325. dst_phys_addr = orig_dst_phys_addr;
  326. dst_addr = orig_dst_addr;
  327. }
  328. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
  329. lower_32_bits(dst_phys_addr));
  330. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
  331. upper_32_bits(dst_phys_addr));
  332. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE,
  333. size);
  334. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
  335. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
  336. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  337. COMMAND_COPY);
  338. wait_for_completion(&test->irq_raised);
  339. dst_crc32 = crc32_le(~0, dst_addr, size);
  340. if (dst_crc32 == src_crc32)
  341. ret = true;
  342. dma_free_coherent(dev, size + alignment, orig_dst_addr,
  343. orig_dst_phys_addr);
  344. err_orig_src_addr:
  345. dma_free_coherent(dev, size + alignment, orig_src_addr,
  346. orig_src_phys_addr);
  347. err:
  348. return ret;
  349. }
  350. static bool pci_endpoint_test_write(struct pci_endpoint_test *test, size_t size)
  351. {
  352. bool ret = false;
  353. u32 reg;
  354. void *addr;
  355. dma_addr_t phys_addr;
  356. struct pci_dev *pdev = test->pdev;
  357. struct device *dev = &pdev->dev;
  358. void *orig_addr;
  359. dma_addr_t orig_phys_addr;
  360. size_t offset;
  361. size_t alignment = test->alignment;
  362. u32 crc32;
  363. if (size > SIZE_MAX - alignment)
  364. goto err;
  365. if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
  366. dev_err(dev, "Invalid IRQ type option\n");
  367. goto err;
  368. }
  369. orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
  370. GFP_KERNEL);
  371. if (!orig_addr) {
  372. dev_err(dev, "Failed to allocate address\n");
  373. ret = false;
  374. goto err;
  375. }
  376. if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
  377. phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
  378. offset = phys_addr - orig_phys_addr;
  379. addr = orig_addr + offset;
  380. } else {
  381. phys_addr = orig_phys_addr;
  382. addr = orig_addr;
  383. }
  384. get_random_bytes(addr, size);
  385. crc32 = crc32_le(~0, addr, size);
  386. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_CHECKSUM,
  387. crc32);
  388. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_SRC_ADDR,
  389. lower_32_bits(phys_addr));
  390. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_SRC_ADDR,
  391. upper_32_bits(phys_addr));
  392. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
  393. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
  394. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
  395. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  396. COMMAND_READ);
  397. wait_for_completion(&test->irq_raised);
  398. reg = pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_STATUS);
  399. if (reg & STATUS_READ_SUCCESS)
  400. ret = true;
  401. dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
  402. err:
  403. return ret;
  404. }
  405. static bool pci_endpoint_test_read(struct pci_endpoint_test *test, size_t size)
  406. {
  407. bool ret = false;
  408. void *addr;
  409. dma_addr_t phys_addr;
  410. struct pci_dev *pdev = test->pdev;
  411. struct device *dev = &pdev->dev;
  412. void *orig_addr;
  413. dma_addr_t orig_phys_addr;
  414. size_t offset;
  415. size_t alignment = test->alignment;
  416. u32 crc32;
  417. if (size > SIZE_MAX - alignment)
  418. goto err;
  419. if (irq_type < IRQ_TYPE_LEGACY || irq_type > IRQ_TYPE_MSIX) {
  420. dev_err(dev, "Invalid IRQ type option\n");
  421. goto err;
  422. }
  423. orig_addr = dma_alloc_coherent(dev, size + alignment, &orig_phys_addr,
  424. GFP_KERNEL);
  425. if (!orig_addr) {
  426. dev_err(dev, "Failed to allocate destination address\n");
  427. ret = false;
  428. goto err;
  429. }
  430. if (alignment && !IS_ALIGNED(orig_phys_addr, alignment)) {
  431. phys_addr = PTR_ALIGN(orig_phys_addr, alignment);
  432. offset = phys_addr - orig_phys_addr;
  433. addr = orig_addr + offset;
  434. } else {
  435. phys_addr = orig_phys_addr;
  436. addr = orig_addr;
  437. }
  438. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_LOWER_DST_ADDR,
  439. lower_32_bits(phys_addr));
  440. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_UPPER_DST_ADDR,
  441. upper_32_bits(phys_addr));
  442. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_SIZE, size);
  443. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_TYPE, irq_type);
  444. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_IRQ_NUMBER, 1);
  445. pci_endpoint_test_writel(test, PCI_ENDPOINT_TEST_COMMAND,
  446. COMMAND_WRITE);
  447. wait_for_completion(&test->irq_raised);
  448. crc32 = crc32_le(~0, addr, size);
  449. if (crc32 == pci_endpoint_test_readl(test, PCI_ENDPOINT_TEST_CHECKSUM))
  450. ret = true;
  451. dma_free_coherent(dev, size + alignment, orig_addr, orig_phys_addr);
  452. err:
  453. return ret;
  454. }
  455. static bool pci_endpoint_test_set_irq(struct pci_endpoint_test *test,
  456. int req_irq_type)
  457. {
  458. struct pci_dev *pdev = test->pdev;
  459. struct device *dev = &pdev->dev;
  460. if (req_irq_type < IRQ_TYPE_LEGACY || req_irq_type > IRQ_TYPE_MSIX) {
  461. dev_err(dev, "Invalid IRQ type option\n");
  462. return false;
  463. }
  464. if (irq_type == req_irq_type)
  465. return true;
  466. pci_endpoint_test_release_irq(test);
  467. pci_endpoint_test_free_irq_vectors(test);
  468. if (!pci_endpoint_test_alloc_irq_vectors(test, req_irq_type))
  469. goto err;
  470. if (!pci_endpoint_test_request_irq(test))
  471. goto err;
  472. irq_type = req_irq_type;
  473. return true;
  474. err:
  475. pci_endpoint_test_free_irq_vectors(test);
  476. irq_type = IRQ_TYPE_UNDEFINED;
  477. return false;
  478. }
  479. static long pci_endpoint_test_ioctl(struct file *file, unsigned int cmd,
  480. unsigned long arg)
  481. {
  482. int ret = -EINVAL;
  483. enum pci_barno bar;
  484. struct pci_endpoint_test *test = to_endpoint_test(file->private_data);
  485. mutex_lock(&test->mutex);
  486. switch (cmd) {
  487. case PCITEST_BAR:
  488. bar = arg;
  489. if (bar < 0 || bar > 5)
  490. goto ret;
  491. ret = pci_endpoint_test_bar(test, bar);
  492. break;
  493. case PCITEST_LEGACY_IRQ:
  494. ret = pci_endpoint_test_legacy_irq(test);
  495. break;
  496. case PCITEST_MSI:
  497. case PCITEST_MSIX:
  498. ret = pci_endpoint_test_msi_irq(test, arg, cmd == PCITEST_MSIX);
  499. break;
  500. case PCITEST_WRITE:
  501. ret = pci_endpoint_test_write(test, arg);
  502. break;
  503. case PCITEST_READ:
  504. ret = pci_endpoint_test_read(test, arg);
  505. break;
  506. case PCITEST_COPY:
  507. ret = pci_endpoint_test_copy(test, arg);
  508. break;
  509. case PCITEST_SET_IRQTYPE:
  510. ret = pci_endpoint_test_set_irq(test, arg);
  511. break;
  512. case PCITEST_GET_IRQTYPE:
  513. ret = irq_type;
  514. break;
  515. }
  516. ret:
  517. mutex_unlock(&test->mutex);
  518. return ret;
  519. }
  520. static const struct file_operations pci_endpoint_test_fops = {
  521. .owner = THIS_MODULE,
  522. .unlocked_ioctl = pci_endpoint_test_ioctl,
  523. };
  524. static int pci_endpoint_test_probe(struct pci_dev *pdev,
  525. const struct pci_device_id *ent)
  526. {
  527. int err;
  528. int id;
  529. char name[20];
  530. enum pci_barno bar;
  531. void __iomem *base;
  532. struct device *dev = &pdev->dev;
  533. struct pci_endpoint_test *test;
  534. struct pci_endpoint_test_data *data;
  535. enum pci_barno test_reg_bar = BAR_0;
  536. struct miscdevice *misc_device;
  537. if (pci_is_bridge(pdev))
  538. return -ENODEV;
  539. test = devm_kzalloc(dev, sizeof(*test), GFP_KERNEL);
  540. if (!test)
  541. return -ENOMEM;
  542. test->test_reg_bar = 0;
  543. test->alignment = 0;
  544. test->pdev = pdev;
  545. if (no_msi)
  546. irq_type = IRQ_TYPE_LEGACY;
  547. data = (struct pci_endpoint_test_data *)ent->driver_data;
  548. if (data) {
  549. test_reg_bar = data->test_reg_bar;
  550. test->alignment = data->alignment;
  551. irq_type = data->irq_type;
  552. }
  553. init_completion(&test->irq_raised);
  554. mutex_init(&test->mutex);
  555. err = pci_enable_device(pdev);
  556. if (err) {
  557. dev_err(dev, "Cannot enable PCI device\n");
  558. return err;
  559. }
  560. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  561. if (err) {
  562. dev_err(dev, "Cannot obtain PCI resources\n");
  563. goto err_disable_pdev;
  564. }
  565. pci_set_master(pdev);
  566. if (!pci_endpoint_test_alloc_irq_vectors(test, irq_type))
  567. goto err_disable_irq;
  568. if (!pci_endpoint_test_request_irq(test))
  569. goto err_disable_irq;
  570. for (bar = BAR_0; bar <= BAR_5; bar++) {
  571. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM) {
  572. base = pci_ioremap_bar(pdev, bar);
  573. if (!base) {
  574. dev_err(dev, "Failed to read BAR%d\n", bar);
  575. WARN_ON(bar == test_reg_bar);
  576. }
  577. test->bar[bar] = base;
  578. }
  579. }
  580. test->base = test->bar[test_reg_bar];
  581. if (!test->base) {
  582. err = -ENOMEM;
  583. dev_err(dev, "Cannot perform PCI test without BAR%d\n",
  584. test_reg_bar);
  585. goto err_iounmap;
  586. }
  587. pci_set_drvdata(pdev, test);
  588. id = ida_simple_get(&pci_endpoint_test_ida, 0, 0, GFP_KERNEL);
  589. if (id < 0) {
  590. err = id;
  591. dev_err(dev, "Unable to get id\n");
  592. goto err_iounmap;
  593. }
  594. snprintf(name, sizeof(name), DRV_MODULE_NAME ".%d", id);
  595. misc_device = &test->miscdev;
  596. misc_device->minor = MISC_DYNAMIC_MINOR;
  597. misc_device->name = kstrdup(name, GFP_KERNEL);
  598. if (!misc_device->name) {
  599. err = -ENOMEM;
  600. goto err_ida_remove;
  601. }
  602. misc_device->fops = &pci_endpoint_test_fops,
  603. err = misc_register(misc_device);
  604. if (err) {
  605. dev_err(dev, "Failed to register device\n");
  606. goto err_kfree_name;
  607. }
  608. return 0;
  609. err_kfree_name:
  610. kfree(misc_device->name);
  611. err_ida_remove:
  612. ida_simple_remove(&pci_endpoint_test_ida, id);
  613. err_iounmap:
  614. for (bar = BAR_0; bar <= BAR_5; bar++) {
  615. if (test->bar[bar])
  616. pci_iounmap(pdev, test->bar[bar]);
  617. }
  618. pci_endpoint_test_release_irq(test);
  619. err_disable_irq:
  620. pci_endpoint_test_free_irq_vectors(test);
  621. pci_release_regions(pdev);
  622. err_disable_pdev:
  623. pci_disable_device(pdev);
  624. return err;
  625. }
  626. static void pci_endpoint_test_remove(struct pci_dev *pdev)
  627. {
  628. int id;
  629. enum pci_barno bar;
  630. struct pci_endpoint_test *test = pci_get_drvdata(pdev);
  631. struct miscdevice *misc_device = &test->miscdev;
  632. if (sscanf(misc_device->name, DRV_MODULE_NAME ".%d", &id) != 1)
  633. return;
  634. if (id < 0)
  635. return;
  636. misc_deregister(&test->miscdev);
  637. kfree(misc_device->name);
  638. ida_simple_remove(&pci_endpoint_test_ida, id);
  639. for (bar = BAR_0; bar <= BAR_5; bar++) {
  640. if (test->bar[bar])
  641. pci_iounmap(pdev, test->bar[bar]);
  642. }
  643. pci_endpoint_test_release_irq(test);
  644. pci_endpoint_test_free_irq_vectors(test);
  645. pci_release_regions(pdev);
  646. pci_disable_device(pdev);
  647. }
  648. static const struct pci_device_id pci_endpoint_test_tbl[] = {
  649. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA74x) },
  650. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_DRA72x) },
  651. { PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, 0xedda) },
  652. { }
  653. };
  654. MODULE_DEVICE_TABLE(pci, pci_endpoint_test_tbl);
  655. static struct pci_driver pci_endpoint_test_driver = {
  656. .name = DRV_MODULE_NAME,
  657. .id_table = pci_endpoint_test_tbl,
  658. .probe = pci_endpoint_test_probe,
  659. .remove = pci_endpoint_test_remove,
  660. };
  661. module_pci_driver(pci_endpoint_test_driver);
  662. MODULE_DESCRIPTION("PCI ENDPOINT TEST HOST DRIVER");
  663. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  664. MODULE_LICENSE("GPL v2");