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