xref: /freebsd/sys/dev/mpt/mpt_pci.c (revision caa59dc8b8587e6511430d59aedc9a1eb5e33f2a)
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 
108a2baed97SMatt Jacob #if __FreeBSD_version < 700000
1094ccac64eSJohn Baldwin #define	pci_msix_count(x)	0
110a2baed97SMatt Jacob #define	pci_msi_count(x)	0
111a2baed97SMatt Jacob #define	pci_alloc_msi(x, y)	1
1124ccac64eSJohn Baldwin #define	pci_alloc_msix(x, y)	1
113a2baed97SMatt Jacob #define	pci_release_msi(x)	do { ; } while (0)
114a2baed97SMatt Jacob #endif
1159b631363SMatt Jacob 
1169b631363SMatt Jacob #ifndef	PCI_VENDOR_LSI
1179b631363SMatt Jacob #define	PCI_VENDOR_LSI			0x1000
1189b631363SMatt Jacob #endif
1199b631363SMatt Jacob 
1209b631363SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC909
1219b631363SMatt Jacob #define	PCI_PRODUCT_LSI_FC909		0x0620
1229b631363SMatt Jacob #endif
1239b631363SMatt Jacob 
124eae4a35fSMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC909A
125eae4a35fSMatt Jacob #define	PCI_PRODUCT_LSI_FC909A		0x0621
126eae4a35fSMatt Jacob #endif
127eae4a35fSMatt Jacob 
128aca01e38SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC919
129aca01e38SMatt Jacob #define	PCI_PRODUCT_LSI_FC919		0x0624
130aca01e38SMatt Jacob #endif
131aca01e38SMatt Jacob 
1329b631363SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC929
1339b631363SMatt Jacob #define	PCI_PRODUCT_LSI_FC929		0x0622
1349b631363SMatt Jacob #endif
1359b631363SMatt Jacob 
136519d7f81SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC929X
137519d7f81SMatt Jacob #define	PCI_PRODUCT_LSI_FC929X		0x0626
138519d7f81SMatt Jacob #endif
139519d7f81SMatt Jacob 
1405b405b07SMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC919X
1415b405b07SMatt Jacob #define	PCI_PRODUCT_LSI_FC919X		0x0628
1425b405b07SMatt Jacob #endif
1435b405b07SMatt Jacob 
1440d3baffaSMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC7X04X
1450d3baffaSMatt Jacob #define	PCI_PRODUCT_LSI_FC7X04X		0x0640
1460d3baffaSMatt Jacob #endif
1470d3baffaSMatt Jacob 
148167197aeSMatt Jacob #ifndef	PCI_PRODUCT_LSI_FC646
149167197aeSMatt Jacob #define	PCI_PRODUCT_LSI_FC646		0x0646
150167197aeSMatt Jacob #endif
151167197aeSMatt Jacob 
1529b631363SMatt Jacob #ifndef	PCI_PRODUCT_LSI_1030
1539b631363SMatt Jacob #define	PCI_PRODUCT_LSI_1030		0x0030
1549b631363SMatt Jacob #endif
1559b631363SMatt Jacob 
156444dd2b6SMatt Jacob #ifndef	PCI_PRODUCT_LSI_SAS1064
157444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1064		0x0050
158444dd2b6SMatt Jacob #endif
159444dd2b6SMatt Jacob 
160444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1064A
161444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1064A	0x005C
162444dd2b6SMatt Jacob #endif
163444dd2b6SMatt Jacob 
164444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1064E
165444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1064E	0x0056
166444dd2b6SMatt Jacob #endif
167444dd2b6SMatt Jacob 
168444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1066
169444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1066		0x005E
170444dd2b6SMatt Jacob #endif
171444dd2b6SMatt Jacob 
172444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1066E
173444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1066E	0x005A
174444dd2b6SMatt Jacob #endif
175444dd2b6SMatt Jacob 
176444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1068
177444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1068		0x0054
178444dd2b6SMatt Jacob #endif
179444dd2b6SMatt Jacob 
180444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1068E
181444dd2b6SMatt Jacob #define PCI_PRODUCT_LSI_SAS1068E	0x0058
182444dd2b6SMatt Jacob #endif
183444dd2b6SMatt Jacob 
184444dd2b6SMatt Jacob #ifndef PCI_PRODUCT_LSI_SAS1078
185079d60f0SDoug Ambrisko #define PCI_PRODUCT_LSI_SAS1078		0x0062
186444dd2b6SMatt Jacob #endif
187444dd2b6SMatt Jacob 
18818b2a6d9SXin LI #ifndef	PCI_PRODUCT_LSI_SAS1078DE
18918b2a6d9SXin LI #define	PCI_PRODUCT_LSI_SAS1078DE	0x007C
19018b2a6d9SXin LI #endif
19118b2a6d9SXin LI 
192019717e0SMatt Jacob #ifndef	PCIM_CMD_SERRESPEN
193019717e0SMatt Jacob #define	PCIM_CMD_SERRESPEN	0x0100
194019717e0SMatt Jacob #endif
195019717e0SMatt Jacob 
196b0a2fdeeSScott Long static int mpt_pci_probe(device_t);
197b0a2fdeeSScott Long static int mpt_pci_attach(device_t);
198b0a2fdeeSScott Long static void mpt_free_bus_resources(struct mpt_softc *mpt);
199b0a2fdeeSScott Long static int mpt_pci_detach(device_t);
200b0a2fdeeSScott Long static int mpt_pci_shutdown(device_t);
201b0a2fdeeSScott Long static int mpt_dma_mem_alloc(struct mpt_softc *mpt);
202b0a2fdeeSScott Long static void mpt_dma_mem_free(struct mpt_softc *mpt);
203b0a2fdeeSScott Long static void mpt_read_config_regs(struct mpt_softc *mpt);
2047104aeefSMatt Jacob static void mpt_pci_intr(void *);
2059b631363SMatt Jacob 
2069b631363SMatt Jacob static device_method_t mpt_methods[] = {
2079b631363SMatt Jacob 	/* Device interface */
208b0a2fdeeSScott Long 	DEVMETHOD(device_probe,		mpt_pci_probe),
209b0a2fdeeSScott Long 	DEVMETHOD(device_attach,	mpt_pci_attach),
210b0a2fdeeSScott Long 	DEVMETHOD(device_detach,	mpt_pci_detach),
211b0a2fdeeSScott Long 	DEVMETHOD(device_shutdown,	mpt_pci_shutdown),
2129b631363SMatt Jacob 	{ 0, 0 }
2139b631363SMatt Jacob };
2149b631363SMatt Jacob 
2159b631363SMatt Jacob static driver_t mpt_driver = {
216b0a2fdeeSScott Long 	"mpt", mpt_methods, sizeof(struct mpt_softc)
2179b631363SMatt Jacob };
2189b631363SMatt Jacob static devclass_t mpt_devclass;
2199b631363SMatt Jacob DRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0);
2205bac03f9SMatt Jacob MODULE_DEPEND(mpt, pci, 1, 1, 1);
2219b631363SMatt Jacob MODULE_VERSION(mpt, 1);
2229b631363SMatt Jacob 
2239b631363SMatt Jacob static int
224b0a2fdeeSScott Long mpt_pci_probe(device_t dev)
2259b631363SMatt Jacob {
2269b631363SMatt Jacob 	char *desc;
2279b631363SMatt Jacob 
228b4c618c0SMatt Jacob 	if (pci_get_vendor(dev) != PCI_VENDOR_LSI) {
2299b631363SMatt Jacob 		return (ENXIO);
230b4c618c0SMatt Jacob 	}
2319b631363SMatt Jacob 
2329b631363SMatt Jacob 	switch ((pci_get_device(dev) & ~1)) {
2339b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC909:
2349b631363SMatt Jacob 		desc = "LSILogic FC909 FC Adapter";
2359b631363SMatt Jacob 		break;
236eae4a35fSMatt Jacob 	case PCI_PRODUCT_LSI_FC909A:
237eae4a35fSMatt Jacob 		desc = "LSILogic FC909A FC Adapter";
238eae4a35fSMatt Jacob 		break;
239aca01e38SMatt Jacob 	case PCI_PRODUCT_LSI_FC919:
240aca01e38SMatt Jacob 		desc = "LSILogic FC919 FC Adapter";
241aca01e38SMatt Jacob 		break;
2429b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC929:
243167197aeSMatt Jacob 		desc = "Dual LSILogic FC929 FC Adapter";
2449b631363SMatt Jacob 		break;
2455b405b07SMatt Jacob 	case PCI_PRODUCT_LSI_FC919X:
246167197aeSMatt Jacob 		desc = "LSILogic FC919 FC PCI-X Adapter";
2475b405b07SMatt Jacob 		break;
248519d7f81SMatt Jacob 	case PCI_PRODUCT_LSI_FC929X:
249167197aeSMatt Jacob 		desc = "Dual LSILogic FC929X 2Gb/s FC PCI-X Adapter";
250167197aeSMatt Jacob 		break;
251167197aeSMatt Jacob 	case PCI_PRODUCT_LSI_FC646:
252167197aeSMatt Jacob 		desc = "Dual LSILogic FC7X04X 4Gb/s FC PCI-Express Adapter";
2530d3baffaSMatt Jacob 		break;
2540d3baffaSMatt Jacob 	case PCI_PRODUCT_LSI_FC7X04X:
255167197aeSMatt Jacob 		desc = "Dual LSILogic FC7X04X 4Gb/s FC PCI-X Adapter";
256519d7f81SMatt Jacob 		break;
2579b631363SMatt Jacob 	case PCI_PRODUCT_LSI_1030:
2589b631363SMatt Jacob 		desc = "LSILogic 1030 Ultra4 Adapter";
2599b631363SMatt Jacob 		break;
260444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064:
261444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064A:
262444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064E:
263444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066:
264444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066E:
265444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068:
266444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068E:
267444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1078:
26818b2a6d9SXin LI 	case PCI_PRODUCT_LSI_SAS1078DE:
269167197aeSMatt Jacob 		desc = "LSILogic SAS/SATA Adapter";
270444dd2b6SMatt Jacob 		break;
2719b631363SMatt Jacob 	default:
2729b631363SMatt Jacob 		return (ENXIO);
2739b631363SMatt Jacob 	}
2749b631363SMatt Jacob 
2759b631363SMatt Jacob 	device_set_desc(dev, desc);
276b0a2fdeeSScott Long 	return (0);
2779b631363SMatt Jacob }
2789b631363SMatt Jacob 
279c87e3f83SMatt Jacob #if	__FreeBSD_version < 500000
2809b631363SMatt Jacob static void
281b0a2fdeeSScott Long mpt_set_options(struct mpt_softc *mpt)
2829b631363SMatt Jacob {
2839b631363SMatt Jacob 	int bitmap;
2849b631363SMatt Jacob 
2859b631363SMatt Jacob 	bitmap = 0;
2869b631363SMatt Jacob 	if (getenv_int("mpt_disable", &bitmap)) {
2879b631363SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
2889b631363SMatt Jacob 			mpt->disabled = 1;
2899b631363SMatt Jacob 		}
2909b631363SMatt Jacob 	}
2919b631363SMatt Jacob 	bitmap = 0;
2929b631363SMatt Jacob 	if (getenv_int("mpt_debug", &bitmap)) {
2939b631363SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
294b0a2fdeeSScott Long 			mpt->verbose = MPT_PRT_DEBUG;
2959b631363SMatt Jacob 		}
2969b631363SMatt Jacob 	}
297c87e3f83SMatt Jacob 	bitmap = 0;
2985089bd63SMatt Jacob 	if (getenv_int("mpt_debug1", &bitmap)) {
299c87e3f83SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
3005089bd63SMatt Jacob 			mpt->verbose = MPT_PRT_DEBUG1;
301c87e3f83SMatt Jacob 		}
302c87e3f83SMatt Jacob 	}
303c87e3f83SMatt Jacob 	bitmap = 0;
3045089bd63SMatt Jacob 	if (getenv_int("mpt_debug2", &bitmap)) {
305c87e3f83SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
3065089bd63SMatt Jacob 			mpt->verbose = MPT_PRT_DEBUG2;
307c87e3f83SMatt Jacob 		}
308c87e3f83SMatt Jacob 	}
309c87e3f83SMatt Jacob 	bitmap = 0;
3105089bd63SMatt Jacob 	if (getenv_int("mpt_debug3", &bitmap)) {
311c87e3f83SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
3125089bd63SMatt Jacob 			mpt->verbose = MPT_PRT_DEBUG3;
313c87e3f83SMatt Jacob 		}
314c87e3f83SMatt Jacob 	}
315b4c618c0SMatt Jacob 
316b4c618c0SMatt Jacob 	mpt->cfg_role = MPT_ROLE_DEFAULT;
317b4c618c0SMatt Jacob 	bitmap = 0;
318b4c618c0SMatt Jacob 	if (getenv_int("mpt_nil_role", &bitmap)) {
319b4c618c0SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
320b4c618c0SMatt Jacob 			mpt->cfg_role = 0;
321b4c618c0SMatt Jacob 		}
322b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
323b4c618c0SMatt Jacob 	}
324b4c618c0SMatt Jacob 	bitmap = 0;
325b4c618c0SMatt Jacob 	if (getenv_int("mpt_tgt_role", &bitmap)) {
326b4c618c0SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
327b4c618c0SMatt Jacob 			mpt->cfg_role |= MPT_ROLE_TARGET;
328b4c618c0SMatt Jacob 		}
329b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
330b4c618c0SMatt Jacob 	}
331b4c618c0SMatt Jacob 	bitmap = 0;
332b4c618c0SMatt Jacob 	if (getenv_int("mpt_ini_role", &bitmap)) {
333b4c618c0SMatt Jacob 		if (bitmap & (1 << mpt->unit)) {
334b4c618c0SMatt Jacob 			mpt->cfg_role |= MPT_ROLE_INITIATOR;
335b4c618c0SMatt Jacob 		}
336b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
337b4c618c0SMatt Jacob 	}
338804625eeSMatt Jacob 	mpt->msi_enable = 0;
3399b631363SMatt Jacob }
3409b631363SMatt Jacob #else
3419b631363SMatt Jacob static void
342b0a2fdeeSScott Long mpt_set_options(struct mpt_softc *mpt)
3439b631363SMatt Jacob {
3449b631363SMatt Jacob 	int tval;
3459b631363SMatt Jacob 
3469b631363SMatt Jacob 	tval = 0;
3479b631363SMatt Jacob 	if (resource_int_value(device_get_name(mpt->dev),
3489b631363SMatt Jacob 	    device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) {
3499b631363SMatt Jacob 		mpt->disabled = 1;
3509b631363SMatt Jacob 	}
3519b631363SMatt Jacob 	tval = 0;
3529b631363SMatt Jacob 	if (resource_int_value(device_get_name(mpt->dev),
3539b631363SMatt Jacob 	    device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) {
35438a56edbSMatt Jacob 		mpt->verbose = tval;
3559b631363SMatt Jacob 	}
356b4c618c0SMatt Jacob 	tval = -1;
357c87e3f83SMatt Jacob 	if (resource_int_value(device_get_name(mpt->dev),
358b4c618c0SMatt Jacob 	    device_get_unit(mpt->dev), "role", &tval) == 0 && tval >= 0 &&
359c87e3f83SMatt Jacob 	    tval <= 3) {
360b4c618c0SMatt Jacob 		mpt->cfg_role = tval;
361b4c618c0SMatt Jacob 		mpt->do_cfg_role = 1;
362c87e3f83SMatt Jacob 	}
363804625eeSMatt Jacob 	tval = 0;
36496dc1a53SMatt Jacob 	mpt->msi_enable = 0;
365*caa59dc8SMarius Strobl 	if (mpt->is_sas)
366804625eeSMatt Jacob 		mpt->msi_enable = 1;
367*caa59dc8SMarius Strobl 	if (resource_int_value(device_get_name(mpt->dev),
368*caa59dc8SMarius Strobl 	    device_get_unit(mpt->dev), "msi_enable", &tval) == 0) {
369*caa59dc8SMarius Strobl 		mpt->msi_enable = tval;
370804625eeSMatt Jacob 	}
3719b631363SMatt Jacob }
3729b631363SMatt Jacob #endif
3739b631363SMatt Jacob 
374019717e0SMatt Jacob static void
375b0a2fdeeSScott Long mpt_link_peer(struct mpt_softc *mpt)
376019717e0SMatt Jacob {
377b0a2fdeeSScott Long 	struct mpt_softc *mpt2;
378019717e0SMatt Jacob 
3795089bd63SMatt Jacob 	if (mpt->unit == 0) {
380019717e0SMatt Jacob 		return;
3815089bd63SMatt Jacob 	}
382019717e0SMatt Jacob 	/*
383019717e0SMatt Jacob 	 * XXX: depends on probe order
384019717e0SMatt Jacob 	 */
385b0a2fdeeSScott Long 	mpt2 = (struct mpt_softc *)devclass_get_softc(mpt_devclass,mpt->unit-1);
386019717e0SMatt Jacob 
387019717e0SMatt Jacob 	if (mpt2 == NULL) {
388019717e0SMatt Jacob 		return;
389019717e0SMatt Jacob 	}
390019717e0SMatt Jacob 	if (pci_get_vendor(mpt2->dev) != pci_get_vendor(mpt->dev)) {
391019717e0SMatt Jacob 		return;
392019717e0SMatt Jacob 	}
393019717e0SMatt Jacob 	if (pci_get_device(mpt2->dev) != pci_get_device(mpt->dev)) {
394019717e0SMatt Jacob 		return;
395019717e0SMatt Jacob 	}
396019717e0SMatt Jacob 	mpt->mpt2 = mpt2;
397019717e0SMatt Jacob 	mpt2->mpt2 = mpt;
398b0a2fdeeSScott Long 	if (mpt->verbose >= MPT_PRT_DEBUG) {
399b0a2fdeeSScott Long 		mpt_prt(mpt, "linking with peer (mpt%d)\n",
400019717e0SMatt Jacob 		    device_get_unit(mpt2->dev));
401019717e0SMatt Jacob 	}
402019717e0SMatt Jacob }
403019717e0SMatt Jacob 
4045089bd63SMatt Jacob static void
4055089bd63SMatt Jacob mpt_unlink_peer(struct mpt_softc *mpt)
4065089bd63SMatt Jacob {
4075089bd63SMatt Jacob 	if (mpt->mpt2) {
4085089bd63SMatt Jacob 		mpt->mpt2->mpt2 = NULL;
4095089bd63SMatt Jacob 	}
4105089bd63SMatt Jacob }
4115089bd63SMatt Jacob 
4129b631363SMatt Jacob static int
413b0a2fdeeSScott Long mpt_pci_attach(device_t dev)
4149b631363SMatt Jacob {
415b0a2fdeeSScott Long 	struct mpt_softc *mpt;
4169b631363SMatt Jacob 	int		  iqd;
417b0a2fdeeSScott Long 	uint32_t	  data, cmd;
418c97bb67aSMatt Jacob 	int		  mpt_io_bar, mpt_mem_bar;
4199b631363SMatt Jacob 
4209b631363SMatt Jacob 	/* Allocate the softc structure */
421b0a2fdeeSScott Long 	mpt  = (struct mpt_softc*)device_get_softc(dev);
4229b631363SMatt Jacob 	if (mpt == NULL) {
4239b631363SMatt Jacob 		device_printf(dev, "cannot allocate softc\n");
4249b631363SMatt Jacob 		return (ENOMEM);
4259b631363SMatt Jacob 	}
4265e073106SMatt Jacob 	memset(mpt, 0, sizeof(struct mpt_softc));
4279b631363SMatt Jacob 	switch ((pci_get_device(dev) & ~1)) {
4289b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC909:
429eae4a35fSMatt Jacob 	case PCI_PRODUCT_LSI_FC909A:
430aca01e38SMatt Jacob 	case PCI_PRODUCT_LSI_FC919:
4319b631363SMatt Jacob 	case PCI_PRODUCT_LSI_FC929:
4325b405b07SMatt Jacob 	case PCI_PRODUCT_LSI_FC919X:
433167197aeSMatt Jacob 	case PCI_PRODUCT_LSI_FC646:
4340d3baffaSMatt Jacob 	case PCI_PRODUCT_LSI_FC7X04X:
4359b631363SMatt Jacob 		mpt->is_fc = 1;
4369b631363SMatt Jacob 		break;
437444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064:
438444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064A:
439444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1064E:
440444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066:
441444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1066E:
442444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068:
443444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1068E:
444444dd2b6SMatt Jacob 	case PCI_PRODUCT_LSI_SAS1078:
44518b2a6d9SXin LI 	case PCI_PRODUCT_LSI_SAS1078DE:
446444dd2b6SMatt Jacob 		mpt->is_sas = 1;
447444dd2b6SMatt Jacob 		break;
4489b631363SMatt Jacob 	default:
4495580ce96SMatt Jacob 		mpt->is_spi = 1;
4509b631363SMatt Jacob 		break;
4519b631363SMatt Jacob 	}
4529b631363SMatt Jacob 	mpt->dev = dev;
4539b631363SMatt Jacob 	mpt->unit = device_get_unit(dev);
454b0a2fdeeSScott Long 	mpt->raid_resync_rate = MPT_RAID_RESYNC_RATE_DEFAULT;
455b0a2fdeeSScott Long 	mpt->raid_mwce_setting = MPT_RAID_MWCE_DEFAULT;
456b0a2fdeeSScott Long 	mpt->raid_queue_depth = MPT_RAID_QUEUE_DEPTH_DEFAULT;
457444dd2b6SMatt Jacob 	mpt->verbose = MPT_PRT_NONE;
4585089bd63SMatt Jacob 	mpt->role = MPT_ROLE_NONE;
4598ba5efbdSMarius Strobl 	mpt->mpt_ini_id = MPT_INI_ID_NONE;
4608ba5efbdSMarius Strobl #ifdef __sparc64__
4618ba5efbdSMarius Strobl 	if (mpt->is_spi)
4628ba5efbdSMarius Strobl 		mpt->mpt_ini_id = OF_getscsinitid(dev);
4638ba5efbdSMarius Strobl #endif
4649b631363SMatt Jacob 	mpt_set_options(mpt);
465444dd2b6SMatt Jacob 	if (mpt->verbose == MPT_PRT_NONE) {
466444dd2b6SMatt Jacob 		mpt->verbose = MPT_PRT_WARN;
467444dd2b6SMatt Jacob 		/* Print INFO level (if any) if bootverbose is set */
4689b631363SMatt Jacob 		mpt->verbose += (bootverbose != 0)? 1 : 0;
469444dd2b6SMatt Jacob 	}
4709b631363SMatt Jacob 	/* Make sure memory access decoders are enabled */
4719b631363SMatt Jacob 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
4729b631363SMatt Jacob 	if ((cmd & PCIM_CMD_MEMEN) == 0) {
4739b631363SMatt Jacob 		device_printf(dev, "Memory accesses disabled");
4745089bd63SMatt Jacob 		return (ENXIO);
4759b631363SMatt Jacob 	}
4769b631363SMatt Jacob 
4779b631363SMatt Jacob 	/*
4789b631363SMatt Jacob 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
4799b631363SMatt Jacob 	 */
4809b631363SMatt Jacob 	cmd |=
4819b631363SMatt Jacob 	    PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN |
4829b631363SMatt Jacob 	    PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
4839b631363SMatt Jacob 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
4849b631363SMatt Jacob 
4859b631363SMatt Jacob 	/*
4869b631363SMatt Jacob 	 * Make sure we've disabled the ROM.
4879b631363SMatt Jacob 	 */
4889b631363SMatt Jacob 	data = pci_read_config(dev, PCIR_BIOS, 4);
489ef360048SJohn Baldwin 	data &= ~PCIM_BIOS_ENABLE;
4909b631363SMatt Jacob 	pci_write_config(dev, PCIR_BIOS, data, 4);
4919b631363SMatt Jacob 
492019717e0SMatt Jacob 	/*
493019717e0SMatt Jacob 	 * Is this part a dual?
494019717e0SMatt Jacob 	 * If so, link with our partner (around yet)
495019717e0SMatt Jacob 	 */
496019717e0SMatt Jacob 	if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929 ||
497167197aeSMatt Jacob 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC646 ||
4980d3baffaSMatt Jacob 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC7X04X ||
499019717e0SMatt Jacob 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_1030) {
500019717e0SMatt Jacob 		mpt_link_peer(mpt);
5019b631363SMatt Jacob 	}
5029b631363SMatt Jacob 
503b0a2fdeeSScott Long 	/*
504c97bb67aSMatt Jacob 	 * Figure out which are the I/O and MEM Bars
505c97bb67aSMatt Jacob 	 */
506c97bb67aSMatt Jacob 	data = pci_read_config(dev, PCIR_BAR(0), 4);
507c97bb67aSMatt Jacob 	if (PCI_BAR_IO(data)) {
508c97bb67aSMatt Jacob 		/* BAR0 is IO, BAR1 is memory */
509c97bb67aSMatt Jacob 		mpt_io_bar = 0;
510c97bb67aSMatt Jacob 		mpt_mem_bar = 1;
511c97bb67aSMatt Jacob 	} else {
512c97bb67aSMatt Jacob 		/* BAR0 is memory, BAR1 is IO */
513c97bb67aSMatt Jacob 		mpt_mem_bar = 0;
514c97bb67aSMatt Jacob 		mpt_io_bar = 1;
515c97bb67aSMatt Jacob 	}
516c97bb67aSMatt Jacob 
517c97bb67aSMatt Jacob 	/*
518b0a2fdeeSScott Long 	 * Set up register access.  PIO mode is required for
519444dd2b6SMatt Jacob 	 * certain reset operations (but must be disabled for
520444dd2b6SMatt Jacob 	 * some cards otherwise).
521b0a2fdeeSScott Long 	 */
522*caa59dc8SMarius Strobl 	mpt_io_bar = PCIR_BAR(mpt_io_bar);
523e58ed102SJohn Baldwin 	mpt->pci_pio_reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
524*caa59dc8SMarius Strobl 	    &mpt_io_bar, RF_ACTIVE);
525b0a2fdeeSScott Long 	if (mpt->pci_pio_reg == NULL) {
526b0a2fdeeSScott Long 		device_printf(dev, "unable to map registers in PIO mode\n");
5279b631363SMatt Jacob 		goto bad;
5289b631363SMatt Jacob 	}
529b0a2fdeeSScott Long 	mpt->pci_pio_st = rman_get_bustag(mpt->pci_pio_reg);
530b0a2fdeeSScott Long 	mpt->pci_pio_sh = rman_get_bushandle(mpt->pci_pio_reg);
531b0a2fdeeSScott Long 
532b0a2fdeeSScott Long 	/* Allocate kernel virtual memory for the 9x9's Mem0 region */
533*caa59dc8SMarius Strobl 	mpt_mem_bar = PCIR_BAR(mpt_mem_bar);
534e58ed102SJohn Baldwin 	mpt->pci_reg = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
535*caa59dc8SMarius Strobl 	    &mpt_mem_bar, RF_ACTIVE);
536b0a2fdeeSScott Long 	if (mpt->pci_reg == NULL) {
537b0a2fdeeSScott Long 		device_printf(dev, "Unable to memory map registers.\n");
538444dd2b6SMatt Jacob 		if (mpt->is_sas) {
539444dd2b6SMatt Jacob 			device_printf(dev, "Giving Up.\n");
540444dd2b6SMatt Jacob 			goto bad;
541444dd2b6SMatt Jacob 		}
542b0a2fdeeSScott Long 		device_printf(dev, "Falling back to PIO mode.\n");
543b0a2fdeeSScott Long 		mpt->pci_st = mpt->pci_pio_st;
544b0a2fdeeSScott Long 		mpt->pci_sh = mpt->pci_pio_sh;
545b0a2fdeeSScott Long 	} else {
5469b631363SMatt Jacob 		mpt->pci_st = rman_get_bustag(mpt->pci_reg);
5479b631363SMatt Jacob 		mpt->pci_sh = rman_get_bushandle(mpt->pci_reg);
548b0a2fdeeSScott Long 	}
5499b631363SMatt Jacob 
5509b631363SMatt Jacob 	/* Get a handle to the interrupt */
5519b631363SMatt Jacob 	iqd = 0;
5524ccac64eSJohn Baldwin 	if (mpt->msi_enable) {
5534ccac64eSJohn Baldwin 		/*
5544ccac64eSJohn Baldwin 		 * First try to alloc an MSI-X message.  If that
5554ccac64eSJohn Baldwin 		 * fails, then try to alloc an MSI message instead.
5564ccac64eSJohn Baldwin 		 */
5574ccac64eSJohn Baldwin 		if (pci_msix_count(dev) == 1) {
5584ccac64eSJohn Baldwin 			mpt->pci_msi_count = 1;
5594ccac64eSJohn Baldwin 			if (pci_alloc_msix(dev, &mpt->pci_msi_count) == 0) {
5604ccac64eSJohn Baldwin 				iqd = 1;
5614ccac64eSJohn Baldwin 			} else {
5624ccac64eSJohn Baldwin 				mpt->pci_msi_count = 0;
5634ccac64eSJohn Baldwin 			}
5644ccac64eSJohn Baldwin 		}
5654ccac64eSJohn Baldwin 		if (iqd == 0 && pci_msi_count(dev) == 1) {
566280388afSJohn Baldwin 			mpt->pci_msi_count = 1;
567804625eeSMatt Jacob 			if (pci_alloc_msi(dev, &mpt->pci_msi_count) == 0) {
568280388afSJohn Baldwin 				iqd = 1;
569804625eeSMatt Jacob 			} else {
570280388afSJohn Baldwin 				mpt->pci_msi_count = 0;
571280388afSJohn Baldwin 			}
572804625eeSMatt Jacob 		}
5734ccac64eSJohn Baldwin 	}
5745f96beb9SNate Lawson 	mpt->pci_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
575*caa59dc8SMarius Strobl 	    RF_ACTIVE | (mpt->pci_msi_count ? 0 : RF_SHAREABLE));
5769b631363SMatt Jacob 	if (mpt->pci_irq == NULL) {
5779b631363SMatt Jacob 		device_printf(dev, "could not allocate interrupt\n");
5789b631363SMatt Jacob 		goto bad;
5799b631363SMatt Jacob 	}
5809b631363SMatt Jacob 
5817104aeefSMatt Jacob 	MPT_LOCK_SETUP(mpt);
5827104aeefSMatt Jacob 
5839b631363SMatt Jacob 	/* Disable interrupts at the part */
5849b631363SMatt Jacob 	mpt_disable_ints(mpt);
5859b631363SMatt Jacob 
586b0a2fdeeSScott Long 	/* Register the interrupt handler */
5875f538376SMatt Jacob 	if (mpt_setup_intr(dev, mpt->pci_irq, MPT_IFLAGS, NULL, mpt_pci_intr,
588b0a2fdeeSScott Long 	    mpt, &mpt->ih)) {
589b0a2fdeeSScott Long 		device_printf(dev, "could not setup interrupt\n");
590b0a2fdeeSScott Long 		goto bad;
591b0a2fdeeSScott Long 	}
592b0a2fdeeSScott Long 
5939b631363SMatt Jacob 	/* Allocate dma memory */
5949b631363SMatt Jacob 	if (mpt_dma_mem_alloc(mpt)) {
5955a4c2d0eSMatt Jacob 		mpt_prt(mpt, "Could not allocate DMA memory\n");
5969b631363SMatt Jacob 		goto bad;
5979b631363SMatt Jacob 	}
5989b631363SMatt Jacob 
5997104aeefSMatt Jacob 	/*
6007104aeefSMatt Jacob 	 * Save the PCI config register values
6017104aeefSMatt Jacob  	 *
6027104aeefSMatt Jacob 	 * Hard resets are known to screw up the BAR for diagnostic
6037104aeefSMatt Jacob 	 * memory accesses (Mem1).
6047104aeefSMatt Jacob 	 *
6057104aeefSMatt Jacob 	 * Using Mem1 is known to make the chip stop responding to
6067104aeefSMatt Jacob 	 * configuration space transfers, so we need to save it now
6077104aeefSMatt Jacob 	 */
6089b631363SMatt Jacob 
6099b631363SMatt Jacob 	mpt_read_config_regs(mpt);
6109b631363SMatt Jacob 
611444dd2b6SMatt Jacob 	/*
612444dd2b6SMatt Jacob 	 * Disable PIO until we need it
613444dd2b6SMatt Jacob 	 */
6149fe6d254SMatt Jacob 	if (mpt->is_sas) {
615444dd2b6SMatt Jacob 		pci_disable_io(dev, SYS_RES_IOPORT);
6169fe6d254SMatt Jacob 	}
617444dd2b6SMatt Jacob 
6189b631363SMatt Jacob 	/* Initialize the hardware */
6199b631363SMatt Jacob 	if (mpt->disabled == 0) {
620b0a2fdeeSScott Long 		if (mpt_attach(mpt) != 0) {
6219b631363SMatt Jacob 			goto bad;
6229b631363SMatt Jacob 		}
623c87e3f83SMatt Jacob 	} else {
624c87e3f83SMatt Jacob 		mpt_prt(mpt, "device disabled at user request\n");
625c87e3f83SMatt Jacob 		goto bad;
6267104aeefSMatt Jacob 	}
6277104aeefSMatt Jacob 
628c87e3f83SMatt Jacob 	mpt->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, mpt_pci_shutdown,
629c87e3f83SMatt Jacob 	    dev, SHUTDOWN_PRI_DEFAULT);
630c87e3f83SMatt Jacob 
631c87e3f83SMatt Jacob 	if (mpt->eh == NULL) {
632c87e3f83SMatt Jacob 		mpt_prt(mpt, "shutdown event registration failed\n");
633c87e3f83SMatt Jacob 		(void) mpt_detach(mpt);
634c87e3f83SMatt Jacob 		goto bad;
635c87e3f83SMatt Jacob 	}
6369b631363SMatt Jacob 	return (0);
6379b631363SMatt Jacob 
6389b631363SMatt Jacob bad:
6399b631363SMatt Jacob 	mpt_dma_mem_free(mpt);
6409b631363SMatt Jacob 	mpt_free_bus_resources(mpt);
6415089bd63SMatt Jacob 	mpt_unlink_peer(mpt);
6425089bd63SMatt Jacob 
6435089bd63SMatt Jacob 	MPT_LOCK_DESTROY(mpt);
6449b631363SMatt Jacob 
6459b631363SMatt Jacob 	/*
6469b631363SMatt Jacob 	 * but return zero to preserve unit numbering
6479b631363SMatt Jacob 	 */
6489b631363SMatt Jacob 	return (0);
6499b631363SMatt Jacob }
6509b631363SMatt Jacob 
6517104aeefSMatt Jacob /*
6529b631363SMatt Jacob  * Free bus resources
6539b631363SMatt Jacob  */
6549b631363SMatt Jacob static void
655b0a2fdeeSScott Long mpt_free_bus_resources(struct mpt_softc *mpt)
6569b631363SMatt Jacob {
6579b631363SMatt Jacob 	if (mpt->ih) {
6589b631363SMatt Jacob 		bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih);
659*caa59dc8SMarius Strobl 		mpt->ih = NULL;
6609b631363SMatt Jacob 	}
6619b631363SMatt Jacob 
6629b631363SMatt Jacob 	if (mpt->pci_irq) {
663280388afSJohn Baldwin 		bus_release_resource(mpt->dev, SYS_RES_IRQ,
664*caa59dc8SMarius Strobl 		    rman_get_rid(mpt->pci_irq), mpt->pci_irq);
665*caa59dc8SMarius Strobl 		mpt->pci_irq = NULL;
6669b631363SMatt Jacob 	}
6679b631363SMatt Jacob 
668280388afSJohn Baldwin 	if (mpt->pci_msi_count) {
669280388afSJohn Baldwin 		pci_release_msi(mpt->dev);
670280388afSJohn Baldwin 		mpt->pci_msi_count = 0;
671280388afSJohn Baldwin 	}
672280388afSJohn Baldwin 
673b0a2fdeeSScott Long 	if (mpt->pci_pio_reg) {
674*caa59dc8SMarius Strobl 		bus_release_resource(mpt->dev, SYS_RES_IOPORT,
675*caa59dc8SMarius Strobl 		    rman_get_rid(mpt->pci_pio_reg), mpt->pci_pio_reg);
676*caa59dc8SMarius Strobl 		mpt->pci_pio_reg = NULL;
677b0a2fdeeSScott Long 	}
6789b631363SMatt Jacob 	if (mpt->pci_reg) {
679*caa59dc8SMarius Strobl 		bus_release_resource(mpt->dev, SYS_RES_MEMORY,
680*caa59dc8SMarius Strobl 		    rman_get_rid(mpt->pci_reg), mpt->pci_reg);
681*caa59dc8SMarius Strobl 		mpt->pci_reg = NULL;
6829b631363SMatt Jacob 	}
6837104aeefSMatt Jacob 	MPT_LOCK_DESTROY(mpt);
6849b631363SMatt Jacob }
6859b631363SMatt Jacob 
6867104aeefSMatt Jacob /*
6879b631363SMatt Jacob  * Disconnect ourselves from the system.
6889b631363SMatt Jacob  */
6899b631363SMatt Jacob static int
690b0a2fdeeSScott Long mpt_pci_detach(device_t dev)
6919b631363SMatt Jacob {
692b0a2fdeeSScott Long 	struct mpt_softc *mpt;
6939b631363SMatt Jacob 
694b0a2fdeeSScott Long 	mpt  = (struct mpt_softc*)device_get_softc(dev);
6959b631363SMatt Jacob 
6969b631363SMatt Jacob 	if (mpt) {
6979b631363SMatt Jacob 		mpt_disable_ints(mpt);
698b0a2fdeeSScott Long 		mpt_detach(mpt);
699b0a2fdeeSScott Long 		mpt_reset(mpt, /*reinit*/FALSE);
7009b631363SMatt Jacob 		mpt_dma_mem_free(mpt);
7019b631363SMatt Jacob 		mpt_free_bus_resources(mpt);
7021d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
7031d79ca0eSMatt Jacob 		if (mpt->eh != NULL) {
70403285072SJohn Baldwin                         EVENTHANDLER_DEREGISTER(shutdown_post_sync, mpt->eh);
7051d79ca0eSMatt Jacob 		}
7069b631363SMatt Jacob 	}
7079b631363SMatt Jacob 	return(0);
7089b631363SMatt Jacob }
7099b631363SMatt Jacob 
7107104aeefSMatt Jacob /*
7119b631363SMatt Jacob  * Disable the hardware
7129b631363SMatt Jacob  */
7139b631363SMatt Jacob static int
714b0a2fdeeSScott Long mpt_pci_shutdown(device_t dev)
7159b631363SMatt Jacob {
716b0a2fdeeSScott Long 	struct mpt_softc *mpt;
7179b631363SMatt Jacob 
718b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)device_get_softc(dev);
719c87e3f83SMatt Jacob 	if (mpt) {
720c87e3f83SMatt Jacob 		int r;
721c87e3f83SMatt Jacob 		r = mpt_shutdown(mpt);
722c87e3f83SMatt Jacob 		return (r);
723c87e3f83SMatt Jacob 	}
7249b631363SMatt Jacob 	return(0);
7259b631363SMatt Jacob }
7269b631363SMatt Jacob 
7279b631363SMatt Jacob static int
728b0a2fdeeSScott Long mpt_dma_mem_alloc(struct mpt_softc *mpt)
7299b631363SMatt Jacob {
7307dec90bcSMatt Jacob 	size_t len;
731b0a2fdeeSScott Long 	struct mpt_map_info mi;
7329b631363SMatt Jacob 
7339b631363SMatt Jacob 	/* Check if we alreay have allocated the reply memory */
734a89ec05eSPeter Wemm 	if (mpt->reply_phys != 0) {
7359b631363SMatt Jacob 		return 0;
7367dec90bcSMatt Jacob 	}
7377dec90bcSMatt Jacob 
738b0a2fdeeSScott Long 	len = sizeof (request_t) * MPT_MAX_REQUESTS(mpt);
7397dec90bcSMatt Jacob #ifdef	RELENG_4
740a163d034SWarner Losh 	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK);
7417dec90bcSMatt Jacob 	if (mpt->request_pool == NULL) {
7425a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate request pool\n");
7437dec90bcSMatt Jacob 		return (1);
7447dec90bcSMatt Jacob 	}
7455e073106SMatt Jacob 	memset(mpt->request_pool, 0, len);
7467dec90bcSMatt Jacob #else
747b0a2fdeeSScott Long 	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
7487dec90bcSMatt Jacob 	if (mpt->request_pool == NULL) {
7495a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate request pool\n");
7507dec90bcSMatt Jacob 		return (1);
7517dec90bcSMatt Jacob 	}
7527dec90bcSMatt Jacob #endif
7539b631363SMatt Jacob 
7549b631363SMatt Jacob 	/*
755444dd2b6SMatt Jacob 	 * Create a parent dma tag for this device.
7569b631363SMatt Jacob 	 *
7575a4c2d0eSMatt Jacob 	 * Align at byte boundaries,
7585a4c2d0eSMatt Jacob 	 * Limit to 32-bit addressing for request/reply queues.
7599b631363SMatt Jacob 	 */
760c686bf22SJohn Birrell 	if (mpt_dma_tag_create(mpt, /*parent*/bus_get_dma_tag(mpt->dev),
761c686bf22SJohn Birrell 	    /*alignment*/1, /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR,
762b0a2fdeeSScott Long 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
763b0a2fdeeSScott Long 	    /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
764fd435d5cSMarius Strobl 	    /*nsegments*/BUS_SPACE_UNRESTRICTED,
765fd435d5cSMarius Strobl 	    /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT, /*flags*/0,
766b0a2fdeeSScott Long 	    &mpt->parent_dmat) != 0) {
7675a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot create parent dma tag\n");
7689b631363SMatt Jacob 		return (1);
7699b631363SMatt Jacob 	}
7709b631363SMatt Jacob 
7719b631363SMatt Jacob 	/* Create a child tag for reply buffers */
7725a4c2d0eSMatt Jacob 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
7735a4c2d0eSMatt Jacob 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
774444dd2b6SMatt Jacob 	    NULL, NULL, 2 * PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0,
775b0a2fdeeSScott Long 	    &mpt->reply_dmat) != 0) {
7765a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot create a dma tag for replies\n");
7779b631363SMatt Jacob 		return (1);
7789b631363SMatt Jacob 	}
7799b631363SMatt Jacob 
7805ab13afcSMarius Strobl 	/* Allocate some DMA accessible memory for replies */
7819b631363SMatt Jacob 	if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply,
7829b631363SMatt Jacob 	    BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) {
7835a4c2d0eSMatt Jacob 		mpt_prt(mpt, "cannot allocate %lu bytes of reply memory\n",
784444dd2b6SMatt Jacob 		    (u_long) (2 * PAGE_SIZE));
7859b631363SMatt Jacob 		return (1);
7869b631363SMatt Jacob 	}
7879b631363SMatt Jacob 
788b0a2fdeeSScott Long 	mi.mpt = mpt;
789b0a2fdeeSScott Long 	mi.error = 0;
7909b631363SMatt Jacob 
7919b631363SMatt Jacob 	/* Load and lock it into "bus space" */
7929b631363SMatt Jacob 	bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply,
793444dd2b6SMatt Jacob 	    2 * PAGE_SIZE, mpt_map_rquest, &mi, 0);
7949b631363SMatt Jacob 
795b0a2fdeeSScott Long 	if (mi.error) {
7965a4c2d0eSMatt Jacob 		mpt_prt(mpt, "error %d loading dma map for DMA reply queue\n",
7975a4c2d0eSMatt Jacob 		    mi.error);
7989b631363SMatt Jacob 		return (1);
7999b631363SMatt Jacob 	}
800b0a2fdeeSScott Long 	mpt->reply_phys = mi.phys;
8019b631363SMatt Jacob 
8029b631363SMatt Jacob 	return (0);
8039b631363SMatt Jacob }
8049b631363SMatt Jacob 
8059b631363SMatt Jacob /* Deallocate memory that was allocated by mpt_dma_mem_alloc
8069b631363SMatt Jacob  */
8079b631363SMatt Jacob static void
808b0a2fdeeSScott Long mpt_dma_mem_free(struct mpt_softc *mpt)
8099b631363SMatt Jacob {
8109b631363SMatt Jacob 
8119b631363SMatt Jacob         /* Make sure we aren't double destroying */
8129b631363SMatt Jacob         if (mpt->reply_dmat == 0) {
8135a4c2d0eSMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
8149b631363SMatt Jacob 		return;
8159b631363SMatt Jacob         }
8169b631363SMatt Jacob 
8179b631363SMatt Jacob 	bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap);
8189b631363SMatt Jacob 	bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap);
8199b631363SMatt Jacob 	bus_dma_tag_destroy(mpt->reply_dmat);
8209b631363SMatt Jacob 	bus_dma_tag_destroy(mpt->parent_dmat);
821*caa59dc8SMarius Strobl 	mpt->reply_dmat = NULL;
8227dec90bcSMatt Jacob 	free(mpt->request_pool, M_DEVBUF);
823*caa59dc8SMarius Strobl 	mpt->request_pool = NULL;
8249b631363SMatt Jacob }
8259b631363SMatt Jacob 
8269b631363SMatt Jacob /* Reads modifiable (via PCI transactions) config registers */
8279b631363SMatt Jacob static void
828b0a2fdeeSScott Long mpt_read_config_regs(struct mpt_softc *mpt)
8299b631363SMatt Jacob {
8309b631363SMatt Jacob 	mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2);
8319b631363SMatt Jacob 	mpt->pci_cfg.LatencyTimer_LineSize =
8329b631363SMatt Jacob 	    pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2);
833e27951b2SJohn Baldwin 	mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_BAR(0), 4);
834e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(1), 4);
835e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(2), 4);
836e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(3), 4);
837e27951b2SJohn Baldwin 	mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(4), 4);
8389b631363SMatt Jacob 	mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4);
8399b631363SMatt Jacob 	mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1);
8409b631363SMatt Jacob 	mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4);
8419b631363SMatt Jacob }
8429b631363SMatt Jacob 
8439b631363SMatt Jacob /* Sets modifiable config registers */
8449b631363SMatt Jacob void
845b0a2fdeeSScott Long mpt_set_config_regs(struct mpt_softc *mpt)
8469b631363SMatt Jacob {
847b0a2fdeeSScott Long 	uint32_t val;
8489b631363SMatt Jacob 
8499b631363SMatt Jacob #define MPT_CHECK(reg, offset, size)					\
8509b631363SMatt Jacob 	val = pci_read_config(mpt->dev, offset, size);			\
8519b631363SMatt Jacob 	if (mpt->pci_cfg.reg != val) {					\
852301472c2SMatt Jacob 		mpt_prt(mpt,						\
8539b631363SMatt Jacob 		    "Restoring " #reg " to 0x%X from 0x%X\n",		\
8549b631363SMatt Jacob 		    mpt->pci_cfg.reg, val);				\
8559b631363SMatt Jacob 	}
8569b631363SMatt Jacob 
857b0a2fdeeSScott Long 	if (mpt->verbose >= MPT_PRT_DEBUG) {
8589b631363SMatt Jacob 		MPT_CHECK(Command, PCIR_COMMAND, 2);
8599b631363SMatt Jacob 		MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2);
860e27951b2SJohn Baldwin 		MPT_CHECK(IO_BAR, PCIR_BAR(0), 4);
861e27951b2SJohn Baldwin 		MPT_CHECK(Mem0_BAR[0], PCIR_BAR(1), 4);
862e27951b2SJohn Baldwin 		MPT_CHECK(Mem0_BAR[1], PCIR_BAR(2), 4);
863e27951b2SJohn Baldwin 		MPT_CHECK(Mem1_BAR[0], PCIR_BAR(3), 4);
864e27951b2SJohn Baldwin 		MPT_CHECK(Mem1_BAR[1], PCIR_BAR(4), 4);
8659b631363SMatt Jacob 		MPT_CHECK(ROM_BAR, PCIR_BIOS, 4);
8669b631363SMatt Jacob 		MPT_CHECK(IntLine, PCIR_INTLINE, 1);
8679b631363SMatt Jacob 		MPT_CHECK(PMCSR, 0x44, 4);
8689b631363SMatt Jacob 	}
8699b631363SMatt Jacob #undef MPT_CHECK
8709b631363SMatt Jacob 
8719b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2);
8729b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_CACHELNSZ,
8739b631363SMatt Jacob 	    mpt->pci_cfg.LatencyTimer_LineSize, 2);
874e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(0), mpt->pci_cfg.IO_BAR, 4);
875e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(1), mpt->pci_cfg.Mem0_BAR[0], 4);
876e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(2), mpt->pci_cfg.Mem0_BAR[1], 4);
877e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(3), mpt->pci_cfg.Mem1_BAR[0], 4);
878e27951b2SJohn Baldwin 	pci_write_config(mpt->dev, PCIR_BAR(4), mpt->pci_cfg.Mem1_BAR[1], 4);
8799b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4);
8809b631363SMatt Jacob 	pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1);
8819b631363SMatt Jacob 	pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4);
8829b631363SMatt Jacob }
8837104aeefSMatt Jacob 
8847104aeefSMatt Jacob static void
8857104aeefSMatt Jacob mpt_pci_intr(void *arg)
8867104aeefSMatt Jacob {
887b0a2fdeeSScott Long 	struct mpt_softc *mpt;
888b0a2fdeeSScott Long 
889b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)arg;
8907104aeefSMatt Jacob 	MPT_LOCK(mpt);
891b0a2fdeeSScott Long 	mpt_intr(mpt);
8927104aeefSMatt Jacob 	MPT_UNLOCK(mpt);
8937104aeefSMatt Jacob }
894