xref: /freebsd/sys/dev/aac/aacvar.h (revision 4afedc314e8e6ded5fb719708bcc78deba2eadcb)
135863739SMike Smith /*-
235863739SMike Smith  * Copyright (c) 2000 Michael Smith
3c6eafcf2SScott Long  * Copyright (c) 2001 Scott Long
435863739SMike Smith  * Copyright (c) 2000 BSDi
5c6eafcf2SScott Long  * Copyright (c) 2001 Adaptec, Inc.
635863739SMike Smith  * All rights reserved.
735863739SMike Smith  *
835863739SMike Smith  * Redistribution and use in source and binary forms, with or without
935863739SMike Smith  * modification, are permitted provided that the following conditions
1035863739SMike Smith  * are met:
1135863739SMike Smith  * 1. Redistributions of source code must retain the above copyright
1235863739SMike Smith  *    notice, this list of conditions and the following disclaimer.
1335863739SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1435863739SMike Smith  *    notice, this list of conditions and the following disclaimer in the
1535863739SMike Smith  *    documentation and/or other materials provided with the distribution.
1635863739SMike Smith  *
1735863739SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1835863739SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1935863739SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2035863739SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2135863739SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2235863739SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2335863739SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2435863739SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2535863739SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2635863739SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2735863739SMike Smith  * SUCH DAMAGE.
2835863739SMike Smith  *
2935863739SMike Smith  *	$FreeBSD$
3035863739SMike Smith  */
3135863739SMike Smith 
329e2e96d8SScott Long #include <sys/bio.h>
339e2e96d8SScott Long #include <sys/lock.h>
349e2e96d8SScott Long #include <sys/mutex.h>
359e2e96d8SScott Long #include <sys/taskqueue.h>
369e2e96d8SScott Long #include <sys/selinfo.h>
37891619a6SPoul-Henning Kamp #include <geom/geom_disk.h>
38891619a6SPoul-Henning Kamp 
399e2e96d8SScott Long 
40914da7d0SScott Long /*
41914da7d0SScott Long  * Driver Parameter Definitions
42914da7d0SScott Long  */
4335863739SMike Smith 
4435863739SMike Smith /*
4535863739SMike Smith  * The firmware interface allows for a 16-bit s/g list length.  We limit
4635863739SMike Smith  * ourselves to a reasonable maximum and ensure alignment.
4735863739SMike Smith  */
4835863739SMike Smith #define AAC_MAXSGENTRIES	64	/* max S/G entries, limit 65535 */
4935863739SMike Smith 
5035863739SMike Smith /*
5135863739SMike Smith  * We allocate a small set of FIBs for the adapter to use to send us messages.
5235863739SMike Smith  */
5335863739SMike Smith #define AAC_ADAPTER_FIBS	8
5435863739SMike Smith 
5535863739SMike Smith /*
56ffb37f33SScott Long  * FIBs are allocated in page-size chunks and can grow up to the 512
57ffb37f33SScott Long  * limit imposed by the hardware.
5835863739SMike Smith  */
598480cc63SScott Long #define AAC_FIB_COUNT		(PAGE_SIZE/sizeof(struct aac_fib))
608480cc63SScott Long #define AAC_PREALLOCATE_FIBS	128
612c81db6cSScott Long #define AAC_MAX_FIBS		504
6235863739SMike Smith 
6335863739SMike Smith /*
64c6eafcf2SScott Long  * The controller reports status events in AIFs.  We hang on to a number of
65c6eafcf2SScott Long  * these in order to pass them out to user-space management tools.
6635863739SMike Smith  */
6735863739SMike Smith #define AAC_AIFQ_LENGTH		64
6835863739SMike Smith 
6935863739SMike Smith /*
7035863739SMike Smith  * Firmware messages are passed in the printf buffer.
7135863739SMike Smith  */
7235863739SMike Smith #define AAC_PRINTF_BUFSIZE	256
7335863739SMike Smith 
7435863739SMike Smith /*
75c6eafcf2SScott Long  * We wait this many seconds for the adapter to come ready if it is still
76c6eafcf2SScott Long  * booting
7735863739SMike Smith  */
7835863739SMike Smith #define AAC_BOOT_TIMEOUT	(3 * 60)
7935863739SMike Smith 
8035863739SMike Smith /*
8135863739SMike Smith  * Timeout for immediate commands.
8235863739SMike Smith  */
830b94a66eSMike Smith #define AAC_IMMEDIATE_TIMEOUT	30		/* seconds */
840b94a66eSMike Smith 
850b94a66eSMike Smith /*
860b94a66eSMike Smith  * Timeout for normal commands
870b94a66eSMike Smith  */
880b94a66eSMike Smith #define AAC_CMD_TIMEOUT		30		/* seconds */
890b94a66eSMike Smith 
900b94a66eSMike Smith /*
910b94a66eSMike Smith  * Rate at which we periodically check for timed out commands and kick the
920b94a66eSMike Smith  * controller.
930b94a66eSMike Smith  */
9436e0bf6eSScott Long #define AAC_PERIODIC_INTERVAL	20		/* seconds */
9535863739SMike Smith 
9635863739SMike Smith /*
9735863739SMike Smith  * Per-container data structure
9835863739SMike Smith  */
9935863739SMike Smith struct aac_container
10035863739SMike Smith {
10135863739SMike Smith 	struct aac_mntobj		co_mntobj;
10235863739SMike Smith 	device_t			co_disk;
10336e0bf6eSScott Long 	int				co_found;
10436e0bf6eSScott Long 	TAILQ_ENTRY(aac_container)	co_link;
10535863739SMike Smith };
10635863739SMike Smith 
10735863739SMike Smith /*
10870545d1aSScott Long  * Per-SIM data structure
10970545d1aSScott Long  */
11070545d1aSScott Long struct aac_sim
11170545d1aSScott Long {
11270545d1aSScott Long 	device_t		sim_dev;
11370545d1aSScott Long 	int			TargetsPerBus;
11470545d1aSScott Long 	int			BusNumber;
11570545d1aSScott Long 	int			InitiatorBusId;
11670545d1aSScott Long 	struct aac_softc	*aac_sc;
11770545d1aSScott Long 	TAILQ_ENTRY(aac_sim)	sim_link;
11870545d1aSScott Long };
11970545d1aSScott Long 
12070545d1aSScott Long /*
12135863739SMike Smith  * Per-disk structure
12235863739SMike Smith  */
12335863739SMike Smith struct aac_disk
12435863739SMike Smith {
12535863739SMike Smith 	device_t			ad_dev;
12635863739SMike Smith 	struct aac_softc		*ad_controller;
12735863739SMike Smith 	struct aac_container		*ad_container;
1280b7ed341SPoul-Henning Kamp 	struct disk			*ad_disk;
12935863739SMike Smith 	int				ad_flags;
13035863739SMike Smith #define AAC_DISK_OPEN	(1<<0)
13135863739SMike Smith 	int				ad_cylinders;
13235863739SMike Smith 	int				ad_heads;
13335863739SMike Smith 	int				ad_sectors;
13435863739SMike Smith 	u_int32_t			ad_size;
13536e0bf6eSScott Long 	int				unit;
13635863739SMike Smith };
13735863739SMike Smith 
13835863739SMike Smith /*
13935863739SMike Smith  * Per-command control structure.
14035863739SMike Smith  */
14135863739SMike Smith struct aac_command
14235863739SMike Smith {
14335863739SMike Smith 	TAILQ_ENTRY(aac_command) cm_link;	/* list linkage */
14435863739SMike Smith 
14535863739SMike Smith 	struct aac_softc	*cm_sc;		/* controller that owns us */
14635863739SMike Smith 
147c6eafcf2SScott Long 	struct aac_fib		*cm_fib;	/* FIB associated with this
148c6eafcf2SScott Long 						 * command */
14935863739SMike Smith 	u_int32_t		cm_fibphys;	/* bus address of the FIB */
150c6eafcf2SScott Long 	struct bio		*cm_data;	/* pointer to data in kernel
151c6eafcf2SScott Long 						 * space */
15235863739SMike Smith 	u_int32_t		cm_datalen;	/* data length */
15335863739SMike Smith 	bus_dmamap_t		cm_datamap;	/* DMA map for bio data */
154c6eafcf2SScott Long 	struct aac_sg_table	*cm_sgtable;	/* pointer to s/g table in
155c6eafcf2SScott Long 						 * command */
15635863739SMike Smith 	int			cm_flags;
157c6eafcf2SScott Long #define AAC_CMD_MAPPED		(1<<0)		/* command has had its data
158c6eafcf2SScott Long 						 * mapped */
159c6eafcf2SScott Long #define AAC_CMD_DATAIN		(1<<1)		/* command involves data moving
160c6eafcf2SScott Long 						 * from controller to host */
161c6eafcf2SScott Long #define AAC_CMD_DATAOUT		(1<<2)		/* command involves data moving
162c6eafcf2SScott Long 						 * from host to controller */
16335863739SMike Smith #define AAC_CMD_COMPLETED	(1<<3)		/* command has been completed */
1640b94a66eSMike Smith #define AAC_CMD_TIMEDOUT	(1<<4)		/* command taken too long */
165f6c4dd3fSScott Long #define AAC_ON_AACQ_FREE	(1<<5)
166f6c4dd3fSScott Long #define AAC_ON_AACQ_READY	(1<<6)
167f6c4dd3fSScott Long #define AAC_ON_AACQ_BUSY	(1<<7)
168ecd1c51fSScott Long #define AAC_ON_AACQ_MASK	((1<<5)|(1<<6)|(1<<7))
169cd481291SScott Long #define AAC_QUEUE_FRZN		(1<<9)		/* Freeze the processing of
170cd481291SScott Long 						 * commands on the queue. */
17135863739SMike Smith 
17235863739SMike Smith 	void			(* cm_complete)(struct aac_command *cm);
17335863739SMike Smith 	void			*cm_private;
1740b94a66eSMike Smith 	time_t			cm_timestamp;	/* command creation time */
17536e0bf6eSScott Long 	int			cm_queue;
176cb0d64b9SScott Long 	int			cm_index;
17735863739SMike Smith };
17835863739SMike Smith 
179ffb37f33SScott Long struct aac_fibmap {
180ffb37f33SScott Long 	TAILQ_ENTRY(aac_fibmap) fm_link;	/* list linkage */
181ffb37f33SScott Long 	struct aac_fib		*aac_fibs;
182ffb37f33SScott Long 	bus_dmamap_t		aac_fibmap;
183ffb37f33SScott Long 	struct aac_command	*aac_commands;
184ffb37f33SScott Long };
185ffb37f33SScott Long 
18635863739SMike Smith /*
18735863739SMike Smith  * We gather a number of adapter-visible items into a single structure.
18835863739SMike Smith  *
18935863739SMike Smith  * The ordering of this strucure may be important; we copy the Linux driver:
19035863739SMike Smith  *
19135863739SMike Smith  * Adapter FIBs
19235863739SMike Smith  * Init struct
19335863739SMike Smith  * Queue headers (Comm Area)
19435863739SMike Smith  * Printf buffer
19535863739SMike Smith  *
19635863739SMike Smith  * In addition, we add:
19735863739SMike Smith  * Sync Fib
19835863739SMike Smith  */
19935863739SMike Smith struct aac_common {
20035863739SMike Smith 	/* fibs for the controller to send us messages */
20135863739SMike Smith 	struct aac_fib		ac_fibs[AAC_ADAPTER_FIBS];
20235863739SMike Smith 
20335863739SMike Smith 	/* the init structure */
20435863739SMike Smith 	struct aac_adapter_init	ac_init;
20535863739SMike Smith 
20635863739SMike Smith 	/* arena within which the queue structures are kept */
207c6eafcf2SScott Long 	u_int8_t		ac_qbuf[sizeof(struct aac_queue_table) +
208c6eafcf2SScott Long 				AAC_QUEUE_ALIGN];
20935863739SMike Smith 
21035863739SMike Smith 	/* buffer for text messages from the controller */
21135863739SMike Smith 	char		       	ac_printf[AAC_PRINTF_BUFSIZE];
21235863739SMike Smith 
21335863739SMike Smith 	/* fib for synchronous commands */
21435863739SMike Smith 	struct aac_fib		ac_sync_fib;
21535863739SMike Smith };
21635863739SMike Smith 
21735863739SMike Smith /*
21835863739SMike Smith  * Interface operations
21935863739SMike Smith  */
22035863739SMike Smith struct aac_interface
22135863739SMike Smith {
22235863739SMike Smith 	int	(*aif_get_fwstatus)(struct aac_softc *sc);
22335863739SMike Smith 	void	(*aif_qnotify)(struct aac_softc *sc, int qbit);
22435863739SMike Smith 	int	(*aif_get_istatus)(struct aac_softc *sc);
225b3457b51SScott Long 	void	(*aif_clr_istatus)(struct aac_softc *sc, int mask);
22635863739SMike Smith 	void	(*aif_set_mailbox)(struct aac_softc *sc, u_int32_t command,
227c6eafcf2SScott Long 				   u_int32_t arg0, u_int32_t arg1,
228c6eafcf2SScott Long 				   u_int32_t arg2, u_int32_t arg3);
229a6d35632SScott Long 	int	(*aif_get_mailbox)(struct aac_softc *sc, int mb);
23035863739SMike Smith 	void	(*aif_set_interrupts)(struct aac_softc *sc, int enable);
23135863739SMike Smith };
23235863739SMike Smith extern struct aac_interface	aac_rx_interface;
23335863739SMike Smith extern struct aac_interface	aac_sa_interface;
234b3457b51SScott Long extern struct aac_interface	aac_fa_interface;
2354afedc31SScott Long extern struct aac_interface	aac_rkt_interface;
23635863739SMike Smith 
23735863739SMike Smith #define AAC_GET_FWSTATUS(sc)		((sc)->aac_if.aif_get_fwstatus((sc)))
23835863739SMike Smith #define AAC_QNOTIFY(sc, qbit)		((sc)->aac_if.aif_qnotify((sc), (qbit)))
23935863739SMike Smith #define AAC_GET_ISTATUS(sc)		((sc)->aac_if.aif_get_istatus((sc)))
240b3457b51SScott Long #define AAC_CLEAR_ISTATUS(sc, mask)	((sc)->aac_if.aif_clr_istatus((sc), \
241c6eafcf2SScott Long 					(mask)))
24235863739SMike Smith #define AAC_SET_MAILBOX(sc, command, arg0, arg1, arg2, arg3) \
243c6eafcf2SScott Long 	((sc)->aac_if.aif_set_mailbox((sc), (command), (arg0), (arg1), (arg2), \
244c6eafcf2SScott Long 	(arg3)))
245a6d35632SScott Long #define AAC_GET_MAILBOX(sc, mb)		((sc)->aac_if.aif_get_mailbox((sc), \
246a6d35632SScott Long 					(mb)))
247914da7d0SScott Long #define	AAC_MASK_INTERRUPTS(sc)		((sc)->aac_if.aif_set_interrupts((sc), \
248914da7d0SScott Long 					0))
249914da7d0SScott Long #define AAC_UNMASK_INTERRUPTS(sc)	((sc)->aac_if.aif_set_interrupts((sc), \
250914da7d0SScott Long 					1))
25135863739SMike Smith 
252c6eafcf2SScott Long #define AAC_SETREG4(sc, reg, val)	bus_space_write_4(sc->aac_btag, \
253c6eafcf2SScott Long 					sc->aac_bhandle, reg, val)
254c6eafcf2SScott Long #define AAC_GETREG4(sc, reg)		bus_space_read_4 (sc->aac_btag, \
255c6eafcf2SScott Long 					sc->aac_bhandle, reg)
256c6eafcf2SScott Long #define AAC_SETREG2(sc, reg, val)	bus_space_write_2(sc->aac_btag, \
257c6eafcf2SScott Long 					sc->aac_bhandle, reg, val)
258c6eafcf2SScott Long #define AAC_GETREG2(sc, reg)		bus_space_read_2 (sc->aac_btag, \
259c6eafcf2SScott Long 					sc->aac_bhandle, reg)
260c6eafcf2SScott Long #define AAC_SETREG1(sc, reg, val)	bus_space_write_1(sc->aac_btag, \
261c6eafcf2SScott Long 					sc->aac_bhandle, reg, val)
262c6eafcf2SScott Long #define AAC_GETREG1(sc, reg)		bus_space_read_1 (sc->aac_btag, \
263c6eafcf2SScott Long 					sc->aac_bhandle, reg)
26435863739SMike Smith 
26535863739SMike Smith /*
26635863739SMike Smith  * Per-controller structure.
26735863739SMike Smith  */
26835863739SMike Smith struct aac_softc
26935863739SMike Smith {
27035863739SMike Smith 	/* bus connections */
27135863739SMike Smith 	device_t		aac_dev;
272c6eafcf2SScott Long 	struct resource		*aac_regs_resource;	/* register interface
273c6eafcf2SScott Long 							 * window */
27435863739SMike Smith 	int			aac_regs_rid;		/* resource ID */
27535863739SMike Smith 	bus_space_handle_t	aac_bhandle;		/* bus space handle */
27635863739SMike Smith 	bus_space_tag_t		aac_btag;		/* bus space tag */
27735863739SMike Smith 	bus_dma_tag_t		aac_parent_dmat;	/* parent DMA tag */
278c6eafcf2SScott Long 	bus_dma_tag_t		aac_buffer_dmat;	/* data buffer/command
279c6eafcf2SScott Long 							 * DMA tag */
28035863739SMike Smith 	struct resource		*aac_irq;		/* interrupt */
28135863739SMike Smith 	int			aac_irq_rid;
28235863739SMike Smith 	void			*aac_intr;		/* interrupt handle */
2835f54d522SScott Long 	eventhandler_tag	eh;
28435863739SMike Smith 
28535863739SMike Smith 	/* controller features, limits and status */
28635863739SMike Smith 	int			aac_state;
28735863739SMike Smith #define AAC_STATE_SUSPEND	(1<<0)
28835863739SMike Smith #define	AAC_STATE_OPEN		(1<<1)
28935863739SMike Smith #define AAC_STATE_INTERRUPTS_ON	(1<<2)
29035863739SMike Smith #define AAC_STATE_AIF_SLEEPER	(1<<3)
29135863739SMike Smith 	struct FsaRevision		aac_revision;
29235863739SMike Smith 
29335863739SMike Smith 	/* controller hardware interface */
29435863739SMike Smith 	int			aac_hwif;
29535863739SMike Smith #define AAC_HWIF_I960RX		0
29635863739SMike Smith #define AAC_HWIF_STRONGARM	1
297b3457b51SScott Long #define AAC_HWIF_FALCON		2
2984afedc31SScott Long #define AAC_HWIF_RKT		3
2990b94a66eSMike Smith #define AAC_HWIF_UNKNOWN	-1
300c6eafcf2SScott Long 	bus_dma_tag_t		aac_common_dmat;	/* common structure
301c6eafcf2SScott Long 							 * DMA tag */
302c6eafcf2SScott Long 	bus_dmamap_t		aac_common_dmamap;	/* common structure
303c6eafcf2SScott Long 							 * DMA map */
30435863739SMike Smith 	struct aac_common	*aac_common;
30535863739SMike Smith 	u_int32_t		aac_common_busaddr;
30635863739SMike Smith 	struct aac_interface	aac_if;
30735863739SMike Smith 
30835863739SMike Smith 	/* command/fib resources */
309c6eafcf2SScott Long 	bus_dma_tag_t		aac_fib_dmat;	/* DMA tag for allocing FIBs */
310ffb37f33SScott Long 	TAILQ_HEAD(,aac_fibmap)	aac_fibmap_tqh;
3118b149b51SJohn Baldwin 	u_int			total_fibs;
312ffb37f33SScott Long 	struct aac_command	*aac_commands;
31335863739SMike Smith 
31435863739SMike Smith 	/* command management */
315c6eafcf2SScott Long 	TAILQ_HEAD(,aac_command) aac_free;	/* command structures
316c6eafcf2SScott Long 						 * available for reuse */
317c6eafcf2SScott Long 	TAILQ_HEAD(,aac_command) aac_ready;	/* commands on hold for
318c6eafcf2SScott Long 						 * controller resources */
3190b94a66eSMike Smith 	TAILQ_HEAD(,aac_command) aac_busy;
32035863739SMike Smith 	struct bio_queue_head	aac_bioq;
32135863739SMike Smith 	struct aac_queue_table	*aac_queues;
32235863739SMike Smith 	struct aac_queue_entry	*aac_qentries[AAC_QUEUE_COUNT];
32335863739SMike Smith 
3240b94a66eSMike Smith 	struct aac_qstat	aac_qstat[AACQ_COUNT];	/* queue statistics */
3250b94a66eSMike Smith 
32635863739SMike Smith 	/* connected containters */
32770545d1aSScott Long 	TAILQ_HEAD(,aac_container)	aac_container_tqh;
328bb6fe253SScott Long 	struct mtx		aac_container_lock;
32935863739SMike Smith 
33003b5fe51SScott Long 	/*
33103b5fe51SScott Long 	 * The general I/O lock.  This protects the sync fib, the lists, the
33203b5fe51SScott Long 	 * queues, and the registers.
33303b5fe51SScott Long 	 */
334bb6fe253SScott Long 	struct mtx		aac_io_lock;
335ae543596SScott Long 
33635863739SMike Smith 	/* delayed activity infrastructure */
337c6eafcf2SScott Long 	struct task		aac_task_complete;	/* deferred-completion
338c6eafcf2SScott Long 							 * task */
33935863739SMike Smith 	struct intr_config_hook	aac_ich;
34035863739SMike Smith 
34135863739SMike Smith 	/* management interface */
34289c9c53dSPoul-Henning Kamp 	struct cdev *aac_dev_t;
343bb6fe253SScott Long 	struct mtx		aac_aifq_lock;
34435863739SMike Smith 	struct aac_aif_command	aac_aifq[AAC_AIFQ_LENGTH];
34535863739SMike Smith 	int			aac_aifq_head;
34635863739SMike Smith 	int			aac_aifq_tail;
347b3457b51SScott Long 	struct selinfo		rcv_select;
34836e0bf6eSScott Long 	struct proc		*aifthread;
34936e0bf6eSScott Long 	int			aifflags;
35036e0bf6eSScott Long #define AAC_AIFFLAGS_RUNNING	(1 << 0)
35170545d1aSScott Long #define AAC_AIFFLAGS_AIF	(1 << 1)
35236e0bf6eSScott Long #define	AAC_AIFFLAGS_EXIT	(1 << 2)
35336e0bf6eSScott Long #define AAC_AIFFLAGS_EXITED	(1 << 3)
35470545d1aSScott Long #define AAC_AIFFLAGS_PRINTF	(1 << 4)
355ae543596SScott Long #define	AAC_AIFFLAGS_ALLOCFIBS	(1 << 5)
356ae543596SScott Long #define AAC_AIFFLAGS_PENDING	(AAC_AIFFLAGS_AIF | AAC_AIFFLAGS_PRINTF | \
357ae543596SScott Long 				 AAC_AIFFLAGS_ALLOCFIBS)
358a6d35632SScott Long 	u_int32_t		flags;
359a6d35632SScott Long #define AAC_FLAGS_PERC2QC	(1 << 0)
360a6d35632SScott Long #define	AAC_FLAGS_ENABLE_CAM	(1 << 1)	/* No SCSI passthrough */
361a6d35632SScott Long #define	AAC_FLAGS_CAM_NORESET	(1 << 2)	/* Fake SCSI resets */
362a6d35632SScott Long #define	AAC_FLAGS_CAM_PASSONLY	(1 << 3)	/* Only create pass devices */
363a6d35632SScott Long #define	AAC_FLAGS_SG_64BIT	(1 << 4)	/* Use 64-bit S/G addresses */
364a6d35632SScott Long #define	AAC_FLAGS_4GB_WINDOW	(1 << 5)	/* Device can access host mem
365a6d35632SScott Long 						 * 2GB-4GB range */
366a6d35632SScott Long #define	AAC_FLAGS_NO4GB		(1 << 6)	/* Can't access host mem >2GB */
367a6d35632SScott Long #define	AAC_FLAGS_256FIBS	(1 << 7)	/* Can only do 256 commands */
3684b00f859SScott Long #define	AAC_FLAGS_BROKEN_MEMMAP (1 << 8)	/* Broken HostPhysMemPages */
369fe3cb0e1SScott Long 
370a6d35632SScott Long 	u_int32_t		supported_options;
371a6d35632SScott Long 	int			aac_max_fibs;
372fe3cb0e1SScott Long 	u_int32_t		scsi_method_id;
37370545d1aSScott Long 	TAILQ_HEAD(,aac_sim)	aac_sim_tqh;
37435863739SMike Smith };
37535863739SMike Smith 
37635863739SMike Smith 
37735863739SMike Smith /*
37835863739SMike Smith  * Public functions
37935863739SMike Smith  */
38035863739SMike Smith extern void		aac_free(struct aac_softc *sc);
38135863739SMike Smith extern int		aac_attach(struct aac_softc *sc);
38235863739SMike Smith extern int		aac_detach(device_t dev);
38335863739SMike Smith extern int		aac_shutdown(device_t dev);
38435863739SMike Smith extern int		aac_suspend(device_t dev);
38535863739SMike Smith extern int		aac_resume(device_t dev);
38635863739SMike Smith extern void		aac_intr(void *arg);
38735863739SMike Smith extern void		aac_submit_bio(struct bio *bp);
3880b94a66eSMike Smith extern void		aac_biodone(struct bio *bp);
389fe3cb0e1SScott Long extern void		aac_startio(struct aac_softc *sc);
390fe3cb0e1SScott Long extern int		aac_alloc_command(struct aac_softc *sc,
391fe3cb0e1SScott Long 					  struct aac_command **cmp);
392fe3cb0e1SScott Long extern void		aac_release_command(struct aac_command *cm);
393cbfd045bSScott Long extern int		aac_sync_fib(struct aac_softc *sc, u_int32_t command,
394cbfd045bSScott Long 				     u_int32_t xferstate, struct aac_fib *fib,
395cbfd045bSScott Long 				     u_int16_t datasize);
39635863739SMike Smith 
39735863739SMike Smith /*
39835863739SMike Smith  * Debugging levels:
39935863739SMike Smith  *  0 - quiet, only emit warnings
40035863739SMike Smith  *  1 - noisy, emit major function points and things done
40135863739SMike Smith  *  2 - extremely noisy, emit trace items in loops, etc.
40235863739SMike Smith  */
40335863739SMike Smith #ifdef AAC_DEBUG
4040b94a66eSMike Smith # define debug(level, fmt, args...)					\
4050b94a66eSMike Smith 	do {								\
4066e551fb6SDavid E. O'Brien 	if (level <=AAC_DEBUG) printf("%s: " fmt "\n", __func__ , ##args); \
4070b94a66eSMike Smith 	} while (0)
4080b94a66eSMike Smith # define debug_called(level)						\
4090b94a66eSMike Smith 	do {								\
410956d569bSDavid E. O'Brien 	if (level <= AAC_DEBUG) printf("%s: called\n", __func__);	\
4110b94a66eSMike Smith 	} while (0)
41235863739SMike Smith 
41335863739SMike Smith extern void	aac_print_queues(struct aac_softc *sc);
41435863739SMike Smith extern void	aac_panic(struct aac_softc *sc, char *reason);
415c6eafcf2SScott Long extern void	aac_print_fib(struct aac_softc *sc, struct aac_fib *fib,
41670148712SPeter Wemm 			      const char *caller);
417c6eafcf2SScott Long extern void	aac_print_aif(struct aac_softc *sc,
418c6eafcf2SScott Long 			      struct aac_aif_command *aif);
41935863739SMike Smith 
4206e551fb6SDavid E. O'Brien #define AAC_PRINT_FIB(sc, fib)	aac_print_fib(sc, fib, __func__)
42135863739SMike Smith 
42235863739SMike Smith #else
42335863739SMike Smith # define debug(level, fmt, args...)
42435863739SMike Smith # define debug_called(level)
42535863739SMike Smith 
42635863739SMike Smith # define aac_print_queues(sc)
42735863739SMike Smith # define aac_panic(sc, reason)
42835863739SMike Smith 
42935863739SMike Smith # define AAC_PRINT_FIB(sc, fib)
43036e0bf6eSScott Long # define aac_print_aif(sc, aac_aif_command)
43135863739SMike Smith #endif
43235863739SMike Smith 
43335863739SMike Smith struct aac_code_lookup {
43435863739SMike Smith 	char	*string;
43535863739SMike Smith 	u_int32_t	code;
43635863739SMike Smith };
43735863739SMike Smith 
438914da7d0SScott Long /*
4390b94a66eSMike Smith  * Queue primitives for driver queues.
44035863739SMike Smith  */
4410b94a66eSMike Smith #define AACQ_ADD(sc, qname)					\
4420b94a66eSMike Smith 	do {							\
443914da7d0SScott Long 		struct aac_qstat *qs;				\
444914da7d0SScott Long 								\
445914da7d0SScott Long 		qs = &(sc)->aac_qstat[qname];			\
4460b94a66eSMike Smith 								\
4470b94a66eSMike Smith 		qs->q_length++;					\
4480b94a66eSMike Smith 		if (qs->q_length > qs->q_max)			\
4490b94a66eSMike Smith 			qs->q_max = qs->q_length;		\
4500b94a66eSMike Smith 	} while (0)
45135863739SMike Smith 
4520b94a66eSMike Smith #define AACQ_REMOVE(sc, qname)    (sc)->aac_qstat[qname].q_length--
4530b94a66eSMike Smith #define AACQ_INIT(sc, qname)				\
4540b94a66eSMike Smith 	do {						\
4550b94a66eSMike Smith 		sc->aac_qstat[qname].q_length = 0;	\
4560b94a66eSMike Smith 		sc->aac_qstat[qname].q_max = 0;		\
4570b94a66eSMike Smith 	} while (0)
4580b94a66eSMike Smith 
4590b94a66eSMike Smith 
4600b94a66eSMike Smith #define AACQ_COMMAND_QUEUE(name, index)					\
4610b94a66eSMike Smith static __inline void							\
4620b94a66eSMike Smith aac_initq_ ## name (struct aac_softc *sc)				\
4630b94a66eSMike Smith {									\
4640b94a66eSMike Smith 	TAILQ_INIT(&sc->aac_ ## name);					\
4650b94a66eSMike Smith 	AACQ_INIT(sc, index);						\
4660b94a66eSMike Smith }									\
4670b94a66eSMike Smith static __inline void							\
4680b94a66eSMike Smith aac_enqueue_ ## name (struct aac_command *cm)				\
4690b94a66eSMike Smith {									\
470f6c4dd3fSScott Long 	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
471f6c4dd3fSScott Long 		printf("command %p is on another queue, flags = %#x\n",	\
472f6c4dd3fSScott Long 		       cm, cm->cm_flags);				\
473f6c4dd3fSScott Long 		panic("command is on another queue");			\
474f6c4dd3fSScott Long 	}								\
4750b94a66eSMike Smith 	TAILQ_INSERT_TAIL(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
476f6c4dd3fSScott Long 	cm->cm_flags |= AAC_ON_ ## index;				\
4770b94a66eSMike Smith 	AACQ_ADD(cm->cm_sc, index);					\
4780b94a66eSMike Smith }									\
4790b94a66eSMike Smith static __inline void							\
4800b94a66eSMike Smith aac_requeue_ ## name (struct aac_command *cm)				\
4810b94a66eSMike Smith {									\
482f6c4dd3fSScott Long 	if ((cm->cm_flags & AAC_ON_AACQ_MASK) != 0) {			\
483f6c4dd3fSScott Long 		printf("command %p is on another queue, flags = %#x\n",	\
484f6c4dd3fSScott Long 		       cm, cm->cm_flags);				\
485f6c4dd3fSScott Long 		panic("command is on another queue");			\
486f6c4dd3fSScott Long 	}								\
4870b94a66eSMike Smith 	TAILQ_INSERT_HEAD(&cm->cm_sc->aac_ ## name, cm, cm_link);	\
488f6c4dd3fSScott Long 	cm->cm_flags |= AAC_ON_ ## index;				\
4890b94a66eSMike Smith 	AACQ_ADD(cm->cm_sc, index);					\
4900b94a66eSMike Smith }									\
4910b94a66eSMike Smith static __inline struct aac_command *					\
4920b94a66eSMike Smith aac_dequeue_ ## name (struct aac_softc *sc)				\
4930b94a66eSMike Smith {									\
4940b94a66eSMike Smith 	struct aac_command *cm;						\
4950b94a66eSMike Smith 									\
4960b94a66eSMike Smith 	if ((cm = TAILQ_FIRST(&sc->aac_ ## name)) != NULL) {		\
497f6c4dd3fSScott Long 		if ((cm->cm_flags & AAC_ON_ ## index) == 0) {		\
498914da7d0SScott Long 			printf("command %p not in queue, flags = %#x, "	\
499914da7d0SScott Long 		       	       "bit = %#x\n", cm, cm->cm_flags,		\
500914da7d0SScott Long 			       AAC_ON_ ## index);			\
501f6c4dd3fSScott Long 			panic("command not in queue");			\
502f6c4dd3fSScott Long 		}							\
5030b94a66eSMike Smith 		TAILQ_REMOVE(&sc->aac_ ## name, cm, cm_link);		\
504f6c4dd3fSScott Long 		cm->cm_flags &= ~AAC_ON_ ## index;			\
5050b94a66eSMike Smith 		AACQ_REMOVE(sc, index);					\
5060b94a66eSMike Smith 	}								\
5070b94a66eSMike Smith 	return(cm);							\
5080b94a66eSMike Smith }									\
5090b94a66eSMike Smith static __inline void							\
5100b94a66eSMike Smith aac_remove_ ## name (struct aac_command *cm)				\
5110b94a66eSMike Smith {									\
512f6c4dd3fSScott Long 	if ((cm->cm_flags & AAC_ON_ ## index) == 0) {			\
513914da7d0SScott Long 		printf("command %p not in queue, flags = %#x, "		\
514914da7d0SScott Long 		       "bit = %#x\n", cm, cm->cm_flags, 		\
515914da7d0SScott Long 		       AAC_ON_ ## index);				\
516f6c4dd3fSScott Long 		panic("command not in queue");				\
517f6c4dd3fSScott Long 	}								\
5180b94a66eSMike Smith 	TAILQ_REMOVE(&cm->cm_sc->aac_ ## name, cm, cm_link);		\
519f6c4dd3fSScott Long 	cm->cm_flags &= ~AAC_ON_ ## index;				\
5200b94a66eSMike Smith 	AACQ_REMOVE(cm->cm_sc, index);					\
5210b94a66eSMike Smith }									\
5220b94a66eSMike Smith struct hack
5230b94a66eSMike Smith 
5240b94a66eSMike Smith AACQ_COMMAND_QUEUE(free, AACQ_FREE);
5250b94a66eSMike Smith AACQ_COMMAND_QUEUE(ready, AACQ_READY);
5260b94a66eSMike Smith AACQ_COMMAND_QUEUE(busy, AACQ_BUSY);
5270b94a66eSMike Smith 
5280b94a66eSMike Smith /*
5290b94a66eSMike Smith  * outstanding bio queue
5300b94a66eSMike Smith  */
53135863739SMike Smith static __inline void
5320b94a66eSMike Smith aac_initq_bio(struct aac_softc *sc)
53335863739SMike Smith {
5340b94a66eSMike Smith 	bioq_init(&sc->aac_bioq);
5350b94a66eSMike Smith 	AACQ_INIT(sc, AACQ_BIO);
53635863739SMike Smith }
53735863739SMike Smith 
53835863739SMike Smith static __inline void
5390b94a66eSMike Smith aac_enqueue_bio(struct aac_softc *sc, struct bio *bp)
54035863739SMike Smith {
5410b94a66eSMike Smith 	bioq_insert_tail(&sc->aac_bioq, bp);
5420b94a66eSMike Smith 	AACQ_ADD(sc, AACQ_BIO);
54335863739SMike Smith }
54435863739SMike Smith 
5450b94a66eSMike Smith static __inline struct bio *
5460b94a66eSMike Smith aac_dequeue_bio(struct aac_softc *sc)
54735863739SMike Smith {
5480b94a66eSMike Smith 	struct bio *bp;
54935863739SMike Smith 
5500b94a66eSMike Smith 	if ((bp = bioq_first(&sc->aac_bioq)) != NULL) {
5510b94a66eSMike Smith 		bioq_remove(&sc->aac_bioq, bp);
5520b94a66eSMike Smith 		AACQ_REMOVE(sc, AACQ_BIO);
55335863739SMike Smith 	}
5540b94a66eSMike Smith 	return(bp);
55535863739SMike Smith }
55636e0bf6eSScott Long 
55736e0bf6eSScott Long static __inline void
55836e0bf6eSScott Long aac_print_printf(struct aac_softc *sc)
55936e0bf6eSScott Long {
560b3457b51SScott Long 	/*
561b3457b51SScott Long 	 * XXX We have the ability to read the length of the printf string
562b3457b51SScott Long 	 * from out of the mailboxes.
563b3457b51SScott Long 	 */
56436e0bf6eSScott Long 	device_printf(sc->aac_dev, "**Monitor** %.*s", AAC_PRINTF_BUFSIZE,
56536e0bf6eSScott Long 		      sc->aac_common->ac_printf);
5669148fa21SScott Long 	sc->aac_common->ac_printf[0] = 0;
56736e0bf6eSScott Long 	AAC_QNOTIFY(sc, AAC_DB_PRINTF);
56836e0bf6eSScott Long }
56903b5fe51SScott Long 
57003b5fe51SScott Long static __inline int
57103b5fe51SScott Long aac_alloc_sync_fib(struct aac_softc *sc, struct aac_fib **fib)
57203b5fe51SScott Long {
57303b5fe51SScott Long 
574bb6fe253SScott Long 	mtx_lock(&sc->aac_io_lock);
57503b5fe51SScott Long 	*fib = &sc->aac_common->ac_sync_fib;
57603b5fe51SScott Long 	return (0);
57703b5fe51SScott Long }
57803b5fe51SScott Long 
57903b5fe51SScott Long static __inline void
58003b5fe51SScott Long aac_release_sync_fib(struct aac_softc *sc)
58103b5fe51SScott Long {
58203b5fe51SScott Long 
583bb6fe253SScott Long 	mtx_unlock(&sc->aac_io_lock);
58403b5fe51SScott Long }
58503b5fe51SScott Long 
586