xref: /openbsd/sys/dev/pci/aac_pci.c (revision 771fbea0)
1 /*	$OpenBSD: aac_pci.c,v 1.25 2014/12/19 22:44:58 guenther Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Michael Smith
5  * Copyright (c) 2000 BSDi
6  * Copyright (c) 2000 Niklas Hallqvist
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	$FreeBSD: /c/ncvs/src/sys/dev/aac/aac_pci.c,v 1.1 2000/09/13 03:20:34 msmith Exp $
31  */
32 
33 /*
34  * This driver would not have rewritten for OpenBSD if it was not for the
35  * hardware donation from Nocom.  I want to thank them for their support.
36  * Of course, credit should go to Mike Smith for the original work he did
37  * in the FreeBSD driver where I found lots of inspiration.
38  * - Niklas Hallqvist
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/queue.h>
47 #include <sys/selinfo.h>
48 #include <sys/rwlock.h>
49 #include <sys/endian.h>
50 
51 #include <machine/bus.h>
52 #include <machine/intr.h>
53 
54 #include <scsi/scsi_all.h>
55 #include <scsi/scsiconf.h>
56 
57 #include <dev/pci/pcidevs.h>
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60 
61 #include <dev/ic/aacreg.h>
62 #include <dev/ic/aacvar.h>
63 
64 int	aac_pci_probe(struct device *, void *, void *);
65 void	aac_pci_attach(struct device *, struct device *, void *);
66 
67 /* Adaptec */
68 #define PCI_PRODUCT_ADP2_AACASR2200S   0x0285
69 #define PCI_PRODUCT_ADP2_AACASR2120S   0x0286
70 #define PCI_PRODUCT_ADP2_AACADPSATA2C  0x0289
71 #define PCI_PRODUCT_ADP2_AACASR2230S   0x028c
72 #define PCI_PRODUCT_ADP2_AACASR2130S   0x028d
73 #define PCI_PRODUCT_ADP2_AACADPSATA4C  0x0290
74 #define PCI_PRODUCT_ADP2_AACADPSATA6C  0x0291
75 #define PCI_PRODUCT_ADP2_AACADPSATA8C  0x0292
76 #define PCI_PRODUCT_ADP2_AACADPSATA16C 0x0293
77 
78 /* Dell */
79 #define PCI_PRODUCT_ADP2_AACCERCSATA6C 0x0291
80 #define PCI_PRODUCT_ADP2_AACPERC320DC  0x0287
81 
82 /* IBM */
83 #define PCI_PRODUCT_ADP2_AACSERVERAID8I 0x02f2
84 #define PCI_PRODUCT_ADP2_AACSERVERAID8I_2 0x0312
85 #define PCI_PRODUCT_ADP2_AACSERVERAID8K 0x9580
86 #define PCI_PRODUCT_ADP2_AACSERVERAID8S 0x034d
87 
88 struct aac_sub_ident {
89 	u_int16_t subvendor;
90 	u_int16_t subdevice;
91 	char *desc;
92 } aac_sub_identifiers[] = {
93 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA2C, "Adaptec 1210SA" }, /* guess */
94 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2130S, "Adaptec 2130S" },
95 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2230S, "Adaptec 2230S" },
96 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA4C, "Adaptec 2410SA" },
97 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA6C, "Adaptec 2610SA" },
98 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA8C, "Adaptec 2810SA" }, /* guess */
99 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA16C, "Adaptec 21610SA" }, /* guess */
100 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2120S, "Adaptec 2120S" },
101 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2200S, "Adaptec 2200S" },
102 	{ PCI_VENDOR_DELL, PCI_PRODUCT_ADP2_AACCERCSATA6C, "Dell CERC-SATA" },
103 	{ PCI_VENDOR_DELL, PCI_PRODUCT_ADP2_AACPERC320DC, "Dell PERC 320/DC" },
104 	{ PCI_VENDOR_IBM, PCI_PRODUCT_ADP2_AACSERVERAID8I, "IBM ServeRAID-8i" },
105 	{ PCI_VENDOR_IBM, PCI_PRODUCT_ADP2_AACSERVERAID8I_2, "IBM ServeRAID-8i" },
106 	{ PCI_VENDOR_IBM, PCI_PRODUCT_ADP2_AACSERVERAID8K, "IBM ServeRAID-8k" },
107 	{ PCI_VENDOR_IBM, PCI_PRODUCT_ADP2_AACSERVERAID8S, "IBM ServeRAID-8s" },
108 	{ 0, 0, "" }
109 };
110 
111 struct aac_ident {
112 	u_int16_t vendor;
113 	u_int16_t device;
114 	u_int16_t subvendor;
115 	u_int16_t subdevice;
116 	int	hwif;
117 } aac_identifiers[] = {
118 	/* Dell PERC 2/Si models */
119 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_2SI, PCI_VENDOR_DELL,
120 	    PCI_PRODUCT_DELL_PERC_2SI, AAC_HWIF_I960RX },
121 	/* Dell PERC 3/Di models */
122 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
123 	    PCI_PRODUCT_DELL_PERC_3DI, AAC_HWIF_I960RX },
124 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
125 	    PCI_PRODUCT_DELL_PERC_3DI_3, AAC_HWIF_I960RX },
126 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
127 	    PCI_PRODUCT_DELL_PERC_3DI_SUB2, AAC_HWIF_I960RX },
128 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
129 	    PCI_PRODUCT_DELL_PERC_3DI_SUB3, AAC_HWIF_I960RX },
130 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
131 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB, AAC_HWIF_I960RX },
132 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
133 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB2, AAC_HWIF_I960RX },
134 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
135 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB3, AAC_HWIF_I960RX },
136 	/* Dell PERC 3/Si models */
137 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3SI, PCI_VENDOR_DELL,
138 	    PCI_PRODUCT_DELL_PERC_3SI, AAC_HWIF_I960RX },
139 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3SI_2, PCI_VENDOR_DELL,
140 	    PCI_PRODUCT_DELL_PERC_3SI_2_SUB, AAC_HWIF_I960RX },
141 	/* Adaptec SATA RAID 2 channel XXX guess */
142 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
143 	    PCI_PRODUCT_ADP2_AACADPSATA2C, AAC_HWIF_I960RX },
144 	/* Adaptec SATA RAID 4 channel */
145 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
146 	    PCI_PRODUCT_ADP2_AACADPSATA4C, AAC_HWIF_I960RX },
147 	/* Adaptec SATA RAID 6 channel XXX guess */
148 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
149 	    PCI_PRODUCT_ADP2_AACADPSATA6C, AAC_HWIF_I960RX },
150 	/* Adaptec SATA RAID 8 channel XXX guess */
151 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
152 	    PCI_PRODUCT_ADP2_AACADPSATA8C, AAC_HWIF_I960RX },
153 	/* Adaptec SATA RAID 16 channel XXX guess */
154 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
155 	    PCI_PRODUCT_ADP2_AACADPSATA16C, AAC_HWIF_I960RX },
156 	/* Dell CERC-SATA */
157 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_DELL,
158 	    PCI_PRODUCT_ADP2_AACCERCSATA6C, AAC_HWIF_I960RX },
159 	/* Dell PERC 320/DC */
160 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_DELL,
161 	    PCI_PRODUCT_ADP2_AACPERC320DC, AAC_HWIF_I960RX },
162 	/* Adaptec ADP-2622 */
163 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AAC2622, PCI_VENDOR_ADP2,
164 	    PCI_PRODUCT_ADP2_AAC2622, AAC_HWIF_I960RX },
165 	/* Adaptec ADP-364 */
166 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21554, PCI_VENDOR_ADP2,
167 	    PCI_PRODUCT_ADP2_AAC364, AAC_HWIF_STRONGARM },
168 	/* Adaptec ADP-3642 */
169 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21554, PCI_VENDOR_ADP2,
170 	    PCI_PRODUCT_ADP2_AAC3642, AAC_HWIF_STRONGARM },
171 	/* Dell PERC 2/QC */
172 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21554, PCI_VENDOR_ADP2,
173 	    PCI_PRODUCT_ADP2_PERC_2QC, AAC_HWIF_STRONGARM },
174 	/* HP NetRAID-4M */
175 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_21554, PCI_VENDOR_HP,
176 	    PCI_PRODUCT_HP_NETRAID_4M, AAC_HWIF_STRONGARM },
177 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
178 	    PCI_PRODUCT_ADP2_ASR2120S, AAC_HWIF_I960RX },
179 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
180 	    PCI_PRODUCT_ADP2_ASR2200S, AAC_HWIF_I960RX },
181 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2120S, PCI_VENDOR_ADP2,
182 	    PCI_PRODUCT_ADP2_AACASR2130S, AAC_HWIF_RKT },
183 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2120S, PCI_VENDOR_ADP2,
184 	    PCI_PRODUCT_ADP2_AACASR2230S, AAC_HWIF_RKT },
185 	/* IBM ServeRAID-8i/8k/8s */
186 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2200S, PCI_VENDOR_IBM,
187 	    PCI_PRODUCT_ADP2_AACSERVERAID8I, AAC_HWIF_I960RX },
188 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2200S, PCI_VENDOR_IBM,
189 	    PCI_PRODUCT_ADP2_AACSERVERAID8I_2, AAC_HWIF_I960RX },
190 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2120S, PCI_VENDOR_IBM,
191 	    PCI_PRODUCT_ADP2_AACSERVERAID8K, AAC_HWIF_RKT },
192 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2200S, PCI_VENDOR_IBM,
193 	    PCI_PRODUCT_ADP2_AACSERVERAID8S, AAC_HWIF_I960RX },
194 	{ 0, 0, 0, 0 }
195 };
196 
197 struct cfattach aac_pci_ca = {
198 	sizeof (struct aac_softc), aac_pci_probe, aac_pci_attach
199 };
200 
201 /*
202  * Determine whether this is one of our supported adapters.
203  */
204 int
205 aac_pci_probe(parent, match, aux)
206 	struct device *parent;
207 	void *match;
208 	void *aux;
209 {
210         struct pci_attach_args *pa = aux;
211 	struct aac_ident *m;
212 	u_int32_t subsysid;
213 
214 	for (m = aac_identifiers; m->vendor != 0; m++)
215 		if (m->vendor == PCI_VENDOR(pa->pa_id) &&
216 		    m->device == PCI_PRODUCT(pa->pa_id)) {
217 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
218 			    PCI_SUBSYS_ID_REG);
219 			if (m->subvendor == PCI_VENDOR(subsysid) &&
220 			    m->subdevice == PCI_PRODUCT(subsysid))
221 				return (1);
222 		}
223 	return (0);
224 }
225 
226 void
227 aac_pci_attach(parent, self, aux)
228         struct device *parent, *self;
229         void *aux;
230 {
231 	struct pci_attach_args *pa = aux;
232 	pci_chipset_tag_t pc = pa->pa_pc;
233 	struct aac_softc *sc = (void *)self;
234 	bus_addr_t membase;
235 	bus_size_t memsize;
236 	pci_intr_handle_t ih;
237 	const char *intrstr;
238 	int state = 0;
239 	struct aac_ident *m;
240 	struct aac_sub_ident *subid;
241 	u_int32_t subsysid;
242 	pcireg_t memtype;
243 
244 	printf(": ");
245 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
246 	if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR(subsysid)) ||
247 	    (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT(subsysid))) {
248 		for (subid = aac_sub_identifiers; subid->subvendor != 0;
249 		    subid++) {
250 			if (subid->subvendor == PCI_VENDOR(subsysid) &&
251 			    subid->subdevice == PCI_PRODUCT(subsysid)) {
252 				printf("%s ", subid->desc);
253 				break;
254 			}
255 		}
256 	}
257 
258 	/*
259 	 * Map control/status registers.
260 	 */
261 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
262 	if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &sc->aac_memt,
263 	    &sc->aac_memh, &membase, &memsize, AAC_REGSIZE)) {
264 		printf("can't find mem space\n");
265 		goto bail_out;
266 	}
267 	state++;
268 
269 	if (pci_intr_map(pa, &ih)) {
270 		printf("couldn't map interrupt\n");
271 		goto bail_out;
272 	}
273 	intrstr = pci_intr_string(pc, ih);
274 	sc->aac_ih = pci_intr_establish(pc, ih, IPL_BIO, aac_intr, sc,
275 	    sc->aac_dev.dv_xname);
276 	if (sc->aac_ih == NULL) {
277 		printf("couldn't establish interrupt");
278 		if (intrstr != NULL)
279 			printf(" at %s", intrstr);
280 		printf("\n");
281 		goto bail_out;
282 	}
283 	state++;
284 	if (intrstr != NULL)
285 		printf("%s\n", intrstr);
286 
287 	sc->aac_dmat = pa->pa_dmat;
288 
289 	for (m = aac_identifiers; m->vendor != 0; m++)
290 		if (m->vendor == PCI_VENDOR(pa->pa_id) &&
291 		    m->device == PCI_PRODUCT(pa->pa_id)) {
292 			if (m->subvendor == PCI_VENDOR(subsysid) &&
293 			    m->subdevice == PCI_PRODUCT(subsysid)) {
294 				sc->aac_hwif = m->hwif;
295 				switch(sc->aac_hwif) {
296 				case AAC_HWIF_I960RX:
297 					AAC_DPRINTF(AAC_D_MISC,
298 					    ("set hardware up for i960Rx"));
299 					sc->aac_if = aac_rx_interface;
300 					break;
301 				case AAC_HWIF_STRONGARM:
302 					AAC_DPRINTF(AAC_D_MISC,
303 					    ("set hardware up for StrongARM"));
304 					sc->aac_if = aac_sa_interface;
305 					break;
306 				case AAC_HWIF_FALCON:
307 					AAC_DPRINTF(AAC_D_MISC,
308 					   ("set hardware up for Falcon/PPC"));
309 					sc->aac_if = aac_fa_interface;
310 					break;
311 				case AAC_HWIF_RKT:
312 					AAC_DPRINTF(AAC_D_MISC,
313 					   ("set hardware up for Rocket/MIPS"));
314 					sc->aac_if = aac_rkt_interface;
315 					break;
316 				default:
317 					sc->aac_hwif = AAC_HWIF_UNKNOWN;
318 					break;
319 				}
320 				break;
321 			}
322 		}
323 
324 	if (aac_attach(sc))
325 		goto bail_out;
326 
327 	return;
328 
329  bail_out:
330 	if (state > 1)
331 		pci_intr_disestablish(pc, sc->aac_ih);
332 	if (state > 0)
333 		bus_space_unmap(sc->aac_memt, sc->aac_memh, memsize);
334 	return;
335 }
336