2 * Copyright (c) 2009 Yahoo! Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 /* PCI/PCI-X/PCIe bus interface for the LSI MPT2 controllers */
32 /* TODO Move headers to mpsvar */
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pci_private.h>
52 #include <dev/mps/mpi/mpi2_type.h>
53 #include <dev/mps/mpi/mpi2.h>
54 #include <dev/mps/mpi/mpi2_ioc.h>
55 #include <dev/mps/mpi/mpi2_cnfg.h>
56 #include <dev/mps/mpi/mpi2_tool.h>
58 #include <sys/queue.h>
59 #include <sys/kthread.h>
60 #include <dev/mps/mps_ioctl.h>
61 #include <dev/mps/mpsvar.h>
63 static int mps_pci_probe(device_t);
64 static int mps_pci_attach(device_t);
65 static int mps_pci_detach(device_t);
66 static int mps_pci_suspend(device_t);
67 static int mps_pci_resume(device_t);
68 static void mps_pci_free(struct mps_softc *);
69 static int mps_alloc_msix(struct mps_softc *sc, int msgs);
70 static int mps_alloc_msi(struct mps_softc *sc, int msgs);
72 static device_method_t mps_methods[] = {
73 DEVMETHOD(device_probe, mps_pci_probe),
74 DEVMETHOD(device_attach, mps_pci_attach),
75 DEVMETHOD(device_detach, mps_pci_detach),
76 DEVMETHOD(device_suspend, mps_pci_suspend),
77 DEVMETHOD(device_resume, mps_pci_resume),
82 static driver_t mps_pci_driver = {
85 sizeof(struct mps_softc)
88 static devclass_t mps_devclass;
89 DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, 0, 0);
90 MODULE_DEPEND(mps, cam, 1, 1, 1);
99 } mps_identifiers[] = {
100 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
101 0xffff, 0xffff, 0, "LSI SAS2004" },
102 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
103 0xffff, 0xffff, 0, "LSI SAS2008" },
104 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
105 0xffff, 0xffff, 0, "LSI SAS2108" },
106 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
107 0xffff, 0xffff, 0, "LSI SAS2108" },
108 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
109 0xffff, 0xffff, 0, "LSI SAS2108" },
110 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
111 0xffff, 0xffff, 0, "LSI SAS2116" },
112 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
113 0xffff, 0xffff, 0, "LSI SAS2116" },
114 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
115 0xffff, 0xffff, 0, "LSI SAS2208" },
116 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
117 0xffff, 0xffff, 0, "LSI SAS2208" },
118 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
119 0xffff, 0xffff, 0, "LSI SAS2208" },
120 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
121 0xffff, 0xffff, 0, "LSI SAS2208" },
122 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
123 0xffff, 0xffff, 0, "LSI SAS2208" },
124 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
125 0xffff, 0xffff, 0, "LSI SAS2208" },
126 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
127 0xffff, 0xffff, 0, "LSI SAS2308" },
128 // Add Customer specific vender/subdevice id before generic
129 // (0xffff) vender/subdevice id.
130 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
131 0x8086, 0x3516, 0, "Intel(R) Integrated RAID Module RMS25JB080" },
132 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
133 0x8086, 0x3517, 0, "Intel(R) Integrated RAID Module RMS25JB040" },
134 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
135 0x8086, 0x3518, 0, "Intel(R) Integrated RAID Module RMS25KB080" },
136 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
137 0x8086, 0x3519, 0, "Intel(R) Integrated RAID Module RMS25KB040" },
138 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
139 0xffff, 0xffff, 0, "LSI SAS2308" },
140 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
141 0xffff, 0xffff, 0, "LSI SAS2308" },
142 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
143 0xffff, 0xffff, MPS_FLAGS_WD_AVAILABLE, "LSI SSS6200" },
144 { 0, 0, 0, 0, 0, NULL }
147 static struct mps_ident *
148 mps_find_ident(device_t dev)
152 for (m = mps_identifiers; m->vendor != 0; m++) {
153 if (m->vendor != pci_get_vendor(dev))
155 if (m->device != pci_get_device(dev))
157 if ((m->subvendor != 0xffff) &&
158 (m->subvendor != pci_get_subvendor(dev)))
160 if ((m->subdevice != 0xffff) &&
161 (m->subdevice != pci_get_subdevice(dev)))
170 mps_pci_probe(device_t dev)
172 struct mps_ident *id;
174 if ((id = mps_find_ident(dev)) != NULL) {
175 device_set_desc(dev, id->desc);
176 return (BUS_PROBE_DEFAULT);
182 mps_pci_attach(device_t dev)
184 struct mps_softc *sc;
188 sc = device_get_softc(dev);
189 bzero(sc, sizeof(*sc));
191 m = mps_find_ident(dev);
192 sc->mps_flags = m->flags;
194 /* Twiddle basic PCI config bits for a sanity check */
195 pci_enable_busmaster(dev);
197 /* Allocate the System Interface Register Set */
198 sc->mps_regs_rid = PCIR_BAR(1);
199 if ((sc->mps_regs_resource = bus_alloc_resource_any(dev,
200 SYS_RES_MEMORY, &sc->mps_regs_rid, RF_ACTIVE)) == NULL) {
201 mps_printf(sc, "Cannot allocate PCI registers\n");
204 sc->mps_btag = rman_get_bustag(sc->mps_regs_resource);
205 sc->mps_bhandle = rman_get_bushandle(sc->mps_regs_resource);
207 /* Allocate the parent DMA tag */
208 if (bus_dma_tag_create( bus_get_dma_tag(dev), /* parent */
209 1, 0, /* algnmnt, boundary */
210 BUS_SPACE_MAXADDR, /* lowaddr */
211 BUS_SPACE_MAXADDR, /* highaddr */
212 NULL, NULL, /* filter, filterarg */
213 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
214 BUS_SPACE_UNRESTRICTED, /* nsegments */
215 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
217 NULL, NULL, /* lockfunc, lockarg */
218 &sc->mps_parent_dmat)) {
219 mps_printf(sc, "Cannot allocate parent DMA tag\n");
224 if ((error = mps_attach(sc)) != 0)
231 mps_pci_setup_interrupts(struct mps_softc *sc)
238 if ((sc->disable_msix == 0) &&
239 ((msgs = pci_msix_count(dev)) >= MPS_MSI_COUNT))
240 error = mps_alloc_msix(sc, MPS_MSI_COUNT);
241 if ((error != 0) && (sc->disable_msi == 0) &&
242 ((msgs = pci_msi_count(dev)) >= MPS_MSI_COUNT))
243 error = mps_alloc_msi(sc, MPS_MSI_COUNT);
246 sc->mps_flags |= MPS_FLAGS_INTX;
247 sc->mps_irq_rid[0] = 0;
248 sc->mps_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ,
249 &sc->mps_irq_rid[0], RF_SHAREABLE | RF_ACTIVE);
250 if (sc->mps_irq[0] == NULL) {
251 mps_printf(sc, "Cannot allocate INTx interrupt\n");
254 error = bus_setup_intr(dev, sc->mps_irq[0],
255 INTR_TYPE_BIO | INTR_MPSAFE, NULL, mps_intr, sc,
256 &sc->mps_intrhand[0]);
258 mps_printf(sc, "Cannot setup INTx interrupt\n");
260 sc->mps_flags |= MPS_FLAGS_MSI;
261 for (i = 0; i < MPS_MSI_COUNT; i++) {
262 sc->mps_irq_rid[i] = i + 1;
263 sc->mps_irq[i] = bus_alloc_resource_any(dev,
264 SYS_RES_IRQ, &sc->mps_irq_rid[i], RF_ACTIVE);
265 if (sc->mps_irq[i] == NULL) {
267 "Cannot allocate MSI interrupt\n");
270 error = bus_setup_intr(dev, sc->mps_irq[i],
271 INTR_TYPE_BIO | INTR_MPSAFE, NULL, mps_intr_msi,
272 sc, &sc->mps_intrhand[i]);
275 "Cannot setup MSI interrupt %d\n", i);
285 mps_pci_detach(device_t dev)
287 struct mps_softc *sc;
290 sc = device_get_softc(dev);
292 if ((error = mps_free(sc)) != 0)
300 mps_pci_free(struct mps_softc *sc)
304 if (sc->mps_parent_dmat != NULL) {
305 bus_dma_tag_destroy(sc->mps_parent_dmat);
308 if (sc->mps_flags & MPS_FLAGS_MSI) {
309 for (i = 0; i < MPS_MSI_COUNT; i++) {
310 if (sc->mps_irq[i] != NULL) {
311 bus_teardown_intr(sc->mps_dev, sc->mps_irq[i],
312 sc->mps_intrhand[i]);
313 bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
314 sc->mps_irq_rid[i], sc->mps_irq[i]);
317 pci_release_msi(sc->mps_dev);
320 if (sc->mps_flags & MPS_FLAGS_INTX) {
321 bus_teardown_intr(sc->mps_dev, sc->mps_irq[0],
322 sc->mps_intrhand[0]);
323 bus_release_resource(sc->mps_dev, SYS_RES_IRQ,
324 sc->mps_irq_rid[0], sc->mps_irq[0]);
327 if (sc->mps_regs_resource != NULL) {
328 bus_release_resource(sc->mps_dev, SYS_RES_MEMORY,
329 sc->mps_regs_rid, sc->mps_regs_resource);
336 mps_pci_suspend(device_t dev)
342 mps_pci_resume(device_t dev)
348 mps_alloc_msix(struct mps_softc *sc, int msgs)
352 error = pci_alloc_msix(sc->mps_dev, &msgs);
357 mps_alloc_msi(struct mps_softc *sc, int msgs)
361 error = pci_alloc_msi(sc->mps_dev, &msgs);
366 mps_pci_restore(struct mps_softc *sc)
368 struct pci_devinfo *dinfo;
370 mps_dprint(sc, MPS_TRACE, "%s\n", __func__);
372 dinfo = device_get_ivars(sc->mps_dev);
374 mps_dprint(sc, MPS_FAULT, "%s: NULL dinfo\n", __func__);
378 pci_cfg_restore(sc->mps_dev, dinfo);