xref: /openbsd/sys/dev/ic/aic79xx.h (revision fabcfecb)
1*fabcfecbSjsg /*	$OpenBSD: aic79xx.h,v 1.34 2024/10/22 21:50:02 jsg Exp $	*/
26dab29ddSmarco 
36dab29ddSmarco /*
46dab29ddSmarco  * Copyright (c) 2004 Milos Urbanek, Kenneth R. Westerback & Marco Peereboom
56dab29ddSmarco  * All rights reserved.
66dab29ddSmarco  *
76dab29ddSmarco  * Redistribution and use in source and binary forms, with or without
86dab29ddSmarco  * modification, are permitted provided that the following conditions
96dab29ddSmarco  * are met:
106dab29ddSmarco  * 1. Redistributions of source code must retain the above copyright
116dab29ddSmarco  *    notice, this list of conditions and the following disclaimer.
126dab29ddSmarco  * 2. Redistributions in binary form must reproduce the above copyright
136dab29ddSmarco  *    notice, this list of conditions and the following disclaimer in the
146dab29ddSmarco  *    documentation and/or other materials provided with the distribution.
156dab29ddSmarco  *
166dab29ddSmarco  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
176dab29ddSmarco  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186dab29ddSmarco  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196dab29ddSmarco  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
206dab29ddSmarco  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216dab29ddSmarco  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226dab29ddSmarco  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236dab29ddSmarco  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246dab29ddSmarco  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256dab29ddSmarco  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266dab29ddSmarco  * SUCH DAMAGE.
276dab29ddSmarco  *
286dab29ddSmarco  */
296dab29ddSmarco 
3056007914Smarco /*
3156007914Smarco  * Core definitions and data structures shareable across OS platforms.
3256007914Smarco  *
3356007914Smarco  * Copyright (c) 1994-2002 Justin T. Gibbs.
3456007914Smarco  * Copyright (c) 2000-2002 Adaptec Inc.
3556007914Smarco  * All rights reserved.
3656007914Smarco  *
3756007914Smarco  * Redistribution and use in source and binary forms, with or without
3856007914Smarco  * modification, are permitted provided that the following conditions
3956007914Smarco  * are met:
4056007914Smarco  * 1. Redistributions of source code must retain the above copyright
4156007914Smarco  *    notice, this list of conditions, and the following disclaimer,
4256007914Smarco  *    without modification.
4356007914Smarco  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
4456007914Smarco  *    substantially similar to the "NO WARRANTY" disclaimer below
4556007914Smarco  *    ("Disclaimer") and any redistribution must be conditioned upon
4656007914Smarco  *    including a substantially similar Disclaimer requirement for further
4756007914Smarco  *    binary redistribution.
4856007914Smarco  * 3. Neither the names of the above-listed copyright holders nor the names
4956007914Smarco  *    of any contributors may be used to endorse or promote products derived
5056007914Smarco  *    from this software without specific prior written permission.
5156007914Smarco  *
5256007914Smarco  * Alternatively, this software may be distributed under the terms of the
5356007914Smarco  * GNU General Public License ("GPL") version 2 as published by the Free
5456007914Smarco  * Software Foundation.
5556007914Smarco  *
5656007914Smarco  * NO WARRANTY
5756007914Smarco  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5856007914Smarco  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5956007914Smarco  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
6056007914Smarco  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
6156007914Smarco  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6256007914Smarco  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6356007914Smarco  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6456007914Smarco  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
6556007914Smarco  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
6656007914Smarco  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6756007914Smarco  * POSSIBILITY OF SUCH DAMAGES.
6856007914Smarco  *
690a7a9938Skrw  * Id: //depot/aic7xxx/aic7xxx/aic79xx.h#107
700a7a9938Skrw  *
710a7a9938Skrw  * FreeBSD: src/sys/dev/aic7xxx/aic79xx.h,v 1.23 2004/11/18 20:22:31 gibbs Exp
7256007914Smarco  */
7356007914Smarco 
7456007914Smarco #ifndef _AIC79XX_H_
7556007914Smarco #define _AIC79XX_H_
7656007914Smarco 
771e6e6ef9Smarco /* #define AHD_DEBUG */
7856007914Smarco 
7956007914Smarco /* Register Definitions */
8056007914Smarco #include <dev/microcode/aic7xxx/aic79xx_reg.h>
8156007914Smarco 
8256007914Smarco #include <dev/ic/aic7xxx_cam.h>
8356007914Smarco 
8456007914Smarco /****************************** Useful Macros *********************************/
8556007914Smarco #ifndef MAX
8656007914Smarco #define MAX(a,b) (((a) > (b)) ? (a) : (b))
8756007914Smarco #endif
8856007914Smarco 
8956007914Smarco #ifndef MIN
9056007914Smarco #define MIN(a,b) (((a) < (b)) ? (a) : (b))
9156007914Smarco #endif
9256007914Smarco 
9356007914Smarco #ifndef TRUE
9456007914Smarco #define TRUE 1
9556007914Smarco #endif
9656007914Smarco #ifndef FALSE
9756007914Smarco #define FALSE 0
9856007914Smarco #endif
9956007914Smarco 
10056007914Smarco #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
10156007914Smarco 
10256007914Smarco #define ALL_CHANNELS '\0'
10356007914Smarco #define ALL_TARGETS_MASK 0xFFFF
10456007914Smarco #define INITIATOR_WILDCARD	(~0)
10556007914Smarco #define	SCB_LIST_NULL		0xFF00
1063b49cc0bSkrw #define	SCB_LIST_NULL_LE	(aic_htole16(SCB_LIST_NULL))
10756007914Smarco #define QOUTFIFO_ENTRY_VALID 0x80
10856007914Smarco #define SCBID_IS_NULL(scbid) (((scbid) & 0xFF00 ) == SCB_LIST_NULL)
10956007914Smarco 
11056007914Smarco #define SCSIID_TARGET(ahd, scsiid)	\
11156007914Smarco 	(((scsiid) & TID) >> TID_SHIFT)
11256007914Smarco #define SCSIID_OUR_ID(scsiid)		\
11356007914Smarco 	((scsiid) & OID)
11456007914Smarco #define SCSIID_CHANNEL(ahd, scsiid) ('A')
11556007914Smarco #define	SCB_IS_SCSIBUS_B(ahd, scb) (0)
11656007914Smarco #define	SCB_GET_OUR_ID(scb) \
11756007914Smarco 	SCSIID_OUR_ID((scb)->hscb->scsiid)
11856007914Smarco #define	SCB_GET_TARGET(ahd, scb) \
11956007914Smarco 	SCSIID_TARGET((ahd), (scb)->hscb->scsiid)
12056007914Smarco #define	SCB_GET_CHANNEL(ahd, scb) \
12156007914Smarco 	SCSIID_CHANNEL(ahd, (scb)->hscb->scsiid)
12256007914Smarco #define	SCB_GET_LUN(scb) \
12356007914Smarco 	((scb)->hscb->lun)
12456007914Smarco #define SCB_GET_TARGET_OFFSET(ahd, scb)	\
12556007914Smarco 	SCB_GET_TARGET(ahd, scb)
12656007914Smarco #define SCB_GET_TARGET_MASK(ahd, scb) \
12756007914Smarco 	(0x01 << (SCB_GET_TARGET_OFFSET(ahd, scb)))
12856007914Smarco #ifdef AHD_DEBUG
12956007914Smarco #define SCB_IS_SILENT(scb)					\
13056007914Smarco 	((ahd_debug & AHD_SHOW_MASKED_ERRORS) == 0		\
13156007914Smarco       && (((scb)->flags & SCB_SILENT) != 0))
13256007914Smarco #else
13356007914Smarco #define SCB_IS_SILENT(scb)					\
13456007914Smarco 	(((scb)->flags & SCB_SILENT) != 0)
13556007914Smarco #endif
13656007914Smarco /*
13756007914Smarco  * TCLs have the following format: TTTTLLLLLLLL
13856007914Smarco  */
13956007914Smarco #define TCL_TARGET_OFFSET(tcl) \
14056007914Smarco 	((((tcl) >> 4) & TID) >> 4)
14156007914Smarco #define TCL_LUN(tcl) \
14256007914Smarco 	(tcl & (AHD_NUM_LUNS - 1))
14356007914Smarco #define BUILD_TCL(scsiid, lun) \
14456007914Smarco 	((lun) | (((scsiid) & TID) << 4))
14556007914Smarco #define BUILD_TCL_RAW(target, channel, lun) \
14656007914Smarco 	((lun) | ((target) << 8))
14756007914Smarco 
14856007914Smarco #define SCB_GET_TAG(scb) \
1496ffc4d0cSkrw 	aic_le16toh(scb->hscb->tag)
15056007914Smarco 
15156007914Smarco #ifndef	AHD_TARGET_MODE
15256007914Smarco #undef	AHD_TMODE_ENABLE
15356007914Smarco #define	AHD_TMODE_ENABLE 0
15456007914Smarco #endif
15556007914Smarco 
15656007914Smarco /**************************** Driver Constants ********************************/
15756007914Smarco /*
15856007914Smarco  * The maximum number of supported targets.
15956007914Smarco  */
16056007914Smarco #define AHD_NUM_TARGETS 16
16156007914Smarco 
16256007914Smarco /*
16356007914Smarco  * The maximum number of supported luns.
16456007914Smarco  * The identify message only supports 64 luns in non-packetized transfers.
16556007914Smarco  * You can have 2^64 luns when information unit transfers are enabled,
16656007914Smarco  * but until we see a need to support that many, we support 256.
16756007914Smarco  */
16856007914Smarco #define AHD_NUM_LUNS_NONPKT 64
16956007914Smarco #define AHD_NUM_LUNS 256
17056007914Smarco 
17156007914Smarco /*
17256007914Smarco  * The maximum transfer per S/G segment.
17356007914Smarco  */
17456007914Smarco #define AHD_MAXTRANSFER_SIZE	 0x00ffffff	/* limited by 24bit counter */
17556007914Smarco 
17656007914Smarco /*
17756007914Smarco  * The maximum amount of SCB storage in hardware on a controller.
17856007914Smarco  * This value represents an upper bound.  Due to software design,
17956007914Smarco  * we may not be able to use this number.
18056007914Smarco  */
18156007914Smarco #define AHD_SCB_MAX	512
18256007914Smarco 
18356007914Smarco /*
18456007914Smarco  * The maximum number of concurrent transactions supported per driver instance.
18556007914Smarco  * Sequencer Control Blocks (SCBs) store per-transaction information.
18656007914Smarco  */
18756007914Smarco #define AHD_MAX_QUEUE	AHD_SCB_MAX
18856007914Smarco 
18956007914Smarco /*
19056007914Smarco  * Define the size of our QIN and QOUT FIFOs.  They must be a power of 2
19156007914Smarco  * in size and accommodate as many transactions as can be queued concurrently.
19256007914Smarco  */
19356007914Smarco #define	AHD_QIN_SIZE	AHD_MAX_QUEUE
19456007914Smarco #define	AHD_QOUT_SIZE	AHD_MAX_QUEUE
19556007914Smarco 
19656007914Smarco #define AHD_QIN_WRAP(x) ((x) & (AHD_QIN_SIZE-1))
19756007914Smarco /*
19856007914Smarco  * The maximum amount of SCB storage we allocate in host memory.
19956007914Smarco  */
20056007914Smarco #define AHD_SCB_MAX_ALLOC AHD_MAX_QUEUE
20156007914Smarco 
20256007914Smarco /*
20356007914Smarco  * Ring Buffer of incoming target commands.
20456007914Smarco  * We allocate 256 to simplify the logic in the sequencer
20556007914Smarco  * by using the natural wrap point of an 8bit counter.
20656007914Smarco  */
20756007914Smarco #define AHD_TMODE_CMDS	256
20856007914Smarco 
20956007914Smarco /* Reset line assertion time in us */
21056007914Smarco #define AHD_BUSRESET_DELAY	25
21156007914Smarco 
21256007914Smarco /******************* Chip Characteristics/Operating Settings  *****************/
21356007914Smarco extern uint32_t ahd_attach_to_HostRAID_controllers;
21456007914Smarco 
21556007914Smarco /*
21656007914Smarco  * Chip Type
21756007914Smarco  * The chip order is from least sophisticated to most sophisticated.
21856007914Smarco  */
21956007914Smarco typedef enum {
22056007914Smarco 	AHD_NONE	= 0x0000,
22156007914Smarco 	AHD_CHIPID_MASK	= 0x00FF,
22256007914Smarco 	AHD_AIC7901	= 0x0001,
22356007914Smarco 	AHD_AIC7902	= 0x0002,
22456007914Smarco 	AHD_AIC7901A	= 0x0003,
22556007914Smarco 	AHD_PCI		= 0x0100,	/* Bus type PCI */
22656007914Smarco 	AHD_PCIX	= 0x0200,	/* Bus type PCIX */
22756007914Smarco 	AHD_BUS_MASK	= 0x0F00
22856007914Smarco } ahd_chip;
22956007914Smarco 
23056007914Smarco /*
23156007914Smarco  * Features available in each chip type.
23256007914Smarco  */
23356007914Smarco typedef enum {
23456007914Smarco 	AHD_FENONE		= 0x00000,
23556007914Smarco 	AHD_WIDE		= 0x00001,/* Wide Channel */
23656007914Smarco 	AHD_MULTI_FUNC		= 0x00100,/* Multi-Function/Channel Device */
23756007914Smarco 	AHD_TARGETMODE		= 0x01000,/* Has tested target mode support */
23856007914Smarco 	AHD_MULTIROLE		= 0x02000,/* Space for two roles at a time */
23956007914Smarco 	AHD_RTI			= 0x04000,/* Retained Training Support */
24056007914Smarco 	AHD_NEW_IOCELL_OPTS	= 0x08000,/* More Signal knobs in the IOCELL */
24156007914Smarco 	AHD_NEW_DFCNTRL_OPTS	= 0x10000,/* SCSIENWRDIS bit */
24256007914Smarco 	AHD_FAST_CDB_DELIVERY	= 0x20000,/* CDB acks released to Output Sync */
24356007914Smarco 	AHD_REMOVABLE		= 0x00000,/* Hot-Swap supported - None so far*/
24456007914Smarco 	AHD_AIC7901_FE		= AHD_FENONE,
24556007914Smarco 	AHD_AIC7901A_FE		= AHD_FENONE,
24656007914Smarco 	AHD_AIC7902_FE		= AHD_MULTI_FUNC
24756007914Smarco } ahd_feature;
24856007914Smarco 
24956007914Smarco /*
25056007914Smarco  * Bugs in the silicon that we work around in software.
25156007914Smarco  */
25256007914Smarco typedef enum {
25356007914Smarco 	AHD_BUGNONE		= 0x0000,
25456007914Smarco 	/*
25556007914Smarco 	 * Rev A hardware fails to update LAST/CURR/NEXTSCB
25656007914Smarco 	 * correctly in certain packetized selection cases.
25756007914Smarco 	 */
25856007914Smarco 	AHD_SENT_SCB_UPDATE_BUG	= 0x0001,
25956007914Smarco 	/* The wrong SCB is accessed to check the abort pending bit. */
26056007914Smarco 	AHD_ABORT_LQI_BUG	= 0x0002,
26156007914Smarco 	/* Packetized bitbucket crosses packet boundaries. */
26256007914Smarco 	AHD_PKT_BITBUCKET_BUG	= 0x0004,
26356007914Smarco 	/* The selection timer runs twice as long as its setting. */
26456007914Smarco 	AHD_LONG_SETIMO_BUG	= 0x0008,
26556007914Smarco 	/* The Non-LQ CRC error status is delayed until phase change. */
26656007914Smarco 	AHD_NLQICRC_DELAYED_BUG	= 0x0010,
26756007914Smarco 	/* The chip must be reset for all outgoing bus resets.  */
26856007914Smarco 	AHD_SCSIRST_BUG		= 0x0020,
26956007914Smarco 	/* Some PCIX fields must be saved and restored across chip reset. */
27056007914Smarco 	AHD_PCIX_CHIPRST_BUG	= 0x0040,
27156007914Smarco 	/* MMAPIO is not functional in PCI-X mode.  */
27256007914Smarco 	AHD_PCIX_MMAPIO_BUG	= 0x0080,
27356007914Smarco 	/* Reads to SCBRAM fail to reset the discard timer. */
27456007914Smarco 	AHD_PCIX_SCBRAM_RD_BUG  = 0x0100,
27556007914Smarco 	/* Bug workarounds that can be disabled on non-PCIX busses. */
27656007914Smarco 	AHD_PCIX_BUG_MASK	= AHD_PCIX_CHIPRST_BUG
27756007914Smarco 				| AHD_PCIX_MMAPIO_BUG
27856007914Smarco 				| AHD_PCIX_SCBRAM_RD_BUG,
27956007914Smarco 	/*
28056007914Smarco 	 * LQOSTOP0 status set even for forced selections with ATN
28156007914Smarco 	 * to perform non-packetized message delivery.
28256007914Smarco 	 */
28356007914Smarco 	AHD_LQO_ATNO_BUG	= 0x0200,
28456007914Smarco 	/* FIFO auto-flush does not always trigger.  */
28556007914Smarco 	AHD_AUTOFLUSH_BUG	= 0x0400,
28656007914Smarco 	/* The CLRLQO registers are not self-clearing. */
28756007914Smarco 	AHD_CLRLQO_AUTOCLR_BUG	= 0x0800,
28856007914Smarco 	/* The PACKETIZED status bit refers to the previous connection. */
28956007914Smarco 	AHD_PKTIZED_STATUS_BUG  = 0x1000,
29056007914Smarco 	/* "Short Luns" are not placed into outgoing LQ packets correctly. */
29156007914Smarco 	AHD_PKT_LUN_BUG		= 0x2000,
29256007914Smarco 	/*
29356007914Smarco 	 * Only the FIFO allocated to the non-packetized connection may
29456007914Smarco 	 * be in use during a non-packetzied connection.
29556007914Smarco 	 */
29656007914Smarco 	AHD_NONPACKFIFO_BUG	= 0x4000,
29756007914Smarco 	/*
2984b1a56afSjsg 	 * Writing to a DFF SCBPTR register may fail if concurrent with
29956007914Smarco 	 * a hardware write to the other DFF SCBPTR register.  This is
30056007914Smarco 	 * not currently a concern in our sequencer since all chips with
30156007914Smarco 	 * this bug have the AHD_NONPACKFIFO_BUG and all writes of concern
30256007914Smarco 	 * occur in non-packetized connections.
30356007914Smarco 	 */
30456007914Smarco 	AHD_MDFF_WSCBPTR_BUG	= 0x8000,
30556007914Smarco 	/* SGHADDR updates are slow. */
30656007914Smarco 	AHD_REG_SLOW_SETTLE_BUG	= 0x10000,
30756007914Smarco 	/*
30856007914Smarco 	 * Changing the MODE_PTR coincident with an interrupt that
30956007914Smarco 	 * switches to a different mode will cause the interrupt to
31056007914Smarco 	 * be in the mode written outside of interrupt context.
31156007914Smarco 	 */
31256007914Smarco 	AHD_SET_MODE_BUG	= 0x20000,
31356007914Smarco 	/* Non-packetized busfree revision does not work. */
31456007914Smarco 	AHD_BUSFREEREV_BUG	= 0x40000,
31556007914Smarco 	/*
31656007914Smarco 	 * Paced transfers are indicated with a non-standard PPR
31756007914Smarco 	 * option bit in the neg table, 160MHz is indicated by
31856007914Smarco 	 * sync factor 0x7, and the offset if off by a factor of 2.
31956007914Smarco 	 */
32056007914Smarco 	AHD_PACED_NEGTABLE_BUG	= 0x80000,
32156007914Smarco 	/* LQOOVERRUN false positives. */
32256007914Smarco 	AHD_LQOOVERRUN_BUG	= 0x100000,
32356007914Smarco 	/*
32456007914Smarco 	 * Controller write to INTSTAT will lose to a host
32556007914Smarco 	 * write to CLRINT.
32656007914Smarco 	 */
32756007914Smarco 	AHD_INTCOLLISION_BUG	= 0x200000,
32856007914Smarco 	/*
32956007914Smarco 	 * The GEM318 violates the SCSI spec by not waiting
33056007914Smarco 	 * the mandated bus settle delay between phase changes
33156007914Smarco 	 * in some situations.  Some aic79xx chip revs. are more
33256007914Smarco 	 * strict in this regard and will treat REQ assertions
33356007914Smarco 	 * that fall within the bus settle delay window as
33456007914Smarco 	 * glitches.  This flag tells the firmware to tolerate
33556007914Smarco 	 * early REQ assertions.
33656007914Smarco 	 */
33756007914Smarco 	AHD_EARLY_REQ_BUG	= 0x400000,
33856007914Smarco 	/*
33956007914Smarco 	 * The LED does not stay on long enough in packetized modes.
34056007914Smarco 	 */
34156007914Smarco 	AHD_FAINT_LED_BUG	= 0x800000
34256007914Smarco } ahd_bug;
34356007914Smarco 
34456007914Smarco /*
34556007914Smarco  * Configuration specific settings.
34656007914Smarco  * The driver determines these settings by probing the
34756007914Smarco  * chip/controller's configuration.
34856007914Smarco  */
34956007914Smarco typedef enum {
35056007914Smarco 	AHD_FNONE	      = 0x00000,
35156007914Smarco 	AHD_BOOT_CHANNEL      = 0x00001,/* We were set as the boot channel. */
35256007914Smarco 	AHD_USEDEFAULTS	      = 0x00004,/*
35356007914Smarco 					 * For cards without an seeprom
35456007914Smarco 					 * or a BIOS to initialize the chip's
35556007914Smarco 					 * SRAM, we use the default target
35656007914Smarco 					 * settings.
35756007914Smarco 					 */
35856007914Smarco 	AHD_SEQUENCER_DEBUG   = 0x00008,
35956007914Smarco 	AHD_RESET_BUS_A	      = 0x00010,
36056007914Smarco 	AHD_EXTENDED_TRANS_A  = 0x00020,
36156007914Smarco 	AHD_TERM_ENB_A	      = 0x00040,
36256007914Smarco 	AHD_SPCHK_ENB_A	      = 0x00080,
36356007914Smarco 	AHD_STPWLEVEL_A	      = 0x00100,
36456007914Smarco 	AHD_INITIATORROLE     = 0x00200,/*
36556007914Smarco 					 * Allow initiator operations on
36656007914Smarco 					 * this controller.
36756007914Smarco 					 */
36856007914Smarco 	AHD_TARGETROLE	      = 0x00400,/*
36956007914Smarco 					 * Allow target operations on this
37056007914Smarco 					 * controller.
37156007914Smarco 					 */
37256007914Smarco 	AHD_RESOURCE_SHORTAGE = 0x00800,
37356007914Smarco 	AHD_TQINFIFO_BLOCKED  = 0x01000,/* Blocked waiting for ATIOs */
37456007914Smarco 	AHD_INT50_SPEEDFLEX   = 0x02000,/*
37556007914Smarco 					 * Internal 50pin connector
37656007914Smarco 					 * sits behind an aic3860
37756007914Smarco 					 */
37856007914Smarco 	AHD_BIOS_ENABLED      = 0x04000,
37956007914Smarco 	AHD_ALL_INTERRUPTS    = 0x08000,
38056007914Smarco 	AHD_39BIT_ADDRESSING  = 0x10000,/* Use 39 bit addressing scheme. */
38156007914Smarco 	AHD_64BIT_ADDRESSING  = 0x20000,/* Use 64 bit addressing scheme. */
38256007914Smarco 	AHD_CURRENT_SENSING   = 0x40000,
38356007914Smarco 	AHD_SCB_CONFIG_USED   = 0x80000,/* No SEEPROM but SCB had info. */
38456007914Smarco 	AHD_HP_BOARD	      = 0x100000,
38556007914Smarco 	AHD_RESET_POLL_ACTIVE = 0x200000,
38656007914Smarco 	AHD_UPDATE_PEND_CMDS  = 0x400000,
38756007914Smarco 	AHD_RUNNING_QOUTFIFO  = 0x800000,
38856007914Smarco 	AHD_HAD_FIRST_SEL     = 0x1000000,
38956007914Smarco 	AHD_SHUTDOWN_RECOVERY = 0x2000000, /* Terminate recovery thread. */
39056007914Smarco 	AHD_HOSTRAID_BOARD    = 0x4000000
39156007914Smarco } ahd_flag;
39256007914Smarco 
39356007914Smarco /************************* Hardware  SCB Definition ***************************/
39456007914Smarco 
39556007914Smarco /*
39656007914Smarco  * The driver keeps up to MAX_SCB scb structures per card in memory.  The SCB
39756007914Smarco  * consists of a "hardware SCB" mirroring the fields available on the card
39856007914Smarco  * and additional information the kernel stores for each transaction.
39956007914Smarco  *
40056007914Smarco  * To minimize space utilization, a portion of the hardware scb stores
40156007914Smarco  * different data during different portions of a SCSI transaction.
40256007914Smarco  * As initialized by the host driver for the initiator role, this area
40356007914Smarco  * contains the SCSI cdb (or a pointer to the  cdb) to be executed.  After
40456007914Smarco  * the cdb has been presented to the target, this area serves to store
40556007914Smarco  * residual transfer information and the SCSI status byte.
40656007914Smarco  * For the target role, the contents of this area do not change, but
40756007914Smarco  * still serve a different purpose than for the initiator role.  See
40856007914Smarco  * struct target_data for details.
40956007914Smarco  */
41056007914Smarco 
41156007914Smarco /*
41254fbbda3Sjsg  * Status information embedded in the shared portion of
41356007914Smarco  * an SCB after passing the cdb to the target.  The kernel
41456007914Smarco  * driver will only read this data for transactions that
41556007914Smarco  * complete abnormally.
41656007914Smarco  */
41756007914Smarco struct initiator_status {
41856007914Smarco 	uint32_t residual_datacnt;	/* Residual in the current S/G seg */
41956007914Smarco 	uint32_t residual_sgptr;	/* The next S/G for this transfer */
42056007914Smarco 	uint8_t	 scsi_status;		/* Standard SCSI status byte */
42156007914Smarco };
42256007914Smarco 
42356007914Smarco struct target_status {
42456007914Smarco 	uint32_t residual_datacnt;	/* Residual in the current S/G seg */
42556007914Smarco 	uint32_t residual_sgptr;	/* The next S/G for this transfer */
42656007914Smarco 	uint8_t  scsi_status;		/* SCSI status to give to initiator */
42756007914Smarco 	uint8_t  target_phases;		/* Bitmap of phases to execute */
42856007914Smarco 	uint8_t  data_phase;		/* Data-In or Data-Out */
42956007914Smarco 	uint8_t  initiator_tag;		/* Initiator's transaction tag */
43056007914Smarco };
43156007914Smarco 
43256007914Smarco /*
43356007914Smarco  * Initiator mode SCB shared data area.
43456007914Smarco  * If the embedded CDB is 12 bytes or less, we embed
43556007914Smarco  * the sense buffer address in the SCB.  This allows
43656007914Smarco  * us to retrieve sense information without interrupting
43756007914Smarco  * the host in packetized mode.
43856007914Smarco  */
43956007914Smarco typedef uint32_t sense_addr_t;
44056007914Smarco #define MAX_CDB_LEN 16
44156007914Smarco #define MAX_CDB_LEN_WITH_SENSE_ADDR (MAX_CDB_LEN - sizeof(sense_addr_t))
44256007914Smarco union initiator_data {
44356007914Smarco 	struct {
44456007914Smarco 		uint64_t cdbptr;
44556007914Smarco 		uint8_t  cdblen;
44656007914Smarco 	} cdb_from_host;
44756007914Smarco 	uint8_t	 cdb[MAX_CDB_LEN];
44856007914Smarco 	struct {
44956007914Smarco 		uint8_t	 cdb[MAX_CDB_LEN_WITH_SENSE_ADDR];
45056007914Smarco 		sense_addr_t sense_addr;
45156007914Smarco 	} cdb_plus_saddr;
45256007914Smarco };
45356007914Smarco 
45456007914Smarco /*
45556007914Smarco  * Target mode version of the shared data SCB segment.
45656007914Smarco  */
45756007914Smarco struct target_data {
45856007914Smarco 	uint32_t spare[2];
45956007914Smarco 	uint8_t  scsi_status;		/* SCSI status to give to initiator */
46056007914Smarco 	uint8_t  target_phases;		/* Bitmap of phases to execute */
46156007914Smarco 	uint8_t  data_phase;		/* Data-In or Data-Out */
46256007914Smarco 	uint8_t  initiator_tag;		/* Initiator's transaction tag */
46356007914Smarco };
46456007914Smarco 
46556007914Smarco struct hardware_scb {
46656007914Smarco /*0*/	union {
46756007914Smarco 		union	initiator_data idata;
46856007914Smarco 		struct	target_data tdata;
46956007914Smarco 		struct	initiator_status istatus;
47056007914Smarco 		struct	target_status tstatus;
47156007914Smarco 	} shared_data;
47256007914Smarco /*
47356007914Smarco  * A word about residuals.
47456007914Smarco  * The scb is presented to the sequencer with the dataptr and datacnt
47556007914Smarco  * fields initialized to the contents of the first S/G element to
47656007914Smarco  * transfer.  The sgptr field is initialized to the bus address for
47756007914Smarco  * the S/G element that follows the first in the in core S/G array
47856007914Smarco  * or'ed with the SG_FULL_RESID flag.  Sgptr may point to an invalid
47956007914Smarco  * S/G entry for this transfer (single S/G element transfer with the
48056007914Smarco  * first elements address and length preloaded in the dataptr/datacnt
48156007914Smarco  * fields).  If no transfer is to occur, sgptr is set to SG_LIST_NULL.
48256007914Smarco  * The SG_FULL_RESID flag ensures that the residual will be correctly
48356007914Smarco  * noted even if no data transfers occur.  Once the data phase is entered,
48456007914Smarco  * the residual sgptr and datacnt are loaded from the sgptr and the
48556007914Smarco  * datacnt fields.  After each S/G element's dataptr and length are
48656007914Smarco  * loaded into the hardware, the residual sgptr is advanced.  After
48756007914Smarco  * each S/G element is expired, its datacnt field is checked to see
48856007914Smarco  * if the LAST_SEG flag is set.  If so, SG_LIST_NULL is set in the
48956007914Smarco  * residual sg ptr and the transfer is considered complete.  If the
49084d3ec93Skrw  * sequencer determines that there is a residual in the transfer, or
49156007914Smarco  * there is non-zero status, it will set the SG_STATUS_VALID flag in
4924b1a56afSjsg  * sgptr and dma the scb back into host memory.  To summarize:
49356007914Smarco  *
49456007914Smarco  * Sequencer:
49556007914Smarco  *	o A residual has occurred if SG_FULL_RESID is set in sgptr,
49656007914Smarco  *	  or residual_sgptr does not have SG_LIST_NULL set.
49756007914Smarco  *
49898c26657Sguenther  *	o We are transferring the last segment if residual_datacnt has
49956007914Smarco  *	  the SG_LAST_SEG flag set.
50056007914Smarco  *
50156007914Smarco  * Host:
50256007914Smarco  *	o A residual can only have occurred if a completed scb has the
50356007914Smarco  *	  SG_STATUS_VALID flag set.  Inspection of the SCSI status field,
50456007914Smarco  *	  the residual_datacnt, and the residual_sgptr field will tell
50556007914Smarco  *	  for sure.
50656007914Smarco  *
50756007914Smarco  *	o residual_sgptr and sgptr refer to the "next" sg entry
50856007914Smarco  *	  and so may point beyond the last valid sg entry for the
50956007914Smarco  *	  transfer.
51056007914Smarco  */
51156007914Smarco #define SG_PTR_MASK	0xFFFFFFF8
51256007914Smarco /*16*/	uint16_t tag;		/* Reused by Sequencer. */
51356007914Smarco /*18*/	uint8_t  control;	/* See SCB_CONTROL in aic79xx.reg for details */
51456007914Smarco /*19*/	uint8_t	 scsiid;	/*
51556007914Smarco 				 * Selection out Id
51656007914Smarco 				 * Our Id (bits 0-3) Their ID (bits 4-7)
51756007914Smarco 				 */
51856007914Smarco /*20*/	uint8_t  lun;
51956007914Smarco /*21*/	uint8_t  task_attribute;
52056007914Smarco /*22*/	uint8_t  cdb_len;
52156007914Smarco /*23*/	uint8_t  task_management;
52256007914Smarco /*24*/	uint64_t dataptr;
52356007914Smarco /*32*/	uint32_t datacnt;	/* Byte 3 is spare. */
52456007914Smarco /*36*/	uint32_t sgptr;
52556007914Smarco /*40*/	uint32_t hscb_busaddr;
52656007914Smarco /*44*/	uint32_t next_hscb_busaddr;
52756007914Smarco /********** Long lun field only downloaded for full 8 byte lun support ********/
52856007914Smarco /*48*/  uint8_t	 pkt_long_lun[8];
52956007914Smarco /******* Fields below are not Downloaded (Sequencer may use for scratch) ******/
53056007914Smarco /*56*/  uint8_t	 spare[8];
53156007914Smarco };
53256007914Smarco 
53356007914Smarco /************************ Kernel SCB Definitions ******************************/
53456007914Smarco /*
53556007914Smarco  * Some fields of the SCB are OS dependent.  Here we collect the
53656007914Smarco  * definitions for elements that all OS platforms need to include
53756007914Smarco  * in there SCB definition.
53856007914Smarco  */
53956007914Smarco 
54056007914Smarco /*
54198c26657Sguenther  * Definition of a scatter/gather element as transferred to the controller.
54256007914Smarco  * The aic7xxx chips only support a 24bit length.  We use the top byte of
54356007914Smarco  * the length to store additional address bits and a flag to indicate
54456007914Smarco  * that a given segment terminates the transfer.  This gives us an
54556007914Smarco  * addressable range of 512GB on machines with 64bit PCI or with chips
54656007914Smarco  * that can support dual address cycles on 32bit PCI busses.
54756007914Smarco  */
54856007914Smarco struct ahd_dma_seg {
54956007914Smarco 	uint32_t	addr;
55056007914Smarco 	uint32_t	len;
55156007914Smarco #define	AHD_DMA_LAST_SEG	0x80000000
55256007914Smarco #define	AHD_SG_HIGH_ADDR_MASK	0x7F000000
55356007914Smarco #define	AHD_SG_LEN_MASK		0x00FFFFFF
55456007914Smarco };
55556007914Smarco 
55656007914Smarco struct ahd_dma64_seg {
55756007914Smarco 	uint64_t	addr;
55856007914Smarco 	uint32_t	len;
55956007914Smarco 	uint32_t	pad;
56056007914Smarco };
56156007914Smarco 
56256007914Smarco struct map_node {
56356007914Smarco 	bus_dmamap_t		 dmamap;
564d63d62f3Skrw 	bus_addr_t		 busaddr;
56556007914Smarco 	uint8_t			*vaddr;
5669fbcc7a8Skrw 	bus_dma_segment_t	 dmaseg;
5679fbcc7a8Skrw 	size_t			 size;
56856007914Smarco 	SLIST_ENTRY(map_node)	 links;
56956007914Smarco };
57056007914Smarco 
57156007914Smarco /*
57256007914Smarco  * The current state of this SCB.
57356007914Smarco  */
57456007914Smarco typedef enum {
57556007914Smarco 	SCB_FLAG_NONE		= 0x00000,
57656007914Smarco 	SCB_TRANSMISSION_ERROR	= 0x00001,/*
57756007914Smarco 					   * We detected a parity or CRC
57856007914Smarco 					   * error that has effected the
57956007914Smarco 					   * payload of the command.  This
58056007914Smarco 					   * flag is checked when normal
58156007914Smarco 					   * status is returned to catch
58256007914Smarco 					   * the case of a target not
58356007914Smarco 					   * responding to our attempt
58456007914Smarco 					   * to report the error.
58556007914Smarco 					   */
58656007914Smarco 	SCB_OTHERTCL_TIMEOUT	= 0x00002,/*
58756007914Smarco 					   * Another device was active
58856007914Smarco 					   * during the first timeout for
58956007914Smarco 					   * this SCB so we gave ourselves
59056007914Smarco 					   * an additional timeout period
59156007914Smarco 					   * in case it was hogging the
59256007914Smarco 					   * bus.
59356007914Smarco 				           */
59456007914Smarco 	SCB_DEVICE_RESET	= 0x00004,
59556007914Smarco 	SCB_SENSE		= 0x00008,
59656007914Smarco 	SCB_CDB32_PTR		= 0x00010,
59756007914Smarco 	SCB_AUTO_NEGOTIATE	= 0x00040,/* Negotiate to achieve goal. */
59856007914Smarco 	SCB_NEGOTIATE		= 0x00080,/* Negotiation forced for command. */
59956007914Smarco 	SCB_ABORT		= 0x00100,
60056007914Smarco 	SCB_ACTIVE		= 0x00200,
60156007914Smarco 	SCB_TARGET_IMMEDIATE	= 0x00400,
60256007914Smarco 	SCB_PACKETIZED		= 0x00800,
60356007914Smarco 	SCB_EXPECT_PPR_BUSFREE	= 0x01000,
60456007914Smarco 	SCB_PKT_SENSE		= 0x02000,
60556007914Smarco 	SCB_CMDPHASE_ABORT	= 0x04000,
60656007914Smarco 	SCB_ON_COL_LIST		= 0x08000,
60756007914Smarco 	SCB_SILENT		= 0x10000,/*
60856007914Smarco 					   * Be quiet about transmission type
60956007914Smarco 					   * errors.  They are expected and we
61056007914Smarco 					   * don't want to upset the user.  This
61156007914Smarco 					   * flag is typically used during DV.
61256007914Smarco 					   */
61356007914Smarco } scb_flag;
61456007914Smarco 
61556007914Smarco struct scb {
61694b57586Skrw 	TAILQ_ENTRY(scb)	  next;
61756007914Smarco 	struct	hardware_scb	 *hscb;
6184cc04bf0Skrw 	LIST_ENTRY(scb)		  timedout_links;
619d63d62f3Skrw 	struct scsi_xfer	 *xs;
62056007914Smarco 
62156007914Smarco 	struct ahd_softc	 *ahd_softc;
62256007914Smarco 	scb_flag		  flags;
62356007914Smarco 	bus_dmamap_t		  dmamap;
62456007914Smarco 	struct map_node		 *hscb_map;
62556007914Smarco 	struct map_node		 *sg_map;
62656007914Smarco 	struct map_node		 *sense_map;
62756007914Smarco 	void			 *sg_list;
62856007914Smarco 	uint8_t			 *sense_data;
62956007914Smarco 	bus_addr_t		  sg_list_busaddr;
63056007914Smarco 	bus_addr_t		  sense_busaddr;
63156007914Smarco 	u_int			  sg_count;/* How full ahd_dma_seg is */
63256007914Smarco #define	AHD_MAX_LQ_CRC_ERRORS 5
63356007914Smarco 	u_int			  crc_retry_count;
63456007914Smarco };
63556007914Smarco 
63656007914Smarco TAILQ_HEAD(scb_tailq, scb);
63756007914Smarco LIST_HEAD(scb_list, scb);
63856007914Smarco 
63956007914Smarco struct scb_data {
64056007914Smarco 	/*
64156007914Smarco 	 * SCBs that will not collide with any active device.
64256007914Smarco 	 */
64394b57586Skrw 	struct scb_tailq free_scbs;
64456007914Smarco 
64556007914Smarco 	/*
64656007914Smarco 	 * Mapping from tag to SCB.
64756007914Smarco 	 */
64856007914Smarco 	struct	scb *scbindex[AHD_SCB_MAX];
64956007914Smarco 
6504cc04bf0Skrw 	u_int		 recovery_scbs;	/* Transactions currently in recovery */
6514cc04bf0Skrw 
65256007914Smarco 	SLIST_HEAD(, map_node) hscb_maps;
65356007914Smarco 	SLIST_HEAD(, map_node) sg_maps;
65456007914Smarco 	SLIST_HEAD(, map_node) sense_maps;
65556007914Smarco 	int		 scbs_left;	/* unallocated scbs in head map_node */
65656007914Smarco 	int		 sgs_left;	/* unallocated sgs in head map_node */
65756007914Smarco 	int		 sense_left;	/* unallocated sense in head map_node */
65856007914Smarco 	uint16_t	 numscbs;
65956007914Smarco 	uint16_t	 maxhscbs;	/* Number of SCBs on the card */
66056007914Smarco 	uint8_t		 init_level;	/*
66156007914Smarco 					 * How far we've initialized
66256007914Smarco 					 * this structure.
66356007914Smarco 					 */
66456007914Smarco };
66556007914Smarco 
66656007914Smarco /************************ Target Mode Definitions *****************************/
66756007914Smarco 
66856007914Smarco /*
6694b1a56afSjsg  * Connection descriptor for select-in requests in target mode.
67056007914Smarco  */
67156007914Smarco struct target_cmd {
67256007914Smarco 	uint8_t scsiid;		/* Our ID and the initiator's ID */
67356007914Smarco 	uint8_t identify;	/* Identify message */
67456007914Smarco 	uint8_t bytes[22];	/*
67556007914Smarco 				 * Bytes contains any additional message
67656007914Smarco 				 * bytes terminated by 0xFF.  The remainder
67756007914Smarco 				 * is the cdb to execute.
67856007914Smarco 				 */
67956007914Smarco 	uint8_t cmd_valid;	/*
68056007914Smarco 				 * When a command is complete, the firmware
68156007914Smarco 				 * will set cmd_valid to all bits set.
68256007914Smarco 				 * After the host has seen the command,
68356007914Smarco 				 * the bits are cleared.  This allows us
68456007914Smarco 				 * to just peek at host memory to determine
68556007914Smarco 				 * if more work is complete. cmd_valid is on
68656007914Smarco 				 * an 8 byte boundary to simplify setting
68756007914Smarco 				 * it on aic7880 hardware which only has
68856007914Smarco 				 * limited direct access to the DMA FIFO.
68956007914Smarco 				 */
69056007914Smarco 	uint8_t pad[7];
69156007914Smarco };
69256007914Smarco 
69356007914Smarco /*
69456007914Smarco  * Number of events we can buffer up if we run out
69556007914Smarco  * of immediate notify ccbs.
69656007914Smarco  */
69756007914Smarco #define AHD_TMODE_EVENT_BUFFER_SIZE 8
69856007914Smarco struct ahd_tmode_event {
69956007914Smarco 	uint8_t initiator_id;
70056007914Smarco 	uint8_t event_type;	/* MSG type or EVENT_TYPE_BUS_RESET */
70156007914Smarco #define	EVENT_TYPE_BUS_RESET 0xFF
70256007914Smarco 	uint8_t event_arg;
70356007914Smarco };
70456007914Smarco 
70556007914Smarco /*
70656007914Smarco  * Per enabled lun target mode state.
70756007914Smarco  * As this state is directly influenced by the host OS'es target mode
70856007914Smarco  * environment, we let the OS module define it.  Forward declare the
70956007914Smarco  * structure here so we can store arrays of them, etc. in OS neutral
71056007914Smarco  * data structures.
71156007914Smarco  */
71256007914Smarco #ifdef AHD_TARGET_MODE
71356007914Smarco struct ahd_tmode_lstate {
71456007914Smarco 	struct ahd_tmode_event event_buffer[AHD_TMODE_EVENT_BUFFER_SIZE];
71556007914Smarco 	uint8_t event_r_idx;
71656007914Smarco 	uint8_t event_w_idx;
71756007914Smarco };
71856007914Smarco #else
71956007914Smarco struct ahd_tmode_lstate;
72056007914Smarco #endif
72156007914Smarco 
72256007914Smarco /******************** Transfer Negotiation Datastructures *********************/
7234b1a56afSjsg #define AHD_TRANS_CUR		0x01	/* Modify current negotiation status */
72456007914Smarco #define AHD_TRANS_ACTIVE	0x03	/* Assume this target is on the bus */
72556007914Smarco #define AHD_TRANS_GOAL		0x04	/* Modify negotiation goal */
72656007914Smarco #define AHD_TRANS_USER		0x08	/* Modify user negotiation settings */
72756007914Smarco #define AHD_PERIOD_10MHz	0x19
72856007914Smarco 
72956007914Smarco #define AHD_WIDTH_UNKNOWN	0xFF
73056007914Smarco #define AHD_PERIOD_UNKNOWN	0xFF
73156007914Smarco #define AHD_OFFSET_UNKNOWN	0xFF
73256007914Smarco #define AHD_PPR_OPTS_UNKNOWN	0xFF
73356007914Smarco 
73456007914Smarco /*
73556007914Smarco  * Transfer Negotiation Information.
73656007914Smarco  */
73756007914Smarco struct ahd_transinfo {
73856007914Smarco 	uint8_t protocol_version;	/* SCSI Revision level */
73956007914Smarco 	uint8_t transport_version;	/* SPI Revision level */
74056007914Smarco 	uint8_t width;			/* Bus width */
74156007914Smarco 	uint8_t period;			/* Sync rate factor */
74256007914Smarco 	uint8_t offset;			/* Sync offset */
74356007914Smarco 	uint8_t ppr_options;		/* Parallel Protocol Request options */
74456007914Smarco };
74556007914Smarco 
74656007914Smarco /*
74756007914Smarco  * Per-initiator current, goal and user transfer negotiation information. */
74856007914Smarco struct ahd_initiator_tinfo {
74956007914Smarco 	struct ahd_transinfo curr;
75056007914Smarco 	struct ahd_transinfo goal;
75156007914Smarco 	struct ahd_transinfo user;
75256007914Smarco };
75356007914Smarco 
75456007914Smarco /*
75556007914Smarco  * Per enabled target ID state.
75656007914Smarco  * Pointers to lun target state as well as sync/wide negotiation information
75756007914Smarco  * for each initiator<->target mapping.  For the initiator role we pretend
75856007914Smarco  * that we are the target and the targets are the initiators since the
75956007914Smarco  * negotiation is the same regardless of role.
76056007914Smarco  */
76156007914Smarco struct ahd_tmode_tstate {
76256007914Smarco 	struct ahd_tmode_lstate*	enabled_luns[AHD_NUM_LUNS];
76356007914Smarco 	struct ahd_initiator_tinfo	transinfo[AHD_NUM_TARGETS];
76456007914Smarco 
76556007914Smarco 	/*
76656007914Smarco 	 * Per initiator state bitmasks.
76756007914Smarco 	 */
76856007914Smarco 	uint16_t	 auto_negotiate;/* Auto Negotiation Required */
76956007914Smarco 	uint16_t	 discenable;	/* Disconnection allowed  */
77056007914Smarco 	uint16_t	 tagenable;	/* Tagged Queuing allowed */
77156007914Smarco };
77256007914Smarco 
77356007914Smarco /*
77456007914Smarco  * Points of interest along the negotiated transfer scale.
77556007914Smarco  */
77656007914Smarco #define AHD_SYNCRATE_160	0x8
77756007914Smarco #define AHD_SYNCRATE_PACED	0x8
77856007914Smarco #define AHD_SYNCRATE_DT		0x9
77956007914Smarco #define AHD_SYNCRATE_ULTRA2	0xa
78056007914Smarco #define AHD_SYNCRATE_ULTRA	0xc
78156007914Smarco #define AHD_SYNCRATE_FAST	0x19
78256007914Smarco #define AHD_SYNCRATE_MIN_DT	AHD_SYNCRATE_FAST
78356007914Smarco #define AHD_SYNCRATE_SYNC	0x32
78456007914Smarco #define AHD_SYNCRATE_MIN	0x60
78556007914Smarco #define	AHD_SYNCRATE_ASYNC	0xFF
78656007914Smarco #define AHD_SYNCRATE_MAX	AHD_SYNCRATE_160
78756007914Smarco 
78856007914Smarco /* Safe and valid period for async negotiations. */
78956007914Smarco #define	AHD_ASYNC_XFER_PERIOD	0x44
79056007914Smarco 
79156007914Smarco /*
79256007914Smarco  * In RevA, the synctable uses a 120MHz rate for the period
79356007914Smarco  * factor 8 and 160MHz for the period factor 7.  The 120MHz
79456007914Smarco  * rate never made it into the official SCSI spec, so we must
79556007914Smarco  * compensate when setting the negotiation table for Rev A
79656007914Smarco  * parts.
79756007914Smarco  */
79856007914Smarco #define AHD_SYNCRATE_REVA_120	0x8
79956007914Smarco #define AHD_SYNCRATE_REVA_160	0x7
80056007914Smarco 
80156007914Smarco /***************************** Lookup Tables **********************************/
80256007914Smarco /*
80356007914Smarco  * Phase -> name and message out response
80456007914Smarco  * to parity errors in each phase table.
80556007914Smarco  */
80656007914Smarco struct ahd_phase_table_entry {
80756007914Smarco         uint8_t phase;
80856007914Smarco         uint8_t mesg_out; /* Message response to parity errors */
80956007914Smarco 	char *phasemsg;
81056007914Smarco };
81156007914Smarco 
81256007914Smarco /************************** Serial EEPROM Format ******************************/
81356007914Smarco 
81456007914Smarco struct seeprom_config {
81556007914Smarco /*
81656007914Smarco  * Per SCSI ID Configuration Flags
81756007914Smarco  */
81856007914Smarco 	uint16_t device_flags[16];	/* words 0-15 */
81956007914Smarco #define		CFXFER		0x003F	/* synchronous transfer rate */
82056007914Smarco #define			CFXFER_ASYNC	0x3F
82156007914Smarco #define		CFQAS		0x0040	/* Negotiate QAS */
82256007914Smarco #define		CFPACKETIZED	0x0080	/* Negotiate Packetized Transfers */
82356007914Smarco #define		CFSTART		0x0100	/* send start unit SCSI command */
82456007914Smarco #define		CFINCBIOS	0x0200	/* include in BIOS scan */
82556007914Smarco #define		CFDISC		0x0400	/* enable disconnection */
82656007914Smarco #define		CFMULTILUNDEV	0x0800	/* Probe multiple luns in BIOS scan */
82756007914Smarco #define		CFWIDEB		0x1000	/* wide bus device */
82856007914Smarco #define		CFHOSTMANAGED	0x8000	/* Managed by a RAID controller */
82956007914Smarco 
83056007914Smarco /*
83156007914Smarco  * BIOS Control Bits
83256007914Smarco  */
83356007914Smarco 	uint16_t bios_control;		/* word 16 */
8349593dc34Smglocker #define		CFSUPREM	0x0001	/* support all removable drives */
8359593dc34Smglocker #define		CFSUPREMB	0x0002	/* support removable boot drives */
83656007914Smarco #define		CFBIOSSTATE	0x000C	/* BIOS Action State */
83756007914Smarco #define		    CFBS_DISABLED	0x00
83856007914Smarco #define		    CFBS_ENABLED	0x04
83956007914Smarco #define		    CFBS_DISABLED_SCAN	0x08
84056007914Smarco #define		CFENABLEDV	0x0010	/* Perform Domain Validation */
84156007914Smarco #define		CFCTRL_A	0x0020	/* BIOS displays Ctrl-A message */
84256007914Smarco #define		CFSPARITY	0x0040	/* SCSI parity */
84356007914Smarco #define		CFEXTEND	0x0080	/* extended translation enabled */
84456007914Smarco #define		CFBOOTCD	0x0100  /* Support Bootable CD-ROM */
84556007914Smarco #define		CFMSG_LEVEL	0x0600	/* BIOS Message Level */
84656007914Smarco #define			CFMSG_VERBOSE	0x0000
84756007914Smarco #define			CFMSG_SILENT	0x0200
84856007914Smarco #define			CFMSG_DIAG	0x0400
84956007914Smarco #define		CFRESETB	0x0800	/* reset SCSI bus at boot */
85056007914Smarco /*		UNUSED		0xf000	*/
85156007914Smarco 
85256007914Smarco /*
85356007914Smarco  * Host Adapter Control Bits
85456007914Smarco  */
85556007914Smarco 	uint16_t adapter_control;	/* word 17 */
85656007914Smarco #define		CFAUTOTERM	0x0001	/* Perform Auto termination */
85756007914Smarco #define		CFSTERM		0x0002	/* SCSI low byte termination */
85856007914Smarco #define		CFWSTERM	0x0004	/* SCSI high byte termination */
85956007914Smarco #define		CFSEAUTOTERM	0x0008	/* Ultra2 Perform secondary Auto Term*/
86056007914Smarco #define		CFSELOWTERM	0x0010	/* Ultra2 secondary low term */
86156007914Smarco #define		CFSEHIGHTERM	0x0020	/* Ultra2 secondary high term */
86256007914Smarco #define		CFSTPWLEVEL	0x0040	/* Termination level control */
86356007914Smarco #define		CFBIOSAUTOTERM	0x0080	/* Perform Auto termination */
86456007914Smarco #define		CFTERM_MENU	0x0100	/* BIOS displays termination menu */
86556007914Smarco #define		CFCLUSTERENB	0x8000	/* Cluster Enable */
86656007914Smarco 
86756007914Smarco /*
86856007914Smarco  * Bus Release Time, Host Adapter ID
86956007914Smarco  */
87056007914Smarco 	uint16_t brtime_id;		/* word 18 */
87156007914Smarco #define		CFSCSIID	0x000f	/* host adapter SCSI ID */
87256007914Smarco /*		UNUSED		0x00f0	*/
87356007914Smarco #define		CFBRTIME	0xff00	/* bus release time/PCI Latency Time */
87456007914Smarco 
87556007914Smarco /*
87656007914Smarco  * Maximum targets
87756007914Smarco  */
87856007914Smarco 	uint16_t max_targets;		/* word 19 */
87956007914Smarco #define		CFMAXTARG	0x00ff	/* maximum targets */
88056007914Smarco #define		CFBOOTLUN	0x0f00	/* Lun to boot from */
88156007914Smarco #define		CFBOOTID	0xf000	/* Target to boot from */
88256007914Smarco 	uint16_t res_1[10];		/* words 20-29 */
88356007914Smarco 	uint16_t signature;		/* BIOS Signature */
88456007914Smarco #define		CFSIGNATURE	0x400
88556007914Smarco 	uint16_t checksum;		/* word 31 */
88656007914Smarco };
88756007914Smarco 
88856007914Smarco /*
88956007914Smarco  * Vital Product Data used during POST and by the BIOS.
89056007914Smarco  */
89156007914Smarco struct vpd_config {
89256007914Smarco 	uint8_t  bios_flags;
89356007914Smarco #define		VPDMASTERBIOS	0x0001
89456007914Smarco #define		VPDBOOTHOST	0x0002
89556007914Smarco 	uint8_t  reserved_1[21];
89656007914Smarco 	uint8_t  resource_type;
89756007914Smarco 	uint8_t  resource_len[2];
89856007914Smarco 	uint8_t  resource_data[8];
89956007914Smarco 	uint8_t  vpd_tag;
90056007914Smarco 	uint16_t vpd_len;
90156007914Smarco 	uint8_t  vpd_keyword[2];
90256007914Smarco 	uint8_t  length;
90356007914Smarco 	uint8_t  revision;
90456007914Smarco 	uint8_t  device_flags;
90556007914Smarco 	uint8_t  termnation_menus[2];
90656007914Smarco 	uint8_t  fifo_threshold;
90756007914Smarco 	uint8_t  end_tag;
90856007914Smarco 	uint8_t  vpd_checksum;
90956007914Smarco 	uint16_t default_target_flags;
91056007914Smarco 	uint16_t default_bios_flags;
91156007914Smarco 	uint16_t default_ctrl_flags;
91256007914Smarco 	uint8_t  default_irq;
91356007914Smarco 	uint8_t  pci_lattime;
91456007914Smarco 	uint8_t  max_target;
91556007914Smarco 	uint8_t  boot_lun;
91656007914Smarco 	uint16_t signature;
91756007914Smarco 	uint8_t  reserved_2;
91856007914Smarco 	uint8_t  checksum;
91956007914Smarco 	uint8_t	 reserved_3[4];
92056007914Smarco };
92156007914Smarco 
92256007914Smarco /****************************** Flexport Logic ********************************/
92356007914Smarco #define FLXADDR_TERMCTL			0x0
92456007914Smarco #define		FLX_TERMCTL_ENSECHIGH	0x8
92556007914Smarco #define		FLX_TERMCTL_ENSECLOW	0x4
92656007914Smarco #define		FLX_TERMCTL_ENPRIHIGH	0x2
92756007914Smarco #define		FLX_TERMCTL_ENPRILOW	0x1
92856007914Smarco #define FLXADDR_ROMSTAT_CURSENSECTL	0x1
92956007914Smarco #define		FLX_ROMSTAT_SEECFG	0xF0
93056007914Smarco #define		FLX_ROMSTAT_EECFG	0x0F
93156007914Smarco #define		FLX_ROMSTAT_SEE_93C66	0x00
93256007914Smarco #define		FLX_ROMSTAT_SEE_NONE	0xF0
93356007914Smarco #define		FLX_ROMSTAT_EE_512x8	0x0
93456007914Smarco #define		FLX_ROMSTAT_EE_1MBx8	0x1
93556007914Smarco #define		FLX_ROMSTAT_EE_2MBx8	0x2
93656007914Smarco #define		FLX_ROMSTAT_EE_4MBx8	0x3
93756007914Smarco #define		FLX_ROMSTAT_EE_16MBx8	0x4
93856007914Smarco #define 		CURSENSE_ENB	0x1
93956007914Smarco #define	FLXADDR_FLEXSTAT		0x2
94056007914Smarco #define		FLX_FSTAT_BUSY		0x1
94156007914Smarco #define FLXADDR_CURRENT_STAT		0x4
94256007914Smarco #define		FLX_CSTAT_SEC_HIGH	0xC0
94356007914Smarco #define		FLX_CSTAT_SEC_LOW	0x30
94456007914Smarco #define		FLX_CSTAT_PRI_HIGH	0x0C
94556007914Smarco #define		FLX_CSTAT_PRI_LOW	0x03
94656007914Smarco #define		FLX_CSTAT_MASK		0x03
94756007914Smarco #define		FLX_CSTAT_SHIFT		2
94856007914Smarco #define		FLX_CSTAT_OKAY		0x0
94956007914Smarco #define		FLX_CSTAT_OVER		0x1
95056007914Smarco #define		FLX_CSTAT_UNDER		0x2
95156007914Smarco #define		FLX_CSTAT_INVALID	0x3
95256007914Smarco 
95356007914Smarco int		ahd_read_seeprom(struct ahd_softc *ahd, uint16_t *buf,
95456007914Smarco 				 u_int start_addr, u_int count, int bstream);
95556007914Smarco 
95656007914Smarco int		ahd_write_seeprom(struct ahd_softc *ahd, uint16_t *buf,
95756007914Smarco 				  u_int start_addr, u_int count);
95856007914Smarco int		ahd_wait_seeprom(struct ahd_softc *ahd);
95956007914Smarco int		ahd_verify_vpd_cksum(struct vpd_config *vpd);
96056007914Smarco int		ahd_verify_cksum(struct seeprom_config *sc);
96156007914Smarco int		ahd_acquire_seeprom(struct ahd_softc *ahd);
96256007914Smarco void		ahd_release_seeprom(struct ahd_softc *ahd);
96356007914Smarco 
96456007914Smarco /****************************  Message Buffer *********************************/
96556007914Smarco typedef enum {
96656007914Smarco 	MSG_FLAG_NONE			= 0x00,
96756007914Smarco 	MSG_FLAG_EXPECT_PPR_BUSFREE	= 0x01,
96856007914Smarco 	MSG_FLAG_IU_REQ_CHANGED		= 0x02,
96956007914Smarco 	MSG_FLAG_EXPECT_IDE_BUSFREE	= 0x04,
97056007914Smarco 	MSG_FLAG_EXPECT_QASREJ_BUSFREE	= 0x08,
97156007914Smarco 	MSG_FLAG_PACKETIZED		= 0x10
97256007914Smarco } ahd_msg_flags;
97356007914Smarco 
97456007914Smarco typedef enum {
97556007914Smarco 	MSG_TYPE_NONE			= 0x00,
97656007914Smarco 	MSG_TYPE_INITIATOR_MSGOUT	= 0x01,
97756007914Smarco 	MSG_TYPE_INITIATOR_MSGIN	= 0x02,
97856007914Smarco 	MSG_TYPE_TARGET_MSGOUT		= 0x03,
97956007914Smarco 	MSG_TYPE_TARGET_MSGIN		= 0x04
98056007914Smarco } ahd_msg_type;
98156007914Smarco 
98256007914Smarco typedef enum {
98356007914Smarco 	MSGLOOP_IN_PROG,
98456007914Smarco 	MSGLOOP_MSGCOMPLETE,
98556007914Smarco 	MSGLOOP_TERMINATED
98656007914Smarco } msg_loop_stat;
98756007914Smarco 
98856007914Smarco /*********************** Software Configuration Structure *********************/
98956007914Smarco struct ahd_suspend_channel_state {
99056007914Smarco 	uint8_t	scsiseq;
99156007914Smarco 	uint8_t	sxfrctl0;
99256007914Smarco 	uint8_t	sxfrctl1;
99356007914Smarco 	uint8_t	simode0;
99456007914Smarco 	uint8_t	simode1;
99556007914Smarco 	uint8_t	seltimer;
99656007914Smarco 	uint8_t	seqctl;
99756007914Smarco };
99856007914Smarco 
99956007914Smarco struct ahd_suspend_state {
100056007914Smarco 	struct	ahd_suspend_channel_state channel[2];
100156007914Smarco 	uint8_t	optionmode;
100256007914Smarco 	uint8_t	dscommand0;
100356007914Smarco 	uint8_t	dspcistatus;
100456007914Smarco 	/* hsmailbox */
100556007914Smarco 	uint8_t	crccontrol1;
100656007914Smarco 	uint8_t	scbbaddr;
100756007914Smarco 	/* Host and sequencer SCB counts */
100856007914Smarco 	uint8_t	dff_thrsh;
100956007914Smarco 	uint8_t	*scratch_ram;
101056007914Smarco 	uint8_t	*btt;
101156007914Smarco };
101256007914Smarco 
101356007914Smarco typedef void (*ahd_bus_intr_t)(struct ahd_softc *);
101456007914Smarco 
101556007914Smarco typedef enum {
101656007914Smarco 	AHD_MODE_DFF0,
101756007914Smarco 	AHD_MODE_DFF1,
101856007914Smarco 	AHD_MODE_CCHAN,
101956007914Smarco 	AHD_MODE_SCSI,
102056007914Smarco 	AHD_MODE_CFG,
102156007914Smarco 	AHD_MODE_UNKNOWN
102256007914Smarco } ahd_mode;
102356007914Smarco 
102456007914Smarco #define AHD_MK_MSK(x) (0x01 << (x))
102556007914Smarco #define AHD_MODE_DFF0_MSK	AHD_MK_MSK(AHD_MODE_DFF0)
102656007914Smarco #define AHD_MODE_DFF1_MSK	AHD_MK_MSK(AHD_MODE_DFF1)
102756007914Smarco #define AHD_MODE_CCHAN_MSK	AHD_MK_MSK(AHD_MODE_CCHAN)
102856007914Smarco #define AHD_MODE_SCSI_MSK	AHD_MK_MSK(AHD_MODE_SCSI)
102956007914Smarco #define AHD_MODE_CFG_MSK	AHD_MK_MSK(AHD_MODE_CFG)
103056007914Smarco #define AHD_MODE_UNKNOWN_MSK	AHD_MK_MSK(AHD_MODE_UNKNOWN)
103156007914Smarco #define AHD_MODE_ANY_MSK (~0)
103256007914Smarco 
103356007914Smarco typedef uint8_t ahd_mode_state;
103456007914Smarco 
103556007914Smarco typedef void ahd_callback_t (void *);
103656007914Smarco 
1037*fabcfecbSjsg struct ahd_completion {
103856007914Smarco 	uint16_t	tag;
103956007914Smarco 	uint8_t		sg_status;
104056007914Smarco 	uint8_t		valid_tag;
104156007914Smarco };
104256007914Smarco 
10432a53356eSkrw #define AIC_SCB_DATA(softc) (&(softc)->scb_data)
10442a53356eSkrw 
104556007914Smarco struct ahd_softc {
104656007914Smarco 	struct device		sc_dev;
104756007914Smarco 	struct device *		sc_child;
104856007914Smarco 
104956007914Smarco 	bus_space_tag_t		tags[2];
105056007914Smarco 	bus_space_handle_t	bshs[2];
105156007914Smarco 
105256007914Smarco 	bus_dma_tag_t		  buffer_dmat;   /* dmat for buffer I/O */
105356007914Smarco 	struct scb_data		scb_data;
105456007914Smarco 
105556007914Smarco 	struct hardware_scb	 *next_queued_hscb;
105656007914Smarco 	struct map_node		 *next_queued_hscb_map;
105756007914Smarco 
105856007914Smarco 	/*
105956007914Smarco 	 * SCBs that have been sent to the controller
106056007914Smarco 	 */
106194b57586Skrw 	TAILQ_HEAD(, scb)	  pending_scbs;
106256007914Smarco 
106356007914Smarco 	/*
10644cc04bf0Skrw 	 * SCBs whose timeout routine has been called.
10654cc04bf0Skrw 	 */
10664cc04bf0Skrw 	LIST_HEAD(, scb)	  timedout_scbs;
10674cc04bf0Skrw 
106894b57586Skrw 	struct mutex		  sc_scb_mtx;
106994b57586Skrw 	struct scsi_iopool	  sc_iopool;
107094b57586Skrw 
10714cc04bf0Skrw 	/*
107256007914Smarco 	 * Current register window mode information.
107356007914Smarco 	 */
107456007914Smarco 	ahd_mode		  dst_mode;
107556007914Smarco 	ahd_mode		  src_mode;
107656007914Smarco 
107756007914Smarco 	/*
107856007914Smarco 	 * Saved register window mode information
107956007914Smarco 	 * used for restore on next unpause.
108056007914Smarco 	 */
108156007914Smarco 	ahd_mode		  saved_dst_mode;
108256007914Smarco 	ahd_mode		  saved_src_mode;
108356007914Smarco 
108456007914Smarco 	/*
108556007914Smarco 	 * Platform specific device information.
108656007914Smarco 	 */
108756007914Smarco 	ahd_dev_softc_t		  dev_softc;
108856007914Smarco 
108956007914Smarco 	/*
109056007914Smarco 	 * Bus specific device information.
109156007914Smarco 	 */
109256007914Smarco 	ahd_bus_intr_t		  bus_intr;
109356007914Smarco 
109456007914Smarco 	/*
109556007914Smarco 	 * Target mode related state kept on a per enabled lun basis.
109656007914Smarco 	 * Targets that are not enabled will have null entries.
109756007914Smarco 	 * As an initiator, we keep one target entry for our initiator
109856007914Smarco 	 * ID to store our sync/wide transfer settings.
109956007914Smarco 	 */
110056007914Smarco 	struct ahd_tmode_tstate  *enabled_targets[AHD_NUM_TARGETS];
110156007914Smarco 
110256007914Smarco 	char			inited_target[AHD_NUM_TARGETS];
110356007914Smarco 
110456007914Smarco 	/*
110556007914Smarco 	 * The black hole device responsible for handling requests for
110656007914Smarco 	 * disabled luns on enabled targets.
110756007914Smarco 	 */
110856007914Smarco 	struct ahd_tmode_lstate  *black_hole;
110956007914Smarco 
111056007914Smarco 	/*
111156007914Smarco 	 * Device instance currently on the bus awaiting a continue TIO
11124b1a56afSjsg 	 * for a command that was not given the disconnect privilege.
111356007914Smarco 	 */
111456007914Smarco 	struct ahd_tmode_lstate  *pending_device;
111556007914Smarco 
111656007914Smarco 	/*
111756007914Smarco 	 * Timer handles for timer driven callbacks.
111856007914Smarco 	 */
1119f8f66019Skrw 	aic_timer_t		  reset_timer;
1120f8f66019Skrw 	aic_timer_t		  stat_timer;
112156007914Smarco 
112256007914Smarco 	/*
112356007914Smarco 	 * Statistics.
112456007914Smarco 	 */
11250a7a9938Skrw #define	AHD_STAT_UPDATE_MS	250
112656007914Smarco #define	AHD_STAT_BUCKETS	4
112756007914Smarco 	u_int			  cmdcmplt_bucket;
112856007914Smarco 	uint32_t		  cmdcmplt_counts[AHD_STAT_BUCKETS];
112956007914Smarco 	uint32_t		  cmdcmplt_total;
113056007914Smarco 
113156007914Smarco 	/*
113256007914Smarco 	 * Card characteristics
113356007914Smarco 	 */
113456007914Smarco 	ahd_chip		  chip;
113556007914Smarco 	ahd_feature		  features;
113656007914Smarco 	ahd_bug			  bugs;
113756007914Smarco 	ahd_flag		  flags;
113856007914Smarco 	struct seeprom_config	 *seep_config;
113956007914Smarco 
114056007914Smarco 	/* Command Queues */
114156007914Smarco 	struct ahd_completion    *qoutfifo;
114256007914Smarco 	uint16_t		  qoutfifonext;
114356007914Smarco 	uint16_t		  qoutfifonext_valid_tag;
114456007914Smarco 	uint16_t		  qinfifonext;
114556007914Smarco 	uint16_t		  qinfifo[AHD_SCB_MAX];
114656007914Smarco 
114756007914Smarco 	/*
114856007914Smarco 	 * Our qfreeze count.  The sequencer compares
114956007914Smarco 	 * this value with its own counter to determine
115056007914Smarco 	 * whether to allow selections to occur.
115156007914Smarco 	 */
115256007914Smarco 	uint16_t		  qfreeze_cnt;
115356007914Smarco 
115456007914Smarco 	/* Values to store in the SEQCTL register for pause and unpause */
115556007914Smarco 	uint8_t			  unpause;
115656007914Smarco 	uint8_t			  pause;
115756007914Smarco 
115856007914Smarco 	/* Critical Section Data */
115956007914Smarco 	struct cs		 *critical_sections;
116056007914Smarco 	u_int			  num_critical_sections;
116156007914Smarco 
116256007914Smarco 	/* Buffer for handling packetized bitbucket. */
116356007914Smarco 	uint8_t			 *overrun_buf;
116456007914Smarco 
116556007914Smarco 	/* Links for chaining softcs */
116656007914Smarco 	TAILQ_ENTRY(ahd_softc)	  links;
116756007914Smarco 
116856007914Smarco 	/* Channel Names ('A', 'B', etc.) */
116956007914Smarco 	char			  channel;
117056007914Smarco 
117156007914Smarco 	/* Initiator Bus ID */
117256007914Smarco 	uint8_t			  our_id;
117356007914Smarco 
117456007914Smarco 	/*
117556007914Smarco 	 * Target incoming command FIFO.
117656007914Smarco 	 */
117756007914Smarco 	struct target_cmd	 *targetcmds;
117856007914Smarco 	uint8_t			  tqinfifonext;
117956007914Smarco 
118056007914Smarco 	/*
11814b1a56afSjsg 	 * Cached version of the hs_mailbox so we can avoid
118256007914Smarco 	 * pausing the sequencer during mailbox updates.
118356007914Smarco 	 */
118456007914Smarco 	uint8_t			  hs_mailbox;
118556007914Smarco 
118656007914Smarco 	/*
118756007914Smarco 	 * Incoming and outgoing message handling.
118856007914Smarco 	 */
118956007914Smarco 	uint8_t			  send_msg_perror;
119056007914Smarco 	ahd_msg_flags		  msg_flags;
119156007914Smarco 	ahd_msg_type		  msg_type;
119256007914Smarco 	uint8_t			  msgout_buf[12];/* Message we are sending */
119356007914Smarco 	uint8_t			  msgin_buf[12];/* Message we are receiving */
119456007914Smarco 	u_int			  msgout_len;	/* Length of message to send */
119556007914Smarco 	u_int			  msgout_index;	/* Current index in msgout */
119656007914Smarco 	u_int			  msgin_index;	/* Current index in msgin */
119756007914Smarco 
119856007914Smarco 	/*
119956007914Smarco 	 * Mapping information for data structures shared
120056007914Smarco 	 * between the sequencer and kernel.
120156007914Smarco 	 */
120256007914Smarco 	bus_dma_tag_t		  parent_dmat;
120356007914Smarco 	struct map_node		  shared_data_map;
120456007914Smarco 
120556007914Smarco 	/* Information saved through suspend/resume cycles */
120656007914Smarco 	struct ahd_suspend_state  suspend_state;
120756007914Smarco 
120856007914Smarco 	/* Number of enabled target mode device on this card */
120956007914Smarco 	u_int			  enabled_luns;
121056007914Smarco 
121156007914Smarco 	/* Initialization level of this data structure */
121256007914Smarco 	u_int			  init_level;
121356007914Smarco 
121456007914Smarco 	/* PCI cacheline size. */
121556007914Smarco 	u_int			  pci_cachesize;
121656007914Smarco 
121756007914Smarco 	/* IO Cell Parameters */
121856007914Smarco 	uint8_t			  iocell_opts[AHD_NUM_PER_DEV_ANNEXCOLS];
121956007914Smarco 
122056007914Smarco 	u_int			  stack_size;
122156007914Smarco 	uint16_t		 *saved_stack;
122256007914Smarco 
122356007914Smarco 	/* Per-Unit descriptive information */
122456007914Smarco 	const char		 *bus_description;
122556007914Smarco 	char			 *name;
122656007914Smarco 	int			  unit;
122756007914Smarco 
122856007914Smarco 	/* Selection Timer settings */
122956007914Smarco 	int			  seltime;
123056007914Smarco 
123156007914Smarco 	/*
123256007914Smarco 	 * Interrupt coalescing settings.
123356007914Smarco 	 */
123456007914Smarco #define	AHD_INT_COALESCING_TIMER_DEFAULT		250 /*us*/
123556007914Smarco #define	AHD_INT_COALESCING_MAXCMDS_DEFAULT		10
123656007914Smarco #define	AHD_INT_COALESCING_MAXCMDS_MAX			127
123756007914Smarco #define	AHD_INT_COALESCING_MINCMDS_DEFAULT		5
123856007914Smarco #define	AHD_INT_COALESCING_MINCMDS_MAX			127
123956007914Smarco #define	AHD_INT_COALESCING_THRESHOLD_DEFAULT		2000
124056007914Smarco #define	AHD_INT_COALESCING_STOP_THRESHOLD_DEFAULT	1000
124156007914Smarco 	u_int			  int_coalescing_timer;
124256007914Smarco 	u_int			  int_coalescing_maxcmds;
124356007914Smarco 	u_int			  int_coalescing_mincmds;
124456007914Smarco 	u_int			  int_coalescing_threshold;
124556007914Smarco 	u_int			  int_coalescing_stop_threshold;
124656007914Smarco 
124756007914Smarco 	uint16_t		  user_discenable;/* Disconnection allowed  */
124856007914Smarco 	uint16_t		  user_tagenable;/* Tagged Queuing allowed */
124956007914Smarco 
125056007914Smarco 	/* Adapter interrupt routine */
125156007914Smarco 	void			  *ih;
12525137e462Skrw 	int			   pcix_off;
125356007914Smarco };
125456007914Smarco 
125556007914Smarco TAILQ_HEAD(ahd_softc_tailq, ahd_softc);
125656007914Smarco extern struct ahd_softc_tailq ahd_tailq;
125756007914Smarco 
125856007914Smarco /*************************** IO Cell Configuration ****************************/
125956007914Smarco #define	AHD_PRECOMP_SLEW_INDEX						\
126056007914Smarco     (AHD_ANNEXCOL_PRECOMP_SLEW - AHD_ANNEXCOL_PER_DEV0)
126156007914Smarco 
126256007914Smarco #define	AHD_AMPLITUDE_INDEX						\
126356007914Smarco     (AHD_ANNEXCOL_AMPLITUDE - AHD_ANNEXCOL_PER_DEV0)
126456007914Smarco 
126556007914Smarco #define AHD_SET_SLEWRATE(ahd, new_slew)					\
126656007914Smarco do {									\
126756007914Smarco     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_SLEWRATE_MASK;	\
126856007914Smarco     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] |=			\
126956007914Smarco 	(((new_slew) << AHD_SLEWRATE_SHIFT) & AHD_SLEWRATE_MASK);	\
127056007914Smarco } while (0)
127156007914Smarco 
127256007914Smarco #define AHD_SET_PRECOMP(ahd, new_pcomp)					\
127356007914Smarco do {									\
127456007914Smarco     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] &= ~AHD_PRECOMP_MASK;	\
127556007914Smarco     (ahd)->iocell_opts[AHD_PRECOMP_SLEW_INDEX] |=			\
127656007914Smarco 	(((new_pcomp) << AHD_PRECOMP_SHIFT) & AHD_PRECOMP_MASK);	\
127756007914Smarco } while (0)
127856007914Smarco 
127956007914Smarco #define AHD_SET_AMPLITUDE(ahd, new_amp)					\
128056007914Smarco do {									\
128156007914Smarco     (ahd)->iocell_opts[AHD_AMPLITUDE_INDEX] &= ~AHD_AMPLITUDE_MASK;	\
128256007914Smarco     (ahd)->iocell_opts[AHD_AMPLITUDE_INDEX] |=				\
128356007914Smarco 	(((new_amp) << AHD_AMPLITUDE_SHIFT) & AHD_AMPLITUDE_MASK);	\
128456007914Smarco } while (0)
128556007914Smarco 
128656007914Smarco /************************ Active Device Information ***************************/
128756007914Smarco typedef enum {
128856007914Smarco 	ROLE_UNKNOWN,
128956007914Smarco 	ROLE_INITIATOR,
129056007914Smarco 	ROLE_TARGET
129156007914Smarco } role_t;
129256007914Smarco 
129356007914Smarco struct ahd_devinfo {
129456007914Smarco 	int	 our_scsiid;
129556007914Smarco 	int	 target_offset;
129656007914Smarco 	uint16_t target_mask;
129756007914Smarco 	u_int	 target;
129856007914Smarco 	u_int	 lun;
129956007914Smarco 	char	 channel;
130056007914Smarco 	role_t	 role;		/*
130156007914Smarco 				 * Only guaranteed to be correct if not
130256007914Smarco 				 * in the busfree state.
130356007914Smarco 				 */
130456007914Smarco };
130556007914Smarco 
130656007914Smarco /****************************** PCI Structures ********************************/
130756007914Smarco #define AHD_PCI_IOADDR PCI_MAPREG_START        /* I/O BAR*/
130856007914Smarco #define AHD_PCI_MEMADDR (PCI_MAPREG_START + 4)  /* Memory BAR */
130956007914Smarco #define AHD_PCI_IOADDR1 (PCI_MAPREG_START + 12)/* Second I/O BAR */
131056007914Smarco 
131156007914Smarco typedef int (ahd_device_setup_t)(struct ahd_softc *, struct pci_attach_args *pa);
131256007914Smarco 
131356007914Smarco struct ahd_pci_identity {
131456007914Smarco 	uint64_t		 full_id;
131556007914Smarco 	uint64_t		 id_mask;
131656007914Smarco 	ahd_device_setup_t	*setup;
131756007914Smarco };
13187cde551cSkn extern const struct ahd_pci_identity ahd_pci_ident_table[];
131956007914Smarco extern const u_int ahd_num_pci_devs;
132056007914Smarco 
132156007914Smarco /*************************** Function Declarations ****************************/
132256007914Smarco /******************************************************************************/
132356007914Smarco void			ahd_reset_cmds_pending(struct ahd_softc *ahd);
132456007914Smarco u_int			ahd_find_busy_tcl(struct ahd_softc *ahd, u_int tcl);
1325d63d62f3Skrw void			ahd_busy_tcl(struct ahd_softc *ahd,
1326d63d62f3Skrw 				     u_int tcl, u_int busyid);
1327d63d62f3Skrw void			ahd_unbusy_tcl(struct ahd_softc *ahd, u_int tcl);
132856007914Smarco 
132956007914Smarco /***************************** PCI Front End *********************************/
133056007914Smarco const struct ahd_pci_identity * ahd_find_pci_device(pcireg_t, pcireg_t);
133156007914Smarco int	ahd_pci_test_register_access(struct ahd_softc *);
133256007914Smarco 
133356007914Smarco /************************** SCB and SCB queue management **********************/
133456007914Smarco int		ahd_probe_scbs(struct ahd_softc *);
133556007914Smarco void		ahd_qinfifo_requeue_tail(struct ahd_softc *ahd,
133656007914Smarco 					 struct scb *scb);
133756007914Smarco int		ahd_match_scb(struct ahd_softc *ahd, struct scb *scb,
133856007914Smarco 			      int target, char channel, int lun,
133956007914Smarco 			      u_int tag, role_t role);
134056007914Smarco 
134156007914Smarco /****************************** Initialization ********************************/
13424cc04bf0Skrw struct ahd_softc	*ahd_alloc(void *platform_arg, char *name);
134356007914Smarco int			 ahd_softc_init(struct ahd_softc *);
134456007914Smarco void			 ahd_controller_info(struct ahd_softc *ahd, char *buf,
134556007914Smarco 					     size_t bufsz);
134656007914Smarco int			 ahd_init(struct ahd_softc *ahd);
134756007914Smarco int			 ahd_default_config(struct ahd_softc *ahd);
134856007914Smarco int			 ahd_parse_vpddata(struct ahd_softc *ahd,
134956007914Smarco 					   struct vpd_config *vpd);
135056007914Smarco int			 ahd_parse_cfgdata(struct ahd_softc *ahd,
135156007914Smarco 					   struct seeprom_config *sc);
135256007914Smarco void			 ahd_intr_enable(struct ahd_softc *ahd, int enable);
135356007914Smarco void			 ahd_update_coalescing_values(struct ahd_softc *ahd,
135456007914Smarco 						      u_int timer,
135556007914Smarco 						      u_int maxcmds,
135656007914Smarco 						      u_int mincmds);
135756007914Smarco void			 ahd_enable_coalescing(struct ahd_softc *ahd,
135856007914Smarco 					       int enable);
135956007914Smarco void			 ahd_pause_and_flushwork(struct ahd_softc *ahd);
136056007914Smarco int			 ahd_suspend(struct ahd_softc *ahd);
136156007914Smarco int			 ahd_resume(struct ahd_softc *ahd);
136256007914Smarco void			 ahd_softc_insert(struct ahd_softc *);
136356007914Smarco struct ahd_softc	*ahd_find_softc(struct ahd_softc *ahd);
136456007914Smarco void			 ahd_set_unit(struct ahd_softc *, int);
136556007914Smarco void			 ahd_set_name(struct ahd_softc *, char *);
136694b57586Skrw void			*ahd_scb_alloc(void *);
136794b57586Skrw void			 ahd_scb_free(void *, void *);
136856007914Smarco void			 ahd_alloc_scbs(struct ahd_softc *ahd);
136956007914Smarco void			 ahd_free(struct ahd_softc *ahd);
137056007914Smarco int			 ahd_reset(struct ahd_softc *ahd, int reinit);
137156007914Smarco void			 ahd_shutdown(void *arg);
137256007914Smarco int			 ahd_write_flexport(struct ahd_softc *ahd,
137356007914Smarco 					    u_int addr, u_int value);
137456007914Smarco int			 ahd_read_flexport(struct ahd_softc *ahd, u_int addr,
137556007914Smarco 					   uint8_t *value);
137656007914Smarco int			 ahd_wait_flexport(struct ahd_softc *ahd);
137756007914Smarco 
137856007914Smarco /*************************** Interrupt Services *******************************/
137956007914Smarco void			ahd_pci_intr(struct ahd_softc *ahd);
138056007914Smarco void			ahd_clear_intstat(struct ahd_softc *ahd);
138156007914Smarco void			ahd_flush_qoutfifo(struct ahd_softc *ahd);
138256007914Smarco void			ahd_run_qoutfifo(struct ahd_softc *ahd);
138356007914Smarco #ifdef AHD_TARGET_MODE
138456007914Smarco void			ahd_run_tqinfifo(struct ahd_softc *ahd, int paused);
138556007914Smarco #endif
138656007914Smarco void			ahd_handle_hwerrint(struct ahd_softc *ahd);
138756007914Smarco void			ahd_handle_seqint(struct ahd_softc *ahd, u_int intstat);
138856007914Smarco void			ahd_handle_scsiint(struct ahd_softc *ahd,
138956007914Smarco 					   u_int intstat);
139056007914Smarco void			ahd_clear_critical_section(struct ahd_softc *ahd);
139156007914Smarco 
139256007914Smarco /***************************** Error Recovery *********************************/
139356007914Smarco typedef enum {
139456007914Smarco 	SEARCH_COMPLETE,
139556007914Smarco 	SEARCH_COUNT,
139656007914Smarco 	SEARCH_REMOVE,
139756007914Smarco 	SEARCH_PRINT
139856007914Smarco } ahd_search_action;
1399a9f3c8f5Skrw void			ahd_done_with_status(struct ahd_softc *ahd,
1400a9f3c8f5Skrw 					     struct scb *scb, uint32_t status);
140156007914Smarco int			ahd_search_qinfifo(struct ahd_softc *ahd, int target,
140256007914Smarco 					   char channel, int lun, u_int tag,
140356007914Smarco 					   role_t role, uint32_t status,
140456007914Smarco 					   ahd_search_action action);
140556007914Smarco void			ahd_freeze_devq(struct ahd_softc *ahd, struct scb *scb);
140656007914Smarco int			ahd_reset_channel(struct ahd_softc *ahd, char channel,
140756007914Smarco 					  int initiate_reset);
140856007914Smarco int			ahd_abort_scbs(struct ahd_softc *ahd, int target,
140956007914Smarco 				       char channel, int lun, u_int tag,
141056007914Smarco 				       role_t role, uint32_t status);
141156007914Smarco void			ahd_restart(struct ahd_softc *ahd);
141256007914Smarco void			ahd_clear_fifo(struct ahd_softc *ahd, u_int fifo);
141356007914Smarco void			ahd_handle_scb_status(struct ahd_softc *ahd,
141456007914Smarco 					      struct scb *scb);
141556007914Smarco void			ahd_handle_scsi_status(struct ahd_softc *ahd,
141656007914Smarco 					       struct scb *scb);
141756007914Smarco void			ahd_calc_residual(struct ahd_softc *ahd,
141856007914Smarco 					  struct scb *scb);
141956007914Smarco void			ahd_timeout(void *);
142056007914Smarco /*************************** Utility Functions ********************************/
142156007914Smarco struct ahd_phase_table_entry*
142256007914Smarco 			ahd_lookup_phase_entry(int phase);
142356007914Smarco void			ahd_compile_devinfo(struct ahd_devinfo *devinfo,
142456007914Smarco 					    u_int our_id, u_int target,
142556007914Smarco 					    u_int lun, char channel,
142656007914Smarco 					    role_t role);
142756007914Smarco /************************** Transfer Negotiation ******************************/
142856007914Smarco void			ahd_find_syncrate(struct ahd_softc *ahd, u_int *period,
142956007914Smarco 					  u_int *ppr_options, u_int maxsync);
143056007914Smarco void			ahd_validate_offset(struct ahd_softc *ahd,
143156007914Smarco 					    struct ahd_initiator_tinfo *tinfo,
143256007914Smarco 					    u_int period, u_int *offset,
143356007914Smarco 					    int wide, role_t role);
143456007914Smarco void			ahd_validate_width(struct ahd_softc *ahd,
143556007914Smarco 					   struct ahd_initiator_tinfo *tinfo,
143656007914Smarco 					   u_int *bus_width,
143756007914Smarco 					   role_t role);
143856007914Smarco void                    ahd_scb_devinfo(struct ahd_softc *,
143956007914Smarco                             struct ahd_devinfo *, struct scb *);
144056007914Smarco 
144156007914Smarco /*
144256007914Smarco  * Negotiation types.  These are used to qualify if we should renegotiate
144356007914Smarco  * even if our goal and current transport parameters are identical.
144456007914Smarco  */
144556007914Smarco typedef enum {
144656007914Smarco 	AHD_NEG_TO_GOAL,	/* Renegotiate only if goal and curr differ. */
144756007914Smarco 	AHD_NEG_IF_NON_ASYNC,	/* Renegotiate so long as goal is non-async. */
14484b1a56afSjsg 	AHD_NEG_ALWAYS		/* Renegotiate even if goal is async. */
144956007914Smarco } ahd_neg_type;
145056007914Smarco int			ahd_update_neg_request(struct ahd_softc*,
145156007914Smarco 					       struct ahd_devinfo*,
145256007914Smarco 					       struct ahd_tmode_tstate*,
145356007914Smarco 					       struct ahd_initiator_tinfo*,
145456007914Smarco 					       ahd_neg_type);
145556007914Smarco void			ahd_set_width(struct ahd_softc *ahd,
145656007914Smarco 				      struct ahd_devinfo *devinfo,
145756007914Smarco 				      u_int width, u_int type, int paused);
145856007914Smarco void			ahd_set_syncrate(struct ahd_softc *ahd,
145956007914Smarco 					 struct ahd_devinfo *devinfo,
146056007914Smarco 					 u_int period, u_int offset,
146156007914Smarco 					 u_int ppr_options,
146256007914Smarco 					 u_int type, int paused);
146356007914Smarco typedef enum {
146456007914Smarco 	AHD_QUEUE_NONE,
146556007914Smarco 	AHD_QUEUE_BASIC,
146656007914Smarco 	AHD_QUEUE_TAGGED
146756007914Smarco } ahd_queue_alg;
146856007914Smarco 
146956007914Smarco void			ahd_set_tags(struct ahd_softc *ahd,
147056007914Smarco 				     struct ahd_devinfo *devinfo,
147156007914Smarco 				     ahd_queue_alg alg);
147256007914Smarco 
147356007914Smarco /**************************** Target Mode *************************************/
147456007914Smarco #ifdef AHD_TARGET_MODE
147556007914Smarco void		ahd_send_lstate_events(struct ahd_softc *,
147656007914Smarco 				       struct ahd_tmode_lstate *);
147756007914Smarco void		ahd_handle_en_lun(struct ahd_softc *ahd,
147856007914Smarco 				  struct cam_sim *sim, union ccb *ccb);
147956007914Smarco cam_status	ahd_find_tmode_devs(struct ahd_softc *ahd,
148056007914Smarco 				    struct cam_sim *sim, union ccb *ccb,
148156007914Smarco 				    struct ahd_tmode_tstate **tstate,
148256007914Smarco 				    struct ahd_tmode_lstate **lstate,
148356007914Smarco 				    int notfound_failure);
148456007914Smarco #ifndef AHD_TMODE_ENABLE
148556007914Smarco #define AHD_TMODE_ENABLE 0
148656007914Smarco #endif
148756007914Smarco #endif
148856007914Smarco /******************************* Debug ***************************************/
148956007914Smarco #ifdef AHD_DEBUG
149056007914Smarco extern uint32_t ahd_debug;
14911e6e6ef9Smarco #define AHD_DEBUG_OPTS		0
149256007914Smarco #define AHD_SHOW_MISC		0x00001
149356007914Smarco #define AHD_SHOW_SENSE		0x00002
149456007914Smarco #define AHD_SHOW_RECOVERY	0x00004
149556007914Smarco #define AHD_DUMP_SEEPROM	0x00008
149656007914Smarco #define AHD_SHOW_TERMCTL	0x00010
149756007914Smarco #define AHD_SHOW_MEMORY		0x00020
149856007914Smarco #define AHD_SHOW_MESSAGES	0x00040
149956007914Smarco #define AHD_SHOW_MODEPTR	0x00080
150056007914Smarco #define AHD_SHOW_SELTO		0x00100
150156007914Smarco #define AHD_SHOW_FIFOS		0x00200
150256007914Smarco #define AHD_SHOW_QFULL		0x00400
150356007914Smarco #define	AHD_SHOW_DV		0x00800
150456007914Smarco #define AHD_SHOW_MASKED_ERRORS	0x01000
150556007914Smarco #define AHD_SHOW_QUEUE		0x02000
150656007914Smarco #define AHD_SHOW_TQIN		0x04000
150756007914Smarco #define AHD_SHOW_SG		0x08000
150856007914Smarco #define AHD_SHOW_INT_COALESCING	0x10000
150956007914Smarco #define AHD_DEBUG_SEQUENCER	0x20000
151056007914Smarco #endif
151156007914Smarco void			ahd_print_scb(struct scb *scb);
151256007914Smarco void			ahd_print_devinfo(struct ahd_softc *ahd,
151356007914Smarco 					  struct ahd_devinfo *devinfo);
151456007914Smarco void			ahd_dump_sglist(struct scb *scb);
151556007914Smarco void			ahd_dump_all_cards_state(void);
151656007914Smarco void			ahd_dump_card_state(struct ahd_softc *ahd);
151756007914Smarco int			ahd_print_register(ahd_reg_parse_entry_t *table,
151856007914Smarco 					   u_int num_entries,
151956007914Smarco 					   const char *name,
152056007914Smarco 					   u_int address,
152156007914Smarco 					   u_int value,
152256007914Smarco 					   u_int *cur_column,
152356007914Smarco 					   u_int wrap_point);
152456007914Smarco void			ahd_dump_scbs(struct ahd_softc *ahd);
15252a53356eSkrw 
152656007914Smarco #endif /* _AIC79XX_H_ */
1527