xref: /freebsd/sys/dev/aac/aac_pci.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2001 Scott Long
4  * Copyright (c) 2000 BSDi
5  * Copyright (c) 2001 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 "opt_aac.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 
42 #include <sys/bio.h>
43 #include <sys/bus.h>
44 #include <sys/conf.h>
45 #include <sys/disk.h>
46 
47 #include <machine/bus_memio.h>
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/rman.h>
51 
52 #include <pci/pcireg.h>
53 #include <pci/pcivar.h>
54 
55 #include <dev/aac/aacreg.h>
56 #include <dev/aac/aac_ioctl.h>
57 #include <dev/aac/aacvar.h>
58 
59 static int	aac_pci_probe(device_t dev);
60 static int	aac_pci_attach(device_t dev);
61 
62 static device_method_t aac_methods[] = {
63 	/* Device interface */
64 	DEVMETHOD(device_probe,		aac_pci_probe),
65 	DEVMETHOD(device_attach,	aac_pci_attach),
66 	DEVMETHOD(device_detach,	aac_detach),
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 static devclass_t	aac_devclass;
82 
83 DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
84 
85 struct aac_ident
86 {
87 	u_int16_t		vendor;
88 	u_int16_t		device;
89 	u_int16_t		subvendor;
90 	u_int16_t		subdevice;
91 	int			hwif;
92 	int			quirks;
93 	char			*desc;
94 } aac_identifiers[] = {
95 	{0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, 0,
96 	"Dell PERC 2/Si"},
97 	{0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, 0,
98 	"Dell PERC 3/Di"},
99 	{0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, 0,
100 	"Dell PERC 3/Si"},
101 	{0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, 0,
102 	"Dell PERC 3/Si"},
103 	{0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, 0,
104 	"Dell PERC 3/Di"},
105 	{0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, 0,
106 	"Dell PERC 3/Di"},
107 	{0x1028, 0x0008, 0x1028, 0x00cf, AAC_HWIF_I960RX, 0,
108 	"Dell PERC 3/Di"},
109 	{0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, 0,
110 	"Dell PERC 3/Di"},
111 	{0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, 0,
112 	"Dell PERC 3/Di"},
113 	{0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, 0,
114 	"Dell PERC 3/Di"},
115 	{0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, 0,
116 	"Adaptec AAC-364"},
117 	{0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM, 0,
118 	 "Adaptec SCSI RAID 5400S"},
119 	{0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_FLAGS_PERC2QC,
120 	 "Dell PERC 2/QC"},
121 	{0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, 0,
122 	 "HP NetRaid-4M"},
123 	{0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
124 	 AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2200S"},
125 	{0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
126 	 AAC_FLAGS_256FIBS, "Dell PERC 320/DC"},
127 	{0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
128 	 AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2120S"},
129 	{0, 0, 0, 0, 0, 0, 0}
130 };
131 
132 /*
133  * Determine whether this is one of our supported adapters.
134  */
135 static int
136 aac_pci_probe(device_t dev)
137 {
138 	struct aac_ident *m;
139 
140 	debug_called(1);
141 
142 	for (m = aac_identifiers; m->vendor != 0; m++) {
143 		if ((m->vendor == pci_get_vendor(dev)) &&
144 		    (m->device == pci_get_device(dev)) &&
145 		    ((m->subvendor == 0) || (m->subvendor ==
146 					     pci_get_subvendor(dev))) &&
147 		    ((m->subdevice == 0) || ((m->subdevice ==
148 					      pci_get_subdevice(dev))))) {
149 
150 			device_set_desc(dev, m->desc);
151 			return(-10);	/* allow room to be overridden */
152 		}
153 	}
154 	return(ENXIO);
155 }
156 
157 /*
158  * Allocate resources for our device, set up the bus interface.
159  */
160 static int
161 aac_pci_attach(device_t dev)
162 {
163 	struct aac_softc *sc;
164 	int i, error;
165 	u_int32_t command;
166 
167 	debug_called(1);
168 
169 	/*
170 	 * Initialise softc.
171 	 */
172 	sc = device_get_softc(dev);
173 	bzero(sc, sizeof(*sc));
174 	sc->aac_dev = dev;
175 
176 	/* assume failure is 'not configured' */
177 	error = ENXIO;
178 
179 	/*
180 	 * Verify that the adapter is correctly set up in PCI space.
181 	 */
182 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
183 	command |= PCIM_CMD_BUSMASTEREN;
184 	pci_write_config(dev, PCIR_COMMAND, command, 2);
185 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
186 	if (!(command & PCIM_CMD_BUSMASTEREN)) {
187 		device_printf(sc->aac_dev, "can't enable bus-master feature\n");
188 		goto out;
189 	}
190 	if ((command & PCIM_CMD_MEMEN) == 0) {
191 		device_printf(sc->aac_dev, "memory window not available\n");
192 		goto out;
193 	}
194 
195 	/*
196 	 * Allocate the PCI register window.
197 	 */
198 	sc->aac_regs_rid = 0x10;	/* first base address register */
199 	if ((sc->aac_regs_resource = bus_alloc_resource(sc->aac_dev,
200 							SYS_RES_MEMORY,
201 							&sc->aac_regs_rid,
202 							0, ~0, 1,
203 							RF_ACTIVE)) == NULL) {
204 		device_printf(sc->aac_dev,
205 			      "couldn't allocate register window\n");
206 		goto out;
207 	}
208 	sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
209 	sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
210 
211 	/*
212 	 * Allocate and connect our interrupt.
213 	 */
214 	sc->aac_irq_rid = 0;
215 	if ((sc->aac_irq = bus_alloc_resource(sc->aac_dev, SYS_RES_IRQ,
216 			   		      &sc->aac_irq_rid, 0, ~0, 1,
217 			   		      RF_SHAREABLE |
218 					      RF_ACTIVE)) == NULL) {
219 		device_printf(sc->aac_dev, "can't allocate interrupt\n");
220 		goto out;
221 	}
222 #ifndef INTR_ENTROPY
223 #define INTR_ENTROPY 0
224 #endif
225 	if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
226 			   INTR_MPSAFE|INTR_TYPE_BIO|INTR_ENTROPY, aac_intr,
227 			   sc, &sc->aac_intr)) {
228 		device_printf(sc->aac_dev, "can't set up interrupt\n");
229 		goto out;
230 	}
231 
232 	/* assume failure is 'out of memory' */
233 	error = ENOMEM;
234 
235 	/*
236 	 * Allocate the parent bus DMA tag appropriate for our PCI interface.
237 	 *
238 	 * Note that some of these controllers are 64-bit capable.
239 	 */
240 	if (bus_dma_tag_create(NULL, 			/* parent */
241 			       PAGE_SIZE, 0,		/* algnmnt, boundary */
242 			       BUS_SPACE_MAXADDR,	/* lowaddr */
243 			       BUS_SPACE_MAXADDR, 	/* highaddr */
244 			       NULL, NULL, 		/* filter, filterarg */
245 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
246 			       AAC_MAXSGENTRIES,	/* nsegments */
247 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
248 			       0,			/* flags */
249 			       &sc->aac_parent_dmat)) {
250 		device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
251 		goto out;
252 	}
253 
254 	/*
255 	 * Detect the hardware interface version, set up the bus interface
256 	 * indirection.
257 	 */
258 	sc->aac_hwif = AAC_HWIF_UNKNOWN;
259 	for (i = 0; aac_identifiers[i].vendor != 0; i++) {
260 		if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) &&
261 		    (aac_identifiers[i].device == pci_get_device(dev)) &&
262 		    (aac_identifiers[i].subvendor == pci_get_subvendor(dev)) &&
263 		    (aac_identifiers[i].subdevice == pci_get_subdevice(dev))) {
264 			sc->aac_hwif = aac_identifiers[i].hwif;
265 			switch(sc->aac_hwif) {
266 			case AAC_HWIF_I960RX:
267 				debug(2, "set hardware up for i960Rx");
268 				sc->aac_if = aac_rx_interface;
269 				break;
270 
271 			case AAC_HWIF_STRONGARM:
272 				debug(2, "set hardware up for StrongARM");
273 				sc->aac_if = aac_sa_interface;
274 				break;
275 			case AAC_HWIF_FALCON:
276 				debug(2, "set hardware up for Falcon/PPC");
277 				sc->aac_if = aac_fa_interface;
278 				break;
279 			}
280 
281 			/* Set up quirks */
282 			sc->flags = aac_identifiers[i].quirks;
283 
284 			break;
285 		}
286 	}
287 	if (sc->aac_hwif == AAC_HWIF_UNKNOWN) {
288 		device_printf(sc->aac_dev, "unknown hardware type\n");
289 		error = ENXIO;
290 		goto out;
291 	}
292 
293 
294 	/*
295 	 * Do bus-independent initialisation.
296 	 */
297 	error = aac_attach(sc);
298 
299 out:
300 	if (error)
301 		aac_free(sc);
302 	return(error);
303 }
304