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