ciss_pci.c revision 1.20 1 1.20 jdolecek /* $NetBSD: ciss_pci.c,v 1.20 2020/07/14 17:19:03 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.20 jdolecek __KERNEL_RCSID(0, "$NetBSD: ciss_pci.c,v 1.20 2020/07/14 17:19:03 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.1 he };
91 1.1 he
92 1.1 he int
93 1.7 cegger ciss_pci_match(device_t parent, cfdata_t match, void *aux)
94 1.1 he {
95 1.1 he struct pci_attach_args *pa = aux;
96 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
97 1.1 he int i;
98 1.1 he
99 1.20 jdolecek for (i = 0; i < __arraycount(ciss_pci_devices); i++)
100 1.1 he {
101 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
102 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
103 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
104 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
105 1.1 he return 1;
106 1.1 he }
107 1.1 he
108 1.1 he return 0;
109 1.1 he }
110 1.1 he
111 1.1 he void
112 1.7 cegger ciss_pci_attach(device_t parent, device_t self, void *aux)
113 1.1 he {
114 1.8 cegger struct ciss_softc *sc = device_private(self);
115 1.1 he struct pci_attach_args *pa = aux;
116 1.1 he bus_size_t size, cfgsz;
117 1.18 jdolecek pci_intr_handle_t *ih;
118 1.1 he const char *intrstr;
119 1.1 he int cfg_bar, memtype;
120 1.1 he pcireg_t reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
121 1.1 he int i;
122 1.11 christos char intrbuf[PCI_INTRSTR_LEN];
123 1.18 jdolecek int (*intr_handler)(void *);
124 1.1 he
125 1.10 chs sc->sc_dev = self;
126 1.10 chs
127 1.13 msaitoh aprint_naive("\n");
128 1.20 jdolecek for (i = 0; i < __arraycount(ciss_pci_devices[i]); i++)
129 1.1 he {
130 1.1 he if ((PCI_VENDOR(pa->pa_id) == ciss_pci_devices[i].vendor &&
131 1.1 he PCI_PRODUCT(pa->pa_id) == ciss_pci_devices[i].product) ||
132 1.1 he (PCI_VENDOR(reg) == ciss_pci_devices[i].vendor &&
133 1.1 he PCI_PRODUCT(reg) == ciss_pci_devices[i].product))
134 1.1 he {
135 1.13 msaitoh aprint_normal(": %s\n", ciss_pci_devices[i].name);
136 1.1 he break;
137 1.1 he }
138 1.1 he }
139 1.1 he
140 1.1 he memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, CISS_BAR);
141 1.1 he if (memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT) &&
142 1.1 he memtype != (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) {
143 1.13 msaitoh aprint_error_dev(self, "wrong BAR type\n");
144 1.1 he return;
145 1.1 he }
146 1.1 he if (pci_mapreg_map(pa, CISS_BAR, memtype, 0,
147 1.1 he &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
148 1.13 msaitoh aprint_error_dev(self, "can't map controller i/o space\n");
149 1.1 he return;
150 1.1 he }
151 1.1 he sc->sc_dmat = pa->pa_dmat;
152 1.1 he
153 1.12 christos sc->iem = CISS_INTR_OPQ_SA5;
154 1.1 he reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
155 1.1 he if (PCI_VENDOR(reg) == PCI_VENDOR_COMPAQ &&
156 1.1 he (PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5i ||
157 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA532 ||
158 1.1 he PCI_PRODUCT(reg) == PCI_PRODUCT_COMPAQ_CSA5312))
159 1.12 christos sc->iem = CISS_INTR_OPQ_SA5B;
160 1.1 he
161 1.1 he cfg_bar = bus_space_read_2(sc->sc_iot, sc->sc_ioh, CISS_CFG_BAR);
162 1.1 he sc->cfgoff = bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_CFG_OFF);
163 1.1 he if (cfg_bar != CISS_BAR) {
164 1.1 he if (pci_mapreg_map(pa, cfg_bar, PCI_MAPREG_TYPE_MEM, 0,
165 1.1 he NULL, &sc->cfg_ioh, NULL, &cfgsz)) {
166 1.13 msaitoh aprint_error_dev(self,
167 1.13 msaitoh "can't map controller config space\n");
168 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
169 1.1 he return;
170 1.1 he }
171 1.1 he } else {
172 1.1 he sc->cfg_ioh = sc->sc_ioh;
173 1.1 he cfgsz = size;
174 1.1 he }
175 1.1 he
176 1.1 he if (sc->cfgoff + sizeof(struct ciss_config) > cfgsz) {
177 1.13 msaitoh aprint_error_dev(self, "unfit config space\n");
178 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
179 1.1 he if (cfg_bar != CISS_BAR)
180 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
181 1.1 he return;
182 1.1 he }
183 1.1 he
184 1.18 jdolecek /* Read the configuration */
185 1.18 jdolecek bus_space_read_region_4(sc->sc_iot, sc->cfg_ioh, sc->cfgoff,
186 1.18 jdolecek (u_int32_t *)&sc->cfg, sizeof(sc->cfg) / 4);
187 1.18 jdolecek
188 1.1 he /* disable interrupts until ready */
189 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
190 1.18 jdolecek bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) |
191 1.18 jdolecek sc->iem | CISS_INTR_OPQ | CISS_INTR_MSI);
192 1.1 he
193 1.18 jdolecek int counts[PCI_INTR_TYPE_SIZE] = {
194 1.18 jdolecek [PCI_INTR_TYPE_INTX] = 1,
195 1.18 jdolecek [PCI_INTR_TYPE_MSI] = 0,
196 1.18 jdolecek [PCI_INTR_TYPE_MSIX] = 0,
197 1.18 jdolecek };
198 1.18 jdolecek int max_type = PCI_INTR_TYPE_INTX;
199 1.18 jdolecek
200 1.18 jdolecek /*
201 1.18 jdolecek * Allow MSI/MSI-X only if PERFORMANT method is supported, SIMPLE
202 1.18 jdolecek * doesn't seem to work with MSI.
203 1.18 jdolecek */
204 1.18 jdolecek if (CISS_PERF_SUPPORTED(sc)) {
205 1.18 jdolecek #if 1
206 1.18 jdolecek counts[PCI_INTR_TYPE_MSI] = counts[PCI_INTR_TYPE_MSIX] = 1;
207 1.18 jdolecek max_type = PCI_INTR_TYPE_MSIX;
208 1.18 jdolecek #endif
209 1.18 jdolecek sc->iem |= CISS_INTR_OPQ | CISS_INTR_MSI;
210 1.18 jdolecek }
211 1.18 jdolecek
212 1.18 jdolecek if (pci_intr_alloc(pa, &ih, counts, max_type)) {
213 1.13 msaitoh aprint_error_dev(self, "can't map interrupt\n");
214 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
215 1.1 he if (cfg_bar != CISS_BAR)
216 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
217 1.1 he return;
218 1.1 he }
219 1.18 jdolecek intrstr = pci_intr_string(pa->pa_pc, ih[0], intrbuf, sizeof(intrbuf));
220 1.18 jdolecek
221 1.18 jdolecek switch (pci_intr_type(pa->pa_pc, ih[0])) {
222 1.18 jdolecek case PCI_INTR_TYPE_INTX:
223 1.18 jdolecek intr_handler = CISS_PERF_SUPPORTED(sc)
224 1.18 jdolecek ? ciss_intr_perf_intx : ciss_intr_simple_intx;
225 1.18 jdolecek break;
226 1.18 jdolecek default:
227 1.18 jdolecek KASSERT(CISS_PERF_SUPPORTED(sc));
228 1.18 jdolecek intr_handler = ciss_intr_perf_msi;
229 1.18 jdolecek break;
230 1.18 jdolecek }
231 1.18 jdolecek
232 1.18 jdolecek sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih[0], IPL_BIO,
233 1.18 jdolecek intr_handler, sc, device_xname(self));
234 1.1 he if (!sc->sc_ih) {
235 1.10 chs aprint_error_dev(sc->sc_dev, "can't establish interrupt");
236 1.1 he if (intrstr)
237 1.9 njoly aprint_error(" at %s", intrstr);
238 1.9 njoly aprint_error("\n");
239 1.18 jdolecek pci_intr_release(pa->pa_pc, ih, 1);
240 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
241 1.1 he if (cfg_bar != CISS_BAR)
242 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
243 1.18 jdolecek return;
244 1.1 he }
245 1.18 jdolecek aprint_normal_dev(self, "interrupting at %s\n", intrstr);
246 1.1 he
247 1.18 jdolecek aprint_normal("%s", device_xname(sc->sc_dev));
248 1.1 he if (ciss_attach(sc)) {
249 1.1 he pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
250 1.1 he sc->sc_ih = NULL;
251 1.1 he bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
252 1.1 he if (cfg_bar != CISS_BAR)
253 1.1 he bus_space_unmap(sc->sc_iot, sc->cfg_ioh, cfgsz);
254 1.1 he return;
255 1.1 he }
256 1.1 he
257 1.1 he /* enable interrupts now */
258 1.1 he bus_space_write_4(sc->sc_iot, sc->sc_ioh, CISS_IMR,
259 1.1 he bus_space_read_4(sc->sc_iot, sc->sc_ioh, CISS_IMR) & ~sc->iem);
260 1.1 he }
261