1101704Smjacob/* $FreeBSD$ */
2139749Simp/*-
3101704Smjacob * Generic defines for LSI '909 FC  adapters.
4101704Smjacob * FreeBSD Version.
5101704Smjacob *
6101704Smjacob * Copyright (c)  2000, 2001 by Greg Ansley
7101704Smjacob *
8101704Smjacob * Redistribution and use in source and binary forms, with or without
9101704Smjacob * modification, are permitted provided that the following conditions
10101704Smjacob * are met:
11101704Smjacob * 1. Redistributions of source code must retain the above copyright
12101704Smjacob *    notice immediately at the beginning of the file, without modification,
13101704Smjacob *    this list of conditions, and the following disclaimer.
14101704Smjacob * 2. The name of the author may not be used to endorse or promote products
15101704Smjacob *    derived from this software without specific prior written permission.
16101704Smjacob *
17101704Smjacob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18101704Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19101704Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20101704Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21101704Smjacob * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22101704Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23101704Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24101704Smjacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25101704Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26101704Smjacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27101704Smjacob * SUCH DAMAGE.
28156000Smjacob */
29156000Smjacob/*-
30156000Smjacob * Copyright (c) 2002, 2006 by Matthew Jacob
31156000Smjacob * All rights reserved.
32156000Smjacob *
33156000Smjacob * Redistribution and use in source and binary forms, with or without
34156000Smjacob * modification, are permitted provided that the following conditions are
35156000Smjacob * met:
36156000Smjacob * 1. Redistributions of source code must retain the above copyright
37156000Smjacob *    notice, this list of conditions and the following disclaimer.
38156000Smjacob * 2. Redistributions in binary form must reproduce at minimum a disclaimer
39156000Smjacob *    substantially similar to the "NO WARRANTY" disclaimer below
40156000Smjacob *    ("Disclaimer") and any redistribution must be conditioned upon including
41156000Smjacob *    a substantially similar Disclaimer requirement for further binary
42156000Smjacob *    redistribution.
43156000Smjacob * 3. Neither the names of the above listed copyright holders nor the names
44156000Smjacob *    of any contributors may be used to endorse or promote products derived
45156000Smjacob *    from this software without specific prior written permission.
46156000Smjacob *
47156000Smjacob * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48156000Smjacob * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49156000Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50156000Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51156000Smjacob * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52156000Smjacob * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53156000Smjacob * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54156000Smjacob * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55156000Smjacob * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56156000Smjacob * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
57156000Smjacob * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58147883Sscottl *
59156000Smjacob * Support from Chris Ellsworth in order to make SAS adapters work
60156000Smjacob * is gratefully acknowledged.
61159052Smjacob *
62159052Smjacob *
63159052Smjacob * Support from LSI-Logic has also gone a great deal toward making this a
64159052Smjacob * workable subsystem and is gratefully acknowledged.
65101704Smjacob */
66101704Smjacob/*
67147883Sscottl * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
68147883Sscottl * Copyright (c) 2004, 2005 Justin T. Gibbs
69147883Sscottl * Copyright (c) 2005, WHEEL Sp. z o.o.
70147883Sscottl * All rights reserved.
71147883Sscottl *
72147883Sscottl * Redistribution and use in source and binary forms, with or without
73147883Sscottl * modification, are permitted provided that the following conditions are
74147883Sscottl * met:
75147883Sscottl * 1. Redistributions of source code must retain the above copyright
76147883Sscottl *    notice, this list of conditions and the following disclaimer.
77147883Sscottl * 2. Redistributions in binary form must reproduce at minimum a disclaimer
78147883Sscottl *    substantially similar to the "NO WARRANTY" disclaimer below
79147883Sscottl *    ("Disclaimer") and any redistribution must be conditioned upon including
80147883Sscottl *    a substantially similar Disclaimer requirement for further binary
81147883Sscottl *    redistribution.
82148679Sgibbs * 3. Neither the names of the above listed copyright holders nor the names
83148679Sgibbs *    of any contributors may be used to endorse or promote products derived
84148679Sgibbs *    from this software without specific prior written permission.
85147883Sscottl *
86147883Sscottl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87147883Sscottl * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88147883Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89147883Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
90147883Sscottl * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91147883Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92147883Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93147883Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94147883Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95147883Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
96147883Sscottl * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97101704Smjacob */
98101704Smjacob
99101704Smjacob#ifndef _MPT_H_
100101704Smjacob#define _MPT_H_
101147883Sscottl
102147883Sscottl/********************************* OS Includes ********************************/
103147883Sscottl#include <sys/param.h>
104147883Sscottl#include <sys/systm.h>
105245983Smarius#include <sys/bus.h>
106245983Smarius#include <sys/condvar.h>
107147883Sscottl#include <sys/endian.h>
108147883Sscottl#include <sys/eventhandler.h>
109147883Sscottl#include <sys/kernel.h>
110147883Sscottl#include <sys/lock.h>
111147883Sscottl#include <sys/malloc.h>
112245983Smarius#include <sys/module.h>
113147883Sscottl#include <sys/mutex.h>
114147883Sscottl#include <sys/proc.h>
115245983Smarius#include <sys/queue.h>
116245983Smarius#include <sys/rman.h>
117245983Smarius#include <sys/types.h>
118147883Sscottl
119245983Smarius#include <machine/bus.h>
120147883Sscottl#include <machine/cpu.h>
121147883Sscottl#include <machine/resource.h>
122147883Sscottl
123207287Smarius#ifdef __sparc64__
124207287Smarius#include <dev/ofw/openfirm.h>
125207287Smarius#include <machine/ofw_machdep.h>
126207287Smarius#endif
127207287Smarius
128155521Smjacob#include <dev/pci/pcireg.h>
129155521Smjacob#include <dev/pci/pcivar.h>
130155521Smjacob
131147883Sscottl#include "opt_ddb.h"
132147883Sscottl
133147883Sscottl/**************************** Register Definitions ****************************/
134147883Sscottl#include <dev/mpt/mpt_reg.h>
135147883Sscottl
136147883Sscottl/******************************* MPI Definitions ******************************/
137147883Sscottl#include <dev/mpt/mpilib/mpi_type.h>
138147883Sscottl#include <dev/mpt/mpilib/mpi.h>
139147883Sscottl#include <dev/mpt/mpilib/mpi_cnfg.h>
140147883Sscottl#include <dev/mpt/mpilib/mpi_ioc.h>
141147883Sscottl#include <dev/mpt/mpilib/mpi_raid.h>
142147883Sscottl
143147883Sscottl/* XXX For mpt_debug.c */
144147883Sscottl#include <dev/mpt/mpilib/mpi_init.h>
145147883Sscottl
146164990Smjacob#define	MPT_S64_2_SCALAR(y)	((((int64_t)y.High) << 32) | (y.Low))
147164990Smjacob#define	MPT_U64_2_SCALAR(y)	((((uint64_t)y.High) << 32) | (y.Low))
148164990Smjacob
149147883Sscottl/****************************** Misc Definitions ******************************/
150165814Smjacob/* #define MPT_TEST_MULTIPATH	1 */
151101704Smjacob#define MPT_OK (0)
152101704Smjacob#define MPT_FAIL (0x10000)
153101704Smjacob
154147883Sscottl#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
155101704Smjacob
156157117Smjacob#define	MPT_ROLE_NONE		0
157157117Smjacob#define	MPT_ROLE_INITIATOR	1
158157117Smjacob#define	MPT_ROLE_TARGET		2
159157117Smjacob#define	MPT_ROLE_BOTH		3
160157117Smjacob#define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
161157117Smjacob
162207287Smarius#define	MPT_INI_ID_NONE		-1
163207287Smarius
164147883Sscottl/**************************** Forward Declarations ****************************/
165147883Sscottlstruct mpt_softc;
166147883Sscottlstruct mpt_personality;
167147883Sscottltypedef struct req_entry request_t;
168101704Smjacob
169147883Sscottl/************************* Personality Module Support *************************/
170147883Sscottltypedef int mpt_load_handler_t(struct mpt_personality *);
171147883Sscottltypedef int mpt_probe_handler_t(struct mpt_softc *);
172147883Sscottltypedef int mpt_attach_handler_t(struct mpt_softc *);
173157117Smjacobtypedef int mpt_enable_handler_t(struct mpt_softc *);
174162133Smjacobtypedef void mpt_ready_handler_t(struct mpt_softc *);
175147883Sscottltypedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
176147883Sscottl				MSG_EVENT_NOTIFY_REPLY *);
177147883Sscottltypedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
178147883Sscottl/* XXX Add return value and use for veto? */
179147883Sscottltypedef void mpt_shutdown_handler_t(struct mpt_softc *);
180147883Sscottltypedef void mpt_detach_handler_t(struct mpt_softc *);
181147883Sscottltypedef int mpt_unload_handler_t(struct mpt_personality *);
182147883Sscottl
183147883Sscottlstruct mpt_personality
184147883Sscottl{
185147883Sscottl	const char		*name;
186147883Sscottl	uint32_t		 id;		/* Assigned identifier. */
187147883Sscottl	u_int			 use_count;	/* Instances using personality*/
188147883Sscottl	mpt_load_handler_t	*load;		/* configure personailty */
189147883Sscottl#define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
190147883Sscottl	mpt_probe_handler_t	*probe;		/* configure personailty */
191147883Sscottl	mpt_attach_handler_t	*attach;	/* initialize device instance */
192157117Smjacob	mpt_enable_handler_t	*enable;	/* enable device */
193162133Smjacob	mpt_ready_handler_t	*ready;		/* final open for business */
194147883Sscottl	mpt_event_handler_t	*event;		/* Handle MPI event. */
195147883Sscottl	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
196147883Sscottl	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
197147883Sscottl	mpt_detach_handler_t	*detach;	/* release device instance */
198147883Sscottl	mpt_unload_handler_t	*unload;	/* Shutdown personality */
199147883Sscottl#define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
200101704Smjacob};
201101704Smjacob
202147883Sscottlint mpt_modevent(module_t, int, void *);
203101704Smjacob
204147883Sscottl/* Maximum supported number of personalities. */
205147883Sscottl#define MPT_MAX_PERSONALITIES	(15)
206101704Smjacob
207147883Sscottl#define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
208147883Sscottl	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
209101704Smjacob
210147883Sscottl#define DECLARE_MPT_PERSONALITY(name, order)				  \
211147883Sscottl	static moduledata_t name##_mod = {				  \
212147883Sscottl		#name, mpt_modevent, &name##_personality		  \
213147883Sscottl	};								  \
214147883Sscottl	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
215147883Sscottl	MODULE_VERSION(name, 1);					  \
216147883Sscottl	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
217147883Sscottl
218147883Sscottl/******************************* Bus DMA Support ******************************/
219147883Sscottl/* XXX Need to update bus_dmamap_sync to take a range argument. */
220147883Sscottl#define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
221147883Sscottl	bus_dmamap_sync(dma_tag, dmamap, op)
222147883Sscottl
223147883Sscottl#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
224147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
225147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
226147883Sscottl			   dma_tagp)					\
227147883Sscottl	bus_dma_tag_create(parent_tag, alignment, boundary,		\
228147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
229147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
230169293Smjacob			   busdma_lock_mutex, &(mpt)->mpt_lock,		\
231147883Sscottl			   dma_tagp)
232147883Sscottlstruct mpt_map_info {
233147883Sscottl	struct mpt_softc *mpt;
234147883Sscottl	int		  error;
235147883Sscottl	uint32_t	  phys;
236147883Sscottl};
237147883Sscottl
238147883Sscottlvoid mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
239147883Sscottl
240147883Sscottl/********************************** Endianess *********************************/
241164990Smjacob#define	MPT_2_HOST64(ptr, tag)	ptr->tag = le64toh(ptr->tag)
242164990Smjacob#define	MPT_2_HOST32(ptr, tag)	ptr->tag = le32toh(ptr->tag)
243164990Smjacob#define	MPT_2_HOST16(ptr, tag)	ptr->tag = le16toh(ptr->tag)
244147883Sscottl
245164990Smjacob#define	HOST_2_MPT64(ptr, tag)	ptr->tag = htole64(ptr->tag)
246164990Smjacob#define	HOST_2_MPT32(ptr, tag)	ptr->tag = htole32(ptr->tag)
247164990Smjacob#define	HOST_2_MPT16(ptr, tag)	ptr->tag = htole16(ptr->tag)
248147883Sscottl
249164998Smjacob#if	_BYTE_ORDER == _BIG_ENDIAN
250164998Smjacobvoid mpt2host_sge_simple_union(SGE_SIMPLE_UNION *);
251164998Smjacobvoid mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *);
252164998Smjacobvoid mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *);
253164998Smjacobvoid mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *);
254186878Smariusvoid mpt2host_config_page_ioc3(CONFIG_PAGE_IOC_3 *);
255186878Smariusvoid mpt2host_config_page_scsi_port_0(CONFIG_PAGE_SCSI_PORT_0 *);
256186878Smariusvoid mpt2host_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *);
257186878Smariusvoid host2mpt_config_page_scsi_port_1(CONFIG_PAGE_SCSI_PORT_1 *);
258186878Smariusvoid mpt2host_config_page_scsi_port_2(CONFIG_PAGE_SCSI_PORT_2 *);
259186878Smariusvoid mpt2host_config_page_scsi_device_0(CONFIG_PAGE_SCSI_DEVICE_0 *);
260186878Smariusvoid mpt2host_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *);
261186878Smariusvoid host2mpt_config_page_scsi_device_1(CONFIG_PAGE_SCSI_DEVICE_1 *);
262186878Smariusvoid mpt2host_config_page_fc_port_0(CONFIG_PAGE_FC_PORT_0 *);
263186878Smariusvoid mpt2host_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *);
264186878Smariusvoid host2mpt_config_page_fc_port_1(CONFIG_PAGE_FC_PORT_1 *);
265164998Smjacobvoid mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *);
266186878Smariusvoid mpt2host_config_page_raid_phys_disk_0(CONFIG_PAGE_RAID_PHYS_DISK_0 *);
267164998Smjacobvoid mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *);
268164998Smjacob#else
269164990Smjacob#define	mpt2host_sge_simple_union(x)		do { ; } while (0)
270164990Smjacob#define	mpt2host_iocfacts_reply(x)		do { ; } while (0)
271164990Smjacob#define	mpt2host_portfacts_reply(x)		do { ; } while (0)
272164990Smjacob#define	mpt2host_config_page_ioc2(x)		do { ; } while (0)
273186878Smarius#define	mpt2host_config_page_ioc3(x)		do { ; } while (0)
274186878Smarius#define	mpt2host_config_page_scsi_port_0(x)	do { ; } while (0)
275186878Smarius#define	mpt2host_config_page_scsi_port_1(x)	do { ; } while (0)
276186878Smarius#define	host2mpt_config_page_scsi_port_1(x)	do { ; } while (0)
277186878Smarius#define	mpt2host_config_page_scsi_port_2(x)	do { ; } while (0)
278186878Smarius#define	mpt2host_config_page_scsi_device_0(x)	do { ; } while (0)
279186878Smarius#define	mpt2host_config_page_scsi_device_1(x)	do { ; } while (0)
280186878Smarius#define	host2mpt_config_page_scsi_device_1(x)	do { ; } while (0)
281186878Smarius#define	mpt2host_config_page_fc_port_0(x)	do { ; } while (0)
282186878Smarius#define	mpt2host_config_page_fc_port_1(x)	do { ; } while (0)
283186878Smarius#define	host2mpt_config_page_fc_port_1(x)	do { ; } while (0)
284164990Smjacob#define	mpt2host_config_page_raid_vol_0(x)	do { ; } while (0)
285186878Smarius#define	mpt2host_config_page_raid_phys_disk_0(x)			\
286186878Smarius	do { ; } while (0)
287164990Smjacob#define	mpt2host_mpi_raid_vol_indicator(x)	do { ; } while (0)
288164990Smjacob#endif
289164990Smjacob
290147883Sscottl/**************************** MPI Transaction State ***************************/
291147883Sscottltypedef enum {
292157354Smjacob	REQ_STATE_NIL		= 0x00,
293157354Smjacob	REQ_STATE_FREE		= 0x01,
294157354Smjacob	REQ_STATE_ALLOCATED	= 0x02,
295157354Smjacob	REQ_STATE_QUEUED	= 0x04,
296157354Smjacob	REQ_STATE_DONE		= 0x08,
297157354Smjacob	REQ_STATE_TIMEDOUT	= 0x10,
298157354Smjacob	REQ_STATE_NEED_WAKEUP	= 0x20,
299157354Smjacob	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
300147883Sscottl	REQ_STATE_MASK		= 0xFF
301147883Sscottl} mpt_req_state_t;
302147883Sscottl
303147883Sscottlstruct req_entry {
304147883Sscottl	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
305147883Sscottl	mpt_req_state_t	state;		/* Request State Information */
306147883Sscottl	uint16_t	index;		/* Index of this entry */
307147883Sscottl	uint16_t	IOCStatus;	/* Completion status */
308220945Smarius	uint16_t	ResponseCode;	/* TMF Response Code */
309157662Smjacob	uint16_t	serno;		/* serial number */
310147883Sscottl	union ccb      *ccb;		/* CAM request */
311147883Sscottl	void	       *req_vbuf;	/* Virtual Address of Entry */
312147883Sscottl	void	       *sense_vbuf;	/* Virtual Address of sense data */
313147883Sscottl	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
314147883Sscottl	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
315159312Smjacob	bus_dmamap_t	dmap;		/* DMA map for data buffers */
316155521Smjacob	struct req_entry *chain;	/* for SGE overallocations */
317169293Smjacob	struct callout  callout;	/* Timeout for the request */
318147883Sscottl};
319147883Sscottl
320170252Sscottltypedef struct mpt_config_params {
321170252Sscottl	u_int		Action;
322170252Sscottl	u_int		PageVersion;
323170252Sscottl	u_int		PageLength;
324170252Sscottl	u_int		PageNumber;
325170252Sscottl	u_int		PageType;
326170252Sscottl	u_int		PageAddress;
327170252Sscottl	u_int		ExtPageLength;
328170252Sscottl	u_int		ExtPageType;
329170252Sscottl} cfgparms_t;
330170252Sscottl
331157117Smjacob/**************************** MPI Target State Info ***************************/
332157117Smjacob
333157117Smjacobtypedef struct {
334157117Smjacob	uint32_t reply_desc;	/* current reply descriptor */
335157117Smjacob	uint32_t resid;		/* current data residual */
336157117Smjacob	uint32_t bytes_xfered;	/* current relative offset */
337157117Smjacob	union ccb *ccb;		/* pointer to currently active ccb */
338157117Smjacob	request_t *req;		/* pointer to currently active assist request */
339160391Smjacob	uint32_t
340160391Smjacob		is_local : 1,
341160391Smjacob		nxfers	 : 31;
342157354Smjacob	uint32_t tag_id;
343157117Smjacob	enum {
344157117Smjacob		TGT_STATE_NIL,
345157354Smjacob		TGT_STATE_LOADING,
346157117Smjacob		TGT_STATE_LOADED,
347157117Smjacob		TGT_STATE_IN_CAM,
348157117Smjacob                TGT_STATE_SETTING_UP_FOR_DATA,
349157117Smjacob                TGT_STATE_MOVING_DATA,
350157117Smjacob                TGT_STATE_MOVING_DATA_AND_STATUS,
351157117Smjacob                TGT_STATE_SENDING_STATUS
352157117Smjacob	} state;
353157117Smjacob} mpt_tgt_state_t;
354157117Smjacob
355157117Smjacob/*
356157117Smjacob * When we get an incoming command it has its own tag which is called the
357157117Smjacob * IoIndex. This is the value we gave that particular command buffer when
358157117Smjacob * we originally assigned it. It's just a number, really. The FC card uses
359157117Smjacob * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
360157117Smjacob * contains pointers the request_t structures related to that IoIndex.
361157117Smjacob *
362157117Smjacob * What *we* do is construct a tag out of the index for the target command
363157117Smjacob * which owns the incoming ATIO plus a rolling sequence number.
364157117Smjacob */
365157117Smjacob#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
366157354Smjacob ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
367157117Smjacob
368157117Smjacob#ifdef	INVARIANTS
369157117Smjacob#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
370157117Smjacob#else
371157354Smjacob#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
372157117Smjacob#endif
373157117Smjacob
374157117Smjacob#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
375157117Smjacob    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
376157117Smjacob
377157117SmjacobSTAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
378157117Smjacob#define	MPT_MAX_LUNS	256
379157117Smjacobtypedef struct {
380157117Smjacob	struct mpt_hdr_stailq	atios;
381157117Smjacob	struct mpt_hdr_stailq	inots;
382157117Smjacob	int enabled;
383157117Smjacob} tgt_resource_t;
384157662Smjacob#define	MPT_MAX_ELS	64
385157117Smjacob
386147883Sscottl/**************************** Handler Registration ****************************/
387101704Smjacob/*
388147883Sscottl * Global table of registered reply handlers.  The
389147883Sscottl * handler is indicated by byte 3 of the request
390147883Sscottl * index submitted to the IOC.  This allows the
391147883Sscottl * driver core to perform generic processing without
392147883Sscottl * any knowledge of per-personality behavior.
393147883Sscottl *
394147883Sscottl * MPT_NUM_REPLY_HANDLERS must be a power of 2
395147883Sscottl * to allow the easy generation of a mask.
396147883Sscottl *
397147883Sscottl * The handler offsets used by the core are hard coded
398147883Sscottl * allowing faster code generation when assigning a handler
399147883Sscottl * to a request.  All "personalities" must use the
400147883Sscottl * the handler registration mechanism.
401147883Sscottl *
402147883Sscottl * The IOC handlers that are rarely executed are placed
403147883Sscottl * at the tail of the table to make it more likely that
404147883Sscottl * all commonly executed handlers fit in a single cache
405147883Sscottl * line.
406101704Smjacob */
407157117Smjacob#define MPT_NUM_REPLY_HANDLERS		(32)
408147883Sscottl#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
409147883Sscottl#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
410147883Sscottl#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
411147883Sscottltypedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
412157117Smjacob    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
413147883Sscottltypedef union {
414147883Sscottl	mpt_reply_handler_t	*reply_handler;
415147883Sscottl} mpt_handler_t;
416101704Smjacob
417147883Sscottltypedef enum {
418147883Sscottl	MPT_HANDLER_REPLY,
419147883Sscottl	MPT_HANDLER_EVENT,
420147883Sscottl	MPT_HANDLER_RESET,
421147883Sscottl	MPT_HANDLER_SHUTDOWN
422147883Sscottl} mpt_handler_type;
423101704Smjacob
424147883Sscottlstruct mpt_handler_record
425147883Sscottl{
426147883Sscottl	LIST_ENTRY(mpt_handler_record)	links;
427147883Sscottl	mpt_handler_t			handler;
428101704Smjacob};
429101704Smjacob
430147883SscottlLIST_HEAD(mpt_handler_list, mpt_handler_record);
431147883Sscottl
432147883Sscottl/*
433147883Sscottl * The handler_id is currently unused but would contain the
434147883Sscottl * handler ID used in the MsgContext field to allow direction
435147883Sscottl * of replies to the handler.  Registrations that don't require
436147883Sscottl * a handler id can pass in NULL for the handler_id.
437147883Sscottl *
438147883Sscottl * Deregistrations for handlers without a handler id should
439147883Sscottl * pass in MPT_HANDLER_ID_NONE.
440147883Sscottl */
441147883Sscottl#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
442147883Sscottlint mpt_register_handler(struct mpt_softc *, mpt_handler_type,
443147883Sscottl			 mpt_handler_t, uint32_t *);
444147883Sscottlint mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
445147883Sscottl			   mpt_handler_t, uint32_t);
446147883Sscottl
447147883Sscottl/******************* Per-Controller Instance Data Structures ******************/
448147883SscottlTAILQ_HEAD(req_queue, req_entry);
449147883Sscottl
450147883Sscottl/* Structure for saving proper values for modifyable PCI config registers */
451147883Sscottlstruct mpt_pci_cfg {
452147883Sscottl	uint16_t Command;
453147883Sscottl	uint16_t LatencyTimer_LineSize;
454147883Sscottl	uint32_t IO_BAR;
455147883Sscottl	uint32_t Mem0_BAR[2];
456147883Sscottl	uint32_t Mem1_BAR[2];
457147883Sscottl	uint32_t ROM_BAR;
458147883Sscottl	uint8_t  IntLine;
459147883Sscottl	uint32_t PMCSR;
460101704Smjacob};
461101704Smjacob
462147883Sscottltypedef enum {
463147883Sscottl	MPT_RVF_NONE		= 0x0,
464147883Sscottl	MPT_RVF_ACTIVE		= 0x1,
465147883Sscottl	MPT_RVF_ANNOUNCED	= 0x2,
466147883Sscottl	MPT_RVF_UP2DATE		= 0x4,
467147883Sscottl	MPT_RVF_REFERENCED	= 0x8,
468147883Sscottl	MPT_RVF_WCE_CHANGED	= 0x10
469147883Sscottl} mpt_raid_volume_flags;
470101704Smjacob
471147883Sscottlstruct mpt_raid_volume {
472147883Sscottl	CONFIG_PAGE_RAID_VOL_0	       *config_page;
473147883Sscottl	MPI_RAID_VOL_INDICATOR		sync_progress;
474147883Sscottl	mpt_raid_volume_flags		flags;
475158933Smjacob	u_int				quiesced_disks;
476101704Smjacob};
477101704Smjacob
478147883Sscottltypedef enum {
479147883Sscottl	MPT_RDF_NONE		= 0x00,
480147883Sscottl	MPT_RDF_ACTIVE		= 0x01,
481147883Sscottl	MPT_RDF_ANNOUNCED	= 0x02,
482147883Sscottl	MPT_RDF_UP2DATE		= 0x04,
483147883Sscottl	MPT_RDF_REFERENCED	= 0x08,
484147883Sscottl	MPT_RDF_QUIESCING	= 0x10,
485147883Sscottl	MPT_RDF_QUIESCED	= 0x20
486147883Sscottl} mpt_raid_disk_flags;
487147883Sscottl
488147883Sscottlstruct mpt_raid_disk {
489147883Sscottl	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
490147883Sscottl	struct mpt_raid_volume	       *volume;
491147883Sscottl	u_int				member_number;
492147883Sscottl	u_int				pass_thru_active;
493147883Sscottl	mpt_raid_disk_flags		flags;
494101704Smjacob};
495101704Smjacob
496147883Sscottlstruct mpt_evtf_record {
497147883Sscottl	MSG_EVENT_NOTIFY_REPLY		reply;
498147883Sscottl	uint32_t			context;
499147883Sscottl	LIST_ENTRY(mpt_evtf_record)	links;
500147883Sscottl};
501101704Smjacob
502147883SscottlLIST_HEAD(mpt_evtf_list, mpt_evtf_record);
503101704Smjacob
504170252Sscottlstruct mptsas_devinfo {
505170252Sscottl	uint16_t	dev_handle;
506170252Sscottl	uint16_t	parent_dev_handle;
507170252Sscottl	uint16_t	enclosure_handle;
508170252Sscottl	uint16_t	slot;
509170252Sscottl	uint8_t		phy_num;
510170252Sscottl	uint8_t		physical_port;
511170252Sscottl	uint8_t		target_id;
512170252Sscottl	uint8_t		bus;
513170252Sscottl	uint64_t	sas_address;
514170252Sscottl	uint32_t	device_info;
515170252Sscottl};
516170252Sscottl
517170252Sscottlstruct mptsas_phyinfo {
518170252Sscottl	uint16_t	handle;
519170252Sscottl	uint8_t		phy_num;
520170252Sscottl	uint8_t		port_id;
521170252Sscottl	uint8_t		negotiated_link_rate;
522170252Sscottl	uint8_t		hw_link_rate;
523170252Sscottl	uint8_t		programmed_link_rate;
524170252Sscottl	uint8_t		sas_port_add_phy;
525170252Sscottl	struct mptsas_devinfo identify;
526170252Sscottl	struct mptsas_devinfo attached;
527170252Sscottl};
528170252Sscottl
529170252Sscottlstruct mptsas_portinfo {
530170252Sscottl	uint16_t			num_phys;
531170252Sscottl	struct mptsas_phyinfo		*phy_info;
532170252Sscottl};
533170252Sscottl
534147883Sscottlstruct mpt_softc {
535147883Sscottl	device_t		dev;
536147883Sscottl	struct mtx		mpt_lock;
537157662Smjacob	int			mpt_locksetup;
538147883Sscottl	uint32_t		mpt_pers_mask;
539160290Smjacob	uint32_t
540231623Smarius				: 7,
541157117Smjacob		unit		: 8,
542165814Smjacob		ready		: 1,
543164990Smjacob		fw_uploaded	: 1,
544164416Smjacob		msi_enable	: 1,
545157117Smjacob		twildcard	: 1,
546157117Smjacob		tenabled	: 1,
547160290Smjacob		do_cfg_role	: 1,
548158982Smjacob		raid_enabled	: 1,
549147883Sscottl		raid_mwce_set	: 1,
550147883Sscottl		getreqwaiter	: 1,
551147883Sscottl		shutdwn_raid    : 1,
552147883Sscottl		shutdwn_recovery: 1,
553147883Sscottl		outofbeer	: 1,
554147883Sscottl		disabled	: 1,
555159178Smjacob		is_spi		: 1,
556157117Smjacob		is_sas		: 1,
557231623Smarius		is_fc		: 1,
558231623Smarius		is_1078		: 1;
559101704Smjacob
560160290Smjacob	u_int			cfg_role;
561160290Smjacob	u_int			role;	/* role: none, ini, target, both */
562160290Smjacob
563147883Sscottl	u_int			verbose;
564165814Smjacob#ifdef	MPT_TEST_MULTIPATH
565165814Smjacob	int			failure_id;
566165814Smjacob#endif
567101704Smjacob
568147883Sscottl	/*
569147883Sscottl	 * IOC Facts
570147883Sscottl	 */
571164990Smjacob	MSG_IOC_FACTS_REPLY	ioc_facts;
572101704Smjacob
573147883Sscottl	/*
574147883Sscottl	 * Port Facts
575147883Sscottl	 */
576164990Smjacob	MSG_PORT_FACTS_REPLY *	port_facts;
577164990Smjacob#define	mpt_max_tgtcmds	port_facts[0].MaxPostedCmdBuffers
578102822Smjacob
579147883Sscottl	/*
580147883Sscottl	 * Device Configuration Information
581147883Sscottl	 */
582147883Sscottl	union {
583147883Sscottl		struct mpt_spi_cfg {
584147883Sscottl			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
585147883Sscottl			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
586147883Sscottl			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
587147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
588147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
589207287Smarius			int				_ini_id;
590147883Sscottl			uint16_t			_tag_enable;
591147883Sscottl			uint16_t			_disc_enable;
592147883Sscottl		} spi;
593147883Sscottl#define	mpt_port_page0		cfg.spi._port_page0
594147883Sscottl#define	mpt_port_page1		cfg.spi._port_page1
595147883Sscottl#define	mpt_port_page2		cfg.spi._port_page2
596147883Sscottl#define	mpt_dev_page0		cfg.spi._dev_page0
597147883Sscottl#define	mpt_dev_page1		cfg.spi._dev_page1
598207287Smarius#define	mpt_ini_id		cfg.spi._ini_id
599147883Sscottl#define	mpt_tag_enable		cfg.spi._tag_enable
600147883Sscottl#define	mpt_disc_enable		cfg.spi._disc_enable
601147883Sscottl		struct mpi_fc_cfg {
602157117Smjacob			CONFIG_PAGE_FC_PORT_0 _port_page0;
603159919Smjacob			uint32_t _port_speed;
604157117Smjacob#define	mpt_fcport_page0	cfg.fc._port_page0
605159919Smjacob#define	mpt_fcport_speed	cfg.fc._port_speed
606147883Sscottl		} fc;
607147883Sscottl	} cfg;
608160397Smjacob	/*
609160397Smjacob	 * Device config information stored up for sysctl to access
610160397Smjacob	 */
611160397Smjacob	union {
612160397Smjacob		struct {
613160397Smjacob			unsigned int initiator_id;
614160397Smjacob		} spi;
615160397Smjacob		struct {
616160397Smjacob			char wwnn[19];
617160397Smjacob			char wwpn[19];
618160397Smjacob		} fc;
619160397Smjacob	} scinfo;
620147883Sscottl
621158982Smjacob	/* Controller Info for RAID information */
622147883Sscottl	CONFIG_PAGE_IOC_2 *	ioc_page2;
623147883Sscottl	CONFIG_PAGE_IOC_3 *	ioc_page3;
624147883Sscottl
625147883Sscottl	/* Raid Data */
626147883Sscottl	struct mpt_raid_volume* raid_volumes;
627147883Sscottl	struct mpt_raid_disk*	raid_disks;
628147883Sscottl	u_int			raid_max_volumes;
629147883Sscottl	u_int			raid_max_disks;
630147883Sscottl	u_int			raid_page0_len;
631147883Sscottl	u_int			raid_wakeup;
632147883Sscottl	u_int			raid_rescan;
633147883Sscottl	u_int			raid_resync_rate;
634147883Sscottl	u_int			raid_mwce_setting;
635147883Sscottl	u_int			raid_queue_depth;
636152444Skan	u_int			raid_nonopt_volumes;
637147883Sscottl	struct proc	       *raid_thread;
638147883Sscottl	struct callout		raid_timer;
639147883Sscottl
640147883Sscottl	/*
641147883Sscottl	 * PCI Hardware info
642147883Sscottl	 */
643147883Sscottl	struct resource *	pci_irq;	/* Interrupt map for chip */
644220945Smarius	void *			ih;		/* Interrupt handle */
645233721Smarius#if 0
646147883Sscottl	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
647233721Smarius#endif
648147883Sscottl
649147883Sscottl	/*
650147883Sscottl	 * DMA Mapping Stuff
651147883Sscottl	 */
652147883Sscottl	struct resource *	pci_reg;	/* Register map for chip */
653147883Sscottl	bus_space_tag_t		pci_st;		/* Bus tag for registers */
654147883Sscottl	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
655147883Sscottl	/* PIO versions of above. */
656147883Sscottl	struct resource *	pci_pio_reg;
657147883Sscottl	bus_space_tag_t		pci_pio_st;
658147883Sscottl	bus_space_handle_t	pci_pio_sh;
659147883Sscottl
660147883Sscottl	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
661147883Sscottl	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
662147883Sscottl	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
663147883Sscottl	uint8_t		       *reply;		/* KVA of reply memory */
664147883Sscottl	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
665147883Sscottl
666147883Sscottl	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
667147883Sscottl	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
668147883Sscottl	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
669147883Sscottl	uint8_t		       *request;	/* KVA of Request memory */
670157117Smjacob	bus_addr_t		request_phys;	/* BusAddr of request memory */
671147883Sscottl
672155521Smjacob	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
673209599Sken	uint32_t		max_cam_seg_cnt;/* calculated from MAXPHYS*/
674155521Smjacob
675155521Smjacob	/*
676155521Smjacob	 * Hardware management
677155521Smjacob	 */
678147883Sscottl	u_int			reset_cnt;
679147883Sscottl
680147883Sscottl	/*
681147883Sscottl	 * CAM && Software Management
682147883Sscottl	 */
683147883Sscottl	request_t	       *request_pool;
684147883Sscottl	struct req_queue	request_free_list;
685147883Sscottl	struct req_queue	request_pending_list;
686147883Sscottl	struct req_queue	request_timeout_list;
687147883Sscottl
688147883Sscottl
689147883Sscottl	struct cam_sim	       *sim;
690147883Sscottl	struct cam_path	       *path;
691147883Sscottl
692147883Sscottl	struct cam_sim	       *phydisk_sim;
693147883Sscottl	struct cam_path	       *phydisk_path;
694147883Sscottl
695147883Sscottl	struct proc	       *recovery_thread;
696147883Sscottl	request_t	       *tmf_req;
697147883Sscottl
698157117Smjacob	/*
699157354Smjacob	 * Deferred frame acks due to resource shortage.
700157354Smjacob	 */
701157354Smjacob	struct mpt_evtf_list	ack_frames;
702159312Smjacob
703157354Smjacob	/*
704157117Smjacob	 * Target Mode Support
705157117Smjacob	 */
706157117Smjacob	uint32_t		scsi_tgt_handler_id;
707157117Smjacob	request_t **		tgt_cmd_ptrs;
708157662Smjacob	request_t **		els_cmd_ptrs;	/* FC only */
709147883Sscottl
710157117Smjacob	/*
711157117Smjacob	 * *snork*- this is chosen to be here *just in case* somebody
712157117Smjacob	 * forgets to point to it exactly and we index off of trt with
713157117Smjacob	 * CAM_LUN_WILDCARD.
714157117Smjacob	 */
715160391Smjacob	tgt_resource_t		trt_wildcard;		/* wildcard luns */
716157117Smjacob	tgt_resource_t		trt[MPT_MAX_LUNS];
717157117Smjacob	uint16_t		tgt_cmds_allocated;
718157662Smjacob	uint16_t		els_cmds_allocated;	/* FC only */
719157117Smjacob
720157117Smjacob	uint16_t		timeouts;	/* timeout count */
721157117Smjacob	uint16_t		success;	/* successes afer timeout */
722157662Smjacob	uint16_t		sequence;	/* Sequence Number */
723157662Smjacob	uint16_t		pad3;
724157117Smjacob
725264950Smarius#if 0
726159312Smjacob	/* Paired port in some dual adapters configurations */
727147883Sscottl	struct mpt_softc *	mpt2;
728264950Smarius#endif
729147883Sscottl
730147883Sscottl	/* FW Image management */
731147883Sscottl	uint32_t		fw_image_size;
732147883Sscottl	uint8_t		       *fw_image;
733147883Sscottl	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
734147883Sscottl	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
735159178Smjacob	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
736147883Sscottl
737170252Sscottl	/* SAS Topology */
738170252Sscottl	struct mptsas_portinfo	*sas_portinfo;
739170252Sscottl
740147883Sscottl	/* Shutdown Event Handler. */
741147883Sscottl	eventhandler_tag         eh;
742147883Sscottl
743178814Sjhb	/* Userland management interface. */
744178814Sjhb	struct cdev		*cdev;
745178814Sjhb
746147883Sscottl	TAILQ_ENTRY(mpt_softc)	links;
747147883Sscottl};
748147883Sscottl
749157662Smjacobstatic __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
750157662Smjacob
751157662Smjacobstatic __inline void
752157662Smjacobmpt_assign_serno(struct mpt_softc *mpt, request_t *req)
753157662Smjacob{
754157662Smjacob	if ((req->serno = mpt->sequence++) == 0) {
755157662Smjacob		req->serno = mpt->sequence++;
756157662Smjacob	}
757157662Smjacob}
758157662Smjacob
759157117Smjacob/***************************** Locking Primitives *****************************/
760147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
761147883Sscottl#define	MPT_LOCK_SETUP(mpt)						\
762147883Sscottl		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
763147883Sscottl		mpt->mpt_locksetup = 1
764147883Sscottl#define	MPT_LOCK_DESTROY(mpt)						\
765147883Sscottl	if (mpt->mpt_locksetup) {					\
766147883Sscottl		mtx_destroy(&mpt->mpt_lock);				\
767147883Sscottl		mpt->mpt_locksetup = 0;					\
768147883Sscottl	}
769147883Sscottl
770147883Sscottl#define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
771147883Sscottl#define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
772157662Smjacob#define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
773169293Smjacob#define	MPT_LOCK_ASSERT(mpt)	mtx_assert(&(mpt)->mpt_lock, MA_OWNED)
774147883Sscottl#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
775147883Sscottl	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
776169293Smjacob#define mpt_req_timeout(req, ticks, func, arg) \
777198262Skan	callout_reset(&(req)->callout, (ticks), (func), (arg))
778169293Smjacob#define mpt_req_untimeout(req, func, arg) \
779169293Smjacob	callout_stop(&(req)->callout)
780198262Skan#define mpt_callout_init(mpt, c) \
781198262Skan	callout_init_mtx(c, &(mpt)->mpt_lock, 0)
782198262Skan#define mpt_callout_drain(mpt, c) \
783198262Skan	callout_drain(c)
784157662Smjacob
785147883Sscottl/******************************* Register Access ******************************/
786147883Sscottlstatic __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
787147883Sscottlstatic __inline uint32_t mpt_read(struct mpt_softc *, int);
788147883Sscottlstatic __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
789147883Sscottlstatic __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
790147883Sscottl
791147883Sscottlstatic __inline void
792147883Sscottlmpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
793147883Sscottl{
794147883Sscottl	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
795147883Sscottl}
796147883Sscottl
797147883Sscottlstatic __inline uint32_t
798147883Sscottlmpt_read(struct mpt_softc *mpt, int offset)
799147883Sscottl{
800147883Sscottl	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
801147883Sscottl}
802147883Sscottl
803147883Sscottl/*
804147883Sscottl * Some operations (e.g. diagnostic register writes while the ARM proccessor
805147883Sscottl * is disabled), must be performed using "PCI pio" operations.  On non-PCI
806147883Sscottl * busses, these operations likely map to normal register accesses.
807147883Sscottl */
808147883Sscottlstatic __inline void
809147883Sscottlmpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
810147883Sscottl{
811231623Smarius	KASSERT(mpt->pci_pio_reg != NULL, ("no PIO resource"));
812147883Sscottl	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
813147883Sscottl}
814147883Sscottl
815147883Sscottlstatic __inline uint32_t
816147883Sscottlmpt_pio_read(struct mpt_softc *mpt, int offset)
817147883Sscottl{
818231623Smarius	KASSERT(mpt->pci_pio_reg != NULL, ("no PIO resource"));
819147883Sscottl	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
820147883Sscottl}
821147883Sscottl/*********************** Reply Frame/Request Management ***********************/
822147883Sscottl/* Max MPT Reply we are willing to accept (must be power of 2) */
823155521Smjacob#define MPT_REPLY_SIZE   	256
824147883Sscottl
825157117Smjacob/*
826157117Smjacob * Must be less than 16384 in order for target mode to work
827157117Smjacob */
828155521Smjacob#define MPT_MAX_REQUESTS(mpt)	512
829155521Smjacob#define MPT_REQUEST_AREA	512
830155521Smjacob#define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
831147883Sscottl#define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
832147883Sscottl
833157354Smjacob#define MPT_CONTEXT_CB_SHIFT	(16)
834157117Smjacob#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
835147883Sscottl#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
836147883Sscottl#define MPT_CONTEXT_TO_CBI(x)	\
837147883Sscottl    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
838157354Smjacob#define MPT_CONTEXT_REQI_MASK	0xFFFF
839157117Smjacob#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
840147883Sscottl
841147883Sscottl/*
842147883Sscottl * Convert a 32bit physical address returned from IOC to an
843147883Sscottl * offset into our reply frame memory or the kvm address needed
844147883Sscottl * to access the data.  The returned address is only the low
845147883Sscottl * 32 bits, so mask our base physical address accordingly.
846147883Sscottl */
847147883Sscottl#define MPT_REPLY_BADDR(x)		\
848147883Sscottl	(x << 1)
849147883Sscottl#define MPT_REPLY_OTOV(m, i) 		\
850147883Sscottl	((void *)(&m->reply[i]))
851147883Sscottl
852147883Sscottl#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
853147883Sscottldo {							\
854158982Smjacob	if (mpt->verbose > MPT_PRT_DEBUG)		\
855147883Sscottl		mpt_dump_reply_frame(mpt, reply_frame);	\
856147883Sscottl} while(0)
857147883Sscottl
858147883Sscottlstatic __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
859147883Sscottlstatic __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
860147883Sscottl
861147883Sscottl/*
862147883Sscottl * Give the reply buffer back to the IOC after we have
863147883Sscottl * finished processing it.
864147883Sscottl */
865147883Sscottlstatic __inline void
866147883Sscottlmpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
867147883Sscottl{
868147883Sscottl     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
869147883Sscottl}
870147883Sscottl
871147883Sscottl/* Get a reply from the IOC */
872147883Sscottlstatic __inline uint32_t
873147883Sscottlmpt_pop_reply_queue(struct mpt_softc *mpt)
874147883Sscottl{
875147883Sscottl     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
876147883Sscottl}
877147883Sscottl
878157354Smjacobvoid
879157354Smjacobmpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
880157354Smjacob
881220945Smarius/************************** Scatter Gather Management **************************/
882155521Smjacob/* MPT_RQSL- size of request frame, in bytes */
883164990Smjacob#define	MPT_RQSL(mpt)		(mpt->ioc_facts.RequestFrameSize << 2)
884155521Smjacob
885155521Smjacob/* MPT_NSGL- how many SG entries can fit in a request frame size */
886155521Smjacob#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
887155521Smjacob
888155521Smjacob/* MPT_NRFM- how many request frames can fit in each request alloc we make */
889155521Smjacob#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
890155521Smjacob
891147883Sscottl/*
892155521Smjacob * MPT_NSGL_FIRST- # of SG elements that can fit after
893155521Smjacob * an I/O request but still within the request frame.
894155521Smjacob * Do this safely based upon SGE_IO_UNION.
895155521Smjacob *
896155521Smjacob * Note that the first element is *within* the SCSI request.
897147883Sscottl */
898155521Smjacob#define	MPT_NSGL_FIRST(mpt)	\
899155521Smjacob    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
900155521Smjacob    sizeof (SGE_IO_UNION))
901147883Sscottl
902147883Sscottl/***************************** IOC Initialization *****************************/
903147883Sscottlint mpt_reset(struct mpt_softc *, int /*reinit*/);
904147883Sscottl
905157117Smjacob/****************************** Debugging ************************************/
906160290Smjacobvoid mpt_dump_data(struct mpt_softc *, const char *, void *, int);
907158933Smjacobvoid mpt_dump_request(struct mpt_softc *, request_t *);
908158933Smjacob
909147883Sscottlenum {
910147883Sscottl	MPT_PRT_ALWAYS,
911147883Sscottl	MPT_PRT_FATAL,
912147883Sscottl	MPT_PRT_ERROR,
913147883Sscottl	MPT_PRT_WARN,
914147883Sscottl	MPT_PRT_INFO,
915159049Smjacob	MPT_PRT_NEGOTIATION,
916147883Sscottl	MPT_PRT_DEBUG,
917157117Smjacob	MPT_PRT_DEBUG1,
918157117Smjacob	MPT_PRT_DEBUG2,
919157117Smjacob	MPT_PRT_DEBUG3,
920155521Smjacob	MPT_PRT_TRACE,
921155521Smjacob	MPT_PRT_NONE=100
922147883Sscottl};
923147883Sscottl
924147883Sscottl#define mpt_lprt(mpt, level, ...)		\
925147883Sscottldo {						\
926147883Sscottl	if (level <= (mpt)->verbose)		\
927147883Sscottl		mpt_prt(mpt, __VA_ARGS__);	\
928147883Sscottl} while (0)
929147883Sscottl
930224493Smarius#if 0
931215325Smarius#define mpt_lprtc(mpt, level, ...)		\
932215325Smariusdo {						\
933215325Smarius	if (level <= (mpt)->verbose)		\
934215325Smarius		mpt_prtc(mpt, __VA_ARGS__);	\
935147883Sscottl} while (0)
936224493Smarius#endif
937245983Smarius
938157117Smjacobvoid mpt_prt(struct mpt_softc *, const char *, ...)
939157117Smjacob	__printflike(2, 3);
940157117Smjacobvoid mpt_prtc(struct mpt_softc *, const char *, ...)
941157117Smjacob	__printflike(2, 3);
942147883Sscottl
943157117Smjacob/**************************** Target Mode Related ***************************/
944157117Smjacobstatic __inline int mpt_cdblen(uint8_t, int);
945157117Smjacobstatic __inline int
946157117Smjacobmpt_cdblen(uint8_t cdb0, int maxlen)
947157117Smjacob{
948157117Smjacob	int group = cdb0 >> 5;
949157117Smjacob	switch (group) {
950157117Smjacob	case 0:
951157117Smjacob		return (6);
952157117Smjacob	case 1:
953157117Smjacob		return (10);
954157117Smjacob	case 4:
955157117Smjacob	case 5:
956157117Smjacob		return (12);
957157117Smjacob	default:
958157117Smjacob		return (16);
959157117Smjacob	}
960157117Smjacob}
961157117Smjacob#ifdef	INVARIANTS
962157117Smjacobstatic __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
963157117Smjacobstatic __inline request_t *
964157117Smjacobmpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
965157117Smjacob{
966157354Smjacob	uint16_t rtg = (tag >> 18);
967231629Smarius	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d", tag));
968157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
969157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
970157117Smjacob	return (mpt->tgt_cmd_ptrs[rtg]);
971157117Smjacob}
972213147Smarius#endif
973157662Smjacob
974157662Smjacobstatic __inline int
975157662Smjacobmpt_req_on_free_list(struct mpt_softc *, request_t *);
976157662Smjacobstatic __inline int
977157662Smjacobmpt_req_on_pending_list(struct mpt_softc *, request_t *);
978157662Smjacob
979157662Smjacob/*
980157662Smjacob * Is request on freelist?
981157662Smjacob */
982157662Smjacobstatic __inline int
983157662Smjacobmpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
984157662Smjacob{
985157662Smjacob	request_t *lrq;
986157662Smjacob
987157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
988157662Smjacob		if (lrq == req) {
989157662Smjacob			return (1);
990157662Smjacob		}
991157662Smjacob	}
992157662Smjacob	return (0);
993157662Smjacob}
994157662Smjacob
995157662Smjacob/*
996157662Smjacob * Is request on pending list?
997157662Smjacob */
998157662Smjacobstatic __inline int
999157662Smjacobmpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1000157662Smjacob{
1001157662Smjacob	request_t *lrq;
1002157662Smjacob
1003157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1004157662Smjacob		if (lrq == req) {
1005157662Smjacob			return (1);
1006157662Smjacob		}
1007157662Smjacob	}
1008157662Smjacob	return (0);
1009157662Smjacob}
1010157662Smjacob
1011213147Smarius#ifdef	INVARIANTS
1012213147Smariusstatic __inline void
1013213147Smariusmpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1014213147Smariusstatic __inline void
1015213147Smariusmpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1016213147Smarius
1017157662Smjacob/*
1018157662Smjacob * Make sure that req *is* part of one of the special lists
1019157662Smjacob */
1020157662Smjacobstatic __inline void
1021157662Smjacobmpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1022157662Smjacob{
1023157662Smjacob	int i;
1024157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1025157662Smjacob		if (req == mpt->els_cmd_ptrs[i]) {
1026157662Smjacob			return;
1027157662Smjacob		}
1028157662Smjacob	}
1029157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1030157662Smjacob		if (req == mpt->tgt_cmd_ptrs[i]) {
1031157662Smjacob			return;
1032157662Smjacob		}
1033157662Smjacob	}
1034231629Smarius	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs",
1035157662Smjacob	    s, line, req, req->serno,
1036157662Smjacob	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1037157662Smjacob}
1038157662Smjacob
1039157662Smjacob/*
1040157662Smjacob * Make sure that req is *not* part of one of the special lists.
1041157662Smjacob */
1042157662Smjacobstatic __inline void
1043157662Smjacobmpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1044157662Smjacob{
1045157662Smjacob	int i;
1046157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1047157662Smjacob		KASSERT(req != mpt->els_cmd_ptrs[i],
1048231629Smarius		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d",
1049157662Smjacob		    s, line, req, req->serno,
1050157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1051157662Smjacob	}
1052157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1053157662Smjacob		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1054231629Smarius		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d",
1055157662Smjacob		    s, line, req, req->serno,
1056157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1057157662Smjacob	}
1058157662Smjacob}
1059157117Smjacob#endif
1060147883Sscottl
1061160391Smjacob/*
1062160391Smjacob * Task Management Types, purely for internal consumption
1063160391Smjacob */
1064157117Smjacobtypedef enum {
1065157117Smjacob	MPT_ABORT_TASK_SET=1234,
1066157117Smjacob	MPT_CLEAR_TASK_SET,
1067157117Smjacob	MPT_TARGET_RESET,
1068157117Smjacob	MPT_CLEAR_ACA,
1069157117Smjacob	MPT_TERMINATE_TASK,
1070157117Smjacob	MPT_NIL_TMT_VALUE=5678
1071157117Smjacob} mpt_task_mgmt_t;
1072157117Smjacob
1073147883Sscottl/**************************** Unclassified Routines ***************************/
1074147883Sscottlvoid		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1075147883Sscottlint		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1076147883Sscottl					 size_t reply_len, void *reply);
1077147883Sscottlint		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1078147883Sscottl			     mpt_req_state_t state, mpt_req_state_t mask,
1079147883Sscottl			     int sleep_ok, int time_ms);
1080147883Sscottlvoid		mpt_enable_ints(struct mpt_softc *mpt);
1081147883Sscottlvoid		mpt_disable_ints(struct mpt_softc *mpt);
1082147883Sscottlint		mpt_attach(struct mpt_softc *mpt);
1083147883Sscottlint		mpt_shutdown(struct mpt_softc *mpt);
1084147883Sscottlint		mpt_detach(struct mpt_softc *mpt);
1085147883Sscottlint		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1086147883Sscottl				       size_t len, void *cmd);
1087147883Sscottlrequest_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1088147883Sscottlvoid		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1089147883Sscottlvoid		mpt_intr(void *arg);
1090147883Sscottlvoid		mpt_check_doorbell(struct mpt_softc *mpt);
1091147883Sscottlvoid		mpt_dump_reply_frame(struct mpt_softc *mpt,
1092147883Sscottl				     MSG_DEFAULT_REPLY *reply_frame);
1093147883Sscottl
1094147883Sscottlint		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1095170252Sscottl				  cfgparms_t *params,
1096147883Sscottl				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1097147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1098170252Sscottlint		mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion,
1099170252Sscottl				       int PageNumber, uint32_t PageAddress,
1100170252Sscottl				       int ExtPageType,
1101170252Sscottl				       CONFIG_EXTENDED_PAGE_HEADER *rslt,
1102170252Sscottl				       int sleep_ok, int timeout_ms);
1103170252Sscottlint		mpt_read_extcfg_page(struct mpt_softc *mpt, int Action,
1104170252Sscottl				     uint32_t PageAddress,
1105170252Sscottl				     CONFIG_EXTENDED_PAGE_HEADER *hdr,
1106170252Sscottl				     void *buf, size_t len, int sleep_ok,
1107170252Sscottl				     int timeout_ms);
1108147883Sscottlint		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1109147883Sscottl				    int /*PageNumber*/,
1110147883Sscottl				    uint32_t /*PageAddress*/,
1111147883Sscottl				    CONFIG_PAGE_HEADER *,
1112147883Sscottl				    int /*sleep_ok*/, int /*timeout_ms*/);
1113147883Sscottlint		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1114147883Sscottl				  uint32_t /*PageAddress*/,
1115147883Sscottl				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1116147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1117147883Sscottlint		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1118147883Sscottl				   uint32_t /*PageAddress*/,
1119147883Sscottl				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1120147883Sscottl				   int /*sleep_ok*/, int /*timeout_ms*/);
1121147883Sscottlstatic __inline int
1122147883Sscottlmpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1123147883Sscottl		      CONFIG_PAGE_HEADER *hdr, size_t len,
1124147883Sscottl		      int sleep_ok, int timeout_ms)
1125147883Sscottl{
1126147883Sscottl	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1127147883Sscottl				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1128147883Sscottl}
1129147883Sscottl
1130147883Sscottlstatic __inline int
1131147883Sscottlmpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1132147883Sscottl		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1133147883Sscottl		       int timeout_ms)
1134147883Sscottl{
1135147883Sscottl	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1136147883Sscottl				   PageAddress, hdr, len, sleep_ok,
1137147883Sscottl				   timeout_ms));
1138147883Sscottl}
1139101704Smjacob/* mpt_debug.c functions */
1140101704Smjacobvoid mpt_print_reply(void *vmsg);
1141147883Sscottlvoid mpt_print_db(uint32_t mb);
1142101704Smjacobvoid mpt_print_config_reply(void *vmsg);
1143147883Sscottlchar *mpt_ioc_diag(uint32_t diag);
1144147883Sscottlvoid mpt_req_state(mpt_req_state_t state);
1145101704Smjacobvoid mpt_print_config_request(void *vmsg);
1146101704Smjacobvoid mpt_print_request(void *vmsg);
1147155521Smjacobvoid mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1148101704Smjacob#endif /* _MPT_H_ */
1149