mpt.c revision 177881
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: head/sys/dev/mpt/mpt.c 177881 2008-04-03 14:39:48Z scottl $");
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);
131164990Smjacobstatic int mpt_configure_ioc(struct mpt_softc *mpt, int, int);
132157117Smjacobstatic int mpt_enable_ioc(struct mpt_softc *mpt, int);
133147883Sscottl
134147883Sscottl/************************* Personality Module Support *************************/
135147883Sscottl/*
136147883Sscottl * We include one extra entry that is guaranteed to be NULL
137147883Sscottl * to simplify our itterator.
138147883Sscottl */
139147883Sscottlstatic struct mpt_personality *mpt_personalities[MPT_MAX_PERSONALITIES + 1];
140147883Sscottlstatic __inline struct mpt_personality*
141147883Sscottl	mpt_pers_find(struct mpt_softc *, u_int);
142147883Sscottlstatic __inline struct mpt_personality*
143147883Sscottl	mpt_pers_find_reverse(struct mpt_softc *, u_int);
144147883Sscottl
145147883Sscottlstatic __inline struct mpt_personality *
146147883Sscottlmpt_pers_find(struct mpt_softc *mpt, u_int start_at)
147101704Smjacob{
148147883Sscottl	KASSERT(start_at <= MPT_MAX_PERSONALITIES,
149147883Sscottl		("mpt_pers_find: starting position out of range\n"));
150147883Sscottl
151147883Sscottl	while (start_at < MPT_MAX_PERSONALITIES
152147883Sscottl	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
153147883Sscottl		start_at++;
154147883Sscottl	}
155147883Sscottl	return (mpt_personalities[start_at]);
156147883Sscottl}
157147883Sscottl
158147883Sscottl/*
159157117Smjacob * Used infrequently, so no need to optimize like a forward
160147883Sscottl * traversal where we use the MAX+1 is guaranteed to be NULL
161147883Sscottl * trick.
162147883Sscottl */
163147883Sscottlstatic __inline struct mpt_personality *
164147883Sscottlmpt_pers_find_reverse(struct mpt_softc *mpt, u_int start_at)
165147883Sscottl{
166147883Sscottl	while (start_at < MPT_MAX_PERSONALITIES
167147883Sscottl	    && (mpt->mpt_pers_mask & (0x1 << start_at)) == 0) {
168147883Sscottl		start_at--;
169147883Sscottl	}
170147883Sscottl	if (start_at < MPT_MAX_PERSONALITIES)
171147883Sscottl		return (mpt_personalities[start_at]);
172147883Sscottl	return (NULL);
173147883Sscottl}
174147883Sscottl
175147883Sscottl#define MPT_PERS_FOREACH(mpt, pers)				\
176147883Sscottl	for (pers = mpt_pers_find(mpt, /*start_at*/0);		\
177147883Sscottl	     pers != NULL;					\
178147883Sscottl	     pers = mpt_pers_find(mpt, /*start_at*/pers->id+1))
179147883Sscottl
180147883Sscottl#define MPT_PERS_FOREACH_REVERSE(mpt, pers)				\
181147883Sscottl	for (pers = mpt_pers_find_reverse(mpt, MPT_MAX_PERSONALITIES-1);\
182147883Sscottl	     pers != NULL;						\
183147883Sscottl	     pers = mpt_pers_find_reverse(mpt, /*start_at*/pers->id-1))
184147883Sscottl
185147883Sscottlstatic mpt_load_handler_t      mpt_stdload;
186147883Sscottlstatic mpt_probe_handler_t     mpt_stdprobe;
187147883Sscottlstatic mpt_attach_handler_t    mpt_stdattach;
188157117Smjacobstatic mpt_enable_handler_t    mpt_stdenable;
189162133Smjacobstatic mpt_ready_handler_t     mpt_stdready;
190147883Sscottlstatic mpt_event_handler_t     mpt_stdevent;
191147883Sscottlstatic mpt_reset_handler_t     mpt_stdreset;
192147883Sscottlstatic mpt_shutdown_handler_t  mpt_stdshutdown;
193147883Sscottlstatic mpt_detach_handler_t    mpt_stddetach;
194147883Sscottlstatic mpt_unload_handler_t    mpt_stdunload;
195147883Sscottlstatic struct mpt_personality mpt_default_personality =
196147883Sscottl{
197147883Sscottl	.load		= mpt_stdload,
198147883Sscottl	.probe		= mpt_stdprobe,
199147883Sscottl	.attach		= mpt_stdattach,
200157117Smjacob	.enable		= mpt_stdenable,
201162133Smjacob	.ready		= mpt_stdready,
202147883Sscottl	.event		= mpt_stdevent,
203147883Sscottl	.reset		= mpt_stdreset,
204147883Sscottl	.shutdown	= mpt_stdshutdown,
205147883Sscottl	.detach		= mpt_stddetach,
206147883Sscottl	.unload		= mpt_stdunload
207147883Sscottl};
208147883Sscottl
209147883Sscottlstatic mpt_load_handler_t      mpt_core_load;
210147883Sscottlstatic mpt_attach_handler_t    mpt_core_attach;
211157117Smjacobstatic mpt_enable_handler_t    mpt_core_enable;
212147883Sscottlstatic mpt_reset_handler_t     mpt_core_ioc_reset;
213147883Sscottlstatic mpt_event_handler_t     mpt_core_event;
214147883Sscottlstatic mpt_shutdown_handler_t  mpt_core_shutdown;
215147883Sscottlstatic mpt_shutdown_handler_t  mpt_core_detach;
216147883Sscottlstatic mpt_unload_handler_t    mpt_core_unload;
217147883Sscottlstatic struct mpt_personality mpt_core_personality =
218147883Sscottl{
219147883Sscottl	.name		= "mpt_core",
220147883Sscottl	.load		= mpt_core_load,
221177808Sscottl//	.attach		= mpt_core_attach,
222177808Sscottl//	.enable		= mpt_core_enable,
223147883Sscottl	.event		= mpt_core_event,
224147883Sscottl	.reset		= mpt_core_ioc_reset,
225147883Sscottl	.shutdown	= mpt_core_shutdown,
226147883Sscottl	.detach		= mpt_core_detach,
227147883Sscottl	.unload		= mpt_core_unload,
228147883Sscottl};
229147883Sscottl
230147883Sscottl/*
231147883Sscottl * Manual declaration so that DECLARE_MPT_PERSONALITY doesn't need
232147883Sscottl * ordering information.  We want the core to always register FIRST.
233147883Sscottl * other modules are set to SI_ORDER_SECOND.
234147883Sscottl */
235147883Sscottlstatic moduledata_t mpt_core_mod = {
236147883Sscottl	"mpt_core", mpt_modevent, &mpt_core_personality
237147883Sscottl};
238147883SscottlDECLARE_MODULE(mpt_core, mpt_core_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
239147883SscottlMODULE_VERSION(mpt_core, 1);
240147883Sscottl
241157117Smjacob#define MPT_PERS_ATTACHED(pers, mpt) ((mpt)->mpt_pers_mask & (0x1 << pers->id))
242147883Sscottl
243147883Sscottlint
244147883Sscottlmpt_modevent(module_t mod, int type, void *data)
245147883Sscottl{
246147883Sscottl	struct mpt_personality *pers;
247147883Sscottl	int error;
248147883Sscottl
249147883Sscottl	pers = (struct mpt_personality *)data;
250147883Sscottl
251147883Sscottl	error = 0;
252147883Sscottl	switch (type) {
253147883Sscottl	case MOD_LOAD:
254147883Sscottl	{
255147883Sscottl		mpt_load_handler_t **def_handler;
256147883Sscottl		mpt_load_handler_t **pers_handler;
257147883Sscottl		int i;
258147883Sscottl
259147883Sscottl		for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
260147883Sscottl			if (mpt_personalities[i] == NULL)
261147883Sscottl				break;
262147883Sscottl		}
263147883Sscottl		if (i >= MPT_MAX_PERSONALITIES) {
264147883Sscottl			error = ENOMEM;
265147883Sscottl			break;
266147883Sscottl		}
267147883Sscottl		pers->id = i;
268147883Sscottl		mpt_personalities[i] = pers;
269147883Sscottl
270147883Sscottl		/* Install standard/noop handlers for any NULL entries. */
271147883Sscottl		def_handler = MPT_PERS_FIRST_HANDLER(&mpt_default_personality);
272147883Sscottl		pers_handler = MPT_PERS_FIRST_HANDLER(pers);
273147883Sscottl		while (pers_handler <= MPT_PERS_LAST_HANDLER(pers)) {
274147883Sscottl			if (*pers_handler == NULL)
275147883Sscottl				*pers_handler = *def_handler;
276147883Sscottl			pers_handler++;
277147883Sscottl			def_handler++;
278147883Sscottl		}
279147883Sscottl
280147883Sscottl		error = (pers->load(pers));
281147883Sscottl		if (error != 0)
282147883Sscottl			mpt_personalities[i] = NULL;
283147883Sscottl		break;
284147883Sscottl	}
285147883Sscottl	case MOD_SHUTDOWN:
286147883Sscottl		break;
287157117Smjacob#if __FreeBSD_version >= 500000
288147883Sscottl	case MOD_QUIESCE:
289147883Sscottl		break;
290157117Smjacob#endif
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
302147883Sscottlint
303147883Sscottlmpt_stdload(struct mpt_personality *pers)
304147883Sscottl{
305147883Sscottl	/* Load is always successfull. */
306147883Sscottl	return (0);
307147883Sscottl}
308147883Sscottl
309147883Sscottlint
310147883Sscottlmpt_stdprobe(struct mpt_softc *mpt)
311147883Sscottl{
312147883Sscottl	/* Probe is always successfull. */
313147883Sscottl	return (0);
314147883Sscottl}
315147883Sscottl
316147883Sscottlint
317147883Sscottlmpt_stdattach(struct mpt_softc *mpt)
318147883Sscottl{
319147883Sscottl	/* Attach is always successfull. */
320147883Sscottl	return (0);
321147883Sscottl}
322147883Sscottl
323147883Sscottlint
324157117Smjacobmpt_stdenable(struct mpt_softc *mpt)
325157117Smjacob{
326157117Smjacob	/* Enable is always successfull. */
327157117Smjacob	return (0);
328157117Smjacob}
329157117Smjacob
330162133Smjacobvoid
331162133Smjacobmpt_stdready(struct mpt_softc *mpt)
332162133Smjacob{
333162133Smjacob}
334162133Smjacob
335162133Smjacob
336157117Smjacobint
337155521Smjacobmpt_stdevent(struct mpt_softc *mpt, request_t *req, MSG_EVENT_NOTIFY_REPLY *msg)
338147883Sscottl{
339155521Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_stdevent: 0x%x\n", msg->Event & 0xFF);
340147883Sscottl	/* Event was not for us. */
341147883Sscottl	return (0);
342147883Sscottl}
343147883Sscottl
344147883Sscottlvoid
345147883Sscottlmpt_stdreset(struct mpt_softc *mpt, int type)
346147883Sscottl{
347147883Sscottl}
348147883Sscottl
349147883Sscottlvoid
350147883Sscottlmpt_stdshutdown(struct mpt_softc *mpt)
351147883Sscottl{
352147883Sscottl}
353147883Sscottl
354147883Sscottlvoid
355147883Sscottlmpt_stddetach(struct mpt_softc *mpt)
356147883Sscottl{
357147883Sscottl}
358147883Sscottl
359147883Sscottlint
360147883Sscottlmpt_stdunload(struct mpt_personality *pers)
361147883Sscottl{
362147883Sscottl	/* Unload is always successfull. */
363147883Sscottl	return (0);
364147883Sscottl}
365147883Sscottl
366162133Smjacob/*
367162133Smjacob * Post driver attachment, we may want to perform some global actions.
368162133Smjacob * Here is the hook to do so.
369162133Smjacob */
370162133Smjacob
371162133Smjacobstatic void
372162133Smjacobmpt_postattach(void *unused)
373162133Smjacob{
374162133Smjacob	struct mpt_softc *mpt;
375162133Smjacob	struct mpt_personality *pers;
376162133Smjacob
377162133Smjacob	TAILQ_FOREACH(mpt, &mpt_tailq, links) {
378162133Smjacob		MPT_PERS_FOREACH(mpt, pers)
379162133Smjacob			pers->ready(mpt);
380162133Smjacob	}
381162133Smjacob}
382162133SmjacobSYSINIT(mptdev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, mpt_postattach, NULL);
383162133Smjacob
384162133Smjacob
385147883Sscottl/******************************* Bus DMA Support ******************************/
386147883Sscottlvoid
387147883Sscottlmpt_map_rquest(void *arg, bus_dma_segment_t *segs, int nseg, int error)
388147883Sscottl{
389147883Sscottl	struct mpt_map_info *map_info;
390147883Sscottl
391147883Sscottl	map_info = (struct mpt_map_info *)arg;
392147883Sscottl	map_info->error = error;
393147883Sscottl	map_info->phys = segs->ds_addr;
394147883Sscottl}
395147883Sscottl
396147883Sscottl/**************************** Reply/Event Handling ****************************/
397147883Sscottlint
398147883Sscottlmpt_register_handler(struct mpt_softc *mpt, mpt_handler_type type,
399147883Sscottl		     mpt_handler_t handler, uint32_t *phandler_id)
400147883Sscottl{
401147883Sscottl
402147883Sscottl	switch (type) {
403147883Sscottl	case MPT_HANDLER_REPLY:
404147883Sscottl	{
405147883Sscottl		u_int cbi;
406147883Sscottl		u_int free_cbi;
407147883Sscottl
408147883Sscottl		if (phandler_id == NULL)
409147883Sscottl			return (EINVAL);
410147883Sscottl
411147883Sscottl		free_cbi = MPT_HANDLER_ID_NONE;
412147883Sscottl		for (cbi = 0; cbi < MPT_NUM_REPLY_HANDLERS; cbi++) {
413147883Sscottl			/*
414147883Sscottl			 * If the same handler is registered multiple
415147883Sscottl			 * times, don't error out.  Just return the
416147883Sscottl			 * index of the original registration.
417147883Sscottl			 */
418147883Sscottl			if (mpt_reply_handlers[cbi] == handler.reply_handler) {
419147883Sscottl				*phandler_id = MPT_CBI_TO_HID(cbi);
420147883Sscottl				return (0);
421147883Sscottl			}
422147883Sscottl
423147883Sscottl			/*
424147883Sscottl			 * Fill from the front in the hope that
425147883Sscottl			 * all registered handlers consume only a
426147883Sscottl			 * single cache line.
427147883Sscottl			 *
428147883Sscottl			 * We don't break on the first empty slot so
429147883Sscottl			 * that the full table is checked to see if
430147883Sscottl			 * this handler was previously registered.
431147883Sscottl			 */
432157117Smjacob			if (free_cbi == MPT_HANDLER_ID_NONE &&
433157117Smjacob			    (mpt_reply_handlers[cbi]
434147883Sscottl			  == mpt_default_reply_handler))
435147883Sscottl				free_cbi = cbi;
436147883Sscottl		}
437157117Smjacob		if (free_cbi == MPT_HANDLER_ID_NONE) {
438147883Sscottl			return (ENOMEM);
439157117Smjacob		}
440147883Sscottl		mpt_reply_handlers[free_cbi] = handler.reply_handler;
441147883Sscottl		*phandler_id = MPT_CBI_TO_HID(free_cbi);
442147883Sscottl		break;
443147883Sscottl	}
444147883Sscottl	default:
445147883Sscottl		mpt_prt(mpt, "mpt_register_handler unknown type %d\n", type);
446147883Sscottl		return (EINVAL);
447147883Sscottl	}
448147883Sscottl	return (0);
449147883Sscottl}
450147883Sscottl
451147883Sscottlint
452147883Sscottlmpt_deregister_handler(struct mpt_softc *mpt, mpt_handler_type type,
453147883Sscottl		       mpt_handler_t handler, uint32_t handler_id)
454147883Sscottl{
455147883Sscottl
456147883Sscottl	switch (type) {
457147883Sscottl	case MPT_HANDLER_REPLY:
458147883Sscottl	{
459147883Sscottl		u_int cbi;
460147883Sscottl
461147883Sscottl		cbi = MPT_CBI(handler_id);
462147883Sscottl		if (cbi >= MPT_NUM_REPLY_HANDLERS
463147883Sscottl		 || mpt_reply_handlers[cbi] != handler.reply_handler)
464147883Sscottl			return (ENOENT);
465147883Sscottl		mpt_reply_handlers[cbi] = mpt_default_reply_handler;
466147883Sscottl		break;
467147883Sscottl	}
468147883Sscottl	default:
469147883Sscottl		mpt_prt(mpt, "mpt_deregister_handler unknown type %d\n", type);
470147883Sscottl		return (EINVAL);
471147883Sscottl	}
472147883Sscottl	return (0);
473147883Sscottl}
474147883Sscottl
475147883Sscottlstatic int
476147883Sscottlmpt_default_reply_handler(struct mpt_softc *mpt, request_t *req,
477157117Smjacob	uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
478147883Sscottl{
479157117Smjacob	mpt_prt(mpt,
480157117Smjacob	    "Default Handler Called: req=%p:%u reply_descriptor=%x frame=%p\n",
481157117Smjacob	    req, req->serno, reply_desc, reply_frame);
482147883Sscottl
483147883Sscottl	if (reply_frame != NULL)
484147883Sscottl		mpt_dump_reply_frame(mpt, reply_frame);
485147883Sscottl
486157117Smjacob	mpt_prt(mpt, "Reply Frame Ignored\n");
487147883Sscottl
488147883Sscottl	return (/*free_reply*/TRUE);
489147883Sscottl}
490147883Sscottl
491147883Sscottlstatic int
492147883Sscottlmpt_config_reply_handler(struct mpt_softc *mpt, request_t *req,
493157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
494147883Sscottl{
495147883Sscottl	if (req != NULL) {
496147883Sscottl
497147883Sscottl		if (reply_frame != NULL) {
498147883Sscottl			MSG_CONFIG *cfgp;
499147883Sscottl			MSG_CONFIG_REPLY *reply;
500147883Sscottl
501147883Sscottl			cfgp = (MSG_CONFIG *)req->req_vbuf;
502147883Sscottl			reply = (MSG_CONFIG_REPLY *)reply_frame;
503147883Sscottl			req->IOCStatus = le16toh(reply_frame->IOCStatus);
504147883Sscottl			bcopy(&reply->Header, &cfgp->Header,
505147883Sscottl			      sizeof(cfgp->Header));
506170252Sscottl			cfgp->ExtPageLength = reply->ExtPageLength;
507170252Sscottl			cfgp->ExtPageType = reply->ExtPageType;
508147883Sscottl		}
509147883Sscottl		req->state &= ~REQ_STATE_QUEUED;
510147883Sscottl		req->state |= REQ_STATE_DONE;
511147883Sscottl		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
512157354Smjacob		if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
513147883Sscottl			wakeup(req);
514160396Smjacob		} else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
515160396Smjacob			/*
516160396Smjacob			 * Whew- we can free this request (late completion)
517160396Smjacob			 */
518160396Smjacob			mpt_free_request(mpt, req);
519157354Smjacob		}
520147883Sscottl	}
521147883Sscottl
522157354Smjacob	return (TRUE);
523147883Sscottl}
524147883Sscottl
525147883Sscottlstatic int
526147883Sscottlmpt_handshake_reply_handler(struct mpt_softc *mpt, request_t *req,
527157117Smjacob uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
528147883Sscottl{
529147883Sscottl	/* Nothing to be done. */
530157354Smjacob	return (TRUE);
531147883Sscottl}
532147883Sscottl
533147883Sscottlstatic int
534147883Sscottlmpt_event_reply_handler(struct mpt_softc *mpt, request_t *req,
535157117Smjacob    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
536147883Sscottl{
537147883Sscottl	int free_reply;
538147883Sscottl
539157354Smjacob	KASSERT(reply_frame != NULL, ("null reply in mpt_event_reply_handler"));
540157354Smjacob	KASSERT(req != NULL, ("null request in mpt_event_reply_handler"));
541147883Sscottl
542147883Sscottl	free_reply = TRUE;
543147883Sscottl	switch (reply_frame->Function) {
544147883Sscottl	case MPI_FUNCTION_EVENT_NOTIFICATION:
545147883Sscottl	{
546147883Sscottl		MSG_EVENT_NOTIFY_REPLY *msg;
547147883Sscottl		struct mpt_personality *pers;
548147883Sscottl		u_int handled;
549147883Sscottl
550147883Sscottl		handled = 0;
551147883Sscottl		msg = (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
552164315Sjb		msg->EventDataLength = le16toh(msg->EventDataLength);
553164315Sjb		msg->IOCStatus = le16toh(msg->IOCStatus);
554164315Sjb		msg->IOCLogInfo = le32toh(msg->IOCLogInfo);
555164315Sjb		msg->Event = le32toh(msg->Event);
556147883Sscottl		MPT_PERS_FOREACH(mpt, pers)
557147883Sscottl			handled += pers->event(mpt, req, msg);
558147883Sscottl
559155521Smjacob		if (handled == 0 && mpt->mpt_pers_mask == 0) {
560156301Smjacob			mpt_lprt(mpt, MPT_PRT_INFO,
561155521Smjacob				"No Handlers For Any Event Notify Frames. "
562155521Smjacob				"Event %#x (ACK %sequired).\n",
563155521Smjacob				msg->Event, msg->AckRequired? "r" : "not r");
564155521Smjacob		} else if (handled == 0) {
565173303Sscottl			mpt_lprt(mpt,
566173303Sscottl				msg->AckRequired? MPT_PRT_WARN : MPT_PRT_INFO,
567155521Smjacob				"Unhandled Event Notify Frame. Event %#x "
568155521Smjacob				"(ACK %sequired).\n",
569155521Smjacob				msg->Event, msg->AckRequired? "r" : "not r");
570155521Smjacob		}
571147883Sscottl
572147883Sscottl		if (msg->AckRequired) {
573147883Sscottl			request_t *ack_req;
574147883Sscottl			uint32_t context;
575147883Sscottl
576164315Sjb			context = req->index | MPT_REPLY_HANDLER_EVENTS;
577157354Smjacob			ack_req = mpt_get_request(mpt, FALSE);
578147883Sscottl			if (ack_req == NULL) {
579147883Sscottl				struct mpt_evtf_record *evtf;
580147883Sscottl
581147883Sscottl				evtf = (struct mpt_evtf_record *)reply_frame;
582147883Sscottl				evtf->context = context;
583147883Sscottl				LIST_INSERT_HEAD(&mpt->ack_frames, evtf, links);
584147883Sscottl				free_reply = FALSE;
585147883Sscottl				break;
586147883Sscottl			}
587147883Sscottl			mpt_send_event_ack(mpt, ack_req, msg, context);
588157354Smjacob			/*
589157354Smjacob			 * Don't check for CONTINUATION_REPLY here
590157354Smjacob			 */
591157354Smjacob			return (free_reply);
592147883Sscottl		}
593147883Sscottl		break;
594147883Sscottl	}
595147883Sscottl	case MPI_FUNCTION_PORT_ENABLE:
596157354Smjacob		mpt_lprt(mpt, MPT_PRT_DEBUG , "enable port reply\n");
597147883Sscottl		break;
598147883Sscottl	case MPI_FUNCTION_EVENT_ACK:
599147883Sscottl		break;
600147883Sscottl	default:
601157354Smjacob		mpt_prt(mpt, "unknown event function: %x\n",
602147883Sscottl			reply_frame->Function);
603147883Sscottl		break;
604147883Sscottl	}
605147883Sscottl
606157354Smjacob	/*
607157354Smjacob	 * I'm not sure that this continuation stuff works as it should.
608157354Smjacob	 *
609157354Smjacob	 * I've had FC async events occur that free the frame up because
610157354Smjacob	 * the continuation bit isn't set, and then additional async events
611157354Smjacob	 * then occur using the same context. As you might imagine, this
612157354Smjacob	 * leads to Very Bad Thing.
613157354Smjacob	 *
614157354Smjacob	 *  Let's just be safe for now and not free them up until we figure
615157354Smjacob	 * out what's actually happening here.
616157354Smjacob	 */
617157354Smjacob#if	0
618157354Smjacob	if ((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0) {
619147883Sscottl		TAILQ_REMOVE(&mpt->request_pending_list, req, links);
620157354Smjacob		mpt_free_request(mpt, req);
621157354Smjacob		mpt_prt(mpt, "event_reply %x for req %p:%u NOT a continuation",
622157354Smjacob		    reply_frame->Function, req, req->serno);
623157354Smjacob		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
624157354Smjacob			MSG_EVENT_NOTIFY_REPLY *msg =
625157354Smjacob			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
626157354Smjacob			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
627157354Smjacob			    msg->Event, msg->AckRequired);
628157354Smjacob		}
629157354Smjacob	} else {
630157354Smjacob		mpt_prt(mpt, "event_reply %x for %p:%u IS a continuation",
631157354Smjacob		    reply_frame->Function, req, req->serno);
632157354Smjacob		if (reply_frame->Function == MPI_FUNCTION_EVENT_NOTIFICATION) {
633157354Smjacob			MSG_EVENT_NOTIFY_REPLY *msg =
634157354Smjacob			    (MSG_EVENT_NOTIFY_REPLY *)reply_frame;
635157354Smjacob			mpt_prtc(mpt, " Event=0x%x AckReq=%d",
636157354Smjacob			    msg->Event, msg->AckRequired);
637157354Smjacob		}
638157354Smjacob		mpt_prtc(mpt, "\n");
639147883Sscottl	}
640157354Smjacob#endif
641147883Sscottl	return (free_reply);
642147883Sscottl}
643147883Sscottl
644147883Sscottl/*
645147883Sscottl * Process an asynchronous event from the IOC.
646147883Sscottl */
647147883Sscottlstatic int
648147883Sscottlmpt_core_event(struct mpt_softc *mpt, request_t *req,
649147883Sscottl	       MSG_EVENT_NOTIFY_REPLY *msg)
650147883Sscottl{
651155521Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_core_event: 0x%x\n",
652155521Smjacob                 msg->Event & 0xFF);
653147883Sscottl	switch(msg->Event & 0xFF) {
654147883Sscottl	case MPI_EVENT_NONE:
655147883Sscottl		break;
656147883Sscottl	case MPI_EVENT_LOG_DATA:
657147883Sscottl	{
658147883Sscottl		int i;
659147883Sscottl
660147883Sscottl		/* Some error occured that LSI wants logged */
661147883Sscottl		mpt_prt(mpt, "EvtLogData: IOCLogInfo: 0x%08x\n",
662147883Sscottl			msg->IOCLogInfo);
663147883Sscottl		mpt_prt(mpt, "\tEvtLogData: Event Data:");
664147883Sscottl		for (i = 0; i < msg->EventDataLength; i++)
665147883Sscottl			mpt_prtc(mpt, "  %08x", msg->Data[i]);
666147883Sscottl		mpt_prtc(mpt, "\n");
667147883Sscottl		break;
668147883Sscottl	}
669147883Sscottl	case MPI_EVENT_EVENT_CHANGE:
670147883Sscottl		/*
671147883Sscottl		 * This is just an acknowledgement
672147883Sscottl		 * of our mpt_send_event_request.
673147883Sscottl		 */
674147883Sscottl		break;
675155521Smjacob	case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
676155521Smjacob		break;
677147883Sscottl	default:
678157354Smjacob		return (0);
679147883Sscottl		break;
680147883Sscottl	}
681157354Smjacob	return (1);
682147883Sscottl}
683147883Sscottl
684147883Sscottlstatic void
685147883Sscottlmpt_send_event_ack(struct mpt_softc *mpt, request_t *ack_req,
686147883Sscottl		   MSG_EVENT_NOTIFY_REPLY *msg, uint32_t context)
687147883Sscottl{
688147883Sscottl	MSG_EVENT_ACK *ackp;
689147883Sscottl
690147883Sscottl	ackp = (MSG_EVENT_ACK *)ack_req->req_vbuf;
691157354Smjacob	memset(ackp, 0, sizeof (*ackp));
692147883Sscottl	ackp->Function = MPI_FUNCTION_EVENT_ACK;
693164315Sjb	ackp->Event = htole32(msg->Event);
694164315Sjb	ackp->EventContext = htole32(msg->EventContext);
695164315Sjb	ackp->MsgContext = htole32(context);
696147883Sscottl	mpt_check_doorbell(mpt);
697147883Sscottl	mpt_send_cmd(mpt, ack_req);
698147883Sscottl}
699147883Sscottl
700147883Sscottl/***************************** Interrupt Handling *****************************/
701147883Sscottlvoid
702147883Sscottlmpt_intr(void *arg)
703147883Sscottl{
704147883Sscottl	struct mpt_softc *mpt;
705157117Smjacob	uint32_t reply_desc;
706157117Smjacob	int ntrips = 0;
707147883Sscottl
708147883Sscottl	mpt = (struct mpt_softc *)arg;
709157662Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG2, "enter mpt_intr\n");
710169293Smjacob	MPT_LOCK_ASSERT(mpt);
711169293Smjacob
712147883Sscottl	while ((reply_desc = mpt_pop_reply_queue(mpt)) != MPT_REPLY_EMPTY) {
713147883Sscottl		request_t	  *req;
714147883Sscottl		MSG_DEFAULT_REPLY *reply_frame;
715147883Sscottl		uint32_t	   reply_baddr;
716157117Smjacob		uint32_t           ctxt_idx;
717147883Sscottl		u_int		   cb_index;
718147883Sscottl		u_int		   req_index;
719147883Sscottl		int		   free_rf;
720147883Sscottl
721147883Sscottl		req = NULL;
722147883Sscottl		reply_frame = NULL;
723147883Sscottl		reply_baddr = 0;
724147883Sscottl		if ((reply_desc & MPI_ADDRESS_REPLY_A_BIT) != 0) {
725147883Sscottl			u_int offset;
726147883Sscottl			/*
727147883Sscottl			 * Insure that the reply frame is coherent.
728147883Sscottl			 */
729157354Smjacob			reply_baddr = MPT_REPLY_BADDR(reply_desc);
730147883Sscottl			offset = reply_baddr - (mpt->reply_phys & 0xFFFFFFFF);
731157117Smjacob			bus_dmamap_sync_range(mpt->reply_dmat,
732157117Smjacob			    mpt->reply_dmap, offset, MPT_REPLY_SIZE,
733157117Smjacob			    BUS_DMASYNC_POSTREAD);
734147883Sscottl			reply_frame = MPT_REPLY_OTOV(mpt, offset);
735157117Smjacob			ctxt_idx = le32toh(reply_frame->MsgContext);
736157117Smjacob		} else {
737157117Smjacob			uint32_t type;
738157117Smjacob
739157117Smjacob			type = MPI_GET_CONTEXT_REPLY_TYPE(reply_desc);
740157117Smjacob			ctxt_idx = reply_desc;
741157117Smjacob			mpt_lprt(mpt, MPT_PRT_DEBUG1, "Context Reply: 0x%08x\n",
742157117Smjacob				    reply_desc);
743157117Smjacob
744157117Smjacob			switch (type) {
745157117Smjacob			case MPI_CONTEXT_REPLY_TYPE_SCSI_INIT:
746157117Smjacob				ctxt_idx &= MPI_CONTEXT_REPLY_CONTEXT_MASK;
747157117Smjacob				break;
748157117Smjacob			case MPI_CONTEXT_REPLY_TYPE_SCSI_TARGET:
749157117Smjacob				ctxt_idx = GET_IO_INDEX(reply_desc);
750157117Smjacob				if (mpt->tgt_cmd_ptrs == NULL) {
751157117Smjacob					mpt_prt(mpt,
752157117Smjacob					    "mpt_intr: no target cmd ptrs\n");
753157117Smjacob					reply_desc = MPT_REPLY_EMPTY;
754157117Smjacob					break;
755157117Smjacob				}
756157117Smjacob				if (ctxt_idx >= mpt->tgt_cmds_allocated) {
757157117Smjacob					mpt_prt(mpt,
758157117Smjacob					    "mpt_intr: bad tgt cmd ctxt %u\n",
759157117Smjacob					    ctxt_idx);
760157117Smjacob					reply_desc = MPT_REPLY_EMPTY;
761157117Smjacob					ntrips = 1000;
762157117Smjacob					break;
763157117Smjacob				}
764157117Smjacob				req = mpt->tgt_cmd_ptrs[ctxt_idx];
765157117Smjacob				if (req == NULL) {
766157117Smjacob					mpt_prt(mpt, "no request backpointer "
767157117Smjacob					    "at index %u", ctxt_idx);
768157117Smjacob					reply_desc = MPT_REPLY_EMPTY;
769157117Smjacob					ntrips = 1000;
770157117Smjacob					break;
771157117Smjacob				}
772157117Smjacob				/*
773157117Smjacob				 * Reformulate ctxt_idx to be just as if
774157117Smjacob				 * it were another type of context reply
775157117Smjacob				 * so the code below will find the request
776157117Smjacob				 * via indexing into the pool.
777157117Smjacob				 */
778157117Smjacob				ctxt_idx =
779157117Smjacob				    req->index | mpt->scsi_tgt_handler_id;
780157117Smjacob				req = NULL;
781157117Smjacob				break;
782157117Smjacob			case MPI_CONTEXT_REPLY_TYPE_LAN:
783157117Smjacob				mpt_prt(mpt, "LAN CONTEXT REPLY: 0x%08x\n",
784157117Smjacob				    reply_desc);
785157117Smjacob				reply_desc = MPT_REPLY_EMPTY;
786157117Smjacob				break;
787157117Smjacob			default:
788157117Smjacob				mpt_prt(mpt, "Context Reply 0x%08x?\n", type);
789157117Smjacob				reply_desc = MPT_REPLY_EMPTY;
790157117Smjacob				break;
791157117Smjacob			}
792157117Smjacob			if (reply_desc == MPT_REPLY_EMPTY) {
793157117Smjacob				if (ntrips++ > 1000) {
794157117Smjacob					break;
795157117Smjacob				}
796157117Smjacob				continue;
797157117Smjacob			}
798147883Sscottl		}
799157117Smjacob
800157117Smjacob		cb_index = MPT_CONTEXT_TO_CBI(ctxt_idx);
801157117Smjacob		req_index = MPT_CONTEXT_TO_REQI(ctxt_idx);
802157117Smjacob		if (req_index < MPT_MAX_REQUESTS(mpt)) {
803147883Sscottl			req = &mpt->request_pool[req_index];
804157354Smjacob		} else {
805157354Smjacob			mpt_prt(mpt, "WARN: mpt_intr index == %d (reply_desc =="
806157354Smjacob			    " 0x%x)\n", req_index, reply_desc);
807157117Smjacob		}
808147883Sscottl
809157117Smjacob		free_rf = mpt_reply_handlers[cb_index](mpt, req,
810157117Smjacob		    reply_desc, reply_frame);
811147883Sscottl
812157354Smjacob		if (reply_frame != NULL && free_rf) {
813147883Sscottl			mpt_free_reply(mpt, reply_baddr);
814157354Smjacob		}
815157117Smjacob
816157117Smjacob		/*
817157117Smjacob		 * If we got ourselves disabled, don't get stuck in a loop
818157117Smjacob		 */
819157117Smjacob		if (mpt->disabled) {
820157117Smjacob			mpt_disable_ints(mpt);
821157117Smjacob			break;
822157117Smjacob		}
823157117Smjacob		if (ntrips++ > 1000) {
824157117Smjacob			break;
825157117Smjacob		}
826147883Sscottl	}
827157662Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG2, "exit mpt_intr\n");
828147883Sscottl}
829147883Sscottl
830147883Sscottl/******************************* Error Recovery *******************************/
831147883Sscottlvoid
832147883Sscottlmpt_complete_request_chain(struct mpt_softc *mpt, struct req_queue *chain,
833147883Sscottl			    u_int iocstatus)
834147883Sscottl{
835147883Sscottl	MSG_DEFAULT_REPLY  ioc_status_frame;
836147883Sscottl	request_t	  *req;
837147883Sscottl
838157354Smjacob	memset(&ioc_status_frame, 0, sizeof(ioc_status_frame));
839147883Sscottl	ioc_status_frame.MsgLength = roundup2(sizeof(ioc_status_frame), 4);
840156104Smjacob	ioc_status_frame.IOCStatus = iocstatus;
841147883Sscottl	while((req = TAILQ_FIRST(chain)) != NULL) {
842147883Sscottl		MSG_REQUEST_HEADER *msg_hdr;
843147883Sscottl		u_int		    cb_index;
844157354Smjacob
845156796Smjacob		TAILQ_REMOVE(chain, req, links);
846147883Sscottl		msg_hdr = (MSG_REQUEST_HEADER *)req->req_vbuf;
847156104Smjacob		ioc_status_frame.Function = msg_hdr->Function;
848156104Smjacob		ioc_status_frame.MsgContext = msg_hdr->MsgContext;
849147883Sscottl		cb_index = MPT_CONTEXT_TO_CBI(le32toh(msg_hdr->MsgContext));
850157117Smjacob		mpt_reply_handlers[cb_index](mpt, req, msg_hdr->MsgContext,
851157117Smjacob		    &ioc_status_frame);
852147883Sscottl	}
853147883Sscottl}
854147883Sscottl
855147883Sscottl/********************************* Diagnostics ********************************/
856147883Sscottl/*
857147883Sscottl * Perform a diagnostic dump of a reply frame.
858147883Sscottl */
859147883Sscottlvoid
860147883Sscottlmpt_dump_reply_frame(struct mpt_softc *mpt, MSG_DEFAULT_REPLY *reply_frame)
861147883Sscottl{
862147883Sscottl	mpt_prt(mpt, "Address Reply:\n");
863147883Sscottl	mpt_print_reply(reply_frame);
864147883Sscottl}
865147883Sscottl
866147883Sscottl/******************************* Doorbell Access ******************************/
867147883Sscottlstatic __inline uint32_t mpt_rd_db(struct mpt_softc *mpt);
868147883Sscottlstatic __inline  uint32_t mpt_rd_intr(struct mpt_softc *mpt);
869147883Sscottl
870147883Sscottlstatic __inline uint32_t
871147883Sscottlmpt_rd_db(struct mpt_softc *mpt)
872147883Sscottl{
873101704Smjacob	return mpt_read(mpt, MPT_OFFSET_DOORBELL);
874101704Smjacob}
875101704Smjacob
876147883Sscottlstatic __inline uint32_t
877147883Sscottlmpt_rd_intr(struct mpt_softc *mpt)
878101704Smjacob{
879101704Smjacob	return mpt_read(mpt, MPT_OFFSET_INTR_STATUS);
880101704Smjacob}
881101704Smjacob
882101704Smjacob/* Busy wait for a door bell to be read by IOC */
883101704Smjacobstatic int
884147883Sscottlmpt_wait_db_ack(struct mpt_softc *mpt)
885101704Smjacob{
886101704Smjacob	int i;
887101704Smjacob	for (i=0; i < MPT_MAX_WAIT; i++) {
888101704Smjacob		if (!MPT_DB_IS_BUSY(mpt_rd_intr(mpt))) {
889101704Smjacob			maxwait_ack = i > maxwait_ack ? i : maxwait_ack;
890157117Smjacob			return (MPT_OK);
891101704Smjacob		}
892157117Smjacob		DELAY(200);
893101704Smjacob	}
894157117Smjacob	return (MPT_FAIL);
895101704Smjacob}
896101704Smjacob
897101704Smjacob/* Busy wait for a door bell interrupt */
898101704Smjacobstatic int
899147883Sscottlmpt_wait_db_int(struct mpt_softc *mpt)
900101704Smjacob{
901101704Smjacob	int i;
902164837Smjacob	for (i = 0; i < MPT_MAX_WAIT; i++) {
903101704Smjacob		if (MPT_DB_INTR(mpt_rd_intr(mpt))) {
904101704Smjacob			maxwait_int = i > maxwait_int ? i : maxwait_int;
905101704Smjacob			return MPT_OK;
906101704Smjacob		}
907101704Smjacob		DELAY(100);
908101704Smjacob	}
909157117Smjacob	return (MPT_FAIL);
910101704Smjacob}
911101704Smjacob
912101704Smjacob/* Wait for IOC to transition to a give state */
913101704Smjacobvoid
914147883Sscottlmpt_check_doorbell(struct mpt_softc *mpt)
915101704Smjacob{
916147883Sscottl	uint32_t db = mpt_rd_db(mpt);
917101704Smjacob	if (MPT_STATE(db) != MPT_DB_STATE_RUNNING) {
918147883Sscottl		mpt_prt(mpt, "Device not running\n");
919101704Smjacob		mpt_print_db(db);
920101704Smjacob	}
921101704Smjacob}
922101704Smjacob
923101704Smjacob/* Wait for IOC to transition to a give state */
924101704Smjacobstatic int
925147883Sscottlmpt_wait_state(struct mpt_softc *mpt, enum DB_STATE_BITS state)
926101704Smjacob{
927101704Smjacob	int i;
928101704Smjacob
929101704Smjacob	for (i = 0; i < MPT_MAX_WAIT; i++) {
930147883Sscottl		uint32_t db = mpt_rd_db(mpt);
931101704Smjacob		if (MPT_STATE(db) == state) {
932101704Smjacob			maxwait_state = i > maxwait_state ? i : maxwait_state;
933101704Smjacob			return (MPT_OK);
934101704Smjacob		}
935101704Smjacob		DELAY(100);
936101704Smjacob	}
937101704Smjacob	return (MPT_FAIL);
938101704Smjacob}
939101704Smjacob
940101704Smjacob
941147883Sscottl/************************* Intialization/Configuration ************************/
942147883Sscottlstatic int mpt_download_fw(struct mpt_softc *mpt);
943147883Sscottl
944101704Smjacob/* Issue the reset COMMAND to the IOC */
945147883Sscottlstatic int
946147883Sscottlmpt_soft_reset(struct mpt_softc *mpt)
947101704Smjacob{
948147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "soft reset\n");
949101704Smjacob
950101704Smjacob	/* Have to use hard reset if we are not in Running state */
951101704Smjacob	if (MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_RUNNING) {
952147883Sscottl		mpt_prt(mpt, "soft reset failed: device not running\n");
953157117Smjacob		return (MPT_FAIL);
954101704Smjacob	}
955101704Smjacob
956101704Smjacob	/* If door bell is in use we don't have a chance of getting
957101704Smjacob	 * a word in since the IOC probably crashed in message
958101704Smjacob	 * processing. So don't waste our time.
959101704Smjacob	 */
960101704Smjacob	if (MPT_DB_IS_IN_USE(mpt_rd_db(mpt))) {
961147883Sscottl		mpt_prt(mpt, "soft reset failed: doorbell wedged\n");
962157117Smjacob		return (MPT_FAIL);
963101704Smjacob	}
964101704Smjacob
965101704Smjacob	/* Send the reset request to the IOC */
966101704Smjacob	mpt_write(mpt, MPT_OFFSET_DOORBELL,
967101704Smjacob	    MPI_FUNCTION_IOC_MESSAGE_UNIT_RESET << MPI_DOORBELL_FUNCTION_SHIFT);
968101704Smjacob	if (mpt_wait_db_ack(mpt) != MPT_OK) {
969147883Sscottl		mpt_prt(mpt, "soft reset failed: ack timeout\n");
970157117Smjacob		return (MPT_FAIL);
971101704Smjacob	}
972101704Smjacob
973101704Smjacob	/* Wait for the IOC to reload and come out of reset state */
974101704Smjacob	if (mpt_wait_state(mpt, MPT_DB_STATE_READY) != MPT_OK) {
975147883Sscottl		mpt_prt(mpt, "soft reset failed: device did not restart\n");
976157117Smjacob		return (MPT_FAIL);
977101704Smjacob	}
978101704Smjacob
979101704Smjacob	return MPT_OK;
980101704Smjacob}
981101704Smjacob
982147883Sscottlstatic int
983147883Sscottlmpt_enable_diag_mode(struct mpt_softc *mpt)
984147883Sscottl{
985147883Sscottl	int try;
986147883Sscottl
987147883Sscottl	try = 20;
988147883Sscottl	while (--try) {
989147883Sscottl
990147883Sscottl		if ((mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC) & MPI_DIAG_DRWE) != 0)
991147883Sscottl			break;
992147883Sscottl
993147883Sscottl		/* Enable diagnostic registers */
994147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFF);
995147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_1ST_KEY_VALUE);
996147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_2ND_KEY_VALUE);
997147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_3RD_KEY_VALUE);
998147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_4TH_KEY_VALUE);
999147883Sscottl		mpt_write(mpt, MPT_OFFSET_SEQUENCE, MPI_WRSEQ_5TH_KEY_VALUE);
1000147883Sscottl
1001147883Sscottl		DELAY(100000);
1002147883Sscottl	}
1003147883Sscottl	if (try == 0)
1004147883Sscottl		return (EIO);
1005147883Sscottl	return (0);
1006147883Sscottl}
1007147883Sscottl
1008147883Sscottlstatic void
1009147883Sscottlmpt_disable_diag_mode(struct mpt_softc *mpt)
1010147883Sscottl{
1011147883Sscottl	mpt_write(mpt, MPT_OFFSET_SEQUENCE, 0xFFFFFFFF);
1012147883Sscottl}
1013147883Sscottl
1014101704Smjacob/* This is a magic diagnostic reset that resets all the ARM
1015156104Smjacob * processors in the chip.
1016101704Smjacob */
1017147883Sscottlstatic void
1018147883Sscottlmpt_hard_reset(struct mpt_softc *mpt)
1019101704Smjacob{
1020147883Sscottl	int error;
1021147883Sscottl	int wait;
1022147883Sscottl	uint32_t diagreg;
1023147883Sscottl
1024147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "hard reset\n");
1025147883Sscottl
1026147883Sscottl	error = mpt_enable_diag_mode(mpt);
1027147883Sscottl	if (error) {
1028147883Sscottl		mpt_prt(mpt, "WARNING - Could not enter diagnostic mode !\n");
1029147883Sscottl		mpt_prt(mpt, "Trying to reset anyway.\n");
1030101704Smjacob	}
1031101704Smjacob
1032147883Sscottl	diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1033101704Smjacob
1034147883Sscottl	/*
1035147883Sscottl	 * This appears to be a workaround required for some
1036147883Sscottl	 * firmware or hardware revs.
1037147883Sscottl	 */
1038147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_DISABLE_ARM);
1039147883Sscottl	DELAY(1000);
1040147883Sscottl
1041101704Smjacob	/* Diag. port is now active so we can now hit the reset bit */
1042147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, diagreg | MPI_DIAG_RESET_ADAPTER);
1043101704Smjacob
1044147883Sscottl        /*
1045147883Sscottl         * Ensure that the reset has finished.  We delay 1ms
1046147883Sscottl         * prior to reading the register to make sure the chip
1047147883Sscottl         * has sufficiently completed its reset to handle register
1048147883Sscottl         * accesses.
1049147883Sscottl         */
1050147883Sscottl	wait = 5000;
1051147883Sscottl	do {
1052147883Sscottl		DELAY(1000);
1053147883Sscottl		diagreg = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
1054147883Sscottl	} while (--wait && (diagreg & MPI_DIAG_RESET_ADAPTER) == 0);
1055101704Smjacob
1056147883Sscottl	if (wait == 0) {
1057147883Sscottl		mpt_prt(mpt, "WARNING - Failed hard reset! "
1058147883Sscottl			"Trying to initialize anyway.\n");
1059147883Sscottl	}
1060101704Smjacob
1061147883Sscottl	/*
1062147883Sscottl	 * If we have firmware to download, it must be loaded before
1063147883Sscottl	 * the controller will become operational.  Do so now.
1064147883Sscottl	 */
1065147883Sscottl	if (mpt->fw_image != NULL) {
1066147883Sscottl
1067147883Sscottl		error = mpt_download_fw(mpt);
1068147883Sscottl
1069147883Sscottl		if (error) {
1070147883Sscottl			mpt_prt(mpt, "WARNING - Firmware Download Failed!\n");
1071147883Sscottl			mpt_prt(mpt, "Trying to initialize anyway.\n");
1072147883Sscottl		}
1073101704Smjacob	}
1074101704Smjacob
1075147883Sscottl	/*
1076147883Sscottl	 * Reseting the controller should have disabled write
1077147883Sscottl	 * access to the diagnostic registers, but disable
1078147883Sscottl	 * manually to be sure.
1079147883Sscottl	 */
1080147883Sscottl	mpt_disable_diag_mode(mpt);
1081101704Smjacob}
1082101704Smjacob
1083147883Sscottlstatic void
1084147883Sscottlmpt_core_ioc_reset(struct mpt_softc *mpt, int type)
1085147883Sscottl{
1086147883Sscottl	/*
1087147883Sscottl	 * Complete all pending requests with a status
1088147883Sscottl	 * appropriate for an IOC reset.
1089147883Sscottl	 */
1090147883Sscottl	mpt_complete_request_chain(mpt, &mpt->request_pending_list,
1091147883Sscottl				   MPI_IOCSTATUS_INVALID_STATE);
1092147883Sscottl}
1093147883Sscottl
1094147883Sscottl
1095101704Smjacob/*
1096101704Smjacob * Reset the IOC when needed. Try software command first then if needed
1097101704Smjacob * poke at the magic diagnostic reset. Note that a hard reset resets
1098101704Smjacob * *both* IOCs on dual function chips (FC929 && LSI1030) as well as
1099101704Smjacob * fouls up the PCI configuration registers.
1100101704Smjacob */
1101101704Smjacobint
1102147883Sscottlmpt_reset(struct mpt_softc *mpt, int reinit)
1103101704Smjacob{
1104147883Sscottl	struct	mpt_personality *pers;
1105147883Sscottl	int	ret;
1106156104Smjacob	int	retry_cnt = 0;
1107101704Smjacob
1108156104Smjacob	/*
1109156104Smjacob	 * Try a soft reset. If that fails, get out the big hammer.
1110156104Smjacob	 */
1111156104Smjacob again:
1112101704Smjacob	if ((ret = mpt_soft_reset(mpt)) != MPT_OK) {
1113156104Smjacob		int	cnt;
1114156104Smjacob		for (cnt = 0; cnt < 5; cnt++) {
1115156104Smjacob			/* Failed; do a hard reset */
1116156104Smjacob			mpt_hard_reset(mpt);
1117101704Smjacob
1118156104Smjacob			/*
1119156104Smjacob			 * Wait for the IOC to reload
1120156104Smjacob			 * and come out of reset state
1121156104Smjacob			 */
1122156104Smjacob			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
1123156104Smjacob			if (ret == MPT_OK) {
1124156104Smjacob				break;
1125156104Smjacob			}
1126156104Smjacob			/*
1127156104Smjacob			 * Okay- try to check again...
1128156104Smjacob			 */
1129156104Smjacob			ret = mpt_wait_state(mpt, MPT_DB_STATE_READY);
1130156104Smjacob			if (ret == MPT_OK) {
1131156104Smjacob				break;
1132156104Smjacob			}
1133156104Smjacob			mpt_prt(mpt, "mpt_reset: failed hard reset (%d:%d)\n",
1134156104Smjacob			    retry_cnt, cnt);
1135156104Smjacob		}
1136101704Smjacob	}
1137101704Smjacob
1138156104Smjacob	if (retry_cnt == 0) {
1139156104Smjacob		/*
1140156104Smjacob		 * Invoke reset handlers.  We bump the reset count so
1141156104Smjacob		 * that mpt_wait_req() understands that regardless of
1142156104Smjacob		 * the specified wait condition, it should stop its wait.
1143156104Smjacob		 */
1144156104Smjacob		mpt->reset_cnt++;
1145156104Smjacob		MPT_PERS_FOREACH(mpt, pers)
1146156104Smjacob			pers->reset(mpt, ret);
1147156104Smjacob	}
1148147883Sscottl
1149157354Smjacob	if (reinit) {
1150157117Smjacob		ret = mpt_enable_ioc(mpt, 1);
1151156104Smjacob		if (ret == MPT_OK) {
1152156104Smjacob			mpt_enable_ints(mpt);
1153156104Smjacob		}
1154156104Smjacob	}
1155156104Smjacob	if (ret != MPT_OK && retry_cnt++ < 2) {
1156156104Smjacob		goto again;
1157156104Smjacob	}
1158101704Smjacob	return ret;
1159101704Smjacob}
1160101704Smjacob
1161101704Smjacob/* Return a command buffer to the free queue */
1162101704Smjacobvoid
1163147883Sscottlmpt_free_request(struct mpt_softc *mpt, request_t *req)
1164101704Smjacob{
1165155521Smjacob	request_t *nxt;
1166147883Sscottl	struct mpt_evtf_record *record;
1167147883Sscottl	uint32_t reply_baddr;
1168147883Sscottl
1169103871Smjacob	if (req == NULL || req != &mpt->request_pool[req->index]) {
1170101704Smjacob		panic("mpt_free_request bad req ptr\n");
1171101704Smjacob		return;
1172101704Smjacob	}
1173155521Smjacob	if ((nxt = req->chain) != NULL) {
1174155521Smjacob		req->chain = NULL;
1175155521Smjacob		mpt_free_request(mpt, nxt);	/* NB: recursion */
1176155521Smjacob	}
1177157354Smjacob	KASSERT(req->state != REQ_STATE_FREE, ("freeing free request"));
1178157354Smjacob	KASSERT(!(req->state & REQ_STATE_LOCKED), ("freeing locked request"));
1179169293Smjacob	MPT_LOCK_ASSERT(mpt);
1180157662Smjacob	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
1181157662Smjacob	    ("mpt_free_request: req %p:%u func %x already on freelist",
1182157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1183157662Smjacob	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
1184157662Smjacob	    ("mpt_free_request: req %p:%u func %x on pending list",
1185157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1186157662Smjacob#ifdef	INVARIANTS
1187157662Smjacob	mpt_req_not_spcl(mpt, req, "mpt_free_request", __LINE__);
1188157662Smjacob#endif
1189157354Smjacob
1190101704Smjacob	req->ccb = NULL;
1191147883Sscottl	if (LIST_EMPTY(&mpt->ack_frames)) {
1192157117Smjacob		/*
1193157117Smjacob		 * Insert free ones at the tail
1194157117Smjacob		 */
1195157354Smjacob		req->serno = 0;
1196157354Smjacob		req->state = REQ_STATE_FREE;
1197157662Smjacob#ifdef	INVARIANTS
1198157662Smjacob		memset(req->req_vbuf, 0xff, sizeof (MSG_REQUEST_HEADER));
1199157662Smjacob#endif
1200157117Smjacob		TAILQ_INSERT_TAIL(&mpt->request_free_list, req, links);
1201147883Sscottl		if (mpt->getreqwaiter != 0) {
1202147883Sscottl			mpt->getreqwaiter = 0;
1203147883Sscottl			wakeup(&mpt->request_free_list);
1204147883Sscottl		}
1205147883Sscottl		return;
1206147883Sscottl	}
1207147883Sscottl
1208147883Sscottl	/*
1209147883Sscottl	 * Process an ack frame deferred due to resource shortage.
1210147883Sscottl	 */
1211147883Sscottl	record = LIST_FIRST(&mpt->ack_frames);
1212147883Sscottl	LIST_REMOVE(record, links);
1213157354Smjacob	req->state = REQ_STATE_ALLOCATED;
1214157662Smjacob	mpt_assign_serno(mpt, req);
1215147883Sscottl	mpt_send_event_ack(mpt, req, &record->reply, record->context);
1216147883Sscottl	reply_baddr = (uint32_t)((uint8_t *)record - mpt->reply)
1217147883Sscottl		    + (mpt->reply_phys & 0xFFFFFFFF);
1218147883Sscottl	mpt_free_reply(mpt, reply_baddr);
1219101704Smjacob}
1220101704Smjacob
1221101704Smjacob/* Get a command buffer from the free queue */
1222101704Smjacobrequest_t *
1223147883Sscottlmpt_get_request(struct mpt_softc *mpt, int sleep_ok)
1224101704Smjacob{
1225101704Smjacob	request_t *req;
1226147883Sscottl
1227147883Sscottlretry:
1228169293Smjacob	MPT_LOCK_ASSERT(mpt);
1229147883Sscottl	req = TAILQ_FIRST(&mpt->request_free_list);
1230101704Smjacob	if (req != NULL) {
1231147883Sscottl		KASSERT(req == &mpt->request_pool[req->index],
1232147883Sscottl		    ("mpt_get_request: corrupted request free list\n"));
1233157354Smjacob		KASSERT(req->state == REQ_STATE_FREE,
1234157662Smjacob		    ("req %p:%u not free on free list %x index %d function %x",
1235157662Smjacob		    req, req->serno, req->state, req->index,
1236157662Smjacob		    ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1237147883Sscottl		TAILQ_REMOVE(&mpt->request_free_list, req, links);
1238147883Sscottl		req->state = REQ_STATE_ALLOCATED;
1239155521Smjacob		req->chain = NULL;
1240157662Smjacob		mpt_assign_serno(mpt, req);
1241171842Sscottl		mpt_callout_init(&req->callout);
1242147883Sscottl	} else if (sleep_ok != 0) {
1243147883Sscottl		mpt->getreqwaiter = 1;
1244147883Sscottl		mpt_sleep(mpt, &mpt->request_free_list, PUSER, "mptgreq", 0);
1245147883Sscottl		goto retry;
1246101704Smjacob	}
1247157354Smjacob	return (req);
1248101704Smjacob}
1249101704Smjacob
1250101704Smjacob/* Pass the command to the IOC */
1251101704Smjacobvoid
1252147883Sscottlmpt_send_cmd(struct mpt_softc *mpt, request_t *req)
1253101704Smjacob{
1254158982Smjacob	if (mpt->verbose > MPT_PRT_DEBUG2) {
1255158932Smjacob		mpt_dump_request(mpt, req);
1256155521Smjacob	}
1257101704Smjacob	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
1258103914Smjacob	    BUS_DMASYNC_PREWRITE);
1259147883Sscottl	req->state |= REQ_STATE_QUEUED;
1260157662Smjacob	KASSERT(mpt_req_on_free_list(mpt, req) == 0,
1261157662Smjacob	    ("req %p:%u func %x on freelist list in mpt_send_cmd",
1262157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1263157662Smjacob	KASSERT(mpt_req_on_pending_list(mpt, req) == 0,
1264157662Smjacob	    ("req %p:%u func %x already on pending list in mpt_send_cmd",
1265157662Smjacob	    req, req->serno, ((MSG_REQUEST_HEADER *)req->req_vbuf)->Function));
1266147883Sscottl	TAILQ_INSERT_HEAD(&mpt->request_pending_list, req, links);
1267147883Sscottl	mpt_write(mpt, MPT_OFFSET_REQUEST_Q, (uint32_t) req->req_pbuf);
1268101704Smjacob}
1269101704Smjacob
1270101704Smjacob/*
1271147883Sscottl * Wait for a request to complete.
1272147883Sscottl *
1273147883Sscottl * Inputs:
1274147883Sscottl *	mpt		softc of controller executing request
1275147883Sscottl *	req		request to wait for
1276147883Sscottl *	sleep_ok	nonzero implies may sleep in this context
1277147883Sscottl *	time_ms		timeout in ms.  0 implies no timeout.
1278147883Sscottl *
1279147883Sscottl * Return Values:
1280147883Sscottl *	0		Request completed
1281147883Sscottl *	non-0		Timeout fired before request completion.
1282101704Smjacob */
1283147883Sscottlint
1284147883Sscottlmpt_wait_req(struct mpt_softc *mpt, request_t *req,
1285147883Sscottl	     mpt_req_state_t state, mpt_req_state_t mask,
1286147883Sscottl	     int sleep_ok, int time_ms)
1287101704Smjacob{
1288147883Sscottl	int   error;
1289147883Sscottl	int   timeout;
1290147883Sscottl	u_int saved_cnt;
1291101704Smjacob
1292147883Sscottl	/*
1293147883Sscottl	 * timeout is in ms.  0 indicates infinite wait.
1294147883Sscottl	 * Convert to ticks or 500us units depending on
1295147883Sscottl	 * our sleep mode.
1296147883Sscottl	 */
1297157117Smjacob	if (sleep_ok != 0) {
1298147883Sscottl		timeout = (time_ms * hz) / 1000;
1299157117Smjacob	} else {
1300147883Sscottl		timeout = time_ms * 2;
1301157117Smjacob	}
1302147883Sscottl	req->state |= REQ_STATE_NEED_WAKEUP;
1303147883Sscottl	mask &= ~REQ_STATE_NEED_WAKEUP;
1304155521Smjacob	saved_cnt = mpt->reset_cnt;
1305157117Smjacob	while ((req->state & mask) != state && mpt->reset_cnt == saved_cnt) {
1306147883Sscottl		if (sleep_ok != 0) {
1307147883Sscottl			error = mpt_sleep(mpt, req, PUSER, "mptreq", timeout);
1308147883Sscottl			if (error == EWOULDBLOCK) {
1309147883Sscottl				timeout = 0;
1310147883Sscottl				break;
1311147883Sscottl			}
1312147883Sscottl		} else {
1313147883Sscottl			if (time_ms != 0 && --timeout == 0) {
1314147883Sscottl				break;
1315147883Sscottl			}
1316147883Sscottl			DELAY(500);
1317147883Sscottl			mpt_intr(mpt);
1318147883Sscottl		}
1319147883Sscottl	}
1320147883Sscottl	req->state &= ~REQ_STATE_NEED_WAKEUP;
1321157117Smjacob	if (mpt->reset_cnt != saved_cnt) {
1322147883Sscottl		return (EIO);
1323157117Smjacob	}
1324157117Smjacob	if (time_ms && timeout <= 0) {
1325157117Smjacob		MSG_REQUEST_HEADER *msg_hdr = req->req_vbuf;
1326160396Smjacob		req->state |= REQ_STATE_TIMEDOUT;
1327157117Smjacob		mpt_prt(mpt, "mpt_wait_req(%x) timed out\n", msg_hdr->Function);
1328147883Sscottl		return (ETIMEDOUT);
1329157117Smjacob	}
1330147883Sscottl	return (0);
1331101704Smjacob}
1332101704Smjacob
1333101704Smjacob/*
1334101704Smjacob * Send a command to the IOC via the handshake register.
1335101704Smjacob *
1336101704Smjacob * Only done at initialization time and for certain unusual
1337101704Smjacob * commands such as device/bus reset as specified by LSI.
1338101704Smjacob */
1339101704Smjacobint
1340147883Sscottlmpt_send_handshake_cmd(struct mpt_softc *mpt, size_t len, void *cmd)
1341101704Smjacob{
1342101704Smjacob	int i;
1343147883Sscottl	uint32_t data, *data32;
1344101704Smjacob
1345101704Smjacob	/* Check condition of the IOC */
1346101704Smjacob	data = mpt_rd_db(mpt);
1347147883Sscottl	if ((MPT_STATE(data) != MPT_DB_STATE_READY
1348147883Sscottl	  && MPT_STATE(data) != MPT_DB_STATE_RUNNING
1349147883Sscottl	  && MPT_STATE(data) != MPT_DB_STATE_FAULT)
1350147883Sscottl	 || MPT_DB_IS_IN_USE(data)) {
1351147883Sscottl		mpt_prt(mpt, "handshake aborted - invalid doorbell state\n");
1352101704Smjacob		mpt_print_db(data);
1353147883Sscottl		return (EBUSY);
1354101704Smjacob	}
1355101704Smjacob
1356101704Smjacob	/* We move things in 32 bit chunks */
1357101704Smjacob	len = (len + 3) >> 2;
1358101704Smjacob	data32 = cmd;
1359101704Smjacob
1360172568Skevlo	/* Clear any left over pending doorbell interrupts */
1361101704Smjacob	if (MPT_DB_INTR(mpt_rd_intr(mpt)))
1362101704Smjacob		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1363101704Smjacob
1364101704Smjacob	/*
1365101704Smjacob	 * Tell the handshake reg. we are going to send a command
1366101704Smjacob         * and how long it is going to be.
1367101704Smjacob	 */
1368101704Smjacob	data = (MPI_FUNCTION_HANDSHAKE << MPI_DOORBELL_FUNCTION_SHIFT) |
1369101704Smjacob	    (len << MPI_DOORBELL_ADD_DWORDS_SHIFT);
1370101704Smjacob	mpt_write(mpt, MPT_OFFSET_DOORBELL, data);
1371101704Smjacob
1372101704Smjacob	/* Wait for the chip to notice */
1373101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1374164837Smjacob		mpt_prt(mpt, "mpt_send_handshake_cmd: db ignored\n");
1375147883Sscottl		return (ETIMEDOUT);
1376101704Smjacob	}
1377101704Smjacob
1378101704Smjacob	/* Clear the interrupt */
1379101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1380101704Smjacob
1381101704Smjacob	if (mpt_wait_db_ack(mpt) != MPT_OK) {
1382164837Smjacob		mpt_prt(mpt, "mpt_send_handshake_cmd: db ack timed out\n");
1383147883Sscottl		return (ETIMEDOUT);
1384101704Smjacob	}
1385101704Smjacob
1386101704Smjacob	/* Send the command */
1387101704Smjacob	for (i = 0; i < len; i++) {
1388164315Sjb		mpt_write(mpt, MPT_OFFSET_DOORBELL, htole32(*data32++));
1389101704Smjacob		if (mpt_wait_db_ack(mpt) != MPT_OK) {
1390156104Smjacob			mpt_prt(mpt,
1391164837Smjacob			    "mpt_send_handshake_cmd: timeout @ index %d\n", i);
1392147883Sscottl			return (ETIMEDOUT);
1393101704Smjacob		}
1394101704Smjacob	}
1395101704Smjacob	return MPT_OK;
1396101704Smjacob}
1397101704Smjacob
1398101704Smjacob/* Get the response from the handshake register */
1399101704Smjacobint
1400147883Sscottlmpt_recv_handshake_reply(struct mpt_softc *mpt, size_t reply_len, void *reply)
1401101704Smjacob{
1402101704Smjacob	int left, reply_left;
1403101704Smjacob	u_int16_t *data16;
1404164315Sjb	uint32_t data;
1405101704Smjacob	MSG_DEFAULT_REPLY *hdr;
1406101704Smjacob
1407101704Smjacob	/* We move things out in 16 bit chunks */
1408101704Smjacob	reply_len >>= 1;
1409101704Smjacob	data16 = (u_int16_t *)reply;
1410101704Smjacob
1411101704Smjacob	hdr = (MSG_DEFAULT_REPLY *)reply;
1412101704Smjacob
1413101704Smjacob	/* Get first word */
1414101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1415147883Sscottl		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout1\n");
1416101704Smjacob		return ETIMEDOUT;
1417101704Smjacob	}
1418164315Sjb	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1419164315Sjb	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
1420101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1421101704Smjacob
1422101704Smjacob	/* Get Second Word */
1423101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1424147883Sscottl		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout2\n");
1425101704Smjacob		return ETIMEDOUT;
1426101704Smjacob	}
1427164315Sjb	data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1428164315Sjb	*data16++ = le16toh(data & MPT_DB_DATA_MASK);
1429101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1430101704Smjacob
1431157354Smjacob	/*
1432157354Smjacob	 * With the second word, we can now look at the length.
1433157354Smjacob	 * Warn about a reply that's too short (except for IOC FACTS REPLY)
1434157354Smjacob	 */
1435157354Smjacob	if ((reply_len >> 1) != hdr->MsgLength &&
1436157354Smjacob	    (hdr->Function != MPI_FUNCTION_IOC_FACTS)){
1437157354Smjacob#if __FreeBSD_version >= 500000
1438103914Smjacob		mpt_prt(mpt, "reply length does not match message length: "
1439157382Smjacob			"got %x; expected %zx for function %x\n",
1440157354Smjacob			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
1441157354Smjacob#else
1442157354Smjacob		mpt_prt(mpt, "reply length does not match message length: "
1443157382Smjacob			"got %x; expected %x for function %x\n",
1444157354Smjacob			hdr->MsgLength << 2, reply_len << 1, hdr->Function);
1445157354Smjacob#endif
1446101704Smjacob	}
1447101704Smjacob
1448101704Smjacob	/* Get rest of the reply; but don't overflow the provided buffer */
1449101704Smjacob	left = (hdr->MsgLength << 1) - 2;
1450101704Smjacob	reply_left =  reply_len - 2;
1451101704Smjacob	while (left--) {
1452101704Smjacob		u_int16_t datum;
1453101704Smjacob
1454101704Smjacob		if (mpt_wait_db_int(mpt) != MPT_OK) {
1455147883Sscottl			mpt_prt(mpt, "mpt_recv_handshake_cmd timeout3\n");
1456101704Smjacob			return ETIMEDOUT;
1457101704Smjacob		}
1458164315Sjb		data = mpt_read(mpt, MPT_OFFSET_DOORBELL);
1459164315Sjb		datum = le16toh(data & MPT_DB_DATA_MASK);
1460101704Smjacob
1461101704Smjacob		if (reply_left-- > 0)
1462164315Sjb			*data16++ = datum;
1463101704Smjacob
1464101704Smjacob		mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1465101704Smjacob	}
1466101704Smjacob
1467101704Smjacob	/* One more wait & clear at the end */
1468101704Smjacob	if (mpt_wait_db_int(mpt) != MPT_OK) {
1469147883Sscottl		mpt_prt(mpt, "mpt_recv_handshake_cmd timeout4\n");
1470101704Smjacob		return ETIMEDOUT;
1471101704Smjacob	}
1472101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_STATUS, 0);
1473101704Smjacob
1474101704Smjacob	if ((hdr->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1475147883Sscottl		if (mpt->verbose >= MPT_PRT_TRACE)
1476101704Smjacob			mpt_print_reply(hdr);
1477101704Smjacob		return (MPT_FAIL | hdr->IOCStatus);
1478101704Smjacob	}
1479101704Smjacob
1480101704Smjacob	return (0);
1481101704Smjacob}
1482101704Smjacob
1483101704Smjacobstatic int
1484147883Sscottlmpt_get_iocfacts(struct mpt_softc *mpt, MSG_IOC_FACTS_REPLY *freplp)
1485101704Smjacob{
1486101704Smjacob	MSG_IOC_FACTS f_req;
1487101704Smjacob	int error;
1488101704Smjacob
1489157354Smjacob	memset(&f_req, 0, sizeof f_req);
1490101704Smjacob	f_req.Function = MPI_FUNCTION_IOC_FACTS;
1491147883Sscottl	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1492101704Smjacob	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
1493164990Smjacob	if (error) {
1494101704Smjacob		return(error);
1495164990Smjacob	}
1496101704Smjacob	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
1497101704Smjacob	return (error);
1498101704Smjacob}
1499101704Smjacob
1500102199Smjacobstatic int
1501164990Smjacobmpt_get_portfacts(struct mpt_softc *mpt, U8 port, MSG_PORT_FACTS_REPLY *freplp)
1502102199Smjacob{
1503102199Smjacob	MSG_PORT_FACTS f_req;
1504102199Smjacob	int error;
1505102199Smjacob
1506147883Sscottl	memset(&f_req, 0, sizeof f_req);
1507102199Smjacob	f_req.Function = MPI_FUNCTION_PORT_FACTS;
1508164990Smjacob	f_req.PortNumber = port;
1509147883Sscottl	f_req.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1510102199Smjacob	error = mpt_send_handshake_cmd(mpt, sizeof f_req, &f_req);
1511164990Smjacob	if (error) {
1512102199Smjacob		return(error);
1513164990Smjacob	}
1514102199Smjacob	error = mpt_recv_handshake_reply(mpt, sizeof (*freplp), freplp);
1515102199Smjacob	return (error);
1516102199Smjacob}
1517102199Smjacob
1518101704Smjacob/*
1519101704Smjacob * Send the initialization request. This is where we specify how many
1520101704Smjacob * SCSI busses and how many devices per bus we wish to emulate.
1521101704Smjacob * This is also the command that specifies the max size of the reply
1522101704Smjacob * frames from the IOC that we will be allocating.
1523101704Smjacob */
1524101704Smjacobstatic int
1525147883Sscottlmpt_send_ioc_init(struct mpt_softc *mpt, uint32_t who)
1526101704Smjacob{
1527101704Smjacob	int error = 0;
1528101704Smjacob	MSG_IOC_INIT init;
1529101704Smjacob	MSG_IOC_INIT_REPLY reply;
1530101704Smjacob
1531157354Smjacob	memset(&init, 0, sizeof init);
1532101704Smjacob	init.WhoInit = who;
1533101704Smjacob	init.Function = MPI_FUNCTION_IOC_INIT;
1534164990Smjacob	init.MaxDevices = 0;	/* at least 256 devices per bus */
1535164990Smjacob	init.MaxBuses = 16;	/* at least 16 busses */
1536155521Smjacob
1537155521Smjacob	init.MsgVersion = htole16(MPI_VERSION);
1538155521Smjacob	init.HeaderVersion = htole16(MPI_HEADER_VERSION);
1539155521Smjacob	init.ReplyFrameSize = htole16(MPT_REPLY_SIZE);
1540147883Sscottl	init.MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
1541101704Smjacob
1542101704Smjacob	if ((error = mpt_send_handshake_cmd(mpt, sizeof init, &init)) != 0) {
1543101704Smjacob		return(error);
1544101704Smjacob	}
1545101704Smjacob
1546101704Smjacob	error = mpt_recv_handshake_reply(mpt, sizeof reply, &reply);
1547101704Smjacob	return (error);
1548101704Smjacob}
1549101704Smjacob
1550102199Smjacob
1551102199Smjacob/*
1552102199Smjacob * Utiltity routine to read configuration headers and pages
1553102199Smjacob */
1554147883Sscottlint
1555170252Sscottlmpt_issue_cfg_req(struct mpt_softc *mpt, request_t *req, cfgparms_t *params,
1556170252Sscottl		  bus_addr_t addr, bus_size_t len, int sleep_ok, int timeout_ms)
1557101704Smjacob{
1558102199Smjacob	MSG_CONFIG *cfgp;
1559147883Sscottl	SGE_SIMPLE32 *se;
1560102199Smjacob
1561102199Smjacob	cfgp = req->req_vbuf;
1562147883Sscottl	memset(cfgp, 0, sizeof *cfgp);
1563170252Sscottl	cfgp->Action = params->Action;
1564102199Smjacob	cfgp->Function = MPI_FUNCTION_CONFIG;
1565170252Sscottl	cfgp->Header.PageVersion = params->PageVersion;
1566170252Sscottl	cfgp->Header.PageNumber = params->PageNumber;
1567170252Sscottl	cfgp->PageAddress = htole32(params->PageAddress);
1568170252Sscottl	if ((params->PageType & MPI_CONFIG_PAGETYPE_MASK) ==
1569170252Sscottl	    MPI_CONFIG_PAGETYPE_EXTENDED) {
1570170252Sscottl		cfgp->Header.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1571170252Sscottl		cfgp->Header.PageLength = 0;
1572170252Sscottl		cfgp->ExtPageLength = htole16(params->ExtPageLength);
1573170252Sscottl		cfgp->ExtPageType = params->ExtPageType;
1574170252Sscottl	} else {
1575170252Sscottl		cfgp->Header.PageType = params->PageType;
1576170252Sscottl		cfgp->Header.PageLength = params->PageLength;
1577170252Sscottl	}
1578147883Sscottl	se = (SGE_SIMPLE32 *)&cfgp->PageBufferSGE;
1579164315Sjb	se->Address = htole32(addr);
1580147883Sscottl	MPI_pSGE_SET_LENGTH(se, len);
1581147883Sscottl	MPI_pSGE_SET_FLAGS(se, (MPI_SGE_FLAGS_SIMPLE_ELEMENT |
1582147883Sscottl	    MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1583147883Sscottl	    MPI_SGE_FLAGS_END_OF_LIST |
1584170252Sscottl	    ((params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT
1585170252Sscottl	  || params->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM)
1586147883Sscottl	   ? MPI_SGE_FLAGS_HOST_TO_IOC : MPI_SGE_FLAGS_IOC_TO_HOST)));
1587164315Sjb	se->FlagsLength = htole32(se->FlagsLength);
1588147883Sscottl	cfgp->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
1589102199Smjacob
1590102199Smjacob	mpt_check_doorbell(mpt);
1591102199Smjacob	mpt_send_cmd(mpt, req);
1592147883Sscottl	return (mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
1593147883Sscottl			     sleep_ok, timeout_ms));
1594147883Sscottl}
1595102199Smjacob
1596170252Sscottlint
1597170252Sscottlmpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion, int PageNumber,
1598170252Sscottl		       uint32_t PageAddress, int ExtPageType,
1599170252Sscottl		       CONFIG_EXTENDED_PAGE_HEADER *rslt,
1600170252Sscottl		       int sleep_ok, int timeout_ms)
1601170252Sscottl{
1602170252Sscottl	request_t  *req;
1603170252Sscottl	cfgparms_t params;
1604170252Sscottl	MSG_CONFIG_REPLY *cfgp;
1605170252Sscottl	int	    error;
1606147883Sscottl
1607170252Sscottl	req = mpt_get_request(mpt, sleep_ok);
1608170252Sscottl	if (req == NULL) {
1609170252Sscottl		mpt_prt(mpt, "mpt_extread_cfg_header: Get request failed!\n");
1610170252Sscottl		return (ENOMEM);
1611170252Sscottl	}
1612170252Sscottl
1613170252Sscottl	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
1614170252Sscottl	params.PageVersion = PageVersion;
1615170252Sscottl	params.PageLength = 0;
1616170252Sscottl	params.PageNumber = PageNumber;
1617170252Sscottl	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1618170252Sscottl	params.PageAddress = PageAddress;
1619170252Sscottl	params.ExtPageType = ExtPageType;
1620170252Sscottl	params.ExtPageLength = 0;
1621170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
1622170252Sscottl				  sleep_ok, timeout_ms);
1623170252Sscottl	if (error != 0) {
1624170252Sscottl		/*
1625170252Sscottl		 * Leave the request. Without resetting the chip, it's
1626170252Sscottl		 * still owned by it and we'll just get into trouble
1627170252Sscottl		 * freeing it now. Mark it as abandoned so that if it
1628170252Sscottl		 * shows up later it can be freed.
1629170252Sscottl		 */
1630170252Sscottl		mpt_prt(mpt, "read_extcfg_header timed out\n");
1631170252Sscottl		return (ETIMEDOUT);
1632170252Sscottl	}
1633170252Sscottl
1634170252Sscottl        switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
1635170252Sscottl	case MPI_IOCSTATUS_SUCCESS:
1636170252Sscottl		cfgp = req->req_vbuf;
1637170252Sscottl		rslt->PageVersion = cfgp->Header.PageVersion;
1638170252Sscottl		rslt->PageNumber = cfgp->Header.PageNumber;
1639170252Sscottl		rslt->PageType = cfgp->Header.PageType;
1640170252Sscottl		rslt->ExtPageLength = cfgp->ExtPageLength;
1641170252Sscottl		rslt->ExtPageType = cfgp->ExtPageType;
1642170252Sscottl		error = 0;
1643170252Sscottl		break;
1644170252Sscottl	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
1645170252Sscottl		mpt_lprt(mpt, MPT_PRT_DEBUG,
1646170252Sscottl		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
1647170252Sscottl		    MPI_CONFIG_PAGETYPE_EXTENDED, PageNumber, PageAddress);
1648170252Sscottl		error = EINVAL;
1649170252Sscottl		break;
1650170252Sscottl	default:
1651170252Sscottl		mpt_prt(mpt, "mpt_read_extcfg_header: Config Info Status %x\n",
1652170252Sscottl			req->IOCStatus);
1653170252Sscottl		error = EIO;
1654170252Sscottl		break;
1655170252Sscottl	}
1656170252Sscottl	mpt_free_request(mpt, req);
1657170252Sscottl	return (error);
1658170252Sscottl}
1659170252Sscottl
1660147883Sscottlint
1661170252Sscottlmpt_read_extcfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1662170252Sscottl		     CONFIG_EXTENDED_PAGE_HEADER *hdr, void *buf, size_t len,
1663170252Sscottl		     int sleep_ok, int timeout_ms)
1664170252Sscottl{
1665170252Sscottl	request_t    *req;
1666170252Sscottl	cfgparms_t    params;
1667170252Sscottl	int	      error;
1668170252Sscottl
1669170252Sscottl	req = mpt_get_request(mpt, sleep_ok);
1670170252Sscottl	if (req == NULL) {
1671170252Sscottl		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
1672170252Sscottl		return (-1);
1673170252Sscottl	}
1674170252Sscottl
1675170252Sscottl	params.Action = Action;
1676170252Sscottl	params.PageVersion = hdr->PageVersion;
1677170252Sscottl	params.PageLength = 0;
1678170252Sscottl	params.PageNumber = hdr->PageNumber;
1679170252Sscottl	params.PageType = MPI_CONFIG_PAGETYPE_EXTENDED;
1680170252Sscottl	params.PageAddress = PageAddress;
1681170252Sscottl	params.ExtPageType = hdr->ExtPageType;
1682170252Sscottl	params.ExtPageLength = hdr->ExtPageLength;
1683170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params,
1684170252Sscottl				  req->req_pbuf + MPT_RQSL(mpt),
1685170252Sscottl				  len, sleep_ok, timeout_ms);
1686170252Sscottl	if (error != 0) {
1687170252Sscottl		mpt_prt(mpt, "read_extcfg_page(%d) timed out\n", Action);
1688170252Sscottl		return (-1);
1689170252Sscottl	}
1690170252Sscottl
1691170252Sscottl	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1692170252Sscottl		mpt_prt(mpt, "mpt_read_extcfg_page: Config Info Status %x\n",
1693170252Sscottl			req->IOCStatus);
1694170252Sscottl		mpt_free_request(mpt, req);
1695170252Sscottl		return (-1);
1696170252Sscottl	}
1697170252Sscottl	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
1698170252Sscottl	    BUS_DMASYNC_POSTREAD);
1699170252Sscottl	memcpy(buf, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
1700170252Sscottl	mpt_free_request(mpt, req);
1701170252Sscottl	return (0);
1702170252Sscottl}
1703170252Sscottl
1704170252Sscottlint
1705147883Sscottlmpt_read_cfg_header(struct mpt_softc *mpt, int PageType, int PageNumber,
1706147883Sscottl		    uint32_t PageAddress, CONFIG_PAGE_HEADER *rslt,
1707147883Sscottl		    int sleep_ok, int timeout_ms)
1708147883Sscottl{
1709147883Sscottl	request_t  *req;
1710170252Sscottl	cfgparms_t params;
1711156104Smjacob	MSG_CONFIG *cfgp;
1712147883Sscottl	int	    error;
1713147883Sscottl
1714147883Sscottl	req = mpt_get_request(mpt, sleep_ok);
1715147883Sscottl	if (req == NULL) {
1716147883Sscottl		mpt_prt(mpt, "mpt_read_cfg_header: Get request failed!\n");
1717156104Smjacob		return (ENOMEM);
1718102199Smjacob	}
1719147883Sscottl
1720170252Sscottl	params.Action = MPI_CONFIG_ACTION_PAGE_HEADER;
1721170252Sscottl	params.PageVersion = 0;
1722170252Sscottl	params.PageLength = 0;
1723170252Sscottl	params.PageNumber = PageNumber;
1724170252Sscottl	params.PageType = PageType;
1725170252Sscottl	params.PageAddress = PageAddress;
1726170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params, /*addr*/0, /*len*/0,
1727147883Sscottl				  sleep_ok, timeout_ms);
1728147883Sscottl	if (error != 0) {
1729160396Smjacob		/*
1730160396Smjacob		 * Leave the request. Without resetting the chip, it's
1731160396Smjacob		 * still owned by it and we'll just get into trouble
1732160396Smjacob		 * freeing it now. Mark it as abandoned so that if it
1733160396Smjacob		 * shows up later it can be freed.
1734160396Smjacob		 */
1735147883Sscottl		mpt_prt(mpt, "read_cfg_header timed out\n");
1736156104Smjacob		return (ETIMEDOUT);
1737147883Sscottl	}
1738147883Sscottl
1739156104Smjacob        switch (req->IOCStatus & MPI_IOCSTATUS_MASK) {
1740156104Smjacob	case MPI_IOCSTATUS_SUCCESS:
1741147883Sscottl		cfgp = req->req_vbuf;
1742147883Sscottl		bcopy(&cfgp->Header, rslt, sizeof(*rslt));
1743147883Sscottl		error = 0;
1744156104Smjacob		break;
1745156104Smjacob	case MPI_IOCSTATUS_CONFIG_INVALID_PAGE:
1746156104Smjacob		mpt_lprt(mpt, MPT_PRT_DEBUG,
1747156104Smjacob		    "Invalid Page Type %d Number %d Addr 0x%0x\n",
1748156104Smjacob		    PageType, PageNumber, PageAddress);
1749156104Smjacob		error = EINVAL;
1750156104Smjacob		break;
1751156104Smjacob	default:
1752156104Smjacob		mpt_prt(mpt, "mpt_read_cfg_header: Config Info Status %x\n",
1753156104Smjacob			req->IOCStatus);
1754156104Smjacob		error = EIO;
1755156104Smjacob		break;
1756147883Sscottl	}
1757102199Smjacob	mpt_free_request(mpt, req);
1758147883Sscottl	return (error);
1759102199Smjacob}
1760102199Smjacob
1761102822Smjacobint
1762147883Sscottlmpt_read_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1763147883Sscottl		  CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1764147883Sscottl		  int timeout_ms)
1765102199Smjacob{
1766147883Sscottl	request_t    *req;
1767170252Sscottl	cfgparms_t    params;
1768147883Sscottl	int	      error;
1769102199Smjacob
1770147883Sscottl	req = mpt_get_request(mpt, sleep_ok);
1771147883Sscottl	if (req == NULL) {
1772147883Sscottl		mpt_prt(mpt, "mpt_read_cfg_page: Get request failed!\n");
1773147883Sscottl		return (-1);
1774147883Sscottl	}
1775102199Smjacob
1776170252Sscottl	params.Action = Action;
1777170252Sscottl	params.PageVersion = hdr->PageVersion;
1778170252Sscottl	params.PageLength = hdr->PageLength;
1779170252Sscottl	params.PageNumber = hdr->PageNumber;
1780170252Sscottl	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1781170252Sscottl	params.PageAddress = PageAddress;
1782170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params,
1783170252Sscottl				  req->req_pbuf + MPT_RQSL(mpt),
1784147883Sscottl				  len, sleep_ok, timeout_ms);
1785147883Sscottl	if (error != 0) {
1786147883Sscottl		mpt_prt(mpt, "read_cfg_page(%d) timed out\n", Action);
1787147883Sscottl		return (-1);
1788147883Sscottl	}
1789102199Smjacob
1790147883Sscottl	if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1791147883Sscottl		mpt_prt(mpt, "mpt_read_cfg_page: Config Info Status %x\n",
1792147883Sscottl			req->IOCStatus);
1793147883Sscottl		mpt_free_request(mpt, req);
1794102199Smjacob		return (-1);
1795102199Smjacob	}
1796102199Smjacob	bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
1797102199Smjacob	    BUS_DMASYNC_POSTREAD);
1798157117Smjacob	memcpy(hdr, ((uint8_t *)req->req_vbuf)+MPT_RQSL(mpt), len);
1799102199Smjacob	mpt_free_request(mpt, req);
1800102199Smjacob	return (0);
1801102199Smjacob}
1802102199Smjacob
1803102822Smjacobint
1804147883Sscottlmpt_write_cfg_page(struct mpt_softc *mpt, int Action, uint32_t PageAddress,
1805147883Sscottl		   CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1806147883Sscottl		   int timeout_ms)
1807102199Smjacob{
1808147883Sscottl	request_t    *req;
1809170252Sscottl	cfgparms_t    params;
1810147883Sscottl	u_int	      hdr_attr;
1811147883Sscottl	int	      error;
1812102199Smjacob
1813102199Smjacob	hdr_attr = hdr->PageType & MPI_CONFIG_PAGEATTR_MASK;
1814102199Smjacob	if (hdr_attr != MPI_CONFIG_PAGEATTR_CHANGEABLE &&
1815102199Smjacob	    hdr_attr != MPI_CONFIG_PAGEATTR_PERSISTENT) {
1816147883Sscottl		mpt_prt(mpt, "page type 0x%x not changeable\n",
1817147883Sscottl			hdr->PageType & MPI_CONFIG_PAGETYPE_MASK);
1818102199Smjacob		return (-1);
1819102199Smjacob	}
1820102199Smjacob
1821160290Smjacob#if	0
1822160290Smjacob	/*
1823160290Smjacob	 * We shouldn't mask off other bits here.
1824160290Smjacob	 */
1825160290Smjacob	hdr->PageType &= MPI_CONFIG_PAGETYPE_MASK;
1826160290Smjacob#endif
1827160290Smjacob
1828147883Sscottl	req = mpt_get_request(mpt, sleep_ok);
1829147883Sscottl	if (req == NULL)
1830147883Sscottl		return (-1);
1831102199Smjacob
1832160290Smjacob	memcpy(((caddr_t)req->req_vbuf) + MPT_RQSL(mpt), hdr, len);
1833160290Smjacob
1834160290Smjacob	/*
1835160290Smjacob	 * There isn't any point in restoring stripped out attributes
1836160290Smjacob	 * if you then mask them going down to issue the request.
1837160290Smjacob	 */
1838160290Smjacob
1839170252Sscottl	params.Action = Action;
1840170252Sscottl	params.PageVersion = hdr->PageVersion;
1841170252Sscottl	params.PageLength = hdr->PageLength;
1842170252Sscottl	params.PageNumber = hdr->PageNumber;
1843170252Sscottl	params.PageAddress = PageAddress;
1844160290Smjacob#if	0
1845147883Sscottl	/* Restore stripped out attributes */
1846147883Sscottl	hdr->PageType |= hdr_attr;
1847170252Sscottl	params.PageType = hdr->PageType & MPI_CONFIG_PAGETYPE_MASK;
1848160290Smjacob#else
1849170252Sscottl	params.PageType = hdr->PageType;
1850170252Sscottl#endif
1851170252Sscottl	error = mpt_issue_cfg_req(mpt, req, &params,
1852160290Smjacob				  req->req_pbuf + MPT_RQSL(mpt),
1853160290Smjacob				  len, sleep_ok, timeout_ms);
1854147883Sscottl	if (error != 0) {
1855147883Sscottl		mpt_prt(mpt, "mpt_write_cfg_page timed out\n");
1856147883Sscottl		return (-1);
1857147883Sscottl	}
1858102199Smjacob
1859147883Sscottl        if ((req->IOCStatus & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
1860147883Sscottl		mpt_prt(mpt, "mpt_write_cfg_page: Config Info Status %x\n",
1861147883Sscottl			req->IOCStatus);
1862147883Sscottl		mpt_free_request(mpt, req);
1863147883Sscottl		return (-1);
1864102199Smjacob	}
1865147883Sscottl	mpt_free_request(mpt, req);
1866147883Sscottl	return (0);
1867147883Sscottl}
1868102199Smjacob
1869147883Sscottl/*
1870147883Sscottl * Read IOC configuration information
1871147883Sscottl */
1872147883Sscottlstatic int
1873147883Sscottlmpt_read_config_info_ioc(struct mpt_softc *mpt)
1874147883Sscottl{
1875147883Sscottl	CONFIG_PAGE_HEADER hdr;
1876147883Sscottl	struct mpt_raid_volume *mpt_raid;
1877147883Sscottl	int rv;
1878147883Sscottl	int i;
1879147883Sscottl	size_t len;
1880147883Sscottl
1881147883Sscottl	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
1882158982Smjacob		2, 0, &hdr, FALSE, 5000);
1883156104Smjacob	/*
1884156104Smjacob	 * If it's an invalid page, so what? Not a supported function....
1885156104Smjacob	 */
1886158982Smjacob	if (rv == EINVAL) {
1887156104Smjacob		return (0);
1888158982Smjacob	}
1889158982Smjacob	if (rv) {
1890156104Smjacob		return (rv);
1891158982Smjacob	}
1892147883Sscottl
1893164990Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG,
1894164990Smjacob	    "IOC Page 2 Header: Version %x len %x PageNumber %x PageType %x\n",
1895164990Smjacob	    hdr.PageVersion, hdr.PageLength << 2,
1896164990Smjacob	    hdr.PageNumber, hdr.PageType);
1897147883Sscottl
1898147883Sscottl	len = hdr.PageLength * sizeof(uint32_t);
1899151075Sscottl	mpt->ioc_page2 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1900158982Smjacob	if (mpt->ioc_page2 == NULL) {
1901158982Smjacob		mpt_prt(mpt, "unable to allocate memory for IOC page 2\n");
1902158982Smjacob		mpt_raid_free_mem(mpt);
1903147883Sscottl		return (ENOMEM);
1904158982Smjacob	}
1905147883Sscottl	memcpy(&mpt->ioc_page2->Header, &hdr, sizeof(hdr));
1906158982Smjacob	rv = mpt_read_cur_cfg_page(mpt, 0,
1907158982Smjacob	    &mpt->ioc_page2->Header, len, FALSE, 5000);
1908147883Sscottl	if (rv) {
1909147883Sscottl		mpt_prt(mpt, "failed to read IOC Page 2\n");
1910158982Smjacob		mpt_raid_free_mem(mpt);
1911158982Smjacob		return (EIO);
1912158982Smjacob	}
1913164990Smjacob	mpt2host_config_page_ioc2(mpt->ioc_page2);
1914158982Smjacob
1915158982Smjacob	if (mpt->ioc_page2->CapabilitiesFlags != 0) {
1916147883Sscottl		uint32_t mask;
1917147883Sscottl
1918147883Sscottl		mpt_prt(mpt, "Capabilities: (");
1919147883Sscottl		for (mask = 1; mask != 0; mask <<= 1) {
1920158982Smjacob			if ((mpt->ioc_page2->CapabilitiesFlags & mask) == 0) {
1921147883Sscottl				continue;
1922158982Smjacob			}
1923147883Sscottl			switch (mask) {
1924147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT:
1925147883Sscottl				mpt_prtc(mpt, " RAID-0");
1926147883Sscottl				break;
1927147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT:
1928147883Sscottl				mpt_prtc(mpt, " RAID-1E");
1929147883Sscottl				break;
1930147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT:
1931147883Sscottl				mpt_prtc(mpt, " RAID-1");
1932147883Sscottl				break;
1933147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_SES_SUPPORT:
1934147883Sscottl				mpt_prtc(mpt, " SES");
1935147883Sscottl				break;
1936147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_SAFTE_SUPPORT:
1937147883Sscottl				mpt_prtc(mpt, " SAFTE");
1938147883Sscottl				break;
1939147883Sscottl			case MPI_IOCPAGE2_CAP_FLAGS_CROSS_CHANNEL_SUPPORT:
1940147883Sscottl				mpt_prtc(mpt, " Multi-Channel-Arrays");
1941147883Sscottl			default:
1942147883Sscottl				break;
1943147883Sscottl			}
1944102199Smjacob		}
1945147883Sscottl		mpt_prtc(mpt, " )\n");
1946147883Sscottl		if ((mpt->ioc_page2->CapabilitiesFlags
1947147883Sscottl		   & (MPI_IOCPAGE2_CAP_FLAGS_IS_SUPPORT
1948147883Sscottl		    | MPI_IOCPAGE2_CAP_FLAGS_IME_SUPPORT
1949147883Sscottl		    | MPI_IOCPAGE2_CAP_FLAGS_IM_SUPPORT)) != 0) {
1950147883Sscottl			mpt_prt(mpt, "%d Active Volume%s(%d Max)\n",
1951147883Sscottl				mpt->ioc_page2->NumActiveVolumes,
1952147883Sscottl				mpt->ioc_page2->NumActiveVolumes != 1
1953147883Sscottl			      ? "s " : " ",
1954147883Sscottl				mpt->ioc_page2->MaxVolumes);
1955147883Sscottl			mpt_prt(mpt, "%d Hidden Drive Member%s(%d Max)\n",
1956147883Sscottl				mpt->ioc_page2->NumActivePhysDisks,
1957147883Sscottl				mpt->ioc_page2->NumActivePhysDisks != 1
1958147883Sscottl			      ? "s " : " ",
1959147883Sscottl				mpt->ioc_page2->MaxPhysDisks);
1960147883Sscottl		}
1961147883Sscottl	}
1962102199Smjacob
1963147883Sscottl	len = mpt->ioc_page2->MaxVolumes * sizeof(struct mpt_raid_volume);
1964158982Smjacob	mpt->raid_volumes = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1965147883Sscottl	if (mpt->raid_volumes == NULL) {
1966147883Sscottl		mpt_prt(mpt, "Could not allocate RAID volume data\n");
1967158982Smjacob		mpt_raid_free_mem(mpt);
1968158982Smjacob		return (ENOMEM);
1969147883Sscottl	}
1970147883Sscottl
1971147883Sscottl	/*
1972147883Sscottl	 * Copy critical data out of ioc_page2 so that we can
1973147883Sscottl	 * safely refresh the page without windows of unreliable
1974147883Sscottl	 * data.
1975147883Sscottl	 */
1976147883Sscottl	mpt->raid_max_volumes =  mpt->ioc_page2->MaxVolumes;
1977147883Sscottl
1978158982Smjacob	len = sizeof(*mpt->raid_volumes->config_page) +
1979158982Smjacob	    (sizeof (RAID_VOL0_PHYS_DISK) * (mpt->ioc_page2->MaxPhysDisks - 1));
1980147883Sscottl	for (i = 0; i < mpt->ioc_page2->MaxVolumes; i++) {
1981147883Sscottl		mpt_raid = &mpt->raid_volumes[i];
1982158982Smjacob		mpt_raid->config_page =
1983158982Smjacob		    malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1984147883Sscottl		if (mpt_raid->config_page == NULL) {
1985147883Sscottl			mpt_prt(mpt, "Could not allocate RAID page data\n");
1986158982Smjacob			mpt_raid_free_mem(mpt);
1987158982Smjacob			return (ENOMEM);
1988147883Sscottl		}
1989147883Sscottl	}
1990147883Sscottl	mpt->raid_page0_len = len;
1991147883Sscottl
1992147883Sscottl	len = mpt->ioc_page2->MaxPhysDisks * sizeof(struct mpt_raid_disk);
1993158982Smjacob	mpt->raid_disks = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
1994147883Sscottl	if (mpt->raid_disks == NULL) {
1995147883Sscottl		mpt_prt(mpt, "Could not allocate RAID disk data\n");
1996158982Smjacob		mpt_raid_free_mem(mpt);
1997158982Smjacob		return (ENOMEM);
1998147883Sscottl	}
1999147883Sscottl	mpt->raid_max_disks =  mpt->ioc_page2->MaxPhysDisks;
2000147883Sscottl
2001158982Smjacob	/*
2002158982Smjacob	 * Load page 3.
2003158982Smjacob	 */
2004147883Sscottl	rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_IOC,
2005158982Smjacob	    3, 0, &hdr, FALSE, 5000);
2006158982Smjacob	if (rv) {
2007158982Smjacob		mpt_raid_free_mem(mpt);
2008147883Sscottl		return (EIO);
2009158982Smjacob	}
2010147883Sscottl
2011147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC Page 3 Header: %x %x %x %x\n",
2012158982Smjacob	    hdr.PageVersion, hdr.PageLength, hdr.PageNumber, hdr.PageType);
2013147883Sscottl
2014147883Sscottl	len = hdr.PageLength * sizeof(uint32_t);
2015151075Sscottl	mpt->ioc_page3 = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2016158982Smjacob	if (mpt->ioc_page3 == NULL) {
2017158982Smjacob		mpt_prt(mpt, "unable to allocate memory for IOC page 3\n");
2018158982Smjacob		mpt_raid_free_mem(mpt);
2019158982Smjacob		return (ENOMEM);
2020158982Smjacob	}
2021147883Sscottl	memcpy(&mpt->ioc_page3->Header, &hdr, sizeof(hdr));
2022158982Smjacob	rv = mpt_read_cur_cfg_page(mpt, 0,
2023158982Smjacob	    &mpt->ioc_page3->Header, len, FALSE, 5000);
2024147883Sscottl	if (rv) {
2025158982Smjacob		mpt_raid_free_mem(mpt);
2026158982Smjacob		return (EIO);
2027102199Smjacob	}
2028147883Sscottl	mpt_raid_wakeup(mpt);
2029102199Smjacob	return (0);
2030102199Smjacob}
2031102199Smjacob
2032102199Smjacob/*
2033102199Smjacob * Enable IOC port
2034102199Smjacob */
2035102199Smjacobstatic int
2036147883Sscottlmpt_send_port_enable(struct mpt_softc *mpt, int port)
2037102199Smjacob{
2038147883Sscottl	request_t	*req;
2039101704Smjacob	MSG_PORT_ENABLE *enable_req;
2040147883Sscottl	int		 error;
2041101704Smjacob
2042147883Sscottl	req = mpt_get_request(mpt, /*sleep_ok*/FALSE);
2043147883Sscottl	if (req == NULL)
2044147883Sscottl		return (-1);
2045101704Smjacob
2046101704Smjacob	enable_req = req->req_vbuf;
2047157354Smjacob	memset(enable_req, 0,  MPT_RQSL(mpt));
2048101704Smjacob
2049101704Smjacob	enable_req->Function   = MPI_FUNCTION_PORT_ENABLE;
2050147883Sscottl	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_CONFIG);
2051101704Smjacob	enable_req->PortNumber = port;
2052101704Smjacob
2053101704Smjacob	mpt_check_doorbell(mpt);
2054147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabling port %d\n", port);
2055147883Sscottl
2056147883Sscottl	mpt_send_cmd(mpt, req);
2057147883Sscottl	error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE,
2058157662Smjacob	    FALSE, (mpt->is_sas || mpt->is_fc)? 30000 : 3000);
2059147883Sscottl	if (error != 0) {
2060157117Smjacob		mpt_prt(mpt, "port %d enable timed out\n", port);
2061147883Sscottl		return (-1);
2062101704Smjacob	}
2063101704Smjacob	mpt_free_request(mpt, req);
2064157117Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "enabled port %d\n", port);
2065101704Smjacob	return (0);
2066101704Smjacob}
2067101704Smjacob
2068101704Smjacob/*
2069101704Smjacob * Enable/Disable asynchronous event reporting.
2070101704Smjacob */
2071101704Smjacobstatic int
2072147883Sscottlmpt_send_event_request(struct mpt_softc *mpt, int onoff)
2073101704Smjacob{
2074101704Smjacob	request_t *req;
2075101704Smjacob	MSG_EVENT_NOTIFY *enable_req;
2076101704Smjacob
2077157354Smjacob	req = mpt_get_request(mpt, FALSE);
2078157354Smjacob	if (req == NULL) {
2079157354Smjacob		return (ENOMEM);
2080157354Smjacob	}
2081101704Smjacob	enable_req = req->req_vbuf;
2082157354Smjacob	memset(enable_req, 0, sizeof *enable_req);
2083101704Smjacob
2084101704Smjacob	enable_req->Function   = MPI_FUNCTION_EVENT_NOTIFICATION;
2085147883Sscottl	enable_req->MsgContext = htole32(req->index | MPT_REPLY_HANDLER_EVENTS);
2086101704Smjacob	enable_req->Switch     = onoff;
2087101704Smjacob
2088101704Smjacob	mpt_check_doorbell(mpt);
2089157354Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "%sabling async events\n",
2090157354Smjacob	    onoff ? "en" : "dis");
2091157354Smjacob	/*
2092157354Smjacob	 * Send the command off, but don't wait for it.
2093157354Smjacob	 */
2094101704Smjacob	mpt_send_cmd(mpt, req);
2095101704Smjacob	return (0);
2096101704Smjacob}
2097101704Smjacob
2098101704Smjacob/*
2099172568Skevlo * Un-mask the interrupts on the chip.
2100101704Smjacob */
2101101704Smjacobvoid
2102147883Sscottlmpt_enable_ints(struct mpt_softc *mpt)
2103101704Smjacob{
2104101704Smjacob	/* Unmask every thing except door bell int */
2105101704Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_MASK, MPT_INTR_DB_MASK);
2106101704Smjacob}
2107101704Smjacob
2108101704Smjacob/*
2109172568Skevlo * Mask the interrupts on the chip.
2110101704Smjacob */
2111101704Smjacobvoid
2112147883Sscottlmpt_disable_ints(struct mpt_softc *mpt)
2113101704Smjacob{
2114101704Smjacob	/* Mask all interrupts */
2115156104Smjacob	mpt_write(mpt, MPT_OFFSET_INTR_MASK,
2116101704Smjacob	    MPT_INTR_REPLY_MASK | MPT_INTR_DB_MASK);
2117101704Smjacob}
2118101704Smjacob
2119147883Sscottlstatic void
2120147883Sscottlmpt_sysctl_attach(struct mpt_softc *mpt)
2121147883Sscottl{
2122157117Smjacob#if __FreeBSD_version >= 500000
2123147883Sscottl	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
2124147883Sscottl	struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
2125147883Sscottl
2126156104Smjacob	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2127147883Sscottl		       "debug", CTLFLAG_RW, &mpt->verbose, 0,
2128147883Sscottl		       "Debugging/Verbose level");
2129160290Smjacob	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2130160290Smjacob		       "role", CTLFLAG_RD, &mpt->role, 0,
2131160290Smjacob		       "HBA role");
2132165814Smjacob#ifdef	MPT_TEST_MULTIPATH
2133165814Smjacob	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
2134165814Smjacob		       "failure_id", CTLFLAG_RW, &mpt->failure_id, -1,
2135165814Smjacob		       "Next Target to Fail");
2136157117Smjacob#endif
2137165814Smjacob#endif
2138147883Sscottl}
2139147883Sscottl
2140101704Smjacobint
2141147883Sscottlmpt_attach(struct mpt_softc *mpt)
2142101704Smjacob{
2143157117Smjacob	struct mpt_personality *pers;
2144147883Sscottl	int i;
2145157117Smjacob	int error;
2146147883Sscottl
2147177808Sscottl	mpt_core_attach(mpt);
2148177808Sscottl	mpt_core_enable(mpt);
2149177808Sscottl
2150162133Smjacob	TAILQ_INSERT_TAIL(&mpt_tailq, mpt, links);
2151147883Sscottl	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2152147883Sscottl		pers = mpt_personalities[i];
2153157117Smjacob		if (pers == NULL) {
2154147883Sscottl			continue;
2155157117Smjacob		}
2156147883Sscottl		if (pers->probe(mpt) == 0) {
2157147883Sscottl			error = pers->attach(mpt);
2158147883Sscottl			if (error != 0) {
2159147883Sscottl				mpt_detach(mpt);
2160147883Sscottl				return (error);
2161147883Sscottl			}
2162147883Sscottl			mpt->mpt_pers_mask |= (0x1 << pers->id);
2163147883Sscottl			pers->use_count++;
2164147883Sscottl		}
2165147883Sscottl	}
2166155521Smjacob
2167157117Smjacob	/*
2168157117Smjacob	 * Now that we've attached everything, do the enable function
2169157117Smjacob	 * for all of the personalities. This allows the personalities
2170157117Smjacob	 * to do setups that are appropriate for them prior to enabling
2171157117Smjacob	 * any ports.
2172157117Smjacob	 */
2173157117Smjacob	for (i = 0; i < MPT_MAX_PERSONALITIES; i++) {
2174157117Smjacob		pers = mpt_personalities[i];
2175157117Smjacob		if (pers != NULL  && MPT_PERS_ATTACHED(pers, mpt) != 0) {
2176157117Smjacob			error = pers->enable(mpt);
2177157117Smjacob			if (error != 0) {
2178157117Smjacob				mpt_prt(mpt, "personality %s attached but would"
2179157117Smjacob				    " not enable (%d)\n", pers->name, error);
2180157117Smjacob				mpt_detach(mpt);
2181157117Smjacob				return (error);
2182157117Smjacob			}
2183157117Smjacob		}
2184157117Smjacob	}
2185147883Sscottl	return (0);
2186147883Sscottl}
2187147883Sscottl
2188147883Sscottlint
2189147883Sscottlmpt_shutdown(struct mpt_softc *mpt)
2190147883Sscottl{
2191147883Sscottl	struct mpt_personality *pers;
2192147883Sscottl
2193157117Smjacob	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2194147883Sscottl		pers->shutdown(mpt);
2195157117Smjacob	}
2196147883Sscottl	return (0);
2197147883Sscottl}
2198147883Sscottl
2199147883Sscottlint
2200147883Sscottlmpt_detach(struct mpt_softc *mpt)
2201147883Sscottl{
2202147883Sscottl	struct mpt_personality *pers;
2203147883Sscottl
2204147883Sscottl	MPT_PERS_FOREACH_REVERSE(mpt, pers) {
2205147883Sscottl		pers->detach(mpt);
2206147883Sscottl		mpt->mpt_pers_mask &= ~(0x1 << pers->id);
2207147883Sscottl		pers->use_count--;
2208147883Sscottl	}
2209162133Smjacob	TAILQ_REMOVE(&mpt_tailq, mpt, links);
2210147883Sscottl	return (0);
2211147883Sscottl}
2212147883Sscottl
2213147883Sscottlint
2214147883Sscottlmpt_core_load(struct mpt_personality *pers)
2215147883Sscottl{
2216147883Sscottl	int i;
2217147883Sscottl
2218147883Sscottl	/*
2219147883Sscottl	 * Setup core handlers and insert the default handler
2220147883Sscottl	 * into all "empty slots".
2221147883Sscottl	 */
2222157117Smjacob	for (i = 0; i < MPT_NUM_REPLY_HANDLERS; i++) {
2223147883Sscottl		mpt_reply_handlers[i] = mpt_default_reply_handler;
2224157117Smjacob	}
2225147883Sscottl
2226147883Sscottl	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_EVENTS)] =
2227147883Sscottl	    mpt_event_reply_handler;
2228147883Sscottl	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_CONFIG)] =
2229147883Sscottl	    mpt_config_reply_handler;
2230147883Sscottl	mpt_reply_handlers[MPT_CBI(MPT_REPLY_HANDLER_HANDSHAKE)] =
2231147883Sscottl	    mpt_handshake_reply_handler;
2232147883Sscottl	return (0);
2233147883Sscottl}
2234147883Sscottl
2235147883Sscottl/*
2236147883Sscottl * Initialize per-instance driver data and perform
2237147883Sscottl * initial controller configuration.
2238147883Sscottl */
2239147883Sscottlint
2240147883Sscottlmpt_core_attach(struct mpt_softc *mpt)
2241147883Sscottl{
2242169293Smjacob        int val, error;
2243101704Smjacob
2244147883Sscottl	LIST_INIT(&mpt->ack_frames);
2245147883Sscottl	/* Put all request buffers on the free list */
2246147883Sscottl	TAILQ_INIT(&mpt->request_pending_list);
2247147883Sscottl	TAILQ_INIT(&mpt->request_free_list);
2248157354Smjacob	TAILQ_INIT(&mpt->request_timeout_list);
2249169293Smjacob	MPT_LOCK(mpt);
2250157117Smjacob	for (val = 0; val < MPT_MAX_REQUESTS(mpt); val++) {
2251157354Smjacob		request_t *req = &mpt->request_pool[val];
2252157354Smjacob		req->state = REQ_STATE_ALLOCATED;
2253157354Smjacob		mpt_free_request(mpt, req);
2254157117Smjacob	}
2255169293Smjacob	MPT_UNLOCK(mpt);
2256157117Smjacob	for (val = 0; val < MPT_MAX_LUNS; val++) {
2257157117Smjacob		STAILQ_INIT(&mpt->trt[val].atios);
2258157117Smjacob		STAILQ_INIT(&mpt->trt[val].inots);
2259157117Smjacob	}
2260157117Smjacob	STAILQ_INIT(&mpt->trt_wildcard.atios);
2261157117Smjacob	STAILQ_INIT(&mpt->trt_wildcard.inots);
2262165814Smjacob#ifdef	MPT_TEST_MULTIPATH
2263165814Smjacob	mpt->failure_id = -1;
2264165814Smjacob#endif
2265157117Smjacob	mpt->scsi_tgt_handler_id = MPT_HANDLER_ID_NONE;
2266147883Sscottl	mpt_sysctl_attach(mpt);
2267147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "doorbell req = %s\n",
2268157117Smjacob	    mpt_ioc_diag(mpt_read(mpt, MPT_OFFSET_DOORBELL)));
2269169293Smjacob
2270169293Smjacob	MPT_LOCK(mpt);
2271177881Sscottl	error = mpt_configure_ioc(mpt, 0, 0);
2272169293Smjacob	MPT_UNLOCK(mpt);
2273169293Smjacob
2274169293Smjacob	return (error);
2275147883Sscottl}
2276147883Sscottl
2277157117Smjacobint
2278157117Smjacobmpt_core_enable(struct mpt_softc *mpt)
2279157117Smjacob{
2280157117Smjacob	/*
2281157117Smjacob	 * We enter with the IOC enabled, but async events
2282157117Smjacob	 * not enabled, ports not enabled and interrupts
2283157117Smjacob	 * not enabled.
2284157117Smjacob	 */
2285169293Smjacob	MPT_LOCK(mpt);
2286157117Smjacob
2287157117Smjacob	/*
2288157117Smjacob	 * Enable asynchronous event reporting- all personalities
2289157117Smjacob	 * have attached so that they should be able to now field
2290157117Smjacob	 * async events.
2291157117Smjacob	 */
2292157117Smjacob	mpt_send_event_request(mpt, 1);
2293157117Smjacob
2294157117Smjacob	/*
2295157117Smjacob	 * Catch any pending interrupts
2296157117Smjacob	 *
2297157117Smjacob	 * This seems to be crucial- otherwise
2298157117Smjacob	 * the portenable below times out.
2299157117Smjacob	 */
2300157117Smjacob	mpt_intr(mpt);
2301157117Smjacob
2302157117Smjacob	/*
2303157117Smjacob	 * Enable Interrupts
2304157117Smjacob	 */
2305157117Smjacob	mpt_enable_ints(mpt);
2306157117Smjacob
2307157117Smjacob	/*
2308157117Smjacob	 * Catch any pending interrupts
2309157117Smjacob	 *
2310157117Smjacob	 * This seems to be crucial- otherwise
2311157117Smjacob	 * the portenable below times out.
2312157117Smjacob	 */
2313157117Smjacob	mpt_intr(mpt);
2314157117Smjacob
2315157117Smjacob	/*
2316157662Smjacob	 * Enable the port.
2317157117Smjacob	 */
2318157117Smjacob	if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2319157117Smjacob		mpt_prt(mpt, "failed to enable port 0\n");
2320169293Smjacob		MPT_UNLOCK(mpt);
2321157117Smjacob		return (ENXIO);
2322157117Smjacob	}
2323169293Smjacob	MPT_UNLOCK(mpt);
2324157117Smjacob	return (0);
2325157117Smjacob}
2326157117Smjacob
2327147883Sscottlvoid
2328147883Sscottlmpt_core_shutdown(struct mpt_softc *mpt)
2329147883Sscottl{
2330157117Smjacob	mpt_disable_ints(mpt);
2331147883Sscottl}
2332147883Sscottl
2333147883Sscottlvoid
2334147883Sscottlmpt_core_detach(struct mpt_softc *mpt)
2335147883Sscottl{
2336164990Smjacob	/*
2337164990Smjacob	 * XXX: FREE MEMORY
2338164990Smjacob	 */
2339157117Smjacob	mpt_disable_ints(mpt);
2340147883Sscottl}
2341147883Sscottl
2342147883Sscottlint
2343147883Sscottlmpt_core_unload(struct mpt_personality *pers)
2344147883Sscottl{
2345147883Sscottl	/* Unload is always successfull. */
2346147883Sscottl	return (0);
2347147883Sscottl}
2348147883Sscottl
2349147883Sscottl#define FW_UPLOAD_REQ_SIZE				\
2350147883Sscottl	(sizeof(MSG_FW_UPLOAD) - sizeof(SGE_MPI_UNION)	\
2351147883Sscottl       + sizeof(FW_UPLOAD_TCSGE) + sizeof(SGE_SIMPLE32))
2352147883Sscottl
2353147883Sscottlstatic int
2354147883Sscottlmpt_upload_fw(struct mpt_softc *mpt)
2355147883Sscottl{
2356147883Sscottl	uint8_t fw_req_buf[FW_UPLOAD_REQ_SIZE];
2357147883Sscottl	MSG_FW_UPLOAD_REPLY fw_reply;
2358147883Sscottl	MSG_FW_UPLOAD *fw_req;
2359147883Sscottl	FW_UPLOAD_TCSGE *tsge;
2360147883Sscottl	SGE_SIMPLE32 *sge;
2361147883Sscottl	uint32_t flags;
2362147883Sscottl	int error;
2363147883Sscottl
2364147883Sscottl	memset(&fw_req_buf, 0, sizeof(fw_req_buf));
2365147883Sscottl	fw_req = (MSG_FW_UPLOAD *)fw_req_buf;
2366147883Sscottl	fw_req->ImageType = MPI_FW_UPLOAD_ITYPE_FW_IOC_MEM;
2367147883Sscottl	fw_req->Function = MPI_FUNCTION_FW_UPLOAD;
2368147883Sscottl	fw_req->MsgContext = htole32(MPT_REPLY_HANDLER_HANDSHAKE);
2369147883Sscottl	tsge = (FW_UPLOAD_TCSGE *)&fw_req->SGL;
2370147883Sscottl	tsge->DetailsLength = 12;
2371147883Sscottl	tsge->Flags = MPI_SGE_FLAGS_TRANSACTION_ELEMENT;
2372147883Sscottl	tsge->ImageSize = htole32(mpt->fw_image_size);
2373147883Sscottl	sge = (SGE_SIMPLE32 *)(tsge + 1);
2374147883Sscottl	flags = (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER
2375147883Sscottl	      | MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_SIMPLE_ELEMENT
2376147883Sscottl	      | MPI_SGE_FLAGS_32_BIT_ADDRESSING | MPI_SGE_FLAGS_IOC_TO_HOST);
2377147883Sscottl	flags <<= MPI_SGE_FLAGS_SHIFT;
2378147883Sscottl	sge->FlagsLength = htole32(flags | mpt->fw_image_size);
2379147883Sscottl	sge->Address = htole32(mpt->fw_phys);
2380147883Sscottl	error = mpt_send_handshake_cmd(mpt, sizeof(fw_req_buf), &fw_req_buf);
2381147883Sscottl	if (error)
2382147883Sscottl		return(error);
2383147883Sscottl	error = mpt_recv_handshake_reply(mpt, sizeof(fw_reply), &fw_reply);
2384147883Sscottl	return (error);
2385147883Sscottl}
2386147883Sscottl
2387147883Sscottlstatic void
2388147883Sscottlmpt_diag_outsl(struct mpt_softc *mpt, uint32_t addr,
2389147883Sscottl	       uint32_t *data, bus_size_t len)
2390147883Sscottl{
2391147883Sscottl	uint32_t *data_end;
2392147883Sscottl
2393147883Sscottl	data_end = data + (roundup2(len, sizeof(uint32_t)) / 4);
2394159919Smjacob	if (mpt->is_sas) {
2395159919Smjacob		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
2396159919Smjacob	}
2397147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, addr);
2398147883Sscottl	while (data != data_end) {
2399147883Sscottl		mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, *data);
2400147883Sscottl		data++;
2401101704Smjacob	}
2402159919Smjacob	if (mpt->is_sas) {
2403159919Smjacob		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2404159919Smjacob	}
2405147883Sscottl}
2406101704Smjacob
2407147883Sscottlstatic int
2408147883Sscottlmpt_download_fw(struct mpt_softc *mpt)
2409147883Sscottl{
2410147883Sscottl	MpiFwHeader_t *fw_hdr;
2411147883Sscottl	int error;
2412147883Sscottl	uint32_t ext_offset;
2413147883Sscottl	uint32_t data;
2414147883Sscottl
2415147883Sscottl	mpt_prt(mpt, "Downloading Firmware - Image Size %d\n",
2416147883Sscottl		mpt->fw_image_size);
2417147883Sscottl
2418147883Sscottl	error = mpt_enable_diag_mode(mpt);
2419147883Sscottl	if (error != 0) {
2420147883Sscottl		mpt_prt(mpt, "Could not enter diagnostic mode!\n");
2421147883Sscottl		return (EIO);
2422101704Smjacob	}
2423101704Smjacob
2424147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC,
2425147883Sscottl		  MPI_DIAG_RW_ENABLE|MPI_DIAG_DISABLE_ARM);
2426147883Sscottl
2427147883Sscottl	fw_hdr = (MpiFwHeader_t *)mpt->fw_image;
2428147883Sscottl	mpt_diag_outsl(mpt, fw_hdr->LoadStartAddress, (uint32_t*)fw_hdr,
2429147883Sscottl		       fw_hdr->ImageSize);
2430147883Sscottl
2431147883Sscottl	ext_offset = fw_hdr->NextImageHeaderOffset;
2432147883Sscottl	while (ext_offset != 0) {
2433147883Sscottl		MpiExtImageHeader_t *ext;
2434147883Sscottl
2435147883Sscottl		ext = (MpiExtImageHeader_t *)((uintptr_t)fw_hdr + ext_offset);
2436147883Sscottl		ext_offset = ext->NextImageHeaderOffset;
2437147883Sscottl
2438147883Sscottl		mpt_diag_outsl(mpt, ext->LoadStartAddress, (uint32_t*)ext,
2439147883Sscottl			       ext->ImageSize);
2440147883Sscottl	}
2441147883Sscottl
2442159919Smjacob	if (mpt->is_sas) {
2443159919Smjacob		pci_enable_io(mpt->dev, SYS_RES_IOPORT);
2444159919Smjacob	}
2445147883Sscottl	/* Setup the address to jump to on reset. */
2446147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, fw_hdr->IopResetRegAddr);
2447147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, fw_hdr->IopResetVectorValue);
2448147883Sscottl
2449101704Smjacob	/*
2450147883Sscottl	 * The controller sets the "flash bad" status after attempting
2451147883Sscottl	 * to auto-boot from flash.  Clear the status so that the controller
2452147883Sscottl	 * will continue the boot process with our newly installed firmware.
2453101704Smjacob	 */
2454147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2455147883Sscottl	data = mpt_pio_read(mpt, MPT_OFFSET_DIAG_DATA) | MPT_DIAG_MEM_CFG_BADFL;
2456147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_ADDR, MPT_DIAG_MEM_CFG_BASE);
2457147883Sscottl	mpt_pio_write(mpt, MPT_OFFSET_DIAG_DATA, data);
2458147883Sscottl
2459159919Smjacob	if (mpt->is_sas) {
2460159919Smjacob		pci_disable_io(mpt->dev, SYS_RES_IOPORT);
2461159919Smjacob	}
2462155521Smjacob
2463147883Sscottl	/*
2464147883Sscottl	 * Re-enable the processor and clear the boot halt flag.
2465147883Sscottl	 */
2466147883Sscottl	data = mpt_read(mpt, MPT_OFFSET_DIAGNOSTIC);
2467147883Sscottl	data &= ~(MPI_DIAG_PREVENT_IOC_BOOT|MPI_DIAG_DISABLE_ARM);
2468147883Sscottl	mpt_write(mpt, MPT_OFFSET_DIAGNOSTIC, data);
2469147883Sscottl
2470147883Sscottl	mpt_disable_diag_mode(mpt);
2471147883Sscottl	return (0);
2472147883Sscottl}
2473147883Sscottl
2474147883Sscottl/*
2475147883Sscottl * Allocate/Initialize data structures for the controller.  Called
2476147883Sscottl * once at instance startup.
2477147883Sscottl */
2478147883Sscottlstatic int
2479164990Smjacobmpt_configure_ioc(struct mpt_softc *mpt, int tn, int needreset)
2480147883Sscottl{
2481164990Smjacob	PTR_MSG_PORT_FACTS_REPLY pfp;
2482164990Smjacob	int error,  port;
2483164990Smjacob	size_t len;
2484147883Sscottl
2485164990Smjacob	if (tn == MPT_MAX_TRYS) {
2486164990Smjacob		return (-1);
2487164990Smjacob	}
2488147883Sscottl
2489164990Smjacob	/*
2490164990Smjacob	 * No need to reset if the IOC is already in the READY state.
2491164990Smjacob	 *
2492164990Smjacob	 * Force reset if initialization failed previously.
2493164990Smjacob	 * Note that a hard_reset of the second channel of a '929
2494164990Smjacob	 * will stop operation of the first channel.  Hopefully, if the
2495164990Smjacob	 * first channel is ok, the second will not require a hard
2496164990Smjacob	 * reset.
2497164990Smjacob	 */
2498164990Smjacob	if (needreset || MPT_STATE(mpt_rd_db(mpt)) != MPT_DB_STATE_READY) {
2499164990Smjacob		if (mpt_reset(mpt, FALSE) != MPT_OK) {
2500164990Smjacob			return (mpt_configure_ioc(mpt, tn++, 1));
2501101704Smjacob		}
2502147883Sscottl		needreset = 0;
2503164990Smjacob	}
2504101704Smjacob
2505164990Smjacob	if (mpt_get_iocfacts(mpt, &mpt->ioc_facts) != MPT_OK) {
2506164990Smjacob		mpt_prt(mpt, "mpt_get_iocfacts failed\n");
2507164990Smjacob		return (mpt_configure_ioc(mpt, tn++, 1));
2508164990Smjacob	}
2509164990Smjacob	mpt2host_iocfacts_reply(&mpt->ioc_facts);
2510102199Smjacob
2511164990Smjacob	mpt_prt(mpt, "MPI Version=%d.%d.%d.%d\n",
2512164990Smjacob	    mpt->ioc_facts.MsgVersion >> 8,
2513164990Smjacob	    mpt->ioc_facts.MsgVersion & 0xFF,
2514164990Smjacob	    mpt->ioc_facts.HeaderVersion >> 8,
2515164990Smjacob	    mpt->ioc_facts.HeaderVersion & 0xFF);
2516155521Smjacob
2517164990Smjacob	/*
2518164990Smjacob	 * Now that we know request frame size, we can calculate
2519164990Smjacob	 * the actual (reasonable) segment limit for read/write I/O.
2520164990Smjacob	 *
2521164990Smjacob	 * This limit is constrained by:
2522164990Smjacob	 *
2523164990Smjacob	 *  + The size of each area we allocate per command (and how
2524164990Smjacob	 *    many chain segments we can fit into it).
2525164990Smjacob	 *  + The total number of areas we've set up.
2526164990Smjacob	 *  + The actual chain depth the card will allow.
2527164990Smjacob	 *
2528164990Smjacob	 * The first area's segment count is limited by the I/O request
2529164990Smjacob	 * at the head of it. We cannot allocate realistically more
2530164990Smjacob	 * than MPT_MAX_REQUESTS areas. Therefore, to account for both
2531164990Smjacob	 * conditions, we'll just start out with MPT_MAX_REQUESTS-2.
2532164990Smjacob	 *
2533164990Smjacob	 */
2534164990Smjacob	/* total number of request areas we (can) allocate */
2535164990Smjacob	mpt->max_seg_cnt = MPT_MAX_REQUESTS(mpt) - 2;
2536155521Smjacob
2537164990Smjacob	/* converted to the number of chain areas possible */
2538164990Smjacob	mpt->max_seg_cnt *= MPT_NRFM(mpt);
2539155521Smjacob
2540164990Smjacob	/* limited by the number of chain areas the card will support */
2541164990Smjacob	if (mpt->max_seg_cnt > mpt->ioc_facts.MaxChainDepth) {
2542164990Smjacob		mpt_lprt(mpt, MPT_PRT_DEBUG,
2543164990Smjacob		    "chain depth limited to %u (from %u)\n",
2544164990Smjacob		    mpt->ioc_facts.MaxChainDepth, mpt->max_seg_cnt);
2545164990Smjacob		mpt->max_seg_cnt = mpt->ioc_facts.MaxChainDepth;
2546164990Smjacob	}
2547155521Smjacob
2548164990Smjacob	/* converted to the number of simple sges in chain segments. */
2549164990Smjacob	mpt->max_seg_cnt *= (MPT_NSGL(mpt) - 1);
2550155521Smjacob
2551164990Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "Maximum Segment Count: %u\n",
2552164990Smjacob	    mpt->max_seg_cnt);
2553164990Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "MsgLength=%u IOCNumber = %d\n",
2554164990Smjacob	    mpt->ioc_facts.MsgLength, mpt->ioc_facts.IOCNumber);
2555164990Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG,
2556164990Smjacob	    "IOCFACTS: GlobalCredits=%d BlockSize=%u bytes "
2557164990Smjacob	    "Request Frame Size %u bytes Max Chain Depth %u\n",
2558164990Smjacob	    mpt->ioc_facts.GlobalCredits, mpt->ioc_facts.BlockSize,
2559164990Smjacob	    mpt->ioc_facts.RequestFrameSize << 2,
2560164990Smjacob	    mpt->ioc_facts.MaxChainDepth);
2561164990Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOCFACTS: Num Ports %d, FWImageSize %d, "
2562164990Smjacob	    "Flags=%#x\n", mpt->ioc_facts.NumberOfPorts,
2563164990Smjacob	    mpt->ioc_facts.FWImageSize, mpt->ioc_facts.Flags);
2564155521Smjacob
2565164990Smjacob	len = mpt->ioc_facts.NumberOfPorts * sizeof (MSG_PORT_FACTS_REPLY);
2566164990Smjacob	mpt->port_facts = malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
2567164990Smjacob	if (mpt->port_facts == NULL) {
2568164990Smjacob		mpt_prt(mpt, "unable to allocate memory for port facts\n");
2569164990Smjacob		return (ENOMEM);
2570164990Smjacob	}
2571147883Sscottl
2572155521Smjacob
2573164990Smjacob	if ((mpt->ioc_facts.Flags & MPI_IOCFACTS_FLAGS_FW_DOWNLOAD_BOOT) &&
2574164990Smjacob	    (mpt->fw_uploaded == 0)) {
2575164990Smjacob		struct mpt_map_info mi;
2576147883Sscottl
2577164990Smjacob		/*
2578164990Smjacob		 * In some configurations, the IOC's firmware is
2579164990Smjacob		 * stored in a shared piece of system NVRAM that
2580164990Smjacob		 * is only accessable via the BIOS.  In this
2581164990Smjacob		 * case, the firmware keeps a copy of firmware in
2582164990Smjacob		 * RAM until the OS driver retrieves it.  Once
2583164990Smjacob		 * retrieved, we are responsible for re-downloading
2584164990Smjacob		 * the firmware after any hard-reset.
2585164990Smjacob		 */
2586164990Smjacob		mpt->fw_image_size = mpt->ioc_facts.FWImageSize;
2587164990Smjacob		error = mpt_dma_tag_create(mpt, mpt->parent_dmat, 1, 0,
2588164990Smjacob		    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
2589164990Smjacob		    mpt->fw_image_size, 1, mpt->fw_image_size, 0,
2590164990Smjacob		    &mpt->fw_dmat);
2591164990Smjacob		if (error != 0) {
2592164990Smjacob			mpt_prt(mpt, "cannot create firmwarew dma tag\n");
2593164990Smjacob			return (ENOMEM);
2594101704Smjacob		}
2595164990Smjacob		error = bus_dmamem_alloc(mpt->fw_dmat,
2596164990Smjacob		    (void **)&mpt->fw_image, BUS_DMA_NOWAIT, &mpt->fw_dmap);
2597164990Smjacob		if (error != 0) {
2598164990Smjacob			mpt_prt(mpt, "cannot allocate firmware memory\n");
2599164990Smjacob			bus_dma_tag_destroy(mpt->fw_dmat);
2600164990Smjacob			return (ENOMEM);
2601164990Smjacob		}
2602164990Smjacob		mi.mpt = mpt;
2603164990Smjacob		mi.error = 0;
2604164990Smjacob		bus_dmamap_load(mpt->fw_dmat, mpt->fw_dmap,
2605164990Smjacob		    mpt->fw_image, mpt->fw_image_size, mpt_map_rquest, &mi, 0);
2606164990Smjacob		mpt->fw_phys = mi.phys;
2607101704Smjacob
2608164990Smjacob		error = mpt_upload_fw(mpt);
2609164990Smjacob		if (error != 0) {
2610164990Smjacob			mpt_prt(mpt, "firmware upload failed.\n");
2611164990Smjacob			bus_dmamap_unload(mpt->fw_dmat, mpt->fw_dmap);
2612164990Smjacob			bus_dmamem_free(mpt->fw_dmat, mpt->fw_image,
2613164990Smjacob			    mpt->fw_dmap);
2614164990Smjacob			bus_dma_tag_destroy(mpt->fw_dmat);
2615164990Smjacob			mpt->fw_image = NULL;
2616164990Smjacob			return (EIO);
2617102199Smjacob		}
2618164990Smjacob		mpt->fw_uploaded = 1;
2619164990Smjacob	}
2620102199Smjacob
2621164990Smjacob	for (port = 0; port < mpt->ioc_facts.NumberOfPorts; port++) {
2622164990Smjacob		pfp = &mpt->port_facts[port];
2623164990Smjacob		error = mpt_get_portfacts(mpt, 0, pfp);
2624164990Smjacob		if (error != MPT_OK) {
2625164990Smjacob			mpt_prt(mpt,
2626164990Smjacob			    "mpt_get_portfacts on port %d failed\n", port);
2627164990Smjacob			free(mpt->port_facts, M_DEVBUF);
2628164990Smjacob			mpt->port_facts = NULL;
2629164990Smjacob			return (mpt_configure_ioc(mpt, tn++, 1));
2630102199Smjacob		}
2631164990Smjacob		mpt2host_portfacts_reply(pfp);
2632157117Smjacob
2633164990Smjacob		if (port > 0) {
2634164990Smjacob			error = MPT_PRT_INFO;
2635102199Smjacob		} else {
2636164990Smjacob			error = MPT_PRT_DEBUG;
2637102199Smjacob		}
2638164990Smjacob		mpt_lprt(mpt, error,
2639164990Smjacob		    "PORTFACTS[%d]: Type %x PFlags %x IID %d MaxDev %d\n",
2640164990Smjacob		    port, pfp->PortType, pfp->ProtocolFlags, pfp->PortSCSIID,
2641164990Smjacob		    pfp->MaxDevices);
2642102199Smjacob
2643164990Smjacob	}
2644157117Smjacob
2645164990Smjacob	/*
2646164990Smjacob	 * XXX: Not yet supporting more than port 0
2647164990Smjacob	 */
2648164990Smjacob	pfp = &mpt->port_facts[0];
2649164990Smjacob	if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_FC) {
2650164990Smjacob		mpt->is_fc = 1;
2651164990Smjacob		mpt->is_sas = 0;
2652164990Smjacob		mpt->is_spi = 0;
2653164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SAS) {
2654164990Smjacob		mpt->is_fc = 0;
2655164990Smjacob		mpt->is_sas = 1;
2656164990Smjacob		mpt->is_spi = 0;
2657164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_SCSI) {
2658164990Smjacob		mpt->is_fc = 0;
2659164990Smjacob		mpt->is_sas = 0;
2660164990Smjacob		mpt->is_spi = 1;
2661164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_ISCSI) {
2662164990Smjacob		mpt_prt(mpt, "iSCSI not supported yet\n");
2663164990Smjacob		return (ENXIO);
2664164990Smjacob	} else if (pfp->PortType == MPI_PORTFACTS_PORTTYPE_INACTIVE) {
2665164990Smjacob		mpt_prt(mpt, "Inactive Port\n");
2666164990Smjacob		return (ENXIO);
2667164990Smjacob	} else {
2668164990Smjacob		mpt_prt(mpt, "unknown Port Type %#x\n", pfp->PortType);
2669164990Smjacob		return (ENXIO);
2670164990Smjacob	}
2671101704Smjacob
2672164990Smjacob	/*
2673164990Smjacob	 * Set our role with what this port supports.
2674164990Smjacob	 *
2675164990Smjacob	 * Note this might be changed later in different modules
2676164990Smjacob	 * if this is different from what is wanted.
2677164990Smjacob	 */
2678164990Smjacob	mpt->role = MPT_ROLE_NONE;
2679164990Smjacob	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_INITIATOR) {
2680164990Smjacob		mpt->role |= MPT_ROLE_INITIATOR;
2681101704Smjacob	}
2682164990Smjacob	if (pfp->ProtocolFlags & MPI_PORTFACTS_PROTOCOL_TARGET) {
2683164990Smjacob		mpt->role |= MPT_ROLE_TARGET;
2684164990Smjacob	}
2685101704Smjacob
2686164990Smjacob	/*
2687164990Smjacob	 * Enable the IOC
2688164990Smjacob	 */
2689177808Sscottl	if (mpt_enable_ioc(mpt, 1) != MPT_OK) {
2690164990Smjacob		mpt_prt(mpt, "unable to initialize IOC\n");
2691164990Smjacob		return (ENXIO);
2692101704Smjacob	}
2693101704Smjacob
2694164990Smjacob	/*
2695164990Smjacob	 * Read IOC configuration information.
2696164990Smjacob	 *
2697164990Smjacob	 * We need this to determine whether or not we have certain
2698164990Smjacob	 * settings for Integrated Mirroring (e.g.).
2699164990Smjacob	 */
2700164990Smjacob	mpt_read_config_info_ioc(mpt);
2701164990Smjacob
2702101704Smjacob	return (0);
2703101704Smjacob}
2704147883Sscottl
2705147883Sscottlstatic int
2706157117Smjacobmpt_enable_ioc(struct mpt_softc *mpt, int portenable)
2707147883Sscottl{
2708147883Sscottl	uint32_t pptr;
2709147883Sscottl	int val;
2710147883Sscottl
2711155521Smjacob	if (mpt_send_ioc_init(mpt, MPI_WHOINIT_HOST_DRIVER) != MPT_OK) {
2712147883Sscottl		mpt_prt(mpt, "mpt_send_ioc_init failed\n");
2713147883Sscottl		return (EIO);
2714147883Sscottl	}
2715147883Sscottl
2716147883Sscottl	mpt_lprt(mpt, MPT_PRT_DEBUG, "mpt_send_ioc_init ok\n");
2717147883Sscottl
2718147883Sscottl	if (mpt_wait_state(mpt, MPT_DB_STATE_RUNNING) != MPT_OK) {
2719147883Sscottl		mpt_prt(mpt, "IOC failed to go to run state\n");
2720147883Sscottl		return (ENXIO);
2721147883Sscottl	}
2722155521Smjacob	mpt_lprt(mpt, MPT_PRT_DEBUG, "IOC now at RUNSTATE\n");
2723147883Sscottl
2724147883Sscottl	/*
2725147883Sscottl	 * Give it reply buffers
2726147883Sscottl	 *
2727147883Sscottl	 * Do *not* exceed global credits.
2728147883Sscottl	 */
2729156104Smjacob	for (val = 0, pptr = mpt->reply_phys;
2730156104Smjacob	    (pptr + MPT_REPLY_SIZE) < (mpt->reply_phys + PAGE_SIZE);
2731147883Sscottl	     pptr += MPT_REPLY_SIZE) {
2732147883Sscottl		mpt_free_reply(mpt, pptr);
2733164990Smjacob		if (++val == mpt->ioc_facts.GlobalCredits - 1)
2734147883Sscottl			break;
2735147883Sscottl	}
2736147883Sscottl
2737157117Smjacob
2738147883Sscottl	/*
2739157354Smjacob	 * Enable the port if asked. This is only done if we're resetting
2740157354Smjacob	 * the IOC after initial startup.
2741147883Sscottl	 */
2742157117Smjacob	if (portenable) {
2743157117Smjacob		/*
2744157117Smjacob		 * Enable asynchronous event reporting
2745157117Smjacob		 */
2746157117Smjacob		mpt_send_event_request(mpt, 1);
2747147883Sscottl
2748157117Smjacob		if (mpt_send_port_enable(mpt, 0) != MPT_OK) {
2749157117Smjacob			mpt_prt(mpt, "failed to enable port 0\n");
2750157117Smjacob			return (ENXIO);
2751157117Smjacob		}
2752147883Sscottl	}
2753156104Smjacob	return (MPT_OK);
2754147883Sscottl}
2755164990Smjacob
2756164990Smjacob/*
2757164990Smjacob * Endian Conversion Functions- only used on Big Endian machines
2758164990Smjacob */
2759164990Smjacob#if	_BYTE_ORDER == _BIG_ENDIAN
2760164990Smjacobvoid
2761164990Smjacobmpt2host_sge_simple_union(SGE_SIMPLE_UNION *sge)
2762164990Smjacob{
2763164990Smjacob	MPT_2_HOST32(sge, FlagsLength);
2764164998Smjacob	MPT_2_HOST32(sge, u.Address64.Low);
2765164998Smjacob	MPT_2_HOST32(sge, u.Address64.High);
2766164998Smjacob}
2767164990Smjacob
2768164990Smjacobvoid
2769164990Smjacobmpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *rp)
2770164990Smjacob{
2771164990Smjacob	MPT_2_HOST16(rp, MsgVersion);
2772164990Smjacob	MPT_2_HOST16(rp, HeaderVersion);
2773164990Smjacob	MPT_2_HOST32(rp, MsgContext);
2774164990Smjacob	MPT_2_HOST16(rp, IOCExceptions);
2775164990Smjacob	MPT_2_HOST16(rp, IOCStatus);
2776164990Smjacob	MPT_2_HOST32(rp, IOCLogInfo);
2777164990Smjacob	MPT_2_HOST16(rp, ReplyQueueDepth);
2778164990Smjacob	MPT_2_HOST16(rp, RequestFrameSize);
2779164990Smjacob	MPT_2_HOST16(rp, Reserved_0101_FWVersion);
2780164990Smjacob	MPT_2_HOST16(rp, ProductID);
2781164990Smjacob	MPT_2_HOST32(rp, CurrentHostMfaHighAddr);
2782164990Smjacob	MPT_2_HOST16(rp, GlobalCredits);
2783164990Smjacob	MPT_2_HOST32(rp, CurrentSenseBufferHighAddr);
2784164990Smjacob	MPT_2_HOST16(rp, CurReplyFrameSize);
2785164990Smjacob	MPT_2_HOST32(rp, FWImageSize);
2786164990Smjacob	MPT_2_HOST32(rp, IOCCapabilities);
2787164990Smjacob	MPT_2_HOST32(rp, FWVersion.Word);
2788164990Smjacob	MPT_2_HOST16(rp, HighPriorityQueueDepth);
2789164990Smjacob	MPT_2_HOST16(rp, Reserved2);
2790164990Smjacob	mpt2host_sge_simple_union(&rp->HostPageBufferSGE);
2791164990Smjacob	MPT_2_HOST32(rp, ReplyFifoHostSignalingAddr);
2792164990Smjacob}
2793164990Smjacob
2794164990Smjacobvoid
2795164990Smjacobmpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *pfp)
2796164990Smjacob{
2797164990Smjacob	MPT_2_HOST16(pfp, Reserved);
2798164990Smjacob	MPT_2_HOST16(pfp, Reserved1);
2799164990Smjacob	MPT_2_HOST32(pfp, MsgContext);
2800164990Smjacob	MPT_2_HOST16(pfp, Reserved2);
2801164990Smjacob	MPT_2_HOST16(pfp, IOCStatus);
2802164990Smjacob	MPT_2_HOST32(pfp, IOCLogInfo);
2803164990Smjacob	MPT_2_HOST16(pfp, MaxDevices);
2804164990Smjacob	MPT_2_HOST16(pfp, PortSCSIID);
2805164990Smjacob	MPT_2_HOST16(pfp, ProtocolFlags);
2806164990Smjacob	MPT_2_HOST16(pfp, MaxPostedCmdBuffers);
2807164990Smjacob	MPT_2_HOST16(pfp, MaxPersistentIDs);
2808164990Smjacob	MPT_2_HOST16(pfp, MaxLanBuckets);
2809164990Smjacob	MPT_2_HOST16(pfp, Reserved4);
2810164990Smjacob	MPT_2_HOST32(pfp, Reserved5);
2811164990Smjacob}
2812164990Smjacobvoid
2813164990Smjacobmpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *ioc2)
2814164990Smjacob{
2815164990Smjacob	int i;
2816164990Smjacob	ioc2->CapabilitiesFlags = htole32(ioc2->CapabilitiesFlags);
2817164990Smjacob	for (i = 0; i < MPI_IOC_PAGE_2_RAID_VOLUME_MAX; i++) {
2818164998Smjacob		MPT_2_HOST16(ioc2, RaidVolume[i].Reserved3);
2819164990Smjacob	}
2820164990Smjacob}
2821164990Smjacob
2822164990Smjacobvoid
2823164990Smjacobmpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *volp)
2824164990Smjacob{
2825164990Smjacob	int i;
2826164990Smjacob	MPT_2_HOST16(volp, VolumeStatus.Reserved);
2827164990Smjacob	MPT_2_HOST16(volp, VolumeSettings.Settings);
2828164990Smjacob	MPT_2_HOST32(volp, MaxLBA);
2829170276Sscottl	MPT_2_HOST32(volp, MaxLBAHigh);
2830164990Smjacob	MPT_2_HOST32(volp, StripeSize);
2831164990Smjacob	MPT_2_HOST32(volp, Reserved2);
2832164990Smjacob	MPT_2_HOST32(volp, Reserved3);
2833164990Smjacob	for (i = 0; i < MPI_RAID_VOL_PAGE_0_PHYSDISK_MAX; i++) {
2834164998Smjacob		MPT_2_HOST16(volp, PhysDisk[i].Reserved);
2835164990Smjacob	}
2836164990Smjacob}
2837164990Smjacob
2838164990Smjacobvoid
2839164990Smjacobmpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *vi)
2840164990Smjacob{
2841164998Smjacob	MPT_2_HOST16(vi, TotalBlocks.High);
2842164998Smjacob	MPT_2_HOST16(vi, TotalBlocks.Low);
2843164998Smjacob	MPT_2_HOST16(vi, BlocksRemaining.High);
2844164998Smjacob	MPT_2_HOST16(vi, BlocksRemaining.Low);
2845164990Smjacob}
2846164990Smjacob#endif
2847