ciss_pci.c revision 1.18 1 1.18 jdolecek /* $NetBSD: ciss_pci.c,v 1.18 2020/07/14 10:37:30 jdolecek 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.18 jdolecek __KERNEL_RCSID(0, "$NetBSD: ciss_pci.c,v 1.18 2020/07/14 10:37:30 jdolecek 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.4 ad #include <sys/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.7 cegger int ciss_pci_match(device_t, cfdata_t, void *);
45 1.7 cegger void ciss_pci_attach(device_t, device_t, void *);
46 1.1 he
47 1.10 chs CFATTACH_DECL_NEW(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.13 msaitoh 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.7 cegger ciss_pci_match(device_t parent, cfdata_t match, void *aux)
229 1.1 he {
230 1.1 he struct pci_attach_args *pa = aux;
231 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
232 1.1 he int i;
233 1.1 he
234 1.1 he for (i = 0; ciss_pci_devices[i].vendor; i++)
235 1.1 he {
236 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
237 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
238 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
239 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
240 1.1 he return 1;
241 1.1 he }
242 1.1 he
243 1.1 he return 0;
244 1.1 he }
245 1.1 he
246 1.14 joerg #ifdef CISS_NO_INTERRUPT_HACK
247 1.14 joerg static void
248 1.14 joerg ciss_intr_wrapper(void *sc_)
249 1.14 joerg {
250 1.14 joerg struct ciss_softc *sc = sc_;
251 1.14 joerg int s;
252 1.14 joerg
253 1.14 joerg s = splbio();
254 1.14 joerg ciss_intr(sc);
255 1.14 joerg splx(s);
256 1.14 joerg callout_schedule(&sc->sc_interrupt_hack, 1);
257 1.14 joerg }
258 1.14 joerg #endif
259 1.14 joerg
260 1.1 he void
261 1.7 cegger ciss_pci_attach(device_t parent, device_t self, void *aux)
262 1.1 he {
263 1.8 cegger struct ciss_softc *sc = device_private(self);
264 1.1 he struct pci_attach_args *pa = aux;
265 1.1 he bus_size_t size, cfgsz;
266 1.18 jdolecek pci_intr_handle_t *ih;
267 1.1 he const char *intrstr;
268 1.1 he int cfg_bar, memtype;
269 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
270 1.1 he int i;
271 1.11 christos char intrbuf[PCI_INTRSTR_LEN];
272 1.18 jdolecek int (*intr_handler)(void *);
273 1.1 he
274 1.14 joerg #ifdef CISS_NO_INTERRUPT_HACK
275 1.14 joerg callout_init(&sc->sc_interrupt_hack, 0);
276 1.14 joerg callout_setfunc(&sc->sc_interrupt_hack, ciss_intr_wrapper, sc);
277 1.14 joerg #endif
278 1.10 chs sc->sc_dev = self;
279 1.10 chs
280 1.13 msaitoh aprint_naive("\n");
281 1.1 he for (i = 0; ciss_pci_devices[i].vendor; i++)
282 1.1 he {
283 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
284 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
285 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
286 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
287 1.1 he {
288 1.13 msaitoh aprint_normal(": %s\n", ciss_pci_devices[i].name);
289 1.1 he break;
290 1.1 he }
291 1.1 he }
292 1.1 he
293 1.1 he memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, CISS_BAR);
294 1.1 he if (memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT) &&
295 1.1 he memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) {
296 1.13 msaitoh aprint_error_dev(self, "wrong BAR type\n");
297 1.1 he return;
298 1.1 he }
299 1.1 he if (pci_mapreg_map(pa, CISS_BAR, memtype, 0,
300 1.1 he &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
301 1.13 msaitoh aprint_error_dev(self, "can't map controller i/o space\n");
302 1.1 he return;
303 1.1 he }
304 1.1 he sc->sc_dmat = pa->pa_dmat;
305 1.1 he
306 1.12 christos sc->iem = CISS_INTR_OPQ_SA5;
307 1.1 he reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
308 1.1 he if (PCI_VENDOR(reg) == PCI_VENDOR_COMPAQ &&
309 1.1 he (PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5i ||
310 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA532 ||
311 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5312))
312 1.12 christos sc->iem = CISS_INTR_OPQ_SA5B;
313 1.1 he
314 1.1 he cfg_bar = bus_space_read_2(sc->sc_iot, sc->sc_ioh, CISS_CFG_BAR);
315 1.1 he sc->cfgoff = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_CFG_OFF);
316 1.1 he if (cfg_bar != CISS_BAR) {
317 1.1 he if (pci_mapreg_map(pa, cfg_bar, PCI_MAPREG_TYPE_MEM, 0,
318 1.1 he NULL, &sc->cfg_ioh, NULL, &cfgsz)) {
319 1.13 msaitoh aprint_error_dev(self,
320 1.13 msaitoh "can't map controller config space\n");
321 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
322 1.1 he return;
323 1.1 he }
324 1.1 he } else {
325 1.1 he sc->cfg_ioh = sc->sc_ioh;
326 1.1 he cfgsz = size;
327 1.1 he }
328 1.1 he
329 1.1 he if (sc->cfgoff + sizeof(struct ciss_config) > cfgsz) {
330 1.13 msaitoh aprint_error_dev(self, "unfit config space\n");
331 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
332 1.1 he if (cfg_bar != CISS_BAR)
333 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
334 1.1 he return;
335 1.1 he }
336 1.1 he
337 1.18 jdolecek /* Read the configuration */
338 1.18 jdolecek bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
339 1.18 jdolecek (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
340 1.18 jdolecek
341 1.1 he /* disable interrupts until ready */
342 1.14 joerg #ifndef CISS_NO_INTERRUPT_HACK
343 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
344 1.18 jdolecek bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) |
345 1.18 jdolecek sc->iem | CISS_INTR_OPQ | CISS_INTR_MSI);
346 1.14 joerg #endif
347 1.1 he
348 1.18 jdolecek int counts[PCI_INTR_TYPE_SIZE] = {
349 1.18 jdolecek [PCI_INTR_TYPE_INTX] = 1,
350 1.18 jdolecek [PCI_INTR_TYPE_MSI] = 0,
351 1.18 jdolecek [PCI_INTR_TYPE_MSIX] = 0,
352 1.18 jdolecek };
353 1.18 jdolecek int max_type = PCI_INTR_TYPE_INTX;
354 1.18 jdolecek
355 1.18 jdolecek /*
356 1.18 jdolecek * Allow MSI/MSI-X only if PERFORMANT method is supported, SIMPLE
357 1.18 jdolecek * doesn't seem to work with MSI.
358 1.18 jdolecek */
359 1.18 jdolecek if (CISS_PERF_SUPPORTED(sc)) {
360 1.18 jdolecek #if 1
361 1.18 jdolecek counts[PCI_INTR_TYPE_MSI] = counts[PCI_INTR_TYPE_MSIX] = 1;
362 1.18 jdolecek max_type = PCI_INTR_TYPE_MSIX;
363 1.18 jdolecek #endif
364 1.18 jdolecek sc->iem |= CISS_INTR_OPQ | CISS_INTR_MSI;
365 1.18 jdolecek }
366 1.18 jdolecek
367 1.18 jdolecek if (pci_intr_alloc(pa, &ih, counts, max_type)) {
368 1.13 msaitoh aprint_error_dev(self, "can't map interrupt\n");
369 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
370 1.1 he if (cfg_bar != CISS_BAR)
371 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
372 1.1 he return;
373 1.1 he }
374 1.18 jdolecek intrstr = pci_intr_string(pa->pa_pc, ih[0], intrbuf, sizeof(intrbuf));
375 1.18 jdolecek
376 1.18 jdolecek switch (pci_intr_type(pa->pa_pc, ih[0])) {
377 1.18 jdolecek case PCI_INTR_TYPE_INTX:
378 1.18 jdolecek intr_handler = CISS_PERF_SUPPORTED(sc)
379 1.18 jdolecek ? ciss_intr_perf_intx : ciss_intr_simple_intx;
380 1.18 jdolecek break;
381 1.18 jdolecek default:
382 1.18 jdolecek KASSERT(CISS_PERF_SUPPORTED(sc));
383 1.18 jdolecek intr_handler = ciss_intr_perf_msi;
384 1.18 jdolecek break;
385 1.18 jdolecek }
386 1.18 jdolecek
387 1.18 jdolecek sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih[0], IPL_BIO,
388 1.18 jdolecek intr_handler, sc, device_xname(self));
389 1.1 he if (!sc->sc_ih) {
390 1.10 chs aprint_error_dev(sc->sc_dev, "can't establish interrupt");
391 1.1 he if (intrstr)
392 1.9 njoly aprint_error(" at %s", intrstr);
393 1.9 njoly aprint_error("\n");
394 1.18 jdolecek pci_intr_release(pa->pa_pc, ih, 1);
395 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
396 1.1 he if (cfg_bar != CISS_BAR)
397 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
398 1.18 jdolecek return;
399 1.1 he }
400 1.18 jdolecek aprint_normal_dev(self, "interrupting at %s\n", intrstr);
401 1.1 he
402 1.18 jdolecek aprint_normal("%s", device_xname(sc->sc_dev));
403 1.1 he if (ciss_attach(sc)) {
404 1.1 he pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
405 1.1 he sc->sc_ih = NULL;
406 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
407 1.1 he if (cfg_bar != CISS_BAR)
408 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
409 1.1 he return;
410 1.1 he }
411 1.1 he
412 1.14 joerg #ifndef CISS_NO_INTERRUPT_HACK
413 1.1 he /* enable interrupts now */
414 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
415 1.1 he bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
416 1.14 joerg #else
417 1.14 joerg callout_schedule(&sc->sc_interrupt_hack, 1);
418 1.14 joerg #endif
419 1.1 he }
420