xref: /freebsd/sys/dev/mpt/mpt_pci.c (revision 280388afe52b813e39d20a7bb6605a02f21d3515)
1aad970f1SDavid E. O'Brien /*-
2eae4a35fSMatt Jacob  * PCI specific probe and attach routines for LSI Fusion Adapters
39b631363SMatt Jacob  * FreeBSD Version.
49b631363SMatt Jacob  *
59b631363SMatt Jacob  * Copyright (c) 2000, 2001 by Greg Ansley
69b631363SMatt Jacob  * Partially derived from Matt Jacob's ISP driver.
7aad970f1SDavid E. O'Brien  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 by Matthew Jacob
8aad970f1SDavid E. O'Brien  * Feral Software
9aad970f1SDavid E. O'Brien  * All rights reserved.
109b631363SMatt Jacob  *
119b631363SMatt Jacob  * Redistribution and use in source and binary forms, with or without
129b631363SMatt Jacob  * modification, are permitted provided that the following conditions
139b631363SMatt Jacob  * are met:
149b631363SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
159b631363SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
169b631363SMatt Jacob  *    this list of conditions, and the following disclaimer.
179b631363SMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
189b631363SMatt Jacob  *    derived from this software without specific prior written permission.
199b631363SMatt Jacob  *
209b631363SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
219b631363SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b631363SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b631363SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
249b631363SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b631363SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b631363SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b631363SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b631363SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b631363SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b631363SMatt Jacob  * SUCH DAMAGE.
319b631363SMatt Jacob  */
320b80d21bSMatt Jacob /*-
330b80d21bSMatt Jacob  * Copyright (c) 2002, 2006 by Matthew Jacob
340b80d21bSMatt Jacob  * All rights reserved.
350b80d21bSMatt Jacob  *
360b80d21bSMatt Jacob  * Redistribution and use in source and binary forms, with or without
370b80d21bSMatt Jacob  * modification, are permitted provided that the following conditions are
380b80d21bSMatt Jacob  * met:
390b80d21bSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
400b80d21bSMatt Jacob  *    notice, this list of conditions and the following disclaimer.
410b80d21bSMatt Jacob  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
420b80d21bSMatt Jacob  *    substantially similar to the "NO WARRANTY" disclaimer below
430b80d21bSMatt Jacob  *    ("Disclaimer") and any redistribution must be conditioned upon including
440b80d21bSMatt Jacob  *    a substantially similar Disclaimer requirement for further binary
450b80d21bSMatt Jacob  *    redistribution.
460b80d21bSMatt Jacob  * 3. Neither the names of the above listed copyright holders nor the names
470b80d21bSMatt Jacob  *    of any contributors may be used to endorse or promote products derived
480b80d21bSMatt Jacob  *    from this software without specific prior written permission.
490b80d21bSMatt Jacob  *
500b80d21bSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
510b80d21bSMatt Jacob  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
520b80d21bSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
530b80d21bSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
540b80d21bSMatt Jacob  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
550b80d21bSMatt Jacob  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
560b80d21bSMatt Jacob  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
570b80d21bSMatt Jacob  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
580b80d21bSMatt Jacob  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
590b80d21bSMatt Jacob  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
600b80d21bSMatt Jacob  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
610b80d21bSMatt Jacob  *
620b80d21bSMatt Jacob  * Support from Chris Ellsworth in order to make SAS adapters work
630b80d21bSMatt Jacob  * is gratefully acknowledged.
64ec5fe39dSMatt Jacob  *
65ec5fe39dSMatt Jacob  * Support from LSI-Logic has also gone a great deal toward making this a
66ec5fe39dSMatt Jacob  * workable subsystem and is gratefully acknowledged.
670b80d21bSMatt Jacob  */
68b0a2fdeeSScott Long /*
69b0a2fdeeSScott Long  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
70b0a2fdeeSScott Long  * Copyright (c) 2005, WHEEL Sp. z o.o.
71b0a2fdeeSScott Long  * Copyright (c) 2004, 2005 Justin T. Gibbs
72b0a2fdeeSScott Long  * All rights reserved.
73b0a2fdeeSScott Long  *
74b0a2fdeeSScott Long  * Redistribution and use in source and binary forms, with or without
75b0a2fdeeSScott Long  * modification, are permitted provided that the following conditions are
76b0a2fdeeSScott Long  * met:
77b0a2fdeeSScott Long  * 1. Redistributions of source code must retain the above copyright
78b0a2fdeeSScott Long  *    notice, this list of conditions and the following disclaimer.
79b0a2fdeeSScott Long  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
80b0a2fdeeSScott Long  *    substantially similar to the "NO WARRANTY" disclaimer below
81b0a2fdeeSScott Long  *    ("Disclaimer") and any redistribution must be conditioned upon including
82b0a2fdeeSScott Long  *    a substantially similar Disclaimer requirement for further binary
83b0a2fdeeSScott Long  *    redistribution.
84286e947fSJustin T. Gibbs  * 3. Neither the names of the above listed copyright holders nor the names
85286e947fSJustin T. Gibbs  *    of any contributors may be used to endorse or promote products derived
86286e947fSJustin T. Gibbs  *    from this software without specific prior written permission.
87b0a2fdeeSScott Long  *
88b0a2fdeeSScott Long  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
89b0a2fdeeSScott Long  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90b0a2fdeeSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91b0a2fdeeSScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
92b0a2fdeeSScott Long  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
93b0a2fdeeSScott Long  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
94b0a2fdeeSScott Long  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
95b0a2fdeeSScott Long  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
96b0a2fdeeSScott Long  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
97b0a2fdeeSScott Long  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
98b0a2fdeeSScott Long  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99b0a2fdeeSScott Long  */
100aad970f1SDavid E. O'Brien 
101aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
102aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
1039b631363SMatt Jacob 
104b0a2fdeeSScott Long #include <dev/mpt/mpt.h>
105b0a2fdeeSScott Long #include <dev/mpt/mpt_cam.h>
106b0a2fdeeSScott Long #include <dev/mpt/mpt_raid.h>
1077104aeefSMatt Jacob 
1089b631363SMatt Jacob 
1099b631363SMatt Jacob #ifndef	PCI_VENDOR_LSI
1109b631363SMatt Jacob #define	PCI_VENDOR_LSI			0x1000
1119b631363SMatt Jacob #endif
1129b631363SMatt Jacob 
1139b631363SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC909
1149b631363SMatt Jacob #define	PCI_PRODUCT_LSI_FC909		0x0620
1159b631363SMatt Jacob #endif
1169b631363SMatt Jacob 
117eae4a35fSMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC909A
118eae4a35fSMatt Jacob #define	PCI_PRODUCT_LSI_FC909A		0x0621
119eae4a35fSMatt Jacob #endif
120eae4a35fSMatt Jacob 
121aca01e38SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC919
122aca01e38SMatt Jacob #define	PCI_PRODUCT_LSI_FC919		0x0624
123aca01e38SMatt Jacob #endif
124aca01e38SMatt Jacob 
1259b631363SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC929
1269b631363SMatt Jacob #define	PCI_PRODUCT_LSI_FC929		0x0622
1279b631363SMatt Jacob #endif
1289b631363SMatt Jacob 
129519d7f81SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC929X
130519d7f81SMatt Jacob #define	PCI_PRODUCT_LSI_FC929X		0x0626
131519d7f81SMatt Jacob #endif
132519d7f81SMatt Jacob 
1335b405b07SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC919X
1345b405b07SMatt Jacob #define	PCI_PRODUCT_LSI_FC919X		0x0628
1355b405b07SMatt Jacob #endif
1365b405b07SMatt Jacob 
1370d3baffaSMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC7X04X
1380d3baffaSMatt Jacob #define	PCI_PRODUCT_LSI_FC7X04X		0x0640
1390d3baffaSMatt Jacob #endif
1400d3baffaSMatt Jacob 
141167197aeSMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC646
142167197aeSMatt Jacob #define	PCI_PRODUCT_LSI_FC646		0x0646
143167197aeSMatt Jacob #endif
144167197aeSMatt Jacob 
1459b631363SMatt Jacob #ifndef	PCI_PRODUCT_LSI_1030
1469b631363SMatt Jacob #define	PCI_PRODUCT_LSI_1030		0x0030
1479b631363SMatt Jacob #endif
1489b631363SMatt Jacob 
149444dd2b6SMatt Jacob #ifndef	PCI_PRODUCT_LSI_SAS1064
150444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1064		0x0050
151444dd2b6SMatt Jacob #endif
152444dd2b6SMatt Jacob 
153444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1064A
154444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1064A	0x005C
155444dd2b6SMatt Jacob #endif
156444dd2b6SMatt Jacob 
157444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1064E
158444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1064E	0x0056
159444dd2b6SMatt Jacob #endif
160444dd2b6SMatt Jacob 
161444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1066
162444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1066		0x005E
163444dd2b6SMatt Jacob #endif
164444dd2b6SMatt Jacob 
165444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1066E
166444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1066E	0x005A
167444dd2b6SMatt Jacob #endif
168444dd2b6SMatt Jacob 
169444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1068
170444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1068		0x0054
171444dd2b6SMatt Jacob #endif
172444dd2b6SMatt Jacob 
173444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1068E
174444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1068E	0x0058
175444dd2b6SMatt Jacob #endif
176444dd2b6SMatt Jacob 
177444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1078
178444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1078		0x0060
179444dd2b6SMatt Jacob #endif
180444dd2b6SMatt Jacob 
181019717e0SMatt Jacob #ifndef	PCIM_CMD_SERRESPEN
182019717e0SMatt Jacob #define	PCIM_CMD_SERRESPEN	0x0100
183019717e0SMatt Jacob #endif
184019717e0SMatt Jacob 
1859b631363SMatt Jacob 
186b0a2fdeeSScott Long #define	MPT_IO_BAR	0
187b0a2fdeeSScott Long #define	MPT_MEM_BAR	1
1889b631363SMatt Jacob 
189b0a2fdeeSScott Long static int mpt_pci_probe(device_t);
190b0a2fdeeSScott Long static int mpt_pci_attach(device_t);
191b0a2fdeeSScott Long static void mpt_free_bus_resources(struct mpt_softc *mpt);
192b0a2fdeeSScott Long static int mpt_pci_detach(device_t);
193b0a2fdeeSScott Long static int mpt_pci_shutdown(device_t);
194b0a2fdeeSScott Long static int mpt_dma_mem_alloc(struct mpt_softc *mpt);
195b0a2fdeeSScott Long static void mpt_dma_mem_free(struct mpt_softc *mpt);
196b0a2fdeeSScott Long static void mpt_read_config_regs(struct mpt_softc *mpt);
1977104aeefSMatt Jacob static void mpt_pci_intr(void *);
1989b631363SMatt Jacob 
1999b631363SMatt Jacob static device_method_t mpt_methods[] = {
2009b631363SMatt Jacob 	/* Device interface */
201b0a2fdeeSScott Long 	DEVMETHOD(device_probe,		mpt_pci_probe),
202b0a2fdeeSScott Long 	DEVMETHOD(device_attach,	mpt_pci_attach),
203b0a2fdeeSScott Long 	DEVMETHOD(device_detach,	mpt_pci_detach),
204b0a2fdeeSScott Long 	DEVMETHOD(device_shutdown,	mpt_pci_shutdown),
2059b631363SMatt Jacob 	{ 0, 0 }
2069b631363SMatt Jacob };
2079b631363SMatt Jacob 
2089b631363SMatt Jacob static driver_t mpt_driver = {
209b0a2fdeeSScott Long 	"mpt", mpt_methods, sizeof(struct mpt_softc)
2109b631363SMatt Jacob };
2119b631363SMatt Jacob static devclass_t mpt_devclass;
2129b631363SMatt Jacob DRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0);
2139b631363SMatt Jacob MODULE_VERSION(mpt, 1);
2149b631363SMatt Jacob 
2159b631363SMatt Jacob static int
216b0a2fdeeSScott Long mpt_pci_probe(device_t dev)
2179b631363SMatt Jacob {
2189b631363SMatt Jacob 	char *desc;
2199b631363SMatt Jacob 
220b4c618c0SMatt Jacob 	if (pci_get_vendor(dev) != PCI_VENDOR_LSI) {
2219b631363SMatt Jacob 		return (ENXIO);
222b4c618c0SMatt Jacob 	}
2239b631363SMatt Jacob 
2249b631363SMatt Jacob 	switch ((pci_get_device(dev) & ~1)) {
2259b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC909:
2269b631363SMatt Jacob 		desc = "LSILogic FC909 FC Adapter";
2279b631363SMatt Jacob 		break;
228eae4a35fSMatt Jacob 	case PCI_PRODUCT_LSI_FC909A:
229eae4a35fSMatt Jacob 		desc = "LSILogic FC909A FC Adapter";
230eae4a35fSMatt Jacob 		break;
231aca01e38SMatt Jacob 	case PCI_PRODUCT_LSI_FC919:
232aca01e38SMatt Jacob 		desc = "LSILogic FC919 FC Adapter";
233aca01e38SMatt Jacob 		break;
2349b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC929:
235167197aeSMatt Jacob 		desc = "Dual LSILogic FC929 FC Adapter";
2369b631363SMatt Jacob 		break;
2375b405b07SMatt Jacob 	case PCI_PRODUCT_LSI_FC919X:
238167197aeSMatt Jacob 		desc = "LSILogic FC919 FC PCI-X Adapter";
2395b405b07SMatt Jacob 		break;
240519d7f81SMatt Jacob 	case PCI_PRODUCT_LSI_FC929X:
241167197aeSMatt Jacob 		desc = "Dual LSILogic FC929X 2Gb/s FC PCI-X Adapter";
242167197aeSMatt Jacob 		break;
243167197aeSMatt Jacob 	case PCI_PRODUCT_LSI_FC646:
244167197aeSMatt Jacob 		desc = "Dual LSILogic FC7X04X 4Gb/s FC PCI-Express Adapter";
2450d3baffaSMatt Jacob 		break;
2460d3baffaSMatt Jacob 	case PCI_PRODUCT_LSI_FC7X04X:
247167197aeSMatt Jacob 		desc = "Dual LSILogic FC7X04X 4Gb/s FC PCI-X Adapter";
248519d7f81SMatt Jacob 		break;
2499b631363SMatt Jacob 	case PCI_PRODUCT_LSI_1030:
2509b631363SMatt Jacob 		desc = "LSILogic 1030 Ultra4 Adapter";
2519b631363SMatt Jacob 		break;
252444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064:
253444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064A:
254444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064E:
255444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066:
256444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066E:
257444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068:
258444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068E:
259444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1078:
260167197aeSMatt Jacob 		desc = "LSILogic SAS/SATA Adapter";
261444dd2b6SMatt Jacob 		break;
2629b631363SMatt Jacob 	default:
2639b631363SMatt Jacob 		return (ENXIO);
2649b631363SMatt Jacob 	}
2659b631363SMatt Jacob 
2669b631363SMatt Jacob 	device_set_desc(dev, desc);
267b0a2fdeeSScott Long 	return (0);
2689b631363SMatt Jacob }
2699b631363SMatt Jacob 
270c87e3f83SMatt Jacob #if	__FreeBSD_version < 500000
2719b631363SMatt Jacob static void
272b0a2fdeeSScott Long mpt_set_options(struct mpt_softc *mpt)
2739b631363SMatt Jacob {
2749b631363SMatt Jacob 	int bitmap;
2759b631363SMatt Jacob 
2769b631363SMatt Jacob 	bitmap = 0;
2779b631363SMatt Jacob 	if (getenv_int("mpt_disable", &bitmap)) {
2789b631363SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
2799b631363SMatt Jacob 			mpt->disabled = 1;
2809b631363SMatt Jacob 		}
2819b631363SMatt Jacob 	}
2829b631363SMatt Jacob 	bitmap = 0;
2839b631363SMatt Jacob 	if (getenv_int("mpt_debug", &bitmap)) {
2849b631363SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
285b0a2fdeeSScott Long 			mpt->verbose = MPT_PRT_DEBUG;
2869b631363SMatt Jacob 		}
2879b631363SMatt Jacob 	}
288c87e3f83SMatt Jacob 	bitmap = 0;
2895089bd63SMatt Jacob 	if (getenv_int("mpt_debug1", &bitmap)) {
290c87e3f83SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
2915089bd63SMatt Jacob 			mpt->verbose = MPT_PRT_DEBUG1;
292c87e3f83SMatt Jacob 		}
293c87e3f83SMatt Jacob 	}
294c87e3f83SMatt Jacob 	bitmap = 0;
2955089bd63SMatt Jacob 	if (getenv_int("mpt_debug2", &bitmap)) {
296c87e3f83SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
2975089bd63SMatt Jacob 			mpt->verbose = MPT_PRT_DEBUG2;
298c87e3f83SMatt Jacob 		}
299c87e3f83SMatt Jacob 	}
300c87e3f83SMatt Jacob 	bitmap = 0;
3015089bd63SMatt Jacob 	if (getenv_int("mpt_debug3", &bitmap)) {
302c87e3f83SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
3035089bd63SMatt Jacob 			mpt->verbose = MPT_PRT_DEBUG3;
304c87e3f83SMatt Jacob 		}
305c87e3f83SMatt Jacob 	}
306b4c618c0SMatt Jacob 
307b4c618c0SMatt Jacob 	mpt->cfg_role = MPT_ROLE_DEFAULT;
308b4c618c0SMatt Jacob 	bitmap = 0;
309b4c618c0SMatt Jacob 	if (getenv_int("mpt_nil_role", &bitmap)) {
310b4c618c0SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
311b4c618c0SMatt Jacob 			mpt->cfg_role = 0;
312b4c618c0SMatt Jacob 		}
313b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
314b4c618c0SMatt Jacob 	}
315b4c618c0SMatt Jacob 	bitmap = 0;
316b4c618c0SMatt Jacob 	if (getenv_int("mpt_tgt_role", &bitmap)) {
317b4c618c0SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
318b4c618c0SMatt Jacob 			mpt->cfg_role |= MPT_ROLE_TARGET;
319b4c618c0SMatt Jacob 		}
320b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
321b4c618c0SMatt Jacob 	}
322b4c618c0SMatt Jacob 	bitmap = 0;
323b4c618c0SMatt Jacob 	if (getenv_int("mpt_ini_role", &bitmap)) {
324b4c618c0SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
325b4c618c0SMatt Jacob 			mpt->cfg_role |= MPT_ROLE_INITIATOR;
326b4c618c0SMatt Jacob 		}
327b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
328b4c618c0SMatt Jacob 	}
3299b631363SMatt Jacob }
3309b631363SMatt Jacob #else
3319b631363SMatt Jacob static void
332b0a2fdeeSScott Long mpt_set_options(struct mpt_softc *mpt)
3339b631363SMatt Jacob {
3349b631363SMatt Jacob 	int tval;
3359b631363SMatt Jacob 
3369b631363SMatt Jacob 	tval = 0;
3379b631363SMatt Jacob 	if (resource_int_value(device_get_name(mpt->dev),
3389b631363SMatt Jacob 	    device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) {
3399b631363SMatt Jacob 		mpt->disabled = 1;
3409b631363SMatt Jacob 	}
3419b631363SMatt Jacob 	tval = 0;
3429b631363SMatt Jacob 	if (resource_int_value(device_get_name(mpt->dev),
3439b631363SMatt Jacob 	    device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) {
34438a56edbSMatt Jacob 		mpt->verbose = tval;
3459b631363SMatt Jacob 	}
346b4c618c0SMatt Jacob 	tval = -1;
347c87e3f83SMatt Jacob 	if (resource_int_value(device_get_name(mpt->dev),
348b4c618c0SMatt Jacob 	    device_get_unit(mpt->dev), "role", &tval) == 0 && tval >= 0 &&
349c87e3f83SMatt Jacob 	    tval <= 3) {
350b4c618c0SMatt Jacob 		mpt->cfg_role = tval;
351b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
352c87e3f83SMatt Jacob 	}
3539b631363SMatt Jacob }
3549b631363SMatt Jacob #endif
3559b631363SMatt Jacob 
3569b631363SMatt Jacob 
357019717e0SMatt Jacob static void
358b0a2fdeeSScott Long mpt_link_peer(struct mpt_softc *mpt)
359019717e0SMatt Jacob {
360b0a2fdeeSScott Long 	struct mpt_softc *mpt2;
361019717e0SMatt Jacob 
3625089bd63SMatt Jacob 	if (mpt->unit == 0) {
363019717e0SMatt Jacob 		return;
3645089bd63SMatt Jacob 	}
365019717e0SMatt Jacob 	/*
366019717e0SMatt Jacob 	 * XXX: depends on probe order
367019717e0SMatt Jacob 	 */
368b0a2fdeeSScott Long 	mpt2 = (struct mpt_softc *)devclass_get_softc(mpt_devclass,mpt->unit-1);
369019717e0SMatt Jacob 
370019717e0SMatt Jacob 	if (mpt2 == NULL) {
371019717e0SMatt Jacob 		return;
372019717e0SMatt Jacob 	}
373019717e0SMatt Jacob 	if (pci_get_vendor(mpt2->dev) != pci_get_vendor(mpt->dev)) {
374019717e0SMatt Jacob 		return;
375019717e0SMatt Jacob 	}
376019717e0SMatt Jacob 	if (pci_get_device(mpt2->dev) != pci_get_device(mpt->dev)) {
377019717e0SMatt Jacob 		return;
378019717e0SMatt Jacob 	}
379019717e0SMatt Jacob 	mpt->mpt2 = mpt2;
380019717e0SMatt Jacob 	mpt2->mpt2 = mpt;
381b0a2fdeeSScott Long 	if (mpt->verbose >= MPT_PRT_DEBUG) {
382b0a2fdeeSScott Long 		mpt_prt(mpt, "linking with peer (mpt%d)\n",
383019717e0SMatt Jacob 		    device_get_unit(mpt2->dev));
384019717e0SMatt Jacob 	}
385019717e0SMatt Jacob }
386019717e0SMatt Jacob 
3875089bd63SMatt Jacob static void
3885089bd63SMatt Jacob mpt_unlink_peer(struct mpt_softc *mpt)
3895089bd63SMatt Jacob {
3905089bd63SMatt Jacob 	if (mpt->mpt2) {
3915089bd63SMatt Jacob 		mpt->mpt2->mpt2 = NULL;
3925089bd63SMatt Jacob 	}
3935089bd63SMatt Jacob }
3945089bd63SMatt Jacob 
395019717e0SMatt Jacob 
3969b631363SMatt Jacob static int
397b0a2fdeeSScott Long mpt_pci_attach(device_t dev)
3989b631363SMatt Jacob {
399b0a2fdeeSScott Long 	struct mpt_softc *mpt;
4009b631363SMatt Jacob 	int		  iqd;
401b0a2fdeeSScott Long 	uint32_t	  data, cmd;
4029b631363SMatt Jacob 
4039b631363SMatt Jacob 	/* Allocate the softc structure */
404b0a2fdeeSScott Long 	mpt  = (struct mpt_softc*)device_get_softc(dev);
4059b631363SMatt Jacob 	if (mpt == NULL) {
4069b631363SMatt Jacob 		device_printf(dev, "cannot allocate softc\n");
4079b631363SMatt Jacob 		return (ENOMEM);
4089b631363SMatt Jacob 	}
4095e073106SMatt Jacob 	memset(mpt, 0, sizeof(struct mpt_softc));
4109b631363SMatt Jacob 	switch ((pci_get_device(dev) & ~1)) {
4119b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC909:
412eae4a35fSMatt Jacob 	case PCI_PRODUCT_LSI_FC909A:
413aca01e38SMatt Jacob 	case PCI_PRODUCT_LSI_FC919:
4149b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC929:
4155b405b07SMatt Jacob 	case PCI_PRODUCT_LSI_FC919X:
416167197aeSMatt Jacob 	case PCI_PRODUCT_LSI_FC646:
4170d3baffaSMatt Jacob 	case PCI_PRODUCT_LSI_FC7X04X:
4189b631363SMatt Jacob 		mpt->is_fc = 1;
4199b631363SMatt Jacob 		break;
420444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064:
421444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064A:
422444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064E:
423444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066:
424444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066E:
425444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068:
426444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068E:
427444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1078:
428444dd2b6SMatt Jacob 		mpt->is_sas = 1;
429444dd2b6SMatt Jacob 		break;
4309b631363SMatt Jacob 	default:
4315580ce96SMatt Jacob 		mpt->is_spi = 1;
4329b631363SMatt Jacob 		break;
4339b631363SMatt Jacob 	}
4349b631363SMatt Jacob 	mpt->dev = dev;
4359b631363SMatt Jacob 	mpt->unit = device_get_unit(dev);
436b0a2fdeeSScott Long 	mpt->raid_resync_rate = MPT_RAID_RESYNC_RATE_DEFAULT;
437b0a2fdeeSScott Long 	mpt->raid_mwce_setting = MPT_RAID_MWCE_DEFAULT;
438b0a2fdeeSScott Long 	mpt->raid_queue_depth = MPT_RAID_QUEUE_DEPTH_DEFAULT;
439444dd2b6SMatt Jacob 	mpt->verbose = MPT_PRT_NONE;
4405089bd63SMatt Jacob 	mpt->role = MPT_ROLE_NONE;
4419b631363SMatt Jacob 	mpt_set_options(mpt);
442444dd2b6SMatt Jacob 	if (mpt->verbose == MPT_PRT_NONE) {
443444dd2b6SMatt Jacob 		mpt->verbose = MPT_PRT_WARN;
444444dd2b6SMatt Jacob 		/* Print INFO level (if any) if bootverbose is set */
4459b631363SMatt Jacob 		mpt->verbose += (bootverbose != 0)? 1 : 0;
446444dd2b6SMatt Jacob 	}
4479b631363SMatt Jacob 	/* Make sure memory access decoders are enabled */
4489b631363SMatt Jacob 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
4499b631363SMatt Jacob 	if ((cmd & PCIM_CMD_MEMEN) == 0) {
4509b631363SMatt Jacob 		device_printf(dev, "Memory accesses disabled");
4515089bd63SMatt Jacob 		return (ENXIO);
4529b631363SMatt Jacob 	}
4539b631363SMatt Jacob 
4549b631363SMatt Jacob 	/*
4559b631363SMatt Jacob 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
4569b631363SMatt Jacob 	 */
4579b631363SMatt Jacob 	cmd |=
4589b631363SMatt Jacob 	    PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN |
4599b631363SMatt Jacob 	    PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
4609b631363SMatt Jacob 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
4619b631363SMatt Jacob 
4629b631363SMatt Jacob 	/*
4639b631363SMatt Jacob 	 * Make sure we've disabled the ROM.
4649b631363SMatt Jacob 	 */
4659b631363SMatt Jacob 	data = pci_read_config(dev, PCIR_BIOS, 4);
4669b631363SMatt Jacob 	data &= ~1;
4679b631363SMatt Jacob 	pci_write_config(dev, PCIR_BIOS, data, 4);
4689b631363SMatt Jacob 
469019717e0SMatt Jacob 	/*
470019717e0SMatt Jacob 	 * Is this part a dual?
471019717e0SMatt Jacob 	 * If so, link with our partner (around yet)
472019717e0SMatt Jacob 	 */
473019717e0SMatt Jacob 	if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929 ||
474167197aeSMatt Jacob 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC646 ||
4750d3baffaSMatt Jacob 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC7X04X ||
476019717e0SMatt Jacob 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_1030) {
477019717e0SMatt Jacob 		mpt_link_peer(mpt);
4789b631363SMatt Jacob 	}
4799b631363SMatt Jacob 
480b0a2fdeeSScott Long 	/*
481b0a2fdeeSScott Long 	 * Set up register access.  PIO mode is required for
482444dd2b6SMatt Jacob 	 * certain reset operations (but must be disabled for
483444dd2b6SMatt Jacob 	 * some cards otherwise).
484b0a2fdeeSScott Long 	 */
485b0a2fdeeSScott Long 	mpt->pci_pio_rid = PCIR_BAR(MPT_IO_BAR);
486b0a2fdeeSScott Long 	mpt->pci_pio_reg = bus_alloc_resource(dev, SYS_RES_IOPORT,
487b0a2fdeeSScott Long 			    &mpt->pci_pio_rid, 0, ~0, 0, RF_ACTIVE);
488b0a2fdeeSScott Long 	if (mpt->pci_pio_reg == NULL) {
489b0a2fdeeSScott Long 		device_printf(dev, "unable to map registers in PIO mode\n");
4909b631363SMatt Jacob 		goto bad;
4919b631363SMatt Jacob 	}
492b0a2fdeeSScott Long 	mpt->pci_pio_st = rman_get_bustag(mpt->pci_pio_reg);
493b0a2fdeeSScott Long 	mpt->pci_pio_sh = rman_get_bushandle(mpt->pci_pio_reg);
494b0a2fdeeSScott Long 
495b0a2fdeeSScott Long 	/* Allocate kernel virtual memory for the 9x9's Mem0 region */
496b0a2fdeeSScott Long 	mpt->pci_mem_rid = PCIR_BAR(MPT_MEM_BAR);
497b0a2fdeeSScott Long 	mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY,
498b0a2fdeeSScott Long 			&mpt->pci_mem_rid, 0, ~0, 0, RF_ACTIVE);
499b0a2fdeeSScott Long 	if (mpt->pci_reg == NULL) {
500b0a2fdeeSScott Long 		device_printf(dev, "Unable to memory map registers.\n");
501444dd2b6SMatt Jacob 		if (mpt->is_sas) {
502444dd2b6SMatt Jacob 			device_printf(dev, "Giving Up.\n");
503444dd2b6SMatt Jacob 			goto bad;
504444dd2b6SMatt Jacob 		}
505b0a2fdeeSScott Long 		device_printf(dev, "Falling back to PIO mode.\n");
506b0a2fdeeSScott Long 		mpt->pci_st = mpt->pci_pio_st;
507b0a2fdeeSScott Long 		mpt->pci_sh = mpt->pci_pio_sh;
508b0a2fdeeSScott Long 	} else {
5099b631363SMatt Jacob 		mpt->pci_st = rman_get_bustag(mpt->pci_reg);
5109b631363SMatt Jacob 		mpt->pci_sh = rman_get_bushandle(mpt->pci_reg);
511b0a2fdeeSScott Long 	}
5129b631363SMatt Jacob 
5139b631363SMatt Jacob 	/* Get a handle to the interrupt */
5149b631363SMatt Jacob 	iqd = 0;
515280388afSJohn Baldwin 	if (pci_msi_count(dev) == 1) {
516280388afSJohn Baldwin 		mpt->pci_msi_count = 1;
517280388afSJohn Baldwin 		if (pci_alloc_msi(dev, &mpt->pci_msi_count) == 0)
518280388afSJohn Baldwin 			iqd = 1;
519280388afSJohn Baldwin 		else
520280388afSJohn Baldwin 			mpt->pci_msi_count = 0;
521280388afSJohn Baldwin 	}
5225f96beb9SNate Lawson 	mpt->pci_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
5235f96beb9SNate Lawson 	    RF_ACTIVE | RF_SHAREABLE);
5249b631363SMatt Jacob 	if (mpt->pci_irq == NULL) {
5259b631363SMatt Jacob 		device_printf(dev, "could not allocate interrupt\n");
5269b631363SMatt Jacob 		goto bad;
5279b631363SMatt Jacob 	}
5289b631363SMatt Jacob 
5297104aeefSMatt Jacob 	MPT_LOCK_SETUP(mpt);
5307104aeefSMatt Jacob 
5319b631363SMatt Jacob 	/* Disable interrupts at the part */
5329b631363SMatt Jacob 	mpt_disable_ints(mpt);
5339b631363SMatt Jacob 
534b0a2fdeeSScott Long 	/* Register the interrupt handler */
535b0a2fdeeSScott Long 	if (bus_setup_intr(dev, mpt->pci_irq, MPT_IFLAGS, mpt_pci_intr,
536b0a2fdeeSScott Long 	    mpt, &mpt->ih)) {
537b0a2fdeeSScott Long 		device_printf(dev, "could not setup interrupt\n");
538b0a2fdeeSScott Long 		goto bad;
539b0a2fdeeSScott Long 	}
540b0a2fdeeSScott Long 
5419b631363SMatt Jacob 	/* Allocate dma memory */
542b0a2fdeeSScott Long /* XXX JGibbs -Should really be done based on IOCFacts. */
5439b631363SMatt Jacob 	if (mpt_dma_mem_alloc(mpt)) {
5445a4c2d0eSMatt Jacob 		mpt_prt(mpt, "Could not allocate DMA memory\n");
5459b631363SMatt Jacob 		goto bad;
5469b631363SMatt Jacob 	}
5479b631363SMatt Jacob 
5487104aeefSMatt Jacob 	/*
5497104aeefSMatt Jacob 	 * Save the PCI config register values
5507104aeefSMatt Jacob  	 *
5517104aeefSMatt Jacob 	 * Hard resets are known to screw up the BAR for diagnostic
5527104aeefSMatt Jacob 	 * memory accesses (Mem1).
5537104aeefSMatt Jacob 	 *
5547104aeefSMatt Jacob 	 * Using Mem1 is known to make the chip stop responding to
5557104aeefSMatt Jacob 	 * configuration space transfers, so we need to save it now
5567104aeefSMatt Jacob 	 */
5579b631363SMatt Jacob 
5589b631363SMatt Jacob 	mpt_read_config_regs(mpt);
5599b631363SMatt Jacob 
560444dd2b6SMatt Jacob 	/*
561444dd2b6SMatt Jacob 	 * Disable PIO until we need it
562444dd2b6SMatt Jacob 	 */
5639fe6d254SMatt Jacob 	if (mpt->is_sas) {
564444dd2b6SMatt Jacob 		pci_disable_io(dev, SYS_RES_IOPORT);
5659fe6d254SMatt Jacob 	}
566444dd2b6SMatt Jacob 
5679b631363SMatt Jacob 	/* Initialize the hardware */
5689b631363SMatt Jacob 	if (mpt->disabled == 0) {
5697104aeefSMatt Jacob 		MPT_LOCK(mpt);
570b0a2fdeeSScott Long 		if (mpt_attach(mpt) != 0) {
5717104aeefSMatt Jacob 			MPT_UNLOCK(mpt);
5729b631363SMatt Jacob 			goto bad;
5739b631363SMatt Jacob 		}
574c87e3f83SMatt Jacob 		MPT_UNLOCK(mpt);
575c87e3f83SMatt Jacob 	} else {
576c87e3f83SMatt Jacob 		mpt_prt(mpt, "device disabled at user request\n");
577c87e3f83SMatt Jacob 		goto bad;
5787104aeefSMatt Jacob 	}
5797104aeefSMatt Jacob 
580c87e3f83SMatt Jacob 	mpt->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, mpt_pci_shutdown,
581c87e3f83SMatt Jacob 	    dev, SHUTDOWN_PRI_DEFAULT);
582c87e3f83SMatt Jacob 
583c87e3f83SMatt Jacob 	if (mpt->eh == NULL) {
584c87e3f83SMatt Jacob 		mpt_prt(mpt, "shutdown event registration failed\n");
585c87e3f83SMatt Jacob 		MPT_LOCK(mpt);
586c87e3f83SMatt Jacob 		(void) mpt_detach(mpt);
587c87e3f83SMatt Jacob 		MPT_UNLOCK(mpt);
588c87e3f83SMatt Jacob 		goto bad;
589c87e3f83SMatt Jacob 	}
5905089bd63SMatt Jacob 	KASSERT(MPT_OWNED(mpt) == 0, ("leaving attach with device locked"));
5919b631363SMatt Jacob 	return (0);
5929b631363SMatt Jacob 
5939b631363SMatt Jacob bad:
5949b631363SMatt Jacob 	mpt_dma_mem_free(mpt);
5959b631363SMatt Jacob 	mpt_free_bus_resources(mpt);
5965089bd63SMatt Jacob 	mpt_unlink_peer(mpt);
5975089bd63SMatt Jacob 
5985089bd63SMatt Jacob 	MPT_LOCK_DESTROY(mpt);
5999b631363SMatt Jacob 
6009b631363SMatt Jacob 	/*
6019b631363SMatt Jacob 	 * but return zero to preserve unit numbering
6029b631363SMatt Jacob 	 */
6039b631363SMatt Jacob 	return (0);
6049b631363SMatt Jacob }
6059b631363SMatt Jacob 
6067104aeefSMatt Jacob /*
6079b631363SMatt Jacob  * Free bus resources
6089b631363SMatt Jacob  */
6099b631363SMatt Jacob static void
610b0a2fdeeSScott Long mpt_free_bus_resources(struct mpt_softc *mpt)
6119b631363SMatt Jacob {
6129b631363SMatt Jacob 	if (mpt->ih) {
6139b631363SMatt Jacob 		bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih);
6149b631363SMatt Jacob 		mpt->ih = 0;
6159b631363SMatt Jacob 	}
6169b631363SMatt Jacob 
6179b631363SMatt Jacob 	if (mpt->pci_irq) {
618280388afSJohn Baldwin 		bus_release_resource(mpt->dev, SYS_RES_IRQ,
619280388afSJohn Baldwin 		    mpt->pci_msi_count ? 1 : 0, mpt->pci_irq);
6209b631363SMatt Jacob 		mpt->pci_irq = 0;
6219b631363SMatt Jacob 	}
6229b631363SMatt Jacob 
623280388afSJohn Baldwin 	if (mpt->pci_msi_count) {
624280388afSJohn Baldwin 		pci_release_msi(mpt->dev);
625280388afSJohn Baldwin 		mpt->pci_msi_count = 0;
626280388afSJohn Baldwin 	}
627280388afSJohn Baldwin 
628b0a2fdeeSScott Long 	if (mpt->pci_pio_reg) {
629b0a2fdeeSScott Long 		bus_release_resource(mpt->dev, SYS_RES_IOPORT, mpt->pci_pio_rid,
630b0a2fdeeSScott Long 			mpt->pci_pio_reg);
631b0a2fdeeSScott Long 		mpt->pci_pio_reg = 0;
632b0a2fdeeSScott Long 	}
6339b631363SMatt Jacob 	if (mpt->pci_reg) {
634b0a2fdeeSScott Long 		bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_mem_rid,
6359b631363SMatt Jacob 			mpt->pci_reg);
6369b631363SMatt Jacob 		mpt->pci_reg = 0;
6379b631363SMatt Jacob 	}
6387104aeefSMatt Jacob 	MPT_LOCK_DESTROY(mpt);
6399b631363SMatt Jacob }
6409b631363SMatt Jacob 
6419b631363SMatt Jacob 
6427104aeefSMatt Jacob /*
6439b631363SMatt Jacob  * Disconnect ourselves from the system.
6449b631363SMatt Jacob  */
6459b631363SMatt Jacob static int
646b0a2fdeeSScott Long mpt_pci_detach(device_t dev)
6479b631363SMatt Jacob {
648b0a2fdeeSScott Long 	struct mpt_softc *mpt;
6499b631363SMatt Jacob 
650b0a2fdeeSScott Long 	mpt  = (struct mpt_softc*)device_get_softc(dev);
6519b631363SMatt Jacob 
6529b631363SMatt Jacob 	if (mpt) {
653c87e3f83SMatt Jacob 		MPT_LOCK(mpt);
6549b631363SMatt Jacob 		mpt_disable_ints(mpt);
655b0a2fdeeSScott Long 		mpt_detach(mpt);
656b0a2fdeeSScott Long 		mpt_reset(mpt, /*reinit*/FALSE);
6579b631363SMatt Jacob 		mpt_dma_mem_free(mpt);
6589b631363SMatt Jacob 		mpt_free_bus_resources(mpt);
6591d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
6601d79ca0eSMatt Jacob 		if (mpt->eh != NULL) {
661b0a2fdeeSScott Long                         EVENTHANDLER_DEREGISTER(shutdown_final, mpt->eh);
6621d79ca0eSMatt Jacob 		}
663c87e3f83SMatt Jacob 		MPT_UNLOCK(mpt);
6649b631363SMatt Jacob 	}
6659b631363SMatt Jacob 	return(0);
6669b631363SMatt Jacob }
6679b631363SMatt Jacob 
6689b631363SMatt Jacob 
6697104aeefSMatt Jacob /*
6709b631363SMatt Jacob  * Disable the hardware
6719b631363SMatt Jacob  */
6729b631363SMatt Jacob static int
673b0a2fdeeSScott Long mpt_pci_shutdown(device_t dev)
6749b631363SMatt Jacob {
675b0a2fdeeSScott Long 	struct mpt_softc *mpt;
6769b631363SMatt Jacob 
677b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)device_get_softc(dev);
678c87e3f83SMatt Jacob 	if (mpt) {
679c87e3f83SMatt Jacob 		int r;
680c87e3f83SMatt Jacob 		MPT_LOCK(mpt);
681c87e3f83SMatt Jacob 		r = mpt_shutdown(mpt);
682c87e3f83SMatt Jacob 		MPT_UNLOCK(mpt);
683c87e3f83SMatt Jacob 		return (r);
684c87e3f83SMatt Jacob 	}
6859b631363SMatt Jacob 	return(0);
6869b631363SMatt Jacob }
6879b631363SMatt Jacob 
6889b631363SMatt Jacob static int
689b0a2fdeeSScott Long mpt_dma_mem_alloc(struct mpt_softc *mpt)
6909b631363SMatt Jacob {
691444dd2b6SMatt Jacob 	int i, error, nsegs;
692b0a2fdeeSScott Long 	uint8_t *vptr;
693b0a2fdeeSScott Long 	uint32_t pptr, end;
6947dec90bcSMatt Jacob 	size_t len;
695b0a2fdeeSScott Long 	struct mpt_map_info mi;
6969b631363SMatt Jacob 
6979b631363SMatt Jacob 	/* Check if we alreay have allocated the reply memory */
698a89ec05eSPeter Wemm 	if (mpt->reply_phys != 0) {
6999b631363SMatt Jacob 		return 0;
7007dec90bcSMatt Jacob 	}
7017dec90bcSMatt Jacob 
702b0a2fdeeSScott Long 	len = sizeof (request_t) * MPT_MAX_REQUESTS(mpt);
7037dec90bcSMatt Jacob #ifdef	RELENG_4
704a163d034SWarner Losh 	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK);
7057dec90bcSMatt Jacob 	if (mpt->request_pool == NULL) {
7065a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate request pool\n");
7077dec90bcSMatt Jacob 		return (1);
7087dec90bcSMatt Jacob 	}
7095e073106SMatt Jacob 	memset(mpt->request_pool, 0, len);
7107dec90bcSMatt Jacob #else
711b0a2fdeeSScott Long 	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
7127dec90bcSMatt Jacob 	if (mpt->request_pool == NULL) {
7135a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate request pool\n");
7147dec90bcSMatt Jacob 		return (1);
7157dec90bcSMatt Jacob 	}
7167dec90bcSMatt Jacob #endif
7179b631363SMatt Jacob 
7189b631363SMatt Jacob 	/*
719444dd2b6SMatt Jacob 	 * Create a parent dma tag for this device.
7209b631363SMatt Jacob 	 *
7215a4c2d0eSMatt Jacob 	 * Align at byte boundaries,
7225a4c2d0eSMatt Jacob 	 * Limit to 32-bit addressing for request/reply queues.
7239b631363SMatt Jacob 	 */
724b0a2fdeeSScott Long 	if (mpt_dma_tag_create(mpt, /*parent*/NULL, /*alignment*/1,
725444dd2b6SMatt Jacob 	    /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR,
726b0a2fdeeSScott Long 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
727b0a2fdeeSScott Long 	    /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
728b0a2fdeeSScott Long 	    /*nsegments*/BUS_SPACE_MAXSIZE_32BIT,
729b0a2fdeeSScott Long 	    /*maxsegsz*/BUS_SPACE_UNRESTRICTED, /*flags*/0,
730b0a2fdeeSScott Long 	    &mpt->parent_dmat) != 0) {
7315a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot create parent dma tag\n");
7329b631363SMatt Jacob 		return (1);
7339b631363SMatt Jacob 	}
7349b631363SMatt Jacob 
7359b631363SMatt Jacob 	/* Create a child tag for reply buffers */
7365a4c2d0eSMatt Jacob 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
7375a4c2d0eSMatt Jacob 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
738444dd2b6SMatt Jacob 	    NULL, NULL, 2 * PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0,
739b0a2fdeeSScott Long 	    &mpt->reply_dmat) != 0) {
7405a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot create a dma tag for replies\n");
7419b631363SMatt Jacob 		return (1);
7429b631363SMatt Jacob 	}
7439b631363SMatt Jacob 
7449b631363SMatt Jacob 	/* Allocate some DMA accessable memory for replies */
7459b631363SMatt Jacob 	if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply,
7469b631363SMatt Jacob 	    BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) {
7475a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate %lu bytes of reply memory\n",
748444dd2b6SMatt Jacob 		    (u_long) (2 * PAGE_SIZE));
7499b631363SMatt Jacob 		return (1);
7509b631363SMatt Jacob 	}
7519b631363SMatt Jacob 
752b0a2fdeeSScott Long 	mi.mpt = mpt;
753b0a2fdeeSScott Long 	mi.error = 0;
7549b631363SMatt Jacob 
7559b631363SMatt Jacob 	/* Load and lock it into "bus space" */
7569b631363SMatt Jacob 	bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply,
757444dd2b6SMatt Jacob 	    2 * PAGE_SIZE, mpt_map_rquest, &mi, 0);
7589b631363SMatt Jacob 
759b0a2fdeeSScott Long 	if (mi.error) {
7605a4c2d0eSMatt Jacob 		mpt_prt(mpt, "error %d loading dma map for DMA reply queue\n",
7615a4c2d0eSMatt Jacob 		    mi.error);
7629b631363SMatt Jacob 		return (1);
7639b631363SMatt Jacob 	}
764b0a2fdeeSScott Long 	mpt->reply_phys = mi.phys;
7659b631363SMatt Jacob 
7669b631363SMatt Jacob 	/* Create a child tag for data buffers */
767444dd2b6SMatt Jacob 
768444dd2b6SMatt Jacob 	/*
769444dd2b6SMatt Jacob 	 * XXX: we should say that nsegs is 'unrestricted, but that
770444dd2b6SMatt Jacob 	 * XXX: tickles a horrible bug in the busdma code. Instead,
771444dd2b6SMatt Jacob 	 * XXX: we'll derive a reasonable segment limit from MAXPHYS
772444dd2b6SMatt Jacob 	 */
773444dd2b6SMatt Jacob 	nsegs = (MAXPHYS / PAGE_SIZE) + 1;
774b0a2fdeeSScott Long 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
7759b631363SMatt Jacob 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
776444dd2b6SMatt Jacob 	    NULL, NULL, MAXBSIZE, nsegs, BUS_SPACE_MAXSIZE_32BIT, 0,
777b0a2fdeeSScott Long 	    &mpt->buffer_dmat) != 0) {
7785a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot create a dma tag for data buffers\n");
7799b631363SMatt Jacob 		return (1);
7809b631363SMatt Jacob 	}
7819b631363SMatt Jacob 
7829b631363SMatt Jacob 	/* Create a child tag for request buffers */
7835a4c2d0eSMatt Jacob 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
7845a4c2d0eSMatt Jacob 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
7857dec90bcSMatt Jacob 	    NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
786b0a2fdeeSScott Long 	    &mpt->request_dmat) != 0) {
7875a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot create a dma tag for requests\n");
7889b631363SMatt Jacob 		return (1);
7899b631363SMatt Jacob 	}
7909b631363SMatt Jacob 
7919b631363SMatt Jacob 	/* Allocate some DMA accessable memory for requests */
7929b631363SMatt Jacob 	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
7939b631363SMatt Jacob 	    BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) {
7945a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate %d bytes of request memory\n",
7957dec90bcSMatt Jacob 		    MPT_REQ_MEM_SIZE(mpt));
7969b631363SMatt Jacob 		return (1);
7979b631363SMatt Jacob 	}
7989b631363SMatt Jacob 
799b0a2fdeeSScott Long 	mi.mpt = mpt;
800b0a2fdeeSScott Long 	mi.error = 0;
8019b631363SMatt Jacob 
8029b631363SMatt Jacob 	/* Load and lock it into "bus space" */
8039b631363SMatt Jacob         bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
804b0a2fdeeSScott Long 	    MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
8059b631363SMatt Jacob 
806b0a2fdeeSScott Long 	if (mi.error) {
8075a4c2d0eSMatt Jacob 		mpt_prt(mpt, "error %d loading dma map for DMA request queue\n",
808b0a2fdeeSScott Long 		    mi.error);
8099b631363SMatt Jacob 		return (1);
8109b631363SMatt Jacob 	}
811b0a2fdeeSScott Long 	mpt->request_phys = mi.phys;
8129b631363SMatt Jacob 
8135a4c2d0eSMatt Jacob 	/*
8145a4c2d0eSMatt Jacob 	 * Now create per-request dma maps
8155a4c2d0eSMatt Jacob 	 */
8169b631363SMatt Jacob 	i = 0;
8179b631363SMatt Jacob 	pptr =  mpt->request_phys;
8189b631363SMatt Jacob 	vptr =  mpt->request;
8197dec90bcSMatt Jacob 	end = pptr + MPT_REQ_MEM_SIZE(mpt);
8209b631363SMatt Jacob 	while(pptr < end) {
8217dec90bcSMatt Jacob 		request_t *req = &mpt->request_pool[i];
8229b631363SMatt Jacob 		req->index = i++;
8239b631363SMatt Jacob 
8249b631363SMatt Jacob 		/* Store location of Request Data */
8259b631363SMatt Jacob 		req->req_pbuf = pptr;
8269b631363SMatt Jacob 		req->req_vbuf = vptr;
8279b631363SMatt Jacob 
8289b631363SMatt Jacob 		pptr += MPT_REQUEST_AREA;
8299b631363SMatt Jacob 		vptr += MPT_REQUEST_AREA;
8309b631363SMatt Jacob 
8319b631363SMatt Jacob 		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
8329b631363SMatt Jacob 		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
8339b631363SMatt Jacob 
8349b631363SMatt Jacob 		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
8359b631363SMatt Jacob 		if (error) {
8365a4c2d0eSMatt Jacob 			mpt_prt(mpt, "error %d creating per-cmd DMA maps\n",
8375a4c2d0eSMatt Jacob 			    error);
8389b631363SMatt Jacob 			return (1);
8399b631363SMatt Jacob 		}
8409b631363SMatt Jacob 	}
8415a4c2d0eSMatt Jacob 
8429b631363SMatt Jacob 	return (0);
8439b631363SMatt Jacob }
8449b631363SMatt Jacob 
8459b631363SMatt Jacob 
8469b631363SMatt Jacob 
8479b631363SMatt Jacob /* Deallocate memory that was allocated by mpt_dma_mem_alloc
8489b631363SMatt Jacob  */
8499b631363SMatt Jacob static void
850b0a2fdeeSScott Long mpt_dma_mem_free(struct mpt_softc *mpt)
8519b631363SMatt Jacob {
8529b631363SMatt Jacob 	int i;
8539b631363SMatt Jacob 
8549b631363SMatt Jacob         /* Make sure we aren't double destroying */
8559b631363SMatt Jacob         if (mpt->reply_dmat == 0) {
8565a4c2d0eSMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
8579b631363SMatt Jacob 		return;
8589b631363SMatt Jacob         }
8599b631363SMatt Jacob 
8607dec90bcSMatt Jacob 	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
8617dec90bcSMatt Jacob 		bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
8629b631363SMatt Jacob 	}
8639b631363SMatt Jacob 	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
8649b631363SMatt Jacob 	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
8659b631363SMatt Jacob 	bus_dma_tag_destroy(mpt->request_dmat);
8669b631363SMatt Jacob 	bus_dma_tag_destroy(mpt->buffer_dmat);
8679b631363SMatt Jacob 	bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap);
8689b631363SMatt Jacob 	bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap);
8699b631363SMatt Jacob 	bus_dma_tag_destroy(mpt->reply_dmat);
8709b631363SMatt Jacob 	bus_dma_tag_destroy(mpt->parent_dmat);
8719b631363SMatt Jacob 	mpt->reply_dmat = 0;
8727dec90bcSMatt Jacob 	free(mpt->request_pool, M_DEVBUF);
8737dec90bcSMatt Jacob 	mpt->request_pool = 0;
8749b631363SMatt Jacob 
8759b631363SMatt Jacob }
8769b631363SMatt Jacob 
8779b631363SMatt Jacob 
8789b631363SMatt Jacob 
8799b631363SMatt Jacob /* Reads modifiable (via PCI transactions) config registers */
8809b631363SMatt Jacob static void
881b0a2fdeeSScott Long mpt_read_config_regs(struct mpt_softc *mpt)
8829b631363SMatt Jacob {
8839b631363SMatt Jacob 	mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2);
8849b631363SMatt Jacob 	mpt->pci_cfg.LatencyTimer_LineSize =
8859b631363SMatt Jacob 	    pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2);
886e27951b2SJohn Baldwin 	mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_BAR(0), 4);
887e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(1), 4);
888e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(2), 4);
889e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(3), 4);
890e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(4), 4);
8919b631363SMatt Jacob 	mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4);
8929b631363SMatt Jacob 	mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1);
8939b631363SMatt Jacob 	mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4);
8949b631363SMatt Jacob }
8959b631363SMatt Jacob 
8969b631363SMatt Jacob /* Sets modifiable config registers */
8979b631363SMatt Jacob void
898b0a2fdeeSScott Long mpt_set_config_regs(struct mpt_softc *mpt)
8999b631363SMatt Jacob {
900b0a2fdeeSScott Long 	uint32_t val;
9019b631363SMatt Jacob 
9029b631363SMatt Jacob #define MPT_CHECK(reg, offset, size)					\
9039b631363SMatt Jacob 	val = pci_read_config(mpt->dev, offset, size);			\
9049b631363SMatt Jacob 	if (mpt->pci_cfg.reg != val) {					\
905301472c2SMatt Jacob 		mpt_prt(mpt,						\
9069b631363SMatt Jacob 		    "Restoring " #reg " to 0x%X from 0x%X\n",		\
9079b631363SMatt Jacob 		    mpt->pci_cfg.reg, val);				\
9089b631363SMatt Jacob 	}
9099b631363SMatt Jacob 
910b0a2fdeeSScott Long 	if (mpt->verbose >= MPT_PRT_DEBUG) {
9119b631363SMatt Jacob 		MPT_CHECK(Command, PCIR_COMMAND, 2);
9129b631363SMatt Jacob 		MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2);
913e27951b2SJohn Baldwin 		MPT_CHECK(IO_BAR, PCIR_BAR(0), 4);
914e27951b2SJohn Baldwin 		MPT_CHECK(Mem0_BAR[0], PCIR_BAR(1), 4);
915e27951b2SJohn Baldwin 		MPT_CHECK(Mem0_BAR[1], PCIR_BAR(2), 4);
916e27951b2SJohn Baldwin 		MPT_CHECK(Mem1_BAR[0], PCIR_BAR(3), 4);
917e27951b2SJohn Baldwin 		MPT_CHECK(Mem1_BAR[1], PCIR_BAR(4), 4);
9189b631363SMatt Jacob 		MPT_CHECK(ROM_BAR, PCIR_BIOS, 4);
9199b631363SMatt Jacob 		MPT_CHECK(IntLine, PCIR_INTLINE, 1);
9209b631363SMatt Jacob 		MPT_CHECK(PMCSR, 0x44, 4);
9219b631363SMatt Jacob 	}
9229b631363SMatt Jacob #undef MPT_CHECK
9239b631363SMatt Jacob 
9249b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2);
9259b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_CACHELNSZ,
9269b631363SMatt Jacob 	    mpt->pci_cfg.LatencyTimer_LineSize, 2);
927e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(0), mpt->pci_cfg.IO_BAR, 4);
928e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(1), mpt->pci_cfg.Mem0_BAR[0], 4);
929e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(2), mpt->pci_cfg.Mem0_BAR[1], 4);
930e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(3), mpt->pci_cfg.Mem1_BAR[0], 4);
931e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(4), mpt->pci_cfg.Mem1_BAR[1], 4);
9329b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4);
9339b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1);
9349b631363SMatt Jacob 	pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4);
9359b631363SMatt Jacob }
9367104aeefSMatt Jacob 
9377104aeefSMatt Jacob static void
9387104aeefSMatt Jacob mpt_pci_intr(void *arg)
9397104aeefSMatt Jacob {
940b0a2fdeeSScott Long 	struct mpt_softc *mpt;
941b0a2fdeeSScott Long 
942b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)arg;
9437104aeefSMatt Jacob 	MPT_LOCK(mpt);
944b0a2fdeeSScott Long 	mpt_intr(mpt);
9457104aeefSMatt Jacob 	MPT_UNLOCK(mpt);
9467104aeefSMatt Jacob }
947