xref: /freebsd/sys/dev/mps/mps_pci.c (revision e17f5b1d307b7b8910d67883e57a9604305906d5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Yahoo! Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /* PCI/PCI-X/PCIe bus interface for the Avago Tech (LSI) MPT2 controllers */
33 
34 /* TODO Move headers to mpsvar */
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/conf.h>
42 #include <sys/malloc.h>
43 #include <sys/sysctl.h>
44 #include <sys/uio.h>
45 
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <sys/rman.h>
49 
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pci_private.h>
53 
54 #include <dev/mps/mpi/mpi2_type.h>
55 #include <dev/mps/mpi/mpi2.h>
56 #include <dev/mps/mpi/mpi2_ioc.h>
57 #include <dev/mps/mpi/mpi2_cnfg.h>
58 #include <dev/mps/mpi/mpi2_tool.h>
59 
60 #include <sys/queue.h>
61 #include <sys/kthread.h>
62 #include <dev/mps/mps_ioctl.h>
63 #include <dev/mps/mpsvar.h>
64 
65 static int	mps_pci_probe(device_t);
66 static int	mps_pci_attach(device_t);
67 static int	mps_pci_detach(device_t);
68 static int	mps_pci_suspend(device_t);
69 static int	mps_pci_resume(device_t);
70 static void	mps_pci_free(struct mps_softc *);
71 static int	mps_alloc_msix(struct mps_softc *sc, int msgs);
72 static int	mps_alloc_msi(struct mps_softc *sc, int msgs);
73 static int	mps_pci_alloc_interrupts(struct mps_softc *sc);
74 
75 static device_method_t mps_methods[] = {
76 	DEVMETHOD(device_probe,		mps_pci_probe),
77 	DEVMETHOD(device_attach,	mps_pci_attach),
78 	DEVMETHOD(device_detach,	mps_pci_detach),
79 	DEVMETHOD(device_suspend,	mps_pci_suspend),
80 	DEVMETHOD(device_resume,	mps_pci_resume),
81 
82 	DEVMETHOD_END
83 };
84 
85 static driver_t mps_pci_driver = {
86 	"mps",
87 	mps_methods,
88 	sizeof(struct mps_softc)
89 };
90 
91 struct mps_ident {
92 	uint16_t	vendor;
93 	uint16_t	device;
94 	uint16_t	subvendor;
95 	uint16_t	subdevice;
96 	u_int		flags;
97 	const char	*desc;
98 } mps_identifiers[] = {
99 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
100 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2004" },
101 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
102 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2008" },
103 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
104 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2108" },
105 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
106 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2108" },
107 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
108 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2108" },
109 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
110 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2116" },
111 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
112 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2116" },
113 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
114 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
115 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
116 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
117 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
118 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
119 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
120 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
121 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
122 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
123 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
124 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2208" },
125 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
126 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2308" },
127 	// Add Customer specific vender/subdevice id before generic
128 	// (0xffff) vender/subdevice id.
129 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
130 	    0x8086, 0x3516, 0, "Intel(R) Integrated RAID Module RMS25JB080" },
131 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
132 	    0x8086, 0x3517, 0, "Intel(R) Integrated RAID Module RMS25JB040" },
133 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
134 	    0x8086, 0x3518, 0, "Intel(R) Integrated RAID Module RMS25KB080" },
135 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
136 	    0x8086, 0x3519, 0, "Intel(R) Integrated RAID Module RMS25KB040" },
137 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
138 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2308" },
139 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
140 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SAS2308" },
141 	{ MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
142 	    0xffff, 0xffff, 0, "Avago Technologies (LSI) SSS6200" },
143 	{ 0, 0, 0, 0, 0, NULL }
144 };
145 
146 static devclass_t	mps_devclass;
147 DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, 0, 0);
148 MODULE_DEPEND(mps, cam, 1, 1, 1);
149 MODULE_PNP_INFO("U16:vendor;U16:device;U16:subvendor;U16:subdevice", pci, mps,
150     mps_identifiers, nitems(mps_identifiers) - 1);
151 static struct mps_ident *
152 mps_find_ident(device_t dev)
153 {
154 	struct mps_ident *m;
155 
156 	for (m = mps_identifiers; m->vendor != 0; m++) {
157 		if (m->vendor != pci_get_vendor(dev))
158 			continue;
159 		if (m->device != pci_get_device(dev))
160 			continue;
161 		if ((m->subvendor != 0xffff) &&
162 		    (m->subvendor != pci_get_subvendor(dev)))
163 			continue;
164 		if ((m->subdevice != 0xffff) &&
165 		    (m->subdevice != pci_get_subdevice(dev)))
166 			continue;
167 		return (m);
168 	}
169 
170 	return (NULL);
171 }
172 
173 static int
174 mps_pci_probe(device_t dev)
175 {
176 	struct mps_ident *id;
177 
178 	if ((id = mps_find_ident(dev)) != NULL) {
179 		device_set_desc(dev, id->desc);
180 		return (BUS_PROBE_DEFAULT);
181 	}
182 	return (ENXIO);
183 }
184 
185 static int
186 mps_pci_attach(device_t dev)
187 {
188 	struct mps_softc *sc;
189 	struct mps_ident *m;
190 	int error;
191 
192 	sc = device_get_softc(dev);
193 	bzero(sc, sizeof(*sc));
194 	sc->mps_dev = dev;
195 	m = mps_find_ident(dev);
196 	sc->mps_flags = m->flags;
197 
198 	mps_get_tunables(sc);
199 
200 	/* Twiddle basic PCI config bits for a sanity check */
201 	pci_enable_busmaster(dev);
202 
203 	/* Allocate the System Interface Register Set */
204 	sc->mps_regs_rid = PCIR_BAR(1);
205 	if ((sc->mps_regs_resource = bus_alloc_resource_any(dev,
206 	    SYS_RES_MEMORY, &sc->mps_regs_rid, RF_ACTIVE)) == NULL) {
207 		mps_printf(sc, "Cannot allocate PCI registers\n");
208 		return (ENXIO);
209 	}
210 	sc->mps_btag = rman_get_bustag(sc->mps_regs_resource);
211 	sc->mps_bhandle = rman_get_bushandle(sc->mps_regs_resource);
212 
213 	/* Allocate the parent DMA tag */
214 	if (bus_dma_tag_create( bus_get_dma_tag(dev),	/* parent */
215 				1, 0,			/* algnmnt, boundary */
216 				BUS_SPACE_MAXADDR,	/* lowaddr */
217 				BUS_SPACE_MAXADDR,	/* highaddr */
218 				NULL, NULL,		/* filter, filterarg */
219 				BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
220 				BUS_SPACE_UNRESTRICTED,	/* nsegments */
221 				BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
222 				0,			/* flags */
223 				NULL, NULL,		/* lockfunc, lockarg */
224 				&sc->mps_parent_dmat)) {
225 		mps_printf(sc, "Cannot allocate parent DMA tag\n");
226 		mps_pci_free(sc);
227 		return (ENOMEM);
228 	}
229 
230 	if (((error = mps_pci_alloc_interrupts(sc)) != 0) ||
231 	    ((error = mps_attach(sc)) != 0))
232 		mps_pci_free(sc);
233 
234 	return (error);
235 }
236 
237 /*
238  * Allocate, but don't assign interrupts early.  Doing it before requesting
239  * the IOCFacts message informs the firmware that we want to do MSI-X
240  * multiqueue.  We might not use all of the available messages, but there's
241  * no reason to re-alloc if we don't.
242  */
243 static int
244 mps_pci_alloc_interrupts(struct mps_softc *sc)
245 {
246 	device_t dev;
247 	int error, msgs;
248 
249 	dev = sc->mps_dev;
250 	error = 0;
251 	msgs = 0;
252 
253 	if (sc->disable_msix == 0) {
254 		msgs = pci_msix_count(dev);
255 		mps_dprint(sc, MPS_INIT, "Counted %d MSI-X messages\n", msgs);
256 		msgs = min(msgs, sc->max_msix);
257 		msgs = min(msgs, MPS_MSIX_MAX);
258 		msgs = min(msgs, 1);	/* XXX */
259 		if (msgs != 0) {
260 			mps_dprint(sc, MPS_INIT, "Attempting to allocate %d "
261 			    "MSI-X messages\n", msgs);
262 			error = mps_alloc_msix(sc, msgs);
263 		}
264 	}
265 	if (((error != 0) || (msgs == 0)) && (sc->disable_msi == 0)) {
266 		msgs = pci_msi_count(dev);
267 		mps_dprint(sc, MPS_INIT, "Counted %d MSI messages\n", msgs);
268 		msgs = min(msgs, MPS_MSI_MAX);
269 		if (msgs != 0) {
270 			mps_dprint(sc, MPS_INIT, "Attempting to allocate %d "
271 			    "MSI messages\n", MPS_MSI_MAX);
272 			error = mps_alloc_msi(sc, MPS_MSI_MAX);
273 		}
274 	}
275 	if ((error != 0) || (msgs == 0)) {
276 		/*
277 		 * If neither MSI or MSI-X are avaiable, assume legacy INTx.
278 		 * This also implies that there will be only 1 queue.
279 		 */
280 		mps_dprint(sc, MPS_INIT, "Falling back to legacy INTx\n");
281 		sc->mps_flags |= MPS_FLAGS_INTX;
282 		msgs = 1;
283 	} else
284 		sc->mps_flags |= MPS_FLAGS_MSI;
285 
286 	sc->msi_msgs = msgs;
287 	mps_dprint(sc, MPS_INIT, "Allocated %d interrupts\n", msgs);
288 
289 	return (error);
290 }
291 
292 int
293 mps_pci_setup_interrupts(struct mps_softc *sc)
294 {
295 	device_t dev;
296 	struct mps_queue *q;
297 	void *ihandler;
298 	int i, error, rid, initial_rid;
299 
300 	dev = sc->mps_dev;
301 	error = ENXIO;
302 
303 	if (sc->mps_flags & MPS_FLAGS_INTX) {
304 		initial_rid = 0;
305 		ihandler = mps_intr;
306 	} else if (sc->mps_flags & MPS_FLAGS_MSI) {
307 		initial_rid = 1;
308 		ihandler = mps_intr_msi;
309 	} else {
310 		mps_dprint(sc, MPS_ERROR|MPS_INIT,
311 		    "Unable to set up interrupts\n");
312 		return (EINVAL);
313 	}
314 
315 	for (i = 0; i < sc->msi_msgs; i++) {
316 		q = &sc->queues[i];
317 		rid = i + initial_rid;
318 		q->irq_rid = rid;
319 		q->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
320 		    &q->irq_rid, RF_ACTIVE);
321 		if (q->irq == NULL) {
322 			mps_dprint(sc, MPS_ERROR|MPS_INIT,
323 			    "Cannot allocate interrupt RID %d\n", rid);
324 			sc->msi_msgs = i;
325 			break;
326 		}
327 		error = bus_setup_intr(dev, q->irq,
328 		    INTR_TYPE_BIO | INTR_MPSAFE, NULL, ihandler,
329 		    sc, &q->intrhand);
330 		if (error) {
331 			mps_dprint(sc, MPS_ERROR|MPS_INIT,
332 			    "Cannot setup interrupt RID %d\n", rid);
333 			sc->msi_msgs = i;
334 			break;
335 		}
336 	}
337 
338 	mps_dprint(sc, MPS_INIT, "Set up %d interrupts\n", sc->msi_msgs);
339 
340 	return (error);
341 }
342 
343 static int
344 mps_pci_detach(device_t dev)
345 {
346 	struct mps_softc *sc;
347 	int error;
348 
349 	sc = device_get_softc(dev);
350 
351 	if ((error = mps_free(sc)) != 0)
352 		return (error);
353 
354 	mps_pci_free(sc);
355 	return (0);
356 }
357 
358 void
359 mps_pci_free_interrupts(struct mps_softc *sc)
360 {
361 	struct mps_queue *q;
362 	int i;
363 
364 	if (sc->queues == NULL)
365 		return;
366 
367 	for (i = 0; i < sc->msi_msgs; i++) {
368 		q = &sc->queues[i];
369 		if (q->irq != NULL) {
370 			bus_teardown_intr(sc->mps_dev, q->irq,
371 			    q->intrhand);
372 			bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
373 			    q->irq_rid, q->irq);
374 		}
375 	}
376 }
377 
378 static void
379 mps_pci_free(struct mps_softc *sc)
380 {
381 
382 	if (sc->mps_parent_dmat != NULL) {
383 		bus_dma_tag_destroy(sc->mps_parent_dmat);
384 	}
385 
386 	mps_pci_free_interrupts(sc);
387 
388 	if (sc->mps_flags & MPS_FLAGS_MSI)
389 		pci_release_msi(sc->mps_dev);
390 
391 	if (sc->mps_regs_resource != NULL) {
392 		bus_release_resource(sc->mps_dev, SYS_RES_MEMORY,
393 		    sc->mps_regs_rid, sc->mps_regs_resource);
394 	}
395 
396 	return;
397 }
398 
399 static int
400 mps_pci_suspend(device_t dev)
401 {
402 	return (EINVAL);
403 }
404 
405 static int
406 mps_pci_resume(device_t dev)
407 {
408 	return (EINVAL);
409 }
410 
411 static int
412 mps_alloc_msix(struct mps_softc *sc, int msgs)
413 {
414 	int error;
415 
416 	error = pci_alloc_msix(sc->mps_dev, &msgs);
417 	return (error);
418 }
419 
420 static int
421 mps_alloc_msi(struct mps_softc *sc, int msgs)
422 {
423 	int error;
424 
425 	error = pci_alloc_msi(sc->mps_dev, &msgs);
426 	return (error);
427 }
428 
429 int
430 mps_pci_restore(struct mps_softc *sc)
431 {
432 	struct pci_devinfo *dinfo;
433 
434 	mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
435 
436 	dinfo = device_get_ivars(sc->mps_dev);
437 	if (dinfo == NULL) {
438 		mps_dprint(sc, MPS_FAULT, "%s: NULL dinfo\n", __func__);
439 		return (EINVAL);
440 	}
441 
442 	pci_cfg_restore(sc->mps_dev, dinfo);
443 	return (0);
444 }
445 
446