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