xref: /freebsd/sys/dev/aac/aac_pci.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
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/aac_ioctl.h>
54 #include <dev/aac/aacvar.h>
55 
56 static int		aac_pci_probe(device_t dev);
57 static int		aac_pci_attach(device_t dev);
58 
59 static device_method_t aac_methods[] = {
60     /* Device interface */
61     DEVMETHOD(device_probe,	aac_pci_probe),
62     DEVMETHOD(device_attach,	aac_pci_attach),
63     DEVMETHOD(device_detach,	aac_detach),
64     DEVMETHOD(device_shutdown,	aac_shutdown),
65     DEVMETHOD(device_suspend,	aac_suspend),
66     DEVMETHOD(device_resume,	aac_resume),
67 
68     DEVMETHOD(bus_print_child,	bus_generic_print_child),
69     DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
70     { 0, 0 }
71 };
72 
73 static driver_t aac_pci_driver = {
74 	"aac",
75 	aac_methods,
76 	sizeof(struct aac_softc)
77 };
78 
79 DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
80 
81 struct aac_ident
82 {
83     u_int16_t		vendor;
84     u_int16_t		device;
85     u_int16_t		subvendor;
86     u_int16_t		subdevice;
87     int			hwif;
88     char		*desc;
89 } aac_identifiers[] = {
90     {0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX,    "Dell PERC 2/Si"},
91     {0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX,    "Dell PERC 3/Di"},
92     {0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX,    "Dell PERC 3/Si"},
93     {0x9005, 0x0282, 0x9005, 0x0282, AAC_HWIF_I960RX,    "Adaptec AAC-2622"},
94     {0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, "Adaptec AAC-364"},
95     {0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, "Adaptec AAC-3642"},
96     {0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, "Dell PERC 2/QC"},
97     {0x1011, 0x0046, 0x9005, 0x1365, AAC_HWIF_STRONGARM, "Dell PERC 3/QC"},	/* XXX guess */
98     {0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, "HP NetRaid-4M"},
99     {0, 0, 0, 0, 0, 0}
100 };
101 
102 /********************************************************************************
103  * Determine whether this is one of our supported adapters.
104  */
105 static int
106 aac_pci_probe(device_t dev)
107 {
108     struct aac_ident	*m;
109 
110     debug_called(1);
111 
112     for (m = aac_identifiers; m->vendor != 0; m++) {
113 	if ((m->vendor == pci_get_vendor(dev)) &&
114 	    (m->device == pci_get_device(dev)) &&
115 	    ((m->subvendor == 0) || (m->subvendor == pci_get_subvendor(dev))) &&
116 	    ((m->subdevice == 0) || ((m->subdevice == pci_get_subdevice(dev))))) {
117 
118 	    device_set_desc(dev, m->desc);
119 	    return(-10);	/* allow room to be overridden */
120 	}
121     }
122     return(ENXIO);
123 }
124 
125 /********************************************************************************
126  * Allocate resources for our device, set up the bus interface.
127  */
128 static int
129 aac_pci_attach(device_t dev)
130 {
131     struct aac_softc	*sc;
132     int			i, error;
133     u_int32_t		command;
134 
135     debug_called(1);
136 
137     /*
138      * Initialise softc.
139      */
140     sc = device_get_softc(dev);
141     bzero(sc, sizeof(*sc));
142     sc->aac_dev = dev;
143 
144     /* assume failure is 'not configured' */
145     error = ENXIO;
146 
147     /*
148      * Verify that the adapter is correctly set up in PCI space.
149      */
150     command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
151     command |= PCIM_CMD_BUSMASTEREN;
152     pci_write_config(dev, PCIR_COMMAND, command, 2);
153     command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
154     if (!(command & PCIM_CMD_BUSMASTEREN)) {
155 	device_printf(sc->aac_dev, "can't enable bus-master feature\n");
156 	goto out;
157     }
158     if ((command & PCIM_CMD_MEMEN) == 0) {
159 	device_printf(sc->aac_dev, "memory window not available\n");
160 	goto out;
161     }
162 
163     /*
164      * Allocate the PCI register window.
165      */
166     sc->aac_regs_rid = 0x10;	/* first base address register */
167     if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev, SYS_RES_MEMORY, &sc->aac_regs_rid,
168 						    0, ~0, 1, RF_ACTIVE)) == NULL) {
169 	device_printf(sc->aac_dev, "couldn't allocate register window\n");
170 	goto out;
171     }
172     sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
173     sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
174 
175     /*
176      * Allocate and connect our interrupt.
177      */
178     sc->aac_irq_rid = 0;
179     if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ, &sc->aac_irq_rid,
180 					  0, ~0, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL) {
181 	device_printf(sc->aac_dev, "can't allocate interrupt\n");
182 	goto out;
183     }
184     if (bus_setup_intr(sc->aac_dev, sc->aac_irq, INTR_TYPE_BIO,  aac_intr, sc, &sc->aac_intr)) {
185 	device_printf(sc->aac_dev, "can't set up interrupt\n");
186 	goto out;
187     }
188 
189     /* assume failure is 'out of memory' */
190     error = ENOMEM;
191 
192     /*
193      * Allocate the parent bus DMA tag appropriate for our PCI interface.
194      *
195      * Note that some of these controllers are 64-bit capable.
196      */
197     if (bus_dma_tag_create(NULL, 			/* parent */
198 			   1, 0, 			/* alignment, boundary */
199 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
200 			   BUS_SPACE_MAXADDR, 		/* highaddr */
201 			   NULL, NULL, 			/* filter, filterarg */
202 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
203 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
204 			   BUS_DMA_ALLOCNOW,		/* flags */
205 			   &sc->aac_parent_dmat)) {
206 	device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
207 	goto out;
208     }
209 
210     /*
211      * Create DMA tag for mapping buffers into controller-addressable space.
212      */
213     if (bus_dma_tag_create(sc->aac_parent_dmat, 	/* parent */
214 			   1, 0, 			/* alignment, boundary */
215 			   BUS_SPACE_MAXADDR,		/* lowaddr */
216 			   BUS_SPACE_MAXADDR, 		/* highaddr */
217 			   NULL, NULL, 			/* filter, filterarg */
218 			   MAXBSIZE, AAC_MAXSGENTRIES,	/* maxsize, nsegments */
219 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
220 			   0,				/* flags */
221 			   &sc->aac_buffer_dmat)) {
222 	device_printf(sc->aac_dev, "can't allocate buffer DMA tag\n");
223 	goto out;
224     }
225 
226     /*
227      * Create DMA tag for mapping FIBs into controller-addressable space..
228      */
229     if (bus_dma_tag_create(sc->aac_parent_dmat, 			/* parent */
230 			   1, 0, 					/* alignment, boundary */
231 			   BUS_SPACE_MAXADDR,				/* lowaddr */
232 			   BUS_SPACE_MAXADDR, 				/* highaddr */
233 			   NULL, NULL, 					/* filter, filterarg */
234 			   AAC_FIB_COUNT * sizeof(struct aac_fib), 1,	/* maxsize, nsegments */
235 			   BUS_SPACE_MAXSIZE_32BIT,			/* maxsegsize */
236 			   0,						/* flags */
237 			   &sc->aac_fib_dmat)) {
238 	device_printf(sc->aac_dev, "can't allocate FIB DMA tag\n");
239 	goto out;
240     }
241 
242     /*
243      * Detect the hardware interface version, set up the bus interface indirection.
244      */
245     sc->aac_hwif = AAC_HWIF_UNKNOWN;
246     for (i = 0; aac_identifiers[i].vendor != 0; i++) {
247 	if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) &&
248 	    (aac_identifiers[i].device == pci_get_device(dev))) {
249 	    sc->aac_hwif = aac_identifiers[i].hwif;
250 	    switch(sc->aac_hwif) {
251 	    case AAC_HWIF_I960RX:
252 		debug(2, "set hardware up for i960Rx");
253 		sc->aac_if = aac_rx_interface;
254 		break;
255 
256 	    case AAC_HWIF_STRONGARM:
257 		debug(2, "set hardware up for StrongARM");
258 		sc->aac_if = aac_sa_interface;
259 		break;
260 	    }
261 	    break;
262 	}
263     }
264     if (sc->aac_hwif == AAC_HWIF_UNKNOWN) {
265 	device_printf(sc->aac_dev, "unknown hardware type\n");
266 	error = ENXIO;
267 	goto out;
268     }
269 
270     /*
271      * Do bus-independent initialisation.
272      */
273     error = aac_attach(sc);
274 
275 out:
276     if (error)
277 	aac_free(sc);
278     return(error);
279 }
280