mfi_pci.c revision 1.20.4.1 1 /* $NetBSD: mfi_pci.c,v 1.20.4.1 2022/05/17 10:29:47 bouyer Exp $ */
2 /* $OpenBSD: mfi_pci.c,v 1.11 2006/08/06 04:40:08 brad Exp $ */
3 /*
4 * Copyright (c) 2006 Marco Peereboom <marco (at) peereboom.us>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/cdefs.h>
20 __KERNEL_RCSID(0, "$NetBSD: mfi_pci.c,v 1.20.4.1 2022/05/17 10:29:47 bouyer Exp $");
21
22 #include <sys/param.h>
23 #include <sys/systm.h>
24 #include <sys/kernel.h>
25 #include <sys/malloc.h>
26 #include <sys/device.h>
27
28 #include <dev/pci/pcidevs.h>
29 #include <dev/pci/pcivar.h>
30
31 #include <sys/bus.h>
32
33 #include <dev/scsipi/scsipi_all.h>
34 #include <dev/scsipi/scsipi_disk.h>
35 #include <dev/scsipi/scsiconf.h>
36
37 #include <dev/ic/mfireg.h>
38 #include <dev/ic/mfivar.h>
39
40 #define MFI_BAR 0x10
41 #define MFI_BAR_GEN2 0x14
42 #define MFI_PCI_MEMSIZE 0x2000 /* 8k */
43
44 struct mfi_pci_softc {
45 struct mfi_softc psc_sc;
46 pci_chipset_tag_t psc_pc;
47 };
48
49 const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *);
50 int mfi_pci_match(device_t, cfdata_t, void *);
51 void mfi_pci_attach(device_t, device_t, void *);
52 int mfi_pci_detach(device_t, int);
53
54 CFATTACH_DECL3_NEW(mfi_pci, sizeof(struct mfi_pci_softc),
55 mfi_pci_match, mfi_pci_attach, mfi_pci_detach, NULL, mfi_rescan,
56 mfi_childdetached, DVF_DETACH_SHUTDOWN);
57
58 struct mfi_pci_subtype {
59 pcireg_t st_vendor;
60 pcireg_t st_product;
61 const char *st_string;
62 };
63
64 static const struct mfi_pci_subtype mfi_1078_subtypes[] = {
65 { PCI_VENDOR_SYMBIOS, 0x1006, "SAS 8888ELP" },
66 { PCI_VENDOR_SYMBIOS, 0x100a, "SAS 8708ELP" },
67 { PCI_VENDOR_SYMBIOS, 0x100f, "SAS 8708E" },
68 { PCI_VENDOR_SYMBIOS, 0x1012, "SAS 8704ELP" },
69 { PCI_VENDOR_SYMBIOS, 0x1013, "SAS 8708EM2" },
70 { PCI_VENDOR_SYMBIOS, 0x1016, "SAS 8880EM2" },
71 { PCI_VENDOR_DELL, 0x1f0a, "Dell PERC 6/e" },
72 { PCI_VENDOR_DELL, 0x1f0b, "Dell PERC 6/i" },
73 { PCI_VENDOR_DELL, 0x1f0c, "Dell PERC 6/i integrated" },
74 { PCI_VENDOR_DELL, 0x1f0d, "Dell CERC 6/i" },
75 { PCI_VENDOR_DELL, 0x1f11, "Dell CERC 6/i integrated" },
76 { 0, 0, NULL }
77 };
78
79 static const struct mfi_pci_subtype mfi_perc5_subtypes[] = {
80 { PCI_VENDOR_DELL, 0x1f01, "Dell PERC 5/e" },
81 { PCI_VENDOR_DELL, 0x1f02, "Dell PERC 5/i" },
82 { PCI_VENDOR_DELL, 0x1f03, "Dell PERC 5/i integrated" },
83 { 0, 0, NULL }
84 };
85
86 static const struct mfi_pci_subtype mfi_gen2_subtypes[] = {
87 { PCI_VENDOR_SYMBIOS, 0x9261, "SAS 9260-8i" },
88 { PCI_VENDOR_SYMBIOS, 0x9263, "SAS 9261-8i" },
89 { PCI_VENDOR_IBM, 0x03c7, "IBM ServeRAID M5014 SAS/SATA" },
90 { PCI_VENDOR_DELL, 0x1f15, "Dell PERC H800 Adapter" },
91 { PCI_VENDOR_DELL, 0x1f16, "Dell PERC H700 Adapter" },
92 { PCI_VENDOR_DELL, 0x1f17, "Dell PERC H700 Integrated" },
93 { PCI_VENDOR_DELL, 0x1f18, "Dell PERC H700 Modular" },
94 { PCI_VENDOR_DELL, 0x1f19, "Dell PERC H700" },
95 { PCI_VENDOR_DELL, 0x1f1a, "Dell PERC H800 Proto Adapter" },
96 { PCI_VENDOR_DELL, 0x1f1b, "Dell PERC H800" },
97 { 0x0, 0, "" }
98 };
99
100 static const struct mfi_pci_subtype mfi_skinny_subtypes[] = {
101 { PCI_VENDOR_IBM, 0x03b1, "IBM ServeRAID M1015 SAS/SATA" },
102 { PCI_VENDOR_DELL, 0x1f78, "Dell PERC H310" },
103 { 0x0, 0, "" }
104 };
105
106 static const struct mfi_pci_subtype mfi_tbolt_subtypes[] = {
107 { PCI_VENDOR_SYMBIOS, 0x9265, "SAS 9265-8i" },
108 { PCI_VENDOR_DELL, 0x1f2d, "Dell PERC H810 Adapter" },
109 { PCI_VENDOR_DELL, 0x1f30, "Dell PERC H710 Embedded" },
110 { PCI_VENDOR_DELL, 0x1f31, "Dell PERC H710P Adapter" },
111 { PCI_VENDOR_DELL, 0x1f33, "Dell PERC H710P Mini (blades)" },
112 { PCI_VENDOR_DELL, 0x1f34, "Dell PERC H710P Mini (blades)" },
113 { PCI_VENDOR_DELL, 0x1f35, "Dell PERC H710 Adapter" },
114 { PCI_VENDOR_DELL, 0x1f37, "Dell PERC H710 Mini (blades)" },
115 { PCI_VENDOR_DELL, 0x1f38, "Dell PERC H710 Mini (monolithics)" },
116 { PCI_VENDOR_INTEL, 0x9265, "Intel (R) RAID Controller RS25DB080" },
117 { PCI_VENDOR_INTEL, 0x9285, "Intel (R) RAID Controller RS25NB008" },
118 { 0x0, 0, "" }
119 };
120
121 static const
122 struct mfi_pci_device {
123 pcireg_t mpd_vendor;
124 pcireg_t mpd_product;
125 enum mfi_iop mpd_iop;
126 const struct mfi_pci_subtype *mpd_subtype;
127 } mfi_pci_devices[] = {
128 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
129 MFI_IOP_XSCALE, NULL },
130 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
131 MFI_IOP_XSCALE, NULL },
132 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1078,
133 MFI_IOP_PPC, mfi_1078_subtypes },
134 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1078DE,
135 MFI_IOP_PPC, mfi_1078_subtypes },
136 { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_5,
137 MFI_IOP_XSCALE, mfi_perc5_subtypes },
138 { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_6,
139 MFI_IOP_PPC, mfi_1078_subtypes },
140 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_1,
141 MFI_IOP_GEN2, mfi_gen2_subtypes },
142 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_2,
143 MFI_IOP_GEN2, mfi_gen2_subtypes },
144 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2008_1,
145 MFI_IOP_SKINNY, mfi_skinny_subtypes },
146 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_2208,
147 MFI_IOP_TBOLT, mfi_tbolt_subtypes },
148 };
149
150 const struct mfi_pci_device *
151 mfi_pci_find_device(struct pci_attach_args *pa)
152 {
153 const struct mfi_pci_device *mpd;
154 int i;
155
156 for (i = 0; i < __arraycount(mfi_pci_devices); i++) {
157 mpd = &mfi_pci_devices[i];
158
159 if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) &&
160 mpd->mpd_product == PCI_PRODUCT(pa->pa_id))
161 return mpd;
162 }
163
164 return NULL;
165 }
166
167 int
168 mfi_pci_match(device_t parent, cfdata_t match, void *aux)
169 {
170 return (mfi_pci_find_device(aux) != NULL) ? 1 : 0;
171 }
172
173 int
174 mfi_pci_detach(device_t self, int flags)
175 {
176 struct mfi_pci_softc *psc = device_private(self);
177 struct mfi_softc *sc = &psc->psc_sc;
178 int error;
179
180 if ((error = mfi_detach(sc, flags)) != 0)
181 return error;
182
183 /* xxx */
184 pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
185 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
186 return 0;
187 }
188
189 void
190 mfi_pci_attach(device_t parent, device_t self, void *aux)
191 {
192 struct mfi_pci_softc *psc = device_private(self);
193 struct mfi_softc *sc = &psc->psc_sc;
194 struct pci_attach_args *pa = aux;
195 const struct mfi_pci_device *mpd;
196 const struct mfi_pci_subtype *st;
197 const char *intrstr;
198 pci_intr_handle_t ih;
199 pcireg_t csr;
200 int regbar;
201 const char *subtype = NULL;
202 uint32_t subsysid;
203 char intrbuf[PCI_INTRSTR_LEN];
204
205 sc->sc_dev = self;
206 psc->psc_pc = pa->pa_pc;
207
208 mpd = mfi_pci_find_device(pa);
209 if (mpd == NULL) {
210 aprint_error(": can't find matching pci device\n");
211 return;
212 }
213
214 if (mpd->mpd_iop == MFI_IOP_GEN2 || mpd->mpd_iop == MFI_IOP_SKINNY ||
215 mpd->mpd_iop == MFI_IOP_TBOLT)
216 regbar = MFI_BAR_GEN2;
217 else
218 regbar = MFI_BAR;
219
220 csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, regbar);
221 csr |= PCI_MAPREG_MEM_TYPE_32BIT;
222 if (pci_mapreg_map(pa, regbar, csr, 0,
223 &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_size)) {
224 aprint_error(": can't map controller pci space\n");
225 return;
226 }
227
228 sc->sc_dmat = pa->pa_dmat;
229 if (mpd->mpd_iop == MFI_IOP_TBOLT && pci_dma64_available(pa)) {
230 sc->sc_datadmat = pa->pa_dmat64;
231 sc->sc_64bit_dma = 1;
232 } else {
233 sc->sc_datadmat = pa->pa_dmat;
234 sc->sc_64bit_dma = 0;
235 }
236
237 if (pci_intr_map(pa, &ih)) {
238 aprint_error(": can't map interrupt\n");
239 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
240 return;
241 }
242 intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
243 if (mpd->mpd_iop == MFI_IOP_TBOLT) {
244 sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_BIO,
245 mfi_tbolt_intrh, sc, device_xname(self));
246 } else {
247 sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_BIO,
248 mfi_intr, sc, device_xname(self));
249 }
250 if (!sc->sc_ih) {
251 aprint_error(": can't establish interrupt");
252 if (intrstr)
253 aprint_error(" at %s", intrstr);
254 aprint_error("\n");
255 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
256 return;
257 }
258
259 subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
260 if (mpd->mpd_subtype != NULL) {
261 st = mpd->mpd_subtype;
262 while (st->st_vendor != 0) {
263 if (PCI_VENDOR(subsysid) == st->st_vendor &&
264 PCI_PRODUCT(subsysid) == st->st_product) {
265 subtype = st->st_string;
266 break;
267 }
268 st++;
269 }
270 if (subtype) {
271 aprint_normal(": %s\n", subtype);
272 } else {
273 aprint_normal(": vendor 0x%x product 0x%x\n",
274 PCI_VENDOR(subsysid), PCI_PRODUCT(subsysid));
275 }
276 }
277
278 aprint_normal("%s: interrupting at %s\n", DEVNAME(sc), intrstr);
279
280 if (mfi_attach(sc, mpd->mpd_iop)) {
281 aprint_error("%s: can't attach", DEVNAME(sc));
282 pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
283 sc->sc_ih = NULL;
284 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
285 }
286 }
287