xref: /freebsd/sys/dev/mpt/mpt.c (revision 0e0521a153f7aa5ca0366b00feaa450d71480cc7)
1098ca2bdSWarner Losh /*-
20b80d21bSMatt Jacob  * Generic routines for LSI Fusion adapters.
39b631363SMatt Jacob  * FreeBSD Version.
49b631363SMatt Jacob  *
59b631363SMatt Jacob  * Copyright (c) 2000, 2001 by Greg Ansley
69b631363SMatt Jacob  *
79b631363SMatt Jacob  * Redistribution and use in source and binary forms, with or without
89b631363SMatt Jacob  * modification, are permitted provided that the following conditions
99b631363SMatt Jacob  * are met:
109b631363SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
119b631363SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
129b631363SMatt Jacob  *    this list of conditions, and the following disclaimer.
139b631363SMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
149b631363SMatt Jacob  *    derived from this software without specific prior written permission.
159b631363SMatt Jacob  *
169b631363SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179b631363SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189b631363SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199b631363SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
209b631363SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219b631363SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229b631363SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239b631363SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249b631363SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259b631363SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269b631363SMatt Jacob  * SUCH DAMAGE.
279b631363SMatt Jacob  */
280b80d21bSMatt Jacob /*-
290b80d21bSMatt Jacob  * Copyright (c) 2002, 2006 by Matthew Jacob
300b80d21bSMatt Jacob  * All rights reserved.
310b80d21bSMatt Jacob  *
320b80d21bSMatt Jacob  * Redistribution and use in source and binary forms, with or without
330b80d21bSMatt Jacob  * modification, are permitted provided that the following conditions are
340b80d21bSMatt Jacob  * met:
350b80d21bSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
360b80d21bSMatt Jacob  *    notice, this list of conditions and the following disclaimer.
370b80d21bSMatt Jacob  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
380b80d21bSMatt Jacob  *    substantially similar to the "NO WARRANTY" disclaimer below
390b80d21bSMatt Jacob  *    ("Disclaimer") and any redistribution must be conditioned upon including
400b80d21bSMatt Jacob  *    a substantially similar Disclaimer requirement for further binary
410b80d21bSMatt Jacob  *    redistribution.
420b80d21bSMatt Jacob  * 3. Neither the names of the above listed copyright holders nor the names
430b80d21bSMatt Jacob  *    of any contributors may be used to endorse or promote products derived
440b80d21bSMatt Jacob  *    from this software without specific prior written permission.
450b80d21bSMatt Jacob  *
460b80d21bSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
470b80d21bSMatt Jacob  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
480b80d21bSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
490b80d21bSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
500b80d21bSMatt Jacob  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
510b80d21bSMatt Jacob  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
520b80d21bSMatt Jacob  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
530b80d21bSMatt Jacob  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
540b80d21bSMatt Jacob  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
550b80d21bSMatt Jacob  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
560b80d21bSMatt Jacob  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
570b80d21bSMatt Jacob  *
580b80d21bSMatt Jacob  * Support from Chris Ellsworth in order to make SAS adapters work
590b80d21bSMatt Jacob  * is gratefully acknowledged.
60ec5fe39dSMatt Jacob  *
61ec5fe39dSMatt Jacob  *
62ec5fe39dSMatt Jacob  * Support from LSI-Logic has also gone a great deal toward making this a
63ec5fe39dSMatt Jacob  * workable subsystem and is gratefully acknowledged.
640b80d21bSMatt Jacob  */
650b80d21bSMatt Jacob /*-
66b0a2fdeeSScott Long  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
67b0a2fdeeSScott Long  * Copyright (c) 2005, WHEEL Sp. z o.o.
68b0a2fdeeSScott Long  * Copyright (c) 2004, 2005 Justin T. Gibbs
69b0a2fdeeSScott Long  * All rights reserved.
70b0a2fdeeSScott Long  *
71b0a2fdeeSScott Long  * Redistribution and use in source and binary forms, with or without
72b0a2fdeeSScott Long  * modification, are permitted provided that the following conditions are
73b0a2fdeeSScott Long  * met:
74b0a2fdeeSScott Long  * 1. Redistributions of source code must retain the above copyright
75b0a2fdeeSScott Long  *    notice, this list of conditions and the following disclaimer.
76b0a2fdeeSScott Long  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
77b0a2fdeeSScott Long  *    substantially similar to the "NO WARRANTY" disclaimer below
78b0a2fdeeSScott Long  *    ("Disclaimer") and any redistribution must be conditioned upon including
79b0a2fdeeSScott Long  *    a substantially similar Disclaimer requirement for further binary
80b0a2fdeeSScott Long  *    redistribution.
81286e947fSJustin T. Gibbs  * 3. Neither the names of the above listed copyright holders nor the names
82286e947fSJustin T. Gibbs  *    of any contributors may be used to endorse or promote products derived
83286e947fSJustin T. Gibbs  *    from this software without specific prior written permission.
84b0a2fdeeSScott Long  *
85b0a2fdeeSScott Long  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
86b0a2fdeeSScott Long  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87b0a2fdeeSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88b0a2fdeeSScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
89b0a2fdeeSScott Long  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
90b0a2fdeeSScott Long  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
91b0a2fdeeSScott Long  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
92b0a2fdeeSScott Long  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
93b0a2fdeeSScott Long  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
94b0a2fdeeSScott Long  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
95b0a2fdeeSScott Long  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
969b631363SMatt Jacob  */
979b631363SMatt Jacob 
989295c6c5SDavid E. O'Brien #include <sys/cdefs.h>
999295c6c5SDavid E. O'Brien __FBSDID("$FreeBSD$");
1009295c6c5SDavid E. O'Brien 
101b0a2fdeeSScott Long #include <dev/mpt/mpt.h>
102b0a2fdeeSScott Long #include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */
103b0a2fdeeSScott Long #include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */
104b0a2fdeeSScott Long 
105b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi.h>
106b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_ioc.h>
107c87e3f83SMatt Jacob #include <dev/mpt/mpilib/mpi_fc.h>
108c87e3f83SMatt Jacob #include <dev/mpt/mpilib/mpi_targ.h>
109b0a2fdeeSScott Long 
110b0a2fdeeSScott Long #include <sys/sysctl.h>
1117104aeefSMatt Jacob 
1129b631363SMatt Jacob #define MPT_MAX_TRYS 3
1139b631363SMatt Jacob #define MPT_MAX_WAIT 300000
1149b631363SMatt Jacob 
1159b631363SMatt Jacob static int maxwait_ack = 0;
1169b631363SMatt Jacob static int maxwait_int = 0;
1179b631363SMatt Jacob static int maxwait_state = 0;
1189b631363SMatt Jacob 
119a7303be1SMatt Jacob static TAILQ_HEAD(, mpt_softc)	mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq);
120b0a2fdeeSScott Long mpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS];
1219b631363SMatt Jacob 
122b0a2fdeeSScott Long static mpt_reply_handler_t mpt_default_reply_handler;
123b0a2fdeeSScott Long static mpt_reply_handler_t mpt_config_reply_handler;
124b0a2fdeeSScott Long static mpt_reply_handler_t mpt_handshake_reply_handler;
125b0a2fdeeSScott Long static mpt_reply_handler_t mpt_event_reply_handler;
126b0a2fdeeSScott Long static void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
127b0a2fdeeSScott Long 			       MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context);
128444dd2b6SMatt Jacob static int mpt_send_event_request(struct mpt_softc *mpt, int onoff);
129b0a2fdeeSScott Long static int mpt_soft_reset(struct mpt_softc *mpt);
130b0a2fdeeSScott Long static void mpt_hard_reset(struct mpt_softc *mpt);
1310e0521a1SMatt Jacob static int mpt_configure_ioc(struct mpt_softc *mpt, int, int);
132c87e3f83SMatt Jacob static int mpt_enable_ioc(struct mpt_softc *mpt, int);
133b0a2fdeeSScott Long 
134b0a2fdeeSScott Long /************************* Personality Module Support *************************/
135b0a2fdeeSScott Long /*
136b0a2fdeeSScott Long  * We include one extra entry that is guaranteed to be NULL
137b0a2fdeeSScott Long  * to simplify our itterator.
138b0a2fdeeSScott Long  */
139b0a2fdeeSScott Long static struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
140b0a2fdeeSScott Long static __inline struct mpt_personality*
141b0a2fdeeSScott Long 	mpt_pers_find(struct mpt_softc *, u_int);
142b0a2fdeeSScott Long static __inline struct mpt_personality*
143b0a2fdeeSScott Long 	mpt_pers_find_reverse(struct mpt_softc *, u_int);
144b0a2fdeeSScott Long 
145b0a2fdeeSScott Long static __inline struct mpt_personality *
146b0a2fdeeSScott Long mpt_pers_find(struct mpt_softc *mpt, u_int start_at)
147b0a2fdeeSScott Long {
148b0a2fdeeSScott Long 	KASSERT(start_at <= MPT_MAX_PERSONALITIES,
149b0a2fdeeSScott Long 		("mpt_pers_find: starting position out of range\n"));
150b0a2fdeeSScott Long 
151b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
152b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
153b0a2fdeeSScott Long 		start_at++;
154b0a2fdeeSScott Long 	}
155b0a2fdeeSScott Long 	return (mpt_personalities[start_at]);
156b0a2fdeeSScott Long }
157b0a2fdeeSScott Long 
158b0a2fdeeSScott Long /*
159c87e3f83SMatt Jacob  * Used infrequently, so no need to optimize like a forward
160b0a2fdeeSScott Long  * traversal where we use the MAX+1 is guaranteed to be NULL
161b0a2fdeeSScott Long  * trick.
162b0a2fdeeSScott Long  */
163b0a2fdeeSScott Long static __inline struct mpt_personality *
164b0a2fdeeSScott Long mpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
165b0a2fdeeSScott Long {
166b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
167b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
168b0a2fdeeSScott Long 		start_at--;
169b0a2fdeeSScott Long 	}
170b0a2fdeeSScott Long 	if (start_at < MPT_MAX_PERSONALITIES)
171b0a2fdeeSScott Long 		return (mpt_personalities[start_at]);
172b0a2fdeeSScott Long 	return (NULL);
173b0a2fdeeSScott Long }
174b0a2fdeeSScott Long 
175b0a2fdeeSScott Long #define MPT_PERS_FOREACH(mpt, pers)				\
176b0a2fdeeSScott Long 	for (pers = mpt_pers_find(mpt, /*start_at*/0);		\
177b0a2fdeeSScott Long 	     pers != NULL;					\
178b0a2fdeeSScott Long 	     pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
179b0a2fdeeSScott Long 
180b0a2fdeeSScott Long #define MPT_PERS_FOREACH_REVERSE(mpt, pers)				\
181b0a2fdeeSScott Long 	for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
182b0a2fdeeSScott Long 	     pers != NULL;						\
183b0a2fdeeSScott Long 	     pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
184b0a2fdeeSScott Long 
185b0a2fdeeSScott Long static mpt_load_handler_t      mpt_stdload;
186b0a2fdeeSScott Long static mpt_probe_handler_t     mpt_stdprobe;
187b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_stdattach;
188c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_stdenable;
189a7303be1SMatt Jacob static mpt_ready_handler_t     mpt_stdready;
190b0a2fdeeSScott Long static mpt_event_handler_t     mpt_stdevent;
191b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_stdreset;
192b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_stdshutdown;
193b0a2fdeeSScott Long static mpt_detach_handler_t    mpt_stddetach;
194b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_stdunload;
195b0a2fdeeSScott Long static struct mpt_personality mpt_default_personality =
196b0a2fdeeSScott Long {
197b0a2fdeeSScott Long 	.load		= mpt_stdload,
198b0a2fdeeSScott Long 	.probe		= mpt_stdprobe,
199b0a2fdeeSScott Long 	.attach		= mpt_stdattach,
200c87e3f83SMatt Jacob 	.enable		= mpt_stdenable,
201a7303be1SMatt Jacob 	.ready		= mpt_stdready,
202b0a2fdeeSScott Long 	.event		= mpt_stdevent,
203b0a2fdeeSScott Long 	.reset		= mpt_stdreset,
204b0a2fdeeSScott Long 	.shutdown	= mpt_stdshutdown,
205b0a2fdeeSScott Long 	.detach		= mpt_stddetach,
206b0a2fdeeSScott Long 	.unload		= mpt_stdunload
207b0a2fdeeSScott Long };
208b0a2fdeeSScott Long 
209b0a2fdeeSScott Long static mpt_load_handler_t      mpt_core_load;
210b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_core_attach;
211c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_core_enable;
212b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_core_ioc_reset;
213b0a2fdeeSScott Long static mpt_event_handler_t     mpt_core_event;
214b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_shutdown;
215b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_detach;
216b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_core_unload;
217b0a2fdeeSScott Long static struct mpt_personality mpt_core_personality =
218b0a2fdeeSScott Long {
219b0a2fdeeSScott Long 	.name		= "mpt_core",
220b0a2fdeeSScott Long 	.load		= mpt_core_load,
221b0a2fdeeSScott Long 	.attach		= mpt_core_attach,
222c87e3f83SMatt Jacob 	.enable		= mpt_core_enable,
223b0a2fdeeSScott Long 	.event		= mpt_core_event,
224b0a2fdeeSScott Long 	.reset		= mpt_core_ioc_reset,
225b0a2fdeeSScott Long 	.shutdown	= mpt_core_shutdown,
226b0a2fdeeSScott Long 	.detach		= mpt_core_detach,
227b0a2fdeeSScott Long 	.unload		= mpt_core_unload,
228b0a2fdeeSScott Long };
229b0a2fdeeSScott Long 
230b0a2fdeeSScott Long /*
231b0a2fdeeSScott Long  * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
232b0a2fdeeSScott Long  * ordering information.  We want the core to always register FIRST.
233b0a2fdeeSScott Long  * other modules are set to SI_ORDER_SECOND.
234b0a2fdeeSScott Long  */
235b0a2fdeeSScott Long static moduledata_t mpt_core_mod = {
236b0a2fdeeSScott Long 	"mpt_core", mpt_modevent, &mpt_core_personality
237b0a2fdeeSScott Long };
238b0a2fdeeSScott Long DECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
239b0a2fdeeSScott Long MODULE_VERSION(mpt_core, 1);
240b0a2fdeeSScott Long 
241c87e3f83SMatt Jacob #define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
242b0a2fdeeSScott Long 
243b0a2fdeeSScott Long int
244b0a2fdeeSScott Long mpt_modevent(module_t mod, int type, void *data)
245b0a2fdeeSScott Long {
246b0a2fdeeSScott Long 	struct mpt_personality *pers;
247b0a2fdeeSScott Long 	int error;
248b0a2fdeeSScott Long 
249b0a2fdeeSScott Long 	pers = (struct mpt_personality *)data;
250b0a2fdeeSScott Long 
251b0a2fdeeSScott Long 	error = 0;
252b0a2fdeeSScott Long 	switch (type) {
253b0a2fdeeSScott Long 	case MOD_LOAD:
254b0a2fdeeSScott Long 	{
255b0a2fdeeSScott Long 		mpt_load_handler_t **def_handler;
256b0a2fdeeSScott Long 		mpt_load_handler_t **pers_handler;
257b0a2fdeeSScott Long 		int i;
258b0a2fdeeSScott Long 
259b0a2fdeeSScott Long 		for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
260b0a2fdeeSScott Long 			if (mpt_personalities[i] == NULL)
261b0a2fdeeSScott Long 				break;
262b0a2fdeeSScott Long 		}
263b0a2fdeeSScott Long 		if (i >= MPT_MAX_PERSONALITIES) {
264b0a2fdeeSScott Long 			error = ENOMEM;
265b0a2fdeeSScott Long 			break;
266b0a2fdeeSScott Long 		}
267b0a2fdeeSScott Long 		pers->id = i;
268b0a2fdeeSScott Long 		mpt_personalities[i] = pers;
269b0a2fdeeSScott Long 
270b0a2fdeeSScott Long 		/* Install standard/noop handlers for any NULL entries. */
271b0a2fdeeSScott Long 		def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
272b0a2fdeeSScott Long 		pers_handler = MPT_PERS_FIRST_HANDLER(pers);
273b0a2fdeeSScott Long 		while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
274b0a2fdeeSScott Long 			if (*pers_handler == NULL)
275b0a2fdeeSScott Long 				*pers_handler = *def_handler;
276b0a2fdeeSScott Long 			pers_handler++;
277b0a2fdeeSScott Long 			def_handler++;
278b0a2fdeeSScott Long 		}
279b0a2fdeeSScott Long 
280b0a2fdeeSScott Long 		error = (pers->load(pers));
281b0a2fdeeSScott Long 		if (error != 0)
282b0a2fdeeSScott Long 			mpt_personalities[i] = NULL;
283b0a2fdeeSScott Long 		break;
284b0a2fdeeSScott Long 	}
285b0a2fdeeSScott Long 	case MOD_SHUTDOWN:
286b0a2fdeeSScott Long 		break;
287c87e3f83SMatt Jacob #if __FreeBSD_version >= 500000
288b0a2fdeeSScott Long 	case MOD_QUIESCE:
289b0a2fdeeSScott Long 		break;
290c87e3f83SMatt Jacob #endif
291b0a2fdeeSScott Long 	case MOD_UNLOAD:
292b0a2fdeeSScott Long 		error = pers->unload(pers);
293b0a2fdeeSScott Long 		mpt_personalities[pers->id] = NULL;
294b0a2fdeeSScott Long 		break;
295b0a2fdeeSScott Long 	default:
296b0a2fdeeSScott Long 		error = EINVAL;
297b0a2fdeeSScott Long 		break;
298b0a2fdeeSScott Long 	}
299b0a2fdeeSScott Long 	return (error);
300b0a2fdeeSScott Long }
301b0a2fdeeSScott Long 
302b0a2fdeeSScott Long int
303b0a2fdeeSScott Long mpt_stdload(struct mpt_personality *pers)
304b0a2fdeeSScott Long {
305b0a2fdeeSScott Long 	/* Load is always successfull. */
306b0a2fdeeSScott Long 	return (0);
307b0a2fdeeSScott Long }
308b0a2fdeeSScott Long 
309b0a2fdeeSScott Long int
310b0a2fdeeSScott Long mpt_stdprobe(struct mpt_softc *mpt)
311b0a2fdeeSScott Long {
312b0a2fdeeSScott Long 	/* Probe is always successfull. */
313b0a2fdeeSScott Long 	return (0);
314b0a2fdeeSScott Long }
315b0a2fdeeSScott Long 
316b0a2fdeeSScott Long int
317b0a2fdeeSScott Long mpt_stdattach(struct mpt_softc *mpt)
318b0a2fdeeSScott Long {
319b0a2fdeeSScott Long 	/* Attach is always successfull. */
320b0a2fdeeSScott Long 	return (0);
321b0a2fdeeSScott Long }
322b0a2fdeeSScott Long 
323b0a2fdeeSScott Long int
324c87e3f83SMatt Jacob mpt_stdenable(struct mpt_softc *mpt)
325c87e3f83SMatt Jacob {
326c87e3f83SMatt Jacob 	/* Enable is always successfull. */
327c87e3f83SMatt Jacob 	return (0);
328c87e3f83SMatt Jacob }
329c87e3f83SMatt Jacob 
330a7303be1SMatt Jacob void
331a7303be1SMatt Jacob mpt_stdready(struct mpt_softc *mpt)
332a7303be1SMatt Jacob {
333a7303be1SMatt Jacob }
334a7303be1SMatt Jacob 
335a7303be1SMatt Jacob 
336c87e3f83SMatt Jacob int
337444dd2b6SMatt Jacob mpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
338b0a2fdeeSScott Long {
339444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
340b0a2fdeeSScott Long 	/* Event was not for us. */
341b0a2fdeeSScott Long 	return (0);
342b0a2fdeeSScott Long }
343b0a2fdeeSScott Long 
344b0a2fdeeSScott Long void
345b0a2fdeeSScott Long mpt_stdreset(struct mpt_softc *mpt, int type)
346b0a2fdeeSScott Long {
347b0a2fdeeSScott Long }
348b0a2fdeeSScott Long 
349b0a2fdeeSScott Long void
350b0a2fdeeSScott Long mpt_stdshutdown(struct mpt_softc *mpt)
351b0a2fdeeSScott Long {
352b0a2fdeeSScott Long }
353b0a2fdeeSScott Long 
354b0a2fdeeSScott Long void
355b0a2fdeeSScott Long mpt_stddetach(struct mpt_softc *mpt)
356b0a2fdeeSScott Long {
357b0a2fdeeSScott Long }
358b0a2fdeeSScott Long 
359b0a2fdeeSScott Long int
360b0a2fdeeSScott Long mpt_stdunload(struct mpt_personality *pers)
361b0a2fdeeSScott Long {
362b0a2fdeeSScott Long 	/* Unload is always successfull. */
363b0a2fdeeSScott Long 	return (0);
364b0a2fdeeSScott Long }
365b0a2fdeeSScott Long 
366a7303be1SMatt Jacob /*
367a7303be1SMatt Jacob  * Post driver attachment, we may want to perform some global actions.
368a7303be1SMatt Jacob  * Here is the hook to do so.
369a7303be1SMatt Jacob  */
370a7303be1SMatt Jacob 
371a7303be1SMatt Jacob static void
372a7303be1SMatt Jacob mpt_postattach(void *unused)
373a7303be1SMatt Jacob {
374a7303be1SMatt Jacob 	struct mpt_softc *mpt;
375a7303be1SMatt Jacob 	struct mpt_personality *pers;
376a7303be1SMatt Jacob 
377a7303be1SMatt Jacob 	TAILQ_FOREACH(mpt, &mpt_tailq, links) {
378a7303be1SMatt Jacob 		MPT_PERS_FOREACH(mpt, pers)
379a7303be1SMatt Jacob 			pers->ready(mpt);
380a7303be1SMatt Jacob 	}
381a7303be1SMatt Jacob }
382a7303be1SMatt Jacob SYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL);
383a7303be1SMatt Jacob 
384a7303be1SMatt Jacob 
385b0a2fdeeSScott Long /******************************* Bus DMA Support ******************************/
386b0a2fdeeSScott Long void
387b0a2fdeeSScott Long mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
388b0a2fdeeSScott Long {
389b0a2fdeeSScott Long 	struct mpt_map_info *map_info;
390b0a2fdeeSScott Long 
391b0a2fdeeSScott Long 	map_info = (struct mpt_map_info *)arg;
392b0a2fdeeSScott Long 	map_info->error = error;
393b0a2fdeeSScott Long 	map_info->phys = segs->ds_addr;
394b0a2fdeeSScott Long }
395b0a2fdeeSScott Long 
396b0a2fdeeSScott Long /**************************** Reply/Event Handling ****************************/
397b0a2fdeeSScott Long int
398b0a2fdeeSScott Long mpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
399b0a2fdeeSScott Long 		     mpt_handler_t handler, uint32_t *phandler_id)
400b0a2fdeeSScott Long {
401b0a2fdeeSScott Long 
402b0a2fdeeSScott Long 	switch (type) {
403b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
404b0a2fdeeSScott Long 	{
405b0a2fdeeSScott Long 		u_int cbi;
406b0a2fdeeSScott Long 		u_int free_cbi;
407b0a2fdeeSScott Long 
408b0a2fdeeSScott Long 		if (phandler_id == NULL)
409b0a2fdeeSScott Long 			return (EINVAL);
410b0a2fdeeSScott Long 
411b0a2fdeeSScott Long 		free_cbi = MPT_HANDLER_ID_NONE;
412b0a2fdeeSScott Long 		for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
413b0a2fdeeSScott Long 			/*
414b0a2fdeeSScott Long 			 * If the same handler is registered multiple
415b0a2fdeeSScott Long 			 * times, don't error out.  Just return the
416b0a2fdeeSScott Long 			 * index of the original registration.
417b0a2fdeeSScott Long 			 */
418b0a2fdeeSScott Long 			if (mpt_reply_handlers[cbi] == handler.reply_handler) {
419b0a2fdeeSScott Long 				*phandler_id = MPT_CBI_TO_HID(cbi);
420b0a2fdeeSScott Long 				return (0);
421b0a2fdeeSScott Long 			}
422b0a2fdeeSScott Long 
423b0a2fdeeSScott Long 			/*
424b0a2fdeeSScott Long 			 * Fill from the front in the hope that
425b0a2fdeeSScott Long 			 * all registered handlers consume only a
426b0a2fdeeSScott Long 			 * single cache line.
427b0a2fdeeSScott Long 			 *
428b0a2fdeeSScott Long 			 * We don't break on the first empty slot so
429b0a2fdeeSScott Long 			 * that the full table is checked to see if
430b0a2fdeeSScott Long 			 * this handler was previously registered.
431b0a2fdeeSScott Long 			 */
432c87e3f83SMatt Jacob 			if (free_cbi == MPT_HANDLER_ID_NONE &&
433c87e3f83SMatt Jacob 			    (mpt_reply_handlers[cbi]
434b0a2fdeeSScott Long 			  == mpt_default_reply_handler))
435b0a2fdeeSScott Long 				free_cbi = cbi;
436b0a2fdeeSScott Long 		}
437c87e3f83SMatt Jacob 		if (free_cbi == MPT_HANDLER_ID_NONE) {
438b0a2fdeeSScott Long 			return (ENOMEM);
439c87e3f83SMatt Jacob 		}
440b0a2fdeeSScott Long 		mpt_reply_handlers[free_cbi] = handler.reply_handler;
441b0a2fdeeSScott Long 		*phandler_id = MPT_CBI_TO_HID(free_cbi);
442b0a2fdeeSScott Long 		break;
443b0a2fdeeSScott Long 	}
444b0a2fdeeSScott Long 	default:
445b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
446b0a2fdeeSScott Long 		return (EINVAL);
447b0a2fdeeSScott Long 	}
448b0a2fdeeSScott Long 	return (0);
449b0a2fdeeSScott Long }
450b0a2fdeeSScott Long 
451b0a2fdeeSScott Long int
452b0a2fdeeSScott Long mpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
453b0a2fdeeSScott Long 		       mpt_handler_t handler, uint32_t handler_id)
454b0a2fdeeSScott Long {
455b0a2fdeeSScott Long 
456b0a2fdeeSScott Long 	switch (type) {
457b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
458b0a2fdeeSScott Long 	{
459b0a2fdeeSScott Long 		u_int cbi;
460b0a2fdeeSScott Long 
461b0a2fdeeSScott Long 		cbi = MPT_CBI(handler_id);
462b0a2fdeeSScott Long 		if (cbi >= MPT_NUM_REPLY_HANDLERS
463b0a2fdeeSScott Long 		 || mpt_reply_handlers[cbi] != handler.reply_handler)
464b0a2fdeeSScott Long 			return (ENOENT);
465b0a2fdeeSScott Long 		mpt_reply_handlers[cbi] = mpt_default_reply_handler;
466b0a2fdeeSScott Long 		break;
467b0a2fdeeSScott Long 	}
468b0a2fdeeSScott Long 	default:
469b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
470b0a2fdeeSScott Long 		return (EINVAL);
471b0a2fdeeSScott Long 	}
472b0a2fdeeSScott Long 	return (0);
473b0a2fdeeSScott Long }
474b0a2fdeeSScott Long 
475b0a2fdeeSScott Long static int
476b0a2fdeeSScott Long mpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
477c87e3f83SMatt Jacob 	uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
478b0a2fdeeSScott Long {
479c87e3f83SMatt Jacob 	mpt_prt(mpt,
480c87e3f83SMatt Jacob 	    "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
481c87e3f83SMatt Jacob 	    req, req->serno, reply_desc, reply_frame);
482b0a2fdeeSScott Long 
483b0a2fdeeSScott Long 	if (reply_frame != NULL)
484b0a2fdeeSScott Long 		mpt_dump_reply_frame(mpt, reply_frame);
485b0a2fdeeSScott Long 
486c87e3f83SMatt Jacob 	mpt_prt(mpt, "Reply Frame Ignored\n");
487b0a2fdeeSScott Long 
488b0a2fdeeSScott Long 	return (/*free_reply*/TRUE);
489b0a2fdeeSScott Long }
490b0a2fdeeSScott Long 
491b0a2fdeeSScott Long static int
492b0a2fdeeSScott Long mpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
493c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
494b0a2fdeeSScott Long {
495b0a2fdeeSScott Long 	if (req != NULL) {
496b0a2fdeeSScott Long 
497b0a2fdeeSScott Long 		if (reply_frame != NULL) {
498b0a2fdeeSScott Long 			MSG_CONFIG *cfgp;
499b0a2fdeeSScott Long 			MSG_CONFIG_REPLY *reply;
500b0a2fdeeSScott Long 
501b0a2fdeeSScott Long 			cfgp = (MSG_CONFIG *)req->req_vbuf;
502b0a2fdeeSScott Long 			reply = (MSG_CONFIG_REPLY *)reply_frame;
503b0a2fdeeSScott Long 			req->IOCStatus = le16toh(reply_frame->IOCStatus);
504b0a2fdeeSScott Long 			bcopy(&reply->Header, &cfgp->Header,
505b0a2fdeeSScott Long 			      sizeof(cfgp->Header));
506b0a2fdeeSScott Long 		}
507b0a2fdeeSScott Long 		req->state &= ~REQ_STATE_QUEUED;
508b0a2fdeeSScott Long 		req->state |= REQ_STATE_DONE;
509b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5105e073106SMatt Jacob 		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
511b0a2fdeeSScott Long 			wakeup(req);
5126621d786SMatt Jacob 		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
5136621d786SMatt Jacob 			/*
5146621d786SMatt Jacob 			 * Whew- we can free this request (late completion)
5156621d786SMatt Jacob 			 */
5166621d786SMatt Jacob 			mpt_free_request(mpt, req);
517b0a2fdeeSScott Long 		}
5185e073106SMatt Jacob 	}
519b0a2fdeeSScott Long 
5205e073106SMatt Jacob 	return (TRUE);
521b0a2fdeeSScott Long }
522b0a2fdeeSScott Long 
523b0a2fdeeSScott Long static int
524b0a2fdeeSScott Long mpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
525c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
526b0a2fdeeSScott Long {
527b0a2fdeeSScott Long 	/* Nothing to be done. */
5285e073106SMatt Jacob 	return (TRUE);
529b0a2fdeeSScott Long }
530b0a2fdeeSScott Long 
531b0a2fdeeSScott Long static int
532b0a2fdeeSScott Long mpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
533c87e3f83SMatt Jacob     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
534b0a2fdeeSScott Long {
535b0a2fdeeSScott Long 	int free_reply;
536b0a2fdeeSScott Long 
5375e073106SMatt Jacob 	KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
5385e073106SMatt Jacob 	KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
539b0a2fdeeSScott Long 
540b0a2fdeeSScott Long 	free_reply = TRUE;
541b0a2fdeeSScott Long 	switch (reply_frame->Function) {
542b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_NOTIFICATION:
543b0a2fdeeSScott Long 	{
544b0a2fdeeSScott Long 		MSG_EVENT_NOTIFY_REPLY *msg;
545b0a2fdeeSScott Long 		struct mpt_personality *pers;
546b0a2fdeeSScott Long 		u_int handled;
547b0a2fdeeSScott Long 
548b0a2fdeeSScott Long 		handled = 0;
549b0a2fdeeSScott Long 		msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
5503f970273SJohn Birrell 		msg->EventDataLength = le16toh(msg->EventDataLength);
5513f970273SJohn Birrell 		msg->IOCStatus = le16toh(msg->IOCStatus);
5523f970273SJohn Birrell 		msg->IOCLogInfo = le32toh(msg->IOCLogInfo);
5533f970273SJohn Birrell 		msg->Event = le32toh(msg->Event);
554b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
555b0a2fdeeSScott Long 			handled += pers->event(mpt, req, msg);
556b0a2fdeeSScott Long 
557444dd2b6SMatt Jacob 		if (handled == 0 && mpt->mpt_pers_mask == 0) {
5581977cbd6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_INFO,
559444dd2b6SMatt Jacob 				"No Handlers For Any Event Notify Frames. "
560444dd2b6SMatt Jacob 				"Event %#x (ACK %sequired).\n",
561444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
562444dd2b6SMatt Jacob 		} else if (handled == 0) {
5631977cbd6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_WARN,
564444dd2b6SMatt Jacob 				"Unhandled Event Notify Frame. Event %#x "
565444dd2b6SMatt Jacob 				"(ACK %sequired).\n",
566444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
567444dd2b6SMatt Jacob 		}
568b0a2fdeeSScott Long 
569b0a2fdeeSScott Long 		if (msg->AckRequired) {
570b0a2fdeeSScott Long 			request_t *ack_req;
571b0a2fdeeSScott Long 			uint32_t context;
572b0a2fdeeSScott Long 
5733f970273SJohn Birrell 			context = req->index | MPT_REPLY_HANDLER_EVENTS;
5745e073106SMatt Jacob 			ack_req = mpt_get_request(mpt, FALSE);
575b0a2fdeeSScott Long 			if (ack_req == NULL) {
576b0a2fdeeSScott Long 				struct mpt_evtf_record *evtf;
577b0a2fdeeSScott Long 
578b0a2fdeeSScott Long 				evtf = (struct mpt_evtf_record *)reply_frame;
579b0a2fdeeSScott Long 				evtf->context = context;
580b0a2fdeeSScott Long 				LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
581b0a2fdeeSScott Long 				free_reply = FALSE;
582b0a2fdeeSScott Long 				break;
583b0a2fdeeSScott Long 			}
584b0a2fdeeSScott Long 			mpt_send_event_ack(mpt, ack_req, msg, context);
5855e073106SMatt Jacob 			/*
5865e073106SMatt Jacob 			 * Don't check for CONTINUATION_REPLY here
5875e073106SMatt Jacob 			 */
5885e073106SMatt Jacob 			return (free_reply);
589b0a2fdeeSScott Long 		}
590b0a2fdeeSScott Long 		break;
591b0a2fdeeSScott Long 	}
592b0a2fdeeSScott Long 	case MPI_FUNCTION_PORT_ENABLE:
593b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
594b0a2fdeeSScott Long 		break;
595b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_ACK:
596b0a2fdeeSScott Long 		break;
597b0a2fdeeSScott Long 	default:
5985e073106SMatt Jacob 		mpt_prt(mpt, "unknown event function: %x\n",
599b0a2fdeeSScott Long 			reply_frame->Function);
600b0a2fdeeSScott Long 		break;
601b0a2fdeeSScott Long 	}
602b0a2fdeeSScott Long 
6035e073106SMatt Jacob 	/*
6045e073106SMatt Jacob 	 * I'm not sure that this continuation stuff works as it should.
6055e073106SMatt Jacob 	 *
6065e073106SMatt Jacob 	 * I've had FC async events occur that free the frame up because
6075e073106SMatt Jacob 	 * the continuation bit isn't set, and then additional async events
6085e073106SMatt Jacob 	 * then occur using the same context. As you might imagine, this
6095e073106SMatt Jacob 	 * leads to Very Bad Thing.
6105e073106SMatt Jacob 	 *
6115e073106SMatt Jacob 	 *  Let's just be safe for now and not free them up until we figure
6125e073106SMatt Jacob 	 * out what's actually happening here.
6135e073106SMatt Jacob 	 */
6145e073106SMatt Jacob #if	0
6155e073106SMatt Jacob 	if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
616b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
617b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
6185e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
6195e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
6205e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6215e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6225e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6235e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6245e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
625b0a2fdeeSScott Long 		}
6265e073106SMatt Jacob 	} else {
6275e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
6285e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
6295e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6305e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6315e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6325e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6335e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
6345e073106SMatt Jacob 		}
6355e073106SMatt Jacob 		mpt_prtc(mpt, "\n");
6365e073106SMatt Jacob 	}
6375e073106SMatt Jacob #endif
638b0a2fdeeSScott Long 	return (free_reply);
639b0a2fdeeSScott Long }
640b0a2fdeeSScott Long 
641b0a2fdeeSScott Long /*
642b0a2fdeeSScott Long  * Process an asynchronous event from the IOC.
643b0a2fdeeSScott Long  */
644b0a2fdeeSScott Long static int
645b0a2fdeeSScott Long mpt_core_event(struct mpt_softc *mpt, request_t *req,
646b0a2fdeeSScott Long 	       MSG_EVENT_NOTIFY_REPLY *msg)
647b0a2fdeeSScott Long {
648444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
649444dd2b6SMatt Jacob                  msg->Event & 0xFF);
650b0a2fdeeSScott Long 	switch(msg->Event & 0xFF) {
651b0a2fdeeSScott Long 	case MPI_EVENT_NONE:
652b0a2fdeeSScott Long 		break;
653b0a2fdeeSScott Long 	case MPI_EVENT_LOG_DATA:
654b0a2fdeeSScott Long 	{
655b0a2fdeeSScott Long 		int i;
656b0a2fdeeSScott Long 
657b0a2fdeeSScott Long 		/* Some error occured that LSI wants logged */
658b0a2fdeeSScott Long 		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
659b0a2fdeeSScott Long 			msg->IOCLogInfo);
660b0a2fdeeSScott Long 		mpt_prt(mpt, "\tEvtLogData: Event Data:");
661b0a2fdeeSScott Long 		for (i = 0; i < msg->EventDataLength; i++)
662b0a2fdeeSScott Long 			mpt_prtc(mpt, "  %08x", msg->Data[i]);
663b0a2fdeeSScott Long 		mpt_prtc(mpt, "\n");
664b0a2fdeeSScott Long 		break;
665b0a2fdeeSScott Long 	}
666b0a2fdeeSScott Long 	case MPI_EVENT_EVENT_CHANGE:
667b0a2fdeeSScott Long 		/*
668b0a2fdeeSScott Long 		 * This is just an acknowledgement
669b0a2fdeeSScott Long 		 * of our mpt_send_event_request.
670b0a2fdeeSScott Long 		 */
671b0a2fdeeSScott Long 		break;
672444dd2b6SMatt Jacob 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
673444dd2b6SMatt Jacob 		break;
674b0a2fdeeSScott Long 	default:
6755e073106SMatt Jacob 		return (0);
676b0a2fdeeSScott Long 		break;
677b0a2fdeeSScott Long 	}
6785e073106SMatt Jacob 	return (1);
679b0a2fdeeSScott Long }
680b0a2fdeeSScott Long 
681b0a2fdeeSScott Long static void
682b0a2fdeeSScott Long mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
683b0a2fdeeSScott Long 		   MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
684b0a2fdeeSScott Long {
685b0a2fdeeSScott Long 	MSG_EVENT_ACK *ackp;
686b0a2fdeeSScott Long 
687b0a2fdeeSScott Long 	ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
6885e073106SMatt Jacob 	memset(ackp, 0, sizeof (*ackp));
689b0a2fdeeSScott Long 	ackp->Function = MPI_FUNCTION_EVENT_ACK;
6903f970273SJohn Birrell 	ackp->Event = htole32(msg->Event);
6913f970273SJohn Birrell 	ackp->EventContext = htole32(msg->EventContext);
6923f970273SJohn Birrell 	ackp->MsgContext = htole32(context);
693b0a2fdeeSScott Long 	mpt_check_doorbell(mpt);
694b0a2fdeeSScott Long 	mpt_send_cmd(mpt, ack_req);
695b0a2fdeeSScott Long }
696b0a2fdeeSScott Long 
697b0a2fdeeSScott Long /***************************** Interrupt Handling *****************************/
698b0a2fdeeSScott Long void
699b0a2fdeeSScott Long mpt_intr(void *arg)
700b0a2fdeeSScott Long {
701b0a2fdeeSScott Long 	struct mpt_softc *mpt;
702b0a2fdeeSScott Long 	uint32_t reply_desc;
703c87e3f83SMatt Jacob 	int ntrips = 0;
704b0a2fdeeSScott Long 
705b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)arg;
7065089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
707b0a2fdeeSScott Long 	while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
708b0a2fdeeSScott Long 		request_t	  *req;
709b0a2fdeeSScott Long 		MSG_DEFAULT_REPLY *reply_frame;
710b0a2fdeeSScott Long 		uint32_t	   reply_baddr;
711c87e3f83SMatt Jacob 		uint32_t           ctxt_idx;
712b0a2fdeeSScott Long 		u_int		   cb_index;
713b0a2fdeeSScott Long 		u_int		   req_index;
714b0a2fdeeSScott Long 		int		   free_rf;
715b0a2fdeeSScott Long 
716b0a2fdeeSScott Long 		req = NULL;
717b0a2fdeeSScott Long 		reply_frame = NULL;
718b0a2fdeeSScott Long 		reply_baddr = 0;
719b0a2fdeeSScott Long 		if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
720b0a2fdeeSScott Long 			u_int offset;
721b0a2fdeeSScott Long 			/*
722b0a2fdeeSScott Long 			 * Insure that the reply frame is coherent.
723b0a2fdeeSScott Long 			 */
7245e073106SMatt Jacob 			reply_baddr = MPT_REPLY_BADDR(reply_desc);
725b0a2fdeeSScott Long 			offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
726c87e3f83SMatt Jacob 			bus_dmamap_sync_range(mpt->reply_dmat,
727c87e3f83SMatt Jacob 			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
728b0a2fdeeSScott Long 			    BUS_DMASYNC_POSTREAD);
729b0a2fdeeSScott Long 			reply_frame = MPT_REPLY_OTOV(mpt, offset);
730c87e3f83SMatt Jacob 			ctxt_idx = le32toh(reply_frame->MsgContext);
731c87e3f83SMatt Jacob 		} else {
732c87e3f83SMatt Jacob 			uint32_t type;
733b0a2fdeeSScott Long 
734c87e3f83SMatt Jacob 			type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
735c87e3f83SMatt Jacob 			ctxt_idx = reply_desc;
736c87e3f83SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
737c87e3f83SMatt Jacob 				    reply_desc);
738c87e3f83SMatt Jacob 
739c87e3f83SMatt Jacob 			switch (type) {
740c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
741c87e3f83SMatt Jacob 				ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
742c87e3f83SMatt Jacob 				break;
743c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
744c87e3f83SMatt Jacob 				ctxt_idx = GET_IO_INDEX(reply_desc);
745c87e3f83SMatt Jacob 				if (mpt->tgt_cmd_ptrs == NULL) {
746c87e3f83SMatt Jacob 					mpt_prt(mpt,
747c87e3f83SMatt Jacob 					    "mpt_intr: no target cmd ptrs\n");
748c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
749c87e3f83SMatt Jacob 					break;
750c87e3f83SMatt Jacob 				}
751c87e3f83SMatt Jacob 				if (ctxt_idx >= mpt->tgt_cmds_allocated) {
752c87e3f83SMatt Jacob 					mpt_prt(mpt,
753c87e3f83SMatt Jacob 					    "mpt_intr: bad tgt cmd ctxt %u\n",
754c87e3f83SMatt Jacob 					    ctxt_idx);
755c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
756c87e3f83SMatt Jacob 					ntrips = 1000;
757c87e3f83SMatt Jacob 					break;
758c87e3f83SMatt Jacob 				}
759c87e3f83SMatt Jacob 				req = mpt->tgt_cmd_ptrs[ctxt_idx];
760c87e3f83SMatt Jacob 				if (req == NULL) {
761c87e3f83SMatt Jacob 					mpt_prt(mpt, "no request backpointer "
762c87e3f83SMatt Jacob 					    "at index %u", ctxt_idx);
763c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
764c87e3f83SMatt Jacob 					ntrips = 1000;
765c87e3f83SMatt Jacob 					break;
766c87e3f83SMatt Jacob 				}
767c87e3f83SMatt Jacob 				/*
768c87e3f83SMatt Jacob 				 * Reformulate ctxt_idx to be just as if
769c87e3f83SMatt Jacob 				 * it were another type of context reply
770c87e3f83SMatt Jacob 				 * so the code below will find the request
771c87e3f83SMatt Jacob 				 * via indexing into the pool.
772c87e3f83SMatt Jacob 				 */
773c87e3f83SMatt Jacob 				ctxt_idx =
774c87e3f83SMatt Jacob 				    req->index | mpt->scsi_tgt_handler_id;
775c87e3f83SMatt Jacob 				req = NULL;
776c87e3f83SMatt Jacob 				break;
777c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_LAN:
778c87e3f83SMatt Jacob 				mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
779c87e3f83SMatt Jacob 				    reply_desc);
780c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
781c87e3f83SMatt Jacob 				break;
782c87e3f83SMatt Jacob 			default:
783c87e3f83SMatt Jacob 				mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
784c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
785c87e3f83SMatt Jacob 				break;
786c87e3f83SMatt Jacob 			}
787c87e3f83SMatt Jacob 			if (reply_desc == MPT_REPLY_EMPTY) {
788c87e3f83SMatt Jacob 				if (ntrips++ > 1000) {
789c87e3f83SMatt Jacob 					break;
790c87e3f83SMatt Jacob 				}
791c87e3f83SMatt Jacob 				continue;
792c87e3f83SMatt Jacob 			}
793c87e3f83SMatt Jacob 		}
794c87e3f83SMatt Jacob 
795c87e3f83SMatt Jacob 		cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
796c87e3f83SMatt Jacob 		req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
797c87e3f83SMatt Jacob 		if (req_index < MPT_MAX_REQUESTS(mpt)) {
798c87e3f83SMatt Jacob 			req = &mpt->request_pool[req_index];
7995e073106SMatt Jacob 		} else {
8005e073106SMatt Jacob 			mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
8015e073106SMatt Jacob 			    " 0x%x)\n", req_index, reply_desc);
802c87e3f83SMatt Jacob 		}
803c87e3f83SMatt Jacob 
804c87e3f83SMatt Jacob 		free_rf = mpt_reply_handlers[cb_index](mpt, req,
805c87e3f83SMatt Jacob 		    reply_desc, reply_frame);
806b0a2fdeeSScott Long 
8075e073106SMatt Jacob 		if (reply_frame != NULL && free_rf) {
808b0a2fdeeSScott Long 			mpt_free_reply(mpt, reply_baddr);
8095e073106SMatt Jacob 		}
810c87e3f83SMatt Jacob 
811c87e3f83SMatt Jacob 		/*
812c87e3f83SMatt Jacob 		 * If we got ourselves disabled, don't get stuck in a loop
813c87e3f83SMatt Jacob 		 */
814c87e3f83SMatt Jacob 		if (mpt->disabled) {
815c87e3f83SMatt Jacob 			mpt_disable_ints(mpt);
816c87e3f83SMatt Jacob 			break;
817c87e3f83SMatt Jacob 		}
818c87e3f83SMatt Jacob 		if (ntrips++ > 1000) {
819c87e3f83SMatt Jacob 			break;
820c87e3f83SMatt Jacob 		}
821b0a2fdeeSScott Long 	}
8225089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
823b0a2fdeeSScott Long }
824b0a2fdeeSScott Long 
825b0a2fdeeSScott Long /******************************* Error Recovery *******************************/
826b0a2fdeeSScott Long void
827b0a2fdeeSScott Long mpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
828b0a2fdeeSScott Long 			    u_int iocstatus)
829b0a2fdeeSScott Long {
830b0a2fdeeSScott Long 	MSG_DEFAULT_REPLY  ioc_status_frame;
831b0a2fdeeSScott Long 	request_t	  *req;
832b0a2fdeeSScott Long 
8335e073106SMatt Jacob 	memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
834b0a2fdeeSScott Long 	ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
835b0a2fdeeSScott Long 	ioc_status_frame.IOCStatus = iocstatus;
836b0a2fdeeSScott Long 	while((req = TAILQ_FIRST(chain)) != NULL) {
837b0a2fdeeSScott Long 		MSG_REQUEST_HEADER *msg_hdr;
838b0a2fdeeSScott Long 		u_int		    cb_index;
8395e073106SMatt Jacob 
8409b7de735SMatt Jacob 		TAILQ_REMOVE(chain, req, links);
841b0a2fdeeSScott Long 		msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
842b0a2fdeeSScott Long 		ioc_status_frame.Function = msg_hdr->Function;
843b0a2fdeeSScott Long 		ioc_status_frame.MsgContext = msg_hdr->MsgContext;
844b0a2fdeeSScott Long 		cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
845c87e3f83SMatt Jacob 		mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
846c87e3f83SMatt Jacob 		    &ioc_status_frame);
847b0a2fdeeSScott Long 	}
848b0a2fdeeSScott Long }
849b0a2fdeeSScott Long 
850b0a2fdeeSScott Long /********************************* Diagnostics ********************************/
851b0a2fdeeSScott Long /*
852b0a2fdeeSScott Long  * Perform a diagnostic dump of a reply frame.
853b0a2fdeeSScott Long  */
854b0a2fdeeSScott Long void
855b0a2fdeeSScott Long mpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
856b0a2fdeeSScott Long {
857b0a2fdeeSScott Long 	mpt_prt(mpt, "Address Reply:\n");
858b0a2fdeeSScott Long 	mpt_print_reply(reply_frame);
859b0a2fdeeSScott Long }
860b0a2fdeeSScott Long 
861b0a2fdeeSScott Long /******************************* Doorbell Access ******************************/
862b0a2fdeeSScott Long static __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
863b0a2fdeeSScott Long static __inline  uint32_t mpt_rd_intr(struct mpt_softc *mpt);
864b0a2fdeeSScott Long 
865b0a2fdeeSScott Long static __inline uint32_t
866b0a2fdeeSScott Long mpt_rd_db(struct mpt_softc *mpt)
8679b631363SMatt Jacob {
8689b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_DOORBELL);
8699b631363SMatt Jacob }
8709b631363SMatt Jacob 
871b0a2fdeeSScott Long static __inline uint32_t
872b0a2fdeeSScott Long mpt_rd_intr(struct mpt_softc *mpt)
8739b631363SMatt Jacob {
8749b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
8759b631363SMatt Jacob }
8769b631363SMatt Jacob 
8779b631363SMatt Jacob /* Busy wait for a door bell to be read by IOC */
8789b631363SMatt Jacob static int
879b0a2fdeeSScott Long mpt_wait_db_ack(struct mpt_softc *mpt)
8809b631363SMatt Jacob {
8819b631363SMatt Jacob 	int i;
8829b631363SMatt Jacob 	for (i=0; i < MPT_MAX_WAIT; i++) {
8839b631363SMatt Jacob 		if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
8849b631363SMatt Jacob 			maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
885c87e3f83SMatt Jacob 			return (MPT_OK);
8869b631363SMatt Jacob 		}
887c87e3f83SMatt Jacob 		DELAY(200);
8889b631363SMatt Jacob 	}
889c87e3f83SMatt Jacob 	return (MPT_FAIL);
8909b631363SMatt Jacob }
8919b631363SMatt Jacob 
8929b631363SMatt Jacob /* Busy wait for a door bell interrupt */
8939b631363SMatt Jacob static int
894b0a2fdeeSScott Long mpt_wait_db_int(struct mpt_softc *mpt)
8959b631363SMatt Jacob {
8969b631363SMatt Jacob 	int i;
8979b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
8989b631363SMatt Jacob 		if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
8999b631363SMatt Jacob 			maxwait_int = i > maxwait_int ? i : maxwait_int;
9009b631363SMatt Jacob 			return MPT_OK;
9019b631363SMatt Jacob 		}
9029b631363SMatt Jacob 		DELAY(100);
9039b631363SMatt Jacob 	}
904c87e3f83SMatt Jacob 	return (MPT_FAIL);
9059b631363SMatt Jacob }
9069b631363SMatt Jacob 
9079b631363SMatt Jacob /* Wait for IOC to transition to a give state */
9089b631363SMatt Jacob void
909b0a2fdeeSScott Long mpt_check_doorbell(struct mpt_softc *mpt)
9109b631363SMatt Jacob {
911b0a2fdeeSScott Long 	uint32_t db = mpt_rd_db(mpt);
9129b631363SMatt Jacob 	if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
913b0a2fdeeSScott Long 		mpt_prt(mpt, "Device not running\n");
9149b631363SMatt Jacob 		mpt_print_db(db);
9159b631363SMatt Jacob 	}
9169b631363SMatt Jacob }
9179b631363SMatt Jacob 
9189b631363SMatt Jacob /* Wait for IOC to transition to a give state */
9199b631363SMatt Jacob static int
920b0a2fdeeSScott Long mpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
9219b631363SMatt Jacob {
9229b631363SMatt Jacob 	int i;
9239b631363SMatt Jacob 
9249b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
925b0a2fdeeSScott Long 		uint32_t db = mpt_rd_db(mpt);
9269b631363SMatt Jacob 		if (MPT_STATE(db) == state) {
9279b631363SMatt Jacob 			maxwait_state = i > maxwait_state ? i : maxwait_state;
9289b631363SMatt Jacob 			return (MPT_OK);
9299b631363SMatt Jacob 		}
9309b631363SMatt Jacob 		DELAY(100);
9319b631363SMatt Jacob 	}
9329b631363SMatt Jacob 	return (MPT_FAIL);
9339b631363SMatt Jacob }
9349b631363SMatt Jacob 
9359b631363SMatt Jacob 
936b0a2fdeeSScott Long /************************* Intialization/Configuration ************************/
937b0a2fdeeSScott Long static int mpt_download_fw(struct mpt_softc *mpt);
938b0a2fdeeSScott Long 
9399b631363SMatt Jacob /* Issue the reset COMMAND to the IOC */
940b0a2fdeeSScott Long static int
941b0a2fdeeSScott Long mpt_soft_reset(struct mpt_softc *mpt)
9429b631363SMatt Jacob {
943b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
9449b631363SMatt Jacob 
9459b631363SMatt Jacob 	/* Have to use hard reset if we are not in Running state */
9469b631363SMatt Jacob 	if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
947b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device not running\n");
948c87e3f83SMatt Jacob 		return (MPT_FAIL);
9499b631363SMatt Jacob 	}
9509b631363SMatt Jacob 
9519b631363SMatt Jacob 	/* If door bell is in use we don't have a chance of getting
9529b631363SMatt Jacob 	 * a word in since the IOC probably crashed in message
9539b631363SMatt Jacob 	 * processing. So don't waste our time.
9549b631363SMatt Jacob 	 */
9559b631363SMatt Jacob 	if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
956b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
957c87e3f83SMatt Jacob 		return (MPT_FAIL);
9589b631363SMatt Jacob 	}
9599b631363SMatt Jacob 
9609b631363SMatt Jacob 	/* Send the reset request to the IOC */
9619b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL,
9629b631363SMatt Jacob 	    MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
9639b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
964b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: ack timeout\n");
965c87e3f83SMatt Jacob 		return (MPT_FAIL);
9669b631363SMatt Jacob 	}
9679b631363SMatt Jacob 
9689b631363SMatt Jacob 	/* Wait for the IOC to reload and come out of reset state */
9699b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
970b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device did not restart\n");
971c87e3f83SMatt Jacob 		return (MPT_FAIL);
9729b631363SMatt Jacob 	}
9739b631363SMatt Jacob 
9749b631363SMatt Jacob 	return MPT_OK;
9759b631363SMatt Jacob }
9769b631363SMatt Jacob 
977b0a2fdeeSScott Long static int
978b0a2fdeeSScott Long mpt_enable_diag_mode(struct mpt_softc *mpt)
979b0a2fdeeSScott Long {
980b0a2fdeeSScott Long 	int try;
981b0a2fdeeSScott Long 
982b0a2fdeeSScott Long 	try = 20;
983b0a2fdeeSScott Long 	while (--try) {
984b0a2fdeeSScott Long 
985b0a2fdeeSScott Long 		if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
986b0a2fdeeSScott Long 			break;
987b0a2fdeeSScott Long 
988b0a2fdeeSScott Long 		/* Enable diagnostic registers */
989b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
990b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
991b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
992b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
993b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
994b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
995b0a2fdeeSScott Long 
996b0a2fdeeSScott Long 		DELAY(100000);
997b0a2fdeeSScott Long 	}
998b0a2fdeeSScott Long 	if (try == 0)
999b0a2fdeeSScott Long 		return (EIO);
1000b0a2fdeeSScott Long 	return (0);
1001b0a2fdeeSScott Long }
1002b0a2fdeeSScott Long 
1003b0a2fdeeSScott Long static void
1004b0a2fdeeSScott Long mpt_disable_diag_mode(struct mpt_softc *mpt)
1005b0a2fdeeSScott Long {
1006b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
1007b0a2fdeeSScott Long }
1008b0a2fdeeSScott Long 
10099b631363SMatt Jacob /* This is a magic diagnostic reset that resets all the ARM
10109b631363SMatt Jacob  * processors in the chip.
10119b631363SMatt Jacob  */
1012b0a2fdeeSScott Long static void
1013b0a2fdeeSScott Long mpt_hard_reset(struct mpt_softc *mpt)
10149b631363SMatt Jacob {
1015b0a2fdeeSScott Long 	int error;
1016b0a2fdeeSScott Long 	int wait;
1017b0a2fdeeSScott Long 	uint32_t diagreg;
10189b631363SMatt Jacob 
1019b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
1020b0a2fdeeSScott Long 
1021b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
1022b0a2fdeeSScott Long 	if (error) {
1023b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
1024b0a2fdeeSScott Long 		mpt_prt(mpt, "Trying to reset anyway.\n");
1025b0a2fdeeSScott Long 	}
1026b0a2fdeeSScott Long 
1027b0a2fdeeSScott Long 	diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1028b0a2fdeeSScott Long 
1029b0a2fdeeSScott Long 	/*
1030b0a2fdeeSScott Long 	 * This appears to be a workaround required for some
1031b0a2fdeeSScott Long 	 * firmware or hardware revs.
1032b0a2fdeeSScott Long 	 */
1033b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1034b0a2fdeeSScott Long 	DELAY(1000);
10359b631363SMatt Jacob 
10369b631363SMatt Jacob 	/* Diag. port is now active so we can now hit the reset bit */
1037b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
10389b631363SMatt Jacob 
1039b0a2fdeeSScott Long         /*
1040b0a2fdeeSScott Long          * Ensure that the reset has finished.  We delay 1ms
1041b0a2fdeeSScott Long          * prior to reading the register to make sure the chip
1042b0a2fdeeSScott Long          * has sufficiently completed its reset to handle register
1043b0a2fdeeSScott Long          * accesses.
1044b0a2fdeeSScott Long          */
1045b0a2fdeeSScott Long 	wait = 5000;
1046b0a2fdeeSScott Long 	do {
1047b0a2fdeeSScott Long 		DELAY(1000);
1048b0a2fdeeSScott Long 		diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1049b0a2fdeeSScott Long 	} while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
10509b631363SMatt Jacob 
1051b0a2fdeeSScott Long 	if (wait == 0) {
1052b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Failed hard reset! "
1053b0a2fdeeSScott Long 			"Trying to initialize anyway.\n");
10549b631363SMatt Jacob 	}
10559b631363SMatt Jacob 
1056b0a2fdeeSScott Long 	/*
1057b0a2fdeeSScott Long 	 * If we have firmware to download, it must be loaded before
1058b0a2fdeeSScott Long 	 * the controller will become operational.  Do so now.
1059b0a2fdeeSScott Long 	 */
1060b0a2fdeeSScott Long 	if (mpt->fw_image != NULL) {
1061b0a2fdeeSScott Long 
1062b0a2fdeeSScott Long 		error = mpt_download_fw(mpt);
1063b0a2fdeeSScott Long 
1064b0a2fdeeSScott Long 		if (error) {
1065b0a2fdeeSScott Long 			mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1066b0a2fdeeSScott Long 			mpt_prt(mpt, "Trying to initialize anyway.\n");
10679b631363SMatt Jacob 		}
1068b0a2fdeeSScott Long 	}
1069b0a2fdeeSScott Long 
1070b0a2fdeeSScott Long 	/*
1071b0a2fdeeSScott Long 	 * Reseting the controller should have disabled write
1072b0a2fdeeSScott Long 	 * access to the diagnostic registers, but disable
1073b0a2fdeeSScott Long 	 * manually to be sure.
1074b0a2fdeeSScott Long 	 */
1075b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
1076b0a2fdeeSScott Long }
1077b0a2fdeeSScott Long 
1078b0a2fdeeSScott Long static void
1079b0a2fdeeSScott Long mpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1080b0a2fdeeSScott Long {
1081b0a2fdeeSScott Long 	/*
1082b0a2fdeeSScott Long 	 * Complete all pending requests with a status
1083b0a2fdeeSScott Long 	 * appropriate for an IOC reset.
1084b0a2fdeeSScott Long 	 */
1085b0a2fdeeSScott Long 	mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1086b0a2fdeeSScott Long 				   MPI_IOCSTATUS_INVALID_STATE);
1087b0a2fdeeSScott Long }
1088b0a2fdeeSScott Long 
10899b631363SMatt Jacob 
10909b631363SMatt Jacob /*
10919b631363SMatt Jacob  * Reset the IOC when needed. Try software command first then if needed
10929b631363SMatt Jacob  * poke at the magic diagnostic reset. Note that a hard reset resets
10939b631363SMatt Jacob  * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
10949b631363SMatt Jacob  * fouls up the PCI configuration registers.
10959b631363SMatt Jacob  */
10969b631363SMatt Jacob int
1097b0a2fdeeSScott Long mpt_reset(struct mpt_softc *mpt, int reinit)
10989b631363SMatt Jacob {
1099b0a2fdeeSScott Long 	struct	mpt_personality *pers;
11009b631363SMatt Jacob 	int	ret;
110129ae59edSMatt Jacob 	int	retry_cnt = 0;
11029b631363SMatt Jacob 
110329ae59edSMatt Jacob 	/*
110429ae59edSMatt Jacob 	 * Try a soft reset. If that fails, get out the big hammer.
110529ae59edSMatt Jacob 	 */
110629ae59edSMatt Jacob  again:
11079b631363SMatt Jacob 	if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
110829ae59edSMatt Jacob 		int	cnt;
110929ae59edSMatt Jacob 		for (cnt = 0; cnt < 5; cnt++) {
11109b631363SMatt Jacob 			/* Failed; do a hard reset */
11119b631363SMatt Jacob 			mpt_hard_reset(mpt);
11129b631363SMatt Jacob 
111329ae59edSMatt Jacob 			/*
111429ae59edSMatt Jacob 			 * Wait for the IOC to reload
111529ae59edSMatt Jacob 			 * and come out of reset state
111629ae59edSMatt Jacob 			 */
11179b631363SMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
111829ae59edSMatt Jacob 			if (ret == MPT_OK) {
111929ae59edSMatt Jacob 				break;
112029ae59edSMatt Jacob 			}
112129ae59edSMatt Jacob 			/*
112229ae59edSMatt Jacob 			 * Okay- try to check again...
112329ae59edSMatt Jacob 			 */
112429ae59edSMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
112529ae59edSMatt Jacob 			if (ret == MPT_OK) {
112629ae59edSMatt Jacob 				break;
112729ae59edSMatt Jacob 			}
112829ae59edSMatt Jacob 			mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
112929ae59edSMatt Jacob 			    retry_cnt, cnt);
113029ae59edSMatt Jacob 		}
11319b631363SMatt Jacob 	}
1132b0a2fdeeSScott Long 
113329ae59edSMatt Jacob 	if (retry_cnt == 0) {
1134b0a2fdeeSScott Long 		/*
1135b0a2fdeeSScott Long 		 * Invoke reset handlers.  We bump the reset count so
1136b0a2fdeeSScott Long 		 * that mpt_wait_req() understands that regardless of
1137b0a2fdeeSScott Long 		 * the specified wait condition, it should stop its wait.
1138b0a2fdeeSScott Long 		 */
1139b0a2fdeeSScott Long 		mpt->reset_cnt++;
1140b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
1141b0a2fdeeSScott Long 			pers->reset(mpt, ret);
114229ae59edSMatt Jacob 	}
1143b0a2fdeeSScott Long 
11445e073106SMatt Jacob 	if (reinit) {
1145c87e3f83SMatt Jacob 		ret = mpt_enable_ioc(mpt, 1);
114629ae59edSMatt Jacob 		if (ret == MPT_OK) {
114729ae59edSMatt Jacob 			mpt_enable_ints(mpt);
114829ae59edSMatt Jacob 		}
114929ae59edSMatt Jacob 	}
115029ae59edSMatt Jacob 	if (ret != MPT_OK && retry_cnt++ < 2) {
115129ae59edSMatt Jacob 		goto again;
115229ae59edSMatt Jacob 	}
11539b631363SMatt Jacob 	return ret;
11549b631363SMatt Jacob }
11559b631363SMatt Jacob 
11569b631363SMatt Jacob /* Return a command buffer to the free queue */
11579b631363SMatt Jacob void
1158b0a2fdeeSScott Long mpt_free_request(struct mpt_softc *mpt, request_t *req)
11599b631363SMatt Jacob {
1160444dd2b6SMatt Jacob 	request_t *nxt;
1161b0a2fdeeSScott Long 	struct mpt_evtf_record *record;
1162b0a2fdeeSScott Long 	uint32_t reply_baddr;
1163b0a2fdeeSScott Long 
11647dec90bcSMatt Jacob 	if (req == NULL || req != &mpt->request_pool[req->index]) {
11659b631363SMatt Jacob 		panic("mpt_free_request bad req ptr\n");
11669b631363SMatt Jacob 		return;
11679b631363SMatt Jacob 	}
1168444dd2b6SMatt Jacob 	if ((nxt = req->chain) != NULL) {
1169444dd2b6SMatt Jacob 		req->chain = NULL;
1170444dd2b6SMatt Jacob 		mpt_free_request(mpt, nxt);	/* NB: recursion */
1171444dd2b6SMatt Jacob 	}
11725e073106SMatt Jacob 	KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
11735e073106SMatt Jacob 	KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
11745089bd63SMatt Jacob 	KASSERT(MPT_OWNED(mpt), ("mpt_free_request: mpt not locked\n"));
11755089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
11765089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x already on freelist",
11775089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
11785089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
11795089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x on pending list",
11805089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
11815089bd63SMatt Jacob #ifdef	INVARIANTS
11825089bd63SMatt Jacob 	mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
11835089bd63SMatt Jacob #endif
11845e073106SMatt Jacob 
11859b631363SMatt Jacob 	req->ccb = NULL;
1186b0a2fdeeSScott Long 	if (LIST_EMPTY(&mpt->ack_frames)) {
1187c87e3f83SMatt Jacob 		/*
1188c87e3f83SMatt Jacob 		 * Insert free ones at the tail
1189c87e3f83SMatt Jacob 		 */
11905e073106SMatt Jacob 		req->serno = 0;
11915e073106SMatt Jacob 		req->state = REQ_STATE_FREE;
11925089bd63SMatt Jacob #ifdef	INVARIANTS
11935089bd63SMatt Jacob 		memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
11945089bd63SMatt Jacob #endif
1195c87e3f83SMatt Jacob 		TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1196b0a2fdeeSScott Long 		if (mpt->getreqwaiter != 0) {
1197b0a2fdeeSScott Long 			mpt->getreqwaiter = 0;
1198b0a2fdeeSScott Long 			wakeup(&mpt->request_free_list);
1199b0a2fdeeSScott Long 		}
1200b0a2fdeeSScott Long 		return;
1201b0a2fdeeSScott Long 	}
1202b0a2fdeeSScott Long 
1203b0a2fdeeSScott Long 	/*
1204b0a2fdeeSScott Long 	 * Process an ack frame deferred due to resource shortage.
1205b0a2fdeeSScott Long 	 */
1206b0a2fdeeSScott Long 	record = LIST_FIRST(&mpt->ack_frames);
1207b0a2fdeeSScott Long 	LIST_REMOVE(record, links);
12085e073106SMatt Jacob 	req->state = REQ_STATE_ALLOCATED;
12095089bd63SMatt Jacob 	mpt_assign_serno(mpt, req);
1210b0a2fdeeSScott Long 	mpt_send_event_ack(mpt, req, &record->reply, record->context);
1211b0a2fdeeSScott Long 	reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply)
1212b0a2fdeeSScott Long 		    + (mpt->reply_phys & 0xFFFFFFFF);
1213b0a2fdeeSScott Long 	mpt_free_reply(mpt, reply_baddr);
12149b631363SMatt Jacob }
12159b631363SMatt Jacob 
12169b631363SMatt Jacob /* Get a command buffer from the free queue */
12179b631363SMatt Jacob request_t *
1218b0a2fdeeSScott Long mpt_get_request(struct mpt_softc *mpt, int sleep_ok)
12199b631363SMatt Jacob {
12209b631363SMatt Jacob 	request_t *req;
1221b0a2fdeeSScott Long 
1222b0a2fdeeSScott Long retry:
12235089bd63SMatt Jacob 	KASSERT(MPT_OWNED(mpt), ("mpt_get_request: mpt not locked\n"));
1224b0a2fdeeSScott Long 	req = TAILQ_FIRST(&mpt->request_free_list);
12259b631363SMatt Jacob 	if (req != NULL) {
1226b0a2fdeeSScott Long 		KASSERT(req == &mpt->request_pool[req->index],
1227b0a2fdeeSScott Long 		    ("mpt_get_request: corrupted request free list\n"));
12285e073106SMatt Jacob 		KASSERT(req->state == REQ_STATE_FREE,
12295089bd63SMatt Jacob 		    ("req %p:%u not free on free list %x index %d function %x",
12305089bd63SMatt Jacob 		    req, req->serno, req->state, req->index,
12315089bd63SMatt Jacob 		    ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1232b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_free_list, req, links);
1233b0a2fdeeSScott Long 		req->state = REQ_STATE_ALLOCATED;
1234444dd2b6SMatt Jacob 		req->chain = NULL;
12355089bd63SMatt Jacob 		mpt_assign_serno(mpt, req);
1236b0a2fdeeSScott Long 	} else if (sleep_ok != 0) {
1237b0a2fdeeSScott Long 		mpt->getreqwaiter = 1;
1238b0a2fdeeSScott Long 		mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1239b0a2fdeeSScott Long 		goto retry;
12409b631363SMatt Jacob 	}
12415e073106SMatt Jacob 	return (req);
12429b631363SMatt Jacob }
12439b631363SMatt Jacob 
12449b631363SMatt Jacob /* Pass the command to the IOC */
12459b631363SMatt Jacob void
1246b0a2fdeeSScott Long mpt_send_cmd(struct mpt_softc *mpt, request_t *req)
12479b631363SMatt Jacob {
12481d79ca0eSMatt Jacob 	if (mpt->verbose > MPT_PRT_DEBUG2) {
12491043516dSMatt Jacob 		mpt_dump_request(mpt, req);
1250444dd2b6SMatt Jacob 	}
12519b631363SMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
12529b631363SMatt Jacob 	    BUS_DMASYNC_PREWRITE);
1253b0a2fdeeSScott Long 	req->state |= REQ_STATE_QUEUED;
12545089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
12555089bd63SMatt Jacob 	    ("req %p:%u func %x on freelist list in mpt_send_cmd",
12565089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
12575089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
12585089bd63SMatt Jacob 	    ("req %p:%u func %x already on pending list in mpt_send_cmd",
12595089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1260b0a2fdeeSScott Long 	TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1261b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
12629b631363SMatt Jacob }
12639b631363SMatt Jacob 
12649b631363SMatt Jacob /*
1265b0a2fdeeSScott Long  * Wait for a request to complete.
1266b0a2fdeeSScott Long  *
1267b0a2fdeeSScott Long  * Inputs:
1268b0a2fdeeSScott Long  *	mpt		softc of controller executing request
1269b0a2fdeeSScott Long  *	req		request to wait for
1270b0a2fdeeSScott Long  *	sleep_ok	nonzero implies may sleep in this context
1271b0a2fdeeSScott Long  *	time_ms		timeout in ms.  0 implies no timeout.
1272b0a2fdeeSScott Long  *
1273b0a2fdeeSScott Long  * Return Values:
1274b0a2fdeeSScott Long  *	0		Request completed
1275b0a2fdeeSScott Long  *	non-0		Timeout fired before request completion.
12769b631363SMatt Jacob  */
1277b0a2fdeeSScott Long int
1278b0a2fdeeSScott Long mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1279b0a2fdeeSScott Long 	     mpt_req_state_t state, mpt_req_state_t mask,
1280b0a2fdeeSScott Long 	     int sleep_ok, int time_ms)
12819b631363SMatt Jacob {
1282b0a2fdeeSScott Long 	int   error;
1283b0a2fdeeSScott Long 	int   timeout;
1284b0a2fdeeSScott Long 	u_int saved_cnt;
12859b631363SMatt Jacob 
1286b0a2fdeeSScott Long 	/*
1287b0a2fdeeSScott Long 	 * timeout is in ms.  0 indicates infinite wait.
1288b0a2fdeeSScott Long 	 * Convert to ticks or 500us units depending on
1289b0a2fdeeSScott Long 	 * our sleep mode.
1290b0a2fdeeSScott Long 	 */
1291c87e3f83SMatt Jacob 	if (sleep_ok != 0) {
1292b0a2fdeeSScott Long 		timeout = (time_ms * hz) / 1000;
1293c87e3f83SMatt Jacob 	} else {
1294b0a2fdeeSScott Long 		timeout = time_ms * 2;
1295c87e3f83SMatt Jacob 	}
1296b0a2fdeeSScott Long 	req->state |= REQ_STATE_NEED_WAKEUP;
1297b0a2fdeeSScott Long 	mask &= ~REQ_STATE_NEED_WAKEUP;
1298444dd2b6SMatt Jacob 	saved_cnt = mpt->reset_cnt;
1299c87e3f83SMatt Jacob 	while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1300b0a2fdeeSScott Long 		if (sleep_ok != 0) {
1301b0a2fdeeSScott Long 			error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout);
1302b0a2fdeeSScott Long 			if (error == EWOULDBLOCK) {
1303b0a2fdeeSScott Long 				timeout = 0;
1304b0a2fdeeSScott Long 				break;
1305b0a2fdeeSScott Long 			}
1306b0a2fdeeSScott Long 		} else {
1307b0a2fdeeSScott Long 			if (time_ms != 0 && --timeout == 0) {
1308b0a2fdeeSScott Long 				break;
1309b0a2fdeeSScott Long 			}
1310b0a2fdeeSScott Long 			DELAY(500);
1311b0a2fdeeSScott Long 			mpt_intr(mpt);
1312b0a2fdeeSScott Long 		}
1313b0a2fdeeSScott Long 	}
1314b0a2fdeeSScott Long 	req->state &= ~REQ_STATE_NEED_WAKEUP;
1315c87e3f83SMatt Jacob 	if (mpt->reset_cnt != saved_cnt) {
1316b0a2fdeeSScott Long 		return (EIO);
1317c87e3f83SMatt Jacob 	}
1318c87e3f83SMatt Jacob 	if (time_ms && timeout <= 0) {
1319c87e3f83SMatt Jacob 		MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
13206621d786SMatt Jacob 		req->state |= REQ_STATE_TIMEDOUT;
1321c87e3f83SMatt Jacob 		mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1322b0a2fdeeSScott Long 		return (ETIMEDOUT);
1323c87e3f83SMatt Jacob 	}
1324b0a2fdeeSScott Long 	return (0);
13259b631363SMatt Jacob }
13269b631363SMatt Jacob 
13279b631363SMatt Jacob /*
13289b631363SMatt Jacob  * Send a command to the IOC via the handshake register.
13299b631363SMatt Jacob  *
13309b631363SMatt Jacob  * Only done at initialization time and for certain unusual
13319b631363SMatt Jacob  * commands such as device/bus reset as specified by LSI.
13329b631363SMatt Jacob  */
13339b631363SMatt Jacob int
1334b0a2fdeeSScott Long mpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
13359b631363SMatt Jacob {
13369b631363SMatt Jacob 	int i;
1337b0a2fdeeSScott Long 	uint32_t data, *data32;
13389b631363SMatt Jacob 
13399b631363SMatt Jacob 	/* Check condition of the IOC */
13409b631363SMatt Jacob 	data = mpt_rd_db(mpt);
1341b0a2fdeeSScott Long 	if ((MPT_STATE(data) != MPT_DB_STATE_READY
1342b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1343b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1344b0a2fdeeSScott Long 	 || MPT_DB_IS_IN_USE(data)) {
1345b0a2fdeeSScott Long 		mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
13469b631363SMatt Jacob 		mpt_print_db(data);
13479b631363SMatt Jacob 		return (EBUSY);
13489b631363SMatt Jacob 	}
13499b631363SMatt Jacob 
13509b631363SMatt Jacob 	/* We move things in 32 bit chunks */
13519b631363SMatt Jacob 	len = (len + 3) >> 2;
13529b631363SMatt Jacob 	data32 = cmd;
13539b631363SMatt Jacob 
13549b631363SMatt Jacob 	/* Clear any left over pending doorbell interupts */
13559b631363SMatt Jacob 	if (MPT_DB_INTR(mpt_rd_intr(mpt)))
13569b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13579b631363SMatt Jacob 
13589b631363SMatt Jacob 	/*
13599b631363SMatt Jacob 	 * Tell the handshake reg. we are going to send a command
13609b631363SMatt Jacob          * and how long it is going to be.
13619b631363SMatt Jacob 	 */
13629b631363SMatt Jacob 	data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
13639b631363SMatt Jacob 	    (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
13649b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
13659b631363SMatt Jacob 
13669b631363SMatt Jacob 	/* Wait for the chip to notice */
13679b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1368a54067ccSMatt Jacob 		mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n");
1369b0a2fdeeSScott Long 		return (ETIMEDOUT);
13709b631363SMatt Jacob 	}
13719b631363SMatt Jacob 
13729b631363SMatt Jacob 	/* Clear the interrupt */
13739b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13749b631363SMatt Jacob 
13759b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
1376a54067ccSMatt Jacob 		mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n");
1377b0a2fdeeSScott Long 		return (ETIMEDOUT);
13789b631363SMatt Jacob 	}
13799b631363SMatt Jacob 
13809b631363SMatt Jacob 	/* Send the command */
13819b631363SMatt Jacob 	for (i = 0; i < len; i++) {
13823f970273SJohn Birrell 		mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++));
13839b631363SMatt Jacob 		if (mpt_wait_db_ack(mpt) != MPT_OK) {
1384301472c2SMatt Jacob 			mpt_prt(mpt,
1385a54067ccSMatt Jacob 			    "mpt_send_handshake_cmd: timeout @ index %d\n", i);
1386b0a2fdeeSScott Long 			return (ETIMEDOUT);
13879b631363SMatt Jacob 		}
13889b631363SMatt Jacob 	}
13899b631363SMatt Jacob 	return MPT_OK;
13909b631363SMatt Jacob }
13919b631363SMatt Jacob 
13929b631363SMatt Jacob /* Get the response from the handshake register */
13939b631363SMatt Jacob int
1394b0a2fdeeSScott Long mpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
13959b631363SMatt Jacob {
13969b631363SMatt Jacob 	int left, reply_left;
13979b631363SMatt Jacob 	u_int16_t *data16;
13983f970273SJohn Birrell 	uint32_t data;
13999b631363SMatt Jacob 	MSG_DEFAULT_REPLY *hdr;
14009b631363SMatt Jacob 
14019b631363SMatt Jacob 	/* We move things out in 16 bit chunks */
14029b631363SMatt Jacob 	reply_len >>= 1;
14039b631363SMatt Jacob 	data16 = (u_int16_t *)reply;
14049b631363SMatt Jacob 
14059b631363SMatt Jacob 	hdr = (MSG_DEFAULT_REPLY *)reply;
14069b631363SMatt Jacob 
14079b631363SMatt Jacob 	/* Get first word */
14089b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1409b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
14109b631363SMatt Jacob 		return ETIMEDOUT;
14119b631363SMatt Jacob 	}
14123f970273SJohn Birrell 	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14133f970273SJohn Birrell 	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14149b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14159b631363SMatt Jacob 
14169b631363SMatt Jacob 	/* Get Second Word */
14179b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1418b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
14199b631363SMatt Jacob 		return ETIMEDOUT;
14209b631363SMatt Jacob 	}
14213f970273SJohn Birrell 	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14223f970273SJohn Birrell 	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14239b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14249b631363SMatt Jacob 
14255e073106SMatt Jacob 	/*
14265e073106SMatt Jacob 	 * With the second word, we can now look at the length.
14275e073106SMatt Jacob 	 * Warn about a reply that's too short (except for IOC FACTS REPLY)
14285e073106SMatt Jacob 	 */
14295e073106SMatt Jacob 	if ((reply_len >> 1) != hdr->MsgLength &&
14305e073106SMatt Jacob 	    (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
14315e073106SMatt Jacob #if __FreeBSD_version >= 500000
1432301472c2SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
143333f31846SMatt Jacob 			"got %x; expected %zx for function %x\n",
14345e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14355e073106SMatt Jacob #else
14365e073106SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
143733f31846SMatt Jacob 			"got %x; expected %x for function %x\n",
14385e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14395e073106SMatt Jacob #endif
14409b631363SMatt Jacob 	}
14419b631363SMatt Jacob 
14429b631363SMatt Jacob 	/* Get rest of the reply; but don't overflow the provided buffer */
14439b631363SMatt Jacob 	left = (hdr->MsgLength << 1) - 2;
14449b631363SMatt Jacob 	reply_left =  reply_len - 2;
14459b631363SMatt Jacob 	while (left--) {
14469b631363SMatt Jacob 		u_int16_t datum;
14479b631363SMatt Jacob 
14489b631363SMatt Jacob 		if (mpt_wait_db_int(mpt) != MPT_OK) {
1449b0a2fdeeSScott Long 			mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
14509b631363SMatt Jacob 			return ETIMEDOUT;
14519b631363SMatt Jacob 		}
14523f970273SJohn Birrell 		data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14533f970273SJohn Birrell 		datum = le16toh(data & MPT_DB_DATA_MASK);
14549b631363SMatt Jacob 
14559b631363SMatt Jacob 		if (reply_left-- > 0)
14563f970273SJohn Birrell 			*data16++ = datum;
14579b631363SMatt Jacob 
14589b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14599b631363SMatt Jacob 	}
14609b631363SMatt Jacob 
14619b631363SMatt Jacob 	/* One more wait & clear at the end */
14629b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1463b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
14649b631363SMatt Jacob 		return ETIMEDOUT;
14659b631363SMatt Jacob 	}
14669b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14679b631363SMatt Jacob 
14689b631363SMatt Jacob 	if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1469b0a2fdeeSScott Long 		if (mpt->verbose >= MPT_PRT_TRACE)
14709b631363SMatt Jacob 			mpt_print_reply(hdr);
14719b631363SMatt Jacob 		return (MPT_FAIL | hdr->IOCStatus);
14729b631363SMatt Jacob 	}
14739b631363SMatt Jacob 
14749b631363SMatt Jacob 	return (0);
14759b631363SMatt Jacob }
14769b631363SMatt Jacob 
14779b631363SMatt Jacob static int
1478b0a2fdeeSScott Long mpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
14799b631363SMatt Jacob {
14809b631363SMatt Jacob 	MSG_IOC_FACTS f_req;
14819b631363SMatt Jacob 	int error;
14829b631363SMatt Jacob 
14835e073106SMatt Jacob 	memset(&f_req, 0, sizeof f_req);
14849b631363SMatt Jacob 	f_req.Function = MPI_FUNCTION_IOC_FACTS;
1485b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
14869b631363SMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
14870e0521a1SMatt Jacob 	if (error) {
14889b631363SMatt Jacob 		return(error);
14890e0521a1SMatt Jacob 	}
14909b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
14919b631363SMatt Jacob 	return (error);
14929b631363SMatt Jacob }
14939b631363SMatt Jacob 
14947104aeefSMatt Jacob static int
14950e0521a1SMatt Jacob mpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp)
14967104aeefSMatt Jacob {
14977104aeefSMatt Jacob 	MSG_PORT_FACTS f_req;
14987104aeefSMatt Jacob 	int error;
14997104aeefSMatt Jacob 
1500b0a2fdeeSScott Long 	memset(&f_req, 0, sizeof f_req);
15017104aeefSMatt Jacob 	f_req.Function = MPI_FUNCTION_PORT_FACTS;
15020e0521a1SMatt Jacob 	f_req.PortNumber = port;
1503b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15047104aeefSMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
15050e0521a1SMatt Jacob 	if (error) {
15067104aeefSMatt Jacob 		return(error);
15070e0521a1SMatt Jacob 	}
15087104aeefSMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
15097104aeefSMatt Jacob 	return (error);
15107104aeefSMatt Jacob }
15117104aeefSMatt Jacob 
15129b631363SMatt Jacob /*
15139b631363SMatt Jacob  * Send the initialization request. This is where we specify how many
15149b631363SMatt Jacob  * SCSI busses and how many devices per bus we wish to emulate.
15159b631363SMatt Jacob  * This is also the command that specifies the max size of the reply
15169b631363SMatt Jacob  * frames from the IOC that we will be allocating.
15179b631363SMatt Jacob  */
15189b631363SMatt Jacob static int
1519b0a2fdeeSScott Long mpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
15209b631363SMatt Jacob {
15219b631363SMatt Jacob 	int error = 0;
15229b631363SMatt Jacob 	MSG_IOC_INIT init;
15239b631363SMatt Jacob 	MSG_IOC_INIT_REPLY reply;
15249b631363SMatt Jacob 
15255e073106SMatt Jacob 	memset(&init, 0, sizeof init);
15269b631363SMatt Jacob 	init.WhoInit = who;
15279b631363SMatt Jacob 	init.Function = MPI_FUNCTION_IOC_INIT;
15280e0521a1SMatt Jacob 	init.MaxDevices = 0;	/* at least 256 devices per bus */
15290e0521a1SMatt Jacob 	init.MaxBuses = 16;	/* at least 16 busses */
1530444dd2b6SMatt Jacob 
1531444dd2b6SMatt Jacob 	init.MsgVersion = htole16(MPI_VERSION);
1532444dd2b6SMatt Jacob 	init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1533444dd2b6SMatt Jacob 	init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1534b0a2fdeeSScott Long 	init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15359b631363SMatt Jacob 
15369b631363SMatt Jacob 	if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
15379b631363SMatt Jacob 		return(error);
15389b631363SMatt Jacob 	}
15399b631363SMatt Jacob 
15409b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
15419b631363SMatt Jacob 	return (error);
15429b631363SMatt Jacob }
15439b631363SMatt Jacob 
15447104aeefSMatt Jacob 
15457104aeefSMatt Jacob /*
15467104aeefSMatt Jacob  * Utiltity routine to read configuration headers and pages
15477104aeefSMatt Jacob  */
1548b0a2fdeeSScott Long int
1549b0a2fdeeSScott Long mpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, u_int Action,
1550b0a2fdeeSScott Long 		  u_int PageVersion, u_int PageLength, u_int PageNumber,
1551b0a2fdeeSScott Long 		  u_int PageType, uint32_t PageAddress, bus_addr_t addr,
1552b0a2fdeeSScott Long 		  bus_size_t len, int sleep_ok, int timeout_ms)
15537104aeefSMatt Jacob {
15547104aeefSMatt Jacob 	MSG_CONFIG *cfgp;
1555b0a2fdeeSScott Long 	SGE_SIMPLE32 *se;
15567104aeefSMatt Jacob 
15577104aeefSMatt Jacob 	cfgp = req->req_vbuf;
1558b0a2fdeeSScott Long 	memset(cfgp, 0, sizeof *cfgp);
1559b0a2fdeeSScott Long 	cfgp->Action = Action;
15607104aeefSMatt Jacob 	cfgp->Function = MPI_FUNCTION_CONFIG;
1561b0a2fdeeSScott Long 	cfgp->Header.PageVersion = PageVersion;
1562b0a2fdeeSScott Long 	cfgp->Header.PageLength = PageLength;
1563b0a2fdeeSScott Long 	cfgp->Header.PageNumber = PageNumber;
1564b0a2fdeeSScott Long 	cfgp->Header.PageType = PageType;
15653f970273SJohn Birrell 	cfgp->PageAddress = htole32(PageAddress);
1566b0a2fdeeSScott Long 	se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
15673f970273SJohn Birrell 	se->Address = htole32(addr);
1568b0a2fdeeSScott Long 	MPI_pSGE_SET_LENGTH(se, len);
1569b0a2fdeeSScott Long 	MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1570b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1571b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_END_OF_LIST |
1572b0a2fdeeSScott Long 	    ((Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
1573b0a2fdeeSScott Long 	  || Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1574b0a2fdeeSScott Long 	   ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
15753f970273SJohn Birrell 	se->FlagsLength = htole32(se->FlagsLength);
1576b0a2fdeeSScott Long 	cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
15777104aeefSMatt Jacob 
15787104aeefSMatt Jacob 	mpt_check_doorbell(mpt);
15797104aeefSMatt Jacob 	mpt_send_cmd(mpt, req);
1580b0a2fdeeSScott Long 	return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1581b0a2fdeeSScott Long 			     sleep_ok, timeout_ms));
15827104aeefSMatt Jacob }
15837104aeefSMatt Jacob 
1584b0a2fdeeSScott Long 
1585b0a2fdeeSScott Long int
1586b0a2fdeeSScott Long mpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1587b0a2fdeeSScott Long 		    uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1588b0a2fdeeSScott Long 		    int sleep_ok, int timeout_ms)
1589b0a2fdeeSScott Long {
1590b0a2fdeeSScott Long 	request_t  *req;
159129ae59edSMatt Jacob 	MSG_CONFIG *cfgp;
1592b0a2fdeeSScott Long 	int	    error;
1593b0a2fdeeSScott Long 
1594b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1595b0a2fdeeSScott Long 	if (req == NULL) {
1596b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
159729ae59edSMatt Jacob 		return (ENOMEM);
15987104aeefSMatt Jacob 	}
1599b0a2fdeeSScott Long 
1600b0a2fdeeSScott Long 	error = mpt_issue_cfg_req(mpt, req, MPI_CONFIG_ACTION_PAGE_HEADER,
1601b0a2fdeeSScott Long 				  /*PageVersion*/0, /*PageLength*/0, PageNumber,
1602b0a2fdeeSScott Long 				  PageType, PageAddress, /*addr*/0, /*len*/0,
1603b0a2fdeeSScott Long 				  sleep_ok, timeout_ms);
1604b0a2fdeeSScott Long 	if (error != 0) {
16056621d786SMatt Jacob 		/*
16066621d786SMatt Jacob 		 * Leave the request. Without resetting the chip, it's
16076621d786SMatt Jacob 		 * still owned by it and we'll just get into trouble
16086621d786SMatt Jacob 		 * freeing it now. Mark it as abandoned so that if it
16096621d786SMatt Jacob 		 * shows up later it can be freed.
16106621d786SMatt Jacob 		 */
1611b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_header timed out\n");
161229ae59edSMatt Jacob 		return (ETIMEDOUT);
1613b0a2fdeeSScott Long 	}
1614b0a2fdeeSScott Long 
161529ae59edSMatt Jacob         switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
161629ae59edSMatt Jacob 	case MPI_IOCSTATUS_SUCCESS:
1617b0a2fdeeSScott Long 		cfgp = req->req_vbuf;
1618b0a2fdeeSScott Long 		bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1619b0a2fdeeSScott Long 		error = 0;
162029ae59edSMatt Jacob 		break;
162129ae59edSMatt Jacob 	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
162229ae59edSMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG,
162329ae59edSMatt Jacob 		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
162429ae59edSMatt Jacob 		    PageType, PageNumber, PageAddress);
162529ae59edSMatt Jacob 		error = EINVAL;
162629ae59edSMatt Jacob 		break;
162729ae59edSMatt Jacob 	default:
162829ae59edSMatt Jacob 		mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
162929ae59edSMatt Jacob 			req->IOCStatus);
163029ae59edSMatt Jacob 		error = EIO;
163129ae59edSMatt Jacob 		break;
1632b0a2fdeeSScott Long 	}
16337104aeefSMatt Jacob 	mpt_free_request(mpt, req);
1634b0a2fdeeSScott Long 	return (error);
16357104aeefSMatt Jacob }
16367104aeefSMatt Jacob 
1637ce68dae5SMatt Jacob int
1638b0a2fdeeSScott Long mpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1639b0a2fdeeSScott Long 		  CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1640b0a2fdeeSScott Long 		  int timeout_ms)
16417104aeefSMatt Jacob {
16427104aeefSMatt Jacob 	request_t    *req;
1643b0a2fdeeSScott Long 	int	      error;
16447104aeefSMatt Jacob 
1645b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1646b0a2fdeeSScott Long 	if (req == NULL) {
1647b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
16487104aeefSMatt Jacob 		return (-1);
16497104aeefSMatt Jacob 	}
16507104aeefSMatt Jacob 
1651b0a2fdeeSScott Long 	error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion,
1652b0a2fdeeSScott Long 				  hdr->PageLength, hdr->PageNumber,
1653b0a2fdeeSScott Long 				  hdr->PageType & MPI_CONFIG_PAGETYPE_MASK,
1654c87e3f83SMatt Jacob 				  PageAddress, req->req_pbuf + MPT_RQSL(mpt),
1655b0a2fdeeSScott Long 				  len, sleep_ok, timeout_ms);
1656b0a2fdeeSScott Long 	if (error != 0) {
1657b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
16587104aeefSMatt Jacob 		return (-1);
16597104aeefSMatt Jacob 	}
1660b0a2fdeeSScott Long 
1661b0a2fdeeSScott Long 	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1662b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1663b0a2fdeeSScott Long 			req->IOCStatus);
1664b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
1665b0a2fdeeSScott Long 		return (-1);
1666b0a2fdeeSScott Long 	}
16677104aeefSMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
16687104aeefSMatt Jacob 	    BUS_DMASYNC_POSTREAD);
1669c87e3f83SMatt Jacob 	memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
16707104aeefSMatt Jacob 	mpt_free_request(mpt, req);
16717104aeefSMatt Jacob 	return (0);
16727104aeefSMatt Jacob }
16737104aeefSMatt Jacob 
1674ce68dae5SMatt Jacob int
1675b0a2fdeeSScott Long mpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1676b0a2fdeeSScott Long 		   CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1677b0a2fdeeSScott Long 		   int timeout_ms)
16787104aeefSMatt Jacob {
16797104aeefSMatt Jacob 	request_t    *req;
1680b0a2fdeeSScott Long 	u_int	      hdr_attr;
1681b0a2fdeeSScott Long 	int	      error;
16827104aeefSMatt Jacob 
16837104aeefSMatt Jacob 	hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
16847104aeefSMatt Jacob 	if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
16857104aeefSMatt Jacob 	    hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1686b0a2fdeeSScott Long 		mpt_prt(mpt, "page type 0x%x not changeable\n",
16877104aeefSMatt Jacob 			hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
16887104aeefSMatt Jacob 		return (-1);
16897104aeefSMatt Jacob 	}
1690b4c618c0SMatt Jacob 
1691b4c618c0SMatt Jacob #if	0
1692b4c618c0SMatt Jacob 	/*
1693b4c618c0SMatt Jacob 	 * We shouldn't mask off other bits here.
1694b4c618c0SMatt Jacob 	 */
1695b4c618c0SMatt Jacob 	hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1696b4c618c0SMatt Jacob #endif
16977104aeefSMatt Jacob 
1698b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1699b0a2fdeeSScott Long 	if (req == NULL)
1700b0a2fdeeSScott Long 		return (-1);
17017104aeefSMatt Jacob 
1702c87e3f83SMatt Jacob 	memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1703b4c618c0SMatt Jacob 
1704b4c618c0SMatt Jacob 	/*
1705b4c618c0SMatt Jacob 	 * There isn't any point in restoring stripped out attributes
1706b4c618c0SMatt Jacob 	 * if you then mask them going down to issue the request.
1707b4c618c0SMatt Jacob 	 */
1708b4c618c0SMatt Jacob 
1709b4c618c0SMatt Jacob #if	0
1710301472c2SMatt Jacob 	/* Restore stripped out attributes */
1711301472c2SMatt Jacob 	hdr->PageType |= hdr_attr;
17127104aeefSMatt Jacob 
1713b0a2fdeeSScott Long 	error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion,
1714b0a2fdeeSScott Long 				  hdr->PageLength, hdr->PageNumber,
1715b0a2fdeeSScott Long 				  hdr->PageType & MPI_CONFIG_PAGETYPE_MASK,
1716c87e3f83SMatt Jacob 				  PageAddress, req->req_pbuf + MPT_RQSL(mpt),
1717b0a2fdeeSScott Long 				  len, sleep_ok, timeout_ms);
1718b4c618c0SMatt Jacob #else
1719b4c618c0SMatt Jacob 	error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion,
1720b4c618c0SMatt Jacob 				  hdr->PageLength, hdr->PageNumber,
1721b4c618c0SMatt Jacob 				  hdr->PageType, PageAddress,
1722b4c618c0SMatt Jacob 				  req->req_pbuf + MPT_RQSL(mpt),
1723b4c618c0SMatt Jacob 				  len, sleep_ok, timeout_ms);
1724b4c618c0SMatt Jacob #endif
1725b0a2fdeeSScott Long 	if (error != 0) {
1726b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
17277104aeefSMatt Jacob 		return (-1);
17287104aeefSMatt Jacob 	}
17297104aeefSMatt Jacob 
1730b0a2fdeeSScott Long         if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1731b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1732b0a2fdeeSScott Long 			req->IOCStatus);
17337104aeefSMatt Jacob 		mpt_free_request(mpt, req);
1734b0a2fdeeSScott Long 		return (-1);
1735b0a2fdeeSScott Long 	}
1736b0a2fdeeSScott Long 	mpt_free_request(mpt, req);
1737b0a2fdeeSScott Long 	return (0);
1738b0a2fdeeSScott Long }
1739b0a2fdeeSScott Long 
1740b0a2fdeeSScott Long /*
1741b0a2fdeeSScott Long  * Read IOC configuration information
1742b0a2fdeeSScott Long  */
1743b0a2fdeeSScott Long static int
1744b0a2fdeeSScott Long mpt_read_config_info_ioc(struct mpt_softc *mpt)
1745b0a2fdeeSScott Long {
1746b0a2fdeeSScott Long 	CONFIG_PAGE_HEADER hdr;
1747b0a2fdeeSScott Long 	struct mpt_raid_volume *mpt_raid;
1748b0a2fdeeSScott Long 	int rv;
1749b0a2fdeeSScott Long 	int i;
1750b0a2fdeeSScott Long 	size_t len;
1751b0a2fdeeSScott Long 
1752b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
17531d79ca0eSMatt Jacob 		2, 0, &hdr, FALSE, 5000);
175429ae59edSMatt Jacob 	/*
175529ae59edSMatt Jacob 	 * If it's an invalid page, so what? Not a supported function....
175629ae59edSMatt Jacob 	 */
17571d79ca0eSMatt Jacob 	if (rv == EINVAL) {
175829ae59edSMatt Jacob 		return (0);
17591d79ca0eSMatt Jacob 	}
17601d79ca0eSMatt Jacob 	if (rv) {
176129ae59edSMatt Jacob 		return (rv);
17621d79ca0eSMatt Jacob 	}
1763b0a2fdeeSScott Long 
17640e0521a1SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG,
17650e0521a1SMatt Jacob 	    "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n",
17660e0521a1SMatt Jacob 	    hdr.PageVersion, hdr.PageLength << 2,
1767b0a2fdeeSScott Long 	    hdr.PageNumber, hdr.PageType);
1768b0a2fdeeSScott Long 
1769b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
1770a3699bcaSScott Long 	mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
17711d79ca0eSMatt Jacob 	if (mpt->ioc_page2 == NULL) {
17721d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
17731d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
1774b0a2fdeeSScott Long 		return (ENOMEM);
17751d79ca0eSMatt Jacob 	}
1776b0a2fdeeSScott Long 	memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
17771d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
17781d79ca0eSMatt Jacob 	    &mpt->ioc_page2->Header, len, FALSE, 5000);
1779b0a2fdeeSScott Long 	if (rv) {
1780b0a2fdeeSScott Long 		mpt_prt(mpt, "failed to read IOC Page 2\n");
17811d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
17821d79ca0eSMatt Jacob 		return (EIO);
17831d79ca0eSMatt Jacob 	}
17840e0521a1SMatt Jacob 	mpt2host_config_page_ioc2(mpt->ioc_page2);
17851d79ca0eSMatt Jacob 
17861d79ca0eSMatt Jacob 	if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1787b0a2fdeeSScott Long 		uint32_t mask;
1788b0a2fdeeSScott Long 
1789b0a2fdeeSScott Long 		mpt_prt(mpt, "Capabilities: (");
1790b0a2fdeeSScott Long 		for (mask = 1; mask != 0; mask <<= 1) {
17911d79ca0eSMatt Jacob 			if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1792b0a2fdeeSScott Long 				continue;
17931d79ca0eSMatt Jacob 			}
1794b0a2fdeeSScott Long 			switch (mask) {
1795b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1796b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-0");
1797b0a2fdeeSScott Long 				break;
1798b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1799b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1E");
1800b0a2fdeeSScott Long 				break;
1801b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1802b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1");
1803b0a2fdeeSScott Long 				break;
1804b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1805b0a2fdeeSScott Long 				mpt_prtc(mpt, " SES");
1806b0a2fdeeSScott Long 				break;
1807b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1808b0a2fdeeSScott Long 				mpt_prtc(mpt, " SAFTE");
1809b0a2fdeeSScott Long 				break;
1810b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1811b0a2fdeeSScott Long 				mpt_prtc(mpt, " Multi-Channel-Arrays");
1812b0a2fdeeSScott Long 			default:
1813b0a2fdeeSScott Long 				break;
1814b0a2fdeeSScott Long 			}
1815b0a2fdeeSScott Long 		}
1816b0a2fdeeSScott Long 		mpt_prtc(mpt, " )\n");
1817b0a2fdeeSScott Long 		if ((mpt->ioc_page2->CapabilitiesFlags
1818b0a2fdeeSScott Long 		   & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1819b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1820b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1821b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1822b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes,
1823b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes != 1
1824b0a2fdeeSScott Long 			      ? "s " : " ",
1825b0a2fdeeSScott Long 				mpt->ioc_page2->MaxVolumes);
1826b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1827b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks,
1828b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks != 1
1829b0a2fdeeSScott Long 			      ? "s " : " ",
1830b0a2fdeeSScott Long 				mpt->ioc_page2->MaxPhysDisks);
1831b0a2fdeeSScott Long 		}
1832b0a2fdeeSScott Long 	}
1833b0a2fdeeSScott Long 
1834b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
18351d79ca0eSMatt Jacob 	mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1836b0a2fdeeSScott Long 	if (mpt->raid_volumes == NULL) {
1837b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID volume data\n");
18381d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18391d79ca0eSMatt Jacob 		return (ENOMEM);
1840b0a2fdeeSScott Long 	}
1841b0a2fdeeSScott Long 
1842b0a2fdeeSScott Long 	/*
1843b0a2fdeeSScott Long 	 * Copy critical data out of ioc_page2 so that we can
1844b0a2fdeeSScott Long 	 * safely refresh the page without windows of unreliable
1845b0a2fdeeSScott Long 	 * data.
1846b0a2fdeeSScott Long 	 */
1847b0a2fdeeSScott Long 	mpt->raid_max_volumes =  mpt->ioc_page2->MaxVolumes;
1848b0a2fdeeSScott Long 
18491d79ca0eSMatt Jacob 	len = sizeof(*mpt->raid_volumes->config_page) +
18501d79ca0eSMatt Jacob 	    (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
1851b0a2fdeeSScott Long 	for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
1852b0a2fdeeSScott Long 		mpt_raid = &mpt->raid_volumes[i];
18531d79ca0eSMatt Jacob 		mpt_raid->config_page =
18541d79ca0eSMatt Jacob 		    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1855b0a2fdeeSScott Long 		if (mpt_raid->config_page == NULL) {
1856b0a2fdeeSScott Long 			mpt_prt(mpt, "Could not allocate RAID page data\n");
18571d79ca0eSMatt Jacob 			mpt_raid_free_mem(mpt);
18581d79ca0eSMatt Jacob 			return (ENOMEM);
1859b0a2fdeeSScott Long 		}
1860b0a2fdeeSScott Long 	}
1861b0a2fdeeSScott Long 	mpt->raid_page0_len = len;
1862b0a2fdeeSScott Long 
1863b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
18641d79ca0eSMatt Jacob 	mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1865b0a2fdeeSScott Long 	if (mpt->raid_disks == NULL) {
1866b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID disk data\n");
18671d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18681d79ca0eSMatt Jacob 		return (ENOMEM);
1869b0a2fdeeSScott Long 	}
1870b0a2fdeeSScott Long 	mpt->raid_max_disks =  mpt->ioc_page2->MaxPhysDisks;
1871b0a2fdeeSScott Long 
18721d79ca0eSMatt Jacob 	/*
18731d79ca0eSMatt Jacob 	 * Load page 3.
18741d79ca0eSMatt Jacob 	 */
1875b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
18761d79ca0eSMatt Jacob 	    3, 0, &hdr, FALSE, 5000);
18771d79ca0eSMatt Jacob 	if (rv) {
18781d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
1879b0a2fdeeSScott Long 		return (EIO);
18801d79ca0eSMatt Jacob 	}
1881b0a2fdeeSScott Long 
1882b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
1883b0a2fdeeSScott Long 	    hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
1884b0a2fdeeSScott Long 
1885b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
1886a3699bcaSScott Long 	mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
18871d79ca0eSMatt Jacob 	if (mpt->ioc_page3 == NULL) {
18881d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
18891d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18901d79ca0eSMatt Jacob 		return (ENOMEM);
1891b0a2fdeeSScott Long 	}
18921d79ca0eSMatt Jacob 	memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
18931d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
18941d79ca0eSMatt Jacob 	    &mpt->ioc_page3->Header, len, FALSE, 5000);
18951d79ca0eSMatt Jacob 	if (rv) {
18961d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18971d79ca0eSMatt Jacob 		return (EIO);
18981d79ca0eSMatt Jacob 	}
1899b0a2fdeeSScott Long 	mpt_raid_wakeup(mpt);
19007104aeefSMatt Jacob 	return (0);
19017104aeefSMatt Jacob }
19027104aeefSMatt Jacob 
19037104aeefSMatt Jacob /*
19047104aeefSMatt Jacob  * Enable IOC port
19057104aeefSMatt Jacob  */
19067104aeefSMatt Jacob static int
1907b0a2fdeeSScott Long mpt_send_port_enable(struct mpt_softc *mpt, int port)
19089b631363SMatt Jacob {
19099b631363SMatt Jacob 	request_t	*req;
19109b631363SMatt Jacob 	MSG_PORT_ENABLE *enable_req;
1911b0a2fdeeSScott Long 	int		 error;
19129b631363SMatt Jacob 
1913b0a2fdeeSScott Long 	req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
1914b0a2fdeeSScott Long 	if (req == NULL)
1915b0a2fdeeSScott Long 		return (-1);
19169b631363SMatt Jacob 
19179b631363SMatt Jacob 	enable_req = req->req_vbuf;
19185e073106SMatt Jacob 	memset(enable_req, 0,  MPT_RQSL(mpt));
19199b631363SMatt Jacob 
19209b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_PORT_ENABLE;
1921b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
19229b631363SMatt Jacob 	enable_req->PortNumber = port;
19239b631363SMatt Jacob 
19249b631363SMatt Jacob 	mpt_check_doorbell(mpt);
1925b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
19269b631363SMatt Jacob 
1927b0a2fdeeSScott Long 	mpt_send_cmd(mpt, req);
1928b0a2fdeeSScott Long 	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
19295089bd63SMatt Jacob 	    FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000);
1930b0a2fdeeSScott Long 	if (error != 0) {
1931c87e3f83SMatt Jacob 		mpt_prt(mpt, "port %d enable timed out\n", port);
19329b631363SMatt Jacob 		return (-1);
19339b631363SMatt Jacob 	}
19349b631363SMatt Jacob 	mpt_free_request(mpt, req);
1935c87e3f83SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
19369b631363SMatt Jacob 	return (0);
19379b631363SMatt Jacob }
19389b631363SMatt Jacob 
19399b631363SMatt Jacob /*
19409b631363SMatt Jacob  * Enable/Disable asynchronous event reporting.
19419b631363SMatt Jacob  */
19429b631363SMatt Jacob static int
1943b0a2fdeeSScott Long mpt_send_event_request(struct mpt_softc *mpt, int onoff)
19449b631363SMatt Jacob {
19459b631363SMatt Jacob 	request_t *req;
19469b631363SMatt Jacob 	MSG_EVENT_NOTIFY *enable_req;
19479b631363SMatt Jacob 
19485e073106SMatt Jacob 	req = mpt_get_request(mpt, FALSE);
19495e073106SMatt Jacob 	if (req == NULL) {
19505e073106SMatt Jacob 		return (ENOMEM);
19515e073106SMatt Jacob 	}
19529b631363SMatt Jacob 	enable_req = req->req_vbuf;
19535e073106SMatt Jacob 	memset(enable_req, 0, sizeof *enable_req);
19549b631363SMatt Jacob 
19559b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_EVENT_NOTIFICATION;
1956b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
19579b631363SMatt Jacob 	enable_req->Switch     = onoff;
19589b631363SMatt Jacob 
19599b631363SMatt Jacob 	mpt_check_doorbell(mpt);
19605e073106SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
19615e073106SMatt Jacob 	    onoff ? "en" : "dis");
19625e073106SMatt Jacob 	/*
19635e073106SMatt Jacob 	 * Send the command off, but don't wait for it.
19645e073106SMatt Jacob 	 */
19659b631363SMatt Jacob 	mpt_send_cmd(mpt, req);
19669b631363SMatt Jacob 	return (0);
19679b631363SMatt Jacob }
19689b631363SMatt Jacob 
19699b631363SMatt Jacob /*
19709b631363SMatt Jacob  * Un-mask the interupts on the chip.
19719b631363SMatt Jacob  */
19729b631363SMatt Jacob void
1973b0a2fdeeSScott Long mpt_enable_ints(struct mpt_softc *mpt)
19749b631363SMatt Jacob {
19759b631363SMatt Jacob 	/* Unmask every thing except door bell int */
19769b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
19779b631363SMatt Jacob }
19789b631363SMatt Jacob 
19799b631363SMatt Jacob /*
19809b631363SMatt Jacob  * Mask the interupts on the chip.
19819b631363SMatt Jacob  */
19829b631363SMatt Jacob void
1983b0a2fdeeSScott Long mpt_disable_ints(struct mpt_softc *mpt)
19849b631363SMatt Jacob {
19859b631363SMatt Jacob 	/* Mask all interrupts */
19869b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK,
19879b631363SMatt Jacob 	    MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
19889b631363SMatt Jacob }
19899b631363SMatt Jacob 
1990b0a2fdeeSScott Long static void
1991b0a2fdeeSScott Long mpt_sysctl_attach(struct mpt_softc *mpt)
19929b631363SMatt Jacob {
1993c87e3f83SMatt Jacob #if __FreeBSD_version >= 500000
1994b0a2fdeeSScott Long 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
1995b0a2fdeeSScott Long 	struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
19969b631363SMatt Jacob 
1997b0a2fdeeSScott Long 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1998b0a2fdeeSScott Long 		       "debug", CTLFLAG_RW, &mpt->verbose, 0,
1999b0a2fdeeSScott Long 		       "Debugging/Verbose level");
2000b4c618c0SMatt Jacob 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2001b4c618c0SMatt Jacob 		       "role", CTLFLAG_RD, &mpt->role, 0,
2002b4c618c0SMatt Jacob 		       "HBA role");
2003c87e3f83SMatt Jacob #endif
20049b631363SMatt Jacob }
20059b631363SMatt Jacob 
2006b0a2fdeeSScott Long int
2007b0a2fdeeSScott Long mpt_attach(struct mpt_softc *mpt)
2008b0a2fdeeSScott Long {
2009b0a2fdeeSScott Long 	struct mpt_personality *pers;
2010c87e3f83SMatt Jacob 	int i;
2011b0a2fdeeSScott Long 	int error;
2012b0a2fdeeSScott Long 
2013a7303be1SMatt Jacob 	TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links);
2014c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2015b0a2fdeeSScott Long 		pers = mpt_personalities[i];
2016c87e3f83SMatt Jacob 		if (pers == NULL) {
2017b0a2fdeeSScott Long 			continue;
2018c87e3f83SMatt Jacob 		}
2019b0a2fdeeSScott Long 		if (pers->probe(mpt) == 0) {
2020b0a2fdeeSScott Long 			error = pers->attach(mpt);
2021b0a2fdeeSScott Long 			if (error != 0) {
2022b0a2fdeeSScott Long 				mpt_detach(mpt);
2023b0a2fdeeSScott Long 				return (error);
2024b0a2fdeeSScott Long 			}
2025b0a2fdeeSScott Long 			mpt->mpt_pers_mask |= (0x1 << pers->id);
2026b0a2fdeeSScott Long 			pers->use_count++;
2027b0a2fdeeSScott Long 		}
2028b0a2fdeeSScott Long 	}
2029444dd2b6SMatt Jacob 
2030c87e3f83SMatt Jacob 	/*
2031c87e3f83SMatt Jacob 	 * Now that we've attached everything, do the enable function
2032c87e3f83SMatt Jacob 	 * for all of the personalities. This allows the personalities
2033c87e3f83SMatt Jacob 	 * to do setups that are appropriate for them prior to enabling
2034c87e3f83SMatt Jacob 	 * any ports.
2035c87e3f83SMatt Jacob 	 */
2036c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2037c87e3f83SMatt Jacob 		pers = mpt_personalities[i];
2038c87e3f83SMatt Jacob 		if (pers != NULL  && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2039c87e3f83SMatt Jacob 			error = pers->enable(mpt);
2040c87e3f83SMatt Jacob 			if (error != 0) {
2041c87e3f83SMatt Jacob 				mpt_prt(mpt, "personality %s attached but would"
2042c87e3f83SMatt Jacob 				    " not enable (%d)\n", pers->name, error);
2043c87e3f83SMatt Jacob 				mpt_detach(mpt);
2044c87e3f83SMatt Jacob 				return (error);
2045c87e3f83SMatt Jacob 			}
2046c87e3f83SMatt Jacob 		}
2047c87e3f83SMatt Jacob 	}
2048b0a2fdeeSScott Long 	return (0);
2049b0a2fdeeSScott Long }
2050b0a2fdeeSScott Long 
2051b0a2fdeeSScott Long int
2052b0a2fdeeSScott Long mpt_shutdown(struct mpt_softc *mpt)
2053b0a2fdeeSScott Long {
2054b0a2fdeeSScott Long 	struct mpt_personality *pers;
2055b0a2fdeeSScott Long 
2056c87e3f83SMatt Jacob 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2057b0a2fdeeSScott Long 		pers->shutdown(mpt);
2058c87e3f83SMatt Jacob 	}
2059b0a2fdeeSScott Long 	return (0);
2060b0a2fdeeSScott Long }
2061b0a2fdeeSScott Long 
2062b0a2fdeeSScott Long int
2063b0a2fdeeSScott Long mpt_detach(struct mpt_softc *mpt)
2064b0a2fdeeSScott Long {
2065b0a2fdeeSScott Long 	struct mpt_personality *pers;
2066b0a2fdeeSScott Long 
2067b0a2fdeeSScott Long 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2068b0a2fdeeSScott Long 		pers->detach(mpt);
2069b0a2fdeeSScott Long 		mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2070b0a2fdeeSScott Long 		pers->use_count--;
2071b0a2fdeeSScott Long 	}
2072a7303be1SMatt Jacob 	TAILQ_REMOVE(&mpt_tailq, mpt, links);
2073b0a2fdeeSScott Long 	return (0);
2074b0a2fdeeSScott Long }
2075b0a2fdeeSScott Long 
2076b0a2fdeeSScott Long int
2077b0a2fdeeSScott Long mpt_core_load(struct mpt_personality *pers)
2078b0a2fdeeSScott Long {
2079b0a2fdeeSScott Long 	int i;
2080b0a2fdeeSScott Long 
2081b0a2fdeeSScott Long 	/*
2082b0a2fdeeSScott Long 	 * Setup core handlers and insert the default handler
2083b0a2fdeeSScott Long 	 * into all "empty slots".
2084b0a2fdeeSScott Long 	 */
2085c87e3f83SMatt Jacob 	for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2086b0a2fdeeSScott Long 		mpt_reply_handlers[i] = mpt_default_reply_handler;
2087c87e3f83SMatt Jacob 	}
2088b0a2fdeeSScott Long 
2089b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2090b0a2fdeeSScott Long 	    mpt_event_reply_handler;
2091b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2092b0a2fdeeSScott Long 	    mpt_config_reply_handler;
2093b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2094b0a2fdeeSScott Long 	    mpt_handshake_reply_handler;
2095b0a2fdeeSScott Long 	return (0);
20969b631363SMatt Jacob }
20979b631363SMatt Jacob 
20989b631363SMatt Jacob /*
2099b0a2fdeeSScott Long  * Initialize per-instance driver data and perform
2100b0a2fdeeSScott Long  * initial controller configuration.
21019b631363SMatt Jacob  */
2102b0a2fdeeSScott Long int
2103b0a2fdeeSScott Long mpt_core_attach(struct mpt_softc *mpt)
2104b0a2fdeeSScott Long {
2105b0a2fdeeSScott Long         int val;
2106c87e3f83SMatt Jacob 
2107b0a2fdeeSScott Long 	LIST_INIT(&mpt->ack_frames);
2108b0a2fdeeSScott Long 	/* Put all request buffers on the free list */
2109b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_pending_list);
2110b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_free_list);
21115e073106SMatt Jacob 	TAILQ_INIT(&mpt->request_timeout_list);
2112c87e3f83SMatt Jacob 	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
21135e073106SMatt Jacob 		request_t *req = &mpt->request_pool[val];
21145e073106SMatt Jacob 		req->state = REQ_STATE_ALLOCATED;
21155e073106SMatt Jacob 		mpt_free_request(mpt, req);
2116c87e3f83SMatt Jacob 	}
2117c87e3f83SMatt Jacob 	for (val = 0; val < MPT_MAX_LUNS; val++) {
2118c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].atios);
2119c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].inots);
2120c87e3f83SMatt Jacob 	}
2121c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.atios);
2122c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.inots);
2123c87e3f83SMatt Jacob 	mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2124b0a2fdeeSScott Long 	mpt_sysctl_attach(mpt);
2125b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2126b0a2fdeeSScott Long 	    mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
21270e0521a1SMatt Jacob 	return (mpt_configure_ioc(mpt, 0, 0));
21289b631363SMatt Jacob }
21299b631363SMatt Jacob 
2130c87e3f83SMatt Jacob int
2131c87e3f83SMatt Jacob mpt_core_enable(struct mpt_softc *mpt)
2132c87e3f83SMatt Jacob {
2133c87e3f83SMatt Jacob 	/*
2134c87e3f83SMatt Jacob 	 * We enter with the IOC enabled, but async events
2135c87e3f83SMatt Jacob 	 * not enabled, ports not enabled and interrupts
2136c87e3f83SMatt Jacob 	 * not enabled.
2137c87e3f83SMatt Jacob 	 */
2138c87e3f83SMatt Jacob 
2139c87e3f83SMatt Jacob 	/*
2140c87e3f83SMatt Jacob 	 * Enable asynchronous event reporting- all personalities
2141c87e3f83SMatt Jacob 	 * have attached so that they should be able to now field
2142c87e3f83SMatt Jacob 	 * async events.
2143c87e3f83SMatt Jacob 	 */
2144c87e3f83SMatt Jacob 	mpt_send_event_request(mpt, 1);
2145c87e3f83SMatt Jacob 
2146c87e3f83SMatt Jacob 	/*
2147c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2148c87e3f83SMatt Jacob 	 *
2149c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2150c87e3f83SMatt Jacob 	 * the portenable below times out.
2151c87e3f83SMatt Jacob 	 */
2152c87e3f83SMatt Jacob 	mpt_intr(mpt);
2153c87e3f83SMatt Jacob 
2154c87e3f83SMatt Jacob 	/*
2155c87e3f83SMatt Jacob 	 * Enable Interrupts
2156c87e3f83SMatt Jacob 	 */
2157c87e3f83SMatt Jacob 	mpt_enable_ints(mpt);
2158c87e3f83SMatt Jacob 
2159c87e3f83SMatt Jacob 	/*
2160c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2161c87e3f83SMatt Jacob 	 *
2162c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2163c87e3f83SMatt Jacob 	 * the portenable below times out.
2164c87e3f83SMatt Jacob 	 */
2165c87e3f83SMatt Jacob 	mpt_intr(mpt);
2166c87e3f83SMatt Jacob 
2167c87e3f83SMatt Jacob 	/*
21685089bd63SMatt Jacob 	 * Enable the port.
2169c87e3f83SMatt Jacob 	 */
2170c87e3f83SMatt Jacob 	if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2171c87e3f83SMatt Jacob 		mpt_prt(mpt, "failed to enable port 0\n");
2172c87e3f83SMatt Jacob 		return (ENXIO);
2173c87e3f83SMatt Jacob 	}
2174c87e3f83SMatt Jacob 	return (0);
2175c87e3f83SMatt Jacob }
2176c87e3f83SMatt Jacob 
2177b0a2fdeeSScott Long void
2178b0a2fdeeSScott Long mpt_core_shutdown(struct mpt_softc *mpt)
2179b0a2fdeeSScott Long {
2180c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2181b0a2fdeeSScott Long }
2182b0a2fdeeSScott Long 
2183b0a2fdeeSScott Long void
2184b0a2fdeeSScott Long mpt_core_detach(struct mpt_softc *mpt)
2185b0a2fdeeSScott Long {
21860e0521a1SMatt Jacob 	/*
21870e0521a1SMatt Jacob 	 * XXX: FREE MEMORY
21880e0521a1SMatt Jacob 	 */
2189c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2190b0a2fdeeSScott Long }
2191b0a2fdeeSScott Long 
2192b0a2fdeeSScott Long int
2193b0a2fdeeSScott Long mpt_core_unload(struct mpt_personality *pers)
2194b0a2fdeeSScott Long {
2195b0a2fdeeSScott Long 	/* Unload is always successfull. */
2196b0a2fdeeSScott Long 	return (0);
2197b0a2fdeeSScott Long }
2198b0a2fdeeSScott Long 
2199b0a2fdeeSScott Long #define FW_UPLOAD_REQ_SIZE				\
2200b0a2fdeeSScott Long 	(sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION)	\
2201b0a2fdeeSScott Long        + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2202b0a2fdeeSScott Long 
2203b0a2fdeeSScott Long static int
2204b0a2fdeeSScott Long mpt_upload_fw(struct mpt_softc *mpt)
2205b0a2fdeeSScott Long {
2206b0a2fdeeSScott Long 	uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2207b0a2fdeeSScott Long 	MSG_FW_UPLOAD_REPLY fw_reply;
2208b0a2fdeeSScott Long 	MSG_FW_UPLOAD *fw_req;
2209b0a2fdeeSScott Long 	FW_UPLOAD_TCSGE *tsge;
2210b0a2fdeeSScott Long 	SGE_SIMPLE32 *sge;
2211b0a2fdeeSScott Long 	uint32_t flags;
2212b0a2fdeeSScott Long 	int error;
2213b0a2fdeeSScott Long 
2214b0a2fdeeSScott Long 	memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2215b0a2fdeeSScott Long 	fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2216b0a2fdeeSScott Long 	fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2217b0a2fdeeSScott Long 	fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2218b0a2fdeeSScott Long 	fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2219b0a2fdeeSScott Long 	tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2220b0a2fdeeSScott Long 	tsge->DetailsLength = 12;
2221b0a2fdeeSScott Long 	tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2222b0a2fdeeSScott Long 	tsge->ImageSize = htole32(mpt->fw_image_size);
2223b0a2fdeeSScott Long 	sge = (SGE_SIMPLE32 *)(tsge + 1);
2224b0a2fdeeSScott Long 	flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2225b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2226b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2227b0a2fdeeSScott Long 	flags <<= MPI_SGE_FLAGS_SHIFT;
2228b0a2fdeeSScott Long 	sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2229b0a2fdeeSScott Long 	sge->Address = htole32(mpt->fw_phys);
2230b0a2fdeeSScott Long 	error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2231b0a2fdeeSScott Long 	if (error)
2232b0a2fdeeSScott Long 		return(error);
2233b0a2fdeeSScott Long 	error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
2234b0a2fdeeSScott Long 	return (error);
2235b0a2fdeeSScott Long }
2236b0a2fdeeSScott Long 
2237b0a2fdeeSScott Long static void
2238b0a2fdeeSScott Long mpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2239b0a2fdeeSScott Long 	       uint32_t *data, bus_size_t len)
2240b0a2fdeeSScott Long {
2241b0a2fdeeSScott Long 	uint32_t *data_end;
2242b0a2fdeeSScott Long 
2243b0a2fdeeSScott Long 	data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
22449fe6d254SMatt Jacob 	if (mpt->is_sas) {
2245444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
22469fe6d254SMatt Jacob 	}
2247b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2248b0a2fdeeSScott Long 	while (data != data_end) {
2249b0a2fdeeSScott Long 		mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2250b0a2fdeeSScott Long 		data++;
2251b0a2fdeeSScott Long 	}
22529fe6d254SMatt Jacob 	if (mpt->is_sas) {
2253444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2254b0a2fdeeSScott Long 	}
22559fe6d254SMatt Jacob }
2256b0a2fdeeSScott Long 
2257b0a2fdeeSScott Long static int
2258b0a2fdeeSScott Long mpt_download_fw(struct mpt_softc *mpt)
2259b0a2fdeeSScott Long {
2260b0a2fdeeSScott Long 	MpiFwHeader_t *fw_hdr;
2261b0a2fdeeSScott Long 	int error;
2262b0a2fdeeSScott Long 	uint32_t ext_offset;
2263b0a2fdeeSScott Long 	uint32_t data;
2264b0a2fdeeSScott Long 
2265b0a2fdeeSScott Long 	mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2266b0a2fdeeSScott Long 		mpt->fw_image_size);
2267b0a2fdeeSScott Long 
2268b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
2269b0a2fdeeSScott Long 	if (error != 0) {
2270b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2271b0a2fdeeSScott Long 		return (EIO);
2272b0a2fdeeSScott Long 	}
2273b0a2fdeeSScott Long 
2274b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2275b0a2fdeeSScott Long 		  MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2276b0a2fdeeSScott Long 
2277b0a2fdeeSScott Long 	fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
2278b0a2fdeeSScott Long 	mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2279b0a2fdeeSScott Long 		       fw_hdr->ImageSize);
2280b0a2fdeeSScott Long 
2281b0a2fdeeSScott Long 	ext_offset = fw_hdr->NextImageHeaderOffset;
2282b0a2fdeeSScott Long 	while (ext_offset != 0) {
2283b0a2fdeeSScott Long 		MpiExtImageHeader_t *ext;
2284b0a2fdeeSScott Long 
2285b0a2fdeeSScott Long 		ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2286b0a2fdeeSScott Long 		ext_offset = ext->NextImageHeaderOffset;
2287b0a2fdeeSScott Long 
2288b0a2fdeeSScott Long 		mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2289b0a2fdeeSScott Long 			       ext->ImageSize);
2290b0a2fdeeSScott Long 	}
2291b0a2fdeeSScott Long 
22929fe6d254SMatt Jacob 	if (mpt->is_sas) {
2293444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
22949fe6d254SMatt Jacob 	}
2295b0a2fdeeSScott Long 	/* Setup the address to jump to on reset. */
2296b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2297b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2298b0a2fdeeSScott Long 
2299b0a2fdeeSScott Long 	/*
2300b0a2fdeeSScott Long 	 * The controller sets the "flash bad" status after attempting
2301b0a2fdeeSScott Long 	 * to auto-boot from flash.  Clear the status so that the controller
2302b0a2fdeeSScott Long 	 * will continue the boot process with our newly installed firmware.
2303b0a2fdeeSScott Long 	 */
2304b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2305b0a2fdeeSScott Long 	data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2306b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2307b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2308b0a2fdeeSScott Long 
23099fe6d254SMatt Jacob 	if (mpt->is_sas) {
2310444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
23119fe6d254SMatt Jacob 	}
2312444dd2b6SMatt Jacob 
2313b0a2fdeeSScott Long 	/*
2314b0a2fdeeSScott Long 	 * Re-enable the processor and clear the boot halt flag.
2315b0a2fdeeSScott Long 	 */
2316b0a2fdeeSScott Long 	data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2317b0a2fdeeSScott Long 	data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2318b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2319b0a2fdeeSScott Long 
2320b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
2321b0a2fdeeSScott Long 	return (0);
2322b0a2fdeeSScott Long }
2323b0a2fdeeSScott Long 
2324b0a2fdeeSScott Long /*
2325b0a2fdeeSScott Long  * Allocate/Initialize data structures for the controller.  Called
2326b0a2fdeeSScott Long  * once at instance startup.
2327b0a2fdeeSScott Long  */
2328b0a2fdeeSScott Long static int
23290e0521a1SMatt Jacob mpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset)
2330b0a2fdeeSScott Long {
23310e0521a1SMatt Jacob 	PTR_MSG_PORT_FACTS_REPLY pfp;
23320e0521a1SMatt Jacob 	int error,  port;
23330e0521a1SMatt Jacob 	size_t len;
2334b0a2fdeeSScott Long 
23350e0521a1SMatt Jacob 	if (tn == MPT_MAX_TRYS) {
23360e0521a1SMatt Jacob 		return (-1);
23370e0521a1SMatt Jacob 	}
2338b0a2fdeeSScott Long 
23399b631363SMatt Jacob 	/*
23409b631363SMatt Jacob 	 * No need to reset if the IOC is already in the READY state.
23419b631363SMatt Jacob 	 *
23429b631363SMatt Jacob 	 * Force reset if initialization failed previously.
23439b631363SMatt Jacob 	 * Note that a hard_reset of the second channel of a '929
23449b631363SMatt Jacob 	 * will stop operation of the first channel.  Hopefully, if the
23459b631363SMatt Jacob 	 * first channel is ok, the second will not require a hard
23469b631363SMatt Jacob 	 * reset.
23479b631363SMatt Jacob 	 */
23480e0521a1SMatt Jacob 	if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) {
2349c87e3f83SMatt Jacob 		if (mpt_reset(mpt, FALSE) != MPT_OK) {
23500e0521a1SMatt Jacob 			return (mpt_configure_ioc(mpt, tn++, 1));
2351c87e3f83SMatt Jacob 		}
2352b0a2fdeeSScott Long 		needreset = 0;
23537104aeefSMatt Jacob 	}
23547104aeefSMatt Jacob 
23550e0521a1SMatt Jacob 	if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) {
23560e0521a1SMatt Jacob 		mpt_prt(mpt, "mpt_get_iocfacts failed\n");
23570e0521a1SMatt Jacob 		return (mpt_configure_ioc(mpt, tn++, 1));
23580e0521a1SMatt Jacob 	}
23590e0521a1SMatt Jacob 	mpt2host_iocfacts_reply(&mpt->ioc_facts);
23600e0521a1SMatt Jacob 
2361b0a2fdeeSScott Long 	mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
23620e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgVersion >> 8,
23630e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgVersion & 0xFF,
23640e0521a1SMatt Jacob 	    mpt->ioc_facts.HeaderVersion >> 8,
23650e0521a1SMatt Jacob 	    mpt->ioc_facts.HeaderVersion & 0xFF);
2366444dd2b6SMatt Jacob 
2367444dd2b6SMatt Jacob 	/*
2368444dd2b6SMatt Jacob 	 * Now that we know request frame size, we can calculate
2369444dd2b6SMatt Jacob 	 * the actual (reasonable) segment limit for read/write I/O.
2370444dd2b6SMatt Jacob 	 *
2371444dd2b6SMatt Jacob 	 * This limit is constrained by:
2372444dd2b6SMatt Jacob 	 *
2373444dd2b6SMatt Jacob 	 *  + The size of each area we allocate per command (and how
2374444dd2b6SMatt Jacob 	 *    many chain segments we can fit into it).
2375444dd2b6SMatt Jacob 	 *  + The total number of areas we've set up.
2376444dd2b6SMatt Jacob 	 *  + The actual chain depth the card will allow.
2377444dd2b6SMatt Jacob 	 *
2378444dd2b6SMatt Jacob 	 * The first area's segment count is limited by the I/O request
2379444dd2b6SMatt Jacob 	 * at the head of it. We cannot allocate realistically more
2380444dd2b6SMatt Jacob 	 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2381444dd2b6SMatt Jacob 	 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2382444dd2b6SMatt Jacob 	 *
2383444dd2b6SMatt Jacob 	 */
2384444dd2b6SMatt Jacob 	/* total number of request areas we (can) allocate */
2385444dd2b6SMatt Jacob 	mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2386444dd2b6SMatt Jacob 
2387444dd2b6SMatt Jacob 	/* converted to the number of chain areas possible */
2388444dd2b6SMatt Jacob 	mpt->max_seg_cnt *= MPT_NRFM(mpt);
2389444dd2b6SMatt Jacob 
2390444dd2b6SMatt Jacob 	/* limited by the number of chain areas the card will support */
23910e0521a1SMatt Jacob 	if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) {
2392444dd2b6SMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG,
2393444dd2b6SMatt Jacob 		    "chain depth limited to %u (from %u)\n",
23940e0521a1SMatt Jacob 		    mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt);
23950e0521a1SMatt Jacob 		mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth;
2396444dd2b6SMatt Jacob 	}
2397444dd2b6SMatt Jacob 
2398444dd2b6SMatt Jacob 	/* converted to the number of simple sges in chain segments. */
2399444dd2b6SMatt Jacob 	mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2400444dd2b6SMatt Jacob 
24010e0521a1SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "Maximum Segment Count: %u\n",
24020e0521a1SMatt Jacob 	    mpt->max_seg_cnt);
24030e0521a1SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "MsgLength=%u IOCNumber = %d\n",
24040e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber);
2405b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG,
2406444dd2b6SMatt Jacob 	    "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2407444dd2b6SMatt Jacob 	    "Request Frame Size %u bytes Max Chain Depth %u\n",
24080e0521a1SMatt Jacob 	    mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize,
24090e0521a1SMatt Jacob 	    mpt->ioc_facts.RequestFrameSize << 2,
24100e0521a1SMatt Jacob 	    mpt->ioc_facts.MaxChainDepth);
24110e0521a1SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOCFACTS: Num Ports %d, FWImageSize %d, "
24120e0521a1SMatt Jacob 	    "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts,
24130e0521a1SMatt Jacob 	    mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags);
24140e0521a1SMatt Jacob 
24150e0521a1SMatt Jacob 	len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY);
24160e0521a1SMatt Jacob 	mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
24170e0521a1SMatt Jacob 	if (mpt->port_facts == NULL) {
24180e0521a1SMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for port facts\n");
24190e0521a1SMatt Jacob 		return (ENOMEM);
24200e0521a1SMatt Jacob 	}
2421b0a2fdeeSScott Long 
2422444dd2b6SMatt Jacob 
24230e0521a1SMatt Jacob 	if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) &&
24240e0521a1SMatt Jacob 	    (mpt->fw_uploaded == 0)) {
2425b0a2fdeeSScott Long 		struct mpt_map_info mi;
2426b0a2fdeeSScott Long 
2427b0a2fdeeSScott Long 		/*
2428b0a2fdeeSScott Long 		 * In some configurations, the IOC's firmware is
2429b0a2fdeeSScott Long 		 * stored in a shared piece of system NVRAM that
2430b0a2fdeeSScott Long 		 * is only accessable via the BIOS.  In this
2431b0a2fdeeSScott Long 		 * case, the firmware keeps a copy of firmware in
2432b0a2fdeeSScott Long 		 * RAM until the OS driver retrieves it.  Once
2433b0a2fdeeSScott Long 		 * retrieved, we are responsible for re-downloading
2434b0a2fdeeSScott Long 		 * the firmware after any hard-reset.
2435b0a2fdeeSScott Long 		 */
24360e0521a1SMatt Jacob 		mpt->fw_image_size = mpt->ioc_facts.FWImageSize;
24370e0521a1SMatt Jacob 		error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0,
24380e0521a1SMatt Jacob 		    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
24390e0521a1SMatt Jacob 		    mpt->fw_image_size, 1, mpt->fw_image_size, 0,
24400e0521a1SMatt Jacob 		    &mpt->fw_dmat);
2441b0a2fdeeSScott Long 		if (error != 0) {
24420e0521a1SMatt Jacob 			mpt_prt(mpt, "cannot create firmwarew dma tag\n");
2443b0a2fdeeSScott Long 			return (ENOMEM);
24449b631363SMatt Jacob 		}
2445b0a2fdeeSScott Long 		error = bus_dmamem_alloc(mpt->fw_dmat,
24460e0521a1SMatt Jacob 		    (void **)&mpt->fw_image, BUS_DMA_NOWAIT, &mpt->fw_dmap);
2447b0a2fdeeSScott Long 		if (error != 0) {
24480e0521a1SMatt Jacob 			mpt_prt(mpt, "cannot allocate firmware memory\n");
2449b0a2fdeeSScott Long 			bus_dma_tag_destroy(mpt->fw_dmat);
2450b0a2fdeeSScott Long 			return (ENOMEM);
2451b0a2fdeeSScott Long 		}
2452b0a2fdeeSScott Long 		mi.mpt = mpt;
2453b0a2fdeeSScott Long 		mi.error = 0;
2454b0a2fdeeSScott Long 		bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
24550e0521a1SMatt Jacob 		    mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0);
2456b0a2fdeeSScott Long 		mpt->fw_phys = mi.phys;
2457b0a2fdeeSScott Long 
2458b0a2fdeeSScott Long 		error = mpt_upload_fw(mpt);
2459b0a2fdeeSScott Long 		if (error != 0) {
24600e0521a1SMatt Jacob 			mpt_prt(mpt, "firmware upload failed.\n");
2461b0a2fdeeSScott Long 			bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2462b0a2fdeeSScott Long 			bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2463b0a2fdeeSScott Long 			    mpt->fw_dmap);
2464b0a2fdeeSScott Long 			bus_dma_tag_destroy(mpt->fw_dmat);
2465b0a2fdeeSScott Long 			mpt->fw_image = NULL;
2466b0a2fdeeSScott Long 			return (EIO);
2467b0a2fdeeSScott Long 		}
24680e0521a1SMatt Jacob 		mpt->fw_uploaded = 1;
2469b0a2fdeeSScott Long 	}
24709b631363SMatt Jacob 
24710e0521a1SMatt Jacob 	for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) {
24720e0521a1SMatt Jacob 		pfp = &mpt->port_facts[port];
24730e0521a1SMatt Jacob 		error = mpt_get_portfacts(mpt, 0, pfp);
24740e0521a1SMatt Jacob 		if (error != MPT_OK) {
24750e0521a1SMatt Jacob 			mpt_prt(mpt,
24760e0521a1SMatt Jacob 			    "mpt_get_portfacts on port %d failed\n", port);
24770e0521a1SMatt Jacob 			free(mpt->port_facts, M_DEVBUF);
24780e0521a1SMatt Jacob 			mpt->port_facts = NULL;
24790e0521a1SMatt Jacob 			return (mpt_configure_ioc(mpt, tn++, 1));
24800e0521a1SMatt Jacob 		}
24810e0521a1SMatt Jacob 		mpt2host_portfacts_reply(pfp);
24820e0521a1SMatt Jacob 
24830e0521a1SMatt Jacob 		if (port > 0) {
24840e0521a1SMatt Jacob 			error = MPT_PRT_INFO;
24850e0521a1SMatt Jacob 		} else {
24860e0521a1SMatt Jacob 			error = MPT_PRT_DEBUG;
24870e0521a1SMatt Jacob 		}
24880e0521a1SMatt Jacob 		mpt_lprt(mpt, error,
24890e0521a1SMatt Jacob 		    "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n",
24900e0521a1SMatt Jacob 		    port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID,
24910e0521a1SMatt Jacob 		    pfp->MaxDevices);
24920e0521a1SMatt Jacob 
24937104aeefSMatt Jacob 	}
24947104aeefSMatt Jacob 
24950e0521a1SMatt Jacob 	/*
24960e0521a1SMatt Jacob 	 * XXX: Not yet supporting more than port 0
24970e0521a1SMatt Jacob 	 */
24980e0521a1SMatt Jacob 	pfp = &mpt->port_facts[0];
24990e0521a1SMatt Jacob 	if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) {
25007104aeefSMatt Jacob 		mpt->is_fc = 1;
2501444dd2b6SMatt Jacob 		mpt->is_sas = 0;
25025580ce96SMatt Jacob 		mpt->is_spi = 0;
25030e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2504444dd2b6SMatt Jacob 		mpt->is_fc = 0;
2505444dd2b6SMatt Jacob 		mpt->is_sas = 1;
25065580ce96SMatt Jacob 		mpt->is_spi = 0;
25070e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) {
25087104aeefSMatt Jacob 		mpt->is_fc = 0;
2509444dd2b6SMatt Jacob 		mpt->is_sas = 0;
25105580ce96SMatt Jacob 		mpt->is_spi = 1;
25110e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) {
25120e0521a1SMatt Jacob 		mpt_prt(mpt, "iSCSI not supported yet\n");
25130e0521a1SMatt Jacob 		return (ENXIO);
25140e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) {
25150e0521a1SMatt Jacob 		mpt_prt(mpt, "Inactive Port\n");
25160e0521a1SMatt Jacob 		return (ENXIO);
25170e0521a1SMatt Jacob 	} else {
25180e0521a1SMatt Jacob 		mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType);
25190e0521a1SMatt Jacob 		return (ENXIO);
25207104aeefSMatt Jacob 	}
25217104aeefSMatt Jacob 
2522c87e3f83SMatt Jacob 	/*
2523b4c618c0SMatt Jacob 	 * Set our role with what this port supports.
2524b4c618c0SMatt Jacob 	 *
2525b4c618c0SMatt Jacob 	 * Note this might be changed later in different modules
2526b4c618c0SMatt Jacob 	 * if this is different from what is wanted.
2527c87e3f83SMatt Jacob 	 */
25285089bd63SMatt Jacob 	mpt->role = MPT_ROLE_NONE;
25290e0521a1SMatt Jacob 	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
25305089bd63SMatt Jacob 		mpt->role |= MPT_ROLE_INITIATOR;
2531c87e3f83SMatt Jacob 	}
25320e0521a1SMatt Jacob 	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
25335089bd63SMatt Jacob 		mpt->role |= MPT_ROLE_TARGET;
2534c87e3f83SMatt Jacob 	}
25350e0521a1SMatt Jacob 
25360e0521a1SMatt Jacob 	/*
25370e0521a1SMatt Jacob 	 * Enable the IOC
25380e0521a1SMatt Jacob 	 */
2539c87e3f83SMatt Jacob 	if (mpt_enable_ioc(mpt, 0) != MPT_OK) {
2540c87e3f83SMatt Jacob 		mpt_prt(mpt, "unable to initialize IOC\n");
2541b0a2fdeeSScott Long 		return (ENXIO);
25427104aeefSMatt Jacob 	}
25437104aeefSMatt Jacob 
2544b0a2fdeeSScott Long 	/*
2545c87e3f83SMatt Jacob 	 * Read IOC configuration information.
25461d79ca0eSMatt Jacob 	 *
25471d79ca0eSMatt Jacob 	 * We need this to determine whether or not we have certain
25481d79ca0eSMatt Jacob 	 * settings for Integrated Mirroring (e.g.).
2549b0a2fdeeSScott Long 	 */
2550b0a2fdeeSScott Long 	mpt_read_config_info_ioc(mpt);
2551b0a2fdeeSScott Long 
2552b0a2fdeeSScott Long 	return (0);
2553b0a2fdeeSScott Long }
2554b0a2fdeeSScott Long 
2555b0a2fdeeSScott Long static int
2556c87e3f83SMatt Jacob mpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2557b0a2fdeeSScott Long {
2558b0a2fdeeSScott Long 	uint32_t pptr;
2559b0a2fdeeSScott Long 	int val;
2560b0a2fdeeSScott Long 
2561444dd2b6SMatt Jacob 	if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2562b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2563b0a2fdeeSScott Long 		return (EIO);
2564b0a2fdeeSScott Long 	}
2565b0a2fdeeSScott Long 
2566b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
25679b631363SMatt Jacob 
25689b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2569b0a2fdeeSScott Long 		mpt_prt(mpt, "IOC failed to go to run state\n");
2570b0a2fdeeSScott Long 		return (ENXIO);
25717104aeefSMatt Jacob 	}
2572444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
25739b631363SMatt Jacob 
25749b631363SMatt Jacob 	/*
25759b631363SMatt Jacob 	 * Give it reply buffers
25769b631363SMatt Jacob 	 *
2577b0a2fdeeSScott Long 	 * Do *not* exceed global credits.
25789b631363SMatt Jacob 	 */
25799b631363SMatt Jacob 	for (val = 0, pptr = mpt->reply_phys;
25809b631363SMatt Jacob 	    (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
25819b631363SMatt Jacob 	     pptr += MPT_REPLY_SIZE) {
25829b631363SMatt Jacob 		mpt_free_reply(mpt, pptr);
25830e0521a1SMatt Jacob 		if (++val == mpt->ioc_facts.GlobalCredits - 1)
25849b631363SMatt Jacob 			break;
25859b631363SMatt Jacob 	}
25869b631363SMatt Jacob 
2587c87e3f83SMatt Jacob 
2588c87e3f83SMatt Jacob 	/*
25895e073106SMatt Jacob 	 * Enable the port if asked. This is only done if we're resetting
25905e073106SMatt Jacob 	 * the IOC after initial startup.
2591c87e3f83SMatt Jacob 	 */
2592c87e3f83SMatt Jacob 	if (portenable) {
25937104aeefSMatt Jacob 		/*
25947104aeefSMatt Jacob 		 * Enable asynchronous event reporting
25957104aeefSMatt Jacob 		 */
25969b631363SMatt Jacob 		mpt_send_event_request(mpt, 1);
25979b631363SMatt Jacob 
25989b631363SMatt Jacob 		if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2599b0a2fdeeSScott Long 			mpt_prt(mpt, "failed to enable port 0\n");
2600b0a2fdeeSScott Long 			return (ENXIO);
26017104aeefSMatt Jacob 		}
2602c87e3f83SMatt Jacob 	}
260329ae59edSMatt Jacob 	return (MPT_OK);
26049b631363SMatt Jacob }
26050e0521a1SMatt Jacob 
26060e0521a1SMatt Jacob /*
26070e0521a1SMatt Jacob  * Endian Conversion Functions- only used on Big Endian machines
26080e0521a1SMatt Jacob  */
26090e0521a1SMatt Jacob #if	_BYTE_ORDER == _BIG_ENDIAN
26100e0521a1SMatt Jacob void
26110e0521a1SMatt Jacob mpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge)
26120e0521a1SMatt Jacob {
26130e0521a1SMatt Jacob 	MPT_2_HOST32(sge, FlagsLength);
26140e0521a1SMatt Jacob 	MPT_2_HOST64(sge, u.Address64);
26150e0521a1SMatt Jacob };
26160e0521a1SMatt Jacob 
26170e0521a1SMatt Jacob void
26180e0521a1SMatt Jacob mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp)
26190e0521a1SMatt Jacob {
26200e0521a1SMatt Jacob 	MPT_2_HOST16(rp, MsgVersion);
26210e0521a1SMatt Jacob 	MPT_2_HOST16(rp, HeaderVersion);
26220e0521a1SMatt Jacob 	MPT_2_HOST32(rp, MsgContext);
26230e0521a1SMatt Jacob 	MPT_2_HOST16(rp, IOCExceptions);
26240e0521a1SMatt Jacob 	MPT_2_HOST16(rp, IOCStatus);
26250e0521a1SMatt Jacob 	MPT_2_HOST32(rp, IOCLogInfo);
26260e0521a1SMatt Jacob 	MPT_2_HOST16(rp, ReplyQueueDepth);
26270e0521a1SMatt Jacob 	MPT_2_HOST16(rp, RequestFrameSize);
26280e0521a1SMatt Jacob 	MPT_2_HOST16(rp, Reserved_0101_FWVersion);
26290e0521a1SMatt Jacob 	MPT_2_HOST16(rp, ProductID);
26300e0521a1SMatt Jacob 	MPT_2_HOST32(rp, CurrentHostMfaHighAddr);
26310e0521a1SMatt Jacob 	MPT_2_HOST16(rp, GlobalCredits);
26320e0521a1SMatt Jacob 	MPT_2_HOST32(rp, CurrentSenseBufferHighAddr);
26330e0521a1SMatt Jacob 	MPT_2_HOST16(rp, CurReplyFrameSize);
26340e0521a1SMatt Jacob 	MPT_2_HOST32(rp, FWImageSize);
26350e0521a1SMatt Jacob 	MPT_2_HOST32(rp, IOCCapabilities);
26360e0521a1SMatt Jacob 	MPT_2_HOST32(rp, FWVersion.Word);
26370e0521a1SMatt Jacob 	MPT_2_HOST16(rp, HighPriorityQueueDepth);
26380e0521a1SMatt Jacob 	MPT_2_HOST16(rp, Reserved2);
26390e0521a1SMatt Jacob 	mpt2host_sge_simple_union(&rp->HostPageBufferSGE);
26400e0521a1SMatt Jacob 	MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr);
26410e0521a1SMatt Jacob }
26420e0521a1SMatt Jacob 
26430e0521a1SMatt Jacob void
26440e0521a1SMatt Jacob mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp)
26450e0521a1SMatt Jacob {
26460e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved);
26470e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved1);
26480e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, MsgContext);
26490e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved2);
26500e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, IOCStatus);
26510e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, IOCLogInfo);
26520e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxDevices);
26530e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, PortSCSIID);
26540e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, ProtocolFlags);
26550e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxPostedCmdBuffers);
26560e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxPersistentIDs);
26570e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxLanBuckets);
26580e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved4);
26590e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, Reserved5);
26600e0521a1SMatt Jacob }
26610e0521a1SMatt Jacob void
26620e0521a1SMatt Jacob mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2)
26630e0521a1SMatt Jacob {
26640e0521a1SMatt Jacob 	int i;
26650e0521a1SMatt Jacob 	ioc2->CapabilitiesFlags = htole32(ioc2->CapabilitiesFlags);
26660e0521a1SMatt Jacob 	for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) {
26670e0521a1SMatt Jacob 		MPT_2_HOST16(ioc2->RaidVolume[i].Reserved3);
26680e0521a1SMatt Jacob 	}
26690e0521a1SMatt Jacob }
26700e0521a1SMatt Jacob 
26710e0521a1SMatt Jacob void
26720e0521a1SMatt Jacob mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp)
26730e0521a1SMatt Jacob {
26740e0521a1SMatt Jacob 	int i;
26750e0521a1SMatt Jacob 	MPT_2_HOST16(volp, VolumeStatus.Reserved);
26760e0521a1SMatt Jacob 	MPT_2_HOST16(volp, VolumeSettings.Settings);
26770e0521a1SMatt Jacob 	MPT_2_HOST32(volp, MaxLBA);
26780e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved1);
26790e0521a1SMatt Jacob 	MPT_2_HOST32(volp, StripeSize);
26800e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved2);
26810e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved3);
26820e0521a1SMatt Jacob 	for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) {
26830e0521a1SMatt Jacob 		MPT_2_HOST16(volpd, PhysDisk[i].Reserved);
26840e0521a1SMatt Jacob 	}
26850e0521a1SMatt Jacob }
26860e0521a1SMatt Jacob 
26870e0521a1SMatt Jacob void
26880e0521a1SMatt Jacob mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi)
26890e0521a1SMatt Jacob {
26900e0521a1SMatt Jacob 	MPT_2_HOST16(vi, TotalBlocks);
26910e0521a1SMatt Jacob 	MPT_2_HOST16(vi, BlocksRemaining);
26920e0521a1SMatt Jacob }
26930e0521a1SMatt Jacob #endif
2694