Home | History | Annotate | Line # | Download | only in pci
mfi_pci.c revision 1.1
      1 /* $OpenBSD: mfi_pci.c,v 1.11 2006/08/06 04:40:08 brad Exp $ */
      2 /*
      3  * Copyright (c) 2006 Marco Peereboom <marco (at) 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/kernel.h>
     21 #include <sys/malloc.h>
     22 #include <sys/device.h>
     23 
     24 #include <dev/pci/pcidevs.h>
     25 #include <dev/pci/pcivar.h>
     26 
     27 #include <machine/bus.h>
     28 
     29 #include <dev/scsipi/scsipi_all.h>
     30 #include <dev/scsipi/scsipi_disk.h>
     31 #include <dev/scsipi/scsiconf.h>
     32 
     33 #include <dev/ic/mfireg.h>
     34 #include <dev/ic/mfivar.h>
     35 
     36 #define	MFI_BAR		0x10
     37 #define	MFI_PCI_MEMSIZE	0x2000 /* 8k */
     38 
     39 int	mfi_pci_find_device(void *);
     40 int	mfi_pci_match(struct device *, struct cfdata *, void *);
     41 void	mfi_pci_attach(struct device *, struct device *, void *);
     42 
     43 CFATTACH_DECL(mfi_pci, sizeof(struct mfi_softc),
     44     mfi_pci_match, mfi_pci_attach, NULL, NULL);
     45 
     46 struct	mfi_pci_device {
     47 	pcireg_t	mpd_vendor;
     48 	pcireg_t	mpd_product;
     49 	pcireg_t	mpd_subvendor;
     50 	pcireg_t	mpd_subproduct;
     51 	const char	*mpd_model;
     52 	uint32_t	mpd_flags;
     53 };
     54 
     55 static const
     56 struct  mfi_pci_device mfi_pci_devices[] = {
     57 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
     58 	  0,			0,		"",			0 },
     59 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
     60 	  0,			0,		"",			0 },
     61 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
     62 	  PCI_VENDOR_DELL,	0x1f01,		"Dell PERC 5/e",	0 },
     63 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
     64 	  PCI_VENDOR_DELL,	0x1f02,		"Dell PERC 5/i",	0 },
     65 	{ 0, 0,
     66 	  0, 0, NULL, 0}
     67 };
     68 
     69 int
     70 mfi_pci_find_device(void *aux) {
     71 	struct pci_attach_args	*pa = aux;
     72 	int			i;
     73 
     74 	for (i = 0; mfi_pci_devices[i].mpd_vendor; i++) {
     75 		if (mfi_pci_devices[i].mpd_vendor == PCI_VENDOR(pa->pa_id) &&
     76 		    mfi_pci_devices[i].mpd_product == PCI_PRODUCT(pa->pa_id)) {
     77 		    	DNPRINTF(MFI_D_MISC, "mfi_pci_find_device: %i\n", i);
     78 			return (i);
     79 		}
     80 	}
     81 
     82 	return (-1);
     83 }
     84 
     85 int
     86 mfi_pci_match(struct device *parent, struct cfdata *match, void *aux)
     87 {
     88 	int			i;
     89 
     90 	if ((i = mfi_pci_find_device(aux)) != -1) {
     91 		DNPRINTF(MFI_D_MISC,
     92 		    "mfi_pci_match: vendor: %04x  product: %04x\n",
     93 		    mfi_pci_devices[i].mpd_vendor,
     94 		    mfi_pci_devices[i].mpd_product);
     95 
     96 		return (1);
     97 	}
     98 
     99 	return (0);
    100 }
    101 
    102 void
    103 mfi_pci_attach(struct device *parent, struct device *self, void *aux)
    104 {
    105 	struct mfi_softc	*sc = (struct mfi_softc *)self;
    106 	struct pci_attach_args	*pa = aux;
    107 	const char		*intrstr;
    108 	pci_intr_handle_t	ih;
    109 	bus_size_t		size;
    110 	pcireg_t		csr;
    111 	uint32_t		subsysid, i;
    112 
    113 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    114 	for (i = 0; mfi_pci_devices[i].mpd_vendor; i++)
    115 		if (mfi_pci_devices[i].mpd_subvendor == PCI_VENDOR(subsysid) &&
    116 		    mfi_pci_devices[i].mpd_subproduct == PCI_PRODUCT(subsysid)){
    117 				aprint_normal(", %s",
    118 				    mfi_pci_devices[i].mpd_model);
    119 				break;
    120 		}
    121 
    122 	csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MFI_BAR);
    123 	csr |= PCI_MAPREG_MEM_TYPE_32BIT;
    124 	if (pci_mapreg_map(pa, MFI_BAR, csr, 0,
    125 	    &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
    126 		aprint_error(": can't map controller pci space\n");
    127 		return;
    128 	}
    129 
    130 	sc->sc_dmat = pa->pa_dmat;
    131 
    132 	if (pci_intr_map(pa, &ih)) {
    133 		aprint_error(": can't map interrupt\n");
    134 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    135 		return;
    136 	}
    137 	intrstr = pci_intr_string(pa->pa_pc, ih);
    138 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc);
    139 	if (!sc->sc_ih) {
    140 		aprint_error(": can't establish interrupt");
    141 		if (intrstr)
    142 			aprint_error(" at %s", intrstr);
    143 		aprint_error("\n");
    144 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    145 		return;
    146 	}
    147 
    148 	aprint_normal(": %s\n", intrstr);
    149 
    150 	if (mfi_attach(sc)) {
    151 		aprint_error("%s: can't attach", DEVNAME(sc));
    152 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    153 		sc->sc_ih = NULL;
    154 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    155 	}
    156 }
    157