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