xref: /freebsd/sys/dev/mpt/mpt.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
1098ca2bdSWarner Losh /*-
20b80d21bSMatt Jacob  * Generic routines for LSI Fusion adapters.
39b631363SMatt Jacob  * FreeBSD Version.
49b631363SMatt Jacob  *
5*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-3-Clause
6*718cf2ccSPedro F. Giffuni  *
79b631363SMatt Jacob  * Copyright (c) 2000, 2001 by Greg Ansley
89b631363SMatt Jacob  *
99b631363SMatt Jacob  * Redistribution and use in source and binary forms, with or without
109b631363SMatt Jacob  * modification, are permitted provided that the following conditions
119b631363SMatt Jacob  * are met:
129b631363SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
139b631363SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
149b631363SMatt Jacob  *    this list of conditions, and the following disclaimer.
159b631363SMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
169b631363SMatt Jacob  *    derived from this software without specific prior written permission.
179b631363SMatt Jacob  *
189b631363SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199b631363SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209b631363SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219b631363SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
229b631363SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239b631363SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249b631363SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259b631363SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269b631363SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279b631363SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289b631363SMatt Jacob  * SUCH DAMAGE.
299b631363SMatt Jacob  */
300b80d21bSMatt Jacob /*-
310b80d21bSMatt Jacob  * Copyright (c) 2002, 2006 by Matthew Jacob
320b80d21bSMatt Jacob  * All rights reserved.
330b80d21bSMatt Jacob  *
340b80d21bSMatt Jacob  * Redistribution and use in source and binary forms, with or without
350b80d21bSMatt Jacob  * modification, are permitted provided that the following conditions are
360b80d21bSMatt Jacob  * met:
370b80d21bSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
380b80d21bSMatt Jacob  *    notice, this list of conditions and the following disclaimer.
390b80d21bSMatt Jacob  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
400b80d21bSMatt Jacob  *    substantially similar to the "NO WARRANTY" disclaimer below
410b80d21bSMatt Jacob  *    ("Disclaimer") and any redistribution must be conditioned upon including
420b80d21bSMatt Jacob  *    a substantially similar Disclaimer requirement for further binary
430b80d21bSMatt Jacob  *    redistribution.
440b80d21bSMatt Jacob  * 3. Neither the names of the above listed copyright holders nor the names
450b80d21bSMatt Jacob  *    of any contributors may be used to endorse or promote products derived
460b80d21bSMatt Jacob  *    from this software without specific prior written permission.
470b80d21bSMatt Jacob  *
480b80d21bSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
490b80d21bSMatt Jacob  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
500b80d21bSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
510b80d21bSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
520b80d21bSMatt Jacob  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
530b80d21bSMatt Jacob  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
540b80d21bSMatt Jacob  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
550b80d21bSMatt Jacob  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
560b80d21bSMatt Jacob  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
570b80d21bSMatt Jacob  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
580b80d21bSMatt Jacob  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
590b80d21bSMatt Jacob  *
600b80d21bSMatt Jacob  * Support from Chris Ellsworth in order to make SAS adapters work
610b80d21bSMatt Jacob  * is gratefully acknowledged.
62ec5fe39dSMatt Jacob  *
63ec5fe39dSMatt Jacob  *
64ec5fe39dSMatt Jacob  * Support from LSI-Logic has also gone a great deal toward making this a
65ec5fe39dSMatt Jacob  * workable subsystem and is gratefully acknowledged.
660b80d21bSMatt Jacob  */
670b80d21bSMatt Jacob /*-
68b0a2fdeeSScott Long  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
69b0a2fdeeSScott Long  * Copyright (c) 2005, WHEEL Sp. z o.o.
70b0a2fdeeSScott Long  * Copyright (c) 2004, 2005 Justin T. Gibbs
71b0a2fdeeSScott Long  * All rights reserved.
72b0a2fdeeSScott Long  *
73b0a2fdeeSScott Long  * Redistribution and use in source and binary forms, with or without
74b0a2fdeeSScott Long  * modification, are permitted provided that the following conditions are
75b0a2fdeeSScott Long  * met:
76b0a2fdeeSScott Long  * 1. Redistributions of source code must retain the above copyright
77b0a2fdeeSScott Long  *    notice, this list of conditions and the following disclaimer.
78b0a2fdeeSScott Long  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
79b0a2fdeeSScott Long  *    substantially similar to the "NO WARRANTY" disclaimer below
80b0a2fdeeSScott Long  *    ("Disclaimer") and any redistribution must be conditioned upon including
81b0a2fdeeSScott Long  *    a substantially similar Disclaimer requirement for further binary
82b0a2fdeeSScott Long  *    redistribution.
83286e947fSJustin T. Gibbs  * 3. Neither the names of the above listed copyright holders nor the names
84286e947fSJustin T. Gibbs  *    of any contributors may be used to endorse or promote products derived
85286e947fSJustin T. Gibbs  *    from this software without specific prior written permission.
86b0a2fdeeSScott Long  *
87b0a2fdeeSScott Long  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88b0a2fdeeSScott Long  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89b0a2fdeeSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90b0a2fdeeSScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
91b0a2fdeeSScott Long  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
92b0a2fdeeSScott Long  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
93b0a2fdeeSScott Long  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
94b0a2fdeeSScott Long  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
95b0a2fdeeSScott Long  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
96b0a2fdeeSScott Long  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
97b0a2fdeeSScott Long  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
989b631363SMatt Jacob  */
999b631363SMatt Jacob 
1009295c6c5SDavid E. O'Brien #include <sys/cdefs.h>
1019295c6c5SDavid E. O'Brien __FBSDID("$FreeBSD$");
1029295c6c5SDavid E. O'Brien 
103b0a2fdeeSScott Long #include <dev/mpt/mpt.h>
104b0a2fdeeSScott Long #include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */
105b0a2fdeeSScott Long #include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */
106b0a2fdeeSScott Long 
107b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi.h>
108b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_ioc.h>
109c87e3f83SMatt Jacob #include <dev/mpt/mpilib/mpi_fc.h>
110c87e3f83SMatt Jacob #include <dev/mpt/mpilib/mpi_targ.h>
111b0a2fdeeSScott Long 
112b0a2fdeeSScott Long #include <sys/sysctl.h>
1137104aeefSMatt Jacob 
1149b631363SMatt Jacob #define MPT_MAX_TRYS 3
1159b631363SMatt Jacob #define MPT_MAX_WAIT 300000
1169b631363SMatt Jacob 
1179b631363SMatt Jacob static int maxwait_ack = 0;
1189b631363SMatt Jacob static int maxwait_int = 0;
1199b631363SMatt Jacob static int maxwait_state = 0;
1209b631363SMatt Jacob 
121a7303be1SMatt Jacob static TAILQ_HEAD(, mpt_softc)	mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq);
122b0a2fdeeSScott Long mpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS];
1239b631363SMatt Jacob 
124b0a2fdeeSScott Long static mpt_reply_handler_t mpt_default_reply_handler;
125b0a2fdeeSScott Long static mpt_reply_handler_t mpt_config_reply_handler;
126b0a2fdeeSScott Long static mpt_reply_handler_t mpt_handshake_reply_handler;
127b0a2fdeeSScott Long static mpt_reply_handler_t mpt_event_reply_handler;
128b0a2fdeeSScott Long static void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
129b0a2fdeeSScott Long 			       MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context);
130444dd2b6SMatt Jacob static int mpt_send_event_request(struct mpt_softc *mpt, int onoff);
131b0a2fdeeSScott Long static int mpt_soft_reset(struct mpt_softc *mpt);
132b0a2fdeeSScott Long static void mpt_hard_reset(struct mpt_softc *mpt);
133cac33e16SMarius Strobl static int mpt_dma_buf_alloc(struct mpt_softc *mpt);
1344201341fSKenneth D. Merry static void mpt_dma_buf_free(struct mpt_softc *mpt);
1350e0521a1SMatt Jacob static int mpt_configure_ioc(struct mpt_softc *mpt, int, int);
136c87e3f83SMatt Jacob static int mpt_enable_ioc(struct mpt_softc *mpt, int);
137b0a2fdeeSScott Long 
138b0a2fdeeSScott Long /************************* Personality Module Support *************************/
139b0a2fdeeSScott Long /*
140b0a2fdeeSScott Long  * We include one extra entry that is guaranteed to be NULL
141b0a2fdeeSScott Long  * to simplify our itterator.
142b0a2fdeeSScott Long  */
143b0a2fdeeSScott Long static struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
144b0a2fdeeSScott Long static __inline struct mpt_personality*
145b0a2fdeeSScott Long 	mpt_pers_find(struct mpt_softc *, u_int);
146b0a2fdeeSScott Long static __inline struct mpt_personality*
147b0a2fdeeSScott Long 	mpt_pers_find_reverse(struct mpt_softc *, u_int);
148b0a2fdeeSScott Long 
149b0a2fdeeSScott Long static __inline struct mpt_personality *
150b0a2fdeeSScott Long mpt_pers_find(struct mpt_softc *mpt, u_int start_at)
151b0a2fdeeSScott Long {
152b0a2fdeeSScott Long 	KASSERT(start_at <= MPT_MAX_PERSONALITIES,
1534349d278SMarius Strobl 		("mpt_pers_find: starting position out of range"));
154b0a2fdeeSScott Long 
155b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
156b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
157b0a2fdeeSScott Long 		start_at++;
158b0a2fdeeSScott Long 	}
159b0a2fdeeSScott Long 	return (mpt_personalities[start_at]);
160b0a2fdeeSScott Long }
161b0a2fdeeSScott Long 
162b0a2fdeeSScott Long /*
163c87e3f83SMatt Jacob  * Used infrequently, so no need to optimize like a forward
164b0a2fdeeSScott Long  * traversal where we use the MAX+1 is guaranteed to be NULL
165b0a2fdeeSScott Long  * trick.
166b0a2fdeeSScott Long  */
167b0a2fdeeSScott Long static __inline struct mpt_personality *
168b0a2fdeeSScott Long mpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
169b0a2fdeeSScott Long {
170b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
171b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
172b0a2fdeeSScott Long 		start_at--;
173b0a2fdeeSScott Long 	}
174b0a2fdeeSScott Long 	if (start_at < MPT_MAX_PERSONALITIES)
175b0a2fdeeSScott Long 		return (mpt_personalities[start_at]);
176b0a2fdeeSScott Long 	return (NULL);
177b0a2fdeeSScott Long }
178b0a2fdeeSScott Long 
179b0a2fdeeSScott Long #define MPT_PERS_FOREACH(mpt, pers)				\
180b0a2fdeeSScott Long 	for (pers = mpt_pers_find(mpt, /*start_at*/0);		\
181b0a2fdeeSScott Long 	     pers != NULL;					\
182b0a2fdeeSScott Long 	     pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
183b0a2fdeeSScott Long 
184b0a2fdeeSScott Long #define MPT_PERS_FOREACH_REVERSE(mpt, pers)				\
185b0a2fdeeSScott Long 	for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
186b0a2fdeeSScott Long 	     pers != NULL;						\
187b0a2fdeeSScott Long 	     pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
188b0a2fdeeSScott Long 
189b0a2fdeeSScott Long static mpt_load_handler_t      mpt_stdload;
190b0a2fdeeSScott Long static mpt_probe_handler_t     mpt_stdprobe;
191b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_stdattach;
192c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_stdenable;
193a7303be1SMatt Jacob static mpt_ready_handler_t     mpt_stdready;
194b0a2fdeeSScott Long static mpt_event_handler_t     mpt_stdevent;
195b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_stdreset;
196b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_stdshutdown;
197b0a2fdeeSScott Long static mpt_detach_handler_t    mpt_stddetach;
198b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_stdunload;
199b0a2fdeeSScott Long static struct mpt_personality mpt_default_personality =
200b0a2fdeeSScott Long {
201b0a2fdeeSScott Long 	.load		= mpt_stdload,
202b0a2fdeeSScott Long 	.probe		= mpt_stdprobe,
203b0a2fdeeSScott Long 	.attach		= mpt_stdattach,
204c87e3f83SMatt Jacob 	.enable		= mpt_stdenable,
205a7303be1SMatt Jacob 	.ready		= mpt_stdready,
206b0a2fdeeSScott Long 	.event		= mpt_stdevent,
207b0a2fdeeSScott Long 	.reset		= mpt_stdreset,
208b0a2fdeeSScott Long 	.shutdown	= mpt_stdshutdown,
209b0a2fdeeSScott Long 	.detach		= mpt_stddetach,
210b0a2fdeeSScott Long 	.unload		= mpt_stdunload
211b0a2fdeeSScott Long };
212b0a2fdeeSScott Long 
213b0a2fdeeSScott Long static mpt_load_handler_t      mpt_core_load;
214b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_core_attach;
215c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_core_enable;
216b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_core_ioc_reset;
217b0a2fdeeSScott Long static mpt_event_handler_t     mpt_core_event;
218b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_shutdown;
219b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_detach;
220b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_core_unload;
221b0a2fdeeSScott Long static struct mpt_personality mpt_core_personality =
222b0a2fdeeSScott Long {
223b0a2fdeeSScott Long 	.name		= "mpt_core",
224b0a2fdeeSScott Long 	.load		= mpt_core_load,
225e955d4fdSScott Long //	.attach		= mpt_core_attach,
226e955d4fdSScott Long //	.enable		= mpt_core_enable,
227b0a2fdeeSScott Long 	.event		= mpt_core_event,
228b0a2fdeeSScott Long 	.reset		= mpt_core_ioc_reset,
229b0a2fdeeSScott Long 	.shutdown	= mpt_core_shutdown,
230b0a2fdeeSScott Long 	.detach		= mpt_core_detach,
231b0a2fdeeSScott Long 	.unload		= mpt_core_unload,
232b0a2fdeeSScott Long };
233b0a2fdeeSScott Long 
234b0a2fdeeSScott Long /*
235b0a2fdeeSScott Long  * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
236b0a2fdeeSScott Long  * ordering information.  We want the core to always register FIRST.
237b0a2fdeeSScott Long  * other modules are set to SI_ORDER_SECOND.
238b0a2fdeeSScott Long  */
239b0a2fdeeSScott Long static moduledata_t mpt_core_mod = {
240b0a2fdeeSScott Long 	"mpt_core", mpt_modevent, &mpt_core_personality
241b0a2fdeeSScott Long };
242b0a2fdeeSScott Long DECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
243b0a2fdeeSScott Long MODULE_VERSION(mpt_core, 1);
244b0a2fdeeSScott Long 
245c87e3f83SMatt Jacob #define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
246b0a2fdeeSScott Long 
247b0a2fdeeSScott Long int
248b0a2fdeeSScott Long mpt_modevent(module_t mod, int type, void *data)
249b0a2fdeeSScott Long {
250b0a2fdeeSScott Long 	struct mpt_personality *pers;
251b0a2fdeeSScott Long 	int error;
252b0a2fdeeSScott Long 
253b0a2fdeeSScott Long 	pers = (struct mpt_personality *)data;
254b0a2fdeeSScott Long 
255b0a2fdeeSScott Long 	error = 0;
256b0a2fdeeSScott Long 	switch (type) {
257b0a2fdeeSScott Long 	case MOD_LOAD:
258b0a2fdeeSScott Long 	{
259b0a2fdeeSScott Long 		mpt_load_handler_t **def_handler;
260b0a2fdeeSScott Long 		mpt_load_handler_t **pers_handler;
261b0a2fdeeSScott Long 		int i;
262b0a2fdeeSScott Long 
263b0a2fdeeSScott Long 		for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
264b0a2fdeeSScott Long 			if (mpt_personalities[i] == NULL)
265b0a2fdeeSScott Long 				break;
266b0a2fdeeSScott Long 		}
267b0a2fdeeSScott Long 		if (i >= MPT_MAX_PERSONALITIES) {
268b0a2fdeeSScott Long 			error = ENOMEM;
269b0a2fdeeSScott Long 			break;
270b0a2fdeeSScott Long 		}
271b0a2fdeeSScott Long 		pers->id = i;
272b0a2fdeeSScott Long 		mpt_personalities[i] = pers;
273b0a2fdeeSScott Long 
274b0a2fdeeSScott Long 		/* Install standard/noop handlers for any NULL entries. */
275b0a2fdeeSScott Long 		def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
276b0a2fdeeSScott Long 		pers_handler = MPT_PERS_FIRST_HANDLER(pers);
277b0a2fdeeSScott Long 		while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
278b0a2fdeeSScott Long 			if (*pers_handler == NULL)
279b0a2fdeeSScott Long 				*pers_handler = *def_handler;
280b0a2fdeeSScott Long 			pers_handler++;
281b0a2fdeeSScott Long 			def_handler++;
282b0a2fdeeSScott Long 		}
283b0a2fdeeSScott Long 
284b0a2fdeeSScott Long 		error = (pers->load(pers));
285b0a2fdeeSScott Long 		if (error != 0)
286b0a2fdeeSScott Long 			mpt_personalities[i] = NULL;
287b0a2fdeeSScott Long 		break;
288b0a2fdeeSScott Long 	}
289b0a2fdeeSScott Long 	case MOD_SHUTDOWN:
290b0a2fdeeSScott Long 		break;
291b0a2fdeeSScott Long 	case MOD_QUIESCE:
292b0a2fdeeSScott Long 		break;
293b0a2fdeeSScott Long 	case MOD_UNLOAD:
294b0a2fdeeSScott Long 		error = pers->unload(pers);
295b0a2fdeeSScott Long 		mpt_personalities[pers->id] = NULL;
296b0a2fdeeSScott Long 		break;
297b0a2fdeeSScott Long 	default:
298b0a2fdeeSScott Long 		error = EINVAL;
299b0a2fdeeSScott Long 		break;
300b0a2fdeeSScott Long 	}
301b0a2fdeeSScott Long 	return (error);
302b0a2fdeeSScott Long }
303b0a2fdeeSScott Long 
30487e255acSMarius Strobl static int
305b0a2fdeeSScott Long mpt_stdload(struct mpt_personality *pers)
306b0a2fdeeSScott Long {
30787e255acSMarius Strobl 
3085ab13afcSMarius Strobl 	/* Load is always successful. */
309b0a2fdeeSScott Long 	return (0);
310b0a2fdeeSScott Long }
311b0a2fdeeSScott Long 
31287e255acSMarius Strobl static int
313b0a2fdeeSScott Long mpt_stdprobe(struct mpt_softc *mpt)
314b0a2fdeeSScott Long {
31587e255acSMarius Strobl 
3165ab13afcSMarius Strobl 	/* Probe is always successful. */
317b0a2fdeeSScott Long 	return (0);
318b0a2fdeeSScott Long }
319b0a2fdeeSScott Long 
32087e255acSMarius Strobl static int
321b0a2fdeeSScott Long mpt_stdattach(struct mpt_softc *mpt)
322b0a2fdeeSScott Long {
32387e255acSMarius Strobl 
3245ab13afcSMarius Strobl 	/* Attach is always successful. */
325b0a2fdeeSScott Long 	return (0);
326b0a2fdeeSScott Long }
327b0a2fdeeSScott Long 
32887e255acSMarius Strobl static int
329c87e3f83SMatt Jacob mpt_stdenable(struct mpt_softc *mpt)
330c87e3f83SMatt Jacob {
33187e255acSMarius Strobl 
3325ab13afcSMarius Strobl 	/* Enable is always successful. */
333c87e3f83SMatt Jacob 	return (0);
334c87e3f83SMatt Jacob }
335c87e3f83SMatt Jacob 
33687e255acSMarius Strobl static void
337a7303be1SMatt Jacob mpt_stdready(struct mpt_softc *mpt)
338a7303be1SMatt Jacob {
33987e255acSMarius Strobl 
340a7303be1SMatt Jacob }
341a7303be1SMatt Jacob 
34287e255acSMarius Strobl static int
343444dd2b6SMatt Jacob mpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
344b0a2fdeeSScott Long {
34587e255acSMarius Strobl 
346444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
347b0a2fdeeSScott Long 	/* Event was not for us. */
348b0a2fdeeSScott Long 	return (0);
349b0a2fdeeSScott Long }
350b0a2fdeeSScott Long 
35187e255acSMarius Strobl static void
352b0a2fdeeSScott Long mpt_stdreset(struct mpt_softc *mpt, int type)
353b0a2fdeeSScott Long {
35487e255acSMarius Strobl 
355b0a2fdeeSScott Long }
356b0a2fdeeSScott Long 
35787e255acSMarius Strobl static void
358b0a2fdeeSScott Long mpt_stdshutdown(struct mpt_softc *mpt)
359b0a2fdeeSScott Long {
36087e255acSMarius Strobl 
361b0a2fdeeSScott Long }
362b0a2fdeeSScott Long 
36387e255acSMarius Strobl static void
364b0a2fdeeSScott Long mpt_stddetach(struct mpt_softc *mpt)
365b0a2fdeeSScott Long {
36687e255acSMarius Strobl 
367b0a2fdeeSScott Long }
368b0a2fdeeSScott Long 
36987e255acSMarius Strobl static int
370b0a2fdeeSScott Long mpt_stdunload(struct mpt_personality *pers)
371b0a2fdeeSScott Long {
37287e255acSMarius Strobl 
3735ab13afcSMarius Strobl 	/* Unload is always successful. */
374b0a2fdeeSScott Long 	return (0);
375b0a2fdeeSScott Long }
376b0a2fdeeSScott Long 
377a7303be1SMatt Jacob /*
378a7303be1SMatt Jacob  * Post driver attachment, we may want to perform some global actions.
379a7303be1SMatt Jacob  * Here is the hook to do so.
380a7303be1SMatt Jacob  */
381a7303be1SMatt Jacob 
382a7303be1SMatt Jacob static void
383a7303be1SMatt Jacob mpt_postattach(void *unused)
384a7303be1SMatt Jacob {
385a7303be1SMatt Jacob 	struct mpt_softc *mpt;
386a7303be1SMatt Jacob 	struct mpt_personality *pers;
387a7303be1SMatt Jacob 
388a7303be1SMatt Jacob 	TAILQ_FOREACH(mpt, &mpt_tailq, links) {
389a7303be1SMatt Jacob 		MPT_PERS_FOREACH(mpt, pers)
390a7303be1SMatt Jacob 			pers->ready(mpt);
391a7303be1SMatt Jacob 	}
392a7303be1SMatt Jacob }
393a7303be1SMatt Jacob SYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL);
394a7303be1SMatt Jacob 
395b0a2fdeeSScott Long /******************************* Bus DMA Support ******************************/
396b0a2fdeeSScott Long void
397b0a2fdeeSScott Long mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
398b0a2fdeeSScott Long {
399b0a2fdeeSScott Long 	struct mpt_map_info *map_info;
400b0a2fdeeSScott Long 
401b0a2fdeeSScott Long 	map_info = (struct mpt_map_info *)arg;
402b0a2fdeeSScott Long 	map_info->error = error;
403b0a2fdeeSScott Long 	map_info->phys = segs->ds_addr;
404b0a2fdeeSScott Long }
405b0a2fdeeSScott Long 
406b0a2fdeeSScott Long /**************************** Reply/Event Handling ****************************/
407b0a2fdeeSScott Long int
408b0a2fdeeSScott Long mpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
409b0a2fdeeSScott Long 		     mpt_handler_t handler, uint32_t *phandler_id)
410b0a2fdeeSScott Long {
411b0a2fdeeSScott Long 
412b0a2fdeeSScott Long 	switch (type) {
413b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
414b0a2fdeeSScott Long 	{
415b0a2fdeeSScott Long 		u_int cbi;
416b0a2fdeeSScott Long 		u_int free_cbi;
417b0a2fdeeSScott Long 
418b0a2fdeeSScott Long 		if (phandler_id == NULL)
419b0a2fdeeSScott Long 			return (EINVAL);
420b0a2fdeeSScott Long 
421b0a2fdeeSScott Long 		free_cbi = MPT_HANDLER_ID_NONE;
422b0a2fdeeSScott Long 		for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
423b0a2fdeeSScott Long 			/*
424b0a2fdeeSScott Long 			 * If the same handler is registered multiple
425b0a2fdeeSScott Long 			 * times, don't error out.  Just return the
426b0a2fdeeSScott Long 			 * index of the original registration.
427b0a2fdeeSScott Long 			 */
428b0a2fdeeSScott Long 			if (mpt_reply_handlers[cbi] == handler.reply_handler) {
429b0a2fdeeSScott Long 				*phandler_id = MPT_CBI_TO_HID(cbi);
430b0a2fdeeSScott Long 				return (0);
431b0a2fdeeSScott Long 			}
432b0a2fdeeSScott Long 
433b0a2fdeeSScott Long 			/*
434b0a2fdeeSScott Long 			 * Fill from the front in the hope that
435b0a2fdeeSScott Long 			 * all registered handlers consume only a
436b0a2fdeeSScott Long 			 * single cache line.
437b0a2fdeeSScott Long 			 *
438b0a2fdeeSScott Long 			 * We don't break on the first empty slot so
439b0a2fdeeSScott Long 			 * that the full table is checked to see if
440b0a2fdeeSScott Long 			 * this handler was previously registered.
441b0a2fdeeSScott Long 			 */
442c87e3f83SMatt Jacob 			if (free_cbi == MPT_HANDLER_ID_NONE &&
443c87e3f83SMatt Jacob 			    (mpt_reply_handlers[cbi]
444b0a2fdeeSScott Long 			  == mpt_default_reply_handler))
445b0a2fdeeSScott Long 				free_cbi = cbi;
446b0a2fdeeSScott Long 		}
447c87e3f83SMatt Jacob 		if (free_cbi == MPT_HANDLER_ID_NONE) {
448b0a2fdeeSScott Long 			return (ENOMEM);
449c87e3f83SMatt Jacob 		}
450b0a2fdeeSScott Long 		mpt_reply_handlers[free_cbi] = handler.reply_handler;
451b0a2fdeeSScott Long 		*phandler_id = MPT_CBI_TO_HID(free_cbi);
452b0a2fdeeSScott Long 		break;
453b0a2fdeeSScott Long 	}
454b0a2fdeeSScott Long 	default:
455b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
456b0a2fdeeSScott Long 		return (EINVAL);
457b0a2fdeeSScott Long 	}
458b0a2fdeeSScott Long 	return (0);
459b0a2fdeeSScott Long }
460b0a2fdeeSScott Long 
461b0a2fdeeSScott Long int
462b0a2fdeeSScott Long mpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
463b0a2fdeeSScott Long 		       mpt_handler_t handler, uint32_t handler_id)
464b0a2fdeeSScott Long {
465b0a2fdeeSScott Long 
466b0a2fdeeSScott Long 	switch (type) {
467b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
468b0a2fdeeSScott Long 	{
469b0a2fdeeSScott Long 		u_int cbi;
470b0a2fdeeSScott Long 
471b0a2fdeeSScott Long 		cbi = MPT_CBI(handler_id);
472b0a2fdeeSScott Long 		if (cbi >= MPT_NUM_REPLY_HANDLERS
473b0a2fdeeSScott Long 		 || mpt_reply_handlers[cbi] != handler.reply_handler)
474b0a2fdeeSScott Long 			return (ENOENT);
475b0a2fdeeSScott Long 		mpt_reply_handlers[cbi] = mpt_default_reply_handler;
476b0a2fdeeSScott Long 		break;
477b0a2fdeeSScott Long 	}
478b0a2fdeeSScott Long 	default:
479b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
480b0a2fdeeSScott Long 		return (EINVAL);
481b0a2fdeeSScott Long 	}
482b0a2fdeeSScott Long 	return (0);
483b0a2fdeeSScott Long }
484b0a2fdeeSScott Long 
485b0a2fdeeSScott Long static int
486b0a2fdeeSScott Long mpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
487c87e3f83SMatt Jacob 	uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
488b0a2fdeeSScott Long {
48987e255acSMarius Strobl 
490c87e3f83SMatt Jacob 	mpt_prt(mpt,
491c87e3f83SMatt Jacob 	    "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
492c87e3f83SMatt Jacob 	    req, req->serno, reply_desc, reply_frame);
493b0a2fdeeSScott Long 
494b0a2fdeeSScott Long 	if (reply_frame != NULL)
495b0a2fdeeSScott Long 		mpt_dump_reply_frame(mpt, reply_frame);
496b0a2fdeeSScott Long 
497c87e3f83SMatt Jacob 	mpt_prt(mpt, "Reply Frame Ignored\n");
498b0a2fdeeSScott Long 
499b0a2fdeeSScott Long 	return (/*free_reply*/TRUE);
500b0a2fdeeSScott Long }
501b0a2fdeeSScott Long 
502b0a2fdeeSScott Long static int
503b0a2fdeeSScott Long mpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
504c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
505b0a2fdeeSScott Long {
506b0a2fdeeSScott Long 
50787e255acSMarius Strobl 	if (req != NULL) {
508b0a2fdeeSScott Long 		if (reply_frame != NULL) {
509b0a2fdeeSScott Long 			MSG_CONFIG *cfgp;
510b0a2fdeeSScott Long 			MSG_CONFIG_REPLY *reply;
511b0a2fdeeSScott Long 
512b0a2fdeeSScott Long 			cfgp = (MSG_CONFIG *)req->req_vbuf;
513b0a2fdeeSScott Long 			reply = (MSG_CONFIG_REPLY *)reply_frame;
514b0a2fdeeSScott Long 			req->IOCStatus = le16toh(reply_frame->IOCStatus);
515b0a2fdeeSScott Long 			bcopy(&reply->Header, &cfgp->Header,
516b0a2fdeeSScott Long 			      sizeof(cfgp->Header));
5171d558d6aSScott Long 			cfgp->ExtPageLength = reply->ExtPageLength;
5181d558d6aSScott Long 			cfgp->ExtPageType = reply->ExtPageType;
519b0a2fdeeSScott Long 		}
520b0a2fdeeSScott Long 		req->state &= ~REQ_STATE_QUEUED;
521b0a2fdeeSScott Long 		req->state |= REQ_STATE_DONE;
522b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5235e073106SMatt Jacob 		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
524b0a2fdeeSScott Long 			wakeup(req);
5256621d786SMatt Jacob 		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
5266621d786SMatt Jacob 			/*
5276621d786SMatt Jacob 			 * Whew- we can free this request (late completion)
5286621d786SMatt Jacob 			 */
5296621d786SMatt Jacob 			mpt_free_request(mpt, req);
530b0a2fdeeSScott Long 		}
5315e073106SMatt Jacob 	}
532b0a2fdeeSScott Long 
5335e073106SMatt Jacob 	return (TRUE);
534b0a2fdeeSScott Long }
535b0a2fdeeSScott Long 
536b0a2fdeeSScott Long static int
537b0a2fdeeSScott Long mpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
538c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
539b0a2fdeeSScott Long {
54087e255acSMarius Strobl 
541b0a2fdeeSScott Long 	/* Nothing to be done. */
5425e073106SMatt Jacob 	return (TRUE);
543b0a2fdeeSScott Long }
544b0a2fdeeSScott Long 
545b0a2fdeeSScott Long static int
546b0a2fdeeSScott Long mpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
547c87e3f83SMatt Jacob     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
548b0a2fdeeSScott Long {
549b0a2fdeeSScott Long 	int free_reply;
550b0a2fdeeSScott Long 
5515e073106SMatt Jacob 	KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
5525e073106SMatt Jacob 	KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
553b0a2fdeeSScott Long 
554b0a2fdeeSScott Long 	free_reply = TRUE;
555b0a2fdeeSScott Long 	switch (reply_frame->Function) {
556b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_NOTIFICATION:
557b0a2fdeeSScott Long 	{
558b0a2fdeeSScott Long 		MSG_EVENT_NOTIFY_REPLY *msg;
559b0a2fdeeSScott Long 		struct mpt_personality *pers;
560b0a2fdeeSScott Long 		u_int handled;
561b0a2fdeeSScott Long 
562b0a2fdeeSScott Long 		handled = 0;
563b0a2fdeeSScott Long 		msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
5643f970273SJohn Birrell 		msg->EventDataLength = le16toh(msg->EventDataLength);
5653f970273SJohn Birrell 		msg->IOCStatus = le16toh(msg->IOCStatus);
5663f970273SJohn Birrell 		msg->IOCLogInfo = le32toh(msg->IOCLogInfo);
5673f970273SJohn Birrell 		msg->Event = le32toh(msg->Event);
568b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
569b0a2fdeeSScott Long 			handled += pers->event(mpt, req, msg);
570b0a2fdeeSScott Long 
571444dd2b6SMatt Jacob 		if (handled == 0 && mpt->mpt_pers_mask == 0) {
5721977cbd6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_INFO,
573444dd2b6SMatt Jacob 				"No Handlers For Any Event Notify Frames. "
574444dd2b6SMatt Jacob 				"Event %#x (ACK %sequired).\n",
575444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
576444dd2b6SMatt Jacob 		} else if (handled == 0) {
577fee67c1aSScott Long 			mpt_lprt(mpt,
578fee67c1aSScott Long 				msg->AckRequired? MPT_PRT_WARN : MPT_PRT_INFO,
579444dd2b6SMatt Jacob 				"Unhandled Event Notify Frame. Event %#x "
580444dd2b6SMatt Jacob 				"(ACK %sequired).\n",
581444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
582444dd2b6SMatt Jacob 		}
583b0a2fdeeSScott Long 
584b0a2fdeeSScott Long 		if (msg->AckRequired) {
585b0a2fdeeSScott Long 			request_t *ack_req;
586b0a2fdeeSScott Long 			uint32_t context;
587b0a2fdeeSScott Long 
5883f970273SJohn Birrell 			context = req->index | MPT_REPLY_HANDLER_EVENTS;
5895e073106SMatt Jacob 			ack_req = mpt_get_request(mpt, FALSE);
590b0a2fdeeSScott Long 			if (ack_req == NULL) {
591b0a2fdeeSScott Long 				struct mpt_evtf_record *evtf;
592b0a2fdeeSScott Long 
593b0a2fdeeSScott Long 				evtf = (struct mpt_evtf_record *)reply_frame;
594b0a2fdeeSScott Long 				evtf->context = context;
595b0a2fdeeSScott Long 				LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
596b0a2fdeeSScott Long 				free_reply = FALSE;
597b0a2fdeeSScott Long 				break;
598b0a2fdeeSScott Long 			}
599b0a2fdeeSScott Long 			mpt_send_event_ack(mpt, ack_req, msg, context);
6005e073106SMatt Jacob 			/*
6015e073106SMatt Jacob 			 * Don't check for CONTINUATION_REPLY here
6025e073106SMatt Jacob 			 */
6035e073106SMatt Jacob 			return (free_reply);
604b0a2fdeeSScott Long 		}
605b0a2fdeeSScott Long 		break;
606b0a2fdeeSScott Long 	}
607b0a2fdeeSScott Long 	case MPI_FUNCTION_PORT_ENABLE:
608b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
609b0a2fdeeSScott Long 		break;
610b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_ACK:
611b0a2fdeeSScott Long 		break;
612b0a2fdeeSScott Long 	default:
6135e073106SMatt Jacob 		mpt_prt(mpt, "unknown event function: %x\n",
614b0a2fdeeSScott Long 			reply_frame->Function);
615b0a2fdeeSScott Long 		break;
616b0a2fdeeSScott Long 	}
617b0a2fdeeSScott Long 
6185e073106SMatt Jacob 	/*
6195e073106SMatt Jacob 	 * I'm not sure that this continuation stuff works as it should.
6205e073106SMatt Jacob 	 *
6215e073106SMatt Jacob 	 * I've had FC async events occur that free the frame up because
6225e073106SMatt Jacob 	 * the continuation bit isn't set, and then additional async events
6235e073106SMatt Jacob 	 * then occur using the same context. As you might imagine, this
6245e073106SMatt Jacob 	 * leads to Very Bad Thing.
6255e073106SMatt Jacob 	 *
6265e073106SMatt Jacob 	 *  Let's just be safe for now and not free them up until we figure
6275e073106SMatt Jacob 	 * out what's actually happening here.
6285e073106SMatt Jacob 	 */
6295e073106SMatt Jacob #if	0
6305e073106SMatt Jacob 	if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
631b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
632b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
6335e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
6345e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
6355e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6365e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6375e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6385e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6395e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
640b0a2fdeeSScott Long 		}
6415e073106SMatt Jacob 	} else {
6425e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
6435e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
6445e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6455e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6465e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6475e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6485e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
6495e073106SMatt Jacob 		}
6505e073106SMatt Jacob 		mpt_prtc(mpt, "\n");
6515e073106SMatt Jacob 	}
6525e073106SMatt Jacob #endif
653b0a2fdeeSScott Long 	return (free_reply);
654b0a2fdeeSScott Long }
655b0a2fdeeSScott Long 
656b0a2fdeeSScott Long /*
657b0a2fdeeSScott Long  * Process an asynchronous event from the IOC.
658b0a2fdeeSScott Long  */
659b0a2fdeeSScott Long static int
660b0a2fdeeSScott Long mpt_core_event(struct mpt_softc *mpt, request_t *req,
661b0a2fdeeSScott Long 	       MSG_EVENT_NOTIFY_REPLY *msg)
662b0a2fdeeSScott Long {
66387e255acSMarius Strobl 
664444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
665444dd2b6SMatt Jacob                  msg->Event & 0xFF);
666b0a2fdeeSScott Long 	switch(msg->Event & 0xFF) {
667b0a2fdeeSScott Long 	case MPI_EVENT_NONE:
668b0a2fdeeSScott Long 		break;
669b0a2fdeeSScott Long 	case MPI_EVENT_LOG_DATA:
670b0a2fdeeSScott Long 	{
671b0a2fdeeSScott Long 		int i;
672b0a2fdeeSScott Long 
6735ab13afcSMarius Strobl 		/* Some error occurred that LSI wants logged */
674b0a2fdeeSScott Long 		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
675b0a2fdeeSScott Long 			msg->IOCLogInfo);
676b0a2fdeeSScott Long 		mpt_prt(mpt, "\tEvtLogData: Event Data:");
677b0a2fdeeSScott Long 		for (i = 0; i < msg->EventDataLength; i++)
678b0a2fdeeSScott Long 			mpt_prtc(mpt, "  %08x", msg->Data[i]);
679b0a2fdeeSScott Long 		mpt_prtc(mpt, "\n");
680b0a2fdeeSScott Long 		break;
681b0a2fdeeSScott Long 	}
682b0a2fdeeSScott Long 	case MPI_EVENT_EVENT_CHANGE:
683b0a2fdeeSScott Long 		/*
684b0a2fdeeSScott Long 		 * This is just an acknowledgement
685b0a2fdeeSScott Long 		 * of our mpt_send_event_request.
686b0a2fdeeSScott Long 		 */
687b0a2fdeeSScott Long 		break;
688444dd2b6SMatt Jacob 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
689444dd2b6SMatt Jacob 		break;
690b0a2fdeeSScott Long 	default:
6915e073106SMatt Jacob 		return (0);
692b0a2fdeeSScott Long 		break;
693b0a2fdeeSScott Long 	}
6945e073106SMatt Jacob 	return (1);
695b0a2fdeeSScott Long }
696b0a2fdeeSScott Long 
697b0a2fdeeSScott Long static void
698b0a2fdeeSScott Long mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
699b0a2fdeeSScott Long 		   MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
700b0a2fdeeSScott Long {
701b0a2fdeeSScott Long 	MSG_EVENT_ACK *ackp;
702b0a2fdeeSScott Long 
703b0a2fdeeSScott Long 	ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
7045e073106SMatt Jacob 	memset(ackp, 0, sizeof (*ackp));
705b0a2fdeeSScott Long 	ackp->Function = MPI_FUNCTION_EVENT_ACK;
7063f970273SJohn Birrell 	ackp->Event = htole32(msg->Event);
7073f970273SJohn Birrell 	ackp->EventContext = htole32(msg->EventContext);
7083f970273SJohn Birrell 	ackp->MsgContext = htole32(context);
709b0a2fdeeSScott Long 	mpt_check_doorbell(mpt);
710b0a2fdeeSScott Long 	mpt_send_cmd(mpt, ack_req);
711b0a2fdeeSScott Long }
712b0a2fdeeSScott Long 
713b0a2fdeeSScott Long /***************************** Interrupt Handling *****************************/
714b0a2fdeeSScott Long void
715b0a2fdeeSScott Long mpt_intr(void *arg)
716b0a2fdeeSScott Long {
717b0a2fdeeSScott Long 	struct mpt_softc *mpt;
718b0a2fdeeSScott Long 	uint32_t reply_desc;
719c87e3f83SMatt Jacob 	int ntrips = 0;
720b0a2fdeeSScott Long 
721b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)arg;
7225089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
723d0a68c27SMatt Jacob 	MPT_LOCK_ASSERT(mpt);
724d0a68c27SMatt Jacob 
725b0a2fdeeSScott Long 	while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
726b0a2fdeeSScott Long 		request_t	  *req;
727b0a2fdeeSScott Long 		MSG_DEFAULT_REPLY *reply_frame;
728b0a2fdeeSScott Long 		uint32_t	   reply_baddr;
729c87e3f83SMatt Jacob 		uint32_t           ctxt_idx;
730b0a2fdeeSScott Long 		u_int		   cb_index;
731b0a2fdeeSScott Long 		u_int		   req_index;
7326c5276c8SMarius Strobl 		u_int		   offset;
733b0a2fdeeSScott Long 		int		   free_rf;
734b0a2fdeeSScott Long 
735b0a2fdeeSScott Long 		req = NULL;
736b0a2fdeeSScott Long 		reply_frame = NULL;
737b0a2fdeeSScott Long 		reply_baddr = 0;
7386c5276c8SMarius Strobl 		offset = 0;
739b0a2fdeeSScott Long 		if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
740b0a2fdeeSScott Long 			/*
7416c5276c8SMarius Strobl 			 * Ensure that the reply frame is coherent.
742b0a2fdeeSScott Long 			 */
7435e073106SMatt Jacob 			reply_baddr = MPT_REPLY_BADDR(reply_desc);
744b0a2fdeeSScott Long 			offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
745c87e3f83SMatt Jacob 			bus_dmamap_sync_range(mpt->reply_dmat,
746c87e3f83SMatt Jacob 			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
747b0a2fdeeSScott Long 			    BUS_DMASYNC_POSTREAD);
748b0a2fdeeSScott Long 			reply_frame = MPT_REPLY_OTOV(mpt, offset);
749c87e3f83SMatt Jacob 			ctxt_idx = le32toh(reply_frame->MsgContext);
750c87e3f83SMatt Jacob 		} else {
751c87e3f83SMatt Jacob 			uint32_t type;
752b0a2fdeeSScott Long 
753c87e3f83SMatt Jacob 			type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
754c87e3f83SMatt Jacob 			ctxt_idx = reply_desc;
755c87e3f83SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
756c87e3f83SMatt Jacob 				    reply_desc);
757c87e3f83SMatt Jacob 
758c87e3f83SMatt Jacob 			switch (type) {
759c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
760c87e3f83SMatt Jacob 				ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
761c87e3f83SMatt Jacob 				break;
762c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
763c87e3f83SMatt Jacob 				ctxt_idx = GET_IO_INDEX(reply_desc);
764c87e3f83SMatt Jacob 				if (mpt->tgt_cmd_ptrs == NULL) {
765c87e3f83SMatt Jacob 					mpt_prt(mpt,
766c87e3f83SMatt Jacob 					    "mpt_intr: no target cmd ptrs\n");
767c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
768c87e3f83SMatt Jacob 					break;
769c87e3f83SMatt Jacob 				}
770c87e3f83SMatt Jacob 				if (ctxt_idx >= mpt->tgt_cmds_allocated) {
771c87e3f83SMatt Jacob 					mpt_prt(mpt,
772c87e3f83SMatt Jacob 					    "mpt_intr: bad tgt cmd ctxt %u\n",
773c87e3f83SMatt Jacob 					    ctxt_idx);
774c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
775c87e3f83SMatt Jacob 					ntrips = 1000;
776c87e3f83SMatt Jacob 					break;
777c87e3f83SMatt Jacob 				}
778c87e3f83SMatt Jacob 				req = mpt->tgt_cmd_ptrs[ctxt_idx];
779c87e3f83SMatt Jacob 				if (req == NULL) {
780c87e3f83SMatt Jacob 					mpt_prt(mpt, "no request backpointer "
781c87e3f83SMatt Jacob 					    "at index %u", ctxt_idx);
782c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
783c87e3f83SMatt Jacob 					ntrips = 1000;
784c87e3f83SMatt Jacob 					break;
785c87e3f83SMatt Jacob 				}
786c87e3f83SMatt Jacob 				/*
787c87e3f83SMatt Jacob 				 * Reformulate ctxt_idx to be just as if
788c87e3f83SMatt Jacob 				 * it were another type of context reply
789c87e3f83SMatt Jacob 				 * so the code below will find the request
790c87e3f83SMatt Jacob 				 * via indexing into the pool.
791c87e3f83SMatt Jacob 				 */
792c87e3f83SMatt Jacob 				ctxt_idx =
793c87e3f83SMatt Jacob 				    req->index | mpt->scsi_tgt_handler_id;
794c87e3f83SMatt Jacob 				req = NULL;
795c87e3f83SMatt Jacob 				break;
796c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_LAN:
797c87e3f83SMatt Jacob 				mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
798c87e3f83SMatt Jacob 				    reply_desc);
799c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
800c87e3f83SMatt Jacob 				break;
801c87e3f83SMatt Jacob 			default:
802c87e3f83SMatt Jacob 				mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
803c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
804c87e3f83SMatt Jacob 				break;
805c87e3f83SMatt Jacob 			}
806c87e3f83SMatt Jacob 			if (reply_desc == MPT_REPLY_EMPTY) {
807c87e3f83SMatt Jacob 				if (ntrips++ > 1000) {
808c87e3f83SMatt Jacob 					break;
809c87e3f83SMatt Jacob 				}
810c87e3f83SMatt Jacob 				continue;
811c87e3f83SMatt Jacob 			}
812c87e3f83SMatt Jacob 		}
813c87e3f83SMatt Jacob 
814c87e3f83SMatt Jacob 		cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
815c87e3f83SMatt Jacob 		req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
816c87e3f83SMatt Jacob 		if (req_index < MPT_MAX_REQUESTS(mpt)) {
817c87e3f83SMatt Jacob 			req = &mpt->request_pool[req_index];
8185e073106SMatt Jacob 		} else {
8195e073106SMatt Jacob 			mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
8205e073106SMatt Jacob 			    " 0x%x)\n", req_index, reply_desc);
821c87e3f83SMatt Jacob 		}
822c87e3f83SMatt Jacob 
8236c5276c8SMarius Strobl 		bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
8246c5276c8SMarius Strobl 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
825c87e3f83SMatt Jacob 		free_rf = mpt_reply_handlers[cb_index](mpt, req,
826c87e3f83SMatt Jacob 		    reply_desc, reply_frame);
827b0a2fdeeSScott Long 
8285e073106SMatt Jacob 		if (reply_frame != NULL && free_rf) {
8296c5276c8SMarius Strobl 			bus_dmamap_sync_range(mpt->reply_dmat,
8306c5276c8SMarius Strobl 			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
8316c5276c8SMarius Strobl 			    BUS_DMASYNC_PREREAD);
832b0a2fdeeSScott Long 			mpt_free_reply(mpt, reply_baddr);
8335e073106SMatt Jacob 		}
834c87e3f83SMatt Jacob 
835c87e3f83SMatt Jacob 		/*
836c87e3f83SMatt Jacob 		 * If we got ourselves disabled, don't get stuck in a loop
837c87e3f83SMatt Jacob 		 */
838c87e3f83SMatt Jacob 		if (mpt->disabled) {
839c87e3f83SMatt Jacob 			mpt_disable_ints(mpt);
840c87e3f83SMatt Jacob 			break;
841c87e3f83SMatt Jacob 		}
842c87e3f83SMatt Jacob 		if (ntrips++ > 1000) {
843c87e3f83SMatt Jacob 			break;
844c87e3f83SMatt Jacob 		}
845b0a2fdeeSScott Long 	}
8465089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
847b0a2fdeeSScott Long }
848b0a2fdeeSScott Long 
849b0a2fdeeSScott Long /******************************* Error Recovery *******************************/
850b0a2fdeeSScott Long void
851b0a2fdeeSScott Long mpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
852b0a2fdeeSScott Long 			    u_int iocstatus)
853b0a2fdeeSScott Long {
854b0a2fdeeSScott Long 	MSG_DEFAULT_REPLY  ioc_status_frame;
855b0a2fdeeSScott Long 	request_t	  *req;
856b0a2fdeeSScott Long 
8575e073106SMatt Jacob 	memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
858b0a2fdeeSScott Long 	ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
859b0a2fdeeSScott Long 	ioc_status_frame.IOCStatus = iocstatus;
860b0a2fdeeSScott Long 	while((req = TAILQ_FIRST(chain)) != NULL) {
861b0a2fdeeSScott Long 		MSG_REQUEST_HEADER *msg_hdr;
862b0a2fdeeSScott Long 		u_int		    cb_index;
8635e073106SMatt Jacob 
8646c5276c8SMarius Strobl 		bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
8656c5276c8SMarius Strobl 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
866b0a2fdeeSScott Long 		msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
867b0a2fdeeSScott Long 		ioc_status_frame.Function = msg_hdr->Function;
868b0a2fdeeSScott Long 		ioc_status_frame.MsgContext = msg_hdr->MsgContext;
869b0a2fdeeSScott Long 		cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
870c87e3f83SMatt Jacob 		mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
871c87e3f83SMatt Jacob 		    &ioc_status_frame);
87267a1357cSMarius Strobl 		if (mpt_req_on_pending_list(mpt, req) != 0)
87367a1357cSMarius Strobl 			TAILQ_REMOVE(chain, req, links);
874b0a2fdeeSScott Long 	}
875b0a2fdeeSScott Long }
876b0a2fdeeSScott Long 
877b0a2fdeeSScott Long /********************************* Diagnostics ********************************/
878b0a2fdeeSScott Long /*
879b0a2fdeeSScott Long  * Perform a diagnostic dump of a reply frame.
880b0a2fdeeSScott Long  */
881b0a2fdeeSScott Long void
882b0a2fdeeSScott Long mpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
883b0a2fdeeSScott Long {
88487e255acSMarius Strobl 
885b0a2fdeeSScott Long 	mpt_prt(mpt, "Address Reply:\n");
886b0a2fdeeSScott Long 	mpt_print_reply(reply_frame);
887b0a2fdeeSScott Long }
888b0a2fdeeSScott Long 
889b0a2fdeeSScott Long /******************************* Doorbell Access ******************************/
890b0a2fdeeSScott Long static __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
891b0a2fdeeSScott Long static __inline  uint32_t mpt_rd_intr(struct mpt_softc *mpt);
892b0a2fdeeSScott Long 
893b0a2fdeeSScott Long static __inline uint32_t
894b0a2fdeeSScott Long mpt_rd_db(struct mpt_softc *mpt)
8959b631363SMatt Jacob {
89687e255acSMarius Strobl 
8979b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_DOORBELL);
8989b631363SMatt Jacob }
8999b631363SMatt Jacob 
900b0a2fdeeSScott Long static __inline uint32_t
901b0a2fdeeSScott Long mpt_rd_intr(struct mpt_softc *mpt)
9029b631363SMatt Jacob {
90387e255acSMarius Strobl 
9049b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
9059b631363SMatt Jacob }
9069b631363SMatt Jacob 
9079b631363SMatt Jacob /* Busy wait for a door bell to be read by IOC */
9089b631363SMatt Jacob static int
909b0a2fdeeSScott Long mpt_wait_db_ack(struct mpt_softc *mpt)
9109b631363SMatt Jacob {
9119b631363SMatt Jacob 	int i;
91287e255acSMarius Strobl 
9139b631363SMatt Jacob 	for (i=0; i < MPT_MAX_WAIT; i++) {
9149b631363SMatt Jacob 		if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
9159b631363SMatt Jacob 			maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
916c87e3f83SMatt Jacob 			return (MPT_OK);
9179b631363SMatt Jacob 		}
918c87e3f83SMatt Jacob 		DELAY(200);
9199b631363SMatt Jacob 	}
920c87e3f83SMatt Jacob 	return (MPT_FAIL);
9219b631363SMatt Jacob }
9229b631363SMatt Jacob 
9239b631363SMatt Jacob /* Busy wait for a door bell interrupt */
9249b631363SMatt Jacob static int
925b0a2fdeeSScott Long mpt_wait_db_int(struct mpt_softc *mpt)
9269b631363SMatt Jacob {
9279b631363SMatt Jacob 	int i;
92887e255acSMarius Strobl 
9299b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
9309b631363SMatt Jacob 		if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
9319b631363SMatt Jacob 			maxwait_int = i > maxwait_int ? i : maxwait_int;
9329b631363SMatt Jacob 			return MPT_OK;
9339b631363SMatt Jacob 		}
9349b631363SMatt Jacob 		DELAY(100);
9359b631363SMatt Jacob 	}
936c87e3f83SMatt Jacob 	return (MPT_FAIL);
9379b631363SMatt Jacob }
9389b631363SMatt Jacob 
9399b631363SMatt Jacob /* Wait for IOC to transition to a give state */
9409b631363SMatt Jacob void
941b0a2fdeeSScott Long mpt_check_doorbell(struct mpt_softc *mpt)
9429b631363SMatt Jacob {
943b0a2fdeeSScott Long 	uint32_t db = mpt_rd_db(mpt);
94487e255acSMarius Strobl 
9459b631363SMatt Jacob 	if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
946b0a2fdeeSScott Long 		mpt_prt(mpt, "Device not running\n");
9479b631363SMatt Jacob 		mpt_print_db(db);
9489b631363SMatt Jacob 	}
9499b631363SMatt Jacob }
9509b631363SMatt Jacob 
9519b631363SMatt Jacob /* Wait for IOC to transition to a give state */
9529b631363SMatt Jacob static int
953b0a2fdeeSScott Long mpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
9549b631363SMatt Jacob {
9559b631363SMatt Jacob 	int i;
9569b631363SMatt Jacob 
9579b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
958b0a2fdeeSScott Long 		uint32_t db = mpt_rd_db(mpt);
9599b631363SMatt Jacob 		if (MPT_STATE(db) == state) {
9609b631363SMatt Jacob 			maxwait_state = i > maxwait_state ? i : maxwait_state;
9619b631363SMatt Jacob 			return (MPT_OK);
9629b631363SMatt Jacob 		}
9639b631363SMatt Jacob 		DELAY(100);
9649b631363SMatt Jacob 	}
9659b631363SMatt Jacob 	return (MPT_FAIL);
9669b631363SMatt Jacob }
9679b631363SMatt Jacob 
9689b631363SMatt Jacob 
969453130d9SPedro F. Giffuni /************************* Initialization/Configuration ************************/
970b0a2fdeeSScott Long static int mpt_download_fw(struct mpt_softc *mpt);
971b0a2fdeeSScott Long 
9729b631363SMatt Jacob /* Issue the reset COMMAND to the IOC */
973b0a2fdeeSScott Long static int
974b0a2fdeeSScott Long mpt_soft_reset(struct mpt_softc *mpt)
9759b631363SMatt Jacob {
97687e255acSMarius Strobl 
977b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
9789b631363SMatt Jacob 
9799b631363SMatt Jacob 	/* Have to use hard reset if we are not in Running state */
9809b631363SMatt Jacob 	if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
981b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device not running\n");
982c87e3f83SMatt Jacob 		return (MPT_FAIL);
9839b631363SMatt Jacob 	}
9849b631363SMatt Jacob 
9859b631363SMatt Jacob 	/* If door bell is in use we don't have a chance of getting
9869b631363SMatt Jacob 	 * a word in since the IOC probably crashed in message
9879b631363SMatt Jacob 	 * processing. So don't waste our time.
9889b631363SMatt Jacob 	 */
9899b631363SMatt Jacob 	if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
990b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
991c87e3f83SMatt Jacob 		return (MPT_FAIL);
9929b631363SMatt Jacob 	}
9939b631363SMatt Jacob 
9949b631363SMatt Jacob 	/* Send the reset request to the IOC */
9959b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL,
9969b631363SMatt Jacob 	    MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
9979b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
998b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: ack timeout\n");
999c87e3f83SMatt Jacob 		return (MPT_FAIL);
10009b631363SMatt Jacob 	}
10019b631363SMatt Jacob 
10029b631363SMatt Jacob 	/* Wait for the IOC to reload and come out of reset state */
10039b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
1004b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device did not restart\n");
1005c87e3f83SMatt Jacob 		return (MPT_FAIL);
10069b631363SMatt Jacob 	}
10079b631363SMatt Jacob 
10089b631363SMatt Jacob 	return MPT_OK;
10099b631363SMatt Jacob }
10109b631363SMatt Jacob 
1011b0a2fdeeSScott Long static int
1012b0a2fdeeSScott Long mpt_enable_diag_mode(struct mpt_softc *mpt)
1013b0a2fdeeSScott Long {
1014b0a2fdeeSScott Long 	int try;
1015b0a2fdeeSScott Long 
1016b0a2fdeeSScott Long 	try = 20;
1017b0a2fdeeSScott Long 	while (--try) {
1018b0a2fdeeSScott Long 
1019b0a2fdeeSScott Long 		if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
1020b0a2fdeeSScott Long 			break;
1021b0a2fdeeSScott Long 
1022b0a2fdeeSScott Long 		/* Enable diagnostic registers */
1023b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
1024b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
1025b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
1026b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
1027b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
1028b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
1029b0a2fdeeSScott Long 
1030b0a2fdeeSScott Long 		DELAY(100000);
1031b0a2fdeeSScott Long 	}
1032b0a2fdeeSScott Long 	if (try == 0)
1033b0a2fdeeSScott Long 		return (EIO);
1034b0a2fdeeSScott Long 	return (0);
1035b0a2fdeeSScott Long }
1036b0a2fdeeSScott Long 
1037b0a2fdeeSScott Long static void
1038b0a2fdeeSScott Long mpt_disable_diag_mode(struct mpt_softc *mpt)
1039b0a2fdeeSScott Long {
104087e255acSMarius Strobl 
1041b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
1042b0a2fdeeSScott Long }
1043b0a2fdeeSScott Long 
10449b631363SMatt Jacob /* This is a magic diagnostic reset that resets all the ARM
10459b631363SMatt Jacob  * processors in the chip.
10469b631363SMatt Jacob  */
1047b0a2fdeeSScott Long static void
1048b0a2fdeeSScott Long mpt_hard_reset(struct mpt_softc *mpt)
10499b631363SMatt Jacob {
1050b0a2fdeeSScott Long 	int error;
1051b0a2fdeeSScott Long 	int wait;
1052b0a2fdeeSScott Long 	uint32_t diagreg;
10539b631363SMatt Jacob 
1054b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
1055b0a2fdeeSScott Long 
105604016bcfSMarius Strobl 	if (mpt->is_1078) {
105704016bcfSMarius Strobl 		mpt_write(mpt, MPT_OFFSET_RESET_1078, 0x07);
105804016bcfSMarius Strobl 		DELAY(1000);
105904016bcfSMarius Strobl 		return;
106004016bcfSMarius Strobl 	}
106104016bcfSMarius Strobl 
1062b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
1063b0a2fdeeSScott Long 	if (error) {
1064b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
1065b0a2fdeeSScott Long 		mpt_prt(mpt, "Trying to reset anyway.\n");
1066b0a2fdeeSScott Long 	}
1067b0a2fdeeSScott Long 
1068b0a2fdeeSScott Long 	diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1069b0a2fdeeSScott Long 
1070b0a2fdeeSScott Long 	/*
1071b0a2fdeeSScott Long 	 * This appears to be a workaround required for some
1072b0a2fdeeSScott Long 	 * firmware or hardware revs.
1073b0a2fdeeSScott Long 	 */
1074b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1075b0a2fdeeSScott Long 	DELAY(1000);
10769b631363SMatt Jacob 
10779b631363SMatt Jacob 	/* Diag. port is now active so we can now hit the reset bit */
1078b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
10799b631363SMatt Jacob 
1080b0a2fdeeSScott Long         /*
1081b0a2fdeeSScott Long          * Ensure that the reset has finished.  We delay 1ms
1082b0a2fdeeSScott Long          * prior to reading the register to make sure the chip
1083b0a2fdeeSScott Long          * has sufficiently completed its reset to handle register
1084b0a2fdeeSScott Long          * accesses.
1085b0a2fdeeSScott Long          */
1086b0a2fdeeSScott Long 	wait = 5000;
1087b0a2fdeeSScott Long 	do {
1088b0a2fdeeSScott Long 		DELAY(1000);
1089b0a2fdeeSScott Long 		diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1090b0a2fdeeSScott Long 	} while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
10919b631363SMatt Jacob 
1092b0a2fdeeSScott Long 	if (wait == 0) {
1093b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Failed hard reset! "
1094b0a2fdeeSScott Long 			"Trying to initialize anyway.\n");
10959b631363SMatt Jacob 	}
10969b631363SMatt Jacob 
1097b0a2fdeeSScott Long 	/*
1098b0a2fdeeSScott Long 	 * If we have firmware to download, it must be loaded before
1099b0a2fdeeSScott Long 	 * the controller will become operational.  Do so now.
1100b0a2fdeeSScott Long 	 */
1101b0a2fdeeSScott Long 	if (mpt->fw_image != NULL) {
1102b0a2fdeeSScott Long 
1103b0a2fdeeSScott Long 		error = mpt_download_fw(mpt);
1104b0a2fdeeSScott Long 
1105b0a2fdeeSScott Long 		if (error) {
1106b0a2fdeeSScott Long 			mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1107b0a2fdeeSScott Long 			mpt_prt(mpt, "Trying to initialize anyway.\n");
11089b631363SMatt Jacob 		}
1109b0a2fdeeSScott Long 	}
1110b0a2fdeeSScott Long 
1111b0a2fdeeSScott Long 	/*
1112b0a2fdeeSScott Long 	 * Reseting the controller should have disabled write
1113b0a2fdeeSScott Long 	 * access to the diagnostic registers, but disable
1114b0a2fdeeSScott Long 	 * manually to be sure.
1115b0a2fdeeSScott Long 	 */
1116b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
1117b0a2fdeeSScott Long }
1118b0a2fdeeSScott Long 
1119b0a2fdeeSScott Long static void
1120b0a2fdeeSScott Long mpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1121b0a2fdeeSScott Long {
112287e255acSMarius Strobl 
1123b0a2fdeeSScott Long 	/*
1124b0a2fdeeSScott Long 	 * Complete all pending requests with a status
1125b0a2fdeeSScott Long 	 * appropriate for an IOC reset.
1126b0a2fdeeSScott Long 	 */
1127b0a2fdeeSScott Long 	mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1128b0a2fdeeSScott Long 				   MPI_IOCSTATUS_INVALID_STATE);
1129b0a2fdeeSScott Long }
1130b0a2fdeeSScott Long 
11319b631363SMatt Jacob /*
11329b631363SMatt Jacob  * Reset the IOC when needed. Try software command first then if needed
11339b631363SMatt Jacob  * poke at the magic diagnostic reset. Note that a hard reset resets
11349b631363SMatt Jacob  * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
11359b631363SMatt Jacob  * fouls up the PCI configuration registers.
11369b631363SMatt Jacob  */
11379b631363SMatt Jacob int
1138b0a2fdeeSScott Long mpt_reset(struct mpt_softc *mpt, int reinit)
11399b631363SMatt Jacob {
1140b0a2fdeeSScott Long 	struct	mpt_personality *pers;
11419b631363SMatt Jacob 	int	ret;
114229ae59edSMatt Jacob 	int	retry_cnt = 0;
11439b631363SMatt Jacob 
114429ae59edSMatt Jacob 	/*
114529ae59edSMatt Jacob 	 * Try a soft reset. If that fails, get out the big hammer.
114629ae59edSMatt Jacob 	 */
114729ae59edSMatt Jacob  again:
11489b631363SMatt Jacob 	if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
114929ae59edSMatt Jacob 		int	cnt;
115029ae59edSMatt Jacob 		for (cnt = 0; cnt < 5; cnt++) {
11519b631363SMatt Jacob 			/* Failed; do a hard reset */
11529b631363SMatt Jacob 			mpt_hard_reset(mpt);
11539b631363SMatt Jacob 
115429ae59edSMatt Jacob 			/*
115529ae59edSMatt Jacob 			 * Wait for the IOC to reload
115629ae59edSMatt Jacob 			 * and come out of reset state
115729ae59edSMatt Jacob 			 */
11589b631363SMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
115929ae59edSMatt Jacob 			if (ret == MPT_OK) {
116029ae59edSMatt Jacob 				break;
116129ae59edSMatt Jacob 			}
116229ae59edSMatt Jacob 			/*
116329ae59edSMatt Jacob 			 * Okay- try to check again...
116429ae59edSMatt Jacob 			 */
116529ae59edSMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
116629ae59edSMatt Jacob 			if (ret == MPT_OK) {
116729ae59edSMatt Jacob 				break;
116829ae59edSMatt Jacob 			}
116929ae59edSMatt Jacob 			mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
117029ae59edSMatt Jacob 			    retry_cnt, cnt);
117129ae59edSMatt Jacob 		}
11729b631363SMatt Jacob 	}
1173b0a2fdeeSScott Long 
117429ae59edSMatt Jacob 	if (retry_cnt == 0) {
1175b0a2fdeeSScott Long 		/*
1176b0a2fdeeSScott Long 		 * Invoke reset handlers.  We bump the reset count so
1177b0a2fdeeSScott Long 		 * that mpt_wait_req() understands that regardless of
1178b0a2fdeeSScott Long 		 * the specified wait condition, it should stop its wait.
1179b0a2fdeeSScott Long 		 */
1180b0a2fdeeSScott Long 		mpt->reset_cnt++;
1181b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
1182b0a2fdeeSScott Long 			pers->reset(mpt, ret);
118329ae59edSMatt Jacob 	}
1184b0a2fdeeSScott Long 
11855e073106SMatt Jacob 	if (reinit) {
1186c87e3f83SMatt Jacob 		ret = mpt_enable_ioc(mpt, 1);
118729ae59edSMatt Jacob 		if (ret == MPT_OK) {
118829ae59edSMatt Jacob 			mpt_enable_ints(mpt);
118929ae59edSMatt Jacob 		}
119029ae59edSMatt Jacob 	}
119129ae59edSMatt Jacob 	if (ret != MPT_OK && retry_cnt++ < 2) {
119229ae59edSMatt Jacob 		goto again;
119329ae59edSMatt Jacob 	}
11949b631363SMatt Jacob 	return ret;
11959b631363SMatt Jacob }
11969b631363SMatt Jacob 
11979b631363SMatt Jacob /* Return a command buffer to the free queue */
11989b631363SMatt Jacob void
1199b0a2fdeeSScott Long mpt_free_request(struct mpt_softc *mpt, request_t *req)
12009b631363SMatt Jacob {
1201444dd2b6SMatt Jacob 	request_t *nxt;
1202b0a2fdeeSScott Long 	struct mpt_evtf_record *record;
12036c5276c8SMarius Strobl 	uint32_t offset, reply_baddr;
1204b0a2fdeeSScott Long 
12057dec90bcSMatt Jacob 	if (req == NULL || req != &mpt->request_pool[req->index]) {
12064349d278SMarius Strobl 		panic("mpt_free_request: bad req ptr");
12079b631363SMatt Jacob 	}
1208444dd2b6SMatt Jacob 	if ((nxt = req->chain) != NULL) {
1209444dd2b6SMatt Jacob 		req->chain = NULL;
1210444dd2b6SMatt Jacob 		mpt_free_request(mpt, nxt);	/* NB: recursion */
1211444dd2b6SMatt Jacob 	}
12125e073106SMatt Jacob 	KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
12135e073106SMatt Jacob 	KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
1214d0a68c27SMatt Jacob 	MPT_LOCK_ASSERT(mpt);
12155089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
12165089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x already on freelist",
12175089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
12185089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
12195089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x on pending list",
12205089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
12215089bd63SMatt Jacob #ifdef	INVARIANTS
12225089bd63SMatt Jacob 	mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
12235089bd63SMatt Jacob #endif
12245e073106SMatt Jacob 
12259b631363SMatt Jacob 	req->ccb = NULL;
1226b0a2fdeeSScott Long 	if (LIST_EMPTY(&mpt->ack_frames)) {
1227c87e3f83SMatt Jacob 		/*
1228c87e3f83SMatt Jacob 		 * Insert free ones at the tail
1229c87e3f83SMatt Jacob 		 */
12305e073106SMatt Jacob 		req->serno = 0;
12315e073106SMatt Jacob 		req->state = REQ_STATE_FREE;
12325089bd63SMatt Jacob #ifdef	INVARIANTS
12335089bd63SMatt Jacob 		memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
12345089bd63SMatt Jacob #endif
1235c87e3f83SMatt Jacob 		TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1236b0a2fdeeSScott Long 		if (mpt->getreqwaiter != 0) {
1237b0a2fdeeSScott Long 			mpt->getreqwaiter = 0;
1238b0a2fdeeSScott Long 			wakeup(&mpt->request_free_list);
1239b0a2fdeeSScott Long 		}
1240b0a2fdeeSScott Long 		return;
1241b0a2fdeeSScott Long 	}
1242b0a2fdeeSScott Long 
1243b0a2fdeeSScott Long 	/*
1244b0a2fdeeSScott Long 	 * Process an ack frame deferred due to resource shortage.
1245b0a2fdeeSScott Long 	 */
1246b0a2fdeeSScott Long 	record = LIST_FIRST(&mpt->ack_frames);
1247b0a2fdeeSScott Long 	LIST_REMOVE(record, links);
12485e073106SMatt Jacob 	req->state = REQ_STATE_ALLOCATED;
12495089bd63SMatt Jacob 	mpt_assign_serno(mpt, req);
1250b0a2fdeeSScott Long 	mpt_send_event_ack(mpt, req, &record->reply, record->context);
12516c5276c8SMarius Strobl 	offset = (uint32_t)((uint8_t *)record - mpt->reply);
12526c5276c8SMarius Strobl 	reply_baddr = offset + (mpt->reply_phys & 0xFFFFFFFF);
12536c5276c8SMarius Strobl 	bus_dmamap_sync_range(mpt->reply_dmat, mpt->reply_dmap, offset,
12546c5276c8SMarius Strobl 	    MPT_REPLY_SIZE, BUS_DMASYNC_PREREAD);
1255b0a2fdeeSScott Long 	mpt_free_reply(mpt, reply_baddr);
12569b631363SMatt Jacob }
12579b631363SMatt Jacob 
12589b631363SMatt Jacob /* Get a command buffer from the free queue */
12599b631363SMatt Jacob request_t *
1260b0a2fdeeSScott Long mpt_get_request(struct mpt_softc *mpt, int sleep_ok)
12619b631363SMatt Jacob {
12629b631363SMatt Jacob 	request_t *req;
1263b0a2fdeeSScott Long 
1264b0a2fdeeSScott Long retry:
1265d0a68c27SMatt Jacob 	MPT_LOCK_ASSERT(mpt);
1266b0a2fdeeSScott Long 	req = TAILQ_FIRST(&mpt->request_free_list);
12679b631363SMatt Jacob 	if (req != NULL) {
1268b0a2fdeeSScott Long 		KASSERT(req == &mpt->request_pool[req->index],
12694349d278SMarius Strobl 		    ("mpt_get_request: corrupted request free list"));
12705e073106SMatt Jacob 		KASSERT(req->state == REQ_STATE_FREE,
12715089bd63SMatt Jacob 		    ("req %p:%u not free on free list %x index %d function %x",
12725089bd63SMatt Jacob 		    req, req->serno, req->state, req->index,
12735089bd63SMatt Jacob 		    ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1274b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_free_list, req, links);
1275b0a2fdeeSScott Long 		req->state = REQ_STATE_ALLOCATED;
1276444dd2b6SMatt Jacob 		req->chain = NULL;
12775089bd63SMatt Jacob 		mpt_assign_serno(mpt, req);
1278b0a2fdeeSScott Long 	} else if (sleep_ok != 0) {
1279b0a2fdeeSScott Long 		mpt->getreqwaiter = 1;
1280b0a2fdeeSScott Long 		mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1281b0a2fdeeSScott Long 		goto retry;
12829b631363SMatt Jacob 	}
12835e073106SMatt Jacob 	return (req);
12849b631363SMatt Jacob }
12859b631363SMatt Jacob 
12869b631363SMatt Jacob /* Pass the command to the IOC */
12879b631363SMatt Jacob void
1288b0a2fdeeSScott Long mpt_send_cmd(struct mpt_softc *mpt, request_t *req)
12899b631363SMatt Jacob {
129087e255acSMarius Strobl 
12911d79ca0eSMatt Jacob 	if (mpt->verbose > MPT_PRT_DEBUG2) {
12921043516dSMatt Jacob 		mpt_dump_request(mpt, req);
1293444dd2b6SMatt Jacob 	}
12949b631363SMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
12956c5276c8SMarius Strobl 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1296b0a2fdeeSScott Long 	req->state |= REQ_STATE_QUEUED;
12975089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
12985089bd63SMatt Jacob 	    ("req %p:%u func %x on freelist list in mpt_send_cmd",
12995089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
13005089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
13015089bd63SMatt Jacob 	    ("req %p:%u func %x already on pending list in mpt_send_cmd",
13025089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1303b0a2fdeeSScott Long 	TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1304b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
13059b631363SMatt Jacob }
13069b631363SMatt Jacob 
13079b631363SMatt Jacob /*
1308b0a2fdeeSScott Long  * Wait for a request to complete.
1309b0a2fdeeSScott Long  *
1310b0a2fdeeSScott Long  * Inputs:
1311b0a2fdeeSScott Long  *	mpt		softc of controller executing request
1312b0a2fdeeSScott Long  *	req		request to wait for
1313b0a2fdeeSScott Long  *	sleep_ok	nonzero implies may sleep in this context
1314b0a2fdeeSScott Long  *	time_ms		timeout in ms.  0 implies no timeout.
1315b0a2fdeeSScott Long  *
1316b0a2fdeeSScott Long  * Return Values:
1317b0a2fdeeSScott Long  *	0		Request completed
1318b0a2fdeeSScott Long  *	non-0		Timeout fired before request completion.
13199b631363SMatt Jacob  */
1320b0a2fdeeSScott Long int
1321b0a2fdeeSScott Long mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1322b0a2fdeeSScott Long 	     mpt_req_state_t state, mpt_req_state_t mask,
1323b0a2fdeeSScott Long 	     int sleep_ok, int time_ms)
13249b631363SMatt Jacob {
1325b0a2fdeeSScott Long 	int   timeout;
1326b0a2fdeeSScott Long 	u_int saved_cnt;
132785c9dd9dSSteven Hartland 	sbintime_t sbt;
13289b631363SMatt Jacob 
1329b0a2fdeeSScott Long 	/*
133085c9dd9dSSteven Hartland 	 * time_ms is in ms, 0 indicates infinite wait.
133185c9dd9dSSteven Hartland 	 * Convert to sbintime_t or 500us units depending on
1332b0a2fdeeSScott Long 	 * our sleep mode.
1333b0a2fdeeSScott Long 	 */
1334c87e3f83SMatt Jacob 	if (sleep_ok != 0) {
133585c9dd9dSSteven Hartland 		sbt = SBT_1MS * time_ms;
133685c9dd9dSSteven Hartland 		/* Set timeout as well so final timeout check works. */
133785c9dd9dSSteven Hartland 		timeout = time_ms;
1338c87e3f83SMatt Jacob 	} else {
13392b7b77f0SIan Lepore 		sbt = 0; /* Squelch bogus gcc warning. */
1340b0a2fdeeSScott Long 		timeout = time_ms * 2;
1341c87e3f83SMatt Jacob 	}
1342b0a2fdeeSScott Long 	req->state |= REQ_STATE_NEED_WAKEUP;
1343b0a2fdeeSScott Long 	mask &= ~REQ_STATE_NEED_WAKEUP;
1344444dd2b6SMatt Jacob 	saved_cnt = mpt->reset_cnt;
1345c87e3f83SMatt Jacob 	while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1346b0a2fdeeSScott Long 		if (sleep_ok != 0) {
134785c9dd9dSSteven Hartland 			if (mpt_sleep(mpt, req, PUSER, "mptreq", sbt) ==
134885c9dd9dSSteven Hartland 			    EWOULDBLOCK) {
1349b0a2fdeeSScott Long 				timeout = 0;
1350b0a2fdeeSScott Long 				break;
1351b0a2fdeeSScott Long 			}
1352b0a2fdeeSScott Long 		} else {
1353b0a2fdeeSScott Long 			if (time_ms != 0 && --timeout == 0) {
1354b0a2fdeeSScott Long 				break;
1355b0a2fdeeSScott Long 			}
1356b0a2fdeeSScott Long 			DELAY(500);
1357b0a2fdeeSScott Long 			mpt_intr(mpt);
1358b0a2fdeeSScott Long 		}
1359b0a2fdeeSScott Long 	}
1360b0a2fdeeSScott Long 	req->state &= ~REQ_STATE_NEED_WAKEUP;
1361c87e3f83SMatt Jacob 	if (mpt->reset_cnt != saved_cnt) {
1362b0a2fdeeSScott Long 		return (EIO);
1363c87e3f83SMatt Jacob 	}
1364c87e3f83SMatt Jacob 	if (time_ms && timeout <= 0) {
1365c87e3f83SMatt Jacob 		MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
13666621d786SMatt Jacob 		req->state |= REQ_STATE_TIMEDOUT;
1367c87e3f83SMatt Jacob 		mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1368b0a2fdeeSScott Long 		return (ETIMEDOUT);
1369c87e3f83SMatt Jacob 	}
1370b0a2fdeeSScott Long 	return (0);
13719b631363SMatt Jacob }
13729b631363SMatt Jacob 
13739b631363SMatt Jacob /*
13749b631363SMatt Jacob  * Send a command to the IOC via the handshake register.
13759b631363SMatt Jacob  *
13769b631363SMatt Jacob  * Only done at initialization time and for certain unusual
13779b631363SMatt Jacob  * commands such as device/bus reset as specified by LSI.
13789b631363SMatt Jacob  */
13799b631363SMatt Jacob int
1380b0a2fdeeSScott Long mpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
13819b631363SMatt Jacob {
13829b631363SMatt Jacob 	int i;
1383b0a2fdeeSScott Long 	uint32_t data, *data32;
13849b631363SMatt Jacob 
13859b631363SMatt Jacob 	/* Check condition of the IOC */
13869b631363SMatt Jacob 	data = mpt_rd_db(mpt);
1387b0a2fdeeSScott Long 	if ((MPT_STATE(data) != MPT_DB_STATE_READY
1388b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1389b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1390b0a2fdeeSScott Long 	 || MPT_DB_IS_IN_USE(data)) {
1391b0a2fdeeSScott Long 		mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
13929b631363SMatt Jacob 		mpt_print_db(data);
13939b631363SMatt Jacob 		return (EBUSY);
13949b631363SMatt Jacob 	}
13959b631363SMatt Jacob 
13969b631363SMatt Jacob 	/* We move things in 32 bit chunks */
13979b631363SMatt Jacob 	len = (len + 3) >> 2;
13989b631363SMatt Jacob 	data32 = cmd;
13999b631363SMatt Jacob 
1400976b0106SKevin Lo 	/* Clear any left over pending doorbell interrupts */
14019b631363SMatt Jacob 	if (MPT_DB_INTR(mpt_rd_intr(mpt)))
14029b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14039b631363SMatt Jacob 
14049b631363SMatt Jacob 	/*
14059b631363SMatt Jacob 	 * Tell the handshake reg. we are going to send a command
14069b631363SMatt Jacob          * and how long it is going to be.
14079b631363SMatt Jacob 	 */
14089b631363SMatt Jacob 	data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
14099b631363SMatt Jacob 	    (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
14109b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
14119b631363SMatt Jacob 
14129b631363SMatt Jacob 	/* Wait for the chip to notice */
14139b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1414a54067ccSMatt Jacob 		mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n");
1415b0a2fdeeSScott Long 		return (ETIMEDOUT);
14169b631363SMatt Jacob 	}
14179b631363SMatt Jacob 
14189b631363SMatt Jacob 	/* Clear the interrupt */
14199b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14209b631363SMatt Jacob 
14219b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
1422a54067ccSMatt Jacob 		mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n");
1423b0a2fdeeSScott Long 		return (ETIMEDOUT);
14249b631363SMatt Jacob 	}
14259b631363SMatt Jacob 
14269b631363SMatt Jacob 	/* Send the command */
14279b631363SMatt Jacob 	for (i = 0; i < len; i++) {
1428e18e2adaSMarius Strobl 		mpt_write_stream(mpt, MPT_OFFSET_DOORBELL, *data32++);
14299b631363SMatt Jacob 		if (mpt_wait_db_ack(mpt) != MPT_OK) {
1430301472c2SMatt Jacob 			mpt_prt(mpt,
1431a54067ccSMatt Jacob 			    "mpt_send_handshake_cmd: timeout @ index %d\n", i);
1432b0a2fdeeSScott Long 			return (ETIMEDOUT);
14339b631363SMatt Jacob 		}
14349b631363SMatt Jacob 	}
14359b631363SMatt Jacob 	return MPT_OK;
14369b631363SMatt Jacob }
14379b631363SMatt Jacob 
14389b631363SMatt Jacob /* Get the response from the handshake register */
14399b631363SMatt Jacob int
1440b0a2fdeeSScott Long mpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
14419b631363SMatt Jacob {
14429b631363SMatt Jacob 	int left, reply_left;
14439b631363SMatt Jacob 	u_int16_t *data16;
14443f970273SJohn Birrell 	uint32_t data;
14459b631363SMatt Jacob 	MSG_DEFAULT_REPLY *hdr;
14469b631363SMatt Jacob 
14479b631363SMatt Jacob 	/* We move things out in 16 bit chunks */
14489b631363SMatt Jacob 	reply_len >>= 1;
14499b631363SMatt Jacob 	data16 = (u_int16_t *)reply;
14509b631363SMatt Jacob 
14519b631363SMatt Jacob 	hdr = (MSG_DEFAULT_REPLY *)reply;
14529b631363SMatt Jacob 
14539b631363SMatt Jacob 	/* Get first word */
14549b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1455b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
14569b631363SMatt Jacob 		return ETIMEDOUT;
14579b631363SMatt Jacob 	}
14583f970273SJohn Birrell 	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14593f970273SJohn Birrell 	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14609b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14619b631363SMatt Jacob 
1462e18e2adaSMarius Strobl 	/* Get second word */
14639b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1464b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
14659b631363SMatt Jacob 		return ETIMEDOUT;
14669b631363SMatt Jacob 	}
14673f970273SJohn Birrell 	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14683f970273SJohn Birrell 	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14699b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14709b631363SMatt Jacob 
14715e073106SMatt Jacob 	/*
14725e073106SMatt Jacob 	 * With the second word, we can now look at the length.
14735e073106SMatt Jacob 	 * Warn about a reply that's too short (except for IOC FACTS REPLY)
14745e073106SMatt Jacob 	 */
14755e073106SMatt Jacob 	if ((reply_len >> 1) != hdr->MsgLength &&
14765e073106SMatt Jacob 	    (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
1477301472c2SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
147833f31846SMatt Jacob 			"got %x; expected %zx for function %x\n",
14795e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14809b631363SMatt Jacob 	}
14819b631363SMatt Jacob 
14829b631363SMatt Jacob 	/* Get rest of the reply; but don't overflow the provided buffer */
14839b631363SMatt Jacob 	left = (hdr->MsgLength << 1) - 2;
14849b631363SMatt Jacob 	reply_left =  reply_len - 2;
14859b631363SMatt Jacob 	while (left--) {
14869b631363SMatt Jacob 		if (mpt_wait_db_int(mpt) != MPT_OK) {
1487b0a2fdeeSScott Long 			mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
14889b631363SMatt Jacob 			return ETIMEDOUT;
14899b631363SMatt Jacob 		}
14903f970273SJohn Birrell 		data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14919b631363SMatt Jacob 		if (reply_left-- > 0)
1492e18e2adaSMarius Strobl 			*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14939b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14949b631363SMatt Jacob 	}
14959b631363SMatt Jacob 
14969b631363SMatt Jacob 	/* One more wait & clear at the end */
14979b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1498b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
14999b631363SMatt Jacob 		return ETIMEDOUT;
15009b631363SMatt Jacob 	}
15019b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
15029b631363SMatt Jacob 
15039b631363SMatt Jacob 	if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1504b0a2fdeeSScott Long 		if (mpt->verbose >= MPT_PRT_TRACE)
15059b631363SMatt Jacob 			mpt_print_reply(hdr);
15069b631363SMatt Jacob 		return (MPT_FAIL | hdr->IOCStatus);
15079b631363SMatt Jacob 	}
15089b631363SMatt Jacob 
15099b631363SMatt Jacob 	return (0);
15109b631363SMatt Jacob }
15119b631363SMatt Jacob 
15129b631363SMatt Jacob static int
1513b0a2fdeeSScott Long mpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
15149b631363SMatt Jacob {
15159b631363SMatt Jacob 	MSG_IOC_FACTS f_req;
15169b631363SMatt Jacob 	int error;
15179b631363SMatt Jacob 
15185e073106SMatt Jacob 	memset(&f_req, 0, sizeof f_req);
15199b631363SMatt Jacob 	f_req.Function = MPI_FUNCTION_IOC_FACTS;
1520b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15219b631363SMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
15220e0521a1SMatt Jacob 	if (error) {
15239b631363SMatt Jacob 		return(error);
15240e0521a1SMatt Jacob 	}
15259b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
15269b631363SMatt Jacob 	return (error);
15279b631363SMatt Jacob }
15289b631363SMatt Jacob 
15297104aeefSMatt Jacob static int
15300e0521a1SMatt Jacob mpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp)
15317104aeefSMatt Jacob {
15327104aeefSMatt Jacob 	MSG_PORT_FACTS f_req;
15337104aeefSMatt Jacob 	int error;
15347104aeefSMatt Jacob 
1535b0a2fdeeSScott Long 	memset(&f_req, 0, sizeof f_req);
15367104aeefSMatt Jacob 	f_req.Function = MPI_FUNCTION_PORT_FACTS;
15370e0521a1SMatt Jacob 	f_req.PortNumber = port;
1538b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15397104aeefSMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
15400e0521a1SMatt Jacob 	if (error) {
15417104aeefSMatt Jacob 		return(error);
15420e0521a1SMatt Jacob 	}
15437104aeefSMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
15447104aeefSMatt Jacob 	return (error);
15457104aeefSMatt Jacob }
15467104aeefSMatt Jacob 
15479b631363SMatt Jacob /*
15489b631363SMatt Jacob  * Send the initialization request. This is where we specify how many
1549db4fcadfSConrad Meyer  * SCSI buses and how many devices per bus we wish to emulate.
15509b631363SMatt Jacob  * This is also the command that specifies the max size of the reply
15519b631363SMatt Jacob  * frames from the IOC that we will be allocating.
15529b631363SMatt Jacob  */
15539b631363SMatt Jacob static int
1554b0a2fdeeSScott Long mpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
15559b631363SMatt Jacob {
15569b631363SMatt Jacob 	int error = 0;
15579b631363SMatt Jacob 	MSG_IOC_INIT init;
15589b631363SMatt Jacob 	MSG_IOC_INIT_REPLY reply;
15599b631363SMatt Jacob 
15605e073106SMatt Jacob 	memset(&init, 0, sizeof init);
15619b631363SMatt Jacob 	init.WhoInit = who;
15629b631363SMatt Jacob 	init.Function = MPI_FUNCTION_IOC_INIT;
15630e0521a1SMatt Jacob 	init.MaxDevices = 0;	/* at least 256 devices per bus */
1564db4fcadfSConrad Meyer 	init.MaxBuses = 16;	/* at least 16 buses */
1565444dd2b6SMatt Jacob 
1566444dd2b6SMatt Jacob 	init.MsgVersion = htole16(MPI_VERSION);
1567444dd2b6SMatt Jacob 	init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1568444dd2b6SMatt Jacob 	init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1569b0a2fdeeSScott Long 	init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15709b631363SMatt Jacob 
15719b631363SMatt Jacob 	if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
15729b631363SMatt Jacob 		return(error);
15739b631363SMatt Jacob 	}
15749b631363SMatt Jacob 
15759b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
15769b631363SMatt Jacob 	return (error);
15779b631363SMatt Jacob }
15789b631363SMatt Jacob 
15797104aeefSMatt Jacob 
15807104aeefSMatt Jacob /*
15817104aeefSMatt Jacob  * Utiltity routine to read configuration headers and pages
15827104aeefSMatt Jacob  */
1583b0a2fdeeSScott Long int
15841d558d6aSScott Long mpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, cfgparms_t *params,
15851d558d6aSScott Long 		  bus_addr_t addr, bus_size_t len, int sleep_ok, int timeout_ms)
15867104aeefSMatt Jacob {
15877104aeefSMatt Jacob 	MSG_CONFIG *cfgp;
1588b0a2fdeeSScott Long 	SGE_SIMPLE32 *se;
15897104aeefSMatt Jacob 
15907104aeefSMatt Jacob 	cfgp = req->req_vbuf;
1591b0a2fdeeSScott Long 	memset(cfgp, 0, sizeof *cfgp);
15921d558d6aSScott Long 	cfgp->Action = params->Action;
15937104aeefSMatt Jacob 	cfgp->Function = MPI_FUNCTION_CONFIG;
15941d558d6aSScott Long 	cfgp->Header.PageVersion = params->PageVersion;
15951d558d6aSScott Long 	cfgp->Header.PageNumber = params->PageNumber;
15961d558d6aSScott Long 	cfgp->PageAddress = htole32(params->PageAddress);
15971d558d6aSScott Long 	if ((params->PageType & MPI_CONFIG_PAGETYPE_MASK) ==
15981d558d6aSScott Long 	    MPI_CONFIG_PAGETYPE_EXTENDED) {
15991d558d6aSScott Long 		cfgp->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
16001d558d6aSScott Long 		cfgp->Header.PageLength = 0;
16011d558d6aSScott Long 		cfgp->ExtPageLength = htole16(params->ExtPageLength);
16021d558d6aSScott Long 		cfgp->ExtPageType = params->ExtPageType;
16031d558d6aSScott Long 	} else {
16041d558d6aSScott Long 		cfgp->Header.PageType = params->PageType;
16051d558d6aSScott Long 		cfgp->Header.PageLength = params->PageLength;
16061d558d6aSScott Long 	}
1607b0a2fdeeSScott Long 	se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
16083f970273SJohn Birrell 	se->Address = htole32(addr);
1609b0a2fdeeSScott Long 	MPI_pSGE_SET_LENGTH(se, len);
1610b0a2fdeeSScott Long 	MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1611b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1612b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_END_OF_LIST |
16131d558d6aSScott Long 	    ((params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
16141d558d6aSScott Long 	  || params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1615b0a2fdeeSScott Long 	   ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
16163f970273SJohn Birrell 	se->FlagsLength = htole32(se->FlagsLength);
1617b0a2fdeeSScott Long 	cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
16187104aeefSMatt Jacob 
16197104aeefSMatt Jacob 	mpt_check_doorbell(mpt);
16207104aeefSMatt Jacob 	mpt_send_cmd(mpt, req);
1621b0a2fdeeSScott Long 	return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1622b0a2fdeeSScott Long 			     sleep_ok, timeout_ms));
16237104aeefSMatt Jacob }
16247104aeefSMatt Jacob 
16251d558d6aSScott Long int
16261d558d6aSScott Long mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion, int PageNumber,
16271d558d6aSScott Long 		       uint32_t PageAddress, int ExtPageType,
16281d558d6aSScott Long 		       CONFIG_EXTENDED_PAGE_HEADER *rslt,
16291d558d6aSScott Long 		       int sleep_ok, int timeout_ms)
16301d558d6aSScott Long {
16311d558d6aSScott Long 	request_t  *req;
16321d558d6aSScott Long 	cfgparms_t params;
16331d558d6aSScott Long 	MSG_CONFIG_REPLY *cfgp;
16341d558d6aSScott Long 	int	    error;
16351d558d6aSScott Long 
16361d558d6aSScott Long 	req = mpt_get_request(mpt, sleep_ok);
16371d558d6aSScott Long 	if (req == NULL) {
16381d558d6aSScott Long 		mpt_prt(mpt, "mpt_extread_cfg_header: Get request failed!\n");
16391d558d6aSScott Long 		return (ENOMEM);
16401d558d6aSScott Long 	}
16411d558d6aSScott Long 
16421d558d6aSScott Long 	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
16431d558d6aSScott Long 	params.PageVersion = PageVersion;
16441d558d6aSScott Long 	params.PageLength = 0;
16451d558d6aSScott Long 	params.PageNumber = PageNumber;
16461d558d6aSScott Long 	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
16471d558d6aSScott Long 	params.PageAddress = PageAddress;
16481d558d6aSScott Long 	params.ExtPageType = ExtPageType;
16491d558d6aSScott Long 	params.ExtPageLength = 0;
16501d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
16511d558d6aSScott Long 				  sleep_ok, timeout_ms);
16521d558d6aSScott Long 	if (error != 0) {
16531d558d6aSScott Long 		/*
16541d558d6aSScott Long 		 * Leave the request. Without resetting the chip, it's
16551d558d6aSScott Long 		 * still owned by it and we'll just get into trouble
16561d558d6aSScott Long 		 * freeing it now. Mark it as abandoned so that if it
16571d558d6aSScott Long 		 * shows up later it can be freed.
16581d558d6aSScott Long 		 */
16591d558d6aSScott Long 		mpt_prt(mpt, "read_extcfg_header timed out\n");
16601d558d6aSScott Long 		return (ETIMEDOUT);
16611d558d6aSScott Long 	}
16621d558d6aSScott Long 
16631d558d6aSScott Long         switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
16641d558d6aSScott Long 	case MPI_IOCSTATUS_SUCCESS:
16651d558d6aSScott Long 		cfgp = req->req_vbuf;
16661d558d6aSScott Long 		rslt->PageVersion = cfgp->Header.PageVersion;
16671d558d6aSScott Long 		rslt->PageNumber = cfgp->Header.PageNumber;
16681d558d6aSScott Long 		rslt->PageType = cfgp->Header.PageType;
16697ee37807SMarius Strobl 		rslt->ExtPageLength = le16toh(cfgp->ExtPageLength);
16701d558d6aSScott Long 		rslt->ExtPageType = cfgp->ExtPageType;
16711d558d6aSScott Long 		error = 0;
16721d558d6aSScott Long 		break;
16731d558d6aSScott Long 	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
16741d558d6aSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG,
16751d558d6aSScott Long 		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
16761d558d6aSScott Long 		    MPI_CONFIG_PAGETYPE_EXTENDED, PageNumber, PageAddress);
16771d558d6aSScott Long 		error = EINVAL;
16781d558d6aSScott Long 		break;
16791d558d6aSScott Long 	default:
16801d558d6aSScott Long 		mpt_prt(mpt, "mpt_read_extcfg_header: Config Info Status %x\n",
16811d558d6aSScott Long 			req->IOCStatus);
16821d558d6aSScott Long 		error = EIO;
16831d558d6aSScott Long 		break;
16841d558d6aSScott Long 	}
16851d558d6aSScott Long 	mpt_free_request(mpt, req);
16861d558d6aSScott Long 	return (error);
16871d558d6aSScott Long }
16881d558d6aSScott Long 
16891d558d6aSScott Long int
16901d558d6aSScott Long mpt_read_extcfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
16911d558d6aSScott Long 		     CONFIG_EXTENDED_PAGE_HEADER *hdr, void *buf, size_t len,
16921d558d6aSScott Long 		     int sleep_ok, int timeout_ms)
16931d558d6aSScott Long {
16941d558d6aSScott Long 	request_t    *req;
16951d558d6aSScott Long 	cfgparms_t    params;
16961d558d6aSScott Long 	int	      error;
16971d558d6aSScott Long 
16981d558d6aSScott Long 	req = mpt_get_request(mpt, sleep_ok);
16991d558d6aSScott Long 	if (req == NULL) {
17007ee37807SMarius Strobl 		mpt_prt(mpt, "mpt_read_extcfg_page: Get request failed!\n");
17011d558d6aSScott Long 		return (-1);
17021d558d6aSScott Long 	}
17031d558d6aSScott Long 
17041d558d6aSScott Long 	params.Action = Action;
17051d558d6aSScott Long 	params.PageVersion = hdr->PageVersion;
17061d558d6aSScott Long 	params.PageLength = 0;
17071d558d6aSScott Long 	params.PageNumber = hdr->PageNumber;
17081d558d6aSScott Long 	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
17091d558d6aSScott Long 	params.PageAddress = PageAddress;
17101d558d6aSScott Long 	params.ExtPageType = hdr->ExtPageType;
17111d558d6aSScott Long 	params.ExtPageLength = hdr->ExtPageLength;
17121d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params,
17131d558d6aSScott Long 				  req->req_pbuf + MPT_RQSL(mpt),
17141d558d6aSScott Long 				  len, sleep_ok, timeout_ms);
17151d558d6aSScott Long 	if (error != 0) {
17161d558d6aSScott Long 		mpt_prt(mpt, "read_extcfg_page(%d) timed out\n", Action);
17171d558d6aSScott Long 		return (-1);
17181d558d6aSScott Long 	}
17191d558d6aSScott Long 
17201d558d6aSScott Long 	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
17211d558d6aSScott Long 		mpt_prt(mpt, "mpt_read_extcfg_page: Config Info Status %x\n",
17221d558d6aSScott Long 			req->IOCStatus);
17231d558d6aSScott Long 		mpt_free_request(mpt, req);
17241d558d6aSScott Long 		return (-1);
17251d558d6aSScott Long 	}
17261d558d6aSScott Long 	memcpy(buf, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
17271d558d6aSScott Long 	mpt_free_request(mpt, req);
17281d558d6aSScott Long 	return (0);
17291d558d6aSScott Long }
1730b0a2fdeeSScott Long 
1731b0a2fdeeSScott Long int
1732b0a2fdeeSScott Long mpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1733b0a2fdeeSScott Long 		    uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1734b0a2fdeeSScott Long 		    int sleep_ok, int timeout_ms)
1735b0a2fdeeSScott Long {
1736b0a2fdeeSScott Long 	request_t  *req;
17371d558d6aSScott Long 	cfgparms_t params;
173829ae59edSMatt Jacob 	MSG_CONFIG *cfgp;
1739b0a2fdeeSScott Long 	int	    error;
1740b0a2fdeeSScott Long 
1741b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1742b0a2fdeeSScott Long 	if (req == NULL) {
1743b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
174429ae59edSMatt Jacob 		return (ENOMEM);
17457104aeefSMatt Jacob 	}
1746b0a2fdeeSScott Long 
17471d558d6aSScott Long 	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
17481d558d6aSScott Long 	params.PageVersion = 0;
17491d558d6aSScott Long 	params.PageLength = 0;
17501d558d6aSScott Long 	params.PageNumber = PageNumber;
17511d558d6aSScott Long 	params.PageType = PageType;
17521d558d6aSScott Long 	params.PageAddress = PageAddress;
17531d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
1754b0a2fdeeSScott Long 				  sleep_ok, timeout_ms);
1755b0a2fdeeSScott Long 	if (error != 0) {
17566621d786SMatt Jacob 		/*
17576621d786SMatt Jacob 		 * Leave the request. Without resetting the chip, it's
17586621d786SMatt Jacob 		 * still owned by it and we'll just get into trouble
17596621d786SMatt Jacob 		 * freeing it now. Mark it as abandoned so that if it
17606621d786SMatt Jacob 		 * shows up later it can be freed.
17616621d786SMatt Jacob 		 */
1762b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_header timed out\n");
176329ae59edSMatt Jacob 		return (ETIMEDOUT);
1764b0a2fdeeSScott Long 	}
1765b0a2fdeeSScott Long 
176629ae59edSMatt Jacob         switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
176729ae59edSMatt Jacob 	case MPI_IOCSTATUS_SUCCESS:
1768b0a2fdeeSScott Long 		cfgp = req->req_vbuf;
1769b0a2fdeeSScott Long 		bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1770b0a2fdeeSScott Long 		error = 0;
177129ae59edSMatt Jacob 		break;
177229ae59edSMatt Jacob 	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
177329ae59edSMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG,
177429ae59edSMatt Jacob 		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
177529ae59edSMatt Jacob 		    PageType, PageNumber, PageAddress);
177629ae59edSMatt Jacob 		error = EINVAL;
177729ae59edSMatt Jacob 		break;
177829ae59edSMatt Jacob 	default:
177929ae59edSMatt Jacob 		mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
178029ae59edSMatt Jacob 			req->IOCStatus);
178129ae59edSMatt Jacob 		error = EIO;
178229ae59edSMatt Jacob 		break;
1783b0a2fdeeSScott Long 	}
17847104aeefSMatt Jacob 	mpt_free_request(mpt, req);
1785b0a2fdeeSScott Long 	return (error);
17867104aeefSMatt Jacob }
17877104aeefSMatt Jacob 
1788ce68dae5SMatt Jacob int
1789b0a2fdeeSScott Long mpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1790b0a2fdeeSScott Long 		  CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1791b0a2fdeeSScott Long 		  int timeout_ms)
17927104aeefSMatt Jacob {
17937104aeefSMatt Jacob 	request_t    *req;
17941d558d6aSScott Long 	cfgparms_t    params;
1795b0a2fdeeSScott Long 	int	      error;
17967104aeefSMatt Jacob 
1797b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1798b0a2fdeeSScott Long 	if (req == NULL) {
1799b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
18007104aeefSMatt Jacob 		return (-1);
18017104aeefSMatt Jacob 	}
18027104aeefSMatt Jacob 
18031d558d6aSScott Long 	params.Action = Action;
18041d558d6aSScott Long 	params.PageVersion = hdr->PageVersion;
18051d558d6aSScott Long 	params.PageLength = hdr->PageLength;
18061d558d6aSScott Long 	params.PageNumber = hdr->PageNumber;
18071d558d6aSScott Long 	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
18081d558d6aSScott Long 	params.PageAddress = PageAddress;
18091d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params,
18101d558d6aSScott Long 				  req->req_pbuf + MPT_RQSL(mpt),
1811b0a2fdeeSScott Long 				  len, sleep_ok, timeout_ms);
1812b0a2fdeeSScott Long 	if (error != 0) {
1813b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
18147104aeefSMatt Jacob 		return (-1);
18157104aeefSMatt Jacob 	}
1816b0a2fdeeSScott Long 
1817b0a2fdeeSScott Long 	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1818b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1819b0a2fdeeSScott Long 			req->IOCStatus);
1820b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
1821b0a2fdeeSScott Long 		return (-1);
1822b0a2fdeeSScott Long 	}
1823c87e3f83SMatt Jacob 	memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
18247104aeefSMatt Jacob 	mpt_free_request(mpt, req);
18257104aeefSMatt Jacob 	return (0);
18267104aeefSMatt Jacob }
18277104aeefSMatt Jacob 
1828ce68dae5SMatt Jacob int
1829b0a2fdeeSScott Long mpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1830b0a2fdeeSScott Long 		   CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1831b0a2fdeeSScott Long 		   int timeout_ms)
18327104aeefSMatt Jacob {
18337104aeefSMatt Jacob 	request_t    *req;
18341d558d6aSScott Long 	cfgparms_t    params;
1835b0a2fdeeSScott Long 	u_int	      hdr_attr;
1836b0a2fdeeSScott Long 	int	      error;
18377104aeefSMatt Jacob 
18387104aeefSMatt Jacob 	hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
18397104aeefSMatt Jacob 	if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
18407104aeefSMatt Jacob 	    hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1841b0a2fdeeSScott Long 		mpt_prt(mpt, "page type 0x%x not changeable\n",
18427104aeefSMatt Jacob 			hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
18437104aeefSMatt Jacob 		return (-1);
18447104aeefSMatt Jacob 	}
1845b4c618c0SMatt Jacob 
1846b4c618c0SMatt Jacob #if	0
1847b4c618c0SMatt Jacob 	/*
1848b4c618c0SMatt Jacob 	 * We shouldn't mask off other bits here.
1849b4c618c0SMatt Jacob 	 */
1850b4c618c0SMatt Jacob 	hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1851b4c618c0SMatt Jacob #endif
18527104aeefSMatt Jacob 
1853b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1854b0a2fdeeSScott Long 	if (req == NULL)
1855b0a2fdeeSScott Long 		return (-1);
18567104aeefSMatt Jacob 
1857c87e3f83SMatt Jacob 	memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1858b4c618c0SMatt Jacob 
1859b4c618c0SMatt Jacob 	/*
1860b4c618c0SMatt Jacob 	 * There isn't any point in restoring stripped out attributes
1861b4c618c0SMatt Jacob 	 * if you then mask them going down to issue the request.
1862b4c618c0SMatt Jacob 	 */
1863b4c618c0SMatt Jacob 
18641d558d6aSScott Long 	params.Action = Action;
18651d558d6aSScott Long 	params.PageVersion = hdr->PageVersion;
18661d558d6aSScott Long 	params.PageLength = hdr->PageLength;
18671d558d6aSScott Long 	params.PageNumber = hdr->PageNumber;
18681d558d6aSScott Long 	params.PageAddress = PageAddress;
1869b4c618c0SMatt Jacob #if	0
1870301472c2SMatt Jacob 	/* Restore stripped out attributes */
1871301472c2SMatt Jacob 	hdr->PageType |= hdr_attr;
18721d558d6aSScott Long 	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1873b4c618c0SMatt Jacob #else
18741d558d6aSScott Long 	params.PageType = hdr->PageType;
18751d558d6aSScott Long #endif
18761d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params,
1877b4c618c0SMatt Jacob 				  req->req_pbuf + MPT_RQSL(mpt),
1878b4c618c0SMatt Jacob 				  len, sleep_ok, timeout_ms);
1879b0a2fdeeSScott Long 	if (error != 0) {
1880b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
18817104aeefSMatt Jacob 		return (-1);
18827104aeefSMatt Jacob 	}
18837104aeefSMatt Jacob 
1884b0a2fdeeSScott Long         if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1885b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1886b0a2fdeeSScott Long 			req->IOCStatus);
18877104aeefSMatt Jacob 		mpt_free_request(mpt, req);
1888b0a2fdeeSScott Long 		return (-1);
1889b0a2fdeeSScott Long 	}
1890b0a2fdeeSScott Long 	mpt_free_request(mpt, req);
1891b0a2fdeeSScott Long 	return (0);
1892b0a2fdeeSScott Long }
1893b0a2fdeeSScott Long 
1894b0a2fdeeSScott Long /*
1895b0a2fdeeSScott Long  * Read IOC configuration information
1896b0a2fdeeSScott Long  */
1897b0a2fdeeSScott Long static int
1898b0a2fdeeSScott Long mpt_read_config_info_ioc(struct mpt_softc *mpt)
1899b0a2fdeeSScott Long {
1900b0a2fdeeSScott Long 	CONFIG_PAGE_HEADER hdr;
1901b0a2fdeeSScott Long 	struct mpt_raid_volume *mpt_raid;
1902b0a2fdeeSScott Long 	int rv;
1903b0a2fdeeSScott Long 	int i;
1904b0a2fdeeSScott Long 	size_t len;
1905b0a2fdeeSScott Long 
1906b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
19071d79ca0eSMatt Jacob 		2, 0, &hdr, FALSE, 5000);
190829ae59edSMatt Jacob 	/*
190929ae59edSMatt Jacob 	 * If it's an invalid page, so what? Not a supported function....
191029ae59edSMatt Jacob 	 */
19111d79ca0eSMatt Jacob 	if (rv == EINVAL) {
191229ae59edSMatt Jacob 		return (0);
19131d79ca0eSMatt Jacob 	}
19141d79ca0eSMatt Jacob 	if (rv) {
191529ae59edSMatt Jacob 		return (rv);
19161d79ca0eSMatt Jacob 	}
1917b0a2fdeeSScott Long 
19180e0521a1SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG,
19190e0521a1SMatt Jacob 	    "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n",
19200e0521a1SMatt Jacob 	    hdr.PageVersion, hdr.PageLength << 2,
1921b0a2fdeeSScott Long 	    hdr.PageNumber, hdr.PageType);
1922b0a2fdeeSScott Long 
1923b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
1924a3699bcaSScott Long 	mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
19251d79ca0eSMatt Jacob 	if (mpt->ioc_page2 == NULL) {
19261d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
19271d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
1928b0a2fdeeSScott Long 		return (ENOMEM);
19291d79ca0eSMatt Jacob 	}
1930b0a2fdeeSScott Long 	memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
19311d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
19321d79ca0eSMatt Jacob 	    &mpt->ioc_page2->Header, len, FALSE, 5000);
1933b0a2fdeeSScott Long 	if (rv) {
1934b0a2fdeeSScott Long 		mpt_prt(mpt, "failed to read IOC Page 2\n");
19351d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
19361d79ca0eSMatt Jacob 		return (EIO);
19371d79ca0eSMatt Jacob 	}
19380e0521a1SMatt Jacob 	mpt2host_config_page_ioc2(mpt->ioc_page2);
19391d79ca0eSMatt Jacob 
19401d79ca0eSMatt Jacob 	if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1941b0a2fdeeSScott Long 		uint32_t mask;
1942b0a2fdeeSScott Long 
1943b0a2fdeeSScott Long 		mpt_prt(mpt, "Capabilities: (");
1944b0a2fdeeSScott Long 		for (mask = 1; mask != 0; mask <<= 1) {
19451d79ca0eSMatt Jacob 			if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1946b0a2fdeeSScott Long 				continue;
19471d79ca0eSMatt Jacob 			}
1948b0a2fdeeSScott Long 			switch (mask) {
1949b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1950b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-0");
1951b0a2fdeeSScott Long 				break;
1952b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1953b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1E");
1954b0a2fdeeSScott Long 				break;
1955b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1956b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1");
1957b0a2fdeeSScott Long 				break;
1958b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1959b0a2fdeeSScott Long 				mpt_prtc(mpt, " SES");
1960b0a2fdeeSScott Long 				break;
1961b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1962b0a2fdeeSScott Long 				mpt_prtc(mpt, " SAFTE");
1963b0a2fdeeSScott Long 				break;
1964b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1965b0a2fdeeSScott Long 				mpt_prtc(mpt, " Multi-Channel-Arrays");
1966b0a2fdeeSScott Long 			default:
1967b0a2fdeeSScott Long 				break;
1968b0a2fdeeSScott Long 			}
1969b0a2fdeeSScott Long 		}
1970b0a2fdeeSScott Long 		mpt_prtc(mpt, " )\n");
1971b0a2fdeeSScott Long 		if ((mpt->ioc_page2->CapabilitiesFlags
1972b0a2fdeeSScott Long 		   & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1973b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1974b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1975b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1976b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes,
1977b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes != 1
1978b0a2fdeeSScott Long 			      ? "s " : " ",
1979b0a2fdeeSScott Long 				mpt->ioc_page2->MaxVolumes);
1980b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1981b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks,
1982b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks != 1
1983b0a2fdeeSScott Long 			      ? "s " : " ",
1984b0a2fdeeSScott Long 				mpt->ioc_page2->MaxPhysDisks);
1985b0a2fdeeSScott Long 		}
1986b0a2fdeeSScott Long 	}
1987b0a2fdeeSScott Long 
1988b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
19891d79ca0eSMatt Jacob 	mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1990b0a2fdeeSScott Long 	if (mpt->raid_volumes == NULL) {
1991b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID volume data\n");
19921d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
19931d79ca0eSMatt Jacob 		return (ENOMEM);
1994b0a2fdeeSScott Long 	}
1995b0a2fdeeSScott Long 
1996b0a2fdeeSScott Long 	/*
1997b0a2fdeeSScott Long 	 * Copy critical data out of ioc_page2 so that we can
1998b0a2fdeeSScott Long 	 * safely refresh the page without windows of unreliable
1999b0a2fdeeSScott Long 	 * data.
2000b0a2fdeeSScott Long 	 */
2001b0a2fdeeSScott Long 	mpt->raid_max_volumes =  mpt->ioc_page2->MaxVolumes;
2002b0a2fdeeSScott Long 
20031d79ca0eSMatt Jacob 	len = sizeof(*mpt->raid_volumes->config_page) +
20041d79ca0eSMatt Jacob 	    (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
2005b0a2fdeeSScott Long 	for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
2006b0a2fdeeSScott Long 		mpt_raid = &mpt->raid_volumes[i];
20071d79ca0eSMatt Jacob 		mpt_raid->config_page =
20081d79ca0eSMatt Jacob 		    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2009b0a2fdeeSScott Long 		if (mpt_raid->config_page == NULL) {
2010b0a2fdeeSScott Long 			mpt_prt(mpt, "Could not allocate RAID page data\n");
20111d79ca0eSMatt Jacob 			mpt_raid_free_mem(mpt);
20121d79ca0eSMatt Jacob 			return (ENOMEM);
2013b0a2fdeeSScott Long 		}
2014b0a2fdeeSScott Long 	}
2015b0a2fdeeSScott Long 	mpt->raid_page0_len = len;
2016b0a2fdeeSScott Long 
2017b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
20181d79ca0eSMatt Jacob 	mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2019b0a2fdeeSScott Long 	if (mpt->raid_disks == NULL) {
2020b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID disk data\n");
20211d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
20221d79ca0eSMatt Jacob 		return (ENOMEM);
2023b0a2fdeeSScott Long 	}
2024b0a2fdeeSScott Long 	mpt->raid_max_disks =  mpt->ioc_page2->MaxPhysDisks;
2025b0a2fdeeSScott Long 
20261d79ca0eSMatt Jacob 	/*
20271d79ca0eSMatt Jacob 	 * Load page 3.
20281d79ca0eSMatt Jacob 	 */
2029b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
20301d79ca0eSMatt Jacob 	    3, 0, &hdr, FALSE, 5000);
20311d79ca0eSMatt Jacob 	if (rv) {
20321d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
2033b0a2fdeeSScott Long 		return (EIO);
20341d79ca0eSMatt Jacob 	}
2035b0a2fdeeSScott Long 
2036b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
2037b0a2fdeeSScott Long 	    hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
2038b0a2fdeeSScott Long 
2039b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
2040a3699bcaSScott Long 	mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
20411d79ca0eSMatt Jacob 	if (mpt->ioc_page3 == NULL) {
20421d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
20431d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
20441d79ca0eSMatt Jacob 		return (ENOMEM);
2045b0a2fdeeSScott Long 	}
20461d79ca0eSMatt Jacob 	memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
20471d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
20481d79ca0eSMatt Jacob 	    &mpt->ioc_page3->Header, len, FALSE, 5000);
20491d79ca0eSMatt Jacob 	if (rv) {
20501d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
20511d79ca0eSMatt Jacob 		return (EIO);
20521d79ca0eSMatt Jacob 	}
20537ee37807SMarius Strobl 	mpt2host_config_page_ioc3(mpt->ioc_page3);
2054b0a2fdeeSScott Long 	mpt_raid_wakeup(mpt);
20557104aeefSMatt Jacob 	return (0);
20567104aeefSMatt Jacob }
20577104aeefSMatt Jacob 
20587104aeefSMatt Jacob /*
20597104aeefSMatt Jacob  * Enable IOC port
20607104aeefSMatt Jacob  */
20617104aeefSMatt Jacob static int
2062b0a2fdeeSScott Long mpt_send_port_enable(struct mpt_softc *mpt, int port)
20639b631363SMatt Jacob {
20649b631363SMatt Jacob 	request_t	*req;
20659b631363SMatt Jacob 	MSG_PORT_ENABLE *enable_req;
2066b0a2fdeeSScott Long 	int		 error;
20679b631363SMatt Jacob 
2068b0a2fdeeSScott Long 	req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
2069b0a2fdeeSScott Long 	if (req == NULL)
2070b0a2fdeeSScott Long 		return (-1);
20719b631363SMatt Jacob 
20729b631363SMatt Jacob 	enable_req = req->req_vbuf;
20735e073106SMatt Jacob 	memset(enable_req, 0,  MPT_RQSL(mpt));
20749b631363SMatt Jacob 
20759b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_PORT_ENABLE;
2076b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
20779b631363SMatt Jacob 	enable_req->PortNumber = port;
20789b631363SMatt Jacob 
20799b631363SMatt Jacob 	mpt_check_doorbell(mpt);
2080b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
20819b631363SMatt Jacob 
2082b0a2fdeeSScott Long 	mpt_send_cmd(mpt, req);
2083b0a2fdeeSScott Long 	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
208484ceb5f6SMarius Strobl 	    FALSE, (mpt->is_sas || mpt->is_fc)? 300000 : 30000);
2085b0a2fdeeSScott Long 	if (error != 0) {
2086c87e3f83SMatt Jacob 		mpt_prt(mpt, "port %d enable timed out\n", port);
20879b631363SMatt Jacob 		return (-1);
20889b631363SMatt Jacob 	}
20899b631363SMatt Jacob 	mpt_free_request(mpt, req);
2090c87e3f83SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
20919b631363SMatt Jacob 	return (0);
20929b631363SMatt Jacob }
20939b631363SMatt Jacob 
20949b631363SMatt Jacob /*
20959b631363SMatt Jacob  * Enable/Disable asynchronous event reporting.
20969b631363SMatt Jacob  */
20979b631363SMatt Jacob static int
2098b0a2fdeeSScott Long mpt_send_event_request(struct mpt_softc *mpt, int onoff)
20999b631363SMatt Jacob {
21009b631363SMatt Jacob 	request_t *req;
21019b631363SMatt Jacob 	MSG_EVENT_NOTIFY *enable_req;
21029b631363SMatt Jacob 
21035e073106SMatt Jacob 	req = mpt_get_request(mpt, FALSE);
21045e073106SMatt Jacob 	if (req == NULL) {
21055e073106SMatt Jacob 		return (ENOMEM);
21065e073106SMatt Jacob 	}
21079b631363SMatt Jacob 	enable_req = req->req_vbuf;
21085e073106SMatt Jacob 	memset(enable_req, 0, sizeof *enable_req);
21099b631363SMatt Jacob 
21109b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_EVENT_NOTIFICATION;
2111b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
21129b631363SMatt Jacob 	enable_req->Switch     = onoff;
21139b631363SMatt Jacob 
21149b631363SMatt Jacob 	mpt_check_doorbell(mpt);
21155e073106SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
21165e073106SMatt Jacob 	    onoff ? "en" : "dis");
21175e073106SMatt Jacob 	/*
21185e073106SMatt Jacob 	 * Send the command off, but don't wait for it.
21195e073106SMatt Jacob 	 */
21209b631363SMatt Jacob 	mpt_send_cmd(mpt, req);
21219b631363SMatt Jacob 	return (0);
21229b631363SMatt Jacob }
21239b631363SMatt Jacob 
21249b631363SMatt Jacob /*
2125976b0106SKevin Lo  * Un-mask the interrupts on the chip.
21269b631363SMatt Jacob  */
21279b631363SMatt Jacob void
2128b0a2fdeeSScott Long mpt_enable_ints(struct mpt_softc *mpt)
21299b631363SMatt Jacob {
213087e255acSMarius Strobl 
21319b631363SMatt Jacob 	/* Unmask every thing except door bell int */
21329b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
21339b631363SMatt Jacob }
21349b631363SMatt Jacob 
21359b631363SMatt Jacob /*
2136976b0106SKevin Lo  * Mask the interrupts on the chip.
21379b631363SMatt Jacob  */
21389b631363SMatt Jacob void
2139b0a2fdeeSScott Long mpt_disable_ints(struct mpt_softc *mpt)
21409b631363SMatt Jacob {
214187e255acSMarius Strobl 
21429b631363SMatt Jacob 	/* Mask all interrupts */
21439b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK,
21449b631363SMatt Jacob 	    MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
21459b631363SMatt Jacob }
21469b631363SMatt Jacob 
2147b0a2fdeeSScott Long static void
2148b0a2fdeeSScott Long mpt_sysctl_attach(struct mpt_softc *mpt)
21499b631363SMatt Jacob {
2150b0a2fdeeSScott Long 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
2151b0a2fdeeSScott Long 	struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
21529b631363SMatt Jacob 
21536dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2154b0a2fdeeSScott Long 		       "debug", CTLFLAG_RW, &mpt->verbose, 0,
2155b0a2fdeeSScott Long 		       "Debugging/Verbose level");
21566dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2157b4c618c0SMatt Jacob 		       "role", CTLFLAG_RD, &mpt->role, 0,
2158b4c618c0SMatt Jacob 		       "HBA role");
2159a2baed97SMatt Jacob #ifdef	MPT_TEST_MULTIPATH
2160a2baed97SMatt Jacob 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2161a2baed97SMatt Jacob 		       "failure_id", CTLFLAG_RW, &mpt->failure_id, -1,
2162a2baed97SMatt Jacob 		       "Next Target to Fail");
2163a2baed97SMatt Jacob #endif
21649b631363SMatt Jacob }
21659b631363SMatt Jacob 
2166b0a2fdeeSScott Long int
2167b0a2fdeeSScott Long mpt_attach(struct mpt_softc *mpt)
2168b0a2fdeeSScott Long {
2169b0a2fdeeSScott Long 	struct mpt_personality *pers;
2170c87e3f83SMatt Jacob 	int i;
2171b0a2fdeeSScott Long 	int error;
2172b0a2fdeeSScott Long 
2173e955d4fdSScott Long 	mpt_core_attach(mpt);
2174e955d4fdSScott Long 	mpt_core_enable(mpt);
2175e955d4fdSScott Long 
2176a7303be1SMatt Jacob 	TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links);
2177c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2178b0a2fdeeSScott Long 		pers = mpt_personalities[i];
2179c87e3f83SMatt Jacob 		if (pers == NULL) {
2180b0a2fdeeSScott Long 			continue;
2181c87e3f83SMatt Jacob 		}
2182b0a2fdeeSScott Long 		if (pers->probe(mpt) == 0) {
2183b0a2fdeeSScott Long 			error = pers->attach(mpt);
2184b0a2fdeeSScott Long 			if (error != 0) {
2185b0a2fdeeSScott Long 				mpt_detach(mpt);
2186b0a2fdeeSScott Long 				return (error);
2187b0a2fdeeSScott Long 			}
2188b0a2fdeeSScott Long 			mpt->mpt_pers_mask |= (0x1 << pers->id);
2189b0a2fdeeSScott Long 			pers->use_count++;
2190b0a2fdeeSScott Long 		}
2191b0a2fdeeSScott Long 	}
2192444dd2b6SMatt Jacob 
2193c87e3f83SMatt Jacob 	/*
2194c87e3f83SMatt Jacob 	 * Now that we've attached everything, do the enable function
2195c87e3f83SMatt Jacob 	 * for all of the personalities. This allows the personalities
2196c87e3f83SMatt Jacob 	 * to do setups that are appropriate for them prior to enabling
2197c87e3f83SMatt Jacob 	 * any ports.
2198c87e3f83SMatt Jacob 	 */
2199c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2200c87e3f83SMatt Jacob 		pers = mpt_personalities[i];
2201c87e3f83SMatt Jacob 		if (pers != NULL  && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2202c87e3f83SMatt Jacob 			error = pers->enable(mpt);
2203c87e3f83SMatt Jacob 			if (error != 0) {
2204c87e3f83SMatt Jacob 				mpt_prt(mpt, "personality %s attached but would"
2205c87e3f83SMatt Jacob 				    " not enable (%d)\n", pers->name, error);
2206c87e3f83SMatt Jacob 				mpt_detach(mpt);
2207c87e3f83SMatt Jacob 				return (error);
2208c87e3f83SMatt Jacob 			}
2209c87e3f83SMatt Jacob 		}
2210c87e3f83SMatt Jacob 	}
2211b0a2fdeeSScott Long 	return (0);
2212b0a2fdeeSScott Long }
2213b0a2fdeeSScott Long 
2214b0a2fdeeSScott Long int
2215b0a2fdeeSScott Long mpt_shutdown(struct mpt_softc *mpt)
2216b0a2fdeeSScott Long {
2217b0a2fdeeSScott Long 	struct mpt_personality *pers;
2218b0a2fdeeSScott Long 
2219c87e3f83SMatt Jacob 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2220b0a2fdeeSScott Long 		pers->shutdown(mpt);
2221c87e3f83SMatt Jacob 	}
2222b0a2fdeeSScott Long 	return (0);
2223b0a2fdeeSScott Long }
2224b0a2fdeeSScott Long 
2225b0a2fdeeSScott Long int
2226b0a2fdeeSScott Long mpt_detach(struct mpt_softc *mpt)
2227b0a2fdeeSScott Long {
2228b0a2fdeeSScott Long 	struct mpt_personality *pers;
2229b0a2fdeeSScott Long 
2230b0a2fdeeSScott Long 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2231b0a2fdeeSScott Long 		pers->detach(mpt);
2232b0a2fdeeSScott Long 		mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2233b0a2fdeeSScott Long 		pers->use_count--;
2234b0a2fdeeSScott Long 	}
2235a7303be1SMatt Jacob 	TAILQ_REMOVE(&mpt_tailq, mpt, links);
2236b0a2fdeeSScott Long 	return (0);
2237b0a2fdeeSScott Long }
2238b0a2fdeeSScott Long 
223987e255acSMarius Strobl static int
2240b0a2fdeeSScott Long mpt_core_load(struct mpt_personality *pers)
2241b0a2fdeeSScott Long {
2242b0a2fdeeSScott Long 	int i;
2243b0a2fdeeSScott Long 
2244b0a2fdeeSScott Long 	/*
2245b0a2fdeeSScott Long 	 * Setup core handlers and insert the default handler
2246b0a2fdeeSScott Long 	 * into all "empty slots".
2247b0a2fdeeSScott Long 	 */
2248c87e3f83SMatt Jacob 	for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2249b0a2fdeeSScott Long 		mpt_reply_handlers[i] = mpt_default_reply_handler;
2250c87e3f83SMatt Jacob 	}
2251b0a2fdeeSScott Long 
2252b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2253b0a2fdeeSScott Long 	    mpt_event_reply_handler;
2254b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2255b0a2fdeeSScott Long 	    mpt_config_reply_handler;
2256b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2257b0a2fdeeSScott Long 	    mpt_handshake_reply_handler;
2258b0a2fdeeSScott Long 	return (0);
22599b631363SMatt Jacob }
22609b631363SMatt Jacob 
22619b631363SMatt Jacob /*
2262b0a2fdeeSScott Long  * Initialize per-instance driver data and perform
2263b0a2fdeeSScott Long  * initial controller configuration.
22649b631363SMatt Jacob  */
226587e255acSMarius Strobl static int
2266b0a2fdeeSScott Long mpt_core_attach(struct mpt_softc *mpt)
2267b0a2fdeeSScott Long {
2268d0a68c27SMatt Jacob         int val, error;
2269c87e3f83SMatt Jacob 
2270b0a2fdeeSScott Long 	LIST_INIT(&mpt->ack_frames);
2271b0a2fdeeSScott Long 	/* Put all request buffers on the free list */
2272b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_pending_list);
2273b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_free_list);
22745e073106SMatt Jacob 	TAILQ_INIT(&mpt->request_timeout_list);
2275c87e3f83SMatt Jacob 	for (val = 0; val < MPT_MAX_LUNS; val++) {
2276c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].atios);
2277c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].inots);
2278c87e3f83SMatt Jacob 	}
2279c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.atios);
2280c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.inots);
2281a2baed97SMatt Jacob #ifdef	MPT_TEST_MULTIPATH
2282a2baed97SMatt Jacob 	mpt->failure_id = -1;
2283a2baed97SMatt Jacob #endif
2284c87e3f83SMatt Jacob 	mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2285b0a2fdeeSScott Long 	mpt_sysctl_attach(mpt);
2286b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2287b0a2fdeeSScott Long 	    mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
2288d0a68c27SMatt Jacob 
2289d0a68c27SMatt Jacob 	MPT_LOCK(mpt);
2290942adee7SScott Long 	error = mpt_configure_ioc(mpt, 0, 0);
2291d0a68c27SMatt Jacob 	MPT_UNLOCK(mpt);
2292d0a68c27SMatt Jacob 
2293d0a68c27SMatt Jacob 	return (error);
22949b631363SMatt Jacob }
22959b631363SMatt Jacob 
229687e255acSMarius Strobl static int
2297c87e3f83SMatt Jacob mpt_core_enable(struct mpt_softc *mpt)
2298c87e3f83SMatt Jacob {
229987e255acSMarius Strobl 
2300c87e3f83SMatt Jacob 	/*
2301c87e3f83SMatt Jacob 	 * We enter with the IOC enabled, but async events
2302c87e3f83SMatt Jacob 	 * not enabled, ports not enabled and interrupts
2303c87e3f83SMatt Jacob 	 * not enabled.
2304c87e3f83SMatt Jacob 	 */
2305d0a68c27SMatt Jacob 	MPT_LOCK(mpt);
2306c87e3f83SMatt Jacob 
2307c87e3f83SMatt Jacob 	/*
2308c87e3f83SMatt Jacob 	 * Enable asynchronous event reporting- all personalities
2309c87e3f83SMatt Jacob 	 * have attached so that they should be able to now field
2310c87e3f83SMatt Jacob 	 * async events.
2311c87e3f83SMatt Jacob 	 */
2312c87e3f83SMatt Jacob 	mpt_send_event_request(mpt, 1);
2313c87e3f83SMatt Jacob 
2314c87e3f83SMatt Jacob 	/*
2315c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2316c87e3f83SMatt Jacob 	 *
2317c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2318c87e3f83SMatt Jacob 	 * the portenable below times out.
2319c87e3f83SMatt Jacob 	 */
2320c87e3f83SMatt Jacob 	mpt_intr(mpt);
2321c87e3f83SMatt Jacob 
2322c87e3f83SMatt Jacob 	/*
2323c87e3f83SMatt Jacob 	 * Enable Interrupts
2324c87e3f83SMatt Jacob 	 */
2325c87e3f83SMatt Jacob 	mpt_enable_ints(mpt);
2326c87e3f83SMatt Jacob 
2327c87e3f83SMatt Jacob 	/*
2328c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2329c87e3f83SMatt Jacob 	 *
2330c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2331c87e3f83SMatt Jacob 	 * the portenable below times out.
2332c87e3f83SMatt Jacob 	 */
2333c87e3f83SMatt Jacob 	mpt_intr(mpt);
2334c87e3f83SMatt Jacob 
2335c87e3f83SMatt Jacob 	/*
23365089bd63SMatt Jacob 	 * Enable the port.
2337c87e3f83SMatt Jacob 	 */
2338c87e3f83SMatt Jacob 	if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2339c87e3f83SMatt Jacob 		mpt_prt(mpt, "failed to enable port 0\n");
2340d0a68c27SMatt Jacob 		MPT_UNLOCK(mpt);
2341c87e3f83SMatt Jacob 		return (ENXIO);
2342c87e3f83SMatt Jacob 	}
2343d0a68c27SMatt Jacob 	MPT_UNLOCK(mpt);
2344c87e3f83SMatt Jacob 	return (0);
2345c87e3f83SMatt Jacob }
2346c87e3f83SMatt Jacob 
234787e255acSMarius Strobl static void
2348b0a2fdeeSScott Long mpt_core_shutdown(struct mpt_softc *mpt)
2349b0a2fdeeSScott Long {
235087e255acSMarius Strobl 
2351c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2352b0a2fdeeSScott Long }
2353b0a2fdeeSScott Long 
235487e255acSMarius Strobl static void
2355b0a2fdeeSScott Long mpt_core_detach(struct mpt_softc *mpt)
2356b0a2fdeeSScott Long {
2357363b8ed7SAlexander Kabaev 	int val;
2358363b8ed7SAlexander Kabaev 
23590e0521a1SMatt Jacob 	/*
23600e0521a1SMatt Jacob 	 * XXX: FREE MEMORY
23610e0521a1SMatt Jacob 	 */
2362c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2363363b8ed7SAlexander Kabaev 
2364363b8ed7SAlexander Kabaev 	/* Make sure no request has pending timeouts. */
2365363b8ed7SAlexander Kabaev 	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2366363b8ed7SAlexander Kabaev 		request_t *req = &mpt->request_pool[val];
2367363b8ed7SAlexander Kabaev 		mpt_callout_drain(mpt, &req->callout);
2368363b8ed7SAlexander Kabaev 	}
23694201341fSKenneth D. Merry 
23704201341fSKenneth D. Merry 	mpt_dma_buf_free(mpt);
2371b0a2fdeeSScott Long }
2372b0a2fdeeSScott Long 
237387e255acSMarius Strobl static int
2374b0a2fdeeSScott Long mpt_core_unload(struct mpt_personality *pers)
2375b0a2fdeeSScott Long {
237687e255acSMarius Strobl 
23775ab13afcSMarius Strobl 	/* Unload is always successful. */
2378b0a2fdeeSScott Long 	return (0);
2379b0a2fdeeSScott Long }
2380b0a2fdeeSScott Long 
2381b0a2fdeeSScott Long #define FW_UPLOAD_REQ_SIZE				\
2382b0a2fdeeSScott Long 	(sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION)	\
2383b0a2fdeeSScott Long        + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2384b0a2fdeeSScott Long 
2385b0a2fdeeSScott Long static int
2386b0a2fdeeSScott Long mpt_upload_fw(struct mpt_softc *mpt)
2387b0a2fdeeSScott Long {
2388b0a2fdeeSScott Long 	uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2389b0a2fdeeSScott Long 	MSG_FW_UPLOAD_REPLY fw_reply;
2390b0a2fdeeSScott Long 	MSG_FW_UPLOAD *fw_req;
2391b0a2fdeeSScott Long 	FW_UPLOAD_TCSGE *tsge;
2392b0a2fdeeSScott Long 	SGE_SIMPLE32 *sge;
2393b0a2fdeeSScott Long 	uint32_t flags;
2394b0a2fdeeSScott Long 	int error;
2395b0a2fdeeSScott Long 
2396b0a2fdeeSScott Long 	memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2397b0a2fdeeSScott Long 	fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2398b0a2fdeeSScott Long 	fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2399b0a2fdeeSScott Long 	fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2400b0a2fdeeSScott Long 	fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2401b0a2fdeeSScott Long 	tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2402b0a2fdeeSScott Long 	tsge->DetailsLength = 12;
2403b0a2fdeeSScott Long 	tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2404b0a2fdeeSScott Long 	tsge->ImageSize = htole32(mpt->fw_image_size);
2405b0a2fdeeSScott Long 	sge = (SGE_SIMPLE32 *)(tsge + 1);
2406b0a2fdeeSScott Long 	flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2407b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2408b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2409b0a2fdeeSScott Long 	flags <<= MPI_SGE_FLAGS_SHIFT;
2410b0a2fdeeSScott Long 	sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2411b0a2fdeeSScott Long 	sge->Address = htole32(mpt->fw_phys);
24126c5276c8SMarius Strobl 	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_PREREAD);
2413b0a2fdeeSScott Long 	error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2414b0a2fdeeSScott Long 	if (error)
2415b0a2fdeeSScott Long 		return(error);
2416b0a2fdeeSScott Long 	error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
24176c5276c8SMarius Strobl 	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_POSTREAD);
2418b0a2fdeeSScott Long 	return (error);
2419b0a2fdeeSScott Long }
2420b0a2fdeeSScott Long 
2421b0a2fdeeSScott Long static void
2422b0a2fdeeSScott Long mpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2423b0a2fdeeSScott Long 	       uint32_t *data, bus_size_t len)
2424b0a2fdeeSScott Long {
2425b0a2fdeeSScott Long 	uint32_t *data_end;
2426b0a2fdeeSScott Long 
2427b0a2fdeeSScott Long 	data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
24289fe6d254SMatt Jacob 	if (mpt->is_sas) {
2429444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
24309fe6d254SMatt Jacob 	}
2431b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2432b0a2fdeeSScott Long 	while (data != data_end) {
2433b0a2fdeeSScott Long 		mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2434b0a2fdeeSScott Long 		data++;
2435b0a2fdeeSScott Long 	}
24369fe6d254SMatt Jacob 	if (mpt->is_sas) {
2437444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2438b0a2fdeeSScott Long 	}
24399fe6d254SMatt Jacob }
2440b0a2fdeeSScott Long 
2441b0a2fdeeSScott Long static int
2442b0a2fdeeSScott Long mpt_download_fw(struct mpt_softc *mpt)
2443b0a2fdeeSScott Long {
2444b0a2fdeeSScott Long 	MpiFwHeader_t *fw_hdr;
2445b0a2fdeeSScott Long 	int error;
2446b0a2fdeeSScott Long 	uint32_t ext_offset;
2447b0a2fdeeSScott Long 	uint32_t data;
2448b0a2fdeeSScott Long 
244904016bcfSMarius Strobl 	if (mpt->pci_pio_reg == NULL) {
245004016bcfSMarius Strobl 		mpt_prt(mpt, "No PIO resource!\n");
245104016bcfSMarius Strobl 		return (ENXIO);
245204016bcfSMarius Strobl 	}
245304016bcfSMarius Strobl 
2454b0a2fdeeSScott Long 	mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2455b0a2fdeeSScott Long 		mpt->fw_image_size);
2456b0a2fdeeSScott Long 
2457b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
2458b0a2fdeeSScott Long 	if (error != 0) {
2459b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2460b0a2fdeeSScott Long 		return (EIO);
2461b0a2fdeeSScott Long 	}
2462b0a2fdeeSScott Long 
2463b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2464b0a2fdeeSScott Long 		  MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2465b0a2fdeeSScott Long 
2466b0a2fdeeSScott Long 	fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
24676c5276c8SMarius Strobl 	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_PREWRITE);
2468b0a2fdeeSScott Long 	mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2469b0a2fdeeSScott Long 		       fw_hdr->ImageSize);
24706c5276c8SMarius Strobl 	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_POSTWRITE);
2471b0a2fdeeSScott Long 
2472b0a2fdeeSScott Long 	ext_offset = fw_hdr->NextImageHeaderOffset;
2473b0a2fdeeSScott Long 	while (ext_offset != 0) {
2474b0a2fdeeSScott Long 		MpiExtImageHeader_t *ext;
2475b0a2fdeeSScott Long 
2476b0a2fdeeSScott Long 		ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2477b0a2fdeeSScott Long 		ext_offset = ext->NextImageHeaderOffset;
24786c5276c8SMarius Strobl 		bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap,
24796c5276c8SMarius Strobl 		    BUS_DMASYNC_PREWRITE);
2480b0a2fdeeSScott Long 		mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2481b0a2fdeeSScott Long 			       ext->ImageSize);
24826c5276c8SMarius Strobl 		bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap,
24836c5276c8SMarius Strobl 		    BUS_DMASYNC_POSTWRITE);
2484b0a2fdeeSScott Long 	}
2485b0a2fdeeSScott Long 
24869fe6d254SMatt Jacob 	if (mpt->is_sas) {
2487444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
24889fe6d254SMatt Jacob 	}
2489b0a2fdeeSScott Long 	/* Setup the address to jump to on reset. */
2490b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2491b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2492b0a2fdeeSScott Long 
2493b0a2fdeeSScott Long 	/*
2494b0a2fdeeSScott Long 	 * The controller sets the "flash bad" status after attempting
2495b0a2fdeeSScott Long 	 * to auto-boot from flash.  Clear the status so that the controller
2496b0a2fdeeSScott Long 	 * will continue the boot process with our newly installed firmware.
2497b0a2fdeeSScott Long 	 */
2498b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2499b0a2fdeeSScott Long 	data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2500b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2501b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2502b0a2fdeeSScott Long 
25039fe6d254SMatt Jacob 	if (mpt->is_sas) {
2504444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
25059fe6d254SMatt Jacob 	}
2506444dd2b6SMatt Jacob 
2507b0a2fdeeSScott Long 	/*
2508b0a2fdeeSScott Long 	 * Re-enable the processor and clear the boot halt flag.
2509b0a2fdeeSScott Long 	 */
2510b0a2fdeeSScott Long 	data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2511b0a2fdeeSScott Long 	data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2512b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2513b0a2fdeeSScott Long 
2514b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
2515b0a2fdeeSScott Long 	return (0);
2516b0a2fdeeSScott Long }
2517b0a2fdeeSScott Long 
2518cac33e16SMarius Strobl static int
25194201341fSKenneth D. Merry mpt_dma_buf_alloc(struct mpt_softc *mpt)
25204201341fSKenneth D. Merry {
25214201341fSKenneth D. Merry 	struct mpt_map_info mi;
25224201341fSKenneth D. Merry 	uint8_t *vptr;
25234201341fSKenneth D. Merry 	uint32_t pptr, end;
25244201341fSKenneth D. Merry 	int i, error;
25254201341fSKenneth D. Merry 
25264201341fSKenneth D. Merry 	/* Create a child tag for data buffers */
25274201341fSKenneth D. Merry 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
25284201341fSKenneth D. Merry 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
2529cac33e16SMarius Strobl 	    NULL, NULL, (mpt->max_cam_seg_cnt - 1) * PAGE_SIZE,
2530cac33e16SMarius Strobl 	    mpt->max_cam_seg_cnt, BUS_SPACE_MAXSIZE_32BIT, 0,
2531cac33e16SMarius Strobl 	    &mpt->buffer_dmat) != 0) {
25324201341fSKenneth D. Merry 		mpt_prt(mpt, "cannot create a dma tag for data buffers\n");
25334201341fSKenneth D. Merry 		return (1);
25344201341fSKenneth D. Merry 	}
25354201341fSKenneth D. Merry 
25364201341fSKenneth D. Merry 	/* Create a child tag for request buffers */
25374201341fSKenneth D. Merry 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
25384201341fSKenneth D. Merry 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
25394201341fSKenneth D. Merry 	    NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
25404201341fSKenneth D. Merry 	    &mpt->request_dmat) != 0) {
25414201341fSKenneth D. Merry 		mpt_prt(mpt, "cannot create a dma tag for requests\n");
25424201341fSKenneth D. Merry 		return (1);
25434201341fSKenneth D. Merry 	}
25444201341fSKenneth D. Merry 
25455ab13afcSMarius Strobl 	/* Allocate some DMA accessible memory for requests */
25464201341fSKenneth D. Merry 	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
25476c5276c8SMarius Strobl 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &mpt->request_dmap) != 0) {
25484201341fSKenneth D. Merry 		mpt_prt(mpt, "cannot allocate %d bytes of request memory\n",
25494201341fSKenneth D. Merry 		    MPT_REQ_MEM_SIZE(mpt));
25504201341fSKenneth D. Merry 		return (1);
25514201341fSKenneth D. Merry 	}
25524201341fSKenneth D. Merry 
25534201341fSKenneth D. Merry 	mi.mpt = mpt;
25544201341fSKenneth D. Merry 	mi.error = 0;
25554201341fSKenneth D. Merry 
25564201341fSKenneth D. Merry 	/* Load and lock it into "bus space" */
25574201341fSKenneth D. Merry 	bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
25584201341fSKenneth D. Merry 	    MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
25594201341fSKenneth D. Merry 
25604201341fSKenneth D. Merry 	if (mi.error) {
25614201341fSKenneth D. Merry 		mpt_prt(mpt, "error %d loading dma map for DMA request queue\n",
25624201341fSKenneth D. Merry 		    mi.error);
25634201341fSKenneth D. Merry 		return (1);
25644201341fSKenneth D. Merry 	}
25654201341fSKenneth D. Merry 	mpt->request_phys = mi.phys;
25664201341fSKenneth D. Merry 
25674201341fSKenneth D. Merry 	/*
25684201341fSKenneth D. Merry 	 * Now create per-request dma maps
25694201341fSKenneth D. Merry 	 */
25704201341fSKenneth D. Merry 	i = 0;
25714201341fSKenneth D. Merry 	pptr =  mpt->request_phys;
25724201341fSKenneth D. Merry 	vptr =  mpt->request;
25734201341fSKenneth D. Merry 	end = pptr + MPT_REQ_MEM_SIZE(mpt);
25744201341fSKenneth D. Merry 	while(pptr < end) {
25754201341fSKenneth D. Merry 		request_t *req = &mpt->request_pool[i];
25764201341fSKenneth D. Merry 		req->index = i++;
25774201341fSKenneth D. Merry 
25784201341fSKenneth D. Merry 		/* Store location of Request Data */
25794201341fSKenneth D. Merry 		req->req_pbuf = pptr;
25804201341fSKenneth D. Merry 		req->req_vbuf = vptr;
25814201341fSKenneth D. Merry 
25824201341fSKenneth D. Merry 		pptr += MPT_REQUEST_AREA;
25834201341fSKenneth D. Merry 		vptr += MPT_REQUEST_AREA;
25844201341fSKenneth D. Merry 
25854201341fSKenneth D. Merry 		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
25864201341fSKenneth D. Merry 		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
25874201341fSKenneth D. Merry 
25884201341fSKenneth D. Merry 		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
25894201341fSKenneth D. Merry 		if (error) {
25904201341fSKenneth D. Merry 			mpt_prt(mpt, "error %d creating per-cmd DMA maps\n",
25914201341fSKenneth D. Merry 			    error);
25924201341fSKenneth D. Merry 			return (1);
25934201341fSKenneth D. Merry 		}
25944201341fSKenneth D. Merry 	}
25954201341fSKenneth D. Merry 
25964201341fSKenneth D. Merry 	return (0);
25974201341fSKenneth D. Merry }
25984201341fSKenneth D. Merry 
25994201341fSKenneth D. Merry static void
26004201341fSKenneth D. Merry mpt_dma_buf_free(struct mpt_softc *mpt)
26014201341fSKenneth D. Merry {
26024201341fSKenneth D. Merry 	int i;
260387e255acSMarius Strobl 
26044201341fSKenneth D. Merry 	if (mpt->request_dmat == 0) {
26054201341fSKenneth D. Merry 		mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
26064201341fSKenneth D. Merry 		return;
26074201341fSKenneth D. Merry 	}
26084201341fSKenneth D. Merry 	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
26094201341fSKenneth D. Merry 		bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
26104201341fSKenneth D. Merry 	}
26114201341fSKenneth D. Merry 	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
26124201341fSKenneth D. Merry 	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
26134201341fSKenneth D. Merry 	bus_dma_tag_destroy(mpt->request_dmat);
26144201341fSKenneth D. Merry 	mpt->request_dmat = 0;
26154201341fSKenneth D. Merry 	bus_dma_tag_destroy(mpt->buffer_dmat);
26164201341fSKenneth D. Merry }
26174201341fSKenneth D. Merry 
2618b0a2fdeeSScott Long /*
2619b0a2fdeeSScott Long  * Allocate/Initialize data structures for the controller.  Called
2620b0a2fdeeSScott Long  * once at instance startup.
2621b0a2fdeeSScott Long  */
2622b0a2fdeeSScott Long static int
26230e0521a1SMatt Jacob mpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset)
2624b0a2fdeeSScott Long {
26250e0521a1SMatt Jacob 	PTR_MSG_PORT_FACTS_REPLY pfp;
26264201341fSKenneth D. Merry 	int error, port, val;
26270e0521a1SMatt Jacob 	size_t len;
2628b0a2fdeeSScott Long 
26290e0521a1SMatt Jacob 	if (tn == MPT_MAX_TRYS) {
26300e0521a1SMatt Jacob 		return (-1);
26310e0521a1SMatt Jacob 	}
2632b0a2fdeeSScott Long 
26339b631363SMatt Jacob 	/*
26349b631363SMatt Jacob 	 * No need to reset if the IOC is already in the READY state.
26359b631363SMatt Jacob 	 *
26369b631363SMatt Jacob 	 * Force reset if initialization failed previously.
26379b631363SMatt Jacob 	 * Note that a hard_reset of the second channel of a '929
26389b631363SMatt Jacob 	 * will stop operation of the first channel.  Hopefully, if the
26399b631363SMatt Jacob 	 * first channel is ok, the second will not require a hard
26409b631363SMatt Jacob 	 * reset.
26419b631363SMatt Jacob 	 */
26420e0521a1SMatt Jacob 	if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) {
2643c87e3f83SMatt Jacob 		if (mpt_reset(mpt, FALSE) != MPT_OK) {
26440e0521a1SMatt Jacob 			return (mpt_configure_ioc(mpt, tn++, 1));
2645c87e3f83SMatt Jacob 		}
2646b0a2fdeeSScott Long 		needreset = 0;
26477104aeefSMatt Jacob 	}
26487104aeefSMatt Jacob 
26490e0521a1SMatt Jacob 	if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) {
26500e0521a1SMatt Jacob 		mpt_prt(mpt, "mpt_get_iocfacts failed\n");
26510e0521a1SMatt Jacob 		return (mpt_configure_ioc(mpt, tn++, 1));
26520e0521a1SMatt Jacob 	}
26530e0521a1SMatt Jacob 	mpt2host_iocfacts_reply(&mpt->ioc_facts);
26540e0521a1SMatt Jacob 
2655b0a2fdeeSScott Long 	mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
26560e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgVersion >> 8,
26570e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgVersion & 0xFF,
26580e0521a1SMatt Jacob 	    mpt->ioc_facts.HeaderVersion >> 8,
26590e0521a1SMatt Jacob 	    mpt->ioc_facts.HeaderVersion & 0xFF);
2660444dd2b6SMatt Jacob 
2661444dd2b6SMatt Jacob 	/*
2662444dd2b6SMatt Jacob 	 * Now that we know request frame size, we can calculate
2663444dd2b6SMatt Jacob 	 * the actual (reasonable) segment limit for read/write I/O.
2664444dd2b6SMatt Jacob 	 *
2665444dd2b6SMatt Jacob 	 * This limit is constrained by:
2666444dd2b6SMatt Jacob 	 *
2667444dd2b6SMatt Jacob 	 *  + The size of each area we allocate per command (and how
2668444dd2b6SMatt Jacob 	 *    many chain segments we can fit into it).
2669444dd2b6SMatt Jacob 	 *  + The total number of areas we've set up.
2670444dd2b6SMatt Jacob 	 *  + The actual chain depth the card will allow.
2671444dd2b6SMatt Jacob 	 *
2672444dd2b6SMatt Jacob 	 * The first area's segment count is limited by the I/O request
2673444dd2b6SMatt Jacob 	 * at the head of it. We cannot allocate realistically more
2674444dd2b6SMatt Jacob 	 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2675444dd2b6SMatt Jacob 	 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2676444dd2b6SMatt Jacob 	 *
2677444dd2b6SMatt Jacob 	 */
2678444dd2b6SMatt Jacob 	/* total number of request areas we (can) allocate */
2679444dd2b6SMatt Jacob 	mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2680444dd2b6SMatt Jacob 
2681444dd2b6SMatt Jacob 	/* converted to the number of chain areas possible */
2682444dd2b6SMatt Jacob 	mpt->max_seg_cnt *= MPT_NRFM(mpt);
2683444dd2b6SMatt Jacob 
2684444dd2b6SMatt Jacob 	/* limited by the number of chain areas the card will support */
26850e0521a1SMatt Jacob 	if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) {
26864201341fSKenneth D. Merry 		mpt_lprt(mpt, MPT_PRT_INFO,
2687444dd2b6SMatt Jacob 		    "chain depth limited to %u (from %u)\n",
26880e0521a1SMatt Jacob 		    mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt);
26890e0521a1SMatt Jacob 		mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth;
2690444dd2b6SMatt Jacob 	}
2691444dd2b6SMatt Jacob 
2692444dd2b6SMatt Jacob 	/* converted to the number of simple sges in chain segments. */
2693444dd2b6SMatt Jacob 	mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2694444dd2b6SMatt Jacob 
26954201341fSKenneth D. Merry 	/*
26964201341fSKenneth D. Merry 	 * Use this as the basis for reporting the maximum I/O size to CAM.
26974201341fSKenneth D. Merry 	 */
26984201341fSKenneth D. Merry 	mpt->max_cam_seg_cnt = min(mpt->max_seg_cnt, (MAXPHYS / PAGE_SIZE) + 1);
26994201341fSKenneth D. Merry 
2700e0629b31SScott Long 	/* XXX Lame Locking! */
2701e0629b31SScott Long 	MPT_UNLOCK(mpt);
27024201341fSKenneth D. Merry 	error = mpt_dma_buf_alloc(mpt);
2703e0629b31SScott Long 	MPT_LOCK(mpt);
2704e0629b31SScott Long 
27054201341fSKenneth D. Merry 	if (error != 0) {
27064201341fSKenneth D. Merry 		mpt_prt(mpt, "mpt_dma_buf_alloc() failed!\n");
27074201341fSKenneth D. Merry 		return (EIO);
27084201341fSKenneth D. Merry 	}
27094201341fSKenneth D. Merry 
27104201341fSKenneth D. Merry 	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
27114201341fSKenneth D. Merry 		request_t *req = &mpt->request_pool[val];
27124201341fSKenneth D. Merry 		req->state = REQ_STATE_ALLOCATED;
27134201341fSKenneth D. Merry 		mpt_callout_init(mpt, &req->callout);
27144201341fSKenneth D. Merry 		mpt_free_request(mpt, req);
27154201341fSKenneth D. Merry 	}
27164201341fSKenneth D. Merry 
27174201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO, "Maximum Segment Count: %u, Maximum "
27184201341fSKenneth D. Merry 		 "CAM Segment Count: %u\n", mpt->max_seg_cnt,
27194201341fSKenneth D. Merry 		 mpt->max_cam_seg_cnt);
27204201341fSKenneth D. Merry 
27214201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO, "MsgLength=%u IOCNumber = %d\n",
27220e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber);
27234201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO,
2724444dd2b6SMatt Jacob 	    "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2725444dd2b6SMatt Jacob 	    "Request Frame Size %u bytes Max Chain Depth %u\n",
27260e0521a1SMatt Jacob 	    mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize,
27270e0521a1SMatt Jacob 	    mpt->ioc_facts.RequestFrameSize << 2,
27280e0521a1SMatt Jacob 	    mpt->ioc_facts.MaxChainDepth);
27294201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO, "IOCFACTS: Num Ports %d, FWImageSize %d, "
27300e0521a1SMatt Jacob 	    "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts,
27310e0521a1SMatt Jacob 	    mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags);
27320e0521a1SMatt Jacob 
27330e0521a1SMatt Jacob 	len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY);
27340e0521a1SMatt Jacob 	mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
27350e0521a1SMatt Jacob 	if (mpt->port_facts == NULL) {
27360e0521a1SMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for port facts\n");
27370e0521a1SMatt Jacob 		return (ENOMEM);
27380e0521a1SMatt Jacob 	}
2739b0a2fdeeSScott Long 
2740444dd2b6SMatt Jacob 
27410e0521a1SMatt Jacob 	if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) &&
27420e0521a1SMatt Jacob 	    (mpt->fw_uploaded == 0)) {
2743b0a2fdeeSScott Long 		struct mpt_map_info mi;
2744b0a2fdeeSScott Long 
2745b0a2fdeeSScott Long 		/*
2746b0a2fdeeSScott Long 		 * In some configurations, the IOC's firmware is
2747b0a2fdeeSScott Long 		 * stored in a shared piece of system NVRAM that
27485ab13afcSMarius Strobl 		 * is only accessible via the BIOS.  In this
2749b0a2fdeeSScott Long 		 * case, the firmware keeps a copy of firmware in
2750b0a2fdeeSScott Long 		 * RAM until the OS driver retrieves it.  Once
2751b0a2fdeeSScott Long 		 * retrieved, we are responsible for re-downloading
2752b0a2fdeeSScott Long 		 * the firmware after any hard-reset.
2753b0a2fdeeSScott Long 		 */
2754e0629b31SScott Long 		MPT_UNLOCK(mpt);
27550e0521a1SMatt Jacob 		mpt->fw_image_size = mpt->ioc_facts.FWImageSize;
27560e0521a1SMatt Jacob 		error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0,
27570e0521a1SMatt Jacob 		    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
27580e0521a1SMatt Jacob 		    mpt->fw_image_size, 1, mpt->fw_image_size, 0,
27590e0521a1SMatt Jacob 		    &mpt->fw_dmat);
2760b0a2fdeeSScott Long 		if (error != 0) {
27616c5276c8SMarius Strobl 			mpt_prt(mpt, "cannot create firmware dma tag\n");
2762e0629b31SScott Long 			MPT_LOCK(mpt);
2763b0a2fdeeSScott Long 			return (ENOMEM);
27649b631363SMatt Jacob 		}
2765b0a2fdeeSScott Long 		error = bus_dmamem_alloc(mpt->fw_dmat,
27666c5276c8SMarius Strobl 		    (void **)&mpt->fw_image, BUS_DMA_NOWAIT |
27676c5276c8SMarius Strobl 		    BUS_DMA_COHERENT, &mpt->fw_dmap);
2768b0a2fdeeSScott Long 		if (error != 0) {
27690e0521a1SMatt Jacob 			mpt_prt(mpt, "cannot allocate firmware memory\n");
2770b0a2fdeeSScott Long 			bus_dma_tag_destroy(mpt->fw_dmat);
2771e0629b31SScott Long 			MPT_LOCK(mpt);
2772b0a2fdeeSScott Long 			return (ENOMEM);
2773b0a2fdeeSScott Long 		}
2774b0a2fdeeSScott Long 		mi.mpt = mpt;
2775b0a2fdeeSScott Long 		mi.error = 0;
2776b0a2fdeeSScott Long 		bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
27770e0521a1SMatt Jacob 		    mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0);
2778b0a2fdeeSScott Long 		mpt->fw_phys = mi.phys;
2779b0a2fdeeSScott Long 
2780e0629b31SScott Long 		MPT_LOCK(mpt);
2781b0a2fdeeSScott Long 		error = mpt_upload_fw(mpt);
2782b0a2fdeeSScott Long 		if (error != 0) {
27830e0521a1SMatt Jacob 			mpt_prt(mpt, "firmware upload failed.\n");
2784b0a2fdeeSScott Long 			bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2785b0a2fdeeSScott Long 			bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2786b0a2fdeeSScott Long 			    mpt->fw_dmap);
2787b0a2fdeeSScott Long 			bus_dma_tag_destroy(mpt->fw_dmat);
2788b0a2fdeeSScott Long 			mpt->fw_image = NULL;
2789b0a2fdeeSScott Long 			return (EIO);
2790b0a2fdeeSScott Long 		}
27910e0521a1SMatt Jacob 		mpt->fw_uploaded = 1;
2792b0a2fdeeSScott Long 	}
27939b631363SMatt Jacob 
27940e0521a1SMatt Jacob 	for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) {
27950e0521a1SMatt Jacob 		pfp = &mpt->port_facts[port];
27960e0521a1SMatt Jacob 		error = mpt_get_portfacts(mpt, 0, pfp);
27970e0521a1SMatt Jacob 		if (error != MPT_OK) {
27980e0521a1SMatt Jacob 			mpt_prt(mpt,
27990e0521a1SMatt Jacob 			    "mpt_get_portfacts on port %d failed\n", port);
28000e0521a1SMatt Jacob 			free(mpt->port_facts, M_DEVBUF);
28010e0521a1SMatt Jacob 			mpt->port_facts = NULL;
28020e0521a1SMatt Jacob 			return (mpt_configure_ioc(mpt, tn++, 1));
28030e0521a1SMatt Jacob 		}
28040e0521a1SMatt Jacob 		mpt2host_portfacts_reply(pfp);
28050e0521a1SMatt Jacob 
28060e0521a1SMatt Jacob 		if (port > 0) {
28070e0521a1SMatt Jacob 			error = MPT_PRT_INFO;
28080e0521a1SMatt Jacob 		} else {
28090e0521a1SMatt Jacob 			error = MPT_PRT_DEBUG;
28100e0521a1SMatt Jacob 		}
28110e0521a1SMatt Jacob 		mpt_lprt(mpt, error,
28120e0521a1SMatt Jacob 		    "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n",
28130e0521a1SMatt Jacob 		    port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID,
28140e0521a1SMatt Jacob 		    pfp->MaxDevices);
28150e0521a1SMatt Jacob 
28167104aeefSMatt Jacob 	}
28177104aeefSMatt Jacob 
28180e0521a1SMatt Jacob 	/*
28190e0521a1SMatt Jacob 	 * XXX: Not yet supporting more than port 0
28200e0521a1SMatt Jacob 	 */
28210e0521a1SMatt Jacob 	pfp = &mpt->port_facts[0];
28220e0521a1SMatt Jacob 	if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) {
28237104aeefSMatt Jacob 		mpt->is_fc = 1;
2824444dd2b6SMatt Jacob 		mpt->is_sas = 0;
28255580ce96SMatt Jacob 		mpt->is_spi = 0;
28260e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2827444dd2b6SMatt Jacob 		mpt->is_fc = 0;
2828444dd2b6SMatt Jacob 		mpt->is_sas = 1;
28295580ce96SMatt Jacob 		mpt->is_spi = 0;
28300e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) {
28317104aeefSMatt Jacob 		mpt->is_fc = 0;
2832444dd2b6SMatt Jacob 		mpt->is_sas = 0;
28335580ce96SMatt Jacob 		mpt->is_spi = 1;
28348ba5efbdSMarius Strobl 		if (mpt->mpt_ini_id == MPT_INI_ID_NONE)
28358ba5efbdSMarius Strobl 			mpt->mpt_ini_id = pfp->PortSCSIID;
28360e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) {
28370e0521a1SMatt Jacob 		mpt_prt(mpt, "iSCSI not supported yet\n");
28380e0521a1SMatt Jacob 		return (ENXIO);
28390e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) {
28400e0521a1SMatt Jacob 		mpt_prt(mpt, "Inactive Port\n");
28410e0521a1SMatt Jacob 		return (ENXIO);
28420e0521a1SMatt Jacob 	} else {
28430e0521a1SMatt Jacob 		mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType);
28440e0521a1SMatt Jacob 		return (ENXIO);
28457104aeefSMatt Jacob 	}
28467104aeefSMatt Jacob 
2847c87e3f83SMatt Jacob 	/*
2848b4c618c0SMatt Jacob 	 * Set our role with what this port supports.
2849b4c618c0SMatt Jacob 	 *
2850b4c618c0SMatt Jacob 	 * Note this might be changed later in different modules
2851b4c618c0SMatt Jacob 	 * if this is different from what is wanted.
2852c87e3f83SMatt Jacob 	 */
28535089bd63SMatt Jacob 	mpt->role = MPT_ROLE_NONE;
28540e0521a1SMatt Jacob 	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
28555089bd63SMatt Jacob 		mpt->role |= MPT_ROLE_INITIATOR;
2856c87e3f83SMatt Jacob 	}
28570e0521a1SMatt Jacob 	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
28585089bd63SMatt Jacob 		mpt->role |= MPT_ROLE_TARGET;
2859c87e3f83SMatt Jacob 	}
28600e0521a1SMatt Jacob 
28610e0521a1SMatt Jacob 	/*
28620e0521a1SMatt Jacob 	 * Enable the IOC
28630e0521a1SMatt Jacob 	 */
2864e955d4fdSScott Long 	if (mpt_enable_ioc(mpt, 1) != MPT_OK) {
2865c87e3f83SMatt Jacob 		mpt_prt(mpt, "unable to initialize IOC\n");
2866b0a2fdeeSScott Long 		return (ENXIO);
28677104aeefSMatt Jacob 	}
28687104aeefSMatt Jacob 
2869b0a2fdeeSScott Long 	/*
2870c87e3f83SMatt Jacob 	 * Read IOC configuration information.
28711d79ca0eSMatt Jacob 	 *
28721d79ca0eSMatt Jacob 	 * We need this to determine whether or not we have certain
28731d79ca0eSMatt Jacob 	 * settings for Integrated Mirroring (e.g.).
2874b0a2fdeeSScott Long 	 */
2875b0a2fdeeSScott Long 	mpt_read_config_info_ioc(mpt);
2876b0a2fdeeSScott Long 
2877b0a2fdeeSScott Long 	return (0);
2878b0a2fdeeSScott Long }
2879b0a2fdeeSScott Long 
2880b0a2fdeeSScott Long static int
2881c87e3f83SMatt Jacob mpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2882b0a2fdeeSScott Long {
2883b0a2fdeeSScott Long 	uint32_t pptr;
2884b0a2fdeeSScott Long 	int val;
2885b0a2fdeeSScott Long 
2886444dd2b6SMatt Jacob 	if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2887b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2888b0a2fdeeSScott Long 		return (EIO);
2889b0a2fdeeSScott Long 	}
2890b0a2fdeeSScott Long 
2891b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
28929b631363SMatt Jacob 
28939b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2894b0a2fdeeSScott Long 		mpt_prt(mpt, "IOC failed to go to run state\n");
2895b0a2fdeeSScott Long 		return (ENXIO);
28967104aeefSMatt Jacob 	}
2897444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
28989b631363SMatt Jacob 
28999b631363SMatt Jacob 	/*
29009b631363SMatt Jacob 	 * Give it reply buffers
29019b631363SMatt Jacob 	 *
2902b0a2fdeeSScott Long 	 * Do *not* exceed global credits.
29039b631363SMatt Jacob 	 */
29049b631363SMatt Jacob 	for (val = 0, pptr = mpt->reply_phys;
29059b631363SMatt Jacob 	    (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
29069b631363SMatt Jacob 	     pptr += MPT_REPLY_SIZE) {
29079b631363SMatt Jacob 		mpt_free_reply(mpt, pptr);
29080e0521a1SMatt Jacob 		if (++val == mpt->ioc_facts.GlobalCredits - 1)
29099b631363SMatt Jacob 			break;
29109b631363SMatt Jacob 	}
29119b631363SMatt Jacob 
2912c87e3f83SMatt Jacob 
2913c87e3f83SMatt Jacob 	/*
29145e073106SMatt Jacob 	 * Enable the port if asked. This is only done if we're resetting
29155e073106SMatt Jacob 	 * the IOC after initial startup.
2916c87e3f83SMatt Jacob 	 */
2917c87e3f83SMatt Jacob 	if (portenable) {
29187104aeefSMatt Jacob 		/*
29197104aeefSMatt Jacob 		 * Enable asynchronous event reporting
29207104aeefSMatt Jacob 		 */
29219b631363SMatt Jacob 		mpt_send_event_request(mpt, 1);
29229b631363SMatt Jacob 
29239b631363SMatt Jacob 		if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
29244201341fSKenneth D. Merry 			mpt_prt(mpt, "%s: failed to enable port 0\n", __func__);
2925b0a2fdeeSScott Long 			return (ENXIO);
29267104aeefSMatt Jacob 		}
2927c87e3f83SMatt Jacob 	}
292829ae59edSMatt Jacob 	return (MPT_OK);
29299b631363SMatt Jacob }
29300e0521a1SMatt Jacob 
29310e0521a1SMatt Jacob /*
29320e0521a1SMatt Jacob  * Endian Conversion Functions- only used on Big Endian machines
29330e0521a1SMatt Jacob  */
29340e0521a1SMatt Jacob #if	_BYTE_ORDER == _BIG_ENDIAN
29350e0521a1SMatt Jacob void
29360e0521a1SMatt Jacob mpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge)
29370e0521a1SMatt Jacob {
29387ee37807SMarius Strobl 
29390e0521a1SMatt Jacob 	MPT_2_HOST32(sge, FlagsLength);
29407185cd36SMatt Jacob 	MPT_2_HOST32(sge, u.Address64.Low);
29417185cd36SMatt Jacob 	MPT_2_HOST32(sge, u.Address64.High);
29427185cd36SMatt Jacob }
29430e0521a1SMatt Jacob 
29440e0521a1SMatt Jacob void
29450e0521a1SMatt Jacob mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp)
29460e0521a1SMatt Jacob {
29477ee37807SMarius Strobl 
29480e0521a1SMatt Jacob 	MPT_2_HOST16(rp, MsgVersion);
29490e0521a1SMatt Jacob 	MPT_2_HOST16(rp, HeaderVersion);
29500e0521a1SMatt Jacob 	MPT_2_HOST32(rp, MsgContext);
29510e0521a1SMatt Jacob 	MPT_2_HOST16(rp, IOCExceptions);
29520e0521a1SMatt Jacob 	MPT_2_HOST16(rp, IOCStatus);
29530e0521a1SMatt Jacob 	MPT_2_HOST32(rp, IOCLogInfo);
29540e0521a1SMatt Jacob 	MPT_2_HOST16(rp, ReplyQueueDepth);
29550e0521a1SMatt Jacob 	MPT_2_HOST16(rp, RequestFrameSize);
29560e0521a1SMatt Jacob 	MPT_2_HOST16(rp, Reserved_0101_FWVersion);
29570e0521a1SMatt Jacob 	MPT_2_HOST16(rp, ProductID);
29580e0521a1SMatt Jacob 	MPT_2_HOST32(rp, CurrentHostMfaHighAddr);
29590e0521a1SMatt Jacob 	MPT_2_HOST16(rp, GlobalCredits);
29600e0521a1SMatt Jacob 	MPT_2_HOST32(rp, CurrentSenseBufferHighAddr);
29610e0521a1SMatt Jacob 	MPT_2_HOST16(rp, CurReplyFrameSize);
29620e0521a1SMatt Jacob 	MPT_2_HOST32(rp, FWImageSize);
29630e0521a1SMatt Jacob 	MPT_2_HOST32(rp, IOCCapabilities);
29640e0521a1SMatt Jacob 	MPT_2_HOST32(rp, FWVersion.Word);
29650e0521a1SMatt Jacob 	MPT_2_HOST16(rp, HighPriorityQueueDepth);
29660e0521a1SMatt Jacob 	MPT_2_HOST16(rp, Reserved2);
29670e0521a1SMatt Jacob 	mpt2host_sge_simple_union(&rp->HostPageBufferSGE);
29680e0521a1SMatt Jacob 	MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr);
29690e0521a1SMatt Jacob }
29700e0521a1SMatt Jacob 
29710e0521a1SMatt Jacob void
29720e0521a1SMatt Jacob mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp)
29730e0521a1SMatt Jacob {
29747ee37807SMarius Strobl 
29750e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved);
29760e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved1);
29770e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, MsgContext);
29780e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved2);
29790e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, IOCStatus);
29800e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, IOCLogInfo);
29810e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxDevices);
29820e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, PortSCSIID);
29830e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, ProtocolFlags);
29840e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxPostedCmdBuffers);
29850e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxPersistentIDs);
29860e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxLanBuckets);
29870e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved4);
29880e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, Reserved5);
29890e0521a1SMatt Jacob }
29907ee37807SMarius Strobl 
29910e0521a1SMatt Jacob void
29920e0521a1SMatt Jacob mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2)
29930e0521a1SMatt Jacob {
29940e0521a1SMatt Jacob 	int i;
29957ee37807SMarius Strobl 
29967ee37807SMarius Strobl 	MPT_2_HOST32(ioc2, CapabilitiesFlags);
29970e0521a1SMatt Jacob 	for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) {
29987185cd36SMatt Jacob 		MPT_2_HOST16(ioc2, RaidVolume[i].Reserved3);
29990e0521a1SMatt Jacob 	}
30000e0521a1SMatt Jacob }
30010e0521a1SMatt Jacob 
30020e0521a1SMatt Jacob void
30037ee37807SMarius Strobl mpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *ioc3)
30047ee37807SMarius Strobl {
30057ee37807SMarius Strobl 
30067ee37807SMarius Strobl 	MPT_2_HOST16(ioc3, Reserved2);
30077ee37807SMarius Strobl }
30087ee37807SMarius Strobl 
30097ee37807SMarius Strobl void
30107ee37807SMarius Strobl mpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *sp0)
30117ee37807SMarius Strobl {
30127ee37807SMarius Strobl 
30137ee37807SMarius Strobl 	MPT_2_HOST32(sp0, Capabilities);
30147ee37807SMarius Strobl 	MPT_2_HOST32(sp0, PhysicalInterface);
30157ee37807SMarius Strobl }
30167ee37807SMarius Strobl 
30177ee37807SMarius Strobl void
30187ee37807SMarius Strobl mpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
30197ee37807SMarius Strobl {
30207ee37807SMarius Strobl 
30217ee37807SMarius Strobl 	MPT_2_HOST32(sp1, Configuration);
30227ee37807SMarius Strobl 	MPT_2_HOST32(sp1, OnBusTimerValue);
30237ee37807SMarius Strobl 	MPT_2_HOST16(sp1, IDConfig);
30247ee37807SMarius Strobl }
30257ee37807SMarius Strobl 
30267ee37807SMarius Strobl void
30277ee37807SMarius Strobl host2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
30287ee37807SMarius Strobl {
30297ee37807SMarius Strobl 
30307ee37807SMarius Strobl 	HOST_2_MPT32(sp1, Configuration);
30317ee37807SMarius Strobl 	HOST_2_MPT32(sp1, OnBusTimerValue);
30327ee37807SMarius Strobl 	HOST_2_MPT16(sp1, IDConfig);
30337ee37807SMarius Strobl }
30347ee37807SMarius Strobl 
30357ee37807SMarius Strobl void
30367ee37807SMarius Strobl mpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *sp2)
30377ee37807SMarius Strobl {
30387ee37807SMarius Strobl 	int i;
30397ee37807SMarius Strobl 
30407ee37807SMarius Strobl 	MPT_2_HOST32(sp2, PortFlags);
30417ee37807SMarius Strobl 	MPT_2_HOST32(sp2, PortSettings);
30427ee37807SMarius Strobl 	for (i = 0; i < sizeof(sp2->DeviceSettings) /
30437ee37807SMarius Strobl 	    sizeof(*sp2->DeviceSettings); i++) {
30447ee37807SMarius Strobl 		MPT_2_HOST16(sp2, DeviceSettings[i].DeviceFlags);
30457ee37807SMarius Strobl 	}
30467ee37807SMarius Strobl }
30477ee37807SMarius Strobl 
30487ee37807SMarius Strobl void
30497ee37807SMarius Strobl mpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *sd0)
30507ee37807SMarius Strobl {
30517ee37807SMarius Strobl 
30527ee37807SMarius Strobl 	MPT_2_HOST32(sd0, NegotiatedParameters);
30537ee37807SMarius Strobl 	MPT_2_HOST32(sd0, Information);
30547ee37807SMarius Strobl }
30557ee37807SMarius Strobl 
30567ee37807SMarius Strobl void
30577ee37807SMarius Strobl mpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
30587ee37807SMarius Strobl {
30597ee37807SMarius Strobl 
30607ee37807SMarius Strobl 	MPT_2_HOST32(sd1, RequestedParameters);
30617ee37807SMarius Strobl 	MPT_2_HOST32(sd1, Reserved);
30627ee37807SMarius Strobl 	MPT_2_HOST32(sd1, Configuration);
30637ee37807SMarius Strobl }
30647ee37807SMarius Strobl 
30657ee37807SMarius Strobl void
30667ee37807SMarius Strobl host2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
30677ee37807SMarius Strobl {
30687ee37807SMarius Strobl 
30697ee37807SMarius Strobl 	HOST_2_MPT32(sd1, RequestedParameters);
30707ee37807SMarius Strobl 	HOST_2_MPT32(sd1, Reserved);
30717ee37807SMarius Strobl 	HOST_2_MPT32(sd1, Configuration);
30727ee37807SMarius Strobl }
30737ee37807SMarius Strobl 
30747ee37807SMarius Strobl void
30757ee37807SMarius Strobl mpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *fp0)
30767ee37807SMarius Strobl {
30777ee37807SMarius Strobl 
30787ee37807SMarius Strobl 	MPT_2_HOST32(fp0, Flags);
30797ee37807SMarius Strobl 	MPT_2_HOST32(fp0, PortIdentifier);
30807ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWNN.Low);
30817ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWNN.High);
30827ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWPN.Low);
30837ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWPN.High);
30847ee37807SMarius Strobl 	MPT_2_HOST32(fp0, SupportedServiceClass);
30857ee37807SMarius Strobl 	MPT_2_HOST32(fp0, SupportedSpeeds);
30867ee37807SMarius Strobl 	MPT_2_HOST32(fp0, CurrentSpeed);
30877ee37807SMarius Strobl 	MPT_2_HOST32(fp0, MaxFrameSize);
30887ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWNN.Low);
30897ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWNN.High);
30907ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWPN.Low);
30917ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWPN.High);
30927ee37807SMarius Strobl 	MPT_2_HOST32(fp0, DiscoveredPortsCount);
30937ee37807SMarius Strobl 	MPT_2_HOST32(fp0, MaxInitiators);
30947ee37807SMarius Strobl }
30957ee37807SMarius Strobl 
30967ee37807SMarius Strobl void
30977ee37807SMarius Strobl mpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
30987ee37807SMarius Strobl {
30997ee37807SMarius Strobl 
31007ee37807SMarius Strobl 	MPT_2_HOST32(fp1, Flags);
31017ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWNN.Low);
31027ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWNN.High);
31037ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWPN.Low);
31047ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWPN.High);
31057ee37807SMarius Strobl }
31067ee37807SMarius Strobl 
31077ee37807SMarius Strobl void
31087ee37807SMarius Strobl host2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
31097ee37807SMarius Strobl {
31107ee37807SMarius Strobl 
31117ee37807SMarius Strobl 	HOST_2_MPT32(fp1, Flags);
31127ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWNN.Low);
31137ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWNN.High);
31147ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWPN.Low);
31157ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWPN.High);
31167ee37807SMarius Strobl }
31177ee37807SMarius Strobl 
31187ee37807SMarius Strobl void
31190e0521a1SMatt Jacob mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp)
31200e0521a1SMatt Jacob {
31210e0521a1SMatt Jacob 	int i;
31227ee37807SMarius Strobl 
31230e0521a1SMatt Jacob 	MPT_2_HOST16(volp, VolumeStatus.Reserved);
31240e0521a1SMatt Jacob 	MPT_2_HOST16(volp, VolumeSettings.Settings);
31250e0521a1SMatt Jacob 	MPT_2_HOST32(volp, MaxLBA);
3126a4907429SScott Long 	MPT_2_HOST32(volp, MaxLBAHigh);
31270e0521a1SMatt Jacob 	MPT_2_HOST32(volp, StripeSize);
31280e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved2);
31290e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved3);
31300e0521a1SMatt Jacob 	for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) {
31317185cd36SMatt Jacob 		MPT_2_HOST16(volp, PhysDisk[i].Reserved);
31320e0521a1SMatt Jacob 	}
31330e0521a1SMatt Jacob }
31340e0521a1SMatt Jacob 
31350e0521a1SMatt Jacob void
31367ee37807SMarius Strobl mpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *rpd0)
31377ee37807SMarius Strobl {
31387ee37807SMarius Strobl 
31397ee37807SMarius Strobl 	MPT_2_HOST32(rpd0, Reserved1);
31407ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, PhysDiskStatus.Reserved);
31417ee37807SMarius Strobl 	MPT_2_HOST32(rpd0, MaxLBA);
31427ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, ErrorData.Reserved);
31437ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, ErrorData.ErrorCount);
31447ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, ErrorData.SmartCount);
31457ee37807SMarius Strobl }
31467ee37807SMarius Strobl 
31477ee37807SMarius Strobl void
31480e0521a1SMatt Jacob mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi)
31490e0521a1SMatt Jacob {
31507ee37807SMarius Strobl 
31517185cd36SMatt Jacob 	MPT_2_HOST16(vi, TotalBlocks.High);
31527185cd36SMatt Jacob 	MPT_2_HOST16(vi, TotalBlocks.Low);
31537185cd36SMatt Jacob 	MPT_2_HOST16(vi, BlocksRemaining.High);
31547185cd36SMatt Jacob 	MPT_2_HOST16(vi, BlocksRemaining.Low);
31550e0521a1SMatt Jacob }
31560e0521a1SMatt Jacob #endif
3157