xref: /netbsd-src/sys/dev/pci/mpt_pci.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: mpt_pci.c,v 1.1 2003/04/16 22:03:01 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * mpt_pci.c:
40  *
41  * NetBSD PCI-specific routines for LSI Fusion adapters.
42  */
43 
44 #include <dev/ic/mpt.h>			/* pulls in all headers */
45 
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 #include <dev/pci/pcidevs.h>
49 
50 #define	MPT_PCI_MMBA		(PCI_MAPREG_START+0x04)
51 
52 struct mpt_pci_softc {
53 	mpt_softc_t sc_mpt;
54 
55 	pci_chipset_tag_t sc_pc;
56 	pcitag_t sc_tag;
57 
58 	void *sc_ih;
59 
60 	/* Saved volatile PCI configuration registers. */
61 	pcireg_t sc_pci_csr;
62 	pcireg_t sc_pci_bhlc;
63 	pcireg_t sc_pci_io_bar;
64 	pcireg_t sc_pci_mem0_bar[2];
65 	pcireg_t sc_pci_mem1_bar[2];
66 	pcireg_t sc_pci_rom_bar;
67 	pcireg_t sc_pci_int;
68 	pcireg_t sc_pci_pmcsr;
69 };
70 
71 static void	mpt_pci_link_peer(mpt_softc_t *);
72 static void	mpt_pci_read_config_regs(mpt_softc_t *);
73 static void	mpt_pci_set_config_regs(mpt_softc_t *);
74 
75 #define	MPP_F_FC	0x01	/* Fibre Channel adapter */
76 #define	MPP_F_DUAL	0x02	/* Dual port adapter */
77 
78 static const struct mpt_pci_product {
79 	pci_vendor_id_t		mpp_vendor;
80 	pci_product_id_t	mpp_product;
81 	int			mpp_flags;
82 	const char		*mpp_name;
83 } mpt_pci_products[] = {
84 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_1030,
85 	  MPP_F_DUAL,
86 	  "LSI Logic 53c1030 Ultra320 SCSI" },
87 
88 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909,
89 	  MPP_F_FC,
90 	  "LSI Logic FC909 FC Adapter" },
91 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909A,
92 	  MPP_F_FC,
93 	  "LSI Logic FC909A FC Adapter" },
94 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929,
95 	  MPP_F_FC | MPP_F_DUAL,
96 	  "LSI Logic FC929 FC Adapter" },
97 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929_1,
98 	  MPP_F_FC | MPP_F_DUAL,
99 	  "LSI Logic FC929 FC Adapter" },
100 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919,
101 	  MPP_F_FC,
102 	  "LSI Logic FC919 FC Adapter" },
103 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919_1,
104 	  MPP_F_FC,
105 	  "LSI Logic FC919 FC Adapter" },
106 
107 	{ 0,			0,
108 	  0,
109 	  NULL },
110 };
111 
112 static const struct mpt_pci_product *
113 mpt_pci_lookup(const struct pci_attach_args *pa)
114 {
115 	const struct mpt_pci_product *mpp;
116 
117 	for (mpp = mpt_pci_products; mpp->mpp_name != NULL; mpp++) {
118 		if (PCI_VENDOR(pa->pa_id) == mpp->mpp_vendor &&
119 		    PCI_PRODUCT(pa->pa_id) == mpp->mpp_product)
120 			return (mpp);
121 	}
122 	return (NULL);
123 }
124 
125 static int
126 mpt_pci_match(struct device *parent, struct cfdata *cf, void *aux)
127 {
128 	struct pci_attach_args *pa = aux;
129 
130 	if (mpt_pci_lookup(pa) != NULL)
131 		return (1);
132 
133 	return (0);
134 }
135 
136 static void
137 mpt_pci_attach(struct device *parent, struct device *self, void *aux)
138 {
139 	struct mpt_pci_softc *psc = (void *) self;
140 	mpt_softc_t *mpt = &psc->sc_mpt;
141 	struct pci_attach_args *pa = aux;
142 	const struct mpt_pci_product *mpp;
143 	pci_intr_handle_t ih;
144 	const char *intrstr;
145 	pcireg_t reg, memtype;
146 	bus_space_tag_t memt;
147 	bus_space_handle_t memh;
148 	int memh_valid;
149 
150 	mpp = mpt_pci_lookup(pa);
151 	if (mpp == NULL) {
152 		printf("\n");
153 		panic("mpt_pci_attach");
154 	}
155 
156 	if (mpp->mpp_flags & MPP_F_FC) {
157 		mpt->is_fc = 1;
158 		aprint_naive(": Fibre Channel controller\n");
159 	} else
160 		aprint_naive(": SCSI controller\n");
161 	aprint_normal(": %s\n", mpp->mpp_name);
162 
163 	psc->sc_pc = pa->pa_pc;
164 	psc->sc_tag = pa->pa_tag;
165 
166 	mpt->sc_dmat = pa->pa_dmat;
167 	mpt->sc_set_config_regs = mpt_pci_set_config_regs;
168 
169 	/*
170 	 * Map the device.
171 	 */
172 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MPT_PCI_MMBA);
173 	switch (memtype) {
174 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
175 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
176 		memh_valid = (pci_mapreg_map(pa, MPT_PCI_MMBA,
177 		    memtype, 0, &memt, &memh, NULL, NULL) == 0);
178 		break;
179 
180 	default:
181 		memh_valid = 0;
182 	}
183 
184 	if (memh_valid) {
185 		mpt->sc_st = memt;
186 		mpt->sc_sh = memh;
187 	} else {
188 		aprint_error("%s: unable to map device registers\n",
189 		    mpt->sc_dev.dv_xname);
190 		return;
191 	}
192 
193 	/*
194 	 * Make sure the PCI command register is properly configured.
195 	 */
196 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
197 	reg |= PCI_COMMAND_MASTER_ENABLE;
198 	/* XXX PCI_COMMAND_INVALIDATE_ENABLE */
199 	/* XXX PCI_COMMAND_PARITY_ENABLE */
200 	/* XXX PCI_COMMAND_SERR_ENABLE */
201 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
202 
203 	/*
204 	 * Ensure that the ROM is diabled.
205 	 */
206 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
207 	reg &= ~1;
208 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, reg);
209 
210 	/*
211 	 * Map and establish our interrupt.
212 	 */
213 	if (pci_intr_map(pa, &ih) != 0) {
214 		aprint_error("%s: unable to map interrupt\n",
215 		    mpt->sc_dev.dv_xname);
216 		return;
217 	}
218 	intrstr = pci_intr_string(pa->pa_pc, ih);
219 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mpt_intr, mpt);
220 	if (psc->sc_ih == NULL) {
221 		aprint_error("%s: unable to establish interrupt",
222 		    mpt->sc_dev.dv_xname);
223 		if (intrstr != NULL)
224 			aprint_normal(" at %s", intrstr);
225 		aprint_normal("\n");
226 		return;
227 	}
228 	aprint_normal("%s: interrupting at %s\n", mpt->sc_dev.dv_xname,
229 	    intrstr);
230 
231 	/* Disable interrupts on the part. */
232 	mpt_disable_ints(mpt);
233 
234 	/* Allocate DMA memory. */
235 	if (mpt_dma_mem_alloc(mpt) != 0) {
236 		aprint_error("%s: unable to allocate DMA memory\n",
237 		    mpt->sc_dev.dv_xname);
238 		return;
239 	}
240 
241 	/*
242 	 * Save the PCI config register values.
243 	 *
244 	 * Hard resets are known to screw up the BAR for diagnostic
245 	 * memory accesses (Mem1).
246 	 *
247 	 * Using Mem1 is know to make the chip stop responding to
248 	 * configuration cycles, so we need to save it now.
249 	 */
250 	mpt_pci_read_config_regs(mpt);
251 
252 	/*
253 	 * If we're a dual-port adapter, try to find our peer.  We
254 	 * need to fix his PCI config registers, too.
255 	 */
256 	if (mpp->mpp_flags & MPP_F_DUAL)
257 		mpt_pci_link_peer(mpt);
258 
259 	/* Initialize the hardware. */
260 	if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) {
261 		/* Error message already printed. */
262 		return;
263 	}
264 
265 	/* Attach to scsipi. */
266 	mpt_scsipi_attach(mpt);
267 }
268 
269 CFATTACH_DECL(mpt_pci, sizeof(struct mpt_pci_softc),
270     mpt_pci_match, mpt_pci_attach, NULL, NULL);
271 
272 /*
273  * Find and remember our peer PCI function on a dual-port device.
274  */
275 static void
276 mpt_pci_link_peer(mpt_softc_t *mpt)
277 {
278 	extern struct cfdriver mpt_cd;
279 
280 	struct mpt_pci_softc *peer_psc, *psc = (void *) mpt;
281 	struct device *dev;
282 	int unit, b, d, f, peer_b, peer_d, peer_f;
283 
284 	pci_decompose_tag(psc->sc_pc, psc->sc_tag, &b, &d, &f);
285 
286 	for (unit = 0; unit < mpt_cd.cd_ndevs; unit++) {
287 		if (unit == mpt->sc_dev.dv_unit)
288 			continue;
289 		dev = device_lookup(&mpt_cd, unit);
290 		if (dev == NULL)
291 			continue;
292 		if (dev->dv_cfattach != &mpt_pci_ca)
293 			continue;
294 		peer_psc = (void *) dev;
295 		if (peer_psc->sc_pc != psc->sc_pc)
296 			continue;
297 		pci_decompose_tag(peer_psc->sc_pc, peer_psc->sc_tag,
298 		    &peer_b, &peer_d, &peer_f);
299 		if (peer_b == b && peer_d == d) {
300 			if (mpt->verbose)
301 				mpt_prt(mpt, "linking with peer: %s",
302 				    peer_psc->sc_mpt.sc_dev.dv_xname);
303 			mpt->mpt2 = (mpt_softc_t *) peer_psc;
304 			peer_psc->sc_mpt.mpt2 = mpt;
305 			return;
306 		}
307 	}
308 }
309 
310 /*
311  * Save the volatile PCI configuration registers.
312  */
313 static void
314 mpt_pci_read_config_regs(mpt_softc_t *mpt)
315 {
316 	struct mpt_pci_softc *psc = (void *) mpt;
317 
318 	psc->sc_pci_csr = pci_conf_read(psc->sc_pc, psc->sc_tag,
319 	    PCI_COMMAND_STATUS_REG);
320 	psc->sc_pci_bhlc = pci_conf_read(psc->sc_pc, psc->sc_tag,
321 	    PCI_BHLC_REG);
322 	psc->sc_pci_io_bar = pci_conf_read(psc->sc_pc, psc->sc_tag,
323 	    PCI_MAPREG_START);
324 	psc->sc_pci_mem0_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag,
325 	    PCI_MAPREG_START+0x04);
326 	psc->sc_pci_mem0_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag,
327 	    PCI_MAPREG_START+0x08);
328 	psc->sc_pci_mem1_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag,
329 	    PCI_MAPREG_START+0x0c);
330 	psc->sc_pci_mem1_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag,
331 	    PCI_MAPREG_START+0x10);
332 	psc->sc_pci_rom_bar = pci_conf_read(psc->sc_pc, psc->sc_tag,
333 	    PCI_MAPREG_ROM);
334 	psc->sc_pci_int = pci_conf_read(psc->sc_pc, psc->sc_tag,
335 	    PCI_INTERRUPT_REG);
336 	psc->sc_pci_pmcsr = pci_conf_read(psc->sc_pc, psc->sc_tag, 0x44);
337 }
338 
339 /*
340  * Restore the volatile PCI configuration registers.
341  */
342 static void
343 mpt_pci_set_config_regs(mpt_softc_t *mpt)
344 {
345 	struct mpt_pci_softc *psc = (void *) mpt;
346 
347 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_COMMAND_STATUS_REG,
348 	    psc->sc_pci_csr);
349 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_BHLC_REG,
350 	    psc->sc_pci_bhlc);
351 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START,
352 	    psc->sc_pci_io_bar);
353 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x04,
354 	    psc->sc_pci_mem0_bar[0]);
355 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x08,
356 	    psc->sc_pci_mem0_bar[1]);
357 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x0c,
358 	    psc->sc_pci_mem1_bar[0]);
359 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x10,
360 	    psc->sc_pci_mem1_bar[1]);
361 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_ROM,
362 	    psc->sc_pci_rom_bar);
363 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_INTERRUPT_REG,
364 	    psc->sc_pci_int);
365 	pci_conf_write(psc->sc_pc, psc->sc_tag, 0x44, psc->sc_pci_pmcsr);
366 }
367