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