1 /* $OpenBSD: mpi_pci.c,v 1.6 2006/06/14 01:51:41 dlg Exp $ */ 2 3 /* 4 * Copyright (c) 2005 David Gwynne <dlg@openbsd.org> 5 * Copyright (c) 2005 Marco Peereboom <marco@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/param.h> 21 #include <sys/systm.h> 22 #include <sys/kernel.h> 23 #include <sys/malloc.h> 24 #include <sys/device.h> 25 26 #include <machine/bus.h> 27 28 #include <dev/pci/pcireg.h> 29 #include <dev/pci/pcivar.h> 30 #include <dev/pci/pcidevs.h> 31 32 #include <scsi/scsi_all.h> 33 #include <scsi/scsiconf.h> 34 35 #include <dev/ic/mpireg.h> 36 #include <dev/ic/mpivar.h> 37 38 int mpi_pci_match(struct device *, void *, void *); 39 void mpi_pci_attach(struct device *, struct device *, void *); 40 int mpi_pci_detach(struct device *, int); 41 42 struct mpi_pci_softc { 43 struct mpi_softc psc_mpi; 44 45 pci_chipset_tag_t psc_pc; 46 pcitag_t psc_tag; 47 48 void *psc_ih; 49 }; 50 51 struct cfattach mpi_pci_ca = { 52 sizeof(struct mpi_pci_softc), mpi_pci_match, mpi_pci_attach, 53 mpi_pci_detach 54 }; 55 56 #define PREAD(s, r) pci_conf_read((s)->psc_pc, (s)->psc_tag, (r)) 57 #define PWRITE(s, r, v) pci_conf_write((s)->psc_pc, (s)->psc_tag, (r), (v)) 58 59 #define MPP_DUAL 0x01 /* Dual port adapter */ 60 61 static const struct pci_matchid mpi_devices[] = { 62 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_1030 }, 63 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_FC909 }, 64 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_FC909A }, 65 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_FC919 }, 66 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_FC919_1 }, 67 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_FC929 }, 68 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_FC929_1 }, 69 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1064 }, 70 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1064A }, 71 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1064E }, 72 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1066 }, 73 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1066E }, 74 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1068 }, 75 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1078 }, 76 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1068E } 77 }; 78 79 int 80 mpi_pci_match(struct device *parent, void *match, void *aux) 81 { 82 struct pci_attach_args *pa = aux; 83 84 return (pci_matchbyid(pa, mpi_devices, 85 sizeof(mpi_devices) / sizeof(mpi_devices[0]))); 86 } 87 88 void 89 mpi_pci_attach(struct device *parent, struct device *self, void *aux) 90 { 91 struct mpi_pci_softc *psc = (void *)self; 92 struct mpi_softc *sc = &psc->psc_mpi; 93 struct pci_attach_args *pa = aux; 94 pcireg_t memtype; 95 int r; 96 pci_intr_handle_t ih; 97 const char *intrstr; 98 99 psc->psc_pc = pa->pa_pc; 100 psc->psc_tag = pa->pa_tag; 101 psc->psc_ih = NULL; 102 sc->sc_dmat = pa->pa_dmat; 103 sc->sc_ios = 0; 104 105 /* find the appropriate memory base */ 106 for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) { 107 memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, r); 108 if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM) 109 break; 110 } 111 if (r >= PCI_MAPREG_END) { 112 printf(": unable to locate system interface registers\n"); 113 return; 114 } 115 116 if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh, 117 NULL, &sc->sc_ios, 0) != 0) { 118 printf(": unable to map system interface registers\n"); 119 return; 120 } 121 122 /* disable the expansion rom */ 123 PWRITE(psc, PCI_ROM_REG, PREAD(psc, PCI_ROM_REG & ~PCI_ROM_ENABLE)); 124 125 /* hook up the interrupt */ 126 if (pci_intr_map(pa, &ih)) { 127 printf(": unable to map interrupt\n"); 128 goto unmap; 129 } 130 intrstr = pci_intr_string(psc->psc_pc, ih); 131 psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO, 132 mpi_intr, sc, sc->sc_dev.dv_xname); 133 if (psc->psc_ih == NULL) { 134 printf(": unable to map interrupt%s%s\n", 135 intrstr == NULL ? "" : " at ", 136 intrstr == NULL ? "" : intrstr); 137 goto unmap; 138 } 139 printf(": %s", intrstr); 140 141 if (mpi_attach(sc) != 0) { 142 /* error printed by mpi_attach */ 143 goto deintr; 144 } 145 146 return; 147 148 deintr: 149 pci_intr_disestablish(psc->psc_pc, psc->psc_ih); 150 psc->psc_ih = NULL; 151 unmap: 152 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 153 sc->sc_ios = 0; 154 } 155 156 int 157 mpi_pci_detach(struct device *self, int flags) 158 { 159 struct mpi_pci_softc *psc = (struct mpi_pci_softc *)self; 160 struct mpi_softc *sc = &psc->psc_mpi; 161 162 mpi_detach(sc); 163 164 if (psc->psc_ih != NULL) { 165 pci_intr_disestablish(psc->psc_pc, psc->psc_ih); 166 psc->psc_ih = NULL; 167 } 168 if (sc->sc_ios != 0) { 169 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios); 170 sc->sc_ios = 0; 171 } 172 173 return (0); 174 } 175