xref: /freebsd/sys/dev/aac/aac_pci.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 /*
31  * PCI bus interface and resource allocation.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 
38 #include <dev/aac/aac_compat.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/devicestat.h>
42 #include <sys/disk.h>
43 
44 #include <machine/bus_memio.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <sys/rman.h>
48 
49 #include <pci/pcireg.h>
50 #include <pci/pcivar.h>
51 
52 #include <dev/aac/aacreg.h>
53 #include <dev/aac/aacvar.h>
54 
55 static int		aac_pci_probe(device_t dev);
56 static int		aac_pci_attach(device_t dev);
57 
58 static device_method_t aac_methods[] = {
59     /* Device interface */
60     DEVMETHOD(device_probe,	aac_pci_probe),
61     DEVMETHOD(device_attach,	aac_pci_attach),
62     DEVMETHOD(device_detach,	aac_detach),
63     DEVMETHOD(device_shutdown,	aac_shutdown),
64     DEVMETHOD(device_suspend,	aac_suspend),
65     DEVMETHOD(device_resume,	aac_resume),
66 
67     DEVMETHOD(bus_print_child,	bus_generic_print_child),
68     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
69     { 0, 0 }
70 };
71 
72 static driver_t aac_pci_driver = {
73 	"aac",
74 	aac_methods,
75 	sizeof(struct aac_softc)
76 };
77 
78 DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
79 
80 struct aac_ident
81 {
82     u_int16_t		vendor;
83     u_int16_t		device;
84     u_int16_t		subvendor;
85     u_int16_t		subdevice;
86     int			hwif;
87     char		*desc;
88 } aac_identifiers[] = {
89     {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX,    "Dell PERC 2/Si"},
90     {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX,    "Dell PERC 3/Di"},
91     {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX,    "Dell PERC 3/Si"},
92     {0x9005, 0x0282, 0x9005, 0x0282, AAC_HWIF_I960RX,    "Adaptec AAC-2622"},
93     {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, "Adaptec AAC-364"},
94     {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, "Adaptec AAC-3642"},
95     {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, "Dell PERC 2/QC"},
96     {0x1011, 0x0046, 0x9005, 0x1365, AAC_HWIF_STRONGARM, "Dell PERC 3/QC"},	/* XXX guess */
97     {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, "HP NetRaid-4M"},
98     {0, 0, 0, 0, 0, 0}
99 };
100 
101 /********************************************************************************
102  * Determine whether this is one of our supported adapters.
103  */
104 static int
105 aac_pci_probe(device_t dev)
106 {
107     struct aac_ident	*m;
108 
109     debug_called(1);
110 
111     for (m = aac_identifiers; m->vendor != 0; m++) {
112 	if ((m->vendor == pci_get_vendor(dev)) &&
113 	    (m->device == pci_get_device(dev)) &&
114 	    ((m->subvendor == 0) || (m->subvendor == pci_get_subvendor(dev))) &&
115 	    ((m->subdevice == 0) || ((m->subdevice == pci_get_subdevice(dev))))) {
116 
117 	    device_set_desc(dev, m->desc);
118 	    return(-10);	/* allow room to be overridden */
119 	}
120     }
121     return(ENXIO);
122 }
123 
124 /********************************************************************************
125  * Allocate resources for our device, set up the bus interface.
126  */
127 static int
128 aac_pci_attach(device_t dev)
129 {
130     struct aac_softc	*sc;
131     int			i, error;
132     u_int32_t		command;
133 
134     debug_called(1);
135 
136     /*
137      * Initialise softc.
138      */
139     sc = device_get_softc(dev);
140     bzero(sc, sizeof(*sc));
141     sc->aac_dev = dev;
142 
143     /* assume failure is 'not configured' */
144     error = ENXIO;
145 
146     /*
147      * Verify that the adapter is correctly set up in PCI space.
148      */
149     command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
150     command |= PCIM_CMD_BUSMASTEREN;
151     pci_write_config(dev, PCIR_COMMAND, command, 2);
152     command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
153     if (!(command & PCIM_CMD_BUSMASTEREN)) {
154 	device_printf(sc->aac_dev, "can't enable bus-master feature\n");
155 	goto out;
156     }
157     if ((command & PCIM_CMD_MEMEN) == 0) {
158 	device_printf(sc->aac_dev, "memory window not available\n");
159 	goto out;
160     }
161 
162     /*
163      * Allocate the PCI register window.
164      */
165     sc->aac_regs_rid = 0x10;	/* first base address register */
166     if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid,
167 						    0, ~0, 1, RF_ACTIVE)) == NULL) {
168 	device_printf(sc->aac_dev, "couldn't allocate register window\n");
169 	goto out;
170     }
171     sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
172     sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
173 
174     /*
175      * Allocate and connect our interrupt.
176      */
177     sc->aac_irq_rid = 0;
178     if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ, &sc->aac_irq_rid,
179 					  0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
180 	device_printf(sc->aac_dev, "can't allocate interrupt\n");
181 	goto out;
182     }
183     if (bus_setup_intr(sc->aac_dev, sc->aac_irq, INTR_TYPE_BIO,  aac_intr, sc, &sc->aac_intr)) {
184 	device_printf(sc->aac_dev, "can't set up interrupt\n");
185 	goto out;
186     }
187 
188     /* assume failure is 'out of memory' */
189     error = ENOMEM;
190 
191     /*
192      * Allocate the parent bus DMA tag appropriate for our PCI interface.
193      *
194      * Note that some of these controllers are 64-bit capable.
195      */
196     if (bus_dma_tag_create(NULL, 			/* parent */
197 			   1, 0, 			/* alignment, boundary */
198 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
199 			   BUS_SPACE_MAXADDR, 		/* highaddr */
200 			   NULL, NULL, 			/* filter, filterarg */
201 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
202 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
203 			   BUS_DMA_ALLOCNOW,		/* flags */
204 			   &sc->aac_parent_dmat)) {
205 	device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
206 	goto out;
207     }
208 
209     /*
210      * Create DMA tag for mapping buffers into controller-addressable space.
211      */
212     if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
213 			   1, 0, 			/* alignment, boundary */
214 			   BUS_SPACE_MAXADDR,		/* lowaddr */
215 			   BUS_SPACE_MAXADDR, 		/* highaddr */
216 			   NULL, NULL, 			/* filter, filterarg */
217 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
218 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
219 			   0,				/* flags */
220 			   &sc->aac_buffer_dmat)) {
221 	device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
222 	goto out;
223     }
224 
225     /*
226      * Create DMA tag for mapping FIBs into controller-addressable space..
227      */
228     if (bus_dma_tag_create(sc->aac_parent_dmat, 			/* parent */
229 			   1, 0, 					/* alignment, boundary */
230 			   BUS_SPACE_MAXADDR,				/* lowaddr */
231 			   BUS_SPACE_MAXADDR, 				/* highaddr */
232 			   NULL, NULL, 					/* filter, filterarg */
233 			   AAC_CLUSTER_COUNT * sizeof(struct aac_fib), 1,/* maxsize, nsegments */
234 			   BUS_SPACE_MAXSIZE_32BIT,			/* maxsegsize */
235 			   0,						/* flags */
236 			   &sc->aac_fib_dmat)) {
237 	device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
238 	goto out;
239     }
240 
241     /*
242      * Detect the hardware interface version, set up the bus interface indirection.
243      */
244     for (i = 0; aac_identifiers[i].vendor != 0; i++) {
245 	if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) &&
246 	    (aac_identifiers[i].device == pci_get_device(dev))) {
247 	    sc->aac_hwif = aac_identifiers[i].hwif;
248 	    switch(sc->aac_hwif) {
249 	    case AAC_HWIF_I960RX:
250 		debug(2, "set hardware up for i960Rx");
251 		sc->aac_if = aac_rx_interface;
252 		break;
253 
254 	    case AAC_HWIF_STRONGARM:
255 		debug(2, "set hardware up for StrongARM");
256 		sc->aac_if = aac_sa_interface;
257 		break;
258 	    }
259 	    break;
260 	}
261     }
262 
263     /*
264      * Do bus-independent initialisation.
265      */
266     error = aac_attach(sc);
267 
268 out:
269     if (error)
270 	aac_free(sc);
271     return(error);
272 }
273