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