aacraid_var.h revision 250963
1119452Sobrien/*-
2101225Sphk * Copyright (c) 2000 Michael Smith
3101225Sphk * Copyright (c) 2001 Scott Long
4101225Sphk * Copyright (c) 2000 BSDi
5101225Sphk * Copyright (c) 2001-2010 Adaptec, Inc.
6101225Sphk * Copyright (c) 2010-2012 PMC-Sierra, Inc.
7101225Sphk * All rights reserved.
8101225Sphk *
9101225Sphk * Redistribution and use in source and binary forms, with or without
10102934Sphk * modification, are permitted provided that the following conditions
11102934Sphk * are met:
12102934Sphk * 1. Redistributions of source code must retain the above copyright
13102934Sphk *    notice, this list of conditions and the following disclaimer.
14102934Sphk * 2. Redistributions in binary form must reproduce the above copyright
15102934Sphk *    notice, this list of conditions and the following disclaimer in the
16102934Sphk *    documentation and/or other materials provided with the distribution.
17102934Sphk *
18102934Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19109327Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20123015Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21123015Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22123015Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23123015Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24123015Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25123015Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26123015Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27123015Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28123015Sphk * SUCH DAMAGE.
29123015Sphk *
30123015Sphk *	$FreeBSD: head/sys/dev/aacraid/aacraid_var.h 250963 2013-05-24 09:22:43Z achim $
31123015Sphk */
32123015Sphk
33123015Sphk#include <sys/bio.h>
34123015Sphk#if __FreeBSD_version >= 800000
35123015Sphk#include <sys/callout.h>
36123015Sphk#endif
37123015Sphk#include <sys/lock.h>
38123015Sphk#include <sys/mutex.h>
39101225Sphk#include <sys/taskqueue.h>
40102934Sphk#include <sys/selinfo.h>
41115683Sobrien#include <geom/geom_disk.h>
42115683Sobrien
43115683Sobrien#define	AAC_TYPE_DEVO			1
44111138Sphk#define	AAC_TYPE_ALPHA			2
45101225Sphk#define	AAC_TYPE_BETA			3
46101225Sphk#define	AAC_TYPE_RELEASE		4
47101225Sphk
48101225Sphk#define	AAC_DRIVER_MAJOR_VERSION	3
49102934Sphk#define	AAC_DRIVER_MINOR_VERSION	1
50123015Sphk#define	AAC_DRIVER_BUGFIX_LEVEL		1
51102934Sphk#define	AAC_DRIVER_TYPE			AAC_TYPE_RELEASE
52102934Sphk
53103482Sphk#ifndef AAC_DRIVER_BUILD
54103482Sphk# define AAC_DRIVER_BUILD 1
55103482Sphk#endif
56103482Sphk
57109327Sphk#if __FreeBSD_version <= 601000
58109327Sphk#define bus_get_dma_tag(x)	NULL
59111647Sphk#endif
60101225Sphk
61121944Sphk/* **************************** NewBUS interrupt Crock ************************/
62101225Sphk#if __FreeBSD_version < 700031
63124144Sphk#define	aac_bus_setup_intr(d, i, f, U, if, ifa, hp)	\
64148231Sphk	bus_setup_intr(d, i, f, if, ifa, hp)
65101225Sphk#else
66102934Sphk#define	aac_bus_setup_intr	bus_setup_intr
67102934Sphk#endif
68102934Sphk
69123015Sphk/* **************************** NewBUS CAM Support ****************************/
70123015Sphk#if __FreeBSD_version < 700049
71124144Sphk#define aac_xpt_bus_register(sim, parent, bus)	\
72127039Sphk	xpt_bus_register(sim, bus)
73102934Sphk#else
74123015Sphk#define aac_xpt_bus_register	xpt_bus_register
75109327Sphk#endif
76127039Sphk
77127039Sphk/**************************** Kernel Thread Support ***************************/
78127039Sphk#if __FreeBSD_version > 800001
79123015Sphk#define aac_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
80127039Sphk	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
81148231Sphk#define	aac_kthread_exit(status)	\
82148231Sphk	kproc_exit(status)
83148231Sphk#else
84148231Sphk#define aac_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
85148231Sphk	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
86148231Sphk#define	aac_kthread_exit(status)	\
87148231Sphk	kthread_exit(status)
88148231Sphk#endif
89148231Sphk
90148231Sphk/*
91148231Sphk * Driver Parameter Definitions
92148231Sphk */
93148231Sphk
94148231Sphk/*
95127039Sphk * We allocate a small set of FIBs for the adapter to use to send us messages.
96130585Sphk */
97109327Sphk#define AAC_ADAPTER_FIBS	8
98109327Sphk
99123015Sphk/*
100123015Sphk * The controller reports status events in AIFs.  We hang on to a number of
101123015Sphk * these in order to pass them out to user-space management tools.
102123015Sphk */
103123015Sphk#define AAC_AIFQ_LENGTH		64
104123015Sphk
105123015Sphk/*
106123015Sphk * Firmware messages are passed in the printf buffer.
107123015Sphk */
108124144Sphk#define AAC_PRINTF_BUFSIZE	256
109123015Sphk
110123015Sphk/*
111123015Sphk * We wait this many seconds for the adapter to come ready if it is still
112123015Sphk * booting
113123015Sphk */
114123015Sphk#define AAC_BOOT_TIMEOUT	(3 * 60)
115123015Sphk
116123015Sphk/*
117126762Sjb * Timeout for immediate commands.
118126762Sjb */
119123015Sphk#define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
120123015Sphk
121123015Sphk/*
122123015Sphk * Timeout for normal commands
123123015Sphk */
124123015Sphk#define AAC_CMD_TIMEOUT		120		/* seconds */
125123015Sphk
126123015Sphk/*
127123015Sphk * Rate at which we periodically check for timed out commands and kick the
128123015Sphk * controller.
129123015Sphk */
130123015Sphk#define AAC_PERIODIC_INTERVAL	20		/* seconds */
131127801Sphk
132127801Sphk#define PASSTHROUGH_BUS		0
133127801Sphk#define CONTAINER_BUS		1
134123015Sphk/*
135123015Sphk * Per-container data structure
136123015Sphk */
137123015Sphkstruct aac_container
138123015Sphk{
139123015Sphk	struct aac_mntobj		co_mntobj;
140123015Sphk	int				co_found;
141123015Sphk	u_int32_t		co_uid;
142123015Sphk	TAILQ_ENTRY(aac_container)	co_link;
143123015Sphk};
144123015Sphk
145123015Sphk/*
146123015Sphk * Per-SIM data structure
147123015Sphk */
148123015Sphkstruct aac_cam;
149123015Sphkstruct aac_sim
150123015Sphk{
151123015Sphk	device_t		sim_dev;
152123015Sphk	int			TargetsPerBus;
153123015Sphk	int			BusNumber;
154123015Sphk	int			BusType;
155123015Sphk	int			InitiatorBusId;
156123015Sphk	struct aac_softc	*aac_sc;
157123015Sphk	struct aac_cam		*aac_cam;
158123015Sphk	TAILQ_ENTRY(aac_sim)	sim_link;
159123015Sphk};
160123015Sphk
161123015Sphk/*
162123015Sphk * Per-disk structure
163123015Sphk */
164126762Sjbstruct aac_disk
165127801Sphk{
166127801Sphk	device_t			ad_dev;
167127801Sphk	struct aac_softc		*ad_controller;
168127801Sphk	struct aac_container		*ad_container;
169123015Sphk	struct disk			*ad_disk;
170123015Sphk	int				ad_flags;
171123015Sphk#define AAC_DISK_OPEN	(1<<0)
172124144Sphk	int				ad_cylinders;
173123015Sphk	int				ad_heads;
174123015Sphk	int				ad_sectors;
175123015Sphk	u_int64_t			ad_size;
176123015Sphk	int				unit;
177123015Sphk};
178123015Sphk
179127039Sphk/*
180127039Sphk * Per-command control structure.
181127039Sphk */
182124144Sphkstruct aac_command
183123015Sphk{
184123015Sphk	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
185123015Sphk
186123015Sphk	struct aac_softc	*cm_sc;		/* controller that owns us */
187123015Sphk
188123015Sphk	struct aac_fib		*cm_fib;	/* FIB associated with this
189123015Sphk						 * command */
190123015Sphk	u_int64_t		cm_fibphys;	/* bus address of the FIB */
191123015Sphk	struct bio		*cm_data;	/* pointer to data in kernel
192124144Sphk						 * space */
193123015Sphk	u_int32_t		cm_datalen;	/* data length */
194123015Sphk	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
195123015Sphk	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
196123015Sphk						 * command */
197123015Sphk	int			cm_flags;
198123015Sphk#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
199123015Sphk						 * mapped */
200123015Sphk#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
201123015Sphk						 * from controller to host */
202123015Sphk#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
203123015Sphk						 * from host to controller */
204123015Sphk#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
205128677Sphk#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
206128677Sphk#define AAC_ON_AACQ_FREE	(1<<5)
207123015Sphk#define AAC_ON_AACQ_READY	(1<<6)
208123015Sphk#define AAC_ON_AACQ_BUSY	(1<<7)
209123015Sphk#define AAC_ON_AACQ_AIF		(1<<8)
210123015Sphk#define AAC_ON_AACQ_NORM	(1<<10)
211123015Sphk#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10))
212123015Sphk#define AAC_CMD_RESET		(1<<9)
213123015Sphk#define AAC_CMD_FASTRESP	(1<<11)
214123015Sphk#define AAC_CMD_WAIT		(1<<12)
215123015Sphk
216123015Sphk	void			(* cm_complete)(struct aac_command *cm);
217123015Sphk	union ccb 		*cm_ccb;
218123015Sphk	time_t			cm_timestamp;	/* command creation time */
219123015Sphk	int			cm_index;
220123015Sphk	bus_dma_tag_t		cm_passthr_dmat;	/* passthrough buffer/command
221123015Sphk							 * DMA tag */
222123015Sphk};
223123015Sphk
224123015Sphkstruct aac_fibmap {
225123015Sphk	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
226109327Sphk	struct aac_fib		*aac_fibs;
227109327Sphk	bus_dmamap_t		aac_fibmap;
228109327Sphk	struct aac_command	*aac_commands;
229109327Sphk};
230127039Sphk
231214346Sjhb/*
232109327Sphk * We gather a number of adapter-visible items into a single structure.
233123015Sphk *
234127039Sphk * The ordering of this strucure may be important; we copy the Linux driver:
235127039Sphk *
236127039Sphk * Adapter FIBs
237127039Sphk * Init struct
238214346Sjhb * Queue headers (Comm Area)
239127039Sphk * Printf buffer
240127039Sphk *
241127039Sphk * In addition, we add:
242214346Sjhb * Sync Fib
243127039Sphk */
244127039Sphkstruct aac_common {
245123015Sphk	/* fibs for the controller to send us messages */
246123015Sphk	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
247123015Sphk
248123015Sphk	/* the init structure */
249123015Sphk	struct aac_adapter_init	ac_init;
250123015Sphk
251123015Sphk	/* buffer for text messages from the controller */
252127039Sphk	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
253123015Sphk
254123015Sphk	/* fib for synchronous commands */
255123015Sphk	struct aac_fib		ac_sync_fib;
256109327Sphk
257123015Sphk	/* response buffer for SRC (new comm. type1) - must be last element */
258109327Sphk	u_int32_t   ac_host_rrq[0];
259123015Sphk};
260123015Sphk
261123015Sphk/*
262123015Sphk * Interface operations
263124144Sphk */
264109327Sphkstruct aac_interface
265123015Sphk{
266123015Sphk	int	(*aif_get_fwstatus)(struct aac_softc *sc);
267127039Sphk	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
268127039Sphk	int	(*aif_get_istatus)(struct aac_softc *sc);
269127039Sphk	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
270127039Sphk	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
271127039Sphk				   u_int32_t arg0, u_int32_t arg1,
272127039Sphk				   u_int32_t arg2, u_int32_t arg3);
273109327Sphk	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
274127039Sphk	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
275109327Sphk	int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
276123015Sphk	int (*aif_get_outb_queue)(struct aac_softc *sc);
277123015Sphk	void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
278123015Sphk};
279124144Sphkextern struct aac_interface	aacraid_src_interface;
280109327Sphkextern struct aac_interface	aacraid_srcv_interface;
281123015Sphk
282109327Sphk#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
283102935Sphk#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
284102935Sphk#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
285102935Sphk#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
286123015Sphk					(mask)))
287123015Sphk#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
288124144Sphk	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
289102935Sphk	(arg3)))
290102935Sphk#define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
291109327Sphk					(mb)))
292109327Sphk#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
293109327Sphk					0))
294109327Sphk#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
295123015Sphk					1))
296123015Sphk#define AAC_SEND_COMMAND(sc, cm)	((sc)->aac_if.aif_send_command((sc), (cm)))
297109327Sphk#define AAC_GET_OUTB_QUEUE(sc)		((sc)->aac_if.aif_get_outb_queue((sc)))
298109327Sphk#define AAC_SET_OUTB_QUEUE(sc, idx)	((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
299102935Sphk
300102935Sphk#define AAC_MEM0_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag0, \
301109327Sphk					sc->aac_bhandle0, reg, val)
302102935Sphk#define AAC_MEM0_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag0, \
303123015Sphk					sc->aac_bhandle0, reg)
304119715Sphk#define AAC_MEM0_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag0, \
305119715Sphk					sc->aac_bhandle0, reg, val)
306102935Sphk#define AAC_MEM0_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag0, \
307102935Sphk					sc->aac_bhandle0, reg)
308109327Sphk#define AAC_MEM0_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag0, \
309109327Sphk					sc->aac_bhandle0, reg, val)
310109327Sphk#define AAC_MEM0_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag0, \
311109327Sphk					sc->aac_bhandle0, reg)
312109327Sphk
313109327Sphk#define AAC_MEM1_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag1, \
314109327Sphk					sc->aac_bhandle1, reg, val)
315170289Sdwmalone#define AAC_MEM1_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag1, \
316109327Sphk					sc->aac_bhandle1, reg)
317109327Sphk#define AAC_MEM1_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag1, \
318109327Sphk					sc->aac_bhandle1, reg, val)
319109327Sphk#define AAC_MEM1_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag1, \
320109327Sphk					sc->aac_bhandle1, reg)
321109327Sphk#define AAC_MEM1_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag1, \
322109327Sphk					sc->aac_bhandle1, reg, val)
323109327Sphk#define AAC_MEM1_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag1, \
324123015Sphk					sc->aac_bhandle1, reg)
325123015Sphk
326123015Sphk/* fib context (IOCTL) */
327123015Sphkstruct aac_fib_context {
328102934Sphk	u_int32_t		unique;
329102934Sphk	int			ctx_idx;
330102934Sphk	int			ctx_wrap;
331102934Sphk	struct aac_fib_context *next, *prev;
332102934Sphk};
333102934Sphk
334124144Sphk/*
335124144Sphk * Per-controller structure.
336102934Sphk */
337102934Sphkstruct aac_softc
338102934Sphk{
339102934Sphk	/* bus connections */
340102934Sphk	device_t		aac_dev;
341123015Sphk	struct resource		*aac_regs_res0, *aac_regs_res1; /* reg. if. window */
342102934Sphk	int			aac_regs_rid0, aac_regs_rid1;		/* resource ID */
343102934Sphk	bus_space_handle_t	aac_bhandle0, aac_bhandle1;		/* bus space handle */
344102934Sphk	bus_space_tag_t		aac_btag0, aac_btag1;		/* bus space tag */
345102934Sphk	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
346136419Sphk	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
347123015Sphk							 * DMA tag */
348103168Ssam	struct resource		*aac_irq;		/* interrupt */
349102935Sphk	int			aac_irq_rid;
350102935Sphk	void			*aac_intr;		/* interrupt handle */
351124144Sphk	eventhandler_tag	eh;
352124144Sphk#if __FreeBSD_version >= 800000
353109327Sphk	struct callout	aac_daemontime;		/* clock daemon callout */
354123015Sphk#else
355109327Sphk	struct callout_handle	timeout_id;	/* timeout handle */
356124144Sphk#endif
357124144Sphk
358124144Sphk	/* controller features, limits and status */
359124144Sphk	int			aac_state;
360109327Sphk#define AAC_STATE_SUSPEND	(1<<0)
361109327Sphk#define	AAC_STATE_UNUSED0	(1<<1)
362109327Sphk#define AAC_STATE_INTERRUPTS_ON	(1<<2)
363102935Sphk#define AAC_STATE_AIF_SLEEPER	(1<<3)
364102934Sphk#define AAC_STATE_RESET		(1<<4)
365102934Sphk	struct FsaRevision		aac_revision;
366103482Sphk
367126370Sphk	/* controller hardware interface */
368101225Sphk	int			aac_hwif;
369165260Sn_hibma#define AAC_HWIF_SRC		5
370111647Sphk#define AAC_HWIF_SRCV		6
371111647Sphk#define AAC_HWIF_UNKNOWN	-1
372126370Sphk	bus_dma_tag_t		aac_common_dmat;	/* common structure
373165260Sn_hibma							 * DMA tag */
374111647Sphk	bus_dmamap_t		aac_common_dmamap;	/* common structure
375111647Sphk							 * DMA map */
376111647Sphk	struct aac_common	*aac_common;
377111647Sphk	u_int32_t		aac_common_busaddr;
378111647Sphk	u_int32_t		aac_host_rrq_idx;
379111647Sphk	struct aac_interface	aac_if;
380111647Sphk
381111647Sphk	/* command/fib resources */
382111647Sphk	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
383111647Sphk	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
384111647Sphk	u_int			total_fibs;
385165260Sn_hibma	struct aac_command	*aac_commands;
386124144Sphk
387111647Sphk	/* command management */
388111647Sphk	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
389124144Sphk						 * available for reuse */
390124144Sphk	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
391124144Sphk						 * controller resources */
392111647Sphk	TAILQ_HEAD(,aac_command) aac_busy;
393111647Sphk	TAILQ_HEAD(,aac_event)	aac_ev_cmfree;
394124144Sphk	struct bio_queue_head	aac_bioq;
395124144Sphk
396124144Sphk	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
397111647Sphk
398111647Sphk	/* connected containters */
399111647Sphk	TAILQ_HEAD(,aac_container)	aac_container_tqh;
400124144Sphk	struct mtx		aac_container_lock;
401124144Sphk
402111647Sphk	/*
403165260Sn_hibma	 * The general I/O lock.  This protects the sync fib, the lists, the
404126370Sphk	 * queues, and the registers.
405126370Sphk	 */
406165260Sn_hibma	struct mtx		aac_io_lock;
407124144Sphk
408124144Sphk	struct intr_config_hook	aac_ich;
409124144Sphk
410124144Sphk	/* sync. transfer mode */
411165260Sn_hibma	struct aac_command *aac_sync_cm;
412165260Sn_hibma
413111647Sphk	/* management interface */
414111647Sphk	struct cdev *aac_dev_t;
415111647Sphk	struct mtx		aac_aifq_lock;
416111647Sphk	struct aac_fib		aac_aifq[AAC_AIFQ_LENGTH];
417111647Sphk	int			aifq_idx;
418201223Srnoland	int			aifq_filled;
419201223Srnoland	int 			aif_pending;
420126370Sphk	struct aac_fib_context *fibctx;
421126370Sphk	struct selinfo		rcv_select;
422126370Sphk	struct proc		*aifthread;
423126370Sphk	int			aifflags;
424126370Sphk#define AAC_AIFFLAGS_RUNNING	(1 << 0)
425126370Sphk#define AAC_AIFFLAGS_AIF	(1 << 1)
426126370Sphk#define	AAC_AIFFLAGS_EXIT	(1 << 2)
427126370Sphk#define AAC_AIFFLAGS_EXITED	(1 << 3)
428130585Sphk#define AAC_AIFFLAGS_PRINTF	(1 << 4)
429101225Sphk#define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
430109327Sphk#define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
431109327Sphk				 AAC_AIFFLAGS_ALLOCFIBS)
432126370Sphk	u_int32_t		flags;
433123015Sphk#define AAC_FLAGS_PERC2QC	(1 << 0)
434123015Sphk#define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
435123015Sphk#define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
436123015Sphk#define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
437109327Sphk#define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
438109327Sphk#define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
439109327Sphk						 * 2GB-4GB range */
440109327Sphk#define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
441123015Sphk#define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
442109327Sphk#define	AAC_FLAGS_BROKEN_MEMMAP (1 << 8)	/* Broken HostPhysMemPages */
443123015Sphk#define AAC_FLAGS_SLAVE	(1 << 9)
444109327Sphk#define AAC_FLAGS_MASTER	(1 << 10)
445123015Sphk#define AAC_FLAGS_NEW_COMM	(1 << 11)	/* New comm. interface supported */
446126370Sphk#define AAC_FLAGS_RAW_IO	(1 << 12)	/* Raw I/O interface */
447109327Sphk#define AAC_FLAGS_ARRAY_64BIT	(1 << 13)	/* 64-bit array size */
448123015Sphk#define AAC_FLAGS_LBA_64BIT	(1 << 14)	/* 64-bit LBA support */
449109327Sphk#define AAC_QUEUE_FRZN		(1 << 15)	/* Freeze the processing of
450109327Sphk						 * commands on the queue. */
451101225Sphk#define AAC_FLAGS_NEW_COMM_TYPE1 (1 << 16)	/* New comm. type1 supported */
452101225Sphk#define AAC_FLAGS_NEW_COMM_TYPE2 (1 << 17)	/* New comm. type2 supported */
453126370Sphk#define AAC_FLAGS_NEW_COMM_TYPE34 (1 << 18)	/* New comm. type3/4 */
454126370Sphk#define AAC_FLAGS_SYNC_MODE (1 << 18)	/* Sync. transfer mode */
455126370Sphk	u_int32_t		hint_flags;		/* driver parameters */
456126370Sphk	int	sim_freezed;				/* flag for sim_freeze/release */
457126370Sphk	u_int32_t		supported_options;
458126370Sphk	u_int32_t		scsi_method_id;
459126370Sphk	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
460126370Sphk
461126370Sphk	u_int32_t	aac_max_fibs;           /* max. FIB count */
462126370Sphk	u_int32_t	aac_max_fibs_alloc;		/* max. alloc. per alloc_commands() */
463126370Sphk	u_int32_t	aac_max_fib_size;		/* max. FIB size */
464126370Sphk	u_int32_t	aac_sg_tablesize;		/* max. sg count from host */
465148231Sphk	u_int32_t	aac_max_sectors;		/* max. I/O size from host (blocks) */
466148231Sphk	u_int32_t	aac_feature_bits;		/* feature bits from suppl. info */
467148231Sphk	u_int32_t	aac_support_opt2;		/* supp. options from suppl. info */
468148231Sphk	u_int32_t	aac_max_aif;			/* max. AIF count */
469148231Sphk#define AAC_CAM_TARGET_WILDCARD ~0
470126370Sphk	void			(*cam_rescan_cb)(struct aac_softc *, uint32_t,
471126370Sphk				    uint32_t);
472126370Sphk	u_int32_t	DebugFlags;		/* Debug print flags bitmap */
473126370Sphk	u_int32_t	DebugOffset;		/* Offset from DPMEM start */
474126370Sphk	u_int32_t	DebugHeaderSize;	/* Size of debug header */
475126370Sphk	u_int32_t	FwDebugFlags;		/* FW Debug Flags */
476126370Sphk	u_int32_t	FwDebugBufferSize;	/* FW Debug Buffer size */
477126370Sphk};
478126370Sphk
479126370Sphk/*
480126370Sphk * Event callback mechanism for the driver
481126370Sphk */
482126370Sphk#define AAC_EVENT_NONE		0x00
483126370Sphk#define AAC_EVENT_CMFREE	0x01
484126370Sphk#define	AAC_EVENT_MASK		0xff
485126370Sphk#define AAC_EVENT_REPEAT	0x100
486126370Sphk
487148231Sphktypedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
488148231Sphk    void *arg);
489148231Sphkstruct aac_event {
490126370Sphk	TAILQ_ENTRY(aac_event)	ev_links;
491126370Sphk	int			ev_type;
492126370Sphk	aac_event_cb_t		*ev_callback;
493126370Sphk	void			*ev_arg;
494126370Sphk};
495126370Sphk
496126370Sphk/*
497126370Sphk * Public functions
498126370Sphk */
499126370Sphkextern void		aacraid_free(struct aac_softc *sc);
500126370Sphkextern int		aacraid_attach(struct aac_softc *sc);
501126370Sphkextern int		aacraid_detach(device_t dev);
502126370Sphkextern int		aacraid_shutdown(device_t dev);
503126370Sphkextern int		aacraid_suspend(device_t dev);
504126370Sphkextern int		aacraid_resume(device_t dev);
505126370Sphkextern void		aacraid_new_intr_type1(void *arg);
506extern void		aacraid_submit_bio(struct bio *bp);
507extern void		aacraid_biodone(struct bio *bp);
508extern void		aacraid_startio(struct aac_softc *sc);
509extern int		aacraid_alloc_command(struct aac_softc *sc,
510					  struct aac_command **cmp);
511extern void		aacraid_release_command(struct aac_command *cm);
512extern void		aacraid_add_event(struct aac_softc *sc, struct aac_event
513				      *event);
514extern void		aacraid_map_command_sg(void *arg, bus_dma_segment_t *segs,
515				int nseg, int error);
516extern int		aacraid_wait_command(struct aac_command *cmp);
517
518/* #define AACRAID_DEBUG */
519
520#ifdef AACRAID_DEBUG
521# define fwprintf(sc, flags, fmt, args...)				\
522	aacraid_fw_printf(sc, flags, "%s: " fmt, __func__, ##args);
523
524extern void	aacraid_print_queues(struct aac_softc *sc);
525extern void	aacraid_print_fib(struct aac_softc *sc, struct aac_fib *fib,
526			      const char *caller);
527extern void	aacraid_print_aif(struct aac_softc *sc,
528			      struct aac_aif_command *aif);
529
530#define AAC_PRINT_FIB(sc, fib)	aacraid_print_fib(sc, fib, __func__)
531
532#else
533# define fwprintf(sc, flags, fmt, args...)
534
535# define aacraid_print_queues(sc)
536
537# define AAC_PRINT_FIB(sc, fib)
538# define aacraid_print_aif(sc, aac_aif_command)
539#endif
540
541struct aac_code_lookup {
542	char	*string;
543	u_int32_t	code;
544};
545
546/*
547 * Queue primitives for driver queues.
548 */
549#define AACQ_ADD(sc, qname)					\
550	do {							\
551		struct aac_qstat *qs;				\
552								\
553		qs = &(sc)->aac_qstat[qname];			\
554								\
555		qs->q_length++;					\
556		if (qs->q_length > qs->q_max)			\
557			qs->q_max = qs->q_length;		\
558	} while (0)
559
560#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
561#define AACQ_INIT(sc, qname)				\
562	do {						\
563		sc->aac_qstat[qname].q_length = 0;	\
564		sc->aac_qstat[qname].q_max = 0;		\
565	} while (0)
566
567
568#define AACQ_COMMAND_QUEUE(name, index)					\
569static __inline void							\
570aac_initq_ ## name (struct aac_softc *sc)				\
571{									\
572	TAILQ_INIT(&sc->aac_ ## name);					\
573	AACQ_INIT(sc, index);						\
574}									\
575static __inline void							\
576aac_enqueue_ ## name (struct aac_command *cm)				\
577{									\
578	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
579		printf("command %p is on another queue, flags = %#x\n",	\
580		       cm, cm->cm_flags);				\
581		panic("command is on another queue");			\
582	}								\
583	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
584	cm->cm_flags |= AAC_ON_ ## index;				\
585	AACQ_ADD(cm->cm_sc, index);					\
586}									\
587static __inline void							\
588aac_requeue_ ## name (struct aac_command *cm)				\
589{									\
590	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
591		printf("command %p is on another queue, flags = %#x\n",	\
592		       cm, cm->cm_flags);				\
593		panic("command is on another queue");			\
594	}								\
595	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
596	cm->cm_flags |= AAC_ON_ ## index;				\
597	AACQ_ADD(cm->cm_sc, index);					\
598}									\
599static __inline struct aac_command *					\
600aac_dequeue_ ## name (struct aac_softc *sc)				\
601{									\
602	struct aac_command *cm;						\
603									\
604	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
605		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
606			printf("command %p not in queue, flags = %#x, "	\
607		       	       "bit = %#x\n", cm, cm->cm_flags,		\
608			       AAC_ON_ ## index);			\
609			panic("command not in queue");			\
610		}							\
611		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
612		cm->cm_flags &= ~AAC_ON_ ## index;			\
613		AACQ_REMOVE(sc, index);					\
614	}								\
615	return(cm);							\
616}									\
617static __inline void							\
618aac_remove_ ## name (struct aac_command *cm)				\
619{									\
620	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
621		printf("command %p not in queue, flags = %#x, "		\
622		       "bit = %#x\n", cm, cm->cm_flags, 		\
623		       AAC_ON_ ## index);				\
624		panic("command not in queue");				\
625	}								\
626	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
627	cm->cm_flags &= ~AAC_ON_ ## index;				\
628	AACQ_REMOVE(cm->cm_sc, index);					\
629}									\
630struct hack
631
632AACQ_COMMAND_QUEUE(free, AACQ_FREE);
633AACQ_COMMAND_QUEUE(ready, AACQ_READY);
634AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
635
636static __inline void
637aac_print_printf(struct aac_softc *sc)
638{
639	/*
640	 * XXX We have the ability to read the length of the printf string
641	 * from out of the mailboxes.
642	 */
643	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
644		      sc->aac_common->ac_printf);
645	sc->aac_common->ac_printf[0] = 0;
646	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
647}
648
649static __inline int
650aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
651{
652
653	mtx_assert(&sc->aac_io_lock, MA_OWNED);
654	*fib = &sc->aac_common->ac_sync_fib;
655	return (0);
656}
657
658static __inline void
659aac_release_sync_fib(struct aac_softc *sc)
660{
661
662	mtx_assert(&sc->aac_io_lock, MA_OWNED);
663}
664