mpt.h revision 178725
1/* $FreeBSD: head/sys/dev/mpt/mpt.h 178725 2008-05-02 17:02:35Z jkim $ */
2/*-
3 * Generic defines for LSI '909 FC  adapters.
4 * FreeBSD Version.
5 *
6 * Copyright (c)  2000, 2001 by Greg Ansley
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice immediately at the beginning of the file, without modification,
13 *    this list of conditions, and the following disclaimer.
14 * 2. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29/*-
30 * Copyright (c) 2002, 2006 by Matthew Jacob
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions are
35 * met:
36 * 1. Redistributions of source code must retain the above copyright
37 *    notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
39 *    substantially similar to the "NO WARRANTY" disclaimer below
40 *    ("Disclaimer") and any redistribution must be conditioned upon including
41 *    a substantially similar Disclaimer requirement for further binary
42 *    redistribution.
43 * 3. Neither the names of the above listed copyright holders nor the names
44 *    of any contributors may be used to endorse or promote products derived
45 *    from this software without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
57 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 *
59 * Support from Chris Ellsworth in order to make SAS adapters work
60 * is gratefully acknowledged.
61 *
62 *
63 * Support from LSI-Logic has also gone a great deal toward making this a
64 * workable subsystem and is gratefully acknowledged.
65 */
66/*
67 * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
68 * Copyright (c) 2004, 2005 Justin T. Gibbs
69 * Copyright (c) 2005, WHEEL Sp. z o.o.
70 * All rights reserved.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions are
74 * met:
75 * 1. Redistributions of source code must retain the above copyright
76 *    notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
78 *    substantially similar to the "NO WARRANTY" disclaimer below
79 *    ("Disclaimer") and any redistribution must be conditioned upon including
80 *    a substantially similar Disclaimer requirement for further binary
81 *    redistribution.
82 * 3. Neither the names of the above listed copyright holders nor the names
83 *    of any contributors may be used to endorse or promote products derived
84 *    from this software without specific prior written permission.
85 *
86 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
87 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
89 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
90 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
91 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
92 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
94 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
95 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
96 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97 */
98
99#ifndef _MPT_H_
100#define _MPT_H_
101
102/********************************* OS Includes ********************************/
103#include <sys/types.h>
104#include <sys/param.h>
105#include <sys/systm.h>
106#include <sys/endian.h>
107#include <sys/eventhandler.h>
108#if __FreeBSD_version < 500000
109#include <sys/kernel.h>
110#include <sys/queue.h>
111#include <sys/malloc.h>
112#include <sys/devicestat.h>
113#else
114#include <sys/lock.h>
115#include <sys/kernel.h>
116#include <sys/queue.h>
117#include <sys/malloc.h>
118#include <sys/mutex.h>
119#include <sys/condvar.h>
120#endif
121#include <sys/proc.h>
122#include <sys/bus.h>
123#include <sys/module.h>
124
125#include <machine/cpu.h>
126#include <machine/resource.h>
127
128#if __FreeBSD_version < 500000
129#include <machine/bus.h>
130#include <machine/clock.h>
131#endif
132
133#include <sys/rman.h>
134
135#if __FreeBSD_version < 500000
136#include <pci/pcireg.h>
137#include <pci/pcivar.h>
138#else
139#include <dev/pci/pcireg.h>
140#include <dev/pci/pcivar.h>
141#endif
142
143#include <machine/bus.h>
144#include "opt_ddb.h"
145
146/**************************** Register Definitions ****************************/
147#include <dev/mpt/mpt_reg.h>
148
149/******************************* MPI Definitions ******************************/
150#include <dev/mpt/mpilib/mpi_type.h>
151#include <dev/mpt/mpilib/mpi.h>
152#include <dev/mpt/mpilib/mpi_cnfg.h>
153#include <dev/mpt/mpilib/mpi_ioc.h>
154#include <dev/mpt/mpilib/mpi_raid.h>
155
156/* XXX For mpt_debug.c */
157#include <dev/mpt/mpilib/mpi_init.h>
158
159#define	MPT_S64_2_SCALAR(y)	((((int64_t)y.High) << 32) | (y.Low))
160#define	MPT_U64_2_SCALAR(y)	((((uint64_t)y.High) << 32) | (y.Low))
161
162/****************************** Misc Definitions ******************************/
163/* #define MPT_TEST_MULTIPATH	1 */
164#define MPT_OK (0)
165#define MPT_FAIL (0x10000)
166
167#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
168
169#define	MPT_ROLE_NONE		0
170#define	MPT_ROLE_INITIATOR	1
171#define	MPT_ROLE_TARGET		2
172#define	MPT_ROLE_BOTH		3
173#define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
174
175/**************************** Forward Declarations ****************************/
176struct mpt_softc;
177struct mpt_personality;
178typedef struct req_entry request_t;
179
180/************************* Personality Module Support *************************/
181typedef int mpt_load_handler_t(struct mpt_personality *);
182typedef int mpt_probe_handler_t(struct mpt_softc *);
183typedef int mpt_attach_handler_t(struct mpt_softc *);
184typedef int mpt_enable_handler_t(struct mpt_softc *);
185typedef void mpt_ready_handler_t(struct mpt_softc *);
186typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
187				MSG_EVENT_NOTIFY_REPLY *);
188typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
189/* XXX Add return value and use for veto? */
190typedef void mpt_shutdown_handler_t(struct mpt_softc *);
191typedef void mpt_detach_handler_t(struct mpt_softc *);
192typedef int mpt_unload_handler_t(struct mpt_personality *);
193
194struct mpt_personality
195{
196	const char		*name;
197	uint32_t		 id;		/* Assigned identifier. */
198	u_int			 use_count;	/* Instances using personality*/
199	mpt_load_handler_t	*load;		/* configure personailty */
200#define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
201	mpt_probe_handler_t	*probe;		/* configure personailty */
202	mpt_attach_handler_t	*attach;	/* initialize device instance */
203	mpt_enable_handler_t	*enable;	/* enable device */
204	mpt_ready_handler_t	*ready;		/* final open for business */
205	mpt_event_handler_t	*event;		/* Handle MPI event. */
206	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
207	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
208	mpt_detach_handler_t	*detach;	/* release device instance */
209	mpt_unload_handler_t	*unload;	/* Shutdown personality */
210#define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
211};
212
213int mpt_modevent(module_t, int, void *);
214
215/* Maximum supported number of personalities. */
216#define MPT_MAX_PERSONALITIES	(15)
217
218#define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
219	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
220
221#define DECLARE_MPT_PERSONALITY(name, order)				  \
222	static moduledata_t name##_mod = {				  \
223		#name, mpt_modevent, &name##_personality		  \
224	};								  \
225	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
226	MODULE_VERSION(name, 1);					  \
227	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
228
229/******************************* Bus DMA Support ******************************/
230/* XXX Need to update bus_dmamap_sync to take a range argument. */
231#define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
232	bus_dmamap_sync(dma_tag, dmamap, op)
233
234#if __FreeBSD_version < 600000
235#define	bus_get_dma_tag(x)	NULL
236#endif
237#if __FreeBSD_version >= 501102
238#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
239			   lowaddr, highaddr, filter, filterarg,	\
240			   maxsize, nsegments, maxsegsz, flags,		\
241			   dma_tagp)					\
242	bus_dma_tag_create(parent_tag, alignment, boundary,		\
243			   lowaddr, highaddr, filter, filterarg,	\
244			   maxsize, nsegments, maxsegsz, flags,		\
245			   busdma_lock_mutex, &(mpt)->mpt_lock,		\
246			   dma_tagp)
247#else
248#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
249			   lowaddr, highaddr, filter, filterarg,	\
250			   maxsize, nsegments, maxsegsz, flags,		\
251			   dma_tagp)					\
252	bus_dma_tag_create(parent_tag, alignment, boundary,		\
253			   lowaddr, highaddr, filter, filterarg,	\
254			   maxsize, nsegments, maxsegsz, flags,		\
255			   dma_tagp)
256#endif
257
258struct mpt_map_info {
259	struct mpt_softc *mpt;
260	int		  error;
261	uint32_t	  phys;
262};
263
264void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
265/* **************************** NewBUS interrupt Crock ************************/
266#if __FreeBSD_version < 700031
267#define	mpt_setup_intr(d, i, f, U, if, ifa, hp)	\
268	bus_setup_intr(d, i, f, if, ifa, hp)
269#else
270#define	mpt_setup_intr	bus_setup_intr
271#endif
272
273/* **************************** NewBUS CAM Support ****************************/
274#if __FreeBSD_version < 700049
275#define mpt_xpt_bus_register(sim, parent, bus)	\
276	xpt_bus_register(sim, bus)
277#else
278#define mpt_xpt_bus_register	xpt_bus_register
279#endif
280
281/**************************** Kernel Thread Support ***************************/
282#if __FreeBSD_version > 800001
283#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
284	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
285#define	mpt_kthread_exit(status)	\
286	kproc_exit(status)
287#elif __FreeBSD_version > 500005
288#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
289	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
290#define	mpt_kthread_exit(status)	\
291	kthread_exit(status)
292#else
293#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
294	kthread_create(func, farg, proc_ptr, fmtstr, arg)
295#define	mpt_kthread_exit(status)	\
296	kthread_exit(status)
297#endif
298
299/****************************** Timer Facilities ******************************/
300#if __FreeBSD_version > 500000
301#define mpt_callout_init(c)	callout_init(c, /*mpsafe*/1);
302#else
303#define mpt_callout_init(c)	callout_init(c);
304#endif
305
306/********************************** Endianess *********************************/
307#define	MPT_2_HOST64(ptr, tag)	ptr->tag = le64toh(ptr->tag)
308#define	MPT_2_HOST32(ptr, tag)	ptr->tag = le32toh(ptr->tag)
309#define	MPT_2_HOST16(ptr, tag)	ptr->tag = le16toh(ptr->tag)
310
311#define	HOST_2_MPT64(ptr, tag)	ptr->tag = htole64(ptr->tag)
312#define	HOST_2_MPT32(ptr, tag)	ptr->tag = htole32(ptr->tag)
313#define	HOST_2_MPT16(ptr, tag)	ptr->tag = htole16(ptr->tag)
314
315#if	_BYTE_ORDER == _BIG_ENDIAN
316void mpt2host_sge_simple_union(SGE_SIMPLE_UNION *);
317void mpt2host_iocfacts_reply(MSG_IOC_FACTS_REPLY *);
318void mpt2host_portfacts_reply(MSG_PORT_FACTS_REPLY *);
319void mpt2host_config_page_ioc2(CONFIG_PAGE_IOC_2 *);
320void mpt2host_config_page_raid_vol_0(CONFIG_PAGE_RAID_VOL_0 *);
321void mpt2host_mpi_raid_vol_indicator(MPI_RAID_VOL_INDICATOR *);
322#else
323#define	mpt2host_sge_simple_union(x)		do { ; } while (0)
324#define	mpt2host_iocfacts_reply(x)		do { ; } while (0)
325#define	mpt2host_portfacts_reply(x)		do { ; } while (0)
326#define	mpt2host_config_page_ioc2(x)		do { ; } while (0)
327#define	mpt2host_config_page_raid_vol_0(x)	do { ; } while (0)
328#define	mpt2host_mpi_raid_vol_indicator(x)	do { ; } while (0)
329#endif
330
331/**************************** MPI Transaction State ***************************/
332typedef enum {
333	REQ_STATE_NIL		= 0x00,
334	REQ_STATE_FREE		= 0x01,
335	REQ_STATE_ALLOCATED	= 0x02,
336	REQ_STATE_QUEUED	= 0x04,
337	REQ_STATE_DONE		= 0x08,
338	REQ_STATE_TIMEDOUT	= 0x10,
339	REQ_STATE_NEED_WAKEUP	= 0x20,
340	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
341	REQ_STATE_MASK		= 0xFF
342} mpt_req_state_t;
343
344struct req_entry {
345	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
346	mpt_req_state_t	state;		/* Request State Information */
347	uint16_t	index;		/* Index of this entry */
348	uint16_t	IOCStatus;	/* Completion status */
349	uint16_t	ResponseCode;	/* TMF Reponse Code */
350	uint16_t	serno;		/* serial number */
351	union ccb      *ccb;		/* CAM request */
352	void	       *req_vbuf;	/* Virtual Address of Entry */
353	void	       *sense_vbuf;	/* Virtual Address of sense data */
354	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
355	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
356	bus_dmamap_t	dmap;		/* DMA map for data buffers */
357	struct req_entry *chain;	/* for SGE overallocations */
358	struct callout  callout;	/* Timeout for the request */
359};
360
361typedef struct mpt_config_params {
362	u_int		Action;
363	u_int		PageVersion;
364	u_int		PageLength;
365	u_int		PageNumber;
366	u_int		PageType;
367	u_int		PageAddress;
368	u_int		ExtPageLength;
369	u_int		ExtPageType;
370} cfgparms_t;
371
372/**************************** MPI Target State Info ***************************/
373
374typedef struct {
375	uint32_t reply_desc;	/* current reply descriptor */
376	uint32_t resid;		/* current data residual */
377	uint32_t bytes_xfered;	/* current relative offset */
378	union ccb *ccb;		/* pointer to currently active ccb */
379	request_t *req;		/* pointer to currently active assist request */
380	uint32_t
381		is_local : 1,
382		nxfers	 : 31;
383	uint32_t tag_id;
384	enum {
385		TGT_STATE_NIL,
386		TGT_STATE_LOADING,
387		TGT_STATE_LOADED,
388		TGT_STATE_IN_CAM,
389                TGT_STATE_SETTING_UP_FOR_DATA,
390                TGT_STATE_MOVING_DATA,
391                TGT_STATE_MOVING_DATA_AND_STATUS,
392                TGT_STATE_SENDING_STATUS
393	} state;
394} mpt_tgt_state_t;
395
396/*
397 * When we get an incoming command it has its own tag which is called the
398 * IoIndex. This is the value we gave that particular command buffer when
399 * we originally assigned it. It's just a number, really. The FC card uses
400 * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
401 * contains pointers the request_t structures related to that IoIndex.
402 *
403 * What *we* do is construct a tag out of the index for the target command
404 * which owns the incoming ATIO plus a rolling sequence number.
405 */
406#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
407 ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
408
409#ifdef	INVARIANTS
410#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
411#else
412#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
413#endif
414
415#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
416    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
417
418STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
419#define	MPT_MAX_LUNS	256
420typedef struct {
421	struct mpt_hdr_stailq	atios;
422	struct mpt_hdr_stailq	inots;
423	int enabled;
424} tgt_resource_t;
425#define	MPT_MAX_ELS	64
426
427/**************************** Handler Registration ****************************/
428/*
429 * Global table of registered reply handlers.  The
430 * handler is indicated by byte 3 of the request
431 * index submitted to the IOC.  This allows the
432 * driver core to perform generic processing without
433 * any knowledge of per-personality behavior.
434 *
435 * MPT_NUM_REPLY_HANDLERS must be a power of 2
436 * to allow the easy generation of a mask.
437 *
438 * The handler offsets used by the core are hard coded
439 * allowing faster code generation when assigning a handler
440 * to a request.  All "personalities" must use the
441 * the handler registration mechanism.
442 *
443 * The IOC handlers that are rarely executed are placed
444 * at the tail of the table to make it more likely that
445 * all commonly executed handlers fit in a single cache
446 * line.
447 */
448#define MPT_NUM_REPLY_HANDLERS		(32)
449#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
450#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
451#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
452typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
453    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
454typedef union {
455	mpt_reply_handler_t	*reply_handler;
456} mpt_handler_t;
457
458typedef enum {
459	MPT_HANDLER_REPLY,
460	MPT_HANDLER_EVENT,
461	MPT_HANDLER_RESET,
462	MPT_HANDLER_SHUTDOWN
463} mpt_handler_type;
464
465struct mpt_handler_record
466{
467	LIST_ENTRY(mpt_handler_record)	links;
468	mpt_handler_t			handler;
469};
470
471LIST_HEAD(mpt_handler_list, mpt_handler_record);
472
473/*
474 * The handler_id is currently unused but would contain the
475 * handler ID used in the MsgContext field to allow direction
476 * of replies to the handler.  Registrations that don't require
477 * a handler id can pass in NULL for the handler_id.
478 *
479 * Deregistrations for handlers without a handler id should
480 * pass in MPT_HANDLER_ID_NONE.
481 */
482#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
483int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
484			 mpt_handler_t, uint32_t *);
485int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
486			   mpt_handler_t, uint32_t);
487
488/******************* Per-Controller Instance Data Structures ******************/
489TAILQ_HEAD(req_queue, req_entry);
490
491/* Structure for saving proper values for modifyable PCI config registers */
492struct mpt_pci_cfg {
493	uint16_t Command;
494	uint16_t LatencyTimer_LineSize;
495	uint32_t IO_BAR;
496	uint32_t Mem0_BAR[2];
497	uint32_t Mem1_BAR[2];
498	uint32_t ROM_BAR;
499	uint8_t  IntLine;
500	uint32_t PMCSR;
501};
502
503typedef enum {
504	MPT_RVF_NONE		= 0x0,
505	MPT_RVF_ACTIVE		= 0x1,
506	MPT_RVF_ANNOUNCED	= 0x2,
507	MPT_RVF_UP2DATE		= 0x4,
508	MPT_RVF_REFERENCED	= 0x8,
509	MPT_RVF_WCE_CHANGED	= 0x10
510} mpt_raid_volume_flags;
511
512struct mpt_raid_volume {
513	CONFIG_PAGE_RAID_VOL_0	       *config_page;
514	MPI_RAID_VOL_INDICATOR		sync_progress;
515	mpt_raid_volume_flags		flags;
516	u_int				quiesced_disks;
517};
518
519typedef enum {
520	MPT_RDF_NONE		= 0x00,
521	MPT_RDF_ACTIVE		= 0x01,
522	MPT_RDF_ANNOUNCED	= 0x02,
523	MPT_RDF_UP2DATE		= 0x04,
524	MPT_RDF_REFERENCED	= 0x08,
525	MPT_RDF_QUIESCING	= 0x10,
526	MPT_RDF_QUIESCED	= 0x20
527} mpt_raid_disk_flags;
528
529struct mpt_raid_disk {
530	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
531	struct mpt_raid_volume	       *volume;
532	u_int				member_number;
533	u_int				pass_thru_active;
534	mpt_raid_disk_flags		flags;
535};
536
537struct mpt_evtf_record {
538	MSG_EVENT_NOTIFY_REPLY		reply;
539	uint32_t			context;
540	LIST_ENTRY(mpt_evtf_record)	links;
541};
542
543LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
544
545struct mptsas_devinfo {
546	uint16_t	dev_handle;
547	uint16_t	parent_dev_handle;
548	uint16_t	enclosure_handle;
549	uint16_t	slot;
550	uint8_t		phy_num;
551	uint8_t		physical_port;
552	uint8_t		target_id;
553	uint8_t		bus;
554	uint64_t	sas_address;
555	uint32_t	device_info;
556};
557
558struct mptsas_phyinfo {
559	uint16_t	handle;
560	uint8_t		phy_num;
561	uint8_t		port_id;
562	uint8_t		negotiated_link_rate;
563	uint8_t		hw_link_rate;
564	uint8_t		programmed_link_rate;
565	uint8_t		sas_port_add_phy;
566	struct mptsas_devinfo identify;
567	struct mptsas_devinfo attached;
568};
569
570struct mptsas_portinfo {
571	uint16_t			num_phys;
572	struct mptsas_phyinfo		*phy_info;
573};
574
575struct mpt_softc {
576	device_t		dev;
577#if __FreeBSD_version < 500000
578	uint32_t		mpt_islocked;
579	int			mpt_splsaved;
580#else
581	struct mtx		mpt_lock;
582	int			mpt_locksetup;
583#endif
584	uint32_t		mpt_pers_mask;
585	uint32_t
586				: 8,
587		unit		: 8,
588		ready		: 1,
589		fw_uploaded	: 1,
590		msi_enable	: 1,
591		twildcard	: 1,
592		tenabled	: 1,
593		do_cfg_role	: 1,
594		raid_enabled	: 1,
595		raid_mwce_set	: 1,
596		getreqwaiter	: 1,
597		shutdwn_raid    : 1,
598		shutdwn_recovery: 1,
599		outofbeer	: 1,
600		disabled	: 1,
601		is_spi		: 1,
602		is_sas		: 1,
603		is_fc		: 1;
604
605	u_int			cfg_role;
606	u_int			role;	/* role: none, ini, target, both */
607
608	u_int			verbose;
609#ifdef	MPT_TEST_MULTIPATH
610	int			failure_id;
611#endif
612
613	/*
614	 * IOC Facts
615	 */
616	MSG_IOC_FACTS_REPLY	ioc_facts;
617
618	/*
619	 * Port Facts
620	 */
621	MSG_PORT_FACTS_REPLY *	port_facts;
622#define	mpt_ini_id	port_facts[0].PortSCSIID
623#define	mpt_max_tgtcmds	port_facts[0].MaxPostedCmdBuffers
624
625	/*
626	 * Device Configuration Information
627	 */
628	union {
629		struct mpt_spi_cfg {
630			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
631			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
632			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
633			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
634			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
635			uint16_t			_tag_enable;
636			uint16_t			_disc_enable;
637		} spi;
638#define	mpt_port_page0		cfg.spi._port_page0
639#define	mpt_port_page1		cfg.spi._port_page1
640#define	mpt_port_page2		cfg.spi._port_page2
641#define	mpt_dev_page0		cfg.spi._dev_page0
642#define	mpt_dev_page1		cfg.spi._dev_page1
643#define	mpt_tag_enable		cfg.spi._tag_enable
644#define	mpt_disc_enable		cfg.spi._disc_enable
645		struct mpi_fc_cfg {
646			CONFIG_PAGE_FC_PORT_0 _port_page0;
647			uint32_t _port_speed;
648#define	mpt_fcport_page0	cfg.fc._port_page0
649#define	mpt_fcport_speed	cfg.fc._port_speed
650		} fc;
651	} cfg;
652#if __FreeBSD_version >= 500000
653	/*
654	 * Device config information stored up for sysctl to access
655	 */
656	union {
657		struct {
658			unsigned int initiator_id;
659		} spi;
660		struct {
661			char wwnn[19];
662			char wwpn[19];
663		} fc;
664	} scinfo;
665#endif
666
667	/* Controller Info for RAID information */
668	CONFIG_PAGE_IOC_2 *	ioc_page2;
669	CONFIG_PAGE_IOC_3 *	ioc_page3;
670
671	/* Raid Data */
672	struct mpt_raid_volume* raid_volumes;
673	struct mpt_raid_disk*	raid_disks;
674	u_int			raid_max_volumes;
675	u_int			raid_max_disks;
676	u_int			raid_page0_len;
677	u_int			raid_wakeup;
678	u_int			raid_rescan;
679	u_int			raid_resync_rate;
680	u_int			raid_mwce_setting;
681	u_int			raid_queue_depth;
682	u_int			raid_nonopt_volumes;
683	struct proc	       *raid_thread;
684	struct callout		raid_timer;
685
686	/*
687	 * PCI Hardware info
688	 */
689	int			pci_msi_count;
690	struct resource *	pci_irq;	/* Interrupt map for chip */
691	void *			ih;		/* Interupt handle */
692	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
693
694	/*
695	 * DMA Mapping Stuff
696	 */
697	struct resource *	pci_reg;	/* Register map for chip */
698	int			pci_mem_rid;	/* Resource ID */
699	bus_space_tag_t		pci_st;		/* Bus tag for registers */
700	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
701	/* PIO versions of above. */
702	int			pci_pio_rid;
703	struct resource *	pci_pio_reg;
704	bus_space_tag_t		pci_pio_st;
705	bus_space_handle_t	pci_pio_sh;
706
707	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
708	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
709	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
710	uint8_t		       *reply;		/* KVA of reply memory */
711	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
712
713	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
714	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
715	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
716	uint8_t		       *request;	/* KVA of Request memory */
717	bus_addr_t		request_phys;	/* BusAddr of request memory */
718
719	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
720
721	/*
722	 * Hardware management
723	 */
724	u_int			reset_cnt;
725
726	/*
727	 * CAM && Software Management
728	 */
729	request_t	       *request_pool;
730	struct req_queue	request_free_list;
731	struct req_queue	request_pending_list;
732	struct req_queue	request_timeout_list;
733
734
735	struct cam_sim	       *sim;
736	struct cam_path	       *path;
737
738	struct cam_sim	       *phydisk_sim;
739	struct cam_path	       *phydisk_path;
740
741	struct proc	       *recovery_thread;
742	request_t	       *tmf_req;
743
744	/*
745	 * Deferred frame acks due to resource shortage.
746	 */
747	struct mpt_evtf_list	ack_frames;
748
749	/*
750	 * Target Mode Support
751	 */
752	uint32_t		scsi_tgt_handler_id;
753	request_t **		tgt_cmd_ptrs;
754	request_t **		els_cmd_ptrs;	/* FC only */
755
756	/*
757	 * *snork*- this is chosen to be here *just in case* somebody
758	 * forgets to point to it exactly and we index off of trt with
759	 * CAM_LUN_WILDCARD.
760	 */
761	tgt_resource_t		trt_wildcard;		/* wildcard luns */
762	tgt_resource_t		trt[MPT_MAX_LUNS];
763	uint16_t		tgt_cmds_allocated;
764	uint16_t		els_cmds_allocated;	/* FC only */
765
766	uint16_t		timeouts;	/* timeout count */
767	uint16_t		success;	/* successes afer timeout */
768	uint16_t		sequence;	/* Sequence Number */
769	uint16_t		pad3;
770
771
772	/* Paired port in some dual adapters configurations */
773	struct mpt_softc *	mpt2;
774
775	/* FW Image management */
776	uint32_t		fw_image_size;
777	uint8_t		       *fw_image;
778	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
779	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
780	bus_addr_t		fw_phys;	/* BusAddr of firmware image */
781
782	/* SAS Topology */
783	struct mptsas_portinfo	*sas_portinfo;
784
785	/* Shutdown Event Handler. */
786	eventhandler_tag         eh;
787
788	TAILQ_ENTRY(mpt_softc)	links;
789};
790
791static __inline void mpt_assign_serno(struct mpt_softc *, request_t *);
792
793static __inline void
794mpt_assign_serno(struct mpt_softc *mpt, request_t *req)
795{
796	if ((req->serno = mpt->sequence++) == 0) {
797		req->serno = mpt->sequence++;
798	}
799}
800
801/***************************** Locking Primitives *****************************/
802#if __FreeBSD_version < 500000
803#define	MPT_IFLAGS		INTR_TYPE_CAM
804#define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
805#define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
806#define	MPT_OWNED(mpt)		mpt->mpt_islocked
807#define	MPT_LOCK_ASSERT(mpt)
808#define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
809#define	CAMLOCK_2_MPTLOCK	MPT_LOCK
810#define	MPT_LOCK_SETUP(mpt)
811#define	MPT_LOCK_DESTROY(mpt)
812
813static __inline void mpt_lockspl(struct mpt_softc *mpt);
814static __inline void mpt_unlockspl(struct mpt_softc *mpt);
815
816static __inline void
817mpt_lockspl(struct mpt_softc *mpt)
818{
819       int s;
820
821       s = splcam();
822       if (mpt->mpt_islocked++ == 0) {
823               mpt->mpt_splsaved = s;
824       } else {
825               splx(s);
826	       panic("Recursed lock with mask: 0x%x\n", s);
827       }
828}
829
830static __inline void
831mpt_unlockspl(struct mpt_softc *mpt)
832{
833       if (mpt->mpt_islocked) {
834               if (--mpt->mpt_islocked == 0) {
835                       splx(mpt->mpt_splsaved);
836               }
837       } else
838	       panic("Negative lock count\n");
839}
840
841static __inline int
842mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
843	   const char *wmesg, int timo)
844{
845	int saved_cnt;
846	int saved_spl;
847	int error;
848
849	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
850	saved_cnt = mpt->mpt_islocked;
851	saved_spl = mpt->mpt_splsaved;
852	mpt->mpt_islocked = 0;
853	error = tsleep(ident, priority, wmesg, timo);
854	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
855	mpt->mpt_islocked = saved_cnt;
856	mpt->mpt_splsaved = saved_spl;
857	return (error);
858}
859
860#define mpt_req_timeout(req, ticks, func, arg) \
861	callout_reset(&(req)->callout, (ticks), (func), (arg));
862#define mpt_req_untimeout(req, func, arg) \
863	callout_stop(&(req)->callout)
864
865#else
866#if 1
867#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
868#define	MPT_LOCK_SETUP(mpt)						\
869		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
870		mpt->mpt_locksetup = 1
871#define	MPT_LOCK_DESTROY(mpt)						\
872	if (mpt->mpt_locksetup) {					\
873		mtx_destroy(&mpt->mpt_lock);				\
874		mpt->mpt_locksetup = 0;					\
875	}
876
877#define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
878#define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
879#define	MPT_OWNED(mpt)		mtx_owned(&(mpt)->mpt_lock)
880#define	MPT_LOCK_ASSERT(mpt)	mtx_assert(&(mpt)->mpt_lock, MA_OWNED)
881#define	MPTLOCK_2_CAMLOCK(mpt)
882#define	CAMLOCK_2_MPTLOCK(mpt)
883#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
884	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
885#define mpt_req_timeout(req, ticks, func, arg) \
886	callout_reset(&(req)->callout, (ticks), (func), (arg));
887#define mpt_req_untimeout(req, func, arg) \
888	callout_stop(&(req)->callout)
889
890#else
891
892#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
893#define	MPT_LOCK_SETUP(mpt)	do { } while (0)
894#define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
895#define	MPT_LOCK_ASSERT(mpt)	mtx_assert(&Giant, MA_OWNED)
896#define	MPT_LOCK(mpt)		mtx_lock(&Giant)
897#define	MPT_UNLOCK(mpt)		mtx_unlock(&Giant)
898#define	MPTLOCK_2_CAMLOCK(mpt)
899#define	CAMLOCK_2_MPTLOCK(mpt)
900
901static __inline int
902mpt_sleep(struct mpt_softc *, void *, int, const char *, int);
903
904#define mpt_ccb_timeout(ccb, ticks, func, arg) \
905	do {	\
906		(ccb)->ccb_h.timeout_ch = timeout((func), (arg), (ticks)); \
907	} while (0)
908#define mpt_ccb_untimeout(ccb, func, arg) \
909	untimeout((func), (arg), (ccb)->ccb_h.timeout_ch)
910#define mpt_ccb_timeout_init(ccb) \
911	callout_handle_init(&(ccb)->ccb_h.timeout_ch)
912
913static __inline int
914mpt_sleep(struct mpt_softc *mpt, void *i, int p, const char *w, int t)
915{
916	int r;
917	r = tsleep(i, p, w, t);
918	return (r);
919}
920#endif
921#endif
922
923/******************************* Register Access ******************************/
924static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
925static __inline uint32_t mpt_read(struct mpt_softc *, int);
926static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
927static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
928
929static __inline void
930mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
931{
932	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
933}
934
935static __inline uint32_t
936mpt_read(struct mpt_softc *mpt, int offset)
937{
938	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
939}
940
941/*
942 * Some operations (e.g. diagnostic register writes while the ARM proccessor
943 * is disabled), must be performed using "PCI pio" operations.  On non-PCI
944 * busses, these operations likely map to normal register accesses.
945 */
946static __inline void
947mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
948{
949	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
950}
951
952static __inline uint32_t
953mpt_pio_read(struct mpt_softc *mpt, int offset)
954{
955	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
956}
957/*********************** Reply Frame/Request Management ***********************/
958/* Max MPT Reply we are willing to accept (must be power of 2) */
959#define MPT_REPLY_SIZE   	256
960
961/*
962 * Must be less than 16384 in order for target mode to work
963 */
964#define MPT_MAX_REQUESTS(mpt)	512
965#define MPT_REQUEST_AREA	512
966#define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
967#define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
968
969#define MPT_CONTEXT_CB_SHIFT	(16)
970#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
971#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
972#define MPT_CONTEXT_TO_CBI(x)	\
973    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
974#define MPT_CONTEXT_REQI_MASK	0xFFFF
975#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
976
977/*
978 * Convert a 32bit physical address returned from IOC to an
979 * offset into our reply frame memory or the kvm address needed
980 * to access the data.  The returned address is only the low
981 * 32 bits, so mask our base physical address accordingly.
982 */
983#define MPT_REPLY_BADDR(x)		\
984	(x << 1)
985#define MPT_REPLY_OTOV(m, i) 		\
986	((void *)(&m->reply[i]))
987
988#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
989do {							\
990	if (mpt->verbose > MPT_PRT_DEBUG)		\
991		mpt_dump_reply_frame(mpt, reply_frame);	\
992} while(0)
993
994static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
995static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
996
997/*
998 * Give the reply buffer back to the IOC after we have
999 * finished processing it.
1000 */
1001static __inline void
1002mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
1003{
1004     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
1005}
1006
1007/* Get a reply from the IOC */
1008static __inline uint32_t
1009mpt_pop_reply_queue(struct mpt_softc *mpt)
1010{
1011     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
1012}
1013
1014void
1015mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
1016
1017/************************** Scatter Gather Managment **************************/
1018/* MPT_RQSL- size of request frame, in bytes */
1019#define	MPT_RQSL(mpt)		(mpt->ioc_facts.RequestFrameSize << 2)
1020
1021/* MPT_NSGL- how many SG entries can fit in a request frame size */
1022#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
1023
1024/* MPT_NRFM- how many request frames can fit in each request alloc we make */
1025#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
1026
1027/*
1028 * MPT_NSGL_FIRST- # of SG elements that can fit after
1029 * an I/O request but still within the request frame.
1030 * Do this safely based upon SGE_IO_UNION.
1031 *
1032 * Note that the first element is *within* the SCSI request.
1033 */
1034#define	MPT_NSGL_FIRST(mpt)	\
1035    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
1036    sizeof (SGE_IO_UNION))
1037
1038/***************************** IOC Initialization *****************************/
1039int mpt_reset(struct mpt_softc *, int /*reinit*/);
1040
1041/****************************** Debugging ************************************/
1042typedef struct mpt_decode_entry {
1043	char    *name;
1044	u_int	 value;
1045	u_int	 mask;
1046} mpt_decode_entry_t;
1047
1048int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
1049		     const char *name, u_int value, u_int *cur_column,
1050		     u_int wrap_point);
1051
1052void mpt_dump_data(struct mpt_softc *, const char *, void *, int);
1053void mpt_dump_request(struct mpt_softc *, request_t *);
1054
1055enum {
1056	MPT_PRT_ALWAYS,
1057	MPT_PRT_FATAL,
1058	MPT_PRT_ERROR,
1059	MPT_PRT_WARN,
1060	MPT_PRT_INFO,
1061	MPT_PRT_NEGOTIATION,
1062	MPT_PRT_DEBUG,
1063	MPT_PRT_DEBUG1,
1064	MPT_PRT_DEBUG2,
1065	MPT_PRT_DEBUG3,
1066	MPT_PRT_TRACE,
1067	MPT_PRT_NONE=100
1068};
1069
1070#if __FreeBSD_version > 500000
1071#define mpt_lprt(mpt, level, ...)		\
1072do {						\
1073	if (level <= (mpt)->verbose)		\
1074		mpt_prt(mpt, __VA_ARGS__);	\
1075} while (0)
1076
1077#define mpt_lprtc(mpt, level, ...)		 \
1078do {						 \
1079	if (level <= (mpt)->debug_level)	 \
1080		mpt_prtc(mpt, __VA_ARGS__);	 \
1081} while (0)
1082#else
1083void mpt_lprt(struct mpt_softc *, int, const char *, ...)
1084	__printflike(3, 4);
1085void mpt_lprtc(struct mpt_softc *, int, const char *, ...)
1086	__printflike(3, 4);
1087#endif
1088void mpt_prt(struct mpt_softc *, const char *, ...)
1089	__printflike(2, 3);
1090void mpt_prtc(struct mpt_softc *, const char *, ...)
1091	__printflike(2, 3);
1092
1093/**************************** Target Mode Related ***************************/
1094static __inline int mpt_cdblen(uint8_t, int);
1095static __inline int
1096mpt_cdblen(uint8_t cdb0, int maxlen)
1097{
1098	int group = cdb0 >> 5;
1099	switch (group) {
1100	case 0:
1101		return (6);
1102	case 1:
1103		return (10);
1104	case 4:
1105	case 5:
1106		return (12);
1107	default:
1108		return (16);
1109	}
1110}
1111#ifdef	INVARIANTS
1112static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
1113static __inline request_t *
1114mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
1115{
1116	uint16_t rtg = (tag >> 18);
1117	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
1118	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
1119	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
1120	return (mpt->tgt_cmd_ptrs[rtg]);
1121}
1122
1123
1124static __inline int
1125mpt_req_on_free_list(struct mpt_softc *, request_t *);
1126static __inline int
1127mpt_req_on_pending_list(struct mpt_softc *, request_t *);
1128
1129static __inline void
1130mpt_req_spcl(struct mpt_softc *, request_t *, const char *, int);
1131static __inline void
1132mpt_req_not_spcl(struct mpt_softc *, request_t *, const char *, int);
1133
1134
1135/*
1136 * Is request on freelist?
1137 */
1138static __inline int
1139mpt_req_on_free_list(struct mpt_softc *mpt, request_t *req)
1140{
1141	request_t *lrq;
1142
1143	TAILQ_FOREACH(lrq, &mpt->request_free_list, links) {
1144		if (lrq == req) {
1145			return (1);
1146		}
1147	}
1148	return (0);
1149}
1150
1151/*
1152 * Is request on pending list?
1153 */
1154static __inline int
1155mpt_req_on_pending_list(struct mpt_softc *mpt, request_t *req)
1156{
1157	request_t *lrq;
1158
1159	TAILQ_FOREACH(lrq, &mpt->request_pending_list, links) {
1160		if (lrq == req) {
1161			return (1);
1162		}
1163	}
1164	return (0);
1165}
1166
1167/*
1168 * Make sure that req *is* part of one of the special lists
1169 */
1170static __inline void
1171mpt_req_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1172{
1173	int i;
1174	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1175		if (req == mpt->els_cmd_ptrs[i]) {
1176			return;
1177		}
1178	}
1179	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1180		if (req == mpt->tgt_cmd_ptrs[i]) {
1181			return;
1182		}
1183	}
1184	panic("%s(%d): req %p:%u function %x not in els or tgt ptrs\n",
1185	    s, line, req, req->serno,
1186	    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function);
1187}
1188
1189/*
1190 * Make sure that req is *not* part of one of the special lists.
1191 */
1192static __inline void
1193mpt_req_not_spcl(struct mpt_softc *mpt, request_t *req, const char *s, int line)
1194{
1195	int i;
1196	for (i = 0; i < mpt->els_cmds_allocated; i++) {
1197		KASSERT(req != mpt->els_cmd_ptrs[i],
1198		    ("%s(%d): req %p:%u func %x in els ptrs at ioindex %d\n",
1199		    s, line, req, req->serno,
1200		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1201	}
1202	for (i = 0; i < mpt->tgt_cmds_allocated; i++) {
1203		KASSERT(req != mpt->tgt_cmd_ptrs[i],
1204		    ("%s(%d): req %p:%u func %x in tgt ptrs at ioindex %d\n",
1205		    s, line, req, req->serno,
1206		    ((PTR_MSG_REQUEST_HEADER)req->req_vbuf)->Function, i));
1207	}
1208}
1209#endif
1210
1211/*
1212 * Task Management Types, purely for internal consumption
1213 */
1214typedef enum {
1215	MPT_ABORT_TASK_SET=1234,
1216	MPT_CLEAR_TASK_SET,
1217	MPT_TARGET_RESET,
1218	MPT_CLEAR_ACA,
1219	MPT_TERMINATE_TASK,
1220	MPT_NIL_TMT_VALUE=5678
1221} mpt_task_mgmt_t;
1222
1223/**************************** Unclassified Routines ***************************/
1224void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
1225int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
1226					 size_t reply_len, void *reply);
1227int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
1228			     mpt_req_state_t state, mpt_req_state_t mask,
1229			     int sleep_ok, int time_ms);
1230void		mpt_enable_ints(struct mpt_softc *mpt);
1231void		mpt_disable_ints(struct mpt_softc *mpt);
1232int		mpt_attach(struct mpt_softc *mpt);
1233int		mpt_shutdown(struct mpt_softc *mpt);
1234int		mpt_detach(struct mpt_softc *mpt);
1235int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
1236				       size_t len, void *cmd);
1237request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
1238void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
1239void		mpt_intr(void *arg);
1240void		mpt_check_doorbell(struct mpt_softc *mpt);
1241void		mpt_dump_reply_frame(struct mpt_softc *mpt,
1242				     MSG_DEFAULT_REPLY *reply_frame);
1243
1244void		mpt_set_config_regs(struct mpt_softc *);
1245int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
1246				  cfgparms_t *params,
1247				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1248				  int /*sleep_ok*/, int /*timeout_ms*/);
1249int		mpt_read_extcfg_header(struct mpt_softc *mpt, int PageVersion,
1250				       int PageNumber, uint32_t PageAddress,
1251				       int ExtPageType,
1252				       CONFIG_EXTENDED_PAGE_HEADER *rslt,
1253				       int sleep_ok, int timeout_ms);
1254int		mpt_read_extcfg_page(struct mpt_softc *mpt, int Action,
1255				     uint32_t PageAddress,
1256				     CONFIG_EXTENDED_PAGE_HEADER *hdr,
1257				     void *buf, size_t len, int sleep_ok,
1258				     int timeout_ms);
1259int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1260				    int /*PageNumber*/,
1261				    uint32_t /*PageAddress*/,
1262				    CONFIG_PAGE_HEADER *,
1263				    int /*sleep_ok*/, int /*timeout_ms*/);
1264int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1265				  uint32_t /*PageAddress*/,
1266				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1267				  int /*sleep_ok*/, int /*timeout_ms*/);
1268int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1269				   uint32_t /*PageAddress*/,
1270				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1271				   int /*sleep_ok*/, int /*timeout_ms*/);
1272static __inline int
1273mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1274		      CONFIG_PAGE_HEADER *hdr, size_t len,
1275		      int sleep_ok, int timeout_ms)
1276{
1277	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1278				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1279}
1280
1281static __inline int
1282mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1283		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1284		       int timeout_ms)
1285{
1286	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1287				   PageAddress, hdr, len, sleep_ok,
1288				   timeout_ms));
1289}
1290/* mpt_debug.c functions */
1291void mpt_print_reply(void *vmsg);
1292void mpt_print_db(uint32_t mb);
1293void mpt_print_config_reply(void *vmsg);
1294char *mpt_ioc_diag(uint32_t diag);
1295void mpt_req_state(mpt_req_state_t state);
1296void mpt_print_config_request(void *vmsg);
1297void mpt_print_request(void *vmsg);
1298void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1299void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1300#endif /* _MPT_H_ */
1301