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"
43 #include "aic7xxx_pci.h"
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
51 static int ahc_linux_pci_dev_probe(struct pci_dev *pdev,
52 const struct pci_device_id *ent);
53 static int ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc,
55 static int ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
57 uint8_t __iomem **maddr);
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
59 static void ahc_linux_pci_dev_remove(struct pci_dev *pdev);
61 /* Define the macro locally since it's different for different class of chips.
63 #define ID(x) ID_C(x, PCI_CLASS_STORAGE_SCSI)
65 static struct pci_device_id ahc_linux_pci_id_table[] = {
66 /* aic7850 based controllers */
67 ID(ID_AHA_2902_04_10_15_20C_30C),
68 /* aic7860 based controllers */
70 ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
71 ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
72 ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
73 ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
74 /* aic7870 based controllers */
81 /* aic7880 based controllers */
82 ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
83 ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
84 ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
85 ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
86 ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
87 ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
88 ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
89 ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
90 ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
91 /* aic7890 based controllers */
94 ID(ID_AHA_2940U2_OEM),
97 ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
99 /* aic7890 based controllers */
101 ID(ID_AHA_29160_CPQ),
107 /* aic7892 based controllers */
108 ID(ID_AHA_2940U_DUAL),
112 ID(ID_AHA_3950U2B_0),
113 ID(ID_AHA_3950U2B_1),
114 ID(ID_AHA_3950U2D_0),
115 ID(ID_AHA_3950U2D_1),
117 /* aic7899 based controllers */
119 ID(ID_AHA_3960D_CPQ),
121 /* Generic chip probes for devices we don't know exactly. */
122 ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
123 ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
124 ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
125 ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
126 ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
127 ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
128 ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
129 ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
130 ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
131 ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
132 ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
133 ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
134 ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
138 MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
140 struct pci_driver aic7xxx_pci_driver = {
142 .probe = ahc_linux_pci_dev_probe,
143 .remove = ahc_linux_pci_dev_remove,
144 .id_table = ahc_linux_pci_id_table
148 ahc_linux_pci_dev_remove(struct pci_dev *pdev)
150 struct ahc_softc *ahc;
154 * We should be able to just perform
155 * the free directly, but check our
156 * list for extra sanity.
159 ahc = ahc_find_softc((struct ahc_softc *)pci_get_drvdata(pdev));
163 TAILQ_REMOVE(&ahc_tailq, ahc, links);
166 ahc_intr_enable(ahc, FALSE);
172 #endif /* !LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) */
175 ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
178 const uint64_t mask_39bit = 0x7FFFFFFFFFULL;
179 struct ahc_softc *ahc;
181 struct ahc_pci_identity *entry;
186 * Some BIOSen report the same device multiple times.
188 TAILQ_FOREACH(ahc, &ahc_tailq, links) {
189 struct pci_dev *probed_pdev;
191 probed_pdev = ahc->dev_softc;
192 if (probed_pdev->bus->number == pdev->bus->number
193 && probed_pdev->devfn == pdev->devfn)
197 /* Skip duplicate. */
202 entry = ahc_find_pci_device(pci);
207 * Allocate a softc for this card and
208 * set it up for attachment by our
209 * common detect routine.
211 sprintf(buf, "ahc_pci:%d:%d:%d",
212 ahc_get_pci_bus(pci),
213 ahc_get_pci_slot(pci),
214 ahc_get_pci_function(pci));
215 name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
219 ahc = ahc_alloc(NULL, name);
222 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
223 if (pci_enable_device(pdev)) {
227 pci_set_master(pdev);
229 if (sizeof(dma_addr_t) > 4
230 && ahc_linux_get_memsize() > 0x80000000
231 && pci_set_dma_mask(pdev, mask_39bit) == 0) {
232 ahc->flags |= AHC_39BIT_ADDRESSING;
233 ahc->platform_data->hw_dma_mask = mask_39bit;
235 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
236 printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
239 ahc->platform_data->hw_dma_mask = DMA_32BIT_MASK;
242 ahc->dev_softc = pci;
243 error = ahc_pci_config(ahc, entry);
248 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
249 pci_set_drvdata(pdev, ahc);
250 if (aic7xxx_detect_complete) {
251 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
252 ahc_linux_register_host(ahc, &aic7xxx_driver_template);
254 printf("aic7xxx: ignoring PCI device found after "
264 ahc_linux_pci_init(void)
266 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
267 /* Translate error or zero return into zero or one */
268 return pci_module_init(&aic7xxx_pci_driver) ? 0 : 1;
270 struct pci_dev *pdev;
274 /* If we don't have a PCI bus, we can't find any adapters. */
275 if (pci_present() == 0)
280 class = PCI_CLASS_STORAGE_SCSI << 8;
281 while ((pdev = pci_find_class(class, pdev)) != NULL) {
286 error = ahc_linux_pci_dev_probe(pdev, /*pci_devid*/NULL);
295 ahc_linux_pci_exit(void)
297 pci_unregister_driver(&aic7xxx_pci_driver);
301 ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, u_long *base)
303 if (aic7xxx_allow_memio == 0)
306 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
307 *base = pci_resource_start(ahc->dev_softc, 0);
309 *base = ahc_pci_read_config(ahc->dev_softc, PCIR_MAPS, 4);
310 *base &= PCI_BASE_ADDRESS_IO_MASK;
314 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
315 if (check_region(*base, 256) != 0)
317 request_region(*base, 256, "aic7xxx");
319 if (request_region(*base, 256, "aic7xxx") == 0)
326 ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
328 uint8_t __iomem **maddr)
334 start = pci_resource_start(ahc->dev_softc, 1);
337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
338 if (request_mem_region(start, 0x1000, "aic7xxx") == 0)
342 *maddr = ioremap_nocache(start, 256);
343 if (*maddr == NULL) {
345 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
346 release_mem_region(start, 0x1000);
356 ahc_pci_map_registers(struct ahc_softc *ahc)
360 uint8_t __iomem *maddr;
364 * If its allowed, we prefer memory mapped access.
366 command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
367 command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
370 error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
372 ahc->platform_data->mem_busaddr = base;
373 ahc->tag = BUS_SPACE_MEMIO;
374 ahc->bsh.maddr = maddr;
375 ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
376 command | PCIM_CMD_MEMEN, 4);
379 * Do a quick test to see if memory mapped
380 * I/O is functioning correctly.
382 if (ahc_pci_test_register_access(ahc) != 0) {
384 printf("aic7xxx: PCI Device %d:%d:%d "
385 "failed memory mapped test. Using PIO.\n",
386 ahc_get_pci_bus(ahc->dev_softc),
387 ahc_get_pci_slot(ahc->dev_softc),
388 ahc_get_pci_function(ahc->dev_softc));
390 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
391 release_mem_region(ahc->platform_data->mem_busaddr,
394 ahc->bsh.maddr = NULL;
397 command |= PCIM_CMD_MEMEN;
399 printf("aic7xxx: PCI%d:%d:%d MEM region 0x%lx "
400 "unavailable. Cannot memory map device.\n",
401 ahc_get_pci_bus(ahc->dev_softc),
402 ahc_get_pci_slot(ahc->dev_softc),
403 ahc_get_pci_function(ahc->dev_softc),
408 * We always prefer memory mapped access.
412 error = ahc_linux_pci_reserve_io_region(ahc, &base);
414 ahc->tag = BUS_SPACE_PIO;
415 ahc->bsh.ioport = base;
416 command |= PCIM_CMD_PORTEN;
418 printf("aic7xxx: PCI%d:%d:%d IO region 0x%lx[0..255] "
419 "unavailable. Cannot map device.\n",
420 ahc_get_pci_bus(ahc->dev_softc),
421 ahc_get_pci_slot(ahc->dev_softc),
422 ahc_get_pci_function(ahc->dev_softc),
426 ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
431 ahc_pci_map_int(struct ahc_softc *ahc)
435 error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
436 SA_SHIRQ, "aic7xxx", ahc);
438 ahc->platform_data->irq = ahc->dev_softc->irq;
444 ahc_power_state_change(struct ahc_softc *ahc, ahc_power_state new_state)
446 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
447 pci_set_power_state(ahc->dev_softc, new_state);
453 * Traverse the capability list looking for
454 * the power management capability.
457 cap_offset = ahc_pci_read_config(ahc->dev_softc,
458 PCIR_CAP_PTR, /*bytes*/1);
459 while (cap_offset != 0) {
461 cap = ahc_pci_read_config(ahc->dev_softc,
462 cap_offset, /*bytes*/4);
463 if ((cap & 0xFF) == 1
464 && ((cap >> 16) & 0x3) > 0) {
467 pm_control = ahc_pci_read_config(ahc->dev_softc,
471 pm_control |= new_state;
472 ahc_pci_write_config(ahc->dev_softc,
474 pm_control, /*bytes*/2);
477 cap_offset = (cap >> 8) & 0xFF;