xref: /netbsd-src/sys/dev/pci/mfi_pci.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: mfi_pci.c,v 1.3 2007/10/19 12:00:52 ad 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@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.3 2007/10/19 12:00:52 ad 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_PCI_MEMSIZE	0x2000 /* 8k */
42 
43 int	mfi_pci_find_device(void *);
44 int	mfi_pci_match(struct device *, struct cfdata *, void *);
45 void	mfi_pci_attach(struct device *, struct device *, void *);
46 
47 CFATTACH_DECL(mfi_pci, sizeof(struct mfi_softc),
48     mfi_pci_match, mfi_pci_attach, NULL, NULL);
49 
50 struct	mfi_pci_device {
51 	pcireg_t	mpd_vendor;
52 	pcireg_t	mpd_product;
53 	pcireg_t	mpd_subvendor;
54 	pcireg_t	mpd_subproduct;
55 	const char	*mpd_model;
56 	uint32_t	mpd_flags;
57 };
58 
59 static const
60 struct  mfi_pci_device mfi_pci_devices[] = {
61 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_SAS,
62 	  0,			0,		"",			0 },
63 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_MEGARAID_VERDE_ZCR,
64 	  0,			0,		"",			0 },
65 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
66 	  PCI_VENDOR_DELL,	0x1f01,		"Dell PERC 5/e",	0 },
67 	{ PCI_VENDOR_DELL,	PCI_PRODUCT_DELL_PERC_5,
68 	  PCI_VENDOR_DELL,	0x1f02,		"Dell PERC 5/i",	0 },
69 	{ 0, 0,
70 	  0, 0, NULL, 0}
71 };
72 
73 int
74 mfi_pci_find_device(void *aux) {
75 	struct pci_attach_args	*pa = aux;
76 	int			i;
77 
78 	for (i = 0; mfi_pci_devices[i].mpd_vendor; i++) {
79 		if (mfi_pci_devices[i].mpd_vendor == PCI_VENDOR(pa->pa_id) &&
80 		    mfi_pci_devices[i].mpd_product == PCI_PRODUCT(pa->pa_id)) {
81 		    	DNPRINTF(MFI_D_MISC, "mfi_pci_find_device: %i\n", i);
82 			return (i);
83 		}
84 	}
85 
86 	return (-1);
87 }
88 
89 int
90 mfi_pci_match(struct device *parent, struct cfdata *match, void *aux)
91 {
92 	int			i;
93 
94 	if ((i = mfi_pci_find_device(aux)) != -1) {
95 		DNPRINTF(MFI_D_MISC,
96 		    "mfi_pci_match: vendor: %04x  product: %04x\n",
97 		    mfi_pci_devices[i].mpd_vendor,
98 		    mfi_pci_devices[i].mpd_product);
99 
100 		return (1);
101 	}
102 
103 	return (0);
104 }
105 
106 void
107 mfi_pci_attach(struct device *parent, struct device *self, void *aux)
108 {
109 	struct mfi_softc	*sc = (struct mfi_softc *)self;
110 	struct pci_attach_args	*pa = aux;
111 	const char		*intrstr;
112 	pci_intr_handle_t	ih;
113 	bus_size_t		size;
114 	pcireg_t		csr;
115 	uint32_t		subsysid, i;
116 
117 	subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
118 	for (i = 0; mfi_pci_devices[i].mpd_vendor; i++)
119 		if (mfi_pci_devices[i].mpd_subvendor == PCI_VENDOR(subsysid) &&
120 		    mfi_pci_devices[i].mpd_subproduct == PCI_PRODUCT(subsysid)){
121 				aprint_normal(", %s",
122 				    mfi_pci_devices[i].mpd_model);
123 				break;
124 		}
125 
126 	csr = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MFI_BAR);
127 	csr |= PCI_MAPREG_MEM_TYPE_32BIT;
128 	if (pci_mapreg_map(pa, MFI_BAR, csr, 0,
129 	    &sc->sc_iot, &sc->sc_ioh, NULL, &size)) {
130 		aprint_error(": can't map controller pci space\n");
131 		return;
132 	}
133 
134 	sc->sc_dmat = pa->pa_dmat;
135 
136 	if (pci_intr_map(pa, &ih)) {
137 		aprint_error(": can't map interrupt\n");
138 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
139 		return;
140 	}
141 	intrstr = pci_intr_string(pa->pa_pc, ih);
142 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mfi_intr, sc);
143 	if (!sc->sc_ih) {
144 		aprint_error(": can't establish interrupt");
145 		if (intrstr)
146 			aprint_error(" at %s", intrstr);
147 		aprint_error("\n");
148 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
149 		return;
150 	}
151 
152 	aprint_normal(": %s\n", intrstr);
153 
154 	if (mfi_attach(sc)) {
155 		aprint_error("%s: can't attach", DEVNAME(sc));
156 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
157 		sc->sc_ih = NULL;
158 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
159 	}
160 }
161