1 /* $OpenBSD: mfi_pci.c,v 1.32 2024/05/24 06:02:58 jsg Exp $ */ 2 /* 3 * Copyright (c) 2006 Marco Peereboom <marco@peereboom.us> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/rwlock.h> 22 #include <sys/sensors.h> 23 24 #include <dev/pci/pcidevs.h> 25 #include <dev/pci/pcivar.h> 26 27 #include <machine/bus.h> 28 29 #include <scsi/scsi_all.h> 30 #include <scsi/scsi_disk.h> 31 #include <scsi/scsiconf.h> 32 33 #include <dev/ic/mfireg.h> 34 #include <dev/ic/mfivar.h> 35 36 #define MFI_BAR 0x10 37 #define MFI_BAR_GEN2 0x14 38 #define MFI_PCI_MEMSIZE 0x2000 /* 8k */ 39 40 int mfi_pci_match(struct device *, void *, void *); 41 void mfi_pci_attach(struct device *, struct device *, void *); 42 43 const struct cfattach mfi_pci_ca = { 44 sizeof(struct mfi_softc), mfi_pci_match, mfi_pci_attach 45 }; 46 47 static const 48 struct mfi_pci_device { 49 pcireg_t mpd_vendor; 50 pcireg_t mpd_product; 51 enum mfi_iop mpd_iop; 52 } mfi_pci_devices[] = { 53 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_SAS, 54 MFI_IOP_XSCALE }, 55 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR, 56 MFI_IOP_XSCALE }, 57 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1078, 58 MFI_IOP_PPC }, 59 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS1078DE, 60 MFI_IOP_PPC }, 61 { PCI_VENDOR_DELL, PCI_PRODUCT_DELL_PERC5, 62 MFI_IOP_XSCALE }, 63 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_1, 64 MFI_IOP_GEN2 }, 65 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2108_2, 66 MFI_IOP_GEN2 }, 67 { PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_SAS2008_1, 68 MFI_IOP_SKINNY } 69 }; 70 71 const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *); 72 73 const struct mfi_pci_device * 74 mfi_pci_find_device(struct pci_attach_args *pa) 75 { 76 const struct mfi_pci_device *mpd; 77 int i; 78 79 for (i = 0; i < nitems(mfi_pci_devices); i++) { 80 mpd = &mfi_pci_devices[i]; 81 82 if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) && 83 mpd->mpd_product == PCI_PRODUCT(pa->pa_id)) 84 return (mpd); 85 } 86 87 return (NULL); 88 } 89 90 int 91 mfi_pci_match(struct device *parent, void *match, void *aux) 92 { 93 return ((mfi_pci_find_device(aux) != NULL) ? 1 : 0); 94 } 95 96 void 97 mfi_pci_attach(struct device *parent, struct device *self, void *aux) 98 { 99 struct mfi_softc *sc = (struct mfi_softc *)self; 100 struct pci_attach_args *pa = aux; 101 const struct mfi_pci_device *mpd; 102 pci_intr_handle_t ih; 103 bus_size_t size; 104 pcireg_t reg; 105 int regbar; 106 107 mpd = mfi_pci_find_device(pa); 108 if (mpd == NULL) { 109 printf(": can't find matching pci device\n"); 110 return; 111 } 112 113 if (mpd->mpd_iop == MFI_IOP_GEN2 || mpd->mpd_iop == MFI_IOP_SKINNY) 114 regbar = MFI_BAR_GEN2; 115 else 116 regbar = MFI_BAR; 117 118 reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, regbar); 119 if (pci_mapreg_map(pa, regbar, reg, 0, 120 &sc->sc_iot, &sc->sc_ioh, NULL, &size, MFI_PCI_MEMSIZE)) { 121 printf(": can't map controller pci space\n"); 122 return; 123 } 124 125 sc->sc_dmat = pa->pa_dmat; 126 127 if (pci_intr_map(pa, &ih) != 0) { 128 printf(": can't map interrupt\n"); 129 goto unmap; 130 } 131 printf(": %s\n", pci_intr_string(pa->pa_pc, ih)); 132 133 sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO | IPL_MPSAFE, 134 mfi_intr, sc, sc->sc_dev.dv_xname); 135 if (!sc->sc_ih) { 136 printf("%s: can't establish interrupt\n", DEVNAME(sc)); 137 goto unmap; 138 } 139 140 if (mfi_attach(sc, mpd->mpd_iop)) { 141 printf("%s: can't attach\n", DEVNAME(sc)); 142 goto unintr; 143 } 144 145 return; 146 unintr: 147 pci_intr_disestablish(pa->pa_pc, sc->sc_ih); 148 sc->sc_ih = NULL; 149 unmap: 150 bus_space_unmap(sc->sc_iot, sc->sc_ioh, size); 151 } 152