mpt.h revision 157354
1/* $FreeBSD: head/sys/dev/mpt/mpt.h 157354 2006-04-01 07:12:18Z 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_NIL		= 0x00,
282	REQ_STATE_FREE		= 0x01,
283	REQ_STATE_ALLOCATED	= 0x02,
284	REQ_STATE_QUEUED	= 0x04,
285	REQ_STATE_DONE		= 0x08,
286	REQ_STATE_TIMEDOUT	= 0x10,
287	REQ_STATE_NEED_WAKEUP	= 0x20,
288	REQ_STATE_LOCKED	= 0x80,	/* can't be freed */
289	REQ_STATE_MASK		= 0xFF
290} mpt_req_state_t;
291
292struct req_entry {
293	TAILQ_ENTRY(req_entry) links;	/* Pointer to next in list */
294	mpt_req_state_t	state;		/* Request State Information */
295	uint16_t	index;		/* Index of this entry */
296	uint16_t	IOCStatus;	/* Completion status */
297	uint32_t	serno;		/* serial number */
298	union ccb      *ccb;		/* CAM request */
299	void	       *req_vbuf;	/* Virtual Address of Entry */
300	void	       *sense_vbuf;	/* Virtual Address of sense data */
301	bus_addr_t	req_pbuf;	/* Physical Address of Entry */
302	bus_addr_t	sense_pbuf;	/* Physical Address of sense data */
303	bus_dmamap_t	dmap;		/* DMA map for data buffer */
304	struct req_entry *chain;	/* for SGE overallocations */
305};
306
307/**************************** MPI Target State Info ***************************/
308
309typedef struct {
310	uint32_t reply_desc;	/* current reply descriptor */
311	uint32_t resid;		/* current data residual */
312	uint32_t bytes_xfered;	/* current relative offset */
313	union ccb *ccb;		/* pointer to currently active ccb */
314	request_t *req;		/* pointer to currently active assist request */
315	int	nxfers;
316	uint32_t tag_id;
317	enum {
318		TGT_STATE_NIL,
319		TGT_STATE_LOADING,
320		TGT_STATE_LOADED,
321		TGT_STATE_IN_CAM,
322                TGT_STATE_SETTING_UP_FOR_DATA,
323                TGT_STATE_MOVING_DATA,
324                TGT_STATE_MOVING_DATA_AND_STATUS,
325                TGT_STATE_SENDING_STATUS
326	} state;
327} mpt_tgt_state_t;
328
329/*
330 * When we get an incoming command it has its own tag which is called the
331 * IoIndex. This is the value we gave that particular command buffer when
332 * we originally assigned it. It's just a number, really. The FC card uses
333 * it as an RX_ID. We can use it to index into mpt->tgt_cmd_ptrs, which
334 * contains pointers the request_t structures related to that IoIndex.
335 *
336 * What *we* do is construct a tag out of the index for the target command
337 * which owns the incoming ATIO plus a rolling sequence number.
338 */
339#define	MPT_MAKE_TAGID(mpt, req, ioindex)	\
340 ((ioindex << 18) | (((mpt->sequence++) & 0x3f) << 12) | (req->index & 0xfff))
341
342#ifdef	INVARIANTS
343#define	MPT_TAG_2_REQ(a, b)		mpt_tag_2_req(a, (uint32_t) b)
344#else
345#define	MPT_TAG_2_REQ(mpt, tag)		mpt->tgt_cmd_ptrs[tag >> 18]
346#endif
347
348#define	MPT_TGT_STATE(mpt, req) ((mpt_tgt_state_t *) \
349    (&((uint8_t *)req->req_vbuf)[MPT_RQSL(mpt) - sizeof (mpt_tgt_state_t)]))
350
351STAILQ_HEAD(mpt_hdr_stailq, ccb_hdr);
352#define	MPT_MAX_LUNS	256
353typedef struct {
354	struct mpt_hdr_stailq	atios;
355	struct mpt_hdr_stailq	inots;
356	int enabled;
357} tgt_resource_t;
358#define	MPT_MAX_ELS	8
359
360/**************************** Handler Registration ****************************/
361/*
362 * Global table of registered reply handlers.  The
363 * handler is indicated by byte 3 of the request
364 * index submitted to the IOC.  This allows the
365 * driver core to perform generic processing without
366 * any knowledge of per-personality behavior.
367 *
368 * MPT_NUM_REPLY_HANDLERS must be a power of 2
369 * to allow the easy generation of a mask.
370 *
371 * The handler offsets used by the core are hard coded
372 * allowing faster code generation when assigning a handler
373 * to a request.  All "personalities" must use the
374 * the handler registration mechanism.
375 *
376 * The IOC handlers that are rarely executed are placed
377 * at the tail of the table to make it more likely that
378 * all commonly executed handlers fit in a single cache
379 * line.
380 */
381#define MPT_NUM_REPLY_HANDLERS		(32)
382#define MPT_REPLY_HANDLER_EVENTS	MPT_CBI_TO_HID(0)
383#define MPT_REPLY_HANDLER_CONFIG	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-1)
384#define MPT_REPLY_HANDLER_HANDSHAKE	MPT_CBI_TO_HID(MPT_NUM_REPLY_HANDLERS-2)
385typedef int mpt_reply_handler_t(struct mpt_softc *mpt, request_t *request,
386    uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame);
387typedef union {
388	mpt_reply_handler_t	*reply_handler;
389} mpt_handler_t;
390
391typedef enum {
392	MPT_HANDLER_REPLY,
393	MPT_HANDLER_EVENT,
394	MPT_HANDLER_RESET,
395	MPT_HANDLER_SHUTDOWN
396} mpt_handler_type;
397
398struct mpt_handler_record
399{
400	LIST_ENTRY(mpt_handler_record)	links;
401	mpt_handler_t			handler;
402};
403
404LIST_HEAD(mpt_handler_list, mpt_handler_record);
405
406/*
407 * The handler_id is currently unused but would contain the
408 * handler ID used in the MsgContext field to allow direction
409 * of replies to the handler.  Registrations that don't require
410 * a handler id can pass in NULL for the handler_id.
411 *
412 * Deregistrations for handlers without a handler id should
413 * pass in MPT_HANDLER_ID_NONE.
414 */
415#define MPT_HANDLER_ID_NONE		(0xFFFFFFFF)
416int mpt_register_handler(struct mpt_softc *, mpt_handler_type,
417			 mpt_handler_t, uint32_t *);
418int mpt_deregister_handler(struct mpt_softc *, mpt_handler_type,
419			   mpt_handler_t, uint32_t);
420
421/******************* Per-Controller Instance Data Structures ******************/
422TAILQ_HEAD(req_queue, req_entry);
423
424/* Structure for saving proper values for modifyable PCI config registers */
425struct mpt_pci_cfg {
426	uint16_t Command;
427	uint16_t LatencyTimer_LineSize;
428	uint32_t IO_BAR;
429	uint32_t Mem0_BAR[2];
430	uint32_t Mem1_BAR[2];
431	uint32_t ROM_BAR;
432	uint8_t  IntLine;
433	uint32_t PMCSR;
434};
435
436typedef enum {
437	MPT_RVF_NONE		= 0x0,
438	MPT_RVF_ACTIVE		= 0x1,
439	MPT_RVF_ANNOUNCED	= 0x2,
440	MPT_RVF_UP2DATE		= 0x4,
441	MPT_RVF_REFERENCED	= 0x8,
442	MPT_RVF_WCE_CHANGED	= 0x10
443} mpt_raid_volume_flags;
444
445struct mpt_raid_volume {
446	CONFIG_PAGE_RAID_VOL_0	       *config_page;
447	MPI_RAID_VOL_INDICATOR		sync_progress;
448	mpt_raid_volume_flags		flags;
449	u_int				quieced_disks;
450};
451
452typedef enum {
453	MPT_RDF_NONE		= 0x00,
454	MPT_RDF_ACTIVE		= 0x01,
455	MPT_RDF_ANNOUNCED	= 0x02,
456	MPT_RDF_UP2DATE		= 0x04,
457	MPT_RDF_REFERENCED	= 0x08,
458	MPT_RDF_QUIESCING	= 0x10,
459	MPT_RDF_QUIESCED	= 0x20
460} mpt_raid_disk_flags;
461
462struct mpt_raid_disk {
463	CONFIG_PAGE_RAID_PHYS_DISK_0	config_page;
464	struct mpt_raid_volume	       *volume;
465	u_int				member_number;
466	u_int				pass_thru_active;
467	mpt_raid_disk_flags		flags;
468};
469
470struct mpt_evtf_record {
471	MSG_EVENT_NOTIFY_REPLY		reply;
472	uint32_t			context;
473	LIST_ENTRY(mpt_evtf_record)	links;
474};
475
476LIST_HEAD(mpt_evtf_list, mpt_evtf_record);
477
478struct mpt_softc {
479	device_t		dev;
480#if __FreeBSD_version < 500000
481	int			mpt_splsaved;
482	uint32_t		mpt_islocked;
483#else
484	struct mtx		mpt_lock;
485#endif
486	uint32_t		mpt_pers_mask;
487	uint32_t		: 8,
488		unit		: 8,
489				: 1,
490		twildcard	: 1,
491		tenabled	: 1,
492		cap 		: 2,	/* none, ini, target, both */
493		role		: 2,	/* none, ini, target, both */
494		raid_mwce_set	: 1,
495		getreqwaiter	: 1,
496		shutdwn_raid    : 1,
497		shutdwn_recovery: 1,
498		outofbeer	: 1,
499		mpt_locksetup	: 1,
500		disabled	: 1,
501		is_sas		: 1,
502		is_fc		: 1;
503
504	u_int			verbose;
505
506	/*
507	 * IOC Facts
508	 */
509	uint16_t	mpt_global_credits;
510	uint16_t	request_frame_size;
511	uint8_t		mpt_max_devices;
512	uint8_t		mpt_max_buses;
513	uint8_t		ioc_facts_flags;
514	uint8_t		padding0;
515
516	/*
517	 * Port Facts
518	 * XXX - Add multi-port support!.
519	 */
520	uint16_t	mpt_ini_id;
521	uint16_t	mpt_port_type;
522	uint16_t	mpt_proto_flags;
523	uint16_t	mpt_max_tgtcmds;
524
525	/*
526	 * Device Configuration Information
527	 */
528	union {
529		struct mpt_spi_cfg {
530			CONFIG_PAGE_SCSI_PORT_0		_port_page0;
531			CONFIG_PAGE_SCSI_PORT_1		_port_page1;
532			CONFIG_PAGE_SCSI_PORT_2		_port_page2;
533			CONFIG_PAGE_SCSI_DEVICE_0	_dev_page0[16];
534			CONFIG_PAGE_SCSI_DEVICE_1	_dev_page1[16];
535			uint16_t			_tag_enable;
536			uint16_t			_disc_enable;
537			uint16_t			_update_params0;
538			uint16_t			_update_params1;
539		} spi;
540#define	mpt_port_page0		cfg.spi._port_page0
541#define	mpt_port_page1		cfg.spi._port_page1
542#define	mpt_port_page2		cfg.spi._port_page2
543#define	mpt_dev_page0		cfg.spi._dev_page0
544#define	mpt_dev_page1		cfg.spi._dev_page1
545#define	mpt_tag_enable		cfg.spi._tag_enable
546#define	mpt_disc_enable		cfg.spi._disc_enable
547#define	mpt_update_params0	cfg.spi._update_params0
548#define	mpt_update_params1	cfg.spi._update_params1
549		struct mpi_fc_cfg {
550			CONFIG_PAGE_FC_PORT_0 _port_page0;
551#define	mpt_fcport_page0	cfg.fc._port_page0
552		} fc;
553	} cfg;
554
555	/* Controller Info */
556	CONFIG_PAGE_IOC_2 *	ioc_page2;
557	CONFIG_PAGE_IOC_3 *	ioc_page3;
558
559	/* Raid Data */
560	struct mpt_raid_volume* raid_volumes;
561	struct mpt_raid_disk*	raid_disks;
562	u_int			raid_max_volumes;
563	u_int			raid_max_disks;
564	u_int			raid_page0_len;
565	u_int			raid_wakeup;
566	u_int			raid_rescan;
567	u_int			raid_resync_rate;
568	u_int			raid_mwce_setting;
569	u_int			raid_queue_depth;
570	u_int			raid_nonopt_volumes;
571	struct proc	       *raid_thread;
572	struct callout		raid_timer;
573
574	/*
575	 * PCI Hardware info
576	 */
577	struct resource *	pci_irq;	/* Interrupt map for chip */
578	void *			ih;		/* Interupt handle */
579	struct mpt_pci_cfg	pci_cfg;	/* saved PCI conf registers */
580
581	/*
582	 * DMA Mapping Stuff
583	 */
584	struct resource *	pci_reg;	/* Register map for chip */
585	int			pci_mem_rid;	/* Resource ID */
586	bus_space_tag_t		pci_st;		/* Bus tag for registers */
587	bus_space_handle_t	pci_sh;		/* Bus handle for registers */
588	/* PIO versions of above. */
589	int			pci_pio_rid;
590	struct resource *	pci_pio_reg;
591	bus_space_tag_t		pci_pio_st;
592	bus_space_handle_t	pci_pio_sh;
593
594	bus_dma_tag_t		parent_dmat;	/* DMA tag for parent PCI bus */
595	bus_dma_tag_t		reply_dmat;	/* DMA tag for reply memory */
596	bus_dmamap_t		reply_dmap;	/* DMA map for reply memory */
597	uint8_t		       *reply;		/* KVA of reply memory */
598	bus_addr_t		reply_phys;	/* BusAddr of reply memory */
599
600	bus_dma_tag_t		buffer_dmat;	/* DMA tag for buffers */
601	bus_dma_tag_t		request_dmat;	/* DMA tag for request memroy */
602	bus_dmamap_t		request_dmap;	/* DMA map for request memroy */
603	uint8_t		       *request;	/* KVA of Request memory */
604	bus_addr_t		request_phys;	/* BusAddr of request memory */
605
606	uint32_t		max_seg_cnt;	/* calculated after IOC facts */
607
608	/*
609	 * Hardware management
610	 */
611	u_int			reset_cnt;
612
613	/*
614	 * CAM && Software Management
615	 */
616	request_t	       *request_pool;
617	struct req_queue	request_free_list;
618	struct req_queue	request_pending_list;
619	struct req_queue	request_timeout_list;
620
621
622	struct cam_sim	       *sim;
623	struct cam_path	       *path;
624
625	struct cam_sim	       *phydisk_sim;
626	struct cam_path	       *phydisk_path;
627
628	struct proc	       *recovery_thread;
629	request_t	       *tmf_req;
630
631	/*
632	 * Deferred frame acks due to resource shortage.
633	 */
634	struct mpt_evtf_list	ack_frames;
635	/*
636	 * Target Mode Support
637	 */
638	uint32_t		scsi_tgt_handler_id;
639	request_t **		tgt_cmd_ptrs;
640
641	/*
642	 * *snork*- this is chosen to be here *just in case* somebody
643	 * forgets to point to it exactly and we index off of trt with
644	 * CAM_LUN_WILDCARD.
645	 */
646	tgt_resource_t		trt_wildcard;	/* wildcard luns */
647	tgt_resource_t		trt[MPT_MAX_LUNS];
648	uint16_t		tgt_cmds_allocated;
649	uint16_t		padding1;
650
651	uint16_t		timeouts;	/* timeout count */
652	uint16_t		success;	/* successes afer timeout */
653	uint32_t		sequence;	/* Sequence Number */
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#define MPT_CONTEXT_CB_SHIFT	(16)
810#define MPT_CBI(handle)		(handle >> MPT_CONTEXT_CB_SHIFT)
811#define MPT_CBI_TO_HID(cbi)	((cbi) << MPT_CONTEXT_CB_SHIFT)
812#define MPT_CONTEXT_TO_CBI(x)	\
813    (((x) >> MPT_CONTEXT_CB_SHIFT) & (MPT_NUM_REPLY_HANDLERS - 1))
814#define MPT_CONTEXT_REQI_MASK	0xFFFF
815#define MPT_CONTEXT_TO_REQI(x)	((x) & MPT_CONTEXT_REQI_MASK)
816
817/*
818 * Convert a 32bit physical address returned from IOC to an
819 * offset into our reply frame memory or the kvm address needed
820 * to access the data.  The returned address is only the low
821 * 32 bits, so mask our base physical address accordingly.
822 */
823#define MPT_REPLY_BADDR(x)		\
824	(x << 1)
825#define MPT_REPLY_OTOV(m, i) 		\
826	((void *)(&m->reply[i]))
827
828#define	MPT_DUMP_REPLY_FRAME(mpt, reply_frame)		\
829do {							\
830	if (mpt->verbose >= MPT_PRT_DEBUG)		\
831		mpt_dump_reply_frame(mpt, reply_frame);	\
832} while(0)
833
834static __inline uint32_t mpt_pop_reply_queue(struct mpt_softc *mpt);
835static __inline void mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr);
836
837/*
838 * Give the reply buffer back to the IOC after we have
839 * finished processing it.
840 */
841static __inline void
842mpt_free_reply(struct mpt_softc *mpt, uint32_t ptr)
843{
844     mpt_write(mpt, MPT_OFFSET_REPLY_Q, ptr);
845}
846
847/* Get a reply from the IOC */
848static __inline uint32_t
849mpt_pop_reply_queue(struct mpt_softc *mpt)
850{
851     return mpt_read(mpt, MPT_OFFSET_REPLY_Q);
852}
853
854void
855mpt_complete_request_chain(struct mpt_softc *, struct req_queue *, u_int);
856
857/************************** Scatter Gather Managment **************************/
858/* MPT_RQSL- size of request frame, in bytes */
859#define	MPT_RQSL(mpt)		(mpt->request_frame_size << 2)
860
861/* MPT_NSGL- how many SG entries can fit in a request frame size */
862#define	MPT_NSGL(mpt)		(MPT_RQSL(mpt) / sizeof (SGE_IO_UNION))
863
864/* MPT_NRFM- how many request frames can fit in each request alloc we make */
865#define	MPT_NRFM(mpt)		(MPT_REQUEST_AREA / MPT_RQSL(mpt))
866
867/*
868 * MPT_NSGL_FIRST- # of SG elements that can fit after
869 * an I/O request but still within the request frame.
870 * Do this safely based upon SGE_IO_UNION.
871 *
872 * Note that the first element is *within* the SCSI request.
873 */
874#define	MPT_NSGL_FIRST(mpt)	\
875    ((MPT_RQSL(mpt) - sizeof (MSG_SCSI_IO_REQUEST) + sizeof (SGE_IO_UNION)) / \
876    sizeof (SGE_IO_UNION))
877
878/***************************** IOC Initialization *****************************/
879int mpt_reset(struct mpt_softc *, int /*reinit*/);
880
881/****************************** Debugging ************************************/
882typedef struct mpt_decode_entry {
883	char    *name;
884	u_int	 value;
885	u_int	 mask;
886} mpt_decode_entry_t;
887
888int mpt_decode_value(mpt_decode_entry_t *table, u_int num_entries,
889		     const char *name, u_int value, u_int *cur_column,
890		     u_int wrap_point);
891
892enum {
893	MPT_PRT_ALWAYS,
894	MPT_PRT_FATAL,
895	MPT_PRT_ERROR,
896	MPT_PRT_WARN,
897	MPT_PRT_INFO,
898	MPT_PRT_DEBUG,
899	MPT_PRT_DEBUG1,
900	MPT_PRT_DEBUG2,
901	MPT_PRT_DEBUG3,
902	MPT_PRT_TRACE,
903	MPT_PRT_NONE=100
904};
905
906#if __FreeBSD_version > 500000
907#define mpt_lprt(mpt, level, ...)		\
908do {						\
909	if (level <= (mpt)->verbose)		\
910		mpt_prt(mpt, __VA_ARGS__);	\
911} while (0)
912
913#define mpt_lprtc(mpt, level, ...)		 \
914do {						 \
915	if (level <= (mpt)->debug_level)	 \
916		mpt_prtc(mpt, __VA_ARGS__);	 \
917} while (0)
918#else
919void mpt_lprt(struct mpt_softc *, int, const char *, ...)
920	__printflike(3, 4);
921void mpt_lprtc(struct mpt_softc *, int, const char *, ...)
922	__printflike(3, 4);
923#endif
924void mpt_prt(struct mpt_softc *, const char *, ...)
925	__printflike(2, 3);
926void mpt_prtc(struct mpt_softc *, const char *, ...)
927	__printflike(2, 3);
928
929/**************************** Target Mode Related ***************************/
930static __inline int mpt_cdblen(uint8_t, int);
931static __inline int
932mpt_cdblen(uint8_t cdb0, int maxlen)
933{
934	int group = cdb0 >> 5;
935	switch (group) {
936	case 0:
937		return (6);
938	case 1:
939		return (10);
940	case 4:
941	case 5:
942		return (12);
943	default:
944		return (16);
945	}
946}
947#ifdef	INVARIANTS
948static __inline request_t * mpt_tag_2_req(struct mpt_softc *, uint32_t);
949static __inline request_t *
950mpt_tag_2_req(struct mpt_softc *mpt, uint32_t tag)
951{
952	uint16_t rtg = (tag >> 18);
953	KASSERT(rtg < mpt->tgt_cmds_allocated, ("bad tag %d\n", tag));
954	KASSERT(mpt->tgt_cmd_ptrs, ("no cmd backpointer array"));
955	KASSERT(mpt->tgt_cmd_ptrs[rtg], ("no cmd backpointer"));
956	return (mpt->tgt_cmd_ptrs[rtg]);
957}
958#endif
959
960typedef enum {
961	MPT_ABORT_TASK_SET=1234,
962	MPT_CLEAR_TASK_SET,
963	MPT_TARGET_RESET,
964	MPT_CLEAR_ACA,
965	MPT_TERMINATE_TASK,
966	MPT_NIL_TMT_VALUE=5678
967} mpt_task_mgmt_t;
968
969/**************************** Unclassified Routines ***************************/
970void		mpt_send_cmd(struct mpt_softc *mpt, request_t *req);
971int		mpt_recv_handshake_reply(struct mpt_softc *mpt,
972					 size_t reply_len, void *reply);
973int		mpt_wait_req(struct mpt_softc *mpt, request_t *req,
974			     mpt_req_state_t state, mpt_req_state_t mask,
975			     int sleep_ok, int time_ms);
976void		mpt_enable_ints(struct mpt_softc *mpt);
977void		mpt_disable_ints(struct mpt_softc *mpt);
978int		mpt_attach(struct mpt_softc *mpt);
979int		mpt_shutdown(struct mpt_softc *mpt);
980int		mpt_detach(struct mpt_softc *mpt);
981int		mpt_send_handshake_cmd(struct mpt_softc *mpt,
982				       size_t len, void *cmd);
983request_t *	mpt_get_request(struct mpt_softc *mpt, int sleep_ok);
984void		mpt_free_request(struct mpt_softc *mpt, request_t *req);
985void		mpt_intr(void *arg);
986void		mpt_check_doorbell(struct mpt_softc *mpt);
987void		mpt_dump_reply_frame(struct mpt_softc *mpt,
988				     MSG_DEFAULT_REPLY *reply_frame);
989
990void		mpt_set_config_regs(struct mpt_softc *);
991int		mpt_issue_cfg_req(struct mpt_softc */*mpt*/, request_t */*req*/,
992				  u_int /*Action*/, u_int /*PageVersion*/,
993				  u_int /*PageLength*/, u_int /*PageNumber*/,
994				  u_int /*PageType*/, uint32_t /*PageAddress*/,
995				  bus_addr_t /*addr*/, bus_size_t/*len*/,
996				  int /*sleep_ok*/, int /*timeout_ms*/);
997int		mpt_read_cfg_header(struct mpt_softc *, int /*PageType*/,
998				    int /*PageNumber*/,
999				    uint32_t /*PageAddress*/,
1000				    CONFIG_PAGE_HEADER *,
1001				    int /*sleep_ok*/, int /*timeout_ms*/);
1002int		mpt_read_cfg_page(struct mpt_softc *t, int /*Action*/,
1003				  uint32_t /*PageAddress*/,
1004				  CONFIG_PAGE_HEADER *, size_t /*len*/,
1005				  int /*sleep_ok*/, int /*timeout_ms*/);
1006int		mpt_write_cfg_page(struct mpt_softc *, int /*Action*/,
1007				   uint32_t /*PageAddress*/,
1008				   CONFIG_PAGE_HEADER *, size_t /*len*/,
1009				   int /*sleep_ok*/, int /*timeout_ms*/);
1010static __inline int
1011mpt_read_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1012		      CONFIG_PAGE_HEADER *hdr, size_t len,
1013		      int sleep_ok, int timeout_ms)
1014{
1015	return (mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
1016				  PageAddress, hdr, len, sleep_ok, timeout_ms));
1017}
1018
1019static __inline int
1020mpt_write_cur_cfg_page(struct mpt_softc *mpt, uint32_t PageAddress,
1021		       CONFIG_PAGE_HEADER *hdr, size_t len, int sleep_ok,
1022		       int timeout_ms)
1023{
1024	return (mpt_write_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT,
1025				   PageAddress, hdr, len, sleep_ok,
1026				   timeout_ms));
1027}
1028
1029/* mpt_debug.c functions */
1030void mpt_print_reply(void *vmsg);
1031void mpt_print_db(uint32_t mb);
1032void mpt_print_config_reply(void *vmsg);
1033char *mpt_ioc_diag(uint32_t diag);
1034void mpt_req_state(mpt_req_state_t state);
1035void mpt_print_config_request(void *vmsg);
1036void mpt_print_request(void *vmsg);
1037void mpt_print_scsi_io_request(MSG_SCSI_IO_REQUEST *msg);
1038void mpt_dump_sgl(SGE_IO_UNION *se, int offset);
1039#endif /* _MPT_H_ */
1040