blob: eaf545014119cda24fe4a115e5b3c3a7ffe9b9e4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "jsm.h"
32
33MODULE_AUTHOR("Digi International, http://www.digi.com");
Christoph Hellwig614a7d62005-04-16 15:25:44 -070034MODULE_DESCRIPTION("Driver for the Digi International "
35 "Neo PCI based product line");
36MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_SUPPORTED_DEVICE("jsm");
38
39#define JSM_DRIVER_NAME "jsm"
40#define NR_PORTS 32
41#define JSM_MINOR_START 0
42
43struct uart_driver jsm_uart_driver = {
44 .owner = THIS_MODULE,
45 .driver_name = JSM_DRIVER_NAME,
46 .dev_name = "ttyn",
V. ANANDA KRISHNAN9539c1d2005-07-27 11:43:48 -070047 .major = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .minor = JSM_MINOR_START,
49 .nr = NR_PORTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Breno Leitaoe6bdf242009-10-14 14:57:51 -030052static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
53 pci_channel_state_t state);
54static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev);
55static void jsm_io_resume(struct pci_dev *pdev);
56
57static struct pci_error_handlers jsm_err_handler = {
58 .error_detected = jsm_io_error_detected,
59 .slot_reset = jsm_io_slot_reset,
60 .resume = jsm_io_resume,
61};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063int jsm_debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064module_param(jsm_debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_PARM_DESC(jsm_debug, "Driver debugging level");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Breno Leitaoaacf17a2009-04-06 17:34:17 +010067static int __devinit jsm_probe_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct jsm_board *brd;
Christoph Hellwig614a7d62005-04-16 15:25:44 -070071 static int adapter_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Christoph Hellwig614a7d62005-04-16 15:25:44 -070073 rc = pci_enable_device(pdev);
74 if (rc) {
75 dev_err(&pdev->dev, "Device enable FAILED\n");
76 goto out;
77 }
78
79 rc = pci_request_regions(pdev, "jsm");
80 if (rc) {
81 dev_err(&pdev->dev, "pci_request_region FAILED\n");
82 goto out_disable_device;
83 }
84
Burman Yan8f31bb32007-02-14 00:33:07 -080085 brd = kzalloc(sizeof(struct jsm_board), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (!brd) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -070087 dev_err(&pdev->dev,
88 "memory allocation for board structure failed\n");
89 rc = -ENOMEM;
90 goto out_release_regions;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* store the info for the board we've found */
Christoph Hellwig614a7d62005-04-16 15:25:44 -070094 brd->boardnum = adapter_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 brd->pci_dev = pdev;
Scott Kilau99da9042008-05-01 04:35:00 -070096 if (pdev->device == PCIE_DEVICE_ID_NEO_4_IBM)
97 brd->maxports = 4;
Adam Lackorzynskiffa75252009-02-18 14:48:34 -080098 else if (pdev->device == PCI_DEVICE_ID_DIGI_NEO_8)
99 brd->maxports = 8;
Scott Kilau99da9042008-05-01 04:35:00 -0700100 else
101 brd->maxports = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 spin_lock_init(&brd->bd_intr_lock);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* store which revision we have */
Auke Kok44c10132007-06-08 15:46:36 -0700106 brd->rev = pdev->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 brd->irq = pdev->irq;
109
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700110 jsm_printk(INIT, INFO, &brd->pci_dev,
111 "jsm_found_board - NEO adapter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700113 /* get the PCI Base Address Registers */
114 brd->membase = pci_resource_start(pdev, 0);
115 brd->membase_end = pci_resource_end(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700117 if (brd->membase & 1)
118 brd->membase &= ~3;
119 else
120 brd->membase &= ~15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700122 /* Assign the board_ops struct */
123 brd->bd_ops = &jsm_neo_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700125 brd->bd_uart_offset = 0x200;
126 brd->bd_dividend = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700128 brd->re_map_membase = ioremap(brd->membase, 0x1000);
129 if (!brd->re_map_membase) {
130 dev_err(&pdev->dev,
131 "card has no PCI Memory resources, "
132 "failing board.\n");
133 rc = -ENOMEM;
134 goto out_kfree_brd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700137 rc = request_irq(brd->irq, brd->bd_ops->intr,
Breno Leitãoa88a4772009-09-24 16:58:22 -0300138 IRQF_SHARED, "JSM", brd);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700139 if (rc) {
140 printk(KERN_WARNING "Failed to hook IRQ %d\n",brd->irq);
141 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
144 rc = jsm_tty_init(brd);
145 if (rc < 0) {
146 dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
Breno Leitaoe713abe2009-04-06 17:34:27 +0100147 rc = -ENXIO;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700148 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
151 rc = jsm_uart_port_init(brd);
152 if (rc < 0) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700153 /* XXX: leaking all resources from jsm_tty_init here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
Breno Leitaoe713abe2009-04-06 17:34:27 +0100155 rc = -ENXIO;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700156 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* Log the information about the board */
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700160 dev_info(&pdev->dev, "board %d: Digi Neo (rev %d), irq %d\n",
161 adapter_count, brd->rev, brd->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /*
164 * allocate flip buffer for board.
165 *
166 * Okay to malloc with GFP_KERNEL, we are not at interrupt
167 * context, and there are no locks held.
168 */
Burman Yan8f31bb32007-02-14 00:33:07 -0800169 brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!brd->flipbuf) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700171 /* XXX: leaking all resources from jsm_tty_init and
172 jsm_uart_port_init here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 dev_err(&pdev->dev, "memory allocation for flipbuf failed\n");
Breno Leitaoe713abe2009-04-06 17:34:27 +0100174 rc = -ENOMEM;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700175 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700178 pci_set_drvdata(pdev, brd);
Breno Leitaoe6bdf242009-10-14 14:57:51 -0300179 pci_save_state(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700182 out_free_irq:
Breno Leitao54862ee2010-02-25 15:31:49 -0300183 jsm_remove_uart_port(brd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 free_irq(brd->irq, brd);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700185 out_iounmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 iounmap(brd->re_map_membase);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700187 out_kfree_brd:
188 kfree(brd);
189 out_release_regions:
190 pci_release_regions(pdev);
191 out_disable_device:
192 pci_disable_device(pdev);
193 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return rc;
195}
196
Uwe Kleine-Könige9fed562009-01-27 11:51:06 +0000197static void __devexit jsm_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700199 struct jsm_board *brd = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int i = 0;
201
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700202 jsm_remove_uart_port(brd);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 free_irq(brd->irq, brd);
205 iounmap(brd->re_map_membase);
206
207 /* Free all allocated channels structs */
208 for (i = 0; i < brd->maxports; i++) {
209 if (brd->channels[i]) {
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700210 kfree(brd->channels[i]->ch_rqueue);
211 kfree(brd->channels[i]->ch_equeue);
212 kfree(brd->channels[i]->ch_wqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 kfree(brd->channels[i]);
214 }
215 }
216
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700217 pci_release_regions(pdev);
218 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 kfree(brd->flipbuf);
220 kfree(brd);
221}
222
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700223static struct pci_device_id jsm_pci_tbl[] = {
224 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
225 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
226 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
227 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
Scott Kilau99da9042008-05-01 04:35:00 -0700228 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
Adam Lackorzynskiffa75252009-02-18 14:48:34 -0800229 { PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700230 { 0, }
231};
232MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700234static struct pci_driver jsm_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .name = "jsm",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .id_table = jsm_pci_tbl,
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700237 .probe = jsm_probe_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .remove = __devexit_p(jsm_remove_one),
Breno Leitaoe6bdf242009-10-14 14:57:51 -0300239 .err_handler = &jsm_err_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
Breno Leitaoe6bdf242009-10-14 14:57:51 -0300242static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
243 pci_channel_state_t state)
244{
245 struct jsm_board *brd = pci_get_drvdata(pdev);
246
247 jsm_remove_uart_port(brd);
248
249 return PCI_ERS_RESULT_NEED_RESET;
250}
251
252static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
253{
254 int rc;
255
256 rc = pci_enable_device(pdev);
257
258 if (rc)
259 return PCI_ERS_RESULT_DISCONNECT;
260
261 pci_set_master(pdev);
262
263 return PCI_ERS_RESULT_RECOVERED;
264}
265
266static void jsm_io_resume(struct pci_dev *pdev)
267{
268 struct jsm_board *brd = pci_get_drvdata(pdev);
269
270 pci_restore_state(pdev);
271
272 jsm_uart_port_init(brd);
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int __init jsm_init_module(void)
276{
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700277 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 rc = uart_register_driver(&jsm_uart_driver);
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700280 if (!rc) {
281 rc = pci_register_driver(&jsm_driver);
282 if (rc)
283 uart_unregister_driver(&jsm_uart_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return rc;
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static void __exit jsm_exit_module(void)
289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 pci_unregister_driver(&jsm_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 uart_unregister_driver(&jsm_uart_driver);
292}
Christoph Hellwig614a7d62005-04-16 15:25:44 -0700293
294module_init(jsm_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295module_exit(jsm_exit_module);