xref: /netbsd-src/sys/dev/pci/mpt_pci.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: mpt_pci.c,v 1.8 2006/03/29 04:32:09 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 <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: mpt_pci.c,v 1.8 2006/03/29 04:32:09 thorpej Exp $");
46 
47 #include <dev/ic/mpt.h>			/* pulls in all headers */
48 
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcidevs.h>
52 
53 #define	MPT_PCI_MMBA		(PCI_MAPREG_START+0x04)
54 
55 struct mpt_pci_softc {
56 	mpt_softc_t sc_mpt;
57 
58 	pci_chipset_tag_t sc_pc;
59 	pcitag_t sc_tag;
60 
61 	void *sc_ih;
62 
63 	/* Saved volatile PCI configuration registers. */
64 	pcireg_t sc_pci_csr;
65 	pcireg_t sc_pci_bhlc;
66 	pcireg_t sc_pci_io_bar;
67 	pcireg_t sc_pci_mem0_bar[2];
68 	pcireg_t sc_pci_mem1_bar[2];
69 	pcireg_t sc_pci_rom_bar;
70 	pcireg_t sc_pci_int;
71 	pcireg_t sc_pci_pmcsr;
72 };
73 
74 static void	mpt_pci_link_peer(mpt_softc_t *);
75 static void	mpt_pci_read_config_regs(mpt_softc_t *);
76 static void	mpt_pci_set_config_regs(mpt_softc_t *);
77 
78 #define	MPP_F_FC	0x01	/* Fibre Channel adapter */
79 #define	MPP_F_DUAL	0x02	/* Dual port adapter */
80 
81 static const struct mpt_pci_product {
82 	pci_vendor_id_t		mpp_vendor;
83 	pci_product_id_t	mpp_product;
84 	int			mpp_flags;
85 	const char		*mpp_name;
86 } mpt_pci_products[] = {
87 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_1030,
88 	  MPP_F_DUAL,
89 	  "LSI Logic 53c1030 Ultra320 SCSI" },
90 
91 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909,
92 	  MPP_F_FC,
93 	  "LSI Logic FC909 FC Adapter" },
94 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC909A,
95 	  MPP_F_FC,
96 	  "LSI Logic FC909A FC Adapter" },
97 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929,
98 	  MPP_F_FC | MPP_F_DUAL,
99 	  "LSI Logic FC929 FC Adapter" },
100 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC929_1,
101 	  MPP_F_FC | MPP_F_DUAL,
102 	  "LSI Logic FC929 FC Adapter" },
103 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919,
104 	  MPP_F_FC,
105 	  "LSI Logic FC919 FC Adapter" },
106 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919_1,
107 	  MPP_F_FC,
108 	  "LSI Logic FC919 FC Adapter" },
109 	{ PCI_VENDOR_SYMBIOS,   PCI_PRODUCT_SYMBIOS_FC929X,
110 	  MPP_F_FC | MPP_F_DUAL,
111 	  "LSI Logic FC929X FC Adapter" },
112 	{ PCI_VENDOR_SYMBIOS,	PCI_PRODUCT_SYMBIOS_FC919X,
113 	  MPP_F_FC,
114 	  "LSI Logic FC919X FC Adapter" },
115 
116 	{ 0,			0,
117 	  0,
118 	  NULL },
119 };
120 
121 static const struct mpt_pci_product *
122 mpt_pci_lookup(const struct pci_attach_args *pa)
123 {
124 	const struct mpt_pci_product *mpp;
125 
126 	for (mpp = mpt_pci_products; mpp->mpp_name != NULL; mpp++) {
127 		if (PCI_VENDOR(pa->pa_id) == mpp->mpp_vendor &&
128 		    PCI_PRODUCT(pa->pa_id) == mpp->mpp_product)
129 			return (mpp);
130 	}
131 	return (NULL);
132 }
133 
134 static int
135 mpt_pci_match(struct device *parent, struct cfdata *cf, void *aux)
136 {
137 	struct pci_attach_args *pa = aux;
138 
139 	if (mpt_pci_lookup(pa) != NULL)
140 		return (1);
141 
142 	return (0);
143 }
144 
145 static void
146 mpt_pci_attach(struct device *parent, struct device *self, void *aux)
147 {
148 	struct mpt_pci_softc *psc = (void *) self;
149 	mpt_softc_t *mpt = &psc->sc_mpt;
150 	struct pci_attach_args *pa = aux;
151 	const struct mpt_pci_product *mpp;
152 	pci_intr_handle_t ih;
153 	const char *intrstr;
154 	pcireg_t reg, memtype;
155 	bus_space_tag_t memt;
156 	bus_space_handle_t memh;
157 	int memh_valid;
158 
159 	mpp = mpt_pci_lookup(pa);
160 	if (mpp == NULL) {
161 		printf("\n");
162 		panic("mpt_pci_attach");
163 	}
164 
165 	if (mpp->mpp_flags & MPP_F_FC) {
166 		mpt->is_fc = 1;
167 		aprint_naive(": Fibre Channel controller\n");
168 	} else
169 		aprint_naive(": SCSI controller\n");
170 	aprint_normal(": %s\n", mpp->mpp_name);
171 
172 	psc->sc_pc = pa->pa_pc;
173 	psc->sc_tag = pa->pa_tag;
174 
175 	mpt->sc_dmat = pa->pa_dmat;
176 	mpt->sc_set_config_regs = mpt_pci_set_config_regs;
177 
178 	/*
179 	 * Map the device.
180 	 */
181 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MPT_PCI_MMBA);
182 	switch (memtype) {
183 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
184 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
185 		memh_valid = (pci_mapreg_map(pa, MPT_PCI_MMBA,
186 		    memtype, 0, &memt, &memh, NULL, NULL) == 0);
187 		break;
188 
189 	default:
190 		memh_valid = 0;
191 	}
192 
193 	if (memh_valid) {
194 		mpt->sc_st = memt;
195 		mpt->sc_sh = memh;
196 	} else {
197 		aprint_error("%s: unable to map device registers\n",
198 		    mpt->sc_dev.dv_xname);
199 		return;
200 	}
201 
202 	/*
203 	 * Make sure the PCI command register is properly configured.
204 	 */
205 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
206 	reg |= PCI_COMMAND_MASTER_ENABLE;
207 	/* XXX PCI_COMMAND_INVALIDATE_ENABLE */
208 	/* XXX PCI_COMMAND_PARITY_ENABLE */
209 	/* XXX PCI_COMMAND_SERR_ENABLE */
210 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
211 
212 	/*
213 	 * Ensure that the ROM is diabled.
214 	 */
215 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM);
216 	reg &= ~1;
217 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_MAPREG_ROM, reg);
218 
219 	/*
220 	 * Map and establish our interrupt.
221 	 */
222 	if (pci_intr_map(pa, &ih) != 0) {
223 		aprint_error("%s: unable to map interrupt\n",
224 		    mpt->sc_dev.dv_xname);
225 		return;
226 	}
227 	intrstr = pci_intr_string(pa->pa_pc, ih);
228 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, mpt_intr, mpt);
229 	if (psc->sc_ih == NULL) {
230 		aprint_error("%s: unable to establish interrupt",
231 		    mpt->sc_dev.dv_xname);
232 		if (intrstr != NULL)
233 			aprint_normal(" at %s", intrstr);
234 		aprint_normal("\n");
235 		return;
236 	}
237 	aprint_normal("%s: interrupting at %s\n", mpt->sc_dev.dv_xname,
238 	    intrstr);
239 
240 	/* Disable interrupts on the part. */
241 	mpt_disable_ints(mpt);
242 
243 	/* Allocate DMA memory. */
244 	if (mpt_dma_mem_alloc(mpt) != 0) {
245 		aprint_error("%s: unable to allocate DMA memory\n",
246 		    mpt->sc_dev.dv_xname);
247 		return;
248 	}
249 
250 	/*
251 	 * Save the PCI config register values.
252 	 *
253 	 * Hard resets are known to screw up the BAR for diagnostic
254 	 * memory accesses (Mem1).
255 	 *
256 	 * Using Mem1 is know to make the chip stop responding to
257 	 * configuration cycles, so we need to save it now.
258 	 */
259 	mpt_pci_read_config_regs(mpt);
260 
261 	/*
262 	 * If we're a dual-port adapter, try to find our peer.  We
263 	 * need to fix his PCI config registers, too.
264 	 */
265 	if (mpp->mpp_flags & MPP_F_DUAL)
266 		mpt_pci_link_peer(mpt);
267 
268 	/* Initialize the hardware. */
269 	if (mpt_init(mpt, MPT_DB_INIT_HOST) != 0) {
270 		/* Error message already printed. */
271 		return;
272 	}
273 
274 	/* Attach to scsipi. */
275 	mpt_scsipi_attach(mpt);
276 }
277 
278 CFATTACH_DECL(mpt_pci, sizeof(struct mpt_pci_softc),
279     mpt_pci_match, mpt_pci_attach, NULL, NULL);
280 
281 /*
282  * Find and remember our peer PCI function on a dual-port device.
283  */
284 static void
285 mpt_pci_link_peer(mpt_softc_t *mpt)
286 {
287 	extern struct cfdriver mpt_cd;
288 
289 	struct mpt_pci_softc *peer_psc, *psc = (void *) mpt;
290 	struct device *dev;
291 	int unit, b, d, f, peer_b, peer_d, peer_f;
292 
293 	pci_decompose_tag(psc->sc_pc, psc->sc_tag, &b, &d, &f);
294 
295 	for (unit = 0; unit < mpt_cd.cd_ndevs; unit++) {
296 		if (unit == device_unit(&mpt->sc_dev))
297 			continue;
298 		dev = device_lookup(&mpt_cd, unit);
299 		if (dev == NULL)
300 			continue;
301 		if (device_parent(&mpt->sc_dev) != device_parent(dev))
302 			continue;
303 		/*
304 		 * If we have the same parent, we know the other one
305 		 * is attached with mpt_pci.
306 		 */
307 		peer_psc = (void *) dev;
308 		if (peer_psc->sc_pc != psc->sc_pc)
309 			continue;
310 		pci_decompose_tag(peer_psc->sc_pc, peer_psc->sc_tag,
311 		    &peer_b, &peer_d, &peer_f);
312 		if (peer_b == b && peer_d == d) {
313 			if (mpt->verbose)
314 				mpt_prt(mpt, "linking with peer: %s",
315 				    peer_psc->sc_mpt.sc_dev.dv_xname);
316 			mpt->mpt2 = (mpt_softc_t *) peer_psc;
317 			peer_psc->sc_mpt.mpt2 = mpt;
318 			return;
319 		}
320 	}
321 }
322 
323 /*
324  * Save the volatile PCI configuration registers.
325  */
326 static void
327 mpt_pci_read_config_regs(mpt_softc_t *mpt)
328 {
329 	struct mpt_pci_softc *psc = (void *) mpt;
330 
331 	psc->sc_pci_csr = pci_conf_read(psc->sc_pc, psc->sc_tag,
332 	    PCI_COMMAND_STATUS_REG);
333 	psc->sc_pci_bhlc = pci_conf_read(psc->sc_pc, psc->sc_tag,
334 	    PCI_BHLC_REG);
335 	psc->sc_pci_io_bar = pci_conf_read(psc->sc_pc, psc->sc_tag,
336 	    PCI_MAPREG_START);
337 	psc->sc_pci_mem0_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag,
338 	    PCI_MAPREG_START+0x04);
339 	psc->sc_pci_mem0_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag,
340 	    PCI_MAPREG_START+0x08);
341 	psc->sc_pci_mem1_bar[0] = pci_conf_read(psc->sc_pc, psc->sc_tag,
342 	    PCI_MAPREG_START+0x0c);
343 	psc->sc_pci_mem1_bar[1] = pci_conf_read(psc->sc_pc, psc->sc_tag,
344 	    PCI_MAPREG_START+0x10);
345 	psc->sc_pci_rom_bar = pci_conf_read(psc->sc_pc, psc->sc_tag,
346 	    PCI_MAPREG_ROM);
347 	psc->sc_pci_int = pci_conf_read(psc->sc_pc, psc->sc_tag,
348 	    PCI_INTERRUPT_REG);
349 	psc->sc_pci_pmcsr = pci_conf_read(psc->sc_pc, psc->sc_tag, 0x44);
350 }
351 
352 /*
353  * Restore the volatile PCI configuration registers.
354  */
355 static void
356 mpt_pci_set_config_regs(mpt_softc_t *mpt)
357 {
358 	struct mpt_pci_softc *psc = (void *) mpt;
359 
360 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_COMMAND_STATUS_REG,
361 	    psc->sc_pci_csr);
362 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_BHLC_REG,
363 	    psc->sc_pci_bhlc);
364 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START,
365 	    psc->sc_pci_io_bar);
366 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x04,
367 	    psc->sc_pci_mem0_bar[0]);
368 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x08,
369 	    psc->sc_pci_mem0_bar[1]);
370 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x0c,
371 	    psc->sc_pci_mem1_bar[0]);
372 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_START+0x10,
373 	    psc->sc_pci_mem1_bar[1]);
374 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_MAPREG_ROM,
375 	    psc->sc_pci_rom_bar);
376 	pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_INTERRUPT_REG,
377 	    psc->sc_pci_int);
378 	pci_conf_write(psc->sc_pc, psc->sc_tag, 0x44, psc->sc_pci_pmcsr);
379 }
380