xref: /freebsd/sys/dev/aacraid/aacraid_var.h (revision 206b73d0)
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-2010 Adaptec, Inc.
8  * Copyright (c) 2010-2012 PMC-Sierra, Inc.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	$FreeBSD$
33  */
34 
35 #include <sys/bio.h>
36 #if __FreeBSD_version >= 800000
37 #include <sys/callout.h>
38 #endif
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/taskqueue.h>
42 #include <sys/selinfo.h>
43 #include <geom/geom_disk.h>
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	3
51 #define	AAC_DRIVER_MINOR_VERSION	2
52 #define	AAC_DRIVER_BUGFIX_LEVEL		10
53 #define	AAC_DRIVER_TYPE			AAC_TYPE_RELEASE
54 
55 #ifndef AAC_DRIVER_BUILD
56 # define AAC_DRIVER_BUILD 1
57 #endif
58 
59 #if __FreeBSD_version <= 601000
60 #define bus_get_dma_tag(x)	NULL
61 #endif
62 
63 /* **************************** NewBUS interrupt Crock ************************/
64 #if __FreeBSD_version < 700031
65 #define	aac_bus_setup_intr(d, i, f, U, if, ifa, hp)	\
66 	bus_setup_intr(d, i, f, if, ifa, hp)
67 #else
68 #define	aac_bus_setup_intr	bus_setup_intr
69 #endif
70 
71 /* **************************** NewBUS CAM Support ****************************/
72 #if __FreeBSD_version < 700049
73 #define aac_xpt_bus_register(sim, parent, bus)	\
74 	xpt_bus_register(sim, bus)
75 #else
76 #define aac_xpt_bus_register	xpt_bus_register
77 #endif
78 
79 /**************************** Kernel Thread Support ***************************/
80 #if __FreeBSD_version > 800001
81 #define aac_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
82 	kproc_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
83 #define	aac_kthread_exit(status)	\
84 	kproc_exit(status)
85 #else
86 #define aac_kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg) \
87 	kthread_create(func, farg, proc_ptr, flags, stackpgs, fmtstr, arg)
88 #define	aac_kthread_exit(status)	\
89 	kthread_exit(status)
90 #endif
91 
92 /*
93  * Driver Parameter Definitions
94  */
95 
96 /*
97  * We allocate a small set of FIBs for the adapter to use to send us messages.
98  */
99 #define AAC_ADAPTER_FIBS	8
100 
101 /*
102  * The controller reports status events in AIFs.  We hang on to a number of
103  * these in order to pass them out to user-space management tools.
104  */
105 #define AAC_AIFQ_LENGTH		64
106 
107 /*
108  * Firmware messages are passed in the printf buffer.
109  */
110 #define AAC_PRINTF_BUFSIZE	256
111 
112 /*
113  * We wait this many seconds for the adapter to come ready if it is still
114  * booting
115  */
116 #define AAC_BOOT_TIMEOUT	(3 * 60)
117 
118 /*
119  * We wait this many seconds for the adapter to come ready
120  * after flash update
121  */
122 #define AAC_FWUPD_TIMEOUT	(5 * 60)
123 
124 /*
125  * Timeout for sync. commands.
126  */
127 #define AAC_SYNC_TIMEOUT	180		/* seconds */
128 
129 /*
130  * Timeout for normal commands
131  */
132 #define AAC_CMD_TIMEOUT		180		/* seconds */
133 
134 /*
135  * Rate at which we periodically check for timed out commands and kick the
136  * controller.
137  */
138 #define AAC_PERIODIC_INTERVAL	20		/* seconds */
139 
140 #define PASSTHROUGH_BUS		0
141 #define CONTAINER_BUS		1
142 /*
143  * Per-container data structure
144  */
145 struct aac_container
146 {
147 	struct aac_mntobj		co_mntobj;
148 	int				co_found;
149 	u_int32_t		co_uid;
150 	TAILQ_ENTRY(aac_container)	co_link;
151 };
152 
153 /*
154  * Per-SIM data structure
155  */
156 struct aac_cam;
157 struct aac_sim
158 {
159 	device_t		sim_dev;
160 	int			TargetsPerBus;
161 	int			BusNumber;
162 	int			BusType;
163 	int			InitiatorBusId;
164 	struct aac_softc	*aac_sc;
165 	struct aac_cam		*aac_cam;
166 	TAILQ_ENTRY(aac_sim)	sim_link;
167 };
168 
169 /*
170  * Per-disk structure
171  */
172 struct aac_disk
173 {
174 	device_t			ad_dev;
175 	struct aac_softc		*ad_controller;
176 	struct aac_container		*ad_container;
177 	struct disk			*ad_disk;
178 	int				ad_flags;
179 #define AAC_DISK_OPEN	(1<<0)
180 	int				ad_cylinders;
181 	int				ad_heads;
182 	int				ad_sectors;
183 	u_int64_t			ad_size;
184 	int				unit;
185 };
186 
187 /*
188  * Per-command control structure.
189  */
190 struct aac_command
191 {
192 	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
193 
194 	struct aac_softc	*cm_sc;		/* controller that owns us */
195 
196 	struct aac_fib		*cm_fib;	/* FIB associated with this
197 						 * command */
198 	u_int64_t		cm_fibphys;	/* bus address of the FIB */
199 	struct bio		*cm_data;	/* pointer to data in kernel
200 						 * space */
201 	u_int32_t		cm_datalen;	/* data length */
202 	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
203 	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
204 						 * command */
205 	int			cm_flags;
206 #define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
207 						 * mapped */
208 #define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
209 						 * from controller to host */
210 #define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
211 						 * from host to controller */
212 #define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
213 #define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
214 #define AAC_ON_AACQ_FREE	(1<<5)
215 #define AAC_ON_AACQ_READY	(1<<6)
216 #define AAC_ON_AACQ_BUSY	(1<<7)
217 #define AAC_ON_AACQ_AIF		(1<<8)
218 #define AAC_ON_AACQ_NORM	(1<<10)
219 #define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<10))
220 #define AAC_CMD_RESET		(1<<9)
221 #define AAC_CMD_FASTRESP	(1<<11)
222 #define AAC_CMD_WAIT		(1<<12)
223 
224 	void			(* cm_complete)(struct aac_command *cm);
225 	union ccb 		*cm_ccb;
226 	time_t			cm_timestamp;	/* command creation time */
227 	int			cm_index;
228 	bus_dma_tag_t		cm_passthr_dmat;	/* passthrough buffer/command
229 							 * DMA tag */
230 };
231 
232 struct aac_fibmap {
233 	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
234 	struct aac_fib		*aac_fibs;
235 	bus_dmamap_t		aac_fibmap;
236 	struct aac_command	*aac_commands;
237 };
238 
239 /*
240  * We gather a number of adapter-visible items into a single structure.
241  *
242  * The ordering of this strucure may be important; we copy the Linux driver:
243  *
244  * Adapter FIBs
245  * Init struct
246  * Queue headers (Comm Area)
247  * Printf buffer
248  *
249  * In addition, we add:
250  * Sync Fib
251  */
252 struct aac_common {
253 	/* fibs for the controller to send us messages */
254 	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
255 
256 	/* the init structure */
257 	struct aac_adapter_init	ac_init;
258 
259 	/* buffer for text messages from the controller */
260 	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
261 
262 	/* fib for synchronous commands */
263 	struct aac_fib		ac_sync_fib;
264 
265 	/* response buffer for SRC (new comm. type1) - must be last element */
266 	u_int32_t   ac_host_rrq[0];
267 };
268 
269 /*
270  * Interface operations
271  */
272 struct aac_interface
273 {
274 	int	(*aif_get_fwstatus)(struct aac_softc *sc);
275 	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
276 	int	(*aif_get_istatus)(struct aac_softc *sc);
277 	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
278 	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
279 				   u_int32_t arg0, u_int32_t arg1,
280 				   u_int32_t arg2, u_int32_t arg3);
281 	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
282 	void	(*aif_access_devreg)(struct aac_softc *sc, int enable);
283 	int (*aif_send_command)(struct aac_softc *sc, struct aac_command *cm);
284 	int (*aif_get_outb_queue)(struct aac_softc *sc);
285 	void (*aif_set_outb_queue)(struct aac_softc *sc, int index);
286 };
287 extern struct aac_interface	aacraid_src_interface;
288 extern struct aac_interface	aacraid_srcv_interface;
289 
290 #define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
291 #define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
292 #define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
293 #define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
294 					(mask)))
295 #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
296 	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
297 	(arg3)))
298 #define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
299 					(mb)))
300 #define	AAC_ACCESS_DEVREG(sc, mode)	((sc)->aac_if.aif_access_devreg((sc), \
301 					mode))
302 #define AAC_SEND_COMMAND(sc, cm)	((sc)->aac_if.aif_send_command((sc), (cm)))
303 #define AAC_GET_OUTB_QUEUE(sc)		((sc)->aac_if.aif_get_outb_queue((sc)))
304 #define AAC_SET_OUTB_QUEUE(sc, idx)	((sc)->aac_if.aif_set_outb_queue((sc), (idx)))
305 
306 #define AAC_MEM0_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag0, \
307 					sc->aac_bhandle0, reg, val)
308 #define AAC_MEM0_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag0, \
309 					sc->aac_bhandle0, reg)
310 #define AAC_MEM0_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag0, \
311 					sc->aac_bhandle0, reg, val)
312 #define AAC_MEM0_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag0, \
313 					sc->aac_bhandle0, reg)
314 #define AAC_MEM0_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag0, \
315 					sc->aac_bhandle0, reg, val)
316 #define AAC_MEM0_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag0, \
317 					sc->aac_bhandle0, reg)
318 
319 #define AAC_MEM1_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag1, \
320 					sc->aac_bhandle1, reg, val)
321 #define AAC_MEM1_GETREG4(sc, reg)	bus_space_read_4(sc->aac_btag1, \
322 					sc->aac_bhandle1, reg)
323 #define AAC_MEM1_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag1, \
324 					sc->aac_bhandle1, reg, val)
325 #define AAC_MEM1_GETREG2(sc, reg)	bus_space_read_2(sc->aac_btag1, \
326 					sc->aac_bhandle1, reg)
327 #define AAC_MEM1_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag1, \
328 					sc->aac_bhandle1, reg, val)
329 #define AAC_MEM1_GETREG1(sc, reg)	bus_space_read_1(sc->aac_btag1, \
330 					sc->aac_bhandle1, reg)
331 
332 /* fib context (IOCTL) */
333 struct aac_fib_context {
334 	u_int32_t		unique;
335 	int			ctx_idx;
336 	int			ctx_wrap;
337 	struct aac_fib_context *next, *prev;
338 };
339 
340 /* MSIX context */
341 struct aac_msix_ctx {
342 	int			vector_no;
343 	struct aac_softc  	*sc;
344 };
345 
346 /*
347  * Per-controller structure.
348  */
349 struct aac_softc
350 {
351 	/* bus connections */
352 	device_t		aac_dev;
353 	struct resource		*aac_regs_res0, *aac_regs_res1; /* reg. if. window */
354 	int			aac_regs_rid0, aac_regs_rid1;		/* resource ID */
355 	bus_space_handle_t	aac_bhandle0, aac_bhandle1;		/* bus space handle */
356 	bus_space_tag_t		aac_btag0, aac_btag1;		/* bus space tag */
357 	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
358 	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
359 							 * DMA tag */
360 	struct resource		*aac_irq[AAC_MAX_MSIX];	 /* interrupt */
361 	int			aac_irq_rid[AAC_MAX_MSIX];
362 	void			*aac_intr[AAC_MAX_MSIX]; /* interrupt handle */
363 	struct aac_msix_ctx	aac_msix[AAC_MAX_MSIX]; /* context */
364 	eventhandler_tag	eh;
365 #if __FreeBSD_version >= 800000
366 	struct callout	aac_daemontime;		/* clock daemon callout */
367 #else
368 	struct callout_handle	timeout_id;	/* timeout handle */
369 #endif
370 
371 	/* controller features, limits and status */
372 	int			aac_state;
373 #define AAC_STATE_SUSPEND	(1<<0)
374 #define	AAC_STATE_UNUSED0	(1<<1)
375 #define AAC_STATE_INTERRUPTS_ON	(1<<2)
376 #define AAC_STATE_AIF_SLEEPER	(1<<3)
377 #define AAC_STATE_RESET		(1<<4)
378 	struct FsaRevision		aac_revision;
379 
380 	/* controller hardware interface */
381 	int			aac_hwif;
382 #define AAC_HWIF_SRC		5
383 #define AAC_HWIF_SRCV		6
384 #define AAC_HWIF_UNKNOWN	-1
385 	bus_dma_tag_t		aac_common_dmat;	/* common structure
386 							 * DMA tag */
387 	bus_dmamap_t		aac_common_dmamap;	/* common structure
388 							 * DMA map */
389 	struct aac_common	*aac_common;
390 	u_int32_t		aac_common_busaddr;
391 	u_int32_t		aac_host_rrq_idx[AAC_MAX_MSIX];
392 	u_int32_t		aac_rrq_outstanding[AAC_MAX_MSIX];
393 	u_int32_t		aac_fibs_pushed_no;
394 	struct aac_interface	aac_if;
395 
396 	/* command/fib resources */
397 	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
398 	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
399 	u_int			total_fibs;
400 	struct aac_command	*aac_commands;
401 
402 	/* command management */
403 	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
404 						 * available for reuse */
405 	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
406 						 * controller resources */
407 	TAILQ_HEAD(,aac_command) aac_busy;
408 	TAILQ_HEAD(,aac_event)	aac_ev_cmfree;
409 	struct bio_queue_head	aac_bioq;
410 
411 	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
412 
413 	/* connected containters */
414 	TAILQ_HEAD(,aac_container)	aac_container_tqh;
415 	struct mtx		aac_container_lock;
416 
417 	/*
418 	 * The general I/O lock.  This protects the sync fib, the lists, the
419 	 * queues, and the registers.
420 	 */
421 	struct mtx		aac_io_lock;
422 
423 	struct intr_config_hook	aac_ich;
424 
425 	/* sync. transfer mode */
426 	struct aac_command *aac_sync_cm;
427 
428 	/* management interface */
429 	struct cdev *aac_dev_t;
430 	struct mtx		aac_aifq_lock;
431 	struct aac_fib		aac_aifq[AAC_AIFQ_LENGTH];
432 	int			aifq_idx;
433 	int			aifq_filled;
434 	int 			aif_pending;
435 	struct aac_fib_context *fibctx;
436 	struct selinfo		rcv_select;
437 	struct proc		*aifthread;
438 	int			aifflags;
439 #define AAC_AIFFLAGS_RUNNING	(1 << 0)
440 #define AAC_AIFFLAGS_AIF	(1 << 1)
441 #define	AAC_AIFFLAGS_EXIT	(1 << 2)
442 #define AAC_AIFFLAGS_EXITED	(1 << 3)
443 #define AAC_AIFFLAGS_PRINTF	(1 << 4)
444 #define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
445 #define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
446 				 AAC_AIFFLAGS_ALLOCFIBS)
447 	u_int32_t		flags;
448 #define AAC_FLAGS_PERC2QC	(1 << 0)
449 #define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
450 #define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
451 #define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
452 #define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
453 #define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
454 						 * 2GB-4GB range */
455 #define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
456 #define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
457 #define	AAC_FLAGS_BROKEN_MEMMAP (1 << 8)	/* Broken HostPhysMemPages */
458 #define AAC_FLAGS_SLAVE	(1 << 9)
459 #define AAC_FLAGS_MASTER	(1 << 10)
460 #define AAC_FLAGS_NEW_COMM	(1 << 11)	/* New comm. interface supported */
461 #define AAC_FLAGS_RAW_IO	(1 << 12)	/* Raw I/O interface */
462 #define AAC_FLAGS_ARRAY_64BIT	(1 << 13)	/* 64-bit array size */
463 #define AAC_FLAGS_LBA_64BIT	(1 << 14)	/* 64-bit LBA support */
464 #define AAC_QUEUE_FRZN		(1 << 15)	/* Freeze the processing of
465 						 * commands on the queue. */
466 #define AAC_FLAGS_NEW_COMM_TYPE1 (1 << 16)	/* New comm. type1 supported */
467 #define AAC_FLAGS_NEW_COMM_TYPE2 (1 << 17)	/* New comm. type2 supported */
468 #define AAC_FLAGS_NEW_COMM_TYPE34 (1 << 18)	/* New comm. type3/4 */
469 #define AAC_FLAGS_SYNC_MODE (1 << 18)	/* Sync. transfer mode */
470 	u_int32_t		hint_flags;		/* driver parameters */
471 	int	sim_freezed;				/* flag for sim_freeze/release */
472 	u_int32_t		supported_options;
473 	u_int32_t		scsi_method_id;
474 	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
475 
476 	u_int32_t	aac_max_fibs;           /* max. FIB count */
477 	u_int32_t	aac_max_fibs_alloc;		/* max. alloc. per alloc_commands() */
478 	u_int32_t	aac_max_fib_size;		/* max. FIB size */
479 	u_int32_t	aac_sg_tablesize;		/* max. sg count from host */
480 	u_int32_t	aac_max_sectors;		/* max. I/O size from host (blocks) */
481 	u_int32_t	aac_feature_bits;		/* feature bits from suppl. info */
482 	u_int32_t	aac_support_opt2;		/* supp. options from suppl. info */
483 	u_int32_t	aac_max_aif;			/* max. AIF count */
484 	u_int32_t	doorbell_mask;			/* for IOP reset */
485 	u_int32_t	aac_max_msix;			/* max. MSI-X vectors */
486 	u_int32_t	aac_vector_cap;			/* MSI-X vector capab.*/
487 	int		msi_enabled;			/* MSI/MSI-X enabled */
488 	int		msi_tupelo;		/* Series 6 support for */
489 						/* single MSI interrupt */
490 #define AAC_CAM_TARGET_WILDCARD ~0
491 	void			(*cam_rescan_cb)(struct aac_softc *, uint32_t,
492 				    uint32_t);
493 	u_int32_t	DebugFlags;		/* Debug print flags bitmap */
494 	u_int32_t	DebugOffset;		/* Offset from DPMEM start */
495 	u_int32_t	DebugHeaderSize;	/* Size of debug header */
496 	u_int32_t	FwDebugFlags;		/* FW Debug Flags */
497 	u_int32_t	FwDebugBufferSize;	/* FW Debug Buffer size */
498 };
499 
500 /*
501  * Event callback mechanism for the driver
502  */
503 #define AAC_EVENT_NONE		0x00
504 #define AAC_EVENT_CMFREE	0x01
505 #define	AAC_EVENT_MASK		0xff
506 #define AAC_EVENT_REPEAT	0x100
507 
508 typedef void aac_event_cb_t(struct aac_softc *sc, struct aac_event *event,
509     void *arg);
510 struct aac_event {
511 	TAILQ_ENTRY(aac_event)	ev_links;
512 	int			ev_type;
513 	aac_event_cb_t		*ev_callback;
514 	void			*ev_arg;
515 };
516 
517 /*
518  * Public functions
519  */
520 extern void		aacraid_free(struct aac_softc *sc);
521 extern int		aacraid_attach(struct aac_softc *sc);
522 extern int		aacraid_detach(device_t dev);
523 extern int		aacraid_shutdown(device_t dev);
524 extern int		aacraid_suspend(device_t dev);
525 extern int		aacraid_resume(device_t dev);
526 extern void		aacraid_new_intr_type1(void *arg);
527 extern void		aacraid_submit_bio(struct bio *bp);
528 extern void		aacraid_biodone(struct bio *bp);
529 extern void		aacraid_startio(struct aac_softc *sc);
530 extern int		aacraid_alloc_command(struct aac_softc *sc,
531 					  struct aac_command **cmp);
532 extern void		aacraid_release_command(struct aac_command *cm);
533 extern void		aacraid_add_event(struct aac_softc *sc, struct aac_event
534 				      *event);
535 extern void		aacraid_map_command_sg(void *arg, bus_dma_segment_t *segs,
536 				int nseg, int error);
537 extern int		aacraid_wait_command(struct aac_command *cmp);
538 
539 #ifdef AACRAID_DEBUG
540 # define fwprintf(sc, flags, fmt, args...)				\
541 	aacraid_fw_printf(sc, flags, "%s: " fmt, __func__, ##args);
542 
543 extern void	aacraid_print_queues(struct aac_softc *sc);
544 extern void	aacraid_print_fib(struct aac_softc *sc, struct aac_fib *fib,
545 			      const char *caller);
546 extern void	aacraid_print_aif(struct aac_softc *sc,
547 			      struct aac_aif_command *aif);
548 
549 #define AAC_PRINT_FIB(sc, fib)	aacraid_print_fib(sc, fib, __func__)
550 
551 #else
552 # define fwprintf(sc, flags, fmt, args...)
553 
554 # define aacraid_print_queues(sc)
555 
556 # define AAC_PRINT_FIB(sc, fib)
557 # define aacraid_print_aif(sc, aac_aif_command)
558 #endif
559 
560 struct aac_code_lookup {
561 	char	*string;
562 	u_int32_t	code;
563 };
564 
565 /*
566  * Queue primitives for driver queues.
567  */
568 #define AACQ_ADD(sc, qname)					\
569 	do {							\
570 		struct aac_qstat *qs;				\
571 								\
572 		qs = &(sc)->aac_qstat[qname];			\
573 								\
574 		qs->q_length++;					\
575 		if (qs->q_length > qs->q_max)			\
576 			qs->q_max = qs->q_length;		\
577 	} while (0)
578 
579 #define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
580 #define AACQ_INIT(sc, qname)				\
581 	do {						\
582 		sc->aac_qstat[qname].q_length = 0;	\
583 		sc->aac_qstat[qname].q_max = 0;		\
584 	} while (0)
585 
586 
587 #define AACQ_COMMAND_QUEUE(name, index)					\
588 static __inline void							\
589 aac_initq_ ## name (struct aac_softc *sc)				\
590 {									\
591 	TAILQ_INIT(&sc->aac_ ## name);					\
592 	AACQ_INIT(sc, index);						\
593 }									\
594 static __inline void							\
595 aac_enqueue_ ## name (struct aac_command *cm)				\
596 {									\
597 	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
598 		printf("command %p is on another queue, flags = %#x\n",	\
599 		       cm, cm->cm_flags);				\
600 		panic("command is on another queue");			\
601 	}								\
602 	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
603 	cm->cm_flags |= AAC_ON_ ## index;				\
604 	AACQ_ADD(cm->cm_sc, index);					\
605 }									\
606 static __inline void							\
607 aac_requeue_ ## name (struct aac_command *cm)				\
608 {									\
609 	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
610 		printf("command %p is on another queue, flags = %#x\n",	\
611 		       cm, cm->cm_flags);				\
612 		panic("command is on another queue");			\
613 	}								\
614 	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
615 	cm->cm_flags |= AAC_ON_ ## index;				\
616 	AACQ_ADD(cm->cm_sc, index);					\
617 }									\
618 static __inline struct aac_command *					\
619 aac_dequeue_ ## name (struct aac_softc *sc)				\
620 {									\
621 	struct aac_command *cm;						\
622 									\
623 	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
624 		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
625 			printf("command %p not in queue, flags = %#x, "	\
626 		       	       "bit = %#x\n", cm, cm->cm_flags,		\
627 			       AAC_ON_ ## index);			\
628 			panic("command not in queue");			\
629 		}							\
630 		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
631 		cm->cm_flags &= ~AAC_ON_ ## index;			\
632 		AACQ_REMOVE(sc, index);					\
633 	}								\
634 	return(cm);							\
635 }									\
636 static __inline void							\
637 aac_remove_ ## name (struct aac_command *cm)				\
638 {									\
639 	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
640 		printf("command %p not in queue, flags = %#x, "		\
641 		       "bit = %#x\n", cm, cm->cm_flags, 		\
642 		       AAC_ON_ ## index);				\
643 		panic("command not in queue");				\
644 	}								\
645 	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
646 	cm->cm_flags &= ~AAC_ON_ ## index;				\
647 	AACQ_REMOVE(cm->cm_sc, index);					\
648 }									\
649 struct hack
650 
651 AACQ_COMMAND_QUEUE(free, AACQ_FREE);
652 AACQ_COMMAND_QUEUE(ready, AACQ_READY);
653 AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
654 
655 static __inline void
656 aac_print_printf(struct aac_softc *sc)
657 {
658 	/*
659 	 * XXX We have the ability to read the length of the printf string
660 	 * from out of the mailboxes.
661 	 */
662 	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
663 		      sc->aac_common->ac_printf);
664 	sc->aac_common->ac_printf[0] = 0;
665 	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
666 }
667 
668 static __inline int
669 aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
670 {
671 
672 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
673 	*fib = &sc->aac_common->ac_sync_fib;
674 	return (0);
675 }
676 
677 static __inline void
678 aac_release_sync_fib(struct aac_softc *sc)
679 {
680 
681 	mtx_assert(&sc->aac_io_lock, MA_OWNED);
682 }
683