xref: /openbsd-src/sys/dev/pci/mpi_pci.c (revision 5a7c6c0ea18db96d44fd6103c8e04de017a1762d)
1 /*	$OpenBSD: mpi_pci.c,v 1.20 2008/05/27 21:52:28 dlg Exp $ */
2 
3 /*
4  * Copyright (c) 2005 David Gwynne <dlg@openbsd.org>
5  * Copyright (c) 2005 Marco Peereboom <marco@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/param.h>
21 #include <sys/systm.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/device.h>
25 
26 #include <machine/bus.h>
27 
28 #include <dev/pci/pcireg.h>
29 #include <dev/pci/pcivar.h>
30 #include <dev/pci/pcidevs.h>
31 
32 #ifdef __sparc64__
33 #include <dev/ofw/openfirm.h>
34 #endif
35 
36 #include <scsi/scsi_all.h>
37 #include <scsi/scsiconf.h>
38 
39 #include <dev/ic/mpireg.h>
40 #include <dev/ic/mpivar.h>
41 
42 int	mpi_pci_match(struct device *, void *, void *);
43 void	mpi_pci_attach(struct device *, struct device *, void *);
44 int	mpi_pci_detach(struct device *, int);
45 
46 struct mpi_pci_softc {
47 	struct mpi_softc	psc_mpi;
48 
49 	pci_chipset_tag_t	psc_pc;
50 	pcitag_t		psc_tag;
51 
52 	void			*psc_ih;
53 };
54 
55 struct cfattach mpi_pci_ca = {
56 	sizeof(struct mpi_pci_softc), mpi_pci_match, mpi_pci_attach,
57 	mpi_pci_detach
58 };
59 
60 #define PREAD(s, r)	pci_conf_read((s)->psc_pc, (s)->psc_tag, (r))
61 #define PWRITE(s, r, v)	pci_conf_write((s)->psc_pc, (s)->psc_tag, (r), (v))
62 
63 static const struct pci_matchid mpi_devices[] = {
64 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_1030 },
65 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909 },
66 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909A },
67 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919 },
68 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919_1 },
69 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919X },
70 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929 },
71 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929_1 },
72 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929X },
73 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC939X },
74 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC949E },
75 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC949X },
76 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064 },
77 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064A },
78 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064E_2 },
79 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064E },
80 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1066 },
81 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1066E },
82 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068 },
83 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068_2 },
84 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068E },
85 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068E_2 }
86 };
87 
88 int
89 mpi_pci_match(struct device *parent, void *match, void *aux)
90 {
91 	return (pci_matchbyid((struct pci_attach_args *)aux, mpi_devices,
92 	    sizeof(mpi_devices) / sizeof(mpi_devices[0])));
93 }
94 
95 void
96 mpi_pci_attach(struct device *parent, struct device *self, void *aux)
97 {
98 	struct mpi_pci_softc		*psc = (void *)self;
99 	struct mpi_softc		*sc = &psc->psc_mpi;
100 	struct pci_attach_args		*pa = aux;
101 	pcireg_t			memtype;
102 	int				r;
103 	pci_intr_handle_t		ih;
104 	const char			*intrstr;
105 #ifdef __sparc64__
106 	int node;
107 #endif
108 
109 	psc->psc_pc = pa->pa_pc;
110 	psc->psc_tag = pa->pa_tag;
111 	psc->psc_ih = NULL;
112 	sc->sc_dmat = pa->pa_dmat;
113 	sc->sc_ios = 0;
114 	sc->sc_target = -1;
115 
116 	/* find the appropriate memory base */
117 	for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
118 		memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, r);
119 		if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM)
120 			break;
121 	}
122 	if (r >= PCI_MAPREG_END) {
123 		printf(": unable to locate system interface registers\n");
124 		return;
125 	}
126 
127 	if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
128 	    NULL, &sc->sc_ios, 0) != 0) {
129 		printf(": unable to map system interface registers\n");
130 		return;
131 	}
132 
133 	/* disable the expansion rom */
134 	PWRITE(psc, PCI_ROM_REG, PREAD(psc, PCI_ROM_REG) & ~PCI_ROM_ENABLE);
135 
136 	/* hook up the interrupt */
137 	if (pci_intr_map(pa, &ih)) {
138 		printf(": unable to map interrupt\n");
139 		goto unmap;
140 	}
141 	intrstr = pci_intr_string(psc->psc_pc, ih);
142 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO,
143 	    mpi_intr, sc, sc->sc_dev.dv_xname);
144 	if (psc->psc_ih == NULL) {
145 		printf(": unable to map interrupt%s%s\n",
146 		    intrstr == NULL ? "" : " at ",
147 		    intrstr == NULL ? "" : intrstr);
148 		goto unmap;
149 	}
150 	printf(": %s", intrstr);
151 
152 	if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG) ==
153 	    PCI_ID_CODE(PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_1030)) {
154 		sc->sc_flags |= MPI_F_SPI;
155 #ifdef __sparc64__
156 		/*
157 		 * Walk up the Open Firmware device tree until we find a
158 		 * "scsi-initiator-id" property.
159 		 */
160 		node = PCITAG_NODE(pa->pa_tag);
161 		while (node) {
162 			if (OF_getprop(node, "scsi-initiator-id",
163 			    &sc->sc_target, sizeof(sc->sc_target)) ==
164 			    sizeof(sc->sc_target))
165 				break;
166 			node = OF_parent(node);
167 		}
168 #endif
169 	}
170 
171 	if (mpi_attach(sc) != 0) {
172 		/* error printed by mpi_attach */
173 		goto deintr;
174 	}
175 
176 	return;
177 
178 deintr:
179 	pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
180 	psc->psc_ih = NULL;
181 unmap:
182 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
183 	sc->sc_ios = 0;
184 }
185 
186 int
187 mpi_pci_detach(struct device *self, int flags)
188 {
189 	struct mpi_pci_softc		*psc = (struct mpi_pci_softc *)self;
190 	struct mpi_softc		*sc = &psc->psc_mpi;
191 
192 	mpi_detach(sc);
193 
194 	if (psc->psc_ih != NULL) {
195 		pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
196 		psc->psc_ih = NULL;
197 	}
198 	if (sc->sc_ios != 0) {
199 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
200 		sc->sc_ios = 0;
201 	}
202 
203 	return (0);
204 }
205