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