mpt.h revision 160391
1101704Smjacob/* $FreeBSD: head/sys/dev/mpt/mpt.h 160391 2006-07-15 22:58:09Z 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 */
320160391Smjacob	uint32_t
321160391Smjacob		is_local : 1,
322160391Smjacob		nxfers	 : 31;
323157354Smjacob	uint32_t tag_id;
324157117Smjacob	enum {
325157117Smjacob		TGT_STATE_NIL,
326157354Smjacob		TGT_STATE_LOADING,
327157117Smjacob		TGT_STATE_LOADED,
328157117Smjacob		TGT_STATE_IN_CAM,
329157117Smjacob                TGT_STATE_SETTING_UP_FOR_DATA,
330157117Smjacob                TGT_STATE_MOVING_DATA,
331157117Smjacob                TGT_STATE_MOVING_DATA_AND_STATUS,
332157117Smjacob                TGT_STATE_SENDING_STATUS
333157117Smjacob	} state;
334157117Smjacob} mpt_tgt_state_t;
335157117Smjacob
336157117Smjacob/*
337157117Smjacob * When we get an incoming command it has its own tag which is called the
338157117Smjacob * IoIndex. This is the value we gave that particular command buffer when
339157117Smjacob * we originally assigned it. It's just a number, really. The FC card uses
340157117Smjacob * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
341157117Smjacob * contains pointers the request_t structures related to that IoIndex.
342157117Smjacob *
343157117Smjacob * What *we* do is construct a tag out of the index for the target command
344157117Smjacob * which owns the incoming ATIO plus a rolling sequence number.
345157117Smjacob */
346157117Smjacob#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
347157354Smjacob ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
348157117Smjacob
349157117Smjacob#ifdef	INVARIANTS
350157117Smjacob#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
351157117Smjacob#else
352157354Smjacob#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
353157117Smjacob#endif
354157117Smjacob
355157117Smjacob#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
356157117Smjacob    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
357157117Smjacob
358157117SmjacobSTAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
359157117Smjacob#define	MPT_MAX_LUNS	256
360157117Smjacobtypedef struct {
361157117Smjacob	struct mpt_hdr_stailq	atios;
362157117Smjacob	struct mpt_hdr_stailq	inots;
363157117Smjacob	int enabled;
364157117Smjacob} tgt_resource_t;
365157662Smjacob#define	MPT_MAX_ELS	64
366157117Smjacob
367147883Sscottl/**************************** Handler Registration ****************************/
368101704Smjacob/*
369147883Sscottl * Global table of registered reply handlers.  The
370147883Sscottl * handler is indicated by byte 3 of the request
371147883Sscottl * index submitted to the IOC.  This allows the
372147883Sscottl * driver core to perform generic processing without
373147883Sscottl * any knowledge of per-personality behavior.
374147883Sscottl *
375147883Sscottl * MPT_NUM_REPLY_HANDLERS must be a power of 2
376147883Sscottl * to allow the easy generation of a mask.
377147883Sscottl *
378147883Sscottl * The handler offsets used by the core are hard coded
379147883Sscottl * allowing faster code generation when assigning a handler
380147883Sscottl * to a request.  All "personalities" must use the
381147883Sscottl * the handler registration mechanism.
382147883Sscottl *
383147883Sscottl * The IOC handlers that are rarely executed are placed
384147883Sscottl * at the tail of the table to make it more likely that
385147883Sscottl * all commonly executed handlers fit in a single cache
386147883Sscottl * line.
387101704Smjacob */
388157117Smjacob#define MPT_NUM_REPLY_HANDLERS		(32)
389147883Sscottl#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
390147883Sscottl#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
391147883Sscottl#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
392147883Sscottltypedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
393157117Smjacob    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
394147883Sscottltypedef union {
395147883Sscottl	mpt_reply_handler_t	*reply_handler;
396147883Sscottl} mpt_handler_t;
397101704Smjacob
398147883Sscottltypedef enum {
399147883Sscottl	MPT_HANDLER_REPLY,
400147883Sscottl	MPT_HANDLER_EVENT,
401147883Sscottl	MPT_HANDLER_RESET,
402147883Sscottl	MPT_HANDLER_SHUTDOWN
403147883Sscottl} mpt_handler_type;
404101704Smjacob
405147883Sscottlstruct mpt_handler_record
406147883Sscottl{
407147883Sscottl	LIST_ENTRY(mpt_handler_record)	links;
408147883Sscottl	mpt_handler_t			handler;
409101704Smjacob};
410101704Smjacob
411147883SscottlLIST_HEAD(mpt_handler_list, mpt_handler_record);
412147883Sscottl
413147883Sscottl/*
414147883Sscottl * The handler_id is currently unused but would contain the
415147883Sscottl * handler ID used in the MsgContext field to allow direction
416147883Sscottl * of replies to the handler.  Registrations that don't require
417147883Sscottl * a handler id can pass in NULL for the handler_id.
418147883Sscottl *
419147883Sscottl * Deregistrations for handlers without a handler id should
420147883Sscottl * pass in MPT_HANDLER_ID_NONE.
421147883Sscottl */
422147883Sscottl#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
423147883Sscottlint mpt_register_handler(struct mpt_softc *, mpt_handler_type,
424147883Sscottl			 mpt_handler_t, uint32_t *);
425147883Sscottlint mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
426147883Sscottl			   mpt_handler_t, uint32_t);
427147883Sscottl
428147883Sscottl/******************* Per-Controller Instance Data Structures ******************/
429147883SscottlTAILQ_HEAD(req_queue, req_entry);
430147883Sscottl
431147883Sscottl/* Structure for saving proper values for modifyable PCI config registers */
432147883Sscottlstruct mpt_pci_cfg {
433147883Sscottl	uint16_t Command;
434147883Sscottl	uint16_t LatencyTimer_LineSize;
435147883Sscottl	uint32_t IO_BAR;
436147883Sscottl	uint32_t Mem0_BAR[2];
437147883Sscottl	uint32_t Mem1_BAR[2];
438147883Sscottl	uint32_t ROM_BAR;
439147883Sscottl	uint8_t  IntLine;
440147883Sscottl	uint32_t PMCSR;
441101704Smjacob};
442101704Smjacob
443147883Sscottltypedef enum {
444147883Sscottl	MPT_RVF_NONE		= 0x0,
445147883Sscottl	MPT_RVF_ACTIVE		= 0x1,
446147883Sscottl	MPT_RVF_ANNOUNCED	= 0x2,
447147883Sscottl	MPT_RVF_UP2DATE		= 0x4,
448147883Sscottl	MPT_RVF_REFERENCED	= 0x8,
449147883Sscottl	MPT_RVF_WCE_CHANGED	= 0x10
450147883Sscottl} mpt_raid_volume_flags;
451101704Smjacob
452147883Sscottlstruct mpt_raid_volume {
453147883Sscottl	CONFIG_PAGE_RAID_VOL_0	       *config_page;
454147883Sscottl	MPI_RAID_VOL_INDICATOR		sync_progress;
455147883Sscottl	mpt_raid_volume_flags		flags;
456158933Smjacob	u_int				quiesced_disks;
457101704Smjacob};
458101704Smjacob
459147883Sscottltypedef enum {
460147883Sscottl	MPT_RDF_NONE		= 0x00,
461147883Sscottl	MPT_RDF_ACTIVE		= 0x01,
462147883Sscottl	MPT_RDF_ANNOUNCED	= 0x02,
463147883Sscottl	MPT_RDF_UP2DATE		= 0x04,
464147883Sscottl	MPT_RDF_REFERENCED	= 0x08,
465147883Sscottl	MPT_RDF_QUIESCING	= 0x10,
466147883Sscottl	MPT_RDF_QUIESCED	= 0x20
467147883Sscottl} mpt_raid_disk_flags;
468147883Sscottl
469147883Sscottlstruct mpt_raid_disk {
470147883Sscottl	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
471147883Sscottl	struct mpt_raid_volume	       *volume;
472147883Sscottl	u_int				member_number;
473147883Sscottl	u_int				pass_thru_active;
474147883Sscottl	mpt_raid_disk_flags		flags;
475101704Smjacob};
476101704Smjacob
477147883Sscottlstruct mpt_evtf_record {
478147883Sscottl	MSG_EVENT_NOTIFY_REPLY		reply;
479147883Sscottl	uint32_t			context;
480147883Sscottl	LIST_ENTRY(mpt_evtf_record)	links;
481147883Sscottl};
482101704Smjacob
483147883SscottlLIST_HEAD(mpt_evtf_list, mpt_evtf_record);
484101704Smjacob
485147883Sscottlstruct mpt_softc {
486147883Sscottl	device_t		dev;
487147883Sscottl#if __FreeBSD_version < 500000
488157662Smjacob	uint32_t		mpt_islocked;
489147883Sscottl	int			mpt_splsaved;
490147883Sscottl#else
491147883Sscottl	struct mtx		mpt_lock;
492157662Smjacob	int			mpt_locksetup;
493147883Sscottl#endif
494147883Sscottl	uint32_t		mpt_pers_mask;
495160290Smjacob	uint32_t
496157117Smjacob		unit		: 8,
497160290Smjacob				: 3,
498157117Smjacob		twildcard	: 1,
499157117Smjacob		tenabled	: 1,
500160290Smjacob		do_cfg_role	: 1,
501158982Smjacob		raid_enabled	: 1,
502147883Sscottl		raid_mwce_set	: 1,
503147883Sscottl		getreqwaiter	: 1,
504147883Sscottl		shutdwn_raid    : 1,
505147883Sscottl		shutdwn_recovery: 1,
506147883Sscottl		outofbeer	: 1,
507147883Sscottl		disabled	: 1,
508159178Smjacob		is_spi		: 1,
509157117Smjacob		is_sas		: 1,
510157117Smjacob		is_fc		: 1;
511101704Smjacob
512160290Smjacob	u_int			cfg_role;
513160290Smjacob	u_int			role;	/* role: none, ini, target, both */
514160290Smjacob
515147883Sscottl	u_int			verbose;
516101704Smjacob
517147883Sscottl	/*
518147883Sscottl	 * IOC Facts
519147883Sscottl	 */
520147883Sscottl	uint16_t	mpt_global_credits;
521147883Sscottl	uint16_t	request_frame_size;
522147883Sscottl	uint8_t		mpt_max_devices;
523147883Sscottl	uint8_t		mpt_max_buses;
524155521Smjacob	uint8_t		ioc_facts_flags;
525157354Smjacob	uint8_t		padding0;
526101704Smjacob
527147883Sscottl	/*
528147883Sscottl	 * Port Facts
529147883Sscottl	 * XXX - Add multi-port support!.
530147883Sscottl	 */
531147883Sscottl	uint16_t	mpt_ini_id;
532147883Sscottl	uint16_t	mpt_port_type;
533147883Sscottl	uint16_t	mpt_proto_flags;
534157117Smjacob	uint16_t	mpt_max_tgtcmds;
535102822Smjacob
536147883Sscottl	/*
537147883Sscottl	 * Device Configuration Information
538147883Sscottl	 */
539147883Sscottl	union {
540147883Sscottl		struct mpt_spi_cfg {
541147883Sscottl			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
542147883Sscottl			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
543147883Sscottl			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
544147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
545147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
546147883Sscottl			uint16_t			_tag_enable;
547147883Sscottl			uint16_t			_disc_enable;
548147883Sscottl		} spi;
549147883Sscottl#define	mpt_port_page0		cfg.spi._port_page0
550147883Sscottl#define	mpt_port_page1		cfg.spi._port_page1
551147883Sscottl#define	mpt_port_page2		cfg.spi._port_page2
552147883Sscottl#define	mpt_dev_page0		cfg.spi._dev_page0
553147883Sscottl#define	mpt_dev_page1		cfg.spi._dev_page1
554147883Sscottl#define	mpt_tag_enable		cfg.spi._tag_enable
555147883Sscottl#define	mpt_disc_enable		cfg.spi._disc_enable
556147883Sscottl		struct mpi_fc_cfg {
557157117Smjacob			CONFIG_PAGE_FC_PORT_0 _port_page0;
558159919Smjacob			uint32_t _port_speed;
559157117Smjacob#define	mpt_fcport_page0	cfg.fc._port_page0
560159919Smjacob#define	mpt_fcport_speed	cfg.fc._port_speed
561147883Sscottl		} fc;
562147883Sscottl	} cfg;
563147883Sscottl
564158982Smjacob	/* Controller Info for RAID information */
565147883Sscottl	CONFIG_PAGE_IOC_2 *	ioc_page2;
566147883Sscottl	CONFIG_PAGE_IOC_3 *	ioc_page3;
567147883Sscottl
568147883Sscottl	/* Raid Data */
569147883Sscottl	struct mpt_raid_volume* raid_volumes;
570147883Sscottl	struct mpt_raid_disk*	raid_disks;
571147883Sscottl	u_int			raid_max_volumes;
572147883Sscottl	u_int			raid_max_disks;
573147883Sscottl	u_int			raid_page0_len;
574147883Sscottl	u_int			raid_wakeup;
575147883Sscottl	u_int			raid_rescan;
576147883Sscottl	u_int			raid_resync_rate;
577147883Sscottl	u_int			raid_mwce_setting;
578147883Sscottl	u_int			raid_queue_depth;
579152444Skan	u_int			raid_nonopt_volumes;
580147883Sscottl	struct proc	       *raid_thread;
581147883Sscottl	struct callout		raid_timer;
582147883Sscottl
583147883Sscottl	/*
584147883Sscottl	 * PCI Hardware info
585147883Sscottl	 */
586147883Sscottl	struct resource *	pci_irq;	/* Interrupt map for chip */
587147883Sscottl	void *			ih;		/* Interupt handle */
588147883Sscottl	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
589147883Sscottl
590147883Sscottl	/*
591147883Sscottl	 * DMA Mapping Stuff
592147883Sscottl	 */
593147883Sscottl	struct resource *	pci_reg;	/* Register map for chip */
594147883Sscottl	int			pci_mem_rid;	/* Resource ID */
595147883Sscottl	bus_space_tag_t		pci_st;		/* Bus tag for registers */
596147883Sscottl	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
597147883Sscottl	/* PIO versions of above. */
598147883Sscottl	int			pci_pio_rid;
599147883Sscottl	struct resource *	pci_pio_reg;
600147883Sscottl	bus_space_tag_t		pci_pio_st;
601147883Sscottl	bus_space_handle_t	pci_pio_sh;
602147883Sscottl
603147883Sscottl	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
604147883Sscottl	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
605147883Sscottl	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
606147883Sscottl	uint8_t		       *reply;		/* KVA of reply memory */
607147883Sscottl	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
608147883Sscottl
609147883Sscottl	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
610147883Sscottl	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
611147883Sscottl	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
612147883Sscottl	uint8_t		       *request;	/* KVA of Request memory */
613157117Smjacob	bus_addr_t		request_phys;	/* BusAddr of request memory */
614147883Sscottl
615155521Smjacob	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
616155521Smjacob
617155521Smjacob	/*
618155521Smjacob	 * Hardware management
619155521Smjacob	 */
620147883Sscottl	u_int			reset_cnt;
621147883Sscottl
622147883Sscottl	/*
623147883Sscottl	 * CAM && Software Management
624147883Sscottl	 */
625147883Sscottl	request_t	       *request_pool;
626147883Sscottl	struct req_queue	request_free_list;
627147883Sscottl	struct req_queue	request_pending_list;
628147883Sscottl	struct req_queue	request_timeout_list;
629147883Sscottl
630147883Sscottl
631147883Sscottl	struct cam_sim	       *sim;
632147883Sscottl	struct cam_path	       *path;
633147883Sscottl
634147883Sscottl	struct cam_sim	       *phydisk_sim;
635147883Sscottl	struct cam_path	       *phydisk_path;
636147883Sscottl
637147883Sscottl	struct proc	       *recovery_thread;
638147883Sscottl	request_t	       *tmf_req;
639147883Sscottl
640157117Smjacob	/*
641157354Smjacob	 * Deferred frame acks due to resource shortage.
642157354Smjacob	 */
643157354Smjacob	struct mpt_evtf_list	ack_frames;
644159312Smjacob
645157354Smjacob	/*
646157117Smjacob	 * Target Mode Support
647157117Smjacob	 */
648157117Smjacob	uint32_t		scsi_tgt_handler_id;
649157117Smjacob	request_t **		tgt_cmd_ptrs;
650157662Smjacob	request_t **		els_cmd_ptrs;	/* FC only */
651147883Sscottl
652157117Smjacob	/*
653157117Smjacob	 * *snork*- this is chosen to be here *just in case* somebody
654157117Smjacob	 * forgets to point to it exactly and we index off of trt with
655157117Smjacob	 * CAM_LUN_WILDCARD.
656157117Smjacob	 */
657160391Smjacob	tgt_resource_t		trt_wildcard;		/* wildcard luns */
658157117Smjacob	tgt_resource_t		trt[MPT_MAX_LUNS];
659157117Smjacob	uint16_t		tgt_cmds_allocated;
660157662Smjacob	uint16_t		els_cmds_allocated;	/* FC only */
661157117Smjacob
662157117Smjacob	uint16_t		timeouts;	/* timeout count */
663157117Smjacob	uint16_t		success;	/* successes afer timeout */
664157662Smjacob	uint16_t		sequence;	/* Sequence Number */
665157662Smjacob	uint16_t		pad3;
666157117Smjacob
667157117Smjacob
668159312Smjacob	/* Paired port in some dual adapters configurations */
669147883Sscottl	struct mpt_softc *	mpt2;
670147883Sscottl
671147883Sscottl	/* FW Image management */
672147883Sscottl	uint32_t		fw_image_size;
673147883Sscottl	uint8_t		       *fw_image;
674147883Sscottl	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
675147883Sscottl	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
676159178Smjacob	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
677147883Sscottl
678147883Sscottl	/* Shutdown Event Handler. */
679147883Sscottl	eventhandler_tag         eh;
680147883Sscottl
681147883Sscottl	TAILQ_ENTRY(mpt_softc)	links;
682147883Sscottl};
683147883Sscottl
684157662Smjacobstatic __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
685157662Smjacob
686157662Smjacobstatic __inline void
687157662Smjacobmpt_assign_serno(struct mpt_softc *mpt, request_t *req)
688157662Smjacob{
689157662Smjacob	if ((req->serno = mpt->sequence++) == 0) {
690157662Smjacob		req->serno = mpt->sequence++;
691157662Smjacob	}
692157662Smjacob}
693157662Smjacob
694157117Smjacob/***************************** Locking Primitives *****************************/
695147883Sscottl#if __FreeBSD_version < 500000
696147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM
697147883Sscottl#define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
698147883Sscottl#define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
699157662Smjacob#define	MPT_OWNED(mpt)		mpt->mpt_islocked
700147883Sscottl#define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
701147883Sscottl#define	CAMLOCK_2_MPTLOCK	MPT_LOCK
702147883Sscottl#define	MPT_LOCK_SETUP(mpt)
703147883Sscottl#define	MPT_LOCK_DESTROY(mpt)
704147883Sscottl
705147883Sscottlstatic __inline void mpt_lockspl(struct mpt_softc *mpt);
706147883Sscottlstatic __inline void mpt_unlockspl(struct mpt_softc *mpt);
707147883Sscottl
708147883Sscottlstatic __inline void
709147883Sscottlmpt_lockspl(struct mpt_softc *mpt)
710147883Sscottl{
711147883Sscottl       int s;
712147883Sscottl
713147883Sscottl       s = splcam();
714147883Sscottl       if (mpt->mpt_islocked++ == 0) {
715147883Sscottl               mpt->mpt_splsaved = s;
716147883Sscottl       } else {
717147883Sscottl               splx(s);
718147883Sscottl	       panic("Recursed lock with mask: 0x%x\n", s);
719147883Sscottl       }
720147883Sscottl}
721147883Sscottl
722147883Sscottlstatic __inline void
723147883Sscottlmpt_unlockspl(struct mpt_softc *mpt)
724147883Sscottl{
725147883Sscottl       if (mpt->mpt_islocked) {
726147883Sscottl               if (--mpt->mpt_islocked == 0) {
727147883Sscottl                       splx(mpt->mpt_splsaved);
728147883Sscottl               }
729147883Sscottl       } else
730147883Sscottl	       panic("Negative lock count\n");
731147883Sscottl}
732147883Sscottl
733147883Sscottlstatic __inline int
734147883Sscottlmpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
735147883Sscottl	   const char *wmesg, int timo)
736147883Sscottl{
737147883Sscottl	int saved_cnt;
738147883Sscottl	int saved_spl;
739147883Sscottl	int error;
740147883Sscottl
741147883Sscottl	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
742147883Sscottl	saved_cnt = mpt->mpt_islocked;
743147883Sscottl	saved_spl = mpt->mpt_splsaved;
744147883Sscottl	mpt->mpt_islocked = 0;
745147883Sscottl	error = tsleep(ident, priority, wmesg, timo);
746157117Smjacob	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
747147883Sscottl	mpt->mpt_islocked = saved_cnt;
748147883Sscottl	mpt->mpt_splsaved = saved_spl;
749147883Sscottl	return (error);
750147883Sscottl}
751147883Sscottl
752147883Sscottl#else
753153072Sru#ifdef	LOCKING_WORKED_AS_IT_SHOULD
754147883Sscottl#error "Shouldn't Be Here!"
755147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
756147883Sscottl#define	MPT_LOCK_SETUP(mpt)						\
757147883Sscottl		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
758147883Sscottl		mpt->mpt_locksetup = 1
759147883Sscottl#define	MPT_LOCK_DESTROY(mpt)						\
760147883Sscottl	if (mpt->mpt_locksetup) {					\
761147883Sscottl		mtx_destroy(&mpt->mpt_lock);				\
762147883Sscottl		mpt->mpt_locksetup = 0;					\
763147883Sscottl	}
764147883Sscottl
765147883Sscottl#define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
766147883Sscottl#define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
767157662Smjacob#define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
768147883Sscottl#define	MPTLOCK_2_CAMLOCK(mpt)	\
769147883Sscottl	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
770147883Sscottl#define	CAMLOCK_2_MPTLOCK(mpt)	\
771147883Sscottl	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
772147883Sscottl#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
773147883Sscottl	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
774157662Smjacob
775147883Sscottl#else
776157662Smjacob
777147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
778147883Sscottl#define	MPT_LOCK_SETUP(mpt)	do { } while (0)
779147883Sscottl#define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
780157662Smjacob#if	0
781157662Smjacob#define	MPT_LOCK(mpt)		\
782157662Smjacob	device_printf(mpt->dev, "LOCK %s:%d\n", __FILE__, __LINE__); 	\
783157662Smjacob	KASSERT(mpt->mpt_locksetup == 0,				\
784157662Smjacob	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
785157662Smjacob	mpt->mpt_locksetup = 1
786157662Smjacob#define	MPT_UNLOCK(mpt)		\
787157662Smjacob	device_printf(mpt->dev, "UNLK %s:%d\n", __FILE__, __LINE__); 	\
788157662Smjacob	KASSERT(mpt->mpt_locksetup == 1,				\
789157662Smjacob	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
790157662Smjacob	mpt->mpt_locksetup = 0
791157662Smjacob#else
792157662Smjacob#define	MPT_LOCK(mpt)							\
793157662Smjacob	KASSERT(mpt->mpt_locksetup == 0,				\
794157662Smjacob	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
795157662Smjacob	mpt->mpt_locksetup = 1
796157662Smjacob#define	MPT_UNLOCK(mpt)							\
797157662Smjacob	KASSERT(mpt->mpt_locksetup == 1,				\
798157662Smjacob	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
799157662Smjacob	mpt->mpt_locksetup = 0
800147883Sscottl#endif
801157662Smjacob#define	MPT_OWNED(mpt)		mpt->mpt_locksetup
802157662Smjacob#define	MPTLOCK_2_CAMLOCK(mpt)	MPT_UNLOCK(mpt)
803157662Smjacob#define	CAMLOCK_2_MPTLOCK(mpt)	MPT_LOCK(mpt)
804157662Smjacob
805157662Smjacobstatic __inline int
806157662Smjacobmpt_sleep(struct mpt_softc *, void *, int, const char *, int);
807157662Smjacob
808157662Smjacobstatic __inline int
809157662Smjacobmpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
810157662Smjacob{
811157662Smjacob	int r;
812157662Smjacob	MPT_UNLOCK(mpt);
813157662Smjacob	r = tsleep(i, p, w, t);
814157662Smjacob	MPT_LOCK(mpt);
815157662Smjacob	return (r);
816157662Smjacob}
817147883Sscottl#endif
818157662Smjacob#endif
819147883Sscottl
820147883Sscottl/******************************* Register Access ******************************/
821147883Sscottlstatic __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
822147883Sscottlstatic __inline uint32_t mpt_read(struct mpt_softc *, int);
823147883Sscottlstatic __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
824147883Sscottlstatic __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
825147883Sscottl
826147883Sscottlstatic __inline void
827147883Sscottlmpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
828147883Sscottl{
829147883Sscottl	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
830147883Sscottl}
831147883Sscottl
832147883Sscottlstatic __inline uint32_t
833147883Sscottlmpt_read(struct mpt_softc *mpt, int offset)
834147883Sscottl{
835147883Sscottl	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
836147883Sscottl}
837147883Sscottl
838147883Sscottl/*
839147883Sscottl * Some operations (e.g. diagnostic register writes while the ARM proccessor
840147883Sscottl * is disabled), must be performed using "PCI pio" operations.  On non-PCI
841147883Sscottl * busses, these operations likely map to normal register accesses.
842147883Sscottl */
843147883Sscottlstatic __inline void
844147883Sscottlmpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
845147883Sscottl{
846147883Sscottl	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
847147883Sscottl}
848147883Sscottl
849147883Sscottlstatic __inline uint32_t
850147883Sscottlmpt_pio_read(struct mpt_softc *mpt, int offset)
851147883Sscottl{
852147883Sscottl	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
853147883Sscottl}
854147883Sscottl/*********************** Reply Frame/Request Management ***********************/
855147883Sscottl/* Max MPT Reply we are willing to accept (must be power of 2) */
856155521Smjacob#define MPT_REPLY_SIZE   	256
857147883Sscottl
858157117Smjacob/*
859157117Smjacob * Must be less than 16384 in order for target mode to work
860157117Smjacob */
861155521Smjacob#define MPT_MAX_REQUESTS(mpt)	512
862155521Smjacob#define MPT_REQUEST_AREA	512
863155521Smjacob#define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
864147883Sscottl#define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
865147883Sscottl
866157354Smjacob#define MPT_CONTEXT_CB_SHIFT	(16)
867157117Smjacob#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
868147883Sscottl#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
869147883Sscottl#define MPT_CONTEXT_TO_CBI(x)	\
870147883Sscottl    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
871157354Smjacob#define MPT_CONTEXT_REQI_MASK	0xFFFF
872157117Smjacob#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
873147883Sscottl
874147883Sscottl/*
875147883Sscottl * Convert a 32bit physical address returned from IOC to an
876147883Sscottl * offset into our reply frame memory or the kvm address needed
877147883Sscottl * to access the data.  The returned address is only the low
878147883Sscottl * 32 bits, so mask our base physical address accordingly.
879147883Sscottl */
880147883Sscottl#define MPT_REPLY_BADDR(x)		\
881147883Sscottl	(x << 1)
882147883Sscottl#define MPT_REPLY_OTOV(m, i) 		\
883147883Sscottl	((void *)(&m->reply[i]))
884147883Sscottl
885147883Sscottl#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
886147883Sscottldo {							\
887158982Smjacob	if (mpt->verbose > MPT_PRT_DEBUG)		\
888147883Sscottl		mpt_dump_reply_frame(mpt, reply_frame);	\
889147883Sscottl} while(0)
890147883Sscottl
891147883Sscottlstatic __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
892147883Sscottlstatic __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
893147883Sscottl
894147883Sscottl/*
895147883Sscottl * Give the reply buffer back to the IOC after we have
896147883Sscottl * finished processing it.
897147883Sscottl */
898147883Sscottlstatic __inline void
899147883Sscottlmpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
900147883Sscottl{
901147883Sscottl     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
902147883Sscottl}
903147883Sscottl
904147883Sscottl/* Get a reply from the IOC */
905147883Sscottlstatic __inline uint32_t
906147883Sscottlmpt_pop_reply_queue(struct mpt_softc *mpt)
907147883Sscottl{
908147883Sscottl     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
909147883Sscottl}
910147883Sscottl
911157354Smjacobvoid
912157354Smjacobmpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
913157354Smjacob
914147883Sscottl/************************** Scatter Gather Managment **************************/
915155521Smjacob/* MPT_RQSL- size of request frame, in bytes */
916155521Smjacob#define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
917155521Smjacob
918155521Smjacob/* MPT_NSGL- how many SG entries can fit in a request frame size */
919155521Smjacob#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
920155521Smjacob
921155521Smjacob/* MPT_NRFM- how many request frames can fit in each request alloc we make */
922155521Smjacob#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
923155521Smjacob
924147883Sscottl/*
925155521Smjacob * MPT_NSGL_FIRST- # of SG elements that can fit after
926155521Smjacob * an I/O request but still within the request frame.
927155521Smjacob * Do this safely based upon SGE_IO_UNION.
928155521Smjacob *
929155521Smjacob * Note that the first element is *within* the SCSI request.
930147883Sscottl */
931155521Smjacob#define	MPT_NSGL_FIRST(mpt)	\
932155521Smjacob    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
933155521Smjacob    sizeof (SGE_IO_UNION))
934147883Sscottl
935147883Sscottl/***************************** IOC Initialization *****************************/
936147883Sscottlint mpt_reset(struct mpt_softc *, int /*reinit*/);
937147883Sscottl
938157117Smjacob/****************************** Debugging ************************************/
939147883Sscottltypedef struct mpt_decode_entry {
940147883Sscottl	char    *name;
941147883Sscottl	u_int	 value;
942147883Sscottl	u_int	 mask;
943147883Sscottl} mpt_decode_entry_t;
944147883Sscottl
945147883Sscottlint mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
946147883Sscottl		     const char *name, u_int value, u_int *cur_column,
947147883Sscottl		     u_int wrap_point);
948147883Sscottl
949160290Smjacobvoid mpt_dump_data(struct mpt_softc *, const char *, void *, int);
950158933Smjacobvoid mpt_dump_request(struct mpt_softc *, request_t *);
951158933Smjacob
952147883Sscottlenum {
953147883Sscottl	MPT_PRT_ALWAYS,
954147883Sscottl	MPT_PRT_FATAL,
955147883Sscottl	MPT_PRT_ERROR,
956147883Sscottl	MPT_PRT_WARN,
957147883Sscottl	MPT_PRT_INFO,
958159049Smjacob	MPT_PRT_NEGOTIATION,
959147883Sscottl	MPT_PRT_DEBUG,
960157117Smjacob	MPT_PRT_DEBUG1,
961157117Smjacob	MPT_PRT_DEBUG2,
962157117Smjacob	MPT_PRT_DEBUG3,
963155521Smjacob	MPT_PRT_TRACE,
964155521Smjacob	MPT_PRT_NONE=100
965147883Sscottl};
966147883Sscottl
967157117Smjacob#if __FreeBSD_version > 500000
968147883Sscottl#define mpt_lprt(mpt, level, ...)		\
969147883Sscottldo {						\
970147883Sscottl	if (level <= (mpt)->verbose)		\
971147883Sscottl		mpt_prt(mpt, __VA_ARGS__);	\
972147883Sscottl} while (0)
973147883Sscottl
974147883Sscottl#define mpt_lprtc(mpt, level, ...)		 \
975147883Sscottldo {						 \
976147883Sscottl	if (level <= (mpt)->debug_level)	 \
977147883Sscottl		mpt_prtc(mpt, __VA_ARGS__);	 \
978147883Sscottl} while (0)
979157117Smjacob#else
980157117Smjacobvoid mpt_lprt(struct mpt_softc *, int, const char *, ...)
981157117Smjacob	__printflike(3, 4);
982157117Smjacobvoid mpt_lprtc(struct mpt_softc *, int, const char *, ...)
983157117Smjacob	__printflike(3, 4);
984157117Smjacob#endif
985157117Smjacobvoid mpt_prt(struct mpt_softc *, const char *, ...)
986157117Smjacob	__printflike(2, 3);
987157117Smjacobvoid mpt_prtc(struct mpt_softc *, const char *, ...)
988157117Smjacob	__printflike(2, 3);
989147883Sscottl
990157117Smjacob/**************************** Target Mode Related ***************************/
991157117Smjacobstatic __inline int mpt_cdblen(uint8_t, int);
992157117Smjacobstatic __inline int
993157117Smjacobmpt_cdblen(uint8_t cdb0, int maxlen)
994157117Smjacob{
995157117Smjacob	int group = cdb0 >> 5;
996157117Smjacob	switch (group) {
997157117Smjacob	case 0:
998157117Smjacob		return (6);
999157117Smjacob	case 1:
1000157117Smjacob		return (10);
1001157117Smjacob	case 4:
1002157117Smjacob	case 5:
1003157117Smjacob		return (12);
1004157117Smjacob	default:
1005157117Smjacob		return (16);
1006157117Smjacob	}
1007157117Smjacob}
1008157117Smjacob#ifdef	INVARIANTS
1009157117Smjacobstatic __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
1010157117Smjacobstatic __inline request_t *
1011157117Smjacobmpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
1012157117Smjacob{
1013157354Smjacob	uint16_t rtg = (tag >> 18);
1014157117Smjacob	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1015157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1016157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1017157117Smjacob	return (mpt->tgt_cmd_ptrs[rtg]);
1018157117Smjacob}
1019157662Smjacob
1020157662Smjacob
1021157662Smjacobstatic __inline int
1022157662Smjacobmpt_req_on_free_list(struct mpt_softc *, request_t *);
1023157662Smjacobstatic __inline int
1024157662Smjacobmpt_req_on_pending_list(struct mpt_softc *, request_t *);
1025157662Smjacob
1026157662Smjacobstatic __inline void
1027157662Smjacobmpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1028157662Smjacobstatic __inline void
1029157662Smjacobmpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1030157662Smjacob
1031157662Smjacob
1032157662Smjacob/*
1033157662Smjacob * Is request on freelist?
1034157662Smjacob */
1035157662Smjacobstatic __inline int
1036157662Smjacobmpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1037157662Smjacob{
1038157662Smjacob	request_t *lrq;
1039157662Smjacob
1040157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1041157662Smjacob		if (lrq == req) {
1042157662Smjacob			return (1);
1043157662Smjacob		}
1044157662Smjacob	}
1045157662Smjacob	return (0);
1046157662Smjacob}
1047157662Smjacob
1048157662Smjacob/*
1049157662Smjacob * Is request on pending list?
1050157662Smjacob */
1051157662Smjacobstatic __inline int
1052157662Smjacobmpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1053157662Smjacob{
1054157662Smjacob	request_t *lrq;
1055157662Smjacob
1056157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1057157662Smjacob		if (lrq == req) {
1058157662Smjacob			return (1);
1059157662Smjacob		}
1060157662Smjacob	}
1061157662Smjacob	return (0);
1062157662Smjacob}
1063157662Smjacob
1064157662Smjacob/*
1065157662Smjacob * Make sure that req *is* part of one of the special lists
1066157662Smjacob */
1067157662Smjacobstatic __inline void
1068157662Smjacobmpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1069157662Smjacob{
1070157662Smjacob	int i;
1071157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1072157662Smjacob		if (req == mpt->els_cmd_ptrs[i]) {
1073157662Smjacob			return;
1074157662Smjacob		}
1075157662Smjacob	}
1076157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1077157662Smjacob		if (req == mpt->tgt_cmd_ptrs[i]) {
1078157662Smjacob			return;
1079157662Smjacob		}
1080157662Smjacob	}
1081157662Smjacob	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1082157662Smjacob	    s, line, req, req->serno,
1083157662Smjacob	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1084157662Smjacob}
1085157662Smjacob
1086157662Smjacob/*
1087157662Smjacob * Make sure that req is *not* part of one of the special lists.
1088157662Smjacob */
1089157662Smjacobstatic __inline void
1090157662Smjacobmpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1091157662Smjacob{
1092157662Smjacob	int i;
1093157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1094157662Smjacob		KASSERT(req != mpt->els_cmd_ptrs[i],
1095157662Smjacob		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1096157662Smjacob		    s, line, req, req->serno,
1097157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1098157662Smjacob	}
1099157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1100157662Smjacob		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1101157662Smjacob		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1102157662Smjacob		    s, line, req, req->serno,
1103157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1104157662Smjacob	}
1105157662Smjacob}
1106157117Smjacob#endif
1107147883Sscottl
1108160391Smjacob/*
1109160391Smjacob * Task Management Types, purely for internal consumption
1110160391Smjacob */
1111157117Smjacobtypedef enum {
1112157117Smjacob	MPT_ABORT_TASK_SET=1234,
1113157117Smjacob	MPT_CLEAR_TASK_SET,
1114157117Smjacob	MPT_TARGET_RESET,
1115157117Smjacob	MPT_CLEAR_ACA,
1116157117Smjacob	MPT_TERMINATE_TASK,
1117157117Smjacob	MPT_NIL_TMT_VALUE=5678
1118157117Smjacob} mpt_task_mgmt_t;
1119157117Smjacob
1120147883Sscottl/**************************** Unclassified Routines ***************************/
1121147883Sscottlvoid		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1122147883Sscottlint		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1123147883Sscottl					 size_t reply_len, void *reply);
1124147883Sscottlint		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1125147883Sscottl			     mpt_req_state_t state, mpt_req_state_t mask,
1126147883Sscottl			     int sleep_ok, int time_ms);
1127147883Sscottlvoid		mpt_enable_ints(struct mpt_softc *mpt);
1128147883Sscottlvoid		mpt_disable_ints(struct mpt_softc *mpt);
1129147883Sscottlint		mpt_attach(struct mpt_softc *mpt);
1130147883Sscottlint		mpt_shutdown(struct mpt_softc *mpt);
1131147883Sscottlint		mpt_detach(struct mpt_softc *mpt);
1132147883Sscottlint		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1133147883Sscottl				       size_t len, void *cmd);
1134147883Sscottlrequest_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1135147883Sscottlvoid		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1136147883Sscottlvoid		mpt_intr(void *arg);
1137147883Sscottlvoid		mpt_check_doorbell(struct mpt_softc *mpt);
1138147883Sscottlvoid		mpt_dump_reply_frame(struct mpt_softc *mpt,
1139147883Sscottl				     MSG_DEFAULT_REPLY *reply_frame);
1140147883Sscottl
1141147883Sscottlvoid		mpt_set_config_regs(struct mpt_softc *);
1142147883Sscottlint		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1143147883Sscottl				  u_int /*Action*/, u_int /*PageVersion*/,
1144147883Sscottl				  u_int /*PageLength*/, u_int /*PageNumber*/,
1145147883Sscottl				  u_int /*PageType*/, uint32_t /*PageAddress*/,
1146147883Sscottl				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1147147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1148147883Sscottlint		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1149147883Sscottl				    int /*PageNumber*/,
1150147883Sscottl				    uint32_t /*PageAddress*/,
1151147883Sscottl				    CONFIG_PAGE_HEADER *,
1152147883Sscottl				    int /*sleep_ok*/, int /*timeout_ms*/);
1153147883Sscottlint		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1154147883Sscottl				  uint32_t /*PageAddress*/,
1155147883Sscottl				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1156147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1157147883Sscottlint		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1158147883Sscottl				   uint32_t /*PageAddress*/,
1159147883Sscottl				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1160147883Sscottl				   int /*sleep_ok*/, int /*timeout_ms*/);
1161147883Sscottlstatic __inline int
1162147883Sscottlmpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1163147883Sscottl		      CONFIG_PAGE_HEADER *hdr, size_t len,
1164147883Sscottl		      int sleep_ok, int timeout_ms)
1165147883Sscottl{
1166147883Sscottl	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1167147883Sscottl				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1168147883Sscottl}
1169147883Sscottl
1170147883Sscottlstatic __inline int
1171147883Sscottlmpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1172147883Sscottl		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1173147883Sscottl		       int timeout_ms)
1174147883Sscottl{
1175147883Sscottl	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1176147883Sscottl				   PageAddress, hdr, len, sleep_ok,
1177147883Sscottl				   timeout_ms));
1178147883Sscottl}
1179147883Sscottl
1180101704Smjacob/* mpt_debug.c functions */
1181101704Smjacobvoid mpt_print_reply(void *vmsg);
1182147883Sscottlvoid mpt_print_db(uint32_t mb);
1183101704Smjacobvoid mpt_print_config_reply(void *vmsg);
1184147883Sscottlchar *mpt_ioc_diag(uint32_t diag);
1185147883Sscottlvoid mpt_req_state(mpt_req_state_t state);
1186101704Smjacobvoid mpt_print_config_request(void *vmsg);
1187101704Smjacobvoid mpt_print_request(void *vmsg);
1188147883Sscottlvoid mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1189155521Smjacobvoid mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1190101704Smjacob#endif /* _MPT_H_ */
1191