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