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