mpt.h revision 159049
1101704Smjacob/* $FreeBSD: head/sys/dev/mpt/mpt.h 159049 2006-05-29 20:15:29Z 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.
61101704Smjacob */
62101704Smjacob/*
63147883Sscottl * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
64147883Sscottl * Copyright (c) 2004, 2005 Justin T. Gibbs
65147883Sscottl * Copyright (c) 2005, WHEEL Sp. z o.o.
66147883Sscottl * All rights reserved.
67147883Sscottl *
68147883Sscottl * Redistribution and use in source and binary forms, with or without
69147883Sscottl * modification, are permitted provided that the following conditions are
70147883Sscottl * met:
71147883Sscottl * 1. Redistributions of source code must retain the above copyright
72147883Sscottl *    notice, this list of conditions and the following disclaimer.
73147883Sscottl * 2. Redistributions in binary form must reproduce at minimum a disclaimer
74147883Sscottl *    substantially similar to the "NO WARRANTY" disclaimer below
75147883Sscottl *    ("Disclaimer") and any redistribution must be conditioned upon including
76147883Sscottl *    a substantially similar Disclaimer requirement for further binary
77147883Sscottl *    redistribution.
78148679Sgibbs * 3. Neither the names of the above listed copyright holders nor the names
79148679Sgibbs *    of any contributors may be used to endorse or promote products derived
80148679Sgibbs *    from this software without specific prior written permission.
81147883Sscottl *
82147883Sscottl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
83147883Sscottl * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84147883Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85147883Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
86147883Sscottl * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87147883Sscottl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88147883Sscottl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
89147883Sscottl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90147883Sscottl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
91147883Sscottl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
92147883Sscottl * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93101704Smjacob */
94101704Smjacob
95101704Smjacob#ifndef _MPT_H_
96101704Smjacob#define _MPT_H_
97147883Sscottl
98147883Sscottl/********************************* OS Includes ********************************/
99147883Sscottl#include <sys/types.h>
100147883Sscottl#include <sys/param.h>
101147883Sscottl#include <sys/systm.h>
102147883Sscottl#include <sys/endian.h>
103147883Sscottl#include <sys/eventhandler.h>
104147883Sscottl#if __FreeBSD_version < 500000
105147883Sscottl#include <sys/kernel.h>
106147883Sscottl#include <sys/queue.h>
107147883Sscottl#include <sys/malloc.h>
108147883Sscottl#else
109147883Sscottl#include <sys/lock.h>
110147883Sscottl#include <sys/kernel.h>
111147883Sscottl#include <sys/queue.h>
112147883Sscottl#include <sys/malloc.h>
113147883Sscottl#include <sys/mutex.h>
114147883Sscottl#include <sys/condvar.h>
115147883Sscottl#endif
116147883Sscottl#include <sys/proc.h>
117147883Sscottl#include <sys/bus.h>
118147883Sscottl#include <sys/module.h>
119147883Sscottl
120147883Sscottl#include <machine/cpu.h>
121147883Sscottl#include <machine/resource.h>
122147883Sscottl
123157117Smjacob#if __FreeBSD_version < 500000
124157117Smjacob#include <machine/bus.h>
125157117Smjacob#endif
126157117Smjacob
127147883Sscottl#include <sys/rman.h>
128147883Sscottl
129155521Smjacob#if __FreeBSD_version < 500000
130155521Smjacob#include <pci/pcireg.h>
131155521Smjacob#include <pci/pcivar.h>
132155521Smjacob#else
133155521Smjacob#include <dev/pci/pcireg.h>
134155521Smjacob#include <dev/pci/pcivar.h>
135155521Smjacob#endif
136155521Smjacob
137155521Smjacob#include <machine/bus.h>
138147883Sscottl#include "opt_ddb.h"
139147883Sscottl
140147883Sscottl/**************************** Register Definitions ****************************/
141147883Sscottl#include <dev/mpt/mpt_reg.h>
142147883Sscottl
143147883Sscottl/******************************* MPI Definitions ******************************/
144147883Sscottl#include <dev/mpt/mpilib/mpi_type.h>
145147883Sscottl#include <dev/mpt/mpilib/mpi.h>
146147883Sscottl#include <dev/mpt/mpilib/mpi_cnfg.h>
147147883Sscottl#include <dev/mpt/mpilib/mpi_ioc.h>
148147883Sscottl#include <dev/mpt/mpilib/mpi_raid.h>
149147883Sscottl
150147883Sscottl/* XXX For mpt_debug.c */
151147883Sscottl#include <dev/mpt/mpilib/mpi_init.h>
152147883Sscottl
153147883Sscottl/****************************** Misc Definitions ******************************/
154101704Smjacob#define MPT_OK (0)
155101704Smjacob#define MPT_FAIL (0x10000)
156101704Smjacob
157147883Sscottl#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
158101704Smjacob
159157117Smjacob#define	MPT_ROLE_NONE		0
160157117Smjacob#define	MPT_ROLE_INITIATOR	1
161157117Smjacob#define	MPT_ROLE_TARGET		2
162157117Smjacob#define	MPT_ROLE_BOTH		3
163157117Smjacob#define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
164157117Smjacob
165147883Sscottl/**************************** Forward Declarations ****************************/
166147883Sscottlstruct mpt_softc;
167147883Sscottlstruct mpt_personality;
168147883Sscottltypedef struct req_entry request_t;
169101704Smjacob
170147883Sscottl/************************* Personality Module Support *************************/
171147883Sscottltypedef int mpt_load_handler_t(struct mpt_personality *);
172147883Sscottltypedef int mpt_probe_handler_t(struct mpt_softc *);
173147883Sscottltypedef int mpt_attach_handler_t(struct mpt_softc *);
174157117Smjacobtypedef int mpt_enable_handler_t(struct mpt_softc *);
175147883Sscottltypedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
176147883Sscottl				MSG_EVENT_NOTIFY_REPLY *);
177147883Sscottltypedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
178147883Sscottl/* XXX Add return value and use for veto? */
179147883Sscottltypedef void mpt_shutdown_handler_t(struct mpt_softc *);
180147883Sscottltypedef void mpt_detach_handler_t(struct mpt_softc *);
181147883Sscottltypedef int mpt_unload_handler_t(struct mpt_personality *);
182147883Sscottl
183147883Sscottlstruct mpt_personality
184147883Sscottl{
185147883Sscottl	const char		*name;
186147883Sscottl	uint32_t		 id;		/* Assigned identifier. */
187147883Sscottl	u_int			 use_count;	/* Instances using personality*/
188147883Sscottl	mpt_load_handler_t	*load;		/* configure personailty */
189147883Sscottl#define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
190147883Sscottl	mpt_probe_handler_t	*probe;		/* configure personailty */
191147883Sscottl	mpt_attach_handler_t	*attach;	/* initialize device instance */
192157117Smjacob	mpt_enable_handler_t	*enable;	/* enable device */
193147883Sscottl	mpt_event_handler_t	*event;		/* Handle MPI event. */
194147883Sscottl	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
195147883Sscottl	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
196147883Sscottl	mpt_detach_handler_t	*detach;	/* release device instance */
197147883Sscottl	mpt_unload_handler_t	*unload;	/* Shutdown personality */
198147883Sscottl#define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
199101704Smjacob};
200101704Smjacob
201147883Sscottlint mpt_modevent(module_t, int, void *);
202101704Smjacob
203147883Sscottl/* Maximum supported number of personalities. */
204147883Sscottl#define MPT_MAX_PERSONALITIES	(15)
205101704Smjacob
206147883Sscottl#define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
207147883Sscottl	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
208101704Smjacob
209147883Sscottl#define DECLARE_MPT_PERSONALITY(name, order)				  \
210147883Sscottl	static moduledata_t name##_mod = {				  \
211147883Sscottl		#name, mpt_modevent, &name##_personality		  \
212147883Sscottl	};								  \
213147883Sscottl	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
214147883Sscottl	MODULE_VERSION(name, 1);					  \
215147883Sscottl	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
216147883Sscottl
217147883Sscottl/******************************* Bus DMA Support ******************************/
218147883Sscottl/* XXX Need to update bus_dmamap_sync to take a range argument. */
219147883Sscottl#define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
220147883Sscottl	bus_dmamap_sync(dma_tag, dmamap, op)
221147883Sscottl
222147883Sscottl#if __FreeBSD_version >= 501102
223147883Sscottl#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
224147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
225147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
226147883Sscottl			   dma_tagp)					\
227147883Sscottl	bus_dma_tag_create(parent_tag, alignment, boundary,		\
228147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
229147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
230147883Sscottl			   busdma_lock_mutex, &Giant,			\
231147883Sscottl			   dma_tagp)
232147883Sscottl#else
233147883Sscottl#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
234147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
235147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
236147883Sscottl			   dma_tagp)					\
237147883Sscottl	bus_dma_tag_create(parent_tag, alignment, boundary,		\
238147883Sscottl			   lowaddr, highaddr, filter, filterarg,	\
239147883Sscottl			   maxsize, nsegments, maxsegsz, flags,		\
240147883Sscottl			   dma_tagp)
241147883Sscottl#endif
242147883Sscottl
243147883Sscottlstruct mpt_map_info {
244147883Sscottl	struct mpt_softc *mpt;
245147883Sscottl	int		  error;
246147883Sscottl	uint32_t	  phys;
247147883Sscottl};
248147883Sscottl
249147883Sscottlvoid mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
250147883Sscottl
251147883Sscottl/**************************** Kernel Thread Support ***************************/
252147883Sscottl#if __FreeBSD_version > 500005
253147883Sscottl#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
254147883Sscottl	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
255147883Sscottl#else
256147883Sscottl#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
257147883Sscottl	kthread_create(func, farg, proc_ptr, fmtstr, arg)
258147883Sscottl#endif
259147883Sscottl
260147883Sscottl/****************************** Timer Facilities ******************************/
261147883Sscottl#if __FreeBSD_version > 500000
262147883Sscottl#define mpt_callout_init(c)	callout_init(c, /*mpsafe*/0);
263147883Sscottl#else
264147883Sscottl#define mpt_callout_init(c)	callout_init(c);
265147883Sscottl#endif
266147883Sscottl
267147883Sscottl/********************************** Endianess *********************************/
268147883Sscottlstatic __inline uint64_t
269147883Sscottlu64toh(U64 s)
270147883Sscottl{
271147883Sscottl	uint64_t result;
272147883Sscottl
273147883Sscottl	result = le32toh(s.Low);
274147883Sscottl	result |= ((uint64_t)le32toh(s.High)) << 32;
275147883Sscottl	return (result);
276147883Sscottl}
277147883Sscottl
278147883Sscottl/**************************** MPI Transaction State ***************************/
279147883Sscottltypedef enum {
280157354Smjacob	REQ_STATE_NIL		= 0x00,
281157354Smjacob	REQ_STATE_FREE		= 0x01,
282157354Smjacob	REQ_STATE_ALLOCATED	= 0x02,
283157354Smjacob	REQ_STATE_QUEUED	= 0x04,
284157354Smjacob	REQ_STATE_DONE		= 0x08,
285157354Smjacob	REQ_STATE_TIMEDOUT	= 0x10,
286157354Smjacob	REQ_STATE_NEED_WAKEUP	= 0x20,
287157354Smjacob	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
288147883Sscottl	REQ_STATE_MASK		= 0xFF
289147883Sscottl} mpt_req_state_t;
290147883Sscottl
291147883Sscottlstruct req_entry {
292147883Sscottl	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
293147883Sscottl	mpt_req_state_t	state;		/* Request State Information */
294147883Sscottl	uint16_t	index;		/* Index of this entry */
295147883Sscottl	uint16_t	IOCStatus;	/* Completion status */
296157662Smjacob	uint16_t	ResponseCode;	/* TMF Reponse Code */
297157662Smjacob	uint16_t	serno;		/* serial number */
298147883Sscottl	union ccb      *ccb;		/* CAM request */
299147883Sscottl	void	       *req_vbuf;	/* Virtual Address of Entry */
300147883Sscottl	void	       *sense_vbuf;	/* Virtual Address of sense data */
301147883Sscottl	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
302147883Sscottl	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
303147883Sscottl	bus_dmamap_t	dmap;		/* DMA map for data buffer */
304155521Smjacob	struct req_entry *chain;	/* for SGE overallocations */
305147883Sscottl};
306147883Sscottl
307157117Smjacob/**************************** MPI Target State Info ***************************/
308157117Smjacob
309157117Smjacobtypedef struct {
310157117Smjacob	uint32_t reply_desc;	/* current reply descriptor */
311157117Smjacob	uint32_t resid;		/* current data residual */
312157117Smjacob	uint32_t bytes_xfered;	/* current relative offset */
313157117Smjacob	union ccb *ccb;		/* pointer to currently active ccb */
314157117Smjacob	request_t *req;		/* pointer to currently active assist request */
315157117Smjacob	int	nxfers;
316157354Smjacob	uint32_t tag_id;
317157117Smjacob	enum {
318157117Smjacob		TGT_STATE_NIL,
319157354Smjacob		TGT_STATE_LOADING,
320157117Smjacob		TGT_STATE_LOADED,
321157117Smjacob		TGT_STATE_IN_CAM,
322157117Smjacob                TGT_STATE_SETTING_UP_FOR_DATA,
323157117Smjacob                TGT_STATE_MOVING_DATA,
324157117Smjacob                TGT_STATE_MOVING_DATA_AND_STATUS,
325157117Smjacob                TGT_STATE_SENDING_STATUS
326157117Smjacob	} state;
327157117Smjacob} mpt_tgt_state_t;
328157117Smjacob
329157117Smjacob/*
330157117Smjacob * When we get an incoming command it has its own tag which is called the
331157117Smjacob * IoIndex. This is the value we gave that particular command buffer when
332157117Smjacob * we originally assigned it. It's just a number, really. The FC card uses
333157117Smjacob * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
334157117Smjacob * contains pointers the request_t structures related to that IoIndex.
335157117Smjacob *
336157117Smjacob * What *we* do is construct a tag out of the index for the target command
337157117Smjacob * which owns the incoming ATIO plus a rolling sequence number.
338157117Smjacob */
339157117Smjacob#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
340157354Smjacob ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
341157117Smjacob
342157117Smjacob#ifdef	INVARIANTS
343157117Smjacob#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
344157117Smjacob#else
345157354Smjacob#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
346157117Smjacob#endif
347157117Smjacob
348157117Smjacob#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
349157117Smjacob    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
350157117Smjacob
351157117SmjacobSTAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
352157117Smjacob#define	MPT_MAX_LUNS	256
353157117Smjacobtypedef struct {
354157117Smjacob	struct mpt_hdr_stailq	atios;
355157117Smjacob	struct mpt_hdr_stailq	inots;
356157117Smjacob	int enabled;
357157117Smjacob} tgt_resource_t;
358157662Smjacob#define	MPT_MAX_ELS	64
359157117Smjacob
360147883Sscottl/**************************** Handler Registration ****************************/
361101704Smjacob/*
362147883Sscottl * Global table of registered reply handlers.  The
363147883Sscottl * handler is indicated by byte 3 of the request
364147883Sscottl * index submitted to the IOC.  This allows the
365147883Sscottl * driver core to perform generic processing without
366147883Sscottl * any knowledge of per-personality behavior.
367147883Sscottl *
368147883Sscottl * MPT_NUM_REPLY_HANDLERS must be a power of 2
369147883Sscottl * to allow the easy generation of a mask.
370147883Sscottl *
371147883Sscottl * The handler offsets used by the core are hard coded
372147883Sscottl * allowing faster code generation when assigning a handler
373147883Sscottl * to a request.  All "personalities" must use the
374147883Sscottl * the handler registration mechanism.
375147883Sscottl *
376147883Sscottl * The IOC handlers that are rarely executed are placed
377147883Sscottl * at the tail of the table to make it more likely that
378147883Sscottl * all commonly executed handlers fit in a single cache
379147883Sscottl * line.
380101704Smjacob */
381157117Smjacob#define MPT_NUM_REPLY_HANDLERS		(32)
382147883Sscottl#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
383147883Sscottl#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
384147883Sscottl#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
385147883Sscottltypedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
386157117Smjacob    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
387147883Sscottltypedef union {
388147883Sscottl	mpt_reply_handler_t	*reply_handler;
389147883Sscottl} mpt_handler_t;
390101704Smjacob
391147883Sscottltypedef enum {
392147883Sscottl	MPT_HANDLER_REPLY,
393147883Sscottl	MPT_HANDLER_EVENT,
394147883Sscottl	MPT_HANDLER_RESET,
395147883Sscottl	MPT_HANDLER_SHUTDOWN
396147883Sscottl} mpt_handler_type;
397101704Smjacob
398147883Sscottlstruct mpt_handler_record
399147883Sscottl{
400147883Sscottl	LIST_ENTRY(mpt_handler_record)	links;
401147883Sscottl	mpt_handler_t			handler;
402101704Smjacob};
403101704Smjacob
404147883SscottlLIST_HEAD(mpt_handler_list, mpt_handler_record);
405147883Sscottl
406147883Sscottl/*
407147883Sscottl * The handler_id is currently unused but would contain the
408147883Sscottl * handler ID used in the MsgContext field to allow direction
409147883Sscottl * of replies to the handler.  Registrations that don't require
410147883Sscottl * a handler id can pass in NULL for the handler_id.
411147883Sscottl *
412147883Sscottl * Deregistrations for handlers without a handler id should
413147883Sscottl * pass in MPT_HANDLER_ID_NONE.
414147883Sscottl */
415147883Sscottl#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
416147883Sscottlint mpt_register_handler(struct mpt_softc *, mpt_handler_type,
417147883Sscottl			 mpt_handler_t, uint32_t *);
418147883Sscottlint mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
419147883Sscottl			   mpt_handler_t, uint32_t);
420147883Sscottl
421147883Sscottl/******************* Per-Controller Instance Data Structures ******************/
422147883SscottlTAILQ_HEAD(req_queue, req_entry);
423147883Sscottl
424147883Sscottl/* Structure for saving proper values for modifyable PCI config registers */
425147883Sscottlstruct mpt_pci_cfg {
426147883Sscottl	uint16_t Command;
427147883Sscottl	uint16_t LatencyTimer_LineSize;
428147883Sscottl	uint32_t IO_BAR;
429147883Sscottl	uint32_t Mem0_BAR[2];
430147883Sscottl	uint32_t Mem1_BAR[2];
431147883Sscottl	uint32_t ROM_BAR;
432147883Sscottl	uint8_t  IntLine;
433147883Sscottl	uint32_t PMCSR;
434101704Smjacob};
435101704Smjacob
436147883Sscottltypedef enum {
437147883Sscottl	MPT_RVF_NONE		= 0x0,
438147883Sscottl	MPT_RVF_ACTIVE		= 0x1,
439147883Sscottl	MPT_RVF_ANNOUNCED	= 0x2,
440147883Sscottl	MPT_RVF_UP2DATE		= 0x4,
441147883Sscottl	MPT_RVF_REFERENCED	= 0x8,
442147883Sscottl	MPT_RVF_WCE_CHANGED	= 0x10
443147883Sscottl} mpt_raid_volume_flags;
444101704Smjacob
445147883Sscottlstruct mpt_raid_volume {
446147883Sscottl	CONFIG_PAGE_RAID_VOL_0	       *config_page;
447147883Sscottl	MPI_RAID_VOL_INDICATOR		sync_progress;
448147883Sscottl	mpt_raid_volume_flags		flags;
449158933Smjacob	u_int				quiesced_disks;
450101704Smjacob};
451101704Smjacob
452147883Sscottltypedef enum {
453147883Sscottl	MPT_RDF_NONE		= 0x00,
454147883Sscottl	MPT_RDF_ACTIVE		= 0x01,
455147883Sscottl	MPT_RDF_ANNOUNCED	= 0x02,
456147883Sscottl	MPT_RDF_UP2DATE		= 0x04,
457147883Sscottl	MPT_RDF_REFERENCED	= 0x08,
458147883Sscottl	MPT_RDF_QUIESCING	= 0x10,
459147883Sscottl	MPT_RDF_QUIESCED	= 0x20
460147883Sscottl} mpt_raid_disk_flags;
461147883Sscottl
462147883Sscottlstruct mpt_raid_disk {
463147883Sscottl	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
464147883Sscottl	struct mpt_raid_volume	       *volume;
465147883Sscottl	u_int				member_number;
466147883Sscottl	u_int				pass_thru_active;
467147883Sscottl	mpt_raid_disk_flags		flags;
468101704Smjacob};
469101704Smjacob
470147883Sscottlstruct mpt_evtf_record {
471147883Sscottl	MSG_EVENT_NOTIFY_REPLY		reply;
472147883Sscottl	uint32_t			context;
473147883Sscottl	LIST_ENTRY(mpt_evtf_record)	links;
474147883Sscottl};
475101704Smjacob
476147883SscottlLIST_HEAD(mpt_evtf_list, mpt_evtf_record);
477101704Smjacob
478147883Sscottlstruct mpt_softc {
479147883Sscottl	device_t		dev;
480147883Sscottl#if __FreeBSD_version < 500000
481157662Smjacob	uint32_t		mpt_islocked;
482147883Sscottl	int			mpt_splsaved;
483147883Sscottl#else
484147883Sscottl	struct mtx		mpt_lock;
485157662Smjacob	int			mpt_locksetup;
486147883Sscottl#endif
487147883Sscottl	uint32_t		mpt_pers_mask;
488157117Smjacob	uint32_t		: 8,
489157117Smjacob		unit		: 8,
490157117Smjacob				: 1,
491157117Smjacob		twildcard	: 1,
492157117Smjacob		tenabled	: 1,
493157117Smjacob		role		: 2,	/* none, ini, target, both */
494158982Smjacob				: 1,
495158982Smjacob		raid_enabled	: 1,
496147883Sscottl		raid_mwce_set	: 1,
497147883Sscottl		getreqwaiter	: 1,
498147883Sscottl		shutdwn_raid    : 1,
499147883Sscottl		shutdwn_recovery: 1,
500147883Sscottl		outofbeer	: 1,
501157662Smjacob				: 1,
502147883Sscottl		disabled	: 1,
503157117Smjacob		is_sas		: 1,
504157117Smjacob		is_fc		: 1;
505101704Smjacob
506147883Sscottl	u_int			verbose;
507101704Smjacob
508147883Sscottl	/*
509147883Sscottl	 * IOC Facts
510147883Sscottl	 */
511147883Sscottl	uint16_t	mpt_global_credits;
512147883Sscottl	uint16_t	request_frame_size;
513147883Sscottl	uint8_t		mpt_max_devices;
514147883Sscottl	uint8_t		mpt_max_buses;
515155521Smjacob	uint8_t		ioc_facts_flags;
516157354Smjacob	uint8_t		padding0;
517101704Smjacob
518147883Sscottl	/*
519147883Sscottl	 * Port Facts
520147883Sscottl	 * XXX - Add multi-port support!.
521147883Sscottl	 */
522147883Sscottl	uint16_t	mpt_ini_id;
523147883Sscottl	uint16_t	mpt_port_type;
524147883Sscottl	uint16_t	mpt_proto_flags;
525157117Smjacob	uint16_t	mpt_max_tgtcmds;
526102822Smjacob
527147883Sscottl	/*
528147883Sscottl	 * Device Configuration Information
529147883Sscottl	 */
530147883Sscottl	union {
531147883Sscottl		struct mpt_spi_cfg {
532147883Sscottl			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
533147883Sscottl			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
534147883Sscottl			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
535147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
536147883Sscottl			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
537147883Sscottl			uint16_t			_tag_enable;
538147883Sscottl			uint16_t			_disc_enable;
539147883Sscottl		} spi;
540147883Sscottl#define	mpt_port_page0		cfg.spi._port_page0
541147883Sscottl#define	mpt_port_page1		cfg.spi._port_page1
542147883Sscottl#define	mpt_port_page2		cfg.spi._port_page2
543147883Sscottl#define	mpt_dev_page0		cfg.spi._dev_page0
544147883Sscottl#define	mpt_dev_page1		cfg.spi._dev_page1
545147883Sscottl#define	mpt_tag_enable		cfg.spi._tag_enable
546147883Sscottl#define	mpt_disc_enable		cfg.spi._disc_enable
547147883Sscottl		struct mpi_fc_cfg {
548157117Smjacob			CONFIG_PAGE_FC_PORT_0 _port_page0;
549157117Smjacob#define	mpt_fcport_page0	cfg.fc._port_page0
550147883Sscottl		} fc;
551147883Sscottl	} cfg;
552147883Sscottl
553158982Smjacob	/* Controller Info for RAID information */
554147883Sscottl	CONFIG_PAGE_IOC_2 *	ioc_page2;
555147883Sscottl	CONFIG_PAGE_IOC_3 *	ioc_page3;
556147883Sscottl
557147883Sscottl	/* Raid Data */
558147883Sscottl	struct mpt_raid_volume* raid_volumes;
559147883Sscottl	struct mpt_raid_disk*	raid_disks;
560147883Sscottl	u_int			raid_max_volumes;
561147883Sscottl	u_int			raid_max_disks;
562147883Sscottl	u_int			raid_page0_len;
563147883Sscottl	u_int			raid_wakeup;
564147883Sscottl	u_int			raid_rescan;
565147883Sscottl	u_int			raid_resync_rate;
566147883Sscottl	u_int			raid_mwce_setting;
567147883Sscottl	u_int			raid_queue_depth;
568152444Skan	u_int			raid_nonopt_volumes;
569147883Sscottl	struct proc	       *raid_thread;
570147883Sscottl	struct callout		raid_timer;
571147883Sscottl
572147883Sscottl	/*
573147883Sscottl	 * PCI Hardware info
574147883Sscottl	 */
575147883Sscottl	struct resource *	pci_irq;	/* Interrupt map for chip */
576147883Sscottl	void *			ih;		/* Interupt handle */
577147883Sscottl	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
578147883Sscottl
579147883Sscottl	/*
580147883Sscottl	 * DMA Mapping Stuff
581147883Sscottl	 */
582147883Sscottl	struct resource *	pci_reg;	/* Register map for chip */
583147883Sscottl	int			pci_mem_rid;	/* Resource ID */
584147883Sscottl	bus_space_tag_t		pci_st;		/* Bus tag for registers */
585147883Sscottl	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
586147883Sscottl	/* PIO versions of above. */
587147883Sscottl	int			pci_pio_rid;
588147883Sscottl	struct resource *	pci_pio_reg;
589147883Sscottl	bus_space_tag_t		pci_pio_st;
590147883Sscottl	bus_space_handle_t	pci_pio_sh;
591147883Sscottl
592147883Sscottl	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
593147883Sscottl	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
594147883Sscottl	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
595147883Sscottl	uint8_t		       *reply;		/* KVA of reply memory */
596147883Sscottl	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
597147883Sscottl
598147883Sscottl	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
599147883Sscottl	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
600147883Sscottl	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
601147883Sscottl	uint8_t		       *request;	/* KVA of Request memory */
602157117Smjacob	bus_addr_t		request_phys;	/* BusAddr of request memory */
603147883Sscottl
604155521Smjacob	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
605155521Smjacob
606155521Smjacob	/*
607155521Smjacob	 * Hardware management
608155521Smjacob	 */
609147883Sscottl	u_int			reset_cnt;
610147883Sscottl
611147883Sscottl	/*
612147883Sscottl	 * CAM && Software Management
613147883Sscottl	 */
614147883Sscottl	request_t	       *request_pool;
615147883Sscottl	struct req_queue	request_free_list;
616147883Sscottl	struct req_queue	request_pending_list;
617147883Sscottl	struct req_queue	request_timeout_list;
618147883Sscottl
619147883Sscottl
620147883Sscottl	struct cam_sim	       *sim;
621147883Sscottl	struct cam_path	       *path;
622147883Sscottl
623147883Sscottl	struct cam_sim	       *phydisk_sim;
624147883Sscottl	struct cam_path	       *phydisk_path;
625147883Sscottl
626147883Sscottl	struct proc	       *recovery_thread;
627147883Sscottl	request_t	       *tmf_req;
628147883Sscottl
629157117Smjacob	/*
630157354Smjacob	 * Deferred frame acks due to resource shortage.
631157354Smjacob	 */
632157354Smjacob	struct mpt_evtf_list	ack_frames;
633157354Smjacob	/*
634157117Smjacob	 * Target Mode Support
635157117Smjacob	 */
636157117Smjacob	uint32_t		scsi_tgt_handler_id;
637157117Smjacob	request_t **		tgt_cmd_ptrs;
638157662Smjacob	request_t **		els_cmd_ptrs;	/* FC only */
639147883Sscottl
640157117Smjacob	/*
641157117Smjacob	 * *snork*- this is chosen to be here *just in case* somebody
642157117Smjacob	 * forgets to point to it exactly and we index off of trt with
643157117Smjacob	 * CAM_LUN_WILDCARD.
644157117Smjacob	 */
645157117Smjacob	tgt_resource_t		trt_wildcard;	/* wildcard luns */
646157117Smjacob	tgt_resource_t		trt[MPT_MAX_LUNS];
647157117Smjacob	uint16_t		tgt_cmds_allocated;
648157662Smjacob	uint16_t		els_cmds_allocated;	/* FC only */
649157117Smjacob
650157117Smjacob	uint16_t		timeouts;	/* timeout count */
651157117Smjacob	uint16_t		success;	/* successes afer timeout */
652157662Smjacob	uint16_t		sequence;	/* Sequence Number */
653157662Smjacob	uint16_t		pad3;
654157117Smjacob
655157117Smjacob
656147883Sscottl	/* Opposing port in a 929 or 1030, or NULL */
657147883Sscottl	struct mpt_softc *	mpt2;
658147883Sscottl
659147883Sscottl	/* FW Image management */
660147883Sscottl	uint32_t		fw_image_size;
661147883Sscottl	uint8_t		       *fw_image;
662147883Sscottl	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
663147883Sscottl	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
664147883Sscottl	bus_addr_t		fw_phys;	/* BusAddr of request memory */
665147883Sscottl
666147883Sscottl	/* Shutdown Event Handler. */
667147883Sscottl	eventhandler_tag         eh;
668147883Sscottl
669147883Sscottl	TAILQ_ENTRY(mpt_softc)	links;
670147883Sscottl};
671147883Sscottl
672157662Smjacobstatic __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
673157662Smjacob
674157662Smjacobstatic __inline void
675157662Smjacobmpt_assign_serno(struct mpt_softc *mpt, request_t *req)
676157662Smjacob{
677157662Smjacob	if ((req->serno = mpt->sequence++) == 0) {
678157662Smjacob		req->serno = mpt->sequence++;
679157662Smjacob	}
680157662Smjacob}
681157662Smjacob
682157117Smjacob/***************************** Locking Primitives *****************************/
683147883Sscottl#if __FreeBSD_version < 500000
684147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM
685147883Sscottl#define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
686147883Sscottl#define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
687157662Smjacob#define	MPT_OWNED(mpt)		mpt->mpt_islocked
688147883Sscottl#define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
689147883Sscottl#define	CAMLOCK_2_MPTLOCK	MPT_LOCK
690147883Sscottl#define	MPT_LOCK_SETUP(mpt)
691147883Sscottl#define	MPT_LOCK_DESTROY(mpt)
692147883Sscottl
693147883Sscottlstatic __inline void mpt_lockspl(struct mpt_softc *mpt);
694147883Sscottlstatic __inline void mpt_unlockspl(struct mpt_softc *mpt);
695147883Sscottl
696147883Sscottlstatic __inline void
697147883Sscottlmpt_lockspl(struct mpt_softc *mpt)
698147883Sscottl{
699147883Sscottl       int s;
700147883Sscottl
701147883Sscottl       s = splcam();
702147883Sscottl       if (mpt->mpt_islocked++ == 0) {
703147883Sscottl               mpt->mpt_splsaved = s;
704147883Sscottl       } else {
705147883Sscottl               splx(s);
706147883Sscottl	       panic("Recursed lock with mask: 0x%x\n", s);
707147883Sscottl       }
708147883Sscottl}
709147883Sscottl
710147883Sscottlstatic __inline void
711147883Sscottlmpt_unlockspl(struct mpt_softc *mpt)
712147883Sscottl{
713147883Sscottl       if (mpt->mpt_islocked) {
714147883Sscottl               if (--mpt->mpt_islocked == 0) {
715147883Sscottl                       splx(mpt->mpt_splsaved);
716147883Sscottl               }
717147883Sscottl       } else
718147883Sscottl	       panic("Negative lock count\n");
719147883Sscottl}
720147883Sscottl
721147883Sscottlstatic __inline int
722147883Sscottlmpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
723147883Sscottl	   const char *wmesg, int timo)
724147883Sscottl{
725147883Sscottl	int saved_cnt;
726147883Sscottl	int saved_spl;
727147883Sscottl	int error;
728147883Sscottl
729147883Sscottl	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
730147883Sscottl	saved_cnt = mpt->mpt_islocked;
731147883Sscottl	saved_spl = mpt->mpt_splsaved;
732147883Sscottl	mpt->mpt_islocked = 0;
733147883Sscottl	error = tsleep(ident, priority, wmesg, timo);
734157117Smjacob	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
735147883Sscottl	mpt->mpt_islocked = saved_cnt;
736147883Sscottl	mpt->mpt_splsaved = saved_spl;
737147883Sscottl	return (error);
738147883Sscottl}
739147883Sscottl
740147883Sscottl#else
741153072Sru#ifdef	LOCKING_WORKED_AS_IT_SHOULD
742147883Sscottl#error "Shouldn't Be Here!"
743147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
744147883Sscottl#define	MPT_LOCK_SETUP(mpt)						\
745147883Sscottl		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
746147883Sscottl		mpt->mpt_locksetup = 1
747147883Sscottl#define	MPT_LOCK_DESTROY(mpt)						\
748147883Sscottl	if (mpt->mpt_locksetup) {					\
749147883Sscottl		mtx_destroy(&mpt->mpt_lock);				\
750147883Sscottl		mpt->mpt_locksetup = 0;					\
751147883Sscottl	}
752147883Sscottl
753147883Sscottl#define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
754147883Sscottl#define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
755157662Smjacob#define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
756147883Sscottl#define	MPTLOCK_2_CAMLOCK(mpt)	\
757147883Sscottl	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
758147883Sscottl#define	CAMLOCK_2_MPTLOCK(mpt)	\
759147883Sscottl	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
760147883Sscottl#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
761147883Sscottl	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
762157662Smjacob
763147883Sscottl#else
764157662Smjacob
765147883Sscottl#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
766147883Sscottl#define	MPT_LOCK_SETUP(mpt)	do { } while (0)
767147883Sscottl#define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
768157662Smjacob#if	0
769157662Smjacob#define	MPT_LOCK(mpt)		\
770157662Smjacob	device_printf(mpt->dev, "LOCK %s:%d\n", __FILE__, __LINE__); 	\
771157662Smjacob	KASSERT(mpt->mpt_locksetup == 0,				\
772157662Smjacob	    ("recursive lock acquire at %s:%d", __FILE__, __LINE__));	\
773157662Smjacob	mpt->mpt_locksetup = 1
774157662Smjacob#define	MPT_UNLOCK(mpt)		\
775157662Smjacob	device_printf(mpt->dev, "UNLK %s:%d\n", __FILE__, __LINE__); 	\
776157662Smjacob	KASSERT(mpt->mpt_locksetup == 1,				\
777157662Smjacob	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
778157662Smjacob	mpt->mpt_locksetup = 0
779157662Smjacob#else
780157662Smjacob#define	MPT_LOCK(mpt)							\
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	KASSERT(mpt->mpt_locksetup == 1,				\
786157662Smjacob	    ("release unowned lock at %s:%d", __FILE__, __LINE__));	\
787157662Smjacob	mpt->mpt_locksetup = 0
788147883Sscottl#endif
789157662Smjacob#define	MPT_OWNED(mpt)		mpt->mpt_locksetup
790157662Smjacob#define	MPTLOCK_2_CAMLOCK(mpt)	MPT_UNLOCK(mpt)
791157662Smjacob#define	CAMLOCK_2_MPTLOCK(mpt)	MPT_LOCK(mpt)
792157662Smjacob
793157662Smjacobstatic __inline int
794157662Smjacobmpt_sleep(struct mpt_softc *, void *, int, const char *, int);
795157662Smjacob
796157662Smjacobstatic __inline int
797157662Smjacobmpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
798157662Smjacob{
799157662Smjacob	int r;
800157662Smjacob	MPT_UNLOCK(mpt);
801157662Smjacob	r = tsleep(i, p, w, t);
802157662Smjacob	MPT_LOCK(mpt);
803157662Smjacob	return (r);
804157662Smjacob}
805147883Sscottl#endif
806157662Smjacob#endif
807147883Sscottl
808147883Sscottl/******************************* Register Access ******************************/
809147883Sscottlstatic __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
810147883Sscottlstatic __inline uint32_t mpt_read(struct mpt_softc *, int);
811147883Sscottlstatic __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
812147883Sscottlstatic __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
813147883Sscottl
814147883Sscottlstatic __inline void
815147883Sscottlmpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
816147883Sscottl{
817147883Sscottl	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
818147883Sscottl}
819147883Sscottl
820147883Sscottlstatic __inline uint32_t
821147883Sscottlmpt_read(struct mpt_softc *mpt, int offset)
822147883Sscottl{
823147883Sscottl	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
824147883Sscottl}
825147883Sscottl
826147883Sscottl/*
827147883Sscottl * Some operations (e.g. diagnostic register writes while the ARM proccessor
828147883Sscottl * is disabled), must be performed using "PCI pio" operations.  On non-PCI
829147883Sscottl * busses, these operations likely map to normal register accesses.
830147883Sscottl */
831147883Sscottlstatic __inline void
832147883Sscottlmpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
833147883Sscottl{
834147883Sscottl	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
835147883Sscottl}
836147883Sscottl
837147883Sscottlstatic __inline uint32_t
838147883Sscottlmpt_pio_read(struct mpt_softc *mpt, int offset)
839147883Sscottl{
840147883Sscottl	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
841147883Sscottl}
842147883Sscottl/*********************** Reply Frame/Request Management ***********************/
843147883Sscottl/* Max MPT Reply we are willing to accept (must be power of 2) */
844155521Smjacob#define MPT_REPLY_SIZE   	256
845147883Sscottl
846157117Smjacob/*
847157117Smjacob * Must be less than 16384 in order for target mode to work
848157117Smjacob */
849155521Smjacob#define MPT_MAX_REQUESTS(mpt)	512
850155521Smjacob#define MPT_REQUEST_AREA	512
851155521Smjacob#define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
852147883Sscottl#define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
853147883Sscottl
854157354Smjacob#define MPT_CONTEXT_CB_SHIFT	(16)
855157117Smjacob#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
856147883Sscottl#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
857147883Sscottl#define MPT_CONTEXT_TO_CBI(x)	\
858147883Sscottl    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
859157354Smjacob#define MPT_CONTEXT_REQI_MASK	0xFFFF
860157117Smjacob#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
861147883Sscottl
862147883Sscottl/*
863147883Sscottl * Convert a 32bit physical address returned from IOC to an
864147883Sscottl * offset into our reply frame memory or the kvm address needed
865147883Sscottl * to access the data.  The returned address is only the low
866147883Sscottl * 32 bits, so mask our base physical address accordingly.
867147883Sscottl */
868147883Sscottl#define MPT_REPLY_BADDR(x)		\
869147883Sscottl	(x << 1)
870147883Sscottl#define MPT_REPLY_OTOV(m, i) 		\
871147883Sscottl	((void *)(&m->reply[i]))
872147883Sscottl
873147883Sscottl#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
874147883Sscottldo {							\
875158982Smjacob	if (mpt->verbose > MPT_PRT_DEBUG)		\
876147883Sscottl		mpt_dump_reply_frame(mpt, reply_frame);	\
877147883Sscottl} while(0)
878147883Sscottl
879147883Sscottlstatic __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
880147883Sscottlstatic __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
881147883Sscottl
882147883Sscottl/*
883147883Sscottl * Give the reply buffer back to the IOC after we have
884147883Sscottl * finished processing it.
885147883Sscottl */
886147883Sscottlstatic __inline void
887147883Sscottlmpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
888147883Sscottl{
889147883Sscottl     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
890147883Sscottl}
891147883Sscottl
892147883Sscottl/* Get a reply from the IOC */
893147883Sscottlstatic __inline uint32_t
894147883Sscottlmpt_pop_reply_queue(struct mpt_softc *mpt)
895147883Sscottl{
896147883Sscottl     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
897147883Sscottl}
898147883Sscottl
899157354Smjacobvoid
900157354Smjacobmpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
901157354Smjacob
902147883Sscottl/************************** Scatter Gather Managment **************************/
903155521Smjacob/* MPT_RQSL- size of request frame, in bytes */
904155521Smjacob#define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
905155521Smjacob
906155521Smjacob/* MPT_NSGL- how many SG entries can fit in a request frame size */
907155521Smjacob#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
908155521Smjacob
909155521Smjacob/* MPT_NRFM- how many request frames can fit in each request alloc we make */
910155521Smjacob#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
911155521Smjacob
912147883Sscottl/*
913155521Smjacob * MPT_NSGL_FIRST- # of SG elements that can fit after
914155521Smjacob * an I/O request but still within the request frame.
915155521Smjacob * Do this safely based upon SGE_IO_UNION.
916155521Smjacob *
917155521Smjacob * Note that the first element is *within* the SCSI request.
918147883Sscottl */
919155521Smjacob#define	MPT_NSGL_FIRST(mpt)	\
920155521Smjacob    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
921155521Smjacob    sizeof (SGE_IO_UNION))
922147883Sscottl
923147883Sscottl/***************************** IOC Initialization *****************************/
924147883Sscottlint mpt_reset(struct mpt_softc *, int /*reinit*/);
925147883Sscottl
926157117Smjacob/****************************** Debugging ************************************/
927147883Sscottltypedef struct mpt_decode_entry {
928147883Sscottl	char    *name;
929147883Sscottl	u_int	 value;
930147883Sscottl	u_int	 mask;
931147883Sscottl} mpt_decode_entry_t;
932147883Sscottl
933147883Sscottlint mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
934147883Sscottl		     const char *name, u_int value, u_int *cur_column,
935147883Sscottl		     u_int wrap_point);
936147883Sscottl
937158933Smjacobvoid mpt_dump_request(struct mpt_softc *, request_t *);
938158933Smjacob
939147883Sscottlenum {
940147883Sscottl	MPT_PRT_ALWAYS,
941147883Sscottl	MPT_PRT_FATAL,
942147883Sscottl	MPT_PRT_ERROR,
943147883Sscottl	MPT_PRT_WARN,
944147883Sscottl	MPT_PRT_INFO,
945159049Smjacob	MPT_PRT_NEGOTIATION,
946147883Sscottl	MPT_PRT_DEBUG,
947157117Smjacob	MPT_PRT_DEBUG1,
948157117Smjacob	MPT_PRT_DEBUG2,
949157117Smjacob	MPT_PRT_DEBUG3,
950155521Smjacob	MPT_PRT_TRACE,
951155521Smjacob	MPT_PRT_NONE=100
952147883Sscottl};
953147883Sscottl
954157117Smjacob#if __FreeBSD_version > 500000
955147883Sscottl#define mpt_lprt(mpt, level, ...)		\
956147883Sscottldo {						\
957147883Sscottl	if (level <= (mpt)->verbose)		\
958147883Sscottl		mpt_prt(mpt, __VA_ARGS__);	\
959147883Sscottl} while (0)
960147883Sscottl
961147883Sscottl#define mpt_lprtc(mpt, level, ...)		 \
962147883Sscottldo {						 \
963147883Sscottl	if (level <= (mpt)->debug_level)	 \
964147883Sscottl		mpt_prtc(mpt, __VA_ARGS__);	 \
965147883Sscottl} while (0)
966157117Smjacob#else
967157117Smjacobvoid mpt_lprt(struct mpt_softc *, int, const char *, ...)
968157117Smjacob	__printflike(3, 4);
969157117Smjacobvoid mpt_lprtc(struct mpt_softc *, int, const char *, ...)
970157117Smjacob	__printflike(3, 4);
971157117Smjacob#endif
972157117Smjacobvoid mpt_prt(struct mpt_softc *, const char *, ...)
973157117Smjacob	__printflike(2, 3);
974157117Smjacobvoid mpt_prtc(struct mpt_softc *, const char *, ...)
975157117Smjacob	__printflike(2, 3);
976147883Sscottl
977157117Smjacob/**************************** Target Mode Related ***************************/
978157117Smjacobstatic __inline int mpt_cdblen(uint8_t, int);
979157117Smjacobstatic __inline int
980157117Smjacobmpt_cdblen(uint8_t cdb0, int maxlen)
981157117Smjacob{
982157117Smjacob	int group = cdb0 >> 5;
983157117Smjacob	switch (group) {
984157117Smjacob	case 0:
985157117Smjacob		return (6);
986157117Smjacob	case 1:
987157117Smjacob		return (10);
988157117Smjacob	case 4:
989157117Smjacob	case 5:
990157117Smjacob		return (12);
991157117Smjacob	default:
992157117Smjacob		return (16);
993157117Smjacob	}
994157117Smjacob}
995157117Smjacob#ifdef	INVARIANTS
996157117Smjacobstatic __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
997157117Smjacobstatic __inline request_t *
998157117Smjacobmpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
999157117Smjacob{
1000157354Smjacob	uint16_t rtg = (tag >> 18);
1001157117Smjacob	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1002157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1003157117Smjacob	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1004157117Smjacob	return (mpt->tgt_cmd_ptrs[rtg]);
1005157117Smjacob}
1006157662Smjacob
1007157662Smjacob
1008157662Smjacobstatic __inline int
1009157662Smjacobmpt_req_on_free_list(struct mpt_softc *, request_t *);
1010157662Smjacobstatic __inline int
1011157662Smjacobmpt_req_on_pending_list(struct mpt_softc *, request_t *);
1012157662Smjacob
1013157662Smjacobstatic __inline void
1014157662Smjacobmpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1015157662Smjacobstatic __inline void
1016157662Smjacobmpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1017157662Smjacob
1018157662Smjacob
1019157662Smjacob/*
1020157662Smjacob * Is request on freelist?
1021157662Smjacob */
1022157662Smjacobstatic __inline int
1023157662Smjacobmpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1024157662Smjacob{
1025157662Smjacob	request_t *lrq;
1026157662Smjacob
1027157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1028157662Smjacob		if (lrq == req) {
1029157662Smjacob			return (1);
1030157662Smjacob		}
1031157662Smjacob	}
1032157662Smjacob	return (0);
1033157662Smjacob}
1034157662Smjacob
1035157662Smjacob/*
1036157662Smjacob * Is request on pending list?
1037157662Smjacob */
1038157662Smjacobstatic __inline int
1039157662Smjacobmpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1040157662Smjacob{
1041157662Smjacob	request_t *lrq;
1042157662Smjacob
1043157662Smjacob	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1044157662Smjacob		if (lrq == req) {
1045157662Smjacob			return (1);
1046157662Smjacob		}
1047157662Smjacob	}
1048157662Smjacob	return (0);
1049157662Smjacob}
1050157662Smjacob
1051157662Smjacob/*
1052157662Smjacob * Make sure that req *is* part of one of the special lists
1053157662Smjacob */
1054157662Smjacobstatic __inline void
1055157662Smjacobmpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1056157662Smjacob{
1057157662Smjacob	int i;
1058157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1059157662Smjacob		if (req == mpt->els_cmd_ptrs[i]) {
1060157662Smjacob			return;
1061157662Smjacob		}
1062157662Smjacob	}
1063157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1064157662Smjacob		if (req == mpt->tgt_cmd_ptrs[i]) {
1065157662Smjacob			return;
1066157662Smjacob		}
1067157662Smjacob	}
1068157662Smjacob	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1069157662Smjacob	    s, line, req, req->serno,
1070157662Smjacob	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1071157662Smjacob}
1072157662Smjacob
1073157662Smjacob/*
1074157662Smjacob * Make sure that req is *not* part of one of the special lists.
1075157662Smjacob */
1076157662Smjacobstatic __inline void
1077157662Smjacobmpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1078157662Smjacob{
1079157662Smjacob	int i;
1080157662Smjacob	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1081157662Smjacob		KASSERT(req != mpt->els_cmd_ptrs[i],
1082157662Smjacob		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1083157662Smjacob		    s, line, req, req->serno,
1084157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1085157662Smjacob	}
1086157662Smjacob	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1087157662Smjacob		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1088157662Smjacob		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1089157662Smjacob		    s, line, req, req->serno,
1090157662Smjacob		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1091157662Smjacob	}
1092157662Smjacob}
1093157117Smjacob#endif
1094147883Sscottl
1095157117Smjacobtypedef enum {
1096157117Smjacob	MPT_ABORT_TASK_SET=1234,
1097157117Smjacob	MPT_CLEAR_TASK_SET,
1098157117Smjacob	MPT_TARGET_RESET,
1099157117Smjacob	MPT_CLEAR_ACA,
1100157117Smjacob	MPT_TERMINATE_TASK,
1101157117Smjacob	MPT_NIL_TMT_VALUE=5678
1102157117Smjacob} mpt_task_mgmt_t;
1103157117Smjacob
1104147883Sscottl/**************************** Unclassified Routines ***************************/
1105147883Sscottlvoid		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1106147883Sscottlint		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1107147883Sscottl					 size_t reply_len, void *reply);
1108147883Sscottlint		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1109147883Sscottl			     mpt_req_state_t state, mpt_req_state_t mask,
1110147883Sscottl			     int sleep_ok, int time_ms);
1111147883Sscottlvoid		mpt_enable_ints(struct mpt_softc *mpt);
1112147883Sscottlvoid		mpt_disable_ints(struct mpt_softc *mpt);
1113147883Sscottlint		mpt_attach(struct mpt_softc *mpt);
1114147883Sscottlint		mpt_shutdown(struct mpt_softc *mpt);
1115147883Sscottlint		mpt_detach(struct mpt_softc *mpt);
1116147883Sscottlint		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1117147883Sscottl				       size_t len, void *cmd);
1118147883Sscottlrequest_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1119147883Sscottlvoid		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1120147883Sscottlvoid		mpt_intr(void *arg);
1121147883Sscottlvoid		mpt_check_doorbell(struct mpt_softc *mpt);
1122147883Sscottlvoid		mpt_dump_reply_frame(struct mpt_softc *mpt,
1123147883Sscottl				     MSG_DEFAULT_REPLY *reply_frame);
1124147883Sscottl
1125147883Sscottlvoid		mpt_set_config_regs(struct mpt_softc *);
1126147883Sscottlint		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1127147883Sscottl				  u_int /*Action*/, u_int /*PageVersion*/,
1128147883Sscottl				  u_int /*PageLength*/, u_int /*PageNumber*/,
1129147883Sscottl				  u_int /*PageType*/, uint32_t /*PageAddress*/,
1130147883Sscottl				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1131147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1132147883Sscottlint		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1133147883Sscottl				    int /*PageNumber*/,
1134147883Sscottl				    uint32_t /*PageAddress*/,
1135147883Sscottl				    CONFIG_PAGE_HEADER *,
1136147883Sscottl				    int /*sleep_ok*/, int /*timeout_ms*/);
1137147883Sscottlint		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1138147883Sscottl				  uint32_t /*PageAddress*/,
1139147883Sscottl				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1140147883Sscottl				  int /*sleep_ok*/, int /*timeout_ms*/);
1141147883Sscottlint		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1142147883Sscottl				   uint32_t /*PageAddress*/,
1143147883Sscottl				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1144147883Sscottl				   int /*sleep_ok*/, int /*timeout_ms*/);
1145147883Sscottlstatic __inline int
1146147883Sscottlmpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1147147883Sscottl		      CONFIG_PAGE_HEADER *hdr, size_t len,
1148147883Sscottl		      int sleep_ok, int timeout_ms)
1149147883Sscottl{
1150147883Sscottl	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1151147883Sscottl				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1152147883Sscottl}
1153147883Sscottl
1154147883Sscottlstatic __inline int
1155147883Sscottlmpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1156147883Sscottl		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1157147883Sscottl		       int timeout_ms)
1158147883Sscottl{
1159147883Sscottl	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1160147883Sscottl				   PageAddress, hdr, len, sleep_ok,
1161147883Sscottl				   timeout_ms));
1162147883Sscottl}
1163147883Sscottl
1164101704Smjacob/* mpt_debug.c functions */
1165101704Smjacobvoid mpt_print_reply(void *vmsg);
1166147883Sscottlvoid mpt_print_db(uint32_t mb);
1167101704Smjacobvoid mpt_print_config_reply(void *vmsg);
1168147883Sscottlchar *mpt_ioc_diag(uint32_t diag);
1169147883Sscottlvoid mpt_req_state(mpt_req_state_t state);
1170101704Smjacobvoid mpt_print_config_request(void *vmsg);
1171101704Smjacobvoid mpt_print_request(void *vmsg);
1172147883Sscottlvoid mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1173155521Smjacobvoid mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1174101704Smjacob#endif /* _MPT_H_ */
1175