xref: /freebsd/sys/dev/mpt/mpt.c (revision 6621d786eb1087e71cddd11719817bafd7c5f32c)
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 
119b0a2fdeeSScott Long 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);
131b0a2fdeeSScott Long static int mpt_configure_ioc(struct mpt_softc *mpt);
132c87e3f83SMatt Jacob static int mpt_enable_ioc(struct mpt_softc *mpt, int);
133b0a2fdeeSScott Long 
134b0a2fdeeSScott Long /************************* Personality Module Support *************************/
135b0a2fdeeSScott Long /*
136b0a2fdeeSScott Long  * We include one extra entry that is guaranteed to be NULL
137b0a2fdeeSScott Long  * to simplify our itterator.
138b0a2fdeeSScott Long  */
139b0a2fdeeSScott Long static struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
140b0a2fdeeSScott Long static __inline struct mpt_personality*
141b0a2fdeeSScott Long 	mpt_pers_find(struct mpt_softc *, u_int);
142b0a2fdeeSScott Long static __inline struct mpt_personality*
143b0a2fdeeSScott Long 	mpt_pers_find_reverse(struct mpt_softc *, u_int);
144b0a2fdeeSScott Long 
145b0a2fdeeSScott Long static __inline struct mpt_personality *
146b0a2fdeeSScott Long mpt_pers_find(struct mpt_softc *mpt, u_int start_at)
147b0a2fdeeSScott Long {
148b0a2fdeeSScott Long 	KASSERT(start_at <= MPT_MAX_PERSONALITIES,
149b0a2fdeeSScott Long 		("mpt_pers_find: starting position out of range\n"));
150b0a2fdeeSScott Long 
151b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
152b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
153b0a2fdeeSScott Long 		start_at++;
154b0a2fdeeSScott Long 	}
155b0a2fdeeSScott Long 	return (mpt_personalities[start_at]);
156b0a2fdeeSScott Long }
157b0a2fdeeSScott Long 
158b0a2fdeeSScott Long /*
159c87e3f83SMatt Jacob  * Used infrequently, so no need to optimize like a forward
160b0a2fdeeSScott Long  * traversal where we use the MAX+1 is guaranteed to be NULL
161b0a2fdeeSScott Long  * trick.
162b0a2fdeeSScott Long  */
163b0a2fdeeSScott Long static __inline struct mpt_personality *
164b0a2fdeeSScott Long mpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
165b0a2fdeeSScott Long {
166b0a2fdeeSScott Long 	while (start_at < MPT_MAX_PERSONALITIES
167b0a2fdeeSScott Long 	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
168b0a2fdeeSScott Long 		start_at--;
169b0a2fdeeSScott Long 	}
170b0a2fdeeSScott Long 	if (start_at < MPT_MAX_PERSONALITIES)
171b0a2fdeeSScott Long 		return (mpt_personalities[start_at]);
172b0a2fdeeSScott Long 	return (NULL);
173b0a2fdeeSScott Long }
174b0a2fdeeSScott Long 
175b0a2fdeeSScott Long #define MPT_PERS_FOREACH(mpt, pers)				\
176b0a2fdeeSScott Long 	for (pers = mpt_pers_find(mpt, /*start_at*/0);		\
177b0a2fdeeSScott Long 	     pers != NULL;					\
178b0a2fdeeSScott Long 	     pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
179b0a2fdeeSScott Long 
180b0a2fdeeSScott Long #define MPT_PERS_FOREACH_REVERSE(mpt, pers)				\
181b0a2fdeeSScott Long 	for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
182b0a2fdeeSScott Long 	     pers != NULL;						\
183b0a2fdeeSScott Long 	     pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
184b0a2fdeeSScott Long 
185b0a2fdeeSScott Long static mpt_load_handler_t      mpt_stdload;
186b0a2fdeeSScott Long static mpt_probe_handler_t     mpt_stdprobe;
187b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_stdattach;
188c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_stdenable;
189b0a2fdeeSScott Long static mpt_event_handler_t     mpt_stdevent;
190b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_stdreset;
191b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_stdshutdown;
192b0a2fdeeSScott Long static mpt_detach_handler_t    mpt_stddetach;
193b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_stdunload;
194b0a2fdeeSScott Long static struct mpt_personality mpt_default_personality =
195b0a2fdeeSScott Long {
196b0a2fdeeSScott Long 	.load		= mpt_stdload,
197b0a2fdeeSScott Long 	.probe		= mpt_stdprobe,
198b0a2fdeeSScott Long 	.attach		= mpt_stdattach,
199c87e3f83SMatt Jacob 	.enable		= mpt_stdenable,
200b0a2fdeeSScott Long 	.event		= mpt_stdevent,
201b0a2fdeeSScott Long 	.reset		= mpt_stdreset,
202b0a2fdeeSScott Long 	.shutdown	= mpt_stdshutdown,
203b0a2fdeeSScott Long 	.detach		= mpt_stddetach,
204b0a2fdeeSScott Long 	.unload		= mpt_stdunload
205b0a2fdeeSScott Long };
206b0a2fdeeSScott Long 
207b0a2fdeeSScott Long static mpt_load_handler_t      mpt_core_load;
208b0a2fdeeSScott Long static mpt_attach_handler_t    mpt_core_attach;
209c87e3f83SMatt Jacob static mpt_enable_handler_t    mpt_core_enable;
210b0a2fdeeSScott Long static mpt_reset_handler_t     mpt_core_ioc_reset;
211b0a2fdeeSScott Long static mpt_event_handler_t     mpt_core_event;
212b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_shutdown;
213b0a2fdeeSScott Long static mpt_shutdown_handler_t  mpt_core_detach;
214b0a2fdeeSScott Long static mpt_unload_handler_t    mpt_core_unload;
215b0a2fdeeSScott Long static struct mpt_personality mpt_core_personality =
216b0a2fdeeSScott Long {
217b0a2fdeeSScott Long 	.name		= "mpt_core",
218b0a2fdeeSScott Long 	.load		= mpt_core_load,
219b0a2fdeeSScott Long 	.attach		= mpt_core_attach,
220c87e3f83SMatt Jacob 	.enable		= mpt_core_enable,
221b0a2fdeeSScott Long 	.event		= mpt_core_event,
222b0a2fdeeSScott Long 	.reset		= mpt_core_ioc_reset,
223b0a2fdeeSScott Long 	.shutdown	= mpt_core_shutdown,
224b0a2fdeeSScott Long 	.detach		= mpt_core_detach,
225b0a2fdeeSScott Long 	.unload		= mpt_core_unload,
226b0a2fdeeSScott Long };
227b0a2fdeeSScott Long 
228b0a2fdeeSScott Long /*
229b0a2fdeeSScott Long  * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
230b0a2fdeeSScott Long  * ordering information.  We want the core to always register FIRST.
231b0a2fdeeSScott Long  * other modules are set to SI_ORDER_SECOND.
232b0a2fdeeSScott Long  */
233b0a2fdeeSScott Long static moduledata_t mpt_core_mod = {
234b0a2fdeeSScott Long 	"mpt_core", mpt_modevent, &mpt_core_personality
235b0a2fdeeSScott Long };
236b0a2fdeeSScott Long DECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
237b0a2fdeeSScott Long MODULE_VERSION(mpt_core, 1);
238b0a2fdeeSScott Long 
239c87e3f83SMatt Jacob #define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
240b0a2fdeeSScott Long 
241b0a2fdeeSScott Long 
242b0a2fdeeSScott Long int
243b0a2fdeeSScott Long mpt_modevent(module_t mod, int type, void *data)
244b0a2fdeeSScott Long {
245b0a2fdeeSScott Long 	struct mpt_personality *pers;
246b0a2fdeeSScott Long 	int error;
247b0a2fdeeSScott Long 
248b0a2fdeeSScott Long 	pers = (struct mpt_personality *)data;
249b0a2fdeeSScott Long 
250b0a2fdeeSScott Long 	error = 0;
251b0a2fdeeSScott Long 	switch (type) {
252b0a2fdeeSScott Long 	case MOD_LOAD:
253b0a2fdeeSScott Long 	{
254b0a2fdeeSScott Long 		mpt_load_handler_t **def_handler;
255b0a2fdeeSScott Long 		mpt_load_handler_t **pers_handler;
256b0a2fdeeSScott Long 		int i;
257b0a2fdeeSScott Long 
258b0a2fdeeSScott Long 		for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
259b0a2fdeeSScott Long 			if (mpt_personalities[i] == NULL)
260b0a2fdeeSScott Long 				break;
261b0a2fdeeSScott Long 		}
262b0a2fdeeSScott Long 		if (i >= MPT_MAX_PERSONALITIES) {
263b0a2fdeeSScott Long 			error = ENOMEM;
264b0a2fdeeSScott Long 			break;
265b0a2fdeeSScott Long 		}
266b0a2fdeeSScott Long 		pers->id = i;
267b0a2fdeeSScott Long 		mpt_personalities[i] = pers;
268b0a2fdeeSScott Long 
269b0a2fdeeSScott Long 		/* Install standard/noop handlers for any NULL entries. */
270b0a2fdeeSScott Long 		def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
271b0a2fdeeSScott Long 		pers_handler = MPT_PERS_FIRST_HANDLER(pers);
272b0a2fdeeSScott Long 		while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
273b0a2fdeeSScott Long 			if (*pers_handler == NULL)
274b0a2fdeeSScott Long 				*pers_handler = *def_handler;
275b0a2fdeeSScott Long 			pers_handler++;
276b0a2fdeeSScott Long 			def_handler++;
277b0a2fdeeSScott Long 		}
278b0a2fdeeSScott Long 
279b0a2fdeeSScott Long 		error = (pers->load(pers));
280b0a2fdeeSScott Long 		if (error != 0)
281b0a2fdeeSScott Long 			mpt_personalities[i] = NULL;
282b0a2fdeeSScott Long 		break;
283b0a2fdeeSScott Long 	}
284b0a2fdeeSScott Long 	case MOD_SHUTDOWN:
285b0a2fdeeSScott Long 		break;
286c87e3f83SMatt Jacob #if __FreeBSD_version >= 500000
287b0a2fdeeSScott Long 	case MOD_QUIESCE:
288b0a2fdeeSScott Long 		break;
289c87e3f83SMatt Jacob #endif
290b0a2fdeeSScott Long 	case MOD_UNLOAD:
291b0a2fdeeSScott Long 		error = pers->unload(pers);
292b0a2fdeeSScott Long 		mpt_personalities[pers->id] = NULL;
293b0a2fdeeSScott Long 		break;
294b0a2fdeeSScott Long 	default:
295b0a2fdeeSScott Long 		error = EINVAL;
296b0a2fdeeSScott Long 		break;
297b0a2fdeeSScott Long 	}
298b0a2fdeeSScott Long 	return (error);
299b0a2fdeeSScott Long }
300b0a2fdeeSScott Long 
301b0a2fdeeSScott Long int
302b0a2fdeeSScott Long mpt_stdload(struct mpt_personality *pers)
303b0a2fdeeSScott Long {
304b0a2fdeeSScott Long 	/* Load is always successfull. */
305b0a2fdeeSScott Long 	return (0);
306b0a2fdeeSScott Long }
307b0a2fdeeSScott Long 
308b0a2fdeeSScott Long int
309b0a2fdeeSScott Long mpt_stdprobe(struct mpt_softc *mpt)
310b0a2fdeeSScott Long {
311b0a2fdeeSScott Long 	/* Probe is always successfull. */
312b0a2fdeeSScott Long 	return (0);
313b0a2fdeeSScott Long }
314b0a2fdeeSScott Long 
315b0a2fdeeSScott Long int
316b0a2fdeeSScott Long mpt_stdattach(struct mpt_softc *mpt)
317b0a2fdeeSScott Long {
318b0a2fdeeSScott Long 	/* Attach is always successfull. */
319b0a2fdeeSScott Long 	return (0);
320b0a2fdeeSScott Long }
321b0a2fdeeSScott Long 
322b0a2fdeeSScott Long int
323c87e3f83SMatt Jacob mpt_stdenable(struct mpt_softc *mpt)
324c87e3f83SMatt Jacob {
325c87e3f83SMatt Jacob 	/* Enable is always successfull. */
326c87e3f83SMatt Jacob 	return (0);
327c87e3f83SMatt Jacob }
328c87e3f83SMatt Jacob 
329c87e3f83SMatt Jacob int
330444dd2b6SMatt Jacob mpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
331b0a2fdeeSScott Long {
332444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
333b0a2fdeeSScott Long 	/* Event was not for us. */
334b0a2fdeeSScott Long 	return (0);
335b0a2fdeeSScott Long }
336b0a2fdeeSScott Long 
337b0a2fdeeSScott Long void
338b0a2fdeeSScott Long mpt_stdreset(struct mpt_softc *mpt, int type)
339b0a2fdeeSScott Long {
340b0a2fdeeSScott Long }
341b0a2fdeeSScott Long 
342b0a2fdeeSScott Long void
343b0a2fdeeSScott Long mpt_stdshutdown(struct mpt_softc *mpt)
344b0a2fdeeSScott Long {
345b0a2fdeeSScott Long }
346b0a2fdeeSScott Long 
347b0a2fdeeSScott Long void
348b0a2fdeeSScott Long mpt_stddetach(struct mpt_softc *mpt)
349b0a2fdeeSScott Long {
350b0a2fdeeSScott Long }
351b0a2fdeeSScott Long 
352b0a2fdeeSScott Long int
353b0a2fdeeSScott Long mpt_stdunload(struct mpt_personality *pers)
354b0a2fdeeSScott Long {
355b0a2fdeeSScott Long 	/* Unload is always successfull. */
356b0a2fdeeSScott Long 	return (0);
357b0a2fdeeSScott Long }
358b0a2fdeeSScott Long 
359b0a2fdeeSScott Long /******************************* Bus DMA Support ******************************/
360b0a2fdeeSScott Long void
361b0a2fdeeSScott Long mpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
362b0a2fdeeSScott Long {
363b0a2fdeeSScott Long 	struct mpt_map_info *map_info;
364b0a2fdeeSScott Long 
365b0a2fdeeSScott Long 	map_info = (struct mpt_map_info *)arg;
366b0a2fdeeSScott Long 	map_info->error = error;
367b0a2fdeeSScott Long 	map_info->phys = segs->ds_addr;
368b0a2fdeeSScott Long }
369b0a2fdeeSScott Long 
370b0a2fdeeSScott Long /**************************** Reply/Event Handling ****************************/
371b0a2fdeeSScott Long int
372b0a2fdeeSScott Long mpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
373b0a2fdeeSScott Long 		     mpt_handler_t handler, uint32_t *phandler_id)
374b0a2fdeeSScott Long {
375b0a2fdeeSScott Long 
376b0a2fdeeSScott Long 	switch (type) {
377b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
378b0a2fdeeSScott Long 	{
379b0a2fdeeSScott Long 		u_int cbi;
380b0a2fdeeSScott Long 		u_int free_cbi;
381b0a2fdeeSScott Long 
382b0a2fdeeSScott Long 		if (phandler_id == NULL)
383b0a2fdeeSScott Long 			return (EINVAL);
384b0a2fdeeSScott Long 
385b0a2fdeeSScott Long 		free_cbi = MPT_HANDLER_ID_NONE;
386b0a2fdeeSScott Long 		for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
387b0a2fdeeSScott Long 			/*
388b0a2fdeeSScott Long 			 * If the same handler is registered multiple
389b0a2fdeeSScott Long 			 * times, don't error out.  Just return the
390b0a2fdeeSScott Long 			 * index of the original registration.
391b0a2fdeeSScott Long 			 */
392b0a2fdeeSScott Long 			if (mpt_reply_handlers[cbi] == handler.reply_handler) {
393b0a2fdeeSScott Long 				*phandler_id = MPT_CBI_TO_HID(cbi);
394b0a2fdeeSScott Long 				return (0);
395b0a2fdeeSScott Long 			}
396b0a2fdeeSScott Long 
397b0a2fdeeSScott Long 			/*
398b0a2fdeeSScott Long 			 * Fill from the front in the hope that
399b0a2fdeeSScott Long 			 * all registered handlers consume only a
400b0a2fdeeSScott Long 			 * single cache line.
401b0a2fdeeSScott Long 			 *
402b0a2fdeeSScott Long 			 * We don't break on the first empty slot so
403b0a2fdeeSScott Long 			 * that the full table is checked to see if
404b0a2fdeeSScott Long 			 * this handler was previously registered.
405b0a2fdeeSScott Long 			 */
406c87e3f83SMatt Jacob 			if (free_cbi == MPT_HANDLER_ID_NONE &&
407c87e3f83SMatt Jacob 			    (mpt_reply_handlers[cbi]
408b0a2fdeeSScott Long 			  == mpt_default_reply_handler))
409b0a2fdeeSScott Long 				free_cbi = cbi;
410b0a2fdeeSScott Long 		}
411c87e3f83SMatt Jacob 		if (free_cbi == MPT_HANDLER_ID_NONE) {
412b0a2fdeeSScott Long 			return (ENOMEM);
413c87e3f83SMatt Jacob 		}
414b0a2fdeeSScott Long 		mpt_reply_handlers[free_cbi] = handler.reply_handler;
415b0a2fdeeSScott Long 		*phandler_id = MPT_CBI_TO_HID(free_cbi);
416b0a2fdeeSScott Long 		break;
417b0a2fdeeSScott Long 	}
418b0a2fdeeSScott Long 	default:
419b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
420b0a2fdeeSScott Long 		return (EINVAL);
421b0a2fdeeSScott Long 	}
422b0a2fdeeSScott Long 	return (0);
423b0a2fdeeSScott Long }
424b0a2fdeeSScott Long 
425b0a2fdeeSScott Long int
426b0a2fdeeSScott Long mpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
427b0a2fdeeSScott Long 		       mpt_handler_t handler, uint32_t handler_id)
428b0a2fdeeSScott Long {
429b0a2fdeeSScott Long 
430b0a2fdeeSScott Long 	switch (type) {
431b0a2fdeeSScott Long 	case MPT_HANDLER_REPLY:
432b0a2fdeeSScott Long 	{
433b0a2fdeeSScott Long 		u_int cbi;
434b0a2fdeeSScott Long 
435b0a2fdeeSScott Long 		cbi = MPT_CBI(handler_id);
436b0a2fdeeSScott Long 		if (cbi >= MPT_NUM_REPLY_HANDLERS
437b0a2fdeeSScott Long 		 || mpt_reply_handlers[cbi] != handler.reply_handler)
438b0a2fdeeSScott Long 			return (ENOENT);
439b0a2fdeeSScott Long 		mpt_reply_handlers[cbi] = mpt_default_reply_handler;
440b0a2fdeeSScott Long 		break;
441b0a2fdeeSScott Long 	}
442b0a2fdeeSScott Long 	default:
443b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
444b0a2fdeeSScott Long 		return (EINVAL);
445b0a2fdeeSScott Long 	}
446b0a2fdeeSScott Long 	return (0);
447b0a2fdeeSScott Long }
448b0a2fdeeSScott Long 
449b0a2fdeeSScott Long static int
450b0a2fdeeSScott Long mpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
451c87e3f83SMatt Jacob 	uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
452b0a2fdeeSScott Long {
453c87e3f83SMatt Jacob 	mpt_prt(mpt,
454c87e3f83SMatt Jacob 	    "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
455c87e3f83SMatt Jacob 	    req, req->serno, reply_desc, reply_frame);
456b0a2fdeeSScott Long 
457b0a2fdeeSScott Long 	if (reply_frame != NULL)
458b0a2fdeeSScott Long 		mpt_dump_reply_frame(mpt, reply_frame);
459b0a2fdeeSScott Long 
460c87e3f83SMatt Jacob 	mpt_prt(mpt, "Reply Frame Ignored\n");
461b0a2fdeeSScott Long 
462b0a2fdeeSScott Long 	return (/*free_reply*/TRUE);
463b0a2fdeeSScott Long }
464b0a2fdeeSScott Long 
465b0a2fdeeSScott Long static int
466b0a2fdeeSScott Long mpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
467c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
468b0a2fdeeSScott Long {
469b0a2fdeeSScott Long 	if (req != NULL) {
470b0a2fdeeSScott Long 
471b0a2fdeeSScott Long 		if (reply_frame != NULL) {
472b0a2fdeeSScott Long 			MSG_CONFIG *cfgp;
473b0a2fdeeSScott Long 			MSG_CONFIG_REPLY *reply;
474b0a2fdeeSScott Long 
475b0a2fdeeSScott Long 			cfgp = (MSG_CONFIG *)req->req_vbuf;
476b0a2fdeeSScott Long 			reply = (MSG_CONFIG_REPLY *)reply_frame;
477b0a2fdeeSScott Long 			req->IOCStatus = le16toh(reply_frame->IOCStatus);
478b0a2fdeeSScott Long 			bcopy(&reply->Header, &cfgp->Header,
479b0a2fdeeSScott Long 			      sizeof(cfgp->Header));
480b0a2fdeeSScott Long 		}
481b0a2fdeeSScott Long 		req->state &= ~REQ_STATE_QUEUED;
482b0a2fdeeSScott Long 		req->state |= REQ_STATE_DONE;
483b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
4845e073106SMatt Jacob 		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
485b0a2fdeeSScott Long 			wakeup(req);
4866621d786SMatt Jacob 		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
4876621d786SMatt Jacob 			/*
4886621d786SMatt Jacob 			 * Whew- we can free this request (late completion)
4896621d786SMatt Jacob 			 */
4906621d786SMatt Jacob 			mpt_free_request(mpt, req);
491b0a2fdeeSScott Long 		}
4925e073106SMatt Jacob 	}
493b0a2fdeeSScott Long 
4945e073106SMatt Jacob 	return (TRUE);
495b0a2fdeeSScott Long }
496b0a2fdeeSScott Long 
497b0a2fdeeSScott Long static int
498b0a2fdeeSScott Long mpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
499c87e3f83SMatt Jacob  uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
500b0a2fdeeSScott Long {
501b0a2fdeeSScott Long 	/* Nothing to be done. */
5025e073106SMatt Jacob 	return (TRUE);
503b0a2fdeeSScott Long }
504b0a2fdeeSScott Long 
505b0a2fdeeSScott Long static int
506b0a2fdeeSScott Long mpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
507c87e3f83SMatt Jacob     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
508b0a2fdeeSScott Long {
509b0a2fdeeSScott Long 	int free_reply;
510b0a2fdeeSScott Long 
5115e073106SMatt Jacob 	KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
5125e073106SMatt Jacob 	KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
513b0a2fdeeSScott Long 
514b0a2fdeeSScott Long 	free_reply = TRUE;
515b0a2fdeeSScott Long 	switch (reply_frame->Function) {
516b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_NOTIFICATION:
517b0a2fdeeSScott Long 	{
518b0a2fdeeSScott Long 		MSG_EVENT_NOTIFY_REPLY *msg;
519b0a2fdeeSScott Long 		struct mpt_personality *pers;
520b0a2fdeeSScott Long 		u_int handled;
521b0a2fdeeSScott Long 
522b0a2fdeeSScott Long 		handled = 0;
523b0a2fdeeSScott Long 		msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
524b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
525b0a2fdeeSScott Long 			handled += pers->event(mpt, req, msg);
526b0a2fdeeSScott Long 
527444dd2b6SMatt Jacob 		if (handled == 0 && mpt->mpt_pers_mask == 0) {
5281977cbd6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_INFO,
529444dd2b6SMatt Jacob 				"No Handlers For Any Event Notify Frames. "
530444dd2b6SMatt Jacob 				"Event %#x (ACK %sequired).\n",
531444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
532444dd2b6SMatt Jacob 		} else if (handled == 0) {
5331977cbd6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_WARN,
534444dd2b6SMatt Jacob 				"Unhandled Event Notify Frame. Event %#x "
535444dd2b6SMatt Jacob 				"(ACK %sequired).\n",
536444dd2b6SMatt Jacob 				msg->Event, msg->AckRequired? "r" : "not r");
537444dd2b6SMatt Jacob 		}
538b0a2fdeeSScott Long 
539b0a2fdeeSScott Long 		if (msg->AckRequired) {
540b0a2fdeeSScott Long 			request_t *ack_req;
541b0a2fdeeSScott Long 			uint32_t context;
542b0a2fdeeSScott Long 
543b0a2fdeeSScott Long 			context = htole32(req->index|MPT_REPLY_HANDLER_EVENTS);
5445e073106SMatt Jacob 			ack_req = mpt_get_request(mpt, FALSE);
545b0a2fdeeSScott Long 			if (ack_req == NULL) {
546b0a2fdeeSScott Long 				struct mpt_evtf_record *evtf;
547b0a2fdeeSScott Long 
548b0a2fdeeSScott Long 				evtf = (struct mpt_evtf_record *)reply_frame;
549b0a2fdeeSScott Long 				evtf->context = context;
550b0a2fdeeSScott Long 				LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
551b0a2fdeeSScott Long 				free_reply = FALSE;
552b0a2fdeeSScott Long 				break;
553b0a2fdeeSScott Long 			}
554b0a2fdeeSScott Long 			mpt_send_event_ack(mpt, ack_req, msg, context);
5555e073106SMatt Jacob 			/*
5565e073106SMatt Jacob 			 * Don't check for CONTINUATION_REPLY here
5575e073106SMatt Jacob 			 */
5585e073106SMatt Jacob 			return (free_reply);
559b0a2fdeeSScott Long 		}
560b0a2fdeeSScott Long 		break;
561b0a2fdeeSScott Long 	}
562b0a2fdeeSScott Long 	case MPI_FUNCTION_PORT_ENABLE:
563b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
564b0a2fdeeSScott Long 		break;
565b0a2fdeeSScott Long 	case MPI_FUNCTION_EVENT_ACK:
566b0a2fdeeSScott Long 		break;
567b0a2fdeeSScott Long 	default:
5685e073106SMatt Jacob 		mpt_prt(mpt, "unknown event function: %x\n",
569b0a2fdeeSScott Long 			reply_frame->Function);
570b0a2fdeeSScott Long 		break;
571b0a2fdeeSScott Long 	}
572b0a2fdeeSScott Long 
5735e073106SMatt Jacob 	/*
5745e073106SMatt Jacob 	 * I'm not sure that this continuation stuff works as it should.
5755e073106SMatt Jacob 	 *
5765e073106SMatt Jacob 	 * I've had FC async events occur that free the frame up because
5775e073106SMatt Jacob 	 * the continuation bit isn't set, and then additional async events
5785e073106SMatt Jacob 	 * then occur using the same context. As you might imagine, this
5795e073106SMatt Jacob 	 * leads to Very Bad Thing.
5805e073106SMatt Jacob 	 *
5815e073106SMatt Jacob 	 *  Let's just be safe for now and not free them up until we figure
5825e073106SMatt Jacob 	 * out what's actually happening here.
5835e073106SMatt Jacob 	 */
5845e073106SMatt Jacob #if	0
5855e073106SMatt Jacob 	if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
586b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
587b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
5885e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
5895e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
5905e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
5915e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
5925e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
5935e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
5945e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
595b0a2fdeeSScott Long 		}
5965e073106SMatt Jacob 	} else {
5975e073106SMatt Jacob 		mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
5985e073106SMatt Jacob 		    reply_frame->Function, req, req->serno);
5995e073106SMatt Jacob 		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
6005e073106SMatt Jacob 			MSG_EVENT_NOTIFY_REPLY *msg =
6015e073106SMatt Jacob 			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
6025e073106SMatt Jacob 			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
6035e073106SMatt Jacob 			    msg->Event, msg->AckRequired);
6045e073106SMatt Jacob 		}
6055e073106SMatt Jacob 		mpt_prtc(mpt, "\n");
6065e073106SMatt Jacob 	}
6075e073106SMatt Jacob #endif
608b0a2fdeeSScott Long 	return (free_reply);
609b0a2fdeeSScott Long }
610b0a2fdeeSScott Long 
611b0a2fdeeSScott Long /*
612b0a2fdeeSScott Long  * Process an asynchronous event from the IOC.
613b0a2fdeeSScott Long  */
614b0a2fdeeSScott Long static int
615b0a2fdeeSScott Long mpt_core_event(struct mpt_softc *mpt, request_t *req,
616b0a2fdeeSScott Long 	       MSG_EVENT_NOTIFY_REPLY *msg)
617b0a2fdeeSScott Long {
618444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
619444dd2b6SMatt Jacob                  msg->Event & 0xFF);
620b0a2fdeeSScott Long 	switch(msg->Event & 0xFF) {
621b0a2fdeeSScott Long 	case MPI_EVENT_NONE:
622b0a2fdeeSScott Long 		break;
623b0a2fdeeSScott Long 	case MPI_EVENT_LOG_DATA:
624b0a2fdeeSScott Long 	{
625b0a2fdeeSScott Long 		int i;
626b0a2fdeeSScott Long 
627b0a2fdeeSScott Long 		/* Some error occured that LSI wants logged */
628b0a2fdeeSScott Long 		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
629b0a2fdeeSScott Long 			msg->IOCLogInfo);
630b0a2fdeeSScott Long 		mpt_prt(mpt, "\tEvtLogData: Event Data:");
631b0a2fdeeSScott Long 		for (i = 0; i < msg->EventDataLength; i++)
632b0a2fdeeSScott Long 			mpt_prtc(mpt, "  %08x", msg->Data[i]);
633b0a2fdeeSScott Long 		mpt_prtc(mpt, "\n");
634b0a2fdeeSScott Long 		break;
635b0a2fdeeSScott Long 	}
636b0a2fdeeSScott Long 	case MPI_EVENT_EVENT_CHANGE:
637b0a2fdeeSScott Long 		/*
638b0a2fdeeSScott Long 		 * This is just an acknowledgement
639b0a2fdeeSScott Long 		 * of our mpt_send_event_request.
640b0a2fdeeSScott Long 		 */
641b0a2fdeeSScott Long 		break;
642444dd2b6SMatt Jacob 	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
643444dd2b6SMatt Jacob 		break;
644b0a2fdeeSScott Long 	default:
6455e073106SMatt Jacob 		return (0);
646b0a2fdeeSScott Long 		break;
647b0a2fdeeSScott Long 	}
6485e073106SMatt Jacob 	return (1);
649b0a2fdeeSScott Long }
650b0a2fdeeSScott Long 
651b0a2fdeeSScott Long static void
652b0a2fdeeSScott Long mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
653b0a2fdeeSScott Long 		   MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
654b0a2fdeeSScott Long {
655b0a2fdeeSScott Long 	MSG_EVENT_ACK *ackp;
656b0a2fdeeSScott Long 
657b0a2fdeeSScott Long 	ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
6585e073106SMatt Jacob 	memset(ackp, 0, sizeof (*ackp));
659b0a2fdeeSScott Long 	ackp->Function = MPI_FUNCTION_EVENT_ACK;
660b0a2fdeeSScott Long 	ackp->Event = msg->Event;
661b0a2fdeeSScott Long 	ackp->EventContext = msg->EventContext;
662b0a2fdeeSScott Long 	ackp->MsgContext = context;
663b0a2fdeeSScott Long 	mpt_check_doorbell(mpt);
664b0a2fdeeSScott Long 	mpt_send_cmd(mpt, ack_req);
665b0a2fdeeSScott Long }
666b0a2fdeeSScott Long 
667b0a2fdeeSScott Long /***************************** Interrupt Handling *****************************/
668b0a2fdeeSScott Long void
669b0a2fdeeSScott Long mpt_intr(void *arg)
670b0a2fdeeSScott Long {
671b0a2fdeeSScott Long 	struct mpt_softc *mpt;
672b0a2fdeeSScott Long 	uint32_t reply_desc;
673c87e3f83SMatt Jacob 	int ntrips = 0;
674b0a2fdeeSScott Long 
675b0a2fdeeSScott Long 	mpt = (struct mpt_softc *)arg;
6765089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
677b0a2fdeeSScott Long 	while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
678b0a2fdeeSScott Long 		request_t	  *req;
679b0a2fdeeSScott Long 		MSG_DEFAULT_REPLY *reply_frame;
680b0a2fdeeSScott Long 		uint32_t	   reply_baddr;
681c87e3f83SMatt Jacob 		uint32_t           ctxt_idx;
682b0a2fdeeSScott Long 		u_int		   cb_index;
683b0a2fdeeSScott Long 		u_int		   req_index;
684b0a2fdeeSScott Long 		int		   free_rf;
685b0a2fdeeSScott Long 
686b0a2fdeeSScott Long 		req = NULL;
687b0a2fdeeSScott Long 		reply_frame = NULL;
688b0a2fdeeSScott Long 		reply_baddr = 0;
689b0a2fdeeSScott Long 		if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
690b0a2fdeeSScott Long 			u_int offset;
691b0a2fdeeSScott Long 			/*
692b0a2fdeeSScott Long 			 * Insure that the reply frame is coherent.
693b0a2fdeeSScott Long 			 */
6945e073106SMatt Jacob 			reply_baddr = MPT_REPLY_BADDR(reply_desc);
695b0a2fdeeSScott Long 			offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
696c87e3f83SMatt Jacob 			bus_dmamap_sync_range(mpt->reply_dmat,
697c87e3f83SMatt Jacob 			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
698b0a2fdeeSScott Long 			    BUS_DMASYNC_POSTREAD);
699b0a2fdeeSScott Long 			reply_frame = MPT_REPLY_OTOV(mpt, offset);
700c87e3f83SMatt Jacob 			ctxt_idx = le32toh(reply_frame->MsgContext);
701c87e3f83SMatt Jacob 		} else {
702c87e3f83SMatt Jacob 			uint32_t type;
703b0a2fdeeSScott Long 
704c87e3f83SMatt Jacob 			type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
705c87e3f83SMatt Jacob 			ctxt_idx = reply_desc;
706c87e3f83SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
707c87e3f83SMatt Jacob 				    reply_desc);
708c87e3f83SMatt Jacob 
709c87e3f83SMatt Jacob 			switch (type) {
710c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
711c87e3f83SMatt Jacob 				ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
712c87e3f83SMatt Jacob 				break;
713c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
714c87e3f83SMatt Jacob 				ctxt_idx = GET_IO_INDEX(reply_desc);
715c87e3f83SMatt Jacob 				if (mpt->tgt_cmd_ptrs == NULL) {
716c87e3f83SMatt Jacob 					mpt_prt(mpt,
717c87e3f83SMatt Jacob 					    "mpt_intr: no target cmd ptrs\n");
718c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
719c87e3f83SMatt Jacob 					break;
720c87e3f83SMatt Jacob 				}
721c87e3f83SMatt Jacob 				if (ctxt_idx >= mpt->tgt_cmds_allocated) {
722c87e3f83SMatt Jacob 					mpt_prt(mpt,
723c87e3f83SMatt Jacob 					    "mpt_intr: bad tgt cmd ctxt %u\n",
724c87e3f83SMatt Jacob 					    ctxt_idx);
725c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
726c87e3f83SMatt Jacob 					ntrips = 1000;
727c87e3f83SMatt Jacob 					break;
728c87e3f83SMatt Jacob 				}
729c87e3f83SMatt Jacob 				req = mpt->tgt_cmd_ptrs[ctxt_idx];
730c87e3f83SMatt Jacob 				if (req == NULL) {
731c87e3f83SMatt Jacob 					mpt_prt(mpt, "no request backpointer "
732c87e3f83SMatt Jacob 					    "at index %u", ctxt_idx);
733c87e3f83SMatt Jacob 					reply_desc = MPT_REPLY_EMPTY;
734c87e3f83SMatt Jacob 					ntrips = 1000;
735c87e3f83SMatt Jacob 					break;
736c87e3f83SMatt Jacob 				}
737c87e3f83SMatt Jacob 				/*
738c87e3f83SMatt Jacob 				 * Reformulate ctxt_idx to be just as if
739c87e3f83SMatt Jacob 				 * it were another type of context reply
740c87e3f83SMatt Jacob 				 * so the code below will find the request
741c87e3f83SMatt Jacob 				 * via indexing into the pool.
742c87e3f83SMatt Jacob 				 */
743c87e3f83SMatt Jacob 				ctxt_idx =
744c87e3f83SMatt Jacob 				    req->index | mpt->scsi_tgt_handler_id;
745c87e3f83SMatt Jacob 				req = NULL;
746c87e3f83SMatt Jacob 				break;
747c87e3f83SMatt Jacob 			case MPI_CONTEXT_REPLY_TYPE_LAN:
748c87e3f83SMatt Jacob 				mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
749c87e3f83SMatt Jacob 				    reply_desc);
750c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
751c87e3f83SMatt Jacob 				break;
752c87e3f83SMatt Jacob 			default:
753c87e3f83SMatt Jacob 				mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
754c87e3f83SMatt Jacob 				reply_desc = MPT_REPLY_EMPTY;
755c87e3f83SMatt Jacob 				break;
756c87e3f83SMatt Jacob 			}
757c87e3f83SMatt Jacob 			if (reply_desc == MPT_REPLY_EMPTY) {
758c87e3f83SMatt Jacob 				if (ntrips++ > 1000) {
759c87e3f83SMatt Jacob 					break;
760c87e3f83SMatt Jacob 				}
761c87e3f83SMatt Jacob 				continue;
762c87e3f83SMatt Jacob 			}
763c87e3f83SMatt Jacob 		}
764c87e3f83SMatt Jacob 
765c87e3f83SMatt Jacob 		cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
766c87e3f83SMatt Jacob 		req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
767c87e3f83SMatt Jacob 		if (req_index < MPT_MAX_REQUESTS(mpt)) {
768c87e3f83SMatt Jacob 			req = &mpt->request_pool[req_index];
7695e073106SMatt Jacob 		} else {
7705e073106SMatt Jacob 			mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
7715e073106SMatt Jacob 			    " 0x%x)\n", req_index, reply_desc);
772c87e3f83SMatt Jacob 		}
773c87e3f83SMatt Jacob 
774c87e3f83SMatt Jacob 		free_rf = mpt_reply_handlers[cb_index](mpt, req,
775c87e3f83SMatt Jacob 		    reply_desc, reply_frame);
776b0a2fdeeSScott Long 
7775e073106SMatt Jacob 		if (reply_frame != NULL && free_rf) {
778b0a2fdeeSScott Long 			mpt_free_reply(mpt, reply_baddr);
7795e073106SMatt Jacob 		}
780c87e3f83SMatt Jacob 
781c87e3f83SMatt Jacob 		/*
782c87e3f83SMatt Jacob 		 * If we got ourselves disabled, don't get stuck in a loop
783c87e3f83SMatt Jacob 		 */
784c87e3f83SMatt Jacob 		if (mpt->disabled) {
785c87e3f83SMatt Jacob 			mpt_disable_ints(mpt);
786c87e3f83SMatt Jacob 			break;
787c87e3f83SMatt Jacob 		}
788c87e3f83SMatt Jacob 		if (ntrips++ > 1000) {
789c87e3f83SMatt Jacob 			break;
790c87e3f83SMatt Jacob 		}
791b0a2fdeeSScott Long 	}
7925089bd63SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
793b0a2fdeeSScott Long }
794b0a2fdeeSScott Long 
795b0a2fdeeSScott Long /******************************* Error Recovery *******************************/
796b0a2fdeeSScott Long void
797b0a2fdeeSScott Long mpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
798b0a2fdeeSScott Long 			    u_int iocstatus)
799b0a2fdeeSScott Long {
800b0a2fdeeSScott Long 	MSG_DEFAULT_REPLY  ioc_status_frame;
801b0a2fdeeSScott Long 	request_t	  *req;
802b0a2fdeeSScott Long 
8035e073106SMatt Jacob 	memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
804b0a2fdeeSScott Long 	ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
805b0a2fdeeSScott Long 	ioc_status_frame.IOCStatus = iocstatus;
806b0a2fdeeSScott Long 	while((req = TAILQ_FIRST(chain)) != NULL) {
807b0a2fdeeSScott Long 		MSG_REQUEST_HEADER *msg_hdr;
808b0a2fdeeSScott Long 		u_int		    cb_index;
8095e073106SMatt Jacob 
8109b7de735SMatt Jacob 		TAILQ_REMOVE(chain, req, links);
811b0a2fdeeSScott Long 		msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
812b0a2fdeeSScott Long 		ioc_status_frame.Function = msg_hdr->Function;
813b0a2fdeeSScott Long 		ioc_status_frame.MsgContext = msg_hdr->MsgContext;
814b0a2fdeeSScott Long 		cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
815c87e3f83SMatt Jacob 		mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
816c87e3f83SMatt Jacob 		    &ioc_status_frame);
817b0a2fdeeSScott Long 	}
818b0a2fdeeSScott Long }
819b0a2fdeeSScott Long 
820b0a2fdeeSScott Long /********************************* Diagnostics ********************************/
821b0a2fdeeSScott Long /*
822b0a2fdeeSScott Long  * Perform a diagnostic dump of a reply frame.
823b0a2fdeeSScott Long  */
824b0a2fdeeSScott Long void
825b0a2fdeeSScott Long mpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
826b0a2fdeeSScott Long {
827b0a2fdeeSScott Long 	mpt_prt(mpt, "Address Reply:\n");
828b0a2fdeeSScott Long 	mpt_print_reply(reply_frame);
829b0a2fdeeSScott Long }
830b0a2fdeeSScott Long 
831b0a2fdeeSScott Long /******************************* Doorbell Access ******************************/
832b0a2fdeeSScott Long static __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
833b0a2fdeeSScott Long static __inline  uint32_t mpt_rd_intr(struct mpt_softc *mpt);
834b0a2fdeeSScott Long 
835b0a2fdeeSScott Long static __inline uint32_t
836b0a2fdeeSScott Long mpt_rd_db(struct mpt_softc *mpt)
8379b631363SMatt Jacob {
8389b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_DOORBELL);
8399b631363SMatt Jacob }
8409b631363SMatt Jacob 
841b0a2fdeeSScott Long static __inline uint32_t
842b0a2fdeeSScott Long mpt_rd_intr(struct mpt_softc *mpt)
8439b631363SMatt Jacob {
8449b631363SMatt Jacob 	return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
8459b631363SMatt Jacob }
8469b631363SMatt Jacob 
8479b631363SMatt Jacob /* Busy wait for a door bell to be read by IOC */
8489b631363SMatt Jacob static int
849b0a2fdeeSScott Long mpt_wait_db_ack(struct mpt_softc *mpt)
8509b631363SMatt Jacob {
8519b631363SMatt Jacob 	int i;
8529b631363SMatt Jacob 	for (i=0; i < MPT_MAX_WAIT; i++) {
8539b631363SMatt Jacob 		if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
8549b631363SMatt Jacob 			maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
855c87e3f83SMatt Jacob 			return (MPT_OK);
8569b631363SMatt Jacob 		}
857c87e3f83SMatt Jacob 		DELAY(200);
8589b631363SMatt Jacob 	}
859c87e3f83SMatt Jacob 	return (MPT_FAIL);
8609b631363SMatt Jacob }
8619b631363SMatt Jacob 
8629b631363SMatt Jacob /* Busy wait for a door bell interrupt */
8639b631363SMatt Jacob static int
864b0a2fdeeSScott Long mpt_wait_db_int(struct mpt_softc *mpt)
8659b631363SMatt Jacob {
8669b631363SMatt Jacob 	int i;
8679b631363SMatt Jacob 	for (i=0; i < MPT_MAX_WAIT; i++) {
8689b631363SMatt Jacob 		if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
8699b631363SMatt Jacob 			maxwait_int = i > maxwait_int ? i : maxwait_int;
8709b631363SMatt Jacob 			return MPT_OK;
8719b631363SMatt Jacob 		}
8729b631363SMatt Jacob 		DELAY(100);
8739b631363SMatt Jacob 	}
874c87e3f83SMatt Jacob 	return (MPT_FAIL);
8759b631363SMatt Jacob }
8769b631363SMatt Jacob 
8779b631363SMatt Jacob /* Wait for IOC to transition to a give state */
8789b631363SMatt Jacob void
879b0a2fdeeSScott Long mpt_check_doorbell(struct mpt_softc *mpt)
8809b631363SMatt Jacob {
881b0a2fdeeSScott Long 	uint32_t db = mpt_rd_db(mpt);
8829b631363SMatt Jacob 	if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
883b0a2fdeeSScott Long 		mpt_prt(mpt, "Device not running\n");
8849b631363SMatt Jacob 		mpt_print_db(db);
8859b631363SMatt Jacob 	}
8869b631363SMatt Jacob }
8879b631363SMatt Jacob 
8889b631363SMatt Jacob /* Wait for IOC to transition to a give state */
8899b631363SMatt Jacob static int
890b0a2fdeeSScott Long mpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
8919b631363SMatt Jacob {
8929b631363SMatt Jacob 	int i;
8939b631363SMatt Jacob 
8949b631363SMatt Jacob 	for (i = 0; i < MPT_MAX_WAIT; i++) {
895b0a2fdeeSScott Long 		uint32_t db = mpt_rd_db(mpt);
8969b631363SMatt Jacob 		if (MPT_STATE(db) == state) {
8979b631363SMatt Jacob 			maxwait_state = i > maxwait_state ? i : maxwait_state;
8989b631363SMatt Jacob 			return (MPT_OK);
8999b631363SMatt Jacob 		}
9009b631363SMatt Jacob 		DELAY(100);
9019b631363SMatt Jacob 	}
9029b631363SMatt Jacob 	return (MPT_FAIL);
9039b631363SMatt Jacob }
9049b631363SMatt Jacob 
9059b631363SMatt Jacob 
906b0a2fdeeSScott Long /************************* Intialization/Configuration ************************/
907b0a2fdeeSScott Long static int mpt_download_fw(struct mpt_softc *mpt);
908b0a2fdeeSScott Long 
9099b631363SMatt Jacob /* Issue the reset COMMAND to the IOC */
910b0a2fdeeSScott Long static int
911b0a2fdeeSScott Long mpt_soft_reset(struct mpt_softc *mpt)
9129b631363SMatt Jacob {
913b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
9149b631363SMatt Jacob 
9159b631363SMatt Jacob 	/* Have to use hard reset if we are not in Running state */
9169b631363SMatt Jacob 	if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
917b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device not running\n");
918c87e3f83SMatt Jacob 		return (MPT_FAIL);
9199b631363SMatt Jacob 	}
9209b631363SMatt Jacob 
9219b631363SMatt Jacob 	/* If door bell is in use we don't have a chance of getting
9229b631363SMatt Jacob 	 * a word in since the IOC probably crashed in message
9239b631363SMatt Jacob 	 * processing. So don't waste our time.
9249b631363SMatt Jacob 	 */
9259b631363SMatt Jacob 	if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
926b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
927c87e3f83SMatt Jacob 		return (MPT_FAIL);
9289b631363SMatt Jacob 	}
9299b631363SMatt Jacob 
9309b631363SMatt Jacob 	/* Send the reset request to the IOC */
9319b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL,
9329b631363SMatt Jacob 	    MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
9339b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
934b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: ack timeout\n");
935c87e3f83SMatt Jacob 		return (MPT_FAIL);
9369b631363SMatt Jacob 	}
9379b631363SMatt Jacob 
9389b631363SMatt Jacob 	/* Wait for the IOC to reload and come out of reset state */
9399b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
940b0a2fdeeSScott Long 		mpt_prt(mpt, "soft reset failed: device did not restart\n");
941c87e3f83SMatt Jacob 		return (MPT_FAIL);
9429b631363SMatt Jacob 	}
9439b631363SMatt Jacob 
9449b631363SMatt Jacob 	return MPT_OK;
9459b631363SMatt Jacob }
9469b631363SMatt Jacob 
947b0a2fdeeSScott Long static int
948b0a2fdeeSScott Long mpt_enable_diag_mode(struct mpt_softc *mpt)
949b0a2fdeeSScott Long {
950b0a2fdeeSScott Long 	int try;
951b0a2fdeeSScott Long 
952b0a2fdeeSScott Long 	try = 20;
953b0a2fdeeSScott Long 	while (--try) {
954b0a2fdeeSScott Long 
955b0a2fdeeSScott Long 		if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
956b0a2fdeeSScott Long 			break;
957b0a2fdeeSScott Long 
958b0a2fdeeSScott Long 		/* Enable diagnostic registers */
959b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
960b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
961b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
962b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
963b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
964b0a2fdeeSScott Long 		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
965b0a2fdeeSScott Long 
966b0a2fdeeSScott Long 		DELAY(100000);
967b0a2fdeeSScott Long 	}
968b0a2fdeeSScott Long 	if (try == 0)
969b0a2fdeeSScott Long 		return (EIO);
970b0a2fdeeSScott Long 	return (0);
971b0a2fdeeSScott Long }
972b0a2fdeeSScott Long 
973b0a2fdeeSScott Long static void
974b0a2fdeeSScott Long mpt_disable_diag_mode(struct mpt_softc *mpt)
975b0a2fdeeSScott Long {
976b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
977b0a2fdeeSScott Long }
978b0a2fdeeSScott Long 
9799b631363SMatt Jacob /* This is a magic diagnostic reset that resets all the ARM
9809b631363SMatt Jacob  * processors in the chip.
9819b631363SMatt Jacob  */
982b0a2fdeeSScott Long static void
983b0a2fdeeSScott Long mpt_hard_reset(struct mpt_softc *mpt)
9849b631363SMatt Jacob {
985b0a2fdeeSScott Long 	int error;
986b0a2fdeeSScott Long 	int wait;
987b0a2fdeeSScott Long 	uint32_t diagreg;
9889b631363SMatt Jacob 
989b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
990b0a2fdeeSScott Long 
991b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
992b0a2fdeeSScott Long 	if (error) {
993b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
994b0a2fdeeSScott Long 		mpt_prt(mpt, "Trying to reset anyway.\n");
995b0a2fdeeSScott Long 	}
996b0a2fdeeSScott Long 
997b0a2fdeeSScott Long 	diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
998b0a2fdeeSScott Long 
999b0a2fdeeSScott Long 	/*
1000b0a2fdeeSScott Long 	 * This appears to be a workaround required for some
1001b0a2fdeeSScott Long 	 * firmware or hardware revs.
1002b0a2fdeeSScott Long 	 */
1003b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1004b0a2fdeeSScott Long 	DELAY(1000);
10059b631363SMatt Jacob 
10069b631363SMatt Jacob 	/* Diag. port is now active so we can now hit the reset bit */
1007b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
10089b631363SMatt Jacob 
1009b0a2fdeeSScott Long         /*
1010b0a2fdeeSScott Long          * Ensure that the reset has finished.  We delay 1ms
1011b0a2fdeeSScott Long          * prior to reading the register to make sure the chip
1012b0a2fdeeSScott Long          * has sufficiently completed its reset to handle register
1013b0a2fdeeSScott Long          * accesses.
1014b0a2fdeeSScott Long          */
1015b0a2fdeeSScott Long 	wait = 5000;
1016b0a2fdeeSScott Long 	do {
1017b0a2fdeeSScott Long 		DELAY(1000);
1018b0a2fdeeSScott Long 		diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1019b0a2fdeeSScott Long 	} while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
10209b631363SMatt Jacob 
1021b0a2fdeeSScott Long 	if (wait == 0) {
1022b0a2fdeeSScott Long 		mpt_prt(mpt, "WARNING - Failed hard reset! "
1023b0a2fdeeSScott Long 			"Trying to initialize anyway.\n");
10249b631363SMatt Jacob 	}
10259b631363SMatt Jacob 
1026b0a2fdeeSScott Long 	/*
1027b0a2fdeeSScott Long 	 * If we have firmware to download, it must be loaded before
1028b0a2fdeeSScott Long 	 * the controller will become operational.  Do so now.
1029b0a2fdeeSScott Long 	 */
1030b0a2fdeeSScott Long 	if (mpt->fw_image != NULL) {
1031b0a2fdeeSScott Long 
1032b0a2fdeeSScott Long 		error = mpt_download_fw(mpt);
1033b0a2fdeeSScott Long 
1034b0a2fdeeSScott Long 		if (error) {
1035b0a2fdeeSScott Long 			mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1036b0a2fdeeSScott Long 			mpt_prt(mpt, "Trying to initialize anyway.\n");
10379b631363SMatt Jacob 		}
1038b0a2fdeeSScott Long 	}
1039b0a2fdeeSScott Long 
1040b0a2fdeeSScott Long 	/*
1041b0a2fdeeSScott Long 	 * Reseting the controller should have disabled write
1042b0a2fdeeSScott Long 	 * access to the diagnostic registers, but disable
1043b0a2fdeeSScott Long 	 * manually to be sure.
1044b0a2fdeeSScott Long 	 */
1045b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
1046b0a2fdeeSScott Long }
1047b0a2fdeeSScott Long 
1048b0a2fdeeSScott Long static void
1049b0a2fdeeSScott Long mpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1050b0a2fdeeSScott Long {
1051b0a2fdeeSScott Long 	/*
1052b0a2fdeeSScott Long 	 * Complete all pending requests with a status
1053b0a2fdeeSScott Long 	 * appropriate for an IOC reset.
1054b0a2fdeeSScott Long 	 */
1055b0a2fdeeSScott Long 	mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1056b0a2fdeeSScott Long 				   MPI_IOCSTATUS_INVALID_STATE);
1057b0a2fdeeSScott Long }
1058b0a2fdeeSScott Long 
10599b631363SMatt Jacob 
10609b631363SMatt Jacob /*
10619b631363SMatt Jacob  * Reset the IOC when needed. Try software command first then if needed
10629b631363SMatt Jacob  * poke at the magic diagnostic reset. Note that a hard reset resets
10639b631363SMatt Jacob  * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
10649b631363SMatt Jacob  * fouls up the PCI configuration registers.
10659b631363SMatt Jacob  */
10669b631363SMatt Jacob int
1067b0a2fdeeSScott Long mpt_reset(struct mpt_softc *mpt, int reinit)
10689b631363SMatt Jacob {
1069b0a2fdeeSScott Long 	struct	mpt_personality *pers;
10709b631363SMatt Jacob 	int	ret;
107129ae59edSMatt Jacob 	int	retry_cnt = 0;
10729b631363SMatt Jacob 
107329ae59edSMatt Jacob 	/*
107429ae59edSMatt Jacob 	 * Try a soft reset. If that fails, get out the big hammer.
107529ae59edSMatt Jacob 	 */
107629ae59edSMatt Jacob  again:
10779b631363SMatt Jacob 	if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
107829ae59edSMatt Jacob 		int	cnt;
107929ae59edSMatt Jacob 		for (cnt = 0; cnt < 5; cnt++) {
10809b631363SMatt Jacob 			/* Failed; do a hard reset */
10819b631363SMatt Jacob 			mpt_hard_reset(mpt);
10829b631363SMatt Jacob 
108329ae59edSMatt Jacob 			/*
108429ae59edSMatt Jacob 			 * Wait for the IOC to reload
108529ae59edSMatt Jacob 			 * and come out of reset state
108629ae59edSMatt Jacob 			 */
10879b631363SMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
108829ae59edSMatt Jacob 			if (ret == MPT_OK) {
108929ae59edSMatt Jacob 				break;
109029ae59edSMatt Jacob 			}
109129ae59edSMatt Jacob 			/*
109229ae59edSMatt Jacob 			 * Okay- try to check again...
109329ae59edSMatt Jacob 			 */
109429ae59edSMatt Jacob 			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
109529ae59edSMatt Jacob 			if (ret == MPT_OK) {
109629ae59edSMatt Jacob 				break;
109729ae59edSMatt Jacob 			}
109829ae59edSMatt Jacob 			mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
109929ae59edSMatt Jacob 			    retry_cnt, cnt);
110029ae59edSMatt Jacob 		}
11019b631363SMatt Jacob 	}
1102b0a2fdeeSScott Long 
110329ae59edSMatt Jacob 	if (retry_cnt == 0) {
1104b0a2fdeeSScott Long 		/*
1105b0a2fdeeSScott Long 		 * Invoke reset handlers.  We bump the reset count so
1106b0a2fdeeSScott Long 		 * that mpt_wait_req() understands that regardless of
1107b0a2fdeeSScott Long 		 * the specified wait condition, it should stop its wait.
1108b0a2fdeeSScott Long 		 */
1109b0a2fdeeSScott Long 		mpt->reset_cnt++;
1110b0a2fdeeSScott Long 		MPT_PERS_FOREACH(mpt, pers)
1111b0a2fdeeSScott Long 			pers->reset(mpt, ret);
111229ae59edSMatt Jacob 	}
1113b0a2fdeeSScott Long 
11145e073106SMatt Jacob 	if (reinit) {
1115c87e3f83SMatt Jacob 		ret = mpt_enable_ioc(mpt, 1);
111629ae59edSMatt Jacob 		if (ret == MPT_OK) {
111729ae59edSMatt Jacob 			mpt_enable_ints(mpt);
111829ae59edSMatt Jacob 		}
111929ae59edSMatt Jacob 	}
112029ae59edSMatt Jacob 	if (ret != MPT_OK && retry_cnt++ < 2) {
112129ae59edSMatt Jacob 		goto again;
112229ae59edSMatt Jacob 	}
11239b631363SMatt Jacob 	return ret;
11249b631363SMatt Jacob }
11259b631363SMatt Jacob 
11269b631363SMatt Jacob /* Return a command buffer to the free queue */
11279b631363SMatt Jacob void
1128b0a2fdeeSScott Long mpt_free_request(struct mpt_softc *mpt, request_t *req)
11299b631363SMatt Jacob {
1130444dd2b6SMatt Jacob 	request_t *nxt;
1131b0a2fdeeSScott Long 	struct mpt_evtf_record *record;
1132b0a2fdeeSScott Long 	uint32_t reply_baddr;
1133b0a2fdeeSScott Long 
11347dec90bcSMatt Jacob 	if (req == NULL || req != &mpt->request_pool[req->index]) {
11359b631363SMatt Jacob 		panic("mpt_free_request bad req ptr\n");
11369b631363SMatt Jacob 		return;
11379b631363SMatt Jacob 	}
1138444dd2b6SMatt Jacob 	if ((nxt = req->chain) != NULL) {
1139444dd2b6SMatt Jacob 		req->chain = NULL;
1140444dd2b6SMatt Jacob 		mpt_free_request(mpt, nxt);	/* NB: recursion */
1141444dd2b6SMatt Jacob 	}
11425e073106SMatt Jacob 	KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
11435e073106SMatt Jacob 	KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
11445089bd63SMatt Jacob 	KASSERT(MPT_OWNED(mpt), ("mpt_free_request: mpt not locked\n"));
11455089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
11465089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x already on freelist",
11475089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
11485089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
11495089bd63SMatt Jacob 	    ("mpt_free_request: req %p:%u func %x on pending list",
11505089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
11515089bd63SMatt Jacob #ifdef	INVARIANTS
11525089bd63SMatt Jacob 	mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
11535089bd63SMatt Jacob #endif
11545e073106SMatt Jacob 
11559b631363SMatt Jacob 	req->ccb = NULL;
1156b0a2fdeeSScott Long 	if (LIST_EMPTY(&mpt->ack_frames)) {
1157c87e3f83SMatt Jacob 		/*
1158c87e3f83SMatt Jacob 		 * Insert free ones at the tail
1159c87e3f83SMatt Jacob 		 */
11605e073106SMatt Jacob 		req->serno = 0;
11615e073106SMatt Jacob 		req->state = REQ_STATE_FREE;
11625089bd63SMatt Jacob #ifdef	INVARIANTS
11635089bd63SMatt Jacob 		memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
11645089bd63SMatt Jacob #endif
1165c87e3f83SMatt Jacob 		TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1166b0a2fdeeSScott Long 		if (mpt->getreqwaiter != 0) {
1167b0a2fdeeSScott Long 			mpt->getreqwaiter = 0;
1168b0a2fdeeSScott Long 			wakeup(&mpt->request_free_list);
1169b0a2fdeeSScott Long 		}
1170b0a2fdeeSScott Long 		return;
1171b0a2fdeeSScott Long 	}
1172b0a2fdeeSScott Long 
1173b0a2fdeeSScott Long 	/*
1174b0a2fdeeSScott Long 	 * Process an ack frame deferred due to resource shortage.
1175b0a2fdeeSScott Long 	 */
1176b0a2fdeeSScott Long 	record = LIST_FIRST(&mpt->ack_frames);
1177b0a2fdeeSScott Long 	LIST_REMOVE(record, links);
11785e073106SMatt Jacob 	req->state = REQ_STATE_ALLOCATED;
11795089bd63SMatt Jacob 	mpt_assign_serno(mpt, req);
1180b0a2fdeeSScott Long 	mpt_send_event_ack(mpt, req, &record->reply, record->context);
1181b0a2fdeeSScott Long 	reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply)
1182b0a2fdeeSScott Long 		    + (mpt->reply_phys & 0xFFFFFFFF);
1183b0a2fdeeSScott Long 	mpt_free_reply(mpt, reply_baddr);
11849b631363SMatt Jacob }
11859b631363SMatt Jacob 
11869b631363SMatt Jacob /* Get a command buffer from the free queue */
11879b631363SMatt Jacob request_t *
1188b0a2fdeeSScott Long mpt_get_request(struct mpt_softc *mpt, int sleep_ok)
11899b631363SMatt Jacob {
11909b631363SMatt Jacob 	request_t *req;
1191b0a2fdeeSScott Long 
1192b0a2fdeeSScott Long retry:
11935089bd63SMatt Jacob 	KASSERT(MPT_OWNED(mpt), ("mpt_get_request: mpt not locked\n"));
1194b0a2fdeeSScott Long 	req = TAILQ_FIRST(&mpt->request_free_list);
11959b631363SMatt Jacob 	if (req != NULL) {
1196b0a2fdeeSScott Long 		KASSERT(req == &mpt->request_pool[req->index],
1197b0a2fdeeSScott Long 		    ("mpt_get_request: corrupted request free list\n"));
11985e073106SMatt Jacob 		KASSERT(req->state == REQ_STATE_FREE,
11995089bd63SMatt Jacob 		    ("req %p:%u not free on free list %x index %d function %x",
12005089bd63SMatt Jacob 		    req, req->serno, req->state, req->index,
12015089bd63SMatt Jacob 		    ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1202b0a2fdeeSScott Long 		TAILQ_REMOVE(&mpt->request_free_list, req, links);
1203b0a2fdeeSScott Long 		req->state = REQ_STATE_ALLOCATED;
1204444dd2b6SMatt Jacob 		req->chain = NULL;
12055089bd63SMatt Jacob 		mpt_assign_serno(mpt, req);
1206b0a2fdeeSScott Long 	} else if (sleep_ok != 0) {
1207b0a2fdeeSScott Long 		mpt->getreqwaiter = 1;
1208b0a2fdeeSScott Long 		mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1209b0a2fdeeSScott Long 		goto retry;
12109b631363SMatt Jacob 	}
12115e073106SMatt Jacob 	return (req);
12129b631363SMatt Jacob }
12139b631363SMatt Jacob 
12149b631363SMatt Jacob /* Pass the command to the IOC */
12159b631363SMatt Jacob void
1216b0a2fdeeSScott Long mpt_send_cmd(struct mpt_softc *mpt, request_t *req)
12179b631363SMatt Jacob {
12181d79ca0eSMatt Jacob 	if (mpt->verbose > MPT_PRT_DEBUG2) {
12191043516dSMatt Jacob 		mpt_dump_request(mpt, req);
1220444dd2b6SMatt Jacob 	}
12219b631363SMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
12229b631363SMatt Jacob 	    BUS_DMASYNC_PREWRITE);
1223b0a2fdeeSScott Long 	req->state |= REQ_STATE_QUEUED;
12245089bd63SMatt Jacob 	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
12255089bd63SMatt Jacob 	    ("req %p:%u func %x on freelist list in mpt_send_cmd",
12265089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
12275089bd63SMatt Jacob 	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
12285089bd63SMatt Jacob 	    ("req %p:%u func %x already on pending list in mpt_send_cmd",
12295089bd63SMatt Jacob 	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1230b0a2fdeeSScott Long 	TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1231b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
12329b631363SMatt Jacob }
12339b631363SMatt Jacob 
12349b631363SMatt Jacob /*
1235b0a2fdeeSScott Long  * Wait for a request to complete.
1236b0a2fdeeSScott Long  *
1237b0a2fdeeSScott Long  * Inputs:
1238b0a2fdeeSScott Long  *	mpt		softc of controller executing request
1239b0a2fdeeSScott Long  *	req		request to wait for
1240b0a2fdeeSScott Long  *	sleep_ok	nonzero implies may sleep in this context
1241b0a2fdeeSScott Long  *	time_ms		timeout in ms.  0 implies no timeout.
1242b0a2fdeeSScott Long  *
1243b0a2fdeeSScott Long  * Return Values:
1244b0a2fdeeSScott Long  *	0		Request completed
1245b0a2fdeeSScott Long  *	non-0		Timeout fired before request completion.
12469b631363SMatt Jacob  */
1247b0a2fdeeSScott Long int
1248b0a2fdeeSScott Long mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1249b0a2fdeeSScott Long 	     mpt_req_state_t state, mpt_req_state_t mask,
1250b0a2fdeeSScott Long 	     int sleep_ok, int time_ms)
12519b631363SMatt Jacob {
1252b0a2fdeeSScott Long 	int   error;
1253b0a2fdeeSScott Long 	int   timeout;
1254b0a2fdeeSScott Long 	u_int saved_cnt;
12559b631363SMatt Jacob 
1256b0a2fdeeSScott Long 	/*
1257b0a2fdeeSScott Long 	 * timeout is in ms.  0 indicates infinite wait.
1258b0a2fdeeSScott Long 	 * Convert to ticks or 500us units depending on
1259b0a2fdeeSScott Long 	 * our sleep mode.
1260b0a2fdeeSScott Long 	 */
1261c87e3f83SMatt Jacob 	if (sleep_ok != 0) {
1262b0a2fdeeSScott Long 		timeout = (time_ms * hz) / 1000;
1263c87e3f83SMatt Jacob 	} else {
1264b0a2fdeeSScott Long 		timeout = time_ms * 2;
1265c87e3f83SMatt Jacob 	}
1266b0a2fdeeSScott Long 	req->state |= REQ_STATE_NEED_WAKEUP;
1267b0a2fdeeSScott Long 	mask &= ~REQ_STATE_NEED_WAKEUP;
1268444dd2b6SMatt Jacob 	saved_cnt = mpt->reset_cnt;
1269c87e3f83SMatt Jacob 	while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1270b0a2fdeeSScott Long 		if (sleep_ok != 0) {
1271b0a2fdeeSScott Long 			error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout);
1272b0a2fdeeSScott Long 			if (error == EWOULDBLOCK) {
1273b0a2fdeeSScott Long 				timeout = 0;
1274b0a2fdeeSScott Long 				break;
1275b0a2fdeeSScott Long 			}
1276b0a2fdeeSScott Long 		} else {
1277b0a2fdeeSScott Long 			if (time_ms != 0 && --timeout == 0) {
1278b0a2fdeeSScott Long 				break;
1279b0a2fdeeSScott Long 			}
1280b0a2fdeeSScott Long 			DELAY(500);
1281b0a2fdeeSScott Long 			mpt_intr(mpt);
1282b0a2fdeeSScott Long 		}
1283b0a2fdeeSScott Long 	}
1284b0a2fdeeSScott Long 	req->state &= ~REQ_STATE_NEED_WAKEUP;
1285c87e3f83SMatt Jacob 	if (mpt->reset_cnt != saved_cnt) {
1286b0a2fdeeSScott Long 		return (EIO);
1287c87e3f83SMatt Jacob 	}
1288c87e3f83SMatt Jacob 	if (time_ms && timeout <= 0) {
1289c87e3f83SMatt Jacob 		MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
12906621d786SMatt Jacob 		req->state |= REQ_STATE_TIMEDOUT;
1291c87e3f83SMatt Jacob 		mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1292b0a2fdeeSScott Long 		return (ETIMEDOUT);
1293c87e3f83SMatt Jacob 	}
1294b0a2fdeeSScott Long 	return (0);
12959b631363SMatt Jacob }
12969b631363SMatt Jacob 
12979b631363SMatt Jacob /*
12989b631363SMatt Jacob  * Send a command to the IOC via the handshake register.
12999b631363SMatt Jacob  *
13009b631363SMatt Jacob  * Only done at initialization time and for certain unusual
13019b631363SMatt Jacob  * commands such as device/bus reset as specified by LSI.
13029b631363SMatt Jacob  */
13039b631363SMatt Jacob int
1304b0a2fdeeSScott Long mpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
13059b631363SMatt Jacob {
13069b631363SMatt Jacob 	int i;
1307b0a2fdeeSScott Long 	uint32_t data, *data32;
13089b631363SMatt Jacob 
13099b631363SMatt Jacob 	/* Check condition of the IOC */
13109b631363SMatt Jacob 	data = mpt_rd_db(mpt);
1311b0a2fdeeSScott Long 	if ((MPT_STATE(data) != MPT_DB_STATE_READY
1312b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1313b0a2fdeeSScott Long 	  && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1314b0a2fdeeSScott Long 	 || MPT_DB_IS_IN_USE(data)) {
1315b0a2fdeeSScott Long 		mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
13169b631363SMatt Jacob 		mpt_print_db(data);
13179b631363SMatt Jacob 		return (EBUSY);
13189b631363SMatt Jacob 	}
13199b631363SMatt Jacob 
13209b631363SMatt Jacob 	/* We move things in 32 bit chunks */
13219b631363SMatt Jacob 	len = (len + 3) >> 2;
13229b631363SMatt Jacob 	data32 = cmd;
13239b631363SMatt Jacob 
13249b631363SMatt Jacob 	/* Clear any left over pending doorbell interupts */
13259b631363SMatt Jacob 	if (MPT_DB_INTR(mpt_rd_intr(mpt)))
13269b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13279b631363SMatt Jacob 
13289b631363SMatt Jacob 	/*
13299b631363SMatt Jacob 	 * Tell the handshake reg. we are going to send a command
13309b631363SMatt Jacob          * and how long it is going to be.
13319b631363SMatt Jacob 	 */
13329b631363SMatt Jacob 	data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
13339b631363SMatt Jacob 	    (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
13349b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
13359b631363SMatt Jacob 
13369b631363SMatt Jacob 	/* Wait for the chip to notice */
13379b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1338b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_send_handshake_cmd timeout1\n");
1339b0a2fdeeSScott Long 		return (ETIMEDOUT);
13409b631363SMatt Jacob 	}
13419b631363SMatt Jacob 
13429b631363SMatt Jacob 	/* Clear the interrupt */
13439b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13449b631363SMatt Jacob 
13459b631363SMatt Jacob 	if (mpt_wait_db_ack(mpt) != MPT_OK) {
1346b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_send_handshake_cmd timeout2\n");
1347b0a2fdeeSScott Long 		return (ETIMEDOUT);
13489b631363SMatt Jacob 	}
13499b631363SMatt Jacob 
13509b631363SMatt Jacob 	/* Send the command */
13519b631363SMatt Jacob 	for (i = 0; i < len; i++) {
13529b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_DOORBELL, *data32++);
13539b631363SMatt Jacob 		if (mpt_wait_db_ack(mpt) != MPT_OK) {
1354301472c2SMatt Jacob 			mpt_prt(mpt,
1355b0a2fdeeSScott Long 				"mpt_send_handshake_cmd timeout! index = %d\n",
1356b0a2fdeeSScott Long 				i);
1357b0a2fdeeSScott Long 			return (ETIMEDOUT);
13589b631363SMatt Jacob 		}
13599b631363SMatt Jacob 	}
13609b631363SMatt Jacob 	return MPT_OK;
13619b631363SMatt Jacob }
13629b631363SMatt Jacob 
13639b631363SMatt Jacob /* Get the response from the handshake register */
13649b631363SMatt Jacob int
1365b0a2fdeeSScott Long mpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
13669b631363SMatt Jacob {
13679b631363SMatt Jacob 	int left, reply_left;
13689b631363SMatt Jacob 	u_int16_t *data16;
13699b631363SMatt Jacob 	MSG_DEFAULT_REPLY *hdr;
13709b631363SMatt Jacob 
13719b631363SMatt Jacob 	/* We move things out in 16 bit chunks */
13729b631363SMatt Jacob 	reply_len >>= 1;
13739b631363SMatt Jacob 	data16 = (u_int16_t *)reply;
13749b631363SMatt Jacob 
13759b631363SMatt Jacob 	hdr = (MSG_DEFAULT_REPLY *)reply;
13769b631363SMatt Jacob 
13779b631363SMatt Jacob 	/* Get first word */
13789b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1379b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
13809b631363SMatt Jacob 		return ETIMEDOUT;
13819b631363SMatt Jacob 	}
13829b631363SMatt Jacob 	*data16++ = mpt_read(mpt, MPT_OFFSET_DOORBELL) & MPT_DB_DATA_MASK;
13839b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13849b631363SMatt Jacob 
13859b631363SMatt Jacob 	/* Get Second Word */
13869b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1387b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
13889b631363SMatt Jacob 		return ETIMEDOUT;
13899b631363SMatt Jacob 	}
13909b631363SMatt Jacob 	*data16++ = mpt_read(mpt, MPT_OFFSET_DOORBELL) & MPT_DB_DATA_MASK;
13919b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
13929b631363SMatt Jacob 
13935e073106SMatt Jacob 	/*
13945e073106SMatt Jacob 	 * With the second word, we can now look at the length.
13955e073106SMatt Jacob 	 * Warn about a reply that's too short (except for IOC FACTS REPLY)
13965e073106SMatt Jacob 	 */
13975e073106SMatt Jacob 	if ((reply_len >> 1) != hdr->MsgLength &&
13985e073106SMatt Jacob 	    (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
13995e073106SMatt Jacob #if __FreeBSD_version >= 500000
1400301472c2SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
140133f31846SMatt Jacob 			"got %x; expected %zx for function %x\n",
14025e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14035e073106SMatt Jacob #else
14045e073106SMatt Jacob 		mpt_prt(mpt, "reply length does not match message length: "
140533f31846SMatt Jacob 			"got %x; expected %x for function %x\n",
14065e073106SMatt Jacob 			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
14075e073106SMatt Jacob #endif
14089b631363SMatt Jacob 	}
14099b631363SMatt Jacob 
14109b631363SMatt Jacob 	/* Get rest of the reply; but don't overflow the provided buffer */
14119b631363SMatt Jacob 	left = (hdr->MsgLength << 1) - 2;
14129b631363SMatt Jacob 	reply_left =  reply_len - 2;
14139b631363SMatt Jacob 	while (left--) {
14149b631363SMatt Jacob 		u_int16_t datum;
14159b631363SMatt Jacob 
14169b631363SMatt Jacob 		if (mpt_wait_db_int(mpt) != MPT_OK) {
1417b0a2fdeeSScott Long 			mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
14189b631363SMatt Jacob 			return ETIMEDOUT;
14199b631363SMatt Jacob 		}
14209b631363SMatt Jacob 		datum = mpt_read(mpt, MPT_OFFSET_DOORBELL);
14219b631363SMatt Jacob 
14229b631363SMatt Jacob 		if (reply_left-- > 0)
14239b631363SMatt Jacob 			*data16++ = datum & MPT_DB_DATA_MASK;
14249b631363SMatt Jacob 
14259b631363SMatt Jacob 		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14269b631363SMatt Jacob 	}
14279b631363SMatt Jacob 
14289b631363SMatt Jacob 	/* One more wait & clear at the end */
14299b631363SMatt Jacob 	if (mpt_wait_db_int(mpt) != MPT_OK) {
1430b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
14319b631363SMatt Jacob 		return ETIMEDOUT;
14329b631363SMatt Jacob 	}
14339b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
14349b631363SMatt Jacob 
14359b631363SMatt Jacob 	if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1436b0a2fdeeSScott Long 		if (mpt->verbose >= MPT_PRT_TRACE)
14379b631363SMatt Jacob 			mpt_print_reply(hdr);
14389b631363SMatt Jacob 		return (MPT_FAIL | hdr->IOCStatus);
14399b631363SMatt Jacob 	}
14409b631363SMatt Jacob 
14419b631363SMatt Jacob 	return (0);
14429b631363SMatt Jacob }
14439b631363SMatt Jacob 
14449b631363SMatt Jacob static int
1445b0a2fdeeSScott Long mpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
14469b631363SMatt Jacob {
14479b631363SMatt Jacob 	MSG_IOC_FACTS f_req;
14489b631363SMatt Jacob 	int error;
14499b631363SMatt Jacob 
14505e073106SMatt Jacob 	memset(&f_req, 0, sizeof f_req);
14519b631363SMatt Jacob 	f_req.Function = MPI_FUNCTION_IOC_FACTS;
1452b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
14539b631363SMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
14549b631363SMatt Jacob 	if (error)
14559b631363SMatt Jacob 		return(error);
14569b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
14579b631363SMatt Jacob 	return (error);
14589b631363SMatt Jacob }
14599b631363SMatt Jacob 
14607104aeefSMatt Jacob static int
1461b0a2fdeeSScott Long mpt_get_portfacts(struct mpt_softc *mpt, MSG_PORT_FACTS_REPLY *freplp)
14627104aeefSMatt Jacob {
14637104aeefSMatt Jacob 	MSG_PORT_FACTS f_req;
14647104aeefSMatt Jacob 	int error;
14657104aeefSMatt Jacob 
14667104aeefSMatt Jacob 	/* XXX: Only getting PORT FACTS for Port 0 */
1467b0a2fdeeSScott Long 	memset(&f_req, 0, sizeof f_req);
14687104aeefSMatt Jacob 	f_req.Function = MPI_FUNCTION_PORT_FACTS;
1469b0a2fdeeSScott Long 	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
14707104aeefSMatt Jacob 	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
14717104aeefSMatt Jacob 	if (error)
14727104aeefSMatt Jacob 		return(error);
14737104aeefSMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
14747104aeefSMatt Jacob 	return (error);
14757104aeefSMatt Jacob }
14767104aeefSMatt Jacob 
14779b631363SMatt Jacob /*
14789b631363SMatt Jacob  * Send the initialization request. This is where we specify how many
14799b631363SMatt Jacob  * SCSI busses and how many devices per bus we wish to emulate.
14809b631363SMatt Jacob  * This is also the command that specifies the max size of the reply
14819b631363SMatt Jacob  * frames from the IOC that we will be allocating.
14829b631363SMatt Jacob  */
14839b631363SMatt Jacob static int
1484b0a2fdeeSScott Long mpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
14859b631363SMatt Jacob {
14869b631363SMatt Jacob 	int error = 0;
14879b631363SMatt Jacob 	MSG_IOC_INIT init;
14889b631363SMatt Jacob 	MSG_IOC_INIT_REPLY reply;
14899b631363SMatt Jacob 
14905e073106SMatt Jacob 	memset(&init, 0, sizeof init);
14919b631363SMatt Jacob 	init.WhoInit = who;
14929b631363SMatt Jacob 	init.Function = MPI_FUNCTION_IOC_INIT;
1493444dd2b6SMatt Jacob 	init.MaxDevices = mpt->mpt_max_devices;
14949b631363SMatt Jacob 	init.MaxBuses = 1;
1495444dd2b6SMatt Jacob 
1496444dd2b6SMatt Jacob 	init.MsgVersion = htole16(MPI_VERSION);
1497444dd2b6SMatt Jacob 	init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1498444dd2b6SMatt Jacob 	init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1499b0a2fdeeSScott Long 	init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
15009b631363SMatt Jacob 
15019b631363SMatt Jacob 	if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
15029b631363SMatt Jacob 		return(error);
15039b631363SMatt Jacob 	}
15049b631363SMatt Jacob 
15059b631363SMatt Jacob 	error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
15069b631363SMatt Jacob 	return (error);
15079b631363SMatt Jacob }
15089b631363SMatt Jacob 
15097104aeefSMatt Jacob 
15107104aeefSMatt Jacob /*
15117104aeefSMatt Jacob  * Utiltity routine to read configuration headers and pages
15127104aeefSMatt Jacob  */
1513b0a2fdeeSScott Long int
1514b0a2fdeeSScott Long mpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, u_int Action,
1515b0a2fdeeSScott Long 		  u_int PageVersion, u_int PageLength, u_int PageNumber,
1516b0a2fdeeSScott Long 		  u_int PageType, uint32_t PageAddress, bus_addr_t addr,
1517b0a2fdeeSScott Long 		  bus_size_t len, int sleep_ok, int timeout_ms)
15187104aeefSMatt Jacob {
15197104aeefSMatt Jacob 	MSG_CONFIG *cfgp;
1520b0a2fdeeSScott Long 	SGE_SIMPLE32 *se;
15217104aeefSMatt Jacob 
15227104aeefSMatt Jacob 	cfgp = req->req_vbuf;
1523b0a2fdeeSScott Long 	memset(cfgp, 0, sizeof *cfgp);
1524b0a2fdeeSScott Long 	cfgp->Action = Action;
15257104aeefSMatt Jacob 	cfgp->Function = MPI_FUNCTION_CONFIG;
1526b0a2fdeeSScott Long 	cfgp->Header.PageVersion = PageVersion;
1527b0a2fdeeSScott Long 	cfgp->Header.PageLength = PageLength;
1528b0a2fdeeSScott Long 	cfgp->Header.PageNumber = PageNumber;
1529b0a2fdeeSScott Long 	cfgp->Header.PageType = PageType;
15307104aeefSMatt Jacob 	cfgp->PageAddress = PageAddress;
1531b0a2fdeeSScott Long 	se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
1532b0a2fdeeSScott Long 	se->Address = addr;
1533b0a2fdeeSScott Long 	MPI_pSGE_SET_LENGTH(se, len);
1534b0a2fdeeSScott Long 	MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1535b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1536b0a2fdeeSScott Long 	    MPI_SGE_FLAGS_END_OF_LIST |
1537b0a2fdeeSScott Long 	    ((Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
1538b0a2fdeeSScott Long 	  || Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1539b0a2fdeeSScott Long 	   ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
1540b0a2fdeeSScott Long 	cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
15417104aeefSMatt Jacob 
15427104aeefSMatt Jacob 	mpt_check_doorbell(mpt);
15437104aeefSMatt Jacob 	mpt_send_cmd(mpt, req);
1544b0a2fdeeSScott Long 	return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1545b0a2fdeeSScott Long 			     sleep_ok, timeout_ms));
15467104aeefSMatt Jacob }
15477104aeefSMatt Jacob 
1548b0a2fdeeSScott Long 
1549b0a2fdeeSScott Long int
1550b0a2fdeeSScott Long mpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1551b0a2fdeeSScott Long 		    uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1552b0a2fdeeSScott Long 		    int sleep_ok, int timeout_ms)
1553b0a2fdeeSScott Long {
1554b0a2fdeeSScott Long 	request_t  *req;
155529ae59edSMatt Jacob 	MSG_CONFIG *cfgp;
1556b0a2fdeeSScott Long 	int	    error;
1557b0a2fdeeSScott Long 
1558b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1559b0a2fdeeSScott Long 	if (req == NULL) {
1560b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
156129ae59edSMatt Jacob 		return (ENOMEM);
15627104aeefSMatt Jacob 	}
1563b0a2fdeeSScott Long 
1564b0a2fdeeSScott Long 	error = mpt_issue_cfg_req(mpt, req, MPI_CONFIG_ACTION_PAGE_HEADER,
1565b0a2fdeeSScott Long 				  /*PageVersion*/0, /*PageLength*/0, PageNumber,
1566b0a2fdeeSScott Long 				  PageType, PageAddress, /*addr*/0, /*len*/0,
1567b0a2fdeeSScott Long 				  sleep_ok, timeout_ms);
1568b0a2fdeeSScott Long 	if (error != 0) {
15696621d786SMatt Jacob 		/*
15706621d786SMatt Jacob 		 * Leave the request. Without resetting the chip, it's
15716621d786SMatt Jacob 		 * still owned by it and we'll just get into trouble
15726621d786SMatt Jacob 		 * freeing it now. Mark it as abandoned so that if it
15736621d786SMatt Jacob 		 * shows up later it can be freed.
15746621d786SMatt Jacob 		 */
1575b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_header timed out\n");
157629ae59edSMatt Jacob 		return (ETIMEDOUT);
1577b0a2fdeeSScott Long 	}
1578b0a2fdeeSScott Long 
157929ae59edSMatt Jacob         switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
158029ae59edSMatt Jacob 	case MPI_IOCSTATUS_SUCCESS:
1581b0a2fdeeSScott Long 		cfgp = req->req_vbuf;
1582b0a2fdeeSScott Long 		bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1583b0a2fdeeSScott Long 		error = 0;
158429ae59edSMatt Jacob 		break;
158529ae59edSMatt Jacob 	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
158629ae59edSMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG,
158729ae59edSMatt Jacob 		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
158829ae59edSMatt Jacob 		    PageType, PageNumber, PageAddress);
158929ae59edSMatt Jacob 		error = EINVAL;
159029ae59edSMatt Jacob 		break;
159129ae59edSMatt Jacob 	default:
159229ae59edSMatt Jacob 		mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
159329ae59edSMatt Jacob 			req->IOCStatus);
159429ae59edSMatt Jacob 		error = EIO;
159529ae59edSMatt Jacob 		break;
1596b0a2fdeeSScott Long 	}
15977104aeefSMatt Jacob 	mpt_free_request(mpt, req);
1598b0a2fdeeSScott Long 	return (error);
15997104aeefSMatt Jacob }
16007104aeefSMatt Jacob 
1601ce68dae5SMatt Jacob int
1602b0a2fdeeSScott Long mpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1603b0a2fdeeSScott Long 		  CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1604b0a2fdeeSScott Long 		  int timeout_ms)
16057104aeefSMatt Jacob {
16067104aeefSMatt Jacob 	request_t    *req;
1607b0a2fdeeSScott Long 	int	      error;
16087104aeefSMatt Jacob 
1609b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1610b0a2fdeeSScott Long 	if (req == NULL) {
1611b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
16127104aeefSMatt Jacob 		return (-1);
16137104aeefSMatt Jacob 	}
16147104aeefSMatt Jacob 
1615b0a2fdeeSScott Long 	error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion,
1616b0a2fdeeSScott Long 				  hdr->PageLength, hdr->PageNumber,
1617b0a2fdeeSScott Long 				  hdr->PageType & MPI_CONFIG_PAGETYPE_MASK,
1618c87e3f83SMatt Jacob 				  PageAddress, req->req_pbuf + MPT_RQSL(mpt),
1619b0a2fdeeSScott Long 				  len, sleep_ok, timeout_ms);
1620b0a2fdeeSScott Long 	if (error != 0) {
1621b0a2fdeeSScott Long 		mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
16227104aeefSMatt Jacob 		return (-1);
16237104aeefSMatt Jacob 	}
1624b0a2fdeeSScott Long 
1625b0a2fdeeSScott Long 	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1626b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1627b0a2fdeeSScott Long 			req->IOCStatus);
1628b0a2fdeeSScott Long 		mpt_free_request(mpt, req);
1629b0a2fdeeSScott Long 		return (-1);
1630b0a2fdeeSScott Long 	}
16317104aeefSMatt Jacob 	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
16327104aeefSMatt Jacob 	    BUS_DMASYNC_POSTREAD);
1633c87e3f83SMatt Jacob 	memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
16347104aeefSMatt Jacob 	mpt_free_request(mpt, req);
16357104aeefSMatt Jacob 	return (0);
16367104aeefSMatt Jacob }
16377104aeefSMatt Jacob 
1638ce68dae5SMatt Jacob int
1639b0a2fdeeSScott Long mpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1640b0a2fdeeSScott Long 		   CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1641b0a2fdeeSScott Long 		   int timeout_ms)
16427104aeefSMatt Jacob {
16437104aeefSMatt Jacob 	request_t    *req;
1644b0a2fdeeSScott Long 	u_int	      hdr_attr;
1645b0a2fdeeSScott Long 	int	      error;
16467104aeefSMatt Jacob 
16477104aeefSMatt Jacob 	hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
16487104aeefSMatt Jacob 	if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
16497104aeefSMatt Jacob 	    hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1650b0a2fdeeSScott Long 		mpt_prt(mpt, "page type 0x%x not changeable\n",
16517104aeefSMatt Jacob 			hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
16527104aeefSMatt Jacob 		return (-1);
16537104aeefSMatt Jacob 	}
1654b4c618c0SMatt Jacob 
1655b4c618c0SMatt Jacob #if	0
1656b4c618c0SMatt Jacob 	/*
1657b4c618c0SMatt Jacob 	 * We shouldn't mask off other bits here.
1658b4c618c0SMatt Jacob 	 */
1659b4c618c0SMatt Jacob 	hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1660b4c618c0SMatt Jacob #endif
16617104aeefSMatt Jacob 
1662b0a2fdeeSScott Long 	req = mpt_get_request(mpt, sleep_ok);
1663b0a2fdeeSScott Long 	if (req == NULL)
1664b0a2fdeeSScott Long 		return (-1);
16657104aeefSMatt Jacob 
1666c87e3f83SMatt Jacob 	memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1667b4c618c0SMatt Jacob 
1668b4c618c0SMatt Jacob 	/*
1669b4c618c0SMatt Jacob 	 * There isn't any point in restoring stripped out attributes
1670b4c618c0SMatt Jacob 	 * if you then mask them going down to issue the request.
1671b4c618c0SMatt Jacob 	 */
1672b4c618c0SMatt Jacob 
1673b4c618c0SMatt Jacob #if	0
1674301472c2SMatt Jacob 	/* Restore stripped out attributes */
1675301472c2SMatt Jacob 	hdr->PageType |= hdr_attr;
16767104aeefSMatt Jacob 
1677b0a2fdeeSScott Long 	error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion,
1678b0a2fdeeSScott Long 				  hdr->PageLength, hdr->PageNumber,
1679b0a2fdeeSScott Long 				  hdr->PageType & MPI_CONFIG_PAGETYPE_MASK,
1680c87e3f83SMatt Jacob 				  PageAddress, req->req_pbuf + MPT_RQSL(mpt),
1681b0a2fdeeSScott Long 				  len, sleep_ok, timeout_ms);
1682b4c618c0SMatt Jacob #else
1683b4c618c0SMatt Jacob 	error = mpt_issue_cfg_req(mpt, req, Action, hdr->PageVersion,
1684b4c618c0SMatt Jacob 				  hdr->PageLength, hdr->PageNumber,
1685b4c618c0SMatt Jacob 				  hdr->PageType, PageAddress,
1686b4c618c0SMatt Jacob 				  req->req_pbuf + MPT_RQSL(mpt),
1687b4c618c0SMatt Jacob 				  len, sleep_ok, timeout_ms);
1688b4c618c0SMatt Jacob #endif
1689b0a2fdeeSScott Long 	if (error != 0) {
1690b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
16917104aeefSMatt Jacob 		return (-1);
16927104aeefSMatt Jacob 	}
16937104aeefSMatt Jacob 
1694b0a2fdeeSScott Long         if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1695b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1696b0a2fdeeSScott Long 			req->IOCStatus);
16977104aeefSMatt Jacob 		mpt_free_request(mpt, req);
1698b0a2fdeeSScott Long 		return (-1);
1699b0a2fdeeSScott Long 	}
1700b0a2fdeeSScott Long 	mpt_free_request(mpt, req);
1701b0a2fdeeSScott Long 	return (0);
1702b0a2fdeeSScott Long }
1703b0a2fdeeSScott Long 
1704b0a2fdeeSScott Long /*
1705b0a2fdeeSScott Long  * Read IOC configuration information
1706b0a2fdeeSScott Long  */
1707b0a2fdeeSScott Long static int
1708b0a2fdeeSScott Long mpt_read_config_info_ioc(struct mpt_softc *mpt)
1709b0a2fdeeSScott Long {
1710b0a2fdeeSScott Long 	CONFIG_PAGE_HEADER hdr;
1711b0a2fdeeSScott Long 	struct mpt_raid_volume *mpt_raid;
1712b0a2fdeeSScott Long 	int rv;
1713b0a2fdeeSScott Long 	int i;
1714b0a2fdeeSScott Long 	size_t len;
1715b0a2fdeeSScott Long 
1716b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
17171d79ca0eSMatt Jacob 		2, 0, &hdr, FALSE, 5000);
171829ae59edSMatt Jacob 	/*
171929ae59edSMatt Jacob 	 * If it's an invalid page, so what? Not a supported function....
172029ae59edSMatt Jacob 	 */
17211d79ca0eSMatt Jacob 	if (rv == EINVAL) {
172229ae59edSMatt Jacob 		return (0);
17231d79ca0eSMatt Jacob 	}
17241d79ca0eSMatt Jacob 	if (rv) {
172529ae59edSMatt Jacob 		return (rv);
17261d79ca0eSMatt Jacob 	}
1727b0a2fdeeSScott Long 
17285e073106SMatt Jacob #if __FreeBSD_version >= 500000
1729a529c6a2SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG,  "IOC Page 2 Header: ver %x, len %zx, "
1730b0a2fdeeSScott Long 		 "num %x, type %x\n", hdr.PageVersion,
1731b0a2fdeeSScott Long 		 hdr.PageLength * sizeof(uint32_t),
1732b0a2fdeeSScott Long 		 hdr.PageNumber, hdr.PageType);
17335e073106SMatt Jacob #else
17345e073106SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG,  "IOC Page 2 Header: ver %x, len %z, "
17355e073106SMatt Jacob 		 "num %x, type %x\n", hdr.PageVersion,
17365e073106SMatt Jacob 		 hdr.PageLength * sizeof(uint32_t),
17375e073106SMatt Jacob 		 hdr.PageNumber, hdr.PageType);
17385e073106SMatt Jacob #endif
1739b0a2fdeeSScott Long 
1740b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
1741a3699bcaSScott Long 	mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
17421d79ca0eSMatt Jacob 	if (mpt->ioc_page2 == NULL) {
17431d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
17441d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
1745b0a2fdeeSScott Long 		return (ENOMEM);
17461d79ca0eSMatt Jacob 	}
1747b0a2fdeeSScott Long 	memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
17481d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
17491d79ca0eSMatt Jacob 	    &mpt->ioc_page2->Header, len, FALSE, 5000);
1750b0a2fdeeSScott Long 	if (rv) {
1751b0a2fdeeSScott Long 		mpt_prt(mpt, "failed to read IOC Page 2\n");
17521d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
17531d79ca0eSMatt Jacob 		return (EIO);
17541d79ca0eSMatt Jacob 	}
17551d79ca0eSMatt Jacob 
17561d79ca0eSMatt Jacob 	if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1757b0a2fdeeSScott Long 		uint32_t mask;
1758b0a2fdeeSScott Long 
1759b0a2fdeeSScott Long 		mpt_prt(mpt, "Capabilities: (");
1760b0a2fdeeSScott Long 		for (mask = 1; mask != 0; mask <<= 1) {
17611d79ca0eSMatt Jacob 			if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1762b0a2fdeeSScott Long 				continue;
17631d79ca0eSMatt Jacob 			}
1764b0a2fdeeSScott Long 			switch (mask) {
1765b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1766b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-0");
1767b0a2fdeeSScott Long 				break;
1768b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1769b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1E");
1770b0a2fdeeSScott Long 				break;
1771b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1772b0a2fdeeSScott Long 				mpt_prtc(mpt, " RAID-1");
1773b0a2fdeeSScott Long 				break;
1774b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1775b0a2fdeeSScott Long 				mpt_prtc(mpt, " SES");
1776b0a2fdeeSScott Long 				break;
1777b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1778b0a2fdeeSScott Long 				mpt_prtc(mpt, " SAFTE");
1779b0a2fdeeSScott Long 				break;
1780b0a2fdeeSScott Long 			case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1781b0a2fdeeSScott Long 				mpt_prtc(mpt, " Multi-Channel-Arrays");
1782b0a2fdeeSScott Long 			default:
1783b0a2fdeeSScott Long 				break;
1784b0a2fdeeSScott Long 			}
1785b0a2fdeeSScott Long 		}
1786b0a2fdeeSScott Long 		mpt_prtc(mpt, " )\n");
1787b0a2fdeeSScott Long 		if ((mpt->ioc_page2->CapabilitiesFlags
1788b0a2fdeeSScott Long 		   & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1789b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1790b0a2fdeeSScott Long 		    | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1791b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1792b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes,
1793b0a2fdeeSScott Long 				mpt->ioc_page2->NumActiveVolumes != 1
1794b0a2fdeeSScott Long 			      ? "s " : " ",
1795b0a2fdeeSScott Long 				mpt->ioc_page2->MaxVolumes);
1796b0a2fdeeSScott Long 			mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1797b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks,
1798b0a2fdeeSScott Long 				mpt->ioc_page2->NumActivePhysDisks != 1
1799b0a2fdeeSScott Long 			      ? "s " : " ",
1800b0a2fdeeSScott Long 				mpt->ioc_page2->MaxPhysDisks);
1801b0a2fdeeSScott Long 		}
1802b0a2fdeeSScott Long 	}
1803b0a2fdeeSScott Long 
1804b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
18051d79ca0eSMatt Jacob 	mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1806b0a2fdeeSScott Long 	if (mpt->raid_volumes == NULL) {
1807b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID volume data\n");
18081d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18091d79ca0eSMatt Jacob 		return (ENOMEM);
1810b0a2fdeeSScott Long 	}
1811b0a2fdeeSScott Long 
1812b0a2fdeeSScott Long 	/*
1813b0a2fdeeSScott Long 	 * Copy critical data out of ioc_page2 so that we can
1814b0a2fdeeSScott Long 	 * safely refresh the page without windows of unreliable
1815b0a2fdeeSScott Long 	 * data.
1816b0a2fdeeSScott Long 	 */
1817b0a2fdeeSScott Long 	mpt->raid_max_volumes =  mpt->ioc_page2->MaxVolumes;
1818b0a2fdeeSScott Long 
18191d79ca0eSMatt Jacob 	len = sizeof(*mpt->raid_volumes->config_page) +
18201d79ca0eSMatt Jacob 	    (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
1821b0a2fdeeSScott Long 	for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
1822b0a2fdeeSScott Long 		mpt_raid = &mpt->raid_volumes[i];
18231d79ca0eSMatt Jacob 		mpt_raid->config_page =
18241d79ca0eSMatt Jacob 		    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1825b0a2fdeeSScott Long 		if (mpt_raid->config_page == NULL) {
1826b0a2fdeeSScott Long 			mpt_prt(mpt, "Could not allocate RAID page data\n");
18271d79ca0eSMatt Jacob 			mpt_raid_free_mem(mpt);
18281d79ca0eSMatt Jacob 			return (ENOMEM);
1829b0a2fdeeSScott Long 		}
1830b0a2fdeeSScott Long 	}
1831b0a2fdeeSScott Long 	mpt->raid_page0_len = len;
1832b0a2fdeeSScott Long 
1833b0a2fdeeSScott Long 	len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
18341d79ca0eSMatt Jacob 	mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1835b0a2fdeeSScott Long 	if (mpt->raid_disks == NULL) {
1836b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not allocate RAID disk data\n");
18371d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18381d79ca0eSMatt Jacob 		return (ENOMEM);
1839b0a2fdeeSScott Long 	}
1840b0a2fdeeSScott Long 	mpt->raid_max_disks =  mpt->ioc_page2->MaxPhysDisks;
1841b0a2fdeeSScott Long 
18421d79ca0eSMatt Jacob 	/*
18431d79ca0eSMatt Jacob 	 * Load page 3.
18441d79ca0eSMatt Jacob 	 */
1845b0a2fdeeSScott Long 	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
18461d79ca0eSMatt Jacob 	    3, 0, &hdr, FALSE, 5000);
18471d79ca0eSMatt Jacob 	if (rv) {
18481d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
1849b0a2fdeeSScott Long 		return (EIO);
18501d79ca0eSMatt Jacob 	}
1851b0a2fdeeSScott Long 
1852b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
1853b0a2fdeeSScott Long 	    hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
1854b0a2fdeeSScott Long 
1855b0a2fdeeSScott Long 	len = hdr.PageLength * sizeof(uint32_t);
1856a3699bcaSScott Long 	mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
18571d79ca0eSMatt Jacob 	if (mpt->ioc_page3 == NULL) {
18581d79ca0eSMatt Jacob 		mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
18591d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18601d79ca0eSMatt Jacob 		return (ENOMEM);
1861b0a2fdeeSScott Long 	}
18621d79ca0eSMatt Jacob 	memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
18631d79ca0eSMatt Jacob 	rv = mpt_read_cur_cfg_page(mpt, 0,
18641d79ca0eSMatt Jacob 	    &mpt->ioc_page3->Header, len, FALSE, 5000);
18651d79ca0eSMatt Jacob 	if (rv) {
18661d79ca0eSMatt Jacob 		mpt_raid_free_mem(mpt);
18671d79ca0eSMatt Jacob 		return (EIO);
18681d79ca0eSMatt Jacob 	}
1869b0a2fdeeSScott Long 	mpt_raid_wakeup(mpt);
18707104aeefSMatt Jacob 	return (0);
18717104aeefSMatt Jacob }
18727104aeefSMatt Jacob 
18737104aeefSMatt Jacob /*
18747104aeefSMatt Jacob  * Enable IOC port
18757104aeefSMatt Jacob  */
18767104aeefSMatt Jacob static int
1877b0a2fdeeSScott Long mpt_send_port_enable(struct mpt_softc *mpt, int port)
18789b631363SMatt Jacob {
18799b631363SMatt Jacob 	request_t	*req;
18809b631363SMatt Jacob 	MSG_PORT_ENABLE *enable_req;
1881b0a2fdeeSScott Long 	int		 error;
18829b631363SMatt Jacob 
1883b0a2fdeeSScott Long 	req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
1884b0a2fdeeSScott Long 	if (req == NULL)
1885b0a2fdeeSScott Long 		return (-1);
18869b631363SMatt Jacob 
18879b631363SMatt Jacob 	enable_req = req->req_vbuf;
18885e073106SMatt Jacob 	memset(enable_req, 0,  MPT_RQSL(mpt));
18899b631363SMatt Jacob 
18909b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_PORT_ENABLE;
1891b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
18929b631363SMatt Jacob 	enable_req->PortNumber = port;
18939b631363SMatt Jacob 
18949b631363SMatt Jacob 	mpt_check_doorbell(mpt);
1895b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
18969b631363SMatt Jacob 
1897b0a2fdeeSScott Long 	mpt_send_cmd(mpt, req);
1898b0a2fdeeSScott Long 	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
18995089bd63SMatt Jacob 	    FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000);
1900b0a2fdeeSScott Long 	if (error != 0) {
1901c87e3f83SMatt Jacob 		mpt_prt(mpt, "port %d enable timed out\n", port);
19029b631363SMatt Jacob 		return (-1);
19039b631363SMatt Jacob 	}
19049b631363SMatt Jacob 	mpt_free_request(mpt, req);
1905c87e3f83SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
19069b631363SMatt Jacob 	return (0);
19079b631363SMatt Jacob }
19089b631363SMatt Jacob 
19099b631363SMatt Jacob /*
19109b631363SMatt Jacob  * Enable/Disable asynchronous event reporting.
19119b631363SMatt Jacob  */
19129b631363SMatt Jacob static int
1913b0a2fdeeSScott Long mpt_send_event_request(struct mpt_softc *mpt, int onoff)
19149b631363SMatt Jacob {
19159b631363SMatt Jacob 	request_t *req;
19169b631363SMatt Jacob 	MSG_EVENT_NOTIFY *enable_req;
19179b631363SMatt Jacob 
19185e073106SMatt Jacob 	req = mpt_get_request(mpt, FALSE);
19195e073106SMatt Jacob 	if (req == NULL) {
19205e073106SMatt Jacob 		return (ENOMEM);
19215e073106SMatt Jacob 	}
19229b631363SMatt Jacob 	enable_req = req->req_vbuf;
19235e073106SMatt Jacob 	memset(enable_req, 0, sizeof *enable_req);
19249b631363SMatt Jacob 
19259b631363SMatt Jacob 	enable_req->Function   = MPI_FUNCTION_EVENT_NOTIFICATION;
1926b0a2fdeeSScott Long 	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
19279b631363SMatt Jacob 	enable_req->Switch     = onoff;
19289b631363SMatt Jacob 
19299b631363SMatt Jacob 	mpt_check_doorbell(mpt);
19305e073106SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
19315e073106SMatt Jacob 	    onoff ? "en" : "dis");
19325e073106SMatt Jacob 	/*
19335e073106SMatt Jacob 	 * Send the command off, but don't wait for it.
19345e073106SMatt Jacob 	 */
19359b631363SMatt Jacob 	mpt_send_cmd(mpt, req);
19369b631363SMatt Jacob 	return (0);
19379b631363SMatt Jacob }
19389b631363SMatt Jacob 
19399b631363SMatt Jacob /*
19409b631363SMatt Jacob  * Un-mask the interupts on the chip.
19419b631363SMatt Jacob  */
19429b631363SMatt Jacob void
1943b0a2fdeeSScott Long mpt_enable_ints(struct mpt_softc *mpt)
19449b631363SMatt Jacob {
19459b631363SMatt Jacob 	/* Unmask every thing except door bell int */
19469b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
19479b631363SMatt Jacob }
19489b631363SMatt Jacob 
19499b631363SMatt Jacob /*
19509b631363SMatt Jacob  * Mask the interupts on the chip.
19519b631363SMatt Jacob  */
19529b631363SMatt Jacob void
1953b0a2fdeeSScott Long mpt_disable_ints(struct mpt_softc *mpt)
19549b631363SMatt Jacob {
19559b631363SMatt Jacob 	/* Mask all interrupts */
19569b631363SMatt Jacob 	mpt_write(mpt, MPT_OFFSET_INTR_MASK,
19579b631363SMatt Jacob 	    MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
19589b631363SMatt Jacob }
19599b631363SMatt Jacob 
1960b0a2fdeeSScott Long static void
1961b0a2fdeeSScott Long mpt_sysctl_attach(struct mpt_softc *mpt)
19629b631363SMatt Jacob {
1963c87e3f83SMatt Jacob #if __FreeBSD_version >= 500000
1964b0a2fdeeSScott Long 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
1965b0a2fdeeSScott Long 	struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
19669b631363SMatt Jacob 
1967b0a2fdeeSScott Long 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1968b0a2fdeeSScott Long 		       "debug", CTLFLAG_RW, &mpt->verbose, 0,
1969b0a2fdeeSScott Long 		       "Debugging/Verbose level");
1970b4c618c0SMatt Jacob 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
1971b4c618c0SMatt Jacob 		       "role", CTLFLAG_RD, &mpt->role, 0,
1972b4c618c0SMatt Jacob 		       "HBA role");
1973c87e3f83SMatt Jacob #endif
19749b631363SMatt Jacob }
19759b631363SMatt Jacob 
1976b0a2fdeeSScott Long int
1977b0a2fdeeSScott Long mpt_attach(struct mpt_softc *mpt)
1978b0a2fdeeSScott Long {
1979b0a2fdeeSScott Long 	struct mpt_personality *pers;
1980c87e3f83SMatt Jacob 	int i;
1981b0a2fdeeSScott Long 	int error;
1982b0a2fdeeSScott Long 
1983c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
1984b0a2fdeeSScott Long 		pers = mpt_personalities[i];
1985c87e3f83SMatt Jacob 		if (pers == NULL) {
1986b0a2fdeeSScott Long 			continue;
1987c87e3f83SMatt Jacob 		}
1988b0a2fdeeSScott Long 		if (pers->probe(mpt) == 0) {
1989b0a2fdeeSScott Long 			error = pers->attach(mpt);
1990b0a2fdeeSScott Long 			if (error != 0) {
1991b0a2fdeeSScott Long 				mpt_detach(mpt);
1992b0a2fdeeSScott Long 				return (error);
1993b0a2fdeeSScott Long 			}
1994b0a2fdeeSScott Long 			mpt->mpt_pers_mask |= (0x1 << pers->id);
1995b0a2fdeeSScott Long 			pers->use_count++;
1996b0a2fdeeSScott Long 		}
1997b0a2fdeeSScott Long 	}
1998444dd2b6SMatt Jacob 
1999c87e3f83SMatt Jacob 	/*
2000c87e3f83SMatt Jacob 	 * Now that we've attached everything, do the enable function
2001c87e3f83SMatt Jacob 	 * for all of the personalities. This allows the personalities
2002c87e3f83SMatt Jacob 	 * to do setups that are appropriate for them prior to enabling
2003c87e3f83SMatt Jacob 	 * any ports.
2004c87e3f83SMatt Jacob 	 */
2005c87e3f83SMatt Jacob 	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2006c87e3f83SMatt Jacob 		pers = mpt_personalities[i];
2007c87e3f83SMatt Jacob 		if (pers != NULL  && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2008c87e3f83SMatt Jacob 			error = pers->enable(mpt);
2009c87e3f83SMatt Jacob 			if (error != 0) {
2010c87e3f83SMatt Jacob 				mpt_prt(mpt, "personality %s attached but would"
2011c87e3f83SMatt Jacob 				    " not enable (%d)\n", pers->name, error);
2012c87e3f83SMatt Jacob 				mpt_detach(mpt);
2013c87e3f83SMatt Jacob 				return (error);
2014c87e3f83SMatt Jacob 			}
2015c87e3f83SMatt Jacob 		}
2016c87e3f83SMatt Jacob 	}
2017b0a2fdeeSScott Long 	return (0);
2018b0a2fdeeSScott Long }
2019b0a2fdeeSScott Long 
2020b0a2fdeeSScott Long int
2021b0a2fdeeSScott Long mpt_shutdown(struct mpt_softc *mpt)
2022b0a2fdeeSScott Long {
2023b0a2fdeeSScott Long 	struct mpt_personality *pers;
2024b0a2fdeeSScott Long 
2025c87e3f83SMatt Jacob 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2026b0a2fdeeSScott Long 		pers->shutdown(mpt);
2027c87e3f83SMatt Jacob 	}
2028b0a2fdeeSScott Long 	return (0);
2029b0a2fdeeSScott Long }
2030b0a2fdeeSScott Long 
2031b0a2fdeeSScott Long int
2032b0a2fdeeSScott Long mpt_detach(struct mpt_softc *mpt)
2033b0a2fdeeSScott Long {
2034b0a2fdeeSScott Long 	struct mpt_personality *pers;
2035b0a2fdeeSScott Long 
2036b0a2fdeeSScott Long 	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2037b0a2fdeeSScott Long 		pers->detach(mpt);
2038b0a2fdeeSScott Long 		mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2039b0a2fdeeSScott Long 		pers->use_count--;
2040b0a2fdeeSScott Long 	}
2041b0a2fdeeSScott Long 
2042b0a2fdeeSScott Long 	return (0);
2043b0a2fdeeSScott Long }
2044b0a2fdeeSScott Long 
2045b0a2fdeeSScott Long int
2046b0a2fdeeSScott Long mpt_core_load(struct mpt_personality *pers)
2047b0a2fdeeSScott Long {
2048b0a2fdeeSScott Long 	int i;
2049b0a2fdeeSScott Long 
2050b0a2fdeeSScott Long 	/*
2051b0a2fdeeSScott Long 	 * Setup core handlers and insert the default handler
2052b0a2fdeeSScott Long 	 * into all "empty slots".
2053b0a2fdeeSScott Long 	 */
2054c87e3f83SMatt Jacob 	for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2055b0a2fdeeSScott Long 		mpt_reply_handlers[i] = mpt_default_reply_handler;
2056c87e3f83SMatt Jacob 	}
2057b0a2fdeeSScott Long 
2058b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2059b0a2fdeeSScott Long 	    mpt_event_reply_handler;
2060b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2061b0a2fdeeSScott Long 	    mpt_config_reply_handler;
2062b0a2fdeeSScott Long 	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2063b0a2fdeeSScott Long 	    mpt_handshake_reply_handler;
2064b0a2fdeeSScott Long 	return (0);
20659b631363SMatt Jacob }
20669b631363SMatt Jacob 
20679b631363SMatt Jacob /*
2068b0a2fdeeSScott Long  * Initialize per-instance driver data and perform
2069b0a2fdeeSScott Long  * initial controller configuration.
20709b631363SMatt Jacob  */
2071b0a2fdeeSScott Long int
2072b0a2fdeeSScott Long mpt_core_attach(struct mpt_softc *mpt)
2073b0a2fdeeSScott Long {
2074b0a2fdeeSScott Long         int val;
2075b0a2fdeeSScott Long 	int error;
2076b0a2fdeeSScott Long 
2077c87e3f83SMatt Jacob 
2078b0a2fdeeSScott Long 	LIST_INIT(&mpt->ack_frames);
2079b0a2fdeeSScott Long 
2080b0a2fdeeSScott Long 	/* Put all request buffers on the free list */
2081b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_pending_list);
2082b0a2fdeeSScott Long 	TAILQ_INIT(&mpt->request_free_list);
20835e073106SMatt Jacob 	TAILQ_INIT(&mpt->request_timeout_list);
2084c87e3f83SMatt Jacob 	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
20855e073106SMatt Jacob 		request_t *req = &mpt->request_pool[val];
20865e073106SMatt Jacob 		req->state = REQ_STATE_ALLOCATED;
20875e073106SMatt Jacob 		mpt_free_request(mpt, req);
2088c87e3f83SMatt Jacob 	}
2089c87e3f83SMatt Jacob 
2090c87e3f83SMatt Jacob 	for (val = 0; val < MPT_MAX_LUNS; val++) {
2091c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].atios);
2092c87e3f83SMatt Jacob 		STAILQ_INIT(&mpt->trt[val].inots);
2093c87e3f83SMatt Jacob 	}
2094c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.atios);
2095c87e3f83SMatt Jacob 	STAILQ_INIT(&mpt->trt_wildcard.inots);
2096c87e3f83SMatt Jacob 
2097c87e3f83SMatt Jacob 	mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2098b0a2fdeeSScott Long 
2099b0a2fdeeSScott Long 	mpt_sysctl_attach(mpt);
2100b0a2fdeeSScott Long 
2101b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2102b0a2fdeeSScott Long 	    mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
2103b0a2fdeeSScott Long 
2104b0a2fdeeSScott Long 	error = mpt_configure_ioc(mpt);
2105b0a2fdeeSScott Long 
2106b0a2fdeeSScott Long 	return (error);
21079b631363SMatt Jacob }
21089b631363SMatt Jacob 
2109c87e3f83SMatt Jacob int
2110c87e3f83SMatt Jacob mpt_core_enable(struct mpt_softc *mpt)
2111c87e3f83SMatt Jacob {
2112c87e3f83SMatt Jacob 	/*
2113c87e3f83SMatt Jacob 	 * We enter with the IOC enabled, but async events
2114c87e3f83SMatt Jacob 	 * not enabled, ports not enabled and interrupts
2115c87e3f83SMatt Jacob 	 * not enabled.
2116c87e3f83SMatt Jacob 	 */
2117c87e3f83SMatt Jacob 
2118c87e3f83SMatt Jacob 	/*
2119c87e3f83SMatt Jacob 	 * Enable asynchronous event reporting- all personalities
2120c87e3f83SMatt Jacob 	 * have attached so that they should be able to now field
2121c87e3f83SMatt Jacob 	 * async events.
2122c87e3f83SMatt Jacob 	 */
2123c87e3f83SMatt Jacob 	mpt_send_event_request(mpt, 1);
2124c87e3f83SMatt Jacob 
2125c87e3f83SMatt Jacob 	/*
2126c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2127c87e3f83SMatt Jacob 	 *
2128c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2129c87e3f83SMatt Jacob 	 * the portenable below times out.
2130c87e3f83SMatt Jacob 	 */
2131c87e3f83SMatt Jacob 	mpt_intr(mpt);
2132c87e3f83SMatt Jacob 
2133c87e3f83SMatt Jacob 	/*
2134c87e3f83SMatt Jacob 	 * Enable Interrupts
2135c87e3f83SMatt Jacob 	 */
2136c87e3f83SMatt Jacob 	mpt_enable_ints(mpt);
2137c87e3f83SMatt Jacob 
2138c87e3f83SMatt Jacob 	/*
2139c87e3f83SMatt Jacob 	 * Catch any pending interrupts
2140c87e3f83SMatt Jacob 	 *
2141c87e3f83SMatt Jacob 	 * This seems to be crucial- otherwise
2142c87e3f83SMatt Jacob 	 * the portenable below times out.
2143c87e3f83SMatt Jacob 	 */
2144c87e3f83SMatt Jacob 	mpt_intr(mpt);
2145c87e3f83SMatt Jacob 
2146c87e3f83SMatt Jacob 	/*
21475089bd63SMatt Jacob 	 * Enable the port.
2148c87e3f83SMatt Jacob 	 */
2149c87e3f83SMatt Jacob 	if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2150c87e3f83SMatt Jacob 		mpt_prt(mpt, "failed to enable port 0\n");
2151c87e3f83SMatt Jacob 		return (ENXIO);
2152c87e3f83SMatt Jacob 	}
2153c87e3f83SMatt Jacob 	return (0);
2154c87e3f83SMatt Jacob }
2155c87e3f83SMatt Jacob 
2156b0a2fdeeSScott Long void
2157b0a2fdeeSScott Long mpt_core_shutdown(struct mpt_softc *mpt)
2158b0a2fdeeSScott Long {
2159c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2160b0a2fdeeSScott Long }
2161b0a2fdeeSScott Long 
2162b0a2fdeeSScott Long void
2163b0a2fdeeSScott Long mpt_core_detach(struct mpt_softc *mpt)
2164b0a2fdeeSScott Long {
2165c87e3f83SMatt Jacob 	mpt_disable_ints(mpt);
2166b0a2fdeeSScott Long }
2167b0a2fdeeSScott Long 
2168b0a2fdeeSScott Long int
2169b0a2fdeeSScott Long mpt_core_unload(struct mpt_personality *pers)
2170b0a2fdeeSScott Long {
2171b0a2fdeeSScott Long 	/* Unload is always successfull. */
2172b0a2fdeeSScott Long 	return (0);
2173b0a2fdeeSScott Long }
2174b0a2fdeeSScott Long 
2175b0a2fdeeSScott Long #define FW_UPLOAD_REQ_SIZE				\
2176b0a2fdeeSScott Long 	(sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION)	\
2177b0a2fdeeSScott Long        + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2178b0a2fdeeSScott Long 
2179b0a2fdeeSScott Long static int
2180b0a2fdeeSScott Long mpt_upload_fw(struct mpt_softc *mpt)
2181b0a2fdeeSScott Long {
2182b0a2fdeeSScott Long 	uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2183b0a2fdeeSScott Long 	MSG_FW_UPLOAD_REPLY fw_reply;
2184b0a2fdeeSScott Long 	MSG_FW_UPLOAD *fw_req;
2185b0a2fdeeSScott Long 	FW_UPLOAD_TCSGE *tsge;
2186b0a2fdeeSScott Long 	SGE_SIMPLE32 *sge;
2187b0a2fdeeSScott Long 	uint32_t flags;
2188b0a2fdeeSScott Long 	int error;
2189b0a2fdeeSScott Long 
2190b0a2fdeeSScott Long 	memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2191b0a2fdeeSScott Long 	fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2192b0a2fdeeSScott Long 	fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2193b0a2fdeeSScott Long 	fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2194b0a2fdeeSScott Long 	fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2195b0a2fdeeSScott Long 	tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2196b0a2fdeeSScott Long 	tsge->DetailsLength = 12;
2197b0a2fdeeSScott Long 	tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2198b0a2fdeeSScott Long 	tsge->ImageSize = htole32(mpt->fw_image_size);
2199b0a2fdeeSScott Long 	sge = (SGE_SIMPLE32 *)(tsge + 1);
2200b0a2fdeeSScott Long 	flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2201b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2202b0a2fdeeSScott Long 	      | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2203b0a2fdeeSScott Long 	flags <<= MPI_SGE_FLAGS_SHIFT;
2204b0a2fdeeSScott Long 	sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2205b0a2fdeeSScott Long 	sge->Address = htole32(mpt->fw_phys);
2206b0a2fdeeSScott Long 	error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2207b0a2fdeeSScott Long 	if (error)
2208b0a2fdeeSScott Long 		return(error);
2209b0a2fdeeSScott Long 	error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
2210b0a2fdeeSScott Long 	return (error);
2211b0a2fdeeSScott Long }
2212b0a2fdeeSScott Long 
2213b0a2fdeeSScott Long static void
2214b0a2fdeeSScott Long mpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2215b0a2fdeeSScott Long 	       uint32_t *data, bus_size_t len)
2216b0a2fdeeSScott Long {
2217b0a2fdeeSScott Long 	uint32_t *data_end;
2218b0a2fdeeSScott Long 
2219b0a2fdeeSScott Long 	data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
22209fe6d254SMatt Jacob 	if (mpt->is_sas) {
2221444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
22229fe6d254SMatt Jacob 	}
2223b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2224b0a2fdeeSScott Long 	while (data != data_end) {
2225b0a2fdeeSScott Long 		mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2226b0a2fdeeSScott Long 		data++;
2227b0a2fdeeSScott Long 	}
22289fe6d254SMatt Jacob 	if (mpt->is_sas) {
2229444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2230b0a2fdeeSScott Long 	}
22319fe6d254SMatt Jacob }
2232b0a2fdeeSScott Long 
2233b0a2fdeeSScott Long static int
2234b0a2fdeeSScott Long mpt_download_fw(struct mpt_softc *mpt)
2235b0a2fdeeSScott Long {
2236b0a2fdeeSScott Long 	MpiFwHeader_t *fw_hdr;
2237b0a2fdeeSScott Long 	int error;
2238b0a2fdeeSScott Long 	uint32_t ext_offset;
2239b0a2fdeeSScott Long 	uint32_t data;
2240b0a2fdeeSScott Long 
2241b0a2fdeeSScott Long 	mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2242b0a2fdeeSScott Long 		mpt->fw_image_size);
2243b0a2fdeeSScott Long 
2244b0a2fdeeSScott Long 	error = mpt_enable_diag_mode(mpt);
2245b0a2fdeeSScott Long 	if (error != 0) {
2246b0a2fdeeSScott Long 		mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2247b0a2fdeeSScott Long 		return (EIO);
2248b0a2fdeeSScott Long 	}
2249b0a2fdeeSScott Long 
2250b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2251b0a2fdeeSScott Long 		  MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2252b0a2fdeeSScott Long 
2253b0a2fdeeSScott Long 	fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
2254b0a2fdeeSScott Long 	mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2255b0a2fdeeSScott Long 		       fw_hdr->ImageSize);
2256b0a2fdeeSScott Long 
2257b0a2fdeeSScott Long 	ext_offset = fw_hdr->NextImageHeaderOffset;
2258b0a2fdeeSScott Long 	while (ext_offset != 0) {
2259b0a2fdeeSScott Long 		MpiExtImageHeader_t *ext;
2260b0a2fdeeSScott Long 
2261b0a2fdeeSScott Long 		ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2262b0a2fdeeSScott Long 		ext_offset = ext->NextImageHeaderOffset;
2263b0a2fdeeSScott Long 
2264b0a2fdeeSScott Long 		mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2265b0a2fdeeSScott Long 			       ext->ImageSize);
2266b0a2fdeeSScott Long 	}
2267b0a2fdeeSScott Long 
22689fe6d254SMatt Jacob 	if (mpt->is_sas) {
2269444dd2b6SMatt Jacob 		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
22709fe6d254SMatt Jacob 	}
2271b0a2fdeeSScott Long 	/* Setup the address to jump to on reset. */
2272b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2273b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2274b0a2fdeeSScott Long 
2275b0a2fdeeSScott Long 	/*
2276b0a2fdeeSScott Long 	 * The controller sets the "flash bad" status after attempting
2277b0a2fdeeSScott Long 	 * to auto-boot from flash.  Clear the status so that the controller
2278b0a2fdeeSScott Long 	 * will continue the boot process with our newly installed firmware.
2279b0a2fdeeSScott Long 	 */
2280b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2281b0a2fdeeSScott Long 	data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2282b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2283b0a2fdeeSScott Long 	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2284b0a2fdeeSScott Long 
22859fe6d254SMatt Jacob 	if (mpt->is_sas) {
2286444dd2b6SMatt Jacob 		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
22879fe6d254SMatt Jacob 	}
2288444dd2b6SMatt Jacob 
2289b0a2fdeeSScott Long 	/*
2290b0a2fdeeSScott Long 	 * Re-enable the processor and clear the boot halt flag.
2291b0a2fdeeSScott Long 	 */
2292b0a2fdeeSScott Long 	data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2293b0a2fdeeSScott Long 	data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2294b0a2fdeeSScott Long 	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2295b0a2fdeeSScott Long 
2296b0a2fdeeSScott Long 	mpt_disable_diag_mode(mpt);
2297b0a2fdeeSScott Long 	return (0);
2298b0a2fdeeSScott Long }
2299b0a2fdeeSScott Long 
2300b0a2fdeeSScott Long /*
2301b0a2fdeeSScott Long  * Allocate/Initialize data structures for the controller.  Called
2302b0a2fdeeSScott Long  * once at instance startup.
2303b0a2fdeeSScott Long  */
2304b0a2fdeeSScott Long static int
2305b0a2fdeeSScott Long mpt_configure_ioc(struct mpt_softc *mpt)
2306b0a2fdeeSScott Long {
2307b0a2fdeeSScott Long         MSG_PORT_FACTS_REPLY pfp;
2308b0a2fdeeSScott Long         MSG_IOC_FACTS_REPLY facts;
2309b0a2fdeeSScott Long 	int try;
2310b0a2fdeeSScott Long 	int needreset;
2311444dd2b6SMatt Jacob 	uint32_t max_chain_depth;
2312b0a2fdeeSScott Long 
2313b0a2fdeeSScott Long 	needreset = 0;
23149b631363SMatt Jacob 	for (try = 0; try < MPT_MAX_TRYS; try++) {
2315b0a2fdeeSScott Long 
23169b631363SMatt Jacob 		/*
23179b631363SMatt Jacob 		 * No need to reset if the IOC is already in the READY state.
23189b631363SMatt Jacob 		 *
23199b631363SMatt Jacob 		 * Force reset if initialization failed previously.
23209b631363SMatt Jacob 		 * Note that a hard_reset of the second channel of a '929
23219b631363SMatt Jacob 		 * will stop operation of the first channel.  Hopefully, if the
23229b631363SMatt Jacob 		 * first channel is ok, the second will not require a hard
23239b631363SMatt Jacob 		 * reset.
23249b631363SMatt Jacob 		 */
2325c87e3f83SMatt Jacob 		if (needreset || MPT_STATE(mpt_rd_db(mpt)) !=
23269b631363SMatt Jacob 		    MPT_DB_STATE_READY) {
2327c87e3f83SMatt Jacob 			if (mpt_reset(mpt, FALSE) != MPT_OK) {
23289b631363SMatt Jacob 				continue;
23299b631363SMatt Jacob 			}
2330c87e3f83SMatt Jacob 		}
2331b0a2fdeeSScott Long 		needreset = 0;
23329b631363SMatt Jacob 
23339b631363SMatt Jacob 		if (mpt_get_iocfacts(mpt, &facts) != MPT_OK) {
2334b0a2fdeeSScott Long 			mpt_prt(mpt, "mpt_get_iocfacts failed\n");
2335b0a2fdeeSScott Long 			needreset = 1;
23369b631363SMatt Jacob 			continue;
23377104aeefSMatt Jacob 		}
23387104aeefSMatt Jacob 
2339b0a2fdeeSScott Long 		mpt->mpt_global_credits = le16toh(facts.GlobalCredits);
2340b0a2fdeeSScott Long 		mpt->request_frame_size = le16toh(facts.RequestFrameSize);
2341444dd2b6SMatt Jacob 		mpt->ioc_facts_flags = facts.Flags;
2342b0a2fdeeSScott Long 		mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
2343b0a2fdeeSScott Long 			    le16toh(facts.MsgVersion) >> 8,
2344b0a2fdeeSScott Long 			    le16toh(facts.MsgVersion) & 0xFF,
2345b0a2fdeeSScott Long 			    le16toh(facts.HeaderVersion) >> 8,
2346b0a2fdeeSScott Long 			    le16toh(facts.HeaderVersion) & 0xFF);
2347444dd2b6SMatt Jacob 
2348444dd2b6SMatt Jacob 		/*
2349444dd2b6SMatt Jacob 		 * Now that we know request frame size, we can calculate
2350444dd2b6SMatt Jacob 		 * the actual (reasonable) segment limit for read/write I/O.
2351444dd2b6SMatt Jacob 		 *
2352444dd2b6SMatt Jacob 		 * This limit is constrained by:
2353444dd2b6SMatt Jacob 		 *
2354444dd2b6SMatt Jacob 		 *  + The size of each area we allocate per command (and how
2355444dd2b6SMatt Jacob                  *    many chain segments we can fit into it).
2356444dd2b6SMatt Jacob                  *  + The total number of areas we've set up.
2357444dd2b6SMatt Jacob 		 *  + The actual chain depth the card will allow.
2358444dd2b6SMatt Jacob 		 *
2359444dd2b6SMatt Jacob 		 * The first area's segment count is limited by the I/O request
2360444dd2b6SMatt Jacob 		 * at the head of it. We cannot allocate realistically more
2361444dd2b6SMatt Jacob 		 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2362444dd2b6SMatt Jacob 		 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2363444dd2b6SMatt Jacob 		 *
2364444dd2b6SMatt Jacob 		 */
2365444dd2b6SMatt Jacob 		max_chain_depth = facts.MaxChainDepth;
2366444dd2b6SMatt Jacob 
2367444dd2b6SMatt Jacob 		/* total number of request areas we (can) allocate */
2368444dd2b6SMatt Jacob 		mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2369444dd2b6SMatt Jacob 
2370444dd2b6SMatt Jacob 		/* converted to the number of chain areas possible */
2371444dd2b6SMatt Jacob 		mpt->max_seg_cnt *= MPT_NRFM(mpt);
2372444dd2b6SMatt Jacob 
2373444dd2b6SMatt Jacob 		/* limited by the number of chain areas the card will support */
2374444dd2b6SMatt Jacob 		if (mpt->max_seg_cnt > max_chain_depth) {
2375444dd2b6SMatt Jacob 			mpt_lprt(mpt, MPT_PRT_DEBUG,
2376444dd2b6SMatt Jacob 			    "chain depth limited to %u (from %u)\n",
2377444dd2b6SMatt Jacob 			    max_chain_depth, mpt->max_seg_cnt);
2378444dd2b6SMatt Jacob 			mpt->max_seg_cnt = max_chain_depth;
2379444dd2b6SMatt Jacob 		}
2380444dd2b6SMatt Jacob 
2381444dd2b6SMatt Jacob 		/* converted to the number of simple sges in chain segments. */
2382444dd2b6SMatt Jacob 		mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2383444dd2b6SMatt Jacob 
2384444dd2b6SMatt Jacob 		mpt_lprt(mpt, MPT_PRT_DEBUG,
2385444dd2b6SMatt Jacob 		    "Maximum Segment Count: %u\n", mpt->max_seg_cnt);
2386b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG,
2387b0a2fdeeSScott Long 			 "MsgLength=%u IOCNumber = %d\n",
2388b0a2fdeeSScott Long 			 facts.MsgLength, facts.IOCNumber);
2389b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG,
2390444dd2b6SMatt Jacob 			 "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2391444dd2b6SMatt Jacob 			 "Request Frame Size %u bytes Max Chain Depth %u\n",
2392444dd2b6SMatt Jacob                          mpt->mpt_global_credits, facts.BlockSize,
2393444dd2b6SMatt Jacob                          mpt->request_frame_size << 2, max_chain_depth);
2394b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG,
2395b0a2fdeeSScott Long 			 "IOCFACTS: Num Ports %d, FWImageSize %d, "
2396b0a2fdeeSScott Long 			 "Flags=%#x\n", facts.NumberOfPorts,
2397b0a2fdeeSScott Long 			 le32toh(facts.FWImageSize), facts.Flags);
2398b0a2fdeeSScott Long 
2399444dd2b6SMatt Jacob 
2400b0a2fdeeSScott Long 		if ((facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) != 0) {
2401b0a2fdeeSScott Long 			struct mpt_map_info mi;
2402b0a2fdeeSScott Long 			int error;
2403b0a2fdeeSScott Long 
2404b0a2fdeeSScott Long 			/*
2405b0a2fdeeSScott Long 			 * In some configurations, the IOC's firmware is
2406b0a2fdeeSScott Long 			 * stored in a shared piece of system NVRAM that
2407b0a2fdeeSScott Long 			 * is only accessable via the BIOS.  In this
2408b0a2fdeeSScott Long 			 * case, the firmware keeps a copy of firmware in
2409b0a2fdeeSScott Long 			 * RAM until the OS driver retrieves it.  Once
2410b0a2fdeeSScott Long 			 * retrieved, we are responsible for re-downloading
2411b0a2fdeeSScott Long 			 * the firmware after any hard-reset.
2412b0a2fdeeSScott Long 			 */
2413b0a2fdeeSScott Long 			mpt->fw_image_size = le32toh(facts.FWImageSize);
2414b0a2fdeeSScott Long 			error = mpt_dma_tag_create(mpt, mpt->parent_dmat,
2415b0a2fdeeSScott Long 			    /*alignment*/1, /*boundary*/0,
2416b0a2fdeeSScott Long 			    /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2417b0a2fdeeSScott Long 			    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL,
2418b0a2fdeeSScott Long 			    /*filterarg*/NULL, mpt->fw_image_size,
2419b0a2fdeeSScott Long 			    /*nsegments*/1, /*maxsegsz*/mpt->fw_image_size,
2420b0a2fdeeSScott Long 			    /*flags*/0, &mpt->fw_dmat);
2421b0a2fdeeSScott Long 			if (error != 0) {
2422b0a2fdeeSScott Long 				mpt_prt(mpt, "cannot create fw dma tag\n");
2423b0a2fdeeSScott Long 				return (ENOMEM);
24249b631363SMatt Jacob 			}
2425b0a2fdeeSScott Long 			error = bus_dmamem_alloc(mpt->fw_dmat,
2426b0a2fdeeSScott Long 			    (void **)&mpt->fw_image, BUS_DMA_NOWAIT,
2427b0a2fdeeSScott Long 			    &mpt->fw_dmap);
2428b0a2fdeeSScott Long 			if (error != 0) {
2429b0a2fdeeSScott Long 				mpt_prt(mpt, "cannot allocate fw mem.\n");
2430b0a2fdeeSScott Long 				bus_dma_tag_destroy(mpt->fw_dmat);
2431b0a2fdeeSScott Long 				return (ENOMEM);
2432b0a2fdeeSScott Long 			}
2433b0a2fdeeSScott Long 			mi.mpt = mpt;
2434b0a2fdeeSScott Long 			mi.error = 0;
2435b0a2fdeeSScott Long 			bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
2436b0a2fdeeSScott Long 			    mpt->fw_image, mpt->fw_image_size, mpt_map_rquest,
2437b0a2fdeeSScott Long 			    &mi, 0);
2438b0a2fdeeSScott Long 			mpt->fw_phys = mi.phys;
2439b0a2fdeeSScott Long 
2440b0a2fdeeSScott Long 			error = mpt_upload_fw(mpt);
2441b0a2fdeeSScott Long 			if (error != 0) {
2442b0a2fdeeSScott Long 				mpt_prt(mpt, "fw upload failed.\n");
2443b0a2fdeeSScott Long 				bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2444b0a2fdeeSScott Long 				bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2445b0a2fdeeSScott Long 				    mpt->fw_dmap);
2446b0a2fdeeSScott Long 				bus_dma_tag_destroy(mpt->fw_dmat);
2447b0a2fdeeSScott Long 				mpt->fw_image = NULL;
2448b0a2fdeeSScott Long 				return (EIO);
2449b0a2fdeeSScott Long 			}
2450b0a2fdeeSScott Long 		}
24519b631363SMatt Jacob 
24527104aeefSMatt Jacob 		if (mpt_get_portfacts(mpt, &pfp) != MPT_OK) {
2453b0a2fdeeSScott Long 			mpt_prt(mpt, "mpt_get_portfacts failed\n");
2454b0a2fdeeSScott Long 			needreset = 1;
24557104aeefSMatt Jacob 			continue;
24567104aeefSMatt Jacob 		}
24577104aeefSMatt Jacob 
2458b0a2fdeeSScott Long 		mpt_lprt(mpt, MPT_PRT_DEBUG,
24597dec90bcSMatt Jacob 			 "PORTFACTS: Type %x PFlags %x IID %d MaxDev %d\n",
24607dec90bcSMatt Jacob 			 pfp.PortType, pfp.ProtocolFlags, pfp.PortSCSIID,
24617dec90bcSMatt Jacob 			 pfp.MaxDevices);
24627104aeefSMatt Jacob 
2463b0a2fdeeSScott Long 		mpt->mpt_port_type = pfp.PortType;
2464b0a2fdeeSScott Long 		mpt->mpt_proto_flags = pfp.ProtocolFlags;
24657104aeefSMatt Jacob 		if (pfp.PortType != MPI_PORTFACTS_PORTTYPE_SCSI &&
2466444dd2b6SMatt Jacob 		    pfp.PortType != MPI_PORTFACTS_PORTTYPE_SAS &&
24677104aeefSMatt Jacob 		    pfp.PortType != MPI_PORTFACTS_PORTTYPE_FC) {
2468b0a2fdeeSScott Long 			mpt_prt(mpt, "Unsupported Port Type (%x)\n",
24697104aeefSMatt Jacob 			    pfp.PortType);
24707104aeefSMatt Jacob 			return (ENXIO);
24717104aeefSMatt Jacob 		}
2472c87e3f83SMatt Jacob 		mpt->mpt_max_tgtcmds = le16toh(pfp.MaxPostedCmdBuffers);
2473c87e3f83SMatt Jacob 
24747104aeefSMatt Jacob 		if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_FC) {
24757104aeefSMatt Jacob 			mpt->is_fc = 1;
2476444dd2b6SMatt Jacob 			mpt->is_sas = 0;
24775580ce96SMatt Jacob 			mpt->is_spi = 0;
2478444dd2b6SMatt Jacob 		} else if (pfp.PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2479444dd2b6SMatt Jacob 			mpt->is_fc = 0;
2480444dd2b6SMatt Jacob 			mpt->is_sas = 1;
24815580ce96SMatt Jacob 			mpt->is_spi = 0;
24827104aeefSMatt Jacob 		} else {
24837104aeefSMatt Jacob 			mpt->is_fc = 0;
2484444dd2b6SMatt Jacob 			mpt->is_sas = 0;
24855580ce96SMatt Jacob 			mpt->is_spi = 1;
24867104aeefSMatt Jacob 		}
24877104aeefSMatt Jacob 		mpt->mpt_ini_id = pfp.PortSCSIID;
2488444dd2b6SMatt Jacob 		mpt->mpt_max_devices = pfp.MaxDevices;
24897104aeefSMatt Jacob 
2490c87e3f83SMatt Jacob 		/*
2491b4c618c0SMatt Jacob 		 * Set our role with what this port supports.
2492b4c618c0SMatt Jacob 		 *
2493b4c618c0SMatt Jacob 		 * Note this might be changed later in different modules
2494b4c618c0SMatt Jacob 		 * if this is different from what is wanted.
2495c87e3f83SMatt Jacob 		 */
24965089bd63SMatt Jacob 		mpt->role = MPT_ROLE_NONE;
2497c87e3f83SMatt Jacob 		if (pfp.ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
24985089bd63SMatt Jacob 			mpt->role |= MPT_ROLE_INITIATOR;
2499c87e3f83SMatt Jacob 		}
2500c87e3f83SMatt Jacob 		if (pfp.ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
25015089bd63SMatt Jacob 			mpt->role |= MPT_ROLE_TARGET;
2502c87e3f83SMatt Jacob 		}
2503c87e3f83SMatt Jacob 		if (mpt_enable_ioc(mpt, 0) != MPT_OK) {
2504c87e3f83SMatt Jacob 			mpt_prt(mpt, "unable to initialize IOC\n");
2505b0a2fdeeSScott Long 			return (ENXIO);
25067104aeefSMatt Jacob 		}
25077104aeefSMatt Jacob 
2508b0a2fdeeSScott Long 		/*
2509c87e3f83SMatt Jacob 		 * Read IOC configuration information.
25101d79ca0eSMatt Jacob 		 *
25111d79ca0eSMatt Jacob 		 * We need this to determine whether or not we have certain
25121d79ca0eSMatt Jacob 		 * settings for Integrated Mirroring (e.g.).
2513b0a2fdeeSScott Long 		 */
2514b0a2fdeeSScott Long 		mpt_read_config_info_ioc(mpt);
2515b0a2fdeeSScott Long 
2516b0a2fdeeSScott Long 		/* Everything worked */
2517b0a2fdeeSScott Long 		break;
2518b0a2fdeeSScott Long 	}
2519b0a2fdeeSScott Long 
2520b0a2fdeeSScott Long 	if (try >= MPT_MAX_TRYS) {
2521b0a2fdeeSScott Long 		mpt_prt(mpt, "failed to initialize IOC");
2522b0a2fdeeSScott Long 		return (EIO);
2523b0a2fdeeSScott Long 	}
2524b0a2fdeeSScott Long 
2525b0a2fdeeSScott Long 	return (0);
2526b0a2fdeeSScott Long }
2527b0a2fdeeSScott Long 
2528b0a2fdeeSScott Long static int
2529c87e3f83SMatt Jacob mpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2530b0a2fdeeSScott Long {
2531b0a2fdeeSScott Long 	uint32_t pptr;
2532b0a2fdeeSScott Long 	int val;
2533b0a2fdeeSScott Long 
2534444dd2b6SMatt Jacob 	if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2535b0a2fdeeSScott Long 		mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2536b0a2fdeeSScott Long 		return (EIO);
2537b0a2fdeeSScott Long 	}
2538b0a2fdeeSScott Long 
2539b0a2fdeeSScott Long 	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
25409b631363SMatt Jacob 
25419b631363SMatt Jacob 	if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2542b0a2fdeeSScott Long 		mpt_prt(mpt, "IOC failed to go to run state\n");
2543b0a2fdeeSScott Long 		return (ENXIO);
25447104aeefSMatt Jacob 	}
2545444dd2b6SMatt Jacob 	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
25469b631363SMatt Jacob 
25479b631363SMatt Jacob 	/*
25489b631363SMatt Jacob 	 * Give it reply buffers
25499b631363SMatt Jacob 	 *
2550b0a2fdeeSScott Long 	 * Do *not* exceed global credits.
25519b631363SMatt Jacob 	 */
25529b631363SMatt Jacob 	for (val = 0, pptr = mpt->reply_phys;
25539b631363SMatt Jacob 	    (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
25549b631363SMatt Jacob 	     pptr += MPT_REPLY_SIZE) {
25559b631363SMatt Jacob 		mpt_free_reply(mpt, pptr);
25569b631363SMatt Jacob 		if (++val == mpt->mpt_global_credits - 1)
25579b631363SMatt Jacob 			break;
25589b631363SMatt Jacob 	}
25599b631363SMatt Jacob 
2560c87e3f83SMatt Jacob 
2561c87e3f83SMatt Jacob 	/*
25625e073106SMatt Jacob 	 * Enable the port if asked. This is only done if we're resetting
25635e073106SMatt Jacob 	 * the IOC after initial startup.
2564c87e3f83SMatt Jacob 	 */
2565c87e3f83SMatt Jacob 	if (portenable) {
25667104aeefSMatt Jacob 		/*
25677104aeefSMatt Jacob 		 * Enable asynchronous event reporting
25687104aeefSMatt Jacob 		 */
25699b631363SMatt Jacob 		mpt_send_event_request(mpt, 1);
25709b631363SMatt Jacob 
25719b631363SMatt Jacob 		if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2572b0a2fdeeSScott Long 			mpt_prt(mpt, "failed to enable port 0\n");
2573b0a2fdeeSScott Long 			return (ENXIO);
25747104aeefSMatt Jacob 		}
2575c87e3f83SMatt Jacob 	}
257629ae59edSMatt Jacob 	return (MPT_OK);
25779b631363SMatt Jacob }
2578