xref: /openbsd-src/sys/dev/pci/aac_pci.c (revision 5738bc6274bbfbba95d07e5dc8d380a26b687449)
1 /*	$OpenBSD: aac_pci.c,v 1.13 2004/08/29 19:07:26 marco 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 
48 #include <machine/bus.h>
49 #include <machine/endian.h>
50 #include <machine/intr.h>
51 
52 #include <scsi/scsi_all.h>
53 #include <scsi/scsiconf.h>
54 
55 #include <dev/pci/pcidevs.h>
56 #include <dev/pci/pcireg.h>
57 #include <dev/pci/pcivar.h>
58 
59 #include <dev/ic/aacreg.h>
60 #include <dev/ic/aacvar.h>
61 
62 int	aac_pci_probe(struct device *, void *, void *);
63 void	aac_pci_attach(struct device *, struct device *, void *);
64 
65 /* Adaptec */
66 #define PCI_PRODUCT_ADP2_AACASR2120S   0x0286
67 #define PCI_PRODUCT_ADP2_AACADPSATA2C  0x0289
68 #define PCI_PRODUCT_ADP2_AACADPSATA4C  0x0290
69 #define PCI_PRODUCT_ADP2_AACADPSATA6C  0x0291
70 #define PCI_PRODUCT_ADP2_AACADPSATA8C  0x0292
71 #define PCI_PRODUCT_ADP2_AACADPSATA16C 0x0293
72 
73 /* Dell */
74 #define PCI_PRODUCT_ADP2_AACCERCSATA6C 0x0291
75 #define PCI_PRODUCT_ADP2_AACPERC320DC  0x0287
76 
77 struct aac_sub_ident {
78 	u_int16_t subvendor;
79 	u_int16_t subdevice;
80 	char *desc;
81 } aac_sub_identifiers[] = {
82 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA2C, "Adaptec 1210SA" }, /* guess */
83 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA4C, "Adaptec 2410SA" },
84 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA6C, "Adaptec 2610SA" },
85 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA8C, "Adaptec 2810SA" }, /* guess */
86 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACADPSATA16C, "Adaptec 21610SA" }, /* guess */
87 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AACASR2120S, "Adaptec 2120S" },
88 	{ PCI_VENDOR_DELL, PCI_PRODUCT_ADP2_AACCERCSATA6C, "Dell CERC-SATA" },
89 	{ PCI_VENDOR_DELL, PCI_PRODUCT_ADP2_AACPERC320DC, "Dell PERC 320/DC" },
90 	{ 0, 0, "" }
91 };
92 
93 struct aac_ident {
94 	u_int16_t vendor;
95 	u_int16_t device;
96 	u_int16_t subvendor;
97 	u_int16_t subdevice;
98 	int	hwif;
99 } aac_identifiers[] = {
100 	/* Dell PERC 2/Si models */
101 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_2SI, PCI_VENDOR_DELL,
102 	    PCI_PRODUCT_DELL_PERC_2SI, AAC_HWIF_I960RX },
103 	/* Dell PERC 3/Di models */
104 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
105 	    PCI_PRODUCT_DELL_PERC_3DI, AAC_HWIF_I960RX },
106 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
107 	    PCI_PRODUCT_DELL_PERC_3DI_2, AAC_HWIF_I960RX },
108 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
109 	    PCI_PRODUCT_DELL_PERC_3DI_3, AAC_HWIF_I960RX },
110 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
111 	    PCI_PRODUCT_DELL_PERC_3DI_SUB2, AAC_HWIF_I960RX },
112 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
113 	    PCI_PRODUCT_DELL_PERC_3DI_SUB3, AAC_HWIF_I960RX },
114 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_2, PCI_VENDOR_DELL,
115 	    PCI_PRODUCT_DELL_PERC_3DI_2_SUB, AAC_HWIF_I960RX },
116 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
117 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB, AAC_HWIF_I960RX },
118 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
119 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB2, AAC_HWIF_I960RX },
120 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
121 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB3, AAC_HWIF_I960RX },
122 	/* Dell PERC 3/Si models */
123 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3SI, PCI_VENDOR_DELL,
124 	    PCI_PRODUCT_DELL_PERC_3SI, AAC_HWIF_I960RX },
125 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3SI_2, PCI_VENDOR_DELL,
126 	    PCI_PRODUCT_DELL_PERC_3SI_2_SUB, AAC_HWIF_I960RX },
127 	/* Adaptec SATA RAID 2 channel XXX guess */
128 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
129 	    PCI_PRODUCT_ADP2_AACADPSATA2C, AAC_HWIF_I960RX },
130 	/* Adaptec SATA RAID 4 channel */
131 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
132 	    PCI_PRODUCT_ADP2_AACADPSATA4C, AAC_HWIF_I960RX },
133 	/* Adaptec SATA RAID 6 channel XXX guess */
134 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
135 	    PCI_PRODUCT_ADP2_AACADPSATA6C, AAC_HWIF_I960RX },
136 	/* Adaptec SATA RAID 8 channel XXX guess */
137 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
138 	    PCI_PRODUCT_ADP2_AACADPSATA8C, AAC_HWIF_I960RX },
139 	/* Adaptec SATA RAID 16 channel XXX guess */
140 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
141 	    PCI_PRODUCT_ADP2_AACADPSATA16C, AAC_HWIF_I960RX },
142 	/* Dell CERC-SATA */
143 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_DELL,
144 	    PCI_PRODUCT_ADP2_AACCERCSATA6C, AAC_HWIF_I960RX },
145 	/* Dell PERC 320/DC */
146 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_DELL,
147 	    PCI_PRODUCT_ADP2_AACPERC320DC, AAC_HWIF_I960RX },
148 	/* Adaptec ADP-2622 */
149 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AAC2622, PCI_VENDOR_ADP2,
150 	    PCI_PRODUCT_ADP2_AAC2622, AAC_HWIF_I960RX },
151 	/* Adaptec ADP-364 */
152 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
153 	    PCI_PRODUCT_ADP2_AAC364, AAC_HWIF_STRONGARM },
154 	/* Adaptec ADP-3642 */
155 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
156 	    PCI_PRODUCT_ADP2_AAC3642, AAC_HWIF_STRONGARM },
157 	/* Dell PERC 2/QC */
158 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
159 	    PCI_PRODUCT_ADP2_PERC_2QC, AAC_HWIF_STRONGARM },
160 	/* HP NetRAID-4M */
161 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_HP,
162 	    PCI_PRODUCT_HP_NETRAID_4M, AAC_HWIF_STRONGARM },
163 	/* Adaptec ASR-2120S */
164 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_ASR2200S, PCI_VENDOR_ADP2,
165 	    PCI_PRODUCT_ADP2_AACASR2120S, AAC_HWIF_I960RX },
166 	{ 0, 0, 0, 0 }
167 };
168 
169 struct cfattach aac_pci_ca = {
170 	sizeof (struct aac_softc), aac_pci_probe, aac_pci_attach
171 };
172 
173 /*
174  * Determine whether this is one of our supported adapters.
175  */
176 int
177 aac_pci_probe(parent, match, aux)
178 	struct device *parent;
179 	void *match;
180 	void *aux;
181 {
182         struct pci_attach_args *pa = aux;
183 	struct aac_ident *m;
184 	u_int32_t subsysid;
185 
186 	for (m = aac_identifiers; m->vendor != 0; m++)
187 		if (m->vendor == PCI_VENDOR(pa->pa_id) &&
188 		    m->device == PCI_PRODUCT(pa->pa_id)) {
189 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
190 			    PCI_SUBSYS_ID_REG);
191 			if (m->subvendor == PCI_VENDOR(subsysid) &&
192 			    m->subdevice == PCI_PRODUCT(subsysid))
193 				return (1);
194 		}
195 	return (0);
196 }
197 
198 void
199 aac_pci_attach(parent, self, aux)
200         struct device *parent, *self;
201         void *aux;
202 {
203 	struct pci_attach_args *pa = aux;
204 	pci_chipset_tag_t pc = pa->pa_pc;
205 	struct aac_softc *sc = (void *)self;
206 	u_int16_t command;
207 	bus_addr_t membase;
208 	bus_size_t memsize;
209 	pci_intr_handle_t ih;
210 	const char *intrstr;
211 	int state = 0;
212 	struct aac_ident *m;
213 	struct aac_sub_ident *subid;
214 	u_int32_t subsysid;
215 
216 	printf(": ");
217 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
218 	if ((PCI_VENDOR(pa->pa_id) != PCI_VENDOR(subsysid)) ||
219 	    (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT(subsysid))) {
220 		for (subid = aac_sub_identifiers; subid->subvendor != 0;
221 		    subid++) {
222 			if (subid->subvendor == PCI_VENDOR(subsysid) &&
223 			    subid->subdevice == PCI_PRODUCT(subsysid)) {
224 				printf("%s ", subid->desc);
225 				break;
226 			}
227 		}
228 	}
229 
230 	/*
231 	 * Verify that the adapter is correctly set up in PCI space.
232 	 */
233 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
234 	command |= PCI_COMMAND_MASTER_ENABLE;
235 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
236 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
237 	AAC_DPRINTF(AAC_D_MISC, ("pci command status reg 0x08x "));
238 	if (!(command & PCI_COMMAND_MASTER_ENABLE)) {
239 		printf("can't enable bus-master feature\n");
240 		goto bail_out;
241 	}
242 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
243 		printf("memory window not available\n");
244 		goto bail_out;
245 	}
246 
247 	/*
248 	 * Map control/status registers.
249 	 */
250 	if (pci_mapreg_map(pa, PCI_MAPREG_START,
251 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_memt,
252 	    &sc->sc_memh, &membase, &memsize, AAC_REGSIZE)) {
253 		printf("can't find mem space\n");
254 		goto bail_out;
255 	}
256 	state++;
257 
258 	if (pci_intr_map(pa, &ih)) {
259 		printf("couldn't map interrupt\n");
260 		goto bail_out;
261 	}
262 	intrstr = pci_intr_string(pc, ih);
263 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, aac_intr, sc,
264 	    sc->sc_dev.dv_xname);
265 	if (sc->sc_ih == NULL) {
266 		printf("couldn't establish interrupt");
267 		if (intrstr != NULL)
268 			printf(" at %s", intrstr);
269 		printf("\n");
270 		goto bail_out;
271 	}
272 	state++;
273 	if (intrstr != NULL)
274 		printf("%s\n", intrstr);
275 
276 	sc->sc_dmat = pa->pa_dmat;
277 
278 	for (m = aac_identifiers; m->vendor != 0; m++)
279 		if (m->vendor == PCI_VENDOR(pa->pa_id) &&
280 		    m->device == PCI_PRODUCT(pa->pa_id)) {
281 			if (m->subvendor == PCI_VENDOR(subsysid) &&
282 			    m->subdevice == PCI_PRODUCT(subsysid)) {
283 				sc->sc_hwif = m->hwif;
284 				switch(sc->sc_hwif) {
285 				case AAC_HWIF_I960RX:
286 					AAC_DPRINTF(AAC_D_MISC,
287 					    ("set hardware up for i960Rx"));
288 					sc->sc_if = aac_rx_interface;
289 					break;
290 
291 				case AAC_HWIF_STRONGARM:
292 					AAC_DPRINTF(AAC_D_MISC,
293 					    ("set hardware up for StrongARM"));
294 					sc->sc_if = aac_sa_interface;
295 					break;
296 				}
297 				break;
298 			}
299 		}
300 
301 	if (aac_attach(sc))
302 		goto bail_out;
303 
304 	return;
305 
306  bail_out:
307 	if (state > 1)
308 		pci_intr_disestablish(pc, sc->sc_ih);
309 	if (state > 0)
310 		bus_space_unmap(sc->sc_memt, sc->sc_memh, memsize);
311 	return;
312 }
313