1139749Simp/*-
2156000Smjacob * Generic routines for LSI Fusion adapters.
3101704Smjacob * FreeBSD Version.
4101704Smjacob *
5101704Smjacob * Copyright (c) 2000, 2001 by Greg Ansley
6101704Smjacob *
7101704Smjacob * Redistribution and use in source and binary forms, with or without
8101704Smjacob * modification, are permitted provided that the following conditions
9101704Smjacob * are met:
10101704Smjacob * 1. Redistributions of source code must retain the above copyright
11101704Smjacob *    notice immediately at the beginning of the file, without modification,
12101704Smjacob *    this list of conditions, and the following disclaimer.
13101704Smjacob * 2. The name of the author may not be used to endorse or promote products
14101704Smjacob *    derived from this software without specific prior written permission.
15101704Smjacob *
16101704Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17101704Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18101704Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19101704Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20101704Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21101704Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22101704Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23101704Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24101704Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25101704Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26101704Smjacob * SUCH DAMAGE.
27156000Smjacob */
28156000Smjacob/*-
29156000Smjacob * Copyright (c) 2002, 2006 by Matthew Jacob
30156000Smjacob * All rights reserved.
31156104Smjacob *
32156000Smjacob * Redistribution and use in source and binary forms, with or without
33156000Smjacob * modification, are permitted provided that the following conditions are
34156000Smjacob * met:
35156000Smjacob * 1. Redistributions of source code must retain the above copyright
36156000Smjacob *    notice, this list of conditions and the following disclaimer.
37156000Smjacob * 2. Redistributions in binary form must reproduce at minimum a disclaimer
38156000Smjacob *    substantially similar to the "NO WARRANTY" disclaimer below
39156000Smjacob *    ("Disclaimer") and any redistribution must be conditioned upon including
40156000Smjacob *    a substantially similar Disclaimer requirement for further binary
41156000Smjacob *    redistribution.
42156000Smjacob * 3. Neither the names of the above listed copyright holders nor the names
43156000Smjacob *    of any contributors may be used to endorse or promote products derived
44156000Smjacob *    from this software without specific prior written permission.
45156104Smjacob *
46156000Smjacob * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47156000Smjacob * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48156000Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49156000Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50156000Smjacob * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51156000Smjacob * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52156000Smjacob * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53156000Smjacob * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54156000Smjacob * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55156000Smjacob * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
56156000Smjacob * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57147883Sscottl *
58156000Smjacob * Support from Chris Ellsworth in order to make SAS adapters work
59156000Smjacob * is gratefully acknowledged.
60159052Smjacob *
61159052Smjacob *
62159052Smjacob * Support from LSI-Logic has also gone a great deal toward making this a
63159052Smjacob * workable subsystem and is gratefully acknowledged.
64101704Smjacob */
65156000Smjacob/*-
66147883Sscottl * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
67147883Sscottl * Copyright (c) 2005, WHEEL Sp. z o.o.
68147883Sscottl * Copyright (c) 2004, 2005 Justin T. Gibbs
69147883Sscottl * All rights reserved.
70156104Smjacob *
71147883Sscottl * Redistribution and use in source and binary forms, with or without
72147883Sscottl * modification, are permitted provided that the following conditions are
73147883Sscottl * met:
74147883Sscottl * 1. Redistributions of source code must retain the above copyright
75147883Sscottl *    notice, this list of conditions and the following disclaimer.
76147883Sscottl * 2. Redistributions in binary form must reproduce at minimum a disclaimer
77147883Sscottl *    substantially similar to the "NO WARRANTY" disclaimer below
78147883Sscottl *    ("Disclaimer") and any redistribution must be conditioned upon including
79147883Sscottl *    a substantially similar Disclaimer requirement for further binary
80147883Sscottl *    redistribution.
81148679Sgibbs * 3. Neither the names of the above listed copyright holders nor the names
82148679Sgibbs *    of any contributors may be used to endorse or promote products derived
83148679Sgibbs *    from this software without specific prior written permission.
84156104Smjacob *
85147883Sscottl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
86147883Sscottl * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87147883Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88147883Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
89147883Sscottl * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
90147883Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
91147883Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
92147883Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
93147883Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
94147883Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
95147883Sscottl * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96101704Smjacob */
97101704Smjacob
98134123Sobrien#include <sys/cdefs.h>
99134123Sobrien__FBSDID("$FreeBSD$");
100134123Sobrien
101147883Sscottl#include <dev/mpt/mpt.h>
102147883Sscottl#include <dev/mpt/mpt_cam.h> /* XXX For static handler registration */
103147883Sscottl#include <dev/mpt/mpt_raid.h> /* XXX For static handler registration */
104102199Smjacob
105147883Sscottl#include <dev/mpt/mpilib/mpi.h>
106147883Sscottl#include <dev/mpt/mpilib/mpi_ioc.h>
107157117Smjacob#include <dev/mpt/mpilib/mpi_fc.h>
108157117Smjacob#include <dev/mpt/mpilib/mpi_targ.h>
109147883Sscottl
110147883Sscottl#include <sys/sysctl.h>
111147883Sscottl
112101704Smjacob#define MPT_MAX_TRYS 3
113101704Smjacob#define MPT_MAX_WAIT 300000
114101704Smjacob
115101704Smjacobstatic int maxwait_ack = 0;
116101704Smjacobstatic int maxwait_int = 0;
117101704Smjacobstatic int maxwait_state = 0;
118101704Smjacob
119162133Smjacobstatic TAILQ_HEAD(, mpt_softc)	mpt_tailq = TAILQ_HEAD_INITIALIZER(mpt_tailq);
120147883Sscottlmpt_reply_handler_t *mpt_reply_handlers[MPT_NUM_REPLY_HANDLERS];
121101704Smjacob
122147883Sscottlstatic mpt_reply_handler_t mpt_default_reply_handler;
123147883Sscottlstatic mpt_reply_handler_t mpt_config_reply_handler;
124147883Sscottlstatic mpt_reply_handler_t mpt_handshake_reply_handler;
125147883Sscottlstatic mpt_reply_handler_t mpt_event_reply_handler;
126147883Sscottlstatic void mpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
127147883Sscottl			       MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context);
128155521Smjacobstatic int mpt_send_event_request(struct mpt_softc *mpt, int onoff);
129147883Sscottlstatic int mpt_soft_reset(struct mpt_softc *mpt);
130147883Sscottlstatic void mpt_hard_reset(struct mpt_softc *mpt);
131209960Smariusstatic int mpt_dma_buf_alloc(struct mpt_softc *mpt);
132209599Skenstatic void mpt_dma_buf_free(struct mpt_softc *mpt);
133164990Smjacobstatic int mpt_configure_ioc(struct mpt_softc *mpt, int, int);
134157117Smjacobstatic int mpt_enable_ioc(struct mpt_softc *mpt, int);
135147883Sscottl
136147883Sscottl/************************* Personality Module Support *************************/
137147883Sscottl/*
138147883Sscottl * We include one extra entry that is guaranteed to be NULL
139147883Sscottl * to simplify our itterator.
140147883Sscottl */
141147883Sscottlstatic struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
142147883Sscottlstatic __inline struct mpt_personality*
143147883Sscottl	mpt_pers_find(struct mpt_softc *, u_int);
144147883Sscottlstatic __inline struct mpt_personality*
145147883Sscottl	mpt_pers_find_reverse(struct mpt_softc *, u_int);
146147883Sscottl
147147883Sscottlstatic __inline struct mpt_personality *
148147883Sscottlmpt_pers_find(struct mpt_softc *mpt, u_int start_at)
149101704Smjacob{
150147883Sscottl	KASSERT(start_at <= MPT_MAX_PERSONALITIES,
151231629Smarius		("mpt_pers_find: starting position out of range"));
152147883Sscottl
153147883Sscottl	while (start_at < MPT_MAX_PERSONALITIES
154147883Sscottl	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
155147883Sscottl		start_at++;
156147883Sscottl	}
157147883Sscottl	return (mpt_personalities[start_at]);
158147883Sscottl}
159147883Sscottl
160147883Sscottl/*
161157117Smjacob * Used infrequently, so no need to optimize like a forward
162147883Sscottl * traversal where we use the MAX+1 is guaranteed to be NULL
163147883Sscottl * trick.
164147883Sscottl */
165147883Sscottlstatic __inline struct mpt_personality *
166147883Sscottlmpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
167147883Sscottl{
168147883Sscottl	while (start_at < MPT_MAX_PERSONALITIES
169147883Sscottl	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
170147883Sscottl		start_at--;
171147883Sscottl	}
172147883Sscottl	if (start_at < MPT_MAX_PERSONALITIES)
173147883Sscottl		return (mpt_personalities[start_at]);
174147883Sscottl	return (NULL);
175147883Sscottl}
176147883Sscottl
177147883Sscottl#define MPT_PERS_FOREACH(mpt, pers)				\
178147883Sscottl	for (pers = mpt_pers_find(mpt, /*start_at*/0);		\
179147883Sscottl	     pers != NULL;					\
180147883Sscottl	     pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
181147883Sscottl
182147883Sscottl#define MPT_PERS_FOREACH_REVERSE(mpt, pers)				\
183147883Sscottl	for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
184147883Sscottl	     pers != NULL;						\
185147883Sscottl	     pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
186147883Sscottl
187147883Sscottlstatic mpt_load_handler_t      mpt_stdload;
188147883Sscottlstatic mpt_probe_handler_t     mpt_stdprobe;
189147883Sscottlstatic mpt_attach_handler_t    mpt_stdattach;
190157117Smjacobstatic mpt_enable_handler_t    mpt_stdenable;
191162133Smjacobstatic mpt_ready_handler_t     mpt_stdready;
192147883Sscottlstatic mpt_event_handler_t     mpt_stdevent;
193147883Sscottlstatic mpt_reset_handler_t     mpt_stdreset;
194147883Sscottlstatic mpt_shutdown_handler_t  mpt_stdshutdown;
195147883Sscottlstatic mpt_detach_handler_t    mpt_stddetach;
196147883Sscottlstatic mpt_unload_handler_t    mpt_stdunload;
197147883Sscottlstatic struct mpt_personality mpt_default_personality =
198147883Sscottl{
199147883Sscottl	.load		= mpt_stdload,
200147883Sscottl	.probe		= mpt_stdprobe,
201147883Sscottl	.attach		= mpt_stdattach,
202157117Smjacob	.enable		= mpt_stdenable,
203162133Smjacob	.ready		= mpt_stdready,
204147883Sscottl	.event		= mpt_stdevent,
205147883Sscottl	.reset		= mpt_stdreset,
206147883Sscottl	.shutdown	= mpt_stdshutdown,
207147883Sscottl	.detach		= mpt_stddetach,
208147883Sscottl	.unload		= mpt_stdunload
209147883Sscottl};
210147883Sscottl
211147883Sscottlstatic mpt_load_handler_t      mpt_core_load;
212147883Sscottlstatic mpt_attach_handler_t    mpt_core_attach;
213157117Smjacobstatic mpt_enable_handler_t    mpt_core_enable;
214147883Sscottlstatic mpt_reset_handler_t     mpt_core_ioc_reset;
215147883Sscottlstatic mpt_event_handler_t     mpt_core_event;
216147883Sscottlstatic mpt_shutdown_handler_t  mpt_core_shutdown;
217147883Sscottlstatic mpt_shutdown_handler_t  mpt_core_detach;
218147883Sscottlstatic mpt_unload_handler_t    mpt_core_unload;
219147883Sscottlstatic struct mpt_personality mpt_core_personality =
220147883Sscottl{
221147883Sscottl	.name		= "mpt_core",
222147883Sscottl	.load		= mpt_core_load,
223177808Sscottl//	.attach		= mpt_core_attach,
224177808Sscottl//	.enable		= mpt_core_enable,
225147883Sscottl	.event		= mpt_core_event,
226147883Sscottl	.reset		= mpt_core_ioc_reset,
227147883Sscottl	.shutdown	= mpt_core_shutdown,
228147883Sscottl	.detach		= mpt_core_detach,
229147883Sscottl	.unload		= mpt_core_unload,
230147883Sscottl};
231147883Sscottl
232147883Sscottl/*
233147883Sscottl * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
234147883Sscottl * ordering information.  We want the core to always register FIRST.
235147883Sscottl * other modules are set to SI_ORDER_SECOND.
236147883Sscottl */
237147883Sscottlstatic moduledata_t mpt_core_mod = {
238147883Sscottl	"mpt_core", mpt_modevent, &mpt_core_personality
239147883Sscottl};
240147883SscottlDECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
241147883SscottlMODULE_VERSION(mpt_core, 1);
242147883Sscottl
243157117Smjacob#define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
244147883Sscottl
245147883Sscottlint
246147883Sscottlmpt_modevent(module_t mod, int type, void *data)
247147883Sscottl{
248147883Sscottl	struct mpt_personality *pers;
249147883Sscottl	int error;
250147883Sscottl
251147883Sscottl	pers = (struct mpt_personality *)data;
252147883Sscottl
253147883Sscottl	error = 0;
254147883Sscottl	switch (type) {
255147883Sscottl	case MOD_LOAD:
256147883Sscottl	{
257147883Sscottl		mpt_load_handler_t **def_handler;
258147883Sscottl		mpt_load_handler_t **pers_handler;
259147883Sscottl		int i;
260147883Sscottl
261147883Sscottl		for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
262147883Sscottl			if (mpt_personalities[i] == NULL)
263147883Sscottl				break;
264147883Sscottl		}
265147883Sscottl		if (i >= MPT_MAX_PERSONALITIES) {
266147883Sscottl			error = ENOMEM;
267147883Sscottl			break;
268147883Sscottl		}
269147883Sscottl		pers->id = i;
270147883Sscottl		mpt_personalities[i] = pers;
271147883Sscottl
272147883Sscottl		/* Install standard/noop handlers for any NULL entries. */
273147883Sscottl		def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
274147883Sscottl		pers_handler = MPT_PERS_FIRST_HANDLER(pers);
275147883Sscottl		while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
276147883Sscottl			if (*pers_handler == NULL)
277147883Sscottl				*pers_handler = *def_handler;
278147883Sscottl			pers_handler++;
279147883Sscottl			def_handler++;
280147883Sscottl		}
281147883Sscottl
282147883Sscottl		error = (pers->load(pers));
283147883Sscottl		if (error != 0)
284147883Sscottl			mpt_personalities[i] = NULL;
285147883Sscottl		break;
286147883Sscottl	}
287147883Sscottl	case MOD_SHUTDOWN:
288147883Sscottl		break;
289147883Sscottl	case MOD_QUIESCE:
290147883Sscottl		break;
291147883Sscottl	case MOD_UNLOAD:
292147883Sscottl		error = pers->unload(pers);
293147883Sscottl		mpt_personalities[pers->id] = NULL;
294147883Sscottl		break;
295147883Sscottl	default:
296147883Sscottl		error = EINVAL;
297147883Sscottl		break;
298147883Sscottl	}
299147883Sscottl	return (error);
300147883Sscottl}
301147883Sscottl
302224493Smariusstatic int
303147883Sscottlmpt_stdload(struct mpt_personality *pers)
304147883Sscottl{
305224493Smarius
306220945Smarius	/* Load is always successful. */
307147883Sscottl	return (0);
308147883Sscottl}
309147883Sscottl
310224493Smariusstatic int
311147883Sscottlmpt_stdprobe(struct mpt_softc *mpt)
312147883Sscottl{
313224493Smarius
314220945Smarius	/* Probe is always successful. */
315147883Sscottl	return (0);
316147883Sscottl}
317147883Sscottl
318224493Smariusstatic int
319147883Sscottlmpt_stdattach(struct mpt_softc *mpt)
320147883Sscottl{
321224493Smarius
322220945Smarius	/* Attach is always successful. */
323147883Sscottl	return (0);
324147883Sscottl}
325147883Sscottl
326224493Smariusstatic int
327157117Smjacobmpt_stdenable(struct mpt_softc *mpt)
328157117Smjacob{
329224493Smarius
330220945Smarius	/* Enable is always successful. */
331157117Smjacob	return (0);
332157117Smjacob}
333157117Smjacob
334224493Smariusstatic void
335162133Smjacobmpt_stdready(struct mpt_softc *mpt)
336162133Smjacob{
337224493Smarius
338162133Smjacob}
339162133Smjacob
340224493Smariusstatic int
341155521Smjacobmpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
342147883Sscottl{
343224493Smarius
344155521Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
345147883Sscottl	/* Event was not for us. */
346147883Sscottl	return (0);
347147883Sscottl}
348147883Sscottl
349224493Smariusstatic void
350147883Sscottlmpt_stdreset(struct mpt_softc *mpt, int type)
351147883Sscottl{
352224493Smarius
353147883Sscottl}
354147883Sscottl
355224493Smariusstatic void
356147883Sscottlmpt_stdshutdown(struct mpt_softc *mpt)
357147883Sscottl{
358224493Smarius
359147883Sscottl}
360147883Sscottl
361224493Smariusstatic void
362147883Sscottlmpt_stddetach(struct mpt_softc *mpt)
363147883Sscottl{
364224493Smarius
365147883Sscottl}
366147883Sscottl
367224493Smariusstatic int
368147883Sscottlmpt_stdunload(struct mpt_personality *pers)
369147883Sscottl{
370224493Smarius
371220945Smarius	/* Unload is always successful. */
372147883Sscottl	return (0);
373147883Sscottl}
374147883Sscottl
375162133Smjacob/*
376162133Smjacob * Post driver attachment, we may want to perform some global actions.
377162133Smjacob * Here is the hook to do so.
378162133Smjacob */
379162133Smjacob
380162133Smjacobstatic void
381162133Smjacobmpt_postattach(void *unused)
382162133Smjacob{
383162133Smjacob	struct mpt_softc *mpt;
384162133Smjacob	struct mpt_personality *pers;
385162133Smjacob
386162133Smjacob	TAILQ_FOREACH(mpt, &mpt_tailq, links) {
387162133Smjacob		MPT_PERS_FOREACH(mpt, pers)
388162133Smjacob			pers->ready(mpt);
389162133Smjacob	}
390162133Smjacob}
391162133SmjacobSYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL);
392162133Smjacob
393147883Sscottl/******************************* Bus DMA Support ******************************/
394147883Sscottlvoid
395147883Sscottlmpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
396147883Sscottl{
397147883Sscottl	struct mpt_map_info *map_info;
398147883Sscottl
399147883Sscottl	map_info = (struct mpt_map_info *)arg;
400147883Sscottl	map_info->error = error;
401147883Sscottl	map_info->phys = segs->ds_addr;
402147883Sscottl}
403147883Sscottl
404147883Sscottl/**************************** Reply/Event Handling ****************************/
405147883Sscottlint
406147883Sscottlmpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
407147883Sscottl		     mpt_handler_t handler, uint32_t *phandler_id)
408147883Sscottl{
409147883Sscottl
410147883Sscottl	switch (type) {
411147883Sscottl	case MPT_HANDLER_REPLY:
412147883Sscottl	{
413147883Sscottl		u_int cbi;
414147883Sscottl		u_int free_cbi;
415147883Sscottl
416147883Sscottl		if (phandler_id == NULL)
417147883Sscottl			return (EINVAL);
418147883Sscottl
419147883Sscottl		free_cbi = MPT_HANDLER_ID_NONE;
420147883Sscottl		for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
421147883Sscottl			/*
422147883Sscottl			 * If the same handler is registered multiple
423147883Sscottl			 * times, don't error out.  Just return the
424147883Sscottl			 * index of the original registration.
425147883Sscottl			 */
426147883Sscottl			if (mpt_reply_handlers[cbi] == handler.reply_handler) {
427147883Sscottl				*phandler_id = MPT_CBI_TO_HID(cbi);
428147883Sscottl				return (0);
429147883Sscottl			}
430147883Sscottl
431147883Sscottl			/*
432147883Sscottl			 * Fill from the front in the hope that
433147883Sscottl			 * all registered handlers consume only a
434147883Sscottl			 * single cache line.
435147883Sscottl			 *
436147883Sscottl			 * We don't break on the first empty slot so
437147883Sscottl			 * that the full table is checked to see if
438147883Sscottl			 * this handler was previously registered.
439147883Sscottl			 */
440157117Smjacob			if (free_cbi == MPT_HANDLER_ID_NONE &&
441157117Smjacob			    (mpt_reply_handlers[cbi]
442147883Sscottl			  == mpt_default_reply_handler))
443147883Sscottl				free_cbi = cbi;
444147883Sscottl		}
445157117Smjacob		if (free_cbi == MPT_HANDLER_ID_NONE) {
446147883Sscottl			return (ENOMEM);
447157117Smjacob		}
448147883Sscottl		mpt_reply_handlers[free_cbi] = handler.reply_handler;
449147883Sscottl		*phandler_id = MPT_CBI_TO_HID(free_cbi);
450147883Sscottl		break;
451147883Sscottl	}
452147883Sscottl	default:
453147883Sscottl		mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
454147883Sscottl		return (EINVAL);
455147883Sscottl	}
456147883Sscottl	return (0);
457147883Sscottl}
458147883Sscottl
459147883Sscottlint
460147883Sscottlmpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
461147883Sscottl		       mpt_handler_t handler, uint32_t handler_id)
462147883Sscottl{
463147883Sscottl
464147883Sscottl	switch (type) {
465147883Sscottl	case MPT_HANDLER_REPLY:
466147883Sscottl	{
467147883Sscottl		u_int cbi;
468147883Sscottl
469147883Sscottl		cbi = MPT_CBI(handler_id);
470147883Sscottl		if (cbi >= MPT_NUM_REPLY_HANDLERS
471147883Sscottl		 || mpt_reply_handlers[cbi] != handler.reply_handler)
472147883Sscottl			return (ENOENT);
473147883Sscottl		mpt_reply_handlers[cbi] = mpt_default_reply_handler;
474147883Sscottl		break;
475147883Sscottl	}
476147883Sscottl	default:
477147883Sscottl		mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
478147883Sscottl		return (EINVAL);
479147883Sscottl	}
480147883Sscottl	return (0);
481147883Sscottl}
482147883Sscottl
483147883Sscottlstatic int
484147883Sscottlmpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
485157117Smjacob	uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
486147883Sscottl{
487224493Smarius
488157117Smjacob	mpt_prt(mpt,
489157117Smjacob	    "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
490157117Smjacob	    req, req->serno, reply_desc, reply_frame);
491147883Sscottl
492147883Sscottl	if (reply_frame != NULL)
493147883Sscottl		mpt_dump_reply_frame(mpt, reply_frame);
494147883Sscottl
495157117Smjacob	mpt_prt(mpt, "Reply Frame Ignored\n");
496147883Sscottl
497147883Sscottl	return (/*free_reply*/TRUE);
498147883Sscottl}
499147883Sscottl
500147883Sscottlstatic int
501147883Sscottlmpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
502157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
503147883Sscottl{
504224493Smarius
505147883Sscottl	if (req != NULL) {
506147883Sscottl		if (reply_frame != NULL) {
507147883Sscottl			MSG_CONFIG *cfgp;
508147883Sscottl			MSG_CONFIG_REPLY *reply;
509147883Sscottl
510147883Sscottl			cfgp = (MSG_CONFIG *)req->req_vbuf;
511147883Sscottl			reply = (MSG_CONFIG_REPLY *)reply_frame;
512147883Sscottl			req->IOCStatus = le16toh(reply_frame->IOCStatus);
513147883Sscottl			bcopy(&reply->Header, &cfgp->Header,
514147883Sscottl			      sizeof(cfgp->Header));
515170252Sscottl			cfgp->ExtPageLength = reply->ExtPageLength;
516170252Sscottl			cfgp->ExtPageType = reply->ExtPageType;
517147883Sscottl		}
518147883Sscottl		req->state &= ~REQ_STATE_QUEUED;
519147883Sscottl		req->state |= REQ_STATE_DONE;
520147883Sscottl		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
521157354Smjacob		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
522147883Sscottl			wakeup(req);
523160396Smjacob		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
524160396Smjacob			/*
525160396Smjacob			 * Whew- we can free this request (late completion)
526160396Smjacob			 */
527160396Smjacob			mpt_free_request(mpt, req);
528157354Smjacob		}
529147883Sscottl	}
530147883Sscottl
531157354Smjacob	return (TRUE);
532147883Sscottl}
533147883Sscottl
534147883Sscottlstatic int
535147883Sscottlmpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
536157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
537147883Sscottl{
538224493Smarius
539147883Sscottl	/* Nothing to be done. */
540157354Smjacob	return (TRUE);
541147883Sscottl}
542147883Sscottl
543147883Sscottlstatic int
544147883Sscottlmpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
545157117Smjacob    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
546147883Sscottl{
547147883Sscottl	int free_reply;
548147883Sscottl
549157354Smjacob	KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
550157354Smjacob	KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
551147883Sscottl
552147883Sscottl	free_reply = TRUE;
553147883Sscottl	switch (reply_frame->Function) {
554147883Sscottl	case MPI_FUNCTION_EVENT_NOTIFICATION:
555147883Sscottl	{
556147883Sscottl		MSG_EVENT_NOTIFY_REPLY *msg;
557147883Sscottl		struct mpt_personality *pers;
558147883Sscottl		u_int handled;
559147883Sscottl
560147883Sscottl		handled = 0;
561147883Sscottl		msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
562164315Sjb		msg->EventDataLength = le16toh(msg->EventDataLength);
563164315Sjb		msg->IOCStatus = le16toh(msg->IOCStatus);
564164315Sjb		msg->IOCLogInfo = le32toh(msg->IOCLogInfo);
565164315Sjb		msg->Event = le32toh(msg->Event);
566147883Sscottl		MPT_PERS_FOREACH(mpt, pers)
567147883Sscottl			handled += pers->event(mpt, req, msg);
568147883Sscottl
569155521Smjacob		if (handled == 0 && mpt->mpt_pers_mask == 0) {
570156301Smjacob			mpt_lprt(mpt, MPT_PRT_INFO,
571155521Smjacob				"No Handlers For Any Event Notify Frames. "
572155521Smjacob				"Event %#x (ACK %sequired).\n",
573155521Smjacob				msg->Event, msg->AckRequired? "r" : "not r");
574155521Smjacob		} else if (handled == 0) {
575173303Sscottl			mpt_lprt(mpt,
576173303Sscottl				msg->AckRequired? MPT_PRT_WARN : MPT_PRT_INFO,
577155521Smjacob				"Unhandled Event Notify Frame. Event %#x "
578155521Smjacob				"(ACK %sequired).\n",
579155521Smjacob				msg->Event, msg->AckRequired? "r" : "not r");
580155521Smjacob		}
581147883Sscottl
582147883Sscottl		if (msg->AckRequired) {
583147883Sscottl			request_t *ack_req;
584147883Sscottl			uint32_t context;
585147883Sscottl
586164315Sjb			context = req->index | MPT_REPLY_HANDLER_EVENTS;
587157354Smjacob			ack_req = mpt_get_request(mpt, FALSE);
588147883Sscottl			if (ack_req == NULL) {
589147883Sscottl				struct mpt_evtf_record *evtf;
590147883Sscottl
591147883Sscottl				evtf = (struct mpt_evtf_record *)reply_frame;
592147883Sscottl				evtf->context = context;
593147883Sscottl				LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
594147883Sscottl				free_reply = FALSE;
595147883Sscottl				break;
596147883Sscottl			}
597147883Sscottl			mpt_send_event_ack(mpt, ack_req, msg, context);
598157354Smjacob			/*
599157354Smjacob			 * Don't check for CONTINUATION_REPLY here
600157354Smjacob			 */
601157354Smjacob			return (free_reply);
602147883Sscottl		}
603147883Sscottl		break;
604147883Sscottl	}
605147883Sscottl	case MPI_FUNCTION_PORT_ENABLE:
606157354Smjacob		mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
607147883Sscottl		break;
608147883Sscottl	case MPI_FUNCTION_EVENT_ACK:
609147883Sscottl		break;
610147883Sscottl	default:
611157354Smjacob		mpt_prt(mpt, "unknown event function: %x\n",
612147883Sscottl			reply_frame->Function);
613147883Sscottl		break;
614147883Sscottl	}
615147883Sscottl
616157354Smjacob	/*
617157354Smjacob	 * I'm not sure that this continuation stuff works as it should.
618157354Smjacob	 *
619157354Smjacob	 * I've had FC async events occur that free the frame up because
620157354Smjacob	 * the continuation bit isn't set, and then additional async events
621157354Smjacob	 * then occur using the same context. As you might imagine, this
622157354Smjacob	 * leads to Very Bad Thing.
623157354Smjacob	 *
624157354Smjacob	 *  Let's just be safe for now and not free them up until we figure
625157354Smjacob	 * out what's actually happening here.
626157354Smjacob	 */
627157354Smjacob#if	0
628157354Smjacob	if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
629147883Sscottl		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
630157354Smjacob		mpt_free_request(mpt, req);
631157354Smjacob		mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
632157354Smjacob		    reply_frame->Function, req, req->serno);
633157354Smjacob		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
634157354Smjacob			MSG_EVENT_NOTIFY_REPLY *msg =
635157354Smjacob			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
636157354Smjacob			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
637157354Smjacob			    msg->Event, msg->AckRequired);
638157354Smjacob		}
639157354Smjacob	} else {
640157354Smjacob		mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
641157354Smjacob		    reply_frame->Function, req, req->serno);
642157354Smjacob		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
643157354Smjacob			MSG_EVENT_NOTIFY_REPLY *msg =
644157354Smjacob			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
645157354Smjacob			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
646157354Smjacob			    msg->Event, msg->AckRequired);
647157354Smjacob		}
648157354Smjacob		mpt_prtc(mpt, "\n");
649147883Sscottl	}
650157354Smjacob#endif
651147883Sscottl	return (free_reply);
652147883Sscottl}
653147883Sscottl
654147883Sscottl/*
655147883Sscottl * Process an asynchronous event from the IOC.
656147883Sscottl */
657147883Sscottlstatic int
658147883Sscottlmpt_core_event(struct mpt_softc *mpt, request_t *req,
659147883Sscottl	       MSG_EVENT_NOTIFY_REPLY *msg)
660147883Sscottl{
661224493Smarius
662155521Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
663155521Smjacob                 msg->Event & 0xFF);
664147883Sscottl	switch(msg->Event & 0xFF) {
665147883Sscottl	case MPI_EVENT_NONE:
666147883Sscottl		break;
667147883Sscottl	case MPI_EVENT_LOG_DATA:
668147883Sscottl	{
669147883Sscottl		int i;
670147883Sscottl
671220945Smarius		/* Some error occurred that LSI wants logged */
672147883Sscottl		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
673147883Sscottl			msg->IOCLogInfo);
674147883Sscottl		mpt_prt(mpt, "\tEvtLogData: Event Data:");
675147883Sscottl		for (i = 0; i < msg->EventDataLength; i++)
676147883Sscottl			mpt_prtc(mpt, "  %08x", msg->Data[i]);
677147883Sscottl		mpt_prtc(mpt, "\n");
678147883Sscottl		break;
679147883Sscottl	}
680147883Sscottl	case MPI_EVENT_EVENT_CHANGE:
681147883Sscottl		/*
682147883Sscottl		 * This is just an acknowledgement
683147883Sscottl		 * of our mpt_send_event_request.
684147883Sscottl		 */
685147883Sscottl		break;
686155521Smjacob	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
687155521Smjacob		break;
688147883Sscottl	default:
689157354Smjacob		return (0);
690147883Sscottl		break;
691147883Sscottl	}
692157354Smjacob	return (1);
693147883Sscottl}
694147883Sscottl
695147883Sscottlstatic void
696147883Sscottlmpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
697147883Sscottl		   MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
698147883Sscottl{
699147883Sscottl	MSG_EVENT_ACK *ackp;
700147883Sscottl
701147883Sscottl	ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
702157354Smjacob	memset(ackp, 0, sizeof (*ackp));
703147883Sscottl	ackp->Function = MPI_FUNCTION_EVENT_ACK;
704164315Sjb	ackp->Event = htole32(msg->Event);
705164315Sjb	ackp->EventContext = htole32(msg->EventContext);
706164315Sjb	ackp->MsgContext = htole32(context);
707147883Sscottl	mpt_check_doorbell(mpt);
708147883Sscottl	mpt_send_cmd(mpt, ack_req);
709147883Sscottl}
710147883Sscottl
711147883Sscottl/***************************** Interrupt Handling *****************************/
712147883Sscottlvoid
713147883Sscottlmpt_intr(void *arg)
714147883Sscottl{
715147883Sscottl	struct mpt_softc *mpt;
716157117Smjacob	uint32_t reply_desc;
717157117Smjacob	int ntrips = 0;
718147883Sscottl
719147883Sscottl	mpt = (struct mpt_softc *)arg;
720157662Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
721169293Smjacob	MPT_LOCK_ASSERT(mpt);
722169293Smjacob
723147883Sscottl	while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
724147883Sscottl		request_t	  *req;
725147883Sscottl		MSG_DEFAULT_REPLY *reply_frame;
726147883Sscottl		uint32_t	   reply_baddr;
727157117Smjacob		uint32_t           ctxt_idx;
728147883Sscottl		u_int		   cb_index;
729147883Sscottl		u_int		   req_index;
730219335Smarius		u_int		   offset;
731147883Sscottl		int		   free_rf;
732147883Sscottl
733147883Sscottl		req = NULL;
734147883Sscottl		reply_frame = NULL;
735147883Sscottl		reply_baddr = 0;
736219335Smarius		offset = 0;
737147883Sscottl		if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
738147883Sscottl			/*
739219335Smarius			 * Ensure that the reply frame is coherent.
740147883Sscottl			 */
741157354Smjacob			reply_baddr = MPT_REPLY_BADDR(reply_desc);
742147883Sscottl			offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
743157117Smjacob			bus_dmamap_sync_range(mpt->reply_dmat,
744157117Smjacob			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
745157117Smjacob			    BUS_DMASYNC_POSTREAD);
746147883Sscottl			reply_frame = MPT_REPLY_OTOV(mpt, offset);
747157117Smjacob			ctxt_idx = le32toh(reply_frame->MsgContext);
748157117Smjacob		} else {
749157117Smjacob			uint32_t type;
750157117Smjacob
751157117Smjacob			type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
752157117Smjacob			ctxt_idx = reply_desc;
753157117Smjacob			mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
754157117Smjacob				    reply_desc);
755157117Smjacob
756157117Smjacob			switch (type) {
757157117Smjacob			case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
758157117Smjacob				ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
759157117Smjacob				break;
760157117Smjacob			case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
761157117Smjacob				ctxt_idx = GET_IO_INDEX(reply_desc);
762157117Smjacob				if (mpt->tgt_cmd_ptrs == NULL) {
763157117Smjacob					mpt_prt(mpt,
764157117Smjacob					    "mpt_intr: no target cmd ptrs\n");
765157117Smjacob					reply_desc = MPT_REPLY_EMPTY;
766157117Smjacob					break;
767157117Smjacob				}
768157117Smjacob				if (ctxt_idx >= mpt->tgt_cmds_allocated) {
769157117Smjacob					mpt_prt(mpt,
770157117Smjacob					    "mpt_intr: bad tgt cmd ctxt %u\n",
771157117Smjacob					    ctxt_idx);
772157117Smjacob					reply_desc = MPT_REPLY_EMPTY;
773157117Smjacob					ntrips = 1000;
774157117Smjacob					break;
775157117Smjacob				}
776157117Smjacob				req = mpt->tgt_cmd_ptrs[ctxt_idx];
777157117Smjacob				if (req == NULL) {
778157117Smjacob					mpt_prt(mpt, "no request backpointer "
779157117Smjacob					    "at index %u", ctxt_idx);
780157117Smjacob					reply_desc = MPT_REPLY_EMPTY;
781157117Smjacob					ntrips = 1000;
782157117Smjacob					break;
783157117Smjacob				}
784157117Smjacob				/*
785157117Smjacob				 * Reformulate ctxt_idx to be just as if
786157117Smjacob				 * it were another type of context reply
787157117Smjacob				 * so the code below will find the request
788157117Smjacob				 * via indexing into the pool.
789157117Smjacob				 */
790157117Smjacob				ctxt_idx =
791157117Smjacob				    req->index | mpt->scsi_tgt_handler_id;
792157117Smjacob				req = NULL;
793157117Smjacob				break;
794157117Smjacob			case MPI_CONTEXT_REPLY_TYPE_LAN:
795157117Smjacob				mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
796157117Smjacob				    reply_desc);
797157117Smjacob				reply_desc = MPT_REPLY_EMPTY;
798157117Smjacob				break;
799157117Smjacob			default:
800157117Smjacob				mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
801157117Smjacob				reply_desc = MPT_REPLY_EMPTY;
802157117Smjacob				break;
803157117Smjacob			}
804157117Smjacob			if (reply_desc == MPT_REPLY_EMPTY) {
805157117Smjacob				if (ntrips++ > 1000) {
806157117Smjacob					break;
807157117Smjacob				}
808157117Smjacob				continue;
809157117Smjacob			}
810147883Sscottl		}
811157117Smjacob
812157117Smjacob		cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
813157117Smjacob		req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
814157117Smjacob		if (req_index < MPT_MAX_REQUESTS(mpt)) {
815147883Sscottl			req = &mpt->request_pool[req_index];
816157354Smjacob		} else {
817157354Smjacob			mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
818157354Smjacob			    " 0x%x)\n", req_index, reply_desc);
819157117Smjacob		}
820147883Sscottl
821219335Smarius		bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
822219335Smarius		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
823157117Smjacob		free_rf = mpt_reply_handlers[cb_index](mpt, req,
824157117Smjacob		    reply_desc, reply_frame);
825147883Sscottl
826157354Smjacob		if (reply_frame != NULL && free_rf) {
827219335Smarius			bus_dmamap_sync_range(mpt->reply_dmat,
828219335Smarius			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
829219335Smarius			    BUS_DMASYNC_PREREAD);
830147883Sscottl			mpt_free_reply(mpt, reply_baddr);
831157354Smjacob		}
832157117Smjacob
833157117Smjacob		/*
834157117Smjacob		 * If we got ourselves disabled, don't get stuck in a loop
835157117Smjacob		 */
836157117Smjacob		if (mpt->disabled) {
837157117Smjacob			mpt_disable_ints(mpt);
838157117Smjacob			break;
839157117Smjacob		}
840157117Smjacob		if (ntrips++ > 1000) {
841157117Smjacob			break;
842157117Smjacob		}
843147883Sscottl	}
844157662Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
845147883Sscottl}
846147883Sscottl
847147883Sscottl/******************************* Error Recovery *******************************/
848147883Sscottlvoid
849147883Sscottlmpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
850147883Sscottl			    u_int iocstatus)
851147883Sscottl{
852147883Sscottl	MSG_DEFAULT_REPLY  ioc_status_frame;
853147883Sscottl	request_t	  *req;
854147883Sscottl
855157354Smjacob	memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
856147883Sscottl	ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
857156104Smjacob	ioc_status_frame.IOCStatus = iocstatus;
858147883Sscottl	while((req = TAILQ_FIRST(chain)) != NULL) {
859147883Sscottl		MSG_REQUEST_HEADER *msg_hdr;
860147883Sscottl		u_int		    cb_index;
861157354Smjacob
862219335Smarius		bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
863219335Smarius		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
864147883Sscottl		msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
865156104Smjacob		ioc_status_frame.Function = msg_hdr->Function;
866156104Smjacob		ioc_status_frame.MsgContext = msg_hdr->MsgContext;
867147883Sscottl		cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
868157117Smjacob		mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
869157117Smjacob		    &ioc_status_frame);
870213105Smarius		if (mpt_req_on_pending_list(mpt, req) != 0)
871213105Smarius			TAILQ_REMOVE(chain, req, links);
872147883Sscottl	}
873147883Sscottl}
874147883Sscottl
875147883Sscottl/********************************* Diagnostics ********************************/
876147883Sscottl/*
877147883Sscottl * Perform a diagnostic dump of a reply frame.
878147883Sscottl */
879147883Sscottlvoid
880147883Sscottlmpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
881147883Sscottl{
882224493Smarius
883147883Sscottl	mpt_prt(mpt, "Address Reply:\n");
884147883Sscottl	mpt_print_reply(reply_frame);
885147883Sscottl}
886147883Sscottl
887147883Sscottl/******************************* Doorbell Access ******************************/
888147883Sscottlstatic __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
889147883Sscottlstatic __inline  uint32_t mpt_rd_intr(struct mpt_softc *mpt);
890147883Sscottl
891147883Sscottlstatic __inline uint32_t
892147883Sscottlmpt_rd_db(struct mpt_softc *mpt)
893147883Sscottl{
894224493Smarius
895101704Smjacob	return mpt_read(mpt, MPT_OFFSET_DOORBELL);
896101704Smjacob}
897101704Smjacob
898147883Sscottlstatic __inline uint32_t
899147883Sscottlmpt_rd_intr(struct mpt_softc *mpt)
900101704Smjacob{
901224493Smarius
902101704Smjacob	return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
903101704Smjacob}
904101704Smjacob
905101704Smjacob/* Busy wait for a door bell to be read by IOC */
906101704Smjacobstatic int
907147883Sscottlmpt_wait_db_ack(struct mpt_softc *mpt)
908101704Smjacob{
909101704Smjacob	int i;
910224493Smarius
911101704Smjacob	for (i=0; i < MPT_MAX_WAIT; i++) {
912101704Smjacob		if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
913101704Smjacob			maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
914157117Smjacob			return (MPT_OK);
915101704Smjacob		}
916157117Smjacob		DELAY(200);
917101704Smjacob	}
918157117Smjacob	return (MPT_FAIL);
919101704Smjacob}
920101704Smjacob
921101704Smjacob/* Busy wait for a door bell interrupt */
922101704Smjacobstatic int
923147883Sscottlmpt_wait_db_int(struct mpt_softc *mpt)
924101704Smjacob{
925101704Smjacob	int i;
926224493Smarius
927164837Smjacob	for (i = 0; i < MPT_MAX_WAIT; i++) {
928101704Smjacob		if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
929101704Smjacob			maxwait_int = i > maxwait_int ? i : maxwait_int;
930101704Smjacob			return MPT_OK;
931101704Smjacob		}
932101704Smjacob		DELAY(100);
933101704Smjacob	}
934157117Smjacob	return (MPT_FAIL);
935101704Smjacob}
936101704Smjacob
937101704Smjacob/* Wait for IOC to transition to a give state */
938101704Smjacobvoid
939147883Sscottlmpt_check_doorbell(struct mpt_softc *mpt)
940101704Smjacob{
941147883Sscottl	uint32_t db = mpt_rd_db(mpt);
942224493Smarius
943101704Smjacob	if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
944147883Sscottl		mpt_prt(mpt, "Device not running\n");
945101704Smjacob		mpt_print_db(db);
946101704Smjacob	}
947101704Smjacob}
948101704Smjacob
949101704Smjacob/* Wait for IOC to transition to a give state */
950101704Smjacobstatic int
951147883Sscottlmpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
952101704Smjacob{
953101704Smjacob	int i;
954101704Smjacob
955101704Smjacob	for (i = 0; i < MPT_MAX_WAIT; i++) {
956147883Sscottl		uint32_t db = mpt_rd_db(mpt);
957101704Smjacob		if (MPT_STATE(db) == state) {
958101704Smjacob			maxwait_state = i > maxwait_state ? i : maxwait_state;
959101704Smjacob			return (MPT_OK);
960101704Smjacob		}
961101704Smjacob		DELAY(100);
962101704Smjacob	}
963101704Smjacob	return (MPT_FAIL);
964101704Smjacob}
965101704Smjacob
966101704Smjacob
967147883Sscottl/************************* Intialization/Configuration ************************/
968147883Sscottlstatic int mpt_download_fw(struct mpt_softc *mpt);
969147883Sscottl
970101704Smjacob/* Issue the reset COMMAND to the IOC */
971147883Sscottlstatic int
972147883Sscottlmpt_soft_reset(struct mpt_softc *mpt)
973101704Smjacob{
974224493Smarius
975147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
976101704Smjacob
977101704Smjacob	/* Have to use hard reset if we are not in Running state */
978101704Smjacob	if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
979147883Sscottl		mpt_prt(mpt, "soft reset failed: device not running\n");
980157117Smjacob		return (MPT_FAIL);
981101704Smjacob	}
982101704Smjacob
983101704Smjacob	/* If door bell is in use we don't have a chance of getting
984101704Smjacob	 * a word in since the IOC probably crashed in message
985101704Smjacob	 * processing. So don't waste our time.
986101704Smjacob	 */
987101704Smjacob	if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
988147883Sscottl		mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
989157117Smjacob		return (MPT_FAIL);
990101704Smjacob	}
991101704Smjacob
992101704Smjacob	/* Send the reset request to the IOC */
993101704Smjacob	mpt_write(mpt, MPT_OFFSET_DOORBELL,
994101704Smjacob	    MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
995101704Smjacob	if (mpt_wait_db_ack(mpt) != MPT_OK) {
996147883Sscottl		mpt_prt(mpt, "soft reset failed: ack timeout\n");
997157117Smjacob		return (MPT_FAIL);
998101704Smjacob	}
999101704Smjacob
1000101704Smjacob	/* Wait for the IOC to reload and come out of reset state */
1001101704Smjacob	if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
1002147883Sscottl		mpt_prt(mpt, "soft reset failed: device did not restart\n");
1003157117Smjacob		return (MPT_FAIL);
1004101704Smjacob	}
1005101704Smjacob
1006101704Smjacob	return MPT_OK;
1007101704Smjacob}
1008101704Smjacob
1009147883Sscottlstatic int
1010147883Sscottlmpt_enable_diag_mode(struct mpt_softc *mpt)
1011147883Sscottl{
1012147883Sscottl	int try;
1013147883Sscottl
1014147883Sscottl	try = 20;
1015147883Sscottl	while (--try) {
1016147883Sscottl
1017147883Sscottl		if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
1018147883Sscottl			break;
1019147883Sscottl
1020147883Sscottl		/* Enable diagnostic registers */
1021147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
1022147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
1023147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
1024147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
1025147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
1026147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
1027147883Sscottl
1028147883Sscottl		DELAY(100000);
1029147883Sscottl	}
1030147883Sscottl	if (try == 0)
1031147883Sscottl		return (EIO);
1032147883Sscottl	return (0);
1033147883Sscottl}
1034147883Sscottl
1035147883Sscottlstatic void
1036147883Sscottlmpt_disable_diag_mode(struct mpt_softc *mpt)
1037147883Sscottl{
1038224493Smarius
1039147883Sscottl	mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
1040147883Sscottl}
1041147883Sscottl
1042101704Smjacob/* This is a magic diagnostic reset that resets all the ARM
1043156104Smjacob * processors in the chip.
1044101704Smjacob */
1045147883Sscottlstatic void
1046147883Sscottlmpt_hard_reset(struct mpt_softc *mpt)
1047101704Smjacob{
1048147883Sscottl	int error;
1049147883Sscottl	int wait;
1050147883Sscottl	uint32_t diagreg;
1051147883Sscottl
1052147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
1053147883Sscottl
1054231623Smarius	if (mpt->is_1078) {
1055231623Smarius		mpt_write(mpt, MPT_OFFSET_RESET_1078, 0x07);
1056231623Smarius		DELAY(1000);
1057231623Smarius		return;
1058231623Smarius	}
1059231623Smarius
1060147883Sscottl	error = mpt_enable_diag_mode(mpt);
1061147883Sscottl	if (error) {
1062147883Sscottl		mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
1063147883Sscottl		mpt_prt(mpt, "Trying to reset anyway.\n");
1064101704Smjacob	}
1065101704Smjacob
1066147883Sscottl	diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1067101704Smjacob
1068147883Sscottl	/*
1069147883Sscottl	 * This appears to be a workaround required for some
1070147883Sscottl	 * firmware or hardware revs.
1071147883Sscottl	 */
1072147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1073147883Sscottl	DELAY(1000);
1074147883Sscottl
1075101704Smjacob	/* Diag. port is now active so we can now hit the reset bit */
1076147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
1077101704Smjacob
1078147883Sscottl        /*
1079147883Sscottl         * Ensure that the reset has finished.  We delay 1ms
1080147883Sscottl         * prior to reading the register to make sure the chip
1081147883Sscottl         * has sufficiently completed its reset to handle register
1082147883Sscottl         * accesses.
1083147883Sscottl         */
1084147883Sscottl	wait = 5000;
1085147883Sscottl	do {
1086147883Sscottl		DELAY(1000);
1087147883Sscottl		diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1088147883Sscottl	} while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
1089101704Smjacob
1090147883Sscottl	if (wait == 0) {
1091147883Sscottl		mpt_prt(mpt, "WARNING - Failed hard reset! "
1092147883Sscottl			"Trying to initialize anyway.\n");
1093147883Sscottl	}
1094101704Smjacob
1095147883Sscottl	/*
1096147883Sscottl	 * If we have firmware to download, it must be loaded before
1097147883Sscottl	 * the controller will become operational.  Do so now.
1098147883Sscottl	 */
1099147883Sscottl	if (mpt->fw_image != NULL) {
1100147883Sscottl
1101147883Sscottl		error = mpt_download_fw(mpt);
1102147883Sscottl
1103147883Sscottl		if (error) {
1104147883Sscottl			mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1105147883Sscottl			mpt_prt(mpt, "Trying to initialize anyway.\n");
1106147883Sscottl		}
1107101704Smjacob	}
1108101704Smjacob
1109147883Sscottl	/*
1110147883Sscottl	 * Reseting the controller should have disabled write
1111147883Sscottl	 * access to the diagnostic registers, but disable
1112147883Sscottl	 * manually to be sure.
1113147883Sscottl	 */
1114147883Sscottl	mpt_disable_diag_mode(mpt);
1115101704Smjacob}
1116101704Smjacob
1117147883Sscottlstatic void
1118147883Sscottlmpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1119147883Sscottl{
1120224493Smarius
1121147883Sscottl	/*
1122147883Sscottl	 * Complete all pending requests with a status
1123147883Sscottl	 * appropriate for an IOC reset.
1124147883Sscottl	 */
1125147883Sscottl	mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1126147883Sscottl				   MPI_IOCSTATUS_INVALID_STATE);
1127147883Sscottl}
1128147883Sscottl
1129101704Smjacob/*
1130101704Smjacob * Reset the IOC when needed. Try software command first then if needed
1131101704Smjacob * poke at the magic diagnostic reset. Note that a hard reset resets
1132101704Smjacob * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
1133101704Smjacob * fouls up the PCI configuration registers.
1134101704Smjacob */
1135101704Smjacobint
1136147883Sscottlmpt_reset(struct mpt_softc *mpt, int reinit)
1137101704Smjacob{
1138147883Sscottl	struct	mpt_personality *pers;
1139147883Sscottl	int	ret;
1140156104Smjacob	int	retry_cnt = 0;
1141101704Smjacob
1142156104Smjacob	/*
1143156104Smjacob	 * Try a soft reset. If that fails, get out the big hammer.
1144156104Smjacob	 */
1145156104Smjacob again:
1146101704Smjacob	if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
1147156104Smjacob		int	cnt;
1148156104Smjacob		for (cnt = 0; cnt < 5; cnt++) {
1149156104Smjacob			/* Failed; do a hard reset */
1150156104Smjacob			mpt_hard_reset(mpt);
1151101704Smjacob
1152156104Smjacob			/*
1153156104Smjacob			 * Wait for the IOC to reload
1154156104Smjacob			 * and come out of reset state
1155156104Smjacob			 */
1156156104Smjacob			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
1157156104Smjacob			if (ret == MPT_OK) {
1158156104Smjacob				break;
1159156104Smjacob			}
1160156104Smjacob			/*
1161156104Smjacob			 * Okay- try to check again...
1162156104Smjacob			 */
1163156104Smjacob			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
1164156104Smjacob			if (ret == MPT_OK) {
1165156104Smjacob				break;
1166156104Smjacob			}
1167156104Smjacob			mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
1168156104Smjacob			    retry_cnt, cnt);
1169156104Smjacob		}
1170101704Smjacob	}
1171101704Smjacob
1172156104Smjacob	if (retry_cnt == 0) {
1173156104Smjacob		/*
1174156104Smjacob		 * Invoke reset handlers.  We bump the reset count so
1175156104Smjacob		 * that mpt_wait_req() understands that regardless of
1176156104Smjacob		 * the specified wait condition, it should stop its wait.
1177156104Smjacob		 */
1178156104Smjacob		mpt->reset_cnt++;
1179156104Smjacob		MPT_PERS_FOREACH(mpt, pers)
1180156104Smjacob			pers->reset(mpt, ret);
1181156104Smjacob	}
1182147883Sscottl
1183157354Smjacob	if (reinit) {
1184157117Smjacob		ret = mpt_enable_ioc(mpt, 1);
1185156104Smjacob		if (ret == MPT_OK) {
1186156104Smjacob			mpt_enable_ints(mpt);
1187156104Smjacob		}
1188156104Smjacob	}
1189156104Smjacob	if (ret != MPT_OK && retry_cnt++ < 2) {
1190156104Smjacob		goto again;
1191156104Smjacob	}
1192101704Smjacob	return ret;
1193101704Smjacob}
1194101704Smjacob
1195101704Smjacob/* Return a command buffer to the free queue */
1196101704Smjacobvoid
1197147883Sscottlmpt_free_request(struct mpt_softc *mpt, request_t *req)
1198101704Smjacob{
1199155521Smjacob	request_t *nxt;
1200147883Sscottl	struct mpt_evtf_record *record;
1201219335Smarius	uint32_t offset, reply_baddr;
1202147883Sscottl
1203103871Smjacob	if (req == NULL || req != &mpt->request_pool[req->index]) {
1204231629Smarius		panic("mpt_free_request: bad req ptr");
1205101704Smjacob	}
1206155521Smjacob	if ((nxt = req->chain) != NULL) {
1207155521Smjacob		req->chain = NULL;
1208155521Smjacob		mpt_free_request(mpt, nxt);	/* NB: recursion */
1209155521Smjacob	}
1210157354Smjacob	KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
1211157354Smjacob	KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
1212169293Smjacob	MPT_LOCK_ASSERT(mpt);
1213157662Smjacob	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
1214157662Smjacob	    ("mpt_free_request: req %p:%u func %x already on freelist",
1215157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1216157662Smjacob	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
1217157662Smjacob	    ("mpt_free_request: req %p:%u func %x on pending list",
1218157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1219157662Smjacob#ifdef	INVARIANTS
1220157662Smjacob	mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
1221157662Smjacob#endif
1222157354Smjacob
1223101704Smjacob	req->ccb = NULL;
1224147883Sscottl	if (LIST_EMPTY(&mpt->ack_frames)) {
1225157117Smjacob		/*
1226157117Smjacob		 * Insert free ones at the tail
1227157117Smjacob		 */
1228157354Smjacob		req->serno = 0;
1229157354Smjacob		req->state = REQ_STATE_FREE;
1230157662Smjacob#ifdef	INVARIANTS
1231157662Smjacob		memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
1232157662Smjacob#endif
1233157117Smjacob		TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1234147883Sscottl		if (mpt->getreqwaiter != 0) {
1235147883Sscottl			mpt->getreqwaiter = 0;
1236147883Sscottl			wakeup(&mpt->request_free_list);
1237147883Sscottl		}
1238147883Sscottl		return;
1239147883Sscottl	}
1240147883Sscottl
1241147883Sscottl	/*
1242147883Sscottl	 * Process an ack frame deferred due to resource shortage.
1243147883Sscottl	 */
1244147883Sscottl	record = LIST_FIRST(&mpt->ack_frames);
1245147883Sscottl	LIST_REMOVE(record, links);
1246157354Smjacob	req->state = REQ_STATE_ALLOCATED;
1247157662Smjacob	mpt_assign_serno(mpt, req);
1248147883Sscottl	mpt_send_event_ack(mpt, req, &record->reply, record->context);
1249219335Smarius	offset = (uint32_t)((uint8_t *)record - mpt->reply);
1250219335Smarius	reply_baddr = offset + (mpt->reply_phys & 0xFFFFFFFF);
1251219335Smarius	bus_dmamap_sync_range(mpt->reply_dmat, mpt->reply_dmap, offset,
1252219335Smarius	    MPT_REPLY_SIZE, BUS_DMASYNC_PREREAD);
1253147883Sscottl	mpt_free_reply(mpt, reply_baddr);
1254101704Smjacob}
1255101704Smjacob
1256101704Smjacob/* Get a command buffer from the free queue */
1257101704Smjacobrequest_t *
1258147883Sscottlmpt_get_request(struct mpt_softc *mpt, int sleep_ok)
1259101704Smjacob{
1260101704Smjacob	request_t *req;
1261147883Sscottl
1262147883Sscottlretry:
1263169293Smjacob	MPT_LOCK_ASSERT(mpt);
1264147883Sscottl	req = TAILQ_FIRST(&mpt->request_free_list);
1265101704Smjacob	if (req != NULL) {
1266147883Sscottl		KASSERT(req == &mpt->request_pool[req->index],
1267231629Smarius		    ("mpt_get_request: corrupted request free list"));
1268157354Smjacob		KASSERT(req->state == REQ_STATE_FREE,
1269157662Smjacob		    ("req %p:%u not free on free list %x index %d function %x",
1270157662Smjacob		    req, req->serno, req->state, req->index,
1271157662Smjacob		    ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1272147883Sscottl		TAILQ_REMOVE(&mpt->request_free_list, req, links);
1273147883Sscottl		req->state = REQ_STATE_ALLOCATED;
1274155521Smjacob		req->chain = NULL;
1275157662Smjacob		mpt_assign_serno(mpt, req);
1276147883Sscottl	} else if (sleep_ok != 0) {
1277147883Sscottl		mpt->getreqwaiter = 1;
1278147883Sscottl		mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1279147883Sscottl		goto retry;
1280101704Smjacob	}
1281157354Smjacob	return (req);
1282101704Smjacob}
1283101704Smjacob
1284101704Smjacob/* Pass the command to the IOC */
1285101704Smjacobvoid
1286147883Sscottlmpt_send_cmd(struct mpt_softc *mpt, request_t *req)
1287101704Smjacob{
1288224493Smarius
1289158982Smjacob	if (mpt->verbose > MPT_PRT_DEBUG2) {
1290158932Smjacob		mpt_dump_request(mpt, req);
1291155521Smjacob	}
1292101704Smjacob	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
1293219335Smarius	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1294147883Sscottl	req->state |= REQ_STATE_QUEUED;
1295157662Smjacob	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
1296157662Smjacob	    ("req %p:%u func %x on freelist list in mpt_send_cmd",
1297157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1298157662Smjacob	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
1299157662Smjacob	    ("req %p:%u func %x already on pending list in mpt_send_cmd",
1300157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1301147883Sscottl	TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1302147883Sscottl	mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
1303101704Smjacob}
1304101704Smjacob
1305101704Smjacob/*
1306147883Sscottl * Wait for a request to complete.
1307147883Sscottl *
1308147883Sscottl * Inputs:
1309147883Sscottl *	mpt		softc of controller executing request
1310147883Sscottl *	req		request to wait for
1311147883Sscottl *	sleep_ok	nonzero implies may sleep in this context
1312147883Sscottl *	time_ms		timeout in ms.  0 implies no timeout.
1313147883Sscottl *
1314147883Sscottl * Return Values:
1315147883Sscottl *	0		Request completed
1316147883Sscottl *	non-0		Timeout fired before request completion.
1317101704Smjacob */
1318147883Sscottlint
1319147883Sscottlmpt_wait_req(struct mpt_softc *mpt, request_t *req,
1320147883Sscottl	     mpt_req_state_t state, mpt_req_state_t mask,
1321147883Sscottl	     int sleep_ok, int time_ms)
1322101704Smjacob{
1323147883Sscottl	int   error;
1324147883Sscottl	int   timeout;
1325147883Sscottl	u_int saved_cnt;
1326101704Smjacob
1327147883Sscottl	/*
1328147883Sscottl	 * timeout is in ms.  0 indicates infinite wait.
1329147883Sscottl	 * Convert to ticks or 500us units depending on
1330147883Sscottl	 * our sleep mode.
1331147883Sscottl	 */
1332157117Smjacob	if (sleep_ok != 0) {
1333147883Sscottl		timeout = (time_ms * hz) / 1000;
1334157117Smjacob	} else {
1335147883Sscottl		timeout = time_ms * 2;
1336157117Smjacob	}
1337147883Sscottl	req->state |= REQ_STATE_NEED_WAKEUP;
1338147883Sscottl	mask &= ~REQ_STATE_NEED_WAKEUP;
1339155521Smjacob	saved_cnt = mpt->reset_cnt;
1340157117Smjacob	while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1341147883Sscottl		if (sleep_ok != 0) {
1342147883Sscottl			error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout);
1343147883Sscottl			if (error == EWOULDBLOCK) {
1344147883Sscottl				timeout = 0;
1345147883Sscottl				break;
1346147883Sscottl			}
1347147883Sscottl		} else {
1348147883Sscottl			if (time_ms != 0 && --timeout == 0) {
1349147883Sscottl				break;
1350147883Sscottl			}
1351147883Sscottl			DELAY(500);
1352147883Sscottl			mpt_intr(mpt);
1353147883Sscottl		}
1354147883Sscottl	}
1355147883Sscottl	req->state &= ~REQ_STATE_NEED_WAKEUP;
1356157117Smjacob	if (mpt->reset_cnt != saved_cnt) {
1357147883Sscottl		return (EIO);
1358157117Smjacob	}
1359157117Smjacob	if (time_ms && timeout <= 0) {
1360157117Smjacob		MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
1361160396Smjacob		req->state |= REQ_STATE_TIMEDOUT;
1362157117Smjacob		mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1363147883Sscottl		return (ETIMEDOUT);
1364157117Smjacob	}
1365147883Sscottl	return (0);
1366101704Smjacob}
1367101704Smjacob
1368101704Smjacob/*
1369101704Smjacob * Send a command to the IOC via the handshake register.
1370101704Smjacob *
1371101704Smjacob * Only done at initialization time and for certain unusual
1372101704Smjacob * commands such as device/bus reset as specified by LSI.
1373101704Smjacob */
1374101704Smjacobint
1375147883Sscottlmpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
1376101704Smjacob{
1377101704Smjacob	int i;
1378147883Sscottl	uint32_t data, *data32;
1379101704Smjacob
1380101704Smjacob	/* Check condition of the IOC */
1381101704Smjacob	data = mpt_rd_db(mpt);
1382147883Sscottl	if ((MPT_STATE(data) != MPT_DB_STATE_READY
1383147883Sscottl	  && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1384147883Sscottl	  && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1385147883Sscottl	 || MPT_DB_IS_IN_USE(data)) {
1386147883Sscottl		mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
1387101704Smjacob		mpt_print_db(data);
1388147883Sscottl		return (EBUSY);
1389101704Smjacob	}
1390101704Smjacob
1391101704Smjacob	/* We move things in 32 bit chunks */
1392101704Smjacob	len = (len + 3) >> 2;
1393101704Smjacob	data32 = cmd;
1394101704Smjacob
1395172568Skevlo	/* Clear any left over pending doorbell interrupts */
1396101704Smjacob	if (MPT_DB_INTR(mpt_rd_intr(mpt)))
1397101704Smjacob		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1398101704Smjacob
1399101704Smjacob	/*
1400101704Smjacob	 * Tell the handshake reg. we are going to send a command
1401101704Smjacob         * and how long it is going to be.
1402101704Smjacob	 */
1403101704Smjacob	data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
1404101704Smjacob	    (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
1405101704Smjacob	mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
1406101704Smjacob
1407101704Smjacob	/* Wait for the chip to notice */
1408101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1409164837Smjacob		mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n");
1410147883Sscottl		return (ETIMEDOUT);
1411101704Smjacob	}
1412101704Smjacob
1413101704Smjacob	/* Clear the interrupt */
1414101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1415101704Smjacob
1416101704Smjacob	if (mpt_wait_db_ack(mpt) != MPT_OK) {
1417164837Smjacob		mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n");
1418147883Sscottl		return (ETIMEDOUT);
1419101704Smjacob	}
1420101704Smjacob
1421101704Smjacob	/* Send the command */
1422101704Smjacob	for (i = 0; i < len; i++) {
1423164315Sjb		mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++));
1424101704Smjacob		if (mpt_wait_db_ack(mpt) != MPT_OK) {
1425156104Smjacob			mpt_prt(mpt,
1426164837Smjacob			    "mpt_send_handshake_cmd: timeout @ index %d\n", i);
1427147883Sscottl			return (ETIMEDOUT);
1428101704Smjacob		}
1429101704Smjacob	}
1430101704Smjacob	return MPT_OK;
1431101704Smjacob}
1432101704Smjacob
1433101704Smjacob/* Get the response from the handshake register */
1434101704Smjacobint
1435147883Sscottlmpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
1436101704Smjacob{
1437101704Smjacob	int left, reply_left;
1438101704Smjacob	u_int16_t *data16;
1439164315Sjb	uint32_t data;
1440101704Smjacob	MSG_DEFAULT_REPLY *hdr;
1441101704Smjacob
1442101704Smjacob	/* We move things out in 16 bit chunks */
1443101704Smjacob	reply_len >>= 1;
1444101704Smjacob	data16 = (u_int16_t *)reply;
1445101704Smjacob
1446101704Smjacob	hdr = (MSG_DEFAULT_REPLY *)reply;
1447101704Smjacob
1448101704Smjacob	/* Get first word */
1449101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1450147883Sscottl		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
1451101704Smjacob		return ETIMEDOUT;
1452101704Smjacob	}
1453164315Sjb	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1454164315Sjb	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
1455101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1456101704Smjacob
1457101704Smjacob	/* Get Second Word */
1458101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1459147883Sscottl		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
1460101704Smjacob		return ETIMEDOUT;
1461101704Smjacob	}
1462164315Sjb	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1463164315Sjb	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
1464101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1465101704Smjacob
1466157354Smjacob	/*
1467157354Smjacob	 * With the second word, we can now look at the length.
1468157354Smjacob	 * Warn about a reply that's too short (except for IOC FACTS REPLY)
1469157354Smjacob	 */
1470157354Smjacob	if ((reply_len >> 1) != hdr->MsgLength &&
1471157354Smjacob	    (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
1472103914Smjacob		mpt_prt(mpt, "reply length does not match message length: "
1473157382Smjacob			"got %x; expected %zx for function %x\n",
1474157354Smjacob			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
1475101704Smjacob	}
1476101704Smjacob
1477101704Smjacob	/* Get rest of the reply; but don't overflow the provided buffer */
1478101704Smjacob	left = (hdr->MsgLength << 1) - 2;
1479101704Smjacob	reply_left =  reply_len - 2;
1480101704Smjacob	while (left--) {
1481101704Smjacob		u_int16_t datum;
1482101704Smjacob
1483101704Smjacob		if (mpt_wait_db_int(mpt) != MPT_OK) {
1484147883Sscottl			mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
1485101704Smjacob			return ETIMEDOUT;
1486101704Smjacob		}
1487164315Sjb		data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1488164315Sjb		datum = le16toh(data & MPT_DB_DATA_MASK);
1489101704Smjacob
1490101704Smjacob		if (reply_left-- > 0)
1491164315Sjb			*data16++ = datum;
1492101704Smjacob
1493101704Smjacob		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1494101704Smjacob	}
1495101704Smjacob
1496101704Smjacob	/* One more wait & clear at the end */
1497101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1498147883Sscottl		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
1499101704Smjacob		return ETIMEDOUT;
1500101704Smjacob	}
1501101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1502101704Smjacob
1503101704Smjacob	if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1504147883Sscottl		if (mpt->verbose >= MPT_PRT_TRACE)
1505101704Smjacob			mpt_print_reply(hdr);
1506101704Smjacob		return (MPT_FAIL | hdr->IOCStatus);
1507101704Smjacob	}
1508101704Smjacob
1509101704Smjacob	return (0);
1510101704Smjacob}
1511101704Smjacob
1512101704Smjacobstatic int
1513147883Sscottlmpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
1514101704Smjacob{
1515101704Smjacob	MSG_IOC_FACTS f_req;
1516101704Smjacob	int error;
1517101704Smjacob
1518157354Smjacob	memset(&f_req, 0, sizeof f_req);
1519101704Smjacob	f_req.Function = MPI_FUNCTION_IOC_FACTS;
1520147883Sscottl	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1521101704Smjacob	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
1522164990Smjacob	if (error) {
1523101704Smjacob		return(error);
1524164990Smjacob	}
1525101704Smjacob	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
1526101704Smjacob	return (error);
1527101704Smjacob}
1528101704Smjacob
1529102199Smjacobstatic int
1530164990Smjacobmpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp)
1531102199Smjacob{
1532102199Smjacob	MSG_PORT_FACTS f_req;
1533102199Smjacob	int error;
1534102199Smjacob
1535147883Sscottl	memset(&f_req, 0, sizeof f_req);
1536102199Smjacob	f_req.Function = MPI_FUNCTION_PORT_FACTS;
1537164990Smjacob	f_req.PortNumber = port;
1538147883Sscottl	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1539102199Smjacob	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
1540164990Smjacob	if (error) {
1541102199Smjacob		return(error);
1542164990Smjacob	}
1543102199Smjacob	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
1544102199Smjacob	return (error);
1545102199Smjacob}
1546102199Smjacob
1547101704Smjacob/*
1548101704Smjacob * Send the initialization request. This is where we specify how many
1549101704Smjacob * SCSI busses and how many devices per bus we wish to emulate.
1550101704Smjacob * This is also the command that specifies the max size of the reply
1551101704Smjacob * frames from the IOC that we will be allocating.
1552101704Smjacob */
1553101704Smjacobstatic int
1554147883Sscottlmpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
1555101704Smjacob{
1556101704Smjacob	int error = 0;
1557101704Smjacob	MSG_IOC_INIT init;
1558101704Smjacob	MSG_IOC_INIT_REPLY reply;
1559101704Smjacob
1560157354Smjacob	memset(&init, 0, sizeof init);
1561101704Smjacob	init.WhoInit = who;
1562101704Smjacob	init.Function = MPI_FUNCTION_IOC_INIT;
1563164990Smjacob	init.MaxDevices = 0;	/* at least 256 devices per bus */
1564164990Smjacob	init.MaxBuses = 16;	/* at least 16 busses */
1565155521Smjacob
1566155521Smjacob	init.MsgVersion = htole16(MPI_VERSION);
1567155521Smjacob	init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1568155521Smjacob	init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1569147883Sscottl	init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1570101704Smjacob
1571101704Smjacob	if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
1572101704Smjacob		return(error);
1573101704Smjacob	}
1574101704Smjacob
1575101704Smjacob	error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
1576101704Smjacob	return (error);
1577101704Smjacob}
1578101704Smjacob
1579102199Smjacob
1580102199Smjacob/*
1581102199Smjacob * Utiltity routine to read configuration headers and pages
1582102199Smjacob */
1583147883Sscottlint
1584170252Sscottlmpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, cfgparms_t *params,
1585170252Sscottl		  bus_addr_t addr, bus_size_t len, int sleep_ok, int timeout_ms)
1586101704Smjacob{
1587102199Smjacob	MSG_CONFIG *cfgp;
1588147883Sscottl	SGE_SIMPLE32 *se;
1589102199Smjacob
1590102199Smjacob	cfgp = req->req_vbuf;
1591147883Sscottl	memset(cfgp, 0, sizeof *cfgp);
1592170252Sscottl	cfgp->Action = params->Action;
1593102199Smjacob	cfgp->Function = MPI_FUNCTION_CONFIG;
1594170252Sscottl	cfgp->Header.PageVersion = params->PageVersion;
1595170252Sscottl	cfgp->Header.PageNumber = params->PageNumber;
1596170252Sscottl	cfgp->PageAddress = htole32(params->PageAddress);
1597170252Sscottl	if ((params->PageType & MPI_CONFIG_PAGETYPE_MASK) ==
1598170252Sscottl	    MPI_CONFIG_PAGETYPE_EXTENDED) {
1599170252Sscottl		cfgp->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1600170252Sscottl		cfgp->Header.PageLength = 0;
1601170252Sscottl		cfgp->ExtPageLength = htole16(params->ExtPageLength);
1602170252Sscottl		cfgp->ExtPageType = params->ExtPageType;
1603170252Sscottl	} else {
1604170252Sscottl		cfgp->Header.PageType = params->PageType;
1605170252Sscottl		cfgp->Header.PageLength = params->PageLength;
1606170252Sscottl	}
1607147883Sscottl	se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
1608164315Sjb	se->Address = htole32(addr);
1609147883Sscottl	MPI_pSGE_SET_LENGTH(se, len);
1610147883Sscottl	MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1611147883Sscottl	    MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1612147883Sscottl	    MPI_SGE_FLAGS_END_OF_LIST |
1613170252Sscottl	    ((params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
1614170252Sscottl	  || params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1615147883Sscottl	   ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
1616164315Sjb	se->FlagsLength = htole32(se->FlagsLength);
1617147883Sscottl	cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
1618102199Smjacob
1619102199Smjacob	mpt_check_doorbell(mpt);
1620102199Smjacob	mpt_send_cmd(mpt, req);
1621147883Sscottl	return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1622147883Sscottl			     sleep_ok, timeout_ms));
1623147883Sscottl}
1624102199Smjacob
1625170252Sscottlint
1626170252Sscottlmpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion, int PageNumber,
1627170252Sscottl		       uint32_t PageAddress, int ExtPageType,
1628170252Sscottl		       CONFIG_EXTENDED_PAGE_HEADER *rslt,
1629170252Sscottl		       int sleep_ok, int timeout_ms)
1630170252Sscottl{
1631170252Sscottl	request_t  *req;
1632170252Sscottl	cfgparms_t params;
1633170252Sscottl	MSG_CONFIG_REPLY *cfgp;
1634170252Sscottl	int	    error;
1635147883Sscottl
1636170252Sscottl	req = mpt_get_request(mpt, sleep_ok);
1637170252Sscottl	if (req == NULL) {
1638170252Sscottl		mpt_prt(mpt, "mpt_extread_cfg_header: Get request failed!\n");
1639170252Sscottl		return (ENOMEM);
1640170252Sscottl	}
1641170252Sscottl
1642170252Sscottl	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
1643170252Sscottl	params.PageVersion = PageVersion;
1644170252Sscottl	params.PageLength = 0;
1645170252Sscottl	params.PageNumber = PageNumber;
1646170252Sscottl	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1647170252Sscottl	params.PageAddress = PageAddress;
1648170252Sscottl	params.ExtPageType = ExtPageType;
1649170252Sscottl	params.ExtPageLength = 0;
1650170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
1651170252Sscottl				  sleep_ok, timeout_ms);
1652170252Sscottl	if (error != 0) {
1653170252Sscottl		/*
1654170252Sscottl		 * Leave the request. Without resetting the chip, it's
1655170252Sscottl		 * still owned by it and we'll just get into trouble
1656170252Sscottl		 * freeing it now. Mark it as abandoned so that if it
1657170252Sscottl		 * shows up later it can be freed.
1658170252Sscottl		 */
1659170252Sscottl		mpt_prt(mpt, "read_extcfg_header timed out\n");
1660170252Sscottl		return (ETIMEDOUT);
1661170252Sscottl	}
1662170252Sscottl
1663170252Sscottl        switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
1664170252Sscottl	case MPI_IOCSTATUS_SUCCESS:
1665170252Sscottl		cfgp = req->req_vbuf;
1666170252Sscottl		rslt->PageVersion = cfgp->Header.PageVersion;
1667170252Sscottl		rslt->PageNumber = cfgp->Header.PageNumber;
1668170252Sscottl		rslt->PageType = cfgp->Header.PageType;
1669186878Smarius		rslt->ExtPageLength = le16toh(cfgp->ExtPageLength);
1670170252Sscottl		rslt->ExtPageType = cfgp->ExtPageType;
1671170252Sscottl		error = 0;
1672170252Sscottl		break;
1673170252Sscottl	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
1674170252Sscottl		mpt_lprt(mpt, MPT_PRT_DEBUG,
1675170252Sscottl		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
1676170252Sscottl		    MPI_CONFIG_PAGETYPE_EXTENDED, PageNumber, PageAddress);
1677170252Sscottl		error = EINVAL;
1678170252Sscottl		break;
1679170252Sscottl	default:
1680170252Sscottl		mpt_prt(mpt, "mpt_read_extcfg_header: Config Info Status %x\n",
1681170252Sscottl			req->IOCStatus);
1682170252Sscottl		error = EIO;
1683170252Sscottl		break;
1684170252Sscottl	}
1685170252Sscottl	mpt_free_request(mpt, req);
1686170252Sscottl	return (error);
1687170252Sscottl}
1688170252Sscottl
1689147883Sscottlint
1690170252Sscottlmpt_read_extcfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1691170252Sscottl		     CONFIG_EXTENDED_PAGE_HEADER *hdr, void *buf, size_t len,
1692170252Sscottl		     int sleep_ok, int timeout_ms)
1693170252Sscottl{
1694170252Sscottl	request_t    *req;
1695170252Sscottl	cfgparms_t    params;
1696170252Sscottl	int	      error;
1697170252Sscottl
1698170252Sscottl	req = mpt_get_request(mpt, sleep_ok);
1699170252Sscottl	if (req == NULL) {
1700186878Smarius		mpt_prt(mpt, "mpt_read_extcfg_page: Get request failed!\n");
1701170252Sscottl		return (-1);
1702170252Sscottl	}
1703170252Sscottl
1704170252Sscottl	params.Action = Action;
1705170252Sscottl	params.PageVersion = hdr->PageVersion;
1706170252Sscottl	params.PageLength = 0;
1707170252Sscottl	params.PageNumber = hdr->PageNumber;
1708170252Sscottl	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1709170252Sscottl	params.PageAddress = PageAddress;
1710170252Sscottl	params.ExtPageType = hdr->ExtPageType;
1711170252Sscottl	params.ExtPageLength = hdr->ExtPageLength;
1712170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params,
1713170252Sscottl				  req->req_pbuf + MPT_RQSL(mpt),
1714170252Sscottl				  len, sleep_ok, timeout_ms);
1715170252Sscottl	if (error != 0) {
1716170252Sscottl		mpt_prt(mpt, "read_extcfg_page(%d) timed out\n", Action);
1717170252Sscottl		return (-1);
1718170252Sscottl	}
1719170252Sscottl
1720170252Sscottl	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1721170252Sscottl		mpt_prt(mpt, "mpt_read_extcfg_page: Config Info Status %x\n",
1722170252Sscottl			req->IOCStatus);
1723170252Sscottl		mpt_free_request(mpt, req);
1724170252Sscottl		return (-1);
1725170252Sscottl	}
1726170252Sscottl	memcpy(buf, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
1727170252Sscottl	mpt_free_request(mpt, req);
1728170252Sscottl	return (0);
1729170252Sscottl}
1730170252Sscottl
1731170252Sscottlint
1732147883Sscottlmpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1733147883Sscottl		    uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1734147883Sscottl		    int sleep_ok, int timeout_ms)
1735147883Sscottl{
1736147883Sscottl	request_t  *req;
1737170252Sscottl	cfgparms_t params;
1738156104Smjacob	MSG_CONFIG *cfgp;
1739147883Sscottl	int	    error;
1740147883Sscottl
1741147883Sscottl	req = mpt_get_request(mpt, sleep_ok);
1742147883Sscottl	if (req == NULL) {
1743147883Sscottl		mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
1744156104Smjacob		return (ENOMEM);
1745102199Smjacob	}
1746147883Sscottl
1747170252Sscottl	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
1748170252Sscottl	params.PageVersion = 0;
1749170252Sscottl	params.PageLength = 0;
1750170252Sscottl	params.PageNumber = PageNumber;
1751170252Sscottl	params.PageType = PageType;
1752170252Sscottl	params.PageAddress = PageAddress;
1753170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
1754147883Sscottl				  sleep_ok, timeout_ms);
1755147883Sscottl	if (error != 0) {
1756160396Smjacob		/*
1757160396Smjacob		 * Leave the request. Without resetting the chip, it's
1758160396Smjacob		 * still owned by it and we'll just get into trouble
1759160396Smjacob		 * freeing it now. Mark it as abandoned so that if it
1760160396Smjacob		 * shows up later it can be freed.
1761160396Smjacob		 */
1762147883Sscottl		mpt_prt(mpt, "read_cfg_header timed out\n");
1763156104Smjacob		return (ETIMEDOUT);
1764147883Sscottl	}
1765147883Sscottl
1766156104Smjacob        switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
1767156104Smjacob	case MPI_IOCSTATUS_SUCCESS:
1768147883Sscottl		cfgp = req->req_vbuf;
1769147883Sscottl		bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1770147883Sscottl		error = 0;
1771156104Smjacob		break;
1772156104Smjacob	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
1773156104Smjacob		mpt_lprt(mpt, MPT_PRT_DEBUG,
1774156104Smjacob		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
1775156104Smjacob		    PageType, PageNumber, PageAddress);
1776156104Smjacob		error = EINVAL;
1777156104Smjacob		break;
1778156104Smjacob	default:
1779156104Smjacob		mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
1780156104Smjacob			req->IOCStatus);
1781156104Smjacob		error = EIO;
1782156104Smjacob		break;
1783147883Sscottl	}
1784102199Smjacob	mpt_free_request(mpt, req);
1785147883Sscottl	return (error);
1786102199Smjacob}
1787102199Smjacob
1788102822Smjacobint
1789147883Sscottlmpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1790147883Sscottl		  CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1791147883Sscottl		  int timeout_ms)
1792102199Smjacob{
1793147883Sscottl	request_t    *req;
1794170252Sscottl	cfgparms_t    params;
1795147883Sscottl	int	      error;
1796102199Smjacob
1797147883Sscottl	req = mpt_get_request(mpt, sleep_ok);
1798147883Sscottl	if (req == NULL) {
1799147883Sscottl		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
1800147883Sscottl		return (-1);
1801147883Sscottl	}
1802102199Smjacob
1803170252Sscottl	params.Action = Action;
1804170252Sscottl	params.PageVersion = hdr->PageVersion;
1805170252Sscottl	params.PageLength = hdr->PageLength;
1806170252Sscottl	params.PageNumber = hdr->PageNumber;
1807170252Sscottl	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1808170252Sscottl	params.PageAddress = PageAddress;
1809170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params,
1810170252Sscottl				  req->req_pbuf + MPT_RQSL(mpt),
1811147883Sscottl				  len, sleep_ok, timeout_ms);
1812147883Sscottl	if (error != 0) {
1813147883Sscottl		mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
1814147883Sscottl		return (-1);
1815147883Sscottl	}
1816102199Smjacob
1817147883Sscottl	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1818147883Sscottl		mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1819147883Sscottl			req->IOCStatus);
1820147883Sscottl		mpt_free_request(mpt, req);
1821102199Smjacob		return (-1);
1822102199Smjacob	}
1823157117Smjacob	memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
1824102199Smjacob	mpt_free_request(mpt, req);
1825102199Smjacob	return (0);
1826102199Smjacob}
1827102199Smjacob
1828102822Smjacobint
1829147883Sscottlmpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1830147883Sscottl		   CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1831147883Sscottl		   int timeout_ms)
1832102199Smjacob{
1833147883Sscottl	request_t    *req;
1834170252Sscottl	cfgparms_t    params;
1835147883Sscottl	u_int	      hdr_attr;
1836147883Sscottl	int	      error;
1837102199Smjacob
1838102199Smjacob	hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
1839102199Smjacob	if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
1840102199Smjacob	    hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1841147883Sscottl		mpt_prt(mpt, "page type 0x%x not changeable\n",
1842147883Sscottl			hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
1843102199Smjacob		return (-1);
1844102199Smjacob	}
1845102199Smjacob
1846160290Smjacob#if	0
1847160290Smjacob	/*
1848160290Smjacob	 * We shouldn't mask off other bits here.
1849160290Smjacob	 */
1850160290Smjacob	hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1851160290Smjacob#endif
1852160290Smjacob
1853147883Sscottl	req = mpt_get_request(mpt, sleep_ok);
1854147883Sscottl	if (req == NULL)
1855147883Sscottl		return (-1);
1856102199Smjacob
1857160290Smjacob	memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1858160290Smjacob
1859160290Smjacob	/*
1860160290Smjacob	 * There isn't any point in restoring stripped out attributes
1861160290Smjacob	 * if you then mask them going down to issue the request.
1862160290Smjacob	 */
1863160290Smjacob
1864170252Sscottl	params.Action = Action;
1865170252Sscottl	params.PageVersion = hdr->PageVersion;
1866170252Sscottl	params.PageLength = hdr->PageLength;
1867170252Sscottl	params.PageNumber = hdr->PageNumber;
1868170252Sscottl	params.PageAddress = PageAddress;
1869160290Smjacob#if	0
1870147883Sscottl	/* Restore stripped out attributes */
1871147883Sscottl	hdr->PageType |= hdr_attr;
1872170252Sscottl	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1873160290Smjacob#else
1874170252Sscottl	params.PageType = hdr->PageType;
1875170252Sscottl#endif
1876170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params,
1877160290Smjacob				  req->req_pbuf + MPT_RQSL(mpt),
1878160290Smjacob				  len, sleep_ok, timeout_ms);
1879147883Sscottl	if (error != 0) {
1880147883Sscottl		mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
1881147883Sscottl		return (-1);
1882147883Sscottl	}
1883102199Smjacob
1884147883Sscottl        if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1885147883Sscottl		mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1886147883Sscottl			req->IOCStatus);
1887147883Sscottl		mpt_free_request(mpt, req);
1888147883Sscottl		return (-1);
1889102199Smjacob	}
1890147883Sscottl	mpt_free_request(mpt, req);
1891147883Sscottl	return (0);
1892147883Sscottl}
1893102199Smjacob
1894147883Sscottl/*
1895147883Sscottl * Read IOC configuration information
1896147883Sscottl */
1897147883Sscottlstatic int
1898147883Sscottlmpt_read_config_info_ioc(struct mpt_softc *mpt)
1899147883Sscottl{
1900147883Sscottl	CONFIG_PAGE_HEADER hdr;
1901147883Sscottl	struct mpt_raid_volume *mpt_raid;
1902147883Sscottl	int rv;
1903147883Sscottl	int i;
1904147883Sscottl	size_t len;
1905147883Sscottl
1906147883Sscottl	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
1907158982Smjacob		2, 0, &hdr, FALSE, 5000);
1908156104Smjacob	/*
1909156104Smjacob	 * If it's an invalid page, so what? Not a supported function....
1910156104Smjacob	 */
1911158982Smjacob	if (rv == EINVAL) {
1912156104Smjacob		return (0);
1913158982Smjacob	}
1914158982Smjacob	if (rv) {
1915156104Smjacob		return (rv);
1916158982Smjacob	}
1917147883Sscottl
1918164990Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG,
1919164990Smjacob	    "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n",
1920164990Smjacob	    hdr.PageVersion, hdr.PageLength << 2,
1921164990Smjacob	    hdr.PageNumber, hdr.PageType);
1922147883Sscottl
1923147883Sscottl	len = hdr.PageLength * sizeof(uint32_t);
1924151075Sscottl	mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1925158982Smjacob	if (mpt->ioc_page2 == NULL) {
1926158982Smjacob		mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
1927158982Smjacob		mpt_raid_free_mem(mpt);
1928147883Sscottl		return (ENOMEM);
1929158982Smjacob	}
1930147883Sscottl	memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
1931158982Smjacob	rv = mpt_read_cur_cfg_page(mpt, 0,
1932158982Smjacob	    &mpt->ioc_page2->Header, len, FALSE, 5000);
1933147883Sscottl	if (rv) {
1934147883Sscottl		mpt_prt(mpt, "failed to read IOC Page 2\n");
1935158982Smjacob		mpt_raid_free_mem(mpt);
1936158982Smjacob		return (EIO);
1937158982Smjacob	}
1938164990Smjacob	mpt2host_config_page_ioc2(mpt->ioc_page2);
1939158982Smjacob
1940158982Smjacob	if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1941147883Sscottl		uint32_t mask;
1942147883Sscottl
1943147883Sscottl		mpt_prt(mpt, "Capabilities: (");
1944147883Sscottl		for (mask = 1; mask != 0; mask <<= 1) {
1945158982Smjacob			if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1946147883Sscottl				continue;
1947158982Smjacob			}
1948147883Sscottl			switch (mask) {
1949147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1950147883Sscottl				mpt_prtc(mpt, " RAID-0");
1951147883Sscottl				break;
1952147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1953147883Sscottl				mpt_prtc(mpt, " RAID-1E");
1954147883Sscottl				break;
1955147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1956147883Sscottl				mpt_prtc(mpt, " RAID-1");
1957147883Sscottl				break;
1958147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1959147883Sscottl				mpt_prtc(mpt, " SES");
1960147883Sscottl				break;
1961147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1962147883Sscottl				mpt_prtc(mpt, " SAFTE");
1963147883Sscottl				break;
1964147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1965147883Sscottl				mpt_prtc(mpt, " Multi-Channel-Arrays");
1966147883Sscottl			default:
1967147883Sscottl				break;
1968147883Sscottl			}
1969102199Smjacob		}
1970147883Sscottl		mpt_prtc(mpt, " )\n");
1971147883Sscottl		if ((mpt->ioc_page2->CapabilitiesFlags
1972147883Sscottl		   & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1973147883Sscottl		    | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1974147883Sscottl		    | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1975147883Sscottl			mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1976147883Sscottl				mpt->ioc_page2->NumActiveVolumes,
1977147883Sscottl				mpt->ioc_page2->NumActiveVolumes != 1
1978147883Sscottl			      ? "s " : " ",
1979147883Sscottl				mpt->ioc_page2->MaxVolumes);
1980147883Sscottl			mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1981147883Sscottl				mpt->ioc_page2->NumActivePhysDisks,
1982147883Sscottl				mpt->ioc_page2->NumActivePhysDisks != 1
1983147883Sscottl			      ? "s " : " ",
1984147883Sscottl				mpt->ioc_page2->MaxPhysDisks);
1985147883Sscottl		}
1986147883Sscottl	}
1987102199Smjacob
1988147883Sscottl	len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
1989158982Smjacob	mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1990147883Sscottl	if (mpt->raid_volumes == NULL) {
1991147883Sscottl		mpt_prt(mpt, "Could not allocate RAID volume data\n");
1992158982Smjacob		mpt_raid_free_mem(mpt);
1993158982Smjacob		return (ENOMEM);
1994147883Sscottl	}
1995147883Sscottl
1996147883Sscottl	/*
1997147883Sscottl	 * Copy critical data out of ioc_page2 so that we can
1998147883Sscottl	 * safely refresh the page without windows of unreliable
1999147883Sscottl	 * data.
2000147883Sscottl	 */
2001147883Sscottl	mpt->raid_max_volumes =  mpt->ioc_page2->MaxVolumes;
2002147883Sscottl
2003158982Smjacob	len = sizeof(*mpt->raid_volumes->config_page) +
2004158982Smjacob	    (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
2005147883Sscottl	for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
2006147883Sscottl		mpt_raid = &mpt->raid_volumes[i];
2007158982Smjacob		mpt_raid->config_page =
2008158982Smjacob		    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2009147883Sscottl		if (mpt_raid->config_page == NULL) {
2010147883Sscottl			mpt_prt(mpt, "Could not allocate RAID page data\n");
2011158982Smjacob			mpt_raid_free_mem(mpt);
2012158982Smjacob			return (ENOMEM);
2013147883Sscottl		}
2014147883Sscottl	}
2015147883Sscottl	mpt->raid_page0_len = len;
2016147883Sscottl
2017147883Sscottl	len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
2018158982Smjacob	mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2019147883Sscottl	if (mpt->raid_disks == NULL) {
2020147883Sscottl		mpt_prt(mpt, "Could not allocate RAID disk data\n");
2021158982Smjacob		mpt_raid_free_mem(mpt);
2022158982Smjacob		return (ENOMEM);
2023147883Sscottl	}
2024147883Sscottl	mpt->raid_max_disks =  mpt->ioc_page2->MaxPhysDisks;
2025147883Sscottl
2026158982Smjacob	/*
2027158982Smjacob	 * Load page 3.
2028158982Smjacob	 */
2029147883Sscottl	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
2030158982Smjacob	    3, 0, &hdr, FALSE, 5000);
2031158982Smjacob	if (rv) {
2032158982Smjacob		mpt_raid_free_mem(mpt);
2033147883Sscottl		return (EIO);
2034158982Smjacob	}
2035147883Sscottl
2036147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
2037158982Smjacob	    hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
2038147883Sscottl
2039147883Sscottl	len = hdr.PageLength * sizeof(uint32_t);
2040151075Sscottl	mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2041158982Smjacob	if (mpt->ioc_page3 == NULL) {
2042158982Smjacob		mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
2043158982Smjacob		mpt_raid_free_mem(mpt);
2044158982Smjacob		return (ENOMEM);
2045158982Smjacob	}
2046147883Sscottl	memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
2047158982Smjacob	rv = mpt_read_cur_cfg_page(mpt, 0,
2048158982Smjacob	    &mpt->ioc_page3->Header, len, FALSE, 5000);
2049147883Sscottl	if (rv) {
2050158982Smjacob		mpt_raid_free_mem(mpt);
2051158982Smjacob		return (EIO);
2052102199Smjacob	}
2053186878Smarius	mpt2host_config_page_ioc3(mpt->ioc_page3);
2054147883Sscottl	mpt_raid_wakeup(mpt);
2055102199Smjacob	return (0);
2056102199Smjacob}
2057102199Smjacob
2058102199Smjacob/*
2059102199Smjacob * Enable IOC port
2060102199Smjacob */
2061102199Smjacobstatic int
2062147883Sscottlmpt_send_port_enable(struct mpt_softc *mpt, int port)
2063102199Smjacob{
2064147883Sscottl	request_t	*req;
2065101704Smjacob	MSG_PORT_ENABLE *enable_req;
2066147883Sscottl	int		 error;
2067101704Smjacob
2068147883Sscottl	req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
2069147883Sscottl	if (req == NULL)
2070147883Sscottl		return (-1);
2071101704Smjacob
2072101704Smjacob	enable_req = req->req_vbuf;
2073157354Smjacob	memset(enable_req, 0,  MPT_RQSL(mpt));
2074101704Smjacob
2075101704Smjacob	enable_req->Function   = MPI_FUNCTION_PORT_ENABLE;
2076147883Sscottl	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
2077101704Smjacob	enable_req->PortNumber = port;
2078101704Smjacob
2079101704Smjacob	mpt_check_doorbell(mpt);
2080147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
2081147883Sscottl
2082147883Sscottl	mpt_send_cmd(mpt, req);
2083147883Sscottl	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
2084227230Smarius	    FALSE, (mpt->is_sas || mpt->is_fc)? 300000 : 30000);
2085147883Sscottl	if (error != 0) {
2086157117Smjacob		mpt_prt(mpt, "port %d enable timed out\n", port);
2087147883Sscottl		return (-1);
2088101704Smjacob	}
2089101704Smjacob	mpt_free_request(mpt, req);
2090157117Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
2091101704Smjacob	return (0);
2092101704Smjacob}
2093101704Smjacob
2094101704Smjacob/*
2095101704Smjacob * Enable/Disable asynchronous event reporting.
2096101704Smjacob */
2097101704Smjacobstatic int
2098147883Sscottlmpt_send_event_request(struct mpt_softc *mpt, int onoff)
2099101704Smjacob{
2100101704Smjacob	request_t *req;
2101101704Smjacob	MSG_EVENT_NOTIFY *enable_req;
2102101704Smjacob
2103157354Smjacob	req = mpt_get_request(mpt, FALSE);
2104157354Smjacob	if (req == NULL) {
2105157354Smjacob		return (ENOMEM);
2106157354Smjacob	}
2107101704Smjacob	enable_req = req->req_vbuf;
2108157354Smjacob	memset(enable_req, 0, sizeof *enable_req);
2109101704Smjacob
2110101704Smjacob	enable_req->Function   = MPI_FUNCTION_EVENT_NOTIFICATION;
2111147883Sscottl	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
2112101704Smjacob	enable_req->Switch     = onoff;
2113101704Smjacob
2114101704Smjacob	mpt_check_doorbell(mpt);
2115157354Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
2116157354Smjacob	    onoff ? "en" : "dis");
2117157354Smjacob	/*
2118157354Smjacob	 * Send the command off, but don't wait for it.
2119157354Smjacob	 */
2120101704Smjacob	mpt_send_cmd(mpt, req);
2121101704Smjacob	return (0);
2122101704Smjacob}
2123101704Smjacob
2124101704Smjacob/*
2125172568Skevlo * Un-mask the interrupts on the chip.
2126101704Smjacob */
2127101704Smjacobvoid
2128147883Sscottlmpt_enable_ints(struct mpt_softc *mpt)
2129101704Smjacob{
2130224493Smarius
2131101704Smjacob	/* Unmask every thing except door bell int */
2132101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
2133101704Smjacob}
2134101704Smjacob
2135101704Smjacob/*
2136172568Skevlo * Mask the interrupts on the chip.
2137101704Smjacob */
2138101704Smjacobvoid
2139147883Sscottlmpt_disable_ints(struct mpt_softc *mpt)
2140101704Smjacob{
2141224493Smarius
2142101704Smjacob	/* Mask all interrupts */
2143156104Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_MASK,
2144101704Smjacob	    MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
2145101704Smjacob}
2146101704Smjacob
2147147883Sscottlstatic void
2148147883Sscottlmpt_sysctl_attach(struct mpt_softc *mpt)
2149147883Sscottl{
2150147883Sscottl	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
2151147883Sscottl	struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
2152147883Sscottl
2153217323Smdf	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2154147883Sscottl		       "debug", CTLFLAG_RW, &mpt->verbose, 0,
2155147883Sscottl		       "Debugging/Verbose level");
2156217323Smdf	SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2157160290Smjacob		       "role", CTLFLAG_RD, &mpt->role, 0,
2158160290Smjacob		       "HBA role");
2159165814Smjacob#ifdef	MPT_TEST_MULTIPATH
2160165814Smjacob	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2161165814Smjacob		       "failure_id", CTLFLAG_RW, &mpt->failure_id, -1,
2162165814Smjacob		       "Next Target to Fail");
2163157117Smjacob#endif
2164147883Sscottl}
2165147883Sscottl
2166101704Smjacobint
2167147883Sscottlmpt_attach(struct mpt_softc *mpt)
2168101704Smjacob{
2169157117Smjacob	struct mpt_personality *pers;
2170147883Sscottl	int i;
2171157117Smjacob	int error;
2172147883Sscottl
2173177808Sscottl	mpt_core_attach(mpt);
2174177808Sscottl	mpt_core_enable(mpt);
2175177808Sscottl
2176162133Smjacob	TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links);
2177147883Sscottl	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2178147883Sscottl		pers = mpt_personalities[i];
2179157117Smjacob		if (pers == NULL) {
2180147883Sscottl			continue;
2181157117Smjacob		}
2182147883Sscottl		if (pers->probe(mpt) == 0) {
2183147883Sscottl			error = pers->attach(mpt);
2184147883Sscottl			if (error != 0) {
2185147883Sscottl				mpt_detach(mpt);
2186147883Sscottl				return (error);
2187147883Sscottl			}
2188147883Sscottl			mpt->mpt_pers_mask |= (0x1 << pers->id);
2189147883Sscottl			pers->use_count++;
2190147883Sscottl		}
2191147883Sscottl	}
2192155521Smjacob
2193157117Smjacob	/*
2194157117Smjacob	 * Now that we've attached everything, do the enable function
2195157117Smjacob	 * for all of the personalities. This allows the personalities
2196157117Smjacob	 * to do setups that are appropriate for them prior to enabling
2197157117Smjacob	 * any ports.
2198157117Smjacob	 */
2199157117Smjacob	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2200157117Smjacob		pers = mpt_personalities[i];
2201157117Smjacob		if (pers != NULL  && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2202157117Smjacob			error = pers->enable(mpt);
2203157117Smjacob			if (error != 0) {
2204157117Smjacob				mpt_prt(mpt, "personality %s attached but would"
2205157117Smjacob				    " not enable (%d)\n", pers->name, error);
2206157117Smjacob				mpt_detach(mpt);
2207157117Smjacob				return (error);
2208157117Smjacob			}
2209157117Smjacob		}
2210157117Smjacob	}
2211147883Sscottl	return (0);
2212147883Sscottl}
2213147883Sscottl
2214147883Sscottlint
2215147883Sscottlmpt_shutdown(struct mpt_softc *mpt)
2216147883Sscottl{
2217147883Sscottl	struct mpt_personality *pers;
2218147883Sscottl
2219157117Smjacob	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2220147883Sscottl		pers->shutdown(mpt);
2221157117Smjacob	}
2222147883Sscottl	return (0);
2223147883Sscottl}
2224147883Sscottl
2225147883Sscottlint
2226147883Sscottlmpt_detach(struct mpt_softc *mpt)
2227147883Sscottl{
2228147883Sscottl	struct mpt_personality *pers;
2229147883Sscottl
2230147883Sscottl	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2231147883Sscottl		pers->detach(mpt);
2232147883Sscottl		mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2233147883Sscottl		pers->use_count--;
2234147883Sscottl	}
2235162133Smjacob	TAILQ_REMOVE(&mpt_tailq, mpt, links);
2236147883Sscottl	return (0);
2237147883Sscottl}
2238147883Sscottl
2239224493Smariusstatic int
2240147883Sscottlmpt_core_load(struct mpt_personality *pers)
2241147883Sscottl{
2242147883Sscottl	int i;
2243147883Sscottl
2244147883Sscottl	/*
2245147883Sscottl	 * Setup core handlers and insert the default handler
2246147883Sscottl	 * into all "empty slots".
2247147883Sscottl	 */
2248157117Smjacob	for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2249147883Sscottl		mpt_reply_handlers[i] = mpt_default_reply_handler;
2250157117Smjacob	}
2251147883Sscottl
2252147883Sscottl	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2253147883Sscottl	    mpt_event_reply_handler;
2254147883Sscottl	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2255147883Sscottl	    mpt_config_reply_handler;
2256147883Sscottl	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2257147883Sscottl	    mpt_handshake_reply_handler;
2258147883Sscottl	return (0);
2259147883Sscottl}
2260147883Sscottl
2261147883Sscottl/*
2262147883Sscottl * Initialize per-instance driver data and perform
2263147883Sscottl * initial controller configuration.
2264147883Sscottl */
2265224493Smariusstatic int
2266147883Sscottlmpt_core_attach(struct mpt_softc *mpt)
2267147883Sscottl{
2268169293Smjacob        int val, error;
2269101704Smjacob
2270147883Sscottl	LIST_INIT(&mpt->ack_frames);
2271147883Sscottl	/* Put all request buffers on the free list */
2272147883Sscottl	TAILQ_INIT(&mpt->request_pending_list);
2273147883Sscottl	TAILQ_INIT(&mpt->request_free_list);
2274157354Smjacob	TAILQ_INIT(&mpt->request_timeout_list);
2275157117Smjacob	for (val = 0; val < MPT_MAX_LUNS; val++) {
2276157117Smjacob		STAILQ_INIT(&mpt->trt[val].atios);
2277157117Smjacob		STAILQ_INIT(&mpt->trt[val].inots);
2278157117Smjacob	}
2279157117Smjacob	STAILQ_INIT(&mpt->trt_wildcard.atios);
2280157117Smjacob	STAILQ_INIT(&mpt->trt_wildcard.inots);
2281165814Smjacob#ifdef	MPT_TEST_MULTIPATH
2282165814Smjacob	mpt->failure_id = -1;
2283165814Smjacob#endif
2284157117Smjacob	mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2285147883Sscottl	mpt_sysctl_attach(mpt);
2286147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2287157117Smjacob	    mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
2288169293Smjacob
2289169293Smjacob	MPT_LOCK(mpt);
2290177881Sscottl	error = mpt_configure_ioc(mpt, 0, 0);
2291169293Smjacob	MPT_UNLOCK(mpt);
2292169293Smjacob
2293169293Smjacob	return (error);
2294147883Sscottl}
2295147883Sscottl
2296224493Smariusstatic int
2297157117Smjacobmpt_core_enable(struct mpt_softc *mpt)
2298157117Smjacob{
2299224493Smarius
2300157117Smjacob	/*
2301157117Smjacob	 * We enter with the IOC enabled, but async events
2302157117Smjacob	 * not enabled, ports not enabled and interrupts
2303157117Smjacob	 * not enabled.
2304157117Smjacob	 */
2305169293Smjacob	MPT_LOCK(mpt);
2306157117Smjacob
2307157117Smjacob	/*
2308157117Smjacob	 * Enable asynchronous event reporting- all personalities
2309157117Smjacob	 * have attached so that they should be able to now field
2310157117Smjacob	 * async events.
2311157117Smjacob	 */
2312157117Smjacob	mpt_send_event_request(mpt, 1);
2313157117Smjacob
2314157117Smjacob	/*
2315157117Smjacob	 * Catch any pending interrupts
2316157117Smjacob	 *
2317157117Smjacob	 * This seems to be crucial- otherwise
2318157117Smjacob	 * the portenable below times out.
2319157117Smjacob	 */
2320157117Smjacob	mpt_intr(mpt);
2321157117Smjacob
2322157117Smjacob	/*
2323157117Smjacob	 * Enable Interrupts
2324157117Smjacob	 */
2325157117Smjacob	mpt_enable_ints(mpt);
2326157117Smjacob
2327157117Smjacob	/*
2328157117Smjacob	 * Catch any pending interrupts
2329157117Smjacob	 *
2330157117Smjacob	 * This seems to be crucial- otherwise
2331157117Smjacob	 * the portenable below times out.
2332157117Smjacob	 */
2333157117Smjacob	mpt_intr(mpt);
2334157117Smjacob
2335157117Smjacob	/*
2336157662Smjacob	 * Enable the port.
2337157117Smjacob	 */
2338157117Smjacob	if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2339157117Smjacob		mpt_prt(mpt, "failed to enable port 0\n");
2340169293Smjacob		MPT_UNLOCK(mpt);
2341157117Smjacob		return (ENXIO);
2342157117Smjacob	}
2343169293Smjacob	MPT_UNLOCK(mpt);
2344157117Smjacob	return (0);
2345157117Smjacob}
2346157117Smjacob
2347224493Smariusstatic void
2348147883Sscottlmpt_core_shutdown(struct mpt_softc *mpt)
2349147883Sscottl{
2350224493Smarius
2351157117Smjacob	mpt_disable_ints(mpt);
2352147883Sscottl}
2353147883Sscottl
2354224493Smariusstatic void
2355147883Sscottlmpt_core_detach(struct mpt_softc *mpt)
2356147883Sscottl{
2357198262Skan	int val;
2358198262Skan
2359164990Smjacob	/*
2360164990Smjacob	 * XXX: FREE MEMORY
2361164990Smjacob	 */
2362157117Smjacob	mpt_disable_ints(mpt);
2363198262Skan
2364198262Skan	/* Make sure no request has pending timeouts. */
2365198262Skan	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2366198262Skan		request_t *req = &mpt->request_pool[val];
2367198262Skan		mpt_callout_drain(mpt, &req->callout);
2368198262Skan	}
2369209599Sken
2370209599Sken	mpt_dma_buf_free(mpt);
2371147883Sscottl}
2372147883Sscottl
2373224493Smariusstatic int
2374147883Sscottlmpt_core_unload(struct mpt_personality *pers)
2375147883Sscottl{
2376224493Smarius
2377220945Smarius	/* Unload is always successful. */
2378147883Sscottl	return (0);
2379147883Sscottl}
2380147883Sscottl
2381147883Sscottl#define FW_UPLOAD_REQ_SIZE				\
2382147883Sscottl	(sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION)	\
2383147883Sscottl       + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2384147883Sscottl
2385147883Sscottlstatic int
2386147883Sscottlmpt_upload_fw(struct mpt_softc *mpt)
2387147883Sscottl{
2388147883Sscottl	uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2389147883Sscottl	MSG_FW_UPLOAD_REPLY fw_reply;
2390147883Sscottl	MSG_FW_UPLOAD *fw_req;
2391147883Sscottl	FW_UPLOAD_TCSGE *tsge;
2392147883Sscottl	SGE_SIMPLE32 *sge;
2393147883Sscottl	uint32_t flags;
2394147883Sscottl	int error;
2395147883Sscottl
2396147883Sscottl	memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2397147883Sscottl	fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2398147883Sscottl	fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2399147883Sscottl	fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2400147883Sscottl	fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2401147883Sscottl	tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2402147883Sscottl	tsge->DetailsLength = 12;
2403147883Sscottl	tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2404147883Sscottl	tsge->ImageSize = htole32(mpt->fw_image_size);
2405147883Sscottl	sge = (SGE_SIMPLE32 *)(tsge + 1);
2406147883Sscottl	flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2407147883Sscottl	      | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2408147883Sscottl	      | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2409147883Sscottl	flags <<= MPI_SGE_FLAGS_SHIFT;
2410147883Sscottl	sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2411147883Sscottl	sge->Address = htole32(mpt->fw_phys);
2412219335Smarius	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_PREREAD);
2413147883Sscottl	error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2414147883Sscottl	if (error)
2415147883Sscottl		return(error);
2416147883Sscottl	error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
2417219335Smarius	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_POSTREAD);
2418147883Sscottl	return (error);
2419147883Sscottl}
2420147883Sscottl
2421147883Sscottlstatic void
2422147883Sscottlmpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2423147883Sscottl	       uint32_t *data, bus_size_t len)
2424147883Sscottl{
2425147883Sscottl	uint32_t *data_end;
2426147883Sscottl
2427147883Sscottl	data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
2428159919Smjacob	if (mpt->is_sas) {
2429159919Smjacob		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
2430159919Smjacob	}
2431147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2432147883Sscottl	while (data != data_end) {
2433147883Sscottl		mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2434147883Sscottl		data++;
2435101704Smjacob	}
2436159919Smjacob	if (mpt->is_sas) {
2437159919Smjacob		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2438159919Smjacob	}
2439147883Sscottl}
2440101704Smjacob
2441147883Sscottlstatic int
2442147883Sscottlmpt_download_fw(struct mpt_softc *mpt)
2443147883Sscottl{
2444147883Sscottl	MpiFwHeader_t *fw_hdr;
2445147883Sscottl	int error;
2446147883Sscottl	uint32_t ext_offset;
2447147883Sscottl	uint32_t data;
2448147883Sscottl
2449231623Smarius	if (mpt->pci_pio_reg == NULL) {
2450231623Smarius		mpt_prt(mpt, "No PIO resource!\n");
2451231623Smarius		return (ENXIO);
2452231623Smarius	}
2453231623Smarius
2454147883Sscottl	mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2455147883Sscottl		mpt->fw_image_size);
2456147883Sscottl
2457147883Sscottl	error = mpt_enable_diag_mode(mpt);
2458147883Sscottl	if (error != 0) {
2459147883Sscottl		mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2460147883Sscottl		return (EIO);
2461101704Smjacob	}
2462101704Smjacob
2463147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2464147883Sscottl		  MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2465147883Sscottl
2466147883Sscottl	fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
2467219335Smarius	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_PREWRITE);
2468147883Sscottl	mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2469147883Sscottl		       fw_hdr->ImageSize);
2470219335Smarius	bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap, BUS_DMASYNC_POSTWRITE);
2471147883Sscottl
2472147883Sscottl	ext_offset = fw_hdr->NextImageHeaderOffset;
2473147883Sscottl	while (ext_offset != 0) {
2474147883Sscottl		MpiExtImageHeader_t *ext;
2475147883Sscottl
2476147883Sscottl		ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2477147883Sscottl		ext_offset = ext->NextImageHeaderOffset;
2478219335Smarius		bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap,
2479219335Smarius		    BUS_DMASYNC_PREWRITE);
2480147883Sscottl		mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2481147883Sscottl			       ext->ImageSize);
2482219335Smarius		bus_dmamap_sync(mpt->fw_dmat, mpt->fw_dmap,
2483219335Smarius		    BUS_DMASYNC_POSTWRITE);
2484147883Sscottl	}
2485147883Sscottl
2486159919Smjacob	if (mpt->is_sas) {
2487159919Smjacob		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
2488159919Smjacob	}
2489147883Sscottl	/* Setup the address to jump to on reset. */
2490147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2491147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2492147883Sscottl
2493101704Smjacob	/*
2494147883Sscottl	 * The controller sets the "flash bad" status after attempting
2495147883Sscottl	 * to auto-boot from flash.  Clear the status so that the controller
2496147883Sscottl	 * will continue the boot process with our newly installed firmware.
2497101704Smjacob	 */
2498147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2499147883Sscottl	data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2500147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2501147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2502147883Sscottl
2503159919Smjacob	if (mpt->is_sas) {
2504159919Smjacob		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2505159919Smjacob	}
2506155521Smjacob
2507147883Sscottl	/*
2508147883Sscottl	 * Re-enable the processor and clear the boot halt flag.
2509147883Sscottl	 */
2510147883Sscottl	data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2511147883Sscottl	data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2512147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2513147883Sscottl
2514147883Sscottl	mpt_disable_diag_mode(mpt);
2515147883Sscottl	return (0);
2516147883Sscottl}
2517147883Sscottl
2518209960Smariusstatic int
2519209599Skenmpt_dma_buf_alloc(struct mpt_softc *mpt)
2520209599Sken{
2521209599Sken	struct mpt_map_info mi;
2522209599Sken	uint8_t *vptr;
2523209599Sken	uint32_t pptr, end;
2524209599Sken	int i, error;
2525209599Sken
2526209599Sken	/* Create a child tag for data buffers */
2527209599Sken	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
2528209599Sken	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
2529209960Smarius	    NULL, NULL, (mpt->max_cam_seg_cnt - 1) * PAGE_SIZE,
2530209960Smarius	    mpt->max_cam_seg_cnt, BUS_SPACE_MAXSIZE_32BIT, 0,
2531209960Smarius	    &mpt->buffer_dmat) != 0) {
2532209599Sken		mpt_prt(mpt, "cannot create a dma tag for data buffers\n");
2533209599Sken		return (1);
2534209599Sken	}
2535209599Sken
2536209599Sken	/* Create a child tag for request buffers */
2537209599Sken	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
2538209599Sken	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
2539209599Sken	    NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
2540209599Sken	    &mpt->request_dmat) != 0) {
2541209599Sken		mpt_prt(mpt, "cannot create a dma tag for requests\n");
2542209599Sken		return (1);
2543209599Sken	}
2544209599Sken
2545220945Smarius	/* Allocate some DMA accessible memory for requests */
2546209599Sken	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
2547219335Smarius	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &mpt->request_dmap) != 0) {
2548209599Sken		mpt_prt(mpt, "cannot allocate %d bytes of request memory\n",
2549209599Sken		    MPT_REQ_MEM_SIZE(mpt));
2550209599Sken		return (1);
2551209599Sken	}
2552209599Sken
2553209599Sken	mi.mpt = mpt;
2554209599Sken	mi.error = 0;
2555209599Sken
2556209599Sken	/* Load and lock it into "bus space" */
2557209599Sken	bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
2558209599Sken	    MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
2559209599Sken
2560209599Sken	if (mi.error) {
2561209599Sken		mpt_prt(mpt, "error %d loading dma map for DMA request queue\n",
2562209599Sken		    mi.error);
2563209599Sken		return (1);
2564209599Sken	}
2565209599Sken	mpt->request_phys = mi.phys;
2566209599Sken
2567209599Sken	/*
2568209599Sken	 * Now create per-request dma maps
2569209599Sken	 */
2570209599Sken	i = 0;
2571209599Sken	pptr =  mpt->request_phys;
2572209599Sken	vptr =  mpt->request;
2573209599Sken	end = pptr + MPT_REQ_MEM_SIZE(mpt);
2574209599Sken	while(pptr < end) {
2575209599Sken		request_t *req = &mpt->request_pool[i];
2576209599Sken		req->index = i++;
2577209599Sken
2578209599Sken		/* Store location of Request Data */
2579209599Sken		req->req_pbuf = pptr;
2580209599Sken		req->req_vbuf = vptr;
2581209599Sken
2582209599Sken		pptr += MPT_REQUEST_AREA;
2583209599Sken		vptr += MPT_REQUEST_AREA;
2584209599Sken
2585209599Sken		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
2586209599Sken		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
2587209599Sken
2588209599Sken		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
2589209599Sken		if (error) {
2590209599Sken			mpt_prt(mpt, "error %d creating per-cmd DMA maps\n",
2591209599Sken			    error);
2592209599Sken			return (1);
2593209599Sken		}
2594209599Sken	}
2595209599Sken
2596209599Sken	return (0);
2597209599Sken}
2598209599Sken
2599209599Skenstatic void
2600209599Skenmpt_dma_buf_free(struct mpt_softc *mpt)
2601209599Sken{
2602209599Sken	int i;
2603224493Smarius
2604209599Sken	if (mpt->request_dmat == 0) {
2605209599Sken		mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
2606209599Sken		return;
2607209599Sken	}
2608209599Sken	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
2609209599Sken		bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
2610209599Sken	}
2611209599Sken	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
2612209599Sken	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
2613209599Sken	bus_dma_tag_destroy(mpt->request_dmat);
2614209599Sken	mpt->request_dmat = 0;
2615209599Sken	bus_dma_tag_destroy(mpt->buffer_dmat);
2616209599Sken}
2617209599Sken
2618147883Sscottl/*
2619147883Sscottl * Allocate/Initialize data structures for the controller.  Called
2620147883Sscottl * once at instance startup.
2621147883Sscottl */
2622147883Sscottlstatic int
2623164990Smjacobmpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset)
2624147883Sscottl{
2625164990Smjacob	PTR_MSG_PORT_FACTS_REPLY pfp;
2626209599Sken	int error, port, val;
2627164990Smjacob	size_t len;
2628147883Sscottl
2629164990Smjacob	if (tn == MPT_MAX_TRYS) {
2630164990Smjacob		return (-1);
2631164990Smjacob	}
2632147883Sscottl
2633164990Smjacob	/*
2634164990Smjacob	 * No need to reset if the IOC is already in the READY state.
2635164990Smjacob	 *
2636164990Smjacob	 * Force reset if initialization failed previously.
2637164990Smjacob	 * Note that a hard_reset of the second channel of a '929
2638164990Smjacob	 * will stop operation of the first channel.  Hopefully, if the
2639164990Smjacob	 * first channel is ok, the second will not require a hard
2640164990Smjacob	 * reset.
2641164990Smjacob	 */
2642164990Smjacob	if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) {
2643164990Smjacob		if (mpt_reset(mpt, FALSE) != MPT_OK) {
2644164990Smjacob			return (mpt_configure_ioc(mpt, tn++, 1));
2645101704Smjacob		}
2646147883Sscottl		needreset = 0;
2647164990Smjacob	}
2648101704Smjacob
2649164990Smjacob	if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) {
2650164990Smjacob		mpt_prt(mpt, "mpt_get_iocfacts failed\n");
2651164990Smjacob		return (mpt_configure_ioc(mpt, tn++, 1));
2652164990Smjacob	}
2653164990Smjacob	mpt2host_iocfacts_reply(&mpt->ioc_facts);
2654102199Smjacob
2655164990Smjacob	mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
2656164990Smjacob	    mpt->ioc_facts.MsgVersion >> 8,
2657164990Smjacob	    mpt->ioc_facts.MsgVersion & 0xFF,
2658164990Smjacob	    mpt->ioc_facts.HeaderVersion >> 8,
2659164990Smjacob	    mpt->ioc_facts.HeaderVersion & 0xFF);
2660155521Smjacob
2661164990Smjacob	/*
2662164990Smjacob	 * Now that we know request frame size, we can calculate
2663164990Smjacob	 * the actual (reasonable) segment limit for read/write I/O.
2664164990Smjacob	 *
2665164990Smjacob	 * This limit is constrained by:
2666164990Smjacob	 *
2667164990Smjacob	 *  + The size of each area we allocate per command (and how
2668164990Smjacob	 *    many chain segments we can fit into it).
2669164990Smjacob	 *  + The total number of areas we've set up.
2670164990Smjacob	 *  + The actual chain depth the card will allow.
2671164990Smjacob	 *
2672164990Smjacob	 * The first area's segment count is limited by the I/O request
2673164990Smjacob	 * at the head of it. We cannot allocate realistically more
2674164990Smjacob	 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2675164990Smjacob	 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2676164990Smjacob	 *
2677164990Smjacob	 */
2678164990Smjacob	/* total number of request areas we (can) allocate */
2679164990Smjacob	mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2680155521Smjacob
2681164990Smjacob	/* converted to the number of chain areas possible */
2682164990Smjacob	mpt->max_seg_cnt *= MPT_NRFM(mpt);
2683155521Smjacob
2684164990Smjacob	/* limited by the number of chain areas the card will support */
2685164990Smjacob	if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) {
2686209599Sken		mpt_lprt(mpt, MPT_PRT_INFO,
2687164990Smjacob		    "chain depth limited to %u (from %u)\n",
2688164990Smjacob		    mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt);
2689164990Smjacob		mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth;
2690164990Smjacob	}
2691155521Smjacob
2692164990Smjacob	/* converted to the number of simple sges in chain segments. */
2693164990Smjacob	mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2694155521Smjacob
2695209599Sken	/*
2696209599Sken	 * Use this as the basis for reporting the maximum I/O size to CAM.
2697209599Sken	 */
2698209599Sken	mpt->max_cam_seg_cnt = min(mpt->max_seg_cnt, (MAXPHYS / PAGE_SIZE) + 1);
2699209599Sken
2700209599Sken	error = mpt_dma_buf_alloc(mpt);
2701209599Sken	if (error != 0) {
2702209599Sken		mpt_prt(mpt, "mpt_dma_buf_alloc() failed!\n");
2703209599Sken		return (EIO);
2704209599Sken	}
2705209599Sken
2706209599Sken	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2707209599Sken		request_t *req = &mpt->request_pool[val];
2708209599Sken		req->state = REQ_STATE_ALLOCATED;
2709209599Sken		mpt_callout_init(mpt, &req->callout);
2710209599Sken		mpt_free_request(mpt, req);
2711209599Sken	}
2712209599Sken
2713209599Sken	mpt_lprt(mpt, MPT_PRT_INFO, "Maximum Segment Count: %u, Maximum "
2714209599Sken		 "CAM Segment Count: %u\n", mpt->max_seg_cnt,
2715209599Sken		 mpt->max_cam_seg_cnt);
2716209599Sken
2717209599Sken	mpt_lprt(mpt, MPT_PRT_INFO, "MsgLength=%u IOCNumber = %d\n",
2718164990Smjacob	    mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber);
2719209599Sken	mpt_lprt(mpt, MPT_PRT_INFO,
2720164990Smjacob	    "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2721164990Smjacob	    "Request Frame Size %u bytes Max Chain Depth %u\n",
2722164990Smjacob	    mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize,
2723164990Smjacob	    mpt->ioc_facts.RequestFrameSize << 2,
2724164990Smjacob	    mpt->ioc_facts.MaxChainDepth);
2725209599Sken	mpt_lprt(mpt, MPT_PRT_INFO, "IOCFACTS: Num Ports %d, FWImageSize %d, "
2726164990Smjacob	    "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts,
2727164990Smjacob	    mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags);
2728155521Smjacob
2729164990Smjacob	len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY);
2730164990Smjacob	mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2731164990Smjacob	if (mpt->port_facts == NULL) {
2732164990Smjacob		mpt_prt(mpt, "unable to allocate memory for port facts\n");
2733164990Smjacob		return (ENOMEM);
2734164990Smjacob	}
2735147883Sscottl
2736155521Smjacob
2737164990Smjacob	if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) &&
2738164990Smjacob	    (mpt->fw_uploaded == 0)) {
2739164990Smjacob		struct mpt_map_info mi;
2740147883Sscottl
2741164990Smjacob		/*
2742164990Smjacob		 * In some configurations, the IOC's firmware is
2743164990Smjacob		 * stored in a shared piece of system NVRAM that
2744220945Smarius		 * is only accessible via the BIOS.  In this
2745164990Smjacob		 * case, the firmware keeps a copy of firmware in
2746164990Smjacob		 * RAM until the OS driver retrieves it.  Once
2747164990Smjacob		 * retrieved, we are responsible for re-downloading
2748164990Smjacob		 * the firmware after any hard-reset.
2749164990Smjacob		 */
2750164990Smjacob		mpt->fw_image_size = mpt->ioc_facts.FWImageSize;
2751164990Smjacob		error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0,
2752164990Smjacob		    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
2753164990Smjacob		    mpt->fw_image_size, 1, mpt->fw_image_size, 0,
2754164990Smjacob		    &mpt->fw_dmat);
2755164990Smjacob		if (error != 0) {
2756219335Smarius			mpt_prt(mpt, "cannot create firmware dma tag\n");
2757164990Smjacob			return (ENOMEM);
2758101704Smjacob		}
2759164990Smjacob		error = bus_dmamem_alloc(mpt->fw_dmat,
2760219335Smarius		    (void **)&mpt->fw_image, BUS_DMA_NOWAIT |
2761219335Smarius		    BUS_DMA_COHERENT, &mpt->fw_dmap);
2762164990Smjacob		if (error != 0) {
2763164990Smjacob			mpt_prt(mpt, "cannot allocate firmware memory\n");
2764164990Smjacob			bus_dma_tag_destroy(mpt->fw_dmat);
2765164990Smjacob			return (ENOMEM);
2766164990Smjacob		}
2767164990Smjacob		mi.mpt = mpt;
2768164990Smjacob		mi.error = 0;
2769164990Smjacob		bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
2770164990Smjacob		    mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0);
2771164990Smjacob		mpt->fw_phys = mi.phys;
2772101704Smjacob
2773164990Smjacob		error = mpt_upload_fw(mpt);
2774164990Smjacob		if (error != 0) {
2775164990Smjacob			mpt_prt(mpt, "firmware upload failed.\n");
2776164990Smjacob			bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2777164990Smjacob			bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2778164990Smjacob			    mpt->fw_dmap);
2779164990Smjacob			bus_dma_tag_destroy(mpt->fw_dmat);
2780164990Smjacob			mpt->fw_image = NULL;
2781164990Smjacob			return (EIO);
2782102199Smjacob		}
2783164990Smjacob		mpt->fw_uploaded = 1;
2784164990Smjacob	}
2785102199Smjacob
2786164990Smjacob	for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) {
2787164990Smjacob		pfp = &mpt->port_facts[port];
2788164990Smjacob		error = mpt_get_portfacts(mpt, 0, pfp);
2789164990Smjacob		if (error != MPT_OK) {
2790164990Smjacob			mpt_prt(mpt,
2791164990Smjacob			    "mpt_get_portfacts on port %d failed\n", port);
2792164990Smjacob			free(mpt->port_facts, M_DEVBUF);
2793164990Smjacob			mpt->port_facts = NULL;
2794164990Smjacob			return (mpt_configure_ioc(mpt, tn++, 1));
2795102199Smjacob		}
2796164990Smjacob		mpt2host_portfacts_reply(pfp);
2797157117Smjacob
2798164990Smjacob		if (port > 0) {
2799164990Smjacob			error = MPT_PRT_INFO;
2800102199Smjacob		} else {
2801164990Smjacob			error = MPT_PRT_DEBUG;
2802102199Smjacob		}
2803164990Smjacob		mpt_lprt(mpt, error,
2804164990Smjacob		    "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n",
2805164990Smjacob		    port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID,
2806164990Smjacob		    pfp->MaxDevices);
2807102199Smjacob
2808164990Smjacob	}
2809157117Smjacob
2810164990Smjacob	/*
2811164990Smjacob	 * XXX: Not yet supporting more than port 0
2812164990Smjacob	 */
2813164990Smjacob	pfp = &mpt->port_facts[0];
2814164990Smjacob	if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) {
2815164990Smjacob		mpt->is_fc = 1;
2816164990Smjacob		mpt->is_sas = 0;
2817164990Smjacob		mpt->is_spi = 0;
2818164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2819164990Smjacob		mpt->is_fc = 0;
2820164990Smjacob		mpt->is_sas = 1;
2821164990Smjacob		mpt->is_spi = 0;
2822164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) {
2823164990Smjacob		mpt->is_fc = 0;
2824164990Smjacob		mpt->is_sas = 0;
2825164990Smjacob		mpt->is_spi = 1;
2826207287Smarius		if (mpt->mpt_ini_id == MPT_INI_ID_NONE)
2827207287Smarius			mpt->mpt_ini_id = pfp->PortSCSIID;
2828164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) {
2829164990Smjacob		mpt_prt(mpt, "iSCSI not supported yet\n");
2830164990Smjacob		return (ENXIO);
2831164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) {
2832164990Smjacob		mpt_prt(mpt, "Inactive Port\n");
2833164990Smjacob		return (ENXIO);
2834164990Smjacob	} else {
2835164990Smjacob		mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType);
2836164990Smjacob		return (ENXIO);
2837164990Smjacob	}
2838101704Smjacob
2839164990Smjacob	/*
2840164990Smjacob	 * Set our role with what this port supports.
2841164990Smjacob	 *
2842164990Smjacob	 * Note this might be changed later in different modules
2843164990Smjacob	 * if this is different from what is wanted.
2844164990Smjacob	 */
2845164990Smjacob	mpt->role = MPT_ROLE_NONE;
2846164990Smjacob	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
2847164990Smjacob		mpt->role |= MPT_ROLE_INITIATOR;
2848101704Smjacob	}
2849164990Smjacob	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2850164990Smjacob		mpt->role |= MPT_ROLE_TARGET;
2851164990Smjacob	}
2852101704Smjacob
2853164990Smjacob	/*
2854164990Smjacob	 * Enable the IOC
2855164990Smjacob	 */
2856177808Sscottl	if (mpt_enable_ioc(mpt, 1) != MPT_OK) {
2857164990Smjacob		mpt_prt(mpt, "unable to initialize IOC\n");
2858164990Smjacob		return (ENXIO);
2859101704Smjacob	}
2860101704Smjacob
2861164990Smjacob	/*
2862164990Smjacob	 * Read IOC configuration information.
2863164990Smjacob	 *
2864164990Smjacob	 * We need this to determine whether or not we have certain
2865164990Smjacob	 * settings for Integrated Mirroring (e.g.).
2866164990Smjacob	 */
2867164990Smjacob	mpt_read_config_info_ioc(mpt);
2868164990Smjacob
2869101704Smjacob	return (0);
2870101704Smjacob}
2871147883Sscottl
2872147883Sscottlstatic int
2873157117Smjacobmpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2874147883Sscottl{
2875147883Sscottl	uint32_t pptr;
2876147883Sscottl	int val;
2877147883Sscottl
2878155521Smjacob	if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2879147883Sscottl		mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2880147883Sscottl		return (EIO);
2881147883Sscottl	}
2882147883Sscottl
2883147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
2884147883Sscottl
2885147883Sscottl	if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2886147883Sscottl		mpt_prt(mpt, "IOC failed to go to run state\n");
2887147883Sscottl		return (ENXIO);
2888147883Sscottl	}
2889155521Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
2890147883Sscottl
2891147883Sscottl	/*
2892147883Sscottl	 * Give it reply buffers
2893147883Sscottl	 *
2894147883Sscottl	 * Do *not* exceed global credits.
2895147883Sscottl	 */
2896156104Smjacob	for (val = 0, pptr = mpt->reply_phys;
2897156104Smjacob	    (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
2898147883Sscottl	     pptr += MPT_REPLY_SIZE) {
2899147883Sscottl		mpt_free_reply(mpt, pptr);
2900164990Smjacob		if (++val == mpt->ioc_facts.GlobalCredits - 1)
2901147883Sscottl			break;
2902147883Sscottl	}
2903147883Sscottl
2904157117Smjacob
2905147883Sscottl	/*
2906157354Smjacob	 * Enable the port if asked. This is only done if we're resetting
2907157354Smjacob	 * the IOC after initial startup.
2908147883Sscottl	 */
2909157117Smjacob	if (portenable) {
2910157117Smjacob		/*
2911157117Smjacob		 * Enable asynchronous event reporting
2912157117Smjacob		 */
2913157117Smjacob		mpt_send_event_request(mpt, 1);
2914147883Sscottl
2915157117Smjacob		if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2916209599Sken			mpt_prt(mpt, "%s: failed to enable port 0\n", __func__);
2917157117Smjacob			return (ENXIO);
2918157117Smjacob		}
2919147883Sscottl	}
2920156104Smjacob	return (MPT_OK);
2921147883Sscottl}
2922164990Smjacob
2923164990Smjacob/*
2924164990Smjacob * Endian Conversion Functions- only used on Big Endian machines
2925164990Smjacob */
2926164990Smjacob#if	_BYTE_ORDER == _BIG_ENDIAN
2927164990Smjacobvoid
2928164990Smjacobmpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge)
2929164990Smjacob{
2930186878Smarius
2931164990Smjacob	MPT_2_HOST32(sge, FlagsLength);
2932164998Smjacob	MPT_2_HOST32(sge, u.Address64.Low);
2933164998Smjacob	MPT_2_HOST32(sge, u.Address64.High);
2934164998Smjacob}
2935164990Smjacob
2936164990Smjacobvoid
2937164990Smjacobmpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp)
2938164990Smjacob{
2939186878Smarius
2940164990Smjacob	MPT_2_HOST16(rp, MsgVersion);
2941164990Smjacob	MPT_2_HOST16(rp, HeaderVersion);
2942164990Smjacob	MPT_2_HOST32(rp, MsgContext);
2943164990Smjacob	MPT_2_HOST16(rp, IOCExceptions);
2944164990Smjacob	MPT_2_HOST16(rp, IOCStatus);
2945164990Smjacob	MPT_2_HOST32(rp, IOCLogInfo);
2946164990Smjacob	MPT_2_HOST16(rp, ReplyQueueDepth);
2947164990Smjacob	MPT_2_HOST16(rp, RequestFrameSize);
2948164990Smjacob	MPT_2_HOST16(rp, Reserved_0101_FWVersion);
2949164990Smjacob	MPT_2_HOST16(rp, ProductID);
2950164990Smjacob	MPT_2_HOST32(rp, CurrentHostMfaHighAddr);
2951164990Smjacob	MPT_2_HOST16(rp, GlobalCredits);
2952164990Smjacob	MPT_2_HOST32(rp, CurrentSenseBufferHighAddr);
2953164990Smjacob	MPT_2_HOST16(rp, CurReplyFrameSize);
2954164990Smjacob	MPT_2_HOST32(rp, FWImageSize);
2955164990Smjacob	MPT_2_HOST32(rp, IOCCapabilities);
2956164990Smjacob	MPT_2_HOST32(rp, FWVersion.Word);
2957164990Smjacob	MPT_2_HOST16(rp, HighPriorityQueueDepth);
2958164990Smjacob	MPT_2_HOST16(rp, Reserved2);
2959164990Smjacob	mpt2host_sge_simple_union(&rp->HostPageBufferSGE);
2960164990Smjacob	MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr);
2961164990Smjacob}
2962164990Smjacob
2963164990Smjacobvoid
2964164990Smjacobmpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp)
2965164990Smjacob{
2966186878Smarius
2967164990Smjacob	MPT_2_HOST16(pfp, Reserved);
2968164990Smjacob	MPT_2_HOST16(pfp, Reserved1);
2969164990Smjacob	MPT_2_HOST32(pfp, MsgContext);
2970164990Smjacob	MPT_2_HOST16(pfp, Reserved2);
2971164990Smjacob	MPT_2_HOST16(pfp, IOCStatus);
2972164990Smjacob	MPT_2_HOST32(pfp, IOCLogInfo);
2973164990Smjacob	MPT_2_HOST16(pfp, MaxDevices);
2974164990Smjacob	MPT_2_HOST16(pfp, PortSCSIID);
2975164990Smjacob	MPT_2_HOST16(pfp, ProtocolFlags);
2976164990Smjacob	MPT_2_HOST16(pfp, MaxPostedCmdBuffers);
2977164990Smjacob	MPT_2_HOST16(pfp, MaxPersistentIDs);
2978164990Smjacob	MPT_2_HOST16(pfp, MaxLanBuckets);
2979164990Smjacob	MPT_2_HOST16(pfp, Reserved4);
2980164990Smjacob	MPT_2_HOST32(pfp, Reserved5);
2981164990Smjacob}
2982186878Smarius
2983164990Smjacobvoid
2984164990Smjacobmpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2)
2985164990Smjacob{
2986164990Smjacob	int i;
2987186878Smarius
2988186878Smarius	MPT_2_HOST32(ioc2, CapabilitiesFlags);
2989164990Smjacob	for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) {
2990164998Smjacob		MPT_2_HOST16(ioc2, RaidVolume[i].Reserved3);
2991164990Smjacob	}
2992164990Smjacob}
2993164990Smjacob
2994164990Smjacobvoid
2995186878Smariusmpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *ioc3)
2996186878Smarius{
2997186878Smarius
2998186878Smarius	MPT_2_HOST16(ioc3, Reserved2);
2999186878Smarius}
3000186878Smarius
3001186878Smariusvoid
3002186878Smariusmpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *sp0)
3003186878Smarius{
3004186878Smarius
3005186878Smarius	MPT_2_HOST32(sp0, Capabilities);
3006186878Smarius	MPT_2_HOST32(sp0, PhysicalInterface);
3007186878Smarius}
3008186878Smarius
3009186878Smariusvoid
3010186878Smariusmpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
3011186878Smarius{
3012186878Smarius
3013186878Smarius	MPT_2_HOST32(sp1, Configuration);
3014186878Smarius	MPT_2_HOST32(sp1, OnBusTimerValue);
3015186878Smarius	MPT_2_HOST16(sp1, IDConfig);
3016186878Smarius}
3017186878Smarius
3018186878Smariusvoid
3019186878Smariushost2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *sp1)
3020186878Smarius{
3021186878Smarius
3022186878Smarius	HOST_2_MPT32(sp1, Configuration);
3023186878Smarius	HOST_2_MPT32(sp1, OnBusTimerValue);
3024186878Smarius	HOST_2_MPT16(sp1, IDConfig);
3025186878Smarius}
3026186878Smarius
3027186878Smariusvoid
3028186878Smariusmpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *sp2)
3029186878Smarius{
3030186878Smarius	int i;
3031186878Smarius
3032186878Smarius	MPT_2_HOST32(sp2, PortFlags);
3033186878Smarius	MPT_2_HOST32(sp2, PortSettings);
3034186878Smarius	for (i = 0; i < sizeof(sp2->DeviceSettings) /
3035186878Smarius	    sizeof(*sp2->DeviceSettings); i++) {
3036186878Smarius		MPT_2_HOST16(sp2, DeviceSettings[i].DeviceFlags);
3037186878Smarius	}
3038186878Smarius}
3039186878Smarius
3040186878Smariusvoid
3041186878Smariusmpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *sd0)
3042186878Smarius{
3043186878Smarius
3044186878Smarius	MPT_2_HOST32(sd0, NegotiatedParameters);
3045186878Smarius	MPT_2_HOST32(sd0, Information);
3046186878Smarius}
3047186878Smarius
3048186878Smariusvoid
3049186878Smariusmpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
3050186878Smarius{
3051186878Smarius
3052186878Smarius	MPT_2_HOST32(sd1, RequestedParameters);
3053186878Smarius	MPT_2_HOST32(sd1, Reserved);
3054186878Smarius	MPT_2_HOST32(sd1, Configuration);
3055186878Smarius}
3056186878Smarius
3057186878Smariusvoid
3058186878Smariushost2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *sd1)
3059186878Smarius{
3060186878Smarius
3061186878Smarius	HOST_2_MPT32(sd1, RequestedParameters);
3062186878Smarius	HOST_2_MPT32(sd1, Reserved);
3063186878Smarius	HOST_2_MPT32(sd1, Configuration);
3064186878Smarius}
3065186878Smarius
3066186878Smariusvoid
3067186878Smariusmpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *fp0)
3068186878Smarius{
3069186878Smarius
3070186878Smarius	MPT_2_HOST32(fp0, Flags);
3071186878Smarius	MPT_2_HOST32(fp0, PortIdentifier);
3072186878Smarius	MPT_2_HOST32(fp0, WWNN.Low);
3073186878Smarius	MPT_2_HOST32(fp0, WWNN.High);
3074186878Smarius	MPT_2_HOST32(fp0, WWPN.Low);
3075186878Smarius	MPT_2_HOST32(fp0, WWPN.High);
3076186878Smarius	MPT_2_HOST32(fp0, SupportedServiceClass);
3077186878Smarius	MPT_2_HOST32(fp0, SupportedSpeeds);
3078186878Smarius	MPT_2_HOST32(fp0, CurrentSpeed);
3079186878Smarius	MPT_2_HOST32(fp0, MaxFrameSize);
3080186878Smarius	MPT_2_HOST32(fp0, FabricWWNN.Low);
3081186878Smarius	MPT_2_HOST32(fp0, FabricWWNN.High);
3082186878Smarius	MPT_2_HOST32(fp0, FabricWWPN.Low);
3083186878Smarius	MPT_2_HOST32(fp0, FabricWWPN.High);
3084186878Smarius	MPT_2_HOST32(fp0, DiscoveredPortsCount);
3085186878Smarius	MPT_2_HOST32(fp0, MaxInitiators);
3086186878Smarius}
3087186878Smarius
3088186878Smariusvoid
3089186878Smariusmpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
3090186878Smarius{
3091186878Smarius
3092186878Smarius	MPT_2_HOST32(fp1, Flags);
3093186878Smarius	MPT_2_HOST32(fp1, NoSEEPROMWWNN.Low);
3094186878Smarius	MPT_2_HOST32(fp1, NoSEEPROMWWNN.High);
3095186878Smarius	MPT_2_HOST32(fp1, NoSEEPROMWWPN.Low);
3096186878Smarius	MPT_2_HOST32(fp1, NoSEEPROMWWPN.High);
3097186878Smarius}
3098186878Smarius
3099186878Smariusvoid
3100186878Smariushost2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *fp1)
3101186878Smarius{
3102186878Smarius
3103186878Smarius	HOST_2_MPT32(fp1, Flags);
3104186878Smarius	HOST_2_MPT32(fp1, NoSEEPROMWWNN.Low);
3105186878Smarius	HOST_2_MPT32(fp1, NoSEEPROMWWNN.High);
3106186878Smarius	HOST_2_MPT32(fp1, NoSEEPROMWWPN.Low);
3107186878Smarius	HOST_2_MPT32(fp1, NoSEEPROMWWPN.High);
3108186878Smarius}
3109186878Smarius
3110186878Smariusvoid
3111164990Smjacobmpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp)
3112164990Smjacob{
3113164990Smjacob	int i;
3114186878Smarius
3115164990Smjacob	MPT_2_HOST16(volp, VolumeStatus.Reserved);
3116164990Smjacob	MPT_2_HOST16(volp, VolumeSettings.Settings);
3117164990Smjacob	MPT_2_HOST32(volp, MaxLBA);
3118170276Sscottl	MPT_2_HOST32(volp, MaxLBAHigh);
3119164990Smjacob	MPT_2_HOST32(volp, StripeSize);
3120164990Smjacob	MPT_2_HOST32(volp, Reserved2);
3121164990Smjacob	MPT_2_HOST32(volp, Reserved3);
3122164990Smjacob	for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) {
3123164998Smjacob		MPT_2_HOST16(volp, PhysDisk[i].Reserved);
3124164990Smjacob	}
3125164990Smjacob}
3126164990Smjacob
3127164990Smjacobvoid
3128186878Smariusmpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *rpd0)
3129186878Smarius{
3130186878Smarius
3131186878Smarius	MPT_2_HOST32(rpd0, Reserved1);
3132186878Smarius	MPT_2_HOST16(rpd0, PhysDiskStatus.Reserved);
3133186878Smarius	MPT_2_HOST32(rpd0, MaxLBA);
3134186878Smarius	MPT_2_HOST16(rpd0, ErrorData.Reserved);
3135186878Smarius	MPT_2_HOST16(rpd0, ErrorData.ErrorCount);
3136186878Smarius	MPT_2_HOST16(rpd0, ErrorData.SmartCount);
3137186878Smarius}
3138186878Smarius
3139186878Smariusvoid
3140164990Smjacobmpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi)
3141164990Smjacob{
3142186878Smarius
3143164998Smjacob	MPT_2_HOST16(vi, TotalBlocks.High);
3144164998Smjacob	MPT_2_HOST16(vi, TotalBlocks.Low);
3145164998Smjacob	MPT_2_HOST16(vi, BlocksRemaining.High);
3146164998Smjacob	MPT_2_HOST16(vi, BlocksRemaining.Low);
3147164990Smjacob}
3148164990Smjacob#endif
3149