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