xref: /openbsd-src/sys/dev/pci/mpi_pci.c (revision a559dc66eeae0e00e260c9139d7dd1cc077def4e)
1 /*	$OpenBSD: mpi_pci.c,v 1.22 2008/10/28 13:44:33 marco 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 "bio.h"
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 #include <sys/sensors.h>
28 #include <sys/rwlock.h>
29 
30 #include <machine/bus.h>
31 
32 #include <dev/pci/pcireg.h>
33 #include <dev/pci/pcivar.h>
34 #include <dev/pci/pcidevs.h>
35 
36 #ifdef __sparc64__
37 #include <dev/ofw/openfirm.h>
38 #endif
39 
40 #include <scsi/scsi_all.h>
41 #include <scsi/scsiconf.h>
42 
43 #include <dev/ic/mpireg.h>
44 #include <dev/ic/mpivar.h>
45 
46 int	mpi_pci_match(struct device *, void *, void *);
47 void	mpi_pci_attach(struct device *, struct device *, void *);
48 int	mpi_pci_detach(struct device *, int);
49 
50 struct mpi_pci_softc {
51 	struct mpi_softc	psc_mpi;
52 
53 	pci_chipset_tag_t	psc_pc;
54 	pcitag_t		psc_tag;
55 
56 	void			*psc_ih;
57 };
58 
59 struct cfattach mpi_pci_ca = {
60 	sizeof(struct mpi_pci_softc), mpi_pci_match, mpi_pci_attach,
61 	mpi_pci_detach
62 };
63 
64 #define PREAD(s, r)	pci_conf_read((s)->psc_pc, (s)->psc_tag, (r))
65 #define PWRITE(s, r, v)	pci_conf_write((s)->psc_pc, (s)->psc_tag, (r), (v))
66 
67 static const struct pci_matchid mpi_devices[] = {
68 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_1030 },
69 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909 },
70 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909A },
71 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919 },
72 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919_1 },
73 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919X },
74 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929 },
75 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929_1 },
76 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929X },
77 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC939X },
78 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC949E },
79 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC949X },
80 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064 },
81 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064A },
82 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064E_2 },
83 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1064E },
84 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1066 },
85 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1066E },
86 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068 },
87 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068_2 },
88 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068E },
89 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_SAS1068E_2 }
90 };
91 
92 int
93 mpi_pci_match(struct device *parent, void *match, void *aux)
94 {
95 	return (pci_matchbyid((struct pci_attach_args *)aux, mpi_devices,
96 	    sizeof(mpi_devices) / sizeof(mpi_devices[0])));
97 }
98 
99 void
100 mpi_pci_attach(struct device *parent, struct device *self, void *aux)
101 {
102 	struct mpi_pci_softc		*psc = (void *)self;
103 	struct mpi_softc		*sc = &psc->psc_mpi;
104 	struct pci_attach_args		*pa = aux;
105 	pcireg_t			memtype;
106 	int				r;
107 	pci_intr_handle_t		ih;
108 	const char			*intrstr;
109 #ifdef __sparc64__
110 	int node;
111 #endif
112 
113 	psc->psc_pc = pa->pa_pc;
114 	psc->psc_tag = pa->pa_tag;
115 	psc->psc_ih = NULL;
116 	sc->sc_dmat = pa->pa_dmat;
117 	sc->sc_ios = 0;
118 	sc->sc_target = -1;
119 
120 	/* find the appropriate memory base */
121 	for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
122 		memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, r);
123 		if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM)
124 			break;
125 	}
126 	if (r >= PCI_MAPREG_END) {
127 		printf(": unable to locate system interface registers\n");
128 		return;
129 	}
130 
131 	if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
132 	    NULL, &sc->sc_ios, 0) != 0) {
133 		printf(": unable to map system interface registers\n");
134 		return;
135 	}
136 
137 	/* disable the expansion rom */
138 	PWRITE(psc, PCI_ROM_REG, PREAD(psc, PCI_ROM_REG) & ~PCI_ROM_ENABLE);
139 
140 	/* hook up the interrupt */
141 	if (pci_intr_map(pa, &ih)) {
142 		printf(": unable to map interrupt\n");
143 		goto unmap;
144 	}
145 	intrstr = pci_intr_string(psc->psc_pc, ih);
146 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO,
147 	    mpi_intr, sc, sc->sc_dev.dv_xname);
148 	if (psc->psc_ih == NULL) {
149 		printf(": unable to map interrupt%s%s\n",
150 		    intrstr == NULL ? "" : " at ",
151 		    intrstr == NULL ? "" : intrstr);
152 		goto unmap;
153 	}
154 	printf(": %s", intrstr);
155 
156 	if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG) ==
157 	    PCI_ID_CODE(PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_1030)) {
158 		sc->sc_flags |= MPI_F_SPI;
159 #ifdef __sparc64__
160 		/*
161 		 * Walk up the Open Firmware device tree until we find a
162 		 * "scsi-initiator-id" property.
163 		 */
164 		node = PCITAG_NODE(pa->pa_tag);
165 		while (node) {
166 			if (OF_getprop(node, "scsi-initiator-id",
167 			    &sc->sc_target, sizeof(sc->sc_target)) ==
168 			    sizeof(sc->sc_target))
169 				break;
170 			node = OF_parent(node);
171 		}
172 #endif
173 	}
174 
175 	if (mpi_attach(sc) != 0) {
176 		/* error printed by mpi_attach */
177 		goto deintr;
178 	}
179 
180 	return;
181 
182 deintr:
183 	pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
184 	psc->psc_ih = NULL;
185 unmap:
186 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
187 	sc->sc_ios = 0;
188 }
189 
190 int
191 mpi_pci_detach(struct device *self, int flags)
192 {
193 	struct mpi_pci_softc		*psc = (struct mpi_pci_softc *)self;
194 	struct mpi_softc		*sc = &psc->psc_mpi;
195 
196 	mpi_detach(sc);
197 
198 	if (psc->psc_ih != NULL) {
199 		pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
200 		psc->psc_ih = NULL;
201 	}
202 	if (sc->sc_ios != 0) {
203 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
204 		sc->sc_ios = 0;
205 	}
206 
207 	return (0);
208 }
209