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