2 * Copyright (c) 2006 IronPort Systems
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 MegaSAS controllers */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/selinfo.h>
38 #include <sys/module.h>
42 #include <sys/malloc.h>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
52 #include <dev/mfi/mfireg.h>
53 #include <dev/mfi/mfi_ioctl.h>
54 #include <dev/mfi/mfivar.h>
56 static int mfi_pci_probe(device_t);
57 static int mfi_pci_attach(device_t);
58 static int mfi_pci_detach(device_t);
59 static int mfi_pci_suspend(device_t);
60 static int mfi_pci_resume(device_t);
61 static void mfi_pci_free(struct mfi_softc *);
63 static device_method_t mfi_methods[] = {
64 DEVMETHOD(device_probe, mfi_pci_probe),
65 DEVMETHOD(device_attach, mfi_pci_attach),
66 DEVMETHOD(device_detach, mfi_pci_detach),
67 DEVMETHOD(device_suspend, mfi_pci_suspend),
68 DEVMETHOD(device_resume, mfi_pci_resume),
69 DEVMETHOD(bus_print_child, bus_generic_print_child),
70 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
74 static driver_t mfi_pci_driver = {
77 sizeof(struct mfi_softc)
80 static devclass_t mfi_devclass;
81 DRIVER_MODULE(mfi, pci, mfi_pci_driver, mfi_devclass, 0, 0);
90 } mfi_identifiers[] = {
91 {0x1000, 0x0411, 0xffff, 0xffff, 0, "LSI MegaSAS 1064R"}, /* Brocton IOP */
92 {0x1000, 0x0413, 0xffff, 0xffff, 0, "LSI MegaSAS 1064R"}, /* Verde ZCR */
93 {0x1028, 0x0015, 0xffff, 0xffff, 0, "Dell PERC 5/i"},
97 static struct mfi_ident *
98 mfi_find_ident(device_t dev)
102 for (m = mfi_identifiers; m->vendor != 0; m++) {
103 if ((m->vendor == pci_get_vendor(dev)) &&
104 (m->device == pci_get_device(dev)) &&
105 ((m->subvendor == pci_get_subvendor(dev)) ||
106 (m->subvendor == 0xffff)) &&
107 ((m->subdevice == pci_get_subdevice(dev)) ||
108 (m->subdevice == 0xffff)))
116 mfi_pci_probe(device_t dev)
118 struct mfi_ident *id;
120 if ((id = mfi_find_ident(dev)) != NULL) {
121 device_set_desc(dev, id->desc);
122 return (BUS_PROBE_DEFAULT);
128 mfi_pci_attach(device_t dev)
130 struct mfi_softc *sc;
135 sc = device_get_softc(dev);
136 bzero(sc, sizeof(*sc));
139 /* Verify that the adapter can be set up in PCI space */
140 command = pci_read_config(dev, PCIR_COMMAND, 2);
141 command |= PCIM_CMD_BUSMASTEREN;
142 pci_write_config(dev, PCIR_COMMAND, command, 2);
143 command = pci_read_config(dev, PCIR_COMMAND, 2);
144 if ((command & PCIM_CMD_BUSMASTEREN) == 0) {
145 device_printf(dev, "Can't enable PCI busmaster\n");
148 if ((command & PCIM_CMD_MEMEN) == 0) {
149 device_printf(dev, "PCI memory window not available\n");
153 /* Allocate PCI registers */
154 sc->mfi_regs_rid = PCIR_BAR(0);
155 if ((sc->mfi_regs_resource = bus_alloc_resource_any(sc->mfi_dev,
156 SYS_RES_MEMORY, &sc->mfi_regs_rid, RF_ACTIVE)) == NULL) {
157 device_printf(dev, "Cannot allocate PCI registers\n");
160 sc->mfi_btag = rman_get_bustag(sc->mfi_regs_resource);
161 sc->mfi_bhandle = rman_get_bushandle(sc->mfi_regs_resource);
165 /* Allocate parent DMA tag */
166 if (bus_dma_tag_create( NULL, /* parent */
167 1, 0, /* algnmnt, boundary */
168 BUS_SPACE_MAXADDR, /* lowaddr */
169 BUS_SPACE_MAXADDR, /* highaddr */
170 NULL, NULL, /* filter, filterarg */
171 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
172 BUS_SPACE_UNRESTRICTED, /* nsegments */
173 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
175 NULL, NULL, /* lockfunc, lockarg */
176 &sc->mfi_parent_dmat)) {
177 device_printf(dev, "Cannot allocate parent DMA tag\n");
181 m = mfi_find_ident(dev);
182 sc->mfi_flags = m->flags;
184 error = mfi_attach(sc);
195 mfi_pci_detach(device_t dev)
197 struct mfi_softc *sc;
201 sc = device_get_softc(dev);
203 mtx_lock(&sc->mfi_io_lock);
204 if ((sc->mfi_flags & MFI_FLAGS_OPEN) != 0) {
205 mtx_unlock(&sc->mfi_io_lock);
209 while ((ld = TAILQ_FIRST(&sc->mfi_ld_tqh)) != NULL) {
210 if ((error = device_delete_child(dev, ld->ld_dev)) != 0) {
211 mtx_unlock(&sc->mfi_io_lock);
214 TAILQ_REMOVE(&sc->mfi_ld_tqh, ld, ld_link);
217 EVENTHANDLER_DEREGISTER(shutdown_final, sc->mfi_eh);
226 mfi_pci_free(struct mfi_softc *sc)
229 if (sc->mfi_regs_resource != NULL) {
230 bus_release_resource(sc->mfi_dev, SYS_RES_MEMORY,
231 sc->mfi_regs_rid, sc->mfi_regs_resource);
238 mfi_pci_suspend(device_t dev)
245 mfi_pci_resume(device_t dev)