Home | History | Annotate | Line # | Download | only in pci
mfi_pci.c revision 1.1
      1  1.1  bouyer /* $OpenBSD: mfi_pci.c,v 1.11 2006/08/06 04:40:08 brad Exp $ */
      2  1.1  bouyer /*
      3  1.1  bouyer  * Copyright (c) 2006 Marco Peereboom <marco (at) peereboom.us>
      4  1.1  bouyer  *
      5  1.1  bouyer  * Permission to use, copy, modify, and distribute this software for any
      6  1.1  bouyer  * purpose with or without fee is hereby granted, provided that the above
      7  1.1  bouyer  * copyright notice and this permission notice appear in all copies.
      8  1.1  bouyer  *
      9  1.1  bouyer  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     10  1.1  bouyer  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11  1.1  bouyer  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     12  1.1  bouyer  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13  1.1  bouyer  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14  1.1  bouyer  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     15  1.1  bouyer  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16  1.1  bouyer  */
     17  1.1  bouyer 
     18  1.1  bouyer #include <sys/param.h>
     19  1.1  bouyer #include <sys/systm.h>
     20  1.1  bouyer #include <sys/kernel.h>
     21  1.1  bouyer #include <sys/malloc.h>
     22  1.1  bouyer #include <sys/device.h>
     23  1.1  bouyer 
     24  1.1  bouyer #include <dev/pci/pcidevs.h>
     25  1.1  bouyer #include <dev/pci/pcivar.h>
     26  1.1  bouyer 
     27  1.1  bouyer #include <machine/bus.h>
     28  1.1  bouyer 
     29  1.1  bouyer #include <dev/scsipi/scsipi_all.h>
     30  1.1  bouyer #include <dev/scsipi/scsipi_disk.h>
     31  1.1  bouyer #include <dev/scsipi/scsiconf.h>
     32  1.1  bouyer 
     33  1.1  bouyer #include <dev/ic/mfireg.h>
     34  1.1  bouyer #include <dev/ic/mfivar.h>
     35  1.1  bouyer 
     36  1.1  bouyer #define	MFI_BAR		0x10
     37  1.1  bouyer #define	MFI_PCI_MEMSIZE	0x2000 /* 8k */
     38  1.1  bouyer 
     39  1.1  bouyer int	mfi_pci_find_device(void *);
     40  1.1  bouyer int	mfi_pci_match(struct device *, struct cfdata *, void *);
     41  1.1  bouyer void	mfi_pci_attach(struct device *, struct device *, void *);
     42  1.1  bouyer 
     43  1.1  bouyer CFATTACH_DECL(mfi_pci, sizeof(struct mfi_softc),
     44  1.1  bouyer     mfi_pci_match, mfi_pci_attach, NULL, NULL);
     45  1.1  bouyer 
     46  1.1  bouyer struct	mfi_pci_device {
     47  1.1  bouyer 	pcireg_t	mpd_vendor;
     48  1.1  bouyer 	pcireg_t	mpd_product;
     49  1.1  bouyer 	pcireg_t	mpd_subvendor;
     50  1.1  bouyer 	pcireg_t	mpd_subproduct;
     51  1.1  bouyer 	const char	*mpd_model;
     52  1.1  bouyer 	uint32_t	mpd_flags;
     53  1.1  bouyer };
     54  1.1  bouyer 
     55  1.1  bouyer static const
     56  1.1  bouyer struct  mfi_pci_device mfi_pci_devices[] = {
     57  1.1  bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
     58  1.1  bouyer 	  0,			0,		"",			0 },
     59  1.1  bouyer 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
     60  1.1  bouyer 	  0,			0,		"",			0 },
     61  1.1  bouyer 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
     62  1.1  bouyer 	  PCI_VENDOR_DELL,	0x1f01,		"Dell PERC 5/e",	0 },
     63  1.1  bouyer 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
     64  1.1  bouyer 	  PCI_VENDOR_DELL,	0x1f02,		"Dell PERC 5/i",	0 },
     65  1.1  bouyer 	{ 0, 0,
     66  1.1  bouyer 	  0, 0, NULL, 0}
     67  1.1  bouyer };
     68  1.1  bouyer 
     69  1.1  bouyer int
     70  1.1  bouyer mfi_pci_find_device(void *aux) {
     71  1.1  bouyer 	struct pci_attach_args	*pa = aux;
     72  1.1  bouyer 	int			i;
     73  1.1  bouyer 
     74  1.1  bouyer 	for (i = 0; mfi_pci_devices[i].mpd_vendor; i++) {
     75  1.1  bouyer 		if (mfi_pci_devices[i].mpd_vendor == PCI_VENDOR(pa->pa_id) &&
     76  1.1  bouyer 		    mfi_pci_devices[i].mpd_product == PCI_PRODUCT(pa->pa_id)) {
     77  1.1  bouyer 		    	DNPRINTF(MFI_D_MISC, "mfi_pci_find_device: %i\n", i);
     78  1.1  bouyer 			return (i);
     79  1.1  bouyer 		}
     80  1.1  bouyer 	}
     81  1.1  bouyer 
     82  1.1  bouyer 	return (-1);
     83  1.1  bouyer }
     84  1.1  bouyer 
     85  1.1  bouyer int
     86  1.1  bouyer mfi_pci_match(struct device *parent, struct cfdata *match, void *aux)
     87  1.1  bouyer {
     88  1.1  bouyer 	int			i;
     89  1.1  bouyer 
     90  1.1  bouyer 	if ((i = mfi_pci_find_device(aux)) != -1) {
     91  1.1  bouyer 		DNPRINTF(MFI_D_MISC,
     92  1.1  bouyer 		    "mfi_pci_match: vendor: %04x  product: %04x\n",
     93  1.1  bouyer 		    mfi_pci_devices[i].mpd_vendor,
     94  1.1  bouyer 		    mfi_pci_devices[i].mpd_product);
     95  1.1  bouyer 
     96  1.1  bouyer 		return (1);
     97  1.1  bouyer 	}
     98  1.1  bouyer 
     99  1.1  bouyer 	return (0);
    100  1.1  bouyer }
    101  1.1  bouyer 
    102  1.1  bouyer void
    103  1.1  bouyer mfi_pci_attach(struct device *parent, struct device *self, void *aux)
    104  1.1  bouyer {
    105  1.1  bouyer 	struct mfi_softc	*sc = (struct mfi_softc *)self;
    106  1.1  bouyer 	struct pci_attach_args	*pa = aux;
    107  1.1  bouyer 	const char		*intrstr;
    108  1.1  bouyer 	pci_intr_handle_t	ih;
    109  1.1  bouyer 	bus_size_t		size;
    110  1.1  bouyer 	pcireg_t		csr;
    111  1.1  bouyer 	uint32_t		subsysid, i;
    112  1.1  bouyer 
    113  1.1  bouyer 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    114  1.1  bouyer 	for (i = 0; mfi_pci_devices[i].mpd_vendor; i++)
    115  1.1  bouyer 		if (mfi_pci_devices[i].mpd_subvendor == PCI_VENDOR(subsysid) &&
    116  1.1  bouyer 		    mfi_pci_devices[i].mpd_subproduct == PCI_PRODUCT(subsysid)){
    117  1.1  bouyer 				aprint_normal(", %s",
    118  1.1  bouyer 				    mfi_pci_devices[i].mpd_model);
    119  1.1  bouyer 				break;
    120  1.1  bouyer 		}
    121  1.1  bouyer 
    122  1.1  bouyer 	csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MFI_BAR);
    123  1.1  bouyer 	csr |= PCI_MAPREG_MEM_TYPE_32BIT;
    124  1.1  bouyer 	if (pci_mapreg_map(pa, MFI_BAR, csr, 0,
    125  1.1  bouyer 	    &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
    126  1.1  bouyer 		aprint_error(": can't map controller pci space\n");
    127  1.1  bouyer 		return;
    128  1.1  bouyer 	}
    129  1.1  bouyer 
    130  1.1  bouyer 	sc->sc_dmat = pa->pa_dmat;
    131  1.1  bouyer 
    132  1.1  bouyer 	if (pci_intr_map(pa, &ih)) {
    133  1.1  bouyer 		aprint_error(": can't map interrupt\n");
    134  1.1  bouyer 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    135  1.1  bouyer 		return;
    136  1.1  bouyer 	}
    137  1.1  bouyer 	intrstr = pci_intr_string(pa->pa_pc, ih);
    138  1.1  bouyer 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc);
    139  1.1  bouyer 	if (!sc->sc_ih) {
    140  1.1  bouyer 		aprint_error(": can't establish interrupt");
    141  1.1  bouyer 		if (intrstr)
    142  1.1  bouyer 			aprint_error(" at %s", intrstr);
    143  1.1  bouyer 		aprint_error("\n");
    144  1.1  bouyer 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    145  1.1  bouyer 		return;
    146  1.1  bouyer 	}
    147  1.1  bouyer 
    148  1.1  bouyer 	aprint_normal(": %s\n", intrstr);
    149  1.1  bouyer 
    150  1.1  bouyer 	if (mfi_attach(sc)) {
    151  1.1  bouyer 		aprint_error("%s: can't attach", DEVNAME(sc));
    152  1.1  bouyer 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    153  1.1  bouyer 		sc->sc_ih = NULL;
    154  1.1  bouyer 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    155  1.1  bouyer 	}
    156  1.1  bouyer }
    157