blob: 108c3e0471fd523ab849d7ebc58d702c0b3ac1d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/************************************************************************
2 * Copyright 2003 Digi International (www.digi.com)
3 *
4 * Copyright (C) 2004 IBM Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the 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, write to the Free Software
18 * Foundation, Inc., 59 * Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
20 *
21 * Contact Information:
22 * Scott H Kilau <Scott_Kilau@digi.com>
Adrian Bunk2a08b4e2006-04-01 01:04:20 +020023 * Wendy Xiong <wendyx@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
V. ANANDA KRISHNANc2236952005-07-27 11:43:49 -070025 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 ***********************************************************************/
27#include <linux/moduleparam.h>
28#include <linux/pci.h>
29
30#include "jsm.h"
31
32MODULE_AUTHOR("Digi International, http://www.digi.com");
Christoph Hellwig614a7d62005-04-16 15:25:44 -070033MODULE_DESCRIPTION("Driver for the Digi International "
34 "Neo PCI based product line");
35MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036MODULE_SUPPORTED_DEVICE("jsm");
37
38#define JSM_DRIVER_NAME "jsm"
39#define NR_PORTS 32
40#define JSM_MINOR_START 0
41
42struct uart_driver jsm_uart_driver = {
43 .owner = THIS_MODULE,
44 .driver_name = JSM_DRIVER_NAME,
45 .dev_name = "ttyn",
V. ANANDA KRISHNAN9539c1d2005-07-27 11:43:48 -070046 .major = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 .minor = JSM_MINOR_START,
48 .nr = NR_PORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Breno Leitaoe6bdf242009-10-14 14:57:51 -030051static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
52 pci_channel_state_t state);
53static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
54static void jsm_io_resume(struct pci_dev *pdev);
55
56static struct pci_error_handlers jsm_err_handler = {
57 .error_detected = jsm_io_error_detected,
58 .slot_reset = jsm_io_slot_reset,
59 .resume = jsm_io_resume,
60};
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int jsm_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063module_param(jsm_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Breno Leitaoaacf17a2009-04-06 17:34:17 +010066static int __devinit jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct jsm_board *brd;
Christoph Hellwig614a7d62005-04-16 15:25:44 -070070 static int adapter_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Christoph Hellwig614a7d62005-04-16 15:25:44 -070072 rc = pci_enable_device(pdev);
73 if (rc) {
74 dev_err(&pdev->dev, "Device enable FAILED\n");
75 goto out;
76 }
77
78 rc = pci_request_regions(pdev, "jsm");
79 if (rc) {
80 dev_err(&pdev->dev, "pci_request_region FAILED\n");
81 goto out_disable_device;
82 }
83
Burman Yan8f31bb32007-02-14 00:33:07 -080084 brd = kzalloc(sizeof(struct jsm_board), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (!brd) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -070086 dev_err(&pdev->dev,
87 "memory allocation for board structure failed\n");
88 rc = -ENOMEM;
89 goto out_release_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* store the info for the board we've found */
Christoph Hellwig614a7d62005-04-16 15:25:44 -070093 brd->boardnum = adapter_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 brd->pci_dev = pdev;
Scott Kilau99da9042008-05-01 04:35:00 -070095 if (pdev->device == PCIE_DEVICE_ID_NEO_4_IBM)
96 brd->maxports = 4;
Adam Lackorzynskiffa75252009-02-18 14:48:34 -080097 else if (pdev->device == PCI_DEVICE_ID_DIGI_NEO_8)
98 brd->maxports = 8;
Scott Kilau99da9042008-05-01 04:35:00 -070099 else
100 brd->maxports = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spin_lock_init(&brd->bd_intr_lock);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* store which revision we have */
Auke Kok44c10132007-06-08 15:46:36 -0700105 brd->rev = pdev->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 brd->irq = pdev->irq;
108
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700109 jsm_printk(INIT, INFO, &brd->pci_dev,
110 "jsm_found_board - NEO adapter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700112 /* get the PCI Base Address Registers */
113 brd->membase = pci_resource_start(pdev, 0);
114 brd->membase_end = pci_resource_end(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700116 if (brd->membase & 1)
117 brd->membase &= ~3;
118 else
119 brd->membase &= ~15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700121 /* Assign the board_ops struct */
122 brd->bd_ops = &jsm_neo_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700124 brd->bd_uart_offset = 0x200;
125 brd->bd_dividend = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700127 brd->re_map_membase = ioremap(brd->membase, 0x1000);
128 if (!brd->re_map_membase) {
129 dev_err(&pdev->dev,
130 "card has no PCI Memory resources, "
131 "failing board.\n");
132 rc = -ENOMEM;
133 goto out_kfree_brd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700136 rc = request_irq(brd->irq, brd->bd_ops->intr,
Breno Leitãoa88a4772009-09-24 16:58:22 -0300137 IRQF_SHARED, "JSM", brd);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700138 if (rc) {
139 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
140 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
143 rc = jsm_tty_init(brd);
144 if (rc < 0) {
145 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
Breno Leitaoe713abe2009-04-06 17:34:27 +0100146 rc = -ENXIO;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700147 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
150 rc = jsm_uart_port_init(brd);
151 if (rc < 0) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700152 /* XXX: leaking all resources from jsm_tty_init here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
Breno Leitaoe713abe2009-04-06 17:34:27 +0100154 rc = -ENXIO;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700155 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* Log the information about the board */
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700159 dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
160 adapter_count, brd->rev, brd->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /*
163 * allocate flip buffer for board.
164 *
165 * Okay to malloc with GFP_KERNEL, we are not at interrupt
166 * context, and there are no locks held.
167 */
Burman Yan8f31bb32007-02-14 00:33:07 -0800168 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (!brd->flipbuf) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700170 /* XXX: leaking all resources from jsm_tty_init and
171 jsm_uart_port_init here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
Breno Leitaoe713abe2009-04-06 17:34:27 +0100173 rc = -ENOMEM;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700174 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700177 pci_set_drvdata(pdev, brd);
Breno Leitaoe6bdf242009-10-14 14:57:51 -0300178 pci_save_state(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700181 out_free_irq:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 free_irq(brd->irq, brd);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700183 out_iounmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 iounmap(brd->re_map_membase);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700185 out_kfree_brd:
186 kfree(brd);
187 out_release_regions:
188 pci_release_regions(pdev);
189 out_disable_device:
190 pci_disable_device(pdev);
191 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return rc;
193}
194
Uwe Kleine-Könige9fed562009-01-27 11:51:06 +0000195static void __devexit jsm_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700197 struct jsm_board *brd = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int i = 0;
199
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700200 jsm_remove_uart_port(brd);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 free_irq(brd->irq, brd);
203 iounmap(brd->re_map_membase);
204
205 /* Free all allocated channels structs */
206 for (i = 0; i < brd->maxports; i++) {
207 if (brd->channels[i]) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700208 kfree(brd->channels[i]->ch_rqueue);
209 kfree(brd->channels[i]->ch_equeue);
210 kfree(brd->channels[i]->ch_wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 kfree(brd->channels[i]);
212 }
213 }
214
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700215 pci_release_regions(pdev);
216 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 kfree(brd->flipbuf);
218 kfree(brd);
219}
220
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700221static struct pci_device_id jsm_pci_tbl[] = {
222 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
223 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
224 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
225 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
Scott Kilau99da9042008-05-01 04:35:00 -0700226 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
Adam Lackorzynskiffa75252009-02-18 14:48:34 -0800227 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700228 { 0, }
229};
230MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700232static struct pci_driver jsm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .name = "jsm",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .id_table = jsm_pci_tbl,
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700235 .probe = jsm_probe_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .remove = __devexit_p(jsm_remove_one),
Breno Leitaoe6bdf242009-10-14 14:57:51 -0300237 .err_handler = &jsm_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Breno Leitaoe6bdf242009-10-14 14:57:51 -0300240static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
241 pci_channel_state_t state)
242{
243 struct jsm_board *brd = pci_get_drvdata(pdev);
244
245 jsm_remove_uart_port(brd);
246
247 return PCI_ERS_RESULT_NEED_RESET;
248}
249
250static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
251{
252 int rc;
253
254 rc = pci_enable_device(pdev);
255
256 if (rc)
257 return PCI_ERS_RESULT_DISCONNECT;
258
259 pci_set_master(pdev);
260
261 return PCI_ERS_RESULT_RECOVERED;
262}
263
264static void jsm_io_resume(struct pci_dev *pdev)
265{
266 struct jsm_board *brd = pci_get_drvdata(pdev);
267
268 pci_restore_state(pdev);
269
270 jsm_uart_port_init(brd);
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static int __init jsm_init_module(void)
274{
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700275 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 rc = uart_register_driver(&jsm_uart_driver);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700278 if (!rc) {
279 rc = pci_register_driver(&jsm_driver);
280 if (rc)
281 uart_unregister_driver(&jsm_uart_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return rc;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static void __exit jsm_exit_module(void)
287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 pci_unregister_driver(&jsm_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 uart_unregister_driver(&jsm_uart_driver);
290}
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700291
292module_init(jsm_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293module_exit(jsm_exit_module);