xref: /linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c (revision 4bdc0d676a643140bdf17dbf7eafedee3d496a3c)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Linux driver attachment glue for PCI based controllers.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (c) 2000-2001 Adaptec Inc.
51da177e4SLinus Torvalds  * All rights reserved.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Redistribution and use in source and binary forms, with or without
81da177e4SLinus Torvalds  * modification, are permitted provided that the following conditions
91da177e4SLinus Torvalds  * are met:
101da177e4SLinus Torvalds  * 1. Redistributions of source code must retain the above copyright
111da177e4SLinus Torvalds  *    notice, this list of conditions, and the following disclaimer,
121da177e4SLinus Torvalds  *    without modification.
131da177e4SLinus Torvalds  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
141da177e4SLinus Torvalds  *    substantially similar to the "NO WARRANTY" disclaimer below
151da177e4SLinus Torvalds  *    ("Disclaimer") and any redistribution must be conditioned upon
161da177e4SLinus Torvalds  *    including a substantially similar Disclaimer requirement for further
171da177e4SLinus Torvalds  *    binary redistribution.
181da177e4SLinus Torvalds  * 3. Neither the names of the above-listed copyright holders nor the names
191da177e4SLinus Torvalds  *    of any contributors may be used to endorse or promote products derived
201da177e4SLinus Torvalds  *    from this software without specific prior written permission.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * Alternatively, this software may be distributed under the terms of the
231da177e4SLinus Torvalds  * GNU General Public License ("GPL") version 2 as published by the Free
241da177e4SLinus Torvalds  * Software Foundation.
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * NO WARRANTY
271da177e4SLinus Torvalds  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
281da177e4SLinus Torvalds  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
291da177e4SLinus Torvalds  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
301da177e4SLinus Torvalds  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
311da177e4SLinus Torvalds  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
321da177e4SLinus Torvalds  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
331da177e4SLinus Torvalds  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
341da177e4SLinus Torvalds  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
351da177e4SLinus Torvalds  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
361da177e4SLinus Torvalds  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
371da177e4SLinus Torvalds  * POSSIBILITY OF SUCH DAMAGES.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm_pci.c#47 $
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include "aic7xxx_osm.h"
431da177e4SLinus Torvalds #include "aic7xxx_pci.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* Define the macro locally since it's different for different class of chips.
461da177e4SLinus Torvalds */
471da177e4SLinus Torvalds #define ID(x)	ID_C(x, PCI_CLASS_STORAGE_SCSI)
481da177e4SLinus Torvalds 
49980b306aSDenys Vlasenko static const struct pci_device_id ahc_linux_pci_id_table[] = {
501da177e4SLinus Torvalds 	/* aic7850 based controllers */
511da177e4SLinus Torvalds 	ID(ID_AHA_2902_04_10_15_20C_30C),
521da177e4SLinus Torvalds 	/* aic7860 based controllers */
531da177e4SLinus Torvalds 	ID(ID_AHA_2930CU),
541da177e4SLinus Torvalds 	ID(ID_AHA_1480A & ID_DEV_VENDOR_MASK),
551da177e4SLinus Torvalds 	ID(ID_AHA_2940AU_0 & ID_DEV_VENDOR_MASK),
561da177e4SLinus Torvalds 	ID(ID_AHA_2940AU_CN & ID_DEV_VENDOR_MASK),
571da177e4SLinus Torvalds 	ID(ID_AHA_2930C_VAR & ID_DEV_VENDOR_MASK),
581da177e4SLinus Torvalds 	/* aic7870 based controllers */
591da177e4SLinus Torvalds 	ID(ID_AHA_2940),
601da177e4SLinus Torvalds 	ID(ID_AHA_3940),
611da177e4SLinus Torvalds 	ID(ID_AHA_398X),
621da177e4SLinus Torvalds 	ID(ID_AHA_2944),
631da177e4SLinus Torvalds 	ID(ID_AHA_3944),
641da177e4SLinus Torvalds 	ID(ID_AHA_4944),
651da177e4SLinus Torvalds 	/* aic7880 based controllers */
661da177e4SLinus Torvalds 	ID(ID_AHA_2940U & ID_DEV_VENDOR_MASK),
671da177e4SLinus Torvalds 	ID(ID_AHA_3940U & ID_DEV_VENDOR_MASK),
681da177e4SLinus Torvalds 	ID(ID_AHA_2944U & ID_DEV_VENDOR_MASK),
691da177e4SLinus Torvalds 	ID(ID_AHA_3944U & ID_DEV_VENDOR_MASK),
701da177e4SLinus Torvalds 	ID(ID_AHA_398XU & ID_DEV_VENDOR_MASK),
711da177e4SLinus Torvalds 	ID(ID_AHA_4944U & ID_DEV_VENDOR_MASK),
721da177e4SLinus Torvalds 	ID(ID_AHA_2930U & ID_DEV_VENDOR_MASK),
731da177e4SLinus Torvalds 	ID(ID_AHA_2940U_PRO & ID_DEV_VENDOR_MASK),
741da177e4SLinus Torvalds 	ID(ID_AHA_2940U_CN & ID_DEV_VENDOR_MASK),
751da177e4SLinus Torvalds 	/* aic7890 based controllers */
761da177e4SLinus Torvalds 	ID(ID_AHA_2930U2),
771da177e4SLinus Torvalds 	ID(ID_AHA_2940U2B),
781da177e4SLinus Torvalds 	ID(ID_AHA_2940U2_OEM),
791da177e4SLinus Torvalds 	ID(ID_AHA_2940U2),
801da177e4SLinus Torvalds 	ID(ID_AHA_2950U2B),
811da177e4SLinus Torvalds 	ID16(ID_AIC7890_ARO & ID_AIC7895_ARO_MASK),
821da177e4SLinus Torvalds 	ID(ID_AAA_131U2),
831da177e4SLinus Torvalds 	/* aic7890 based controllers */
841da177e4SLinus Torvalds 	ID(ID_AHA_29160),
851da177e4SLinus Torvalds 	ID(ID_AHA_29160_CPQ),
861da177e4SLinus Torvalds 	ID(ID_AHA_29160N),
871da177e4SLinus Torvalds 	ID(ID_AHA_29160C),
881da177e4SLinus Torvalds 	ID(ID_AHA_29160B),
891da177e4SLinus Torvalds 	ID(ID_AHA_19160B),
901da177e4SLinus Torvalds 	ID(ID_AIC7892_ARO),
911da177e4SLinus Torvalds 	/* aic7892 based controllers */
921da177e4SLinus Torvalds 	ID(ID_AHA_2940U_DUAL),
931da177e4SLinus Torvalds 	ID(ID_AHA_3940AU),
941da177e4SLinus Torvalds 	ID(ID_AHA_3944AU),
951da177e4SLinus Torvalds 	ID(ID_AIC7895_ARO),
961da177e4SLinus Torvalds 	ID(ID_AHA_3950U2B_0),
971da177e4SLinus Torvalds 	ID(ID_AHA_3950U2B_1),
981da177e4SLinus Torvalds 	ID(ID_AHA_3950U2D_0),
991da177e4SLinus Torvalds 	ID(ID_AHA_3950U2D_1),
1001da177e4SLinus Torvalds 	ID(ID_AIC7896_ARO),
1011da177e4SLinus Torvalds 	/* aic7899 based controllers */
1021da177e4SLinus Torvalds 	ID(ID_AHA_3960D),
1031da177e4SLinus Torvalds 	ID(ID_AHA_3960D_CPQ),
1041da177e4SLinus Torvalds 	ID(ID_AIC7899_ARO),
1051da177e4SLinus Torvalds 	/* Generic chip probes for devices we don't know exactly. */
1061da177e4SLinus Torvalds 	ID(ID_AIC7850 & ID_DEV_VENDOR_MASK),
1071da177e4SLinus Torvalds 	ID(ID_AIC7855 & ID_DEV_VENDOR_MASK),
1081da177e4SLinus Torvalds 	ID(ID_AIC7859 & ID_DEV_VENDOR_MASK),
1091da177e4SLinus Torvalds 	ID(ID_AIC7860 & ID_DEV_VENDOR_MASK),
1101da177e4SLinus Torvalds 	ID(ID_AIC7870 & ID_DEV_VENDOR_MASK),
1111da177e4SLinus Torvalds 	ID(ID_AIC7880 & ID_DEV_VENDOR_MASK),
1121da177e4SLinus Torvalds  	ID16(ID_AIC7890 & ID_9005_GENERIC_MASK),
1131da177e4SLinus Torvalds  	ID16(ID_AIC7892 & ID_9005_GENERIC_MASK),
1141da177e4SLinus Torvalds 	ID(ID_AIC7895 & ID_DEV_VENDOR_MASK),
1151da177e4SLinus Torvalds 	ID16(ID_AIC7896 & ID_9005_GENERIC_MASK),
1161da177e4SLinus Torvalds 	ID16(ID_AIC7899 & ID_9005_GENERIC_MASK),
1171da177e4SLinus Torvalds 	ID(ID_AIC7810 & ID_DEV_VENDOR_MASK),
1181da177e4SLinus Torvalds 	ID(ID_AIC7815 & ID_DEV_VENDOR_MASK),
1191da177e4SLinus Torvalds 	{ 0 }
1201da177e4SLinus Torvalds };
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds MODULE_DEVICE_TABLE(pci, ahc_linux_pci_id_table);
1231da177e4SLinus Torvalds 
124b5720729SHannes Reinecke #ifdef CONFIG_PM
125b5720729SHannes Reinecke static int
126b5720729SHannes Reinecke ahc_linux_pci_dev_suspend(struct pci_dev *pdev, pm_message_t mesg)
127b5720729SHannes Reinecke {
128b5720729SHannes Reinecke 	struct ahc_softc *ahc = pci_get_drvdata(pdev);
129b5720729SHannes Reinecke 	int rc;
130b5720729SHannes Reinecke 
131b5720729SHannes Reinecke 	if ((rc = ahc_suspend(ahc)))
132b5720729SHannes Reinecke 		return rc;
133b5720729SHannes Reinecke 
134b5720729SHannes Reinecke 	pci_save_state(pdev);
135b5720729SHannes Reinecke 	pci_disable_device(pdev);
136b5720729SHannes Reinecke 
1373a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
138b5720729SHannes Reinecke 		pci_set_power_state(pdev, PCI_D3hot);
139b5720729SHannes Reinecke 
140b5720729SHannes Reinecke 	return rc;
141b5720729SHannes Reinecke }
142b5720729SHannes Reinecke 
143b5720729SHannes Reinecke static int
144b5720729SHannes Reinecke ahc_linux_pci_dev_resume(struct pci_dev *pdev)
145b5720729SHannes Reinecke {
146b5720729SHannes Reinecke 	struct ahc_softc *ahc = pci_get_drvdata(pdev);
147b5720729SHannes Reinecke 	int rc;
148b5720729SHannes Reinecke 
149b5720729SHannes Reinecke 	pci_set_power_state(pdev, PCI_D0);
150b5720729SHannes Reinecke 	pci_restore_state(pdev);
151b5720729SHannes Reinecke 
152b5720729SHannes Reinecke 	if ((rc = pci_enable_device(pdev))) {
153b5720729SHannes Reinecke 		dev_printk(KERN_ERR, &pdev->dev,
154b5720729SHannes Reinecke 			   "failed to enable device after resume (%d)\n", rc);
155b5720729SHannes Reinecke 		return rc;
156b5720729SHannes Reinecke 	}
157b5720729SHannes Reinecke 
158b5720729SHannes Reinecke 	pci_set_master(pdev);
159b5720729SHannes Reinecke 
160b5720729SHannes Reinecke 	ahc_pci_resume(ahc);
161b5720729SHannes Reinecke 
162b5720729SHannes Reinecke 	return (ahc_resume(ahc));
163b5720729SHannes Reinecke }
16403e7925dSFUJITA Tomonori #endif
165b5720729SHannes Reinecke 
1661da177e4SLinus Torvalds static void
1671da177e4SLinus Torvalds ahc_linux_pci_dev_remove(struct pci_dev *pdev)
1681da177e4SLinus Torvalds {
1693d65692aSChristoph Hellwig 	struct ahc_softc *ahc = pci_get_drvdata(pdev);
1701da177e4SLinus Torvalds 	u_long s;
1711da177e4SLinus Torvalds 
17297af50f6SJames Bottomley 	if (ahc->platform_data && ahc->platform_data->host)
17397af50f6SJames Bottomley 			scsi_remove_host(ahc->platform_data->host);
17497af50f6SJames Bottomley 
1751da177e4SLinus Torvalds 	ahc_lock(ahc, &s);
1761da177e4SLinus Torvalds 	ahc_intr_enable(ahc, FALSE);
1771da177e4SLinus Torvalds 	ahc_unlock(ahc, &s);
1781da177e4SLinus Torvalds 	ahc_free(ahc);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1813a4f5c60SJames Bottomley static void
1823a4f5c60SJames Bottomley ahc_linux_pci_inherit_flags(struct ahc_softc *ahc)
1833a4f5c60SJames Bottomley {
1843a4f5c60SJames Bottomley 	struct pci_dev *pdev = ahc->dev_softc, *master_pdev;
1853a4f5c60SJames Bottomley 	unsigned int master_devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
1863a4f5c60SJames Bottomley 
1873a4f5c60SJames Bottomley 	master_pdev = pci_get_slot(pdev->bus, master_devfn);
1883a4f5c60SJames Bottomley 	if (master_pdev) {
1893a4f5c60SJames Bottomley 		struct ahc_softc *master = pci_get_drvdata(master_pdev);
1903a4f5c60SJames Bottomley 		if (master) {
1913a4f5c60SJames Bottomley 			ahc->flags &= ~AHC_BIOS_ENABLED;
1923a4f5c60SJames Bottomley 			ahc->flags |= master->flags & AHC_BIOS_ENABLED;
1933a4f5c60SJames Bottomley 
1943a4f5c60SJames Bottomley 			ahc->flags &= ~AHC_PRIMARY_CHANNEL;
1953a4f5c60SJames Bottomley 			ahc->flags |= master->flags & AHC_PRIMARY_CHANNEL;
1963a4f5c60SJames Bottomley 		} else
1973a4f5c60SJames Bottomley 			printk(KERN_ERR "aic7xxx: no multichannel peer found!\n");
1983a4f5c60SJames Bottomley 		pci_dev_put(master_pdev);
1993a4f5c60SJames Bottomley 	}
2003a4f5c60SJames Bottomley }
2013a4f5c60SJames Bottomley 
2021da177e4SLinus Torvalds static int
2031da177e4SLinus Torvalds ahc_linux_pci_dev_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	char		 buf[80];
2061da177e4SLinus Torvalds 	const uint64_t	 mask_39bit = 0x7FFFFFFFFFULL;
2071da177e4SLinus Torvalds 	struct		 ahc_softc *ahc;
2081da177e4SLinus Torvalds 	ahc_dev_softc_t	 pci;
209980b306aSDenys Vlasenko 	const struct ahc_pci_identity *entry;
2101da177e4SLinus Torvalds 	char		*name;
2111da177e4SLinus Torvalds 	int		 error;
212a89f29f6SJames Bottomley 	struct device	*dev = &pdev->dev;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	pci = pdev;
2151da177e4SLinus Torvalds 	entry = ahc_find_pci_device(pci);
2161da177e4SLinus Torvalds 	if (entry == NULL)
2171da177e4SLinus Torvalds 		return (-ENODEV);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/*
2201da177e4SLinus Torvalds 	 * Allocate a softc for this card and
2211da177e4SLinus Torvalds 	 * set it up for attachment by our
2221da177e4SLinus Torvalds 	 * common detect routine.
2231da177e4SLinus Torvalds 	 */
2241da177e4SLinus Torvalds 	sprintf(buf, "ahc_pci:%d:%d:%d",
2251da177e4SLinus Torvalds 		ahc_get_pci_bus(pci),
2261da177e4SLinus Torvalds 		ahc_get_pci_slot(pci),
2271da177e4SLinus Torvalds 		ahc_get_pci_function(pci));
2281f3d2d9eSHimangi Saraogi 	name = kstrdup(buf, GFP_ATOMIC);
2291da177e4SLinus Torvalds 	if (name == NULL)
2301da177e4SLinus Torvalds 		return (-ENOMEM);
2311da177e4SLinus Torvalds 	ahc = ahc_alloc(NULL, name);
2321da177e4SLinus Torvalds 	if (ahc == NULL)
2331da177e4SLinus Torvalds 		return (-ENOMEM);
2341da177e4SLinus Torvalds 	if (pci_enable_device(pdev)) {
2351da177e4SLinus Torvalds 		ahc_free(ahc);
2361da177e4SLinus Torvalds 		return (-ENODEV);
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 	pci_set_master(pdev);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (sizeof(dma_addr_t) > 4
241a89f29f6SJames Bottomley 	    && ahc->features & AHC_LARGE_SCBS
242a89f29f6SJames Bottomley 	    && dma_set_mask(dev, mask_39bit) == 0
243284901a9SYang Hongyang 	    && dma_get_required_mask(dev) > DMA_BIT_MASK(32)) {
2441da177e4SLinus Torvalds 		ahc->flags |= AHC_39BIT_ADDRESSING;
2451da177e4SLinus Torvalds 	} else {
246284901a9SYang Hongyang 		if (dma_set_mask(dev, DMA_BIT_MASK(32))) {
2474a6fae1dSJesper Juhl 			ahc_free(ahc);
2481da177e4SLinus Torvalds 			printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n");
2491da177e4SLinus Torvalds                 	return (-ENODEV);
2501da177e4SLinus Torvalds 		}
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 	ahc->dev_softc = pci;
253144ec974SChristoph Hellwig 	ahc->dev = &pci->dev;
2541da177e4SLinus Torvalds 	error = ahc_pci_config(ahc, entry);
2551da177e4SLinus Torvalds 	if (error != 0) {
2561da177e4SLinus Torvalds 		ahc_free(ahc);
2571da177e4SLinus Torvalds 		return (-error);
2581da177e4SLinus Torvalds 	}
2593a4f5c60SJames Bottomley 
2603a4f5c60SJames Bottomley 	/*
2613a4f5c60SJames Bottomley 	 * Second Function PCI devices need to inherit some
2623a4f5c60SJames Bottomley 	 * settings from function 0.
2633a4f5c60SJames Bottomley 	 */
2643a4f5c60SJames Bottomley 	if ((ahc->features & AHC_MULTI_FUNC) && PCI_FUNC(pdev->devfn) != 0)
2653a4f5c60SJames Bottomley 		ahc_linux_pci_inherit_flags(ahc);
2663a4f5c60SJames Bottomley 
2671da177e4SLinus Torvalds 	pci_set_drvdata(pdev, ahc);
2681da177e4SLinus Torvalds 	ahc_linux_register_host(ahc, &aic7xxx_driver_template);
2691da177e4SLinus Torvalds 	return (0);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
272be0d6768SDenys Vlasenko /******************************* PCI Routines *********************************/
273be0d6768SDenys Vlasenko uint32_t
274be0d6768SDenys Vlasenko ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
275be0d6768SDenys Vlasenko {
276be0d6768SDenys Vlasenko 	switch (width) {
277be0d6768SDenys Vlasenko 	case 1:
278be0d6768SDenys Vlasenko 	{
279be0d6768SDenys Vlasenko 		uint8_t retval;
280be0d6768SDenys Vlasenko 
281be0d6768SDenys Vlasenko 		pci_read_config_byte(pci, reg, &retval);
282be0d6768SDenys Vlasenko 		return (retval);
283be0d6768SDenys Vlasenko 	}
284be0d6768SDenys Vlasenko 	case 2:
285be0d6768SDenys Vlasenko 	{
286be0d6768SDenys Vlasenko 		uint16_t retval;
287be0d6768SDenys Vlasenko 		pci_read_config_word(pci, reg, &retval);
288be0d6768SDenys Vlasenko 		return (retval);
289be0d6768SDenys Vlasenko 	}
290be0d6768SDenys Vlasenko 	case 4:
291be0d6768SDenys Vlasenko 	{
292be0d6768SDenys Vlasenko 		uint32_t retval;
293be0d6768SDenys Vlasenko 		pci_read_config_dword(pci, reg, &retval);
294be0d6768SDenys Vlasenko 		return (retval);
295be0d6768SDenys Vlasenko 	}
296be0d6768SDenys Vlasenko 	default:
297be0d6768SDenys Vlasenko 		panic("ahc_pci_read_config: Read size too big");
298be0d6768SDenys Vlasenko 		/* NOTREACHED */
299be0d6768SDenys Vlasenko 		return (0);
300be0d6768SDenys Vlasenko 	}
301be0d6768SDenys Vlasenko }
302be0d6768SDenys Vlasenko 
303be0d6768SDenys Vlasenko void
304be0d6768SDenys Vlasenko ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
305be0d6768SDenys Vlasenko {
306be0d6768SDenys Vlasenko 	switch (width) {
307be0d6768SDenys Vlasenko 	case 1:
308be0d6768SDenys Vlasenko 		pci_write_config_byte(pci, reg, value);
309be0d6768SDenys Vlasenko 		break;
310be0d6768SDenys Vlasenko 	case 2:
311be0d6768SDenys Vlasenko 		pci_write_config_word(pci, reg, value);
312be0d6768SDenys Vlasenko 		break;
313be0d6768SDenys Vlasenko 	case 4:
314be0d6768SDenys Vlasenko 		pci_write_config_dword(pci, reg, value);
315be0d6768SDenys Vlasenko 		break;
316be0d6768SDenys Vlasenko 	default:
317be0d6768SDenys Vlasenko 		panic("ahc_pci_write_config: Write size too big");
318be0d6768SDenys Vlasenko 		/* NOTREACHED */
319be0d6768SDenys Vlasenko 	}
320be0d6768SDenys Vlasenko }
321be0d6768SDenys Vlasenko 
322be0d6768SDenys Vlasenko 
32303e7925dSFUJITA Tomonori static struct pci_driver aic7xxx_pci_driver = {
32403e7925dSFUJITA Tomonori 	.name		= "aic7xxx",
32503e7925dSFUJITA Tomonori 	.probe		= ahc_linux_pci_dev_probe,
32603e7925dSFUJITA Tomonori #ifdef CONFIG_PM
32703e7925dSFUJITA Tomonori 	.suspend	= ahc_linux_pci_dev_suspend,
32803e7925dSFUJITA Tomonori 	.resume		= ahc_linux_pci_dev_resume,
32903e7925dSFUJITA Tomonori #endif
33003e7925dSFUJITA Tomonori 	.remove		= ahc_linux_pci_dev_remove,
33103e7925dSFUJITA Tomonori 	.id_table	= ahc_linux_pci_id_table
33203e7925dSFUJITA Tomonori };
33303e7925dSFUJITA Tomonori 
3341da177e4SLinus Torvalds int
3351da177e4SLinus Torvalds ahc_linux_pci_init(void)
3361da177e4SLinus Torvalds {
337dcbccbdeSHenrik Kretzschmar 	return pci_register_driver(&aic7xxx_pci_driver);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds void
3411da177e4SLinus Torvalds ahc_linux_pci_exit(void)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	pci_unregister_driver(&aic7xxx_pci_driver);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static int
34744850413SSergei Shtylyov ahc_linux_pci_reserve_io_region(struct ahc_softc *ahc, resource_size_t *base)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	if (aic7xxx_allow_memio == 0)
3501da177e4SLinus Torvalds 		return (ENOMEM);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	*base = pci_resource_start(ahc->dev_softc, 0);
3531da177e4SLinus Torvalds 	if (*base == 0)
3541da177e4SLinus Torvalds 		return (ENOMEM);
355172c122dSHarvey Harrison 	if (!request_region(*base, 256, "aic7xxx"))
3561da177e4SLinus Torvalds 		return (ENOMEM);
3571da177e4SLinus Torvalds 	return (0);
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds static int
3611da177e4SLinus Torvalds ahc_linux_pci_reserve_mem_region(struct ahc_softc *ahc,
36244850413SSergei Shtylyov 				 resource_size_t *bus_addr,
3631da177e4SLinus Torvalds 				 uint8_t __iomem **maddr)
3641da177e4SLinus Torvalds {
36544850413SSergei Shtylyov 	resource_size_t	start;
3661da177e4SLinus Torvalds 	int	error;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	error = 0;
3691da177e4SLinus Torvalds 	start = pci_resource_start(ahc->dev_softc, 1);
3701da177e4SLinus Torvalds 	if (start != 0) {
3711da177e4SLinus Torvalds 		*bus_addr = start;
372172c122dSHarvey Harrison 		if (!request_mem_region(start, 0x1000, "aic7xxx"))
3731da177e4SLinus Torvalds 			error = ENOMEM;
3741da177e4SLinus Torvalds 		if (error == 0) {
375*4bdc0d67SChristoph Hellwig 			*maddr = ioremap(start, 256);
3761da177e4SLinus Torvalds 			if (*maddr == NULL) {
3771da177e4SLinus Torvalds 				error = ENOMEM;
3781da177e4SLinus Torvalds 				release_mem_region(start, 0x1000);
3791da177e4SLinus Torvalds 			}
3801da177e4SLinus Torvalds 		}
3811da177e4SLinus Torvalds 	} else
3821da177e4SLinus Torvalds 		error = ENOMEM;
3831da177e4SLinus Torvalds 	return (error);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds int
3871da177e4SLinus Torvalds ahc_pci_map_registers(struct ahc_softc *ahc)
3881da177e4SLinus Torvalds {
3891da177e4SLinus Torvalds 	uint32_t command;
39044850413SSergei Shtylyov 	resource_size_t	base;
3911da177e4SLinus Torvalds 	uint8_t	__iomem *maddr;
3921da177e4SLinus Torvalds 	int	 error;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	/*
3951da177e4SLinus Torvalds 	 * If its allowed, we prefer memory mapped access.
3961da177e4SLinus Torvalds 	 */
3971da177e4SLinus Torvalds 	command = ahc_pci_read_config(ahc->dev_softc, PCIR_COMMAND, 4);
3981da177e4SLinus Torvalds 	command &= ~(PCIM_CMD_PORTEN|PCIM_CMD_MEMEN);
3991da177e4SLinus Torvalds 	base = 0;
4001da177e4SLinus Torvalds 	maddr = NULL;
4011da177e4SLinus Torvalds 	error = ahc_linux_pci_reserve_mem_region(ahc, &base, &maddr);
4021da177e4SLinus Torvalds 	if (error == 0) {
4031da177e4SLinus Torvalds 		ahc->platform_data->mem_busaddr = base;
4041da177e4SLinus Torvalds 		ahc->tag = BUS_SPACE_MEMIO;
4051da177e4SLinus Torvalds 		ahc->bsh.maddr = maddr;
4061da177e4SLinus Torvalds 		ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND,
4071da177e4SLinus Torvalds 				     command | PCIM_CMD_MEMEN, 4);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		/*
4101da177e4SLinus Torvalds 		 * Do a quick test to see if memory mapped
4111da177e4SLinus Torvalds 		 * I/O is functioning correctly.
4121da177e4SLinus Torvalds 		 */
4131da177e4SLinus Torvalds 		if (ahc_pci_test_register_access(ahc) != 0) {
4141da177e4SLinus Torvalds 
41548813cf9SPekka Enberg 			printk("aic7xxx: PCI Device %d:%d:%d "
4161da177e4SLinus Torvalds 			       "failed memory mapped test.  Using PIO.\n",
4171da177e4SLinus Torvalds 			       ahc_get_pci_bus(ahc->dev_softc),
4181da177e4SLinus Torvalds 			       ahc_get_pci_slot(ahc->dev_softc),
4191da177e4SLinus Torvalds 			       ahc_get_pci_function(ahc->dev_softc));
4201da177e4SLinus Torvalds 			iounmap(maddr);
4211da177e4SLinus Torvalds 			release_mem_region(ahc->platform_data->mem_busaddr,
4221da177e4SLinus Torvalds 					   0x1000);
4231da177e4SLinus Torvalds 			ahc->bsh.maddr = NULL;
4241da177e4SLinus Torvalds 			maddr = NULL;
4251da177e4SLinus Torvalds 		} else
4261da177e4SLinus Torvalds 			command |= PCIM_CMD_MEMEN;
4271da177e4SLinus Torvalds 	} else {
42848813cf9SPekka Enberg 		printk("aic7xxx: PCI%d:%d:%d MEM region 0x%llx "
4291da177e4SLinus Torvalds 		       "unavailable. Cannot memory map device.\n",
4301da177e4SLinus Torvalds 		       ahc_get_pci_bus(ahc->dev_softc),
4311da177e4SLinus Torvalds 		       ahc_get_pci_slot(ahc->dev_softc),
4321da177e4SLinus Torvalds 		       ahc_get_pci_function(ahc->dev_softc),
43344850413SSergei Shtylyov 		       (unsigned long long)base);
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/*
4371da177e4SLinus Torvalds 	 * We always prefer memory mapped access.
4381da177e4SLinus Torvalds 	 */
4391da177e4SLinus Torvalds 	if (maddr == NULL) {
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		error = ahc_linux_pci_reserve_io_region(ahc, &base);
4421da177e4SLinus Torvalds 		if (error == 0) {
4431da177e4SLinus Torvalds 			ahc->tag = BUS_SPACE_PIO;
44444850413SSergei Shtylyov 			ahc->bsh.ioport = (u_long)base;
4451da177e4SLinus Torvalds 			command |= PCIM_CMD_PORTEN;
4461da177e4SLinus Torvalds 		} else {
44748813cf9SPekka Enberg 			printk("aic7xxx: PCI%d:%d:%d IO region 0x%llx[0..255] "
4481da177e4SLinus Torvalds 			       "unavailable. Cannot map device.\n",
4491da177e4SLinus Torvalds 			       ahc_get_pci_bus(ahc->dev_softc),
4501da177e4SLinus Torvalds 			       ahc_get_pci_slot(ahc->dev_softc),
4511da177e4SLinus Torvalds 			       ahc_get_pci_function(ahc->dev_softc),
45244850413SSergei Shtylyov 			       (unsigned long long)base);
4531da177e4SLinus Torvalds 		}
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 	ahc_pci_write_config(ahc->dev_softc, PCIR_COMMAND, command, 4);
4561da177e4SLinus Torvalds 	return (error);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds int
4601da177e4SLinus Torvalds ahc_pci_map_int(struct ahc_softc *ahc)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	int error;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	error = request_irq(ahc->dev_softc->irq, ahc_linux_isr,
4651d6f359aSThomas Gleixner 			    IRQF_SHARED, "aic7xxx", ahc);
4661da177e4SLinus Torvalds 	if (error == 0)
4671da177e4SLinus Torvalds 		ahc->platform_data->irq = ahc->dev_softc->irq;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	return (-error);
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
472