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