aacvar.h revision 200251
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2001 Scott Long
4 * Copyright (c) 2000 BSDi
5 * Copyright (c) 2001 Adaptec, Inc.
6 * All rights reserved.
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, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
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
21 * FOR 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 *	$FreeBSD: head/sys/dev/aac/aacvar.h 200251 2009-12-08 05:35:51Z jkim $
30 */
31
32#include <sys/bio.h>
33#include <sys/callout.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/taskqueue.h>
37#include <sys/selinfo.h>
38#include <geom/geom_disk.h>
39
40#ifndef AAC_DRIVER_BUILD
41# define AAC_DRIVER_BUILD 1
42#endif
43
44/*
45 * Driver Parameter Definitions
46 */
47
48/*
49 * The firmware interface allows for a 16-bit s/g list length.  We limit
50 * ourselves to a reasonable maximum and ensure alignment.
51 */
52#define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
53
54/*
55 * We allocate a small set of FIBs for the adapter to use to send us messages.
56 */
57#define AAC_ADAPTER_FIBS	8
58
59/*
60 * The controller reports status events in AIFs.  We hang on to a number of
61 * these in order to pass them out to user-space management tools.
62 */
63#define AAC_AIFQ_LENGTH		64
64
65/*
66 * Firmware messages are passed in the printf buffer.
67 */
68#define AAC_PRINTF_BUFSIZE	256
69
70/*
71 * We wait this many seconds for the adapter to come ready if it is still
72 * booting
73 */
74#define AAC_BOOT_TIMEOUT	(3 * 60)
75
76/*
77 * Timeout for immediate commands.
78 */
79#define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
80
81/*
82 * Timeout for normal commands
83 */
84#define AAC_CMD_TIMEOUT		120		/* seconds */
85
86/*
87 * Rate at which we periodically check for timed out commands and kick the
88 * controller.
89 */
90#define AAC_PERIODIC_INTERVAL	20		/* seconds */
91
92/*
93 * Per-container data structure
94 */
95struct aac_container
96{
97	struct aac_mntobj		co_mntobj;
98	device_t			co_disk;
99	int				co_found;
100	TAILQ_ENTRY(aac_container)	co_link;
101};
102
103/*
104 * Per-SIM data structure
105 */
106struct aac_sim
107{
108	device_t		sim_dev;
109	int			TargetsPerBus;
110	int			BusNumber;
111	int			InitiatorBusId;
112	struct aac_softc	*aac_sc;
113	TAILQ_ENTRY(aac_sim)	sim_link;
114};
115
116/*
117 * Per-disk structure
118 */
119struct aac_disk
120{
121	device_t			ad_dev;
122	struct aac_softc		*ad_controller;
123	struct aac_container		*ad_container;
124	struct disk			*ad_disk;
125	int				ad_flags;
126#define AAC_DISK_OPEN	(1<<0)
127	int				ad_cylinders;
128	int				ad_heads;
129	int				ad_sectors;
130	u_int64_t			ad_size;
131	int				unit;
132};
133
134/*
135 * Per-command control structure.
136 */
137struct aac_command
138{
139	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
140
141	struct aac_softc	*cm_sc;		/* controller that owns us */
142
143	struct aac_fib		*cm_fib;	/* FIB associated with this
144						 * command */
145	u_int64_t		cm_fibphys;	/* bus address of the FIB */
146	struct bio		*cm_data;	/* pointer to data in kernel
147						 * space */
148	u_int32_t		cm_datalen;	/* data length */
149	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
150	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
151						 * command */
152	int			cm_flags;
153#define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
154						 * mapped */
155#define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
156						 * from controller to host */
157#define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
158						 * from host to controller */
159#define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
160#define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
161#define AAC_ON_AACQ_FREE	(1<<5)
162#define AAC_ON_AACQ_READY	(1<<6)
163#define AAC_ON_AACQ_BUSY	(1<<7)
164#define AAC_ON_AACQ_AIF		(1<<8)
165#define AAC_ON_AACQ_NORM	(1<<10)
166#define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10))
167#define AAC_QUEUE_FRZN		(1<<9)		/* Freeze the processing of
168						 * commands on the queue. */
169
170	void			(* cm_complete)(struct aac_command *cm);
171	void			*cm_private;
172	time_t			cm_timestamp;	/* command creation time */
173	int			cm_queue;
174	int			cm_index;
175};
176
177struct aac_fibmap {
178	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
179	struct aac_fib		*aac_fibs;
180	bus_dmamap_t		aac_fibmap;
181	struct aac_command	*aac_commands;
182};
183
184/*
185 * We gather a number of adapter-visible items into a single structure.
186 *
187 * The ordering of this strucure may be important; we copy the Linux driver:
188 *
189 * Adapter FIBs
190 * Init struct
191 * Queue headers (Comm Area)
192 * Printf buffer
193 *
194 * In addition, we add:
195 * Sync Fib
196 */
197struct aac_common {
198	/* fibs for the controller to send us messages */
199	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
200
201	/* the init structure */
202	struct aac_adapter_init	ac_init;
203
204	/* arena within which the queue structures are kept */
205	u_int8_t		ac_qbuf[sizeof(struct aac_queue_table) +
206				AAC_QUEUE_ALIGN];
207
208	/* buffer for text messages from the controller */
209	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
210
211	/* fib for synchronous commands */
212	struct aac_fib		ac_sync_fib;
213};
214
215/*
216 * Interface operations
217 */
218struct aac_interface
219{
220	int	(*aif_get_fwstatus)(struct aac_softc *sc);
221	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
222	int	(*aif_get_istatus)(struct aac_softc *sc);
223	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
224	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
225				   u_int32_t arg0, u_int32_t arg1,
226				   u_int32_t arg2, u_int32_t arg3);
227	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
228	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
229	int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
230	int (*aif_get_outb_queue)(struct aac_softc *sc);
231	void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
232};
233extern struct aac_interface	aac_rx_interface;
234extern struct aac_interface	aac_sa_interface;
235extern struct aac_interface	aac_fa_interface;
236extern struct aac_interface	aac_rkt_interface;
237
238#define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
239#define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
240#define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
241#define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
242					(mask)))
243#define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
244	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
245	(arg3)))
246#define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
247					(mb)))
248#define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
249					0))
250#define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
251					1))
252#define AAC_SEND_COMMAND(sc, cm)	((sc)->aac_if.aif_send_command((sc), (cm)))
253#define AAC_GET_OUTB_QUEUE(sc)		((sc)->aac_if.aif_get_outb_queue((sc)))
254#define AAC_SET_OUTB_QUEUE(sc, idx)	((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
255
256#define AAC_MEM0_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag0, \
257					sc->aac_bhandle0, reg, val)
258#define AAC_MEM0_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag0, \
259					sc->aac_bhandle0, reg)
260#define AAC_MEM0_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag0, \
261					sc->aac_bhandle0, reg, val)
262#define AAC_MEM0_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag0, \
263					sc->aac_bhandle0, reg)
264#define AAC_MEM0_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag0, \
265					sc->aac_bhandle0, reg, val)
266#define AAC_MEM0_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag0, \
267					sc->aac_bhandle0, reg)
268
269#define AAC_MEM1_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag1, \
270					sc->aac_bhandle1, reg, val)
271#define AAC_MEM1_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag1, \
272					sc->aac_bhandle1, reg)
273#define AAC_MEM1_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag1, \
274					sc->aac_bhandle1, reg, val)
275#define AAC_MEM1_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag1, \
276					sc->aac_bhandle1, reg)
277#define AAC_MEM1_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag1, \
278					sc->aac_bhandle1, reg, val)
279#define AAC_MEM1_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag1, \
280					sc->aac_bhandle1, reg)
281
282/* fib context (IOCTL) */
283struct aac_fib_context {
284	u_int32_t		unique;
285	int			ctx_idx;
286	int			ctx_wrap;
287	struct aac_fib_context *next, *prev;
288};
289
290/*
291 * Per-controller structure.
292 */
293struct aac_softc
294{
295	/* bus connections */
296	device_t		aac_dev;
297	struct resource		*aac_regs_res0, *aac_regs_res1; /* reg. if. window */
298	int			aac_regs_rid0, aac_regs_rid1;		/* resource ID */
299	bus_space_handle_t	aac_bhandle0, aac_bhandle1;		/* bus space handle */
300	bus_space_tag_t		aac_btag0, aac_btag1;		/* bus space tag */
301	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
302	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
303							 * DMA tag */
304	struct resource		*aac_irq;		/* interrupt */
305	int			aac_irq_rid;
306	void			*aac_intr;		/* interrupt handle */
307	eventhandler_tag	eh;
308
309	/* controller features, limits and status */
310	int			aac_state;
311#define AAC_STATE_SUSPEND	(1<<0)
312#define	AAC_STATE_OPEN		(1<<1)
313#define AAC_STATE_INTERRUPTS_ON	(1<<2)
314#define AAC_STATE_AIF_SLEEPER	(1<<3)
315	int			aac_open_cnt;
316	struct FsaRevision		aac_revision;
317
318	/* controller hardware interface */
319	int			aac_hwif;
320#define AAC_HWIF_I960RX		0
321#define AAC_HWIF_STRONGARM	1
322#define AAC_HWIF_FALCON		2
323#define AAC_HWIF_RKT		3
324#define	AAC_HWIF_NARK		4
325#define AAC_HWIF_UNKNOWN	-1
326	bus_dma_tag_t		aac_common_dmat;	/* common structure
327							 * DMA tag */
328	bus_dmamap_t		aac_common_dmamap;	/* common structure
329							 * DMA map */
330	struct aac_common	*aac_common;
331	u_int32_t		aac_common_busaddr;
332	struct aac_interface	aac_if;
333
334	/* command/fib resources */
335	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
336	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
337	u_int			total_fibs;
338	struct aac_command	*aac_commands;
339
340	/* command management */
341	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
342						 * available for reuse */
343	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
344						 * controller resources */
345	TAILQ_HEAD(,aac_command) aac_busy;
346	TAILQ_HEAD(,aac_event)	aac_ev_cmfree;
347	struct bio_queue_head	aac_bioq;
348	struct aac_queue_table	*aac_queues;
349	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
350
351	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
352
353	/* connected containters */
354	TAILQ_HEAD(,aac_container)	aac_container_tqh;
355	struct mtx		aac_container_lock;
356
357	/*
358	 * The general I/O lock.  This protects the sync fib, the lists, the
359	 * queues, and the registers.
360	 */
361	struct mtx		aac_io_lock;
362
363	/* delayed activity infrastructure */
364	struct task		aac_task_complete;	/* deferred-completion
365							 * task */
366	struct intr_config_hook	aac_ich;
367
368	/* management interface */
369	struct cdev *aac_dev_t;
370	struct mtx		aac_aifq_lock;
371	struct aac_fib		aac_aifq[AAC_AIFQ_LENGTH];
372	int			aifq_idx;
373	int			aifq_filled;
374	struct aac_fib_context *fibctx;
375	struct selinfo		rcv_select;
376	struct proc		*aifthread;
377	int			aifflags;
378#define AAC_AIFFLAGS_RUNNING	(1 << 0)
379#define AAC_AIFFLAGS_AIF	(1 << 1)
380#define	AAC_AIFFLAGS_EXIT	(1 << 2)
381#define AAC_AIFFLAGS_EXITED	(1 << 3)
382#define AAC_AIFFLAGS_PRINTF	(1 << 4)
383#define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
384#define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
385				 AAC_AIFFLAGS_ALLOCFIBS)
386	u_int32_t		flags;
387#define AAC_FLAGS_PERC2QC	(1 << 0)
388#define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
389#define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
390#define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
391#define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
392#define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
393						 * 2GB-4GB range */
394#define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
395#define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
396#define	AAC_FLAGS_BROKEN_MEMMAP (1 << 8)	/* Broken HostPhysMemPages */
397#define AAC_FLAGS_SLAVE	(1 << 9)
398#define AAC_FLAGS_MASTER	(1 << 10)
399#define AAC_FLAGS_NEW_COMM	(1 << 11)	/* New comm. interface supported */
400#define AAC_FLAGS_RAW_IO	(1 << 12)	/* Raw I/O interface */
401#define AAC_FLAGS_ARRAY_64BIT	(1 << 13)	/* 64-bit array size */
402#define	AAC_FLAGS_LBA_64BIT	(1 << 14)	/* 64-bit LBA support */
403
404	u_int32_t		supported_options;
405	u_int32_t		scsi_method_id;
406	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
407
408	struct callout	aac_daemontime;		/* clock daemon callout */
409
410	u_int32_t	aac_max_fibs;           /* max. FIB count */
411	u_int32_t	aac_max_fibs_alloc;		/* max. alloc. per alloc_commands() */
412	u_int32_t	aac_max_fib_size;		/* max. FIB size */
413	u_int32_t	aac_sg_tablesize;		/* max. sg count from host */
414	u_int32_t	aac_max_sectors;		/* max. I/O size from host (blocks) */
415};
416
417/*
418 * Event callback mechanism for the driver
419 */
420#define AAC_EVENT_NONE		0x00
421#define AAC_EVENT_CMFREE	0x01
422#define	AAC_EVENT_MASK		0xff
423#define AAC_EVENT_REPEAT	0x100
424
425typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
426    void *arg);
427struct aac_event {
428	TAILQ_ENTRY(aac_event)	ev_links;
429	int			ev_type;
430	aac_event_cb_t		*ev_callback;
431	void			*ev_arg;
432};
433
434/*
435 * Public functions
436 */
437extern void		aac_free(struct aac_softc *sc);
438extern int		aac_attach(struct aac_softc *sc);
439extern int		aac_detach(device_t dev);
440extern int		aac_shutdown(device_t dev);
441extern int		aac_suspend(device_t dev);
442extern int		aac_resume(device_t dev);
443extern void		aac_new_intr(void *arg);
444extern int		aac_filter(void *arg);
445extern void		aac_submit_bio(struct bio *bp);
446extern void		aac_biodone(struct bio *bp);
447extern void		aac_startio(struct aac_softc *sc);
448extern int		aac_alloc_command(struct aac_softc *sc,
449					  struct aac_command **cmp);
450extern void		aac_release_command(struct aac_command *cm);
451extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
452				     u_int32_t xferstate, struct aac_fib *fib,
453				     u_int16_t datasize);
454extern void		aac_add_event(struct aac_softc *sc, struct aac_event
455				      *event);
456
457#ifdef AAC_DEBUG
458extern int	aac_debug_enable;
459# define fwprintf(sc, flags, fmt, args...)				\
460do {									\
461	if (!aac_debug_enable)						\
462		break;							\
463	if (sc != NULL)							\
464		device_printf(((struct aac_softc *)sc)->aac_dev,	\
465		    "%s: " fmt "\n", __func__, ##args);			\
466	else								\
467		printf("%s: " fmt "\n", __func__, ##args);		\
468} while(0)
469
470extern void	aac_print_queues(struct aac_softc *sc);
471extern void	aac_panic(struct aac_softc *sc, char *reason);
472extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
473			      const char *caller);
474extern void	aac_print_aif(struct aac_softc *sc,
475			      struct aac_aif_command *aif);
476
477#define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
478
479#else
480# define fwprintf(sc, flags, fmt, args...)
481
482# define aac_print_queues(sc)
483# define aac_panic(sc, reason)
484
485# define AAC_PRINT_FIB(sc, fib)
486# define aac_print_aif(sc, aac_aif_command)
487#endif
488
489struct aac_code_lookup {
490	char	*string;
491	u_int32_t	code;
492};
493
494/*
495 * Queue primitives for driver queues.
496 */
497#define AACQ_ADD(sc, qname)					\
498	do {							\
499		struct aac_qstat *qs;				\
500								\
501		qs = &(sc)->aac_qstat[qname];			\
502								\
503		qs->q_length++;					\
504		if (qs->q_length > qs->q_max)			\
505			qs->q_max = qs->q_length;		\
506	} while (0)
507
508#define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
509#define AACQ_INIT(sc, qname)				\
510	do {						\
511		sc->aac_qstat[qname].q_length = 0;	\
512		sc->aac_qstat[qname].q_max = 0;		\
513	} while (0)
514
515
516#define AACQ_COMMAND_QUEUE(name, index)					\
517static __inline void							\
518aac_initq_ ## name (struct aac_softc *sc)				\
519{									\
520	TAILQ_INIT(&sc->aac_ ## name);					\
521	AACQ_INIT(sc, index);						\
522}									\
523static __inline void							\
524aac_enqueue_ ## name (struct aac_command *cm)				\
525{									\
526	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
527		printf("command %p is on another queue, flags = %#x\n",	\
528		       cm, cm->cm_flags);				\
529		panic("command is on another queue");			\
530	}								\
531	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
532	cm->cm_flags |= AAC_ON_ ## index;				\
533	AACQ_ADD(cm->cm_sc, index);					\
534}									\
535static __inline void							\
536aac_requeue_ ## name (struct aac_command *cm)				\
537{									\
538	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
539		printf("command %p is on another queue, flags = %#x\n",	\
540		       cm, cm->cm_flags);				\
541		panic("command is on another queue");			\
542	}								\
543	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
544	cm->cm_flags |= AAC_ON_ ## index;				\
545	AACQ_ADD(cm->cm_sc, index);					\
546}									\
547static __inline struct aac_command *					\
548aac_dequeue_ ## name (struct aac_softc *sc)				\
549{									\
550	struct aac_command *cm;						\
551									\
552	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
553		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
554			printf("command %p not in queue, flags = %#x, "	\
555		       	       "bit = %#x\n", cm, cm->cm_flags,		\
556			       AAC_ON_ ## index);			\
557			panic("command not in queue");			\
558		}							\
559		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
560		cm->cm_flags &= ~AAC_ON_ ## index;			\
561		AACQ_REMOVE(sc, index);					\
562	}								\
563	return(cm);							\
564}									\
565static __inline void							\
566aac_remove_ ## name (struct aac_command *cm)				\
567{									\
568	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
569		printf("command %p not in queue, flags = %#x, "		\
570		       "bit = %#x\n", cm, cm->cm_flags, 		\
571		       AAC_ON_ ## index);				\
572		panic("command not in queue");				\
573	}								\
574	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
575	cm->cm_flags &= ~AAC_ON_ ## index;				\
576	AACQ_REMOVE(cm->cm_sc, index);					\
577}									\
578struct hack
579
580AACQ_COMMAND_QUEUE(free, AACQ_FREE);
581AACQ_COMMAND_QUEUE(ready, AACQ_READY);
582AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
583
584/*
585 * outstanding bio queue
586 */
587static __inline void
588aac_initq_bio(struct aac_softc *sc)
589{
590	bioq_init(&sc->aac_bioq);
591	AACQ_INIT(sc, AACQ_BIO);
592}
593
594static __inline void
595aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
596{
597	bioq_insert_tail(&sc->aac_bioq, bp);
598	AACQ_ADD(sc, AACQ_BIO);
599}
600
601static __inline struct bio *
602aac_dequeue_bio(struct aac_softc *sc)
603{
604	struct bio *bp;
605
606	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
607		bioq_remove(&sc->aac_bioq, bp);
608		AACQ_REMOVE(sc, AACQ_BIO);
609	}
610	return(bp);
611}
612
613static __inline void
614aac_print_printf(struct aac_softc *sc)
615{
616	/*
617	 * XXX We have the ability to read the length of the printf string
618	 * from out of the mailboxes.
619	 */
620	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
621		      sc->aac_common->ac_printf);
622	sc->aac_common->ac_printf[0] = 0;
623	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
624}
625
626static __inline int
627aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
628{
629
630	mtx_assert(&sc->aac_io_lock, MA_OWNED);
631	*fib = &sc->aac_common->ac_sync_fib;
632	return (0);
633}
634
635static __inline void
636aac_release_sync_fib(struct aac_softc *sc)
637{
638
639	mtx_assert(&sc->aac_io_lock, MA_OWNED);
640}
641
642