xref: /freebsd/sys/dev/mpt/mpt.h (revision 7bd6fde3)
1 /* $FreeBSD$ */
2 /*-
3  * Generic defines for LSI '909 FC  adapters.
4  * FreeBSD Version.
5  *
6  * Copyright (c)  2000, 2001 by Greg Ansley
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*-
30  * Copyright (c) 2002, 2006 by Matthew Jacob
31  * All rights reserved.
32  *
33  * Redistribution and use in source and binary forms, with or without
34  * modification, are permitted provided that the following conditions are
35  * met:
36  * 1. Redistributions of source code must retain the above copyright
37  *    notice, this list of conditions and the following disclaimer.
38  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
39  *    substantially similar to the "NO WARRANTY" disclaimer below
40  *    ("Disclaimer") and any redistribution must be conditioned upon including
41  *    a substantially similar Disclaimer requirement for further binary
42  *    redistribution.
43  * 3. Neither the names of the above listed copyright holders nor the names
44  *    of any contributors may be used to endorse or promote products derived
45  *    from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
57  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  *
59  * Support from Chris Ellsworth in order to make SAS adapters work
60  * is gratefully acknowledged.
61  *
62  *
63  * Support from LSI-Logic has also gone a great deal toward making this a
64  * workable subsystem and is gratefully acknowledged.
65  */
66 /*
67  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
68  * Copyright (c) 2004, 2005 Justin T. Gibbs
69  * Copyright (c) 2005, WHEEL Sp. z o.o.
70  * All rights reserved.
71  *
72  * Redistribution and use in source and binary forms, with or without
73  * modification, are permitted provided that the following conditions are
74  * met:
75  * 1. Redistributions of source code must retain the above copyright
76  *    notice, this list of conditions and the following disclaimer.
77  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
78  *    substantially similar to the "NO WARRANTY" disclaimer below
79  *    ("Disclaimer") and any redistribution must be conditioned upon including
80  *    a substantially similar Disclaimer requirement for further binary
81  *    redistribution.
82  * 3. Neither the names of the above listed copyright holders nor the names
83  *    of any contributors may be used to endorse or promote products derived
84  *    from this software without specific prior written permission.
85  *
86  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
90  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
96  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97  */
98 
99 #ifndef _MPT_H_
100 #define _MPT_H_
101 
102 /********************************* OS Includes ********************************/
103 #include <sys/types.h>
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/endian.h>
107 #include <sys/eventhandler.h>
108 #if __FreeBSD_version < 500000
109 #include <sys/kernel.h>
110 #include <sys/queue.h>
111 #include <sys/malloc.h>
112 #else
113 #include <sys/lock.h>
114 #include <sys/kernel.h>
115 #include <sys/queue.h>
116 #include <sys/malloc.h>
117 #include <sys/mutex.h>
118 #include <sys/condvar.h>
119 #endif
120 #include <sys/proc.h>
121 #include <sys/bus.h>
122 #include <sys/module.h>
123 
124 #include <machine/cpu.h>
125 #include <machine/resource.h>
126 
127 #if __FreeBSD_version < 500000
128 #include <machine/bus.h>
129 #include <machine/clock.h>
130 #endif
131 
132 #include <sys/rman.h>
133 
134 #if __FreeBSD_version < 500000
135 #include <pci/pcireg.h>
136 #include <pci/pcivar.h>
137 #else
138 #include <dev/pci/pcireg.h>
139 #include <dev/pci/pcivar.h>
140 #endif
141 
142 #include <machine/bus.h>
143 #include "opt_ddb.h"
144 
145 /**************************** Register Definitions ****************************/
146 #include <dev/mpt/mpt_reg.h>
147 
148 /******************************* MPI Definitions ******************************/
149 #include <dev/mpt/mpilib/mpi_type.h>
150 #include <dev/mpt/mpilib/mpi.h>
151 #include <dev/mpt/mpilib/mpi_cnfg.h>
152 #include <dev/mpt/mpilib/mpi_ioc.h>
153 #include <dev/mpt/mpilib/mpi_raid.h>
154 
155 /* XXX For mpt_debug.c */
156 #include <dev/mpt/mpilib/mpi_init.h>
157 
158 #define	MPT_S64_2_SCALAR(y)	((((int64_t)y.High) << 32) | (y.Low))
159 #define	MPT_U64_2_SCALAR(y)	((((uint64_t)y.High) << 32) | (y.Low))
160 
161 /****************************** Misc Definitions ******************************/
162 /* #define MPT_TEST_MULTIPATH	1 */
163 #define MPT_OK (0)
164 #define MPT_FAIL (0x10000)
165 
166 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
167 
168 #define	MPT_ROLE_NONE		0
169 #define	MPT_ROLE_INITIATOR	1
170 #define	MPT_ROLE_TARGET		2
171 #define	MPT_ROLE_BOTH		3
172 #define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
173 
174 /**************************** Forward Declarations ****************************/
175 struct mpt_softc;
176 struct mpt_personality;
177 typedef struct req_entry request_t;
178 
179 /************************* Personality Module Support *************************/
180 typedef int mpt_load_handler_t(struct mpt_personality *);
181 typedef int mpt_probe_handler_t(struct mpt_softc *);
182 typedef int mpt_attach_handler_t(struct mpt_softc *);
183 typedef int mpt_enable_handler_t(struct mpt_softc *);
184 typedef void mpt_ready_handler_t(struct mpt_softc *);
185 typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
186 				MSG_EVENT_NOTIFY_REPLY *);
187 typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
188 /* XXX Add return value and use for veto? */
189 typedef void mpt_shutdown_handler_t(struct mpt_softc *);
190 typedef void mpt_detach_handler_t(struct mpt_softc *);
191 typedef int mpt_unload_handler_t(struct mpt_personality *);
192 
193 struct mpt_personality
194 {
195 	const char		*name;
196 	uint32_t		 id;		/* Assigned identifier. */
197 	u_int			 use_count;	/* Instances using personality*/
198 	mpt_load_handler_t	*load;		/* configure personailty */
199 #define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
200 	mpt_probe_handler_t	*probe;		/* configure personailty */
201 	mpt_attach_handler_t	*attach;	/* initialize device instance */
202 	mpt_enable_handler_t	*enable;	/* enable device */
203 	mpt_ready_handler_t	*ready;		/* final open for business */
204 	mpt_event_handler_t	*event;		/* Handle MPI event. */
205 	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
206 	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
207 	mpt_detach_handler_t	*detach;	/* release device instance */
208 	mpt_unload_handler_t	*unload;	/* Shutdown personality */
209 #define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
210 };
211 
212 int mpt_modevent(module_t, int, void *);
213 
214 /* Maximum supported number of personalities. */
215 #define MPT_MAX_PERSONALITIES	(15)
216 
217 #define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
218 	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
219 
220 #define DECLARE_MPT_PERSONALITY(name, order)				  \
221 	static moduledata_t name##_mod = {				  \
222 		#name, mpt_modevent, &name##_personality		  \
223 	};								  \
224 	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
225 	MODULE_VERSION(name, 1);					  \
226 	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
227 
228 /******************************* Bus DMA Support ******************************/
229 /* XXX Need to update bus_dmamap_sync to take a range argument. */
230 #define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
231 	bus_dmamap_sync(dma_tag, dmamap, op)
232 
233 #if __FreeBSD_version >= 501102
234 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
235 			   lowaddr, highaddr, filter, filterarg,	\
236 			   maxsize, nsegments, maxsegsz, flags,		\
237 			   dma_tagp)					\
238 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
239 			   lowaddr, highaddr, filter, filterarg,	\
240 			   maxsize, nsegments, maxsegsz, flags,		\
241 			   busdma_lock_mutex, &Giant,			\
242 			   dma_tagp)
243 #else
244 #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
245 			   lowaddr, highaddr, filter, filterarg,	\
246 			   maxsize, nsegments, maxsegsz, flags,		\
247 			   dma_tagp)					\
248 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
249 			   lowaddr, highaddr, filter, filterarg,	\
250 			   maxsize, nsegments, maxsegsz, flags,		\
251 			   dma_tagp)
252 #endif
253 
254 struct mpt_map_info {
255 	struct mpt_softc *mpt;
256 	int		  error;
257 	uint32_t	  phys;
258 };
259 
260 void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
261 /* **************************** NewBUS interrupt Crock ************************/
262 #if __FreeBSD_version < 700031
263 #define	mpt_setup_intr(d, i, f, U, if, ifa, hp)	\
264 	bus_setup_intr(d, i, f, if, ifa, hp)
265 #else
266 #define	mpt_setup_intr	bus_setup_intr
267 #endif
268 
269 /**************************** Kernel Thread Support ***************************/
270 #if __FreeBSD_version > 500005
271 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
272 	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
273 #else
274 #define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
275 	kthread_create(func, farg, proc_ptr, fmtstr, arg)
276 #endif
277 
278 /****************************** Timer Facilities ******************************/
279 #if __FreeBSD_version > 500000
280 #define mpt_callout_init(c)	callout_init(c, /*mpsafe*/0);
281 #else
282 #define mpt_callout_init(c)	callout_init(c);
283 #endif
284 
285 /********************************** Endianess *********************************/
286 #define	MPT_2_HOST64(ptr, tag)	ptr->tag = le64toh(ptr->tag)
287 #define	MPT_2_HOST32(ptr, tag)	ptr->tag = le32toh(ptr->tag)
288 #define	MPT_2_HOST16(ptr, tag)	ptr->tag = le16toh(ptr->tag)
289 
290 #define	HOST_2_MPT64(ptr, tag)	ptr->tag = htole64(ptr->tag)
291 #define	HOST_2_MPT32(ptr, tag)	ptr->tag = htole32(ptr->tag)
292 #define	HOST_2_MPT16(ptr, tag)	ptr->tag = htole16(ptr->tag)
293 
294 #if	_BYTE_ORDER == _BIG_ENDIAN
295 void mpt2host_sge_simple_union(SGE_SIMPLE_UNION *);
296 void mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *);
297 void mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *);
298 void mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *);
299 void mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *);
300 void mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *);
301 #else
302 #define	mpt2host_sge_simple_union(x)		do { ; } while (0)
303 #define	mpt2host_iocfacts_reply(x)		do { ; } while (0)
304 #define	mpt2host_portfacts_reply(x)		do { ; } while (0)
305 #define	mpt2host_config_page_ioc2(x)		do { ; } while (0)
306 #define	mpt2host_config_page_raid_vol_0(x)	do { ; } while (0)
307 #define	mpt2host_mpi_raid_vol_indicator(x)	do { ; } while (0)
308 #endif
309 
310 /**************************** MPI Transaction State ***************************/
311 typedef enum {
312 	REQ_STATE_NIL		= 0x00,
313 	REQ_STATE_FREE		= 0x01,
314 	REQ_STATE_ALLOCATED	= 0x02,
315 	REQ_STATE_QUEUED	= 0x04,
316 	REQ_STATE_DONE		= 0x08,
317 	REQ_STATE_TIMEDOUT	= 0x10,
318 	REQ_STATE_NEED_WAKEUP	= 0x20,
319 	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
320 	REQ_STATE_MASK		= 0xFF
321 } mpt_req_state_t;
322 
323 struct req_entry {
324 	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
325 	mpt_req_state_t	state;		/* Request State Information */
326 	uint16_t	index;		/* Index of this entry */
327 	uint16_t	IOCStatus;	/* Completion status */
328 	uint16_t	ResponseCode;	/* TMF Reponse Code */
329 	uint16_t	serno;		/* serial number */
330 	union ccb      *ccb;		/* CAM request */
331 	void	       *req_vbuf;	/* Virtual Address of Entry */
332 	void	       *sense_vbuf;	/* Virtual Address of sense data */
333 	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
334 	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
335 	bus_dmamap_t	dmap;		/* DMA map for data buffers */
336 	struct req_entry *chain;	/* for SGE overallocations */
337 };
338 
339 /**************************** MPI Target State Info ***************************/
340 
341 typedef struct {
342 	uint32_t reply_desc;	/* current reply descriptor */
343 	uint32_t resid;		/* current data residual */
344 	uint32_t bytes_xfered;	/* current relative offset */
345 	union ccb *ccb;		/* pointer to currently active ccb */
346 	request_t *req;		/* pointer to currently active assist request */
347 	uint32_t
348 		is_local : 1,
349 		nxfers	 : 31;
350 	uint32_t tag_id;
351 	enum {
352 		TGT_STATE_NIL,
353 		TGT_STATE_LOADING,
354 		TGT_STATE_LOADED,
355 		TGT_STATE_IN_CAM,
356                 TGT_STATE_SETTING_UP_FOR_DATA,
357                 TGT_STATE_MOVING_DATA,
358                 TGT_STATE_MOVING_DATA_AND_STATUS,
359                 TGT_STATE_SENDING_STATUS
360 	} state;
361 } mpt_tgt_state_t;
362 
363 /*
364  * When we get an incoming command it has its own tag which is called the
365  * IoIndex. This is the value we gave that particular command buffer when
366  * we originally assigned it. It's just a number, really. The FC card uses
367  * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
368  * contains pointers the request_t structures related to that IoIndex.
369  *
370  * What *we* do is construct a tag out of the index for the target command
371  * which owns the incoming ATIO plus a rolling sequence number.
372  */
373 #define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
374  ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
375 
376 #ifdef	INVARIANTS
377 #define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
378 #else
379 #define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
380 #endif
381 
382 #define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
383     (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
384 
385 STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
386 #define	MPT_MAX_LUNS	256
387 typedef struct {
388 	struct mpt_hdr_stailq	atios;
389 	struct mpt_hdr_stailq	inots;
390 	int enabled;
391 } tgt_resource_t;
392 #define	MPT_MAX_ELS	64
393 
394 /**************************** Handler Registration ****************************/
395 /*
396  * Global table of registered reply handlers.  The
397  * handler is indicated by byte 3 of the request
398  * index submitted to the IOC.  This allows the
399  * driver core to perform generic processing without
400  * any knowledge of per-personality behavior.
401  *
402  * MPT_NUM_REPLY_HANDLERS must be a power of 2
403  * to allow the easy generation of a mask.
404  *
405  * The handler offsets used by the core are hard coded
406  * allowing faster code generation when assigning a handler
407  * to a request.  All "personalities" must use the
408  * the handler registration mechanism.
409  *
410  * The IOC handlers that are rarely executed are placed
411  * at the tail of the table to make it more likely that
412  * all commonly executed handlers fit in a single cache
413  * line.
414  */
415 #define MPT_NUM_REPLY_HANDLERS		(32)
416 #define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
417 #define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
418 #define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
419 typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
420     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
421 typedef union {
422 	mpt_reply_handler_t	*reply_handler;
423 } mpt_handler_t;
424 
425 typedef enum {
426 	MPT_HANDLER_REPLY,
427 	MPT_HANDLER_EVENT,
428 	MPT_HANDLER_RESET,
429 	MPT_HANDLER_SHUTDOWN
430 } mpt_handler_type;
431 
432 struct mpt_handler_record
433 {
434 	LIST_ENTRY(mpt_handler_record)	links;
435 	mpt_handler_t			handler;
436 };
437 
438 LIST_HEAD(mpt_handler_list, mpt_handler_record);
439 
440 /*
441  * The handler_id is currently unused but would contain the
442  * handler ID used in the MsgContext field to allow direction
443  * of replies to the handler.  Registrations that don't require
444  * a handler id can pass in NULL for the handler_id.
445  *
446  * Deregistrations for handlers without a handler id should
447  * pass in MPT_HANDLER_ID_NONE.
448  */
449 #define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
450 int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
451 			 mpt_handler_t, uint32_t *);
452 int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
453 			   mpt_handler_t, uint32_t);
454 
455 /******************* Per-Controller Instance Data Structures ******************/
456 TAILQ_HEAD(req_queue, req_entry);
457 
458 /* Structure for saving proper values for modifyable PCI config registers */
459 struct mpt_pci_cfg {
460 	uint16_t Command;
461 	uint16_t LatencyTimer_LineSize;
462 	uint32_t IO_BAR;
463 	uint32_t Mem0_BAR[2];
464 	uint32_t Mem1_BAR[2];
465 	uint32_t ROM_BAR;
466 	uint8_t  IntLine;
467 	uint32_t PMCSR;
468 };
469 
470 typedef enum {
471 	MPT_RVF_NONE		= 0x0,
472 	MPT_RVF_ACTIVE		= 0x1,
473 	MPT_RVF_ANNOUNCED	= 0x2,
474 	MPT_RVF_UP2DATE		= 0x4,
475 	MPT_RVF_REFERENCED	= 0x8,
476 	MPT_RVF_WCE_CHANGED	= 0x10
477 } mpt_raid_volume_flags;
478 
479 struct mpt_raid_volume {
480 	CONFIG_PAGE_RAID_VOL_0	       *config_page;
481 	MPI_RAID_VOL_INDICATOR		sync_progress;
482 	mpt_raid_volume_flags		flags;
483 	u_int				quiesced_disks;
484 };
485 
486 typedef enum {
487 	MPT_RDF_NONE		= 0x00,
488 	MPT_RDF_ACTIVE		= 0x01,
489 	MPT_RDF_ANNOUNCED	= 0x02,
490 	MPT_RDF_UP2DATE		= 0x04,
491 	MPT_RDF_REFERENCED	= 0x08,
492 	MPT_RDF_QUIESCING	= 0x10,
493 	MPT_RDF_QUIESCED	= 0x20
494 } mpt_raid_disk_flags;
495 
496 struct mpt_raid_disk {
497 	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
498 	struct mpt_raid_volume	       *volume;
499 	u_int				member_number;
500 	u_int				pass_thru_active;
501 	mpt_raid_disk_flags		flags;
502 };
503 
504 struct mpt_evtf_record {
505 	MSG_EVENT_NOTIFY_REPLY		reply;
506 	uint32_t			context;
507 	LIST_ENTRY(mpt_evtf_record)	links;
508 };
509 
510 LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
511 
512 struct mpt_softc {
513 	device_t		dev;
514 #if __FreeBSD_version < 500000
515 	uint32_t		mpt_islocked;
516 	int			mpt_splsaved;
517 #else
518 	struct mtx		mpt_lock;
519 	int			mpt_locksetup;
520 #endif
521 	uint32_t		mpt_pers_mask;
522 	uint32_t
523 				: 8,
524 		unit		: 8,
525 		ready		: 1,
526 		fw_uploaded	: 1,
527 		msi_enable	: 1,
528 		twildcard	: 1,
529 		tenabled	: 1,
530 		do_cfg_role	: 1,
531 		raid_enabled	: 1,
532 		raid_mwce_set	: 1,
533 		getreqwaiter	: 1,
534 		shutdwn_raid    : 1,
535 		shutdwn_recovery: 1,
536 		outofbeer	: 1,
537 		disabled	: 1,
538 		is_spi		: 1,
539 		is_sas		: 1,
540 		is_fc		: 1;
541 
542 	u_int			cfg_role;
543 	u_int			role;	/* role: none, ini, target, both */
544 
545 	u_int			verbose;
546 #ifdef	MPT_TEST_MULTIPATH
547 	int			failure_id;
548 #endif
549 
550 	/*
551 	 * IOC Facts
552 	 */
553 	MSG_IOC_FACTS_REPLY	ioc_facts;
554 
555 	/*
556 	 * Port Facts
557 	 */
558 	MSG_PORT_FACTS_REPLY *	port_facts;
559 #define	mpt_ini_id	port_facts[0].PortSCSIID
560 #define	mpt_max_tgtcmds	port_facts[0].MaxPostedCmdBuffers
561 
562 	/*
563 	 * Device Configuration Information
564 	 */
565 	union {
566 		struct mpt_spi_cfg {
567 			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
568 			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
569 			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
570 			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
571 			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
572 			uint16_t			_tag_enable;
573 			uint16_t			_disc_enable;
574 		} spi;
575 #define	mpt_port_page0		cfg.spi._port_page0
576 #define	mpt_port_page1		cfg.spi._port_page1
577 #define	mpt_port_page2		cfg.spi._port_page2
578 #define	mpt_dev_page0		cfg.spi._dev_page0
579 #define	mpt_dev_page1		cfg.spi._dev_page1
580 #define	mpt_tag_enable		cfg.spi._tag_enable
581 #define	mpt_disc_enable		cfg.spi._disc_enable
582 		struct mpi_fc_cfg {
583 			CONFIG_PAGE_FC_PORT_0 _port_page0;
584 			uint32_t _port_speed;
585 #define	mpt_fcport_page0	cfg.fc._port_page0
586 #define	mpt_fcport_speed	cfg.fc._port_speed
587 		} fc;
588 	} cfg;
589 #if __FreeBSD_version >= 500000
590 	/*
591 	 * Device config information stored up for sysctl to access
592 	 */
593 	union {
594 		struct {
595 			unsigned int initiator_id;
596 		} spi;
597 		struct {
598 			char wwnn[19];
599 			char wwpn[19];
600 		} fc;
601 	} scinfo;
602 #endif
603 
604 	/* Controller Info for RAID information */
605 	CONFIG_PAGE_IOC_2 *	ioc_page2;
606 	CONFIG_PAGE_IOC_3 *	ioc_page3;
607 
608 	/* Raid Data */
609 	struct mpt_raid_volume* raid_volumes;
610 	struct mpt_raid_disk*	raid_disks;
611 	u_int			raid_max_volumes;
612 	u_int			raid_max_disks;
613 	u_int			raid_page0_len;
614 	u_int			raid_wakeup;
615 	u_int			raid_rescan;
616 	u_int			raid_resync_rate;
617 	u_int			raid_mwce_setting;
618 	u_int			raid_queue_depth;
619 	u_int			raid_nonopt_volumes;
620 	struct proc	       *raid_thread;
621 	struct callout		raid_timer;
622 
623 	/*
624 	 * PCI Hardware info
625 	 */
626 	int			pci_msi_count;
627 	struct resource *	pci_irq;	/* Interrupt map for chip */
628 	void *			ih;		/* Interupt handle */
629 	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
630 
631 	/*
632 	 * DMA Mapping Stuff
633 	 */
634 	struct resource *	pci_reg;	/* Register map for chip */
635 	int			pci_mem_rid;	/* Resource ID */
636 	bus_space_tag_t		pci_st;		/* Bus tag for registers */
637 	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
638 	/* PIO versions of above. */
639 	int			pci_pio_rid;
640 	struct resource *	pci_pio_reg;
641 	bus_space_tag_t		pci_pio_st;
642 	bus_space_handle_t	pci_pio_sh;
643 
644 	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
645 	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
646 	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
647 	uint8_t		       *reply;		/* KVA of reply memory */
648 	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
649 
650 	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
651 	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
652 	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
653 	uint8_t		       *request;	/* KVA of Request memory */
654 	bus_addr_t		request_phys;	/* BusAddr of request memory */
655 
656 	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
657 
658 	/*
659 	 * Hardware management
660 	 */
661 	u_int			reset_cnt;
662 
663 	/*
664 	 * CAM && Software Management
665 	 */
666 	request_t	       *request_pool;
667 	struct req_queue	request_free_list;
668 	struct req_queue	request_pending_list;
669 	struct req_queue	request_timeout_list;
670 
671 
672 	struct cam_sim	       *sim;
673 	struct cam_path	       *path;
674 
675 	struct cam_sim	       *phydisk_sim;
676 	struct cam_path	       *phydisk_path;
677 
678 	struct proc	       *recovery_thread;
679 	request_t	       *tmf_req;
680 
681 	/*
682 	 * Deferred frame acks due to resource shortage.
683 	 */
684 	struct mpt_evtf_list	ack_frames;
685 
686 	/*
687 	 * Target Mode Support
688 	 */
689 	uint32_t		scsi_tgt_handler_id;
690 	request_t **		tgt_cmd_ptrs;
691 	request_t **		els_cmd_ptrs;	/* FC only */
692 
693 	/*
694 	 * *snork*- this is chosen to be here *just in case* somebody
695 	 * forgets to point to it exactly and we index off of trt with
696 	 * CAM_LUN_WILDCARD.
697 	 */
698 	tgt_resource_t		trt_wildcard;		/* wildcard luns */
699 	tgt_resource_t		trt[MPT_MAX_LUNS];
700 	uint16_t		tgt_cmds_allocated;
701 	uint16_t		els_cmds_allocated;	/* FC only */
702 
703 	uint16_t		timeouts;	/* timeout count */
704 	uint16_t		success;	/* successes afer timeout */
705 	uint16_t		sequence;	/* Sequence Number */
706 	uint16_t		pad3;
707 
708 
709 	/* Paired port in some dual adapters configurations */
710 	struct mpt_softc *	mpt2;
711 
712 	/* FW Image management */
713 	uint32_t		fw_image_size;
714 	uint8_t		       *fw_image;
715 	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
716 	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
717 	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
718 
719 	/* Shutdown Event Handler. */
720 	eventhandler_tag         eh;
721 
722 	TAILQ_ENTRY(mpt_softc)	links;
723 };
724 
725 static __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
726 
727 static __inline void
728 mpt_assign_serno(struct mpt_softc *mpt, request_t *req)
729 {
730 	if ((req->serno = mpt->sequence++) == 0) {
731 		req->serno = mpt->sequence++;
732 	}
733 }
734 
735 /***************************** Locking Primitives *****************************/
736 #if __FreeBSD_version < 500000
737 #define	MPT_IFLAGS		INTR_TYPE_CAM
738 #define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
739 #define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
740 #define	MPT_OWNED(mpt)		mpt->mpt_islocked
741 #define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
742 #define	CAMLOCK_2_MPTLOCK	MPT_LOCK
743 #define	MPT_LOCK_SETUP(mpt)
744 #define	MPT_LOCK_DESTROY(mpt)
745 
746 static __inline void mpt_lockspl(struct mpt_softc *mpt);
747 static __inline void mpt_unlockspl(struct mpt_softc *mpt);
748 
749 static __inline void
750 mpt_lockspl(struct mpt_softc *mpt)
751 {
752        int s;
753 
754        s = splcam();
755        if (mpt->mpt_islocked++ == 0) {
756                mpt->mpt_splsaved = s;
757        } else {
758                splx(s);
759 	       panic("Recursed lock with mask: 0x%x\n", s);
760        }
761 }
762 
763 static __inline void
764 mpt_unlockspl(struct mpt_softc *mpt)
765 {
766        if (mpt->mpt_islocked) {
767                if (--mpt->mpt_islocked == 0) {
768                        splx(mpt->mpt_splsaved);
769                }
770        } else
771 	       panic("Negative lock count\n");
772 }
773 
774 static __inline int
775 mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
776 	   const char *wmesg, int timo)
777 {
778 	int saved_cnt;
779 	int saved_spl;
780 	int error;
781 
782 	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
783 	saved_cnt = mpt->mpt_islocked;
784 	saved_spl = mpt->mpt_splsaved;
785 	mpt->mpt_islocked = 0;
786 	error = tsleep(ident, priority, wmesg, timo);
787 	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
788 	mpt->mpt_islocked = saved_cnt;
789 	mpt->mpt_splsaved = saved_spl;
790 	return (error);
791 }
792 
793 #else
794 #ifdef	LOCKING_WORKED_AS_IT_SHOULD
795 #error "Shouldn't Be Here!"
796 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
797 #define	MPT_LOCK_SETUP(mpt)						\
798 		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
799 		mpt->mpt_locksetup = 1
800 #define	MPT_LOCK_DESTROY(mpt)						\
801 	if (mpt->mpt_locksetup) {					\
802 		mtx_destroy(&mpt->mpt_lock);				\
803 		mpt->mpt_locksetup = 0;					\
804 	}
805 
806 #define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
807 #define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
808 #define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
809 #define	MPTLOCK_2_CAMLOCK(mpt)	\
810 	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
811 #define	CAMLOCK_2_MPTLOCK(mpt)	\
812 	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
813 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
814 	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
815 
816 #else
817 
818 #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
819 #define	MPT_LOCK_SETUP(mpt)	do { } while (0)
820 #define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
821 #if	0
822 #define	MPT_LOCK(mpt)		\
823 	device_printf(mpt->dev, "LOCK %s:%d\n", __FILE__, __LINE__); 	\
824 	KASSERT(mpt->mpt_locksetup == 0,				\
825 	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
826 	mpt->mpt_locksetup = 1
827 #define	MPT_UNLOCK(mpt)		\
828 	device_printf(mpt->dev, "UNLK %s:%d\n", __FILE__, __LINE__); 	\
829 	KASSERT(mpt->mpt_locksetup == 1,				\
830 	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
831 	mpt->mpt_locksetup = 0
832 #else
833 #define	MPT_LOCK(mpt)							\
834 	KASSERT(mpt->mpt_locksetup == 0,				\
835 	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
836 	mpt->mpt_locksetup = 1
837 #define	MPT_UNLOCK(mpt)							\
838 	KASSERT(mpt->mpt_locksetup == 1,				\
839 	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
840 	mpt->mpt_locksetup = 0
841 #endif
842 #define	MPT_OWNED(mpt)		mpt->mpt_locksetup
843 #define	MPTLOCK_2_CAMLOCK(mpt)	MPT_UNLOCK(mpt)
844 #define	CAMLOCK_2_MPTLOCK(mpt)	MPT_LOCK(mpt)
845 
846 static __inline int
847 mpt_sleep(struct mpt_softc *, void *, int, const char *, int);
848 
849 static __inline int
850 mpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
851 {
852 	int r;
853 	MPT_UNLOCK(mpt);
854 	r = tsleep(i, p, w, t);
855 	MPT_LOCK(mpt);
856 	return (r);
857 }
858 #endif
859 #endif
860 
861 /******************************* Register Access ******************************/
862 static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
863 static __inline uint32_t mpt_read(struct mpt_softc *, int);
864 static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
865 static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
866 
867 static __inline void
868 mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
869 {
870 	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
871 }
872 
873 static __inline uint32_t
874 mpt_read(struct mpt_softc *mpt, int offset)
875 {
876 	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
877 }
878 
879 /*
880  * Some operations (e.g. diagnostic register writes while the ARM proccessor
881  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
882  * busses, these operations likely map to normal register accesses.
883  */
884 static __inline void
885 mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
886 {
887 	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
888 }
889 
890 static __inline uint32_t
891 mpt_pio_read(struct mpt_softc *mpt, int offset)
892 {
893 	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
894 }
895 /*********************** Reply Frame/Request Management ***********************/
896 /* Max MPT Reply we are willing to accept (must be power of 2) */
897 #define MPT_REPLY_SIZE   	256
898 
899 /*
900  * Must be less than 16384 in order for target mode to work
901  */
902 #define MPT_MAX_REQUESTS(mpt)	512
903 #define MPT_REQUEST_AREA	512
904 #define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
905 #define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
906 
907 #define MPT_CONTEXT_CB_SHIFT	(16)
908 #define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
909 #define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
910 #define MPT_CONTEXT_TO_CBI(x)	\
911     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
912 #define MPT_CONTEXT_REQI_MASK	0xFFFF
913 #define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
914 
915 /*
916  * Convert a 32bit physical address returned from IOC to an
917  * offset into our reply frame memory or the kvm address needed
918  * to access the data.  The returned address is only the low
919  * 32 bits, so mask our base physical address accordingly.
920  */
921 #define MPT_REPLY_BADDR(x)		\
922 	(x << 1)
923 #define MPT_REPLY_OTOV(m, i) 		\
924 	((void *)(&m->reply[i]))
925 
926 #define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
927 do {							\
928 	if (mpt->verbose > MPT_PRT_DEBUG)		\
929 		mpt_dump_reply_frame(mpt, reply_frame);	\
930 } while(0)
931 
932 static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
933 static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
934 
935 /*
936  * Give the reply buffer back to the IOC after we have
937  * finished processing it.
938  */
939 static __inline void
940 mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
941 {
942      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
943 }
944 
945 /* Get a reply from the IOC */
946 static __inline uint32_t
947 mpt_pop_reply_queue(struct mpt_softc *mpt)
948 {
949      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
950 }
951 
952 void
953 mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
954 
955 /************************** Scatter Gather Managment **************************/
956 /* MPT_RQSL- size of request frame, in bytes */
957 #define	MPT_RQSL(mpt)		(mpt->ioc_facts.RequestFrameSize << 2)
958 
959 /* MPT_NSGL- how many SG entries can fit in a request frame size */
960 #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
961 
962 /* MPT_NRFM- how many request frames can fit in each request alloc we make */
963 #define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
964 
965 /*
966  * MPT_NSGL_FIRST- # of SG elements that can fit after
967  * an I/O request but still within the request frame.
968  * Do this safely based upon SGE_IO_UNION.
969  *
970  * Note that the first element is *within* the SCSI request.
971  */
972 #define	MPT_NSGL_FIRST(mpt)	\
973     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
974     sizeof (SGE_IO_UNION))
975 
976 /***************************** IOC Initialization *****************************/
977 int mpt_reset(struct mpt_softc *, int /*reinit*/);
978 
979 /****************************** Debugging ************************************/
980 typedef struct mpt_decode_entry {
981 	char    *name;
982 	u_int	 value;
983 	u_int	 mask;
984 } mpt_decode_entry_t;
985 
986 int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
987 		     const char *name, u_int value, u_int *cur_column,
988 		     u_int wrap_point);
989 
990 void mpt_dump_data(struct mpt_softc *, const char *, void *, int);
991 void mpt_dump_request(struct mpt_softc *, request_t *);
992 
993 enum {
994 	MPT_PRT_ALWAYS,
995 	MPT_PRT_FATAL,
996 	MPT_PRT_ERROR,
997 	MPT_PRT_WARN,
998 	MPT_PRT_INFO,
999 	MPT_PRT_NEGOTIATION,
1000 	MPT_PRT_DEBUG,
1001 	MPT_PRT_DEBUG1,
1002 	MPT_PRT_DEBUG2,
1003 	MPT_PRT_DEBUG3,
1004 	MPT_PRT_TRACE,
1005 	MPT_PRT_NONE=100
1006 };
1007 
1008 #if __FreeBSD_version > 500000
1009 #define mpt_lprt(mpt, level, ...)		\
1010 do {						\
1011 	if (level <= (mpt)->verbose)		\
1012 		mpt_prt(mpt, __VA_ARGS__);	\
1013 } while (0)
1014 
1015 #define mpt_lprtc(mpt, level, ...)		 \
1016 do {						 \
1017 	if (level <= (mpt)->debug_level)	 \
1018 		mpt_prtc(mpt, __VA_ARGS__);	 \
1019 } while (0)
1020 #else
1021 void mpt_lprt(struct mpt_softc *, int, const char *, ...)
1022 	__printflike(3, 4);
1023 void mpt_lprtc(struct mpt_softc *, int, const char *, ...)
1024 	__printflike(3, 4);
1025 #endif
1026 void mpt_prt(struct mpt_softc *, const char *, ...)
1027 	__printflike(2, 3);
1028 void mpt_prtc(struct mpt_softc *, const char *, ...)
1029 	__printflike(2, 3);
1030 
1031 /**************************** Target Mode Related ***************************/
1032 static __inline int mpt_cdblen(uint8_t, int);
1033 static __inline int
1034 mpt_cdblen(uint8_t cdb0, int maxlen)
1035 {
1036 	int group = cdb0 >> 5;
1037 	switch (group) {
1038 	case 0:
1039 		return (6);
1040 	case 1:
1041 		return (10);
1042 	case 4:
1043 	case 5:
1044 		return (12);
1045 	default:
1046 		return (16);
1047 	}
1048 }
1049 #ifdef	INVARIANTS
1050 static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
1051 static __inline request_t *
1052 mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
1053 {
1054 	uint16_t rtg = (tag >> 18);
1055 	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1056 	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1057 	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1058 	return (mpt->tgt_cmd_ptrs[rtg]);
1059 }
1060 
1061 
1062 static __inline int
1063 mpt_req_on_free_list(struct mpt_softc *, request_t *);
1064 static __inline int
1065 mpt_req_on_pending_list(struct mpt_softc *, request_t *);
1066 
1067 static __inline void
1068 mpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1069 static __inline void
1070 mpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1071 
1072 
1073 /*
1074  * Is request on freelist?
1075  */
1076 static __inline int
1077 mpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1078 {
1079 	request_t *lrq;
1080 
1081 	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1082 		if (lrq == req) {
1083 			return (1);
1084 		}
1085 	}
1086 	return (0);
1087 }
1088 
1089 /*
1090  * Is request on pending list?
1091  */
1092 static __inline int
1093 mpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1094 {
1095 	request_t *lrq;
1096 
1097 	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1098 		if (lrq == req) {
1099 			return (1);
1100 		}
1101 	}
1102 	return (0);
1103 }
1104 
1105 /*
1106  * Make sure that req *is* part of one of the special lists
1107  */
1108 static __inline void
1109 mpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1110 {
1111 	int i;
1112 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1113 		if (req == mpt->els_cmd_ptrs[i]) {
1114 			return;
1115 		}
1116 	}
1117 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1118 		if (req == mpt->tgt_cmd_ptrs[i]) {
1119 			return;
1120 		}
1121 	}
1122 	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1123 	    s, line, req, req->serno,
1124 	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1125 }
1126 
1127 /*
1128  * Make sure that req is *not* part of one of the special lists.
1129  */
1130 static __inline void
1131 mpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1132 {
1133 	int i;
1134 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1135 		KASSERT(req != mpt->els_cmd_ptrs[i],
1136 		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1137 		    s, line, req, req->serno,
1138 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1139 	}
1140 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1141 		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1142 		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1143 		    s, line, req, req->serno,
1144 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1145 	}
1146 }
1147 #endif
1148 
1149 /*
1150  * Task Management Types, purely for internal consumption
1151  */
1152 typedef enum {
1153 	MPT_ABORT_TASK_SET=1234,
1154 	MPT_CLEAR_TASK_SET,
1155 	MPT_TARGET_RESET,
1156 	MPT_CLEAR_ACA,
1157 	MPT_TERMINATE_TASK,
1158 	MPT_NIL_TMT_VALUE=5678
1159 } mpt_task_mgmt_t;
1160 
1161 /**************************** Unclassified Routines ***************************/
1162 void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1163 int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1164 					 size_t reply_len, void *reply);
1165 int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1166 			     mpt_req_state_t state, mpt_req_state_t mask,
1167 			     int sleep_ok, int time_ms);
1168 void		mpt_enable_ints(struct mpt_softc *mpt);
1169 void		mpt_disable_ints(struct mpt_softc *mpt);
1170 int		mpt_attach(struct mpt_softc *mpt);
1171 int		mpt_shutdown(struct mpt_softc *mpt);
1172 int		mpt_detach(struct mpt_softc *mpt);
1173 int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1174 				       size_t len, void *cmd);
1175 request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1176 void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1177 void		mpt_intr(void *arg);
1178 void		mpt_check_doorbell(struct mpt_softc *mpt);
1179 void		mpt_dump_reply_frame(struct mpt_softc *mpt,
1180 				     MSG_DEFAULT_REPLY *reply_frame);
1181 
1182 void		mpt_set_config_regs(struct mpt_softc *);
1183 int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1184 				  u_int /*Action*/, u_int /*PageVersion*/,
1185 				  u_int /*PageLength*/, u_int /*PageNumber*/,
1186 				  u_int /*PageType*/, uint32_t /*PageAddress*/,
1187 				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1188 				  int /*sleep_ok*/, int /*timeout_ms*/);
1189 int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1190 				    int /*PageNumber*/,
1191 				    uint32_t /*PageAddress*/,
1192 				    CONFIG_PAGE_HEADER *,
1193 				    int /*sleep_ok*/, int /*timeout_ms*/);
1194 int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1195 				  uint32_t /*PageAddress*/,
1196 				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1197 				  int /*sleep_ok*/, int /*timeout_ms*/);
1198 int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1199 				   uint32_t /*PageAddress*/,
1200 				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1201 				   int /*sleep_ok*/, int /*timeout_ms*/);
1202 static __inline int
1203 mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1204 		      CONFIG_PAGE_HEADER *hdr, size_t len,
1205 		      int sleep_ok, int timeout_ms)
1206 {
1207 	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1208 				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1209 }
1210 
1211 static __inline int
1212 mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1213 		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1214 		       int timeout_ms)
1215 {
1216 	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1217 				   PageAddress, hdr, len, sleep_ok,
1218 				   timeout_ms));
1219 }
1220 /* mpt_debug.c functions */
1221 void mpt_print_reply(void *vmsg);
1222 void mpt_print_db(uint32_t mb);
1223 void mpt_print_config_reply(void *vmsg);
1224 char *mpt_ioc_diag(uint32_t diag);
1225 void mpt_req_state(mpt_req_state_t state);
1226 void mpt_print_config_request(void *vmsg);
1227 void mpt_print_request(void *vmsg);
1228 void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1229 void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1230 #endif /* _MPT_H_ */
1231