xref: /freebsd/sys/dev/mpt/mpt.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1098ca2bdSWarner Losh /*-
29b631363SMatt Jacob  * Generic defines for LSI '909 FC  adapters.
39b631363SMatt Jacob  * FreeBSD Version.
49b631363SMatt Jacob  *
5*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause AND BSD-3-Clause
6718cf2ccSPedro F. Giffuni  *
79b631363SMatt Jacob  * Copyright (c)  2000, 2001 by Greg Ansley
89b631363SMatt Jacob  *
99b631363SMatt Jacob  * Redistribution and use in source and binary forms, with or without
109b631363SMatt Jacob  * modification, are permitted provided that the following conditions
119b631363SMatt Jacob  * are met:
129b631363SMatt Jacob  * 1. Redistributions of source code must retain the above copyright
139b631363SMatt Jacob  *    notice immediately at the beginning of the file, without modification,
149b631363SMatt Jacob  *    this list of conditions, and the following disclaimer.
159b631363SMatt Jacob  * 2. The name of the author may not be used to endorse or promote products
169b631363SMatt Jacob  *    derived from this software without specific prior written permission.
179b631363SMatt Jacob  *
189b631363SMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199b631363SMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209b631363SMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219b631363SMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
229b631363SMatt Jacob  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239b631363SMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249b631363SMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259b631363SMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269b631363SMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279b631363SMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289b631363SMatt Jacob  * SUCH DAMAGE.
290b80d21bSMatt Jacob  */
300b80d21bSMatt Jacob /*-
310b80d21bSMatt Jacob  * Copyright (c) 2002, 2006 by Matthew Jacob
320b80d21bSMatt Jacob  * All rights reserved.
33b0a2fdeeSScott Long  *
340b80d21bSMatt Jacob  * Redistribution and use in source and binary forms, with or without
350b80d21bSMatt Jacob  * modification, are permitted provided that the following conditions are
360b80d21bSMatt Jacob  * met:
370b80d21bSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
380b80d21bSMatt Jacob  *    notice, this list of conditions and the following disclaimer.
390b80d21bSMatt Jacob  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
400b80d21bSMatt Jacob  *    substantially similar to the "NO WARRANTY" disclaimer below
410b80d21bSMatt Jacob  *    ("Disclaimer") and any redistribution must be conditioned upon including
420b80d21bSMatt Jacob  *    a substantially similar Disclaimer requirement for further binary
430b80d21bSMatt Jacob  *    redistribution.
440b80d21bSMatt Jacob  * 3. Neither the names of the above listed copyright holders nor the names
450b80d21bSMatt Jacob  *    of any contributors may be used to endorse or promote products derived
460b80d21bSMatt Jacob  *    from this software without specific prior written permission.
470b80d21bSMatt Jacob  *
480b80d21bSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
490b80d21bSMatt Jacob  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
500b80d21bSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
510b80d21bSMatt Jacob  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
520b80d21bSMatt Jacob  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
530b80d21bSMatt Jacob  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
540b80d21bSMatt Jacob  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
550b80d21bSMatt Jacob  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
560b80d21bSMatt Jacob  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
570b80d21bSMatt Jacob  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
580b80d21bSMatt Jacob  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
590b80d21bSMatt Jacob  *
600b80d21bSMatt Jacob  * Support from Chris Ellsworth in order to make SAS adapters work
610b80d21bSMatt Jacob  * is gratefully acknowledged.
62ec5fe39dSMatt Jacob  *
63ec5fe39dSMatt Jacob  *
64ec5fe39dSMatt Jacob  * Support from LSI-Logic has also gone a great deal toward making this a
65ec5fe39dSMatt Jacob  * workable subsystem and is gratefully acknowledged.
669b631363SMatt Jacob  */
679b631363SMatt Jacob /*
68b0a2fdeeSScott Long  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
69b0a2fdeeSScott Long  * Copyright (c) 2004, 2005 Justin T. Gibbs
70b0a2fdeeSScott Long  * Copyright (c) 2005, WHEEL Sp. z o.o.
71b0a2fdeeSScott Long  * All rights reserved.
72b0a2fdeeSScott Long  *
73b0a2fdeeSScott Long  * Redistribution and use in source and binary forms, with or without
74b0a2fdeeSScott Long  * modification, are permitted provided that the following conditions are
75b0a2fdeeSScott Long  * met:
76b0a2fdeeSScott Long  * 1. Redistributions of source code must retain the above copyright
77b0a2fdeeSScott Long  *    notice, this list of conditions and the following disclaimer.
78b0a2fdeeSScott Long  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
79b0a2fdeeSScott Long  *    substantially similar to the "NO WARRANTY" disclaimer below
80b0a2fdeeSScott Long  *    ("Disclaimer") and any redistribution must be conditioned upon including
81b0a2fdeeSScott Long  *    a substantially similar Disclaimer requirement for further binary
82b0a2fdeeSScott Long  *    redistribution.
83286e947fSJustin T. Gibbs  * 3. Neither the names of the above listed copyright holders nor the names
84286e947fSJustin T. Gibbs  *    of any contributors may be used to endorse or promote products derived
85286e947fSJustin T. Gibbs  *    from this software without specific prior written permission.
86b0a2fdeeSScott Long  *
87b0a2fdeeSScott Long  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88b0a2fdeeSScott Long  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89b0a2fdeeSScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90b0a2fdeeSScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
91b0a2fdeeSScott Long  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
92b0a2fdeeSScott Long  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
93b0a2fdeeSScott Long  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
94b0a2fdeeSScott Long  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
95b0a2fdeeSScott Long  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
96b0a2fdeeSScott Long  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
97b0a2fdeeSScott Long  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
989b631363SMatt Jacob  */
999b631363SMatt Jacob 
1009b631363SMatt Jacob #ifndef _MPT_H_
1019b631363SMatt Jacob #define _MPT_H_
102b0a2fdeeSScott Long 
103b0a2fdeeSScott Long /********************************* OS Includes ********************************/
104b0a2fdeeSScott Long #include <sys/param.h>
105b0a2fdeeSScott Long #include <sys/systm.h>
1064b847ffaSMarius Strobl #include <sys/bus.h>
1074b847ffaSMarius Strobl #include <sys/condvar.h>
108b0a2fdeeSScott Long #include <sys/endian.h>
109b0a2fdeeSScott Long #include <sys/eventhandler.h>
110b0a2fdeeSScott Long #include <sys/kernel.h>
111b0a2fdeeSScott Long #include <sys/lock.h>
112b0a2fdeeSScott Long #include <sys/malloc.h>
113b0a2fdeeSScott Long #include <sys/module.h>
1144b847ffaSMarius Strobl #include <sys/mutex.h>
1154b847ffaSMarius Strobl #include <sys/proc.h>
1164b847ffaSMarius Strobl #include <sys/queue.h>
1174b847ffaSMarius Strobl #include <sys/rman.h>
1184b847ffaSMarius Strobl #include <sys/types.h>
119b0a2fdeeSScott Long 
1204b847ffaSMarius Strobl #include <machine/bus.h>
121b0a2fdeeSScott Long #include <machine/cpu.h>
122b0a2fdeeSScott Long #include <machine/resource.h>
123b0a2fdeeSScott Long 
124444dd2b6SMatt Jacob #include <dev/pci/pcireg.h>
125444dd2b6SMatt Jacob #include <dev/pci/pcivar.h>
126444dd2b6SMatt Jacob 
127b0a2fdeeSScott Long #include "opt_ddb.h"
128b0a2fdeeSScott Long 
129b0a2fdeeSScott Long /**************************** Register Definitions ****************************/
130b0a2fdeeSScott Long #include <dev/mpt/mpt_reg.h>
131b0a2fdeeSScott Long 
132b0a2fdeeSScott Long /******************************* MPI Definitions ******************************/
133b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_type.h>
134b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi.h>
135b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_cnfg.h>
136b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_ioc.h>
137b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_raid.h>
138b0a2fdeeSScott Long 
139b0a2fdeeSScott Long /* XXX For mpt_debug.c */
140b0a2fdeeSScott Long #include <dev/mpt/mpilib/mpi_init.h>
141b0a2fdeeSScott Long 
1420e0521a1SMatt Jacob #define	MPT_S64_2_SCALAR(y)	((((int64_t)y.High) << 32) | (y.Low))
1430e0521a1SMatt Jacob #define	MPT_U64_2_SCALAR(y)	((((uint64_t)y.High) << 32) | (y.Low))
1440e0521a1SMatt Jacob 
145b0a2fdeeSScott Long /****************************** Misc Definitions ******************************/
146a2baed97SMatt Jacob /* #define MPT_TEST_MULTIPATH	1 */
1479b631363SMatt Jacob #define MPT_OK (0)
1489b631363SMatt Jacob #define MPT_FAIL (0x10000)
1499b631363SMatt Jacob 
150b0a2fdeeSScott Long #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
1519b631363SMatt Jacob 
152c87e3f83SMatt Jacob #define	MPT_ROLE_NONE		0
153c87e3f83SMatt Jacob #define	MPT_ROLE_INITIATOR	1
154c87e3f83SMatt Jacob #define	MPT_ROLE_TARGET		2
155c87e3f83SMatt Jacob #define	MPT_ROLE_BOTH		3
156c87e3f83SMatt Jacob #define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
157c87e3f83SMatt Jacob 
1588ba5efbdSMarius Strobl #define	MPT_INI_ID_NONE		-1
1598ba5efbdSMarius Strobl 
160b0a2fdeeSScott Long /**************************** Forward Declarations ****************************/
161b0a2fdeeSScott Long struct mpt_softc;
162b0a2fdeeSScott Long struct mpt_personality;
163b0a2fdeeSScott Long typedef struct req_entry request_t;
1649b631363SMatt Jacob 
165b0a2fdeeSScott Long /************************* Personality Module Support *************************/
166b0a2fdeeSScott Long typedef int mpt_load_handler_t(struct mpt_personality *);
167b0a2fdeeSScott Long typedef int mpt_probe_handler_t(struct mpt_softc *);
168b0a2fdeeSScott Long typedef int mpt_attach_handler_t(struct mpt_softc *);
169c87e3f83SMatt Jacob typedef int mpt_enable_handler_t(struct mpt_softc *);
170a7303be1SMatt Jacob typedef void mpt_ready_handler_t(struct mpt_softc *);
171b0a2fdeeSScott Long typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
172b0a2fdeeSScott Long 				MSG_EVENT_NOTIFY_REPLY *);
173b0a2fdeeSScott Long typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
174b0a2fdeeSScott Long /* XXX Add return value and use for veto? */
175b0a2fdeeSScott Long typedef void mpt_shutdown_handler_t(struct mpt_softc *);
176b0a2fdeeSScott Long typedef void mpt_detach_handler_t(struct mpt_softc *);
177b0a2fdeeSScott Long typedef int mpt_unload_handler_t(struct mpt_personality *);
178b0a2fdeeSScott Long 
179b0a2fdeeSScott Long struct mpt_personality
180b0a2fdeeSScott Long {
181b0a2fdeeSScott Long 	const char		*name;
182b0a2fdeeSScott Long 	uint32_t		 id;		/* Assigned identifier. */
183b0a2fdeeSScott Long 	u_int			 use_count;	/* Instances using personality*/
184b0a2fdeeSScott Long 	mpt_load_handler_t	*load;		/* configure personailty */
185b0a2fdeeSScott Long #define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
186b0a2fdeeSScott Long 	mpt_probe_handler_t	*probe;		/* configure personailty */
187b0a2fdeeSScott Long 	mpt_attach_handler_t	*attach;	/* initialize device instance */
188c87e3f83SMatt Jacob 	mpt_enable_handler_t	*enable;	/* enable device */
189a7303be1SMatt Jacob 	mpt_ready_handler_t	*ready;		/* final open for business */
190b0a2fdeeSScott Long 	mpt_event_handler_t	*event;		/* Handle MPI event. */
191b0a2fdeeSScott Long 	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
192b0a2fdeeSScott Long 	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
193b0a2fdeeSScott Long 	mpt_detach_handler_t	*detach;	/* release device instance */
194b0a2fdeeSScott Long 	mpt_unload_handler_t	*unload;	/* Shutdown personality */
195b0a2fdeeSScott Long #define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
1969b631363SMatt Jacob };
1979b631363SMatt Jacob 
198b0a2fdeeSScott Long int mpt_modevent(module_t, int, void *);
1999b631363SMatt Jacob 
200b0a2fdeeSScott Long /* Maximum supported number of personalities. */
201b0a2fdeeSScott Long #define MPT_MAX_PERSONALITIES	(15)
2029b631363SMatt Jacob 
203b0a2fdeeSScott Long #define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
204b0a2fdeeSScott Long 	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
205b0a2fdeeSScott Long 
206b0a2fdeeSScott Long #define DECLARE_MPT_PERSONALITY(name, order)				  \
207b0a2fdeeSScott Long 	static moduledata_t name##_mod = {				  \
208b0a2fdeeSScott Long 		#name, mpt_modevent, &name##_personality		  \
209b0a2fdeeSScott Long 	};								  \
210b0a2fdeeSScott Long 	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
211b0a2fdeeSScott Long 	MODULE_VERSION(name, 1);					  \
212b0a2fdeeSScott Long 	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
213b0a2fdeeSScott Long 
214b0a2fdeeSScott Long /******************************* Bus DMA Support ******************************/
215b0a2fdeeSScott Long /* XXX Need to update bus_dmamap_sync to take a range argument. */
216b0a2fdeeSScott Long #define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
217b0a2fdeeSScott Long 	bus_dmamap_sync(dma_tag, dmamap, op)
218b0a2fdeeSScott Long 
219b0a2fdeeSScott Long #define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
220b0a2fdeeSScott Long 			   lowaddr, highaddr, filter, filterarg,	\
221b0a2fdeeSScott Long 			   maxsize, nsegments, maxsegsz, flags,		\
222b0a2fdeeSScott Long 			   dma_tagp)					\
223b0a2fdeeSScott Long 	bus_dma_tag_create(parent_tag, alignment, boundary,		\
224b0a2fdeeSScott Long 			   lowaddr, highaddr, filter, filterarg,	\
225b0a2fdeeSScott Long 			   maxsize, nsegments, maxsegsz, flags,		\
226d0a68c27SMatt Jacob 			   busdma_lock_mutex, &(mpt)->mpt_lock,		\
227b0a2fdeeSScott Long 			   dma_tagp)
228b0a2fdeeSScott Long struct mpt_map_info {
229b0a2fdeeSScott Long 	struct mpt_softc *mpt;
230b0a2fdeeSScott Long 	int		  error;
231b0a2fdeeSScott Long 	uint32_t	  phys;
232b0a2fdeeSScott Long };
233b0a2fdeeSScott Long 
234b0a2fdeeSScott Long void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
235b0a2fdeeSScott Long 
236453130d9SPedro F. Giffuni /********************************* Endianness *********************************/
2370e0521a1SMatt Jacob #define	MPT_2_HOST64(ptr, tag)	ptr->tag = le64toh(ptr->tag)
2380e0521a1SMatt Jacob #define	MPT_2_HOST32(ptr, tag)	ptr->tag = le32toh(ptr->tag)
2390e0521a1SMatt Jacob #define	MPT_2_HOST16(ptr, tag)	ptr->tag = le16toh(ptr->tag)
240b0a2fdeeSScott Long 
2410e0521a1SMatt Jacob #define	HOST_2_MPT64(ptr, tag)	ptr->tag = htole64(ptr->tag)
2420e0521a1SMatt Jacob #define	HOST_2_MPT32(ptr, tag)	ptr->tag = htole32(ptr->tag)
2430e0521a1SMatt Jacob #define	HOST_2_MPT16(ptr, tag)	ptr->tag = htole16(ptr->tag)
2440e0521a1SMatt Jacob 
2457185cd36SMatt Jacob #if	_BYTE_ORDER == _BIG_ENDIAN
2460e0521a1SMatt Jacob void mpt2host_sge_simple_union(SGE_SIMPLE_UNION *);
2470e0521a1SMatt Jacob void mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *);
2480e0521a1SMatt Jacob void mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *);
2490e0521a1SMatt Jacob void mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *);
2507ee37807SMarius Strobl void mpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *);
2517ee37807SMarius Strobl void mpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *);
2527ee37807SMarius Strobl void mpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *);
2537ee37807SMarius Strobl void host2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *);
2547ee37807SMarius Strobl void mpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *);
2557ee37807SMarius Strobl void mpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *);
2567ee37807SMarius Strobl void mpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *);
2577ee37807SMarius Strobl void host2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *);
2587ee37807SMarius Strobl void mpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *);
2597ee37807SMarius Strobl void mpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *);
2607ee37807SMarius Strobl void host2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *);
2610e0521a1SMatt Jacob void mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *);
2627ee37807SMarius Strobl void mpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *);
2630e0521a1SMatt Jacob void mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *);
2647185cd36SMatt Jacob #else
2657185cd36SMatt Jacob #define	mpt2host_sge_simple_union(x)		do { ; } while (0)
2667185cd36SMatt Jacob #define	mpt2host_iocfacts_reply(x)		do { ; } while (0)
2677185cd36SMatt Jacob #define	mpt2host_portfacts_reply(x)		do { ; } while (0)
2687185cd36SMatt Jacob #define	mpt2host_config_page_ioc2(x)		do { ; } while (0)
2697ee37807SMarius Strobl #define	mpt2host_config_page_ioc3(x)		do { ; } while (0)
2707ee37807SMarius Strobl #define	mpt2host_config_page_scsi_port_0(x)	do { ; } while (0)
2717ee37807SMarius Strobl #define	mpt2host_config_page_scsi_port_1(x)	do { ; } while (0)
2727ee37807SMarius Strobl #define	host2mpt_config_page_scsi_port_1(x)	do { ; } while (0)
2737ee37807SMarius Strobl #define	mpt2host_config_page_scsi_port_2(x)	do { ; } while (0)
2747ee37807SMarius Strobl #define	mpt2host_config_page_scsi_device_0(x)	do { ; } while (0)
2757ee37807SMarius Strobl #define	mpt2host_config_page_scsi_device_1(x)	do { ; } while (0)
2767ee37807SMarius Strobl #define	host2mpt_config_page_scsi_device_1(x)	do { ; } while (0)
2777ee37807SMarius Strobl #define	mpt2host_config_page_fc_port_0(x)	do { ; } while (0)
2787ee37807SMarius Strobl #define	mpt2host_config_page_fc_port_1(x)	do { ; } while (0)
2797ee37807SMarius Strobl #define	host2mpt_config_page_fc_port_1(x)	do { ; } while (0)
2807185cd36SMatt Jacob #define	mpt2host_config_page_raid_vol_0(x)	do { ; } while (0)
2817ee37807SMarius Strobl #define	mpt2host_config_page_raid_phys_disk_0(x)			\
2827ee37807SMarius Strobl 	do { ; } while (0)
2837185cd36SMatt Jacob #define	mpt2host_mpi_raid_vol_indicator(x)	do { ; } while (0)
2840e0521a1SMatt Jacob #endif
285b0a2fdeeSScott Long 
286b0a2fdeeSScott Long /**************************** MPI Transaction State ***************************/
287b0a2fdeeSScott Long typedef enum {
2885e073106SMatt Jacob 	REQ_STATE_NIL		= 0x00,
2895e073106SMatt Jacob 	REQ_STATE_FREE		= 0x01,
2905e073106SMatt Jacob 	REQ_STATE_ALLOCATED	= 0x02,
2915e073106SMatt Jacob 	REQ_STATE_QUEUED	= 0x04,
2925e073106SMatt Jacob 	REQ_STATE_DONE		= 0x08,
2935e073106SMatt Jacob 	REQ_STATE_TIMEDOUT	= 0x10,
2945e073106SMatt Jacob 	REQ_STATE_NEED_WAKEUP	= 0x20,
2955e073106SMatt Jacob 	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
296b0a2fdeeSScott Long 	REQ_STATE_MASK		= 0xFF
297b0a2fdeeSScott Long } mpt_req_state_t;
298b0a2fdeeSScott Long 
299b0a2fdeeSScott Long struct req_entry {
300b0a2fdeeSScott Long 	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
301b0a2fdeeSScott Long 	mpt_req_state_t	state;		/* Request State Information */
302b0a2fdeeSScott Long 	uint16_t	index;		/* Index of this entry */
303b0a2fdeeSScott Long 	uint16_t	IOCStatus;	/* Completion status */
3045ab13afcSMarius Strobl 	uint16_t	ResponseCode;	/* TMF Response Code */
3055089bd63SMatt Jacob 	uint16_t	serno;		/* serial number */
306b0a2fdeeSScott Long 	union ccb      *ccb;		/* CAM request */
307b0a2fdeeSScott Long 	void	       *req_vbuf;	/* Virtual Address of Entry */
308b0a2fdeeSScott Long 	void	       *sense_vbuf;	/* Virtual Address of sense data */
309b0a2fdeeSScott Long 	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
310b0a2fdeeSScott Long 	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
311fcd9a16bSMatt Jacob 	bus_dmamap_t	dmap;		/* DMA map for data buffers */
312444dd2b6SMatt Jacob 	struct req_entry *chain;	/* for SGE overallocations */
313d0a68c27SMatt Jacob 	struct callout  callout;	/* Timeout for the request */
314b0a2fdeeSScott Long };
315b0a2fdeeSScott Long 
3161d558d6aSScott Long typedef struct mpt_config_params {
3171d558d6aSScott Long 	u_int		Action;
3181d558d6aSScott Long 	u_int		PageVersion;
3191d558d6aSScott Long 	u_int		PageLength;
3201d558d6aSScott Long 	u_int		PageNumber;
3211d558d6aSScott Long 	u_int		PageType;
3221d558d6aSScott Long 	u_int		PageAddress;
3231d558d6aSScott Long 	u_int		ExtPageLength;
3241d558d6aSScott Long 	u_int		ExtPageType;
3251d558d6aSScott Long } cfgparms_t;
3261d558d6aSScott Long 
327c87e3f83SMatt Jacob /**************************** MPI Target State Info ***************************/
328c87e3f83SMatt Jacob typedef struct {
329c87e3f83SMatt Jacob 	uint32_t reply_desc;	/* current reply descriptor */
330c87e3f83SMatt Jacob 	uint32_t bytes_xfered;	/* current relative offset */
331501b1685SAlexander Motin 	int resid;		/* current data residual */
332c87e3f83SMatt Jacob 	union ccb *ccb;		/* pointer to currently active ccb */
333c87e3f83SMatt Jacob 	request_t *req;		/* pointer to currently active assist request */
33473651fd1SMatt Jacob 	uint32_t
33573651fd1SMatt Jacob 		is_local : 1,
33673651fd1SMatt Jacob 		nxfers	 : 31;
33767445003SAlexander Motin 	uint32_t tag_id;	/* Our local tag. */
33867445003SAlexander Motin 	uint16_t itag;		/* Initiator tag. */
339c87e3f83SMatt Jacob 	enum {
340c87e3f83SMatt Jacob 		TGT_STATE_NIL,
3415e073106SMatt Jacob 		TGT_STATE_LOADING,
342c87e3f83SMatt Jacob 		TGT_STATE_LOADED,
343c87e3f83SMatt Jacob 		TGT_STATE_IN_CAM,
344c87e3f83SMatt Jacob                 TGT_STATE_SETTING_UP_FOR_DATA,
345c87e3f83SMatt Jacob                 TGT_STATE_MOVING_DATA,
346c87e3f83SMatt Jacob                 TGT_STATE_MOVING_DATA_AND_STATUS,
347c87e3f83SMatt Jacob                 TGT_STATE_SENDING_STATUS
348c87e3f83SMatt Jacob 	} state;
349c87e3f83SMatt Jacob } mpt_tgt_state_t;
350c87e3f83SMatt Jacob 
351c87e3f83SMatt Jacob /*
352c87e3f83SMatt Jacob  * When we get an incoming command it has its own tag which is called the
353c87e3f83SMatt Jacob  * IoIndex. This is the value we gave that particular command buffer when
354c87e3f83SMatt Jacob  * we originally assigned it. It's just a number, really. The FC card uses
355c87e3f83SMatt Jacob  * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
356c87e3f83SMatt Jacob  * contains pointers the request_t structures related to that IoIndex.
357c87e3f83SMatt Jacob  *
358c87e3f83SMatt Jacob  * What *we* do is construct a tag out of the index for the target command
359c87e3f83SMatt Jacob  * which owns the incoming ATIO plus a rolling sequence number.
360c87e3f83SMatt Jacob  */
361c87e3f83SMatt Jacob #define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
3625e073106SMatt Jacob  ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
363c87e3f83SMatt Jacob 
364c87e3f83SMatt Jacob #ifdef	INVARIANTS
365c87e3f83SMatt Jacob #define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
366c87e3f83SMatt Jacob #else
3675e073106SMatt Jacob #define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
368c87e3f83SMatt Jacob #endif
369c87e3f83SMatt Jacob 
370c87e3f83SMatt Jacob #define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
371c87e3f83SMatt Jacob     (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
372c87e3f83SMatt Jacob 
373c87e3f83SMatt Jacob STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
374c87e3f83SMatt Jacob #define	MPT_MAX_LUNS	256
375c87e3f83SMatt Jacob typedef struct {
376c87e3f83SMatt Jacob 	struct mpt_hdr_stailq	atios;
377c87e3f83SMatt Jacob 	struct mpt_hdr_stailq	inots;
378c87e3f83SMatt Jacob 	int enabled;
379c87e3f83SMatt Jacob } tgt_resource_t;
3805089bd63SMatt Jacob #define	MPT_MAX_ELS	64
381c87e3f83SMatt Jacob 
382b0a2fdeeSScott Long /**************************** Handler Registration ****************************/
383b0a2fdeeSScott Long /*
384b0a2fdeeSScott Long  * Global table of registered reply handlers.  The
385b0a2fdeeSScott Long  * handler is indicated by byte 3 of the request
386b0a2fdeeSScott Long  * index submitted to the IOC.  This allows the
387b0a2fdeeSScott Long  * driver core to perform generic processing without
388b0a2fdeeSScott Long  * any knowledge of per-personality behavior.
389b0a2fdeeSScott Long  *
390b0a2fdeeSScott Long  * MPT_NUM_REPLY_HANDLERS must be a power of 2
391b0a2fdeeSScott Long  * to allow the easy generation of a mask.
392b0a2fdeeSScott Long  *
393b0a2fdeeSScott Long  * The handler offsets used by the core are hard coded
394b0a2fdeeSScott Long  * allowing faster code generation when assigning a handler
395b0a2fdeeSScott Long  * to a request.  All "personalities" must use the
396b0a2fdeeSScott Long  * the handler registration mechanism.
397b0a2fdeeSScott Long  *
398b0a2fdeeSScott Long  * The IOC handlers that are rarely executed are placed
399b0a2fdeeSScott Long  * at the tail of the table to make it more likely that
400b0a2fdeeSScott Long  * all commonly executed handlers fit in a single cache
401b0a2fdeeSScott Long  * line.
402b0a2fdeeSScott Long  */
403c87e3f83SMatt Jacob #define MPT_NUM_REPLY_HANDLERS		(32)
404b0a2fdeeSScott Long #define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
405b0a2fdeeSScott Long #define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
406b0a2fdeeSScott Long #define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
407b0a2fdeeSScott Long typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
408c87e3f83SMatt Jacob     uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
409b0a2fdeeSScott Long typedef union {
410b0a2fdeeSScott Long 	mpt_reply_handler_t	*reply_handler;
411b0a2fdeeSScott Long } mpt_handler_t;
412b0a2fdeeSScott Long 
413b0a2fdeeSScott Long typedef enum {
414b0a2fdeeSScott Long 	MPT_HANDLER_REPLY,
415b0a2fdeeSScott Long 	MPT_HANDLER_EVENT,
416b0a2fdeeSScott Long 	MPT_HANDLER_RESET,
417b0a2fdeeSScott Long 	MPT_HANDLER_SHUTDOWN
418b0a2fdeeSScott Long } mpt_handler_type;
419b0a2fdeeSScott Long 
420b0a2fdeeSScott Long struct mpt_handler_record
421b0a2fdeeSScott Long {
422b0a2fdeeSScott Long 	LIST_ENTRY(mpt_handler_record)	links;
423b0a2fdeeSScott Long 	mpt_handler_t			handler;
424b0a2fdeeSScott Long };
425b0a2fdeeSScott Long 
426b0a2fdeeSScott Long LIST_HEAD(mpt_handler_list, mpt_handler_record);
4279b631363SMatt Jacob 
4289b631363SMatt Jacob /*
429b0a2fdeeSScott Long  * The handler_id is currently unused but would contain the
430b0a2fdeeSScott Long  * handler ID used in the MsgContext field to allow direction
431b0a2fdeeSScott Long  * of replies to the handler.  Registrations that don't require
432b0a2fdeeSScott Long  * a handler id can pass in NULL for the handler_id.
433b0a2fdeeSScott Long  *
434b0a2fdeeSScott Long  * Deregistrations for handlers without a handler id should
435b0a2fdeeSScott Long  * pass in MPT_HANDLER_ID_NONE.
4369b631363SMatt Jacob  */
437b0a2fdeeSScott Long #define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
438b0a2fdeeSScott Long int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
439b0a2fdeeSScott Long 			 mpt_handler_t, uint32_t *);
440b0a2fdeeSScott Long int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
441b0a2fdeeSScott Long 			   mpt_handler_t, uint32_t);
4429b631363SMatt Jacob 
443b0a2fdeeSScott Long /******************* Per-Controller Instance Data Structures ******************/
444b0a2fdeeSScott Long TAILQ_HEAD(req_queue, req_entry);
4459b631363SMatt Jacob 
446b0a2fdeeSScott Long /* Structure for saving proper values for modifyable PCI config registers */
447b0a2fdeeSScott Long struct mpt_pci_cfg {
448b0a2fdeeSScott Long 	uint16_t Command;
449b0a2fdeeSScott Long 	uint16_t LatencyTimer_LineSize;
450b0a2fdeeSScott Long 	uint32_t IO_BAR;
451b0a2fdeeSScott Long 	uint32_t Mem0_BAR[2];
452b0a2fdeeSScott Long 	uint32_t Mem1_BAR[2];
453b0a2fdeeSScott Long 	uint32_t ROM_BAR;
454b0a2fdeeSScott Long 	uint8_t  IntLine;
455b0a2fdeeSScott Long 	uint32_t PMCSR;
4569b631363SMatt Jacob };
4579b631363SMatt Jacob 
458b0a2fdeeSScott Long typedef enum {
459b0a2fdeeSScott Long 	MPT_RVF_NONE		= 0x0,
460b0a2fdeeSScott Long 	MPT_RVF_ACTIVE		= 0x1,
461b0a2fdeeSScott Long 	MPT_RVF_ANNOUNCED	= 0x2,
462b0a2fdeeSScott Long 	MPT_RVF_UP2DATE		= 0x4,
463b0a2fdeeSScott Long 	MPT_RVF_REFERENCED	= 0x8,
464b0a2fdeeSScott Long 	MPT_RVF_WCE_CHANGED	= 0x10
465b0a2fdeeSScott Long } mpt_raid_volume_flags;
466b0a2fdeeSScott Long 
467b0a2fdeeSScott Long struct mpt_raid_volume {
468b0a2fdeeSScott Long 	CONFIG_PAGE_RAID_VOL_0	       *config_page;
469b0a2fdeeSScott Long 	MPI_RAID_VOL_INDICATOR		sync_progress;
470b0a2fdeeSScott Long 	mpt_raid_volume_flags		flags;
4710e3b145eSMatt Jacob 	u_int				quiesced_disks;
4729b631363SMatt Jacob };
4739b631363SMatt Jacob 
474b0a2fdeeSScott Long typedef enum {
475b0a2fdeeSScott Long 	MPT_RDF_NONE		= 0x00,
476b0a2fdeeSScott Long 	MPT_RDF_ACTIVE		= 0x01,
477b0a2fdeeSScott Long 	MPT_RDF_ANNOUNCED	= 0x02,
478b0a2fdeeSScott Long 	MPT_RDF_UP2DATE		= 0x04,
479b0a2fdeeSScott Long 	MPT_RDF_REFERENCED	= 0x08,
480b0a2fdeeSScott Long 	MPT_RDF_QUIESCING	= 0x10,
481b0a2fdeeSScott Long 	MPT_RDF_QUIESCED	= 0x20
482b0a2fdeeSScott Long } mpt_raid_disk_flags;
4839b631363SMatt Jacob 
484b0a2fdeeSScott Long struct mpt_raid_disk {
485b0a2fdeeSScott Long 	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
486b0a2fdeeSScott Long 	struct mpt_raid_volume	       *volume;
487b0a2fdeeSScott Long 	u_int				member_number;
488b0a2fdeeSScott Long 	u_int				pass_thru_active;
489b0a2fdeeSScott Long 	mpt_raid_disk_flags		flags;
4909b631363SMatt Jacob };
4919b631363SMatt Jacob 
492b0a2fdeeSScott Long struct mpt_evtf_record {
493b0a2fdeeSScott Long 	MSG_EVENT_NOTIFY_REPLY		reply;
494b0a2fdeeSScott Long 	uint32_t			context;
495b0a2fdeeSScott Long 	LIST_ENTRY(mpt_evtf_record)	links;
4969b631363SMatt Jacob };
4979b631363SMatt Jacob 
498b0a2fdeeSScott Long LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
4999b631363SMatt Jacob 
5001d558d6aSScott Long struct mptsas_devinfo {
5011d558d6aSScott Long 	uint16_t	dev_handle;
5021d558d6aSScott Long 	uint16_t	parent_dev_handle;
5031d558d6aSScott Long 	uint16_t	enclosure_handle;
5041d558d6aSScott Long 	uint16_t	slot;
5051d558d6aSScott Long 	uint8_t		phy_num;
5061d558d6aSScott Long 	uint8_t		physical_port;
5071d558d6aSScott Long 	uint8_t		target_id;
5081d558d6aSScott Long 	uint8_t		bus;
5091d558d6aSScott Long 	uint64_t	sas_address;
5101d558d6aSScott Long 	uint32_t	device_info;
5111d558d6aSScott Long };
5121d558d6aSScott Long 
5131d558d6aSScott Long struct mptsas_phyinfo {
5141d558d6aSScott Long 	uint16_t	handle;
5151d558d6aSScott Long 	uint8_t		phy_num;
5161d558d6aSScott Long 	uint8_t		port_id;
5171d558d6aSScott Long 	uint8_t		negotiated_link_rate;
5181d558d6aSScott Long 	uint8_t		hw_link_rate;
5191d558d6aSScott Long 	uint8_t		programmed_link_rate;
5201d558d6aSScott Long 	uint8_t		sas_port_add_phy;
5211d558d6aSScott Long 	struct mptsas_devinfo identify;
5221d558d6aSScott Long 	struct mptsas_devinfo attached;
5231d558d6aSScott Long };
5241d558d6aSScott Long 
5251d558d6aSScott Long struct mptsas_portinfo {
5261d558d6aSScott Long 	uint16_t			num_phys;
5271d558d6aSScott Long 	struct mptsas_phyinfo		*phy_info;
5281d558d6aSScott Long };
5291d558d6aSScott Long 
530b0a2fdeeSScott Long struct mpt_softc {
531b0a2fdeeSScott Long 	device_t		dev;
532b0a2fdeeSScott Long 	struct mtx		mpt_lock;
5335089bd63SMatt Jacob 	int			mpt_locksetup;
534b0a2fdeeSScott Long 	uint32_t		mpt_pers_mask;
535b4c618c0SMatt Jacob 	uint32_t
53604016bcfSMarius Strobl 				: 7,
537c87e3f83SMatt Jacob 		unit		: 8,
538a2baed97SMatt Jacob 		ready		: 1,
5390e0521a1SMatt Jacob 		fw_uploaded	: 1,
540804625eeSMatt Jacob 		msi_enable	: 1,
541c87e3f83SMatt Jacob 		twildcard	: 1,
542c87e3f83SMatt Jacob 		tenabled	: 1,
543b4c618c0SMatt Jacob 		do_cfg_role	: 1,
5441d79ca0eSMatt Jacob 		raid_enabled	: 1,
545b0a2fdeeSScott Long 		raid_mwce_set	: 1,
546b0a2fdeeSScott Long 		getreqwaiter	: 1,
547b0a2fdeeSScott Long 		shutdwn_raid    : 1,
548b0a2fdeeSScott Long 		shutdwn_recovery: 1,
549b0a2fdeeSScott Long 		outofbeer	: 1,
550b0a2fdeeSScott Long 		disabled	: 1,
5515580ce96SMatt Jacob 		is_spi		: 1,
552c87e3f83SMatt Jacob 		is_sas		: 1,
55304016bcfSMarius Strobl 		is_fc		: 1,
55404016bcfSMarius Strobl 		is_1078		: 1;
5559b631363SMatt Jacob 
556b4c618c0SMatt Jacob 	u_int			cfg_role;
557b4c618c0SMatt Jacob 	u_int			role;	/* role: none, ini, target, both */
558b4c618c0SMatt Jacob 
559b0a2fdeeSScott Long 	u_int			verbose;
560a2baed97SMatt Jacob #ifdef	MPT_TEST_MULTIPATH
561a2baed97SMatt Jacob 	int			failure_id;
562a2baed97SMatt Jacob #endif
5639b631363SMatt Jacob 
564b0a2fdeeSScott Long 	/*
565b0a2fdeeSScott Long 	 * IOC Facts
566b0a2fdeeSScott Long 	 */
5670e0521a1SMatt Jacob 	MSG_IOC_FACTS_REPLY	ioc_facts;
5689b631363SMatt Jacob 
569b0a2fdeeSScott Long 	/*
570b0a2fdeeSScott Long 	 * Port Facts
571b0a2fdeeSScott Long 	 */
5720e0521a1SMatt Jacob 	MSG_PORT_FACTS_REPLY *	port_facts;
5730e0521a1SMatt Jacob #define	mpt_max_tgtcmds	port_facts[0].MaxPostedCmdBuffers
5749b631363SMatt Jacob 
575b0a2fdeeSScott Long 	/*
576b0a2fdeeSScott Long 	 * Device Configuration Information
577b0a2fdeeSScott Long 	 */
578b0a2fdeeSScott Long 	union {
579b0a2fdeeSScott Long 		struct mpt_spi_cfg {
580b0a2fdeeSScott Long 			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
581b0a2fdeeSScott Long 			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
582b0a2fdeeSScott Long 			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
583b0a2fdeeSScott Long 			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
584b0a2fdeeSScott Long 			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
5858ba5efbdSMarius Strobl 			int				_ini_id;
586b0a2fdeeSScott Long 			uint16_t			_tag_enable;
587b0a2fdeeSScott Long 			uint16_t			_disc_enable;
588b0a2fdeeSScott Long 		} spi;
589b0a2fdeeSScott Long #define	mpt_port_page0		cfg.spi._port_page0
590b0a2fdeeSScott Long #define	mpt_port_page1		cfg.spi._port_page1
591b0a2fdeeSScott Long #define	mpt_port_page2		cfg.spi._port_page2
592b0a2fdeeSScott Long #define	mpt_dev_page0		cfg.spi._dev_page0
593b0a2fdeeSScott Long #define	mpt_dev_page1		cfg.spi._dev_page1
5948ba5efbdSMarius Strobl #define	mpt_ini_id		cfg.spi._ini_id
595b0a2fdeeSScott Long #define	mpt_tag_enable		cfg.spi._tag_enable
596b0a2fdeeSScott Long #define	mpt_disc_enable		cfg.spi._disc_enable
597b0a2fdeeSScott Long 		struct mpi_fc_cfg {
598c87e3f83SMatt Jacob 			CONFIG_PAGE_FC_PORT_0 _port_page0;
5999fe6d254SMatt Jacob 			uint32_t _port_speed;
600c87e3f83SMatt Jacob #define	mpt_fcport_page0	cfg.fc._port_page0
6019fe6d254SMatt Jacob #define	mpt_fcport_speed	cfg.fc._port_speed
602b0a2fdeeSScott Long 		} fc;
603b0a2fdeeSScott Long 	} cfg;
604970043d7SMatt Jacob 	/*
605970043d7SMatt Jacob 	 * Device config information stored up for sysctl to access
606970043d7SMatt Jacob 	 */
607970043d7SMatt Jacob 	union {
608970043d7SMatt Jacob 		struct {
609970043d7SMatt Jacob 			unsigned int initiator_id;
610970043d7SMatt Jacob 		} spi;
611970043d7SMatt Jacob 		struct {
612555dbebfSAlexander Motin 			uint64_t wwnn;
613555dbebfSAlexander Motin 			uint64_t wwpn;
614471f963dSAlexander Motin 			uint32_t portid;
615970043d7SMatt Jacob 		} fc;
616970043d7SMatt Jacob 	} scinfo;
617b0a2fdeeSScott Long 
6181d79ca0eSMatt Jacob 	/* Controller Info for RAID information */
619b0a2fdeeSScott Long 	CONFIG_PAGE_IOC_2 *	ioc_page2;
620b0a2fdeeSScott Long 	CONFIG_PAGE_IOC_3 *	ioc_page3;
621b0a2fdeeSScott Long 
622b0a2fdeeSScott Long 	/* Raid Data */
623b0a2fdeeSScott Long 	struct mpt_raid_volume* raid_volumes;
624b0a2fdeeSScott Long 	struct mpt_raid_disk*	raid_disks;
625b0a2fdeeSScott Long 	u_int			raid_max_volumes;
626b0a2fdeeSScott Long 	u_int			raid_max_disks;
627b0a2fdeeSScott Long 	u_int			raid_page0_len;
628b0a2fdeeSScott Long 	u_int			raid_wakeup;
629b0a2fdeeSScott Long 	u_int			raid_rescan;
630b0a2fdeeSScott Long 	u_int			raid_resync_rate;
631b0a2fdeeSScott Long 	u_int			raid_mwce_setting;
632b0a2fdeeSScott Long 	u_int			raid_queue_depth;
6335cc0208aSAlexander Kabaev 	u_int			raid_nonopt_volumes;
634b0a2fdeeSScott Long 	struct proc	       *raid_thread;
635b0a2fdeeSScott Long 	struct callout		raid_timer;
636b0a2fdeeSScott Long 
637b0a2fdeeSScott Long 	/*
638b0a2fdeeSScott Long 	 * PCI Hardware info
639b0a2fdeeSScott Long 	 */
640b0a2fdeeSScott Long 	struct resource *	pci_irq;	/* Interrupt map for chip */
6415ab13afcSMarius Strobl 	void *			ih;		/* Interrupt handle */
642fd61f421SMarius Strobl #if 0
643b0a2fdeeSScott Long 	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
644fd61f421SMarius Strobl #endif
645b0a2fdeeSScott Long 
646b0a2fdeeSScott Long 	/*
647b0a2fdeeSScott Long 	 * DMA Mapping Stuff
648b0a2fdeeSScott Long 	 */
649b0a2fdeeSScott Long 	struct resource *	pci_reg;	/* Register map for chip */
650b0a2fdeeSScott Long 	bus_space_tag_t		pci_st;		/* Bus tag for registers */
651b0a2fdeeSScott Long 	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
652b0a2fdeeSScott Long 	/* PIO versions of above. */
653b0a2fdeeSScott Long 	struct resource *	pci_pio_reg;
654b0a2fdeeSScott Long 	bus_space_tag_t		pci_pio_st;
655b0a2fdeeSScott Long 	bus_space_handle_t	pci_pio_sh;
656b0a2fdeeSScott Long 
657b0a2fdeeSScott Long 	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
658b0a2fdeeSScott Long 	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
659b0a2fdeeSScott Long 	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
660b0a2fdeeSScott Long 	uint8_t		       *reply;		/* KVA of reply memory */
661b0a2fdeeSScott Long 	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
662b0a2fdeeSScott Long 
663b0a2fdeeSScott Long 	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
664453130d9SPedro F. Giffuni 	bus_dma_tag_t		request_dmat;	/* DMA tag for request memory */
665453130d9SPedro F. Giffuni 	bus_dmamap_t		request_dmap;	/* DMA map for request memory */
666b0a2fdeeSScott Long 	uint8_t		       *request;	/* KVA of Request memory */
667c87e3f83SMatt Jacob 	bus_addr_t		request_phys;	/* BusAddr of request memory */
668b0a2fdeeSScott Long 
669444dd2b6SMatt Jacob 	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
670cd853791SKonstantin Belousov 	uint32_t		max_cam_seg_cnt;/* calculated from maxphys */
671444dd2b6SMatt Jacob 
672444dd2b6SMatt Jacob 	/*
673444dd2b6SMatt Jacob 	 * Hardware management
674444dd2b6SMatt Jacob 	 */
675b0a2fdeeSScott Long 	u_int			reset_cnt;
676b0a2fdeeSScott Long 
677b0a2fdeeSScott Long 	/*
678b0a2fdeeSScott Long 	 * CAM && Software Management
679b0a2fdeeSScott Long 	 */
680b0a2fdeeSScott Long 	request_t	       *request_pool;
681b0a2fdeeSScott Long 	struct req_queue	request_free_list;
682b0a2fdeeSScott Long 	struct req_queue	request_pending_list;
683b0a2fdeeSScott Long 	struct req_queue	request_timeout_list;
684b0a2fdeeSScott Long 
685b0a2fdeeSScott Long 	struct cam_sim	       *sim;
686b0a2fdeeSScott Long 	struct cam_path	       *path;
687b0a2fdeeSScott Long 
688b0a2fdeeSScott Long 	struct cam_sim	       *phydisk_sim;
689b0a2fdeeSScott Long 	struct cam_path	       *phydisk_path;
690b0a2fdeeSScott Long 
691b0a2fdeeSScott Long 	struct proc	       *recovery_thread;
692b0a2fdeeSScott Long 	request_t	       *tmf_req;
693b0a2fdeeSScott Long 
694c87e3f83SMatt Jacob 	/*
6955e073106SMatt Jacob 	 * Deferred frame acks due to resource shortage.
6965e073106SMatt Jacob 	 */
6975e073106SMatt Jacob 	struct mpt_evtf_list	ack_frames;
698fcd9a16bSMatt Jacob 
6995e073106SMatt Jacob 	/*
700c87e3f83SMatt Jacob 	 * Target Mode Support
701c87e3f83SMatt Jacob 	 */
702c87e3f83SMatt Jacob 	uint32_t		scsi_tgt_handler_id;
703c87e3f83SMatt Jacob 	request_t **		tgt_cmd_ptrs;
7045089bd63SMatt Jacob 	request_t **		els_cmd_ptrs;	/* FC only */
705c87e3f83SMatt Jacob 
706c87e3f83SMatt Jacob 	/*
707c87e3f83SMatt Jacob 	 * *snork*- this is chosen to be here *just in case* somebody
708c87e3f83SMatt Jacob 	 * forgets to point to it exactly and we index off of trt with
709c87e3f83SMatt Jacob 	 * CAM_LUN_WILDCARD.
710c87e3f83SMatt Jacob 	 */
711c87e3f83SMatt Jacob 	tgt_resource_t		trt_wildcard;		/* wildcard luns */
712c87e3f83SMatt Jacob 	tgt_resource_t		trt[MPT_MAX_LUNS];
713c87e3f83SMatt Jacob 	uint16_t		tgt_cmds_allocated;
7145089bd63SMatt Jacob 	uint16_t		els_cmds_allocated;	/* FC only */
715c87e3f83SMatt Jacob 
716c87e3f83SMatt Jacob 	uint16_t		timeouts;	/* timeout count */
717c87e3f83SMatt Jacob 	uint16_t		success;	/* successes afer timeout */
7185089bd63SMatt Jacob 	uint16_t		sequence;	/* Sequence Number */
7195089bd63SMatt Jacob 	uint16_t		pad3;
720c87e3f83SMatt Jacob 
721e6ebfc7cSMarius Strobl #if 0
722fcd9a16bSMatt Jacob 	/* Paired port in some dual adapters configurations */
723b0a2fdeeSScott Long 	struct mpt_softc *	mpt2;
724e6ebfc7cSMarius Strobl #endif
725b0a2fdeeSScott Long 
726b0a2fdeeSScott Long 	/* FW Image management */
727b0a2fdeeSScott Long 	uint32_t		fw_image_size;
728b0a2fdeeSScott Long 	uint8_t		       *fw_image;
729b0a2fdeeSScott Long 	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
730b0a2fdeeSScott Long 	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
7315580ce96SMatt Jacob 	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
732b0a2fdeeSScott Long 
7331d558d6aSScott Long 	/* SAS Topology */
7341d558d6aSScott Long 	struct mptsas_portinfo	*sas_portinfo;
7351d558d6aSScott Long 
736b0a2fdeeSScott Long 	/* Shutdown Event Handler. */
737b0a2fdeeSScott Long 	eventhandler_tag         eh;
738b0a2fdeeSScott Long 
739ee98c4a5SJohn Baldwin 	/* Userland management interface. */
740ee98c4a5SJohn Baldwin 	struct cdev		*cdev;
741ee98c4a5SJohn Baldwin 
742b0a2fdeeSScott Long 	TAILQ_ENTRY(mpt_softc)	links;
743b0a2fdeeSScott Long };
744b0a2fdeeSScott Long 
7455089bd63SMatt Jacob static __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
7465089bd63SMatt Jacob 
7475089bd63SMatt Jacob static __inline void
mpt_assign_serno(struct mpt_softc * mpt,request_t * req)7485089bd63SMatt Jacob mpt_assign_serno(struct mpt_softc *mpt, request_t *req)
7495089bd63SMatt Jacob {
7505089bd63SMatt Jacob 	if ((req->serno = mpt->sequence++) == 0) {
7515089bd63SMatt Jacob 		req->serno = mpt->sequence++;
7525089bd63SMatt Jacob 	}
7535089bd63SMatt Jacob }
7545089bd63SMatt Jacob 
755c87e3f83SMatt Jacob /***************************** Locking Primitives *****************************/
756b0a2fdeeSScott Long #define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
757b0a2fdeeSScott Long #define	MPT_LOCK_SETUP(mpt)						\
758b0a2fdeeSScott Long 		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
759b0a2fdeeSScott Long 		mpt->mpt_locksetup = 1
760b0a2fdeeSScott Long #define	MPT_LOCK_DESTROY(mpt)						\
761b0a2fdeeSScott Long 	if (mpt->mpt_locksetup) {					\
762b0a2fdeeSScott Long 		mtx_destroy(&mpt->mpt_lock);				\
763b0a2fdeeSScott Long 		mpt->mpt_locksetup = 0;					\
764b0a2fdeeSScott Long 	}
765b0a2fdeeSScott Long 
766b0a2fdeeSScott Long #define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
767b0a2fdeeSScott Long #define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
7685089bd63SMatt Jacob #define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
769d0a68c27SMatt Jacob #define	MPT_LOCK_ASSERT(mpt)	mtx_assert(&(mpt)->mpt_lock, MA_OWNED)
77085c9dd9dSSteven Hartland #define mpt_sleep(mpt, ident, priority, wmesg, sbt) \
77185c9dd9dSSteven Hartland     msleep_sbt(ident, &(mpt)->mpt_lock, priority, wmesg, sbt, 0, 0)
77285c9dd9dSSteven Hartland #define mpt_req_timeout(req, sbt, func, arg) \
77385c9dd9dSSteven Hartland     callout_reset_sbt(&(req)->callout, (sbt), 0, (func), (arg), 0)
774d0a68c27SMatt Jacob #define mpt_req_untimeout(req, func, arg) \
775d0a68c27SMatt Jacob 	callout_stop(&(req)->callout)
776363b8ed7SAlexander Kabaev #define mpt_callout_init(mpt, c) \
777363b8ed7SAlexander Kabaev 	callout_init_mtx(c, &(mpt)->mpt_lock, 0)
778363b8ed7SAlexander Kabaev #define mpt_callout_drain(mpt, c) \
779363b8ed7SAlexander Kabaev 	callout_drain(c)
7805089bd63SMatt Jacob 
781b0a2fdeeSScott Long /******************************* Register Access ******************************/
782b0a2fdeeSScott Long static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
783e18e2adaSMarius Strobl static __inline void mpt_write_stream(struct mpt_softc *, size_t, uint32_t);
784b0a2fdeeSScott Long static __inline uint32_t mpt_read(struct mpt_softc *, int);
785b0a2fdeeSScott Long static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
786b0a2fdeeSScott Long static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
787b0a2fdeeSScott Long 
788b0a2fdeeSScott Long static __inline void
mpt_write(struct mpt_softc * mpt,size_t offset,uint32_t val)789b0a2fdeeSScott Long mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
790b0a2fdeeSScott Long {
791b0a2fdeeSScott Long 	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
792b0a2fdeeSScott Long }
793b0a2fdeeSScott Long 
794e18e2adaSMarius Strobl static __inline void
mpt_write_stream(struct mpt_softc * mpt,size_t offset,uint32_t val)795e18e2adaSMarius Strobl mpt_write_stream(struct mpt_softc *mpt, size_t offset, uint32_t val)
796e18e2adaSMarius Strobl {
797e18e2adaSMarius Strobl 	bus_space_write_stream_4(mpt->pci_st, mpt->pci_sh, offset, val);
798e18e2adaSMarius Strobl }
799e18e2adaSMarius Strobl 
800b0a2fdeeSScott Long static __inline uint32_t
mpt_read(struct mpt_softc * mpt,int offset)801b0a2fdeeSScott Long mpt_read(struct mpt_softc *mpt, int offset)
802b0a2fdeeSScott Long {
803b0a2fdeeSScott Long 	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
804b0a2fdeeSScott Long }
805b0a2fdeeSScott Long 
806b0a2fdeeSScott Long /*
807a37c6fccSGordon Bergling  * Some operations (e.g. diagnostic register writes while the ARM processor
808b0a2fdeeSScott Long  * is disabled), must be performed using "PCI pio" operations.  On non-PCI
809db4fcadfSConrad Meyer  * buses, these operations likely map to normal register accesses.
810b0a2fdeeSScott Long  */
811b0a2fdeeSScott Long static __inline void
mpt_pio_write(struct mpt_softc * mpt,size_t offset,uint32_t val)812b0a2fdeeSScott Long mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
813b0a2fdeeSScott Long {
81404016bcfSMarius Strobl 	KASSERT(mpt->pci_pio_reg != NULL, ("no PIO resource"));
815b0a2fdeeSScott Long 	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
816b0a2fdeeSScott Long }
817b0a2fdeeSScott Long 
818b0a2fdeeSScott Long static __inline uint32_t
mpt_pio_read(struct mpt_softc * mpt,int offset)819b0a2fdeeSScott Long mpt_pio_read(struct mpt_softc *mpt, int offset)
820b0a2fdeeSScott Long {
82104016bcfSMarius Strobl 	KASSERT(mpt->pci_pio_reg != NULL, ("no PIO resource"));
822b0a2fdeeSScott Long 	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
823b0a2fdeeSScott Long }
824e18e2adaSMarius Strobl 
825b0a2fdeeSScott Long /*********************** Reply Frame/Request Management ***********************/
826b0a2fdeeSScott Long /* Max MPT Reply we are willing to accept (must be power of 2) */
827444dd2b6SMatt Jacob #define MPT_REPLY_SIZE   	256
828b0a2fdeeSScott Long 
829c87e3f83SMatt Jacob /*
830c87e3f83SMatt Jacob  * Must be less than 16384 in order for target mode to work
831c87e3f83SMatt Jacob  */
832444dd2b6SMatt Jacob #define MPT_MAX_REQUESTS(mpt)	512
833b0a2fdeeSScott Long #define MPT_REQUEST_AREA	512
834444dd2b6SMatt Jacob #define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
835b0a2fdeeSScott Long #define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
836b0a2fdeeSScott Long 
8375e073106SMatt Jacob #define MPT_CONTEXT_CB_SHIFT	(16)
838b0a2fdeeSScott Long #define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
839b0a2fdeeSScott Long #define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
840b0a2fdeeSScott Long #define MPT_CONTEXT_TO_CBI(x)	\
841b0a2fdeeSScott Long     (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
8425e073106SMatt Jacob #define MPT_CONTEXT_REQI_MASK	0xFFFF
843c87e3f83SMatt Jacob #define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
844b0a2fdeeSScott Long 
845b0a2fdeeSScott Long /*
846b0a2fdeeSScott Long  * Convert a 32bit physical address returned from IOC to an
847b0a2fdeeSScott Long  * offset into our reply frame memory or the kvm address needed
848b0a2fdeeSScott Long  * to access the data.  The returned address is only the low
849b0a2fdeeSScott Long  * 32 bits, so mask our base physical address accordingly.
850b0a2fdeeSScott Long  */
851b0a2fdeeSScott Long #define MPT_REPLY_BADDR(x)		\
852b0a2fdeeSScott Long 	(x << 1)
853b0a2fdeeSScott Long #define MPT_REPLY_OTOV(m, i) 		\
854b0a2fdeeSScott Long 	((void *)(&m->reply[i]))
855b0a2fdeeSScott Long 
856b0a2fdeeSScott Long #define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
857b0a2fdeeSScott Long do {							\
8581d79ca0eSMatt Jacob 	if (mpt->verbose > MPT_PRT_DEBUG)		\
859b0a2fdeeSScott Long 		mpt_dump_reply_frame(mpt, reply_frame);	\
860b0a2fdeeSScott Long } while(0)
861b0a2fdeeSScott Long 
862b0a2fdeeSScott Long static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
863b0a2fdeeSScott Long static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
864b0a2fdeeSScott Long 
865b0a2fdeeSScott Long /*
866b0a2fdeeSScott Long  * Give the reply buffer back to the IOC after we have
867b0a2fdeeSScott Long  * finished processing it.
868b0a2fdeeSScott Long  */
869b0a2fdeeSScott Long static __inline void
mpt_free_reply(struct mpt_softc * mpt,uint32_t ptr)870b0a2fdeeSScott Long mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
871b0a2fdeeSScott Long {
872b0a2fdeeSScott Long      mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
873b0a2fdeeSScott Long }
874b0a2fdeeSScott Long 
875b0a2fdeeSScott Long /* Get a reply from the IOC */
876b0a2fdeeSScott Long static __inline uint32_t
mpt_pop_reply_queue(struct mpt_softc * mpt)877b0a2fdeeSScott Long mpt_pop_reply_queue(struct mpt_softc *mpt)
878b0a2fdeeSScott Long {
879b0a2fdeeSScott Long      return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
880b0a2fdeeSScott Long }
881b0a2fdeeSScott Long 
8825e073106SMatt Jacob void
8835e073106SMatt Jacob mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
8845e073106SMatt Jacob 
8855ab13afcSMarius Strobl /************************** Scatter Gather Management **************************/
886444dd2b6SMatt Jacob /* MPT_RQSL- size of request frame, in bytes */
8870e0521a1SMatt Jacob #define	MPT_RQSL(mpt)		(mpt->ioc_facts.RequestFrameSize << 2)
888b0a2fdeeSScott Long 
889444dd2b6SMatt Jacob /* MPT_NSGL- how many SG entries can fit in a request frame size */
890444dd2b6SMatt Jacob #define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
891444dd2b6SMatt Jacob 
892444dd2b6SMatt Jacob /* MPT_NRFM- how many request frames can fit in each request alloc we make */
893444dd2b6SMatt Jacob #define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
894444dd2b6SMatt Jacob 
895444dd2b6SMatt Jacob /*
896444dd2b6SMatt Jacob  * MPT_NSGL_FIRST- # of SG elements that can fit after
897444dd2b6SMatt Jacob  * an I/O request but still within the request frame.
898444dd2b6SMatt Jacob  * Do this safely based upon SGE_IO_UNION.
899444dd2b6SMatt Jacob  *
900444dd2b6SMatt Jacob  * Note that the first element is *within* the SCSI request.
901444dd2b6SMatt Jacob  */
902b0a2fdeeSScott Long #define	MPT_NSGL_FIRST(mpt)	\
903444dd2b6SMatt Jacob     ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
904444dd2b6SMatt Jacob     sizeof (SGE_IO_UNION))
905b0a2fdeeSScott Long 
906b0a2fdeeSScott Long /***************************** IOC Initialization *****************************/
907b0a2fdeeSScott Long int mpt_reset(struct mpt_softc *, int /*reinit*/);
908b0a2fdeeSScott Long 
909c87e3f83SMatt Jacob /****************************** Debugging ************************************/
910b4c618c0SMatt Jacob void mpt_dump_data(struct mpt_softc *, const char *, void *, int);
9110e3b145eSMatt Jacob void mpt_dump_request(struct mpt_softc *, request_t *);
9120e3b145eSMatt Jacob 
913b0a2fdeeSScott Long enum {
914b0a2fdeeSScott Long 	MPT_PRT_ALWAYS,
915b0a2fdeeSScott Long 	MPT_PRT_FATAL,
916b0a2fdeeSScott Long 	MPT_PRT_ERROR,
917b0a2fdeeSScott Long 	MPT_PRT_WARN,
918b0a2fdeeSScott Long 	MPT_PRT_INFO,
919ac219b98SMatt Jacob 	MPT_PRT_NEGOTIATION,
920b0a2fdeeSScott Long 	MPT_PRT_DEBUG,
921c87e3f83SMatt Jacob 	MPT_PRT_DEBUG1,
922c87e3f83SMatt Jacob 	MPT_PRT_DEBUG2,
923c87e3f83SMatt Jacob 	MPT_PRT_DEBUG3,
924444dd2b6SMatt Jacob 	MPT_PRT_TRACE,
925444dd2b6SMatt Jacob 	MPT_PRT_NONE=100
926b0a2fdeeSScott Long };
927b0a2fdeeSScott Long 
928b0a2fdeeSScott Long #define mpt_lprt(mpt, level, ...)		\
929b0a2fdeeSScott Long do {						\
9305a62cbcaSRyan Libby 	if ((level) <= (mpt)->verbose)		\
931b0a2fdeeSScott Long 		mpt_prt(mpt, __VA_ARGS__);	\
932b0a2fdeeSScott Long } while (0)
933b0a2fdeeSScott Long 
93487e255acSMarius Strobl #if 0
935b0a2fdeeSScott Long #define mpt_lprtc(mpt, level, ...)		\
936b0a2fdeeSScott Long do {						\
9375a62cbcaSRyan Libby 	if ((level) <= (mpt)->verbose)		\
938b0a2fdeeSScott Long 		mpt_prtc(mpt, __VA_ARGS__);	\
939b0a2fdeeSScott Long } while (0)
94087e255acSMarius Strobl #endif
9414b847ffaSMarius Strobl 
942c87e3f83SMatt Jacob void mpt_prt(struct mpt_softc *, const char *, ...)
943c87e3f83SMatt Jacob 	__printflike(2, 3);
944c87e3f83SMatt Jacob void mpt_prtc(struct mpt_softc *, const char *, ...)
945c87e3f83SMatt Jacob 	__printflike(2, 3);
946b0a2fdeeSScott Long 
947c87e3f83SMatt Jacob /**************************** Target Mode Related ***************************/
948c87e3f83SMatt Jacob #ifdef	INVARIANTS
949c87e3f83SMatt Jacob static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
950c87e3f83SMatt Jacob static __inline request_t *
mpt_tag_2_req(struct mpt_softc * mpt,uint32_t tag)951c87e3f83SMatt Jacob mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
952c87e3f83SMatt Jacob {
9535e073106SMatt Jacob 	uint16_t rtg = (tag >> 18);
9544349d278SMarius Strobl 	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d", tag));
955c87e3f83SMatt Jacob 	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
956c87e3f83SMatt Jacob 	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
957c87e3f83SMatt Jacob 	return (mpt->tgt_cmd_ptrs[rtg]);
958c87e3f83SMatt Jacob }
959cd23a6e0SMarius Strobl #endif
9605089bd63SMatt Jacob 
9615089bd63SMatt Jacob static __inline int
9625089bd63SMatt Jacob mpt_req_on_free_list(struct mpt_softc *, request_t *);
9635089bd63SMatt Jacob static __inline int
9645089bd63SMatt Jacob mpt_req_on_pending_list(struct mpt_softc *, request_t *);
9655089bd63SMatt Jacob 
9665089bd63SMatt Jacob /*
9675089bd63SMatt Jacob  * Is request on freelist?
9685089bd63SMatt Jacob  */
9695089bd63SMatt Jacob static __inline int
mpt_req_on_free_list(struct mpt_softc * mpt,request_t * req)9705089bd63SMatt Jacob mpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
9715089bd63SMatt Jacob {
9725089bd63SMatt Jacob 	request_t *lrq;
9735089bd63SMatt Jacob 
9745089bd63SMatt Jacob 	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
9755089bd63SMatt Jacob 		if (lrq == req) {
9765089bd63SMatt Jacob 			return (1);
9775089bd63SMatt Jacob 		}
9785089bd63SMatt Jacob 	}
9795089bd63SMatt Jacob 	return (0);
9805089bd63SMatt Jacob }
9815089bd63SMatt Jacob 
9825089bd63SMatt Jacob /*
9835089bd63SMatt Jacob  * Is request on pending list?
9845089bd63SMatt Jacob  */
9855089bd63SMatt Jacob static __inline int
mpt_req_on_pending_list(struct mpt_softc * mpt,request_t * req)9865089bd63SMatt Jacob mpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
9875089bd63SMatt Jacob {
9885089bd63SMatt Jacob 	request_t *lrq;
9895089bd63SMatt Jacob 
9905089bd63SMatt Jacob 	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
9915089bd63SMatt Jacob 		if (lrq == req) {
9925089bd63SMatt Jacob 			return (1);
9935089bd63SMatt Jacob 		}
9945089bd63SMatt Jacob 	}
9955089bd63SMatt Jacob 	return (0);
9965089bd63SMatt Jacob }
9975089bd63SMatt Jacob 
998cd23a6e0SMarius Strobl #ifdef	INVARIANTS
999cd23a6e0SMarius Strobl static __inline void
1000cd23a6e0SMarius Strobl mpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1001cd23a6e0SMarius Strobl static __inline void
1002cd23a6e0SMarius Strobl mpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1003cd23a6e0SMarius Strobl 
10045089bd63SMatt Jacob /*
10055089bd63SMatt Jacob  * Make sure that req *is* part of one of the special lists
10065089bd63SMatt Jacob  */
10075089bd63SMatt Jacob static __inline void
mpt_req_spcl(struct mpt_softc * mpt,request_t * req,const char * s,int line)10085089bd63SMatt Jacob mpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
10095089bd63SMatt Jacob {
10105089bd63SMatt Jacob 	int i;
10115089bd63SMatt Jacob 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
10125089bd63SMatt Jacob 		if (req == mpt->els_cmd_ptrs[i]) {
10135089bd63SMatt Jacob 			return;
10145089bd63SMatt Jacob 		}
10155089bd63SMatt Jacob 	}
10165089bd63SMatt Jacob 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
10175089bd63SMatt Jacob 		if (req == mpt->tgt_cmd_ptrs[i]) {
10185089bd63SMatt Jacob 			return;
10195089bd63SMatt Jacob 		}
10205089bd63SMatt Jacob 	}
10214349d278SMarius Strobl 	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs",
10225089bd63SMatt Jacob 	    s, line, req, req->serno,
10235089bd63SMatt Jacob 	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
10245089bd63SMatt Jacob }
10255089bd63SMatt Jacob 
10265089bd63SMatt Jacob /*
10275089bd63SMatt Jacob  * Make sure that req is *not* part of one of the special lists.
10285089bd63SMatt Jacob  */
10295089bd63SMatt Jacob static __inline void
mpt_req_not_spcl(struct mpt_softc * mpt,request_t * req,const char * s,int line)10305089bd63SMatt Jacob mpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
10315089bd63SMatt Jacob {
10325089bd63SMatt Jacob 	int i;
10335089bd63SMatt Jacob 	for (i = 0; i < mpt->els_cmds_allocated; i++) {
10345089bd63SMatt Jacob 		KASSERT(req != mpt->els_cmd_ptrs[i],
10354349d278SMarius Strobl 		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d",
10365089bd63SMatt Jacob 		    s, line, req, req->serno,
10375089bd63SMatt Jacob 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
10385089bd63SMatt Jacob 	}
10395089bd63SMatt Jacob 	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
10405089bd63SMatt Jacob 		KASSERT(req != mpt->tgt_cmd_ptrs[i],
10414349d278SMarius Strobl 		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d",
10425089bd63SMatt Jacob 		    s, line, req, req->serno,
10435089bd63SMatt Jacob 		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
10445089bd63SMatt Jacob 	}
10455089bd63SMatt Jacob }
1046c87e3f83SMatt Jacob #endif
1047c87e3f83SMatt Jacob 
104873651fd1SMatt Jacob /*
104973651fd1SMatt Jacob  * Task Management Types, purely for internal consumption
105073651fd1SMatt Jacob  */
1051c87e3f83SMatt Jacob typedef enum {
105267445003SAlexander Motin 	MPT_QUERY_TASK_SET=1234,
105367445003SAlexander Motin 	MPT_ABORT_TASK_SET,
1054c87e3f83SMatt Jacob 	MPT_CLEAR_TASK_SET,
105567445003SAlexander Motin 	MPT_QUERY_ASYNC_EVENT,
105667445003SAlexander Motin 	MPT_LOGICAL_UNIT_RESET,
1057c87e3f83SMatt Jacob 	MPT_TARGET_RESET,
1058c87e3f83SMatt Jacob 	MPT_CLEAR_ACA,
1059c87e3f83SMatt Jacob 	MPT_NIL_TMT_VALUE=5678
1060c87e3f83SMatt Jacob } mpt_task_mgmt_t;
1061b0a2fdeeSScott Long 
1062b0a2fdeeSScott Long /**************************** Unclassified Routines ***************************/
1063b0a2fdeeSScott Long void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1064b0a2fdeeSScott Long int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1065b0a2fdeeSScott Long 					 size_t reply_len, void *reply);
1066b0a2fdeeSScott Long int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1067b0a2fdeeSScott Long 			     mpt_req_state_t state, mpt_req_state_t mask,
1068b0a2fdeeSScott Long 			     int sleep_ok, int time_ms);
1069b0a2fdeeSScott Long void		mpt_enable_ints(struct mpt_softc *mpt);
1070b0a2fdeeSScott Long void		mpt_disable_ints(struct mpt_softc *mpt);
1071b0a2fdeeSScott Long int		mpt_attach(struct mpt_softc *mpt);
1072b0a2fdeeSScott Long int		mpt_shutdown(struct mpt_softc *mpt);
1073b0a2fdeeSScott Long int		mpt_detach(struct mpt_softc *mpt);
1074b0a2fdeeSScott Long int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1075b0a2fdeeSScott Long 				       size_t len, void *cmd);
1076b0a2fdeeSScott Long request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1077b0a2fdeeSScott Long void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1078b0a2fdeeSScott Long void		mpt_intr(void *arg);
1079b0a2fdeeSScott Long void		mpt_check_doorbell(struct mpt_softc *mpt);
1080b0a2fdeeSScott Long void		mpt_dump_reply_frame(struct mpt_softc *mpt,
1081b0a2fdeeSScott Long 				     MSG_DEFAULT_REPLY *reply_frame);
1082b0a2fdeeSScott Long 
1083b0a2fdeeSScott Long int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
10841d558d6aSScott Long 				  cfgparms_t *params,
1085b0a2fdeeSScott Long 				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1086b0a2fdeeSScott Long 				  int /*sleep_ok*/, int /*timeout_ms*/);
10871d558d6aSScott Long int		mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion,
10881d558d6aSScott Long 				       int PageNumber, uint32_t PageAddress,
10891d558d6aSScott Long 				       int ExtPageType,
10901d558d6aSScott Long 				       CONFIG_EXTENDED_PAGE_HEADER *rslt,
10911d558d6aSScott Long 				       int sleep_ok, int timeout_ms);
10921d558d6aSScott Long int		mpt_read_extcfg_page(struct mpt_softc *mpt, int Action,
10931d558d6aSScott Long 				     uint32_t PageAddress,
10941d558d6aSScott Long 				     CONFIG_EXTENDED_PAGE_HEADER *hdr,
10951d558d6aSScott Long 				     void *buf, size_t len, int sleep_ok,
10961d558d6aSScott Long 				     int timeout_ms);
1097b0a2fdeeSScott Long int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1098b0a2fdeeSScott Long 				    int /*PageNumber*/,
1099b0a2fdeeSScott Long 				    uint32_t /*PageAddress*/,
1100b0a2fdeeSScott Long 				    CONFIG_PAGE_HEADER *,
1101b0a2fdeeSScott Long 				    int /*sleep_ok*/, int /*timeout_ms*/);
1102b0a2fdeeSScott Long int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1103b0a2fdeeSScott Long 				  uint32_t /*PageAddress*/,
1104b0a2fdeeSScott Long 				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1105b0a2fdeeSScott Long 				  int /*sleep_ok*/, int /*timeout_ms*/);
1106b0a2fdeeSScott Long int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1107b0a2fdeeSScott Long 				   uint32_t /*PageAddress*/,
1108b0a2fdeeSScott Long 				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1109b0a2fdeeSScott Long 				   int /*sleep_ok*/, int /*timeout_ms*/);
1110b0a2fdeeSScott Long static __inline int
mpt_read_cur_cfg_page(struct mpt_softc * mpt,uint32_t PageAddress,CONFIG_PAGE_HEADER * hdr,size_t len,int sleep_ok,int timeout_ms)1111b0a2fdeeSScott Long mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1112b0a2fdeeSScott Long 		      CONFIG_PAGE_HEADER *hdr, size_t len,
1113b0a2fdeeSScott Long 		      int sleep_ok, int timeout_ms)
1114b0a2fdeeSScott Long {
1115b0a2fdeeSScott Long 	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1116b0a2fdeeSScott Long 				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1117b0a2fdeeSScott Long }
1118b0a2fdeeSScott Long 
1119b0a2fdeeSScott Long static __inline int
mpt_write_cur_cfg_page(struct mpt_softc * mpt,uint32_t PageAddress,CONFIG_PAGE_HEADER * hdr,size_t len,int sleep_ok,int timeout_ms)1120b0a2fdeeSScott Long mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1121b0a2fdeeSScott Long 		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1122b0a2fdeeSScott Long 		       int timeout_ms)
1123b0a2fdeeSScott Long {
1124b0a2fdeeSScott Long 	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1125b0a2fdeeSScott Long 				   PageAddress, hdr, len, sleep_ok,
1126b0a2fdeeSScott Long 				   timeout_ms));
1127b0a2fdeeSScott Long }
1128e18e2adaSMarius Strobl 
11299b631363SMatt Jacob /* mpt_debug.c functions */
11309b631363SMatt Jacob void mpt_print_reply(void *vmsg);
1131b0a2fdeeSScott Long void mpt_print_db(uint32_t mb);
11329b631363SMatt Jacob void mpt_print_config_reply(void *vmsg);
1133b0a2fdeeSScott Long char *mpt_ioc_diag(uint32_t diag);
1134b0a2fdeeSScott Long void mpt_req_state(mpt_req_state_t state);
11359b631363SMatt Jacob void mpt_print_config_request(void *vmsg);
11369b631363SMatt Jacob void mpt_print_request(void *vmsg);
1137444dd2b6SMatt Jacob void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1138e18e2adaSMarius Strobl 
11399b631363SMatt Jacob #endif /* _MPT_H_ */
1140