2 * Linux driver attachment glue for PCI based U320 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/aic79xx_osm_pci.c#25 $
42 #include "aic79xx_osm.h"
43 #include "aic79xx_inline.h"
44 #include "aic79xx_pci.h"
46 /* Define the macro locally since it's different for different class of chips.
52 static const struct pci_device_id ahd_linux_pci_id_table[] = {
53 /* aic7901 based controllers */
57 /* aic7902 based controllers */
67 ID(ID_AHA_39320D_B_HP),
68 /* Generic chip probes for devices we don't know exactly. */
69 ID16(ID_AIC7901 & ID_9005_GENERIC_MASK),
70 ID(ID_AIC7901A & ID_DEV_VENDOR_MASK),
71 ID16(ID_AIC7902 & ID_9005_GENERIC_MASK),
75 MODULE_DEVICE_TABLE(pci, ahd_linux_pci_id_table);
79 ahd_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg)
81 struct ahd_softc *ahd = pci_get_drvdata(pdev);
84 if ((rc = ahd_suspend(ahd)))
90 pci_disable_device(pdev);
92 if (mesg.event & PM_EVENT_SLEEP)
93 pci_set_power_state(pdev, PCI_D3hot);
99 ahd_linux_pci_dev_resume(struct pci_dev *pdev)
101 struct ahd_softc *ahd = pci_get_drvdata(pdev);
104 pci_set_power_state(pdev, PCI_D0);
105 pci_restore_state(pdev);
107 if ((rc = pci_enable_device(pdev))) {
108 dev_printk(KERN_ERR, &pdev->dev,
109 "failed to enable device after resume (%d)\n", rc);
113 pci_set_master(pdev);
124 ahd_linux_pci_dev_remove(struct pci_dev *pdev)
126 struct ahd_softc *ahd = pci_get_drvdata(pdev);
129 if (ahd->platform_data && ahd->platform_data->host)
130 scsi_remove_host(ahd->platform_data->host);
133 ahd_intr_enable(ahd, FALSE);
139 ahd_linux_pci_inherit_flags(struct ahd_softc *ahd)
141 struct pci_dev *pdev = ahd->dev_softc, *master_pdev;
142 unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
144 master_pdev = pci_get_slot(pdev->bus, master_devfn);
146 struct ahd_softc *master = pci_get_drvdata(master_pdev);
148 ahd->flags &= ~AHD_BIOS_ENABLED;
149 ahd->flags |= master->flags & AHD_BIOS_ENABLED;
151 printk(KERN_ERR "aic79xx: no multichannel peer found!\n");
152 pci_dev_put(master_pdev);
157 ahd_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
160 struct ahd_softc *ahd;
162 const struct ahd_pci_identity *entry;
165 struct device *dev = &pdev->dev;
168 entry = ahd_find_pci_device(pci);
173 * Allocate a softc for this card and
174 * set it up for attachment by our
175 * common detect routine.
177 sprintf(buf, "ahd_pci:%d:%d:%d",
178 ahd_get_pci_bus(pci),
179 ahd_get_pci_slot(pci),
180 ahd_get_pci_function(pci));
181 name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT);
185 ahd = ahd_alloc(NULL, name);
188 if (pci_enable_device(pdev)) {
192 pci_set_master(pdev);
194 if (sizeof(dma_addr_t) > 4) {
195 const u64 required_mask = dma_get_required_mask(dev);
197 if (required_mask > DMA_BIT_MASK(39) &&
198 dma_set_mask(dev, DMA_BIT_MASK(64)) == 0)
199 ahd->flags |= AHD_64BIT_ADDRESSING;
200 else if (required_mask > DMA_BIT_MASK(32) &&
201 dma_set_mask(dev, DMA_BIT_MASK(39)) == 0)
202 ahd->flags |= AHD_39BIT_ADDRESSING;
204 dma_set_mask(dev, DMA_BIT_MASK(32));
206 dma_set_mask(dev, DMA_BIT_MASK(32));
208 ahd->dev_softc = pci;
209 error = ahd_pci_config(ahd, entry);
216 * Second Function PCI devices need to inherit some
217 * * settings from function 0.
219 if ((ahd->features & AHD_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
220 ahd_linux_pci_inherit_flags(ahd);
222 pci_set_drvdata(pdev, ahd);
224 ahd_linux_register_host(ahd, &aic79xx_driver_template);
228 static struct pci_driver aic79xx_pci_driver = {
230 .probe = ahd_linux_pci_dev_probe,
232 .suspend = ahd_linux_pci_dev_suspend,
233 .resume = ahd_linux_pci_dev_resume,
235 .remove = ahd_linux_pci_dev_remove,
236 .id_table = ahd_linux_pci_id_table
240 ahd_linux_pci_init(void)
242 return pci_register_driver(&aic79xx_pci_driver);
246 ahd_linux_pci_exit(void)
248 pci_unregister_driver(&aic79xx_pci_driver);
252 ahd_linux_pci_reserve_io_regions(struct ahd_softc *ahd, resource_size_t *base,
253 resource_size_t *base2)
255 *base = pci_resource_start(ahd->dev_softc, 0);
257 * This is really the 3rd bar and should be at index 2,
258 * but the Linux PCI code doesn't know how to "count" 64bit
261 *base2 = pci_resource_start(ahd->dev_softc, 3);
262 if (*base == 0 || *base2 == 0)
264 if (!request_region(*base, 256, "aic79xx"))
266 if (!request_region(*base2, 256, "aic79xx")) {
267 release_region(*base, 256);
274 ahd_linux_pci_reserve_mem_region(struct ahd_softc *ahd,
275 resource_size_t *bus_addr,
276 uint8_t __iomem **maddr)
278 resource_size_t start;
279 resource_size_t base_page;
283 if (aic79xx_allow_memio == 0)
286 if ((ahd->bugs & AHD_PCIX_MMAPIO_BUG) != 0)
289 start = pci_resource_start(ahd->dev_softc, 1);
290 base_page = start & PAGE_MASK;
291 base_offset = start - base_page;
294 if (!request_mem_region(start, 0x1000, "aic79xx"))
297 *maddr = ioremap_nocache(base_page, base_offset + 512);
298 if (*maddr == NULL) {
300 release_mem_region(start, 0x1000);
302 *maddr += base_offset;
310 ahd_pci_map_registers(struct ahd_softc *ahd)
313 resource_size_t base;
314 uint8_t __iomem *maddr;
318 * If its allowed, we prefer memory mapped access.
320 command = ahd_pci_read_config(ahd->dev_softc, PCIR_COMMAND, 4);
321 command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
324 error = ahd_linux_pci_reserve_mem_region(ahd, &base, &maddr);
326 ahd->platform_data->mem_busaddr = base;
327 ahd->tags[0] = BUS_SPACE_MEMIO;
328 ahd->bshs[0].maddr = maddr;
329 ahd->tags[1] = BUS_SPACE_MEMIO;
330 ahd->bshs[1].maddr = maddr + 0x100;
331 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND,
332 command | PCIM_CMD_MEMEN, 4);
334 if (ahd_pci_test_register_access(ahd) != 0) {
336 printf("aic79xx: PCI Device %d:%d:%d "
337 "failed memory mapped test. Using PIO.\n",
338 ahd_get_pci_bus(ahd->dev_softc),
339 ahd_get_pci_slot(ahd->dev_softc),
340 ahd_get_pci_function(ahd->dev_softc));
342 release_mem_region(ahd->platform_data->mem_busaddr,
344 ahd->bshs[0].maddr = NULL;
347 command |= PCIM_CMD_MEMEN;
348 } else if (bootverbose) {
349 printf("aic79xx: PCI%d:%d:%d MEM region 0x%llx "
350 "unavailable. Cannot memory map device.\n",
351 ahd_get_pci_bus(ahd->dev_softc),
352 ahd_get_pci_slot(ahd->dev_softc),
353 ahd_get_pci_function(ahd->dev_softc),
354 (unsigned long long)base);
358 resource_size_t base2;
360 error = ahd_linux_pci_reserve_io_regions(ahd, &base, &base2);
362 ahd->tags[0] = BUS_SPACE_PIO;
363 ahd->tags[1] = BUS_SPACE_PIO;
364 ahd->bshs[0].ioport = (u_long)base;
365 ahd->bshs[1].ioport = (u_long)base2;
366 command |= PCIM_CMD_PORTEN;
368 printf("aic79xx: PCI%d:%d:%d IO regions 0x%llx and "
369 "0x%llx unavailable. Cannot map device.\n",
370 ahd_get_pci_bus(ahd->dev_softc),
371 ahd_get_pci_slot(ahd->dev_softc),
372 ahd_get_pci_function(ahd->dev_softc),
373 (unsigned long long)base,
374 (unsigned long long)base2);
377 ahd_pci_write_config(ahd->dev_softc, PCIR_COMMAND, command, 4);
382 ahd_pci_map_int(struct ahd_softc *ahd)
386 error = request_irq(ahd->dev_softc->irq, ahd_linux_isr,
387 IRQF_SHARED, "aic79xx", ahd);
389 ahd->platform_data->irq = ahd->dev_softc->irq;
395 ahd_power_state_change(struct ahd_softc *ahd, ahd_power_state new_state)
397 pci_set_power_state(ahd->dev_softc, new_state);