xref: /openbsd-src/sys/dev/pci/mpi_pci.c (revision 5a64e4d9535177cedbd6b0c8006497fb1241efa7)
1 /*	$OpenBSD: mpi_pci.c,v 1.19 2008/05/26 12:48:28 kettenis 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 #ifdef __sparc64__
117 	/*
118 	 * Walk up the Open Firmware device tree until we find a
119 	 * "scsi-initiator-id" property.
120 	 */
121 	node = PCITAG_NODE(pa->pa_tag);
122 	while (node) {
123 		if (OF_getprop(node, "scsi-initiator-id",  &sc->sc_target,
124 		    sizeof(sc->sc_target)) == sizeof(sc->sc_target))
125 			break;
126 
127 		node = OF_parent(node);
128 	}
129 #endif
130 
131 	/* find the appropriate memory base */
132 	for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
133 		memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, r);
134 		if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM)
135 			break;
136 	}
137 	if (r >= PCI_MAPREG_END) {
138 		printf(": unable to locate system interface registers\n");
139 		return;
140 	}
141 
142 	if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
143 	    NULL, &sc->sc_ios, 0) != 0) {
144 		printf(": unable to map system interface registers\n");
145 		return;
146 	}
147 
148 	/* disable the expansion rom */
149 	PWRITE(psc, PCI_ROM_REG, PREAD(psc, PCI_ROM_REG) & ~PCI_ROM_ENABLE);
150 
151 	/* hook up the interrupt */
152 	if (pci_intr_map(pa, &ih)) {
153 		printf(": unable to map interrupt\n");
154 		goto unmap;
155 	}
156 	intrstr = pci_intr_string(psc->psc_pc, ih);
157 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO,
158 	    mpi_intr, sc, sc->sc_dev.dv_xname);
159 	if (psc->psc_ih == NULL) {
160 		printf(": unable to map interrupt%s%s\n",
161 		    intrstr == NULL ? "" : " at ",
162 		    intrstr == NULL ? "" : intrstr);
163 		goto unmap;
164 	}
165 	printf(": %s", intrstr);
166 
167 	if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG) ==
168 	    PCI_ID_CODE(PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_1030))
169 		sc->sc_flags |= MPI_F_SPI;
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