Home | History | Annotate | Line # | Download | only in pci
mfi_pci.c revision 1.5.22.1
      1 /* $NetBSD: mfi_pci.c,v 1.5.22.1 2010/04/21 00:27:42 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.5.22.1 2010/04/21 00:27:42 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 const struct mfi_pci_device *mfi_pci_find_device(struct pci_attach_args *);
     45 int	mfi_pci_match(device_t, cfdata_t, void *);
     46 void	mfi_pci_attach(device_t, device_t, void *);
     47 
     48 CFATTACH_DECL_NEW(mfi_pci, sizeof(struct mfi_softc),
     49     mfi_pci_match, mfi_pci_attach, NULL, NULL);
     50 
     51 struct mfi_pci_subtype {
     52 	pcireg_t 	st_vendor;
     53 	pcireg_t 	st_product;
     54 	const char 	*st_string;
     55 };
     56 
     57 static const struct mfi_pci_subtype mfi_1078_subtypes[] = {
     58 	{ PCI_VENDOR_SYMBIOS, 	0x1006, 	"SAS 8888ELP" },
     59 	{ PCI_VENDOR_SYMBIOS, 	0x100a, 	"SAS 8708ELP" },
     60 	{ PCI_VENDOR_SYMBIOS, 	0x100f, 	"SAS 8708E" },
     61 	{ PCI_VENDOR_SYMBIOS, 	0x1012, 	"SAS 8704ELP" },
     62 	{ PCI_VENDOR_SYMBIOS, 	0x1013, 	"SAS 8708EM2" },
     63 	{ PCI_VENDOR_SYMBIOS, 	0x1016, 	"SAS 8880EM2" },
     64 	{ PCI_VENDOR_DELL, 	0x1f0a, 	"Dell PERC 6/e" },
     65 	{ PCI_VENDOR_DELL, 	0x1f0b, 	"Dell PERC 6/i" },
     66 	{ PCI_VENDOR_DELL, 	0x1f0c, 	"Dell PERC 6/i integrated" },
     67 	{ PCI_VENDOR_DELL, 	0x1f0d, 	"Dell CERC 6/i" },
     68 	{ PCI_VENDOR_DELL, 	0x1f11, 	"Dell CERC 6/i integrated" },
     69 	{ 0, 			0, 		NULL }
     70 };
     71 
     72 static const struct mfi_pci_subtype mfi_perc5_subtypes[] = {
     73 	{ PCI_VENDOR_DELL,	0x1f01, 	"Dell PERC 5/e" },
     74 	{ PCI_VENDOR_DELL, 	0x1f02, 	"Dell PERC 5/i" },
     75 	{ PCI_VENDOR_DELL, 	0x1f03, 	"Dell PERC 5/i integrated" },
     76 	{ 0, 			0, 		NULL }
     77 };
     78 
     79 static const struct mfi_pci_subtype mfi_gen2_subtypes[] = {
     80 	{ PCI_VENDOR_SYMBIOS,	0x9261,		"SAS 9260-8i" },
     81 	{ 0x0,			0,		"" }
     82 };
     83 
     84 static const
     85 struct mfi_pci_device {
     86 	pcireg_t			mpd_vendor;
     87 	pcireg_t			mpd_product;
     88 	enum mfi_iop			mpd_iop;
     89 	const struct mfi_pci_subtype 	*mpd_subtype;
     90 } mfi_pci_devices[] = {
     91 	{ PCI_VENDOR_SYMBIOS, 	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
     92 	  MFI_IOP_XSCALE, 	NULL },
     93 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
     94 	  MFI_IOP_XSCALE,	NULL },
     95 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078,
     96 	  MFI_IOP_PPC,		mfi_1078_subtypes },
     97 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1078DE,
     98 	  MFI_IOP_PPC,		mfi_1078_subtypes },
     99 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
    100 	  MFI_IOP_XSCALE,	mfi_perc5_subtypes },
    101 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_6,
    102 	  MFI_IOP_PPC, 		mfi_1078_subtypes },
    103 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_1,
    104 	  MFI_IOP_GEN2,		mfi_gen2_subtypes },
    105 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS2108_2,
    106 	  MFI_IOP_GEN2,		mfi_gen2_subtypes },
    107 };
    108 
    109 const struct mfi_pci_device *
    110 mfi_pci_find_device(struct pci_attach_args *pa)
    111 {
    112 	const struct mfi_pci_device *mpd;
    113 	int i;
    114 
    115 	for (i = 0; i < __arraycount(mfi_pci_devices); i++) {
    116 		mpd = &mfi_pci_devices[i];
    117 
    118 		if (mpd->mpd_vendor == PCI_VENDOR(pa->pa_id) &&
    119 		    mpd->mpd_product == PCI_PRODUCT(pa->pa_id))
    120 			return mpd;
    121 	}
    122 
    123 	return NULL;
    124 }
    125 
    126 int
    127 mfi_pci_match(device_t parent, cfdata_t match, void *aux)
    128 {
    129 	return (mfi_pci_find_device(aux) != NULL) ? 1 : 0;
    130 }
    131 
    132 void
    133 mfi_pci_attach(device_t parent, device_t self, void *aux)
    134 {
    135 	struct mfi_softc	*sc = device_private(self);
    136 	struct pci_attach_args	*pa = aux;
    137 	const struct mfi_pci_device *mpd;
    138 	const struct mfi_pci_subtype *st;
    139 	const char		*intrstr;
    140 	pci_intr_handle_t	ih;
    141 	bus_size_t		size;
    142 	pcireg_t		csr;
    143 	int			regbar;
    144 	const char 		*subtype = NULL;
    145 	uint32_t		subsysid;
    146 
    147 	sc->sc_dev = self;
    148 	mpd = mfi_pci_find_device(pa);
    149 	if (mpd == NULL) {
    150 		printf(": can't find matching pci device\n");
    151 		return;
    152 	}
    153 
    154 	if (mpd->mpd_iop == MFI_IOP_GEN2)
    155 		regbar = MFI_BAR_GEN2;
    156 	else
    157 		regbar = MFI_BAR;
    158 
    159 	csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, regbar);
    160 	csr |= PCI_MAPREG_MEM_TYPE_32BIT;
    161 	if (pci_mapreg_map(pa, regbar, csr, 0,
    162 	    &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
    163 		aprint_error(": can't map controller pci space\n");
    164 		return;
    165 	}
    166 
    167 	sc->sc_dmat = pa->pa_dmat;
    168 
    169 	if (pci_intr_map(pa, &ih)) {
    170 		aprint_error(": can't map interrupt\n");
    171 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    172 		return;
    173 	}
    174 	intrstr = pci_intr_string(pa->pa_pc, ih);
    175 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc);
    176 	if (!sc->sc_ih) {
    177 		aprint_error(": can't establish interrupt");
    178 		if (intrstr)
    179 			aprint_error(" at %s", intrstr);
    180 		aprint_error("\n");
    181 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    182 		return;
    183 	}
    184 
    185 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    186 	if (mpd->mpd_subtype != NULL) {
    187 		st = mpd->mpd_subtype;
    188 		while (st->st_vendor != 0) {
    189 			if (PCI_VENDOR(subsysid) == st->st_vendor &&
    190 			    PCI_PRODUCT(subsysid) == st->st_product) {
    191 				subtype = st->st_string;
    192 				break;
    193 			}
    194 			st++;
    195 		}
    196 		if (subtype)
    197 			aprint_normal(": %s\n", subtype);
    198 	}
    199 
    200 	aprint_normal("%s: interrupting at %s\n", DEVNAME(sc), intrstr);
    201 
    202 	if (mfi_attach(sc, mpd->mpd_iop)) {
    203 		aprint_error("%s: can't attach", DEVNAME(sc));
    204 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    205 		sc->sc_ih = NULL;
    206 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    207 	}
    208 }
    209