pci_endpoint_test.c 17 KB

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