aic7xxx_osm_pci.c revision dedd831081052028f35aaf924ea3d6c55109074f
1/*
2 * Linux driver attachment glue for PCI based controllers.
3 *
4 * Copyright (c) 2000-2001 Adaptec Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions, and the following disclaimer,
12 *    without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 *    substantially similar to the "NO WARRANTY" disclaimer below
15 *    ("Disclaimer") and any redistribution must be conditioned upon
16 *    including a substantially similar Disclaimer requirement for further
17 *    binary redistribution.
18 * 3. Neither the names of the above-listed copyright holders nor the names
19 *    of any contributors may be used to endorse or promote products derived
20 *    from this software without specific prior written permission.
21 *
22 * Alternatively, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2 as published by the Free
24 * Software Foundation.
25 *
26 * NO WARRANTY
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
38 *
39 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c#47 $
40 */
41
42#include "aic7xxx_osm.h"
43#include "aic7xxx_pci.h"
44
45static int	ahc_linux_pci_dev_probe(struct pci_dev *pdev,
46					const struct pci_device_id *ent);
47static int	ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc,
48						u_long *base);
49static int	ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
50						 u_long *bus_addr,
51						 uint8_t __iomem **maddr);
52static void	ahc_linux_pci_dev_remove(struct pci_dev *pdev);
53
54/* Define the macro locally since it's different for different class of chips.
55*/
56#define ID(x)	ID_C(x, PCI_CLASS_STORAGE_SCSI)
57
58static struct pci_device_id ahc_linux_pci_id_table[] = {
59	/* aic7850 based controllers */
60	ID(ID_AHA_2902_04_10_15_20C_30C),
61	/* aic7860 based controllers */
62	ID(ID_AHA_2930CU),
63	ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
64	ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
65	ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
66	ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
67	/* aic7870 based controllers */
68	ID(ID_AHA_2940),
69	ID(ID_AHA_3940),
70	ID(ID_AHA_398X),
71	ID(ID_AHA_2944),
72	ID(ID_AHA_3944),
73	ID(ID_AHA_4944),
74	/* aic7880 based controllers */
75	ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
76	ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
77	ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
78	ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
79	ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
80	ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
81	ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
82	ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
83	ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
84	/* aic7890 based controllers */
85	ID(ID_AHA_2930U2),
86	ID(ID_AHA_2940U2B),
87	ID(ID_AHA_2940U2_OEM),
88	ID(ID_AHA_2940U2),
89	ID(ID_AHA_2950U2B),
90	ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
91	ID(ID_AAA_131U2),
92	/* aic7890 based controllers */
93	ID(ID_AHA_29160),
94	ID(ID_AHA_29160_CPQ),
95	ID(ID_AHA_29160N),
96	ID(ID_AHA_29160C),
97	ID(ID_AHA_29160B),
98	ID(ID_AHA_19160B),
99	ID(ID_AIC7892_ARO),
100	/* aic7892 based controllers */
101	ID(ID_AHA_2940U_DUAL),
102	ID(ID_AHA_3940AU),
103	ID(ID_AHA_3944AU),
104	ID(ID_AIC7895_ARO),
105	ID(ID_AHA_3950U2B_0),
106	ID(ID_AHA_3950U2B_1),
107	ID(ID_AHA_3950U2D_0),
108	ID(ID_AHA_3950U2D_1),
109	ID(ID_AIC7896_ARO),
110	/* aic7899 based controllers */
111	ID(ID_AHA_3960D),
112	ID(ID_AHA_3960D_CPQ),
113	ID(ID_AIC7899_ARO),
114	/* Generic chip probes for devices we don't know exactly. */
115	ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
116	ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
117	ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
118	ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
119	ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
120	ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
121 	ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
122 	ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
123	ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
124	ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
125	ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
126	ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
127	ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
128	{ 0 }
129};
130
131MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
132
133struct pci_driver aic7xxx_pci_driver = {
134	.name		= "aic7xxx",
135	.probe		= ahc_linux_pci_dev_probe,
136	.remove		= ahc_linux_pci_dev_remove,
137	.id_table	= ahc_linux_pci_id_table
138};
139
140static void
141ahc_linux_pci_dev_remove(struct pci_dev *pdev)
142{
143	struct ahc_softc *ahc;
144	u_long l;
145
146	/*
147	 * We should be able to just perform
148	 * the free directly, but check our
149	 * list for extra sanity.
150	 */
151	ahc_list_lock(&l);
152	ahc = ahc_find_softc((struct ahc_softc *)pci_get_drvdata(pdev));
153	if (ahc != NULL) {
154		u_long s;
155
156		TAILQ_REMOVE(&ahc_tailq, ahc, links);
157		ahc_list_unlock(&l);
158		ahc_lock(ahc, &s);
159		ahc_intr_enable(ahc, FALSE);
160		ahc_unlock(ahc, &s);
161		ahc_free(ahc);
162	} else
163		ahc_list_unlock(&l);
164}
165
166static int
167ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
168{
169	char		 buf[80];
170	const uint64_t	 mask_39bit = 0x7FFFFFFFFFULL;
171	struct		 ahc_softc *ahc;
172	ahc_dev_softc_t	 pci;
173	struct		 ahc_pci_identity *entry;
174	char		*name;
175	int		 error;
176
177	/*
178	 * Some BIOSen report the same device multiple times.
179	 */
180	TAILQ_FOREACH(ahc, &ahc_tailq, links) {
181		struct pci_dev *probed_pdev;
182
183		probed_pdev = ahc->dev_softc;
184		if (probed_pdev->bus->number == pdev->bus->number
185		 && probed_pdev->devfn == pdev->devfn)
186			break;
187	}
188	if (ahc != NULL) {
189		/* Skip duplicate. */
190		return (-ENODEV);
191	}
192
193	pci = pdev;
194	entry = ahc_find_pci_device(pci);
195	if (entry == NULL)
196		return (-ENODEV);
197
198	/*
199	 * Allocate a softc for this card and
200	 * set it up for attachment by our
201	 * common detect routine.
202	 */
203	sprintf(buf, "ahc_pci:%d:%d:%d",
204		ahc_get_pci_bus(pci),
205		ahc_get_pci_slot(pci),
206		ahc_get_pci_function(pci));
207	name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
208	if (name == NULL)
209		return (-ENOMEM);
210	strcpy(name, buf);
211	ahc = ahc_alloc(NULL, name);
212	if (ahc == NULL)
213		return (-ENOMEM);
214	if (pci_enable_device(pdev)) {
215		ahc_free(ahc);
216		return (-ENODEV);
217	}
218	pci_set_master(pdev);
219
220	if (sizeof(dma_addr_t) > 4
221	 && ahc_linux_get_memsize() > 0x80000000
222	 && pci_set_dma_mask(pdev, mask_39bit) == 0) {
223		ahc->flags |= AHC_39BIT_ADDRESSING;
224		ahc->platform_data->hw_dma_mask = mask_39bit;
225	} else {
226		if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
227			printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
228                	return (-ENODEV);
229		}
230		ahc->platform_data->hw_dma_mask = DMA_32BIT_MASK;
231	}
232	ahc->dev_softc = pci;
233	error = ahc_pci_config(ahc, entry);
234	if (error != 0) {
235		ahc_free(ahc);
236		return (-error);
237	}
238	pci_set_drvdata(pdev, ahc);
239	if (aic7xxx_detect_complete)
240		ahc_linux_register_host(ahc, &aic7xxx_driver_template);
241	return (0);
242}
243
244int
245ahc_linux_pci_init(void)
246{
247	/* Translate error or zero return into zero or one */
248	return pci_module_init(&aic7xxx_pci_driver) ? 0 : 1;
249}
250
251void
252ahc_linux_pci_exit(void)
253{
254	pci_unregister_driver(&aic7xxx_pci_driver);
255}
256
257static int
258ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, u_long *base)
259{
260	if (aic7xxx_allow_memio == 0)
261		return (ENOMEM);
262
263	*base = pci_resource_start(ahc->dev_softc, 0);
264	if (*base == 0)
265		return (ENOMEM);
266	if (request_region(*base, 256, "aic7xxx") == 0)
267		return (ENOMEM);
268	return (0);
269}
270
271static int
272ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
273				 u_long *bus_addr,
274				 uint8_t __iomem **maddr)
275{
276	u_long	start;
277	int	error;
278
279	error = 0;
280	start = pci_resource_start(ahc->dev_softc, 1);
281	if (start != 0) {
282		*bus_addr = start;
283		if (request_mem_region(start, 0x1000, "aic7xxx") == 0)
284			error = ENOMEM;
285		if (error == 0) {
286			*maddr = ioremap_nocache(start, 256);
287			if (*maddr == NULL) {
288				error = ENOMEM;
289				release_mem_region(start, 0x1000);
290			}
291		}
292	} else
293		error = ENOMEM;
294	return (error);
295}
296
297int
298ahc_pci_map_registers(struct ahc_softc *ahc)
299{
300	uint32_t command;
301	u_long	 base;
302	uint8_t	__iomem *maddr;
303	int	 error;
304
305	/*
306	 * If its allowed, we prefer memory mapped access.
307	 */
308	command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
309	command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
310	base = 0;
311	maddr = NULL;
312	error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
313	if (error == 0) {
314		ahc->platform_data->mem_busaddr = base;
315		ahc->tag = BUS_SPACE_MEMIO;
316		ahc->bsh.maddr = maddr;
317		ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
318				     command | PCIM_CMD_MEMEN, 4);
319
320		/*
321		 * Do a quick test to see if memory mapped
322		 * I/O is functioning correctly.
323		 */
324		if (ahc_pci_test_register_access(ahc) != 0) {
325
326			printf("aic7xxx: PCI Device %d:%d:%d "
327			       "failed memory mapped test.  Using PIO.\n",
328			       ahc_get_pci_bus(ahc->dev_softc),
329			       ahc_get_pci_slot(ahc->dev_softc),
330			       ahc_get_pci_function(ahc->dev_softc));
331			iounmap(maddr);
332			release_mem_region(ahc->platform_data->mem_busaddr,
333					   0x1000);
334			ahc->bsh.maddr = NULL;
335			maddr = NULL;
336		} else
337			command |= PCIM_CMD_MEMEN;
338	} else {
339		printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
340		       "unavailable. Cannot memory map device.\n",
341		       ahc_get_pci_bus(ahc->dev_softc),
342		       ahc_get_pci_slot(ahc->dev_softc),
343		       ahc_get_pci_function(ahc->dev_softc),
344		       base);
345	}
346
347	/*
348	 * We always prefer memory mapped access.
349	 */
350	if (maddr == NULL) {
351
352		error = ahc_linux_pci_reserve_io_region(ahc, &base);
353		if (error == 0) {
354			ahc->tag = BUS_SPACE_PIO;
355			ahc->bsh.ioport = base;
356			command |= PCIM_CMD_PORTEN;
357		} else {
358			printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
359			       "unavailable. Cannot map device.\n",
360			       ahc_get_pci_bus(ahc->dev_softc),
361			       ahc_get_pci_slot(ahc->dev_softc),
362			       ahc_get_pci_function(ahc->dev_softc),
363			       base);
364		}
365	}
366	ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
367	return (error);
368}
369
370int
371ahc_pci_map_int(struct ahc_softc *ahc)
372{
373	int error;
374
375	error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
376			    SA_SHIRQ, "aic7xxx", ahc);
377	if (error == 0)
378		ahc->platform_data->irq = ahc->dev_softc->irq;
379
380	return (-error);
381}
382
383