162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Core definitions and data structures shareable across OS platforms.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 1994-2001 Justin T. Gibbs.
562306a36Sopenharmony_ci * Copyright (c) 2000-2001 Adaptec Inc.
662306a36Sopenharmony_ci * All rights reserved.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
962306a36Sopenharmony_ci * modification, are permitted provided that the following conditions
1062306a36Sopenharmony_ci * are met:
1162306a36Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
1262306a36Sopenharmony_ci *    notice, this list of conditions, and the following disclaimer,
1362306a36Sopenharmony_ci *    without modification.
1462306a36Sopenharmony_ci * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1562306a36Sopenharmony_ci *    substantially similar to the "NO WARRANTY" disclaimer below
1662306a36Sopenharmony_ci *    ("Disclaimer") and any redistribution must be conditioned upon
1762306a36Sopenharmony_ci *    including a substantially similar Disclaimer requirement for further
1862306a36Sopenharmony_ci *    binary redistribution.
1962306a36Sopenharmony_ci * 3. Neither the names of the above-listed copyright holders nor the names
2062306a36Sopenharmony_ci *    of any contributors may be used to endorse or promote products derived
2162306a36Sopenharmony_ci *    from this software without specific prior written permission.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * Alternatively, this software may be distributed under the terms of the
2462306a36Sopenharmony_ci * GNU General Public License ("GPL") version 2 as published by the Free
2562306a36Sopenharmony_ci * Software Foundation.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * NO WARRANTY
2862306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2962306a36Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3062306a36Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3162306a36Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3262306a36Sopenharmony_ci * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3362306a36Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3462306a36Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3562306a36Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3662306a36Sopenharmony_ci * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3762306a36Sopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3862306a36Sopenharmony_ci * POSSIBILITY OF SUCH DAMAGES.
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * $Id: //depot/aic7xxx/aic7xxx/aic7xxx.h#85 $
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * $FreeBSD$
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci#ifndef _AIC7XXX_H_
4662306a36Sopenharmony_ci#define _AIC7XXX_H_
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* Register Definitions */
4962306a36Sopenharmony_ci#include "aic7xxx_reg.h"
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/************************* Forward Declarations *******************************/
5262306a36Sopenharmony_cistruct ahc_platform_data;
5362306a36Sopenharmony_cistruct scb_platform_data;
5462306a36Sopenharmony_cistruct seeprom_descriptor;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/****************************** Useful Macros *********************************/
5762306a36Sopenharmony_ci#ifndef TRUE
5862306a36Sopenharmony_ci#define TRUE 1
5962306a36Sopenharmony_ci#endif
6062306a36Sopenharmony_ci#ifndef FALSE
6162306a36Sopenharmony_ci#define FALSE 0
6262306a36Sopenharmony_ci#endif
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define ALL_CHANNELS '\0'
6562306a36Sopenharmony_ci#define ALL_TARGETS_MASK 0xFFFF
6662306a36Sopenharmony_ci#define INITIATOR_WILDCARD	(~0)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define SCSIID_TARGET(ahc, scsiid) \
6962306a36Sopenharmony_ci	(((scsiid) & ((((ahc)->features & AHC_TWIN) != 0) ? TWIN_TID : TID)) \
7062306a36Sopenharmony_ci	>> TID_SHIFT)
7162306a36Sopenharmony_ci#define SCSIID_OUR_ID(scsiid) \
7262306a36Sopenharmony_ci	((scsiid) & OID)
7362306a36Sopenharmony_ci#define SCSIID_CHANNEL(ahc, scsiid) \
7462306a36Sopenharmony_ci	((((ahc)->features & AHC_TWIN) != 0) \
7562306a36Sopenharmony_ci        ? ((((scsiid) & TWIN_CHNLB) != 0) ? 'B' : 'A') \
7662306a36Sopenharmony_ci       : 'A')
7762306a36Sopenharmony_ci#define	SCB_IS_SCSIBUS_B(ahc, scb) \
7862306a36Sopenharmony_ci	(SCSIID_CHANNEL(ahc, (scb)->hscb->scsiid) == 'B')
7962306a36Sopenharmony_ci#define	SCB_GET_OUR_ID(scb) \
8062306a36Sopenharmony_ci	SCSIID_OUR_ID((scb)->hscb->scsiid)
8162306a36Sopenharmony_ci#define	SCB_GET_TARGET(ahc, scb) \
8262306a36Sopenharmony_ci	SCSIID_TARGET((ahc), (scb)->hscb->scsiid)
8362306a36Sopenharmony_ci#define	SCB_GET_CHANNEL(ahc, scb) \
8462306a36Sopenharmony_ci	SCSIID_CHANNEL(ahc, (scb)->hscb->scsiid)
8562306a36Sopenharmony_ci#define	SCB_GET_LUN(scb) \
8662306a36Sopenharmony_ci	((scb)->hscb->lun & LID)
8762306a36Sopenharmony_ci#define SCB_GET_TARGET_OFFSET(ahc, scb)	\
8862306a36Sopenharmony_ci	(SCB_GET_TARGET(ahc, scb) + (SCB_IS_SCSIBUS_B(ahc, scb) ? 8 : 0))
8962306a36Sopenharmony_ci#define SCB_GET_TARGET_MASK(ahc, scb) \
9062306a36Sopenharmony_ci	(0x01 << (SCB_GET_TARGET_OFFSET(ahc, scb)))
9162306a36Sopenharmony_ci#ifdef AHC_DEBUG
9262306a36Sopenharmony_ci#define SCB_IS_SILENT(scb)					\
9362306a36Sopenharmony_ci	((ahc_debug & AHC_SHOW_MASKED_ERRORS) == 0		\
9462306a36Sopenharmony_ci      && (((scb)->flags & SCB_SILENT) != 0))
9562306a36Sopenharmony_ci#else
9662306a36Sopenharmony_ci#define SCB_IS_SILENT(scb)					\
9762306a36Sopenharmony_ci	(((scb)->flags & SCB_SILENT) != 0)
9862306a36Sopenharmony_ci#endif
9962306a36Sopenharmony_ci#define TCL_TARGET_OFFSET(tcl) \
10062306a36Sopenharmony_ci	((((tcl) >> 4) & TID) >> 4)
10162306a36Sopenharmony_ci#define TCL_LUN(tcl) \
10262306a36Sopenharmony_ci	(tcl & (AHC_NUM_LUNS - 1))
10362306a36Sopenharmony_ci#define BUILD_TCL(scsiid, lun) \
10462306a36Sopenharmony_ci	((lun) | (((scsiid) & TID) << 4))
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci#ifndef	AHC_TARGET_MODE
10762306a36Sopenharmony_ci#undef	AHC_TMODE_ENABLE
10862306a36Sopenharmony_ci#define	AHC_TMODE_ENABLE 0
10962306a36Sopenharmony_ci#endif
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**************************** Driver Constants ********************************/
11262306a36Sopenharmony_ci/*
11362306a36Sopenharmony_ci * The maximum number of supported targets.
11462306a36Sopenharmony_ci */
11562306a36Sopenharmony_ci#define AHC_NUM_TARGETS 16
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/*
11862306a36Sopenharmony_ci * The maximum number of supported luns.
11962306a36Sopenharmony_ci * The identify message only supports 64 luns in SPI3.
12062306a36Sopenharmony_ci * You can have 2^64 luns when information unit transfers are enabled,
12162306a36Sopenharmony_ci * but it is doubtful this driver will ever support IUTs.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_ci#define AHC_NUM_LUNS 64
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * The maximum transfer per S/G segment.
12762306a36Sopenharmony_ci */
12862306a36Sopenharmony_ci#define AHC_MAXTRANSFER_SIZE	 0x00ffffff	/* limited by 24bit counter */
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/*
13162306a36Sopenharmony_ci * The maximum amount of SCB storage in hardware on a controller.
13262306a36Sopenharmony_ci * This value represents an upper bound.  Controllers vary in the number
13362306a36Sopenharmony_ci * they actually support.
13462306a36Sopenharmony_ci */
13562306a36Sopenharmony_ci#define AHC_SCB_MAX	255
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/*
13862306a36Sopenharmony_ci * The maximum number of concurrent transactions supported per driver instance.
13962306a36Sopenharmony_ci * Sequencer Control Blocks (SCBs) store per-transaction information.  Although
14062306a36Sopenharmony_ci * the space for SCBs on the host adapter varies by model, the driver will
14162306a36Sopenharmony_ci * page the SCBs between host and controller memory as needed.  We are limited
14262306a36Sopenharmony_ci * to 253 because:
14362306a36Sopenharmony_ci * 	1) The 8bit nature of the RISC engine holds us to an 8bit value.
14462306a36Sopenharmony_ci * 	2) We reserve one value, 255, to represent the invalid element.
14562306a36Sopenharmony_ci *	3) Our input queue scheme requires one SCB to always be reserved
14662306a36Sopenharmony_ci *	   in advance of queuing any SCBs.  This takes us down to 254.
14762306a36Sopenharmony_ci *	4) To handle our output queue correctly on machines that only
14862306a36Sopenharmony_ci * 	   support 32bit stores, we must clear the array 4 bytes at a
14962306a36Sopenharmony_ci *	   time.  To avoid colliding with a DMA write from the sequencer,
15062306a36Sopenharmony_ci *	   we must be sure that 4 slots are empty when we write to clear
15162306a36Sopenharmony_ci *	   the queue.  This reduces us to 253 SCBs: 1 that just completed
15262306a36Sopenharmony_ci *	   and the known three additional empty slots in the queue that
15362306a36Sopenharmony_ci *	   precede it.
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_ci#define AHC_MAX_QUEUE	253
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci/*
15862306a36Sopenharmony_ci * The maximum amount of SCB storage we allocate in host memory.  This
15962306a36Sopenharmony_ci * number should reflect the 1 additional SCB we require to handle our
16062306a36Sopenharmony_ci * qinfifo mechanism.
16162306a36Sopenharmony_ci */
16262306a36Sopenharmony_ci#define AHC_SCB_MAX_ALLOC (AHC_MAX_QUEUE+1)
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci/*
16562306a36Sopenharmony_ci * Ring Buffer of incoming target commands.
16662306a36Sopenharmony_ci * We allocate 256 to simplify the logic in the sequencer
16762306a36Sopenharmony_ci * by using the natural wrap point of an 8bit counter.
16862306a36Sopenharmony_ci */
16962306a36Sopenharmony_ci#define AHC_TMODE_CMDS	256
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/* Reset line assertion time in us */
17262306a36Sopenharmony_ci#define AHC_BUSRESET_DELAY	25
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/******************* Chip Characteristics/Operating Settings  *****************/
17562306a36Sopenharmony_ci/*
17662306a36Sopenharmony_ci * Chip Type
17762306a36Sopenharmony_ci * The chip order is from least sophisticated to most sophisticated.
17862306a36Sopenharmony_ci */
17962306a36Sopenharmony_citypedef enum {
18062306a36Sopenharmony_ci	AHC_NONE	= 0x0000,
18162306a36Sopenharmony_ci	AHC_CHIPID_MASK	= 0x00FF,
18262306a36Sopenharmony_ci	AHC_AIC7770	= 0x0001,
18362306a36Sopenharmony_ci	AHC_AIC7850	= 0x0002,
18462306a36Sopenharmony_ci	AHC_AIC7855	= 0x0003,
18562306a36Sopenharmony_ci	AHC_AIC7859	= 0x0004,
18662306a36Sopenharmony_ci	AHC_AIC7860	= 0x0005,
18762306a36Sopenharmony_ci	AHC_AIC7870	= 0x0006,
18862306a36Sopenharmony_ci	AHC_AIC7880	= 0x0007,
18962306a36Sopenharmony_ci	AHC_AIC7895	= 0x0008,
19062306a36Sopenharmony_ci	AHC_AIC7895C	= 0x0009,
19162306a36Sopenharmony_ci	AHC_AIC7890	= 0x000a,
19262306a36Sopenharmony_ci	AHC_AIC7896	= 0x000b,
19362306a36Sopenharmony_ci	AHC_AIC7892	= 0x000c,
19462306a36Sopenharmony_ci	AHC_AIC7899	= 0x000d,
19562306a36Sopenharmony_ci	AHC_VL		= 0x0100,	/* Bus type VL */
19662306a36Sopenharmony_ci	AHC_EISA	= 0x0200,	/* Bus type EISA */
19762306a36Sopenharmony_ci	AHC_PCI		= 0x0400,	/* Bus type PCI */
19862306a36Sopenharmony_ci	AHC_BUS_MASK	= 0x0F00
19962306a36Sopenharmony_ci} ahc_chip;
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci/*
20262306a36Sopenharmony_ci * Features available in each chip type.
20362306a36Sopenharmony_ci */
20462306a36Sopenharmony_citypedef enum {
20562306a36Sopenharmony_ci	AHC_FENONE	= 0x00000,
20662306a36Sopenharmony_ci	AHC_ULTRA	= 0x00001,	/* Supports 20MHz Transfers */
20762306a36Sopenharmony_ci	AHC_ULTRA2	= 0x00002,	/* Supports 40MHz Transfers */
20862306a36Sopenharmony_ci	AHC_WIDE  	= 0x00004,	/* Wide Channel */
20962306a36Sopenharmony_ci	AHC_TWIN	= 0x00008,	/* Twin Channel */
21062306a36Sopenharmony_ci	AHC_MORE_SRAM	= 0x00010,	/* 80 bytes instead of 64 */
21162306a36Sopenharmony_ci	AHC_CMD_CHAN	= 0x00020,	/* Has a Command DMA Channel */
21262306a36Sopenharmony_ci	AHC_QUEUE_REGS	= 0x00040,	/* Has Queue management registers */
21362306a36Sopenharmony_ci	AHC_SG_PRELOAD	= 0x00080,	/* Can perform auto-SG preload */
21462306a36Sopenharmony_ci	AHC_SPIOCAP	= 0x00100,	/* Has a Serial Port I/O Cap Register */
21562306a36Sopenharmony_ci	AHC_MULTI_TID	= 0x00200,	/* Has bitmask of TIDs for select-in */
21662306a36Sopenharmony_ci	AHC_HS_MAILBOX	= 0x00400,	/* Has HS_MAILBOX register */
21762306a36Sopenharmony_ci	AHC_DT		= 0x00800,	/* Double Transition transfers */
21862306a36Sopenharmony_ci	AHC_NEW_TERMCTL	= 0x01000,	/* Newer termination scheme */
21962306a36Sopenharmony_ci	AHC_MULTI_FUNC	= 0x02000,	/* Multi-Function Twin Channel Device */
22062306a36Sopenharmony_ci	AHC_LARGE_SCBS	= 0x04000,	/* 64byte SCBs */
22162306a36Sopenharmony_ci	AHC_AUTORATE	= 0x08000,	/* Automatic update of SCSIRATE/OFFSET*/
22262306a36Sopenharmony_ci	AHC_AUTOPAUSE	= 0x10000,	/* Automatic pause on register access */
22362306a36Sopenharmony_ci	AHC_TARGETMODE	= 0x20000,	/* Has tested target mode support */
22462306a36Sopenharmony_ci	AHC_MULTIROLE	= 0x40000,	/* Space for two roles at a time */
22562306a36Sopenharmony_ci	AHC_REMOVABLE	= 0x80000,	/* Hot-Swap supported */
22662306a36Sopenharmony_ci	AHC_HVD		= 0x100000,	/* HVD rather than SE */
22762306a36Sopenharmony_ci	AHC_AIC7770_FE	= AHC_FENONE,
22862306a36Sopenharmony_ci	/*
22962306a36Sopenharmony_ci	 * The real 7850 does not support Ultra modes, but there are
23062306a36Sopenharmony_ci	 * several cards that use the generic 7850 PCI ID even though
23162306a36Sopenharmony_ci	 * they are using an Ultra capable chip (7859/7860).  We start
23262306a36Sopenharmony_ci	 * out with the AHC_ULTRA feature set and then check the DEVSTATUS
23362306a36Sopenharmony_ci	 * register to determine if the capability is really present.
23462306a36Sopenharmony_ci	 */
23562306a36Sopenharmony_ci	AHC_AIC7850_FE	= AHC_SPIOCAP|AHC_AUTOPAUSE|AHC_TARGETMODE|AHC_ULTRA,
23662306a36Sopenharmony_ci	AHC_AIC7860_FE	= AHC_AIC7850_FE,
23762306a36Sopenharmony_ci	AHC_AIC7870_FE	= AHC_TARGETMODE|AHC_AUTOPAUSE,
23862306a36Sopenharmony_ci	AHC_AIC7880_FE	= AHC_AIC7870_FE|AHC_ULTRA,
23962306a36Sopenharmony_ci	/*
24062306a36Sopenharmony_ci	 * Although we have space for both the initiator and
24162306a36Sopenharmony_ci	 * target roles on ULTRA2 chips, we currently disable
24262306a36Sopenharmony_ci	 * the initiator role to allow multi-scsi-id target mode
24362306a36Sopenharmony_ci	 * configurations.  We can only respond on the same SCSI
24462306a36Sopenharmony_ci	 * ID as our initiator role if we allow initiator operation.
24562306a36Sopenharmony_ci	 * At some point, we should add a configuration knob to
24662306a36Sopenharmony_ci	 * allow both roles to be loaded.
24762306a36Sopenharmony_ci	 */
24862306a36Sopenharmony_ci	AHC_AIC7890_FE	= AHC_MORE_SRAM|AHC_CMD_CHAN|AHC_ULTRA2
24962306a36Sopenharmony_ci			  |AHC_QUEUE_REGS|AHC_SG_PRELOAD|AHC_MULTI_TID
25062306a36Sopenharmony_ci			  |AHC_HS_MAILBOX|AHC_NEW_TERMCTL|AHC_LARGE_SCBS
25162306a36Sopenharmony_ci			  |AHC_TARGETMODE,
25262306a36Sopenharmony_ci	AHC_AIC7892_FE	= AHC_AIC7890_FE|AHC_DT|AHC_AUTORATE|AHC_AUTOPAUSE,
25362306a36Sopenharmony_ci	AHC_AIC7895_FE	= AHC_AIC7880_FE|AHC_MORE_SRAM|AHC_AUTOPAUSE
25462306a36Sopenharmony_ci			  |AHC_CMD_CHAN|AHC_MULTI_FUNC|AHC_LARGE_SCBS,
25562306a36Sopenharmony_ci	AHC_AIC7895C_FE	= AHC_AIC7895_FE|AHC_MULTI_TID,
25662306a36Sopenharmony_ci	AHC_AIC7896_FE	= AHC_AIC7890_FE|AHC_MULTI_FUNC,
25762306a36Sopenharmony_ci	AHC_AIC7899_FE	= AHC_AIC7892_FE|AHC_MULTI_FUNC
25862306a36Sopenharmony_ci} ahc_feature;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/*
26162306a36Sopenharmony_ci * Bugs in the silicon that we work around in software.
26262306a36Sopenharmony_ci */
26362306a36Sopenharmony_citypedef enum {
26462306a36Sopenharmony_ci	AHC_BUGNONE		= 0x00,
26562306a36Sopenharmony_ci	/*
26662306a36Sopenharmony_ci	 * On all chips prior to the U2 product line,
26762306a36Sopenharmony_ci	 * the WIDEODD S/G segment feature does not
26862306a36Sopenharmony_ci	 * work during scsi->HostBus transfers.
26962306a36Sopenharmony_ci	 */
27062306a36Sopenharmony_ci	AHC_TMODE_WIDEODD_BUG	= 0x01,
27162306a36Sopenharmony_ci	/*
27262306a36Sopenharmony_ci	 * On the aic7890/91 Rev 0 chips, the autoflush
27362306a36Sopenharmony_ci	 * feature does not work.  A manual flush of
27462306a36Sopenharmony_ci	 * the DMA FIFO is required.
27562306a36Sopenharmony_ci	 */
27662306a36Sopenharmony_ci	AHC_AUTOFLUSH_BUG	= 0x02,
27762306a36Sopenharmony_ci	/*
27862306a36Sopenharmony_ci	 * On many chips, cacheline streaming does not work.
27962306a36Sopenharmony_ci	 */
28062306a36Sopenharmony_ci	AHC_CACHETHEN_BUG	= 0x04,
28162306a36Sopenharmony_ci	/*
28262306a36Sopenharmony_ci	 * On the aic7896/97 chips, cacheline
28362306a36Sopenharmony_ci	 * streaming must be enabled.
28462306a36Sopenharmony_ci	 */
28562306a36Sopenharmony_ci	AHC_CACHETHEN_DIS_BUG	= 0x08,
28662306a36Sopenharmony_ci	/*
28762306a36Sopenharmony_ci	 * PCI 2.1 Retry failure on non-empty data fifo.
28862306a36Sopenharmony_ci	 */
28962306a36Sopenharmony_ci	AHC_PCI_2_1_RETRY_BUG	= 0x10,
29062306a36Sopenharmony_ci	/*
29162306a36Sopenharmony_ci	 * Controller does not handle cacheline residuals
29262306a36Sopenharmony_ci	 * properly on S/G segments if PCI MWI instructions
29362306a36Sopenharmony_ci	 * are allowed.
29462306a36Sopenharmony_ci	 */
29562306a36Sopenharmony_ci	AHC_PCI_MWI_BUG		= 0x20,
29662306a36Sopenharmony_ci	/*
29762306a36Sopenharmony_ci	 * An SCB upload using the SCB channel's
29862306a36Sopenharmony_ci	 * auto array entry copy feature may
29962306a36Sopenharmony_ci	 * corrupt data.  This appears to only
30062306a36Sopenharmony_ci	 * occur on 66MHz systems.
30162306a36Sopenharmony_ci	 */
30262306a36Sopenharmony_ci	AHC_SCBCHAN_UPLOAD_BUG	= 0x40
30362306a36Sopenharmony_ci} ahc_bug;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci/*
30662306a36Sopenharmony_ci * Configuration specific settings.
30762306a36Sopenharmony_ci * The driver determines these settings by probing the
30862306a36Sopenharmony_ci * chip/controller's configuration.
30962306a36Sopenharmony_ci */
31062306a36Sopenharmony_citypedef enum {
31162306a36Sopenharmony_ci	AHC_FNONE	      = 0x000,
31262306a36Sopenharmony_ci	AHC_PRIMARY_CHANNEL   = 0x003,  /*
31362306a36Sopenharmony_ci					 * The channel that should
31462306a36Sopenharmony_ci					 * be probed first.
31562306a36Sopenharmony_ci					 */
31662306a36Sopenharmony_ci	AHC_USEDEFAULTS	      = 0x004,  /*
31762306a36Sopenharmony_ci					 * For cards without an seeprom
31862306a36Sopenharmony_ci					 * or a BIOS to initialize the chip's
31962306a36Sopenharmony_ci					 * SRAM, we use the default target
32062306a36Sopenharmony_ci					 * settings.
32162306a36Sopenharmony_ci					 */
32262306a36Sopenharmony_ci	AHC_SEQUENCER_DEBUG   = 0x008,
32362306a36Sopenharmony_ci	AHC_SHARED_SRAM	      = 0x010,
32462306a36Sopenharmony_ci	AHC_LARGE_SEEPROM     = 0x020,  /* Uses C56_66 not C46 */
32562306a36Sopenharmony_ci	AHC_RESET_BUS_A	      = 0x040,
32662306a36Sopenharmony_ci	AHC_RESET_BUS_B	      = 0x080,
32762306a36Sopenharmony_ci	AHC_EXTENDED_TRANS_A  = 0x100,
32862306a36Sopenharmony_ci	AHC_EXTENDED_TRANS_B  = 0x200,
32962306a36Sopenharmony_ci	AHC_TERM_ENB_A	      = 0x400,
33062306a36Sopenharmony_ci	AHC_TERM_ENB_B	      = 0x800,
33162306a36Sopenharmony_ci	AHC_INITIATORROLE     = 0x1000,  /*
33262306a36Sopenharmony_ci					  * Allow initiator operations on
33362306a36Sopenharmony_ci					  * this controller.
33462306a36Sopenharmony_ci					  */
33562306a36Sopenharmony_ci	AHC_TARGETROLE	      = 0x2000,  /*
33662306a36Sopenharmony_ci					  * Allow target operations on this
33762306a36Sopenharmony_ci					  * controller.
33862306a36Sopenharmony_ci					  */
33962306a36Sopenharmony_ci	AHC_NEWEEPROM_FMT     = 0x4000,
34062306a36Sopenharmony_ci	AHC_TQINFIFO_BLOCKED  = 0x10000,  /* Blocked waiting for ATIOs */
34162306a36Sopenharmony_ci	AHC_INT50_SPEEDFLEX   = 0x20000,  /*
34262306a36Sopenharmony_ci					   * Internal 50pin connector
34362306a36Sopenharmony_ci					   * sits behind an aic3860
34462306a36Sopenharmony_ci					   */
34562306a36Sopenharmony_ci	AHC_SCB_BTT	      = 0x40000,  /*
34662306a36Sopenharmony_ci					   * The busy targets table is
34762306a36Sopenharmony_ci					   * stored in SCB space rather
34862306a36Sopenharmony_ci					   * than SRAM.
34962306a36Sopenharmony_ci					   */
35062306a36Sopenharmony_ci	AHC_BIOS_ENABLED      = 0x80000,
35162306a36Sopenharmony_ci	AHC_ALL_INTERRUPTS    = 0x100000,
35262306a36Sopenharmony_ci	AHC_PAGESCBS	      = 0x400000,  /* Enable SCB paging */
35362306a36Sopenharmony_ci	AHC_EDGE_INTERRUPT    = 0x800000,  /* Device uses edge triggered ints */
35462306a36Sopenharmony_ci	AHC_39BIT_ADDRESSING  = 0x1000000, /* Use 39 bit addressing scheme. */
35562306a36Sopenharmony_ci	AHC_LSCBS_ENABLED     = 0x2000000, /* 64Byte SCBs enabled */
35662306a36Sopenharmony_ci	AHC_SCB_CONFIG_USED   = 0x4000000, /* No SEEPROM but SCB2 had info. */
35762306a36Sopenharmony_ci	AHC_NO_BIOS_INIT      = 0x8000000, /* No BIOS left over settings. */
35862306a36Sopenharmony_ci	AHC_DISABLE_PCI_PERR  = 0x10000000,
35962306a36Sopenharmony_ci	AHC_HAS_TERM_LOGIC    = 0x20000000
36062306a36Sopenharmony_ci} ahc_flag;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci/************************* Hardware  SCB Definition ***************************/
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci/*
36562306a36Sopenharmony_ci * The driver keeps up to MAX_SCB scb structures per card in memory.  The SCB
36662306a36Sopenharmony_ci * consists of a "hardware SCB" mirroring the fields available on the card
36762306a36Sopenharmony_ci * and additional information the kernel stores for each transaction.
36862306a36Sopenharmony_ci *
36962306a36Sopenharmony_ci * To minimize space utilization, a portion of the hardware scb stores
37062306a36Sopenharmony_ci * different data during different portions of a SCSI transaction.
37162306a36Sopenharmony_ci * As initialized by the host driver for the initiator role, this area
37262306a36Sopenharmony_ci * contains the SCSI cdb (or a pointer to the  cdb) to be executed.  After
37362306a36Sopenharmony_ci * the cdb has been presented to the target, this area serves to store
37462306a36Sopenharmony_ci * residual transfer information and the SCSI status byte.
37562306a36Sopenharmony_ci * For the target role, the contents of this area do not change, but
37662306a36Sopenharmony_ci * still serve a different purpose than for the initiator role.  See
37762306a36Sopenharmony_ci * struct target_data for details.
37862306a36Sopenharmony_ci */
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci/*
38162306a36Sopenharmony_ci * Status information embedded in the shared poriton of
38262306a36Sopenharmony_ci * an SCB after passing the cdb to the target.  The kernel
38362306a36Sopenharmony_ci * driver will only read this data for transactions that
38462306a36Sopenharmony_ci * complete abnormally (non-zero status byte).
38562306a36Sopenharmony_ci */
38662306a36Sopenharmony_cistruct status_pkt {
38762306a36Sopenharmony_ci	uint32_t residual_datacnt;	/* Residual in the current S/G seg */
38862306a36Sopenharmony_ci	uint32_t residual_sg_ptr;	/* The next S/G for this transfer */
38962306a36Sopenharmony_ci	uint8_t	 scsi_status;		/* Standard SCSI status byte */
39062306a36Sopenharmony_ci};
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci/*
39362306a36Sopenharmony_ci * Target mode version of the shared data SCB segment.
39462306a36Sopenharmony_ci */
39562306a36Sopenharmony_cistruct target_data {
39662306a36Sopenharmony_ci	uint32_t residual_datacnt;	/* Residual in the current S/G seg */
39762306a36Sopenharmony_ci	uint32_t residual_sg_ptr;	/* The next S/G for this transfer */
39862306a36Sopenharmony_ci	uint8_t  scsi_status;		/* SCSI status to give to initiator */
39962306a36Sopenharmony_ci	uint8_t  target_phases;		/* Bitmap of phases to execute */
40062306a36Sopenharmony_ci	uint8_t  data_phase;		/* Data-In or Data-Out */
40162306a36Sopenharmony_ci	uint8_t  initiator_tag;		/* Initiator's transaction tag */
40262306a36Sopenharmony_ci};
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_cistruct hardware_scb {
40562306a36Sopenharmony_ci/*0*/	union {
40662306a36Sopenharmony_ci		/*
40762306a36Sopenharmony_ci		 * If the cdb is 12 bytes or less, we embed it directly
40862306a36Sopenharmony_ci		 * in the SCB.  For longer cdbs, we embed the address
40962306a36Sopenharmony_ci		 * of the cdb payload as seen by the chip and a DMA
41062306a36Sopenharmony_ci		 * is used to pull it in.
41162306a36Sopenharmony_ci		 */
41262306a36Sopenharmony_ci		uint8_t	 cdb[12];
41362306a36Sopenharmony_ci		uint32_t cdb_ptr;
41462306a36Sopenharmony_ci		struct	 status_pkt status;
41562306a36Sopenharmony_ci		struct	 target_data tdata;
41662306a36Sopenharmony_ci	} shared_data;
41762306a36Sopenharmony_ci/*
41862306a36Sopenharmony_ci * A word about residuals.
41962306a36Sopenharmony_ci * The scb is presented to the sequencer with the dataptr and datacnt
42062306a36Sopenharmony_ci * fields initialized to the contents of the first S/G element to
42162306a36Sopenharmony_ci * transfer.  The sgptr field is initialized to the bus address for
42262306a36Sopenharmony_ci * the S/G element that follows the first in the in core S/G array
42362306a36Sopenharmony_ci * or'ed with the SG_FULL_RESID flag.  Sgptr may point to an invalid
42462306a36Sopenharmony_ci * S/G entry for this transfer (single S/G element transfer with the
42562306a36Sopenharmony_ci * first elements address and length preloaded in the dataptr/datacnt
42662306a36Sopenharmony_ci * fields).  If no transfer is to occur, sgptr is set to SG_LIST_NULL.
42762306a36Sopenharmony_ci * The SG_FULL_RESID flag ensures that the residual will be correctly
42862306a36Sopenharmony_ci * noted even if no data transfers occur.  Once the data phase is entered,
42962306a36Sopenharmony_ci * the residual sgptr and datacnt are loaded from the sgptr and the
43062306a36Sopenharmony_ci * datacnt fields.  After each S/G element's dataptr and length are
43162306a36Sopenharmony_ci * loaded into the hardware, the residual sgptr is advanced.  After
43262306a36Sopenharmony_ci * each S/G element is expired, its datacnt field is checked to see
43362306a36Sopenharmony_ci * if the LAST_SEG flag is set.  If so, SG_LIST_NULL is set in the
43462306a36Sopenharmony_ci * residual sg ptr and the transfer is considered complete.  If the
43562306a36Sopenharmony_ci * sequencer determines that there is a residual in the tranfer, it
43662306a36Sopenharmony_ci * will set the SG_RESID_VALID flag in sgptr and dma the scb back into
43762306a36Sopenharmony_ci * host memory.  To sumarize:
43862306a36Sopenharmony_ci *
43962306a36Sopenharmony_ci * Sequencer:
44062306a36Sopenharmony_ci *	o A residual has occurred if SG_FULL_RESID is set in sgptr,
44162306a36Sopenharmony_ci *	  or residual_sgptr does not have SG_LIST_NULL set.
44262306a36Sopenharmony_ci *
44362306a36Sopenharmony_ci *	o We are transferring the last segment if residual_datacnt has
44462306a36Sopenharmony_ci *	  the SG_LAST_SEG flag set.
44562306a36Sopenharmony_ci *
44662306a36Sopenharmony_ci * Host:
44762306a36Sopenharmony_ci *	o A residual has occurred if a completed scb has the
44862306a36Sopenharmony_ci *	  SG_RESID_VALID flag set.
44962306a36Sopenharmony_ci *
45062306a36Sopenharmony_ci *	o residual_sgptr and sgptr refer to the "next" sg entry
45162306a36Sopenharmony_ci *	  and so may point beyond the last valid sg entry for the
45262306a36Sopenharmony_ci *	  transfer.
45362306a36Sopenharmony_ci */
45462306a36Sopenharmony_ci/*12*/	uint32_t dataptr;
45562306a36Sopenharmony_ci/*16*/	uint32_t datacnt;		/*
45662306a36Sopenharmony_ci					 * Byte 3 (numbered from 0) of
45762306a36Sopenharmony_ci					 * the datacnt is really the
45862306a36Sopenharmony_ci					 * 4th byte in that data address.
45962306a36Sopenharmony_ci					 */
46062306a36Sopenharmony_ci/*20*/	uint32_t sgptr;
46162306a36Sopenharmony_ci#define SG_PTR_MASK	0xFFFFFFF8
46262306a36Sopenharmony_ci/*24*/	uint8_t  control;	/* See SCB_CONTROL in aic7xxx.reg for details */
46362306a36Sopenharmony_ci/*25*/	uint8_t  scsiid;	/* what to load in the SCSIID register */
46462306a36Sopenharmony_ci/*26*/	uint8_t  lun;
46562306a36Sopenharmony_ci/*27*/	uint8_t  tag;			/*
46662306a36Sopenharmony_ci					 * Index into our kernel SCB array.
46762306a36Sopenharmony_ci					 * Also used as the tag for tagged I/O
46862306a36Sopenharmony_ci					 */
46962306a36Sopenharmony_ci/*28*/	uint8_t  cdb_len;
47062306a36Sopenharmony_ci/*29*/	uint8_t  scsirate;		/* Value for SCSIRATE register */
47162306a36Sopenharmony_ci/*30*/	uint8_t  scsioffset;		/* Value for SCSIOFFSET register */
47262306a36Sopenharmony_ci/*31*/	uint8_t  next;			/*
47362306a36Sopenharmony_ci					 * Used for threading SCBs in the
47462306a36Sopenharmony_ci					 * "Waiting for Selection" and
47562306a36Sopenharmony_ci					 * "Disconnected SCB" lists down
47662306a36Sopenharmony_ci					 * in the sequencer.
47762306a36Sopenharmony_ci					 */
47862306a36Sopenharmony_ci/*32*/	uint8_t  cdb32[32];		/*
47962306a36Sopenharmony_ci					 * CDB storage for cdbs of size
48062306a36Sopenharmony_ci					 * 13->32.  We store them here
48162306a36Sopenharmony_ci					 * because hardware scbs are
48262306a36Sopenharmony_ci					 * allocated from DMA safe
48362306a36Sopenharmony_ci					 * memory so we are guaranteed
48462306a36Sopenharmony_ci					 * the controller can access
48562306a36Sopenharmony_ci					 * this data.
48662306a36Sopenharmony_ci					 */
48762306a36Sopenharmony_ci};
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ci/************************ Kernel SCB Definitions ******************************/
49062306a36Sopenharmony_ci/*
49162306a36Sopenharmony_ci * Some fields of the SCB are OS dependent.  Here we collect the
49262306a36Sopenharmony_ci * definitions for elements that all OS platforms need to include
49362306a36Sopenharmony_ci * in there SCB definition.
49462306a36Sopenharmony_ci */
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci/*
49762306a36Sopenharmony_ci * Definition of a scatter/gather element as transferred to the controller.
49862306a36Sopenharmony_ci * The aic7xxx chips only support a 24bit length.  We use the top byte of
49962306a36Sopenharmony_ci * the length to store additional address bits and a flag to indicate
50062306a36Sopenharmony_ci * that a given segment terminates the transfer.  This gives us an
50162306a36Sopenharmony_ci * addressable range of 512GB on machines with 64bit PCI or with chips
50262306a36Sopenharmony_ci * that can support dual address cycles on 32bit PCI busses.
50362306a36Sopenharmony_ci */
50462306a36Sopenharmony_cistruct ahc_dma_seg {
50562306a36Sopenharmony_ci	uint32_t	addr;
50662306a36Sopenharmony_ci	uint32_t	len;
50762306a36Sopenharmony_ci#define	AHC_DMA_LAST_SEG	0x80000000
50862306a36Sopenharmony_ci#define	AHC_SG_HIGH_ADDR_MASK	0x7F000000
50962306a36Sopenharmony_ci#define	AHC_SG_LEN_MASK		0x00FFFFFF
51062306a36Sopenharmony_ci};
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_cistruct sg_map_node {
51362306a36Sopenharmony_ci	bus_dmamap_t		 sg_dmamap;
51462306a36Sopenharmony_ci	dma_addr_t		 sg_physaddr;
51562306a36Sopenharmony_ci	struct ahc_dma_seg*	 sg_vaddr;
51662306a36Sopenharmony_ci	SLIST_ENTRY(sg_map_node) links;
51762306a36Sopenharmony_ci};
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci/*
52062306a36Sopenharmony_ci * The current state of this SCB.
52162306a36Sopenharmony_ci */
52262306a36Sopenharmony_citypedef enum {
52362306a36Sopenharmony_ci	SCB_FREE		= 0x0000,
52462306a36Sopenharmony_ci	SCB_OTHERTCL_TIMEOUT	= 0x0002,/*
52562306a36Sopenharmony_ci					  * Another device was active
52662306a36Sopenharmony_ci					  * during the first timeout for
52762306a36Sopenharmony_ci					  * this SCB so we gave ourselves
52862306a36Sopenharmony_ci					  * an additional timeout period
52962306a36Sopenharmony_ci					  * in case it was hogging the
53062306a36Sopenharmony_ci					  * bus.
53162306a36Sopenharmony_ci				          */
53262306a36Sopenharmony_ci	SCB_DEVICE_RESET	= 0x0004,
53362306a36Sopenharmony_ci	SCB_SENSE		= 0x0008,
53462306a36Sopenharmony_ci	SCB_CDB32_PTR		= 0x0010,
53562306a36Sopenharmony_ci	SCB_RECOVERY_SCB	= 0x0020,
53662306a36Sopenharmony_ci	SCB_AUTO_NEGOTIATE	= 0x0040,/* Negotiate to achieve goal. */
53762306a36Sopenharmony_ci	SCB_NEGOTIATE		= 0x0080,/* Negotiation forced for command. */
53862306a36Sopenharmony_ci	SCB_ABORT		= 0x0100,
53962306a36Sopenharmony_ci	SCB_UNTAGGEDQ		= 0x0200,
54062306a36Sopenharmony_ci	SCB_ACTIVE		= 0x0400,
54162306a36Sopenharmony_ci	SCB_TARGET_IMMEDIATE	= 0x0800,
54262306a36Sopenharmony_ci	SCB_TRANSMISSION_ERROR	= 0x1000,/*
54362306a36Sopenharmony_ci					  * We detected a parity or CRC
54462306a36Sopenharmony_ci					  * error that has effected the
54562306a36Sopenharmony_ci					  * payload of the command.  This
54662306a36Sopenharmony_ci					  * flag is checked when normal
54762306a36Sopenharmony_ci					  * status is returned to catch
54862306a36Sopenharmony_ci					  * the case of a target not
54962306a36Sopenharmony_ci					  * responding to our attempt
55062306a36Sopenharmony_ci					  * to report the error.
55162306a36Sopenharmony_ci					  */
55262306a36Sopenharmony_ci	SCB_TARGET_SCB		= 0x2000,
55362306a36Sopenharmony_ci	SCB_SILENT		= 0x4000 /*
55462306a36Sopenharmony_ci					  * Be quiet about transmission type
55562306a36Sopenharmony_ci					  * errors.  They are expected and we
55662306a36Sopenharmony_ci					  * don't want to upset the user.  This
55762306a36Sopenharmony_ci					  * flag is typically used during DV.
55862306a36Sopenharmony_ci					  */
55962306a36Sopenharmony_ci} scb_flag;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_cistruct scb {
56262306a36Sopenharmony_ci	struct	hardware_scb	 *hscb;
56362306a36Sopenharmony_ci	union {
56462306a36Sopenharmony_ci		SLIST_ENTRY(scb)  sle;
56562306a36Sopenharmony_ci		TAILQ_ENTRY(scb)  tqe;
56662306a36Sopenharmony_ci	} links;
56762306a36Sopenharmony_ci	LIST_ENTRY(scb)		  pending_links;
56862306a36Sopenharmony_ci	ahc_io_ctx_t		  io_ctx;
56962306a36Sopenharmony_ci	struct ahc_softc	 *ahc_softc;
57062306a36Sopenharmony_ci	scb_flag		  flags;
57162306a36Sopenharmony_ci	struct scb_platform_data *platform_data;
57262306a36Sopenharmony_ci	struct sg_map_node	 *sg_map;
57362306a36Sopenharmony_ci	struct ahc_dma_seg 	 *sg_list;
57462306a36Sopenharmony_ci	dma_addr_t		  sg_list_phys;
57562306a36Sopenharmony_ci	u_int			  sg_count;/* How full ahc_dma_seg is */
57662306a36Sopenharmony_ci};
57762306a36Sopenharmony_ci
57862306a36Sopenharmony_cistruct scb_data {
57962306a36Sopenharmony_ci	SLIST_HEAD(, scb) free_scbs;	/*
58062306a36Sopenharmony_ci					 * Pool of SCBs ready to be assigned
58162306a36Sopenharmony_ci					 * commands to execute.
58262306a36Sopenharmony_ci					 */
58362306a36Sopenharmony_ci	struct	scb *scbindex[256];	/*
58462306a36Sopenharmony_ci					 * Mapping from tag to SCB.
58562306a36Sopenharmony_ci					 * As tag identifiers are an
58662306a36Sopenharmony_ci					 * 8bit value, we provide space
58762306a36Sopenharmony_ci					 * for all possible tag values.
58862306a36Sopenharmony_ci					 * Any lookups to entries at or
58962306a36Sopenharmony_ci					 * above AHC_SCB_MAX_ALLOC will
59062306a36Sopenharmony_ci					 * always fail.
59162306a36Sopenharmony_ci					 */
59262306a36Sopenharmony_ci	struct	hardware_scb	*hscbs;	/* Array of hardware SCBs */
59362306a36Sopenharmony_ci	struct	scb *scbarray;		/* Array of kernel SCBs */
59462306a36Sopenharmony_ci	struct	scsi_sense_data *sense; /* Per SCB sense data */
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	/*
59762306a36Sopenharmony_ci	 * "Bus" addresses of our data structures.
59862306a36Sopenharmony_ci	 */
59962306a36Sopenharmony_ci	bus_dma_tag_t	 hscb_dmat;	/* dmat for our hardware SCB array */
60062306a36Sopenharmony_ci	bus_dmamap_t	 hscb_dmamap;
60162306a36Sopenharmony_ci	dma_addr_t	 hscb_busaddr;
60262306a36Sopenharmony_ci	bus_dma_tag_t	 sense_dmat;
60362306a36Sopenharmony_ci	bus_dmamap_t	 sense_dmamap;
60462306a36Sopenharmony_ci	dma_addr_t	 sense_busaddr;
60562306a36Sopenharmony_ci	bus_dma_tag_t	 sg_dmat;	/* dmat for our sg segments */
60662306a36Sopenharmony_ci	SLIST_HEAD(, sg_map_node) sg_maps;
60762306a36Sopenharmony_ci	uint8_t	numscbs;
60862306a36Sopenharmony_ci	uint8_t	maxhscbs;		/* Number of SCBs on the card */
60962306a36Sopenharmony_ci	uint8_t	init_level;		/*
61062306a36Sopenharmony_ci					 * How far we've initialized
61162306a36Sopenharmony_ci					 * this structure.
61262306a36Sopenharmony_ci					 */
61362306a36Sopenharmony_ci};
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_ci/************************ Target Mode Definitions *****************************/
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ci/*
61862306a36Sopenharmony_ci * Connection descriptor for select-in requests in target mode.
61962306a36Sopenharmony_ci */
62062306a36Sopenharmony_cistruct target_cmd {
62162306a36Sopenharmony_ci	uint8_t scsiid;		/* Our ID and the initiator's ID */
62262306a36Sopenharmony_ci	uint8_t identify;	/* Identify message */
62362306a36Sopenharmony_ci	uint8_t bytes[22];	/*
62462306a36Sopenharmony_ci				 * Bytes contains any additional message
62562306a36Sopenharmony_ci				 * bytes terminated by 0xFF.  The remainder
62662306a36Sopenharmony_ci				 * is the cdb to execute.
62762306a36Sopenharmony_ci				 */
62862306a36Sopenharmony_ci	uint8_t cmd_valid;	/*
62962306a36Sopenharmony_ci				 * When a command is complete, the firmware
63062306a36Sopenharmony_ci				 * will set cmd_valid to all bits set.
63162306a36Sopenharmony_ci				 * After the host has seen the command,
63262306a36Sopenharmony_ci				 * the bits are cleared.  This allows us
63362306a36Sopenharmony_ci				 * to just peek at host memory to determine
63462306a36Sopenharmony_ci				 * if more work is complete. cmd_valid is on
63562306a36Sopenharmony_ci				 * an 8 byte boundary to simplify setting
63662306a36Sopenharmony_ci				 * it on aic7880 hardware which only has
63762306a36Sopenharmony_ci				 * limited direct access to the DMA FIFO.
63862306a36Sopenharmony_ci				 */
63962306a36Sopenharmony_ci	uint8_t pad[7];
64062306a36Sopenharmony_ci};
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci/*
64362306a36Sopenharmony_ci * Number of events we can buffer up if we run out
64462306a36Sopenharmony_ci * of immediate notify ccbs.
64562306a36Sopenharmony_ci */
64662306a36Sopenharmony_ci#define AHC_TMODE_EVENT_BUFFER_SIZE 8
64762306a36Sopenharmony_cistruct ahc_tmode_event {
64862306a36Sopenharmony_ci	uint8_t initiator_id;
64962306a36Sopenharmony_ci	uint8_t event_type;	/* MSG type or EVENT_TYPE_BUS_RESET */
65062306a36Sopenharmony_ci#define	EVENT_TYPE_BUS_RESET 0xFF
65162306a36Sopenharmony_ci	uint8_t event_arg;
65262306a36Sopenharmony_ci};
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci/*
65562306a36Sopenharmony_ci * Per enabled lun target mode state.
65662306a36Sopenharmony_ci * As this state is directly influenced by the host OS'es target mode
65762306a36Sopenharmony_ci * environment, we let the OS module define it.  Forward declare the
65862306a36Sopenharmony_ci * structure here so we can store arrays of them, etc. in OS neutral
65962306a36Sopenharmony_ci * data structures.
66062306a36Sopenharmony_ci */
66162306a36Sopenharmony_ci#ifdef AHC_TARGET_MODE
66262306a36Sopenharmony_cistruct ahc_tmode_lstate {
66362306a36Sopenharmony_ci	struct cam_path *path;
66462306a36Sopenharmony_ci	struct ccb_hdr_slist accept_tios;
66562306a36Sopenharmony_ci	struct ccb_hdr_slist immed_notifies;
66662306a36Sopenharmony_ci	struct ahc_tmode_event event_buffer[AHC_TMODE_EVENT_BUFFER_SIZE];
66762306a36Sopenharmony_ci	uint8_t event_r_idx;
66862306a36Sopenharmony_ci	uint8_t event_w_idx;
66962306a36Sopenharmony_ci};
67062306a36Sopenharmony_ci#else
67162306a36Sopenharmony_cistruct ahc_tmode_lstate;
67262306a36Sopenharmony_ci#endif
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci/******************** Transfer Negotiation Datastructures *********************/
67562306a36Sopenharmony_ci#define AHC_TRANS_CUR		0x01	/* Modify current neogtiation status */
67662306a36Sopenharmony_ci#define AHC_TRANS_ACTIVE	0x03	/* Assume this target is on the bus */
67762306a36Sopenharmony_ci#define AHC_TRANS_GOAL		0x04	/* Modify negotiation goal */
67862306a36Sopenharmony_ci#define AHC_TRANS_USER		0x08	/* Modify user negotiation settings */
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci#define AHC_WIDTH_UNKNOWN	0xFF
68162306a36Sopenharmony_ci#define AHC_PERIOD_UNKNOWN	0xFF
68262306a36Sopenharmony_ci#define AHC_OFFSET_UNKNOWN	0xFF
68362306a36Sopenharmony_ci#define AHC_PPR_OPTS_UNKNOWN	0xFF
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci/*
68662306a36Sopenharmony_ci * Transfer Negotiation Information.
68762306a36Sopenharmony_ci */
68862306a36Sopenharmony_cistruct ahc_transinfo {
68962306a36Sopenharmony_ci	uint8_t protocol_version;	/* SCSI Revision level */
69062306a36Sopenharmony_ci	uint8_t transport_version;	/* SPI Revision level */
69162306a36Sopenharmony_ci	uint8_t width;			/* Bus width */
69262306a36Sopenharmony_ci	uint8_t period;			/* Sync rate factor */
69362306a36Sopenharmony_ci	uint8_t offset;			/* Sync offset */
69462306a36Sopenharmony_ci	uint8_t ppr_options;		/* Parallel Protocol Request options */
69562306a36Sopenharmony_ci};
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci/*
69862306a36Sopenharmony_ci * Per-initiator current, goal and user transfer negotiation information. */
69962306a36Sopenharmony_cistruct ahc_initiator_tinfo {
70062306a36Sopenharmony_ci	uint8_t scsirate;		/* Computed value for SCSIRATE reg */
70162306a36Sopenharmony_ci	struct ahc_transinfo curr;
70262306a36Sopenharmony_ci	struct ahc_transinfo goal;
70362306a36Sopenharmony_ci	struct ahc_transinfo user;
70462306a36Sopenharmony_ci};
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_ci/*
70762306a36Sopenharmony_ci * Per enabled target ID state.
70862306a36Sopenharmony_ci * Pointers to lun target state as well as sync/wide negotiation information
70962306a36Sopenharmony_ci * for each initiator<->target mapping.  For the initiator role we pretend
71062306a36Sopenharmony_ci * that we are the target and the targets are the initiators since the
71162306a36Sopenharmony_ci * negotiation is the same regardless of role.
71262306a36Sopenharmony_ci */
71362306a36Sopenharmony_cistruct ahc_tmode_tstate {
71462306a36Sopenharmony_ci	struct ahc_tmode_lstate*	enabled_luns[AHC_NUM_LUNS];
71562306a36Sopenharmony_ci	struct ahc_initiator_tinfo	transinfo[AHC_NUM_TARGETS];
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ci	/*
71862306a36Sopenharmony_ci	 * Per initiator state bitmasks.
71962306a36Sopenharmony_ci	 */
72062306a36Sopenharmony_ci	uint16_t	 auto_negotiate;/* Auto Negotiation Required */
72162306a36Sopenharmony_ci	uint16_t	 ultraenb;	/* Using ultra sync rate  */
72262306a36Sopenharmony_ci	uint16_t	 discenable;	/* Disconnection allowed  */
72362306a36Sopenharmony_ci	uint16_t	 tagenable;	/* Tagged Queuing allowed */
72462306a36Sopenharmony_ci};
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci/*
72762306a36Sopenharmony_ci * Data structure for our table of allowed synchronous transfer rates.
72862306a36Sopenharmony_ci */
72962306a36Sopenharmony_cistruct ahc_syncrate {
73062306a36Sopenharmony_ci	u_int sxfr_u2;	/* Value of the SXFR parameter for Ultra2+ Chips */
73162306a36Sopenharmony_ci	u_int sxfr;	/* Value of the SXFR parameter for <= Ultra Chips */
73262306a36Sopenharmony_ci#define		ULTRA_SXFR 0x100	/* Rate Requires Ultra Mode set */
73362306a36Sopenharmony_ci#define		ST_SXFR	   0x010	/* Rate Single Transition Only */
73462306a36Sopenharmony_ci#define		DT_SXFR	   0x040	/* Rate Double Transition Only */
73562306a36Sopenharmony_ci	uint8_t period; /* Period to send to SCSI target */
73662306a36Sopenharmony_ci	const char *rate;
73762306a36Sopenharmony_ci};
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ci/* Safe and valid period for async negotiations. */
74062306a36Sopenharmony_ci#define	AHC_ASYNC_XFER_PERIOD 0x45
74162306a36Sopenharmony_ci#define	AHC_ULTRA2_XFER_PERIOD 0x0a
74262306a36Sopenharmony_ci
74362306a36Sopenharmony_ci/*
74462306a36Sopenharmony_ci * Indexes into our table of syncronous transfer rates.
74562306a36Sopenharmony_ci */
74662306a36Sopenharmony_ci#define AHC_SYNCRATE_DT		0
74762306a36Sopenharmony_ci#define AHC_SYNCRATE_ULTRA2	1
74862306a36Sopenharmony_ci#define AHC_SYNCRATE_ULTRA	3
74962306a36Sopenharmony_ci#define AHC_SYNCRATE_FAST	6
75062306a36Sopenharmony_ci#define AHC_SYNCRATE_MAX	AHC_SYNCRATE_DT
75162306a36Sopenharmony_ci#define	AHC_SYNCRATE_MIN	13
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_ci/***************************** Lookup Tables **********************************/
75462306a36Sopenharmony_ci/*
75562306a36Sopenharmony_ci * Phase -> name and message out response
75662306a36Sopenharmony_ci * to parity errors in each phase table.
75762306a36Sopenharmony_ci */
75862306a36Sopenharmony_cistruct ahc_phase_table_entry {
75962306a36Sopenharmony_ci        uint8_t phase;
76062306a36Sopenharmony_ci        uint8_t mesg_out; /* Message response to parity errors */
76162306a36Sopenharmony_ci	char *phasemsg;
76262306a36Sopenharmony_ci};
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci/************************** Serial EEPROM Format ******************************/
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_cistruct seeprom_config {
76762306a36Sopenharmony_ci/*
76862306a36Sopenharmony_ci * Per SCSI ID Configuration Flags
76962306a36Sopenharmony_ci */
77062306a36Sopenharmony_ci	uint16_t device_flags[16];	/* words 0-15 */
77162306a36Sopenharmony_ci#define		CFXFER		0x0007	/* synchronous transfer rate */
77262306a36Sopenharmony_ci#define		CFSYNCH		0x0008	/* enable synchronous transfer */
77362306a36Sopenharmony_ci#define		CFDISC		0x0010	/* enable disconnection */
77462306a36Sopenharmony_ci#define		CFWIDEB		0x0020	/* wide bus device */
77562306a36Sopenharmony_ci#define		CFSYNCHISULTRA	0x0040	/* CFSYNCH is an ultra offset (2940AU)*/
77662306a36Sopenharmony_ci#define		CFSYNCSINGLE	0x0080	/* Single-Transition signalling */
77762306a36Sopenharmony_ci#define		CFSTART		0x0100	/* send start unit SCSI command */
77862306a36Sopenharmony_ci#define		CFINCBIOS	0x0200	/* include in BIOS scan */
77962306a36Sopenharmony_ci#define		CFRNFOUND	0x0400	/* report even if not found */
78062306a36Sopenharmony_ci#define		CFMULTILUNDEV	0x0800	/* Probe multiple luns in BIOS scan */
78162306a36Sopenharmony_ci#define		CFWBCACHEENB	0x4000	/* Enable W-Behind Cache on disks */
78262306a36Sopenharmony_ci#define		CFWBCACHENOP	0xc000	/* Don't touch W-Behind Cache */
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ci/*
78562306a36Sopenharmony_ci * BIOS Control Bits
78662306a36Sopenharmony_ci */
78762306a36Sopenharmony_ci	uint16_t bios_control;		/* word 16 */
78862306a36Sopenharmony_ci#define		CFSUPREM	0x0001	/* support all removeable drives */
78962306a36Sopenharmony_ci#define		CFSUPREMB	0x0002	/* support removeable boot drives */
79062306a36Sopenharmony_ci#define		CFBIOSEN	0x0004	/* BIOS enabled */
79162306a36Sopenharmony_ci#define		CFBIOS_BUSSCAN	0x0008	/* Have the BIOS Scan the Bus */
79262306a36Sopenharmony_ci#define		CFSM2DRV	0x0010	/* support more than two drives */
79362306a36Sopenharmony_ci#define		CFSTPWLEVEL	0x0010	/* Termination level control */
79462306a36Sopenharmony_ci#define		CF284XEXTEND	0x0020	/* extended translation (284x cards) */
79562306a36Sopenharmony_ci#define		CFCTRL_A	0x0020	/* BIOS displays Ctrl-A message */
79662306a36Sopenharmony_ci#define		CFTERM_MENU	0x0040	/* BIOS displays termination menu */
79762306a36Sopenharmony_ci#define		CFEXTEND	0x0080	/* extended translation enabled */
79862306a36Sopenharmony_ci#define		CFSCAMEN	0x0100	/* SCAM enable */
79962306a36Sopenharmony_ci#define		CFMSG_LEVEL	0x0600	/* BIOS Message Level */
80062306a36Sopenharmony_ci#define			CFMSG_VERBOSE	0x0000
80162306a36Sopenharmony_ci#define			CFMSG_SILENT	0x0200
80262306a36Sopenharmony_ci#define			CFMSG_DIAG	0x0400
80362306a36Sopenharmony_ci#define		CFBOOTCD	0x0800  /* Support Bootable CD-ROM */
80462306a36Sopenharmony_ci/*		UNUSED		0xff00	*/
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci/*
80762306a36Sopenharmony_ci * Host Adapter Control Bits
80862306a36Sopenharmony_ci */
80962306a36Sopenharmony_ci	uint16_t adapter_control;	/* word 17 */
81062306a36Sopenharmony_ci#define		CFAUTOTERM	0x0001	/* Perform Auto termination */
81162306a36Sopenharmony_ci#define		CFULTRAEN	0x0002	/* Ultra SCSI speed enable */
81262306a36Sopenharmony_ci#define		CF284XSELTO     0x0003	/* Selection timeout (284x cards) */
81362306a36Sopenharmony_ci#define		CF284XFIFO      0x000C	/* FIFO Threshold (284x cards) */
81462306a36Sopenharmony_ci#define		CFSTERM		0x0004	/* SCSI low byte termination */
81562306a36Sopenharmony_ci#define		CFWSTERM	0x0008	/* SCSI high byte termination */
81662306a36Sopenharmony_ci#define		CFSPARITY	0x0010	/* SCSI parity */
81762306a36Sopenharmony_ci#define		CF284XSTERM     0x0020	/* SCSI low byte term (284x cards) */
81862306a36Sopenharmony_ci#define		CFMULTILUN	0x0020
81962306a36Sopenharmony_ci#define		CFRESETB	0x0040	/* reset SCSI bus at boot */
82062306a36Sopenharmony_ci#define		CFCLUSTERENB	0x0080	/* Cluster Enable */
82162306a36Sopenharmony_ci#define		CFBOOTCHAN	0x0300	/* probe this channel first */
82262306a36Sopenharmony_ci#define		CFBOOTCHANSHIFT 8
82362306a36Sopenharmony_ci#define		CFSEAUTOTERM	0x0400	/* Ultra2 Perform secondary Auto Term*/
82462306a36Sopenharmony_ci#define		CFSELOWTERM	0x0800	/* Ultra2 secondary low term */
82562306a36Sopenharmony_ci#define		CFSEHIGHTERM	0x1000	/* Ultra2 secondary high term */
82662306a36Sopenharmony_ci#define		CFENABLEDV	0x4000	/* Perform Domain Validation*/
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ci/*
82962306a36Sopenharmony_ci * Bus Release Time, Host Adapter ID
83062306a36Sopenharmony_ci */
83162306a36Sopenharmony_ci	uint16_t brtime_id;		/* word 18 */
83262306a36Sopenharmony_ci#define		CFSCSIID	0x000f	/* host adapter SCSI ID */
83362306a36Sopenharmony_ci/*		UNUSED		0x00f0	*/
83462306a36Sopenharmony_ci#define		CFBRTIME	0xff00	/* bus release time */
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci/*
83762306a36Sopenharmony_ci * Maximum targets
83862306a36Sopenharmony_ci */
83962306a36Sopenharmony_ci	uint16_t max_targets;		/* word 19 */
84062306a36Sopenharmony_ci#define		CFMAXTARG	0x00ff	/* maximum targets */
84162306a36Sopenharmony_ci#define		CFBOOTLUN	0x0f00	/* Lun to boot from */
84262306a36Sopenharmony_ci#define		CFBOOTID	0xf000	/* Target to boot from */
84362306a36Sopenharmony_ci	uint16_t res_1[10];		/* words 20-29 */
84462306a36Sopenharmony_ci	uint16_t signature;		/* Signature == 0x250 */
84562306a36Sopenharmony_ci#define		CFSIGNATURE	0x250
84662306a36Sopenharmony_ci#define		CFSIGNATURE2	0x300
84762306a36Sopenharmony_ci	uint16_t checksum;		/* word 31 */
84862306a36Sopenharmony_ci};
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_ci/****************************  Message Buffer *********************************/
85162306a36Sopenharmony_citypedef enum {
85262306a36Sopenharmony_ci	MSG_TYPE_NONE			= 0x00,
85362306a36Sopenharmony_ci	MSG_TYPE_INITIATOR_MSGOUT	= 0x01,
85462306a36Sopenharmony_ci	MSG_TYPE_INITIATOR_MSGIN	= 0x02,
85562306a36Sopenharmony_ci	MSG_TYPE_TARGET_MSGOUT		= 0x03,
85662306a36Sopenharmony_ci	MSG_TYPE_TARGET_MSGIN		= 0x04
85762306a36Sopenharmony_ci} ahc_msg_type;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_citypedef enum {
86062306a36Sopenharmony_ci	MSGLOOP_IN_PROG,
86162306a36Sopenharmony_ci	MSGLOOP_MSGCOMPLETE,
86262306a36Sopenharmony_ci	MSGLOOP_TERMINATED
86362306a36Sopenharmony_ci} msg_loop_stat;
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_ci/*********************** Software Configuration Structure *********************/
86662306a36Sopenharmony_ciTAILQ_HEAD(scb_tailq, scb);
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_cistruct ahc_aic7770_softc {
86962306a36Sopenharmony_ci	/*
87062306a36Sopenharmony_ci	 * Saved register state used for chip_init().
87162306a36Sopenharmony_ci	 */
87262306a36Sopenharmony_ci	uint8_t busspd;
87362306a36Sopenharmony_ci	uint8_t bustime;
87462306a36Sopenharmony_ci};
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_cistruct ahc_pci_softc {
87762306a36Sopenharmony_ci	/*
87862306a36Sopenharmony_ci	 * Saved register state used for chip_init().
87962306a36Sopenharmony_ci	 */
88062306a36Sopenharmony_ci	uint32_t  devconfig;
88162306a36Sopenharmony_ci	uint16_t  targcrccnt;
88262306a36Sopenharmony_ci	uint8_t   command;
88362306a36Sopenharmony_ci	uint8_t   csize_lattime;
88462306a36Sopenharmony_ci	uint8_t   optionmode;
88562306a36Sopenharmony_ci	uint8_t   crccontrol1;
88662306a36Sopenharmony_ci	uint8_t   dscommand0;
88762306a36Sopenharmony_ci	uint8_t   dspcistatus;
88862306a36Sopenharmony_ci	uint8_t   scbbaddr;
88962306a36Sopenharmony_ci	uint8_t   dff_thrsh;
89062306a36Sopenharmony_ci};
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ciunion ahc_bus_softc {
89362306a36Sopenharmony_ci	struct ahc_aic7770_softc aic7770_softc;
89462306a36Sopenharmony_ci	struct ahc_pci_softc pci_softc;
89562306a36Sopenharmony_ci};
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_citypedef void (*ahc_bus_intr_t)(struct ahc_softc *);
89862306a36Sopenharmony_citypedef int (*ahc_bus_chip_init_t)(struct ahc_softc *);
89962306a36Sopenharmony_citypedef void ahc_callback_t (void *);
90062306a36Sopenharmony_ci
90162306a36Sopenharmony_cistruct ahc_softc {
90262306a36Sopenharmony_ci	bus_space_tag_t           tag;
90362306a36Sopenharmony_ci	bus_space_handle_t        bsh;
90462306a36Sopenharmony_ci	struct scb_data		 *scb_data;
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	struct scb		 *next_queued_scb;
90762306a36Sopenharmony_ci
90862306a36Sopenharmony_ci	/*
90962306a36Sopenharmony_ci	 * SCBs that have been sent to the controller
91062306a36Sopenharmony_ci	 */
91162306a36Sopenharmony_ci	BSD_LIST_HEAD(, scb)	  pending_scbs;
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_ci	/*
91462306a36Sopenharmony_ci	 * Counting lock for deferring the release of additional
91562306a36Sopenharmony_ci	 * untagged transactions from the untagged_queues.  When
91662306a36Sopenharmony_ci	 * the lock is decremented to 0, all queues in the
91762306a36Sopenharmony_ci	 * untagged_queues array are run.
91862306a36Sopenharmony_ci	 */
91962306a36Sopenharmony_ci	u_int			  untagged_queue_lock;
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci	/*
92262306a36Sopenharmony_ci	 * Per-target queue of untagged-transactions.  The
92362306a36Sopenharmony_ci	 * transaction at the head of the queue is the
92462306a36Sopenharmony_ci	 * currently pending untagged transaction for the
92562306a36Sopenharmony_ci	 * target.  The driver only allows a single untagged
92662306a36Sopenharmony_ci	 * transaction per target.
92762306a36Sopenharmony_ci	 */
92862306a36Sopenharmony_ci	struct scb_tailq	  untagged_queues[AHC_NUM_TARGETS];
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	/*
93162306a36Sopenharmony_ci	 * Bus attachment specific data.
93262306a36Sopenharmony_ci	 */
93362306a36Sopenharmony_ci	union ahc_bus_softc	  bus_softc;
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci	/*
93662306a36Sopenharmony_ci	 * Platform specific data.
93762306a36Sopenharmony_ci	 */
93862306a36Sopenharmony_ci	struct ahc_platform_data *platform_data;
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	/*
94162306a36Sopenharmony_ci	 * Platform specific device information.
94262306a36Sopenharmony_ci	 */
94362306a36Sopenharmony_ci	ahc_dev_softc_t		  dev_softc;
94462306a36Sopenharmony_ci	struct device		  *dev;
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_ci	/*
94762306a36Sopenharmony_ci	 * Bus specific device information.
94862306a36Sopenharmony_ci	 */
94962306a36Sopenharmony_ci	ahc_bus_intr_t		  bus_intr;
95062306a36Sopenharmony_ci
95162306a36Sopenharmony_ci	/*
95262306a36Sopenharmony_ci	 * Bus specific initialization required
95362306a36Sopenharmony_ci	 * after a chip reset.
95462306a36Sopenharmony_ci	 */
95562306a36Sopenharmony_ci	ahc_bus_chip_init_t	  bus_chip_init;
95662306a36Sopenharmony_ci
95762306a36Sopenharmony_ci	/*
95862306a36Sopenharmony_ci	 * Target mode related state kept on a per enabled lun basis.
95962306a36Sopenharmony_ci	 * Targets that are not enabled will have null entries.
96062306a36Sopenharmony_ci	 * As an initiator, we keep one target entry for our initiator
96162306a36Sopenharmony_ci	 * ID to store our sync/wide transfer settings.
96262306a36Sopenharmony_ci	 */
96362306a36Sopenharmony_ci	struct ahc_tmode_tstate  *enabled_targets[AHC_NUM_TARGETS];
96462306a36Sopenharmony_ci
96562306a36Sopenharmony_ci	/*
96662306a36Sopenharmony_ci	 * The black hole device responsible for handling requests for
96762306a36Sopenharmony_ci	 * disabled luns on enabled targets.
96862306a36Sopenharmony_ci	 */
96962306a36Sopenharmony_ci	struct ahc_tmode_lstate  *black_hole;
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci	/*
97262306a36Sopenharmony_ci	 * Device instance currently on the bus awaiting a continue TIO
97362306a36Sopenharmony_ci	 * for a command that was not given the disconnect priveledge.
97462306a36Sopenharmony_ci	 */
97562306a36Sopenharmony_ci	struct ahc_tmode_lstate  *pending_device;
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci	/*
97862306a36Sopenharmony_ci	 * Card characteristics
97962306a36Sopenharmony_ci	 */
98062306a36Sopenharmony_ci	ahc_chip		  chip;
98162306a36Sopenharmony_ci	ahc_feature		  features;
98262306a36Sopenharmony_ci	ahc_bug			  bugs;
98362306a36Sopenharmony_ci	ahc_flag		  flags;
98462306a36Sopenharmony_ci	struct seeprom_config	 *seep_config;
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_ci	/* Values to store in the SEQCTL register for pause and unpause */
98762306a36Sopenharmony_ci	uint8_t			  unpause;
98862306a36Sopenharmony_ci	uint8_t			  pause;
98962306a36Sopenharmony_ci
99062306a36Sopenharmony_ci	/* Command Queues */
99162306a36Sopenharmony_ci	uint8_t			  qoutfifonext;
99262306a36Sopenharmony_ci	uint8_t			  qinfifonext;
99362306a36Sopenharmony_ci	uint8_t			 *qoutfifo;
99462306a36Sopenharmony_ci	uint8_t			 *qinfifo;
99562306a36Sopenharmony_ci
99662306a36Sopenharmony_ci	/* Critical Section Data */
99762306a36Sopenharmony_ci	struct cs		 *critical_sections;
99862306a36Sopenharmony_ci	u_int			  num_critical_sections;
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ci	/* Channel Names ('A', 'B', etc.) */
100162306a36Sopenharmony_ci	char			  channel;
100262306a36Sopenharmony_ci	char			  channel_b;
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci	/* Initiator Bus ID */
100562306a36Sopenharmony_ci	uint8_t			  our_id;
100662306a36Sopenharmony_ci	uint8_t			  our_id_b;
100762306a36Sopenharmony_ci
100862306a36Sopenharmony_ci	/*
100962306a36Sopenharmony_ci	 * PCI error detection.
101062306a36Sopenharmony_ci	 */
101162306a36Sopenharmony_ci	int			  unsolicited_ints;
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ci	/*
101462306a36Sopenharmony_ci	 * Target incoming command FIFO.
101562306a36Sopenharmony_ci	 */
101662306a36Sopenharmony_ci	struct target_cmd	 *targetcmds;
101762306a36Sopenharmony_ci	uint8_t			  tqinfifonext;
101862306a36Sopenharmony_ci
101962306a36Sopenharmony_ci	/*
102062306a36Sopenharmony_ci	 * Cached copy of the sequencer control register.
102162306a36Sopenharmony_ci	 */
102262306a36Sopenharmony_ci	uint8_t			  seqctl;
102362306a36Sopenharmony_ci
102462306a36Sopenharmony_ci	/*
102562306a36Sopenharmony_ci	 * Incoming and outgoing message handling.
102662306a36Sopenharmony_ci	 */
102762306a36Sopenharmony_ci	uint8_t			  send_msg_perror;
102862306a36Sopenharmony_ci	ahc_msg_type		  msg_type;
102962306a36Sopenharmony_ci	uint8_t			  msgout_buf[12];/* Message we are sending */
103062306a36Sopenharmony_ci	uint8_t			  msgin_buf[12];/* Message we are receiving */
103162306a36Sopenharmony_ci	u_int			  msgout_len;	/* Length of message to send */
103262306a36Sopenharmony_ci	u_int			  msgout_index;	/* Current index in msgout */
103362306a36Sopenharmony_ci	u_int			  msgin_index;	/* Current index in msgin */
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_ci	/*
103662306a36Sopenharmony_ci	 * Mapping information for data structures shared
103762306a36Sopenharmony_ci	 * between the sequencer and kernel.
103862306a36Sopenharmony_ci	 */
103962306a36Sopenharmony_ci	bus_dma_tag_t		  parent_dmat;
104062306a36Sopenharmony_ci	bus_dma_tag_t		  shared_data_dmat;
104162306a36Sopenharmony_ci	bus_dmamap_t		  shared_data_dmamap;
104262306a36Sopenharmony_ci	dma_addr_t		  shared_data_busaddr;
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	/*
104562306a36Sopenharmony_ci	 * Bus address of the one byte buffer used to
104662306a36Sopenharmony_ci	 * work-around a DMA bug for chips <= aic7880
104762306a36Sopenharmony_ci	 * in target mode.
104862306a36Sopenharmony_ci	 */
104962306a36Sopenharmony_ci	dma_addr_t		  dma_bug_buf;
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci	/* Number of enabled target mode device on this card */
105262306a36Sopenharmony_ci	u_int			  enabled_luns;
105362306a36Sopenharmony_ci
105462306a36Sopenharmony_ci	/* Initialization level of this data structure */
105562306a36Sopenharmony_ci	u_int			  init_level;
105662306a36Sopenharmony_ci
105762306a36Sopenharmony_ci	/* PCI cacheline size. */
105862306a36Sopenharmony_ci	u_int			  pci_cachesize;
105962306a36Sopenharmony_ci
106062306a36Sopenharmony_ci	/*
106162306a36Sopenharmony_ci	 * Count of parity errors we have seen as a target.
106262306a36Sopenharmony_ci	 * We auto-disable parity error checking after seeing
106362306a36Sopenharmony_ci	 * AHC_PCI_TARGET_PERR_THRESH number of errors.
106462306a36Sopenharmony_ci	 */
106562306a36Sopenharmony_ci	u_int			  pci_target_perr_count;
106662306a36Sopenharmony_ci#define		AHC_PCI_TARGET_PERR_THRESH	10
106762306a36Sopenharmony_ci
106862306a36Sopenharmony_ci	/* Maximum number of sequencer instructions supported. */
106962306a36Sopenharmony_ci	u_int			  instruction_ram_size;
107062306a36Sopenharmony_ci
107162306a36Sopenharmony_ci	/* Per-Unit descriptive information */
107262306a36Sopenharmony_ci	const char		 *description;
107362306a36Sopenharmony_ci	char			 *name;
107462306a36Sopenharmony_ci	int			  unit;
107562306a36Sopenharmony_ci
107662306a36Sopenharmony_ci	/* Selection Timer settings */
107762306a36Sopenharmony_ci	int			  seltime;
107862306a36Sopenharmony_ci	int			  seltime_b;
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci	uint16_t	 	  user_discenable;/* Disconnection allowed  */
108162306a36Sopenharmony_ci	uint16_t		  user_tagenable;/* Tagged Queuing allowed */
108262306a36Sopenharmony_ci};
108362306a36Sopenharmony_ci
108462306a36Sopenharmony_ci/************************ Active Device Information ***************************/
108562306a36Sopenharmony_citypedef enum {
108662306a36Sopenharmony_ci	ROLE_UNKNOWN,
108762306a36Sopenharmony_ci	ROLE_INITIATOR,
108862306a36Sopenharmony_ci	ROLE_TARGET
108962306a36Sopenharmony_ci} role_t;
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_cistruct ahc_devinfo {
109262306a36Sopenharmony_ci	int	 our_scsiid;
109362306a36Sopenharmony_ci	int	 target_offset;
109462306a36Sopenharmony_ci	uint16_t target_mask;
109562306a36Sopenharmony_ci	u_int	 target;
109662306a36Sopenharmony_ci	u_int	 lun;
109762306a36Sopenharmony_ci	char	 channel;
109862306a36Sopenharmony_ci	role_t	 role;		/*
109962306a36Sopenharmony_ci				 * Only guaranteed to be correct if not
110062306a36Sopenharmony_ci				 * in the busfree state.
110162306a36Sopenharmony_ci				 */
110262306a36Sopenharmony_ci};
110362306a36Sopenharmony_ci
110462306a36Sopenharmony_ci/****************************** PCI Structures ********************************/
110562306a36Sopenharmony_citypedef int (ahc_device_setup_t)(struct ahc_softc *);
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_cistruct ahc_pci_identity {
110862306a36Sopenharmony_ci	uint64_t		 full_id;
110962306a36Sopenharmony_ci	uint64_t		 id_mask;
111062306a36Sopenharmony_ci	const char		*name;
111162306a36Sopenharmony_ci	ahc_device_setup_t	*setup;
111262306a36Sopenharmony_ci};
111362306a36Sopenharmony_ci
111462306a36Sopenharmony_ci/***************************** VL/EISA Declarations ***************************/
111562306a36Sopenharmony_cistruct aic7770_identity {
111662306a36Sopenharmony_ci	uint32_t		 full_id;
111762306a36Sopenharmony_ci	uint32_t		 id_mask;
111862306a36Sopenharmony_ci	const char		*name;
111962306a36Sopenharmony_ci	ahc_device_setup_t	*setup;
112062306a36Sopenharmony_ci};
112162306a36Sopenharmony_ciextern struct aic7770_identity aic7770_ident_table[];
112262306a36Sopenharmony_ciextern const int ahc_num_aic7770_devs;
112362306a36Sopenharmony_ci
112462306a36Sopenharmony_ci#define AHC_EISA_SLOT_OFFSET	0xc00
112562306a36Sopenharmony_ci#define AHC_EISA_IOSIZE		0x100
112662306a36Sopenharmony_ci
112762306a36Sopenharmony_ci/*************************** Function Declarations ****************************/
112862306a36Sopenharmony_ci/******************************************************************************/
112962306a36Sopenharmony_ci
113062306a36Sopenharmony_ci/***************************** PCI Front End *********************************/
113162306a36Sopenharmony_ciconst struct ahc_pci_identity	*ahc_find_pci_device(ahc_dev_softc_t);
113262306a36Sopenharmony_ciint			 ahc_pci_config(struct ahc_softc *,
113362306a36Sopenharmony_ci					const struct ahc_pci_identity *);
113462306a36Sopenharmony_ciint			 ahc_pci_test_register_access(struct ahc_softc *);
113562306a36Sopenharmony_civoid __maybe_unused	 ahc_pci_resume(struct ahc_softc *ahc);
113662306a36Sopenharmony_ci
113762306a36Sopenharmony_ci/*************************** EISA/VL Front End ********************************/
113862306a36Sopenharmony_cistruct aic7770_identity *aic7770_find_device(uint32_t);
113962306a36Sopenharmony_ciint			 aic7770_config(struct ahc_softc *ahc,
114062306a36Sopenharmony_ci					struct aic7770_identity *,
114162306a36Sopenharmony_ci					u_int port);
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci/************************** SCB and SCB queue management **********************/
114462306a36Sopenharmony_ciint		ahc_probe_scbs(struct ahc_softc *);
114562306a36Sopenharmony_civoid		ahc_qinfifo_requeue_tail(struct ahc_softc *ahc,
114662306a36Sopenharmony_ci					 struct scb *scb);
114762306a36Sopenharmony_ciint		ahc_match_scb(struct ahc_softc *ahc, struct scb *scb,
114862306a36Sopenharmony_ci			      int target, char channel, int lun,
114962306a36Sopenharmony_ci			      u_int tag, role_t role);
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ci/****************************** Initialization ********************************/
115262306a36Sopenharmony_cistruct ahc_softc	*ahc_alloc(void *platform_arg, char *name);
115362306a36Sopenharmony_ciint			 ahc_softc_init(struct ahc_softc *);
115462306a36Sopenharmony_civoid			 ahc_controller_info(struct ahc_softc *ahc, char *buf);
115562306a36Sopenharmony_ciint			 ahc_chip_init(struct ahc_softc *ahc);
115662306a36Sopenharmony_ciint			 ahc_init(struct ahc_softc *ahc);
115762306a36Sopenharmony_civoid			 ahc_intr_enable(struct ahc_softc *ahc, int enable);
115862306a36Sopenharmony_civoid			 ahc_pause_and_flushwork(struct ahc_softc *ahc);
115962306a36Sopenharmony_ciint __maybe_unused	 ahc_suspend(struct ahc_softc *ahc);
116062306a36Sopenharmony_ciint __maybe_unused	 ahc_resume(struct ahc_softc *ahc);
116162306a36Sopenharmony_civoid			 ahc_set_unit(struct ahc_softc *, int);
116262306a36Sopenharmony_civoid			 ahc_set_name(struct ahc_softc *, char *);
116362306a36Sopenharmony_civoid			 ahc_free(struct ahc_softc *ahc);
116462306a36Sopenharmony_ciint			 ahc_reset(struct ahc_softc *ahc, int reinit);
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci/***************************** Error Recovery *********************************/
116762306a36Sopenharmony_citypedef enum {
116862306a36Sopenharmony_ci	SEARCH_COMPLETE,
116962306a36Sopenharmony_ci	SEARCH_COUNT,
117062306a36Sopenharmony_ci	SEARCH_REMOVE
117162306a36Sopenharmony_ci} ahc_search_action;
117262306a36Sopenharmony_ciint			ahc_search_qinfifo(struct ahc_softc *ahc, int target,
117362306a36Sopenharmony_ci					   char channel, int lun, u_int tag,
117462306a36Sopenharmony_ci					   role_t role, uint32_t status,
117562306a36Sopenharmony_ci					   ahc_search_action action);
117662306a36Sopenharmony_ciint			ahc_search_untagged_queues(struct ahc_softc *ahc,
117762306a36Sopenharmony_ci						   ahc_io_ctx_t ctx,
117862306a36Sopenharmony_ci						   int target, char channel,
117962306a36Sopenharmony_ci						   int lun, uint32_t status,
118062306a36Sopenharmony_ci						   ahc_search_action action);
118162306a36Sopenharmony_ciint			ahc_search_disc_list(struct ahc_softc *ahc, int target,
118262306a36Sopenharmony_ci					     char channel, int lun, u_int tag,
118362306a36Sopenharmony_ci					     int stop_on_first, int remove,
118462306a36Sopenharmony_ci					     int save_state);
118562306a36Sopenharmony_ciint			ahc_reset_channel(struct ahc_softc *ahc, char channel,
118662306a36Sopenharmony_ci					  int initiate_reset);
118762306a36Sopenharmony_ci
118862306a36Sopenharmony_ci/*************************** Utility Functions ********************************/
118962306a36Sopenharmony_civoid			ahc_compile_devinfo(struct ahc_devinfo *devinfo,
119062306a36Sopenharmony_ci					    u_int our_id, u_int target,
119162306a36Sopenharmony_ci					    u_int lun, char channel,
119262306a36Sopenharmony_ci					    role_t role);
119362306a36Sopenharmony_ci/************************** Transfer Negotiation ******************************/
119462306a36Sopenharmony_ciconst struct ahc_syncrate*	ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
119562306a36Sopenharmony_ci					  u_int *ppr_options, u_int maxsync);
119662306a36Sopenharmony_ciu_int			ahc_find_period(struct ahc_softc *ahc,
119762306a36Sopenharmony_ci					u_int scsirate, u_int maxsync);
119862306a36Sopenharmony_ci/*
119962306a36Sopenharmony_ci * Negotiation types.  These are used to qualify if we should renegotiate
120062306a36Sopenharmony_ci * even if our goal and current transport parameters are identical.
120162306a36Sopenharmony_ci */
120262306a36Sopenharmony_citypedef enum {
120362306a36Sopenharmony_ci	AHC_NEG_TO_GOAL,	/* Renegotiate only if goal and curr differ. */
120462306a36Sopenharmony_ci	AHC_NEG_IF_NON_ASYNC,	/* Renegotiate so long as goal is non-async. */
120562306a36Sopenharmony_ci	AHC_NEG_ALWAYS		/* Renegotiat even if goal is async. */
120662306a36Sopenharmony_ci} ahc_neg_type;
120762306a36Sopenharmony_ciint			ahc_update_neg_request(struct ahc_softc*,
120862306a36Sopenharmony_ci					       struct ahc_devinfo*,
120962306a36Sopenharmony_ci					       struct ahc_tmode_tstate*,
121062306a36Sopenharmony_ci					       struct ahc_initiator_tinfo*,
121162306a36Sopenharmony_ci					       ahc_neg_type);
121262306a36Sopenharmony_civoid			ahc_set_width(struct ahc_softc *ahc,
121362306a36Sopenharmony_ci				      struct ahc_devinfo *devinfo,
121462306a36Sopenharmony_ci				      u_int width, u_int type, int paused);
121562306a36Sopenharmony_civoid			ahc_set_syncrate(struct ahc_softc *ahc,
121662306a36Sopenharmony_ci					 struct ahc_devinfo *devinfo,
121762306a36Sopenharmony_ci					 const struct ahc_syncrate *syncrate,
121862306a36Sopenharmony_ci					 u_int period, u_int offset,
121962306a36Sopenharmony_ci					 u_int ppr_options,
122062306a36Sopenharmony_ci					 u_int type, int paused);
122162306a36Sopenharmony_citypedef enum {
122262306a36Sopenharmony_ci	AHC_QUEUE_NONE,
122362306a36Sopenharmony_ci	AHC_QUEUE_BASIC,
122462306a36Sopenharmony_ci	AHC_QUEUE_TAGGED
122562306a36Sopenharmony_ci} ahc_queue_alg;
122662306a36Sopenharmony_ci
122762306a36Sopenharmony_ci/**************************** Target Mode *************************************/
122862306a36Sopenharmony_ci#ifdef AHC_TARGET_MODE
122962306a36Sopenharmony_civoid		ahc_send_lstate_events(struct ahc_softc *,
123062306a36Sopenharmony_ci				       struct ahc_tmode_lstate *);
123162306a36Sopenharmony_civoid		ahc_handle_en_lun(struct ahc_softc *ahc,
123262306a36Sopenharmony_ci				  struct cam_sim *sim, union ccb *ccb);
123362306a36Sopenharmony_cicam_status	ahc_find_tmode_devs(struct ahc_softc *ahc,
123462306a36Sopenharmony_ci				    struct cam_sim *sim, union ccb *ccb,
123562306a36Sopenharmony_ci				    struct ahc_tmode_tstate **tstate,
123662306a36Sopenharmony_ci				    struct ahc_tmode_lstate **lstate,
123762306a36Sopenharmony_ci				    int notfound_failure);
123862306a36Sopenharmony_ci#ifndef AHC_TMODE_ENABLE
123962306a36Sopenharmony_ci#define AHC_TMODE_ENABLE 0
124062306a36Sopenharmony_ci#endif
124162306a36Sopenharmony_ci#endif
124262306a36Sopenharmony_ci/******************************* Debug ***************************************/
124362306a36Sopenharmony_ci#ifdef AHC_DEBUG
124462306a36Sopenharmony_ciextern uint32_t ahc_debug;
124562306a36Sopenharmony_ci#define	AHC_SHOW_MISC		0x0001
124662306a36Sopenharmony_ci#define	AHC_SHOW_SENSE		0x0002
124762306a36Sopenharmony_ci#define AHC_DUMP_SEEPROM	0x0004
124862306a36Sopenharmony_ci#define AHC_SHOW_TERMCTL	0x0008
124962306a36Sopenharmony_ci#define AHC_SHOW_MEMORY		0x0010
125062306a36Sopenharmony_ci#define AHC_SHOW_MESSAGES	0x0020
125162306a36Sopenharmony_ci#define	AHC_SHOW_DV		0x0040
125262306a36Sopenharmony_ci#define AHC_SHOW_SELTO		0x0080
125362306a36Sopenharmony_ci#define AHC_SHOW_QFULL		0x0200
125462306a36Sopenharmony_ci#define AHC_SHOW_QUEUE		0x0400
125562306a36Sopenharmony_ci#define AHC_SHOW_TQIN		0x0800
125662306a36Sopenharmony_ci#define AHC_SHOW_MASKED_ERRORS	0x1000
125762306a36Sopenharmony_ci#define AHC_DEBUG_SEQUENCER	0x2000
125862306a36Sopenharmony_ci#endif
125962306a36Sopenharmony_civoid			ahc_print_devinfo(struct ahc_softc *ahc,
126062306a36Sopenharmony_ci					  struct ahc_devinfo *dev);
126162306a36Sopenharmony_civoid			ahc_dump_card_state(struct ahc_softc *ahc);
126262306a36Sopenharmony_ciint			ahc_print_register(const ahc_reg_parse_entry_t *table,
126362306a36Sopenharmony_ci					   u_int num_entries,
126462306a36Sopenharmony_ci					   const char *name,
126562306a36Sopenharmony_ci					   u_int address,
126662306a36Sopenharmony_ci					   u_int value,
126762306a36Sopenharmony_ci					   u_int *cur_column,
126862306a36Sopenharmony_ci					   u_int wrap_point);
126962306a36Sopenharmony_ci/******************************* SEEPROM *************************************/
127062306a36Sopenharmony_ciint		ahc_acquire_seeprom(struct ahc_softc *ahc,
127162306a36Sopenharmony_ci				    struct seeprom_descriptor *sd);
127262306a36Sopenharmony_civoid		ahc_release_seeprom(struct seeprom_descriptor *sd);
127362306a36Sopenharmony_ci#endif /* _AIC7XXX_H_ */
1274