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