jsm_driver.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /************************************************************************
  3. * Copyright 2003 Digi International (www.digi.com)
  4. *
  5. * Copyright (C) 2004 IBM Corporation. All rights reserved.
  6. *
  7. * Contact Information:
  8. * Scott H Kilau <Scott_Kilau@digi.com>
  9. * Wendy Xiong <wendyx@us.ibm.com>
  10. *
  11. *
  12. ***********************************************************************/
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/slab.h>
  16. #include "jsm.h"
  17. MODULE_AUTHOR("Digi International, http://www.digi.com");
  18. MODULE_DESCRIPTION("Driver for the Digi International Neo and Classic PCI based product line");
  19. MODULE_LICENSE("GPL");
  20. MODULE_SUPPORTED_DEVICE("jsm");
  21. #define JSM_DRIVER_NAME "jsm"
  22. #define NR_PORTS 32
  23. #define JSM_MINOR_START 0
  24. struct uart_driver jsm_uart_driver = {
  25. .owner = THIS_MODULE,
  26. .driver_name = JSM_DRIVER_NAME,
  27. .dev_name = "ttyn",
  28. .major = 0,
  29. .minor = JSM_MINOR_START,
  30. .nr = NR_PORTS,
  31. };
  32. static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
  33. pci_channel_state_t state);
  34. static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
  35. static void jsm_io_resume(struct pci_dev *pdev);
  36. static const struct pci_error_handlers jsm_err_handler = {
  37. .error_detected = jsm_io_error_detected,
  38. .slot_reset = jsm_io_slot_reset,
  39. .resume = jsm_io_resume,
  40. };
  41. int jsm_debug;
  42. module_param(jsm_debug, int, 0);
  43. MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
  44. static int jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  45. {
  46. int rc = 0;
  47. struct jsm_board *brd;
  48. static int adapter_count;
  49. rc = pci_enable_device(pdev);
  50. if (rc) {
  51. dev_err(&pdev->dev, "Device enable FAILED\n");
  52. goto out;
  53. }
  54. rc = pci_request_regions(pdev, JSM_DRIVER_NAME);
  55. if (rc) {
  56. dev_err(&pdev->dev, "pci_request_region FAILED\n");
  57. goto out_disable_device;
  58. }
  59. brd = kzalloc(sizeof(*brd), GFP_KERNEL);
  60. if (!brd) {
  61. rc = -ENOMEM;
  62. goto out_release_regions;
  63. }
  64. /* store the info for the board we've found */
  65. brd->boardnum = adapter_count++;
  66. brd->pci_dev = pdev;
  67. switch (pdev->device) {
  68. case PCI_DEVICE_ID_NEO_2DB9:
  69. case PCI_DEVICE_ID_NEO_2DB9PRI:
  70. case PCI_DEVICE_ID_NEO_2RJ45:
  71. case PCI_DEVICE_ID_NEO_2RJ45PRI:
  72. case PCI_DEVICE_ID_NEO_2_422_485:
  73. brd->maxports = 2;
  74. break;
  75. case PCI_DEVICE_ID_CLASSIC_4:
  76. case PCI_DEVICE_ID_CLASSIC_4_422:
  77. case PCI_DEVICE_ID_NEO_4:
  78. case PCIE_DEVICE_ID_NEO_4:
  79. case PCIE_DEVICE_ID_NEO_4RJ45:
  80. case PCIE_DEVICE_ID_NEO_4_IBM:
  81. brd->maxports = 4;
  82. break;
  83. case PCI_DEVICE_ID_CLASSIC_8:
  84. case PCI_DEVICE_ID_CLASSIC_8_422:
  85. case PCI_DEVICE_ID_DIGI_NEO_8:
  86. case PCIE_DEVICE_ID_NEO_8:
  87. case PCIE_DEVICE_ID_NEO_8RJ45:
  88. brd->maxports = 8;
  89. break;
  90. default:
  91. brd->maxports = 1;
  92. break;
  93. }
  94. spin_lock_init(&brd->bd_intr_lock);
  95. /* store which revision we have */
  96. brd->rev = pdev->revision;
  97. brd->irq = pdev->irq;
  98. switch (pdev->device) {
  99. case PCI_DEVICE_ID_CLASSIC_4:
  100. case PCI_DEVICE_ID_CLASSIC_4_422:
  101. case PCI_DEVICE_ID_CLASSIC_8:
  102. case PCI_DEVICE_ID_CLASSIC_8_422:
  103. jsm_dbg(INIT, &brd->pci_dev,
  104. "jsm_found_board - Classic adapter\n");
  105. /*
  106. * For PCI ClassicBoards
  107. * PCI Local Address (.i.e. "resource" number) space
  108. * 0 PLX Memory Mapped Config
  109. * 1 PLX I/O Mapped Config
  110. * 2 I/O Mapped UARTs and Status
  111. * 3 Memory Mapped VPD
  112. * 4 Memory Mapped UARTs and Status
  113. */
  114. /* Get the PCI Base Address Registers */
  115. brd->membase = pci_resource_start(pdev, 4);
  116. brd->membase_end = pci_resource_end(pdev, 4);
  117. if (brd->membase & 0x1)
  118. brd->membase &= ~0x3;
  119. else
  120. brd->membase &= ~0xF;
  121. brd->iobase = pci_resource_start(pdev, 1);
  122. brd->iobase_end = pci_resource_end(pdev, 1);
  123. brd->iobase = ((unsigned int)(brd->iobase)) & 0xFFFE;
  124. /* Assign the board_ops struct */
  125. brd->bd_ops = &jsm_cls_ops;
  126. brd->bd_uart_offset = 0x8;
  127. brd->bd_dividend = 921600;
  128. brd->re_map_membase = ioremap(brd->membase,
  129. pci_resource_len(pdev, 4));
  130. if (!brd->re_map_membase) {
  131. dev_err(&pdev->dev,
  132. "Card has no PCI Memory resources, failing board.\n");
  133. rc = -ENOMEM;
  134. goto out_kfree_brd;
  135. }
  136. /*
  137. * Enable Local Interrupt 1 (0x1),
  138. * Local Interrupt 1 Polarity Active high (0x2),
  139. * Enable PCI interrupt (0x43)
  140. */
  141. outb(0x43, brd->iobase + 0x4c);
  142. break;
  143. case PCI_DEVICE_ID_NEO_2DB9:
  144. case PCI_DEVICE_ID_NEO_2DB9PRI:
  145. case PCI_DEVICE_ID_NEO_2RJ45:
  146. case PCI_DEVICE_ID_NEO_2RJ45PRI:
  147. case PCI_DEVICE_ID_NEO_2_422_485:
  148. case PCI_DEVICE_ID_NEO_4:
  149. case PCIE_DEVICE_ID_NEO_4:
  150. case PCIE_DEVICE_ID_NEO_4RJ45:
  151. case PCIE_DEVICE_ID_NEO_4_IBM:
  152. case PCI_DEVICE_ID_DIGI_NEO_8:
  153. case PCIE_DEVICE_ID_NEO_8:
  154. case PCIE_DEVICE_ID_NEO_8RJ45:
  155. jsm_dbg(INIT, &brd->pci_dev, "jsm_found_board - NEO adapter\n");
  156. /* get the PCI Base Address Registers */
  157. brd->membase = pci_resource_start(pdev, 0);
  158. brd->membase_end = pci_resource_end(pdev, 0);
  159. if (brd->membase & 1)
  160. brd->membase &= ~0x3;
  161. else
  162. brd->membase &= ~0xF;
  163. /* Assign the board_ops struct */
  164. brd->bd_ops = &jsm_neo_ops;
  165. brd->bd_uart_offset = 0x200;
  166. brd->bd_dividend = 921600;
  167. brd->re_map_membase = ioremap(brd->membase,
  168. pci_resource_len(pdev, 0));
  169. if (!brd->re_map_membase) {
  170. dev_err(&pdev->dev,
  171. "Card has no PCI Memory resources, failing board.\n");
  172. rc = -ENOMEM;
  173. goto out_kfree_brd;
  174. }
  175. break;
  176. default:
  177. return -ENXIO;
  178. }
  179. rc = request_irq(brd->irq, brd->bd_ops->intr, IRQF_SHARED, "JSM", brd);
  180. if (rc) {
  181. dev_warn(&pdev->dev, "Failed to hook IRQ %d\n", brd->irq);
  182. goto out_iounmap;
  183. }
  184. rc = jsm_tty_init(brd);
  185. if (rc < 0) {
  186. dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
  187. rc = -ENXIO;
  188. goto out_free_irq;
  189. }
  190. rc = jsm_uart_port_init(brd);
  191. if (rc < 0) {
  192. /* XXX: leaking all resources from jsm_tty_init here! */
  193. dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
  194. rc = -ENXIO;
  195. goto out_free_irq;
  196. }
  197. /* Log the information about the board */
  198. dev_info(&pdev->dev, "board %d: Digi Classic/Neo (rev %d), irq %d\n",
  199. adapter_count, brd->rev, brd->irq);
  200. pci_set_drvdata(pdev, brd);
  201. pci_save_state(pdev);
  202. return 0;
  203. out_free_irq:
  204. jsm_remove_uart_port(brd);
  205. free_irq(brd->irq, brd);
  206. out_iounmap:
  207. iounmap(brd->re_map_membase);
  208. out_kfree_brd:
  209. kfree(brd);
  210. out_release_regions:
  211. pci_release_regions(pdev);
  212. out_disable_device:
  213. pci_disable_device(pdev);
  214. out:
  215. return rc;
  216. }
  217. static void jsm_remove_one(struct pci_dev *pdev)
  218. {
  219. struct jsm_board *brd = pci_get_drvdata(pdev);
  220. int i = 0;
  221. switch (pdev->device) {
  222. case PCI_DEVICE_ID_CLASSIC_4:
  223. case PCI_DEVICE_ID_CLASSIC_4_422:
  224. case PCI_DEVICE_ID_CLASSIC_8:
  225. case PCI_DEVICE_ID_CLASSIC_8_422:
  226. /* Tell card not to interrupt anymore. */
  227. outb(0x0, brd->iobase + 0x4c);
  228. break;
  229. default:
  230. break;
  231. }
  232. jsm_remove_uart_port(brd);
  233. free_irq(brd->irq, brd);
  234. iounmap(brd->re_map_membase);
  235. /* Free all allocated channels structs */
  236. for (i = 0; i < brd->maxports; i++) {
  237. if (brd->channels[i]) {
  238. kfree(brd->channels[i]->ch_rqueue);
  239. kfree(brd->channels[i]->ch_equeue);
  240. kfree(brd->channels[i]);
  241. }
  242. }
  243. pci_release_regions(pdev);
  244. pci_disable_device(pdev);
  245. kfree(brd);
  246. }
  247. static const struct pci_device_id jsm_pci_tbl[] = {
  248. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
  249. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
  250. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
  251. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
  252. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
  253. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
  254. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_4), 0, 0, 6 },
  255. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_1_422), 0, 0, 7 },
  256. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_1_422_485), 0, 0, 8 },
  257. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2_422_485), 0, 0, 9 },
  258. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_8), 0, 0, 10 },
  259. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4), 0, 0, 11 },
  260. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4RJ45), 0, 0, 12 },
  261. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_8RJ45), 0, 0, 13 },
  262. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_4), 0, 0, 14 },
  263. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_4_422), 0, 0, 15 },
  264. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_8), 0, 0, 16 },
  265. { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_8_422), 0, 0, 17 },
  266. { 0, }
  267. };
  268. MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
  269. static struct pci_driver jsm_driver = {
  270. .name = JSM_DRIVER_NAME,
  271. .id_table = jsm_pci_tbl,
  272. .probe = jsm_probe_one,
  273. .remove = jsm_remove_one,
  274. .err_handler = &jsm_err_handler,
  275. };
  276. static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
  277. pci_channel_state_t state)
  278. {
  279. struct jsm_board *brd = pci_get_drvdata(pdev);
  280. jsm_remove_uart_port(brd);
  281. return PCI_ERS_RESULT_NEED_RESET;
  282. }
  283. static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
  284. {
  285. int rc;
  286. rc = pci_enable_device(pdev);
  287. if (rc)
  288. return PCI_ERS_RESULT_DISCONNECT;
  289. pci_set_master(pdev);
  290. return PCI_ERS_RESULT_RECOVERED;
  291. }
  292. static void jsm_io_resume(struct pci_dev *pdev)
  293. {
  294. struct jsm_board *brd = pci_get_drvdata(pdev);
  295. pci_restore_state(pdev);
  296. pci_save_state(pdev);
  297. jsm_uart_port_init(brd);
  298. }
  299. static int __init jsm_init_module(void)
  300. {
  301. int rc;
  302. rc = uart_register_driver(&jsm_uart_driver);
  303. if (!rc) {
  304. rc = pci_register_driver(&jsm_driver);
  305. if (rc)
  306. uart_unregister_driver(&jsm_uart_driver);
  307. }
  308. return rc;
  309. }
  310. static void __exit jsm_exit_module(void)
  311. {
  312. pci_unregister_driver(&jsm_driver);
  313. uart_unregister_driver(&jsm_uart_driver);
  314. }
  315. module_init(jsm_init_module);
  316. module_exit(jsm_exit_module);