Home | History | Annotate | Line # | Download | only in pci
mfi_pci.c revision 1.12.16.2.2.1
      1 /* $NetBSD: mfi_pci.c,v 1.12.16.2.2.1 2012/11/01 16:45:02 matt 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 (at) 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.12.16.2.2.1 2012/11/01 16:45:02 matt 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_BAR_GEN2	0x14
     42 #define	MFI_PCI_MEMSIZE	0x2000 /* 8k */
     43 
     44 struct mfi_pci_softc {
     45 	struct mfi_softc	psc_sc;
     46 	pci_chipset_tag_t	psc_pc;
     47 };
     48 
     49 const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *);
     50 int	mfi_pci_match(device_t, cfdata_t, void *);
     51 void	mfi_pci_attach(device_t, device_t, void *);
     52 int	mfi_pci_detach(device_t, int);
     53 
     54 CFATTACH_DECL3_NEW(mfi_pci, sizeof(struct mfi_pci_softc),
     55     mfi_pci_match, mfi_pci_attach, mfi_pci_detach, NULL, mfi_rescan,
     56     mfi_childdetached, DVF_DETACH_SHUTDOWN);
     57 
     58 struct mfi_pci_subtype {
     59 	pcireg_t 	st_vendor;
     60 	pcireg_t 	st_product;
     61 	const char 	*st_string;
     62 };
     63 
     64 static const struct mfi_pci_subtype mfi_1078_subtypes[] = {
     65 	{ PCI_VENDOR_SYMBIOS, 	0x1006, 	"SAS 8888ELP" },
     66 	{ PCI_VENDOR_SYMBIOS, 	0x100a, 	"SAS 8708ELP" },
     67 	{ PCI_VENDOR_SYMBIOS, 	0x100f, 	"SAS 8708E" },
     68 	{ PCI_VENDOR_SYMBIOS, 	0x1012, 	"SAS 8704ELP" },
     69 	{ PCI_VENDOR_SYMBIOS, 	0x1013, 	"SAS 8708EM2" },
     70 	{ PCI_VENDOR_SYMBIOS, 	0x1016, 	"SAS 8880EM2" },
     71 	{ PCI_VENDOR_DELL, 	0x1f0a, 	"Dell PERC 6/e" },
     72 	{ PCI_VENDOR_DELL, 	0x1f0b, 	"Dell PERC 6/i" },
     73 	{ PCI_VENDOR_DELL, 	0x1f0c, 	"Dell PERC 6/i integrated" },
     74 	{ PCI_VENDOR_DELL, 	0x1f0d, 	"Dell CERC 6/i" },
     75 	{ PCI_VENDOR_DELL, 	0x1f11, 	"Dell CERC 6/i integrated" },
     76 	{ 0, 			0, 		NULL }
     77 };
     78 
     79 static const struct mfi_pci_subtype mfi_perc5_subtypes[] = {
     80 	{ PCI_VENDOR_DELL,	0x1f01, 	"Dell PERC 5/e" },
     81 	{ PCI_VENDOR_DELL, 	0x1f02, 	"Dell PERC 5/i" },
     82 	{ PCI_VENDOR_DELL, 	0x1f03, 	"Dell PERC 5/i integrated" },
     83 	{ 0, 			0, 		NULL }
     84 };
     85 
     86 static const struct mfi_pci_subtype mfi_gen2_subtypes[] = {
     87 	{ PCI_VENDOR_SYMBIOS,	0x9261,		"SAS 9260-8i" },
     88 	{ PCI_VENDOR_SYMBIOS,	0x9263,		"SAS 9261-8i" },
     89 	{ PCI_VENDOR_IBM,	0x03c7,		"IBM ServeRAID M5014 SAS/SATA" },
     90 	{ 0x0,			0,		"" }
     91 };
     92 
     93 static const struct mfi_pci_subtype mfi_skinny_subtypes[] = {
     94 	{ PCI_VENDOR_IBM,	0x03b1,		"IBM ServeRAID M1015 SAS/SATA" },
     95 	{ 0x0,			0,		"" }
     96 };
     97 
     98 static const
     99 struct mfi_pci_device {
    100 	pcireg_t			mpd_vendor;
    101 	pcireg_t			mpd_product;
    102 	enum mfi_iop			mpd_iop;
    103 	const struct mfi_pci_subtype 	*mpd_subtype;
    104 } mfi_pci_devices[] = {
    105 	{ PCI_VENDOR_SYMBIOS, 	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
    106 	  MFI_IOP_XSCALE, 	NULL },
    107 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
    108 	  MFI_IOP_XSCALE,	NULL },
    109 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078,
    110 	  MFI_IOP_PPC,		mfi_1078_subtypes },
    111 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078DE,
    112 	  MFI_IOP_PPC,		mfi_1078_subtypes },
    113 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
    114 	  MFI_IOP_XSCALE,	mfi_perc5_subtypes },
    115 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_6,
    116 	  MFI_IOP_PPC, 		mfi_1078_subtypes },
    117 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_1,
    118 	  MFI_IOP_GEN2,		mfi_gen2_subtypes },
    119 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_2,
    120 	  MFI_IOP_GEN2,		mfi_gen2_subtypes },
    121 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2008_1,
    122 	  MFI_IOP_SKINNY,	mfi_skinny_subtypes },
    123 };
    124 
    125 const struct mfi_pci_device *
    126 mfi_pci_find_device(struct pci_attach_args *pa)
    127 {
    128 	const struct mfi_pci_device *mpd;
    129 	int i;
    130 
    131 	for (i = 0; i < __arraycount(mfi_pci_devices); i++) {
    132 		mpd = &mfi_pci_devices[i];
    133 
    134 		if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) &&
    135 		    mpd->mpd_product == PCI_PRODUCT(pa->pa_id))
    136 			return mpd;
    137 	}
    138 
    139 	return NULL;
    140 }
    141 
    142 int
    143 mfi_pci_match(device_t parent, cfdata_t match, void *aux)
    144 {
    145 	return (mfi_pci_find_device(aux) != NULL) ? 1 : 0;
    146 }
    147 
    148 int
    149 mfi_pci_detach(device_t self, int flags)
    150 {
    151 	struct mfi_pci_softc	*psc = device_private(self);
    152 	struct mfi_softc	*sc = &psc->psc_sc;
    153 	int error;
    154 
    155 	if ((error = mfi_detach(sc, flags)) != 0)
    156 		return error;
    157 
    158 	/* xxx */
    159 	pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
    160 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
    161 	return 0;
    162 }
    163 
    164 void
    165 mfi_pci_attach(device_t parent, device_t self, void *aux)
    166 {
    167 	struct mfi_pci_softc	*psc = device_private(self);
    168 	struct mfi_softc	*sc = &psc->psc_sc;
    169 	struct pci_attach_args	*pa = aux;
    170 	const struct mfi_pci_device *mpd;
    171 	const struct mfi_pci_subtype *st;
    172 	const char		*intrstr;
    173 	pci_intr_handle_t	ih;
    174 	pcireg_t		csr;
    175 	int			regbar;
    176 	const char 		*subtype = NULL;
    177 	uint32_t		subsysid;
    178 
    179 	sc->sc_dev = self;
    180 	psc->psc_pc = pa->pa_pc;
    181 
    182 	mpd = mfi_pci_find_device(pa);
    183 	if (mpd == NULL) {
    184 		printf(": can't find matching pci device\n");
    185 		return;
    186 	}
    187 
    188 	sc->sc_flags = mpd->mpd_iop;
    189 
    190 	if (mpd->mpd_iop == MFI_IOP_GEN2 || mpd->mpd_iop == MFI_IOP_SKINNY)
    191 		regbar = MFI_BAR_GEN2;
    192 	else
    193 		regbar = MFI_BAR;
    194 
    195 	csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, regbar);
    196 	csr |= PCI_MAPREG_MEM_TYPE_32BIT;
    197 	if (pci_mapreg_map(pa, regbar, csr, 0,
    198 	    &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_size)) {
    199 		aprint_error(": can't map controller pci space\n");
    200 		return;
    201 	}
    202 
    203 	sc->sc_dmat = pa->pa_dmat;
    204 
    205 	if (pci_intr_map(pa, &ih)) {
    206 		aprint_error(": can't map interrupt\n");
    207 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
    208 		return;
    209 	}
    210 	intrstr = pci_intr_string(pa->pa_pc, ih);
    211 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc);
    212 	if (!sc->sc_ih) {
    213 		aprint_error(": can't establish interrupt");
    214 		if (intrstr)
    215 			aprint_error(" at %s", intrstr);
    216 		aprint_error("\n");
    217 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
    218 		return;
    219 	}
    220 
    221 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    222 	if (mpd->mpd_subtype != NULL) {
    223 		st = mpd->mpd_subtype;
    224 		while (st->st_vendor != 0) {
    225 			if (PCI_VENDOR(subsysid) == st->st_vendor &&
    226 			    PCI_PRODUCT(subsysid) == st->st_product) {
    227 				subtype = st->st_string;
    228 				break;
    229 			}
    230 			st++;
    231 		}
    232 		if (subtype)
    233 			aprint_normal(": %s\n", subtype);
    234 	}
    235 
    236 	aprint_normal("%s: interrupting at %s\n", DEVNAME(sc), intrstr);
    237 
    238 	if (mfi_attach(sc, mpd->mpd_iop)) {
    239 		aprint_error("%s: can't attach", DEVNAME(sc));
    240 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    241 		sc->sc_ih = NULL;
    242 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_size);
    243 	}
    244 }
    245