ciss_pci.c revision 1.23 1 1.23 thorpej /* $NetBSD: ciss_pci.c,v 1.23 2023/12/20 05:08:34 thorpej 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.23 thorpej __KERNEL_RCSID(0, "$NetBSD: ciss_pci.c,v 1.23 2023/12/20 05:08:34 thorpej 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/device.h>
28 1.1 he
29 1.1 he #include <dev/pci/pcidevs.h>
30 1.1 he #include <dev/pci/pcivar.h>
31 1.1 he
32 1.4 ad #include <sys/bus.h>
33 1.1 he
34 1.1 he #include <dev/scsipi/scsipi_all.h>
35 1.1 he #include <dev/scsipi/scsipi_disk.h>
36 1.1 he #include <dev/scsipi/scsipiconf.h>
37 1.1 he
38 1.1 he #include <dev/ic/cissreg.h>
39 1.1 he #include <dev/ic/cissvar.h>
40 1.1 he
41 1.1 he #define CISS_BAR 0x10
42 1.1 he
43 1.7 cegger int ciss_pci_match(device_t, cfdata_t, void *);
44 1.7 cegger void ciss_pci_attach(device_t, device_t, void *);
45 1.1 he
46 1.10 chs CFATTACH_DECL_NEW(ciss_pci, sizeof(struct ciss_softc),
47 1.1 he ciss_pci_match, ciss_pci_attach, NULL, NULL);
48 1.1 he
49 1.20 jdolecek
50 1.20 jdolecek static 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.20 jdolecek #define CISS_PCI_DEVICE(v, p, d) { PCI_VENDOR_##v, PCI_PRODUCT_##v##_##p, d }
56 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA532, "Compaq Smart Array 532"),
57 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA5300, "Compaq Smart Array 5300 V1"),
58 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA5300_2, "Compaq Smart Array 5300 V2"),
59 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA5312, "Compaq Smart Array 5312"),
60 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA5i, "Compaq Smart Array 5i"),
61 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA5i_2, "Compaq Smart Array 5i V2"),
62 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA6i, "Compaq Smart Array 6i"),
63 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA641, "Compaq Smart Array 641"),
64 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA642, "Compaq Smart Array 642"),
65 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA6400, "Compaq Smart Array 6400"),
66 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA6400EM, "Compaq Smart Array 6400EM"),
67 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA6422, "Compaq Smart Array 6422"),
68 1.20 jdolecek CISS_PCI_DEVICE(COMPAQ, CSA64XX, "Compaq Smart Array 64XX"),
69 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAE200, "Smart Array E200"),
70 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAE200I_1, "HP Smart Array E200I-1"),
71 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAE200I_2, "HP Smart Array E200I-2"),
72 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAE200I_3, "HP Smart Array E200I-3"),
73 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAP600, "HP Smart Array P600"),
74 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAP800, "HP Smart Array P800"),
75 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSAV100, "HP Smart Array V100"),
76 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_1, "HP Smart Array 1"),
77 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_2, "HP Smart Array 2"),
78 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_3, "HP Smart Array 3"),
79 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_4, "HP Smart Array 4"),
80 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_5, "HP Smart Array 5"),
81 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_6, "HP Smart Array 6"),
82 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_7, "HP Smart Array 7"),
83 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_8, "HP Smart Array 8"),
84 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_9, "HP Smart Array 9"),
85 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_10, "HP Smart Array 10"),
86 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_11, "HP Smart Array 11"),
87 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_12, "HP Smart Array 12"),
88 1.20 jdolecek CISS_PCI_DEVICE(HP, HPSA_13, "HP Smart Array 13"),
89 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P700M, "Smart Array P700m"),
90 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P212, "Smart Array P212"),
91 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P410, "Smart Array P410"),
92 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P410I, "Smart Array P410i"),
93 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P411, "Smart Array P411"),
94 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P812, "Smart Array P822"),
95 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P712M, "Smart Array P712m"),
96 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_14, "Smart Array 14"),
97 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P222, "Smart Array P222"),
98 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P420, "Smart Array P420"),
99 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P421, "Smart Array P421"),
100 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P822, "Smart Array P822"),
101 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P420I, "Smart Array P420i"),
102 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P220I, "Smart Array P220i"),
103 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P721I, "Smart Array P721i"),
104 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P430I, "Smart Array P430i"),
105 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P830I, "Smart Array P830i"),
106 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P430, "Smart Array P430"),
107 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P431, "Smart Array P431"),
108 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P830, "Smart Array P830"),
109 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P731M, "Smart Array P731m"),
110 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P230I, "Smart Array P230i"),
111 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P530, "Smart Array P530"),
112 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P531, "Smart Array P531"),
113 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P244BR, "Smart Array P244br"),
114 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P741M, "Smart Array P741m"),
115 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_H240AR, "Smart Array H240ar"),
116 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P440AR, "Smart Array H440ar"),
117 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P840AR, "Smart Array P840ar"),
118 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P440, "Smart Array P440"),
119 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P441, "Smart Array P441"),
120 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P841, "Smart Array P841"),
121 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_H244BR, "Smart Array H244br"),
122 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_H240, "Smart Array H240"),
123 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_H241, "Smart Array H241"),
124 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P246BR, "Smart Array P246br"),
125 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P840, "Smart Array P840"),
126 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P542D, "Smart Array P542d"),
127 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_P240NR, "Smart Array P240nr"),
128 1.22 jdolecek CISS_PCI_DEVICE(HP, HPSA_H240NR, "Smart Array H240nr"),
129 1.1 he };
130 1.1 he
131 1.1 he int
132 1.7 cegger ciss_pci_match(device_t parent, cfdata_t match, void *aux)
133 1.1 he {
134 1.1 he struct pci_attach_args *pa = aux;
135 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
136 1.1 he int i;
137 1.1 he
138 1.20 jdolecek for (i = 0; i < __arraycount(ciss_pci_devices); i++)
139 1.1 he {
140 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
141 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
142 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
143 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
144 1.1 he return 1;
145 1.1 he }
146 1.1 he
147 1.1 he return 0;
148 1.1 he }
149 1.1 he
150 1.1 he void
151 1.7 cegger ciss_pci_attach(device_t parent, device_t self, void *aux)
152 1.1 he {
153 1.8 cegger struct ciss_softc *sc = device_private(self);
154 1.1 he struct pci_attach_args *pa = aux;
155 1.1 he bus_size_t size, cfgsz;
156 1.18 jdolecek pci_intr_handle_t *ih;
157 1.1 he const char *intrstr;
158 1.1 he int cfg_bar, memtype;
159 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
160 1.1 he int i;
161 1.11 christos char intrbuf[PCI_INTRSTR_LEN];
162 1.18 jdolecek int (*intr_handler)(void *);
163 1.1 he
164 1.10 chs sc->sc_dev = self;
165 1.10 chs
166 1.13 msaitoh aprint_naive("\n");
167 1.21 jdolecek for (i = 0; i < __arraycount(ciss_pci_devices); i++)
168 1.1 he {
169 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
170 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
171 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
172 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
173 1.1 he {
174 1.13 msaitoh aprint_normal(": %s\n", ciss_pci_devices[i].name);
175 1.1 he break;
176 1.1 he }
177 1.1 he }
178 1.1 he
179 1.1 he memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, CISS_BAR);
180 1.1 he if (memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT) &&
181 1.1 he memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) {
182 1.13 msaitoh aprint_error_dev(self, "wrong BAR type\n");
183 1.1 he return;
184 1.1 he }
185 1.1 he if (pci_mapreg_map(pa, CISS_BAR, memtype, 0,
186 1.1 he &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
187 1.13 msaitoh aprint_error_dev(self, "can't map controller i/o space\n");
188 1.1 he return;
189 1.1 he }
190 1.1 he sc->sc_dmat = pa->pa_dmat;
191 1.1 he
192 1.12 christos sc->iem = CISS_INTR_OPQ_SA5;
193 1.1 he reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
194 1.1 he if (PCI_VENDOR(reg) == PCI_VENDOR_COMPAQ &&
195 1.1 he (PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5i ||
196 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA532 ||
197 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5312))
198 1.12 christos sc->iem = CISS_INTR_OPQ_SA5B;
199 1.1 he
200 1.1 he cfg_bar = bus_space_read_2(sc->sc_iot, sc->sc_ioh, CISS_CFG_BAR);
201 1.1 he sc->cfgoff = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_CFG_OFF);
202 1.1 he if (cfg_bar != CISS_BAR) {
203 1.1 he if (pci_mapreg_map(pa, cfg_bar, PCI_MAPREG_TYPE_MEM, 0,
204 1.1 he NULL, &sc->cfg_ioh, NULL, &cfgsz)) {
205 1.13 msaitoh aprint_error_dev(self,
206 1.13 msaitoh "can't map controller config space\n");
207 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
208 1.1 he return;
209 1.1 he }
210 1.1 he } else {
211 1.1 he sc->cfg_ioh = sc->sc_ioh;
212 1.1 he cfgsz = size;
213 1.1 he }
214 1.1 he
215 1.1 he if (sc->cfgoff + sizeof(struct ciss_config) > cfgsz) {
216 1.13 msaitoh aprint_error_dev(self, "unfit config space\n");
217 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
218 1.1 he if (cfg_bar != CISS_BAR)
219 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
220 1.1 he return;
221 1.1 he }
222 1.1 he
223 1.18 jdolecek /* Read the configuration */
224 1.18 jdolecek bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
225 1.18 jdolecek (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
226 1.18 jdolecek
227 1.1 he /* disable interrupts until ready */
228 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
229 1.18 jdolecek bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) |
230 1.18 jdolecek sc->iem | CISS_INTR_OPQ | CISS_INTR_MSI);
231 1.1 he
232 1.18 jdolecek int counts[PCI_INTR_TYPE_SIZE] = {
233 1.18 jdolecek [PCI_INTR_TYPE_INTX] = 1,
234 1.18 jdolecek [PCI_INTR_TYPE_MSI] = 0,
235 1.18 jdolecek [PCI_INTR_TYPE_MSIX] = 0,
236 1.18 jdolecek };
237 1.18 jdolecek int max_type = PCI_INTR_TYPE_INTX;
238 1.18 jdolecek
239 1.18 jdolecek /*
240 1.18 jdolecek * Allow MSI/MSI-X only if PERFORMANT method is supported, SIMPLE
241 1.18 jdolecek * doesn't seem to work with MSI.
242 1.18 jdolecek */
243 1.18 jdolecek if (CISS_PERF_SUPPORTED(sc)) {
244 1.18 jdolecek #if 1
245 1.18 jdolecek counts[PCI_INTR_TYPE_MSI] = counts[PCI_INTR_TYPE_MSIX] = 1;
246 1.18 jdolecek max_type = PCI_INTR_TYPE_MSIX;
247 1.18 jdolecek #endif
248 1.18 jdolecek sc->iem |= CISS_INTR_OPQ | CISS_INTR_MSI;
249 1.18 jdolecek }
250 1.18 jdolecek
251 1.18 jdolecek if (pci_intr_alloc(pa, &ih, counts, max_type)) {
252 1.13 msaitoh aprint_error_dev(self, "can't map interrupt\n");
253 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
254 1.1 he if (cfg_bar != CISS_BAR)
255 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
256 1.1 he return;
257 1.1 he }
258 1.18 jdolecek intrstr = pci_intr_string(pa->pa_pc, ih[0], intrbuf, sizeof(intrbuf));
259 1.18 jdolecek
260 1.18 jdolecek switch (pci_intr_type(pa->pa_pc, ih[0])) {
261 1.18 jdolecek case PCI_INTR_TYPE_INTX:
262 1.18 jdolecek intr_handler = CISS_PERF_SUPPORTED(sc)
263 1.18 jdolecek ? ciss_intr_perf_intx : ciss_intr_simple_intx;
264 1.18 jdolecek break;
265 1.18 jdolecek default:
266 1.18 jdolecek KASSERT(CISS_PERF_SUPPORTED(sc));
267 1.18 jdolecek intr_handler = ciss_intr_perf_msi;
268 1.18 jdolecek break;
269 1.18 jdolecek }
270 1.18 jdolecek
271 1.18 jdolecek sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih[0], IPL_BIO,
272 1.18 jdolecek intr_handler, sc, device_xname(self));
273 1.1 he if (!sc->sc_ih) {
274 1.10 chs aprint_error_dev(sc->sc_dev, "can't establish interrupt");
275 1.1 he if (intrstr)
276 1.9 njoly aprint_error(" at %s", intrstr);
277 1.9 njoly aprint_error("\n");
278 1.18 jdolecek pci_intr_release(pa->pa_pc, ih, 1);
279 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
280 1.1 he if (cfg_bar != CISS_BAR)
281 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
282 1.18 jdolecek return;
283 1.1 he }
284 1.18 jdolecek aprint_normal_dev(self, "interrupting at %s\n", intrstr);
285 1.1 he
286 1.18 jdolecek aprint_normal("%s", device_xname(sc->sc_dev));
287 1.1 he if (ciss_attach(sc)) {
288 1.1 he pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
289 1.1 he sc->sc_ih = NULL;
290 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
291 1.1 he if (cfg_bar != CISS_BAR)
292 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
293 1.1 he return;
294 1.1 he }
295 1.1 he
296 1.1 he /* enable interrupts now */
297 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
298 1.1 he bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
299 1.1 he }
300