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