1 /*- 2 * Copyright (c) 2009 Yahoo! Inc. 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 MPT2 controllers */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 #include <sys/conf.h> 39 #include <sys/malloc.h> 40 #include <sys/sysctl.h> 41 #include <sys/uio.h> 42 43 #include <machine/bus.h> 44 #include <machine/resource.h> 45 #include <sys/rman.h> 46 47 #include <dev/pci/pcireg.h> 48 #include <dev/pci/pcivar.h> 49 50 #include <dev/mps/mpi/mpi2_type.h> 51 #include <dev/mps/mpi/mpi2.h> 52 #include <dev/mps/mpi/mpi2_ioc.h> 53 #include <dev/mps/mpi/mpi2_cnfg.h> 54 55 #include <dev/mps/mpsvar.h> 56 57 static int mps_pci_probe(device_t); 58 static int mps_pci_attach(device_t); 59 static int mps_pci_detach(device_t); 60 static int mps_pci_suspend(device_t); 61 static int mps_pci_resume(device_t); 62 static void mps_pci_free(struct mps_softc *); 63 static int mps_alloc_msix(struct mps_softc *sc, int msgs); 64 static int mps_alloc_msi(struct mps_softc *sc, int msgs); 65 66 int mps_disable_msix = 0; 67 TUNABLE_INT("hw.mps.disable_msix", &mps_disable_msix); 68 SYSCTL_INT(_hw_mps, OID_AUTO, disable_msix, CTLFLAG_RD, &mps_disable_msix, 0, 69 "Disable MSIX interrupts\n"); 70 int mps_disable_msi = 0; 71 TUNABLE_INT("hw.mps.disable_msi", &mps_disable_msi); 72 SYSCTL_INT(_hw_mps, OID_AUTO, disable_msi, CTLFLAG_RD, &mps_disable_msi, 0, 73 "Disable MSI interrupts\n"); 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 static devclass_t mps_devclass; 92 DRIVER_MODULE(mps, pci, mps_pci_driver, mps_devclass, 0, 0); 93 94 struct mps_ident { 95 uint16_t vendor; 96 uint16_t device; 97 uint16_t subvendor; 98 uint16_t subdevice; 99 u_int flags; 100 const char *desc; 101 } mps_identifiers[] = { 102 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004, 103 0xffff, 0xffff, 0, "LSI SAS2004" }, 104 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008, 105 0xffff, 0xffff, 0, "LSI SAS2008" }, 106 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1, 107 0xffff, 0xffff, 0, "LSI SAS2108" }, 108 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2, 109 0xffff, 0xffff, 0, "LSI SAS2108" }, 110 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3, 111 0xffff, 0xffff, 0, "LSI SAS2108" }, 112 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1, 113 0xffff, 0xffff, 0, "LSI SAS2116" }, 114 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2, 115 0xffff, 0xffff, 0, "LSI SAS2116" }, 116 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1, 117 0xffff, 0xffff, 0, "LSI SAS2208" }, 118 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2, 119 0xffff, 0xffff, 0, "LSI SAS2208" }, 120 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3, 121 0xffff, 0xffff, 0, "LSI SAS2208" }, 122 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4, 123 0xffff, 0xffff, 0, "LSI SAS2208" }, 124 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5, 125 0xffff, 0xffff, 0, "LSI SAS2208" }, 126 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6, 127 0xffff, 0xffff, 0, "LSI SAS2208" }, 128 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_7, 129 0xffff, 0xffff, 0, "LSI SAS2208" }, 130 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_8, 131 0xffff, 0xffff, 0, "LSI SAS2208" }, 132 { 0, 0, 0, 0, 0, NULL } 133 }; 134 135 static struct mps_ident * 136 mps_find_ident(device_t dev) 137 { 138 struct mps_ident *m; 139 140 for (m = mps_identifiers; m->vendor != 0; m++) { 141 if (m->vendor != pci_get_vendor(dev)) 142 continue; 143 if (m->device != pci_get_device(dev)) 144 continue; 145 if ((m->subvendor != 0xffff) && 146 (m->subvendor != pci_get_subvendor(dev))) 147 continue; 148 if ((m->subdevice != 0xffff) && 149 (m->subdevice != pci_get_subdevice(dev))) 150 continue; 151 return (m); 152 } 153 154 return (NULL); 155 } 156 157 static int 158 mps_pci_probe(device_t dev) 159 { 160 struct mps_ident *id; 161 162 if ((id = mps_find_ident(dev)) != NULL) { 163 device_set_desc(dev, id->desc); 164 return (BUS_PROBE_DEFAULT); 165 } 166 return (ENXIO); 167 } 168 169 static int 170 mps_pci_attach(device_t dev) 171 { 172 struct mps_softc *sc; 173 struct mps_ident *m; 174 uint16_t command; 175 int error; 176 177 sc = device_get_softc(dev); 178 bzero(sc, sizeof(*sc)); 179 sc->mps_dev = dev; 180 m = mps_find_ident(dev); 181 sc->mps_flags = m->flags; 182 183 /* Twiddle basic PCI config bits for a sanity check */ 184 command = pci_read_config(dev, PCIR_COMMAND, 2); 185 command |= PCIM_CMD_BUSMASTEREN; 186 pci_write_config(dev, PCIR_COMMAND, command, 2); 187 command = pci_read_config(dev, PCIR_COMMAND, 2); 188 if ((command & PCIM_CMD_BUSMASTEREN) == 0) { 189 device_printf(dev, "Cannot enable PCI busmaster\n"); 190 return (ENXIO); 191 } 192 if ((command & PCIM_CMD_MEMEN) == 0) { 193 device_printf(dev, "PCI memory window not available\n"); 194 return (ENXIO); 195 } 196 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 device_printf(dev, "Cannot allocate PCI registers\n"); 202 return (ENXIO); 203 } 204 sc->mps_btag = rman_get_bustag(sc->mps_regs_resource); 205 sc->mps_bhandle = rman_get_bushandle(sc->mps_regs_resource); 206 207 /* Allocate the parent DMA tag */ 208 if (bus_dma_tag_create( NULL, /* 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 */ 216 0, /* flags */ 217 NULL, NULL, /* lockfunc, lockarg */ 218 &sc->mps_parent_dmat)) { 219 device_printf(dev, "Cannot allocate parent DMA tag\n"); 220 mps_pci_free(sc); 221 return (ENOMEM); 222 } 223 224 if ((error = mps_attach(sc)) != 0) 225 mps_pci_free(sc); 226 227 return (error); 228 } 229 230 int 231 mps_pci_setup_interrupts(struct mps_softc *sc) 232 { 233 device_t dev; 234 int i, error, msgs; 235 236 dev = sc->mps_dev; 237 error = ENXIO; 238 if ((mps_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) && (mps_disable_msi == 0) && 242 ((msgs = pci_msi_count(dev)) >= MPS_MSI_COUNT)) 243 error = mps_alloc_msi(sc, MPS_MSI_COUNT); 244 245 if (error != 0) { 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 device_printf(dev, "Cannot allocate INTx interrupt\n"); 252 return (ENXIO); 253 } 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]); 257 if (error) 258 device_printf(dev, "Cannot setup INTx interrupt\n"); 259 } else { 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) { 266 device_printf(dev, 267 "Cannot allocate MSI interrupt\n"); 268 return (ENXIO); 269 } 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]); 273 if (error) { 274 device_printf(dev, 275 "Cannot setup MSI interrupt %d\n", i); 276 break; 277 } 278 } 279 } 280 281 return (error); 282 } 283 284 static int 285 mps_pci_detach(device_t dev) 286 { 287 struct mps_softc *sc; 288 int error; 289 290 sc = device_get_softc(dev); 291 292 if ((error = mps_free(sc)) != 0) 293 return (error); 294 295 mps_pci_free(sc); 296 return (0); 297 } 298 299 static void 300 mps_pci_free(struct mps_softc *sc) 301 { 302 int i; 303 304 if (sc->mps_parent_dmat != NULL) { 305 bus_dma_tag_destroy(sc->mps_parent_dmat); 306 } 307 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]); 315 } 316 } 317 pci_release_msi(sc->mps_dev); 318 } 319 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]); 325 } 326 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); 330 } 331 332 return; 333 } 334 335 static int 336 mps_pci_suspend(device_t dev) 337 { 338 return (EINVAL); 339 } 340 341 static int 342 mps_pci_resume(device_t dev) 343 { 344 return (EINVAL); 345 } 346 347 static int 348 mps_alloc_msix(struct mps_softc *sc, int msgs) 349 { 350 int error; 351 352 error = pci_alloc_msix(sc->mps_dev, &msgs); 353 return (error); 354 } 355 356 static int 357 mps_alloc_msi(struct mps_softc *sc, int msgs) 358 { 359 int error; 360 361 error = pci_alloc_msi(sc->mps_dev, &msgs); 362 return (error); 363 } 364 365