1 /*- 2 * Copyright (c) 2006 IronPort Systems 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 /* PCI/PCI-X/PCIe bus interface for the LSI MegaSAS controllers */ 31 32 #include "opt_mfi.h" 33 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> 39 #include <sys/bus.h> 40 #include <sys/conf.h> 41 #include <sys/bio.h> 42 #include <sys/malloc.h> 43 #include <sys/uio.h> 44 45 #include <machine/bus.h> 46 #include <machine/resource.h> 47 #include <sys/rman.h> 48 49 #include <dev/pci/pcireg.h> 50 #include <dev/pci/pcivar.h> 51 52 #include <dev/mfi/mfireg.h> 53 #include <dev/mfi/mfi_ioctl.h> 54 #include <dev/mfi/mfivar.h> 55 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 *); 62 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), 71 { 0, 0 } 72 }; 73 74 static driver_t mfi_pci_driver = { 75 "mfi", 76 mfi_methods, 77 sizeof(struct mfi_softc) 78 }; 79 80 static devclass_t mfi_devclass; 81 DRIVER_MODULE(mfi, pci, mfi_pci_driver, mfi_devclass, 0, 0); 82 83 struct mfi_ident { 84 uint16_t vendor; 85 uint16_t device; 86 uint16_t subvendor; 87 uint16_t subdevice; 88 int flags; 89 const char *desc; 90 } mfi_identifiers[] = { 91 {0x1000, 0x0411, 0xffff, 0xffff, 0, "LSI MegaSAS 1064R"}, 92 {0x1028, 0x0015, 0xffff, 0xffff, 0, "Dell PERC 5/i"}, 93 {0, 0, 0, 0, 0, NULL} 94 }; 95 96 static struct mfi_ident * 97 mfi_find_ident(device_t dev) 98 { 99 struct mfi_ident *m; 100 101 for (m = mfi_identifiers; m->vendor != 0; m++) { 102 if ((m->vendor == pci_get_vendor(dev)) && 103 (m->device == pci_get_device(dev)) && 104 ((m->subvendor == pci_get_subvendor(dev)) || 105 (m->subvendor == 0xffff)) && 106 ((m->subdevice == pci_get_subdevice(dev)) || 107 (m->subdevice == 0xffff))) 108 return (m); 109 } 110 111 return (NULL); 112 } 113 114 static int 115 mfi_pci_probe(device_t dev) 116 { 117 struct mfi_ident *id; 118 119 if ((id = mfi_find_ident(dev)) != NULL) { 120 device_set_desc(dev, id->desc); 121 return (BUS_PROBE_DEFAULT); 122 } 123 return (ENXIO); 124 } 125 126 static int 127 mfi_pci_attach(device_t dev) 128 { 129 struct mfi_softc *sc; 130 struct mfi_ident *m; 131 uint32_t command; 132 int error; 133 134 sc = device_get_softc(dev); 135 bzero(sc, sizeof(*sc)); 136 sc->mfi_dev = dev; 137 138 /* Verify that the adapter can be set up in PCI space */ 139 command = pci_read_config(dev, PCIR_COMMAND, 2); 140 command |= PCIM_CMD_BUSMASTEREN; 141 pci_write_config(dev, PCIR_COMMAND, command, 2); 142 command = pci_read_config(dev, PCIR_COMMAND, 2); 143 if ((command & PCIM_CMD_BUSMASTEREN) == 0) { 144 device_printf(dev, "Can't enable PCI busmaster\n"); 145 return (ENXIO); 146 } 147 if ((command & PCIM_CMD_MEMEN) == 0) { 148 device_printf(dev, "PCI memory window not available\n"); 149 return (ENXIO); 150 } 151 152 /* Allocate PCI registers */ 153 sc->mfi_regs_rid = PCIR_BAR(0); 154 if ((sc->mfi_regs_resource = bus_alloc_resource_any(sc->mfi_dev, 155 SYS_RES_MEMORY, &sc->mfi_regs_rid, RF_ACTIVE)) == NULL) { 156 device_printf(dev, "Cannot allocate PCI registers\n"); 157 return (ENXIO); 158 } 159 sc->mfi_btag = rman_get_bustag(sc->mfi_regs_resource); 160 sc->mfi_bhandle = rman_get_bushandle(sc->mfi_regs_resource); 161 162 error = ENOMEM; 163 164 /* Allocate parent DMA tag */ 165 if (bus_dma_tag_create( NULL, /* parent */ 166 1, 0, /* algnmnt, boundary */ 167 BUS_SPACE_MAXADDR, /* lowaddr */ 168 BUS_SPACE_MAXADDR, /* highaddr */ 169 NULL, NULL, /* filter, filterarg */ 170 BUS_SPACE_MAXSIZE_32BIT,/* maxsize */ 171 BUS_SPACE_UNRESTRICTED, /* nsegments */ 172 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */ 173 0, /* flags */ 174 NULL, NULL, /* lockfunc, lockarg */ 175 &sc->mfi_parent_dmat)) { 176 device_printf(dev, "Cannot allocate parent DMA tag\n"); 177 goto out; 178 } 179 180 m = mfi_find_ident(dev); 181 sc->mfi_flags = m->flags; 182 183 error = mfi_attach(sc); 184 out: 185 if (error) { 186 mfi_free(sc); 187 mfi_pci_free(sc); 188 } 189 190 return (error); 191 } 192 193 static int 194 mfi_pci_detach(device_t dev) 195 { 196 struct mfi_softc *sc; 197 struct mfi_ld *ld; 198 int error; 199 200 sc = device_get_softc(dev); 201 202 if ((sc->mfi_flags & MFI_FLAGS_OPEN) != 0) 203 return (EBUSY); 204 205 while ((ld = TAILQ_FIRST(&sc->mfi_ld_tqh)) != NULL) { 206 error = device_delete_child(dev, ld->ld_disk); 207 if (error) 208 return (error); 209 TAILQ_REMOVE(&sc->mfi_ld_tqh, ld, ld_link); 210 free(ld->ld_info, M_MFIBUF); 211 free(ld, M_MFIBUF); 212 } 213 214 EVENTHANDLER_DEREGISTER(shutdown_final, sc->mfi_eh); 215 216 mfi_shutdown(sc); 217 mfi_free(sc); 218 mfi_pci_free(sc); 219 return (0); 220 } 221 222 static void 223 mfi_pci_free(struct mfi_softc *sc) 224 { 225 226 if (sc->mfi_regs_resource != NULL) { 227 bus_release_resource(sc->mfi_dev, SYS_RES_MEMORY, 228 sc->mfi_regs_rid, sc->mfi_regs_resource); 229 } 230 231 return; 232 } 233 234 static int 235 mfi_pci_suspend(device_t dev) 236 { 237 238 return (EINVAL); 239 } 240 241 static int 242 mfi_pci_resume(device_t dev) 243 { 244 245 return (EINVAL); 246 } 247