1 /* $NetBSD: mfi_pci.c,v 1.8 2009/05/12 08:23:01 cegger Exp $ */ 2 /* $OpenBSD: mfi_pci.c,v 1.11 2006/08/06 04:40:08 brad Exp $ */ 3 /* 4 * Copyright (c) 2006 Marco Peereboom <marco@peereboom.us> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/cdefs.h> 20 __KERNEL_RCSID(0, "$NetBSD: mfi_pci.c,v 1.8 2009/05/12 08:23:01 cegger Exp $"); 21 22 #include <sys/param.h> 23 #include <sys/systm.h> 24 #include <sys/kernel.h> 25 #include <sys/malloc.h> 26 #include <sys/device.h> 27 28 #include <dev/pci/pcidevs.h> 29 #include <dev/pci/pcivar.h> 30 31 #include <sys/bus.h> 32 33 #include <dev/scsipi/scsipi_all.h> 34 #include <dev/scsipi/scsipi_disk.h> 35 #include <dev/scsipi/scsiconf.h> 36 37 #include <dev/ic/mfireg.h> 38 #include <dev/ic/mfivar.h> 39 40 #define MFI_BAR 0x10 41 #define MFI_PCI_MEMSIZE 0x2000 /* 8k */ 42 43 const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *); 44 int mfi_pci_match(device_t, cfdata_t, void *); 45 void mfi_pci_attach(device_t, device_t, void *); 46 47 CFATTACH_DECL(mfi_pci, sizeof(struct mfi_softc), 48 mfi_pci_match, mfi_pci_attach, NULL, NULL); 49 50 struct mfi_pci_subtype { 51 pcireg_t st_vendor; 52 pcireg_t st_product; 53 const char *st_string; 54 }; 55 56 static const struct mfi_pci_subtype mfi_1078_subtypes[] = { 57 { PCI_VENDOR_SYMBIOS, 0x1006, "SAS 8888ELP" }, 58 { PCI_VENDOR_SYMBIOS, 0x100a, "SAS 8708ELP" }, 59 { PCI_VENDOR_SYMBIOS, 0x100f, "SAS 8708E" }, 60 { PCI_VENDOR_SYMBIOS, 0x1012, "SAS 8704ELP" }, 61 { PCI_VENDOR_SYMBIOS, 0x1013, "SAS 8708EM2" }, 62 { PCI_VENDOR_SYMBIOS, 0x1016, "SAS 8880EM2" }, 63 { PCI_VENDOR_DELL, 0x1f0a, "Dell PERC 6/e" }, 64 { PCI_VENDOR_DELL, 0x1f0b, "Dell PERC 6/i" }, 65 { PCI_VENDOR_DELL, 0x1f0c, "Dell PERC 6/i integrated" }, 66 { PCI_VENDOR_DELL, 0x1f0d, "Dell CERC 6/i" }, 67 { PCI_VENDOR_DELL, 0x1f11, "Dell CERC 6/i integrated" }, 68 { 0, 0, NULL } 69 }; 70 71 static const struct mfi_pci_subtype mfi_perc5_subtypes[] = { 72 { PCI_VENDOR_DELL, 0x1f01, "Dell PERC 5/e" }, 73 { PCI_VENDOR_DELL, 0x1f02, "Dell PERC 5/i" }, 74 { PCI_VENDOR_DELL, 0x1f03, "Dell PERC 5/i integrated" }, 75 { 0, 0, NULL } 76 }; 77 78 static const 79 struct mfi_pci_device { 80 pcireg_t mpd_vendor; 81 pcireg_t mpd_product; 82 enum mfi_iop mpd_iop; 83 const struct mfi_pci_subtype *mpd_subtype; 84 } mfi_pci_devices[] = { 85 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_SAS, 86 MFI_IOP_XSCALE, NULL }, 87 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR, 88 MFI_IOP_XSCALE, NULL }, 89 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1078, 90 MFI_IOP_PPC, mfi_1078_subtypes }, 91 { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_5, 92 MFI_IOP_XSCALE, mfi_perc5_subtypes }, 93 { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC_6, 94 MFI_IOP_PPC, mfi_1078_subtypes }, 95 }; 96 97 const struct mfi_pci_device * 98 mfi_pci_find_device(struct pci_attach_args *pa) 99 { 100 const struct mfi_pci_device *mpd; 101 int i; 102 103 for (i = 0; i < __arraycount(mfi_pci_devices); i++) { 104 mpd = &mfi_pci_devices[i]; 105 106 if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) && 107 mpd->mpd_product == PCI_PRODUCT(pa->pa_id)) 108 return mpd; 109 } 110 111 return NULL; 112 } 113 114 int 115 mfi_pci_match(device_t parent, cfdata_t match, void *aux) 116 { 117 return (mfi_pci_find_device(aux) != NULL) ? 1 : 0; 118 } 119 120 void 121 mfi_pci_attach(device_t parent, device_t self, void *aux) 122 { 123 struct mfi_softc *sc = device_private(self); 124 struct pci_attach_args *pa = aux; 125 const struct mfi_pci_device *mpd; 126 const struct mfi_pci_subtype *st; 127 const char *intrstr; 128 pci_intr_handle_t ih; 129 bus_size_t size; 130 pcireg_t csr; 131 const char *subtype = NULL; 132 uint32_t subsysid; 133 134 csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MFI_BAR); 135 csr |= PCI_MAPREG_MEM_TYPE_32BIT; 136 if (pci_mapreg_map(pa, MFI_BAR, csr, 0, 137 &sc->sc_iot, &sc->sc_ioh, NULL, &size)) { 138 aprint_error(": can't map controller pci space\n"); 139 return; 140 } 141 142 sc->sc_dmat = pa->pa_dmat; 143 144 if (pci_intr_map(pa, &ih)) { 145 aprint_error(": can't map interrupt\n"); 146 bus_space_unmap(sc->sc_iot, sc->sc_ioh, size); 147 return; 148 } 149 intrstr = pci_intr_string(pa->pa_pc, ih); 150 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc); 151 if (!sc->sc_ih) { 152 aprint_error(": can't establish interrupt"); 153 if (intrstr) 154 aprint_error(" at %s", intrstr); 155 aprint_error("\n"); 156 bus_space_unmap(sc->sc_iot, sc->sc_ioh, size); 157 return; 158 } 159 160 mpd = mfi_pci_find_device(pa); 161 162 subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG); 163 if (mpd->mpd_subtype != NULL) { 164 st = mpd->mpd_subtype; 165 while (st->st_vendor != 0) { 166 if (PCI_VENDOR(subsysid) == st->st_vendor && 167 PCI_PRODUCT(subsysid) == st->st_product) { 168 subtype = st->st_string; 169 break; 170 } 171 st++; 172 } 173 if (subtype) 174 aprint_normal(": %s\n", subtype); 175 } 176 177 aprint_normal("%s: interrupting at %s\n", DEVNAME(sc), intrstr); 178 179 if (mfi_attach(sc, mpd->mpd_iop)) { 180 aprint_error("%s: can't attach", DEVNAME(sc)); 181 pci_intr_disestablish(pa->pa_pc, sc->sc_ih); 182 sc->sc_ih = NULL; 183 bus_space_unmap(sc->sc_iot, sc->sc_ioh, size); 184 } 185 } 186