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