xref: /freebsd/sys/dev/aac/aac_pci.c (revision a9f3d2c7564679735fd2a35ab50616ab8b8a0b0b)
135863739SMike Smith /*-
235863739SMike Smith  * Copyright (c) 2000 Michael Smith
3c6eafcf2SScott Long  * Copyright (c) 2001 Scott Long
435863739SMike Smith  * Copyright (c) 2000 BSDi
5fadfef89SScott Long  * Copyright (c) 2001 Adaptec, Inc.
635863739SMike Smith  * All rights reserved.
735863739SMike Smith  *
835863739SMike Smith  * Redistribution and use in source and binary forms, with or without
935863739SMike Smith  * modification, are permitted provided that the following conditions
1035863739SMike Smith  * are met:
1135863739SMike Smith  * 1. Redistributions of source code must retain the above copyright
1235863739SMike Smith  *    notice, this list of conditions and the following disclaimer.
1335863739SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1435863739SMike Smith  *    notice, this list of conditions and the following disclaimer in the
1535863739SMike Smith  *    documentation and/or other materials provided with the distribution.
1635863739SMike Smith  *
1735863739SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1835863739SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1935863739SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2035863739SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2135863739SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2235863739SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2335863739SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2435863739SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2535863739SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2635863739SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2735863739SMike Smith  * SUCH DAMAGE.
2835863739SMike Smith  *
2935863739SMike Smith  *	$FreeBSD$
3035863739SMike Smith  */
3135863739SMike Smith 
3235863739SMike Smith /*
3335863739SMike Smith  * PCI bus interface and resource allocation.
3435863739SMike Smith  */
3535863739SMike Smith 
36f6c4dd3fSScott Long #include "opt_aac.h"
37f6c4dd3fSScott Long 
3835863739SMike Smith #include <sys/param.h>
3935863739SMike Smith #include <sys/systm.h>
4035863739SMike Smith #include <sys/kernel.h>
4135863739SMike Smith 
4235863739SMike Smith #include <dev/aac/aac_compat.h>
4335863739SMike Smith #include <sys/bus.h>
4435863739SMike Smith #include <sys/conf.h>
4535863739SMike Smith #include <sys/devicestat.h>
4635863739SMike Smith #include <sys/disk.h>
4735863739SMike Smith 
4835863739SMike Smith #include <machine/bus_memio.h>
4935863739SMike Smith #include <machine/bus.h>
5035863739SMike Smith #include <machine/resource.h>
5135863739SMike Smith #include <sys/rman.h>
5235863739SMike Smith 
5335863739SMike Smith #include <pci/pcireg.h>
5435863739SMike Smith #include <pci/pcivar.h>
5535863739SMike Smith 
5635863739SMike Smith #include <dev/aac/aacreg.h>
570b94a66eSMike Smith #include <dev/aac/aac_ioctl.h>
5835863739SMike Smith #include <dev/aac/aacvar.h>
5935863739SMike Smith 
6035863739SMike Smith static int	aac_pci_probe(device_t dev);
6135863739SMike Smith static int	aac_pci_attach(device_t dev);
6235863739SMike Smith 
6335863739SMike Smith static device_method_t aac_methods[] = {
6435863739SMike Smith 	/* Device interface */
6535863739SMike Smith 	DEVMETHOD(device_probe,		aac_pci_probe),
6635863739SMike Smith 	DEVMETHOD(device_attach,	aac_pci_attach),
6735863739SMike Smith 	DEVMETHOD(device_detach,	aac_detach),
6835863739SMike Smith 	DEVMETHOD(device_suspend,	aac_suspend),
6935863739SMike Smith 	DEVMETHOD(device_resume,	aac_resume),
7035863739SMike Smith 
7135863739SMike Smith 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
7235863739SMike Smith 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
7335863739SMike Smith 	{ 0, 0 }
7435863739SMike Smith };
7535863739SMike Smith 
7635863739SMike Smith static driver_t aac_pci_driver = {
7735863739SMike Smith 	"aac",
7835863739SMike Smith 	aac_methods,
7935863739SMike Smith 	sizeof(struct aac_softc)
8035863739SMike Smith };
8135863739SMike Smith 
82e45bef2aSMike Smith static devclass_t	aac_devclass;
83e45bef2aSMike Smith 
8435863739SMike Smith DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
8535863739SMike Smith 
8635863739SMike Smith struct aac_ident
8735863739SMike Smith {
8835863739SMike Smith 	u_int16_t		vendor;
8935863739SMike Smith 	u_int16_t		device;
9035863739SMike Smith 	u_int16_t		subvendor;
9135863739SMike Smith 	u_int16_t		subdevice;
9235863739SMike Smith 	int			hwif;
93fe3cb0e1SScott Long 	int			quirks;
9435863739SMike Smith 	char			*desc;
9535863739SMike Smith } aac_identifiers[] = {
96fe3cb0e1SScott Long 	{0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
97fe3cb0e1SScott Long 	"Dell PERC 2/Si"},
98fe3cb0e1SScott Long 	{0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
99fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
100fe3cb0e1SScott Long 	{0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
101fe3cb0e1SScott Long 	"Dell PERC 3/Si"},
102fe3cb0e1SScott Long 	{0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
103fe3cb0e1SScott Long 	"Dell PERC 3/Si"},
104fe3cb0e1SScott Long 	{0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
105fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
106fe3cb0e1SScott Long 	{0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
107fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
108fe3cb0e1SScott Long 	{0x1028, 0x0008, 0x1028, 0x00cf, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
109fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
110fe3cb0e1SScott Long 	{0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
111fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
112fe3cb0e1SScott Long 	{0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
113fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
114fe3cb0e1SScott Long 	{0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, AAC_QUIRK_NOCAM,
115fe3cb0e1SScott Long 	"Dell PERC 3/Di"},
116fe3cb0e1SScott Long 	{0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, AAC_QUIRK_NOCAM,
117fe3cb0e1SScott Long 	"Adaptec AAC-364"},
118a9f3d2c7SScott Long 	{0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, AAC_QUIRK_NOCAM,
119a9f3d2c7SScott Long 	 "Adaptec SCSI RAID 5400S"},
120fe3cb0e1SScott Long 	{0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_QUIRK_NOCAM |
121fe3cb0e1SScott Long 	 AAC_QUIRK_PERC2QC, "Dell PERC 2/QC"},
122a9f3d2c7SScott Long 	{0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, AAC_QUIRK_NOCAM,
123a9f3d2c7SScott Long 	 "HP NetRaid-4M"},
124fe3cb0e1SScott Long 	{0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, 0,
1250e6020f3SScott Long 	 "Adaptec SCSI RAID 2200S"},
126c52bf372SScott Long 	{0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, 0,
127c52bf372SScott Long 	 "Dell PERC 320/DC"},
128fe3cb0e1SScott Long 	{0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, 0,
1290e6020f3SScott Long 	 "Adaptec SCSI RAID 2120S"},
130fe3cb0e1SScott Long 	{0, 0, 0, 0, 0, 0, 0}
13135863739SMike Smith };
13235863739SMike Smith 
133914da7d0SScott Long /*
13435863739SMike Smith  * Determine whether this is one of our supported adapters.
13535863739SMike Smith  */
13635863739SMike Smith static int
13735863739SMike Smith aac_pci_probe(device_t dev)
13835863739SMike Smith {
13935863739SMike Smith 	struct aac_ident *m;
14035863739SMike Smith 
14135863739SMike Smith 	debug_called(1);
14235863739SMike Smith 
14335863739SMike Smith 	for (m = aac_identifiers; m->vendor != 0; m++) {
14435863739SMike Smith 		if ((m->vendor == pci_get_vendor(dev)) &&
14535863739SMike Smith 		    (m->device == pci_get_device(dev)) &&
146914da7d0SScott Long 		    ((m->subvendor == 0) || (m->subvendor ==
147914da7d0SScott Long 					     pci_get_subvendor(dev))) &&
148914da7d0SScott Long 		    ((m->subdevice == 0) || ((m->subdevice ==
149914da7d0SScott Long 					      pci_get_subdevice(dev))))) {
15035863739SMike Smith 
15135863739SMike Smith 			device_set_desc(dev, m->desc);
152e10f2ee5SMike Smith 			return(-10);	/* allow room to be overridden */
15335863739SMike Smith 		}
15435863739SMike Smith 	}
15535863739SMike Smith 	return(ENXIO);
15635863739SMike Smith }
15735863739SMike Smith 
158914da7d0SScott Long /*
15935863739SMike Smith  * Allocate resources for our device, set up the bus interface.
16035863739SMike Smith  */
16135863739SMike Smith static int
16235863739SMike Smith aac_pci_attach(device_t dev)
16335863739SMike Smith {
16435863739SMike Smith 	struct aac_softc *sc;
16535863739SMike Smith 	int i, error;
16635863739SMike Smith 	u_int32_t command;
16735863739SMike Smith 
16835863739SMike Smith 	debug_called(1);
16935863739SMike Smith 
17035863739SMike Smith 	/*
17135863739SMike Smith 	 * Initialise softc.
17235863739SMike Smith 	 */
17335863739SMike Smith 	sc = device_get_softc(dev);
17435863739SMike Smith 	bzero(sc, sizeof(*sc));
17535863739SMike Smith 	sc->aac_dev = dev;
17635863739SMike Smith 
17735863739SMike Smith 	/* assume failure is 'not configured' */
17835863739SMike Smith 	error = ENXIO;
17935863739SMike Smith 
18035863739SMike Smith 	/*
18135863739SMike Smith 	 * Verify that the adapter is correctly set up in PCI space.
18235863739SMike Smith 	 */
18335863739SMike Smith 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
18435863739SMike Smith 	command |= PCIM_CMD_BUSMASTEREN;
18535863739SMike Smith 	pci_write_config(dev, PCIR_COMMAND, command, 2);
18635863739SMike Smith 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
18735863739SMike Smith 	if (!(command & PCIM_CMD_BUSMASTEREN)) {
18835863739SMike Smith 		device_printf(sc->aac_dev, "can't enable bus-master feature\n");
18935863739SMike Smith 		goto out;
19035863739SMike Smith 	}
19135863739SMike Smith 	if ((command & PCIM_CMD_MEMEN) == 0) {
19235863739SMike Smith 		device_printf(sc->aac_dev, "memory window not available\n");
19335863739SMike Smith 		goto out;
19435863739SMike Smith 	}
19535863739SMike Smith 
19635863739SMike Smith 	/*
19735863739SMike Smith 	 * Allocate the PCI register window.
19835863739SMike Smith 	 */
19935863739SMike Smith 	sc->aac_regs_rid = 0x10;	/* first base address register */
200914da7d0SScott Long 	if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev,
201914da7d0SScott Long 							SYS_RES_MEMORY,
202914da7d0SScott Long 							&sc->aac_regs_rid,
203914da7d0SScott Long 							0, ~0, 1,
204c6eafcf2SScott Long 							RF_ACTIVE)) == NULL) {
205914da7d0SScott Long 		device_printf(sc->aac_dev,
206914da7d0SScott Long 			      "couldn't allocate register window\n");
20735863739SMike Smith 		goto out;
20835863739SMike Smith 	}
20935863739SMike Smith 	sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
21035863739SMike Smith 	sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
21135863739SMike Smith 
21235863739SMike Smith 	/*
21335863739SMike Smith 	 * Allocate and connect our interrupt.
21435863739SMike Smith 	 */
21535863739SMike Smith 	sc->aac_irq_rid = 0;
216c6eafcf2SScott Long 	if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ,
217c6eafcf2SScott Long 			   		      &sc->aac_irq_rid, 0, ~0, 1,
218914da7d0SScott Long 			   		      RF_SHAREABLE |
219914da7d0SScott Long 					      RF_ACTIVE)) == NULL) {
22035863739SMike Smith 		device_printf(sc->aac_dev, "can't allocate interrupt\n");
22135863739SMike Smith 		goto out;
22235863739SMike Smith 	}
2235e7e7ec9SScott Long #ifndef INTR_ENTROPY
22436e0bf6eSScott Long #define INTR_ENTROPY 0
22536e0bf6eSScott Long #endif
226c6eafcf2SScott Long 	if (bus_setup_intr(sc->aac_dev, sc->aac_irq, INTR_TYPE_BIO|INTR_ENTROPY,
227c6eafcf2SScott Long 			   aac_intr, sc, &sc->aac_intr)) {
22835863739SMike Smith 		device_printf(sc->aac_dev, "can't set up interrupt\n");
22935863739SMike Smith 		goto out;
23035863739SMike Smith 	}
23135863739SMike Smith 
23235863739SMike Smith 	/* assume failure is 'out of memory' */
23335863739SMike Smith 	error = ENOMEM;
23435863739SMike Smith 
23535863739SMike Smith 	/*
23635863739SMike Smith 	 * Allocate the parent bus DMA tag appropriate for our PCI interface.
23735863739SMike Smith 	 *
23835863739SMike Smith 	 * Note that some of these controllers are 64-bit capable.
23935863739SMike Smith 	 */
24035863739SMike Smith 	if (bus_dma_tag_create(NULL, 			/* parent */
241cbfd045bSScott Long 			       PAGE_SIZE, 0,		/* algnmnt, boundary */
24235863739SMike Smith 			       BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
24335863739SMike Smith 			       BUS_SPACE_MAXADDR, 	/* highaddr */
24435863739SMike Smith 			       NULL, NULL, 		/* filter, filterarg */
245914da7d0SScott Long 			       MAXBSIZE, 		/* maxsize */
246914da7d0SScott Long 			       AAC_MAXSGENTRIES,	/* nsegments */
24735863739SMike Smith 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
24835863739SMike Smith 			       BUS_DMA_ALLOCNOW,	/* flags */
24935863739SMike Smith 			       &sc->aac_parent_dmat)) {
25035863739SMike Smith 		device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
25135863739SMike Smith 		goto out;
25235863739SMike Smith 	}
25335863739SMike Smith 
25435863739SMike Smith 	/*
25535863739SMike Smith 	 * Create DMA tag for mapping buffers into controller-addressable space.
25635863739SMike Smith 	 */
25735863739SMike Smith 	if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
258c6eafcf2SScott Long 			   1, 0, 			/* algnmnt, boundary */
25935863739SMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
26035863739SMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
26135863739SMike Smith 			   NULL, NULL, 			/* filter, filterarg */
26235863739SMike Smith 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
26335863739SMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
26435863739SMike Smith 			   0,				/* flags */
26535863739SMike Smith 			   &sc->aac_buffer_dmat)) {
26635863739SMike Smith 		device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
26735863739SMike Smith 		goto out;
26835863739SMike Smith 	}
26935863739SMike Smith 
27035863739SMike Smith 	/*
27135863739SMike Smith 	 * Create DMA tag for mapping FIBs into controller-addressable space..
27235863739SMike Smith 	 */
27335863739SMike Smith 	if (bus_dma_tag_create(sc->aac_parent_dmat,	/* parent */
274c6eafcf2SScott Long 			   1, 0, 			/* algnmnt, boundary */
27535863739SMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
27635863739SMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
27735863739SMike Smith 			   NULL, NULL, 			/* filter, filterarg */
278c6eafcf2SScott Long 			   AAC_FIB_COUNT *
279c6eafcf2SScott Long 			   sizeof(struct aac_fib), 1,	/* maxsize, nsegments */
28035863739SMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
28135863739SMike Smith 			   0,				/* flags */
28235863739SMike Smith 			   &sc->aac_fib_dmat)) {
283fe3cb0e1SScott Long 		device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");;
28435863739SMike Smith 		goto out;
28535863739SMike Smith 	}
28635863739SMike Smith 
28735863739SMike Smith 	/*
288c6eafcf2SScott Long 	 * Detect the hardware interface version, set up the bus interface
289c6eafcf2SScott Long 	 * indirection.
29035863739SMike Smith 	 */
2910b94a66eSMike Smith 	sc->aac_hwif = AAC_HWIF_UNKNOWN;
29235863739SMike Smith 	for (i = 0; aac_identifiers[i].vendor != 0; i++) {
29335863739SMike Smith 		if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) &&
294fe3cb0e1SScott Long 		    (aac_identifiers[i].device == pci_get_device(dev)) &&
295fe3cb0e1SScott Long 		    (aac_identifiers[i].subvendor == pci_get_subvendor(dev)) &&
296fe3cb0e1SScott Long 		    (aac_identifiers[i].subdevice == pci_get_subdevice(dev))) {
29735863739SMike Smith 			sc->aac_hwif = aac_identifiers[i].hwif;
29835863739SMike Smith 			switch(sc->aac_hwif) {
29935863739SMike Smith 			case AAC_HWIF_I960RX:
30035863739SMike Smith 				debug(2, "set hardware up for i960Rx");
30135863739SMike Smith 				sc->aac_if = aac_rx_interface;
30235863739SMike Smith 				break;
30335863739SMike Smith 
30435863739SMike Smith 			case AAC_HWIF_STRONGARM:
30535863739SMike Smith 				debug(2, "set hardware up for StrongARM");
30635863739SMike Smith 				sc->aac_if = aac_sa_interface;
30735863739SMike Smith 				break;
308b3457b51SScott Long 			case AAC_HWIF_FALCON:
309b3457b51SScott Long 				debug(2, "set hardware up for Falcon/PPC");
310b3457b51SScott Long 				sc->aac_if = aac_fa_interface;
311b3457b51SScott Long 				break;
31235863739SMike Smith 			}
313fe3cb0e1SScott Long 
314fe3cb0e1SScott Long 			/* Set up quirks */
315fe3cb0e1SScott Long 			sc->quirks = aac_identifiers[i].quirks;
316fe3cb0e1SScott Long 
31735863739SMike Smith 			break;
31835863739SMike Smith 		}
31935863739SMike Smith 	}
3200b94a66eSMike Smith 	if (sc->aac_hwif == AAC_HWIF_UNKNOWN) {
3210b94a66eSMike Smith 		device_printf(sc->aac_dev, "unknown hardware type\n");
3220b94a66eSMike Smith 		error = ENXIO;
3230b94a66eSMike Smith 		goto out;
3240b94a66eSMike Smith 	}
32535863739SMike Smith 
326fe94b852SScott Long 
327fe94b852SScott Long 	/*
32835863739SMike Smith 	 * Do bus-independent initialisation.
32935863739SMike Smith 	 */
33035863739SMike Smith 	error = aac_attach(sc);
33135863739SMike Smith 
33235863739SMike Smith out:
33335863739SMike Smith 	if (error)
33435863739SMike Smith 		aac_free(sc);
33535863739SMike Smith 	return(error);
33635863739SMike Smith }
337