xref: /openbsd-src/sys/dev/pci/mpi_pci.c (revision 0f9891f1fafd8f53a63c41edb56ce51e2589b910)
1 /*	$OpenBSD: mpi_pci.c,v 1.27 2024/05/24 06:02:58 jsg 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/device.h>
23 #include <sys/sensors.h>
24 #include <sys/rwlock.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 const 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
mpi_pci_match(struct device * parent,void * match,void * aux)89 mpi_pci_match(struct device *parent, void *match, void *aux)
90 {
91 	return (pci_matchbyid(aux, mpi_devices, nitems(mpi_devices)));
92 }
93 
94 void
mpi_pci_attach(struct device * parent,struct device * self,void * aux)95 mpi_pci_attach(struct device *parent, struct device *self, void *aux)
96 {
97 	struct mpi_pci_softc		*psc = (void *)self;
98 	struct mpi_softc		*sc = &psc->psc_mpi;
99 	struct pci_attach_args		*pa = aux;
100 	pcireg_t			memtype;
101 	int				r;
102 	pci_intr_handle_t		ih;
103 	const char			*intrstr;
104 #ifdef __sparc64__
105 	int node;
106 #endif
107 
108 	psc->psc_pc = pa->pa_pc;
109 	psc->psc_tag = pa->pa_tag;
110 	psc->psc_ih = NULL;
111 	sc->sc_dmat = pa->pa_dmat;
112 	sc->sc_ios = 0;
113 	sc->sc_target = -1;
114 
115 	/* find the appropriate memory base */
116 	for (r = PCI_MAPREG_START; r < PCI_MAPREG_END; r += sizeof(memtype)) {
117 		memtype = pci_mapreg_type(psc->psc_pc, psc->psc_tag, r);
118 		if ((memtype & PCI_MAPREG_TYPE_MASK) == PCI_MAPREG_TYPE_MEM)
119 			break;
120 	}
121 	if (r >= PCI_MAPREG_END) {
122 		printf(": unable to locate system interface registers\n");
123 		return;
124 	}
125 
126 	if (pci_mapreg_map(pa, r, memtype, 0, &sc->sc_iot, &sc->sc_ioh,
127 	    NULL, &sc->sc_ios, 0) != 0) {
128 		printf(": unable to map system interface registers\n");
129 		return;
130 	}
131 
132 	/* disable the expansion rom */
133 	PWRITE(psc, PCI_ROM_REG, PREAD(psc, PCI_ROM_REG) & ~PCI_ROM_ENABLE);
134 
135 	/* hook up the interrupt */
136 	if (pci_intr_map_msi(pa, &ih) != 0 && pci_intr_map(pa, &ih) != 0) {
137 		printf(": unable to map interrupt\n");
138 		goto unmap;
139 	}
140 	intrstr = pci_intr_string(psc->psc_pc, ih);
141 	psc->psc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_BIO | IPL_MPSAFE,
142 	    mpi_intr, sc, sc->sc_dev.dv_xname);
143 	if (psc->psc_ih == NULL) {
144 		printf(": unable to map interrupt%s%s\n",
145 		    intrstr == NULL ? "" : " at ",
146 		    intrstr == NULL ? "" : intrstr);
147 		goto unmap;
148 	}
149 	printf(": %s", intrstr);
150 
151 	if (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG) ==
152 	    PCI_ID_CODE(PCI_VENDOR_SYMBIOS, PCI_PRODUCT_SYMBIOS_1030)) {
153 		sc->sc_flags |= MPI_F_SPI;
154 #ifdef __sparc64__
155 		/*
156 		 * Walk up the Open Firmware device tree until we find a
157 		 * "scsi-initiator-id" property.
158 		 */
159 		node = PCITAG_NODE(pa->pa_tag);
160 		while (node) {
161 			if (OF_getprop(node, "scsi-initiator-id",
162 			    &sc->sc_target, sizeof(sc->sc_target)) ==
163 			    sizeof(sc->sc_target))
164 				break;
165 			node = OF_parent(node);
166 		}
167 #endif
168 	}
169 
170 	if (mpi_attach(sc) != 0) {
171 		/* error printed by mpi_attach */
172 		goto deintr;
173 	}
174 
175 	return;
176 
177 deintr:
178 	pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
179 	psc->psc_ih = NULL;
180 unmap:
181 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
182 	sc->sc_ios = 0;
183 }
184 
185 int
mpi_pci_detach(struct device * self,int flags)186 mpi_pci_detach(struct device *self, int flags)
187 {
188 	struct mpi_pci_softc		*psc = (struct mpi_pci_softc *)self;
189 	struct mpi_softc		*sc = &psc->psc_mpi;
190 
191 	mpi_detach(sc);
192 
193 	if (psc->psc_ih != NULL) {
194 		pci_intr_disestablish(psc->psc_pc, psc->psc_ih);
195 		psc->psc_ih = NULL;
196 	}
197 	if (sc->sc_ios != 0) {
198 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
199 		sc->sc_ios = 0;
200 	}
201 
202 	return (0);
203 }
204