xref: /dragonfly/sys/dev/disk/mpt/mpt.h (revision d4ef6694)
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 	struct sysctl_ctx_list		mpt_sysctl_ctx;
740 	struct sysctl_oid		*mpt_sysctl_tree;
741 
742 	TAILQ_ENTRY(mpt_softc)	links;
743 };
744 
745 static __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
746 
747 static __inline void
748 mpt_assign_serno(struct mpt_softc *mpt, request_t *req)
749 {
750 	if ((req->serno = mpt->sequence++) == 0) {
751 		req->serno = mpt->sequence++;
752 	}
753 }
754 
755 /***************************** Locking Primitives *****************************/
756 #define	MPT_IFLAGS		INTR_MPSAFE
757 #define	MPT_LOCK_SETUP(mpt)						\
758 		lockinit(&mpt->mpt_lock, "mpt", 0, LK_CANRECURSE);	\
759 		mpt->mpt_locksetup = 1
760 #define	MPT_LOCK_DESTROY(mpt)						\
761 	if (mpt->mpt_locksetup) {					\
762 		lockuninit(&mpt->mpt_lock);				\
763 		mpt->mpt_locksetup = 0;					\
764 	}
765 
766 #define	MPT_LOCK(mpt)		lockmgr(&(mpt)->mpt_lock, LK_EXCLUSIVE)
767 #define	MPT_UNLOCK(mpt)		lockmgr(&(mpt)->mpt_lock, LK_RELEASE)
768 #define	MPT_OWNED(mpt)		lockstatus(&(mpt)->mpt_lock, curthread)
769 #define	MPT_LOCK_ASSERT(mpt)	KKASSERT(lockstatus(&(mpt)->mpt_lock, curthread) != 0)
770 #define mpt_sleep(mpt, ident, priority, wmesg, timo) \
771 	lksleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
772 #define mpt_req_timeout(req, ticks, func, arg) \
773 	callout_reset(&(req)->callout, (ticks), (func), (arg))
774 #define mpt_req_untimeout(req, func, arg) \
775 	callout_stop(&(req)->callout)
776 #define mpt_callout_init(mpt, c) \
777 	callout_init_mp(c)
778 #define mpt_callout_drain(mpt, c) \
779 	callout_stop_sync(c)
780 
781 /******************************* Register Access ******************************/
782 static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
783 static __inline uint32_t mpt_read(struct mpt_softc *, int);
784 static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
785 static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
786 
787 static __inline void
788 mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
789 {
790 	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
791 }
792 
793 static __inline uint32_t
794 mpt_read(struct mpt_softc *mpt, int offset)
795 {
796 	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
797 }
798 
799 /*
800  * Some operations (e.g. diagnostic register writes while the ARM proccessor
801  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
802  * busses, these operations likely map to normal register accesses.
803  */
804 static __inline void
805 mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
806 {
807 	KASSERT(mpt->pci_pio_reg != NULL, ("no PIO resource"));
808 	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
809 }
810 
811 static __inline uint32_t
812 mpt_pio_read(struct mpt_softc *mpt, int offset)
813 {
814 	KASSERT(mpt->pci_pio_reg != NULL, ("no PIO resource"));
815 	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
816 }
817 /*********************** Reply Frame/Request Management ***********************/
818 /* Max MPT Reply we are willing to accept (must be power of 2) */
819 #define MPT_REPLY_SIZE   	256
820 
821 /*
822  * Must be less than 16384 in order for target mode to work
823  */
824 #define MPT_MAX_REQUESTS(mpt)	512
825 #define MPT_REQUEST_AREA	512
826 #define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
827 #define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
828 
829 #define MPT_CONTEXT_CB_SHIFT	(16)
830 #define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
831 #define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
832 #define MPT_CONTEXT_TO_CBI(x)	\
833     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
834 #define MPT_CONTEXT_REQI_MASK	0xFFFF
835 #define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
836 
837 /*
838  * Convert a 32bit physical address returned from IOC to an
839  * offset into our reply frame memory or the kvm address needed
840  * to access the data.  The returned address is only the low
841  * 32 bits, so mask our base physical address accordingly.
842  */
843 #define MPT_REPLY_BADDR(x)		\
844 	(x << 1)
845 #define MPT_REPLY_OTOV(m, i) 		\
846 	((void *)(&m->reply[i]))
847 
848 #define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
849 do {							\
850 	if (mpt->verbose > MPT_PRT_DEBUG)		\
851 		mpt_dump_reply_frame(mpt, reply_frame);	\
852 } while(0)
853 
854 static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
855 static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
856 
857 /*
858  * Give the reply buffer back to the IOC after we have
859  * finished processing it.
860  */
861 static __inline void
862 mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
863 {
864      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
865 }
866 
867 /* Get a reply from the IOC */
868 static __inline uint32_t
869 mpt_pop_reply_queue(struct mpt_softc *mpt)
870 {
871      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
872 }
873 
874 void
875 mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
876 
877 /************************** Scatter Gather Management **************************/
878 /* MPT_RQSL- size of request frame, in bytes */
879 #define	MPT_RQSL(mpt)		(mpt->ioc_facts.RequestFrameSize << 2)
880 
881 /* MPT_NSGL- how many SG entries can fit in a request frame size */
882 #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
883 
884 /* MPT_NRFM- how many request frames can fit in each request alloc we make */
885 #define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
886 
887 /*
888  * MPT_NSGL_FIRST- # of SG elements that can fit after
889  * an I/O request but still within the request frame.
890  * Do this safely based upon SGE_IO_UNION.
891  *
892  * Note that the first element is *within* the SCSI request.
893  */
894 #define	MPT_NSGL_FIRST(mpt)	\
895     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
896     sizeof (SGE_IO_UNION))
897 
898 /***************************** IOC Initialization *****************************/
899 int mpt_reset(struct mpt_softc *, int /*reinit*/);
900 
901 /****************************** Debugging ************************************/
902 void mpt_dump_data(struct mpt_softc *, const char *, void *, int);
903 void mpt_dump_request(struct mpt_softc *, request_t *);
904 
905 enum {
906 	MPT_PRT_ALWAYS,
907 	MPT_PRT_FATAL,
908 	MPT_PRT_ERROR,
909 	MPT_PRT_WARN,
910 	MPT_PRT_INFO,
911 	MPT_PRT_NEGOTIATION,
912 	MPT_PRT_DEBUG,
913 	MPT_PRT_DEBUG1,
914 	MPT_PRT_DEBUG2,
915 	MPT_PRT_DEBUG3,
916 	MPT_PRT_TRACE,
917 	MPT_PRT_NONE=100
918 };
919 
920 #define mpt_lprt(mpt, level, ...)		\
921 do {						\
922 	if (level <= (mpt)->verbose)		\
923 		mpt_prt(mpt, __VA_ARGS__);	\
924 } while (0)
925 
926 #if 0
927 #define mpt_lprtc(mpt, level, ...)		\
928 do {						\
929 	if (level <= (mpt)->verbose)		\
930 		mpt_prtc(mpt, __VA_ARGS__);	\
931 } while (0)
932 #endif
933 void mpt_prt(struct mpt_softc *, const char *, ...)
934 	__printflike(2, 3);
935 void mpt_prtc(struct mpt_softc *, const char *, ...)
936 	__printflike(2, 3);
937 
938 /**************************** Target Mode Related ***************************/
939 static __inline int mpt_cdblen(uint8_t, int);
940 static __inline int
941 mpt_cdblen(uint8_t cdb0, int maxlen)
942 {
943 	int group = cdb0 >> 5;
944 	switch (group) {
945 	case 0:
946 		return (6);
947 	case 1:
948 		return (10);
949 	case 4:
950 	case 5:
951 		return (12);
952 	default:
953 		return (16);
954 	}
955 }
956 #ifdef	INVARIANTS
957 static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
958 static __inline request_t *
959 mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
960 {
961 	uint16_t rtg = (tag >> 18);
962 	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d", tag));
963 	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
964 	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
965 	return (mpt->tgt_cmd_ptrs[rtg]);
966 }
967 #endif
968 
969 static __inline int
970 mpt_req_on_free_list(struct mpt_softc *, request_t *);
971 static __inline int
972 mpt_req_on_pending_list(struct mpt_softc *, request_t *);
973 
974 /*
975  * Is request on freelist?
976  */
977 static __inline int
978 mpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
979 {
980 	request_t *lrq;
981 
982 	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
983 		if (lrq == req) {
984 			return (1);
985 		}
986 	}
987 	return (0);
988 }
989 
990 /*
991  * Is request on pending list?
992  */
993 static __inline int
994 mpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
995 {
996 	request_t *lrq;
997 
998 	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
999 		if (lrq == req) {
1000 			return (1);
1001 		}
1002 	}
1003 	return (0);
1004 }
1005 
1006 #ifdef	INVARIANTS
1007 static __inline void
1008 mpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1009 static __inline void
1010 mpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1011 
1012 /*
1013  * Make sure that req *is* part of one of the special lists
1014  */
1015 static __inline void
1016 mpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1017 {
1018 	int i;
1019 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1020 		if (req == mpt->els_cmd_ptrs[i]) {
1021 			return;
1022 		}
1023 	}
1024 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1025 		if (req == mpt->tgt_cmd_ptrs[i]) {
1026 			return;
1027 		}
1028 	}
1029 	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs",
1030 	    s, line, req, req->serno,
1031 	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1032 }
1033 
1034 /*
1035  * Make sure that req is *not* part of one of the special lists.
1036  */
1037 static __inline void
1038 mpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1039 {
1040 	int i;
1041 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1042 		KASSERT(req != mpt->els_cmd_ptrs[i],
1043 		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d",
1044 		    s, line, req, req->serno,
1045 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1046 	}
1047 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1048 		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1049 		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d",
1050 		    s, line, req, req->serno,
1051 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1052 	}
1053 }
1054 #endif
1055 
1056 /*
1057  * Task Management Types, purely for internal consumption
1058  */
1059 typedef enum {
1060 	MPT_ABORT_TASK_SET=1234,
1061 	MPT_CLEAR_TASK_SET,
1062 	MPT_TARGET_RESET,
1063 	MPT_CLEAR_ACA,
1064 	MPT_TERMINATE_TASK,
1065 	MPT_NIL_TMT_VALUE=5678
1066 } mpt_task_mgmt_t;
1067 
1068 /**************************** Unclassified Routines ***************************/
1069 void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1070 int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1071 					 size_t reply_len, void *reply);
1072 int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1073 			     mpt_req_state_t state, mpt_req_state_t mask,
1074 			     int sleep_ok, int time_ms);
1075 void		mpt_enable_ints(struct mpt_softc *mpt);
1076 void		mpt_disable_ints(struct mpt_softc *mpt);
1077 int		mpt_attach(struct mpt_softc *mpt);
1078 int		mpt_shutdown(struct mpt_softc *mpt);
1079 int		mpt_detach(struct mpt_softc *mpt);
1080 int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1081 				       size_t len, void *cmd);
1082 request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1083 void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1084 void		mpt_intr(void *arg);
1085 void		mpt_check_doorbell(struct mpt_softc *mpt);
1086 void		mpt_dump_reply_frame(struct mpt_softc *mpt,
1087 				     MSG_DEFAULT_REPLY *reply_frame);
1088 
1089 int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1090 				  cfgparms_t *params,
1091 				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1092 				  int /*sleep_ok*/, int /*timeout_ms*/);
1093 int		mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion,
1094 				       int PageNumber, uint32_t PageAddress,
1095 				       int ExtPageType,
1096 				       CONFIG_EXTENDED_PAGE_HEADER *rslt,
1097 				       int sleep_ok, int timeout_ms);
1098 int		mpt_read_extcfg_page(struct mpt_softc *mpt, int Action,
1099 				     uint32_t PageAddress,
1100 				     CONFIG_EXTENDED_PAGE_HEADER *hdr,
1101 				     void *buf, size_t len, int sleep_ok,
1102 				     int timeout_ms);
1103 int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1104 				    int /*PageNumber*/,
1105 				    uint32_t /*PageAddress*/,
1106 				    CONFIG_PAGE_HEADER *,
1107 				    int /*sleep_ok*/, int /*timeout_ms*/);
1108 int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1109 				  uint32_t /*PageAddress*/,
1110 				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1111 				  int /*sleep_ok*/, int /*timeout_ms*/);
1112 int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1113 				   uint32_t /*PageAddress*/,
1114 				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1115 				   int /*sleep_ok*/, int /*timeout_ms*/);
1116 static __inline int
1117 mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1118 		      CONFIG_PAGE_HEADER *hdr, size_t len,
1119 		      int sleep_ok, int timeout_ms)
1120 {
1121 	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1122 				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1123 }
1124 
1125 static __inline int
1126 mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1127 		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1128 		       int timeout_ms)
1129 {
1130 	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1131 				   PageAddress, hdr, len, sleep_ok,
1132 				   timeout_ms));
1133 }
1134 /* mpt_debug.c functions */
1135 void mpt_print_reply(void *vmsg);
1136 void mpt_print_db(uint32_t mb);
1137 void mpt_print_config_reply(void *vmsg);
1138 char *mpt_ioc_diag(uint32_t diag);
1139 void mpt_req_state(mpt_req_state_t state);
1140 void mpt_print_config_request(void *vmsg);
1141 void mpt_print_request(void *vmsg);
1142 void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1143 #endif /* _MPT_H_ */
1144