xref: /freebsd/sys/dev/aac/aac_pci.c (revision 7afc53b8dfcc7d5897920ce6cc7e842fbb4ab813)
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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * PCI bus interface and resource allocation.
35  */
36 
37 #include "opt_aac.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 
44 #include <sys/bio.h>
45 #include <sys/bus.h>
46 #include <sys/conf.h>
47 #include <sys/disk.h>
48 
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <sys/rman.h>
52 
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
55 
56 #include <dev/aac/aacreg.h>
57 #include <sys/aac_ioctl.h>
58 #include <dev/aac/aacvar.h>
59 
60 static int	aac_pci_probe(device_t dev);
61 static int	aac_pci_attach(device_t dev);
62 
63 static device_method_t aac_methods[] = {
64 	/* Device interface */
65 	DEVMETHOD(device_probe,		aac_pci_probe),
66 	DEVMETHOD(device_attach,	aac_pci_attach),
67 	DEVMETHOD(device_detach,	aac_detach),
68 	DEVMETHOD(device_suspend,	aac_suspend),
69 	DEVMETHOD(device_resume,	aac_resume),
70 
71 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
72 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
73 	{ 0, 0 }
74 };
75 
76 static driver_t aac_pci_driver = {
77 	"aac",
78 	aac_methods,
79 	sizeof(struct aac_softc)
80 };
81 
82 static devclass_t	aac_devclass;
83 
84 DRIVER_MODULE(aac, pci, aac_pci_driver, aac_devclass, 0, 0);
85 
86 struct aac_ident
87 {
88 	u_int16_t		vendor;
89 	u_int16_t		device;
90 	u_int16_t		subvendor;
91 	u_int16_t		subdevice;
92 	int			hwif;
93 	int			quirks;
94 	char			*desc;
95 } aac_identifiers[] = {
96 	{0x1028, 0x0001, 0x1028, 0x0001, AAC_HWIF_I960RX, 0,
97 	"Dell PERC 2/Si"},
98 	{0x1028, 0x0002, 0x1028, 0x0002, AAC_HWIF_I960RX, 0,
99 	"Dell PERC 3/Di"},
100 	{0x1028, 0x0003, 0x1028, 0x0003, AAC_HWIF_I960RX, 0,
101 	"Dell PERC 3/Si"},
102 	{0x1028, 0x0004, 0x1028, 0x00d0, AAC_HWIF_I960RX, 0,
103 	"Dell PERC 3/Si"},
104 	{0x1028, 0x0002, 0x1028, 0x00d1, AAC_HWIF_I960RX, 0,
105 	"Dell PERC 3/Di"},
106 	{0x1028, 0x0002, 0x1028, 0x00d9, AAC_HWIF_I960RX, 0,
107 	"Dell PERC 3/Di"},
108 	{0x1028, 0x000a, 0x1028, 0x0106, AAC_HWIF_I960RX, 0,
109 	"Dell PERC 3/Di"},
110 	{0x1028, 0x000a, 0x1028, 0x011b, AAC_HWIF_I960RX, 0,
111 	"Dell PERC 3/Di"},
112 	{0x1028, 0x000a, 0x1028, 0x0121, AAC_HWIF_I960RX, 0,
113 	"Dell PERC 3/Di"},
114 	{0x1011, 0x0046, 0x9005, 0x0364, AAC_HWIF_STRONGARM, 0,
115 	"Adaptec AAC-364"},
116 	{0x1011, 0x0046, 0x9005, 0x0365, AAC_HWIF_STRONGARM,
117 	 AAC_FLAGS_BROKEN_MEMMAP, "Adaptec SCSI RAID 5400S"},
118 	{0x1011, 0x0046, 0x9005, 0x1364, AAC_HWIF_STRONGARM, AAC_FLAGS_PERC2QC,
119 	 "Dell PERC 2/QC"},
120 	{0x1011, 0x0046, 0x103c, 0x10c2, AAC_HWIF_STRONGARM, 0,
121 	 "HP NetRaid-4M"},
122 	{0x9005, 0x0285, 0x9005, 0x0285, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
123 	 AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2200S"},
124 	{0x9005, 0x0285, 0x1028, 0x0287, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
125 	 AAC_FLAGS_256FIBS, "Dell PERC 320/DC"},
126 	{0x9005, 0x0285, 0x9005, 0x0286, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB |
127 	 AAC_FLAGS_256FIBS, "Adaptec SCSI RAID 2120S"},
128 	{0x9005, 0x0285, 0x9005, 0x0290, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
129 	 "Adaptec SCSI RAID 2410SA"},
130 	{0x9005, 0x0285, 0x1028, 0x0291, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
131 	 "Dell CERC SATA RAID 2"},
132 	{0x9005, 0x0285, 0x9005, 0x0292, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
133 	 "Adaptec SCSI RAID 2810SA"},
134 	{0x9005, 0x0285, 0x9005, 0x0293, AAC_HWIF_I960RX, AAC_FLAGS_NO4GB,
135 	 "Adaptec SCSI RAID 21610SA"},
136 	{0x9005, 0x0286, 0x9005, 0x028c, AAC_HWIF_RKT, 0,
137 	 "Adaptec SCSI RAID 2230S"},
138 	{0x9005, 0x0286, 0x9005, 0x028d, AAC_HWIF_RKT, 0,
139 	 "Adaptec SCSI RAID 2130S"},
140 	{0, 0, 0, 0, 0, 0, 0}
141 };
142 
143 /*
144  * Determine whether this is one of our supported adapters.
145  */
146 static int
147 aac_pci_probe(device_t dev)
148 {
149 	struct aac_ident *m;
150 
151 	debug_called(1);
152 
153 	for (m = aac_identifiers; m->vendor != 0; m++) {
154 		if ((m->vendor == pci_get_vendor(dev)) &&
155 		    (m->device == pci_get_device(dev)) &&
156 		    ((m->subvendor == 0) || (m->subvendor ==
157 					     pci_get_subvendor(dev))) &&
158 		    ((m->subdevice == 0) || ((m->subdevice ==
159 					      pci_get_subdevice(dev))))) {
160 
161 			device_set_desc(dev, m->desc);
162 			return(BUS_PROBE_DEFAULT);
163 		}
164 	}
165 	return(ENXIO);
166 }
167 
168 /*
169  * Allocate resources for our device, set up the bus interface.
170  */
171 static int
172 aac_pci_attach(device_t dev)
173 {
174 	struct aac_softc *sc;
175 	int i, error;
176 	u_int32_t command;
177 
178 	debug_called(1);
179 
180 	/*
181 	 * Initialise softc.
182 	 */
183 	sc = device_get_softc(dev);
184 	bzero(sc, sizeof(*sc));
185 	sc->aac_dev = dev;
186 
187 	/* assume failure is 'not configured' */
188 	error = ENXIO;
189 
190 	/*
191 	 * Verify that the adapter is correctly set up in PCI space.
192 	 */
193 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
194 	command |= PCIM_CMD_BUSMASTEREN;
195 	pci_write_config(dev, PCIR_COMMAND, command, 2);
196 	command = pci_read_config(sc->aac_dev, PCIR_COMMAND, 2);
197 	if (!(command & PCIM_CMD_BUSMASTEREN)) {
198 		device_printf(sc->aac_dev, "can't enable bus-master feature\n");
199 		goto out;
200 	}
201 	if ((command & PCIM_CMD_MEMEN) == 0) {
202 		device_printf(sc->aac_dev, "memory window not available\n");
203 		goto out;
204 	}
205 
206 	/*
207 	 * Allocate the PCI register window.
208 	 */
209 	sc->aac_regs_rid = PCIR_BAR(0);
210 	if ((sc->aac_regs_resource = bus_alloc_resource_any(sc->aac_dev,
211 							    SYS_RES_MEMORY,
212 							    &sc->aac_regs_rid,
213 							    RF_ACTIVE)) ==
214 							    NULL) {
215 		device_printf(sc->aac_dev,
216 			      "couldn't allocate register window\n");
217 		goto out;
218 	}
219 	sc->aac_btag = rman_get_bustag(sc->aac_regs_resource);
220 	sc->aac_bhandle = rman_get_bushandle(sc->aac_regs_resource);
221 
222 	/*
223 	 * Allocate and connect our interrupt.
224 	 */
225 	sc->aac_irq_rid = 0;
226 	if ((sc->aac_irq = bus_alloc_resource_any(sc->aac_dev, SYS_RES_IRQ,
227 			   			  &sc->aac_irq_rid,
228 			   			  RF_SHAREABLE |
229 						  RF_ACTIVE)) == NULL) {
230 		device_printf(sc->aac_dev, "can't allocate interrupt\n");
231 		goto out;
232 	}
233 	if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
234 			   INTR_FAST|INTR_TYPE_BIO, aac_intr,
235 			   sc, &sc->aac_intr)) {
236 		device_printf(sc->aac_dev, "can't set up FAST interrupt\n");
237 		if (bus_setup_intr(sc->aac_dev, sc->aac_irq,
238 				   INTR_MPSAFE|INTR_ENTROPY|INTR_TYPE_BIO,
239 				   aac_intr, sc, &sc->aac_intr)) {
240 			device_printf(sc->aac_dev,
241 				      "can't set up MPSAFE interrupt\n");
242 			goto out;
243 		}
244 	}
245 
246 	/* assume failure is 'out of memory' */
247 	error = ENOMEM;
248 
249 	/*
250 	 * Allocate the parent bus DMA tag appropriate for our PCI interface.
251 	 *
252 	 * Note that some of these controllers are 64-bit capable.
253 	 */
254 	if (bus_dma_tag_create(NULL, 			/* parent */
255 			       PAGE_SIZE, 0,		/* algnmnt, boundary */
256 			       BUS_SPACE_MAXADDR,	/* lowaddr */
257 			       BUS_SPACE_MAXADDR, 	/* highaddr */
258 			       NULL, NULL, 		/* filter, filterarg */
259 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
260 			       AAC_MAXSGENTRIES,	/* nsegments */
261 			       BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
262 			       0,			/* flags */
263 			       NULL, NULL,		/* No locking needed */
264 			       &sc->aac_parent_dmat)) {
265 		device_printf(sc->aac_dev, "can't allocate parent DMA tag\n");
266 		goto out;
267 	}
268 
269 	/*
270 	 * Detect the hardware interface version, set up the bus interface
271 	 * indirection.
272 	 */
273 	for (i = 0; aac_identifiers[i].vendor != 0; i++) {
274 		if ((aac_identifiers[i].vendor == pci_get_vendor(dev)) &&
275 		    (aac_identifiers[i].device == pci_get_device(dev)) &&
276 		    (aac_identifiers[i].subvendor == pci_get_subvendor(dev)) &&
277 		    (aac_identifiers[i].subdevice == pci_get_subdevice(dev))) {
278 			sc->aac_hwif = aac_identifiers[i].hwif;
279 			switch(sc->aac_hwif) {
280 			case AAC_HWIF_I960RX:
281 				debug(2, "set hardware up for i960Rx");
282 				sc->aac_if = aac_rx_interface;
283 				break;
284 			case AAC_HWIF_STRONGARM:
285 				debug(2, "set hardware up for StrongARM");
286 				sc->aac_if = aac_sa_interface;
287 				break;
288 			case AAC_HWIF_FALCON:
289 				debug(2, "set hardware up for Falcon/PPC");
290 				sc->aac_if = aac_fa_interface;
291 				break;
292 			case AAC_HWIF_RKT:
293 				debug(2, "setu hardware up for Rocket/MIPS");
294 				sc->aac_if = aac_rkt_interface;
295 				break;
296 			default:
297 				sc->aac_hwif = AAC_HWIF_UNKNOWN;
298 				break;
299 			}
300 
301 			/* Set up quirks */
302 			sc->flags = aac_identifiers[i].quirks;
303 
304 			break;
305 		}
306 	}
307 	if (sc->aac_hwif == AAC_HWIF_UNKNOWN) {
308 		device_printf(sc->aac_dev, "unknown hardware type\n");
309 		error = ENXIO;
310 		goto out;
311 	}
312 
313 
314 	/*
315 	 * Do bus-independent initialisation.
316 	 */
317 	error = aac_attach(sc);
318 
319 out:
320 	if (error)
321 		aac_free(sc);
322 	return(error);
323 }
324 
325 /*
326  * Do nothing driver that will attach to the SCSI channels of a Dell PERC
327  * controller.  This is needed to keep the power management subsystem from
328  * trying to power down these devices.
329  */
330 static int aacch_probe(device_t dev);
331 static int aacch_attach(device_t dev);
332 static int aacch_detach(device_t dev);
333 
334 static device_method_t aacch_methods[] = {
335 	/* Device interface */
336 	DEVMETHOD(device_probe,		aacch_probe),
337 	DEVMETHOD(device_attach,	aacch_attach),
338 	DEVMETHOD(device_detach,	aacch_detach),
339 	{ 0, 0 }
340 };
341 
342 struct aacch_softc {
343 	device_t	dev;
344 };
345 
346 static driver_t aacch_driver = {
347 	"aacch",
348 	aacch_methods,
349 	sizeof(struct aacch_softc)
350 };
351 
352 static devclass_t	aacch_devclass;
353 DRIVER_MODULE(aacch, pci, aacch_driver, aacch_devclass, 0, 0);
354 
355 static int
356 aacch_probe(device_t dev)
357 {
358 
359 	if ((pci_get_subvendor(dev) != 0x9005) ||
360 	    (pci_get_subdevice(dev) != 0x00c5))
361 		return (ENXIO);
362 
363 	device_set_desc(dev, "AAC RAID Channel");
364 	return (-10);
365 }
366 
367 static int
368 aacch_attach(device_t dev)
369 {
370 	struct aacch_softc *sc;
371 
372 	sc = device_get_softc(dev);
373 
374 	sc->dev = dev;
375 
376 	return (0);
377 }
378 
379 static int
380 aacch_detach(device_t dev)
381 {
382 
383 	return (0);
384 }
385 
386