xref: /freebsd/sys/dev/aac/aac_pci.c (revision 358637397e27108084efceb0c42bc0e5b05df024)
135863739SMike Smith /*-
235863739SMike Smith  * Copyright (c) 2000 Michael Smith
335863739SMike Smith  * Copyright (c) 2000 BSDi
435863739SMike Smith  * All rights reserved.
535863739SMike Smith  *
635863739SMike Smith  * Redistribution and use in source and binary forms, with or without
735863739SMike Smith  * modification, are permitted provided that the following conditions
835863739SMike Smith  * are met:
935863739SMike Smith  * 1. Redistributions of source code must retain the above copyright
1035863739SMike Smith  *    notice, this list of conditions and the following disclaimer.
1135863739SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1235863739SMike Smith  *    notice, this list of conditions and the following disclaimer in the
1335863739SMike Smith  *    documentation and/or other materials provided with the distribution.
1435863739SMike Smith  *
1535863739SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1635863739SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1735863739SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1835863739SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1935863739SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2035863739SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2135863739SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2235863739SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2335863739SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2435863739SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2535863739SMike Smith  * SUCH DAMAGE.
2635863739SMike Smith  *
2735863739SMike Smith  *	$FreeBSD$
2835863739SMike Smith  */
2935863739SMike Smith 
3035863739SMike Smith /*
3135863739SMike Smith  * PCI bus interface and resource allocation.
3235863739SMike Smith  */
3335863739SMike Smith 
3435863739SMike Smith #include <sys/param.h>
3535863739SMike Smith #include <sys/systm.h>
3635863739SMike Smith #include <sys/kernel.h>
3735863739SMike Smith 
3835863739SMike Smith #include <dev/aac/aac_compat.h>
3935863739SMike Smith #include <sys/bus.h>
4035863739SMike Smith #include <sys/conf.h>
4135863739SMike Smith #include <sys/devicestat.h>
4235863739SMike Smith #include <sys/disk.h>
4335863739SMike Smith 
4435863739SMike Smith #include <machine/bus_memio.h>
4535863739SMike Smith #include <machine/bus.h>
4635863739SMike Smith #include <machine/resource.h>
4735863739SMike Smith #include <sys/rman.h>
4835863739SMike Smith 
4935863739SMike Smith #include <pci/pcireg.h>
5035863739SMike Smith #include <pci/pcivar.h>
5135863739SMike Smith 
5235863739SMike Smith #include <dev/aac/aacreg.h>
5335863739SMike Smith #include <dev/aac/aacvar.h>
5435863739SMike Smith 
5535863739SMike Smith static int		aac_pci_probe(device_t dev);
5635863739SMike Smith static int		aac_pci_attach(device_t dev);
5735863739SMike Smith 
5835863739SMike Smith static device_method_t aac_methods[] = {
5935863739SMike Smith     /* Device interface */
6035863739SMike Smith     DEVMETHOD(device_probe,	aac_pci_probe),
6135863739SMike Smith     DEVMETHOD(device_attach,	aac_pci_attach),
6235863739SMike Smith     DEVMETHOD(device_detach,	aac_detach),
6335863739SMike Smith     DEVMETHOD(device_shutdown,	aac_shutdown),
6435863739SMike Smith     DEVMETHOD(device_suspend,	aac_suspend),
6535863739SMike Smith     DEVMETHOD(device_resume,	aac_resume),
6635863739SMike Smith 
6735863739SMike Smith     DEVMETHOD(bus_print_child,	bus_generic_print_child),
6835863739SMike Smith     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
6935863739SMike Smith     { 0, 0 }
7035863739SMike Smith };
7135863739SMike Smith 
7235863739SMike Smith static driver_t aac_pci_driver = {
7335863739SMike Smith 	"aac",
7435863739SMike Smith 	aac_methods,
7535863739SMike Smith 	sizeof(struct aac_softc)
7635863739SMike Smith };
7735863739SMike Smith 
7835863739SMike Smith DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
7935863739SMike Smith 
8035863739SMike Smith struct aac_ident
8135863739SMike Smith {
8235863739SMike Smith     u_int16_t		vendor;
8335863739SMike Smith     u_int16_t		device;
8435863739SMike Smith     u_int16_t		subvendor;
8535863739SMike Smith     u_int16_t		subdevice;
8635863739SMike Smith     int			hwif;
8735863739SMike Smith     char		*desc;
8835863739SMike Smith } aac_identifiers[] = {
8935863739SMike Smith     {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX,    "Dell PERC 2/Si"},
9035863739SMike Smith     {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX,    "Dell PERC 3/Di"},
9135863739SMike Smith     {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX,    "Dell PERC 3/Si"},
9235863739SMike Smith     {0x9005, 0x0282, 0x9005, 0x0282, AAC_HWIF_I960RX,    "Adaptec AAC-2622"},
9335863739SMike Smith     {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, "Adaptec AAC-364"},
9435863739SMike Smith     {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, "Adaptec AAC-3642"},
9535863739SMike Smith     {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, "Dell PERC 2/QC"},
9635863739SMike Smith     {0x1011, 0x0046, 0x9005, 0x1365, AAC_HWIF_STRONGARM, "Dell PERC 3/QC"},	/* XXX guess */
9735863739SMike Smith     {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, "HP NetRaid-4M"},
9835863739SMike Smith     {0, 0, 0, 0, 0, 0}
9935863739SMike Smith };
10035863739SMike Smith 
10135863739SMike Smith /********************************************************************************
10235863739SMike Smith  * Determine whether this is one of our supported adapters.
10335863739SMike Smith  */
10435863739SMike Smith static int
10535863739SMike Smith aac_pci_probe(device_t dev)
10635863739SMike Smith {
10735863739SMike Smith     struct aac_ident	*m;
10835863739SMike Smith 
10935863739SMike Smith     debug_called(1);
11035863739SMike Smith 
11135863739SMike Smith     for (m = aac_identifiers; m->vendor != 0; m++) {
11235863739SMike Smith 	if ((m->vendor == pci_get_vendor(dev)) &&
11335863739SMike Smith 	    (m->device == pci_get_device(dev)) &&
11435863739SMike Smith 	    ((m->subvendor == 0) || (m->subvendor == pci_get_subvendor(dev))) &&
11535863739SMike Smith 	    ((m->subdevice == 0) || ((m->subdevice == pci_get_subdevice(dev))))) {
11635863739SMike Smith 
11735863739SMike Smith 	    device_set_desc(dev, m->desc);
11835863739SMike Smith 	    return(0);
11935863739SMike Smith 	}
12035863739SMike Smith     }
12135863739SMike Smith     return(ENXIO);
12235863739SMike Smith }
12335863739SMike Smith 
12435863739SMike Smith /********************************************************************************
12535863739SMike Smith  * Allocate resources for our device, set up the bus interface.
12635863739SMike Smith  */
12735863739SMike Smith static int
12835863739SMike Smith aac_pci_attach(device_t dev)
12935863739SMike Smith {
13035863739SMike Smith     struct aac_softc	*sc;
13135863739SMike Smith     int			i, error;
13235863739SMike Smith     u_int32_t		command;
13335863739SMike Smith 
13435863739SMike Smith     debug_called(1);
13535863739SMike Smith 
13635863739SMike Smith     /*
13735863739SMike Smith      * Initialise softc.
13835863739SMike Smith      */
13935863739SMike Smith     sc = device_get_softc(dev);
14035863739SMike Smith     bzero(sc, sizeof(*sc));
14135863739SMike Smith     sc->aac_dev = dev;
14235863739SMike Smith 
14335863739SMike Smith     /* assume failure is 'not configured' */
14435863739SMike Smith     error = ENXIO;
14535863739SMike Smith 
14635863739SMike Smith     /*
14735863739SMike Smith      * Verify that the adapter is correctly set up in PCI space.
14835863739SMike Smith      */
14935863739SMike Smith     command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
15035863739SMike Smith     command |= PCIM_CMD_BUSMASTEREN;
15135863739SMike Smith     pci_write_config(dev, PCIR_COMMAND, command, 2);
15235863739SMike Smith     command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
15335863739SMike Smith     if (!(command & PCIM_CMD_BUSMASTEREN)) {
15435863739SMike Smith 	device_printf(sc->aac_dev, "can't enable bus-master feature\n");
15535863739SMike Smith 	goto out;
15635863739SMike Smith     }
15735863739SMike Smith     if ((command & PCIM_CMD_MEMEN) == 0) {
15835863739SMike Smith 	device_printf(sc->aac_dev, "memory window not available\n");
15935863739SMike Smith 	goto out;
16035863739SMike Smith     }
16135863739SMike Smith 
16235863739SMike Smith     /*
16335863739SMike Smith      * Allocate the PCI register window.
16435863739SMike Smith      */
16535863739SMike Smith     sc->aac_regs_rid = 0x10;	/* first base address register */
16635863739SMike Smith     if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid,
16735863739SMike Smith 						    0, ~0, 1, RF_ACTIVE)) == NULL) {
16835863739SMike Smith 	device_printf(sc->aac_dev, "couldn't allocate register window\n");
16935863739SMike Smith 	goto out;
17035863739SMike Smith     }
17135863739SMike Smith     sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
17235863739SMike Smith     sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
17335863739SMike Smith 
17435863739SMike Smith     /*
17535863739SMike Smith      * Allocate and connect our interrupt.
17635863739SMike Smith      */
17735863739SMike Smith     sc->aac_irq_rid = 0;
17835863739SMike Smith     if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ, &sc->aac_irq_rid,
17935863739SMike Smith 					  0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
18035863739SMike Smith 	device_printf(sc->aac_dev, "can't allocate interrupt\n");
18135863739SMike Smith 	goto out;
18235863739SMike Smith     }
18335863739SMike Smith     if (bus_setup_intr(sc->aac_dev, sc->aac_irq, INTR_TYPE_BIO,  aac_intr, sc, &sc->aac_intr)) {
18435863739SMike Smith 	device_printf(sc->aac_dev, "can't set up interrupt\n");
18535863739SMike Smith 	goto out;
18635863739SMike Smith     }
18735863739SMike Smith 
18835863739SMike Smith     /* assume failure is 'out of memory' */
18935863739SMike Smith     error = ENOMEM;
19035863739SMike Smith 
19135863739SMike Smith     /*
19235863739SMike Smith      * Allocate the parent bus DMA tag appropriate for our PCI interface.
19335863739SMike Smith      *
19435863739SMike Smith      * Note that some of these controllers are 64-bit capable.
19535863739SMike Smith      */
19635863739SMike Smith     if (bus_dma_tag_create(NULL, 			/* parent */
19735863739SMike Smith 			   1, 0, 			/* alignment, boundary */
19835863739SMike Smith 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
19935863739SMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
20035863739SMike Smith 			   NULL, NULL, 			/* filter, filterarg */
20135863739SMike Smith 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
20235863739SMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
20335863739SMike Smith 			   BUS_DMA_ALLOCNOW,		/* flags */
20435863739SMike Smith 			   &sc->aac_parent_dmat)) {
20535863739SMike Smith 	device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
20635863739SMike Smith 	goto out;
20735863739SMike Smith     }
20835863739SMike Smith 
20935863739SMike Smith     /*
21035863739SMike Smith      * Create DMA tag for mapping buffers into controller-addressable space.
21135863739SMike Smith      */
21235863739SMike Smith     if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
21335863739SMike Smith 			   1, 0, 			/* alignment, boundary */
21435863739SMike Smith 			   BUS_SPACE_MAXADDR,		/* lowaddr */
21535863739SMike Smith 			   BUS_SPACE_MAXADDR, 		/* highaddr */
21635863739SMike Smith 			   NULL, NULL, 			/* filter, filterarg */
21735863739SMike Smith 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
21835863739SMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
21935863739SMike Smith 			   0,				/* flags */
22035863739SMike Smith 			   &sc->aac_buffer_dmat)) {
22135863739SMike Smith 	device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
22235863739SMike Smith 	goto out;
22335863739SMike Smith     }
22435863739SMike Smith 
22535863739SMike Smith     /*
22635863739SMike Smith      * Create DMA tag for mapping FIBs into controller-addressable space..
22735863739SMike Smith      */
22835863739SMike Smith     if (bus_dma_tag_create(sc->aac_parent_dmat, 			/* parent */
22935863739SMike Smith 			   1, 0, 					/* alignment, boundary */
23035863739SMike Smith 			   BUS_SPACE_MAXADDR,				/* lowaddr */
23135863739SMike Smith 			   BUS_SPACE_MAXADDR, 				/* highaddr */
23235863739SMike Smith 			   NULL, NULL, 					/* filter, filterarg */
23335863739SMike Smith 			   AAC_CLUSTER_COUNT * sizeof(struct aac_fib), 1,/* maxsize, nsegments */
23435863739SMike Smith 			   BUS_SPACE_MAXSIZE_32BIT,			/* maxsegsize */
23535863739SMike Smith 			   0,						/* flags */
23635863739SMike Smith 			   &sc->aac_fib_dmat)) {
23735863739SMike Smith 	device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
23835863739SMike Smith 	goto out;
23935863739SMike Smith     }
24035863739SMike Smith 
24135863739SMike Smith     /*
24235863739SMike Smith      * Detect the hardware interface version, set up the bus interface indirection.
24335863739SMike Smith      */
24435863739SMike Smith     for (i = 0; aac_identifiers[i].vendor != 0; i++) {
24535863739SMike Smith 	if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) &&
24635863739SMike Smith 	    (aac_identifiers[i].device == pci_get_device(dev))) {
24735863739SMike Smith 	    sc->aac_hwif = aac_identifiers[i].hwif;
24835863739SMike Smith 	    switch(sc->aac_hwif) {
24935863739SMike Smith 	    case AAC_HWIF_I960RX:
25035863739SMike Smith 		debug(2, "set hardware up for i960Rx");
25135863739SMike Smith 		sc->aac_if = aac_rx_interface;
25235863739SMike Smith 		break;
25335863739SMike Smith 
25435863739SMike Smith 	    case AAC_HWIF_STRONGARM:
25535863739SMike Smith 		debug(2, "set hardware up for StrongARM");
25635863739SMike Smith 		sc->aac_if = aac_sa_interface;
25735863739SMike Smith 		break;
25835863739SMike Smith 	    }
25935863739SMike Smith 	    break;
26035863739SMike Smith 	}
26135863739SMike Smith     }
26235863739SMike Smith 
26335863739SMike Smith     /*
26435863739SMike Smith      * Do bus-independent initialisation.
26535863739SMike Smith      */
26635863739SMike Smith     error = aac_attach(sc);
26735863739SMike Smith 
26835863739SMike Smith out:
26935863739SMike Smith     if (error)
27035863739SMike Smith 	aac_free(sc);
27135863739SMike Smith     return(error);
27235863739SMike Smith }
273