xref: /freebsd/sys/dev/mpt/mpt.c (revision 6dc7dc9a3ece89691444e906783892ccaa77a192)
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);
131cac33e16SMarius Strobl static int mpt_dma_buf_alloc(struct mpt_softc *mpt);
1324201341fSKenneth D. Merry static void mpt_dma_buf_free(struct mpt_softc *mpt);
1330e0521a1SMatt Jacob static int mpt_configure_ioc(struct mpt_softc *mpt, int, int);
134c87e3f83SMatt Jacob static int mpt_enable_ioc(struct mpt_softc *mpt, int);
135b0a2fdeeSScott Long 
136b0a2fdeeSScott Long /************************* Personality Module Support *************************/
137b0a2fdeeSScott Long /*
138b0a2fdeeSScott Long  * We include one extra entry that is guaranteed to be NULL
139b0a2fdeeSScott Long  * to simplify our itterator.
140b0a2fdeeSScott Long  */
141b0a2fdeeSScott Long static struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
142b0a2fdeeSScott Long static __inline struct mpt_personality*
143b0a2fdeeSScott Long 	mpt_pers_find(struct mpt_softc *, u_int);
144b0a2fdeeSScott Long static __inline struct mpt_personality*
145b0a2fdeeSScott Long 	mpt_pers_find_reverse(struct mpt_softc *, u_int);
146b0a2fdeeSScott Long 
147b0a2fdeeSScott Long static __inline struct mpt_personality *
148b0a2fdeeSScott Long mpt_pers_find(struct mpt_softc *mpt, u_int start_at)
149b0a2fdeeSScott Long {
150b0a2fdeeSScott Long 	KASSERT(start_at <= MPT_MAX_PERSONALITIES,
151b0a2fdeeSScott Long 		("mpt_pers_find: starting position out of range\n"));
152b0a2fdeeSScott Long 
153b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
154b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
155b0a2fdeeSScott Long 		start_at++;
156b0a2fdeeSScott Long 	}
157b0a2fdeeSScott Long 	return (mpt_personalities[start_at]);
158b0a2fdeeSScott Long }
159b0a2fdeeSScott Long 
160b0a2fdeeSScott Long /*
161c87e3f83SMatt Jacob  * Used infrequently, so no need to optimize like a forward
162b0a2fdeeSScott Long  * traversal where we use the MAX+1 is guaranteed to be NULL
163b0a2fdeeSScott Long  * trick.
164b0a2fdeeSScott Long  */
165b0a2fdeeSScott Long static __inline struct mpt_personality *
166b0a2fdeeSScott Long mpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
167b0a2fdeeSScott Long {
168b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
169b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
170b0a2fdeeSScott Long 		start_at--;
171b0a2fdeeSScott Long 	}
172b0a2fdeeSScott Long 	if (start_at < MPT_MAX_PERSONALITIES)
173b0a2fdeeSScott Long 		return (mpt_personalities[start_at]);
174b0a2fdeeSScott Long 	return (NULL);
175b0a2fdeeSScott Long }
176b0a2fdeeSScott Long 
177b0a2fdeeSScott Long #define MPT_PERS_FOREACH(mpt, pers)				\
178b0a2fdeeSScott Long 	for (pers = mpt_pers_find(mpt, /*start_at*/0);		\
179b0a2fdeeSScott Long 	     pers != NULL;					\
180b0a2fdeeSScott Long 	     pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
181b0a2fdeeSScott Long 
182b0a2fdeeSScott Long #define MPT_PERS_FOREACH_REVERSE(mpt, pers)				\
183b0a2fdeeSScott Long 	for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
184b0a2fdeeSScott Long 	     pers != NULL;						\
185b0a2fdeeSScott Long 	     pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
186b0a2fdeeSScott Long 
187b0a2fdeeSScott Long static mpt_load_handler_t      mpt_stdload;
188b0a2fdeeSScott Long static mpt_probe_handler_t     mpt_stdprobe;
189b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_stdattach;
190c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_stdenable;
191a7303be1SMatt Jacob static mpt_ready_handler_t     mpt_stdready;
192b0a2fdeeSScott Long static mpt_event_handler_t     mpt_stdevent;
193b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_stdreset;
194b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_stdshutdown;
195b0a2fdeeSScott Long static mpt_detach_handler_t    mpt_stddetach;
196b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_stdunload;
197b0a2fdeeSScott Long static struct mpt_personality mpt_default_personality =
198b0a2fdeeSScott Long {
199b0a2fdeeSScott Long 	.load		= mpt_stdload,
200b0a2fdeeSScott Long 	.probe		= mpt_stdprobe,
201b0a2fdeeSScott Long 	.attach		= mpt_stdattach,
202c87e3f83SMatt Jacob 	.enable		= mpt_stdenable,
203a7303be1SMatt Jacob 	.ready		= mpt_stdready,
204b0a2fdeeSScott Long 	.event		= mpt_stdevent,
205b0a2fdeeSScott Long 	.reset		= mpt_stdreset,
206b0a2fdeeSScott Long 	.shutdown	= mpt_stdshutdown,
207b0a2fdeeSScott Long 	.detach		= mpt_stddetach,
208b0a2fdeeSScott Long 	.unload		= mpt_stdunload
209b0a2fdeeSScott Long };
210b0a2fdeeSScott Long 
211b0a2fdeeSScott Long static mpt_load_handler_t      mpt_core_load;
212b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_core_attach;
213c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_core_enable;
214b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_core_ioc_reset;
215b0a2fdeeSScott Long static mpt_event_handler_t     mpt_core_event;
216b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_shutdown;
217b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_detach;
218b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_core_unload;
219b0a2fdeeSScott Long static struct mpt_personality mpt_core_personality =
220b0a2fdeeSScott Long {
221b0a2fdeeSScott Long 	.name		= "mpt_core",
222b0a2fdeeSScott Long 	.load		= mpt_core_load,
223e955d4fdSScott Long //	.attach		= mpt_core_attach,
224e955d4fdSScott Long //	.enable		= mpt_core_enable,
225b0a2fdeeSScott Long 	.event		= mpt_core_event,
226b0a2fdeeSScott Long 	.reset		= mpt_core_ioc_reset,
227b0a2fdeeSScott Long 	.shutdown	= mpt_core_shutdown,
228b0a2fdeeSScott Long 	.detach		= mpt_core_detach,
229b0a2fdeeSScott Long 	.unload		= mpt_core_unload,
230b0a2fdeeSScott Long };
231b0a2fdeeSScott Long 
232b0a2fdeeSScott Long /*
233b0a2fdeeSScott Long  * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
234b0a2fdeeSScott Long  * ordering information.  We want the core to always register FIRST.
235b0a2fdeeSScott Long  * other modules are set to SI_ORDER_SECOND.
236b0a2fdeeSScott Long  */
237b0a2fdeeSScott Long static moduledata_t mpt_core_mod = {
238b0a2fdeeSScott Long 	"mpt_core", mpt_modevent, &mpt_core_personality
239b0a2fdeeSScott Long };
240b0a2fdeeSScott Long DECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
241b0a2fdeeSScott Long MODULE_VERSION(mpt_core, 1);
242b0a2fdeeSScott Long 
243c87e3f83SMatt Jacob #define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
244b0a2fdeeSScott Long 
245b0a2fdeeSScott Long int
246b0a2fdeeSScott Long mpt_modevent(module_t mod, int type, void *data)
247b0a2fdeeSScott Long {
248b0a2fdeeSScott Long 	struct mpt_personality *pers;
249b0a2fdeeSScott Long 	int error;
250b0a2fdeeSScott Long 
251b0a2fdeeSScott Long 	pers = (struct mpt_personality *)data;
252b0a2fdeeSScott Long 
253b0a2fdeeSScott Long 	error = 0;
254b0a2fdeeSScott Long 	switch (type) {
255b0a2fdeeSScott Long 	case MOD_LOAD:
256b0a2fdeeSScott Long 	{
257b0a2fdeeSScott Long 		mpt_load_handler_t **def_handler;
258b0a2fdeeSScott Long 		mpt_load_handler_t **pers_handler;
259b0a2fdeeSScott Long 		int i;
260b0a2fdeeSScott Long 
261b0a2fdeeSScott Long 		for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
262b0a2fdeeSScott Long 			if (mpt_personalities[i] == NULL)
263b0a2fdeeSScott Long 				break;
264b0a2fdeeSScott Long 		}
265b0a2fdeeSScott Long 		if (i >= MPT_MAX_PERSONALITIES) {
266b0a2fdeeSScott Long 			error = ENOMEM;
267b0a2fdeeSScott Long 			break;
268b0a2fdeeSScott Long 		}
269b0a2fdeeSScott Long 		pers->id = i;
270b0a2fdeeSScott Long 		mpt_personalities[i] = pers;
271b0a2fdeeSScott Long 
272b0a2fdeeSScott Long 		/* Install standard/noop handlers for any NULL entries. */
273b0a2fdeeSScott Long 		def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
274b0a2fdeeSScott Long 		pers_handler = MPT_PERS_FIRST_HANDLER(pers);
275b0a2fdeeSScott Long 		while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
276b0a2fdeeSScott Long 			if (*pers_handler == NULL)
277b0a2fdeeSScott Long 				*pers_handler = *def_handler;
278b0a2fdeeSScott Long 			pers_handler++;
279b0a2fdeeSScott Long 			def_handler++;
280b0a2fdeeSScott Long 		}
281b0a2fdeeSScott Long 
282b0a2fdeeSScott Long 		error = (pers->load(pers));
283b0a2fdeeSScott Long 		if (error != 0)
284b0a2fdeeSScott Long 			mpt_personalities[i] = NULL;
285b0a2fdeeSScott Long 		break;
286b0a2fdeeSScott Long 	}
287b0a2fdeeSScott Long 	case MOD_SHUTDOWN:
288b0a2fdeeSScott Long 		break;
289c87e3f83SMatt Jacob #if __FreeBSD_version >= 500000
290b0a2fdeeSScott Long 	case MOD_QUIESCE:
291b0a2fdeeSScott Long 		break;
292c87e3f83SMatt Jacob #endif
293b0a2fdeeSScott Long 	case MOD_UNLOAD:
294b0a2fdeeSScott Long 		error = pers->unload(pers);
295b0a2fdeeSScott Long 		mpt_personalities[pers->id] = NULL;
296b0a2fdeeSScott Long 		break;
297b0a2fdeeSScott Long 	default:
298b0a2fdeeSScott Long 		error = EINVAL;
299b0a2fdeeSScott Long 		break;
300b0a2fdeeSScott Long 	}
301b0a2fdeeSScott Long 	return (error);
302b0a2fdeeSScott Long }
303b0a2fdeeSScott Long 
304b0a2fdeeSScott Long int
305b0a2fdeeSScott Long mpt_stdload(struct mpt_personality *pers)
306b0a2fdeeSScott Long {
307b0a2fdeeSScott Long 	/* Load is always successfull. */
308b0a2fdeeSScott Long 	return (0);
309b0a2fdeeSScott Long }
310b0a2fdeeSScott Long 
311b0a2fdeeSScott Long int
312b0a2fdeeSScott Long mpt_stdprobe(struct mpt_softc *mpt)
313b0a2fdeeSScott Long {
314b0a2fdeeSScott Long 	/* Probe is always successfull. */
315b0a2fdeeSScott Long 	return (0);
316b0a2fdeeSScott Long }
317b0a2fdeeSScott Long 
318b0a2fdeeSScott Long int
319b0a2fdeeSScott Long mpt_stdattach(struct mpt_softc *mpt)
320b0a2fdeeSScott Long {
321b0a2fdeeSScott Long 	/* Attach is always successfull. */
322b0a2fdeeSScott Long 	return (0);
323b0a2fdeeSScott Long }
324b0a2fdeeSScott Long 
325b0a2fdeeSScott Long int
326c87e3f83SMatt Jacob mpt_stdenable(struct mpt_softc *mpt)
327c87e3f83SMatt Jacob {
328c87e3f83SMatt Jacob 	/* Enable is always successfull. */
329c87e3f83SMatt Jacob 	return (0);
330c87e3f83SMatt Jacob }
331c87e3f83SMatt Jacob 
332a7303be1SMatt Jacob void
333a7303be1SMatt Jacob mpt_stdready(struct mpt_softc *mpt)
334a7303be1SMatt Jacob {
335a7303be1SMatt Jacob }
336a7303be1SMatt Jacob 
337a7303be1SMatt Jacob 
338c87e3f83SMatt Jacob int
339444dd2b6SMatt Jacob mpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
340b0a2fdeeSScott Long {
341444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
342b0a2fdeeSScott Long 	/* Event was not for us. */
343b0a2fdeeSScott Long 	return (0);
344b0a2fdeeSScott Long }
345b0a2fdeeSScott Long 
346b0a2fdeeSScott Long void
347b0a2fdeeSScott Long mpt_stdreset(struct mpt_softc *mpt, int type)
348b0a2fdeeSScott Long {
349b0a2fdeeSScott Long }
350b0a2fdeeSScott Long 
351b0a2fdeeSScott Long void
352b0a2fdeeSScott Long mpt_stdshutdown(struct mpt_softc *mpt)
353b0a2fdeeSScott Long {
354b0a2fdeeSScott Long }
355b0a2fdeeSScott Long 
356b0a2fdeeSScott Long void
357b0a2fdeeSScott Long mpt_stddetach(struct mpt_softc *mpt)
358b0a2fdeeSScott Long {
359b0a2fdeeSScott Long }
360b0a2fdeeSScott Long 
361b0a2fdeeSScott Long int
362b0a2fdeeSScott Long mpt_stdunload(struct mpt_personality *pers)
363b0a2fdeeSScott Long {
364b0a2fdeeSScott Long 	/* Unload is always successfull. */
365b0a2fdeeSScott Long 	return (0);
366b0a2fdeeSScott Long }
367b0a2fdeeSScott Long 
368a7303be1SMatt Jacob /*
369a7303be1SMatt Jacob  * Post driver attachment, we may want to perform some global actions.
370a7303be1SMatt Jacob  * Here is the hook to do so.
371a7303be1SMatt Jacob  */
372a7303be1SMatt Jacob 
373a7303be1SMatt Jacob static void
374a7303be1SMatt Jacob mpt_postattach(void *unused)
375a7303be1SMatt Jacob {
376a7303be1SMatt Jacob 	struct mpt_softc *mpt;
377a7303be1SMatt Jacob 	struct mpt_personality *pers;
378a7303be1SMatt Jacob 
379a7303be1SMatt Jacob 	TAILQ_FOREACH(mpt, &mpt_tailq, links) {
380a7303be1SMatt Jacob 		MPT_PERS_FOREACH(mpt, pers)
381a7303be1SMatt Jacob 			pers->ready(mpt);
382a7303be1SMatt Jacob 	}
383a7303be1SMatt Jacob }
384a7303be1SMatt Jacob SYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL);
385a7303be1SMatt Jacob 
386a7303be1SMatt Jacob 
387b0a2fdeeSScott Long /******************************* Bus DMA Support ******************************/
388b0a2fdeeSScott Long void
389b0a2fdeeSScott Long mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
390b0a2fdeeSScott Long {
391b0a2fdeeSScott Long 	struct mpt_map_info *map_info;
392b0a2fdeeSScott Long 
393b0a2fdeeSScott Long 	map_info = (struct mpt_map_info *)arg;
394b0a2fdeeSScott Long 	map_info->error = error;
395b0a2fdeeSScott Long 	map_info->phys = segs->ds_addr;
396b0a2fdeeSScott Long }
397b0a2fdeeSScott Long 
398b0a2fdeeSScott Long /**************************** Reply/Event Handling ****************************/
399b0a2fdeeSScott Long int
400b0a2fdeeSScott Long mpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
401b0a2fdeeSScott Long 		     mpt_handler_t handler, uint32_t *phandler_id)
402b0a2fdeeSScott Long {
403b0a2fdeeSScott Long 
404b0a2fdeeSScott Long 	switch (type) {
405b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
406b0a2fdeeSScott Long 	{
407b0a2fdeeSScott Long 		u_int cbi;
408b0a2fdeeSScott Long 		u_int free_cbi;
409b0a2fdeeSScott Long 
410b0a2fdeeSScott Long 		if (phandler_id == NULL)
411b0a2fdeeSScott Long 			return (EINVAL);
412b0a2fdeeSScott Long 
413b0a2fdeeSScott Long 		free_cbi = MPT_HANDLER_ID_NONE;
414b0a2fdeeSScott Long 		for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
415b0a2fdeeSScott Long 			/*
416b0a2fdeeSScott Long 			 * If the same handler is registered multiple
417b0a2fdeeSScott Long 			 * times, don't error out.  Just return the
418b0a2fdeeSScott Long 			 * index of the original registration.
419b0a2fdeeSScott Long 			 */
420b0a2fdeeSScott Long 			if (mpt_reply_handlers[cbi] == handler.reply_handler) {
421b0a2fdeeSScott Long 				*phandler_id = MPT_CBI_TO_HID(cbi);
422b0a2fdeeSScott Long 				return (0);
423b0a2fdeeSScott Long 			}
424b0a2fdeeSScott Long 
425b0a2fdeeSScott Long 			/*
426b0a2fdeeSScott Long 			 * Fill from the front in the hope that
427b0a2fdeeSScott Long 			 * all registered handlers consume only a
428b0a2fdeeSScott Long 			 * single cache line.
429b0a2fdeeSScott Long 			 *
430b0a2fdeeSScott Long 			 * We don't break on the first empty slot so
431b0a2fdeeSScott Long 			 * that the full table is checked to see if
432b0a2fdeeSScott Long 			 * this handler was previously registered.
433b0a2fdeeSScott Long 			 */
434c87e3f83SMatt Jacob 			if (free_cbi == MPT_HANDLER_ID_NONE &&
435c87e3f83SMatt Jacob 			    (mpt_reply_handlers[cbi]
436b0a2fdeeSScott Long 			  == mpt_default_reply_handler))
437b0a2fdeeSScott Long 				free_cbi = cbi;
438b0a2fdeeSScott Long 		}
439c87e3f83SMatt Jacob 		if (free_cbi == MPT_HANDLER_ID_NONE) {
440b0a2fdeeSScott Long 			return (ENOMEM);
441c87e3f83SMatt Jacob 		}
442b0a2fdeeSScott Long 		mpt_reply_handlers[free_cbi] = handler.reply_handler;
443b0a2fdeeSScott Long 		*phandler_id = MPT_CBI_TO_HID(free_cbi);
444b0a2fdeeSScott Long 		break;
445b0a2fdeeSScott Long 	}
446b0a2fdeeSScott Long 	default:
447b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
448b0a2fdeeSScott Long 		return (EINVAL);
449b0a2fdeeSScott Long 	}
450b0a2fdeeSScott Long 	return (0);
451b0a2fdeeSScott Long }
452b0a2fdeeSScott Long 
453b0a2fdeeSScott Long int
454b0a2fdeeSScott Long mpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
455b0a2fdeeSScott Long 		       mpt_handler_t handler, uint32_t handler_id)
456b0a2fdeeSScott Long {
457b0a2fdeeSScott Long 
458b0a2fdeeSScott Long 	switch (type) {
459b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
460b0a2fdeeSScott Long 	{
461b0a2fdeeSScott Long 		u_int cbi;
462b0a2fdeeSScott Long 
463b0a2fdeeSScott Long 		cbi = MPT_CBI(handler_id);
464b0a2fdeeSScott Long 		if (cbi >= MPT_NUM_REPLY_HANDLERS
465b0a2fdeeSScott Long 		 || mpt_reply_handlers[cbi] != handler.reply_handler)
466b0a2fdeeSScott Long 			return (ENOENT);
467b0a2fdeeSScott Long 		mpt_reply_handlers[cbi] = mpt_default_reply_handler;
468b0a2fdeeSScott Long 		break;
469b0a2fdeeSScott Long 	}
470b0a2fdeeSScott Long 	default:
471b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
472b0a2fdeeSScott Long 		return (EINVAL);
473b0a2fdeeSScott Long 	}
474b0a2fdeeSScott Long 	return (0);
475b0a2fdeeSScott Long }
476b0a2fdeeSScott Long 
477b0a2fdeeSScott Long static int
478b0a2fdeeSScott Long mpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
479c87e3f83SMatt Jacob 	uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
480b0a2fdeeSScott Long {
481c87e3f83SMatt Jacob 	mpt_prt(mpt,
482c87e3f83SMatt Jacob 	    "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
483c87e3f83SMatt Jacob 	    req, req->serno, reply_desc, reply_frame);
484b0a2fdeeSScott Long 
485b0a2fdeeSScott Long 	if (reply_frame != NULL)
486b0a2fdeeSScott Long 		mpt_dump_reply_frame(mpt, reply_frame);
487b0a2fdeeSScott Long 
488c87e3f83SMatt Jacob 	mpt_prt(mpt, "Reply Frame Ignored\n");
489b0a2fdeeSScott Long 
490b0a2fdeeSScott Long 	return (/*free_reply*/TRUE);
491b0a2fdeeSScott Long }
492b0a2fdeeSScott Long 
493b0a2fdeeSScott Long static int
494b0a2fdeeSScott Long mpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
495c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
496b0a2fdeeSScott Long {
497b0a2fdeeSScott Long 	if (req != NULL) {
498b0a2fdeeSScott Long 
499b0a2fdeeSScott Long 		if (reply_frame != NULL) {
500b0a2fdeeSScott Long 			MSG_CONFIG *cfgp;
501b0a2fdeeSScott Long 			MSG_CONFIG_REPLY *reply;
502b0a2fdeeSScott Long 
503b0a2fdeeSScott Long 			cfgp = (MSG_CONFIG *)req->req_vbuf;
504b0a2fdeeSScott Long 			reply = (MSG_CONFIG_REPLY *)reply_frame;
505b0a2fdeeSScott Long 			req->IOCStatus = le16toh(reply_frame->IOCStatus);
506b0a2fdeeSScott Long 			bcopy(&reply->Header, &cfgp->Header,
507b0a2fdeeSScott Long 			      sizeof(cfgp->Header));
5081d558d6aSScott Long 			cfgp->ExtPageLength = reply->ExtPageLength;
5091d558d6aSScott Long 			cfgp->ExtPageType = reply->ExtPageType;
510b0a2fdeeSScott Long 		}
511b0a2fdeeSScott Long 		req->state &= ~REQ_STATE_QUEUED;
512b0a2fdeeSScott Long 		req->state |= REQ_STATE_DONE;
513b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5145e073106SMatt Jacob 		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
515b0a2fdeeSScott Long 			wakeup(req);
5166621d786SMatt Jacob 		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
5176621d786SMatt Jacob 			/*
5186621d786SMatt Jacob 			 * Whew- we can free this request (late completion)
5196621d786SMatt Jacob 			 */
5206621d786SMatt Jacob 			mpt_free_request(mpt, req);
521b0a2fdeeSScott Long 		}
5225e073106SMatt Jacob 	}
523b0a2fdeeSScott Long 
5245e073106SMatt Jacob 	return (TRUE);
525b0a2fdeeSScott Long }
526b0a2fdeeSScott Long 
527b0a2fdeeSScott Long static int
528b0a2fdeeSScott Long mpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
529c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
530b0a2fdeeSScott Long {
531b0a2fdeeSScott Long 	/* Nothing to be done. */
5325e073106SMatt Jacob 	return (TRUE);
533b0a2fdeeSScott Long }
534b0a2fdeeSScott Long 
535b0a2fdeeSScott Long static int
536b0a2fdeeSScott Long mpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
537c87e3f83SMatt Jacob     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
538b0a2fdeeSScott Long {
539b0a2fdeeSScott Long 	int free_reply;
540b0a2fdeeSScott Long 
5415e073106SMatt Jacob 	KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
5425e073106SMatt Jacob 	KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
543b0a2fdeeSScott Long 
544b0a2fdeeSScott Long 	free_reply = TRUE;
545b0a2fdeeSScott Long 	switch (reply_frame->Function) {
546b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_NOTIFICATION:
547b0a2fdeeSScott Long 	{
548b0a2fdeeSScott Long 		MSG_EVENT_NOTIFY_REPLY *msg;
549b0a2fdeeSScott Long 		struct mpt_personality *pers;
550b0a2fdeeSScott Long 		u_int handled;
551b0a2fdeeSScott Long 
552b0a2fdeeSScott Long 		handled = 0;
553b0a2fdeeSScott Long 		msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
5543f970273SJohn Birrell 		msg->EventDataLength = le16toh(msg->EventDataLength);
5553f970273SJohn Birrell 		msg->IOCStatus = le16toh(msg->IOCStatus);
5563f970273SJohn Birrell 		msg->IOCLogInfo = le32toh(msg->IOCLogInfo);
5573f970273SJohn Birrell 		msg->Event = le32toh(msg->Event);
558b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
559b0a2fdeeSScott Long 			handled += pers->event(mpt, req, msg);
560b0a2fdeeSScott Long 
561444dd2b6SMatt Jacob 		if (handled == 0 && mpt->mpt_pers_mask == 0) {
5621977cbd6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_INFO,
563444dd2b6SMatt Jacob 				"No Handlers For Any Event Notify Frames. "
564444dd2b6SMatt Jacob 				"Event %#x (ACK %sequired).\n",
565444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
566444dd2b6SMatt Jacob 		} else if (handled == 0) {
567fee67c1aSScott Long 			mpt_lprt(mpt,
568fee67c1aSScott Long 				msg->AckRequired? MPT_PRT_WARN : MPT_PRT_INFO,
569444dd2b6SMatt Jacob 				"Unhandled Event Notify Frame. Event %#x "
570444dd2b6SMatt Jacob 				"(ACK %sequired).\n",
571444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
572444dd2b6SMatt Jacob 		}
573b0a2fdeeSScott Long 
574b0a2fdeeSScott Long 		if (msg->AckRequired) {
575b0a2fdeeSScott Long 			request_t *ack_req;
576b0a2fdeeSScott Long 			uint32_t context;
577b0a2fdeeSScott Long 
5783f970273SJohn Birrell 			context = req->index | MPT_REPLY_HANDLER_EVENTS;
5795e073106SMatt Jacob 			ack_req = mpt_get_request(mpt, FALSE);
580b0a2fdeeSScott Long 			if (ack_req == NULL) {
581b0a2fdeeSScott Long 				struct mpt_evtf_record *evtf;
582b0a2fdeeSScott Long 
583b0a2fdeeSScott Long 				evtf = (struct mpt_evtf_record *)reply_frame;
584b0a2fdeeSScott Long 				evtf->context = context;
585b0a2fdeeSScott Long 				LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
586b0a2fdeeSScott Long 				free_reply = FALSE;
587b0a2fdeeSScott Long 				break;
588b0a2fdeeSScott Long 			}
589b0a2fdeeSScott Long 			mpt_send_event_ack(mpt, ack_req, msg, context);
5905e073106SMatt Jacob 			/*
5915e073106SMatt Jacob 			 * Don't check for CONTINUATION_REPLY here
5925e073106SMatt Jacob 			 */
5935e073106SMatt Jacob 			return (free_reply);
594b0a2fdeeSScott Long 		}
595b0a2fdeeSScott Long 		break;
596b0a2fdeeSScott Long 	}
597b0a2fdeeSScott Long 	case MPI_FUNCTION_PORT_ENABLE:
598b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
599b0a2fdeeSScott Long 		break;
600b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_ACK:
601b0a2fdeeSScott Long 		break;
602b0a2fdeeSScott Long 	default:
6035e073106SMatt Jacob 		mpt_prt(mpt, "unknown event function: %x\n",
604b0a2fdeeSScott Long 			reply_frame->Function);
605b0a2fdeeSScott Long 		break;
606b0a2fdeeSScott Long 	}
607b0a2fdeeSScott Long 
6085e073106SMatt Jacob 	/*
6095e073106SMatt Jacob 	 * I'm not sure that this continuation stuff works as it should.
6105e073106SMatt Jacob 	 *
6115e073106SMatt Jacob 	 * I've had FC async events occur that free the frame up because
6125e073106SMatt Jacob 	 * the continuation bit isn't set, and then additional async events
6135e073106SMatt Jacob 	 * then occur using the same context. As you might imagine, this
6145e073106SMatt Jacob 	 * leads to Very Bad Thing.
6155e073106SMatt Jacob 	 *
6165e073106SMatt Jacob 	 *  Let's just be safe for now and not free them up until we figure
6175e073106SMatt Jacob 	 * out what's actually happening here.
6185e073106SMatt Jacob 	 */
6195e073106SMatt Jacob #if	0
6205e073106SMatt Jacob 	if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
621b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
622b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
6235e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
6245e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
6255e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6265e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6275e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6285e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6295e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
630b0a2fdeeSScott Long 		}
6315e073106SMatt Jacob 	} else {
6325e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
6335e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
6345e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6355e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6365e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6375e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6385e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
6395e073106SMatt Jacob 		}
6405e073106SMatt Jacob 		mpt_prtc(mpt, "\n");
6415e073106SMatt Jacob 	}
6425e073106SMatt Jacob #endif
643b0a2fdeeSScott Long 	return (free_reply);
644b0a2fdeeSScott Long }
645b0a2fdeeSScott Long 
646b0a2fdeeSScott Long /*
647b0a2fdeeSScott Long  * Process an asynchronous event from the IOC.
648b0a2fdeeSScott Long  */
649b0a2fdeeSScott Long static int
650b0a2fdeeSScott Long mpt_core_event(struct mpt_softc *mpt, request_t *req,
651b0a2fdeeSScott Long 	       MSG_EVENT_NOTIFY_REPLY *msg)
652b0a2fdeeSScott Long {
653444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
654444dd2b6SMatt Jacob                  msg->Event & 0xFF);
655b0a2fdeeSScott Long 	switch(msg->Event & 0xFF) {
656b0a2fdeeSScott Long 	case MPI_EVENT_NONE:
657b0a2fdeeSScott Long 		break;
658b0a2fdeeSScott Long 	case MPI_EVENT_LOG_DATA:
659b0a2fdeeSScott Long 	{
660b0a2fdeeSScott Long 		int i;
661b0a2fdeeSScott Long 
662b0a2fdeeSScott Long 		/* Some error occured that LSI wants logged */
663b0a2fdeeSScott Long 		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
664b0a2fdeeSScott Long 			msg->IOCLogInfo);
665b0a2fdeeSScott Long 		mpt_prt(mpt, "\tEvtLogData: Event Data:");
666b0a2fdeeSScott Long 		for (i = 0; i < msg->EventDataLength; i++)
667b0a2fdeeSScott Long 			mpt_prtc(mpt, "  %08x", msg->Data[i]);
668b0a2fdeeSScott Long 		mpt_prtc(mpt, "\n");
669b0a2fdeeSScott Long 		break;
670b0a2fdeeSScott Long 	}
671b0a2fdeeSScott Long 	case MPI_EVENT_EVENT_CHANGE:
672b0a2fdeeSScott Long 		/*
673b0a2fdeeSScott Long 		 * This is just an acknowledgement
674b0a2fdeeSScott Long 		 * of our mpt_send_event_request.
675b0a2fdeeSScott Long 		 */
676b0a2fdeeSScott Long 		break;
677444dd2b6SMatt Jacob 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
678444dd2b6SMatt Jacob 		break;
679b0a2fdeeSScott Long 	default:
6805e073106SMatt Jacob 		return (0);
681b0a2fdeeSScott Long 		break;
682b0a2fdeeSScott Long 	}
6835e073106SMatt Jacob 	return (1);
684b0a2fdeeSScott Long }
685b0a2fdeeSScott Long 
686b0a2fdeeSScott Long static void
687b0a2fdeeSScott Long mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
688b0a2fdeeSScott Long 		   MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
689b0a2fdeeSScott Long {
690b0a2fdeeSScott Long 	MSG_EVENT_ACK *ackp;
691b0a2fdeeSScott Long 
692b0a2fdeeSScott Long 	ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
6935e073106SMatt Jacob 	memset(ackp, 0, sizeof (*ackp));
694b0a2fdeeSScott Long 	ackp->Function = MPI_FUNCTION_EVENT_ACK;
6953f970273SJohn Birrell 	ackp->Event = htole32(msg->Event);
6963f970273SJohn Birrell 	ackp->EventContext = htole32(msg->EventContext);
6973f970273SJohn Birrell 	ackp->MsgContext = htole32(context);
698b0a2fdeeSScott Long 	mpt_check_doorbell(mpt);
699b0a2fdeeSScott Long 	mpt_send_cmd(mpt, ack_req);
700b0a2fdeeSScott Long }
701b0a2fdeeSScott Long 
702b0a2fdeeSScott Long /***************************** Interrupt Handling *****************************/
703b0a2fdeeSScott Long void
704b0a2fdeeSScott Long mpt_intr(void *arg)
705b0a2fdeeSScott Long {
706b0a2fdeeSScott Long 	struct mpt_softc *mpt;
707b0a2fdeeSScott Long 	uint32_t reply_desc;
708c87e3f83SMatt Jacob 	int ntrips = 0;
709b0a2fdeeSScott Long 
710b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)arg;
7115089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
712d0a68c27SMatt Jacob 	MPT_LOCK_ASSERT(mpt);
713d0a68c27SMatt Jacob 
714b0a2fdeeSScott Long 	while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
715b0a2fdeeSScott Long 		request_t	  *req;
716b0a2fdeeSScott Long 		MSG_DEFAULT_REPLY *reply_frame;
717b0a2fdeeSScott Long 		uint32_t	   reply_baddr;
718c87e3f83SMatt Jacob 		uint32_t           ctxt_idx;
719b0a2fdeeSScott Long 		u_int		   cb_index;
720b0a2fdeeSScott Long 		u_int		   req_index;
721b0a2fdeeSScott Long 		int		   free_rf;
722b0a2fdeeSScott Long 
723b0a2fdeeSScott Long 		req = NULL;
724b0a2fdeeSScott Long 		reply_frame = NULL;
725b0a2fdeeSScott Long 		reply_baddr = 0;
726b0a2fdeeSScott Long 		if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
727b0a2fdeeSScott Long 			u_int offset;
728b0a2fdeeSScott Long 			/*
729b0a2fdeeSScott Long 			 * Insure that the reply frame is coherent.
730b0a2fdeeSScott Long 			 */
7315e073106SMatt Jacob 			reply_baddr = MPT_REPLY_BADDR(reply_desc);
732b0a2fdeeSScott Long 			offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
733c87e3f83SMatt Jacob 			bus_dmamap_sync_range(mpt->reply_dmat,
734c87e3f83SMatt Jacob 			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
735b0a2fdeeSScott Long 			    BUS_DMASYNC_POSTREAD);
736b0a2fdeeSScott Long 			reply_frame = MPT_REPLY_OTOV(mpt, offset);
737c87e3f83SMatt Jacob 			ctxt_idx = le32toh(reply_frame->MsgContext);
738c87e3f83SMatt Jacob 		} else {
739c87e3f83SMatt Jacob 			uint32_t type;
740b0a2fdeeSScott Long 
741c87e3f83SMatt Jacob 			type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
742c87e3f83SMatt Jacob 			ctxt_idx = reply_desc;
743c87e3f83SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
744c87e3f83SMatt Jacob 				    reply_desc);
745c87e3f83SMatt Jacob 
746c87e3f83SMatt Jacob 			switch (type) {
747c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
748c87e3f83SMatt Jacob 				ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
749c87e3f83SMatt Jacob 				break;
750c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
751c87e3f83SMatt Jacob 				ctxt_idx = GET_IO_INDEX(reply_desc);
752c87e3f83SMatt Jacob 				if (mpt->tgt_cmd_ptrs == NULL) {
753c87e3f83SMatt Jacob 					mpt_prt(mpt,
754c87e3f83SMatt Jacob 					    "mpt_intr: no target cmd ptrs\n");
755c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
756c87e3f83SMatt Jacob 					break;
757c87e3f83SMatt Jacob 				}
758c87e3f83SMatt Jacob 				if (ctxt_idx >= mpt->tgt_cmds_allocated) {
759c87e3f83SMatt Jacob 					mpt_prt(mpt,
760c87e3f83SMatt Jacob 					    "mpt_intr: bad tgt cmd ctxt %u\n",
761c87e3f83SMatt Jacob 					    ctxt_idx);
762c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
763c87e3f83SMatt Jacob 					ntrips = 1000;
764c87e3f83SMatt Jacob 					break;
765c87e3f83SMatt Jacob 				}
766c87e3f83SMatt Jacob 				req = mpt->tgt_cmd_ptrs[ctxt_idx];
767c87e3f83SMatt Jacob 				if (req == NULL) {
768c87e3f83SMatt Jacob 					mpt_prt(mpt, "no request backpointer "
769c87e3f83SMatt Jacob 					    "at index %u", ctxt_idx);
770c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
771c87e3f83SMatt Jacob 					ntrips = 1000;
772c87e3f83SMatt Jacob 					break;
773c87e3f83SMatt Jacob 				}
774c87e3f83SMatt Jacob 				/*
775c87e3f83SMatt Jacob 				 * Reformulate ctxt_idx to be just as if
776c87e3f83SMatt Jacob 				 * it were another type of context reply
777c87e3f83SMatt Jacob 				 * so the code below will find the request
778c87e3f83SMatt Jacob 				 * via indexing into the pool.
779c87e3f83SMatt Jacob 				 */
780c87e3f83SMatt Jacob 				ctxt_idx =
781c87e3f83SMatt Jacob 				    req->index | mpt->scsi_tgt_handler_id;
782c87e3f83SMatt Jacob 				req = NULL;
783c87e3f83SMatt Jacob 				break;
784c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_LAN:
785c87e3f83SMatt Jacob 				mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
786c87e3f83SMatt Jacob 				    reply_desc);
787c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
788c87e3f83SMatt Jacob 				break;
789c87e3f83SMatt Jacob 			default:
790c87e3f83SMatt Jacob 				mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
791c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
792c87e3f83SMatt Jacob 				break;
793c87e3f83SMatt Jacob 			}
794c87e3f83SMatt Jacob 			if (reply_desc == MPT_REPLY_EMPTY) {
795c87e3f83SMatt Jacob 				if (ntrips++ > 1000) {
796c87e3f83SMatt Jacob 					break;
797c87e3f83SMatt Jacob 				}
798c87e3f83SMatt Jacob 				continue;
799c87e3f83SMatt Jacob 			}
800c87e3f83SMatt Jacob 		}
801c87e3f83SMatt Jacob 
802c87e3f83SMatt Jacob 		cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
803c87e3f83SMatt Jacob 		req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
804c87e3f83SMatt Jacob 		if (req_index < MPT_MAX_REQUESTS(mpt)) {
805c87e3f83SMatt Jacob 			req = &mpt->request_pool[req_index];
8065e073106SMatt Jacob 		} else {
8075e073106SMatt Jacob 			mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
8085e073106SMatt Jacob 			    " 0x%x)\n", req_index, reply_desc);
809c87e3f83SMatt Jacob 		}
810c87e3f83SMatt Jacob 
811c87e3f83SMatt Jacob 		free_rf = mpt_reply_handlers[cb_index](mpt, req,
812c87e3f83SMatt Jacob 		    reply_desc, reply_frame);
813b0a2fdeeSScott Long 
8145e073106SMatt Jacob 		if (reply_frame != NULL && free_rf) {
815b0a2fdeeSScott Long 			mpt_free_reply(mpt, reply_baddr);
8165e073106SMatt Jacob 		}
817c87e3f83SMatt Jacob 
818c87e3f83SMatt Jacob 		/*
819c87e3f83SMatt Jacob 		 * If we got ourselves disabled, don't get stuck in a loop
820c87e3f83SMatt Jacob 		 */
821c87e3f83SMatt Jacob 		if (mpt->disabled) {
822c87e3f83SMatt Jacob 			mpt_disable_ints(mpt);
823c87e3f83SMatt Jacob 			break;
824c87e3f83SMatt Jacob 		}
825c87e3f83SMatt Jacob 		if (ntrips++ > 1000) {
826c87e3f83SMatt Jacob 			break;
827c87e3f83SMatt Jacob 		}
828b0a2fdeeSScott Long 	}
8295089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
830b0a2fdeeSScott Long }
831b0a2fdeeSScott Long 
832b0a2fdeeSScott Long /******************************* Error Recovery *******************************/
833b0a2fdeeSScott Long void
834b0a2fdeeSScott Long mpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
835b0a2fdeeSScott Long 			    u_int iocstatus)
836b0a2fdeeSScott Long {
837b0a2fdeeSScott Long 	MSG_DEFAULT_REPLY  ioc_status_frame;
838b0a2fdeeSScott Long 	request_t	  *req;
839b0a2fdeeSScott Long 
8405e073106SMatt Jacob 	memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
841b0a2fdeeSScott Long 	ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
842b0a2fdeeSScott Long 	ioc_status_frame.IOCStatus = iocstatus;
843b0a2fdeeSScott Long 	while((req = TAILQ_FIRST(chain)) != NULL) {
844b0a2fdeeSScott Long 		MSG_REQUEST_HEADER *msg_hdr;
845b0a2fdeeSScott Long 		u_int		    cb_index;
8465e073106SMatt Jacob 
847b0a2fdeeSScott Long 		msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
848b0a2fdeeSScott Long 		ioc_status_frame.Function = msg_hdr->Function;
849b0a2fdeeSScott Long 		ioc_status_frame.MsgContext = msg_hdr->MsgContext;
850b0a2fdeeSScott Long 		cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
851c87e3f83SMatt Jacob 		mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
852c87e3f83SMatt Jacob 		    &ioc_status_frame);
85367a1357cSMarius Strobl 		if (mpt_req_on_pending_list(mpt, req) != 0)
85467a1357cSMarius Strobl 			TAILQ_REMOVE(chain, req, links);
855b0a2fdeeSScott Long 	}
856b0a2fdeeSScott Long }
857b0a2fdeeSScott Long 
858b0a2fdeeSScott Long /********************************* Diagnostics ********************************/
859b0a2fdeeSScott Long /*
860b0a2fdeeSScott Long  * Perform a diagnostic dump of a reply frame.
861b0a2fdeeSScott Long  */
862b0a2fdeeSScott Long void
863b0a2fdeeSScott Long mpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
864b0a2fdeeSScott Long {
865b0a2fdeeSScott Long 	mpt_prt(mpt, "Address Reply:\n");
866b0a2fdeeSScott Long 	mpt_print_reply(reply_frame);
867b0a2fdeeSScott Long }
868b0a2fdeeSScott Long 
869b0a2fdeeSScott Long /******************************* Doorbell Access ******************************/
870b0a2fdeeSScott Long static __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
871b0a2fdeeSScott Long static __inline  uint32_t mpt_rd_intr(struct mpt_softc *mpt);
872b0a2fdeeSScott Long 
873b0a2fdeeSScott Long static __inline uint32_t
874b0a2fdeeSScott Long mpt_rd_db(struct mpt_softc *mpt)
8759b631363SMatt Jacob {
8769b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_DOORBELL);
8779b631363SMatt Jacob }
8789b631363SMatt Jacob 
879b0a2fdeeSScott Long static __inline uint32_t
880b0a2fdeeSScott Long mpt_rd_intr(struct mpt_softc *mpt)
8819b631363SMatt Jacob {
8829b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
8839b631363SMatt Jacob }
8849b631363SMatt Jacob 
8859b631363SMatt Jacob /* Busy wait for a door bell to be read by IOC */
8869b631363SMatt Jacob static int
887b0a2fdeeSScott Long mpt_wait_db_ack(struct mpt_softc *mpt)
8889b631363SMatt Jacob {
8899b631363SMatt Jacob 	int i;
8909b631363SMatt Jacob 	for (i=0; i < MPT_MAX_WAIT; i++) {
8919b631363SMatt Jacob 		if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
8929b631363SMatt Jacob 			maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
893c87e3f83SMatt Jacob 			return (MPT_OK);
8949b631363SMatt Jacob 		}
895c87e3f83SMatt Jacob 		DELAY(200);
8969b631363SMatt Jacob 	}
897c87e3f83SMatt Jacob 	return (MPT_FAIL);
8989b631363SMatt Jacob }
8999b631363SMatt Jacob 
9009b631363SMatt Jacob /* Busy wait for a door bell interrupt */
9019b631363SMatt Jacob static int
902b0a2fdeeSScott Long mpt_wait_db_int(struct mpt_softc *mpt)
9039b631363SMatt Jacob {
9049b631363SMatt Jacob 	int i;
9059b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
9069b631363SMatt Jacob 		if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
9079b631363SMatt Jacob 			maxwait_int = i > maxwait_int ? i : maxwait_int;
9089b631363SMatt Jacob 			return MPT_OK;
9099b631363SMatt Jacob 		}
9109b631363SMatt Jacob 		DELAY(100);
9119b631363SMatt Jacob 	}
912c87e3f83SMatt Jacob 	return (MPT_FAIL);
9139b631363SMatt Jacob }
9149b631363SMatt Jacob 
9159b631363SMatt Jacob /* Wait for IOC to transition to a give state */
9169b631363SMatt Jacob void
917b0a2fdeeSScott Long mpt_check_doorbell(struct mpt_softc *mpt)
9189b631363SMatt Jacob {
919b0a2fdeeSScott Long 	uint32_t db = mpt_rd_db(mpt);
9209b631363SMatt Jacob 	if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
921b0a2fdeeSScott Long 		mpt_prt(mpt, "Device not running\n");
9229b631363SMatt Jacob 		mpt_print_db(db);
9239b631363SMatt Jacob 	}
9249b631363SMatt Jacob }
9259b631363SMatt Jacob 
9269b631363SMatt Jacob /* Wait for IOC to transition to a give state */
9279b631363SMatt Jacob static int
928b0a2fdeeSScott Long mpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
9299b631363SMatt Jacob {
9309b631363SMatt Jacob 	int i;
9319b631363SMatt Jacob 
9329b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
933b0a2fdeeSScott Long 		uint32_t db = mpt_rd_db(mpt);
9349b631363SMatt Jacob 		if (MPT_STATE(db) == state) {
9359b631363SMatt Jacob 			maxwait_state = i > maxwait_state ? i : maxwait_state;
9369b631363SMatt Jacob 			return (MPT_OK);
9379b631363SMatt Jacob 		}
9389b631363SMatt Jacob 		DELAY(100);
9399b631363SMatt Jacob 	}
9409b631363SMatt Jacob 	return (MPT_FAIL);
9419b631363SMatt Jacob }
9429b631363SMatt Jacob 
9439b631363SMatt Jacob 
944b0a2fdeeSScott Long /************************* Intialization/Configuration ************************/
945b0a2fdeeSScott Long static int mpt_download_fw(struct mpt_softc *mpt);
946b0a2fdeeSScott Long 
9479b631363SMatt Jacob /* Issue the reset COMMAND to the IOC */
948b0a2fdeeSScott Long static int
949b0a2fdeeSScott Long mpt_soft_reset(struct mpt_softc *mpt)
9509b631363SMatt Jacob {
951b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
9529b631363SMatt Jacob 
9539b631363SMatt Jacob 	/* Have to use hard reset if we are not in Running state */
9549b631363SMatt Jacob 	if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
955b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device not running\n");
956c87e3f83SMatt Jacob 		return (MPT_FAIL);
9579b631363SMatt Jacob 	}
9589b631363SMatt Jacob 
9599b631363SMatt Jacob 	/* If door bell is in use we don't have a chance of getting
9609b631363SMatt Jacob 	 * a word in since the IOC probably crashed in message
9619b631363SMatt Jacob 	 * processing. So don't waste our time.
9629b631363SMatt Jacob 	 */
9639b631363SMatt Jacob 	if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
964b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
965c87e3f83SMatt Jacob 		return (MPT_FAIL);
9669b631363SMatt Jacob 	}
9679b631363SMatt Jacob 
9689b631363SMatt Jacob 	/* Send the reset request to the IOC */
9699b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL,
9709b631363SMatt Jacob 	    MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
9719b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
972b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: ack timeout\n");
973c87e3f83SMatt Jacob 		return (MPT_FAIL);
9749b631363SMatt Jacob 	}
9759b631363SMatt Jacob 
9769b631363SMatt Jacob 	/* Wait for the IOC to reload and come out of reset state */
9779b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
978b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device did not restart\n");
979c87e3f83SMatt Jacob 		return (MPT_FAIL);
9809b631363SMatt Jacob 	}
9819b631363SMatt Jacob 
9829b631363SMatt Jacob 	return MPT_OK;
9839b631363SMatt Jacob }
9849b631363SMatt Jacob 
985b0a2fdeeSScott Long static int
986b0a2fdeeSScott Long mpt_enable_diag_mode(struct mpt_softc *mpt)
987b0a2fdeeSScott Long {
988b0a2fdeeSScott Long 	int try;
989b0a2fdeeSScott Long 
990b0a2fdeeSScott Long 	try = 20;
991b0a2fdeeSScott Long 	while (--try) {
992b0a2fdeeSScott Long 
993b0a2fdeeSScott Long 		if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
994b0a2fdeeSScott Long 			break;
995b0a2fdeeSScott Long 
996b0a2fdeeSScott Long 		/* Enable diagnostic registers */
997b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
998b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
999b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
1000b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
1001b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
1002b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
1003b0a2fdeeSScott Long 
1004b0a2fdeeSScott Long 		DELAY(100000);
1005b0a2fdeeSScott Long 	}
1006b0a2fdeeSScott Long 	if (try == 0)
1007b0a2fdeeSScott Long 		return (EIO);
1008b0a2fdeeSScott Long 	return (0);
1009b0a2fdeeSScott Long }
1010b0a2fdeeSScott Long 
1011b0a2fdeeSScott Long static void
1012b0a2fdeeSScott Long mpt_disable_diag_mode(struct mpt_softc *mpt)
1013b0a2fdeeSScott Long {
1014b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
1015b0a2fdeeSScott Long }
1016b0a2fdeeSScott Long 
10179b631363SMatt Jacob /* This is a magic diagnostic reset that resets all the ARM
10189b631363SMatt Jacob  * processors in the chip.
10199b631363SMatt Jacob  */
1020b0a2fdeeSScott Long static void
1021b0a2fdeeSScott Long mpt_hard_reset(struct mpt_softc *mpt)
10229b631363SMatt Jacob {
1023b0a2fdeeSScott Long 	int error;
1024b0a2fdeeSScott Long 	int wait;
1025b0a2fdeeSScott Long 	uint32_t diagreg;
10269b631363SMatt Jacob 
1027b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
1028b0a2fdeeSScott Long 
1029b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
1030b0a2fdeeSScott Long 	if (error) {
1031b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
1032b0a2fdeeSScott Long 		mpt_prt(mpt, "Trying to reset anyway.\n");
1033b0a2fdeeSScott Long 	}
1034b0a2fdeeSScott Long 
1035b0a2fdeeSScott Long 	diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1036b0a2fdeeSScott Long 
1037b0a2fdeeSScott Long 	/*
1038b0a2fdeeSScott Long 	 * This appears to be a workaround required for some
1039b0a2fdeeSScott Long 	 * firmware or hardware revs.
1040b0a2fdeeSScott Long 	 */
1041b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1042b0a2fdeeSScott Long 	DELAY(1000);
10439b631363SMatt Jacob 
10449b631363SMatt Jacob 	/* Diag. port is now active so we can now hit the reset bit */
1045b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
10469b631363SMatt Jacob 
1047b0a2fdeeSScott Long         /*
1048b0a2fdeeSScott Long          * Ensure that the reset has finished.  We delay 1ms
1049b0a2fdeeSScott Long          * prior to reading the register to make sure the chip
1050b0a2fdeeSScott Long          * has sufficiently completed its reset to handle register
1051b0a2fdeeSScott Long          * accesses.
1052b0a2fdeeSScott Long          */
1053b0a2fdeeSScott Long 	wait = 5000;
1054b0a2fdeeSScott Long 	do {
1055b0a2fdeeSScott Long 		DELAY(1000);
1056b0a2fdeeSScott Long 		diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1057b0a2fdeeSScott Long 	} while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
10589b631363SMatt Jacob 
1059b0a2fdeeSScott Long 	if (wait == 0) {
1060b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Failed hard reset! "
1061b0a2fdeeSScott Long 			"Trying to initialize anyway.\n");
10629b631363SMatt Jacob 	}
10639b631363SMatt Jacob 
1064b0a2fdeeSScott Long 	/*
1065b0a2fdeeSScott Long 	 * If we have firmware to download, it must be loaded before
1066b0a2fdeeSScott Long 	 * the controller will become operational.  Do so now.
1067b0a2fdeeSScott Long 	 */
1068b0a2fdeeSScott Long 	if (mpt->fw_image != NULL) {
1069b0a2fdeeSScott Long 
1070b0a2fdeeSScott Long 		error = mpt_download_fw(mpt);
1071b0a2fdeeSScott Long 
1072b0a2fdeeSScott Long 		if (error) {
1073b0a2fdeeSScott Long 			mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1074b0a2fdeeSScott Long 			mpt_prt(mpt, "Trying to initialize anyway.\n");
10759b631363SMatt Jacob 		}
1076b0a2fdeeSScott Long 	}
1077b0a2fdeeSScott Long 
1078b0a2fdeeSScott Long 	/*
1079b0a2fdeeSScott Long 	 * Reseting the controller should have disabled write
1080b0a2fdeeSScott Long 	 * access to the diagnostic registers, but disable
1081b0a2fdeeSScott Long 	 * manually to be sure.
1082b0a2fdeeSScott Long 	 */
1083b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
1084b0a2fdeeSScott Long }
1085b0a2fdeeSScott Long 
1086b0a2fdeeSScott Long static void
1087b0a2fdeeSScott Long mpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1088b0a2fdeeSScott Long {
1089b0a2fdeeSScott Long 	/*
1090b0a2fdeeSScott Long 	 * Complete all pending requests with a status
1091b0a2fdeeSScott Long 	 * appropriate for an IOC reset.
1092b0a2fdeeSScott Long 	 */
1093b0a2fdeeSScott Long 	mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1094b0a2fdeeSScott Long 				   MPI_IOCSTATUS_INVALID_STATE);
1095b0a2fdeeSScott Long }
1096b0a2fdeeSScott Long 
10979b631363SMatt Jacob 
10989b631363SMatt Jacob /*
10999b631363SMatt Jacob  * Reset the IOC when needed. Try software command first then if needed
11009b631363SMatt Jacob  * poke at the magic diagnostic reset. Note that a hard reset resets
11019b631363SMatt Jacob  * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
11029b631363SMatt Jacob  * fouls up the PCI configuration registers.
11039b631363SMatt Jacob  */
11049b631363SMatt Jacob int
1105b0a2fdeeSScott Long mpt_reset(struct mpt_softc *mpt, int reinit)
11069b631363SMatt Jacob {
1107b0a2fdeeSScott Long 	struct	mpt_personality *pers;
11089b631363SMatt Jacob 	int	ret;
110929ae59edSMatt Jacob 	int	retry_cnt = 0;
11109b631363SMatt Jacob 
111129ae59edSMatt Jacob 	/*
111229ae59edSMatt Jacob 	 * Try a soft reset. If that fails, get out the big hammer.
111329ae59edSMatt Jacob 	 */
111429ae59edSMatt Jacob  again:
11159b631363SMatt Jacob 	if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
111629ae59edSMatt Jacob 		int	cnt;
111729ae59edSMatt Jacob 		for (cnt = 0; cnt < 5; cnt++) {
11189b631363SMatt Jacob 			/* Failed; do a hard reset */
11199b631363SMatt Jacob 			mpt_hard_reset(mpt);
11209b631363SMatt Jacob 
112129ae59edSMatt Jacob 			/*
112229ae59edSMatt Jacob 			 * Wait for the IOC to reload
112329ae59edSMatt Jacob 			 * and come out of reset state
112429ae59edSMatt Jacob 			 */
11259b631363SMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
112629ae59edSMatt Jacob 			if (ret == MPT_OK) {
112729ae59edSMatt Jacob 				break;
112829ae59edSMatt Jacob 			}
112929ae59edSMatt Jacob 			/*
113029ae59edSMatt Jacob 			 * Okay- try to check again...
113129ae59edSMatt Jacob 			 */
113229ae59edSMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
113329ae59edSMatt Jacob 			if (ret == MPT_OK) {
113429ae59edSMatt Jacob 				break;
113529ae59edSMatt Jacob 			}
113629ae59edSMatt Jacob 			mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
113729ae59edSMatt Jacob 			    retry_cnt, cnt);
113829ae59edSMatt Jacob 		}
11399b631363SMatt Jacob 	}
1140b0a2fdeeSScott Long 
114129ae59edSMatt Jacob 	if (retry_cnt == 0) {
1142b0a2fdeeSScott Long 		/*
1143b0a2fdeeSScott Long 		 * Invoke reset handlers.  We bump the reset count so
1144b0a2fdeeSScott Long 		 * that mpt_wait_req() understands that regardless of
1145b0a2fdeeSScott Long 		 * the specified wait condition, it should stop its wait.
1146b0a2fdeeSScott Long 		 */
1147b0a2fdeeSScott Long 		mpt->reset_cnt++;
1148b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
1149b0a2fdeeSScott Long 			pers->reset(mpt, ret);
115029ae59edSMatt Jacob 	}
1151b0a2fdeeSScott Long 
11525e073106SMatt Jacob 	if (reinit) {
1153c87e3f83SMatt Jacob 		ret = mpt_enable_ioc(mpt, 1);
115429ae59edSMatt Jacob 		if (ret == MPT_OK) {
115529ae59edSMatt Jacob 			mpt_enable_ints(mpt);
115629ae59edSMatt Jacob 		}
115729ae59edSMatt Jacob 	}
115829ae59edSMatt Jacob 	if (ret != MPT_OK && retry_cnt++ < 2) {
115929ae59edSMatt Jacob 		goto again;
116029ae59edSMatt Jacob 	}
11619b631363SMatt Jacob 	return ret;
11629b631363SMatt Jacob }
11639b631363SMatt Jacob 
11649b631363SMatt Jacob /* Return a command buffer to the free queue */
11659b631363SMatt Jacob void
1166b0a2fdeeSScott Long mpt_free_request(struct mpt_softc *mpt, request_t *req)
11679b631363SMatt Jacob {
1168444dd2b6SMatt Jacob 	request_t *nxt;
1169b0a2fdeeSScott Long 	struct mpt_evtf_record *record;
1170b0a2fdeeSScott Long 	uint32_t reply_baddr;
1171b0a2fdeeSScott Long 
11727dec90bcSMatt Jacob 	if (req == NULL || req != &mpt->request_pool[req->index]) {
11739b631363SMatt Jacob 		panic("mpt_free_request bad req ptr\n");
11749b631363SMatt Jacob 		return;
11759b631363SMatt Jacob 	}
1176444dd2b6SMatt Jacob 	if ((nxt = req->chain) != NULL) {
1177444dd2b6SMatt Jacob 		req->chain = NULL;
1178444dd2b6SMatt Jacob 		mpt_free_request(mpt, nxt);	/* NB: recursion */
1179444dd2b6SMatt Jacob 	}
11805e073106SMatt Jacob 	KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
11815e073106SMatt Jacob 	KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
1182d0a68c27SMatt Jacob 	MPT_LOCK_ASSERT(mpt);
11835089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
11845089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x already on freelist",
11855089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
11865089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
11875089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x on pending list",
11885089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
11895089bd63SMatt Jacob #ifdef	INVARIANTS
11905089bd63SMatt Jacob 	mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
11915089bd63SMatt Jacob #endif
11925e073106SMatt Jacob 
11939b631363SMatt Jacob 	req->ccb = NULL;
1194b0a2fdeeSScott Long 	if (LIST_EMPTY(&mpt->ack_frames)) {
1195c87e3f83SMatt Jacob 		/*
1196c87e3f83SMatt Jacob 		 * Insert free ones at the tail
1197c87e3f83SMatt Jacob 		 */
11985e073106SMatt Jacob 		req->serno = 0;
11995e073106SMatt Jacob 		req->state = REQ_STATE_FREE;
12005089bd63SMatt Jacob #ifdef	INVARIANTS
12015089bd63SMatt Jacob 		memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
12025089bd63SMatt Jacob #endif
1203c87e3f83SMatt Jacob 		TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1204b0a2fdeeSScott Long 		if (mpt->getreqwaiter != 0) {
1205b0a2fdeeSScott Long 			mpt->getreqwaiter = 0;
1206b0a2fdeeSScott Long 			wakeup(&mpt->request_free_list);
1207b0a2fdeeSScott Long 		}
1208b0a2fdeeSScott Long 		return;
1209b0a2fdeeSScott Long 	}
1210b0a2fdeeSScott Long 
1211b0a2fdeeSScott Long 	/*
1212b0a2fdeeSScott Long 	 * Process an ack frame deferred due to resource shortage.
1213b0a2fdeeSScott Long 	 */
1214b0a2fdeeSScott Long 	record = LIST_FIRST(&mpt->ack_frames);
1215b0a2fdeeSScott Long 	LIST_REMOVE(record, links);
12165e073106SMatt Jacob 	req->state = REQ_STATE_ALLOCATED;
12175089bd63SMatt Jacob 	mpt_assign_serno(mpt, req);
1218b0a2fdeeSScott Long 	mpt_send_event_ack(mpt, req, &record->reply, record->context);
1219b0a2fdeeSScott Long 	reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply)
1220b0a2fdeeSScott Long 		    + (mpt->reply_phys & 0xFFFFFFFF);
1221b0a2fdeeSScott Long 	mpt_free_reply(mpt, reply_baddr);
12229b631363SMatt Jacob }
12239b631363SMatt Jacob 
12249b631363SMatt Jacob /* Get a command buffer from the free queue */
12259b631363SMatt Jacob request_t *
1226b0a2fdeeSScott Long mpt_get_request(struct mpt_softc *mpt, int sleep_ok)
12279b631363SMatt Jacob {
12289b631363SMatt Jacob 	request_t *req;
1229b0a2fdeeSScott Long 
1230b0a2fdeeSScott Long retry:
1231d0a68c27SMatt Jacob 	MPT_LOCK_ASSERT(mpt);
1232b0a2fdeeSScott Long 	req = TAILQ_FIRST(&mpt->request_free_list);
12339b631363SMatt Jacob 	if (req != NULL) {
1234b0a2fdeeSScott Long 		KASSERT(req == &mpt->request_pool[req->index],
1235b0a2fdeeSScott Long 		    ("mpt_get_request: corrupted request free list\n"));
12365e073106SMatt Jacob 		KASSERT(req->state == REQ_STATE_FREE,
12375089bd63SMatt Jacob 		    ("req %p:%u not free on free list %x index %d function %x",
12385089bd63SMatt Jacob 		    req, req->serno, req->state, req->index,
12395089bd63SMatt Jacob 		    ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1240b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_free_list, req, links);
1241b0a2fdeeSScott Long 		req->state = REQ_STATE_ALLOCATED;
1242444dd2b6SMatt Jacob 		req->chain = NULL;
12435089bd63SMatt Jacob 		mpt_assign_serno(mpt, req);
1244b0a2fdeeSScott Long 	} else if (sleep_ok != 0) {
1245b0a2fdeeSScott Long 		mpt->getreqwaiter = 1;
1246b0a2fdeeSScott Long 		mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1247b0a2fdeeSScott Long 		goto retry;
12489b631363SMatt Jacob 	}
12495e073106SMatt Jacob 	return (req);
12509b631363SMatt Jacob }
12519b631363SMatt Jacob 
12529b631363SMatt Jacob /* Pass the command to the IOC */
12539b631363SMatt Jacob void
1254b0a2fdeeSScott Long mpt_send_cmd(struct mpt_softc *mpt, request_t *req)
12559b631363SMatt Jacob {
12561d79ca0eSMatt Jacob 	if (mpt->verbose > MPT_PRT_DEBUG2) {
12571043516dSMatt Jacob 		mpt_dump_request(mpt, req);
1258444dd2b6SMatt Jacob 	}
12599b631363SMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
12609b631363SMatt Jacob 	    BUS_DMASYNC_PREWRITE);
1261b0a2fdeeSScott Long 	req->state |= REQ_STATE_QUEUED;
12625089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
12635089bd63SMatt Jacob 	    ("req %p:%u func %x on freelist list in mpt_send_cmd",
12645089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
12655089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
12665089bd63SMatt Jacob 	    ("req %p:%u func %x already on pending list in mpt_send_cmd",
12675089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1268b0a2fdeeSScott Long 	TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1269b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
12709b631363SMatt Jacob }
12719b631363SMatt Jacob 
12729b631363SMatt Jacob /*
1273b0a2fdeeSScott Long  * Wait for a request to complete.
1274b0a2fdeeSScott Long  *
1275b0a2fdeeSScott Long  * Inputs:
1276b0a2fdeeSScott Long  *	mpt		softc of controller executing request
1277b0a2fdeeSScott Long  *	req		request to wait for
1278b0a2fdeeSScott Long  *	sleep_ok	nonzero implies may sleep in this context
1279b0a2fdeeSScott Long  *	time_ms		timeout in ms.  0 implies no timeout.
1280b0a2fdeeSScott Long  *
1281b0a2fdeeSScott Long  * Return Values:
1282b0a2fdeeSScott Long  *	0		Request completed
1283b0a2fdeeSScott Long  *	non-0		Timeout fired before request completion.
12849b631363SMatt Jacob  */
1285b0a2fdeeSScott Long int
1286b0a2fdeeSScott Long mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1287b0a2fdeeSScott Long 	     mpt_req_state_t state, mpt_req_state_t mask,
1288b0a2fdeeSScott Long 	     int sleep_ok, int time_ms)
12899b631363SMatt Jacob {
1290b0a2fdeeSScott Long 	int   error;
1291b0a2fdeeSScott Long 	int   timeout;
1292b0a2fdeeSScott Long 	u_int saved_cnt;
12939b631363SMatt Jacob 
1294b0a2fdeeSScott Long 	/*
1295b0a2fdeeSScott Long 	 * timeout is in ms.  0 indicates infinite wait.
1296b0a2fdeeSScott Long 	 * Convert to ticks or 500us units depending on
1297b0a2fdeeSScott Long 	 * our sleep mode.
1298b0a2fdeeSScott Long 	 */
1299c87e3f83SMatt Jacob 	if (sleep_ok != 0) {
1300b0a2fdeeSScott Long 		timeout = (time_ms * hz) / 1000;
1301c87e3f83SMatt Jacob 	} else {
1302b0a2fdeeSScott Long 		timeout = time_ms * 2;
1303c87e3f83SMatt Jacob 	}
1304b0a2fdeeSScott Long 	req->state |= REQ_STATE_NEED_WAKEUP;
1305b0a2fdeeSScott Long 	mask &= ~REQ_STATE_NEED_WAKEUP;
1306444dd2b6SMatt Jacob 	saved_cnt = mpt->reset_cnt;
1307c87e3f83SMatt Jacob 	while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1308b0a2fdeeSScott Long 		if (sleep_ok != 0) {
1309b0a2fdeeSScott Long 			error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout);
1310b0a2fdeeSScott Long 			if (error == EWOULDBLOCK) {
1311b0a2fdeeSScott Long 				timeout = 0;
1312b0a2fdeeSScott Long 				break;
1313b0a2fdeeSScott Long 			}
1314b0a2fdeeSScott Long 		} else {
1315b0a2fdeeSScott Long 			if (time_ms != 0 && --timeout == 0) {
1316b0a2fdeeSScott Long 				break;
1317b0a2fdeeSScott Long 			}
1318b0a2fdeeSScott Long 			DELAY(500);
1319b0a2fdeeSScott Long 			mpt_intr(mpt);
1320b0a2fdeeSScott Long 		}
1321b0a2fdeeSScott Long 	}
1322b0a2fdeeSScott Long 	req->state &= ~REQ_STATE_NEED_WAKEUP;
1323c87e3f83SMatt Jacob 	if (mpt->reset_cnt != saved_cnt) {
1324b0a2fdeeSScott Long 		return (EIO);
1325c87e3f83SMatt Jacob 	}
1326c87e3f83SMatt Jacob 	if (time_ms && timeout <= 0) {
1327c87e3f83SMatt Jacob 		MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
13286621d786SMatt Jacob 		req->state |= REQ_STATE_TIMEDOUT;
1329c87e3f83SMatt Jacob 		mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1330b0a2fdeeSScott Long 		return (ETIMEDOUT);
1331c87e3f83SMatt Jacob 	}
1332b0a2fdeeSScott Long 	return (0);
13339b631363SMatt Jacob }
13349b631363SMatt Jacob 
13359b631363SMatt Jacob /*
13369b631363SMatt Jacob  * Send a command to the IOC via the handshake register.
13379b631363SMatt Jacob  *
13389b631363SMatt Jacob  * Only done at initialization time and for certain unusual
13399b631363SMatt Jacob  * commands such as device/bus reset as specified by LSI.
13409b631363SMatt Jacob  */
13419b631363SMatt Jacob int
1342b0a2fdeeSScott Long mpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
13439b631363SMatt Jacob {
13449b631363SMatt Jacob 	int i;
1345b0a2fdeeSScott Long 	uint32_t data, *data32;
13469b631363SMatt Jacob 
13479b631363SMatt Jacob 	/* Check condition of the IOC */
13489b631363SMatt Jacob 	data = mpt_rd_db(mpt);
1349b0a2fdeeSScott Long 	if ((MPT_STATE(data) != MPT_DB_STATE_READY
1350b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1351b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1352b0a2fdeeSScott Long 	 || MPT_DB_IS_IN_USE(data)) {
1353b0a2fdeeSScott Long 		mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
13549b631363SMatt Jacob 		mpt_print_db(data);
13559b631363SMatt Jacob 		return (EBUSY);
13569b631363SMatt Jacob 	}
13579b631363SMatt Jacob 
13589b631363SMatt Jacob 	/* We move things in 32 bit chunks */
13599b631363SMatt Jacob 	len = (len + 3) >> 2;
13609b631363SMatt Jacob 	data32 = cmd;
13619b631363SMatt Jacob 
1362976b0106SKevin Lo 	/* Clear any left over pending doorbell interrupts */
13639b631363SMatt Jacob 	if (MPT_DB_INTR(mpt_rd_intr(mpt)))
13649b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13659b631363SMatt Jacob 
13669b631363SMatt Jacob 	/*
13679b631363SMatt Jacob 	 * Tell the handshake reg. we are going to send a command
13689b631363SMatt Jacob          * and how long it is going to be.
13699b631363SMatt Jacob 	 */
13709b631363SMatt Jacob 	data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
13719b631363SMatt Jacob 	    (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
13729b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
13739b631363SMatt Jacob 
13749b631363SMatt Jacob 	/* Wait for the chip to notice */
13759b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1376a54067ccSMatt Jacob 		mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n");
1377b0a2fdeeSScott Long 		return (ETIMEDOUT);
13789b631363SMatt Jacob 	}
13799b631363SMatt Jacob 
13809b631363SMatt Jacob 	/* Clear the interrupt */
13819b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13829b631363SMatt Jacob 
13839b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
1384a54067ccSMatt Jacob 		mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n");
1385b0a2fdeeSScott Long 		return (ETIMEDOUT);
13869b631363SMatt Jacob 	}
13879b631363SMatt Jacob 
13889b631363SMatt Jacob 	/* Send the command */
13899b631363SMatt Jacob 	for (i = 0; i < len; i++) {
13903f970273SJohn Birrell 		mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++));
13919b631363SMatt Jacob 		if (mpt_wait_db_ack(mpt) != MPT_OK) {
1392301472c2SMatt Jacob 			mpt_prt(mpt,
1393a54067ccSMatt Jacob 			    "mpt_send_handshake_cmd: timeout @ index %d\n", i);
1394b0a2fdeeSScott Long 			return (ETIMEDOUT);
13959b631363SMatt Jacob 		}
13969b631363SMatt Jacob 	}
13979b631363SMatt Jacob 	return MPT_OK;
13989b631363SMatt Jacob }
13999b631363SMatt Jacob 
14009b631363SMatt Jacob /* Get the response from the handshake register */
14019b631363SMatt Jacob int
1402b0a2fdeeSScott Long mpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
14039b631363SMatt Jacob {
14049b631363SMatt Jacob 	int left, reply_left;
14059b631363SMatt Jacob 	u_int16_t *data16;
14063f970273SJohn Birrell 	uint32_t data;
14079b631363SMatt Jacob 	MSG_DEFAULT_REPLY *hdr;
14089b631363SMatt Jacob 
14099b631363SMatt Jacob 	/* We move things out in 16 bit chunks */
14109b631363SMatt Jacob 	reply_len >>= 1;
14119b631363SMatt Jacob 	data16 = (u_int16_t *)reply;
14129b631363SMatt Jacob 
14139b631363SMatt Jacob 	hdr = (MSG_DEFAULT_REPLY *)reply;
14149b631363SMatt Jacob 
14159b631363SMatt Jacob 	/* Get first word */
14169b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1417b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
14189b631363SMatt Jacob 		return ETIMEDOUT;
14199b631363SMatt Jacob 	}
14203f970273SJohn Birrell 	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14213f970273SJohn Birrell 	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14229b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14239b631363SMatt Jacob 
14249b631363SMatt Jacob 	/* Get Second Word */
14259b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1426b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
14279b631363SMatt Jacob 		return ETIMEDOUT;
14289b631363SMatt Jacob 	}
14293f970273SJohn Birrell 	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14303f970273SJohn Birrell 	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
14319b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14329b631363SMatt Jacob 
14335e073106SMatt Jacob 	/*
14345e073106SMatt Jacob 	 * With the second word, we can now look at the length.
14355e073106SMatt Jacob 	 * Warn about a reply that's too short (except for IOC FACTS REPLY)
14365e073106SMatt Jacob 	 */
14375e073106SMatt Jacob 	if ((reply_len >> 1) != hdr->MsgLength &&
14385e073106SMatt Jacob 	    (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
14395e073106SMatt Jacob #if __FreeBSD_version >= 500000
1440301472c2SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
144133f31846SMatt Jacob 			"got %x; expected %zx for function %x\n",
14425e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14435e073106SMatt Jacob #else
14445e073106SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
144533f31846SMatt Jacob 			"got %x; expected %x for function %x\n",
14465e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14475e073106SMatt Jacob #endif
14489b631363SMatt Jacob 	}
14499b631363SMatt Jacob 
14509b631363SMatt Jacob 	/* Get rest of the reply; but don't overflow the provided buffer */
14519b631363SMatt Jacob 	left = (hdr->MsgLength << 1) - 2;
14529b631363SMatt Jacob 	reply_left =  reply_len - 2;
14539b631363SMatt Jacob 	while (left--) {
14549b631363SMatt Jacob 		u_int16_t datum;
14559b631363SMatt Jacob 
14569b631363SMatt Jacob 		if (mpt_wait_db_int(mpt) != MPT_OK) {
1457b0a2fdeeSScott Long 			mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
14589b631363SMatt Jacob 			return ETIMEDOUT;
14599b631363SMatt Jacob 		}
14603f970273SJohn Birrell 		data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14613f970273SJohn Birrell 		datum = le16toh(data & MPT_DB_DATA_MASK);
14629b631363SMatt Jacob 
14639b631363SMatt Jacob 		if (reply_left-- > 0)
14643f970273SJohn Birrell 			*data16++ = datum;
14659b631363SMatt Jacob 
14669b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14679b631363SMatt Jacob 	}
14689b631363SMatt Jacob 
14699b631363SMatt Jacob 	/* One more wait & clear at the end */
14709b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1471b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
14729b631363SMatt Jacob 		return ETIMEDOUT;
14739b631363SMatt Jacob 	}
14749b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14759b631363SMatt Jacob 
14769b631363SMatt Jacob 	if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1477b0a2fdeeSScott Long 		if (mpt->verbose >= MPT_PRT_TRACE)
14789b631363SMatt Jacob 			mpt_print_reply(hdr);
14799b631363SMatt Jacob 		return (MPT_FAIL | hdr->IOCStatus);
14809b631363SMatt Jacob 	}
14819b631363SMatt Jacob 
14829b631363SMatt Jacob 	return (0);
14839b631363SMatt Jacob }
14849b631363SMatt Jacob 
14859b631363SMatt Jacob static int
1486b0a2fdeeSScott Long mpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
14879b631363SMatt Jacob {
14889b631363SMatt Jacob 	MSG_IOC_FACTS f_req;
14899b631363SMatt Jacob 	int error;
14909b631363SMatt Jacob 
14915e073106SMatt Jacob 	memset(&f_req, 0, sizeof f_req);
14929b631363SMatt Jacob 	f_req.Function = MPI_FUNCTION_IOC_FACTS;
1493b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
14949b631363SMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
14950e0521a1SMatt Jacob 	if (error) {
14969b631363SMatt Jacob 		return(error);
14970e0521a1SMatt Jacob 	}
14989b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
14999b631363SMatt Jacob 	return (error);
15009b631363SMatt Jacob }
15019b631363SMatt Jacob 
15027104aeefSMatt Jacob static int
15030e0521a1SMatt Jacob mpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp)
15047104aeefSMatt Jacob {
15057104aeefSMatt Jacob 	MSG_PORT_FACTS f_req;
15067104aeefSMatt Jacob 	int error;
15077104aeefSMatt Jacob 
1508b0a2fdeeSScott Long 	memset(&f_req, 0, sizeof f_req);
15097104aeefSMatt Jacob 	f_req.Function = MPI_FUNCTION_PORT_FACTS;
15100e0521a1SMatt Jacob 	f_req.PortNumber = port;
1511b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15127104aeefSMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
15130e0521a1SMatt Jacob 	if (error) {
15147104aeefSMatt Jacob 		return(error);
15150e0521a1SMatt Jacob 	}
15167104aeefSMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
15177104aeefSMatt Jacob 	return (error);
15187104aeefSMatt Jacob }
15197104aeefSMatt Jacob 
15209b631363SMatt Jacob /*
15219b631363SMatt Jacob  * Send the initialization request. This is where we specify how many
15229b631363SMatt Jacob  * SCSI busses and how many devices per bus we wish to emulate.
15239b631363SMatt Jacob  * This is also the command that specifies the max size of the reply
15249b631363SMatt Jacob  * frames from the IOC that we will be allocating.
15259b631363SMatt Jacob  */
15269b631363SMatt Jacob static int
1527b0a2fdeeSScott Long mpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
15289b631363SMatt Jacob {
15299b631363SMatt Jacob 	int error = 0;
15309b631363SMatt Jacob 	MSG_IOC_INIT init;
15319b631363SMatt Jacob 	MSG_IOC_INIT_REPLY reply;
15329b631363SMatt Jacob 
15335e073106SMatt Jacob 	memset(&init, 0, sizeof init);
15349b631363SMatt Jacob 	init.WhoInit = who;
15359b631363SMatt Jacob 	init.Function = MPI_FUNCTION_IOC_INIT;
15360e0521a1SMatt Jacob 	init.MaxDevices = 0;	/* at least 256 devices per bus */
15370e0521a1SMatt Jacob 	init.MaxBuses = 16;	/* at least 16 busses */
1538444dd2b6SMatt Jacob 
1539444dd2b6SMatt Jacob 	init.MsgVersion = htole16(MPI_VERSION);
1540444dd2b6SMatt Jacob 	init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1541444dd2b6SMatt Jacob 	init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1542b0a2fdeeSScott Long 	init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15439b631363SMatt Jacob 
15449b631363SMatt Jacob 	if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
15459b631363SMatt Jacob 		return(error);
15469b631363SMatt Jacob 	}
15479b631363SMatt Jacob 
15489b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
15499b631363SMatt Jacob 	return (error);
15509b631363SMatt Jacob }
15519b631363SMatt Jacob 
15527104aeefSMatt Jacob 
15537104aeefSMatt Jacob /*
15547104aeefSMatt Jacob  * Utiltity routine to read configuration headers and pages
15557104aeefSMatt Jacob  */
1556b0a2fdeeSScott Long int
15571d558d6aSScott Long mpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, cfgparms_t *params,
15581d558d6aSScott Long 		  bus_addr_t addr, bus_size_t len, int sleep_ok, int timeout_ms)
15597104aeefSMatt Jacob {
15607104aeefSMatt Jacob 	MSG_CONFIG *cfgp;
1561b0a2fdeeSScott Long 	SGE_SIMPLE32 *se;
15627104aeefSMatt Jacob 
15637104aeefSMatt Jacob 	cfgp = req->req_vbuf;
1564b0a2fdeeSScott Long 	memset(cfgp, 0, sizeof *cfgp);
15651d558d6aSScott Long 	cfgp->Action = params->Action;
15667104aeefSMatt Jacob 	cfgp->Function = MPI_FUNCTION_CONFIG;
15671d558d6aSScott Long 	cfgp->Header.PageVersion = params->PageVersion;
15681d558d6aSScott Long 	cfgp->Header.PageNumber = params->PageNumber;
15691d558d6aSScott Long 	cfgp->PageAddress = htole32(params->PageAddress);
15701d558d6aSScott Long 	if ((params->PageType & MPI_CONFIG_PAGETYPE_MASK) ==
15711d558d6aSScott Long 	    MPI_CONFIG_PAGETYPE_EXTENDED) {
15721d558d6aSScott Long 		cfgp->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
15731d558d6aSScott Long 		cfgp->Header.PageLength = 0;
15741d558d6aSScott Long 		cfgp->ExtPageLength = htole16(params->ExtPageLength);
15751d558d6aSScott Long 		cfgp->ExtPageType = params->ExtPageType;
15761d558d6aSScott Long 	} else {
15771d558d6aSScott Long 		cfgp->Header.PageType = params->PageType;
15781d558d6aSScott Long 		cfgp->Header.PageLength = params->PageLength;
15791d558d6aSScott Long 	}
1580b0a2fdeeSScott Long 	se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
15813f970273SJohn Birrell 	se->Address = htole32(addr);
1582b0a2fdeeSScott Long 	MPI_pSGE_SET_LENGTH(se, len);
1583b0a2fdeeSScott Long 	MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1584b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1585b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_END_OF_LIST |
15861d558d6aSScott Long 	    ((params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
15871d558d6aSScott Long 	  || params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1588b0a2fdeeSScott Long 	   ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
15893f970273SJohn Birrell 	se->FlagsLength = htole32(se->FlagsLength);
1590b0a2fdeeSScott Long 	cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
15917104aeefSMatt Jacob 
15927104aeefSMatt Jacob 	mpt_check_doorbell(mpt);
15937104aeefSMatt Jacob 	mpt_send_cmd(mpt, req);
1594b0a2fdeeSScott Long 	return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1595b0a2fdeeSScott Long 			     sleep_ok, timeout_ms));
15967104aeefSMatt Jacob }
15977104aeefSMatt Jacob 
15981d558d6aSScott Long int
15991d558d6aSScott Long mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion, int PageNumber,
16001d558d6aSScott Long 		       uint32_t PageAddress, int ExtPageType,
16011d558d6aSScott Long 		       CONFIG_EXTENDED_PAGE_HEADER *rslt,
16021d558d6aSScott Long 		       int sleep_ok, int timeout_ms)
16031d558d6aSScott Long {
16041d558d6aSScott Long 	request_t  *req;
16051d558d6aSScott Long 	cfgparms_t params;
16061d558d6aSScott Long 	MSG_CONFIG_REPLY *cfgp;
16071d558d6aSScott Long 	int	    error;
16081d558d6aSScott Long 
16091d558d6aSScott Long 	req = mpt_get_request(mpt, sleep_ok);
16101d558d6aSScott Long 	if (req == NULL) {
16111d558d6aSScott Long 		mpt_prt(mpt, "mpt_extread_cfg_header: Get request failed!\n");
16121d558d6aSScott Long 		return (ENOMEM);
16131d558d6aSScott Long 	}
16141d558d6aSScott Long 
16151d558d6aSScott Long 	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
16161d558d6aSScott Long 	params.PageVersion = PageVersion;
16171d558d6aSScott Long 	params.PageLength = 0;
16181d558d6aSScott Long 	params.PageNumber = PageNumber;
16191d558d6aSScott Long 	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
16201d558d6aSScott Long 	params.PageAddress = PageAddress;
16211d558d6aSScott Long 	params.ExtPageType = ExtPageType;
16221d558d6aSScott Long 	params.ExtPageLength = 0;
16231d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
16241d558d6aSScott Long 				  sleep_ok, timeout_ms);
16251d558d6aSScott Long 	if (error != 0) {
16261d558d6aSScott Long 		/*
16271d558d6aSScott Long 		 * Leave the request. Without resetting the chip, it's
16281d558d6aSScott Long 		 * still owned by it and we'll just get into trouble
16291d558d6aSScott Long 		 * freeing it now. Mark it as abandoned so that if it
16301d558d6aSScott Long 		 * shows up later it can be freed.
16311d558d6aSScott Long 		 */
16321d558d6aSScott Long 		mpt_prt(mpt, "read_extcfg_header timed out\n");
16331d558d6aSScott Long 		return (ETIMEDOUT);
16341d558d6aSScott Long 	}
16351d558d6aSScott Long 
16361d558d6aSScott Long         switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
16371d558d6aSScott Long 	case MPI_IOCSTATUS_SUCCESS:
16381d558d6aSScott Long 		cfgp = req->req_vbuf;
16391d558d6aSScott Long 		rslt->PageVersion = cfgp->Header.PageVersion;
16401d558d6aSScott Long 		rslt->PageNumber = cfgp->Header.PageNumber;
16411d558d6aSScott Long 		rslt->PageType = cfgp->Header.PageType;
16427ee37807SMarius Strobl 		rslt->ExtPageLength = le16toh(cfgp->ExtPageLength);
16431d558d6aSScott Long 		rslt->ExtPageType = cfgp->ExtPageType;
16441d558d6aSScott Long 		error = 0;
16451d558d6aSScott Long 		break;
16461d558d6aSScott Long 	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
16471d558d6aSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG,
16481d558d6aSScott Long 		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
16491d558d6aSScott Long 		    MPI_CONFIG_PAGETYPE_EXTENDED, PageNumber, PageAddress);
16501d558d6aSScott Long 		error = EINVAL;
16511d558d6aSScott Long 		break;
16521d558d6aSScott Long 	default:
16531d558d6aSScott Long 		mpt_prt(mpt, "mpt_read_extcfg_header: Config Info Status %x\n",
16541d558d6aSScott Long 			req->IOCStatus);
16551d558d6aSScott Long 		error = EIO;
16561d558d6aSScott Long 		break;
16571d558d6aSScott Long 	}
16581d558d6aSScott Long 	mpt_free_request(mpt, req);
16591d558d6aSScott Long 	return (error);
16601d558d6aSScott Long }
16611d558d6aSScott Long 
16621d558d6aSScott Long int
16631d558d6aSScott Long mpt_read_extcfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
16641d558d6aSScott Long 		     CONFIG_EXTENDED_PAGE_HEADER *hdr, void *buf, size_t len,
16651d558d6aSScott Long 		     int sleep_ok, int timeout_ms)
16661d558d6aSScott Long {
16671d558d6aSScott Long 	request_t    *req;
16681d558d6aSScott Long 	cfgparms_t    params;
16691d558d6aSScott Long 	int	      error;
16701d558d6aSScott Long 
16711d558d6aSScott Long 	req = mpt_get_request(mpt, sleep_ok);
16721d558d6aSScott Long 	if (req == NULL) {
16737ee37807SMarius Strobl 		mpt_prt(mpt, "mpt_read_extcfg_page: Get request failed!\n");
16741d558d6aSScott Long 		return (-1);
16751d558d6aSScott Long 	}
16761d558d6aSScott Long 
16771d558d6aSScott Long 	params.Action = Action;
16781d558d6aSScott Long 	params.PageVersion = hdr->PageVersion;
16791d558d6aSScott Long 	params.PageLength = 0;
16801d558d6aSScott Long 	params.PageNumber = hdr->PageNumber;
16811d558d6aSScott Long 	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
16821d558d6aSScott Long 	params.PageAddress = PageAddress;
16831d558d6aSScott Long 	params.ExtPageType = hdr->ExtPageType;
16841d558d6aSScott Long 	params.ExtPageLength = hdr->ExtPageLength;
16851d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params,
16861d558d6aSScott Long 				  req->req_pbuf + MPT_RQSL(mpt),
16871d558d6aSScott Long 				  len, sleep_ok, timeout_ms);
16881d558d6aSScott Long 	if (error != 0) {
16891d558d6aSScott Long 		mpt_prt(mpt, "read_extcfg_page(%d) timed out\n", Action);
16901d558d6aSScott Long 		return (-1);
16911d558d6aSScott Long 	}
16921d558d6aSScott Long 
16931d558d6aSScott Long 	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
16941d558d6aSScott Long 		mpt_prt(mpt, "mpt_read_extcfg_page: Config Info Status %x\n",
16951d558d6aSScott Long 			req->IOCStatus);
16961d558d6aSScott Long 		mpt_free_request(mpt, req);
16971d558d6aSScott Long 		return (-1);
16981d558d6aSScott Long 	}
16991d558d6aSScott Long 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
17001d558d6aSScott Long 	    BUS_DMASYNC_POSTREAD);
17011d558d6aSScott Long 	memcpy(buf, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
17021d558d6aSScott Long 	mpt_free_request(mpt, req);
17031d558d6aSScott Long 	return (0);
17041d558d6aSScott Long }
1705b0a2fdeeSScott Long 
1706b0a2fdeeSScott Long int
1707b0a2fdeeSScott Long mpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1708b0a2fdeeSScott Long 		    uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1709b0a2fdeeSScott Long 		    int sleep_ok, int timeout_ms)
1710b0a2fdeeSScott Long {
1711b0a2fdeeSScott Long 	request_t  *req;
17121d558d6aSScott Long 	cfgparms_t params;
171329ae59edSMatt Jacob 	MSG_CONFIG *cfgp;
1714b0a2fdeeSScott Long 	int	    error;
1715b0a2fdeeSScott Long 
1716b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1717b0a2fdeeSScott Long 	if (req == NULL) {
1718b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
171929ae59edSMatt Jacob 		return (ENOMEM);
17207104aeefSMatt Jacob 	}
1721b0a2fdeeSScott Long 
17221d558d6aSScott Long 	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
17231d558d6aSScott Long 	params.PageVersion = 0;
17241d558d6aSScott Long 	params.PageLength = 0;
17251d558d6aSScott Long 	params.PageNumber = PageNumber;
17261d558d6aSScott Long 	params.PageType = PageType;
17271d558d6aSScott Long 	params.PageAddress = PageAddress;
17281d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
1729b0a2fdeeSScott Long 				  sleep_ok, timeout_ms);
1730b0a2fdeeSScott Long 	if (error != 0) {
17316621d786SMatt Jacob 		/*
17326621d786SMatt Jacob 		 * Leave the request. Without resetting the chip, it's
17336621d786SMatt Jacob 		 * still owned by it and we'll just get into trouble
17346621d786SMatt Jacob 		 * freeing it now. Mark it as abandoned so that if it
17356621d786SMatt Jacob 		 * shows up later it can be freed.
17366621d786SMatt Jacob 		 */
1737b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_header timed out\n");
173829ae59edSMatt Jacob 		return (ETIMEDOUT);
1739b0a2fdeeSScott Long 	}
1740b0a2fdeeSScott Long 
174129ae59edSMatt Jacob         switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
174229ae59edSMatt Jacob 	case MPI_IOCSTATUS_SUCCESS:
1743b0a2fdeeSScott Long 		cfgp = req->req_vbuf;
1744b0a2fdeeSScott Long 		bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1745b0a2fdeeSScott Long 		error = 0;
174629ae59edSMatt Jacob 		break;
174729ae59edSMatt Jacob 	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
174829ae59edSMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG,
174929ae59edSMatt Jacob 		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
175029ae59edSMatt Jacob 		    PageType, PageNumber, PageAddress);
175129ae59edSMatt Jacob 		error = EINVAL;
175229ae59edSMatt Jacob 		break;
175329ae59edSMatt Jacob 	default:
175429ae59edSMatt Jacob 		mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
175529ae59edSMatt Jacob 			req->IOCStatus);
175629ae59edSMatt Jacob 		error = EIO;
175729ae59edSMatt Jacob 		break;
1758b0a2fdeeSScott Long 	}
17597104aeefSMatt Jacob 	mpt_free_request(mpt, req);
1760b0a2fdeeSScott Long 	return (error);
17617104aeefSMatt Jacob }
17627104aeefSMatt Jacob 
1763ce68dae5SMatt Jacob int
1764b0a2fdeeSScott Long mpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1765b0a2fdeeSScott Long 		  CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1766b0a2fdeeSScott Long 		  int timeout_ms)
17677104aeefSMatt Jacob {
17687104aeefSMatt Jacob 	request_t    *req;
17691d558d6aSScott Long 	cfgparms_t    params;
1770b0a2fdeeSScott Long 	int	      error;
17717104aeefSMatt Jacob 
1772b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1773b0a2fdeeSScott Long 	if (req == NULL) {
1774b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
17757104aeefSMatt Jacob 		return (-1);
17767104aeefSMatt Jacob 	}
17777104aeefSMatt Jacob 
17781d558d6aSScott Long 	params.Action = Action;
17791d558d6aSScott Long 	params.PageVersion = hdr->PageVersion;
17801d558d6aSScott Long 	params.PageLength = hdr->PageLength;
17811d558d6aSScott Long 	params.PageNumber = hdr->PageNumber;
17821d558d6aSScott Long 	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
17831d558d6aSScott Long 	params.PageAddress = PageAddress;
17841d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params,
17851d558d6aSScott Long 				  req->req_pbuf + MPT_RQSL(mpt),
1786b0a2fdeeSScott Long 				  len, sleep_ok, timeout_ms);
1787b0a2fdeeSScott Long 	if (error != 0) {
1788b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
17897104aeefSMatt Jacob 		return (-1);
17907104aeefSMatt Jacob 	}
1791b0a2fdeeSScott Long 
1792b0a2fdeeSScott Long 	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1793b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1794b0a2fdeeSScott Long 			req->IOCStatus);
1795b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
1796b0a2fdeeSScott Long 		return (-1);
1797b0a2fdeeSScott Long 	}
17987104aeefSMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
17997104aeefSMatt Jacob 	    BUS_DMASYNC_POSTREAD);
1800c87e3f83SMatt Jacob 	memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
18017104aeefSMatt Jacob 	mpt_free_request(mpt, req);
18027104aeefSMatt Jacob 	return (0);
18037104aeefSMatt Jacob }
18047104aeefSMatt Jacob 
1805ce68dae5SMatt Jacob int
1806b0a2fdeeSScott Long mpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1807b0a2fdeeSScott Long 		   CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1808b0a2fdeeSScott Long 		   int timeout_ms)
18097104aeefSMatt Jacob {
18107104aeefSMatt Jacob 	request_t    *req;
18111d558d6aSScott Long 	cfgparms_t    params;
1812b0a2fdeeSScott Long 	u_int	      hdr_attr;
1813b0a2fdeeSScott Long 	int	      error;
18147104aeefSMatt Jacob 
18157104aeefSMatt Jacob 	hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
18167104aeefSMatt Jacob 	if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
18177104aeefSMatt Jacob 	    hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1818b0a2fdeeSScott Long 		mpt_prt(mpt, "page type 0x%x not changeable\n",
18197104aeefSMatt Jacob 			hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
18207104aeefSMatt Jacob 		return (-1);
18217104aeefSMatt Jacob 	}
1822b4c618c0SMatt Jacob 
1823b4c618c0SMatt Jacob #if	0
1824b4c618c0SMatt Jacob 	/*
1825b4c618c0SMatt Jacob 	 * We shouldn't mask off other bits here.
1826b4c618c0SMatt Jacob 	 */
1827b4c618c0SMatt Jacob 	hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1828b4c618c0SMatt Jacob #endif
18297104aeefSMatt Jacob 
1830b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1831b0a2fdeeSScott Long 	if (req == NULL)
1832b0a2fdeeSScott Long 		return (-1);
18337104aeefSMatt Jacob 
1834c87e3f83SMatt Jacob 	memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1835b4c618c0SMatt Jacob 
1836b4c618c0SMatt Jacob 	/*
1837b4c618c0SMatt Jacob 	 * There isn't any point in restoring stripped out attributes
1838b4c618c0SMatt Jacob 	 * if you then mask them going down to issue the request.
1839b4c618c0SMatt Jacob 	 */
1840b4c618c0SMatt Jacob 
18411d558d6aSScott Long 	params.Action = Action;
18421d558d6aSScott Long 	params.PageVersion = hdr->PageVersion;
18431d558d6aSScott Long 	params.PageLength = hdr->PageLength;
18441d558d6aSScott Long 	params.PageNumber = hdr->PageNumber;
18451d558d6aSScott Long 	params.PageAddress = PageAddress;
1846b4c618c0SMatt Jacob #if	0
1847301472c2SMatt Jacob 	/* Restore stripped out attributes */
1848301472c2SMatt Jacob 	hdr->PageType |= hdr_attr;
18491d558d6aSScott Long 	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1850b4c618c0SMatt Jacob #else
18511d558d6aSScott Long 	params.PageType = hdr->PageType;
18521d558d6aSScott Long #endif
18531d558d6aSScott Long 	error = mpt_issue_cfg_req(mpt, req, &params,
1854b4c618c0SMatt Jacob 				  req->req_pbuf + MPT_RQSL(mpt),
1855b4c618c0SMatt Jacob 				  len, sleep_ok, timeout_ms);
1856b0a2fdeeSScott Long 	if (error != 0) {
1857b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
18587104aeefSMatt Jacob 		return (-1);
18597104aeefSMatt Jacob 	}
18607104aeefSMatt Jacob 
1861b0a2fdeeSScott Long         if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1862b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1863b0a2fdeeSScott Long 			req->IOCStatus);
18647104aeefSMatt Jacob 		mpt_free_request(mpt, req);
1865b0a2fdeeSScott Long 		return (-1);
1866b0a2fdeeSScott Long 	}
1867b0a2fdeeSScott Long 	mpt_free_request(mpt, req);
1868b0a2fdeeSScott Long 	return (0);
1869b0a2fdeeSScott Long }
1870b0a2fdeeSScott Long 
1871b0a2fdeeSScott Long /*
1872b0a2fdeeSScott Long  * Read IOC configuration information
1873b0a2fdeeSScott Long  */
1874b0a2fdeeSScott Long static int
1875b0a2fdeeSScott Long mpt_read_config_info_ioc(struct mpt_softc *mpt)
1876b0a2fdeeSScott Long {
1877b0a2fdeeSScott Long 	CONFIG_PAGE_HEADER hdr;
1878b0a2fdeeSScott Long 	struct mpt_raid_volume *mpt_raid;
1879b0a2fdeeSScott Long 	int rv;
1880b0a2fdeeSScott Long 	int i;
1881b0a2fdeeSScott Long 	size_t len;
1882b0a2fdeeSScott Long 
1883b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
18841d79ca0eSMatt Jacob 		2, 0, &hdr, FALSE, 5000);
188529ae59edSMatt Jacob 	/*
188629ae59edSMatt Jacob 	 * If it's an invalid page, so what? Not a supported function....
188729ae59edSMatt Jacob 	 */
18881d79ca0eSMatt Jacob 	if (rv == EINVAL) {
188929ae59edSMatt Jacob 		return (0);
18901d79ca0eSMatt Jacob 	}
18911d79ca0eSMatt Jacob 	if (rv) {
189229ae59edSMatt Jacob 		return (rv);
18931d79ca0eSMatt Jacob 	}
1894b0a2fdeeSScott Long 
18950e0521a1SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG,
18960e0521a1SMatt Jacob 	    "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n",
18970e0521a1SMatt Jacob 	    hdr.PageVersion, hdr.PageLength << 2,
1898b0a2fdeeSScott Long 	    hdr.PageNumber, hdr.PageType);
1899b0a2fdeeSScott Long 
1900b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
1901a3699bcaSScott Long 	mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
19021d79ca0eSMatt Jacob 	if (mpt->ioc_page2 == NULL) {
19031d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
19041d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
1905b0a2fdeeSScott Long 		return (ENOMEM);
19061d79ca0eSMatt Jacob 	}
1907b0a2fdeeSScott Long 	memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
19081d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
19091d79ca0eSMatt Jacob 	    &mpt->ioc_page2->Header, len, FALSE, 5000);
1910b0a2fdeeSScott Long 	if (rv) {
1911b0a2fdeeSScott Long 		mpt_prt(mpt, "failed to read IOC Page 2\n");
19121d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
19131d79ca0eSMatt Jacob 		return (EIO);
19141d79ca0eSMatt Jacob 	}
19150e0521a1SMatt Jacob 	mpt2host_config_page_ioc2(mpt->ioc_page2);
19161d79ca0eSMatt Jacob 
19171d79ca0eSMatt Jacob 	if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1918b0a2fdeeSScott Long 		uint32_t mask;
1919b0a2fdeeSScott Long 
1920b0a2fdeeSScott Long 		mpt_prt(mpt, "Capabilities: (");
1921b0a2fdeeSScott Long 		for (mask = 1; mask != 0; mask <<= 1) {
19221d79ca0eSMatt Jacob 			if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1923b0a2fdeeSScott Long 				continue;
19241d79ca0eSMatt Jacob 			}
1925b0a2fdeeSScott Long 			switch (mask) {
1926b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1927b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-0");
1928b0a2fdeeSScott Long 				break;
1929b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1930b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1E");
1931b0a2fdeeSScott Long 				break;
1932b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1933b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1");
1934b0a2fdeeSScott Long 				break;
1935b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1936b0a2fdeeSScott Long 				mpt_prtc(mpt, " SES");
1937b0a2fdeeSScott Long 				break;
1938b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1939b0a2fdeeSScott Long 				mpt_prtc(mpt, " SAFTE");
1940b0a2fdeeSScott Long 				break;
1941b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1942b0a2fdeeSScott Long 				mpt_prtc(mpt, " Multi-Channel-Arrays");
1943b0a2fdeeSScott Long 			default:
1944b0a2fdeeSScott Long 				break;
1945b0a2fdeeSScott Long 			}
1946b0a2fdeeSScott Long 		}
1947b0a2fdeeSScott Long 		mpt_prtc(mpt, " )\n");
1948b0a2fdeeSScott Long 		if ((mpt->ioc_page2->CapabilitiesFlags
1949b0a2fdeeSScott Long 		   & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1950b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1951b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1952b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1953b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes,
1954b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes != 1
1955b0a2fdeeSScott Long 			      ? "s " : " ",
1956b0a2fdeeSScott Long 				mpt->ioc_page2->MaxVolumes);
1957b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1958b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks,
1959b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks != 1
1960b0a2fdeeSScott Long 			      ? "s " : " ",
1961b0a2fdeeSScott Long 				mpt->ioc_page2->MaxPhysDisks);
1962b0a2fdeeSScott Long 		}
1963b0a2fdeeSScott Long 	}
1964b0a2fdeeSScott Long 
1965b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
19661d79ca0eSMatt Jacob 	mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1967b0a2fdeeSScott Long 	if (mpt->raid_volumes == NULL) {
1968b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID volume data\n");
19691d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
19701d79ca0eSMatt Jacob 		return (ENOMEM);
1971b0a2fdeeSScott Long 	}
1972b0a2fdeeSScott Long 
1973b0a2fdeeSScott Long 	/*
1974b0a2fdeeSScott Long 	 * Copy critical data out of ioc_page2 so that we can
1975b0a2fdeeSScott Long 	 * safely refresh the page without windows of unreliable
1976b0a2fdeeSScott Long 	 * data.
1977b0a2fdeeSScott Long 	 */
1978b0a2fdeeSScott Long 	mpt->raid_max_volumes =  mpt->ioc_page2->MaxVolumes;
1979b0a2fdeeSScott Long 
19801d79ca0eSMatt Jacob 	len = sizeof(*mpt->raid_volumes->config_page) +
19811d79ca0eSMatt Jacob 	    (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
1982b0a2fdeeSScott Long 	for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
1983b0a2fdeeSScott Long 		mpt_raid = &mpt->raid_volumes[i];
19841d79ca0eSMatt Jacob 		mpt_raid->config_page =
19851d79ca0eSMatt Jacob 		    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1986b0a2fdeeSScott Long 		if (mpt_raid->config_page == NULL) {
1987b0a2fdeeSScott Long 			mpt_prt(mpt, "Could not allocate RAID page data\n");
19881d79ca0eSMatt Jacob 			mpt_raid_free_mem(mpt);
19891d79ca0eSMatt Jacob 			return (ENOMEM);
1990b0a2fdeeSScott Long 		}
1991b0a2fdeeSScott Long 	}
1992b0a2fdeeSScott Long 	mpt->raid_page0_len = len;
1993b0a2fdeeSScott Long 
1994b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
19951d79ca0eSMatt Jacob 	mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1996b0a2fdeeSScott Long 	if (mpt->raid_disks == NULL) {
1997b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID disk data\n");
19981d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
19991d79ca0eSMatt Jacob 		return (ENOMEM);
2000b0a2fdeeSScott Long 	}
2001b0a2fdeeSScott Long 	mpt->raid_max_disks =  mpt->ioc_page2->MaxPhysDisks;
2002b0a2fdeeSScott Long 
20031d79ca0eSMatt Jacob 	/*
20041d79ca0eSMatt Jacob 	 * Load page 3.
20051d79ca0eSMatt Jacob 	 */
2006b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
20071d79ca0eSMatt Jacob 	    3, 0, &hdr, FALSE, 5000);
20081d79ca0eSMatt Jacob 	if (rv) {
20091d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
2010b0a2fdeeSScott Long 		return (EIO);
20111d79ca0eSMatt Jacob 	}
2012b0a2fdeeSScott Long 
2013b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
2014b0a2fdeeSScott Long 	    hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
2015b0a2fdeeSScott Long 
2016b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
2017a3699bcaSScott Long 	mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
20181d79ca0eSMatt Jacob 	if (mpt->ioc_page3 == NULL) {
20191d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
20201d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
20211d79ca0eSMatt Jacob 		return (ENOMEM);
2022b0a2fdeeSScott Long 	}
20231d79ca0eSMatt Jacob 	memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
20241d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
20251d79ca0eSMatt Jacob 	    &mpt->ioc_page3->Header, len, FALSE, 5000);
20261d79ca0eSMatt Jacob 	if (rv) {
20271d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
20281d79ca0eSMatt Jacob 		return (EIO);
20291d79ca0eSMatt Jacob 	}
20307ee37807SMarius Strobl 	mpt2host_config_page_ioc3(mpt->ioc_page3);
2031b0a2fdeeSScott Long 	mpt_raid_wakeup(mpt);
20327104aeefSMatt Jacob 	return (0);
20337104aeefSMatt Jacob }
20347104aeefSMatt Jacob 
20357104aeefSMatt Jacob /*
20367104aeefSMatt Jacob  * Enable IOC port
20377104aeefSMatt Jacob  */
20387104aeefSMatt Jacob static int
2039b0a2fdeeSScott Long mpt_send_port_enable(struct mpt_softc *mpt, int port)
20409b631363SMatt Jacob {
20419b631363SMatt Jacob 	request_t	*req;
20429b631363SMatt Jacob 	MSG_PORT_ENABLE *enable_req;
2043b0a2fdeeSScott Long 	int		 error;
20449b631363SMatt Jacob 
2045b0a2fdeeSScott Long 	req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
2046b0a2fdeeSScott Long 	if (req == NULL)
2047b0a2fdeeSScott Long 		return (-1);
20489b631363SMatt Jacob 
20499b631363SMatt Jacob 	enable_req = req->req_vbuf;
20505e073106SMatt Jacob 	memset(enable_req, 0,  MPT_RQSL(mpt));
20519b631363SMatt Jacob 
20529b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_PORT_ENABLE;
2053b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
20549b631363SMatt Jacob 	enable_req->PortNumber = port;
20559b631363SMatt Jacob 
20569b631363SMatt Jacob 	mpt_check_doorbell(mpt);
2057b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
20589b631363SMatt Jacob 
2059b0a2fdeeSScott Long 	mpt_send_cmd(mpt, req);
2060b0a2fdeeSScott Long 	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
20615089bd63SMatt Jacob 	    FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000);
2062b0a2fdeeSScott Long 	if (error != 0) {
2063c87e3f83SMatt Jacob 		mpt_prt(mpt, "port %d enable timed out\n", port);
20649b631363SMatt Jacob 		return (-1);
20659b631363SMatt Jacob 	}
20669b631363SMatt Jacob 	mpt_free_request(mpt, req);
2067c87e3f83SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
20689b631363SMatt Jacob 	return (0);
20699b631363SMatt Jacob }
20709b631363SMatt Jacob 
20719b631363SMatt Jacob /*
20729b631363SMatt Jacob  * Enable/Disable asynchronous event reporting.
20739b631363SMatt Jacob  */
20749b631363SMatt Jacob static int
2075b0a2fdeeSScott Long mpt_send_event_request(struct mpt_softc *mpt, int onoff)
20769b631363SMatt Jacob {
20779b631363SMatt Jacob 	request_t *req;
20789b631363SMatt Jacob 	MSG_EVENT_NOTIFY *enable_req;
20799b631363SMatt Jacob 
20805e073106SMatt Jacob 	req = mpt_get_request(mpt, FALSE);
20815e073106SMatt Jacob 	if (req == NULL) {
20825e073106SMatt Jacob 		return (ENOMEM);
20835e073106SMatt Jacob 	}
20849b631363SMatt Jacob 	enable_req = req->req_vbuf;
20855e073106SMatt Jacob 	memset(enable_req, 0, sizeof *enable_req);
20869b631363SMatt Jacob 
20879b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_EVENT_NOTIFICATION;
2088b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
20899b631363SMatt Jacob 	enable_req->Switch     = onoff;
20909b631363SMatt Jacob 
20919b631363SMatt Jacob 	mpt_check_doorbell(mpt);
20925e073106SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
20935e073106SMatt Jacob 	    onoff ? "en" : "dis");
20945e073106SMatt Jacob 	/*
20955e073106SMatt Jacob 	 * Send the command off, but don't wait for it.
20965e073106SMatt Jacob 	 */
20979b631363SMatt Jacob 	mpt_send_cmd(mpt, req);
20989b631363SMatt Jacob 	return (0);
20999b631363SMatt Jacob }
21009b631363SMatt Jacob 
21019b631363SMatt Jacob /*
2102976b0106SKevin Lo  * Un-mask the interrupts on the chip.
21039b631363SMatt Jacob  */
21049b631363SMatt Jacob void
2105b0a2fdeeSScott Long mpt_enable_ints(struct mpt_softc *mpt)
21069b631363SMatt Jacob {
21079b631363SMatt Jacob 	/* Unmask every thing except door bell int */
21089b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
21099b631363SMatt Jacob }
21109b631363SMatt Jacob 
21119b631363SMatt Jacob /*
2112976b0106SKevin Lo  * Mask the interrupts on the chip.
21139b631363SMatt Jacob  */
21149b631363SMatt Jacob void
2115b0a2fdeeSScott Long mpt_disable_ints(struct mpt_softc *mpt)
21169b631363SMatt Jacob {
21179b631363SMatt Jacob 	/* Mask all interrupts */
21189b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK,
21199b631363SMatt Jacob 	    MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
21209b631363SMatt Jacob }
21219b631363SMatt Jacob 
2122b0a2fdeeSScott Long static void
2123b0a2fdeeSScott Long mpt_sysctl_attach(struct mpt_softc *mpt)
21249b631363SMatt Jacob {
2125c87e3f83SMatt Jacob #if __FreeBSD_version >= 500000
2126b0a2fdeeSScott Long 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
2127b0a2fdeeSScott Long 	struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
21289b631363SMatt Jacob 
2129*6dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2130b0a2fdeeSScott Long 		       "debug", CTLFLAG_RW, &mpt->verbose, 0,
2131b0a2fdeeSScott Long 		       "Debugging/Verbose level");
2132*6dc7dc9aSMatthew D Fleming 	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2133b4c618c0SMatt Jacob 		       "role", CTLFLAG_RD, &mpt->role, 0,
2134b4c618c0SMatt Jacob 		       "HBA role");
2135a2baed97SMatt Jacob #ifdef	MPT_TEST_MULTIPATH
2136a2baed97SMatt Jacob 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2137a2baed97SMatt Jacob 		       "failure_id", CTLFLAG_RW, &mpt->failure_id, -1,
2138a2baed97SMatt Jacob 		       "Next Target to Fail");
2139a2baed97SMatt Jacob #endif
2140c87e3f83SMatt Jacob #endif
21419b631363SMatt Jacob }
21429b631363SMatt Jacob 
2143b0a2fdeeSScott Long int
2144b0a2fdeeSScott Long mpt_attach(struct mpt_softc *mpt)
2145b0a2fdeeSScott Long {
2146b0a2fdeeSScott Long 	struct mpt_personality *pers;
2147c87e3f83SMatt Jacob 	int i;
2148b0a2fdeeSScott Long 	int error;
2149b0a2fdeeSScott Long 
2150e955d4fdSScott Long 	mpt_core_attach(mpt);
2151e955d4fdSScott Long 	mpt_core_enable(mpt);
2152e955d4fdSScott Long 
2153a7303be1SMatt Jacob 	TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links);
2154c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2155b0a2fdeeSScott Long 		pers = mpt_personalities[i];
2156c87e3f83SMatt Jacob 		if (pers == NULL) {
2157b0a2fdeeSScott Long 			continue;
2158c87e3f83SMatt Jacob 		}
2159b0a2fdeeSScott Long 		if (pers->probe(mpt) == 0) {
2160b0a2fdeeSScott Long 			error = pers->attach(mpt);
2161b0a2fdeeSScott Long 			if (error != 0) {
2162b0a2fdeeSScott Long 				mpt_detach(mpt);
2163b0a2fdeeSScott Long 				return (error);
2164b0a2fdeeSScott Long 			}
2165b0a2fdeeSScott Long 			mpt->mpt_pers_mask |= (0x1 << pers->id);
2166b0a2fdeeSScott Long 			pers->use_count++;
2167b0a2fdeeSScott Long 		}
2168b0a2fdeeSScott Long 	}
2169444dd2b6SMatt Jacob 
2170c87e3f83SMatt Jacob 	/*
2171c87e3f83SMatt Jacob 	 * Now that we've attached everything, do the enable function
2172c87e3f83SMatt Jacob 	 * for all of the personalities. This allows the personalities
2173c87e3f83SMatt Jacob 	 * to do setups that are appropriate for them prior to enabling
2174c87e3f83SMatt Jacob 	 * any ports.
2175c87e3f83SMatt Jacob 	 */
2176c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2177c87e3f83SMatt Jacob 		pers = mpt_personalities[i];
2178c87e3f83SMatt Jacob 		if (pers != NULL  && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2179c87e3f83SMatt Jacob 			error = pers->enable(mpt);
2180c87e3f83SMatt Jacob 			if (error != 0) {
2181c87e3f83SMatt Jacob 				mpt_prt(mpt, "personality %s attached but would"
2182c87e3f83SMatt Jacob 				    " not enable (%d)\n", pers->name, error);
2183c87e3f83SMatt Jacob 				mpt_detach(mpt);
2184c87e3f83SMatt Jacob 				return (error);
2185c87e3f83SMatt Jacob 			}
2186c87e3f83SMatt Jacob 		}
2187c87e3f83SMatt Jacob 	}
2188b0a2fdeeSScott Long 	return (0);
2189b0a2fdeeSScott Long }
2190b0a2fdeeSScott Long 
2191b0a2fdeeSScott Long int
2192b0a2fdeeSScott Long mpt_shutdown(struct mpt_softc *mpt)
2193b0a2fdeeSScott Long {
2194b0a2fdeeSScott Long 	struct mpt_personality *pers;
2195b0a2fdeeSScott Long 
2196c87e3f83SMatt Jacob 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2197b0a2fdeeSScott Long 		pers->shutdown(mpt);
2198c87e3f83SMatt Jacob 	}
2199b0a2fdeeSScott Long 	return (0);
2200b0a2fdeeSScott Long }
2201b0a2fdeeSScott Long 
2202b0a2fdeeSScott Long int
2203b0a2fdeeSScott Long mpt_detach(struct mpt_softc *mpt)
2204b0a2fdeeSScott Long {
2205b0a2fdeeSScott Long 	struct mpt_personality *pers;
2206b0a2fdeeSScott Long 
2207b0a2fdeeSScott Long 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2208b0a2fdeeSScott Long 		pers->detach(mpt);
2209b0a2fdeeSScott Long 		mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2210b0a2fdeeSScott Long 		pers->use_count--;
2211b0a2fdeeSScott Long 	}
2212a7303be1SMatt Jacob 	TAILQ_REMOVE(&mpt_tailq, mpt, links);
2213b0a2fdeeSScott Long 	return (0);
2214b0a2fdeeSScott Long }
2215b0a2fdeeSScott Long 
2216b0a2fdeeSScott Long int
2217b0a2fdeeSScott Long mpt_core_load(struct mpt_personality *pers)
2218b0a2fdeeSScott Long {
2219b0a2fdeeSScott Long 	int i;
2220b0a2fdeeSScott Long 
2221b0a2fdeeSScott Long 	/*
2222b0a2fdeeSScott Long 	 * Setup core handlers and insert the default handler
2223b0a2fdeeSScott Long 	 * into all "empty slots".
2224b0a2fdeeSScott Long 	 */
2225c87e3f83SMatt Jacob 	for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2226b0a2fdeeSScott Long 		mpt_reply_handlers[i] = mpt_default_reply_handler;
2227c87e3f83SMatt Jacob 	}
2228b0a2fdeeSScott Long 
2229b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2230b0a2fdeeSScott Long 	    mpt_event_reply_handler;
2231b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2232b0a2fdeeSScott Long 	    mpt_config_reply_handler;
2233b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2234b0a2fdeeSScott Long 	    mpt_handshake_reply_handler;
2235b0a2fdeeSScott Long 	return (0);
22369b631363SMatt Jacob }
22379b631363SMatt Jacob 
22389b631363SMatt Jacob /*
2239b0a2fdeeSScott Long  * Initialize per-instance driver data and perform
2240b0a2fdeeSScott Long  * initial controller configuration.
22419b631363SMatt Jacob  */
2242b0a2fdeeSScott Long int
2243b0a2fdeeSScott Long mpt_core_attach(struct mpt_softc *mpt)
2244b0a2fdeeSScott Long {
2245d0a68c27SMatt Jacob         int val, error;
2246c87e3f83SMatt Jacob 
2247b0a2fdeeSScott Long 	LIST_INIT(&mpt->ack_frames);
2248b0a2fdeeSScott Long 	/* Put all request buffers on the free list */
2249b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_pending_list);
2250b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_free_list);
22515e073106SMatt Jacob 	TAILQ_INIT(&mpt->request_timeout_list);
2252c87e3f83SMatt Jacob 	for (val = 0; val < MPT_MAX_LUNS; val++) {
2253c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].atios);
2254c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].inots);
2255c87e3f83SMatt Jacob 	}
2256c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.atios);
2257c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.inots);
2258a2baed97SMatt Jacob #ifdef	MPT_TEST_MULTIPATH
2259a2baed97SMatt Jacob 	mpt->failure_id = -1;
2260a2baed97SMatt Jacob #endif
2261c87e3f83SMatt Jacob 	mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2262b0a2fdeeSScott Long 	mpt_sysctl_attach(mpt);
2263b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2264b0a2fdeeSScott Long 	    mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
2265d0a68c27SMatt Jacob 
2266d0a68c27SMatt Jacob 	MPT_LOCK(mpt);
2267942adee7SScott Long 	error = mpt_configure_ioc(mpt, 0, 0);
2268d0a68c27SMatt Jacob 	MPT_UNLOCK(mpt);
2269d0a68c27SMatt Jacob 
2270d0a68c27SMatt Jacob 	return (error);
22719b631363SMatt Jacob }
22729b631363SMatt Jacob 
2273c87e3f83SMatt Jacob int
2274c87e3f83SMatt Jacob mpt_core_enable(struct mpt_softc *mpt)
2275c87e3f83SMatt Jacob {
2276c87e3f83SMatt Jacob 	/*
2277c87e3f83SMatt Jacob 	 * We enter with the IOC enabled, but async events
2278c87e3f83SMatt Jacob 	 * not enabled, ports not enabled and interrupts
2279c87e3f83SMatt Jacob 	 * not enabled.
2280c87e3f83SMatt Jacob 	 */
2281d0a68c27SMatt Jacob 	MPT_LOCK(mpt);
2282c87e3f83SMatt Jacob 
2283c87e3f83SMatt Jacob 	/*
2284c87e3f83SMatt Jacob 	 * Enable asynchronous event reporting- all personalities
2285c87e3f83SMatt Jacob 	 * have attached so that they should be able to now field
2286c87e3f83SMatt Jacob 	 * async events.
2287c87e3f83SMatt Jacob 	 */
2288c87e3f83SMatt Jacob 	mpt_send_event_request(mpt, 1);
2289c87e3f83SMatt Jacob 
2290c87e3f83SMatt Jacob 	/*
2291c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2292c87e3f83SMatt Jacob 	 *
2293c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2294c87e3f83SMatt Jacob 	 * the portenable below times out.
2295c87e3f83SMatt Jacob 	 */
2296c87e3f83SMatt Jacob 	mpt_intr(mpt);
2297c87e3f83SMatt Jacob 
2298c87e3f83SMatt Jacob 	/*
2299c87e3f83SMatt Jacob 	 * Enable Interrupts
2300c87e3f83SMatt Jacob 	 */
2301c87e3f83SMatt Jacob 	mpt_enable_ints(mpt);
2302c87e3f83SMatt Jacob 
2303c87e3f83SMatt Jacob 	/*
2304c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2305c87e3f83SMatt Jacob 	 *
2306c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2307c87e3f83SMatt Jacob 	 * the portenable below times out.
2308c87e3f83SMatt Jacob 	 */
2309c87e3f83SMatt Jacob 	mpt_intr(mpt);
2310c87e3f83SMatt Jacob 
2311c87e3f83SMatt Jacob 	/*
23125089bd63SMatt Jacob 	 * Enable the port.
2313c87e3f83SMatt Jacob 	 */
2314c87e3f83SMatt Jacob 	if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2315c87e3f83SMatt Jacob 		mpt_prt(mpt, "failed to enable port 0\n");
2316d0a68c27SMatt Jacob 		MPT_UNLOCK(mpt);
2317c87e3f83SMatt Jacob 		return (ENXIO);
2318c87e3f83SMatt Jacob 	}
2319d0a68c27SMatt Jacob 	MPT_UNLOCK(mpt);
2320c87e3f83SMatt Jacob 	return (0);
2321c87e3f83SMatt Jacob }
2322c87e3f83SMatt Jacob 
2323b0a2fdeeSScott Long void
2324b0a2fdeeSScott Long mpt_core_shutdown(struct mpt_softc *mpt)
2325b0a2fdeeSScott Long {
2326c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2327b0a2fdeeSScott Long }
2328b0a2fdeeSScott Long 
2329b0a2fdeeSScott Long void
2330b0a2fdeeSScott Long mpt_core_detach(struct mpt_softc *mpt)
2331b0a2fdeeSScott Long {
2332363b8ed7SAlexander Kabaev 	int val;
2333363b8ed7SAlexander Kabaev 
23340e0521a1SMatt Jacob 	/*
23350e0521a1SMatt Jacob 	 * XXX: FREE MEMORY
23360e0521a1SMatt Jacob 	 */
2337c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2338363b8ed7SAlexander Kabaev 
2339363b8ed7SAlexander Kabaev 	/* Make sure no request has pending timeouts. */
2340363b8ed7SAlexander Kabaev 	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2341363b8ed7SAlexander Kabaev 		request_t *req = &mpt->request_pool[val];
2342363b8ed7SAlexander Kabaev 		mpt_callout_drain(mpt, &req->callout);
2343363b8ed7SAlexander Kabaev 	}
23444201341fSKenneth D. Merry 
23454201341fSKenneth D. Merry 	mpt_dma_buf_free(mpt);
2346b0a2fdeeSScott Long }
2347b0a2fdeeSScott Long 
2348b0a2fdeeSScott Long int
2349b0a2fdeeSScott Long mpt_core_unload(struct mpt_personality *pers)
2350b0a2fdeeSScott Long {
2351b0a2fdeeSScott Long 	/* Unload is always successfull. */
2352b0a2fdeeSScott Long 	return (0);
2353b0a2fdeeSScott Long }
2354b0a2fdeeSScott Long 
2355b0a2fdeeSScott Long #define FW_UPLOAD_REQ_SIZE				\
2356b0a2fdeeSScott Long 	(sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION)	\
2357b0a2fdeeSScott Long        + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2358b0a2fdeeSScott Long 
2359b0a2fdeeSScott Long static int
2360b0a2fdeeSScott Long mpt_upload_fw(struct mpt_softc *mpt)
2361b0a2fdeeSScott Long {
2362b0a2fdeeSScott Long 	uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2363b0a2fdeeSScott Long 	MSG_FW_UPLOAD_REPLY fw_reply;
2364b0a2fdeeSScott Long 	MSG_FW_UPLOAD *fw_req;
2365b0a2fdeeSScott Long 	FW_UPLOAD_TCSGE *tsge;
2366b0a2fdeeSScott Long 	SGE_SIMPLE32 *sge;
2367b0a2fdeeSScott Long 	uint32_t flags;
2368b0a2fdeeSScott Long 	int error;
2369b0a2fdeeSScott Long 
2370b0a2fdeeSScott Long 	memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2371b0a2fdeeSScott Long 	fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2372b0a2fdeeSScott Long 	fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2373b0a2fdeeSScott Long 	fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2374b0a2fdeeSScott Long 	fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2375b0a2fdeeSScott Long 	tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2376b0a2fdeeSScott Long 	tsge->DetailsLength = 12;
2377b0a2fdeeSScott Long 	tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2378b0a2fdeeSScott Long 	tsge->ImageSize = htole32(mpt->fw_image_size);
2379b0a2fdeeSScott Long 	sge = (SGE_SIMPLE32 *)(tsge + 1);
2380b0a2fdeeSScott Long 	flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2381b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2382b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2383b0a2fdeeSScott Long 	flags <<= MPI_SGE_FLAGS_SHIFT;
2384b0a2fdeeSScott Long 	sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2385b0a2fdeeSScott Long 	sge->Address = htole32(mpt->fw_phys);
2386b0a2fdeeSScott Long 	error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2387b0a2fdeeSScott Long 	if (error)
2388b0a2fdeeSScott Long 		return(error);
2389b0a2fdeeSScott Long 	error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
2390b0a2fdeeSScott Long 	return (error);
2391b0a2fdeeSScott Long }
2392b0a2fdeeSScott Long 
2393b0a2fdeeSScott Long static void
2394b0a2fdeeSScott Long mpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2395b0a2fdeeSScott Long 	       uint32_t *data, bus_size_t len)
2396b0a2fdeeSScott Long {
2397b0a2fdeeSScott Long 	uint32_t *data_end;
2398b0a2fdeeSScott Long 
2399b0a2fdeeSScott Long 	data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
24009fe6d254SMatt Jacob 	if (mpt->is_sas) {
2401444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
24029fe6d254SMatt Jacob 	}
2403b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2404b0a2fdeeSScott Long 	while (data != data_end) {
2405b0a2fdeeSScott Long 		mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2406b0a2fdeeSScott Long 		data++;
2407b0a2fdeeSScott Long 	}
24089fe6d254SMatt Jacob 	if (mpt->is_sas) {
2409444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2410b0a2fdeeSScott Long 	}
24119fe6d254SMatt Jacob }
2412b0a2fdeeSScott Long 
2413b0a2fdeeSScott Long static int
2414b0a2fdeeSScott Long mpt_download_fw(struct mpt_softc *mpt)
2415b0a2fdeeSScott Long {
2416b0a2fdeeSScott Long 	MpiFwHeader_t *fw_hdr;
2417b0a2fdeeSScott Long 	int error;
2418b0a2fdeeSScott Long 	uint32_t ext_offset;
2419b0a2fdeeSScott Long 	uint32_t data;
2420b0a2fdeeSScott Long 
2421b0a2fdeeSScott Long 	mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2422b0a2fdeeSScott Long 		mpt->fw_image_size);
2423b0a2fdeeSScott Long 
2424b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
2425b0a2fdeeSScott Long 	if (error != 0) {
2426b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2427b0a2fdeeSScott Long 		return (EIO);
2428b0a2fdeeSScott Long 	}
2429b0a2fdeeSScott Long 
2430b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2431b0a2fdeeSScott Long 		  MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2432b0a2fdeeSScott Long 
2433b0a2fdeeSScott Long 	fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
2434b0a2fdeeSScott Long 	mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2435b0a2fdeeSScott Long 		       fw_hdr->ImageSize);
2436b0a2fdeeSScott Long 
2437b0a2fdeeSScott Long 	ext_offset = fw_hdr->NextImageHeaderOffset;
2438b0a2fdeeSScott Long 	while (ext_offset != 0) {
2439b0a2fdeeSScott Long 		MpiExtImageHeader_t *ext;
2440b0a2fdeeSScott Long 
2441b0a2fdeeSScott Long 		ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2442b0a2fdeeSScott Long 		ext_offset = ext->NextImageHeaderOffset;
2443b0a2fdeeSScott Long 
2444b0a2fdeeSScott Long 		mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2445b0a2fdeeSScott Long 			       ext->ImageSize);
2446b0a2fdeeSScott Long 	}
2447b0a2fdeeSScott Long 
24489fe6d254SMatt Jacob 	if (mpt->is_sas) {
2449444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
24509fe6d254SMatt Jacob 	}
2451b0a2fdeeSScott Long 	/* Setup the address to jump to on reset. */
2452b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2453b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2454b0a2fdeeSScott Long 
2455b0a2fdeeSScott Long 	/*
2456b0a2fdeeSScott Long 	 * The controller sets the "flash bad" status after attempting
2457b0a2fdeeSScott Long 	 * to auto-boot from flash.  Clear the status so that the controller
2458b0a2fdeeSScott Long 	 * will continue the boot process with our newly installed firmware.
2459b0a2fdeeSScott Long 	 */
2460b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2461b0a2fdeeSScott Long 	data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2462b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2463b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2464b0a2fdeeSScott Long 
24659fe6d254SMatt Jacob 	if (mpt->is_sas) {
2466444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
24679fe6d254SMatt Jacob 	}
2468444dd2b6SMatt Jacob 
2469b0a2fdeeSScott Long 	/*
2470b0a2fdeeSScott Long 	 * Re-enable the processor and clear the boot halt flag.
2471b0a2fdeeSScott Long 	 */
2472b0a2fdeeSScott Long 	data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2473b0a2fdeeSScott Long 	data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2474b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2475b0a2fdeeSScott Long 
2476b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
2477b0a2fdeeSScott Long 	return (0);
2478b0a2fdeeSScott Long }
2479b0a2fdeeSScott Long 
2480cac33e16SMarius Strobl static int
24814201341fSKenneth D. Merry mpt_dma_buf_alloc(struct mpt_softc *mpt)
24824201341fSKenneth D. Merry {
24834201341fSKenneth D. Merry 	struct mpt_map_info mi;
24844201341fSKenneth D. Merry 	uint8_t *vptr;
24854201341fSKenneth D. Merry 	uint32_t pptr, end;
24864201341fSKenneth D. Merry 	int i, error;
24874201341fSKenneth D. Merry 
24884201341fSKenneth D. Merry 	/* Create a child tag for data buffers */
24894201341fSKenneth D. Merry 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
24904201341fSKenneth D. Merry 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
2491cac33e16SMarius Strobl 	    NULL, NULL, (mpt->max_cam_seg_cnt - 1) * PAGE_SIZE,
2492cac33e16SMarius Strobl 	    mpt->max_cam_seg_cnt, BUS_SPACE_MAXSIZE_32BIT, 0,
2493cac33e16SMarius Strobl 	    &mpt->buffer_dmat) != 0) {
24944201341fSKenneth D. Merry 		mpt_prt(mpt, "cannot create a dma tag for data buffers\n");
24954201341fSKenneth D. Merry 		return (1);
24964201341fSKenneth D. Merry 	}
24974201341fSKenneth D. Merry 
24984201341fSKenneth D. Merry 	/* Create a child tag for request buffers */
24994201341fSKenneth D. Merry 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
25004201341fSKenneth D. Merry 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
25014201341fSKenneth D. Merry 	    NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
25024201341fSKenneth D. Merry 	    &mpt->request_dmat) != 0) {
25034201341fSKenneth D. Merry 		mpt_prt(mpt, "cannot create a dma tag for requests\n");
25044201341fSKenneth D. Merry 		return (1);
25054201341fSKenneth D. Merry 	}
25064201341fSKenneth D. Merry 
25074201341fSKenneth D. Merry 	/* Allocate some DMA accessable memory for requests */
25084201341fSKenneth D. Merry 	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
25094201341fSKenneth D. Merry 	    BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) {
25104201341fSKenneth D. Merry 		mpt_prt(mpt, "cannot allocate %d bytes of request memory\n",
25114201341fSKenneth D. Merry 		    MPT_REQ_MEM_SIZE(mpt));
25124201341fSKenneth D. Merry 		return (1);
25134201341fSKenneth D. Merry 	}
25144201341fSKenneth D. Merry 
25154201341fSKenneth D. Merry 	mi.mpt = mpt;
25164201341fSKenneth D. Merry 	mi.error = 0;
25174201341fSKenneth D. Merry 
25184201341fSKenneth D. Merry 	/* Load and lock it into "bus space" */
25194201341fSKenneth D. Merry 	bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
25204201341fSKenneth D. Merry 	    MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
25214201341fSKenneth D. Merry 
25224201341fSKenneth D. Merry 	if (mi.error) {
25234201341fSKenneth D. Merry 		mpt_prt(mpt, "error %d loading dma map for DMA request queue\n",
25244201341fSKenneth D. Merry 		    mi.error);
25254201341fSKenneth D. Merry 		return (1);
25264201341fSKenneth D. Merry 	}
25274201341fSKenneth D. Merry 	mpt->request_phys = mi.phys;
25284201341fSKenneth D. Merry 
25294201341fSKenneth D. Merry 	/*
25304201341fSKenneth D. Merry 	 * Now create per-request dma maps
25314201341fSKenneth D. Merry 	 */
25324201341fSKenneth D. Merry 	i = 0;
25334201341fSKenneth D. Merry 	pptr =  mpt->request_phys;
25344201341fSKenneth D. Merry 	vptr =  mpt->request;
25354201341fSKenneth D. Merry 	end = pptr + MPT_REQ_MEM_SIZE(mpt);
25364201341fSKenneth D. Merry 	while(pptr < end) {
25374201341fSKenneth D. Merry 		request_t *req = &mpt->request_pool[i];
25384201341fSKenneth D. Merry 		req->index = i++;
25394201341fSKenneth D. Merry 
25404201341fSKenneth D. Merry 		/* Store location of Request Data */
25414201341fSKenneth D. Merry 		req->req_pbuf = pptr;
25424201341fSKenneth D. Merry 		req->req_vbuf = vptr;
25434201341fSKenneth D. Merry 
25444201341fSKenneth D. Merry 		pptr += MPT_REQUEST_AREA;
25454201341fSKenneth D. Merry 		vptr += MPT_REQUEST_AREA;
25464201341fSKenneth D. Merry 
25474201341fSKenneth D. Merry 		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
25484201341fSKenneth D. Merry 		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
25494201341fSKenneth D. Merry 
25504201341fSKenneth D. Merry 		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
25514201341fSKenneth D. Merry 		if (error) {
25524201341fSKenneth D. Merry 			mpt_prt(mpt, "error %d creating per-cmd DMA maps\n",
25534201341fSKenneth D. Merry 			    error);
25544201341fSKenneth D. Merry 			return (1);
25554201341fSKenneth D. Merry 		}
25564201341fSKenneth D. Merry 	}
25574201341fSKenneth D. Merry 
25584201341fSKenneth D. Merry 	return (0);
25594201341fSKenneth D. Merry }
25604201341fSKenneth D. Merry 
25614201341fSKenneth D. Merry static void
25624201341fSKenneth D. Merry mpt_dma_buf_free(struct mpt_softc *mpt)
25634201341fSKenneth D. Merry {
25644201341fSKenneth D. Merry 	int i;
25654201341fSKenneth D. Merry 	if (mpt->request_dmat == 0) {
25664201341fSKenneth D. Merry 		mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
25674201341fSKenneth D. Merry 		return;
25684201341fSKenneth D. Merry 	}
25694201341fSKenneth D. Merry 	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
25704201341fSKenneth D. Merry 		bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
25714201341fSKenneth D. Merry 	}
25724201341fSKenneth D. Merry 	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
25734201341fSKenneth D. Merry 	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
25744201341fSKenneth D. Merry 	bus_dma_tag_destroy(mpt->request_dmat);
25754201341fSKenneth D. Merry 	mpt->request_dmat = 0;
25764201341fSKenneth D. Merry 	bus_dma_tag_destroy(mpt->buffer_dmat);
25774201341fSKenneth D. Merry }
25784201341fSKenneth D. Merry 
2579b0a2fdeeSScott Long /*
2580b0a2fdeeSScott Long  * Allocate/Initialize data structures for the controller.  Called
2581b0a2fdeeSScott Long  * once at instance startup.
2582b0a2fdeeSScott Long  */
2583b0a2fdeeSScott Long static int
25840e0521a1SMatt Jacob mpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset)
2585b0a2fdeeSScott Long {
25860e0521a1SMatt Jacob 	PTR_MSG_PORT_FACTS_REPLY pfp;
25874201341fSKenneth D. Merry 	int error, port, val;
25880e0521a1SMatt Jacob 	size_t len;
2589b0a2fdeeSScott Long 
25900e0521a1SMatt Jacob 	if (tn == MPT_MAX_TRYS) {
25910e0521a1SMatt Jacob 		return (-1);
25920e0521a1SMatt Jacob 	}
2593b0a2fdeeSScott Long 
25949b631363SMatt Jacob 	/*
25959b631363SMatt Jacob 	 * No need to reset if the IOC is already in the READY state.
25969b631363SMatt Jacob 	 *
25979b631363SMatt Jacob 	 * Force reset if initialization failed previously.
25989b631363SMatt Jacob 	 * Note that a hard_reset of the second channel of a '929
25999b631363SMatt Jacob 	 * will stop operation of the first channel.  Hopefully, if the
26009b631363SMatt Jacob 	 * first channel is ok, the second will not require a hard
26019b631363SMatt Jacob 	 * reset.
26029b631363SMatt Jacob 	 */
26030e0521a1SMatt Jacob 	if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) {
2604c87e3f83SMatt Jacob 		if (mpt_reset(mpt, FALSE) != MPT_OK) {
26050e0521a1SMatt Jacob 			return (mpt_configure_ioc(mpt, tn++, 1));
2606c87e3f83SMatt Jacob 		}
2607b0a2fdeeSScott Long 		needreset = 0;
26087104aeefSMatt Jacob 	}
26097104aeefSMatt Jacob 
26100e0521a1SMatt Jacob 	if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) {
26110e0521a1SMatt Jacob 		mpt_prt(mpt, "mpt_get_iocfacts failed\n");
26120e0521a1SMatt Jacob 		return (mpt_configure_ioc(mpt, tn++, 1));
26130e0521a1SMatt Jacob 	}
26140e0521a1SMatt Jacob 	mpt2host_iocfacts_reply(&mpt->ioc_facts);
26150e0521a1SMatt Jacob 
2616b0a2fdeeSScott Long 	mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
26170e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgVersion >> 8,
26180e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgVersion & 0xFF,
26190e0521a1SMatt Jacob 	    mpt->ioc_facts.HeaderVersion >> 8,
26200e0521a1SMatt Jacob 	    mpt->ioc_facts.HeaderVersion & 0xFF);
2621444dd2b6SMatt Jacob 
2622444dd2b6SMatt Jacob 	/*
2623444dd2b6SMatt Jacob 	 * Now that we know request frame size, we can calculate
2624444dd2b6SMatt Jacob 	 * the actual (reasonable) segment limit for read/write I/O.
2625444dd2b6SMatt Jacob 	 *
2626444dd2b6SMatt Jacob 	 * This limit is constrained by:
2627444dd2b6SMatt Jacob 	 *
2628444dd2b6SMatt Jacob 	 *  + The size of each area we allocate per command (and how
2629444dd2b6SMatt Jacob 	 *    many chain segments we can fit into it).
2630444dd2b6SMatt Jacob 	 *  + The total number of areas we've set up.
2631444dd2b6SMatt Jacob 	 *  + The actual chain depth the card will allow.
2632444dd2b6SMatt Jacob 	 *
2633444dd2b6SMatt Jacob 	 * The first area's segment count is limited by the I/O request
2634444dd2b6SMatt Jacob 	 * at the head of it. We cannot allocate realistically more
2635444dd2b6SMatt Jacob 	 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2636444dd2b6SMatt Jacob 	 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2637444dd2b6SMatt Jacob 	 *
2638444dd2b6SMatt Jacob 	 */
2639444dd2b6SMatt Jacob 	/* total number of request areas we (can) allocate */
2640444dd2b6SMatt Jacob 	mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2641444dd2b6SMatt Jacob 
2642444dd2b6SMatt Jacob 	/* converted to the number of chain areas possible */
2643444dd2b6SMatt Jacob 	mpt->max_seg_cnt *= MPT_NRFM(mpt);
2644444dd2b6SMatt Jacob 
2645444dd2b6SMatt Jacob 	/* limited by the number of chain areas the card will support */
26460e0521a1SMatt Jacob 	if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) {
26474201341fSKenneth D. Merry 		mpt_lprt(mpt, MPT_PRT_INFO,
2648444dd2b6SMatt Jacob 		    "chain depth limited to %u (from %u)\n",
26490e0521a1SMatt Jacob 		    mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt);
26500e0521a1SMatt Jacob 		mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth;
2651444dd2b6SMatt Jacob 	}
2652444dd2b6SMatt Jacob 
2653444dd2b6SMatt Jacob 	/* converted to the number of simple sges in chain segments. */
2654444dd2b6SMatt Jacob 	mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2655444dd2b6SMatt Jacob 
26564201341fSKenneth D. Merry 	/*
26574201341fSKenneth D. Merry 	 * Use this as the basis for reporting the maximum I/O size to CAM.
26584201341fSKenneth D. Merry 	 */
26594201341fSKenneth D. Merry 	mpt->max_cam_seg_cnt = min(mpt->max_seg_cnt, (MAXPHYS / PAGE_SIZE) + 1);
26604201341fSKenneth D. Merry 
26614201341fSKenneth D. Merry 	error = mpt_dma_buf_alloc(mpt);
26624201341fSKenneth D. Merry 	if (error != 0) {
26634201341fSKenneth D. Merry 		mpt_prt(mpt, "mpt_dma_buf_alloc() failed!\n");
26644201341fSKenneth D. Merry 		return (EIO);
26654201341fSKenneth D. Merry 	}
26664201341fSKenneth D. Merry 
26674201341fSKenneth D. Merry 	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
26684201341fSKenneth D. Merry 		request_t *req = &mpt->request_pool[val];
26694201341fSKenneth D. Merry 		req->state = REQ_STATE_ALLOCATED;
26704201341fSKenneth D. Merry 		mpt_callout_init(mpt, &req->callout);
26714201341fSKenneth D. Merry 		mpt_free_request(mpt, req);
26724201341fSKenneth D. Merry 	}
26734201341fSKenneth D. Merry 
26744201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO, "Maximum Segment Count: %u, Maximum "
26754201341fSKenneth D. Merry 		 "CAM Segment Count: %u\n", mpt->max_seg_cnt,
26764201341fSKenneth D. Merry 		 mpt->max_cam_seg_cnt);
26774201341fSKenneth D. Merry 
26784201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO, "MsgLength=%u IOCNumber = %d\n",
26790e0521a1SMatt Jacob 	    mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber);
26804201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO,
2681444dd2b6SMatt Jacob 	    "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2682444dd2b6SMatt Jacob 	    "Request Frame Size %u bytes Max Chain Depth %u\n",
26830e0521a1SMatt Jacob 	    mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize,
26840e0521a1SMatt Jacob 	    mpt->ioc_facts.RequestFrameSize << 2,
26850e0521a1SMatt Jacob 	    mpt->ioc_facts.MaxChainDepth);
26864201341fSKenneth D. Merry 	mpt_lprt(mpt, MPT_PRT_INFO, "IOCFACTS: Num Ports %d, FWImageSize %d, "
26870e0521a1SMatt Jacob 	    "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts,
26880e0521a1SMatt Jacob 	    mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags);
26890e0521a1SMatt Jacob 
26900e0521a1SMatt Jacob 	len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY);
26910e0521a1SMatt Jacob 	mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
26920e0521a1SMatt Jacob 	if (mpt->port_facts == NULL) {
26930e0521a1SMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for port facts\n");
26940e0521a1SMatt Jacob 		return (ENOMEM);
26950e0521a1SMatt Jacob 	}
2696b0a2fdeeSScott Long 
2697444dd2b6SMatt Jacob 
26980e0521a1SMatt Jacob 	if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) &&
26990e0521a1SMatt Jacob 	    (mpt->fw_uploaded == 0)) {
2700b0a2fdeeSScott Long 		struct mpt_map_info mi;
2701b0a2fdeeSScott Long 
2702b0a2fdeeSScott Long 		/*
2703b0a2fdeeSScott Long 		 * In some configurations, the IOC's firmware is
2704b0a2fdeeSScott Long 		 * stored in a shared piece of system NVRAM that
2705b0a2fdeeSScott Long 		 * is only accessable via the BIOS.  In this
2706b0a2fdeeSScott Long 		 * case, the firmware keeps a copy of firmware in
2707b0a2fdeeSScott Long 		 * RAM until the OS driver retrieves it.  Once
2708b0a2fdeeSScott Long 		 * retrieved, we are responsible for re-downloading
2709b0a2fdeeSScott Long 		 * the firmware after any hard-reset.
2710b0a2fdeeSScott Long 		 */
27110e0521a1SMatt Jacob 		mpt->fw_image_size = mpt->ioc_facts.FWImageSize;
27120e0521a1SMatt Jacob 		error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0,
27130e0521a1SMatt Jacob 		    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
27140e0521a1SMatt Jacob 		    mpt->fw_image_size, 1, mpt->fw_image_size, 0,
27150e0521a1SMatt Jacob 		    &mpt->fw_dmat);
2716b0a2fdeeSScott Long 		if (error != 0) {
27170e0521a1SMatt Jacob 			mpt_prt(mpt, "cannot create firmwarew dma tag\n");
2718b0a2fdeeSScott Long 			return (ENOMEM);
27199b631363SMatt Jacob 		}
2720b0a2fdeeSScott Long 		error = bus_dmamem_alloc(mpt->fw_dmat,
27210e0521a1SMatt Jacob 		    (void **)&mpt->fw_image, BUS_DMA_NOWAIT, &mpt->fw_dmap);
2722b0a2fdeeSScott Long 		if (error != 0) {
27230e0521a1SMatt Jacob 			mpt_prt(mpt, "cannot allocate firmware memory\n");
2724b0a2fdeeSScott Long 			bus_dma_tag_destroy(mpt->fw_dmat);
2725b0a2fdeeSScott Long 			return (ENOMEM);
2726b0a2fdeeSScott Long 		}
2727b0a2fdeeSScott Long 		mi.mpt = mpt;
2728b0a2fdeeSScott Long 		mi.error = 0;
2729b0a2fdeeSScott Long 		bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
27300e0521a1SMatt Jacob 		    mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0);
2731b0a2fdeeSScott Long 		mpt->fw_phys = mi.phys;
2732b0a2fdeeSScott Long 
2733b0a2fdeeSScott Long 		error = mpt_upload_fw(mpt);
2734b0a2fdeeSScott Long 		if (error != 0) {
27350e0521a1SMatt Jacob 			mpt_prt(mpt, "firmware upload failed.\n");
2736b0a2fdeeSScott Long 			bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2737b0a2fdeeSScott Long 			bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2738b0a2fdeeSScott Long 			    mpt->fw_dmap);
2739b0a2fdeeSScott Long 			bus_dma_tag_destroy(mpt->fw_dmat);
2740b0a2fdeeSScott Long 			mpt->fw_image = NULL;
2741b0a2fdeeSScott Long 			return (EIO);
2742b0a2fdeeSScott Long 		}
27430e0521a1SMatt Jacob 		mpt->fw_uploaded = 1;
2744b0a2fdeeSScott Long 	}
27459b631363SMatt Jacob 
27460e0521a1SMatt Jacob 	for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) {
27470e0521a1SMatt Jacob 		pfp = &mpt->port_facts[port];
27480e0521a1SMatt Jacob 		error = mpt_get_portfacts(mpt, 0, pfp);
27490e0521a1SMatt Jacob 		if (error != MPT_OK) {
27500e0521a1SMatt Jacob 			mpt_prt(mpt,
27510e0521a1SMatt Jacob 			    "mpt_get_portfacts on port %d failed\n", port);
27520e0521a1SMatt Jacob 			free(mpt->port_facts, M_DEVBUF);
27530e0521a1SMatt Jacob 			mpt->port_facts = NULL;
27540e0521a1SMatt Jacob 			return (mpt_configure_ioc(mpt, tn++, 1));
27550e0521a1SMatt Jacob 		}
27560e0521a1SMatt Jacob 		mpt2host_portfacts_reply(pfp);
27570e0521a1SMatt Jacob 
27580e0521a1SMatt Jacob 		if (port > 0) {
27590e0521a1SMatt Jacob 			error = MPT_PRT_INFO;
27600e0521a1SMatt Jacob 		} else {
27610e0521a1SMatt Jacob 			error = MPT_PRT_DEBUG;
27620e0521a1SMatt Jacob 		}
27630e0521a1SMatt Jacob 		mpt_lprt(mpt, error,
27640e0521a1SMatt Jacob 		    "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n",
27650e0521a1SMatt Jacob 		    port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID,
27660e0521a1SMatt Jacob 		    pfp->MaxDevices);
27670e0521a1SMatt Jacob 
27687104aeefSMatt Jacob 	}
27697104aeefSMatt Jacob 
27700e0521a1SMatt Jacob 	/*
27710e0521a1SMatt Jacob 	 * XXX: Not yet supporting more than port 0
27720e0521a1SMatt Jacob 	 */
27730e0521a1SMatt Jacob 	pfp = &mpt->port_facts[0];
27740e0521a1SMatt Jacob 	if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) {
27757104aeefSMatt Jacob 		mpt->is_fc = 1;
2776444dd2b6SMatt Jacob 		mpt->is_sas = 0;
27775580ce96SMatt Jacob 		mpt->is_spi = 0;
27780e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2779444dd2b6SMatt Jacob 		mpt->is_fc = 0;
2780444dd2b6SMatt Jacob 		mpt->is_sas = 1;
27815580ce96SMatt Jacob 		mpt->is_spi = 0;
27820e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) {
27837104aeefSMatt Jacob 		mpt->is_fc = 0;
2784444dd2b6SMatt Jacob 		mpt->is_sas = 0;
27855580ce96SMatt Jacob 		mpt->is_spi = 1;
27868ba5efbdSMarius Strobl 		if (mpt->mpt_ini_id == MPT_INI_ID_NONE)
27878ba5efbdSMarius Strobl 			mpt->mpt_ini_id = pfp->PortSCSIID;
27880e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) {
27890e0521a1SMatt Jacob 		mpt_prt(mpt, "iSCSI not supported yet\n");
27900e0521a1SMatt Jacob 		return (ENXIO);
27910e0521a1SMatt Jacob 	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) {
27920e0521a1SMatt Jacob 		mpt_prt(mpt, "Inactive Port\n");
27930e0521a1SMatt Jacob 		return (ENXIO);
27940e0521a1SMatt Jacob 	} else {
27950e0521a1SMatt Jacob 		mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType);
27960e0521a1SMatt Jacob 		return (ENXIO);
27977104aeefSMatt Jacob 	}
27987104aeefSMatt Jacob 
2799c87e3f83SMatt Jacob 	/*
2800b4c618c0SMatt Jacob 	 * Set our role with what this port supports.
2801b4c618c0SMatt Jacob 	 *
2802b4c618c0SMatt Jacob 	 * Note this might be changed later in different modules
2803b4c618c0SMatt Jacob 	 * if this is different from what is wanted.
2804c87e3f83SMatt Jacob 	 */
28055089bd63SMatt Jacob 	mpt->role = MPT_ROLE_NONE;
28060e0521a1SMatt Jacob 	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
28075089bd63SMatt Jacob 		mpt->role |= MPT_ROLE_INITIATOR;
2808c87e3f83SMatt Jacob 	}
28090e0521a1SMatt Jacob 	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
28105089bd63SMatt Jacob 		mpt->role |= MPT_ROLE_TARGET;
2811c87e3f83SMatt Jacob 	}
28120e0521a1SMatt Jacob 
28130e0521a1SMatt Jacob 	/*
28140e0521a1SMatt Jacob 	 * Enable the IOC
28150e0521a1SMatt Jacob 	 */
2816e955d4fdSScott Long 	if (mpt_enable_ioc(mpt, 1) != MPT_OK) {
2817c87e3f83SMatt Jacob 		mpt_prt(mpt, "unable to initialize IOC\n");
2818b0a2fdeeSScott Long 		return (ENXIO);
28197104aeefSMatt Jacob 	}
28207104aeefSMatt Jacob 
2821b0a2fdeeSScott Long 	/*
2822c87e3f83SMatt Jacob 	 * Read IOC configuration information.
28231d79ca0eSMatt Jacob 	 *
28241d79ca0eSMatt Jacob 	 * We need this to determine whether or not we have certain
28251d79ca0eSMatt Jacob 	 * settings for Integrated Mirroring (e.g.).
2826b0a2fdeeSScott Long 	 */
2827b0a2fdeeSScott Long 	mpt_read_config_info_ioc(mpt);
2828b0a2fdeeSScott Long 
2829b0a2fdeeSScott Long 	return (0);
2830b0a2fdeeSScott Long }
2831b0a2fdeeSScott Long 
2832b0a2fdeeSScott Long static int
2833c87e3f83SMatt Jacob mpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2834b0a2fdeeSScott Long {
2835b0a2fdeeSScott Long 	uint32_t pptr;
2836b0a2fdeeSScott Long 	int val;
2837b0a2fdeeSScott Long 
2838444dd2b6SMatt Jacob 	if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2839b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2840b0a2fdeeSScott Long 		return (EIO);
2841b0a2fdeeSScott Long 	}
2842b0a2fdeeSScott Long 
2843b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
28449b631363SMatt Jacob 
28459b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2846b0a2fdeeSScott Long 		mpt_prt(mpt, "IOC failed to go to run state\n");
2847b0a2fdeeSScott Long 		return (ENXIO);
28487104aeefSMatt Jacob 	}
2849444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
28509b631363SMatt Jacob 
28519b631363SMatt Jacob 	/*
28529b631363SMatt Jacob 	 * Give it reply buffers
28539b631363SMatt Jacob 	 *
2854b0a2fdeeSScott Long 	 * Do *not* exceed global credits.
28559b631363SMatt Jacob 	 */
28569b631363SMatt Jacob 	for (val = 0, pptr = mpt->reply_phys;
28579b631363SMatt Jacob 	    (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
28589b631363SMatt Jacob 	     pptr += MPT_REPLY_SIZE) {
28599b631363SMatt Jacob 		mpt_free_reply(mpt, pptr);
28600e0521a1SMatt Jacob 		if (++val == mpt->ioc_facts.GlobalCredits - 1)
28619b631363SMatt Jacob 			break;
28629b631363SMatt Jacob 	}
28639b631363SMatt Jacob 
2864c87e3f83SMatt Jacob 
2865c87e3f83SMatt Jacob 	/*
28665e073106SMatt Jacob 	 * Enable the port if asked. This is only done if we're resetting
28675e073106SMatt Jacob 	 * the IOC after initial startup.
2868c87e3f83SMatt Jacob 	 */
2869c87e3f83SMatt Jacob 	if (portenable) {
28707104aeefSMatt Jacob 		/*
28717104aeefSMatt Jacob 		 * Enable asynchronous event reporting
28727104aeefSMatt Jacob 		 */
28739b631363SMatt Jacob 		mpt_send_event_request(mpt, 1);
28749b631363SMatt Jacob 
28759b631363SMatt Jacob 		if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
28764201341fSKenneth D. Merry 			mpt_prt(mpt, "%s: failed to enable port 0\n", __func__);
2877b0a2fdeeSScott Long 			return (ENXIO);
28787104aeefSMatt Jacob 		}
2879c87e3f83SMatt Jacob 	}
288029ae59edSMatt Jacob 	return (MPT_OK);
28819b631363SMatt Jacob }
28820e0521a1SMatt Jacob 
28830e0521a1SMatt Jacob /*
28840e0521a1SMatt Jacob  * Endian Conversion Functions- only used on Big Endian machines
28850e0521a1SMatt Jacob  */
28860e0521a1SMatt Jacob #if	_BYTE_ORDER == _BIG_ENDIAN
28870e0521a1SMatt Jacob void
28880e0521a1SMatt Jacob mpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge)
28890e0521a1SMatt Jacob {
28907ee37807SMarius Strobl 
28910e0521a1SMatt Jacob 	MPT_2_HOST32(sge, FlagsLength);
28927185cd36SMatt Jacob 	MPT_2_HOST32(sge, u.Address64.Low);
28937185cd36SMatt Jacob 	MPT_2_HOST32(sge, u.Address64.High);
28947185cd36SMatt Jacob }
28950e0521a1SMatt Jacob 
28960e0521a1SMatt Jacob void
28970e0521a1SMatt Jacob mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp)
28980e0521a1SMatt Jacob {
28997ee37807SMarius Strobl 
29000e0521a1SMatt Jacob 	MPT_2_HOST16(rp, MsgVersion);
29010e0521a1SMatt Jacob 	MPT_2_HOST16(rp, HeaderVersion);
29020e0521a1SMatt Jacob 	MPT_2_HOST32(rp, MsgContext);
29030e0521a1SMatt Jacob 	MPT_2_HOST16(rp, IOCExceptions);
29040e0521a1SMatt Jacob 	MPT_2_HOST16(rp, IOCStatus);
29050e0521a1SMatt Jacob 	MPT_2_HOST32(rp, IOCLogInfo);
29060e0521a1SMatt Jacob 	MPT_2_HOST16(rp, ReplyQueueDepth);
29070e0521a1SMatt Jacob 	MPT_2_HOST16(rp, RequestFrameSize);
29080e0521a1SMatt Jacob 	MPT_2_HOST16(rp, Reserved_0101_FWVersion);
29090e0521a1SMatt Jacob 	MPT_2_HOST16(rp, ProductID);
29100e0521a1SMatt Jacob 	MPT_2_HOST32(rp, CurrentHostMfaHighAddr);
29110e0521a1SMatt Jacob 	MPT_2_HOST16(rp, GlobalCredits);
29120e0521a1SMatt Jacob 	MPT_2_HOST32(rp, CurrentSenseBufferHighAddr);
29130e0521a1SMatt Jacob 	MPT_2_HOST16(rp, CurReplyFrameSize);
29140e0521a1SMatt Jacob 	MPT_2_HOST32(rp, FWImageSize);
29150e0521a1SMatt Jacob 	MPT_2_HOST32(rp, IOCCapabilities);
29160e0521a1SMatt Jacob 	MPT_2_HOST32(rp, FWVersion.Word);
29170e0521a1SMatt Jacob 	MPT_2_HOST16(rp, HighPriorityQueueDepth);
29180e0521a1SMatt Jacob 	MPT_2_HOST16(rp, Reserved2);
29190e0521a1SMatt Jacob 	mpt2host_sge_simple_union(&rp->HostPageBufferSGE);
29200e0521a1SMatt Jacob 	MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr);
29210e0521a1SMatt Jacob }
29220e0521a1SMatt Jacob 
29230e0521a1SMatt Jacob void
29240e0521a1SMatt Jacob mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp)
29250e0521a1SMatt Jacob {
29267ee37807SMarius Strobl 
29270e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved);
29280e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved1);
29290e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, MsgContext);
29300e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved2);
29310e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, IOCStatus);
29320e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, IOCLogInfo);
29330e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxDevices);
29340e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, PortSCSIID);
29350e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, ProtocolFlags);
29360e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxPostedCmdBuffers);
29370e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxPersistentIDs);
29380e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, MaxLanBuckets);
29390e0521a1SMatt Jacob 	MPT_2_HOST16(pfp, Reserved4);
29400e0521a1SMatt Jacob 	MPT_2_HOST32(pfp, Reserved5);
29410e0521a1SMatt Jacob }
29427ee37807SMarius Strobl 
29430e0521a1SMatt Jacob void
29440e0521a1SMatt Jacob mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2)
29450e0521a1SMatt Jacob {
29460e0521a1SMatt Jacob 	int i;
29477ee37807SMarius Strobl 
29487ee37807SMarius Strobl 	MPT_2_HOST32(ioc2, CapabilitiesFlags);
29490e0521a1SMatt Jacob 	for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) {
29507185cd36SMatt Jacob 		MPT_2_HOST16(ioc2, RaidVolume[i].Reserved3);
29510e0521a1SMatt Jacob 	}
29520e0521a1SMatt Jacob }
29530e0521a1SMatt Jacob 
29540e0521a1SMatt Jacob void
29557ee37807SMarius Strobl mpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *ioc3)
29567ee37807SMarius Strobl {
29577ee37807SMarius Strobl 
29587ee37807SMarius Strobl 	MPT_2_HOST16(ioc3, Reserved2);
29597ee37807SMarius Strobl }
29607ee37807SMarius Strobl 
29617ee37807SMarius Strobl void
29627ee37807SMarius Strobl mpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *sp0)
29637ee37807SMarius Strobl {
29647ee37807SMarius Strobl 
29657ee37807SMarius Strobl 	MPT_2_HOST32(sp0, Capabilities);
29667ee37807SMarius Strobl 	MPT_2_HOST32(sp0, PhysicalInterface);
29677ee37807SMarius Strobl }
29687ee37807SMarius Strobl 
29697ee37807SMarius Strobl void
29707ee37807SMarius Strobl mpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
29717ee37807SMarius Strobl {
29727ee37807SMarius Strobl 
29737ee37807SMarius Strobl 	MPT_2_HOST32(sp1, Configuration);
29747ee37807SMarius Strobl 	MPT_2_HOST32(sp1, OnBusTimerValue);
29757ee37807SMarius Strobl 	MPT_2_HOST16(sp1, IDConfig);
29767ee37807SMarius Strobl }
29777ee37807SMarius Strobl 
29787ee37807SMarius Strobl void
29797ee37807SMarius Strobl host2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
29807ee37807SMarius Strobl {
29817ee37807SMarius Strobl 
29827ee37807SMarius Strobl 	HOST_2_MPT32(sp1, Configuration);
29837ee37807SMarius Strobl 	HOST_2_MPT32(sp1, OnBusTimerValue);
29847ee37807SMarius Strobl 	HOST_2_MPT16(sp1, IDConfig);
29857ee37807SMarius Strobl }
29867ee37807SMarius Strobl 
29877ee37807SMarius Strobl void
29887ee37807SMarius Strobl mpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *sp2)
29897ee37807SMarius Strobl {
29907ee37807SMarius Strobl 	int i;
29917ee37807SMarius Strobl 
29927ee37807SMarius Strobl 	MPT_2_HOST32(sp2, PortFlags);
29937ee37807SMarius Strobl 	MPT_2_HOST32(sp2, PortSettings);
29947ee37807SMarius Strobl 	for (i = 0; i < sizeof(sp2->DeviceSettings) /
29957ee37807SMarius Strobl 	    sizeof(*sp2->DeviceSettings); i++) {
29967ee37807SMarius Strobl 		MPT_2_HOST16(sp2, DeviceSettings[i].DeviceFlags);
29977ee37807SMarius Strobl 	}
29987ee37807SMarius Strobl }
29997ee37807SMarius Strobl 
30007ee37807SMarius Strobl void
30017ee37807SMarius Strobl mpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *sd0)
30027ee37807SMarius Strobl {
30037ee37807SMarius Strobl 
30047ee37807SMarius Strobl 	MPT_2_HOST32(sd0, NegotiatedParameters);
30057ee37807SMarius Strobl 	MPT_2_HOST32(sd0, Information);
30067ee37807SMarius Strobl }
30077ee37807SMarius Strobl 
30087ee37807SMarius Strobl void
30097ee37807SMarius Strobl mpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
30107ee37807SMarius Strobl {
30117ee37807SMarius Strobl 
30127ee37807SMarius Strobl 	MPT_2_HOST32(sd1, RequestedParameters);
30137ee37807SMarius Strobl 	MPT_2_HOST32(sd1, Reserved);
30147ee37807SMarius Strobl 	MPT_2_HOST32(sd1, Configuration);
30157ee37807SMarius Strobl }
30167ee37807SMarius Strobl 
30177ee37807SMarius Strobl void
30187ee37807SMarius Strobl host2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
30197ee37807SMarius Strobl {
30207ee37807SMarius Strobl 
30217ee37807SMarius Strobl 	HOST_2_MPT32(sd1, RequestedParameters);
30227ee37807SMarius Strobl 	HOST_2_MPT32(sd1, Reserved);
30237ee37807SMarius Strobl 	HOST_2_MPT32(sd1, Configuration);
30247ee37807SMarius Strobl }
30257ee37807SMarius Strobl 
30267ee37807SMarius Strobl void
30277ee37807SMarius Strobl mpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *fp0)
30287ee37807SMarius Strobl {
30297ee37807SMarius Strobl 
30307ee37807SMarius Strobl 	MPT_2_HOST32(fp0, Flags);
30317ee37807SMarius Strobl 	MPT_2_HOST32(fp0, PortIdentifier);
30327ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWNN.Low);
30337ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWNN.High);
30347ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWPN.Low);
30357ee37807SMarius Strobl 	MPT_2_HOST32(fp0, WWPN.High);
30367ee37807SMarius Strobl 	MPT_2_HOST32(fp0, SupportedServiceClass);
30377ee37807SMarius Strobl 	MPT_2_HOST32(fp0, SupportedSpeeds);
30387ee37807SMarius Strobl 	MPT_2_HOST32(fp0, CurrentSpeed);
30397ee37807SMarius Strobl 	MPT_2_HOST32(fp0, MaxFrameSize);
30407ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWNN.Low);
30417ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWNN.High);
30427ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWPN.Low);
30437ee37807SMarius Strobl 	MPT_2_HOST32(fp0, FabricWWPN.High);
30447ee37807SMarius Strobl 	MPT_2_HOST32(fp0, DiscoveredPortsCount);
30457ee37807SMarius Strobl 	MPT_2_HOST32(fp0, MaxInitiators);
30467ee37807SMarius Strobl }
30477ee37807SMarius Strobl 
30487ee37807SMarius Strobl void
30497ee37807SMarius Strobl mpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
30507ee37807SMarius Strobl {
30517ee37807SMarius Strobl 
30527ee37807SMarius Strobl 	MPT_2_HOST32(fp1, Flags);
30537ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWNN.Low);
30547ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWNN.High);
30557ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWPN.Low);
30567ee37807SMarius Strobl 	MPT_2_HOST32(fp1, NoSEEPROMWWPN.High);
30577ee37807SMarius Strobl }
30587ee37807SMarius Strobl 
30597ee37807SMarius Strobl void
30607ee37807SMarius Strobl host2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
30617ee37807SMarius Strobl {
30627ee37807SMarius Strobl 
30637ee37807SMarius Strobl 	HOST_2_MPT32(fp1, Flags);
30647ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWNN.Low);
30657ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWNN.High);
30667ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWPN.Low);
30677ee37807SMarius Strobl 	HOST_2_MPT32(fp1, NoSEEPROMWWPN.High);
30687ee37807SMarius Strobl }
30697ee37807SMarius Strobl 
30707ee37807SMarius Strobl void
30710e0521a1SMatt Jacob mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp)
30720e0521a1SMatt Jacob {
30730e0521a1SMatt Jacob 	int i;
30747ee37807SMarius Strobl 
30750e0521a1SMatt Jacob 	MPT_2_HOST16(volp, VolumeStatus.Reserved);
30760e0521a1SMatt Jacob 	MPT_2_HOST16(volp, VolumeSettings.Settings);
30770e0521a1SMatt Jacob 	MPT_2_HOST32(volp, MaxLBA);
3078a4907429SScott Long 	MPT_2_HOST32(volp, MaxLBAHigh);
30790e0521a1SMatt Jacob 	MPT_2_HOST32(volp, StripeSize);
30800e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved2);
30810e0521a1SMatt Jacob 	MPT_2_HOST32(volp, Reserved3);
30820e0521a1SMatt Jacob 	for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) {
30837185cd36SMatt Jacob 		MPT_2_HOST16(volp, PhysDisk[i].Reserved);
30840e0521a1SMatt Jacob 	}
30850e0521a1SMatt Jacob }
30860e0521a1SMatt Jacob 
30870e0521a1SMatt Jacob void
30887ee37807SMarius Strobl mpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *rpd0)
30897ee37807SMarius Strobl {
30907ee37807SMarius Strobl 
30917ee37807SMarius Strobl 	MPT_2_HOST32(rpd0, Reserved1);
30927ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, PhysDiskStatus.Reserved);
30937ee37807SMarius Strobl 	MPT_2_HOST32(rpd0, MaxLBA);
30947ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, ErrorData.Reserved);
30957ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, ErrorData.ErrorCount);
30967ee37807SMarius Strobl 	MPT_2_HOST16(rpd0, ErrorData.SmartCount);
30977ee37807SMarius Strobl }
30987ee37807SMarius Strobl 
30997ee37807SMarius Strobl void
31000e0521a1SMatt Jacob mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi)
31010e0521a1SMatt Jacob {
31027ee37807SMarius Strobl 
31037185cd36SMatt Jacob 	MPT_2_HOST16(vi, TotalBlocks.High);
31047185cd36SMatt Jacob 	MPT_2_HOST16(vi, TotalBlocks.Low);
31057185cd36SMatt Jacob 	MPT_2_HOST16(vi, BlocksRemaining.High);
31067185cd36SMatt Jacob 	MPT_2_HOST16(vi, BlocksRemaining.Low);
31070e0521a1SMatt Jacob }
31080e0521a1SMatt Jacob #endif
3109