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