mpt.h revision 160290
1101704Smjacob/* $FreeBSD: head/sys/dev/mpt/mpt.h 160290 2006-07-12 07:48:50Z mjacob $ */
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/types.h>
104147883Sscottl#include <sys/param.h>
105147883Sscottl#include <sys/systm.h>
106147883Sscottl#include <sys/endian.h>
107147883Sscottl#include <sys/eventhandler.h>
108147883Sscottl#if __FreeBSD_version < 500000
109147883Sscottl#include <sys/kernel.h>
110147883Sscottl#include <sys/queue.h>
111147883Sscottl#include <sys/malloc.h>
112147883Sscottl#else
113147883Sscottl#include <sys/lock.h>
114147883Sscottl#include <sys/kernel.h>
115147883Sscottl#include <sys/queue.h>
116147883Sscottl#include <sys/malloc.h>
117147883Sscottl#include <sys/mutex.h>
118147883Sscottl#include <sys/condvar.h>
119147883Sscottl#endif
120147883Sscottl#include <sys/proc.h>
121147883Sscottl#include <sys/bus.h>
122147883Sscottl#include <sys/module.h>
123147883Sscottl
124147883Sscottl#include <machine/cpu.h>
125147883Sscottl#include <machine/resource.h>
126147883Sscottl
127157117Smjacob#if __FreeBSD_version < 500000
128157117Smjacob#include <machine/bus.h>
129159179Smjacob#include <machine/clock.h>
130157117Smjacob#endif
131157117Smjacob
132147883Sscottl#include <sys/rman.h>
133147883Sscottl
134155521Smjacob#if __FreeBSD_version < 500000
135155521Smjacob#include <pci/pcireg.h>
136155521Smjacob#include <pci/pcivar.h>
137155521Smjacob#else
138155521Smjacob#include <dev/pci/pcireg.h>
139155521Smjacob#include <dev/pci/pcivar.h>
140155521Smjacob#endif
141155521Smjacob
142155521Smjacob#include <machine/bus.h>
143147883Sscottl#include "opt_ddb.h"
144147883Sscottl
145147883Sscottl/**************************** Register Definitions ****************************/
146147883Sscottl#include <dev/mpt/mpt_reg.h>
147147883Sscottl
148147883Sscottl/******************************* MPI Definitions ******************************/
149147883Sscottl#include <dev/mpt/mpilib/mpi_type.h>
150147883Sscottl#include <dev/mpt/mpilib/mpi.h>
151147883Sscottl#include <dev/mpt/mpilib/mpi_cnfg.h>
152147883Sscottl#include <dev/mpt/mpilib/mpi_ioc.h>
153147883Sscottl#include <dev/mpt/mpilib/mpi_raid.h>
154147883Sscottl
155147883Sscottl/* XXX For mpt_debug.c */
156147883Sscottl#include <dev/mpt/mpilib/mpi_init.h>
157147883Sscottl
158147883Sscottl/****************************** Misc Definitions ******************************/
159101704Smjacob#define MPT_OK (0)
160101704Smjacob#define MPT_FAIL (0x10000)
161101704Smjacob
162147883Sscottl#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
163101704Smjacob
164157117Smjacob#define	MPT_ROLE_NONE		0
165157117Smjacob#define	MPT_ROLE_INITIATOR	1
166157117Smjacob#define	MPT_ROLE_TARGET		2
167157117Smjacob#define	MPT_ROLE_BOTH		3
168157117Smjacob#define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
169157117Smjacob
170147883Sscottl/**************************** Forward Declarations ****************************/
171147883Sscottlstruct mpt_softc;
172147883Sscottlstruct mpt_personality;
173147883Sscottltypedef struct req_entry request_t;
174101704Smjacob
175147883Sscottl/************************* Personality Module Support *************************/
176147883Sscottltypedef int mpt_load_handler_t(struct mpt_personality *);
177147883Sscottltypedef int mpt_probe_handler_t(struct mpt_softc *);
178147883Sscottltypedef int mpt_attach_handler_t(struct mpt_softc *);
179157117Smjacobtypedef int mpt_enable_handler_t(struct mpt_softc *);
180147883Sscottltypedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
181147883Sscottl				MSG_EVENT_NOTIFY_REPLY *);
182147883Sscottltypedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
183147883Sscottl/* XXX Add return value and use for veto? */
184147883Sscottltypedef void mpt_shutdown_handler_t(struct mpt_softc *);
185147883Sscottltypedef void mpt_detach_handler_t(struct mpt_softc *);
186147883Sscottltypedef int mpt_unload_handler_t(struct mpt_personality *);
187147883Sscottl
188147883Sscottlstruct mpt_personality
189147883Sscottl{
190147883Sscottl	const char		*name;
191147883Sscottl	uint32_t		 id;		/* Assigned identifier. */
192147883Sscottl	u_int			 use_count;	/* Instances using personality*/
193147883Sscottl	mpt_load_handler_t	*load;		/* configure personailty */
194147883Sscottl#define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
195147883Sscottl	mpt_probe_handler_t	*probe;		/* configure personailty */
196147883Sscottl	mpt_attach_handler_t	*attach;	/* initialize device instance */
197157117Smjacob	mpt_enable_handler_t	*enable;	/* enable device */
198147883Sscottl	mpt_event_handler_t	*event;		/* Handle MPI event. */
199147883Sscottl	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
200147883Sscottl	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
201147883Sscottl	mpt_detach_handler_t	*detach;	/* release device instance */
202147883Sscottl	mpt_unload_handler_t	*unload;	/* Shutdown personality */
203147883Sscottl#define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
204101704Smjacob};
205101704Smjacob
206147883Sscottlint mpt_modevent(module_t, int, void *);
207101704Smjacob
208147883Sscottl/* Maximum supported number of personalities. */
209147883Sscottl#define MPT_MAX_PERSONALITIES	(15)
210101704Smjacob
211147883Sscottl#define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
212147883Sscottl	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
213101704Smjacob
214147883Sscottl#define DECLARE_MPT_PERSONALITY(name, order)				  \
215147883Sscottl	static moduledata_t name##_mod = {				  \
216147883Sscottl		#name, mpt_modevent, &name##_personality		  \
217147883Sscottl	};								  \
218147883Sscottl	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
219147883Sscottl	MODULE_VERSION(name, 1);					  \
220147883Sscottl	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
221147883Sscottl
222147883Sscottl/******************************* Bus DMA Support ******************************/
223147883Sscottl/* XXX Need to update bus_dmamap_sync to take a range argument. */
224147883Sscottl#define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
225147883Sscottl	bus_dmamap_sync(dma_tag, dmamap, op)
226147883Sscottl
227147883Sscottl#if __FreeBSD_version >= 501102
228147883Sscottl#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
229147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
230147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
231147883Sscottl			   dma_tagp)					\
232147883Sscottl	bus_dma_tag_create(parent_tag, alignment, boundary,		\
233147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
234147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
235147883Sscottl			   busdma_lock_mutex, &Giant,			\
236147883Sscottl			   dma_tagp)
237147883Sscottl#else
238147883Sscottl#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
239147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
240147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
241147883Sscottl			   dma_tagp)					\
242147883Sscottl	bus_dma_tag_create(parent_tag, alignment, boundary,		\
243147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
244147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
245147883Sscottl			   dma_tagp)
246147883Sscottl#endif
247147883Sscottl
248147883Sscottlstruct mpt_map_info {
249147883Sscottl	struct mpt_softc *mpt;
250147883Sscottl	int		  error;
251147883Sscottl	uint32_t	  phys;
252147883Sscottl};
253147883Sscottl
254147883Sscottlvoid mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
255147883Sscottl
256147883Sscottl/**************************** Kernel Thread Support ***************************/
257147883Sscottl#if __FreeBSD_version > 500005
258147883Sscottl#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
259147883Sscottl	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
260147883Sscottl#else
261147883Sscottl#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
262147883Sscottl	kthread_create(func, farg, proc_ptr, fmtstr, arg)
263147883Sscottl#endif
264147883Sscottl
265147883Sscottl/****************************** Timer Facilities ******************************/
266147883Sscottl#if __FreeBSD_version > 500000
267147883Sscottl#define mpt_callout_init(c)	callout_init(c, /*mpsafe*/0);
268147883Sscottl#else
269147883Sscottl#define mpt_callout_init(c)	callout_init(c);
270147883Sscottl#endif
271147883Sscottl
272147883Sscottl/********************************** Endianess *********************************/
273147883Sscottlstatic __inline uint64_t
274147883Sscottlu64toh(U64 s)
275147883Sscottl{
276147883Sscottl	uint64_t result;
277147883Sscottl
278147883Sscottl	result = le32toh(s.Low);
279147883Sscottl	result |= ((uint64_t)le32toh(s.High)) << 32;
280147883Sscottl	return (result);
281147883Sscottl}
282147883Sscottl
283147883Sscottl/**************************** MPI Transaction State ***************************/
284147883Sscottltypedef enum {
285157354Smjacob	REQ_STATE_NIL		= 0x00,
286157354Smjacob	REQ_STATE_FREE		= 0x01,
287157354Smjacob	REQ_STATE_ALLOCATED	= 0x02,
288157354Smjacob	REQ_STATE_QUEUED	= 0x04,
289157354Smjacob	REQ_STATE_DONE		= 0x08,
290157354Smjacob	REQ_STATE_TIMEDOUT	= 0x10,
291157354Smjacob	REQ_STATE_NEED_WAKEUP	= 0x20,
292157354Smjacob	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
293147883Sscottl	REQ_STATE_MASK		= 0xFF
294147883Sscottl} mpt_req_state_t;
295147883Sscottl
296147883Sscottlstruct req_entry {
297147883Sscottl	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
298147883Sscottl	mpt_req_state_t	state;		/* Request State Information */
299147883Sscottl	uint16_t	index;		/* Index of this entry */
300147883Sscottl	uint16_t	IOCStatus;	/* Completion status */
301157662Smjacob	uint16_t	ResponseCode;	/* TMF Reponse Code */
302157662Smjacob	uint16_t	serno;		/* serial number */
303147883Sscottl	union ccb      *ccb;		/* CAM request */
304147883Sscottl	void	       *req_vbuf;	/* Virtual Address of Entry */
305147883Sscottl	void	       *sense_vbuf;	/* Virtual Address of sense data */
306147883Sscottl	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
307147883Sscottl	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
308159312Smjacob	bus_dmamap_t	dmap;		/* DMA map for data buffers */
309155521Smjacob	struct req_entry *chain;	/* for SGE overallocations */
310147883Sscottl};
311147883Sscottl
312157117Smjacob/**************************** MPI Target State Info ***************************/
313157117Smjacob
314157117Smjacobtypedef struct {
315157117Smjacob	uint32_t reply_desc;	/* current reply descriptor */
316157117Smjacob	uint32_t resid;		/* current data residual */
317157117Smjacob	uint32_t bytes_xfered;	/* current relative offset */
318157117Smjacob	union ccb *ccb;		/* pointer to currently active ccb */
319157117Smjacob	request_t *req;		/* pointer to currently active assist request */
320157117Smjacob	int	nxfers;
321157354Smjacob	uint32_t tag_id;
322157117Smjacob	enum {
323157117Smjacob		TGT_STATE_NIL,
324157354Smjacob		TGT_STATE_LOADING,
325157117Smjacob		TGT_STATE_LOADED,
326157117Smjacob		TGT_STATE_IN_CAM,
327157117Smjacob                TGT_STATE_SETTING_UP_FOR_DATA,
328157117Smjacob                TGT_STATE_MOVING_DATA,
329157117Smjacob                TGT_STATE_MOVING_DATA_AND_STATUS,
330157117Smjacob                TGT_STATE_SENDING_STATUS
331157117Smjacob	} state;
332157117Smjacob} mpt_tgt_state_t;
333157117Smjacob
334157117Smjacob/*
335157117Smjacob * When we get an incoming command it has its own tag which is called the
336157117Smjacob * IoIndex. This is the value we gave that particular command buffer when
337157117Smjacob * we originally assigned it. It's just a number, really. The FC card uses
338157117Smjacob * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
339157117Smjacob * contains pointers the request_t structures related to that IoIndex.
340157117Smjacob *
341157117Smjacob * What *we* do is construct a tag out of the index for the target command
342157117Smjacob * which owns the incoming ATIO plus a rolling sequence number.
343157117Smjacob */
344157117Smjacob#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
345157354Smjacob ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
346157117Smjacob
347157117Smjacob#ifdef	INVARIANTS
348157117Smjacob#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
349157117Smjacob#else
350157354Smjacob#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
351157117Smjacob#endif
352157117Smjacob
353157117Smjacob#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
354157117Smjacob    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
355157117Smjacob
356157117SmjacobSTAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
357157117Smjacob#define	MPT_MAX_LUNS	256
358157117Smjacobtypedef struct {
359157117Smjacob	struct mpt_hdr_stailq	atios;
360157117Smjacob	struct mpt_hdr_stailq	inots;
361157117Smjacob	int enabled;
362157117Smjacob} tgt_resource_t;
363157662Smjacob#define	MPT_MAX_ELS	64
364157117Smjacob
365147883Sscottl/**************************** Handler Registration ****************************/
366101704Smjacob/*
367147883Sscottl * Global table of registered reply handlers.  The
368147883Sscottl * handler is indicated by byte 3 of the request
369147883Sscottl * index submitted to the IOC.  This allows the
370147883Sscottl * driver core to perform generic processing without
371147883Sscottl * any knowledge of per-personality behavior.
372147883Sscottl *
373147883Sscottl * MPT_NUM_REPLY_HANDLERS must be a power of 2
374147883Sscottl * to allow the easy generation of a mask.
375147883Sscottl *
376147883Sscottl * The handler offsets used by the core are hard coded
377147883Sscottl * allowing faster code generation when assigning a handler
378147883Sscottl * to a request.  All "personalities" must use the
379147883Sscottl * the handler registration mechanism.
380147883Sscottl *
381147883Sscottl * The IOC handlers that are rarely executed are placed
382147883Sscottl * at the tail of the table to make it more likely that
383147883Sscottl * all commonly executed handlers fit in a single cache
384147883Sscottl * line.
385101704Smjacob */
386157117Smjacob#define MPT_NUM_REPLY_HANDLERS		(32)
387147883Sscottl#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
388147883Sscottl#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
389147883Sscottl#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
390147883Sscottltypedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
391157117Smjacob    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
392147883Sscottltypedef union {
393147883Sscottl	mpt_reply_handler_t	*reply_handler;
394147883Sscottl} mpt_handler_t;
395101704Smjacob
396147883Sscottltypedef enum {
397147883Sscottl	MPT_HANDLER_REPLY,
398147883Sscottl	MPT_HANDLER_EVENT,
399147883Sscottl	MPT_HANDLER_RESET,
400147883Sscottl	MPT_HANDLER_SHUTDOWN
401147883Sscottl} mpt_handler_type;
402101704Smjacob
403147883Sscottlstruct mpt_handler_record
404147883Sscottl{
405147883Sscottl	LIST_ENTRY(mpt_handler_record)	links;
406147883Sscottl	mpt_handler_t			handler;
407101704Smjacob};
408101704Smjacob
409147883SscottlLIST_HEAD(mpt_handler_list, mpt_handler_record);
410147883Sscottl
411147883Sscottl/*
412147883Sscottl * The handler_id is currently unused but would contain the
413147883Sscottl * handler ID used in the MsgContext field to allow direction
414147883Sscottl * of replies to the handler.  Registrations that don't require
415147883Sscottl * a handler id can pass in NULL for the handler_id.
416147883Sscottl *
417147883Sscottl * Deregistrations for handlers without a handler id should
418147883Sscottl * pass in MPT_HANDLER_ID_NONE.
419147883Sscottl */
420147883Sscottl#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
421147883Sscottlint mpt_register_handler(struct mpt_softc *, mpt_handler_type,
422147883Sscottl			 mpt_handler_t, uint32_t *);
423147883Sscottlint mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
424147883Sscottl			   mpt_handler_t, uint32_t);
425147883Sscottl
426147883Sscottl/******************* Per-Controller Instance Data Structures ******************/
427147883SscottlTAILQ_HEAD(req_queue, req_entry);
428147883Sscottl
429147883Sscottl/* Structure for saving proper values for modifyable PCI config registers */
430147883Sscottlstruct mpt_pci_cfg {
431147883Sscottl	uint16_t Command;
432147883Sscottl	uint16_t LatencyTimer_LineSize;
433147883Sscottl	uint32_t IO_BAR;
434147883Sscottl	uint32_t Mem0_BAR[2];
435147883Sscottl	uint32_t Mem1_BAR[2];
436147883Sscottl	uint32_t ROM_BAR;
437147883Sscottl	uint8_t  IntLine;
438147883Sscottl	uint32_t PMCSR;
439101704Smjacob};
440101704Smjacob
441147883Sscottltypedef enum {
442147883Sscottl	MPT_RVF_NONE		= 0x0,
443147883Sscottl	MPT_RVF_ACTIVE		= 0x1,
444147883Sscottl	MPT_RVF_ANNOUNCED	= 0x2,
445147883Sscottl	MPT_RVF_UP2DATE		= 0x4,
446147883Sscottl	MPT_RVF_REFERENCED	= 0x8,
447147883Sscottl	MPT_RVF_WCE_CHANGED	= 0x10
448147883Sscottl} mpt_raid_volume_flags;
449101704Smjacob
450147883Sscottlstruct mpt_raid_volume {
451147883Sscottl	CONFIG_PAGE_RAID_VOL_0	       *config_page;
452147883Sscottl	MPI_RAID_VOL_INDICATOR		sync_progress;
453147883Sscottl	mpt_raid_volume_flags		flags;
454158933Smjacob	u_int				quiesced_disks;
455101704Smjacob};
456101704Smjacob
457147883Sscottltypedef enum {
458147883Sscottl	MPT_RDF_NONE		= 0x00,
459147883Sscottl	MPT_RDF_ACTIVE		= 0x01,
460147883Sscottl	MPT_RDF_ANNOUNCED	= 0x02,
461147883Sscottl	MPT_RDF_UP2DATE		= 0x04,
462147883Sscottl	MPT_RDF_REFERENCED	= 0x08,
463147883Sscottl	MPT_RDF_QUIESCING	= 0x10,
464147883Sscottl	MPT_RDF_QUIESCED	= 0x20
465147883Sscottl} mpt_raid_disk_flags;
466147883Sscottl
467147883Sscottlstruct mpt_raid_disk {
468147883Sscottl	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
469147883Sscottl	struct mpt_raid_volume	       *volume;
470147883Sscottl	u_int				member_number;
471147883Sscottl	u_int				pass_thru_active;
472147883Sscottl	mpt_raid_disk_flags		flags;
473101704Smjacob};
474101704Smjacob
475147883Sscottlstruct mpt_evtf_record {
476147883Sscottl	MSG_EVENT_NOTIFY_REPLY		reply;
477147883Sscottl	uint32_t			context;
478147883Sscottl	LIST_ENTRY(mpt_evtf_record)	links;
479147883Sscottl};
480101704Smjacob
481147883SscottlLIST_HEAD(mpt_evtf_list, mpt_evtf_record);
482101704Smjacob
483147883Sscottlstruct mpt_softc {
484147883Sscottl	device_t		dev;
485147883Sscottl#if __FreeBSD_version < 500000
486157662Smjacob	uint32_t		mpt_islocked;
487147883Sscottl	int			mpt_splsaved;
488147883Sscottl#else
489147883Sscottl	struct mtx		mpt_lock;
490157662Smjacob	int			mpt_locksetup;
491147883Sscottl#endif
492147883Sscottl	uint32_t		mpt_pers_mask;
493160290Smjacob	uint32_t
494157117Smjacob		unit		: 8,
495160290Smjacob				: 3,
496157117Smjacob		twildcard	: 1,
497157117Smjacob		tenabled	: 1,
498160290Smjacob		do_cfg_role	: 1,
499158982Smjacob		raid_enabled	: 1,
500147883Sscottl		raid_mwce_set	: 1,
501147883Sscottl		getreqwaiter	: 1,
502147883Sscottl		shutdwn_raid    : 1,
503147883Sscottl		shutdwn_recovery: 1,
504147883Sscottl		outofbeer	: 1,
505147883Sscottl		disabled	: 1,
506159178Smjacob		is_spi		: 1,
507157117Smjacob		is_sas		: 1,
508157117Smjacob		is_fc		: 1;
509101704Smjacob
510160290Smjacob	u_int			cfg_role;
511160290Smjacob	u_int			role;	/* role: none, ini, target, both */
512160290Smjacob
513147883Sscottl	u_int			verbose;
514101704Smjacob
515147883Sscottl	/*
516147883Sscottl	 * IOC Facts
517147883Sscottl	 */
518147883Sscottl	uint16_t	mpt_global_credits;
519147883Sscottl	uint16_t	request_frame_size;
520147883Sscottl	uint8_t		mpt_max_devices;
521147883Sscottl	uint8_t		mpt_max_buses;
522155521Smjacob	uint8_t		ioc_facts_flags;
523157354Smjacob	uint8_t		padding0;
524101704Smjacob
525147883Sscottl	/*
526147883Sscottl	 * Port Facts
527147883Sscottl	 * XXX - Add multi-port support!.
528147883Sscottl	 */
529147883Sscottl	uint16_t	mpt_ini_id;
530147883Sscottl	uint16_t	mpt_port_type;
531147883Sscottl	uint16_t	mpt_proto_flags;
532157117Smjacob	uint16_t	mpt_max_tgtcmds;
533102822Smjacob
534147883Sscottl	/*
535147883Sscottl	 * Device Configuration Information
536147883Sscottl	 */
537147883Sscottl	union {
538147883Sscottl		struct mpt_spi_cfg {
539147883Sscottl			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
540147883Sscottl			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
541147883Sscottl			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
542147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
543147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
544147883Sscottl			uint16_t			_tag_enable;
545147883Sscottl			uint16_t			_disc_enable;
546147883Sscottl		} spi;
547147883Sscottl#define	mpt_port_page0		cfg.spi._port_page0
548147883Sscottl#define	mpt_port_page1		cfg.spi._port_page1
549147883Sscottl#define	mpt_port_page2		cfg.spi._port_page2
550147883Sscottl#define	mpt_dev_page0		cfg.spi._dev_page0
551147883Sscottl#define	mpt_dev_page1		cfg.spi._dev_page1
552147883Sscottl#define	mpt_tag_enable		cfg.spi._tag_enable
553147883Sscottl#define	mpt_disc_enable		cfg.spi._disc_enable
554147883Sscottl		struct mpi_fc_cfg {
555157117Smjacob			CONFIG_PAGE_FC_PORT_0 _port_page0;
556159919Smjacob			uint32_t _port_speed;
557157117Smjacob#define	mpt_fcport_page0	cfg.fc._port_page0
558159919Smjacob#define	mpt_fcport_speed	cfg.fc._port_speed
559147883Sscottl		} fc;
560147883Sscottl	} cfg;
561147883Sscottl
562158982Smjacob	/* Controller Info for RAID information */
563147883Sscottl	CONFIG_PAGE_IOC_2 *	ioc_page2;
564147883Sscottl	CONFIG_PAGE_IOC_3 *	ioc_page3;
565147883Sscottl
566147883Sscottl	/* Raid Data */
567147883Sscottl	struct mpt_raid_volume* raid_volumes;
568147883Sscottl	struct mpt_raid_disk*	raid_disks;
569147883Sscottl	u_int			raid_max_volumes;
570147883Sscottl	u_int			raid_max_disks;
571147883Sscottl	u_int			raid_page0_len;
572147883Sscottl	u_int			raid_wakeup;
573147883Sscottl	u_int			raid_rescan;
574147883Sscottl	u_int			raid_resync_rate;
575147883Sscottl	u_int			raid_mwce_setting;
576147883Sscottl	u_int			raid_queue_depth;
577152444Skan	u_int			raid_nonopt_volumes;
578147883Sscottl	struct proc	       *raid_thread;
579147883Sscottl	struct callout		raid_timer;
580147883Sscottl
581147883Sscottl	/*
582147883Sscottl	 * PCI Hardware info
583147883Sscottl	 */
584147883Sscottl	struct resource *	pci_irq;	/* Interrupt map for chip */
585147883Sscottl	void *			ih;		/* Interupt handle */
586147883Sscottl	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
587147883Sscottl
588147883Sscottl	/*
589147883Sscottl	 * DMA Mapping Stuff
590147883Sscottl	 */
591147883Sscottl	struct resource *	pci_reg;	/* Register map for chip */
592147883Sscottl	int			pci_mem_rid;	/* Resource ID */
593147883Sscottl	bus_space_tag_t		pci_st;		/* Bus tag for registers */
594147883Sscottl	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
595147883Sscottl	/* PIO versions of above. */
596147883Sscottl	int			pci_pio_rid;
597147883Sscottl	struct resource *	pci_pio_reg;
598147883Sscottl	bus_space_tag_t		pci_pio_st;
599147883Sscottl	bus_space_handle_t	pci_pio_sh;
600147883Sscottl
601147883Sscottl	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
602147883Sscottl	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
603147883Sscottl	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
604147883Sscottl	uint8_t		       *reply;		/* KVA of reply memory */
605147883Sscottl	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
606147883Sscottl
607147883Sscottl	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
608147883Sscottl	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
609147883Sscottl	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
610147883Sscottl	uint8_t		       *request;	/* KVA of Request memory */
611157117Smjacob	bus_addr_t		request_phys;	/* BusAddr of request memory */
612147883Sscottl
613155521Smjacob	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
614155521Smjacob
615155521Smjacob	/*
616155521Smjacob	 * Hardware management
617155521Smjacob	 */
618147883Sscottl	u_int			reset_cnt;
619147883Sscottl
620147883Sscottl	/*
621147883Sscottl	 * CAM && Software Management
622147883Sscottl	 */
623147883Sscottl	request_t	       *request_pool;
624147883Sscottl	struct req_queue	request_free_list;
625147883Sscottl	struct req_queue	request_pending_list;
626147883Sscottl	struct req_queue	request_timeout_list;
627147883Sscottl
628147883Sscottl
629147883Sscottl	struct cam_sim	       *sim;
630147883Sscottl	struct cam_path	       *path;
631147883Sscottl
632147883Sscottl	struct cam_sim	       *phydisk_sim;
633147883Sscottl	struct cam_path	       *phydisk_path;
634147883Sscottl
635147883Sscottl	struct proc	       *recovery_thread;
636147883Sscottl	request_t	       *tmf_req;
637147883Sscottl
638157117Smjacob	/*
639157354Smjacob	 * Deferred frame acks due to resource shortage.
640157354Smjacob	 */
641157354Smjacob	struct mpt_evtf_list	ack_frames;
642159312Smjacob
643157354Smjacob	/*
644157117Smjacob	 * Target Mode Support
645157117Smjacob	 */
646157117Smjacob	uint32_t		scsi_tgt_handler_id;
647157117Smjacob	request_t **		tgt_cmd_ptrs;
648157662Smjacob	request_t **		els_cmd_ptrs;	/* FC only */
649147883Sscottl
650157117Smjacob	/*
651157117Smjacob	 * *snork*- this is chosen to be here *just in case* somebody
652157117Smjacob	 * forgets to point to it exactly and we index off of trt with
653157117Smjacob	 * CAM_LUN_WILDCARD.
654157117Smjacob	 */
655157117Smjacob	tgt_resource_t		trt_wildcard;	/* wildcard luns */
656157117Smjacob	tgt_resource_t		trt[MPT_MAX_LUNS];
657157117Smjacob	uint16_t		tgt_cmds_allocated;
658157662Smjacob	uint16_t		els_cmds_allocated;	/* FC only */
659157117Smjacob
660157117Smjacob	uint16_t		timeouts;	/* timeout count */
661157117Smjacob	uint16_t		success;	/* successes afer timeout */
662157662Smjacob	uint16_t		sequence;	/* Sequence Number */
663157662Smjacob	uint16_t		pad3;
664157117Smjacob
665157117Smjacob
666159312Smjacob	/* Paired port in some dual adapters configurations */
667147883Sscottl	struct mpt_softc *	mpt2;
668147883Sscottl
669147883Sscottl	/* FW Image management */
670147883Sscottl	uint32_t		fw_image_size;
671147883Sscottl	uint8_t		       *fw_image;
672147883Sscottl	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
673147883Sscottl	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
674159178Smjacob	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
675147883Sscottl
676147883Sscottl	/* Shutdown Event Handler. */
677147883Sscottl	eventhandler_tag         eh;
678147883Sscottl
679147883Sscottl	TAILQ_ENTRY(mpt_softc)	links;
680147883Sscottl};
681147883Sscottl
682157662Smjacobstatic __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
683157662Smjacob
684157662Smjacobstatic __inline void
685157662Smjacobmpt_assign_serno(struct mpt_softc *mpt, request_t *req)
686157662Smjacob{
687157662Smjacob	if ((req->serno = mpt->sequence++) == 0) {
688157662Smjacob		req->serno = mpt->sequence++;
689157662Smjacob	}
690157662Smjacob}
691157662Smjacob
692157117Smjacob/***************************** Locking Primitives *****************************/
693147883Sscottl#if __FreeBSD_version < 500000
694147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM
695147883Sscottl#define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
696147883Sscottl#define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
697157662Smjacob#define	MPT_OWNED(mpt)		mpt->mpt_islocked
698147883Sscottl#define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
699147883Sscottl#define	CAMLOCK_2_MPTLOCK	MPT_LOCK
700147883Sscottl#define	MPT_LOCK_SETUP(mpt)
701147883Sscottl#define	MPT_LOCK_DESTROY(mpt)
702147883Sscottl
703147883Sscottlstatic __inline void mpt_lockspl(struct mpt_softc *mpt);
704147883Sscottlstatic __inline void mpt_unlockspl(struct mpt_softc *mpt);
705147883Sscottl
706147883Sscottlstatic __inline void
707147883Sscottlmpt_lockspl(struct mpt_softc *mpt)
708147883Sscottl{
709147883Sscottl       int s;
710147883Sscottl
711147883Sscottl       s = splcam();
712147883Sscottl       if (mpt->mpt_islocked++ == 0) {
713147883Sscottl               mpt->mpt_splsaved = s;
714147883Sscottl       } else {
715147883Sscottl               splx(s);
716147883Sscottl	       panic("Recursed lock with mask: 0x%x\n", s);
717147883Sscottl       }
718147883Sscottl}
719147883Sscottl
720147883Sscottlstatic __inline void
721147883Sscottlmpt_unlockspl(struct mpt_softc *mpt)
722147883Sscottl{
723147883Sscottl       if (mpt->mpt_islocked) {
724147883Sscottl               if (--mpt->mpt_islocked == 0) {
725147883Sscottl                       splx(mpt->mpt_splsaved);
726147883Sscottl               }
727147883Sscottl       } else
728147883Sscottl	       panic("Negative lock count\n");
729147883Sscottl}
730147883Sscottl
731147883Sscottlstatic __inline int
732147883Sscottlmpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
733147883Sscottl	   const char *wmesg, int timo)
734147883Sscottl{
735147883Sscottl	int saved_cnt;
736147883Sscottl	int saved_spl;
737147883Sscottl	int error;
738147883Sscottl
739147883Sscottl	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
740147883Sscottl	saved_cnt = mpt->mpt_islocked;
741147883Sscottl	saved_spl = mpt->mpt_splsaved;
742147883Sscottl	mpt->mpt_islocked = 0;
743147883Sscottl	error = tsleep(ident, priority, wmesg, timo);
744157117Smjacob	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
745147883Sscottl	mpt->mpt_islocked = saved_cnt;
746147883Sscottl	mpt->mpt_splsaved = saved_spl;
747147883Sscottl	return (error);
748147883Sscottl}
749147883Sscottl
750147883Sscottl#else
751153072Sru#ifdef	LOCKING_WORKED_AS_IT_SHOULD
752147883Sscottl#error "Shouldn't Be Here!"
753147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
754147883Sscottl#define	MPT_LOCK_SETUP(mpt)						\
755147883Sscottl		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
756147883Sscottl		mpt->mpt_locksetup = 1
757147883Sscottl#define	MPT_LOCK_DESTROY(mpt)						\
758147883Sscottl	if (mpt->mpt_locksetup) {					\
759147883Sscottl		mtx_destroy(&mpt->mpt_lock);				\
760147883Sscottl		mpt->mpt_locksetup = 0;					\
761147883Sscottl	}
762147883Sscottl
763147883Sscottl#define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
764147883Sscottl#define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
765157662Smjacob#define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
766147883Sscottl#define	MPTLOCK_2_CAMLOCK(mpt)	\
767147883Sscottl	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
768147883Sscottl#define	CAMLOCK_2_MPTLOCK(mpt)	\
769147883Sscottl	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
770147883Sscottl#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
771147883Sscottl	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
772157662Smjacob
773147883Sscottl#else
774157662Smjacob
775147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
776147883Sscottl#define	MPT_LOCK_SETUP(mpt)	do { } while (0)
777147883Sscottl#define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
778157662Smjacob#if	0
779157662Smjacob#define	MPT_LOCK(mpt)		\
780157662Smjacob	device_printf(mpt->dev, "LOCK %s:%d\n", __FILE__, __LINE__); 	\
781157662Smjacob	KASSERT(mpt->mpt_locksetup == 0,				\
782157662Smjacob	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
783157662Smjacob	mpt->mpt_locksetup = 1
784157662Smjacob#define	MPT_UNLOCK(mpt)		\
785157662Smjacob	device_printf(mpt->dev, "UNLK %s:%d\n", __FILE__, __LINE__); 	\
786157662Smjacob	KASSERT(mpt->mpt_locksetup == 1,				\
787157662Smjacob	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
788157662Smjacob	mpt->mpt_locksetup = 0
789157662Smjacob#else
790157662Smjacob#define	MPT_LOCK(mpt)							\
791157662Smjacob	KASSERT(mpt->mpt_locksetup == 0,				\
792157662Smjacob	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
793157662Smjacob	mpt->mpt_locksetup = 1
794157662Smjacob#define	MPT_UNLOCK(mpt)							\
795157662Smjacob	KASSERT(mpt->mpt_locksetup == 1,				\
796157662Smjacob	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
797157662Smjacob	mpt->mpt_locksetup = 0
798147883Sscottl#endif
799157662Smjacob#define	MPT_OWNED(mpt)		mpt->mpt_locksetup
800157662Smjacob#define	MPTLOCK_2_CAMLOCK(mpt)	MPT_UNLOCK(mpt)
801157662Smjacob#define	CAMLOCK_2_MPTLOCK(mpt)	MPT_LOCK(mpt)
802157662Smjacob
803157662Smjacobstatic __inline int
804157662Smjacobmpt_sleep(struct mpt_softc *, void *, int, const char *, int);
805157662Smjacob
806157662Smjacobstatic __inline int
807157662Smjacobmpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
808157662Smjacob{
809157662Smjacob	int r;
810157662Smjacob	MPT_UNLOCK(mpt);
811157662Smjacob	r = tsleep(i, p, w, t);
812157662Smjacob	MPT_LOCK(mpt);
813157662Smjacob	return (r);
814157662Smjacob}
815147883Sscottl#endif
816157662Smjacob#endif
817147883Sscottl
818147883Sscottl/******************************* Register Access ******************************/
819147883Sscottlstatic __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
820147883Sscottlstatic __inline uint32_t mpt_read(struct mpt_softc *, int);
821147883Sscottlstatic __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
822147883Sscottlstatic __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
823147883Sscottl
824147883Sscottlstatic __inline void
825147883Sscottlmpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
826147883Sscottl{
827147883Sscottl	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
828147883Sscottl}
829147883Sscottl
830147883Sscottlstatic __inline uint32_t
831147883Sscottlmpt_read(struct mpt_softc *mpt, int offset)
832147883Sscottl{
833147883Sscottl	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
834147883Sscottl}
835147883Sscottl
836147883Sscottl/*
837147883Sscottl * Some operations (e.g. diagnostic register writes while the ARM proccessor
838147883Sscottl * is disabled), must be performed using "PCI pio" operations.  On non-PCI
839147883Sscottl * busses, these operations likely map to normal register accesses.
840147883Sscottl */
841147883Sscottlstatic __inline void
842147883Sscottlmpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
843147883Sscottl{
844147883Sscottl	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
845147883Sscottl}
846147883Sscottl
847147883Sscottlstatic __inline uint32_t
848147883Sscottlmpt_pio_read(struct mpt_softc *mpt, int offset)
849147883Sscottl{
850147883Sscottl	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
851147883Sscottl}
852147883Sscottl/*********************** Reply Frame/Request Management ***********************/
853147883Sscottl/* Max MPT Reply we are willing to accept (must be power of 2) */
854155521Smjacob#define MPT_REPLY_SIZE   	256
855147883Sscottl
856157117Smjacob/*
857157117Smjacob * Must be less than 16384 in order for target mode to work
858157117Smjacob */
859155521Smjacob#define MPT_MAX_REQUESTS(mpt)	512
860155521Smjacob#define MPT_REQUEST_AREA	512
861155521Smjacob#define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
862147883Sscottl#define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
863147883Sscottl
864157354Smjacob#define MPT_CONTEXT_CB_SHIFT	(16)
865157117Smjacob#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
866147883Sscottl#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
867147883Sscottl#define MPT_CONTEXT_TO_CBI(x)	\
868147883Sscottl    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
869157354Smjacob#define MPT_CONTEXT_REQI_MASK	0xFFFF
870157117Smjacob#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
871147883Sscottl
872147883Sscottl/*
873147883Sscottl * Convert a 32bit physical address returned from IOC to an
874147883Sscottl * offset into our reply frame memory or the kvm address needed
875147883Sscottl * to access the data.  The returned address is only the low
876147883Sscottl * 32 bits, so mask our base physical address accordingly.
877147883Sscottl */
878147883Sscottl#define MPT_REPLY_BADDR(x)		\
879147883Sscottl	(x << 1)
880147883Sscottl#define MPT_REPLY_OTOV(m, i) 		\
881147883Sscottl	((void *)(&m->reply[i]))
882147883Sscottl
883147883Sscottl#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
884147883Sscottldo {							\
885158982Smjacob	if (mpt->verbose > MPT_PRT_DEBUG)		\
886147883Sscottl		mpt_dump_reply_frame(mpt, reply_frame);	\
887147883Sscottl} while(0)
888147883Sscottl
889147883Sscottlstatic __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
890147883Sscottlstatic __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
891147883Sscottl
892147883Sscottl/*
893147883Sscottl * Give the reply buffer back to the IOC after we have
894147883Sscottl * finished processing it.
895147883Sscottl */
896147883Sscottlstatic __inline void
897147883Sscottlmpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
898147883Sscottl{
899147883Sscottl     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
900147883Sscottl}
901147883Sscottl
902147883Sscottl/* Get a reply from the IOC */
903147883Sscottlstatic __inline uint32_t
904147883Sscottlmpt_pop_reply_queue(struct mpt_softc *mpt)
905147883Sscottl{
906147883Sscottl     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
907147883Sscottl}
908147883Sscottl
909157354Smjacobvoid
910157354Smjacobmpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
911157354Smjacob
912147883Sscottl/************************** Scatter Gather Managment **************************/
913155521Smjacob/* MPT_RQSL- size of request frame, in bytes */
914155521Smjacob#define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
915155521Smjacob
916155521Smjacob/* MPT_NSGL- how many SG entries can fit in a request frame size */
917155521Smjacob#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
918155521Smjacob
919155521Smjacob/* MPT_NRFM- how many request frames can fit in each request alloc we make */
920155521Smjacob#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
921155521Smjacob
922147883Sscottl/*
923155521Smjacob * MPT_NSGL_FIRST- # of SG elements that can fit after
924155521Smjacob * an I/O request but still within the request frame.
925155521Smjacob * Do this safely based upon SGE_IO_UNION.
926155521Smjacob *
927155521Smjacob * Note that the first element is *within* the SCSI request.
928147883Sscottl */
929155521Smjacob#define	MPT_NSGL_FIRST(mpt)	\
930155521Smjacob    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
931155521Smjacob    sizeof (SGE_IO_UNION))
932147883Sscottl
933147883Sscottl/***************************** IOC Initialization *****************************/
934147883Sscottlint mpt_reset(struct mpt_softc *, int /*reinit*/);
935147883Sscottl
936157117Smjacob/****************************** Debugging ************************************/
937147883Sscottltypedef struct mpt_decode_entry {
938147883Sscottl	char    *name;
939147883Sscottl	u_int	 value;
940147883Sscottl	u_int	 mask;
941147883Sscottl} mpt_decode_entry_t;
942147883Sscottl
943147883Sscottlint mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
944147883Sscottl		     const char *name, u_int value, u_int *cur_column,
945147883Sscottl		     u_int wrap_point);
946147883Sscottl
947160290Smjacobvoid mpt_dump_data(struct mpt_softc *, const char *, void *, int);
948158933Smjacobvoid mpt_dump_request(struct mpt_softc *, request_t *);
949158933Smjacob
950147883Sscottlenum {
951147883Sscottl	MPT_PRT_ALWAYS,
952147883Sscottl	MPT_PRT_FATAL,
953147883Sscottl	MPT_PRT_ERROR,
954147883Sscottl	MPT_PRT_WARN,
955147883Sscottl	MPT_PRT_INFO,
956159049Smjacob	MPT_PRT_NEGOTIATION,
957147883Sscottl	MPT_PRT_DEBUG,
958157117Smjacob	MPT_PRT_DEBUG1,
959157117Smjacob	MPT_PRT_DEBUG2,
960157117Smjacob	MPT_PRT_DEBUG3,
961155521Smjacob	MPT_PRT_TRACE,
962155521Smjacob	MPT_PRT_NONE=100
963147883Sscottl};
964147883Sscottl
965157117Smjacob#if __FreeBSD_version > 500000
966147883Sscottl#define mpt_lprt(mpt, level, ...)		\
967147883Sscottldo {						\
968147883Sscottl	if (level <= (mpt)->verbose)		\
969147883Sscottl		mpt_prt(mpt, __VA_ARGS__);	\
970147883Sscottl} while (0)
971147883Sscottl
972147883Sscottl#define mpt_lprtc(mpt, level, ...)		 \
973147883Sscottldo {						 \
974147883Sscottl	if (level <= (mpt)->debug_level)	 \
975147883Sscottl		mpt_prtc(mpt, __VA_ARGS__);	 \
976147883Sscottl} while (0)
977157117Smjacob#else
978157117Smjacobvoid mpt_lprt(struct mpt_softc *, int, const char *, ...)
979157117Smjacob	__printflike(3, 4);
980157117Smjacobvoid mpt_lprtc(struct mpt_softc *, int, const char *, ...)
981157117Smjacob	__printflike(3, 4);
982157117Smjacob#endif
983157117Smjacobvoid mpt_prt(struct mpt_softc *, const char *, ...)
984157117Smjacob	__printflike(2, 3);
985157117Smjacobvoid mpt_prtc(struct mpt_softc *, const char *, ...)
986157117Smjacob	__printflike(2, 3);
987147883Sscottl
988157117Smjacob/**************************** Target Mode Related ***************************/
989157117Smjacobstatic __inline int mpt_cdblen(uint8_t, int);
990157117Smjacobstatic __inline int
991157117Smjacobmpt_cdblen(uint8_t cdb0, int maxlen)
992157117Smjacob{
993157117Smjacob	int group = cdb0 >> 5;
994157117Smjacob	switch (group) {
995157117Smjacob	case 0:
996157117Smjacob		return (6);
997157117Smjacob	case 1:
998157117Smjacob		return (10);
999157117Smjacob	case 4:
1000157117Smjacob	case 5:
1001157117Smjacob		return (12);
1002157117Smjacob	default:
1003157117Smjacob		return (16);
1004157117Smjacob	}
1005157117Smjacob}
1006157117Smjacob#ifdef	INVARIANTS
1007157117Smjacobstatic __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
1008157117Smjacobstatic __inline request_t *
1009157117Smjacobmpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
1010157117Smjacob{
1011157354Smjacob	uint16_t rtg = (tag >> 18);
1012157117Smjacob	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1013157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1014157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1015157117Smjacob	return (mpt->tgt_cmd_ptrs[rtg]);
1016157117Smjacob}
1017157662Smjacob
1018157662Smjacob
1019157662Smjacobstatic __inline int
1020157662Smjacobmpt_req_on_free_list(struct mpt_softc *, request_t *);
1021157662Smjacobstatic __inline int
1022157662Smjacobmpt_req_on_pending_list(struct mpt_softc *, request_t *);
1023157662Smjacob
1024157662Smjacobstatic __inline void
1025157662Smjacobmpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1026157662Smjacobstatic __inline void
1027157662Smjacobmpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1028157662Smjacob
1029157662Smjacob
1030157662Smjacob/*
1031157662Smjacob * Is request on freelist?
1032157662Smjacob */
1033157662Smjacobstatic __inline int
1034157662Smjacobmpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1035157662Smjacob{
1036157662Smjacob	request_t *lrq;
1037157662Smjacob
1038157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1039157662Smjacob		if (lrq == req) {
1040157662Smjacob			return (1);
1041157662Smjacob		}
1042157662Smjacob	}
1043157662Smjacob	return (0);
1044157662Smjacob}
1045157662Smjacob
1046157662Smjacob/*
1047157662Smjacob * Is request on pending list?
1048157662Smjacob */
1049157662Smjacobstatic __inline int
1050157662Smjacobmpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1051157662Smjacob{
1052157662Smjacob	request_t *lrq;
1053157662Smjacob
1054157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1055157662Smjacob		if (lrq == req) {
1056157662Smjacob			return (1);
1057157662Smjacob		}
1058157662Smjacob	}
1059157662Smjacob	return (0);
1060157662Smjacob}
1061157662Smjacob
1062157662Smjacob/*
1063157662Smjacob * Make sure that req *is* part of one of the special lists
1064157662Smjacob */
1065157662Smjacobstatic __inline void
1066157662Smjacobmpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1067157662Smjacob{
1068157662Smjacob	int i;
1069157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1070157662Smjacob		if (req == mpt->els_cmd_ptrs[i]) {
1071157662Smjacob			return;
1072157662Smjacob		}
1073157662Smjacob	}
1074157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1075157662Smjacob		if (req == mpt->tgt_cmd_ptrs[i]) {
1076157662Smjacob			return;
1077157662Smjacob		}
1078157662Smjacob	}
1079157662Smjacob	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1080157662Smjacob	    s, line, req, req->serno,
1081157662Smjacob	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1082157662Smjacob}
1083157662Smjacob
1084157662Smjacob/*
1085157662Smjacob * Make sure that req is *not* part of one of the special lists.
1086157662Smjacob */
1087157662Smjacobstatic __inline void
1088157662Smjacobmpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1089157662Smjacob{
1090157662Smjacob	int i;
1091157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1092157662Smjacob		KASSERT(req != mpt->els_cmd_ptrs[i],
1093157662Smjacob		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1094157662Smjacob		    s, line, req, req->serno,
1095157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1096157662Smjacob	}
1097157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1098157662Smjacob		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1099157662Smjacob		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1100157662Smjacob		    s, line, req, req->serno,
1101157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1102157662Smjacob	}
1103157662Smjacob}
1104157117Smjacob#endif
1105147883Sscottl
1106157117Smjacobtypedef enum {
1107157117Smjacob	MPT_ABORT_TASK_SET=1234,
1108157117Smjacob	MPT_CLEAR_TASK_SET,
1109157117Smjacob	MPT_TARGET_RESET,
1110157117Smjacob	MPT_CLEAR_ACA,
1111157117Smjacob	MPT_TERMINATE_TASK,
1112157117Smjacob	MPT_NIL_TMT_VALUE=5678
1113157117Smjacob} mpt_task_mgmt_t;
1114157117Smjacob
1115147883Sscottl/**************************** Unclassified Routines ***************************/
1116147883Sscottlvoid		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1117147883Sscottlint		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1118147883Sscottl					 size_t reply_len, void *reply);
1119147883Sscottlint		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1120147883Sscottl			     mpt_req_state_t state, mpt_req_state_t mask,
1121147883Sscottl			     int sleep_ok, int time_ms);
1122147883Sscottlvoid		mpt_enable_ints(struct mpt_softc *mpt);
1123147883Sscottlvoid		mpt_disable_ints(struct mpt_softc *mpt);
1124147883Sscottlint		mpt_attach(struct mpt_softc *mpt);
1125147883Sscottlint		mpt_shutdown(struct mpt_softc *mpt);
1126147883Sscottlint		mpt_detach(struct mpt_softc *mpt);
1127147883Sscottlint		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1128147883Sscottl				       size_t len, void *cmd);
1129147883Sscottlrequest_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1130147883Sscottlvoid		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1131147883Sscottlvoid		mpt_intr(void *arg);
1132147883Sscottlvoid		mpt_check_doorbell(struct mpt_softc *mpt);
1133147883Sscottlvoid		mpt_dump_reply_frame(struct mpt_softc *mpt,
1134147883Sscottl				     MSG_DEFAULT_REPLY *reply_frame);
1135147883Sscottl
1136147883Sscottlvoid		mpt_set_config_regs(struct mpt_softc *);
1137147883Sscottlint		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1138147883Sscottl				  u_int /*Action*/, u_int /*PageVersion*/,
1139147883Sscottl				  u_int /*PageLength*/, u_int /*PageNumber*/,
1140147883Sscottl				  u_int /*PageType*/, uint32_t /*PageAddress*/,
1141147883Sscottl				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1142147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1143147883Sscottlint		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1144147883Sscottl				    int /*PageNumber*/,
1145147883Sscottl				    uint32_t /*PageAddress*/,
1146147883Sscottl				    CONFIG_PAGE_HEADER *,
1147147883Sscottl				    int /*sleep_ok*/, int /*timeout_ms*/);
1148147883Sscottlint		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1149147883Sscottl				  uint32_t /*PageAddress*/,
1150147883Sscottl				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1151147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1152147883Sscottlint		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1153147883Sscottl				   uint32_t /*PageAddress*/,
1154147883Sscottl				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1155147883Sscottl				   int /*sleep_ok*/, int /*timeout_ms*/);
1156147883Sscottlstatic __inline int
1157147883Sscottlmpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1158147883Sscottl		      CONFIG_PAGE_HEADER *hdr, size_t len,
1159147883Sscottl		      int sleep_ok, int timeout_ms)
1160147883Sscottl{
1161147883Sscottl	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1162147883Sscottl				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1163147883Sscottl}
1164147883Sscottl
1165147883Sscottlstatic __inline int
1166147883Sscottlmpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1167147883Sscottl		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1168147883Sscottl		       int timeout_ms)
1169147883Sscottl{
1170147883Sscottl	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1171147883Sscottl				   PageAddress, hdr, len, sleep_ok,
1172147883Sscottl				   timeout_ms));
1173147883Sscottl}
1174147883Sscottl
1175101704Smjacob/* mpt_debug.c functions */
1176101704Smjacobvoid mpt_print_reply(void *vmsg);
1177147883Sscottlvoid mpt_print_db(uint32_t mb);
1178101704Smjacobvoid mpt_print_config_reply(void *vmsg);
1179147883Sscottlchar *mpt_ioc_diag(uint32_t diag);
1180147883Sscottlvoid mpt_req_state(mpt_req_state_t state);
1181101704Smjacobvoid mpt_print_config_request(void *vmsg);
1182101704Smjacobvoid mpt_print_request(void *vmsg);
1183147883Sscottlvoid mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1184155521Smjacobvoid mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1185101704Smjacob#endif /* _MPT_H_ */
1186