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