xref: /openbsd-src/sys/dev/pci/aac_pci.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: aac_pci.c,v 1.9 2002/07/27 19:21:16 aaron 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 struct aac_ident {
66 	u_int16_t vendor;
67 	u_int16_t device;
68 	u_int16_t subvendor;
69 	u_int16_t subdevice;
70 	int	hwif;
71 } aac_identifiers[] = {
72 	/* Dell PERC 2/Si models */
73 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_2SI, PCI_VENDOR_DELL,
74 	    PCI_PRODUCT_DELL_PERC_2SI, AAC_HWIF_I960RX },
75 	/* Dell PERC 3/Di models */
76 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
77 	    PCI_PRODUCT_DELL_PERC_3DI, AAC_HWIF_I960RX },
78 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
79 	    PCI_PRODUCT_DELL_PERC_3DI_2, AAC_HWIF_I960RX },
80 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
81 	    PCI_PRODUCT_DELL_PERC_3DI_3, AAC_HWIF_I960RX },
82 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
83 	    PCI_PRODUCT_DELL_PERC_3DI_SUB2, AAC_HWIF_I960RX },
84 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI, PCI_VENDOR_DELL,
85 	    PCI_PRODUCT_DELL_PERC_3DI_SUB3, AAC_HWIF_I960RX },
86 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_2, PCI_VENDOR_DELL,
87 	    PCI_PRODUCT_DELL_PERC_3DI_2_SUB, AAC_HWIF_I960RX },
88 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
89 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB, AAC_HWIF_I960RX },
90 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
91 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB2, AAC_HWIF_I960RX },
92 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3DI_3, PCI_VENDOR_DELL,
93 	    PCI_PRODUCT_DELL_PERC_3DI_3_SUB3, AAC_HWIF_I960RX },
94 	/* Dell PERC 3/Si models */
95 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3SI, PCI_VENDOR_DELL,
96 	    PCI_PRODUCT_DELL_PERC_3SI, AAC_HWIF_I960RX },
97 	{ PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_3SI_2, PCI_VENDOR_DELL,
98 	    PCI_PRODUCT_DELL_PERC_3SI_2_SUB, AAC_HWIF_I960RX },
99 	/* Adaptec ADP-2622 */
100 	{ PCI_VENDOR_ADP2, PCI_PRODUCT_ADP2_AAC2622, PCI_VENDOR_ADP2,
101 	    PCI_PRODUCT_ADP2_AAC2622, AAC_HWIF_I960RX },
102 	/* Adaptec ADP-364 */
103 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
104 	    PCI_PRODUCT_ADP2_AAC364, AAC_HWIF_STRONGARM },
105 	/* Adaptec ADP-3642 */
106 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
107 	    PCI_PRODUCT_ADP2_AAC3642, AAC_HWIF_STRONGARM },
108 	/* Dell PERC 2/QC */
109 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
110 	    PCI_PRODUCT_ADP2_PERC_2QC, AAC_HWIF_STRONGARM },
111 	/* Dell PERC 3/QC */
112 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_ADP2,
113 	    PCI_PRODUCT_ADP2_PERC_3QC, AAC_HWIF_STRONGARM },
114 	/* HP NetRAID-4M */
115 	{ PCI_VENDOR_DEC, PCI_PRODUCT_DEC_CPQ42XX, PCI_VENDOR_HP,
116 	    PCI_PRODUCT_HP_NETRAID_4M, AAC_HWIF_STRONGARM },
117 	{ 0, 0, 0, 0 }
118 };
119 
120 struct cfattach aac_pci_ca = {
121 	sizeof (struct aac_softc), aac_pci_probe, aac_pci_attach
122 };
123 
124 /*
125  * Determine whether this is one of our supported adapters.
126  */
127 int
128 aac_pci_probe(parent, match, aux)
129 	struct device *parent;
130 	void *match;
131 	void *aux;
132 {
133         struct pci_attach_args *pa = aux;
134 	struct aac_ident *m;
135 	u_int32_t subsysid;
136 
137 	for (m = aac_identifiers; m->vendor != 0; m++)
138 		if (m->vendor == PCI_VENDOR(pa->pa_id) &&
139 		    m->device == PCI_PRODUCT(pa->pa_id)) {
140 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
141 			    PCI_SUBSYS_ID_REG);
142 			if (m->subvendor == PCI_VENDOR(subsysid) &&
143 			    m->subdevice == PCI_PRODUCT(subsysid))
144 				return (1);
145 		}
146 	return (0);
147 }
148 
149 void
150 aac_pci_attach(parent, self, aux)
151         struct device *parent, *self;
152         void *aux;
153 {
154 	struct pci_attach_args *pa = aux;
155 	pci_chipset_tag_t pc = pa->pa_pc;
156 	struct aac_softc *sc = (void *)self;
157 	u_int16_t command;
158 	bus_addr_t membase;
159 	bus_size_t memsize;
160 	pci_intr_handle_t ih;
161 	const char *intrstr;
162 	int state = 0;
163 	struct aac_ident *m;
164 	u_int32_t subsysid;
165 
166 	printf(": ");
167 
168 	/*
169 	 * Verify that the adapter is correctly set up in PCI space.
170 	 */
171 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
172 	command |= PCI_COMMAND_MASTER_ENABLE;
173 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
174 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
175 	AAC_DPRINTF(AAC_D_MISC, ("pci command status reg 0x08x "));
176 	if (!(command & PCI_COMMAND_MASTER_ENABLE)) {
177 		printf("can't enable bus-master feature\n");
178 		goto bail_out;
179 	}
180 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
181 		printf("memory window not available\n");
182 		goto bail_out;
183 	}
184 
185 	/*
186 	 * Map control/status registers.
187 	 */
188 	if (pci_mapreg_map(pa, PCI_MAPREG_START,
189 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, &sc->sc_memt,
190 	    &sc->sc_memh, &membase, &memsize, AAC_REGSIZE)) {
191 		printf("can't find mem space\n");
192 		goto bail_out;
193 	}
194 	state++;
195 
196 	if (pci_intr_map(pa, &ih)) {
197 		printf("couldn't map interrupt\n");
198 		goto bail_out;
199 	}
200 	intrstr = pci_intr_string(pc, ih);
201 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, aac_intr, sc,
202 	    sc->sc_dev.dv_xname);
203 	if (sc->sc_ih == NULL) {
204 		printf("couldn't establish interrupt");
205 		if (intrstr != NULL)
206 			printf(" at %s", intrstr);
207 		printf("\n");
208 		goto bail_out;
209 	}
210 	state++;
211 	if (intrstr != NULL)
212 		printf("%s\n", intrstr);
213 
214 	sc->sc_dmat = pa->pa_dmat;
215 
216 	for (m = aac_identifiers; m->vendor != 0; m++)
217 		if (m->vendor == PCI_VENDOR(pa->pa_id) &&
218 		    m->device == PCI_PRODUCT(pa->pa_id)) {
219 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
220 			    PCI_SUBSYS_ID_REG);
221 			if (m->subvendor == PCI_VENDOR(subsysid) &&
222 			    m->subdevice == PCI_PRODUCT(subsysid)) {
223 				sc->sc_hwif = m->hwif;
224 				switch(sc->sc_hwif) {
225 				case AAC_HWIF_I960RX:
226 					AAC_DPRINTF(AAC_D_MISC,
227 					    ("set hardware up for i960Rx"));
228 					sc->sc_if = aac_rx_interface;
229 					break;
230 
231 				case AAC_HWIF_STRONGARM:
232 					AAC_DPRINTF(AAC_D_MISC,
233 					    ("set hardware up for StrongARM"));
234 					sc->sc_if = aac_sa_interface;
235 					break;
236 				}
237 				break;
238 			}
239 		}
240 
241 	if (aac_attach(sc))
242 		goto bail_out;
243 
244 	return;
245 
246  bail_out:
247 	if (state > 1)
248 		pci_intr_disestablish(pc, sc->sc_ih);
249 	if (state > 0)
250 		bus_space_unmap(sc->sc_memt, sc->sc_memh, memsize);
251 	return;
252 }
253