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