aacvar.h revision 212661
1295367Sdes/*-
2218767Sdes * Copyright (c) 2000 Michael Smith
3218767Sdes * Copyright (c) 2001 Scott Long
4218767Sdes * Copyright (c) 2000 BSDi
5218767Sdes * Copyright (c) 2001 Adaptec, Inc.
6218767Sdes * All rights reserved.
7218767Sdes *
8218767Sdes * Redistribution and use in source and binary forms, with or without
9218767Sdes * modification, are permitted provided that the following conditions
10218767Sdes * are met:
11218767Sdes * 1. Redistributions of source code must retain the above copyright
12218767Sdes *    notice, this list of conditions and the following disclaimer.
13218767Sdes * 2. Redistributions in binary form must reproduce the above copyright
14218767Sdes *    notice, this list of conditions and the following disclaimer in the
15218767Sdes *    documentation and/or other materials provided with the distribution.
16218767Sdes *
17218767Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18218767Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19218767Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20218767Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21218767Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22218767Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23218767Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24218767Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25218767Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26218767Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27218767Sdes * SUCH DAMAGE.
28218767Sdes *
29295367Sdes *	$FreeBSD: head/sys/dev/aac/aacvar.h 212661 2010-09-15 14:24:21Z attilio $
30295367Sdes */
31218767Sdes
32218767Sdes#include <sys/bio.h>
33218767Sdes#include <sys/callout.h>
34218767Sdes#include <sys/lock.h>
35295367Sdes#include <sys/mutex.h>
36295367Sdes#include <sys/taskqueue.h>
37295367Sdes#include <sys/selinfo.h>
38218767Sdes#include <geom/geom_disk.h>
39295367Sdes
40218767Sdes#define	AAC_TYPE_DEVO			1
41218767Sdes#define	AAC_TYPE_ALPHA			2
42218767Sdes#define	AAC_TYPE_BETA			3
43218767Sdes#define	AAC_TYPE_RELEASE		4
44218767Sdes
45295367Sdes#define	AAC_DRIVER_MAJOR_VERSION	2
46295367Sdes#define	AAC_DRIVER_MINOR_VERSION	1
47295367Sdes#define	AAC_DRIVER_BUGFIX_LEVEL		9
48295367Sdes#define	AAC_DRIVER_TYPE			AAC_TYPE_RELEASE
49218767Sdes
50295367Sdes#ifndef AAC_DRIVER_BUILD
51218767Sdes# define AAC_DRIVER_BUILD 1
52295367Sdes#endif
53295367Sdes
54218767Sdes/*
55295367Sdes * Driver Parameter Definitions
56295367Sdes */
57295367Sdes
58295367Sdes/*
59295367Sdes * The firmware interface allows for a 16-bit s/g list length.  We limit
60295367Sdes * ourselves to a reasonable maximum and ensure alignment.
61295367Sdes */
62295367Sdes#define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
63295367Sdes
64295367Sdes/*
65218767Sdes * We allocate a small set of FIBs for the adapter to use to send us messages.
66295367Sdes */
67218767Sdes#define AAC_ADAPTER_FIBS	8
68295367Sdes
69295367Sdes/*
70295367Sdes * The controller reports status events in AIFs.  We hang on to a number of
71218767Sdes * these in order to pass them out to user-space management tools.
72295367Sdes */
73295367Sdes#define AAC_AIFQ_LENGTH		64
74295367Sdes
75295367Sdes/*
76295367Sdes * Firmware messages are passed in the printf buffer.
77218767Sdes */
78295367Sdes#define AAC_PRINTF_BUFSIZE	256
79295367Sdes
80295367Sdes/*
81295367Sdes * We wait this many seconds for the adapter to come ready if it is still
82295367Sdes * booting
83295367Sdes */
84295367Sdes#define AAC_BOOT_TIMEOUT	(3 * 60)
85295367Sdes
86218767Sdes/*
87218767Sdes * Timeout for immediate commands.
88218767Sdes */
89218767Sdes#define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
90295367Sdes
91218767Sdes/*
92218767Sdes * Timeout for normal commands
93218767Sdes */
94295367Sdes#define AAC_CMD_TIMEOUT		120		/* seconds */
95295367Sdes
96295367Sdes/*
97295367Sdes * Rate at which we periodically check for timed out commands and kick the
98295367Sdes * controller.
99295367Sdes */
100295367Sdes#define AAC_PERIODIC_INTERVAL	20		/* seconds */
101295367Sdes
102295367Sdes/*
103295367Sdes * Per-container data structure
104295367Sdes */
105295367Sdesstruct aac_container
106295367Sdes{
107295367Sdes	struct aac_mntobj		co_mntobj;
108295367Sdes	device_t			co_disk;
109295367Sdes	int				co_found;
110295367Sdes	TAILQ_ENTRY(aac_container)	co_link;
111295367Sdes};
112295367Sdes
113218767Sdes/*
114218767Sdes * Per-SIM data structure
115218767Sdes */
116295367Sdesstruct aac_sim
117218767Sdes{
118295367Sdes	device_t		sim_dev;
119295367Sdes	int			TargetsPerBus;
120295367Sdes	int			BusNumber;
121295367Sdes	int			InitiatorBusId;
122295367Sdes	struct aac_softc	*aac_sc;
123218767Sdes	TAILQ_ENTRY(aac_sim)	sim_link;
124218767Sdes};
125218767Sdes
126295367Sdes/*
127295367Sdes * Per-disk structure
128295367Sdes */
129295367Sdesstruct aac_disk
130295367Sdes{
131218767Sdes	device_t			ad_dev;
132295367Sdes	struct aac_softc		*ad_controller;
133295367Sdes	struct aac_container		*ad_container;
134295367Sdes	struct disk			*ad_disk;
135295367Sdes	int				ad_flags;
136295367Sdes#define AAC_DISK_OPEN	(1<<0)
137218767Sdes	int				ad_cylinders;
138295367Sdes	int				ad_heads;
139218767Sdes	int				ad_sectors;
140218767Sdes	u_int64_t			ad_size;
141218767Sdes	int				unit;
142295367Sdes};
143295367Sdes
144295367Sdes/*
145295367Sdes * Per-command control structure.
146295367Sdes */
147262566Sdesstruct aac_command
148218767Sdes{
149218767Sdes	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
150218767Sdes
151295367Sdes	struct aac_softc	*cm_sc;		/* controller that owns us */
152295367Sdes
153218767Sdes	struct aac_fib		*cm_fib;	/* FIB associated with this
154218767Sdes						 * command */
155218767Sdes	u_int64_t		cm_fibphys;	/* bus address of the FIB */
156218767Sdes	struct bio		*cm_data;	/* pointer to data in kernel
157295367Sdes						 * space */
158295367Sdes	u_int32_t		cm_datalen;	/* data length */
159218767Sdes	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
160218767Sdes	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
161218767Sdes						 * command */
162218767Sdes	int			cm_flags;
163295367Sdes#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
164295367Sdes						 * mapped */
165295367Sdes#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
166295367Sdes						 * from controller to host */
167295367Sdes#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
168218767Sdes						 * from host to controller */
169218767Sdes#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
170218767Sdes#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
171218767Sdes#define AAC_ON_AACQ_FREE	(1<<5)
172295367Sdes#define AAC_ON_AACQ_READY	(1<<6)
173295367Sdes#define AAC_ON_AACQ_BUSY	(1<<7)
174295367Sdes#define AAC_ON_AACQ_AIF		(1<<8)
175218767Sdes#define AAC_ON_AACQ_NORM	(1<<10)
176218767Sdes#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10))
177218767Sdes#define AAC_QUEUE_FRZN		(1<<9)		/* Freeze the processing of
178295367Sdes						 * commands on the queue. */
179218767Sdes
180295367Sdes	void			(* cm_complete)(struct aac_command *cm);
181295367Sdes	void			*cm_private;
182295367Sdes	time_t			cm_timestamp;	/* command creation time */
183295367Sdes	int			cm_queue;
184295367Sdes	int			cm_index;
185295367Sdes};
186218767Sdes
187295367Sdesstruct aac_fibmap {
188295367Sdes	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
189295367Sdes	struct aac_fib		*aac_fibs;
190295367Sdes	bus_dmamap_t		aac_fibmap;
191295367Sdes	struct aac_command	*aac_commands;
192295367Sdes};
193295367Sdes
194295367Sdes/*
195295367Sdes * We gather a number of adapter-visible items into a single structure.
196295367Sdes *
197295367Sdes * The ordering of this strucure may be important; we copy the Linux driver:
198295367Sdes *
199295367Sdes * Adapter FIBs
200295367Sdes * Init struct
201295367Sdes * Queue headers (Comm Area)
202295367Sdes * Printf buffer
203295367Sdes *
204255767Sdes * In addition, we add:
205295367Sdes * Sync Fib
206295367Sdes */
207295367Sdesstruct aac_common {
208218767Sdes	/* fibs for the controller to send us messages */
209	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
210
211	/* the init structure */
212	struct aac_adapter_init	ac_init;
213
214	/* arena within which the queue structures are kept */
215	u_int8_t		ac_qbuf[sizeof(struct aac_queue_table) +
216				AAC_QUEUE_ALIGN];
217
218	/* buffer for text messages from the controller */
219	char			ac_printf[AAC_PRINTF_BUFSIZE];
220
221	/* fib for synchronous commands */
222	struct aac_fib		ac_sync_fib;
223};
224
225/*
226 * Interface operations
227 */
228struct aac_interface
229{
230	int	(*aif_get_fwstatus)(struct aac_softc *sc);
231	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
232	int	(*aif_get_istatus)(struct aac_softc *sc);
233	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
234	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
235				   u_int32_t arg0, u_int32_t arg1,
236				   u_int32_t arg2, u_int32_t arg3);
237	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
238	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
239	int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
240	int (*aif_get_outb_queue)(struct aac_softc *sc);
241	void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
242};
243extern struct aac_interface	aac_rx_interface;
244extern struct aac_interface	aac_sa_interface;
245extern struct aac_interface	aac_fa_interface;
246extern struct aac_interface	aac_rkt_interface;
247
248#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
249#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
250#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
251#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
252					(mask)))
253#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
254	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
255	(arg3)))
256#define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
257					(mb)))
258#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
259					0))
260#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
261					1))
262#define AAC_SEND_COMMAND(sc, cm)	((sc)->aac_if.aif_send_command((sc), (cm)))
263#define AAC_GET_OUTB_QUEUE(sc)		((sc)->aac_if.aif_get_outb_queue((sc)))
264#define AAC_SET_OUTB_QUEUE(sc, idx)	((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
265
266#define AAC_MEM0_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag0, \
267					sc->aac_bhandle0, reg, val)
268#define AAC_MEM0_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag0, \
269					sc->aac_bhandle0, reg)
270#define AAC_MEM0_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag0, \
271					sc->aac_bhandle0, reg, val)
272#define AAC_MEM0_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag0, \
273					sc->aac_bhandle0, reg)
274#define AAC_MEM0_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag0, \
275					sc->aac_bhandle0, reg, val)
276#define AAC_MEM0_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag0, \
277					sc->aac_bhandle0, reg)
278
279#define AAC_MEM1_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag1, \
280					sc->aac_bhandle1, reg, val)
281#define AAC_MEM1_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag1, \
282					sc->aac_bhandle1, reg)
283#define AAC_MEM1_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag1, \
284					sc->aac_bhandle1, reg, val)
285#define AAC_MEM1_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag1, \
286					sc->aac_bhandle1, reg)
287#define AAC_MEM1_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag1, \
288					sc->aac_bhandle1, reg, val)
289#define AAC_MEM1_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag1, \
290					sc->aac_bhandle1, reg)
291
292/* fib context (IOCTL) */
293struct aac_fib_context {
294	u_int32_t		unique;
295	int			ctx_idx;
296	int			ctx_wrap;
297	struct aac_fib_context *next, *prev;
298};
299
300/*
301 * Per-controller structure.
302 */
303struct aac_softc
304{
305	/* bus connections */
306	device_t		aac_dev;
307	struct resource		*aac_regs_res0, *aac_regs_res1; /* reg. if. window */
308	int			aac_regs_rid0, aac_regs_rid1;		/* resource ID */
309	bus_space_handle_t	aac_bhandle0, aac_bhandle1;		/* bus space handle */
310	bus_space_tag_t		aac_btag0, aac_btag1;		/* bus space tag */
311	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
312	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
313							 * DMA tag */
314	struct resource		*aac_irq;		/* interrupt */
315	int			aac_irq_rid;
316	void			*aac_intr;		/* interrupt handle */
317	eventhandler_tag	eh;
318
319	/* controller features, limits and status */
320	int			aac_state;
321#define AAC_STATE_SUSPEND	(1<<0)
322#define	AAC_STATE_UNUSED0	(1<<1)
323#define AAC_STATE_INTERRUPTS_ON	(1<<2)
324#define AAC_STATE_AIF_SLEEPER	(1<<3)
325	struct FsaRevision		aac_revision;
326
327	/* controller hardware interface */
328	int			aac_hwif;
329#define AAC_HWIF_I960RX		0
330#define AAC_HWIF_STRONGARM	1
331#define AAC_HWIF_RKT		3
332#define	AAC_HWIF_NARK		4
333#define AAC_HWIF_UNKNOWN	-1
334	bus_dma_tag_t		aac_common_dmat;	/* common structure
335							 * DMA tag */
336	bus_dmamap_t		aac_common_dmamap;	/* common structure
337							 * DMA map */
338	struct aac_common	*aac_common;
339	u_int32_t		aac_common_busaddr;
340	struct aac_interface	aac_if;
341
342	/* command/fib resources */
343	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
344	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
345	u_int			total_fibs;
346	struct aac_command	*aac_commands;
347
348	/* command management */
349	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
350						 * available for reuse */
351	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
352						 * controller resources */
353	TAILQ_HEAD(,aac_command) aac_busy;
354	TAILQ_HEAD(,aac_event)	aac_ev_cmfree;
355	struct bio_queue_head	aac_bioq;
356	struct aac_queue_table	*aac_queues;
357	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
358
359	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
360
361	/* connected containters */
362	TAILQ_HEAD(,aac_container)	aac_container_tqh;
363	struct mtx		aac_container_lock;
364
365	/*
366	 * The general I/O lock.  This protects the sync fib, the lists, the
367	 * queues, and the registers.
368	 */
369	struct mtx		aac_io_lock;
370
371	/* delayed activity infrastructure */
372	struct task		aac_task_complete;	/* deferred-completion
373							 * task */
374	struct intr_config_hook	aac_ich;
375
376	/* management interface */
377	struct cdev *aac_dev_t;
378	struct mtx		aac_aifq_lock;
379	struct aac_fib		aac_aifq[AAC_AIFQ_LENGTH];
380	int			aifq_idx;
381	int			aifq_filled;
382	struct aac_fib_context *fibctx;
383	struct selinfo		rcv_select;
384	struct proc		*aifthread;
385	int			aifflags;
386#define AAC_AIFFLAGS_RUNNING	(1 << 0)
387#define AAC_AIFFLAGS_AIF	(1 << 1)
388#define	AAC_AIFFLAGS_EXIT	(1 << 2)
389#define AAC_AIFFLAGS_EXITED	(1 << 3)
390#define AAC_AIFFLAGS_PRINTF	(1 << 4)
391#define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
392#define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
393				 AAC_AIFFLAGS_ALLOCFIBS)
394	u_int32_t		flags;
395#define AAC_FLAGS_PERC2QC	(1 << 0)
396#define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
397#define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
398#define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
399#define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
400#define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
401						 * 2GB-4GB range */
402#define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
403#define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
404#define	AAC_FLAGS_BROKEN_MEMMAP (1 << 8)	/* Broken HostPhysMemPages */
405#define AAC_FLAGS_SLAVE	(1 << 9)
406#define AAC_FLAGS_MASTER	(1 << 10)
407#define AAC_FLAGS_NEW_COMM	(1 << 11)	/* New comm. interface supported */
408#define AAC_FLAGS_RAW_IO	(1 << 12)	/* Raw I/O interface */
409#define AAC_FLAGS_ARRAY_64BIT	(1 << 13)	/* 64-bit array size */
410#define	AAC_FLAGS_LBA_64BIT	(1 << 14)	/* 64-bit LBA support */
411
412	u_int32_t		supported_options;
413	u_int32_t		scsi_method_id;
414	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
415
416	struct callout	aac_daemontime;		/* clock daemon callout */
417
418	u_int32_t	aac_max_fibs;		/* max. FIB count */
419	u_int32_t	aac_max_fibs_alloc;		/* max. alloc. per alloc_commands() */
420	u_int32_t	aac_max_fib_size;		/* max. FIB size */
421	u_int32_t	aac_sg_tablesize;		/* max. sg count from host */
422	u_int32_t	aac_max_sectors;		/* max. I/O size from host (blocks) */
423};
424
425/*
426 * Event callback mechanism for the driver
427 */
428#define AAC_EVENT_NONE		0x00
429#define AAC_EVENT_CMFREE	0x01
430#define	AAC_EVENT_MASK		0xff
431#define AAC_EVENT_REPEAT	0x100
432
433typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
434    void *arg);
435struct aac_event {
436	TAILQ_ENTRY(aac_event)	ev_links;
437	int			ev_type;
438	aac_event_cb_t		*ev_callback;
439	void			*ev_arg;
440};
441
442/*
443 * Public functions
444 */
445extern void		aac_free(struct aac_softc *sc);
446extern int		aac_attach(struct aac_softc *sc);
447extern int		aac_detach(device_t dev);
448extern int		aac_shutdown(device_t dev);
449extern int		aac_suspend(device_t dev);
450extern int		aac_resume(device_t dev);
451extern void		aac_new_intr(void *arg);
452extern int		aac_filter(void *arg);
453extern void		aac_submit_bio(struct bio *bp);
454extern void		aac_biodone(struct bio *bp);
455extern void		aac_startio(struct aac_softc *sc);
456extern int		aac_alloc_command(struct aac_softc *sc,
457					  struct aac_command **cmp);
458extern void		aac_release_command(struct aac_command *cm);
459extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
460				     u_int32_t xferstate, struct aac_fib *fib,
461				     u_int16_t datasize);
462extern void		aac_add_event(struct aac_softc *sc, struct aac_event
463				      *event);
464
465#ifdef AAC_DEBUG
466extern int	aac_debug_enable;
467# define fwprintf(sc, flags, fmt, args...)				\
468do {									\
469	if (!aac_debug_enable)						\
470		break;							\
471	if (sc != NULL)							\
472		device_printf(((struct aac_softc *)sc)->aac_dev,	\
473		    "%s: " fmt "\n", __func__, ##args);			\
474	else								\
475		printf("%s: " fmt "\n", __func__, ##args);		\
476} while(0)
477
478extern void	aac_print_queues(struct aac_softc *sc);
479extern void	aac_panic(struct aac_softc *sc, char *reason);
480extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
481			      const char *caller);
482extern void	aac_print_aif(struct aac_softc *sc,
483			      struct aac_aif_command *aif);
484
485#define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
486
487#else
488# define fwprintf(sc, flags, fmt, args...)
489
490# define aac_print_queues(sc)
491# define aac_panic(sc, reason)
492
493# define AAC_PRINT_FIB(sc, fib)
494# define aac_print_aif(sc, aac_aif_command)
495#endif
496
497struct aac_code_lookup {
498	char	*string;
499	u_int32_t	code;
500};
501
502/*
503 * Queue primitives for driver queues.
504 */
505#define AACQ_ADD(sc, qname)					\
506	do {							\
507		struct aac_qstat *qs;				\
508								\
509		qs = &(sc)->aac_qstat[qname];			\
510								\
511		qs->q_length++;					\
512		if (qs->q_length > qs->q_max)			\
513			qs->q_max = qs->q_length;		\
514	} while (0)
515
516#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
517#define AACQ_INIT(sc, qname)				\
518	do {						\
519		sc->aac_qstat[qname].q_length = 0;	\
520		sc->aac_qstat[qname].q_max = 0;		\
521	} while (0)
522
523
524#define AACQ_COMMAND_QUEUE(name, index)					\
525static __inline void							\
526aac_initq_ ## name (struct aac_softc *sc)				\
527{									\
528	TAILQ_INIT(&sc->aac_ ## name);					\
529	AACQ_INIT(sc, index);						\
530}									\
531static __inline void							\
532aac_enqueue_ ## name (struct aac_command *cm)				\
533{									\
534	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
535		printf("command %p is on another queue, flags = %#x\n",	\
536		       cm, cm->cm_flags);				\
537		panic("command is on another queue");			\
538	}								\
539	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
540	cm->cm_flags |= AAC_ON_ ## index;				\
541	AACQ_ADD(cm->cm_sc, index);					\
542}									\
543static __inline void							\
544aac_requeue_ ## name (struct aac_command *cm)				\
545{									\
546	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
547		printf("command %p is on another queue, flags = %#x\n",	\
548		       cm, cm->cm_flags);				\
549		panic("command is on another queue");			\
550	}								\
551	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
552	cm->cm_flags |= AAC_ON_ ## index;				\
553	AACQ_ADD(cm->cm_sc, index);					\
554}									\
555static __inline struct aac_command *					\
556aac_dequeue_ ## name (struct aac_softc *sc)				\
557{									\
558	struct aac_command *cm;						\
559									\
560	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
561		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
562			printf("command %p not in queue, flags = %#x, "	\
563			       "bit = %#x\n", cm, cm->cm_flags,		\
564			       AAC_ON_ ## index);			\
565			panic("command not in queue");			\
566		}							\
567		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
568		cm->cm_flags &= ~AAC_ON_ ## index;			\
569		AACQ_REMOVE(sc, index);					\
570	}								\
571	return(cm);							\
572}									\
573static __inline void							\
574aac_remove_ ## name (struct aac_command *cm)				\
575{									\
576	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
577		printf("command %p not in queue, flags = %#x, "		\
578		       "bit = %#x\n", cm, cm->cm_flags, 		\
579		       AAC_ON_ ## index);				\
580		panic("command not in queue");				\
581	}								\
582	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
583	cm->cm_flags &= ~AAC_ON_ ## index;				\
584	AACQ_REMOVE(cm->cm_sc, index);					\
585}									\
586struct hack
587
588AACQ_COMMAND_QUEUE(free, AACQ_FREE);
589AACQ_COMMAND_QUEUE(ready, AACQ_READY);
590AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
591
592/*
593 * outstanding bio queue
594 */
595static __inline void
596aac_initq_bio(struct aac_softc *sc)
597{
598	bioq_init(&sc->aac_bioq);
599	AACQ_INIT(sc, AACQ_BIO);
600}
601
602static __inline void
603aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
604{
605	bioq_insert_tail(&sc->aac_bioq, bp);
606	AACQ_ADD(sc, AACQ_BIO);
607}
608
609static __inline struct bio *
610aac_dequeue_bio(struct aac_softc *sc)
611{
612	struct bio *bp;
613
614	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
615		bioq_remove(&sc->aac_bioq, bp);
616		AACQ_REMOVE(sc, AACQ_BIO);
617	}
618	return(bp);
619}
620
621static __inline void
622aac_print_printf(struct aac_softc *sc)
623{
624	/*
625	 * XXX We have the ability to read the length of the printf string
626	 * from out of the mailboxes.
627	 */
628	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
629		      sc->aac_common->ac_printf);
630	sc->aac_common->ac_printf[0] = 0;
631	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
632}
633
634static __inline int
635aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
636{
637
638	mtx_assert(&sc->aac_io_lock, MA_OWNED);
639	*fib = &sc->aac_common->ac_sync_fib;
640	return (0);
641}
642
643static __inline void
644aac_release_sync_fib(struct aac_softc *sc)
645{
646
647	mtx_assert(&sc->aac_io_lock, MA_OWNED);
648}
649
650