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