mpt.h revision 157117
1/* $FreeBSD: head/sys/dev/mpt/mpt.h 157117 2006-03-25 07:08:27Z mjacob $ */
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 * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
64 * Copyright (c) 2004, 2005 Justin T. Gibbs
65 * Copyright (c) 2005, WHEEL Sp. z o.o.
66 * All rights reserved.
67 *
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions are
70 * met:
71 * 1. Redistributions of source code must retain the above copyright
72 *    notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
74 *    substantially similar to the "NO WARRANTY" disclaimer below
75 *    ("Disclaimer") and any redistribution must be conditioned upon including
76 *    a substantially similar Disclaimer requirement for further binary
77 *    redistribution.
78 * 3. Neither the names of the above listed copyright holders nor the names
79 *    of any contributors may be used to endorse or promote products derived
80 *    from this software without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
83 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
86 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
87 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
88 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
89 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
90 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
91 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
92 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93 */
94
95#ifndef _MPT_H_
96#define _MPT_H_
97
98/********************************* OS Includes ********************************/
99#include <sys/types.h>
100#include <sys/param.h>
101#include <sys/systm.h>
102#include <sys/endian.h>
103#include <sys/eventhandler.h>
104#if __FreeBSD_version < 500000
105#include <sys/kernel.h>
106#include <sys/queue.h>
107#include <sys/malloc.h>
108#else
109#include <sys/lock.h>
110#include <sys/kernel.h>
111#include <sys/queue.h>
112#include <sys/malloc.h>
113#include <sys/mutex.h>
114#include <sys/condvar.h>
115#endif
116#include <sys/proc.h>
117#include <sys/bus.h>
118#include <sys/module.h>
119
120#include <machine/clock.h>
121#include <machine/cpu.h>
122#include <machine/resource.h>
123
124#if __FreeBSD_version < 500000
125#include <machine/bus.h>
126#endif
127
128#include <sys/rman.h>
129
130#if __FreeBSD_version < 500000
131#include <pci/pcireg.h>
132#include <pci/pcivar.h>
133#else
134#include <dev/pci/pcireg.h>
135#include <dev/pci/pcivar.h>
136#endif
137
138#include <machine/bus.h>
139#include "opt_ddb.h"
140
141/**************************** Register Definitions ****************************/
142#include <dev/mpt/mpt_reg.h>
143
144/******************************* MPI Definitions ******************************/
145#include <dev/mpt/mpilib/mpi_type.h>
146#include <dev/mpt/mpilib/mpi.h>
147#include <dev/mpt/mpilib/mpi_cnfg.h>
148#include <dev/mpt/mpilib/mpi_ioc.h>
149#include <dev/mpt/mpilib/mpi_raid.h>
150
151/* XXX For mpt_debug.c */
152#include <dev/mpt/mpilib/mpi_init.h>
153
154/****************************** Misc Definitions ******************************/
155#define MPT_OK (0)
156#define MPT_FAIL (0x10000)
157
158#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
159
160#define	MPT_ROLE_NONE		0
161#define	MPT_ROLE_INITIATOR	1
162#define	MPT_ROLE_TARGET		2
163#define	MPT_ROLE_BOTH		3
164#define	MPT_ROLE_DEFAULT	MPT_ROLE_INITIATOR
165
166/**************************** Forward Declarations ****************************/
167struct mpt_softc;
168struct mpt_personality;
169typedef struct req_entry request_t;
170
171/************************* Personality Module Support *************************/
172typedef int mpt_load_handler_t(struct mpt_personality *);
173typedef int mpt_probe_handler_t(struct mpt_softc *);
174typedef int mpt_attach_handler_t(struct mpt_softc *);
175typedef int mpt_enable_handler_t(struct mpt_softc *);
176typedef int mpt_event_handler_t(struct mpt_softc *, request_t *,
177				MSG_EVENT_NOTIFY_REPLY *);
178typedef void mpt_reset_handler_t(struct mpt_softc *, int /*type*/);
179/* XXX Add return value and use for veto? */
180typedef void mpt_shutdown_handler_t(struct mpt_softc *);
181typedef void mpt_detach_handler_t(struct mpt_softc *);
182typedef int mpt_unload_handler_t(struct mpt_personality *);
183
184struct mpt_personality
185{
186	const char		*name;
187	uint32_t		 id;		/* Assigned identifier. */
188	u_int			 use_count;	/* Instances using personality*/
189	mpt_load_handler_t	*load;		/* configure personailty */
190#define MPT_PERS_FIRST_HANDLER(pers) (&(pers)->load)
191	mpt_probe_handler_t	*probe;		/* configure personailty */
192	mpt_attach_handler_t	*attach;	/* initialize device instance */
193	mpt_enable_handler_t	*enable;	/* enable device */
194	mpt_event_handler_t	*event;		/* Handle MPI event. */
195	mpt_reset_handler_t	*reset;		/* Re-init after reset. */
196	mpt_shutdown_handler_t	*shutdown;	/* Shutdown instance. */
197	mpt_detach_handler_t	*detach;	/* release device instance */
198	mpt_unload_handler_t	*unload;	/* Shutdown personality */
199#define MPT_PERS_LAST_HANDLER(pers) (&(pers)->unload)
200};
201
202int mpt_modevent(module_t, int, void *);
203
204/* Maximum supported number of personalities. */
205#define MPT_MAX_PERSONALITIES	(15)
206
207#define MPT_PERSONALITY_DEPEND(name, dep, vmin, vpref, vmax) \
208	MODULE_DEPEND(name, dep, vmin, vpref, vmax)
209
210#define DECLARE_MPT_PERSONALITY(name, order)				  \
211	static moduledata_t name##_mod = {				  \
212		#name, mpt_modevent, &name##_personality		  \
213	};								  \
214	DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, order);	  \
215	MODULE_VERSION(name, 1);					  \
216	MPT_PERSONALITY_DEPEND(name, mpt_core, 1, 1, 1)
217
218/******************************* Bus DMA Support ******************************/
219/* XXX Need to update bus_dmamap_sync to take a range argument. */
220#define bus_dmamap_sync_range(dma_tag, dmamap, offset, len, op)	\
221	bus_dmamap_sync(dma_tag, dmamap, op)
222
223#if __FreeBSD_version >= 501102
224#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
225			   lowaddr, highaddr, filter, filterarg,	\
226			   maxsize, nsegments, maxsegsz, flags,		\
227			   dma_tagp)					\
228	bus_dma_tag_create(parent_tag, alignment, boundary,		\
229			   lowaddr, highaddr, filter, filterarg,	\
230			   maxsize, nsegments, maxsegsz, flags,		\
231			   busdma_lock_mutex, &Giant,			\
232			   dma_tagp)
233#else
234#define mpt_dma_tag_create(mpt, parent_tag, alignment, boundary,	\
235			   lowaddr, highaddr, filter, filterarg,	\
236			   maxsize, nsegments, maxsegsz, flags,		\
237			   dma_tagp)					\
238	bus_dma_tag_create(parent_tag, alignment, boundary,		\
239			   lowaddr, highaddr, filter, filterarg,	\
240			   maxsize, nsegments, maxsegsz, flags,		\
241			   dma_tagp)
242#endif
243
244struct mpt_map_info {
245	struct mpt_softc *mpt;
246	int		  error;
247	uint32_t	  phys;
248};
249
250void mpt_map_rquest(void *, bus_dma_segment_t *, int, int);
251
252/**************************** Kernel Thread Support ***************************/
253#if __FreeBSD_version > 500005
254#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
255	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
256#else
257#define mpt_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
258	kthread_create(func, farg, proc_ptr, fmtstr, arg)
259#endif
260
261/****************************** Timer Facilities ******************************/
262#if __FreeBSD_version > 500000
263#define mpt_callout_init(c)	callout_init(c, /*mpsafe*/0);
264#else
265#define mpt_callout_init(c)	callout_init(c);
266#endif
267
268/********************************** Endianess *********************************/
269static __inline uint64_t
270u64toh(U64 s)
271{
272	uint64_t result;
273
274	result = le32toh(s.Low);
275	result |= ((uint64_t)le32toh(s.High)) << 32;
276	return (result);
277}
278
279/**************************** MPI Transaction State ***************************/
280typedef enum {
281	REQ_STATE_FREE		= 0x00,
282	REQ_STATE_ALLOCATED	= 0x01,
283	REQ_STATE_QUEUED	= 0x02,
284	REQ_STATE_DONE		= 0x04,
285	REQ_STATE_TIMEDOUT	= 0x08,
286	REQ_STATE_NEED_WAKEUP	= 0x10,
287	REQ_STATE_MASK		= 0xFF
288} mpt_req_state_t;
289
290struct req_entry {
291	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
292	mpt_req_state_t	state;		/* Request State Information */
293	uint16_t	index;		/* Index of this entry */
294	uint16_t	IOCStatus;	/* Completion status */
295	uint16_t	serno;		/* serial number */
296	union ccb      *ccb;		/* CAM request */
297	void	       *req_vbuf;	/* Virtual Address of Entry */
298	void	       *sense_vbuf;	/* Virtual Address of sense data */
299	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
300	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
301	bus_dmamap_t	dmap;		/* DMA map for data buffer */
302	struct req_entry *chain;	/* for SGE overallocations */
303};
304
305/**************************** MPI Target State Info ***************************/
306
307typedef struct {
308	uint32_t reply_desc;	/* current reply descriptor */
309	uint32_t resid;		/* current data residual */
310	uint32_t bytes_xfered;	/* current relative offset */
311	union ccb *ccb;		/* pointer to currently active ccb */
312	request_t *req;		/* pointer to currently active assist request */
313	int	flags;
314#define	BOGUS_JO	0x01
315	int	nxfers;
316	enum {
317		TGT_STATE_NIL,
318		TGT_STATE_LOADED,
319		TGT_STATE_IN_CAM,
320                TGT_STATE_SETTING_UP_FOR_DATA,
321                TGT_STATE_MOVING_DATA,
322                TGT_STATE_MOVING_DATA_AND_STATUS,
323                TGT_STATE_SENDING_STATUS
324	} state;
325} mpt_tgt_state_t;
326
327/*
328 * When we get an incoming command it has its own tag which is called the
329 * IoIndex. This is the value we gave that particular command buffer when
330 * we originally assigned it. It's just a number, really. The FC card uses
331 * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
332 * contains pointers the request_t structures related to that IoIndex.
333 *
334 * What *we* do is construct a tag out of the index for the target command
335 * which owns the incoming ATIO plus a rolling sequence number.
336 */
337#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
338	((ioindex << 16) | (mpt->sequence++))
339
340#ifdef	INVARIANTS
341#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
342#else
343#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 16]
344#endif
345
346#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
347    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
348
349STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
350#define	MPT_MAX_LUNS	256
351typedef struct {
352	struct mpt_hdr_stailq	atios;
353	struct mpt_hdr_stailq	inots;
354	int enabled;
355} tgt_resource_t;
356#define	MPT_MAX_ELS	8
357
358/**************************** Handler Registration ****************************/
359/*
360 * Global table of registered reply handlers.  The
361 * handler is indicated by byte 3 of the request
362 * index submitted to the IOC.  This allows the
363 * driver core to perform generic processing without
364 * any knowledge of per-personality behavior.
365 *
366 * MPT_NUM_REPLY_HANDLERS must be a power of 2
367 * to allow the easy generation of a mask.
368 *
369 * The handler offsets used by the core are hard coded
370 * allowing faster code generation when assigning a handler
371 * to a request.  All "personalities" must use the
372 * the handler registration mechanism.
373 *
374 * The IOC handlers that are rarely executed are placed
375 * at the tail of the table to make it more likely that
376 * all commonly executed handlers fit in a single cache
377 * line.
378 */
379#define MPT_NUM_REPLY_HANDLERS		(32)
380#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
381#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
382#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
383typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
384    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
385typedef union {
386	mpt_reply_handler_t	*reply_handler;
387} mpt_handler_t;
388
389typedef enum {
390	MPT_HANDLER_REPLY,
391	MPT_HANDLER_EVENT,
392	MPT_HANDLER_RESET,
393	MPT_HANDLER_SHUTDOWN
394} mpt_handler_type;
395
396struct mpt_handler_record
397{
398	LIST_ENTRY(mpt_handler_record)	links;
399	mpt_handler_t			handler;
400};
401
402LIST_HEAD(mpt_handler_list, mpt_handler_record);
403
404/*
405 * The handler_id is currently unused but would contain the
406 * handler ID used in the MsgContext field to allow direction
407 * of replies to the handler.  Registrations that don't require
408 * a handler id can pass in NULL for the handler_id.
409 *
410 * Deregistrations for handlers without a handler id should
411 * pass in MPT_HANDLER_ID_NONE.
412 */
413#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
414int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
415			 mpt_handler_t, uint32_t *);
416int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
417			   mpt_handler_t, uint32_t);
418
419/******************* Per-Controller Instance Data Structures ******************/
420TAILQ_HEAD(req_queue, req_entry);
421
422/* Structure for saving proper values for modifyable PCI config registers */
423struct mpt_pci_cfg {
424	uint16_t Command;
425	uint16_t LatencyTimer_LineSize;
426	uint32_t IO_BAR;
427	uint32_t Mem0_BAR[2];
428	uint32_t Mem1_BAR[2];
429	uint32_t ROM_BAR;
430	uint8_t  IntLine;
431	uint32_t PMCSR;
432};
433
434typedef enum {
435	MPT_RVF_NONE		= 0x0,
436	MPT_RVF_ACTIVE		= 0x1,
437	MPT_RVF_ANNOUNCED	= 0x2,
438	MPT_RVF_UP2DATE		= 0x4,
439	MPT_RVF_REFERENCED	= 0x8,
440	MPT_RVF_WCE_CHANGED	= 0x10
441} mpt_raid_volume_flags;
442
443struct mpt_raid_volume {
444	CONFIG_PAGE_RAID_VOL_0	       *config_page;
445	MPI_RAID_VOL_INDICATOR		sync_progress;
446	mpt_raid_volume_flags		flags;
447	u_int				quieced_disks;
448};
449
450typedef enum {
451	MPT_RDF_NONE		= 0x00,
452	MPT_RDF_ACTIVE		= 0x01,
453	MPT_RDF_ANNOUNCED	= 0x02,
454	MPT_RDF_UP2DATE		= 0x04,
455	MPT_RDF_REFERENCED	= 0x08,
456	MPT_RDF_QUIESCING	= 0x10,
457	MPT_RDF_QUIESCED	= 0x20
458} mpt_raid_disk_flags;
459
460struct mpt_raid_disk {
461	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
462	struct mpt_raid_volume	       *volume;
463	u_int				member_number;
464	u_int				pass_thru_active;
465	mpt_raid_disk_flags		flags;
466};
467
468struct mpt_evtf_record {
469	MSG_EVENT_NOTIFY_REPLY		reply;
470	uint32_t			context;
471	LIST_ENTRY(mpt_evtf_record)	links;
472};
473
474LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
475
476struct mpt_softc {
477	device_t		dev;
478#if __FreeBSD_version < 500000
479	int			mpt_splsaved;
480	uint32_t		mpt_islocked;
481#else
482	struct mtx		mpt_lock;
483#endif
484	uint32_t		mpt_pers_mask;
485	uint32_t		: 8,
486		unit		: 8,
487				: 1,
488		twildcard	: 1,
489		tenabled	: 1,
490		cap 		: 2,	/* none, ini, target, both */
491		role		: 2,	/* none, ini, target, both */
492		raid_mwce_set	: 1,
493		getreqwaiter	: 1,
494		shutdwn_raid    : 1,
495		shutdwn_recovery: 1,
496		outofbeer	: 1,
497		mpt_locksetup	: 1,
498		disabled	: 1,
499		is_sas		: 1,
500		is_fc		: 1;
501
502	u_int			verbose;
503
504	/*
505	 * IOC Facts
506	 */
507	uint16_t	mpt_global_credits;
508	uint16_t	request_frame_size;
509	uint8_t		mpt_max_devices;
510	uint8_t		mpt_max_buses;
511	uint8_t		ioc_facts_flags;
512
513	/*
514	 * Port Facts
515	 * XXX - Add multi-port support!.
516	 */
517	uint16_t	mpt_ini_id;
518	uint16_t	mpt_port_type;
519	uint16_t	mpt_proto_flags;
520	uint16_t	mpt_max_tgtcmds;
521
522	/*
523	 * Device Configuration Information
524	 */
525	union {
526		struct mpt_spi_cfg {
527			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
528			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
529			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
530			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
531			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
532			uint16_t			_tag_enable;
533			uint16_t			_disc_enable;
534			uint16_t			_update_params0;
535			uint16_t			_update_params1;
536		} spi;
537#define	mpt_port_page0		cfg.spi._port_page0
538#define	mpt_port_page1		cfg.spi._port_page1
539#define	mpt_port_page2		cfg.spi._port_page2
540#define	mpt_dev_page0		cfg.spi._dev_page0
541#define	mpt_dev_page1		cfg.spi._dev_page1
542#define	mpt_tag_enable		cfg.spi._tag_enable
543#define	mpt_disc_enable		cfg.spi._disc_enable
544#define	mpt_update_params0	cfg.spi._update_params0
545#define	mpt_update_params1	cfg.spi._update_params1
546		struct mpi_fc_cfg {
547			CONFIG_PAGE_FC_PORT_0 _port_page0;
548#define	mpt_fcport_page0	cfg.fc._port_page0
549		} fc;
550	} cfg;
551
552	/* Controller Info */
553	CONFIG_PAGE_IOC_2 *	ioc_page2;
554	CONFIG_PAGE_IOC_3 *	ioc_page3;
555
556	/* Raid Data */
557	struct mpt_raid_volume* raid_volumes;
558	struct mpt_raid_disk*	raid_disks;
559	u_int			raid_max_volumes;
560	u_int			raid_max_disks;
561	u_int			raid_page0_len;
562	u_int			raid_wakeup;
563	u_int			raid_rescan;
564	u_int			raid_resync_rate;
565	u_int			raid_mwce_setting;
566	u_int			raid_queue_depth;
567	u_int			raid_nonopt_volumes;
568	struct proc	       *raid_thread;
569	struct callout		raid_timer;
570
571	/*
572	 * PCI Hardware info
573	 */
574	struct resource *	pci_irq;	/* Interrupt map for chip */
575	void *			ih;		/* Interupt handle */
576	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
577
578	/*
579	 * DMA Mapping Stuff
580	 */
581	struct resource *	pci_reg;	/* Register map for chip */
582	int			pci_mem_rid;	/* Resource ID */
583	bus_space_tag_t		pci_st;		/* Bus tag for registers */
584	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
585	/* PIO versions of above. */
586	int			pci_pio_rid;
587	struct resource *	pci_pio_reg;
588	bus_space_tag_t		pci_pio_st;
589	bus_space_handle_t	pci_pio_sh;
590
591	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
592	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
593	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
594	uint8_t		       *reply;		/* KVA of reply memory */
595	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
596
597	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
598	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
599	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
600	uint8_t		       *request;	/* KVA of Request memory */
601	bus_addr_t		request_phys;	/* BusAddr of request memory */
602
603	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
604
605	/*
606	 * Hardware management
607	 */
608	u_int			reset_cnt;
609
610	/*
611	 * CAM && Software Management
612	 */
613	request_t	       *request_pool;
614	struct req_queue	request_free_list;
615	struct req_queue	request_pending_list;
616	struct req_queue	request_timeout_list;
617
618	/*
619	 * Deferred frame acks due to resource shortage.
620	 */
621	struct mpt_evtf_list	ack_frames;
622
623
624	struct cam_sim	       *sim;
625	struct cam_path	       *path;
626
627	struct cam_sim	       *phydisk_sim;
628	struct cam_path	       *phydisk_path;
629
630	struct proc	       *recovery_thread;
631	request_t	       *tmf_req;
632
633	/*
634	 * Target Mode Support
635	 */
636	uint32_t		scsi_tgt_handler_id;
637	request_t **		tgt_cmd_ptrs;
638
639	/*
640	 * *snork*- this is chosen to be here *just in case* somebody
641	 * forgets to point to it exactly and we index off of trt with
642	 * CAM_LUN_WILDCARD.
643	 */
644	tgt_resource_t		trt_wildcard;	/* wildcard luns */
645	tgt_resource_t		trt[MPT_MAX_LUNS];
646	uint16_t		tgt_cmds_allocated;
647
648	/*
649	 * Stuff..
650	 */
651	uint16_t		sequence;	/* Sequence Number */
652	uint16_t		timeouts;	/* timeout count */
653	uint16_t		success;	/* successes afer timeout */
654
655
656	/* Opposing port in a 929 or 1030, or NULL */
657	struct mpt_softc *	mpt2;
658
659	/* FW Image management */
660	uint32_t		fw_image_size;
661	uint8_t		       *fw_image;
662	bus_dma_tag_t		fw_dmat;	/* DMA tag for firmware image */
663	bus_dmamap_t		fw_dmap;	/* DMA map for firmware image */
664	bus_addr_t		fw_phys;	/* BusAddr of request memory */
665
666	/* Shutdown Event Handler. */
667	eventhandler_tag         eh;
668
669	TAILQ_ENTRY(mpt_softc)	links;
670};
671
672/***************************** Locking Primitives *****************************/
673#if __FreeBSD_version < 500000
674#define	MPT_IFLAGS		INTR_TYPE_CAM
675#define	MPT_LOCK(mpt)		mpt_lockspl(mpt)
676#define	MPT_UNLOCK(mpt)		mpt_unlockspl(mpt)
677#define	MPTLOCK_2_CAMLOCK	MPT_UNLOCK
678#define	CAMLOCK_2_MPTLOCK	MPT_LOCK
679#define	MPT_LOCK_SETUP(mpt)
680#define	MPT_LOCK_DESTROY(mpt)
681
682static __inline void mpt_lockspl(struct mpt_softc *mpt);
683static __inline void mpt_unlockspl(struct mpt_softc *mpt);
684
685static __inline void
686mpt_lockspl(struct mpt_softc *mpt)
687{
688       int s;
689
690       s = splcam();
691       if (mpt->mpt_islocked++ == 0) {
692               mpt->mpt_splsaved = s;
693       } else {
694               splx(s);
695	       panic("Recursed lock with mask: 0x%x\n", s);
696       }
697}
698
699static __inline void
700mpt_unlockspl(struct mpt_softc *mpt)
701{
702       if (mpt->mpt_islocked) {
703               if (--mpt->mpt_islocked == 0) {
704                       splx(mpt->mpt_splsaved);
705               }
706       } else
707	       panic("Negative lock count\n");
708}
709
710static __inline int
711mpt_sleep(struct mpt_softc *mpt, void *ident, int priority,
712	   const char *wmesg, int timo)
713{
714	int saved_cnt;
715	int saved_spl;
716	int error;
717
718	KASSERT(mpt->mpt_islocked <= 1, ("Invalid lock count on tsleep"));
719	saved_cnt = mpt->mpt_islocked;
720	saved_spl = mpt->mpt_splsaved;
721	mpt->mpt_islocked = 0;
722	error = tsleep(ident, priority, wmesg, timo);
723	KASSERT(mpt->mpt_islocked == 0, ("Invalid lock count on wakeup"));
724	mpt->mpt_islocked = saved_cnt;
725	mpt->mpt_splsaved = saved_spl;
726	return (error);
727}
728
729#else
730#ifdef	LOCKING_WORKED_AS_IT_SHOULD
731#error "Shouldn't Be Here!"
732#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE
733#define	MPT_LOCK_SETUP(mpt)						\
734		mtx_init(&mpt->mpt_lock, "mpt", NULL, MTX_DEF);		\
735		mpt->mpt_locksetup = 1
736#define	MPT_LOCK_DESTROY(mpt)						\
737	if (mpt->mpt_locksetup) {					\
738		mtx_destroy(&mpt->mpt_lock);				\
739		mpt->mpt_locksetup = 0;					\
740	}
741
742#define	MPT_LOCK(mpt)		mtx_lock(&(mpt)->mpt_lock)
743#define	MPT_UNLOCK(mpt)		mtx_unlock(&(mpt)->mpt_lock)
744#define	MPTLOCK_2_CAMLOCK(mpt)	\
745	mtx_unlock(&(mpt)->mpt_lock); mtx_lock(&Giant)
746#define	CAMLOCK_2_MPTLOCK(mpt)	\
747	mtx_unlock(&Giant); mtx_lock(&(mpt)->mpt_lock)
748#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
749	msleep(ident, &(mpt)->mpt_lock, priority, wmesg, timo)
750#else
751#define	MPT_IFLAGS		INTR_TYPE_CAM | INTR_ENTROPY
752#define	MPT_LOCK_SETUP(mpt)	do { } while (0)
753#define	MPT_LOCK_DESTROY(mpt)	do { } while (0)
754#define	MPT_LOCK(mpt)		do { } while (0)
755#define	MPT_UNLOCK(mpt)		do { } while (0)
756#define	MPTLOCK_2_CAMLOCK(mpt)	do { } while (0)
757#define	CAMLOCK_2_MPTLOCK(mpt)	do { } while (0)
758#define mpt_sleep(mpt, ident, priority, wmesg, timo) \
759	tsleep(ident, priority, wmesg, timo)
760#endif
761#endif
762
763/******************************* Register Access ******************************/
764static __inline void mpt_write(struct mpt_softc *, size_t, uint32_t);
765static __inline uint32_t mpt_read(struct mpt_softc *, int);
766static __inline void mpt_pio_write(struct mpt_softc *, size_t, uint32_t);
767static __inline uint32_t mpt_pio_read(struct mpt_softc *, int);
768
769static __inline void
770mpt_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
771{
772	bus_space_write_4(mpt->pci_st, mpt->pci_sh, offset, val);
773}
774
775static __inline uint32_t
776mpt_read(struct mpt_softc *mpt, int offset)
777{
778	return (bus_space_read_4(mpt->pci_st, mpt->pci_sh, offset));
779}
780
781/*
782 * Some operations (e.g. diagnostic register writes while the ARM proccessor
783 * is disabled), must be performed using "PCI pio" operations.  On non-PCI
784 * busses, these operations likely map to normal register accesses.
785 */
786static __inline void
787mpt_pio_write(struct mpt_softc *mpt, size_t offset, uint32_t val)
788{
789	bus_space_write_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset, val);
790}
791
792static __inline uint32_t
793mpt_pio_read(struct mpt_softc *mpt, int offset)
794{
795	return (bus_space_read_4(mpt->pci_pio_st, mpt->pci_pio_sh, offset));
796}
797/*********************** Reply Frame/Request Management ***********************/
798/* Max MPT Reply we are willing to accept (must be power of 2) */
799#define MPT_REPLY_SIZE   	256
800
801/*
802 * Must be less than 16384 in order for target mode to work
803 */
804#define MPT_MAX_REQUESTS(mpt)	512
805#define MPT_REQUEST_AREA	512
806#define MPT_SENSE_SIZE		32	/* included in MPT_REQUEST_AREA */
807#define MPT_REQ_MEM_SIZE(mpt)	(MPT_MAX_REQUESTS(mpt) * MPT_REQUEST_AREA)
808
809/*
810 * Currently we try to pack both callbacks and request indices into 14 bits
811 * so that we don't have to get fancy when we get a target mode context
812 * reply (which only has 14 bits of IoIndex value) or a normal scsi
813 * initiator context reply (where we get bits 28..0 of context).
814 */
815#define MPT_CONTEXT_CB_SHIFT	(14)
816#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
817#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
818#define MPT_CONTEXT_TO_CBI(x)	\
819    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
820#define MPT_CONTEXT_REQI_MASK	0x3FFF
821#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
822
823/*
824 * Convert a 32bit physical address returned from IOC to an
825 * offset into our reply frame memory or the kvm address needed
826 * to access the data.  The returned address is only the low
827 * 32 bits, so mask our base physical address accordingly.
828 */
829#define MPT_REPLY_BADDR(x)		\
830	(x << 1)
831#define MPT_REPLY_OTOV(m, i) 		\
832	((void *)(&m->reply[i]))
833
834#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
835do {							\
836	if (mpt->verbose >= MPT_PRT_DEBUG)		\
837		mpt_dump_reply_frame(mpt, reply_frame);	\
838} while(0)
839
840static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
841static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
842
843/*
844 * Give the reply buffer back to the IOC after we have
845 * finished processing it.
846 */
847static __inline void
848mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
849{
850     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
851}
852
853/* Get a reply from the IOC */
854static __inline uint32_t
855mpt_pop_reply_queue(struct mpt_softc *mpt)
856{
857     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
858}
859
860void mpt_complete_request_chain(struct mpt_softc *mpt,
861				struct req_queue *chain, u_int iocstatus);
862/************************** Scatter Gather Managment **************************/
863/* MPT_RQSL- size of request frame, in bytes */
864#define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
865
866/* MPT_NSGL- how many SG entries can fit in a request frame size */
867#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
868
869/* MPT_NRFM- how many request frames can fit in each request alloc we make */
870#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
871
872/*
873 * MPT_NSGL_FIRST- # of SG elements that can fit after
874 * an I/O request but still within the request frame.
875 * Do this safely based upon SGE_IO_UNION.
876 *
877 * Note that the first element is *within* the SCSI request.
878 */
879#define	MPT_NSGL_FIRST(mpt)	\
880    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
881    sizeof (SGE_IO_UNION))
882
883/***************************** IOC Initialization *****************************/
884int mpt_reset(struct mpt_softc *, int /*reinit*/);
885
886/****************************** Debugging ************************************/
887typedef struct mpt_decode_entry {
888	char    *name;
889	u_int	 value;
890	u_int	 mask;
891} mpt_decode_entry_t;
892
893int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
894		     const char *name, u_int value, u_int *cur_column,
895		     u_int wrap_point);
896
897enum {
898	MPT_PRT_ALWAYS,
899	MPT_PRT_FATAL,
900	MPT_PRT_ERROR,
901	MPT_PRT_WARN,
902	MPT_PRT_INFO,
903	MPT_PRT_DEBUG,
904	MPT_PRT_DEBUG1,
905	MPT_PRT_DEBUG2,
906	MPT_PRT_DEBUG3,
907	MPT_PRT_TRACE,
908	MPT_PRT_NONE=100
909};
910
911#if __FreeBSD_version > 500000
912#define mpt_lprt(mpt, level, ...)		\
913do {						\
914	if (level <= (mpt)->verbose)		\
915		mpt_prt(mpt, __VA_ARGS__);	\
916} while (0)
917
918#define mpt_lprtc(mpt, level, ...)		 \
919do {						 \
920	if (level <= (mpt)->debug_level)	 \
921		mpt_prtc(mpt, __VA_ARGS__);	 \
922} while (0)
923#else
924void mpt_lprt(struct mpt_softc *, int, const char *, ...)
925	__printflike(3, 4);
926void mpt_lprtc(struct mpt_softc *, int, const char *, ...)
927	__printflike(3, 4);
928#endif
929void mpt_prt(struct mpt_softc *, const char *, ...)
930	__printflike(2, 3);
931void mpt_prtc(struct mpt_softc *, const char *, ...)
932	__printflike(2, 3);
933
934/**************************** Target Mode Related ***************************/
935static __inline int mpt_cdblen(uint8_t, int);
936static __inline int
937mpt_cdblen(uint8_t cdb0, int maxlen)
938{
939	int group = cdb0 >> 5;
940	switch (group) {
941	case 0:
942		return (6);
943	case 1:
944		return (10);
945	case 4:
946	case 5:
947		return (12);
948	default:
949		return (16);
950	}
951}
952#ifdef	INVARIANTS
953static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
954static __inline request_t *
955mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
956{
957	uint16_t rtg = (tag >> 16);
958	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
959	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
960	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
961	return (mpt->tgt_cmd_ptrs[rtg]);
962}
963#endif
964
965typedef enum {
966	MPT_ABORT_TASK_SET=1234,
967	MPT_CLEAR_TASK_SET,
968	MPT_TARGET_RESET,
969	MPT_CLEAR_ACA,
970	MPT_TERMINATE_TASK,
971	MPT_NIL_TMT_VALUE=5678
972} mpt_task_mgmt_t;
973
974/**************************** Unclassified Routines ***************************/
975void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
976int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
977					 size_t reply_len, void *reply);
978int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
979			     mpt_req_state_t state, mpt_req_state_t mask,
980			     int sleep_ok, int time_ms);
981void		mpt_enable_ints(struct mpt_softc *mpt);
982void		mpt_disable_ints(struct mpt_softc *mpt);
983int		mpt_attach(struct mpt_softc *mpt);
984int		mpt_shutdown(struct mpt_softc *mpt);
985int		mpt_detach(struct mpt_softc *mpt);
986int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
987				       size_t len, void *cmd);
988request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
989void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
990void		mpt_intr(void *arg);
991void		mpt_check_doorbell(struct mpt_softc *mpt);
992void		mpt_dump_reply_frame(struct mpt_softc *mpt,
993				     MSG_DEFAULT_REPLY *reply_frame);
994
995void		mpt_set_config_regs(struct mpt_softc *);
996int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
997				  u_int /*Action*/, u_int /*PageVersion*/,
998				  u_int /*PageLength*/, u_int /*PageNumber*/,
999				  u_int /*PageType*/, uint32_t /*PageAddress*/,
1000				  bus_addr_t /*addr*/, bus_size_t/*len*/,
1001				  int /*sleep_ok*/, int /*timeout_ms*/);
1002int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
1003				    int /*PageNumber*/,
1004				    uint32_t /*PageAddress*/,
1005				    CONFIG_PAGE_HEADER *,
1006				    int /*sleep_ok*/, int /*timeout_ms*/);
1007int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1008				  uint32_t /*PageAddress*/,
1009				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1010				  int /*sleep_ok*/, int /*timeout_ms*/);
1011int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1012				   uint32_t /*PageAddress*/,
1013				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1014				   int /*sleep_ok*/, int /*timeout_ms*/);
1015static __inline int
1016mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1017		      CONFIG_PAGE_HEADER *hdr, size_t len,
1018		      int sleep_ok, int timeout_ms)
1019{
1020	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1021				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1022}
1023
1024static __inline int
1025mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1026		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1027		       int timeout_ms)
1028{
1029	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1030				   PageAddress, hdr, len, sleep_ok,
1031				   timeout_ms));
1032}
1033
1034/* mpt_debug.c functions */
1035void mpt_print_reply(void *vmsg);
1036void mpt_print_db(uint32_t mb);
1037void mpt_print_config_reply(void *vmsg);
1038char *mpt_ioc_diag(uint32_t diag);
1039void mpt_req_state(mpt_req_state_t state);
1040void mpt_print_config_request(void *vmsg);
1041void mpt_print_request(void *vmsg);
1042void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1043void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1044#endif /* _MPT_H_ */
1045