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