2 * Linux driver attachment glue for PCI based controllers.
4 * Copyright (c) 2000-2001 Adaptec Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
39 * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c#47 $
42 #include "aic7xxx_osm.h"
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
50 static int ahc_linux_pci_dev_probe(struct pci_dev
*pdev
,
51 const struct pci_device_id
*ent
);
52 static int ahc_linux_pci_reserve_io_region(struct ahc_softc
*ahc
,
54 static int ahc_linux_pci_reserve_mem_region(struct ahc_softc
*ahc
,
57 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
58 static void ahc_linux_pci_dev_remove(struct pci_dev
*pdev
);
60 /* We do our own ID filtering. So, grab all SCSI storage class devices. */
61 static struct pci_device_id ahc_linux_pci_id_table
[] = {
63 0x9004, PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
64 PCI_CLASS_STORAGE_SCSI
<< 8, 0xFFFF00, 0
67 0x9005, PCI_ANY_ID
, PCI_ANY_ID
, PCI_ANY_ID
,
68 PCI_CLASS_STORAGE_SCSI
<< 8, 0xFFFF00, 0
73 MODULE_DEVICE_TABLE(pci
, ahc_linux_pci_id_table
);
75 struct pci_driver aic7xxx_pci_driver
= {
77 .probe
= ahc_linux_pci_dev_probe
,
78 .remove
= ahc_linux_pci_dev_remove
,
79 .id_table
= ahc_linux_pci_id_table
83 ahc_linux_pci_dev_remove(struct pci_dev
*pdev
)
85 struct ahc_softc
*ahc
;
89 * We should be able to just perform
90 * the free directly, but check our
91 * list for extra sanity.
94 ahc
= ahc_find_softc((struct ahc_softc
*)pci_get_drvdata(pdev
));
99 ahc_intr_enable(ahc
, FALSE
);
105 #endif /* !LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) */
108 ahc_linux_pci_dev_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
111 bus_addr_t mask_39bit
;
112 struct ahc_softc
*ahc
;
114 struct ahc_pci_identity
*entry
;
119 * Some BIOSen report the same device multiple times.
121 TAILQ_FOREACH(ahc
, &ahc_tailq
, links
) {
122 struct pci_dev
*probed_pdev
;
124 probed_pdev
= ahc
->dev_softc
;
125 if (probed_pdev
->bus
->number
== pdev
->bus
->number
126 && probed_pdev
->devfn
== pdev
->devfn
)
130 /* Skip duplicate. */
135 entry
= ahc_find_pci_device(pci
);
140 * Allocate a softc for this card and
141 * set it up for attachment by our
142 * common detect routine.
144 sprintf(buf
, "ahc_pci:%d:%d:%d",
145 ahc_get_pci_bus(pci
),
146 ahc_get_pci_slot(pci
),
147 ahc_get_pci_function(pci
));
148 name
= malloc(strlen(buf
) + 1, M_DEVBUF
, M_NOWAIT
);
152 ahc
= ahc_alloc(NULL
, name
);
155 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
156 if (pci_enable_device(pdev
)) {
160 pci_set_master(pdev
);
162 mask_39bit
= (bus_addr_t
)0x7FFFFFFFFFULL
;
163 if (sizeof(bus_addr_t
) > 4
164 && ahc_linux_get_memsize() > 0x80000000
165 && ahc_pci_set_dma_mask(pdev
, mask_39bit
) == 0) {
166 ahc
->flags
|= AHC_39BIT_ADDRESSING
;
167 ahc
->platform_data
->hw_dma_mask
= mask_39bit
;
169 if (ahc_pci_set_dma_mask(pdev
, 0xFFFFFFFF)) {
170 printk(KERN_WARNING
"aic7xxx: No suitable DMA available.\n");
173 ahc
->platform_data
->hw_dma_mask
= 0xFFFFFFFF;
176 ahc
->dev_softc
= pci
;
177 error
= ahc_pci_config(ahc
, entry
);
182 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
183 pci_set_drvdata(pdev
, ahc
);
184 if (aic7xxx_detect_complete
) {
185 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
186 ahc_linux_register_host(ahc
, &aic7xxx_driver_template
);
188 printf("aic7xxx: ignoring PCI device found after "
198 ahc_linux_pci_init(void)
200 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
201 /* Translate error or zero return into zero or one */
202 return pci_module_init(&aic7xxx_pci_driver
) ? 0 : 1;
204 struct pci_dev
*pdev
;
208 /* If we don't have a PCI bus, we can't find any adapters. */
209 if (pci_present() == 0)
214 class = PCI_CLASS_STORAGE_SCSI
<< 8;
215 while ((pdev
= pci_find_class(class, pdev
)) != NULL
) {
220 error
= ahc_linux_pci_dev_probe(pdev
, /*pci_devid*/NULL
);
229 ahc_linux_pci_exit(void)
231 pci_unregister_driver(&aic7xxx_pci_driver
);
235 ahc_linux_pci_reserve_io_region(struct ahc_softc
*ahc
, u_long
*base
)
237 if (aic7xxx_allow_memio
== 0)
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
241 *base
= pci_resource_start(ahc
->dev_softc
, 0);
243 *base
= ahc_pci_read_config(ahc
->dev_softc
, PCIR_MAPS
, 4);
244 *base
&= PCI_BASE_ADDRESS_IO_MASK
;
248 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
249 if (check_region(*base
, 256) != 0)
251 request_region(*base
, 256, "aic7xxx");
253 if (request_region(*base
, 256, "aic7xxx") == 0)
260 ahc_linux_pci_reserve_mem_region(struct ahc_softc
*ahc
,
270 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
271 start
= pci_resource_start(ahc
->dev_softc
, 1);
272 base_page
= start
& PAGE_MASK
;
273 base_offset
= start
- base_page
;
275 start
= ahc_pci_read_config(ahc
->dev_softc
, PCIR_MAPS
+4, 4);
276 base_offset
= start
& PCI_BASE_ADDRESS_MEM_MASK
;
277 base_page
= base_offset
& PAGE_MASK
;
278 base_offset
-= base_page
;
282 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
283 if (request_mem_region(start
, 0x1000, "aic7xxx") == 0)
287 *maddr
= ioremap_nocache(base_page
, base_offset
+ 256);
288 if (*maddr
== NULL
) {
290 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
291 release_mem_region(start
, 0x1000);
294 *maddr
+= base_offset
;
302 ahc_pci_map_registers(struct ahc_softc
*ahc
)
310 * If its allowed, we prefer memory mapped access.
312 command
= ahc_pci_read_config(ahc
->dev_softc
, PCIR_COMMAND
, 4);
313 command
&= ~(PCIM_CMD_PORTEN
|PCIM_CMD_MEMEN
);
316 error
= ahc_linux_pci_reserve_mem_region(ahc
, &base
, &maddr
);
318 ahc
->platform_data
->mem_busaddr
= base
;
319 ahc
->tag
= BUS_SPACE_MEMIO
;
320 ahc
->bsh
.maddr
= maddr
;
321 ahc_pci_write_config(ahc
->dev_softc
, PCIR_COMMAND
,
322 command
| PCIM_CMD_MEMEN
, 4);
325 * Do a quick test to see if memory mapped
326 * I/O is functioning correctly.
328 if (ahc_pci_test_register_access(ahc
) != 0) {
330 printf("aic7xxx: PCI Device %d:%d:%d "
331 "failed memory mapped test. Using PIO.\n",
332 ahc_get_pci_bus(ahc
->dev_softc
),
333 ahc_get_pci_slot(ahc
->dev_softc
),
334 ahc_get_pci_function(ahc
->dev_softc
));
335 iounmap((void *)((u_long
)maddr
& PAGE_MASK
));
336 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
337 release_mem_region(ahc
->platform_data
->mem_busaddr
,
340 ahc
->bsh
.maddr
= NULL
;
343 command
|= PCIM_CMD_MEMEN
;
345 printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
346 "unavailable. Cannot memory map device.\n",
347 ahc_get_pci_bus(ahc
->dev_softc
),
348 ahc_get_pci_slot(ahc
->dev_softc
),
349 ahc_get_pci_function(ahc
->dev_softc
),
354 * We always prefer memory mapped access.
358 error
= ahc_linux_pci_reserve_io_region(ahc
, &base
);
360 ahc
->tag
= BUS_SPACE_PIO
;
361 ahc
->bsh
.ioport
= base
;
362 command
|= PCIM_CMD_PORTEN
;
364 printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
365 "unavailable. Cannot map device.\n",
366 ahc_get_pci_bus(ahc
->dev_softc
),
367 ahc_get_pci_slot(ahc
->dev_softc
),
368 ahc_get_pci_function(ahc
->dev_softc
),
372 ahc_pci_write_config(ahc
->dev_softc
, PCIR_COMMAND
, command
, 4);
377 ahc_pci_map_int(struct ahc_softc
*ahc
)
381 error
= request_irq(ahc
->dev_softc
->irq
, ahc_linux_isr
,
382 SA_SHIRQ
, "aic7xxx", ahc
);
384 ahc
->platform_data
->irq
= ahc
->dev_softc
->irq
;
390 ahc_power_state_change(struct ahc_softc
*ahc
, ahc_power_state new_state
)
392 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
393 pci_set_power_state(ahc
->dev_softc
, new_state
);
399 * Traverse the capability list looking for
400 * the power management capability.
403 cap_offset
= ahc_pci_read_config(ahc
->dev_softc
,
404 PCIR_CAP_PTR
, /*bytes*/1);
405 while (cap_offset
!= 0) {
407 cap
= ahc_pci_read_config(ahc
->dev_softc
,
408 cap_offset
, /*bytes*/4);
409 if ((cap
& 0xFF) == 1
410 && ((cap
>> 16) & 0x3) > 0) {
413 pm_control
= ahc_pci_read_config(ahc
->dev_softc
,
417 pm_control
|= new_state
;
418 ahc_pci_write_config(ahc
->dev_softc
,
420 pm_control
, /*bytes*/2);
423 cap_offset
= (cap
>> 8) & 0xFF;