ciss_pci.c revision 1.2 1 1.2 christos /* $NetBSD: ciss_pci.c,v 1.2 2006/10/12 01:31:28 christos Exp $ */
2 1.1 he /* $OpenBSD: ciss_pci.c,v 1.9 2005/12/13 15:56:01 brad Exp $ */
3 1.1 he
4 1.1 he /*
5 1.1 he * Copyright (c) 2005 Michael Shalayeff
6 1.1 he * All rights reserved.
7 1.1 he *
8 1.1 he * Permission to use, copy, modify, and distribute this software for any
9 1.1 he * purpose with or without fee is hereby granted, provided that the above
10 1.1 he * copyright notice and this permission notice appear in all copies.
11 1.1 he *
12 1.1 he * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1 he * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1 he * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1 he * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1 he * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
17 1.1 he * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
18 1.1 he * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1 he */
20 1.1 he
21 1.1 he #include <sys/cdefs.h>
22 1.2 christos __KERNEL_RCSID(0, "$NetBSD: ciss_pci.c,v 1.2 2006/10/12 01:31:28 christos Exp $");
23 1.1 he
24 1.1 he #include <sys/param.h>
25 1.1 he #include <sys/systm.h>
26 1.1 he #include <sys/kernel.h>
27 1.1 he #include <sys/malloc.h>
28 1.1 he #include <sys/device.h>
29 1.1 he
30 1.1 he #include <dev/pci/pcidevs.h>
31 1.1 he #include <dev/pci/pcivar.h>
32 1.1 he
33 1.1 he #include <machine/bus.h>
34 1.1 he
35 1.1 he #include <dev/scsipi/scsipi_all.h>
36 1.1 he #include <dev/scsipi/scsipi_disk.h>
37 1.1 he #include <dev/scsipi/scsipiconf.h>
38 1.1 he
39 1.1 he #include <dev/ic/cissreg.h>
40 1.1 he #include <dev/ic/cissvar.h>
41 1.1 he
42 1.1 he #define CISS_BAR 0x10
43 1.1 he
44 1.1 he int ciss_pci_match(struct device *, struct cfdata *, void *);
45 1.1 he void ciss_pci_attach(struct device *, struct device *, void *);
46 1.1 he
47 1.1 he CFATTACH_DECL(ciss_pci, sizeof(struct ciss_softc),
48 1.1 he ciss_pci_match, ciss_pci_attach, NULL, NULL);
49 1.1 he
50 1.1 he const struct {
51 1.1 he int vendor;
52 1.1 he int product;
53 1.1 he const char *name;
54 1.1 he } ciss_pci_devices[] = {
55 1.1 he {
56 1.1 he PCI_VENDOR_COMPAQ,
57 1.1 he PCI_PRODUCT_COMPAQ_CSA532,
58 1.1 he "Compaq Smart Array 532"
59 1.1 he },
60 1.1 he {
61 1.1 he PCI_VENDOR_COMPAQ,
62 1.1 he PCI_PRODUCT_COMPAQ_CSA5300,
63 1.1 he "Compaq Smart Array 5300 V1"
64 1.1 he },
65 1.1 he {
66 1.1 he PCI_VENDOR_COMPAQ,
67 1.1 he PCI_PRODUCT_COMPAQ_CSA5300_2,
68 1.1 he "Compaq Smart Array 5300 V2"
69 1.1 he },
70 1.1 he {
71 1.1 he PCI_VENDOR_COMPAQ,
72 1.1 he PCI_PRODUCT_COMPAQ_CSA5312,
73 1.1 he "Compaq Smart Array 5312"
74 1.1 he },
75 1.1 he {
76 1.1 he PCI_VENDOR_COMPAQ,
77 1.1 he PCI_PRODUCT_COMPAQ_CSA5i,
78 1.1 he "Compaq Smart Array 5i"
79 1.1 he },
80 1.1 he {
81 1.1 he PCI_VENDOR_COMPAQ,
82 1.1 he PCI_PRODUCT_COMPAQ_CSA5i_2,
83 1.1 he "Compaq Smart Array 5i V2"
84 1.1 he },
85 1.1 he {
86 1.1 he PCI_VENDOR_COMPAQ,
87 1.1 he PCI_PRODUCT_COMPAQ_CSA6i,
88 1.1 he "Compaq Smart Array 6i"
89 1.1 he },
90 1.1 he {
91 1.1 he PCI_VENDOR_COMPAQ,
92 1.1 he PCI_PRODUCT_COMPAQ_CSA641,
93 1.1 he "Compaq Smart Array 641"
94 1.1 he },
95 1.1 he {
96 1.1 he PCI_VENDOR_COMPAQ,
97 1.1 he PCI_PRODUCT_COMPAQ_CSA642,
98 1.1 he "Compaq Smart Array 642"
99 1.1 he },
100 1.1 he {
101 1.1 he PCI_VENDOR_COMPAQ,
102 1.1 he PCI_PRODUCT_COMPAQ_CSA6400,
103 1.1 he "Compaq Smart Array 6400"
104 1.1 he },
105 1.1 he {
106 1.1 he PCI_VENDOR_COMPAQ,
107 1.1 he PCI_PRODUCT_COMPAQ_CSA6400EM,
108 1.1 he "Compaq Smart Array 6400EM"
109 1.1 he },
110 1.1 he {
111 1.1 he PCI_VENDOR_COMPAQ,
112 1.1 he PCI_PRODUCT_COMPAQ_CSA6422,
113 1.1 he "Compaq Smart Array 6422"
114 1.1 he },
115 1.1 he {
116 1.1 he PCI_VENDOR_COMPAQ,
117 1.1 he PCI_PRODUCT_COMPAQ_CSA64XX,
118 1.1 he "Compaq Smart Array 64XX"
119 1.1 he },
120 1.1 he {
121 1.1 he PCI_VENDOR_HP,
122 1.1 he PCI_PRODUCT_HP_HPSAE200,
123 1.1 he "Smart Array E200"
124 1.1 he },
125 1.1 he {
126 1.1 he PCI_VENDOR_HP,
127 1.1 he PCI_PRODUCT_HP_HPSAE200I_1,
128 1.1 he "HP Smart Array E200I-1"
129 1.1 he },
130 1.1 he {
131 1.1 he PCI_VENDOR_HP,
132 1.1 he PCI_PRODUCT_HP_HPSAE200I_2,
133 1.1 he "HP Smart Array E200I-2"
134 1.1 he },
135 1.1 he {
136 1.1 he PCI_VENDOR_HP,
137 1.1 he PCI_PRODUCT_HP_HPSAE200I_3,
138 1.1 he "HP Smart Array E200I-3"
139 1.1 he },
140 1.1 he {
141 1.1 he PCI_VENDOR_HP,
142 1.1 he PCI_PRODUCT_HP_HPSAP600,
143 1.1 he "HP Smart Array P600"
144 1.1 he },
145 1.1 he {
146 1.1 he PCI_VENDOR_HP,
147 1.1 he PCI_PRODUCT_HP_HPSAP800,
148 1.1 he "HP Smart Array P800"
149 1.1 he },
150 1.1 he {
151 1.1 he PCI_VENDOR_HP,
152 1.1 he PCI_PRODUCT_HP_HPSAV100,
153 1.1 he "HP Smart Array V100"
154 1.1 he },
155 1.1 he {
156 1.1 he PCI_VENDOR_HP,
157 1.1 he PCI_PRODUCT_HP_HPSA_1,
158 1.1 he "HP Smart Array 1"
159 1.1 he },
160 1.1 he {
161 1.1 he PCI_VENDOR_HP,
162 1.1 he PCI_PRODUCT_HP_HPSA_2,
163 1.1 he "HP Smart Array 2"
164 1.1 he },
165 1.1 he {
166 1.1 he PCI_VENDOR_HP,
167 1.1 he PCI_PRODUCT_HP_HPSA_3,
168 1.1 he "HP Smart Array 3"
169 1.1 he },
170 1.1 he {
171 1.1 he PCI_VENDOR_HP,
172 1.1 he PCI_PRODUCT_HP_HPSA_4,
173 1.1 he "HP Smart Array 4"
174 1.1 he },
175 1.1 he {
176 1.1 he PCI_VENDOR_HP,
177 1.1 he PCI_PRODUCT_HP_HPSA_5,
178 1.1 he "HP Smart Array 5"
179 1.1 he },
180 1.1 he {
181 1.1 he PCI_VENDOR_HP,
182 1.1 he PCI_PRODUCT_HP_HPSA_6,
183 1.1 he "HP Smart Array 6"
184 1.1 he },
185 1.1 he {
186 1.1 he PCI_VENDOR_HP,
187 1.1 he PCI_PRODUCT_HP_HPSA_7,
188 1.1 he "HP Smart Array 7"
189 1.1 he },
190 1.1 he {
191 1.1 he PCI_VENDOR_HP,
192 1.1 he PCI_PRODUCT_HP_HPSA_8,
193 1.1 he "HP Smart Array 8"
194 1.1 he },
195 1.1 he {
196 1.1 he PCI_VENDOR_HP,
197 1.1 he PCI_PRODUCT_HP_HPSA_9,
198 1.1 he "HP Smart Array 9"
199 1.1 he },
200 1.1 he {
201 1.1 he PCI_VENDOR_HP,
202 1.1 he PCI_PRODUCT_HP_HPSA_10,
203 1.1 he "HP Smart Array 10"
204 1.1 he },
205 1.1 he {
206 1.1 he PCI_VENDOR_HP,
207 1.1 he PCI_PRODUCT_HP_HPSA_11,
208 1.1 he "HP Smart Array 11"
209 1.1 he },
210 1.1 he {
211 1.1 he PCI_VENDOR_HP,
212 1.1 he PCI_PRODUCT_HP_HPSA_12,
213 1.1 he "HP Smart Array 12"
214 1.1 he },
215 1.1 he {
216 1.1 he PCI_VENDOR_HP,
217 1.1 he PCI_PRODUCT_HP_HPSA_13,
218 1.1 he "HP Smart Array 13"
219 1.1 he },
220 1.1 he {
221 1.1 he 0,
222 1.1 he 0,
223 1.1 he NULL
224 1.1 he }
225 1.1 he };
226 1.1 he
227 1.1 he int
228 1.2 christos ciss_pci_match(struct device *parent __unused, struct cfdata *match __unused,
229 1.2 christos void *aux)
230 1.1 he {
231 1.1 he struct pci_attach_args *pa = aux;
232 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
233 1.1 he int i;
234 1.1 he
235 1.1 he for (i = 0; ciss_pci_devices[i].vendor; i++)
236 1.1 he {
237 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
238 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
239 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
240 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
241 1.1 he return 1;
242 1.1 he }
243 1.1 he
244 1.1 he return 0;
245 1.1 he }
246 1.1 he
247 1.1 he void
248 1.2 christos ciss_pci_attach(struct device *parent __unused, struct device *self, void *aux)
249 1.1 he {
250 1.1 he struct ciss_softc *sc = (struct ciss_softc *)self;
251 1.1 he struct pci_attach_args *pa = aux;
252 1.1 he bus_size_t size, cfgsz;
253 1.1 he pci_intr_handle_t ih;
254 1.1 he const char *intrstr;
255 1.1 he int cfg_bar, memtype;
256 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
257 1.1 he int i;
258 1.1 he
259 1.1 he for (i = 0; ciss_pci_devices[i].vendor; i++)
260 1.1 he {
261 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
262 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
263 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
264 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
265 1.1 he {
266 1.1 he printf(": %s\n", ciss_pci_devices[i].name);
267 1.1 he break;
268 1.1 he }
269 1.1 he }
270 1.1 he
271 1.1 he memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, CISS_BAR);
272 1.1 he if (memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT) &&
273 1.1 he memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) {
274 1.1 he printf(": wrong BAR type\n");
275 1.1 he return;
276 1.1 he }
277 1.1 he if (pci_mapreg_map(pa, CISS_BAR, memtype, 0,
278 1.1 he &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
279 1.1 he printf(": can't map controller i/o space\n");
280 1.1 he return;
281 1.1 he }
282 1.1 he sc->sc_dmat = pa->pa_dmat;
283 1.1 he
284 1.1 he sc->iem = CISS_READYENA;
285 1.1 he reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
286 1.1 he if (PCI_VENDOR(reg) == PCI_VENDOR_COMPAQ &&
287 1.1 he (PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5i ||
288 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA532 ||
289 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5312))
290 1.1 he sc->iem = CISS_READYENAB;
291 1.1 he
292 1.1 he cfg_bar = bus_space_read_2(sc->sc_iot, sc->sc_ioh, CISS_CFG_BAR);
293 1.1 he sc->cfgoff = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_CFG_OFF);
294 1.1 he if (cfg_bar != CISS_BAR) {
295 1.1 he if (pci_mapreg_map(pa, cfg_bar, PCI_MAPREG_TYPE_MEM, 0,
296 1.1 he NULL, &sc->cfg_ioh, NULL, &cfgsz)) {
297 1.1 he printf(": can't map controller config space\n");
298 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
299 1.1 he return;
300 1.1 he }
301 1.1 he } else {
302 1.1 he sc->cfg_ioh = sc->sc_ioh;
303 1.1 he cfgsz = size;
304 1.1 he }
305 1.1 he
306 1.1 he if (sc->cfgoff + sizeof(struct ciss_config) > cfgsz) {
307 1.1 he printf(": unfit config space\n");
308 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
309 1.1 he if (cfg_bar != CISS_BAR)
310 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
311 1.1 he return;
312 1.1 he }
313 1.1 he
314 1.1 he /* disable interrupts until ready */
315 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
316 1.1 he bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) | sc->iem);
317 1.1 he
318 1.1 he if (pci_intr_map(pa, &ih)) {
319 1.1 he printf(": can't map interrupt\n");
320 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
321 1.1 he if (cfg_bar != CISS_BAR)
322 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
323 1.1 he return;
324 1.1 he }
325 1.1 he intrstr = pci_intr_string(pa->pa_pc, ih);
326 1.1 he sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, ciss_intr, sc);
327 1.1 he if (!sc->sc_ih) {
328 1.1 he printf("%s: can't establish interrupt", sc->sc_dev.dv_xname);
329 1.1 he if (intrstr)
330 1.1 he printf(" at %s", intrstr);
331 1.1 he printf("\n");
332 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
333 1.1 he if (cfg_bar != CISS_BAR)
334 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
335 1.1 he }
336 1.1 he
337 1.1 he printf("%s: interrupting at %s\n%s", sc->sc_dev.dv_xname, intrstr,
338 1.1 he sc->sc_dev.dv_xname);
339 1.1 he
340 1.1 he if (ciss_attach(sc)) {
341 1.1 he pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
342 1.1 he sc->sc_ih = NULL;
343 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
344 1.1 he if (cfg_bar != CISS_BAR)
345 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
346 1.1 he return;
347 1.1 he }
348 1.1 he
349 1.1 he /* enable interrupts now */
350 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
351 1.1 he bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
352 1.1 he }
353