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