• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, https://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 
jsm_probe_one(struct pci_dev * pdev,const struct pci_device_id * ent)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 		rc = -ENXIO;
216 		goto out_kfree_brd;
217 	}
218 
219 	rc = request_irq(brd->irq, brd->bd_ops->intr, IRQF_SHARED, "JSM", brd);
220 	if (rc) {
221 		dev_warn(&pdev->dev, "Failed to hook IRQ %d\n", brd->irq);
222 		goto out_iounmap;
223 	}
224 
225 	rc = jsm_tty_init(brd);
226 	if (rc < 0) {
227 		dev_err(&pdev->dev, "Can't init tty devices (%d)\n", rc);
228 		rc = -ENXIO;
229 		goto out_free_irq;
230 	}
231 
232 	rc = jsm_uart_port_init(brd);
233 	if (rc < 0) {
234 		/* XXX: leaking all resources from jsm_tty_init here! */
235 		dev_err(&pdev->dev, "Can't init uart port (%d)\n", rc);
236 		rc = -ENXIO;
237 		goto out_free_irq;
238 	}
239 
240 	/* Log the information about the board */
241 	dev_info(&pdev->dev, "board %d: Digi Classic/Neo (rev %d), irq %d\n",
242 			adapter_count, brd->rev, brd->irq);
243 
244 	pci_set_drvdata(pdev, brd);
245 	pci_save_state(pdev);
246 
247 	return 0;
248  out_free_irq:
249 	jsm_remove_uart_port(brd);
250 	free_irq(brd->irq, brd);
251  out_iounmap:
252 	iounmap(brd->re_map_membase);
253  out_kfree_brd:
254 	kfree(brd);
255  out_release_regions:
256 	pci_release_regions(pdev);
257  out_disable_device:
258 	pci_disable_device(pdev);
259  out:
260 	return rc;
261 }
262 
jsm_remove_one(struct pci_dev * pdev)263 static void jsm_remove_one(struct pci_dev *pdev)
264 {
265 	struct jsm_board *brd = pci_get_drvdata(pdev);
266 	int i = 0;
267 
268 	switch (pdev->device) {
269 	case PCI_DEVICE_ID_CLASSIC_4:
270 	case PCI_DEVICE_ID_CLASSIC_4_422:
271 	case PCI_DEVICE_ID_CLASSIC_8:
272 	case PCI_DEVICE_ID_CLASSIC_8_422:
273 		/* Tell card not to interrupt anymore. */
274 		outb(0x0, brd->iobase + 0x4c);
275 		break;
276 	default:
277 		break;
278 	}
279 
280 	jsm_remove_uart_port(brd);
281 
282 	free_irq(brd->irq, brd);
283 	iounmap(brd->re_map_membase);
284 
285 	/* Free all allocated channels structs */
286 	for (i = 0; i < brd->maxports; i++) {
287 		if (brd->channels[i]) {
288 			kfree(brd->channels[i]->ch_rqueue);
289 			kfree(brd->channels[i]->ch_equeue);
290 			kfree(brd->channels[i]);
291 		}
292 	}
293 
294 	pci_release_regions(pdev);
295 	pci_disable_device(pdev);
296 	kfree(brd);
297 }
298 
299 static const struct pci_device_id jsm_pci_tbl[] = {
300 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9), 0, 0, 0 },
301 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2DB9PRI), 0, 0, 1 },
302 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45), 0, 0, 2 },
303 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2RJ45PRI), 0, 0, 3 },
304 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4_IBM), 0, 0, 4 },
305 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_DIGI_NEO_8), 0, 0, 5 },
306 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_4), 0, 0, 6 },
307 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_1_422), 0, 0, 7 },
308 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_1_422_485), 0, 0, 8 },
309 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_NEO_2_422_485), 0, 0, 9 },
310 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_8), 0, 0, 10 },
311 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4), 0, 0, 11 },
312 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_4RJ45), 0, 0, 12 },
313 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCIE_DEVICE_ID_NEO_8RJ45), 0, 0, 13 },
314 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_4), 0, 0, 14 },
315 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_4_422), 0, 0, 15 },
316 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_8), 0, 0, 16 },
317 	{ PCI_DEVICE(PCI_VENDOR_ID_DIGI, PCI_DEVICE_ID_CLASSIC_8_422), 0, 0, 17 },
318 	{ 0, }
319 };
320 MODULE_DEVICE_TABLE(pci, jsm_pci_tbl);
321 
322 static struct pci_driver jsm_driver = {
323 	.name		= JSM_DRIVER_NAME,
324 	.id_table	= jsm_pci_tbl,
325 	.probe		= jsm_probe_one,
326 	.remove		= jsm_remove_one,
327 	.err_handler    = &jsm_err_handler,
328 };
329 
jsm_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)330 static pci_ers_result_t jsm_io_error_detected(struct pci_dev *pdev,
331 					pci_channel_state_t state)
332 {
333 	struct jsm_board *brd = pci_get_drvdata(pdev);
334 
335 	jsm_remove_uart_port(brd);
336 
337 	return PCI_ERS_RESULT_NEED_RESET;
338 }
339 
jsm_io_slot_reset(struct pci_dev * pdev)340 static pci_ers_result_t jsm_io_slot_reset(struct pci_dev *pdev)
341 {
342 	int rc;
343 
344 	rc = pci_enable_device(pdev);
345 
346 	if (rc)
347 		return PCI_ERS_RESULT_DISCONNECT;
348 
349 	pci_set_master(pdev);
350 
351 	return PCI_ERS_RESULT_RECOVERED;
352 }
353 
jsm_io_resume(struct pci_dev * pdev)354 static void jsm_io_resume(struct pci_dev *pdev)
355 {
356 	struct jsm_board *brd = pci_get_drvdata(pdev);
357 
358 	pci_restore_state(pdev);
359 	pci_save_state(pdev);
360 
361 	jsm_uart_port_init(brd);
362 }
363 
jsm_init_module(void)364 static int __init jsm_init_module(void)
365 {
366 	int rc;
367 
368 	rc = uart_register_driver(&jsm_uart_driver);
369 	if (!rc) {
370 		rc = pci_register_driver(&jsm_driver);
371 		if (rc)
372 			uart_unregister_driver(&jsm_uart_driver);
373 	}
374 	return rc;
375 }
376 
jsm_exit_module(void)377 static void __exit jsm_exit_module(void)
378 {
379 	pci_unregister_driver(&jsm_driver);
380 	uart_unregister_driver(&jsm_uart_driver);
381 }
382 
383 module_init(jsm_init_module);
384 module_exit(jsm_exit_module);
385