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