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