Home | History | Annotate | Line # | Download | only in ic
aic7xxxvar.h revision 1.35
      1 /*
      2  * Core definitions and data structures shareable across OS platforms.
      3  *
      4  * Copyright (c) 1994-2001 Justin T. Gibbs.
      5  * Copyright (c) 2000-2001 Adaptec Inc.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions, and the following disclaimer,
     13  *    without modification.
     14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     15  *    substantially similar to the "NO WARRANTY" disclaimer below
     16  *    ("Disclaimer") and any redistribution must be conditioned upon
     17  *    including a substantially similar Disclaimer requirement for further
     18  *    binary redistribution.
     19  * 3. Neither the names of the above-listed copyright holders nor the names
     20  *    of any contributors may be used to endorse or promote products derived
     21  *    from this software without specific prior written permission.
     22  *
     23  * Alternatively, this software may be distributed under the terms of the
     24  * GNU General Public License ("GPL") version 2 as published by the Free
     25  * Software Foundation.
     26  *
     27  * NO WARRANTY
     28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     32  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     37  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGES.
     39  *
     40  * $Id: aic7xxxvar.h,v 1.35 2003/04/20 16:04:54 fvdl Exp $
     41  *
     42  * $FreeBSD: /repoman/r/ncvs/src/sys/dev/aic7xxx/aic7xxx.h,v 1.44 2003/01/20 20:44:55 gibbs Exp $
     43  */
     44 /*
     45  * Ported from FreeBSD by Pascal Renauld, Network Storage Solutions, Inc. - April 2003
     46  */
     47 
     48 #ifndef _AIC7XXXVAR_H_
     49 #define _AIC7XXXVAR_H_
     50 
     51 #undef AHC_DEBUG
     52 
     53 /* Register Definitions */
     54 #include <dev/microcode/aic7xxx/aic7xxx_reg.h>
     55 
     56 #include <dev/ic/aic7xxx_cam.h>
     57 
     58 #define	AIC_OP_OR	0x0
     59 #define	AIC_OP_AND	0x1
     60 #define AIC_OP_XOR	0x2
     61 #define	AIC_OP_ADD	0x3
     62 #define	AIC_OP_ADC	0x4
     63 #define	AIC_OP_ROL	0x5
     64 #define	AIC_OP_BMOV	0x6
     65 
     66 #define	AIC_OP_JMP	0x8
     67 #define AIC_OP_JC	0x9
     68 #define AIC_OP_JNC	0xa
     69 #define AIC_OP_CALL	0xb
     70 #define	AIC_OP_JNE	0xc
     71 #define	AIC_OP_JNZ	0xd
     72 #define	AIC_OP_JE	0xe
     73 #define	AIC_OP_JZ	0xf
     74 
     75 /* Pseudo Ops */
     76 #define	AIC_OP_SHL	0x10
     77 #define	AIC_OP_SHR	0x20
     78 #define	AIC_OP_ROR	0x30
     79 
     80 struct ins_format1 {
     81 #if BYTE_ORDER == LITTLE_ENDIAN
     82 	uint32_t	immediate	: 8,
     83 			source		: 9,
     84 			destination	: 9,
     85 			ret		: 1,
     86 			opcode		: 4,
     87 			parity		: 1;
     88 #else
     89 	uint32_t	parity		: 1,
     90 			opcode		: 4,
     91 			ret		: 1,
     92 			destination	: 9,
     93 			source		: 9,
     94 			immediate	: 8;
     95 #endif
     96 };
     97 
     98 struct ins_format2 {
     99 #if BYTE_ORDER == LITTLE_ENDIAN
    100 	uint32_t	shift_control	: 8,
    101 			source		: 9,
    102 			destination	: 9,
    103 			ret		: 1,
    104 			opcode		: 4,
    105 			parity		: 1;
    106 #else
    107 	uint32_t	parity		: 1,
    108 			opcode		: 4,
    109 			ret		: 1,
    110 			destination	: 9,
    111 			source		: 9,
    112 			shift_control	: 8;
    113 #endif
    114 };
    115 
    116 struct ins_format3 {
    117 #if BYTE_ORDER == LITTLE_ENDIAN
    118 	uint32_t	immediate	: 8,
    119 			source		: 9,
    120 			address		: 10,
    121 			opcode		: 4,
    122 			parity		: 1;
    123 #else
    124 	uint32_t	parity		: 1,
    125 			opcode		: 4,
    126 			address		: 10,
    127 			source		: 9,
    128 			immediate	: 8;
    129 #endif
    130 };
    131 
    132 union ins_formats {
    133 		struct ins_format1 format1;
    134 		struct ins_format2 format2;
    135 		struct ins_format3 format3;
    136 		uint8_t		   bytes[4];
    137 		uint32_t	   integer;
    138 };
    139 
    140 /************************* Forward Declarations *******************************/
    141 struct ahc_platform_data;
    142 struct scb_platform_data;
    143 struct seeprom_descriptor;
    144 
    145 /****************************** Useful Macros *********************************/
    146 #ifndef MAX
    147 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
    148 #endif
    149 
    150 #ifndef MIN
    151 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
    152 #endif
    153 
    154 #ifndef TRUE
    155 #define TRUE 1
    156 #endif
    157 #ifndef FALSE
    158 #define FALSE 0
    159 #endif
    160 
    161 #define NUM_ELEMENTS(array) (sizeof(array) / sizeof(*array))
    162 
    163 #define ALL_CHANNELS '\0'
    164 #define ALL_TARGETS_MASK 0xFFFF
    165 #define INITIATOR_WILDCARD	(~0)
    166 
    167 #define SCSIID_TARGET(ahc, scsiid) \
    168 	(((scsiid) & ((((ahc)->features & AHC_TWIN) != 0) ? TWIN_TID : TID)) \
    169 	>> TID_SHIFT)
    170 #define SCSIID_OUR_ID(scsiid) \
    171 	((scsiid) & OID)
    172 #define SCSIID_CHANNEL(ahc, scsiid) \
    173 	((((ahc)->features & AHC_TWIN) != 0) \
    174         ? ((((scsiid) & TWIN_CHNLB) != 0) ? 'B' : 'A') \
    175        : 'A')
    176 #define	SCB_IS_SCSIBUS_B(ahc, scb) \
    177 	(SCSIID_CHANNEL(ahc, (scb)->hscb->scsiid) == 'B')
    178 #define	SCB_GET_OUR_ID(scb) \
    179 	SCSIID_OUR_ID((scb)->hscb->scsiid)
    180 #define	SCB_GET_TARGET(ahc, scb) \
    181 	SCSIID_TARGET((ahc), (scb)->hscb->scsiid)
    182 #define	SCB_GET_CHANNEL(ahc, scb) \
    183 	SCSIID_CHANNEL(ahc, (scb)->hscb->scsiid)
    184 #define	SCB_GET_LUN(scb) \
    185 	((scb)->hscb->lun)
    186 #define SCB_GET_TARGET_OFFSET(ahc, scb)	\
    187 	(SCB_GET_TARGET(ahc, scb))
    188 #define SCB_GET_TARGET_MASK(ahc, scb) \
    189 	(0x01 << (SCB_GET_TARGET_OFFSET(ahc, scb)))
    190 #ifdef AHC_DEBUG
    191 #define SCB_IS_SILENT(scb)					\
    192 	((ahc_debug & AHC_SHOW_MASKED_ERRORS) == 0		\
    193       && (((scb)->flags & SCB_SILENT) != 0))
    194 #else
    195 #define SCB_IS_SILENT(scb)					\
    196 	(((scb)->flags & SCB_SILENT) != 0)
    197 #endif
    198 #define TCL_TARGET_OFFSET(tcl) \
    199 	((((tcl) >> 4) & TID) >> 4)
    200 #define TCL_LUN(tcl) \
    201 	(tcl & (AHC_NUM_LUNS - 1))
    202 #define BUILD_TCL(scsiid, lun) \
    203 	((lun) | (((scsiid) & TID) << 4))
    204 
    205 #ifndef	AHC_TARGET_MODE
    206 #undef	AHC_TMODE_ENABLE
    207 #define	AHC_TMODE_ENABLE 0
    208 #endif
    209 
    210 /**************************** Driver Constants ********************************/
    211 /*
    212  * The maximum number of supported targets.
    213  */
    214 #define AHC_NUM_TARGETS 16
    215 
    216 /*
    217  * The maximum number of supported luns.
    218  * The identify message only supports 64 luns in SPI3.
    219  * You can have 2^64 luns when information unit transfers are enabled,
    220  * but it is doubtful this driver will ever support IUTs.
    221  */
    222 #define AHC_NUM_LUNS 64
    223 
    224 /*
    225  * The maximum transfer per S/G segment.
    226  */
    227 #define AHC_MAXTRANSFER_SIZE	 0x00ffffff	/* limited by 24bit counter */
    228 
    229 /*
    230  * The maximum amount of SCB storage in hardware on a controller.
    231  * This value represents an upper bound.  Controllers vary in the number
    232  * they actually support.
    233  */
    234 #define AHC_SCB_MAX	255
    235 
    236 /*
    237  * The maximum number of concurrent transactions supported per driver instance.
    238  * Sequencer Control Blocks (SCBs) store per-transaction information.  Although
    239  * the space for SCBs on the host adapter varies by model, the driver will
    240  * page the SCBs between host and controller memory as needed.  We are limited
    241  * to 253 because:
    242  * 	1) The 8bit nature of the RISC engine holds us to an 8bit value.
    243  * 	2) We reserve one value, 255, to represent the invalid element.
    244  *	3) Our input queue scheme requires one SCB to always be reserved
    245  *	   in advance of queuing any SCBs.  This takes us down to 254.
    246  *	4) To handle our output queue correctly on machines that only
    247  * 	   support 32bit stores, we must clear the array 4 bytes at a
    248  *	   time.  To avoid colliding with a DMA write from the sequencer,
    249  *	   we must be sure that 4 slots are empty when we write to clear
    250  *	   the queue.  This reduces us to 253 SCBs: 1 that just completed
    251  *	   and the known three additional empty slots in the queue that
    252  *	   precede it.
    253  */
    254 #define AHC_MAX_QUEUE	253
    255 
    256 /*
    257  * The maximum amount of SCB storage we allocate in host memory.  This
    258  * number should reflect the 1 additional SCB we require to handle our
    259  * qinfifo mechanism.
    260  */
    261 #define AHC_SCB_MAX_ALLOC (AHC_MAX_QUEUE+1)
    262 
    263 /*
    264  * Ring Buffer of incoming target commands.
    265  * We allocate 256 to simplify the logic in the sequencer
    266  * by using the natural wrap point of an 8bit counter.
    267  */
    268 #define AHC_TMODE_CMDS	256
    269 
    270 /* Reset line assertion time in us */
    271 #define AHC_BUSRESET_DELAY	25
    272 
    273 /******************* Chip Characteristics/Operating Settings  *****************/
    274 /*
    275  * Chip Type
    276  * The chip order is from least sophisticated to most sophisticated.
    277  */
    278 typedef enum {
    279 	AHC_NONE	= 0x0000,
    280 	AHC_CHIPID_MASK	= 0x00FF,
    281 	AHC_AIC7770	= 0x0001,
    282 	AHC_AIC7850	= 0x0002,
    283 	AHC_AIC7855	= 0x0003,
    284 	AHC_AIC7859	= 0x0004,
    285 	AHC_AIC7860	= 0x0005,
    286 	AHC_AIC7870	= 0x0006,
    287 	AHC_AIC7880	= 0x0007,
    288 	AHC_AIC7895	= 0x0008,
    289 	AHC_AIC7895C	= 0x0009,
    290 	AHC_AIC7890	= 0x000a,
    291 	AHC_AIC7896	= 0x000b,
    292 	AHC_AIC7892	= 0x000c,
    293 	AHC_AIC7899	= 0x000d,
    294 	AHC_VL		= 0x0100,	/* Bus type VL */
    295 	AHC_EISA	= 0x0200,	/* Bus type EISA */
    296 	AHC_PCI		= 0x0400,	/* Bus type PCI */
    297 	AHC_BUS_MASK	= 0x0F00
    298 } ahc_chip;
    299 
    300 /*
    301  * Features available in each chip type.
    302  */
    303 typedef enum {
    304 	AHC_FENONE	= 0x00000,
    305 	AHC_ULTRA	= 0x00001,	/* Supports 20MHz Transfers */
    306 	AHC_ULTRA2	= 0x00002,	/* Supports 40MHz Transfers */
    307 	AHC_WIDE  	= 0x00004,	/* Wide Channel */
    308 	AHC_TWIN	= 0x00008,	/* Twin Channel */
    309 	AHC_MORE_SRAM	= 0x00010,	/* 80 bytes instead of 64 */
    310 	AHC_CMD_CHAN	= 0x00020,	/* Has a Command DMA Channel */
    311 	AHC_QUEUE_REGS	= 0x00040,	/* Has Queue management registers */
    312 	AHC_SG_PRELOAD	= 0x00080,	/* Can perform auto-SG preload */
    313 	AHC_SPIOCAP	= 0x00100,	/* Has a Serial Port I/O Cap Register */
    314 	AHC_MULTI_TID	= 0x00200,	/* Has bitmask of TIDs for select-in */
    315 	AHC_HS_MAILBOX	= 0x00400,	/* Has HS_MAILBOX register */
    316 	AHC_DT		= 0x00800,	/* Double Transition transfers */
    317 	AHC_NEW_TERMCTL	= 0x01000,	/* Newer termination scheme */
    318 	AHC_MULTI_FUNC	= 0x02000,	/* Multi-Function Twin Channel Device */
    319 	AHC_LARGE_SCBS	= 0x04000,	/* 64byte SCBs */
    320 	AHC_AUTORATE	= 0x08000,	/* Automatic update of SCSIRATE/OFFSET*/
    321 	AHC_AUTOPAUSE	= 0x10000,	/* Automatic pause on register access */
    322 	AHC_TARGETMODE	= 0x20000,	/* Has tested target mode support */
    323 	AHC_MULTIROLE	= 0x40000,	/* Space for two roles at a time */
    324 	AHC_REMOVABLE	= 0x80000,	/* Hot-Swap supported */
    325 	AHC_AIC7770_FE	= AHC_FENONE,
    326 	/*
    327 	 * The real 7850 does not support Ultra modes, but there are
    328 	 * several cards that use the generic 7850 PCI ID even though
    329 	 * they are using an Ultra capable chip (7859/7860).  We start
    330 	 * out with the AHC_ULTRA feature set and then check the DEVSTATUS
    331 	 * register to determine if the capability is really present.
    332 	 */
    333 	AHC_AIC7850_FE	= AHC_SPIOCAP|AHC_AUTOPAUSE|AHC_TARGETMODE|AHC_ULTRA,
    334 	AHC_AIC7860_FE	= AHC_AIC7850_FE,
    335 	AHC_AIC7870_FE	= AHC_TARGETMODE,
    336 	AHC_AIC7880_FE	= AHC_AIC7870_FE|AHC_ULTRA,
    337 	/*
    338 	 * Although we have space for both the initiator and
    339 	 * target roles on ULTRA2 chips, we currently disable
    340 	 * the initiator role to allow multi-scsi-id target mode
    341 	 * configurations.  We can only respond on the same SCSI
    342 	 * ID as our initiator role if we allow initiator operation.
    343 	 * At some point, we should add a configuration knob to
    344 	 * allow both roles to be loaded.
    345 	 */
    346 	AHC_AIC7890_FE	= AHC_MORE_SRAM|AHC_CMD_CHAN|AHC_ULTRA2
    347 			  |AHC_QUEUE_REGS|AHC_SG_PRELOAD|AHC_MULTI_TID
    348 			  |AHC_HS_MAILBOX|AHC_NEW_TERMCTL|AHC_LARGE_SCBS
    349 			  |AHC_TARGETMODE,
    350 	AHC_AIC7892_FE	= AHC_AIC7890_FE|AHC_DT|AHC_AUTORATE|AHC_AUTOPAUSE,
    351 	AHC_AIC7895_FE	= AHC_AIC7880_FE|AHC_MORE_SRAM|AHC_AUTOPAUSE
    352 			  |AHC_CMD_CHAN|AHC_MULTI_FUNC|AHC_LARGE_SCBS,
    353 	AHC_AIC7895C_FE	= AHC_AIC7895_FE|AHC_MULTI_TID,
    354 	AHC_AIC7896_FE	= AHC_AIC7890_FE|AHC_MULTI_FUNC,
    355 	AHC_AIC7899_FE	= AHC_AIC7892_FE|AHC_MULTI_FUNC
    356 } ahc_feature;
    357 
    358 /*
    359  * Bugs in the silicon that we work around in software.
    360  */
    361 typedef enum {
    362 	AHC_BUGNONE		= 0x00,
    363 	/*
    364 	 * On all chips prior to the U2 product line,
    365 	 * the WIDEODD S/G segment feature does not
    366 	 * work during scsi->HostBus transfers.
    367 	 */
    368 	AHC_TMODE_WIDEODD_BUG	= 0x01,
    369 	/*
    370 	 * On the aic7890/91 Rev 0 chips, the autoflush
    371 	 * feature does not work.  A manual flush of
    372 	 * the DMA FIFO is required.
    373 	 */
    374 	AHC_AUTOFLUSH_BUG	= 0x02,
    375 	/*
    376 	 * On many chips, cacheline streaming does not work.
    377 	 */
    378 	AHC_CACHETHEN_BUG	= 0x04,
    379 	/*
    380 	 * On the aic7896/97 chips, cacheline
    381 	 * streaming must be enabled.
    382 	 */
    383 	AHC_CACHETHEN_DIS_BUG	= 0x08,
    384 	/*
    385 	 * PCI 2.1 Retry failure on non-empty data fifo.
    386 	 */
    387 	AHC_PCI_2_1_RETRY_BUG	= 0x10,
    388 	/*
    389 	 * Controller does not handle cacheline residuals
    390 	 * properly on S/G segments if PCI MWI instructions
    391 	 * are allowed.
    392 	 */
    393 	AHC_PCI_MWI_BUG		= 0x20,
    394 	/*
    395 	 * An SCB upload using the SCB channel's
    396 	 * auto array entry copy feature may
    397 	 * corrupt data.  This appears to only
    398 	 * occur on 66MHz systems.
    399 	 */
    400 	AHC_SCBCHAN_UPLOAD_BUG	= 0x40
    401 } ahc_bug;
    402 
    403 /*
    404  * Configuration specific settings.
    405  * The driver determines these settings by probing the
    406  * chip/controller's configuration.
    407  */
    408 typedef enum {
    409 	AHC_FNONE	      = 0x000,
    410 	AHC_PRIMARY_CHANNEL   = 0x003,  /*
    411 					 * The channel that should
    412 					 * be probed first.
    413 					 */
    414 	AHC_USEDEFAULTS	      = 0x004,  /*
    415 					 * For cards without an seeprom
    416 					 * or a BIOS to initialize the chip's
    417 					 * SRAM, we use the default target
    418 					 * settings.
    419 					 */
    420 	AHC_SEQUENCER_DEBUG   = 0x008,
    421 	AHC_SHARED_SRAM	      = 0x010,
    422 	AHC_LARGE_SEEPROM     = 0x020,  /* Uses C56_66 not C46 */
    423 	AHC_RESET_BUS_A	      = 0x040,
    424 	AHC_RESET_BUS_B	      = 0x080,
    425 	AHC_EXTENDED_TRANS_A  = 0x100,
    426 	AHC_EXTENDED_TRANS_B  = 0x200,
    427 	AHC_TERM_ENB_A	      = 0x400,
    428 	AHC_TERM_ENB_B	      = 0x800,
    429 	AHC_INITIATORROLE     = 0x1000,  /*
    430 					  * Allow initiator operations on
    431 					  * this controller.
    432 					  */
    433 	AHC_TARGETROLE	      = 0x2000,  /*
    434 					  * Allow target operations on this
    435 					  * controller.
    436 					  */
    437 	AHC_NEWEEPROM_FMT     = 0x4000,
    438 	AHC_RESOURCE_SHORTAGE = 0x8000,
    439 	AHC_TQINFIFO_BLOCKED  = 0x10000,  /* Blocked waiting for ATIOs */
    440 	AHC_INT50_SPEEDFLEX   = 0x20000,  /*
    441 					   * Internal 50pin connector
    442 					   * sits behind an aic3860
    443 					   */
    444 	AHC_SCB_BTT	      = 0x40000,  /*
    445 					   * The busy targets table is
    446 					   * stored in SCB space rather
    447 					   * than SRAM.
    448 					   */
    449 	AHC_BIOS_ENABLED      = 0x80000,
    450 	AHC_ALL_INTERRUPTS    = 0x100000,
    451 	AHC_PAGESCBS	      = 0x400000,  /* Enable SCB paging */
    452 	AHC_EDGE_INTERRUPT    = 0x800000,  /* Device uses edge triggered ints */
    453 	AHC_39BIT_ADDRESSING  = 0x1000000, /* Use 39 bit addressing scheme. */
    454 	AHC_LSCBS_ENABLED     = 0x2000000, /* 64Byte SCBs enabled */
    455 	AHC_SCB_CONFIG_USED   = 0x4000000, /* No SEEPROM but SCB2 had info. */
    456 	AHC_NO_BIOS_INIT      = 0x8000000, /* No BIOS left over settings. */
    457 	AHC_DISABLE_PCI_PERR  = 0x10000000
    458 } ahc_flag;
    459 
    460 /************************* Hardware  SCB Definition ***************************/
    461 
    462 /*
    463  * The driver keeps up to MAX_SCB scb structures per card in memory.  The SCB
    464  * consists of a "hardware SCB" mirroring the fields availible on the card
    465  * and additional information the kernel stores for each transaction.
    466  *
    467  * To minimize space utilization, a portion of the hardware scb stores
    468  * different data during different portions of a SCSI transaction.
    469  * As initialized by the host driver for the initiator role, this area
    470  * contains the SCSI cdb (or a pointer to the  cdb) to be executed.  After
    471  * the cdb has been presented to the target, this area serves to store
    472  * residual transfer information and the SCSI status byte.
    473  * For the target role, the contents of this area do not change, but
    474  * still serve a different purpose than for the initiator role.  See
    475  * struct target_data for details.
    476  */
    477 
    478 /*
    479  * Status information embedded in the shared poriton of
    480  * an SCB after passing the cdb to the target.  The kernel
    481  * driver will only read this data for transactions that
    482  * complete abnormally (non-zero status byte).
    483  */
    484 struct status_pkt {
    485 	uint32_t residual_datacnt;	/* Residual in the current S/G seg */
    486 	uint32_t residual_sg_ptr;	/* The next S/G for this transfer */
    487 	uint8_t	 scsi_status;		/* Standard SCSI status byte */
    488 };
    489 
    490 /*
    491  * Target mode version of the shared data SCB segment.
    492  */
    493 struct target_data {
    494 	uint32_t residual_datacnt;	/* Residual in the current S/G seg */
    495 	uint32_t residual_sg_ptr;	/* The next S/G for this transfer */
    496 	uint8_t  scsi_status;		/* SCSI status to give to initiator */
    497 	uint8_t  target_phases;		/* Bitmap of phases to execute */
    498 	uint8_t  data_phase;		/* Data-In or Data-Out */
    499 	uint8_t  initiator_tag;		/* Initiator's transaction tag */
    500 };
    501 
    502 struct hardware_scb {
    503 /*0*/	union {
    504 		/*
    505 		 * If the cdb is 12 bytes or less, we embed it directly
    506 		 * in the SCB.  For longer cdbs, we embed the address
    507 		 * of the cdb payload as seen by the chip and a DMA
    508 		 * is used to pull it in.
    509 		 */
    510 		uint8_t	 cdb[12];
    511 		uint32_t cdb_ptr;
    512 		struct	 status_pkt status;
    513 		struct	 target_data tdata;
    514 	} shared_data;
    515 /*
    516  * A word about residuals.
    517  * The scb is presented to the sequencer with the dataptr and datacnt
    518  * fields initialized to the contents of the first S/G element to
    519  * transfer.  The sgptr field is initialized to the bus address for
    520  * the S/G element that follows the first in the in core S/G array
    521  * or'ed with the SG_FULL_RESID flag.  Sgptr may point to an invalid
    522  * S/G entry for this transfer (single S/G element transfer with the
    523  * first elements address and length preloaded in the dataptr/datacnt
    524  * fields).  If no transfer is to occur, sgptr is set to SG_LIST_NULL.
    525  * The SG_FULL_RESID flag ensures that the residual will be correctly
    526  * noted even if no data transfers occur.  Once the data phase is entered,
    527  * the residual sgptr and datacnt are loaded from the sgptr and the
    528  * datacnt fields.  After each S/G element's dataptr and length are
    529  * loaded into the hardware, the residual sgptr is advanced.  After
    530  * each S/G element is expired, its datacnt field is checked to see
    531  * if the LAST_SEG flag is set.  If so, SG_LIST_NULL is set in the
    532  * residual sg ptr and the transfer is considered complete.  If the
    533  * sequencer determines that there is a residual in the tranfer, it
    534  * will set the SG_RESID_VALID flag in sgptr and dma the scb back into
    535  * host memory.  To sumarize:
    536  *
    537  * Sequencer:
    538  *	o A residual has occurred if SG_FULL_RESID is set in sgptr,
    539  *	  or residual_sgptr does not have SG_LIST_NULL set.
    540  *
    541  *	o We are transfering the last segment if residual_datacnt has
    542  *	  the SG_LAST_SEG flag set.
    543  *
    544  * Host:
    545  *	o A residual has occurred if a completed scb has the
    546  *	  SG_RESID_VALID flag set.
    547  *
    548  *	o residual_sgptr and sgptr refer to the "next" sg entry
    549  *	  and so may point beyond the last valid sg entry for the
    550  *	  transfer.
    551  */
    552 /*12*/	uint32_t dataptr;
    553 /*16*/	uint32_t datacnt;		/*
    554 					 * Byte 3 (numbered from 0) of
    555 					 * the datacnt is really the
    556 					 * 4th byte in that data address.
    557 					 */
    558 /*20*/	uint32_t sgptr;
    559 #define SG_PTR_MASK	0xFFFFFFF8
    560 /*24*/	uint8_t  control;	/* See SCB_CONTROL in aic7xxx.reg for details */
    561 /*25*/	uint8_t  scsiid;	/* what to load in the SCSIID register */
    562 /*26*/	uint8_t  lun;
    563 /*27*/	uint8_t  tag;			/*
    564 					 * Index into our kernel SCB array.
    565 					 * Also used as the tag for tagged I/O
    566 					 */
    567 /*28*/	uint8_t  cdb_len;
    568 /*29*/	uint8_t  scsirate;		/* Value for SCSIRATE register */
    569 /*30*/	uint8_t  scsioffset;		/* Value for SCSIOFFSET register */
    570 /*31*/	uint8_t  next;			/*
    571 					 * Used for threading SCBs in the
    572 					 * "Waiting for Selection" and
    573 					 * "Disconnected SCB" lists down
    574 					 * in the sequencer.
    575 					 */
    576 /*32*/	uint8_t  cdb32[32];		/*
    577 					 * CDB storage for cdbs of size
    578 					 * 13->32.  We store them here
    579 					 * because hardware scbs are
    580 					 * allocated from DMA safe
    581 					 * memory so we are guaranteed
    582 					 * the controller can access
    583 					 * this data.
    584 					 */
    585 };
    586 
    587 /************************ Kernel SCB Definitions ******************************/
    588 /*
    589  * Some fields of the SCB are OS dependent.  Here we collect the
    590  * definitions for elements that all OS platforms need to include
    591  * in there SCB definition.
    592  */
    593 
    594 /*
    595  * Definition of a scatter/gather element as transfered to the controller.
    596  * The aic7xxx chips only support a 24bit length.  We use the top byte of
    597  * the length to store additional address bits and a flag to indicate
    598  * that a given segment terminates the transfer.  This gives us an
    599  * addressable range of 512GB on machines with 64bit PCI or with chips
    600  * that can support dual address cycles on 32bit PCI busses.
    601  */
    602 struct ahc_dma_seg {
    603 	uint32_t	addr;
    604 	uint32_t	len;
    605 #define	AHC_DMA_LAST_SEG	0x80000000
    606 #define	AHC_SG_HIGH_ADDR_MASK	0x7F000000
    607 #define	AHC_SG_LEN_MASK		0x00FFFFFF
    608 };
    609 
    610 struct sg_map_node {
    611 	bus_dmamap_t		 sg_dmamap;
    612 	bus_addr_t		 sg_physaddr;
    613 	bus_dma_segment_t	 sg_dmasegs;
    614 	int			 sg_nseg;
    615 	struct ahc_dma_seg*	 sg_vaddr;
    616 	SLIST_ENTRY(sg_map_node) links;
    617 };
    618 
    619 struct ahc_pci_busdata {
    620 	pci_chipset_tag_t pc;
    621 	pcitag_t tag;
    622 	u_int dev;
    623 	u_int func;
    624 	pcireg_t class;
    625 };
    626 
    627 /*
    628  * The current state of this SCB.
    629  */
    630 typedef enum {
    631 	SCB_FREE		= 0x0000,
    632 	SCB_OTHERTCL_TIMEOUT	= 0x0002,/*
    633 					  * Another device was active
    634 					  * during the first timeout for
    635 					  * this SCB so we gave ourselves
    636 					  * an additional timeout period
    637 					  * in case it was hogging the
    638 					  * bus.
    639 				          */
    640 	SCB_DEVICE_RESET	= 0x0004,
    641 	SCB_SENSE		= 0x0008,
    642 	SCB_CDB32_PTR		= 0x0010,
    643 	SCB_RECOVERY_SCB	= 0x0020,
    644 	SCB_AUTO_NEGOTIATE	= 0x0040,/* Negotiate to achieve goal. */
    645 	SCB_NEGOTIATE		= 0x0080,/* Negotiation forced for command. */
    646 	SCB_ABORT		= 0x0100,
    647 	SCB_UNTAGGEDQ		= 0x0200,
    648 	SCB_ACTIVE		= 0x0400,
    649 	SCB_TARGET_IMMEDIATE	= 0x0800,
    650 	SCB_TRANSMISSION_ERROR	= 0x1000,/*
    651 					  * We detected a parity or CRC
    652 					  * error that has effected the
    653 					  * payload of the command.  This
    654 					  * flag is checked when normal
    655 					  * status is returned to catch
    656 					  * the case of a target not
    657 					  * responding to our attempt
    658 					  * to report the error.
    659 					  */
    660 	SCB_TARGET_SCB		= 0x2000,
    661 	SCB_SILENT		= 0x4000,/*
    662 					  * Be quiet about transmission type
    663 					  * errors.  They are expected and we
    664 					  * don't want to upset the user.  This
    665 					  * flag is typically used during DV.
    666 					  */
    667 	SCB_FREEZE_QUEUE	= 0x8000
    668 } scb_flag;
    669 
    670 struct scb {
    671 	struct	hardware_scb	 *hscb;
    672 	union {
    673 		SLIST_ENTRY(scb)  sle;
    674 		TAILQ_ENTRY(scb)  tqe;
    675 	} links;
    676 	LIST_ENTRY(scb)		  pending_links;
    677 
    678 	struct scsipi_xfer	 *xs;
    679 	struct ahc_softc	 *ahc_softc;
    680 	scb_flag		  flags;
    681 #ifndef __linux__
    682 	bus_dmamap_t		  dmamap;
    683 #endif
    684 	struct scb_platform_data *platform_data;
    685 	struct sg_map_node	 *sg_map;
    686 	struct ahc_dma_seg 	 *sg_list;
    687 	bus_addr_t		  sg_list_phys;
    688 	u_int			  sg_count;/* How full ahc_dma_seg is */
    689 };
    690 
    691 struct scb_data {
    692 	SLIST_HEAD(, scb) free_scbs;	/*
    693 					 * Pool of SCBs ready to be assigned
    694 					 * commands to execute.
    695 					 */
    696 	struct	scb *scbindex[256];	/*
    697 					 * Mapping from tag to SCB.
    698 					 * As tag identifiers are an
    699 					 * 8bit value, we provide space
    700 					 * for all possible tag values.
    701 					 * Any lookups to entries at or
    702 					 * above AHC_SCB_MAX_ALLOC will
    703 					 * always fail.
    704 					 */
    705 	struct	hardware_scb	*hscbs;	/* Array of hardware SCBs */
    706 	struct	scb *scbarray;		/* Array of kernel SCBs */
    707 	struct	scsipi_sense_data *sense; /* Per SCB sense data */
    708 
    709 	/*
    710 	 * "Bus" addresses of our data structures.
    711 	 */
    712 	bus_dmamap_t	 hscb_dmamap;
    713 	bus_addr_t	 hscb_busaddr;
    714 	bus_dma_segment_t hscb_seg;
    715 	int		  hscb_nseg;
    716 	int		  hscb_size;
    717 
    718 	bus_dmamap_t	 sense_dmamap;
    719 	bus_addr_t	 sense_busaddr;
    720 	bus_dma_segment_t sense_seg;
    721 	int		  sense_nseg;
    722 	int		  sense_size;
    723 
    724 	SLIST_HEAD(, sg_map_node) sg_maps;
    725 	uint8_t	numscbs;
    726 	uint8_t	maxhscbs;		/* Number of SCBs on the card */
    727 	uint8_t	init_level;		/*
    728 					 * How far we've initialized
    729 					 * this structure.
    730 					 */
    731 };
    732 
    733 /************************ Target Mode Definitions *****************************/
    734 
    735 /*
    736  * Connection desciptor for select-in requests in target mode.
    737  */
    738 struct target_cmd {
    739 	uint8_t scsiid;		/* Our ID and the initiator's ID */
    740 	uint8_t identify;	/* Identify message */
    741 	uint8_t bytes[22];	/*
    742 				 * Bytes contains any additional message
    743 				 * bytes terminated by 0xFF.  The remainder
    744 				 * is the cdb to execute.
    745 				 */
    746 	uint8_t cmd_valid;	/*
    747 				 * When a command is complete, the firmware
    748 				 * will set cmd_valid to all bits set.
    749 				 * After the host has seen the command,
    750 				 * the bits are cleared.  This allows us
    751 				 * to just peek at host memory to determine
    752 				 * if more work is complete. cmd_valid is on
    753 				 * an 8 byte boundary to simplify setting
    754 				 * it on aic7880 hardware which only has
    755 				 * limited direct access to the DMA FIFO.
    756 				 */
    757 	uint8_t pad[7];
    758 };
    759 
    760 /*
    761  * Number of events we can buffer up if we run out
    762  * of immediate notify ccbs.
    763  */
    764 #define AHC_TMODE_EVENT_BUFFER_SIZE 8
    765 struct ahc_tmode_event {
    766 	uint8_t initiator_id;
    767 	uint8_t event_type;	/* MSG type or EVENT_TYPE_BUS_RESET */
    768 #define	EVENT_TYPE_BUS_RESET 0xFF
    769 	uint8_t event_arg;
    770 };
    771 
    772 /*
    773  * Per enabled lun target mode state.
    774  * As this state is directly influenced by the host OS'es target mode
    775  * environment, we let the OS module define it.  Forward declare the
    776  * structure here so we can store arrays of them, etc. in OS neutral
    777  * data structures.
    778  */
    779 #ifdef AHC_TARGET_MODE
    780 struct ahc_tmode_lstate {
    781 #if 0
    782 	struct cam_path *path;
    783 	struct ccb_hdr_slist accept_tios;
    784 	struct ccb_hdr_slist immed_notifies;
    785 #endif
    786 	struct ahc_tmode_event event_buffer[AHC_TMODE_EVENT_BUFFER_SIZE];
    787 	uint8_t event_r_idx;
    788 	uint8_t event_w_idx;
    789 };
    790 #else
    791 struct ahc_tmode_lstate;
    792 #endif
    793 
    794 /******************** Transfer Negotiation Datastructures *********************/
    795 #define AHC_TRANS_CUR		0x01	/* Modify current neogtiation status */
    796 #define AHC_TRANS_ACTIVE	0x03	/* Assume this target is on the bus */
    797 #define AHC_TRANS_GOAL		0x04	/* Modify negotiation goal */
    798 #define AHC_TRANS_USER		0x08	/* Modify user negotiation settings */
    799 
    800 #define AHC_WIDTH_UNKNOWN	0xFF
    801 #define AHC_PERIOD_UNKNOWN	0xFF
    802 #define AHC_OFFSET_UNKNOWN	0x0
    803 #define AHC_PPR_OPTS_UNKNOWN	0xFF
    804 
    805 /*
    806  * Transfer Negotiation Information.
    807  */
    808 struct ahc_transinfo {
    809 	uint8_t protocol_version;	/* SCSI Revision level */
    810 	uint8_t transport_version;	/* SPI Revision level */
    811 	uint8_t width;			/* Bus width */
    812 	uint8_t period;			/* Sync rate factor */
    813 	uint8_t offset;			/* Sync offset */
    814 	uint8_t ppr_options;		/* Parallel Protocol Request options */
    815 };
    816 
    817 /*
    818  * Per-initiator current, goal and user transfer negotiation information. */
    819 struct ahc_initiator_tinfo {
    820 	uint8_t scsirate;		/* Computed value for SCSIRATE reg */
    821 	struct ahc_transinfo curr;
    822 	struct ahc_transinfo goal;
    823 	struct ahc_transinfo user;
    824 };
    825 
    826 /*
    827  * Per enabled target ID state.
    828  * Pointers to lun target state as well as sync/wide negotiation information
    829  * for each initiator<->target mapping.  For the initiator role we pretend
    830  * that we are the target and the targets are the initiators since the
    831  * negotiation is the same regardless of role.
    832  */
    833 struct ahc_tmode_tstate {
    834 	struct ahc_tmode_lstate*	enabled_luns[AHC_NUM_LUNS];
    835 	struct ahc_initiator_tinfo	transinfo[AHC_NUM_TARGETS];
    836 
    837 	/*
    838 	 * Per initiator state bitmasks.
    839 	 */
    840 	uint16_t	 auto_negotiate;/* Auto Negotiation Required */
    841 	uint16_t	 ultraenb;	/* Using ultra sync rate  */
    842 	uint16_t	 discenable;	/* Disconnection allowed  */
    843 	uint16_t	 tagenable;	/* Tagged Queuing allowed */
    844 };
    845 
    846 /*
    847  * Data structure for our table of allowed synchronous transfer rates.
    848  */
    849 struct ahc_syncrate {
    850 	u_int sxfr_u2;	/* Value of the SXFR parameter for Ultra2+ Chips */
    851 	u_int sxfr;	/* Value of the SXFR parameter for <= Ultra Chips */
    852 #define		ULTRA_SXFR 0x100	/* Rate Requires Ultra Mode set */
    853 #define		ST_SXFR	   0x010	/* Rate Single Transition Only */
    854 #define		DT_SXFR	   0x040	/* Rate Double Transition Only */
    855 	uint8_t period; /* Period to send to SCSI target */
    856 	char *rate;
    857 };
    858 
    859 /* Safe and valid period for async negotiations. */
    860 #define	AHC_ASYNC_XFER_PERIOD 0x45
    861 #define	AHC_ULTRA2_XFER_PERIOD 0x0a
    862 
    863 /*
    864  * Indexes into our table of syncronous transfer rates.
    865  */
    866 #define AHC_SYNCRATE_DT		0
    867 #define AHC_SYNCRATE_ULTRA2	1
    868 #define AHC_SYNCRATE_ULTRA	3
    869 #define AHC_SYNCRATE_FAST	6
    870 #define AHC_SYNCRATE_MAX	AHC_SYNCRATE_DT
    871 #define	AHC_SYNCRATE_MIN	13
    872 
    873 /***************************** Lookup Tables **********************************/
    874 /*
    875  * Phase -> name and message out response
    876  * to parity errors in each phase table.
    877  */
    878 struct ahc_phase_table_entry {
    879         uint8_t phase;
    880         uint8_t mesg_out; /* Message response to parity errors */
    881 	char *phasemsg;
    882 };
    883 
    884 /************************** Serial EEPROM Format ******************************/
    885 
    886 struct seeprom_config {
    887 /*
    888  * Per SCSI ID Configuration Flags
    889  */
    890 	uint16_t device_flags[16];	/* words 0-15 */
    891 #define		CFXFER		0x0007	/* synchronous transfer rate */
    892 #define		CFSYNCH		0x0008	/* enable synchronous transfer */
    893 #define		CFDISC		0x0010	/* enable disconnection */
    894 #define		CFWIDEB		0x0020	/* wide bus device */
    895 #define		CFSYNCHISULTRA	0x0040	/* CFSYNCH is an ultra offset (2940AU)*/
    896 #define		CFSYNCSINGLE	0x0080	/* Single-Transition signalling */
    897 #define		CFSTART		0x0100	/* send start unit SCSI command */
    898 #define		CFINCBIOS	0x0200	/* include in BIOS scan */
    899 #define		CFRNFOUND	0x0400	/* report even if not found */
    900 #define		CFMULTILUNDEV	0x0800	/* Probe multiple luns in BIOS scan */
    901 #define		CFWBCACHEENB	0x4000	/* Enable W-Behind Cache on disks */
    902 #define		CFWBCACHENOP	0xc000	/* Don't touch W-Behind Cache */
    903 
    904 /*
    905  * BIOS Control Bits
    906  */
    907 	uint16_t bios_control;		/* word 16 */
    908 #define		CFSUPREM	0x0001	/* support all removeable drives */
    909 #define		CFSUPREMB	0x0002	/* support removeable boot drives */
    910 #define		CFBIOSEN	0x0004	/* BIOS enabled */
    911 #define		CFBIOS_BUSSCAN	0x0008	/* Have the BIOS Scan the Bus */
    912 #define		CFSM2DRV	0x0010	/* support more than two drives */
    913 #define		CFSTPWLEVEL	0x0010	/* Termination level control */
    914 #define		CF284XEXTEND	0x0020	/* extended translation (284x cards) */
    915 #define		CFCTRL_A	0x0020	/* BIOS displays Ctrl-A message */
    916 #define		CFTERM_MENU	0x0040	/* BIOS displays termination menu */
    917 #define		CFEXTEND	0x0080	/* extended translation enabled */
    918 #define		CFSCAMEN	0x0100	/* SCAM enable */
    919 #define		CFMSG_LEVEL	0x0600	/* BIOS Message Level */
    920 #define			CFMSG_VERBOSE	0x0000
    921 #define			CFMSG_SILENT	0x0200
    922 #define			CFMSG_DIAG	0x0400
    923 #define		CFBOOTCD	0x0800  /* Support Bootable CD-ROM */
    924 /*		UNUSED		0xff00	*/
    925 
    926 /*
    927  * Host Adapter Control Bits
    928  */
    929 	uint16_t adapter_control;	/* word 17 */
    930 #define		CFAUTOTERM	0x0001	/* Perform Auto termination */
    931 #define		CFULTRAEN	0x0002	/* Ultra SCSI speed enable */
    932 #define		CF284XSELTO     0x0003	/* Selection timeout (284x cards) */
    933 #define		CF284XFIFO      0x000C	/* FIFO Threshold (284x cards) */
    934 #define		CFSTERM		0x0004	/* SCSI low byte termination */
    935 #define		CFWSTERM	0x0008	/* SCSI high byte termination */
    936 #define		CFSPARITY	0x0010	/* SCSI parity */
    937 #define		CF284XSTERM     0x0020	/* SCSI low byte term (284x cards) */
    938 #define		CFMULTILUN	0x0020
    939 #define		CFRESETB	0x0040	/* reset SCSI bus at boot */
    940 #define		CFCLUSTERENB	0x0080	/* Cluster Enable */
    941 #define		CFBOOTCHAN	0x0300	/* probe this channel first */
    942 #define		CFBOOTCHANSHIFT 8
    943 #define		CFSEAUTOTERM	0x0400	/* Ultra2 Perform secondary Auto Term*/
    944 #define		CFSELOWTERM	0x0800	/* Ultra2 secondary low term */
    945 #define		CFSEHIGHTERM	0x1000	/* Ultra2 secondary high term */
    946 #define		CFENABLEDV	0x4000	/* Perform Domain Validation*/
    947 
    948 /*
    949  * Bus Release Time, Host Adapter ID
    950  */
    951 	uint16_t brtime_id;		/* word 18 */
    952 #define		CFSCSIID	0x000f	/* host adapter SCSI ID */
    953 /*		UNUSED		0x00f0	*/
    954 #define		CFBRTIME	0xff00	/* bus release time */
    955 
    956 /*
    957  * Maximum targets
    958  */
    959 	uint16_t max_targets;		/* word 19 */
    960 #define		CFMAXTARG	0x00ff	/* maximum targets */
    961 #define		CFBOOTLUN	0x0f00	/* Lun to boot from */
    962 #define		CFBOOTID	0xf000	/* Target to boot from */
    963 	uint16_t res_1[10];		/* words 20-29 */
    964 	uint16_t signature;		/* Signature == 0x250 */
    965 #define		CFSIGNATURE	0x250
    966 #define		CFSIGNATURE2	0x300
    967 	uint16_t checksum;		/* word 31 */
    968 };
    969 
    970 /****************************  Message Buffer *********************************/
    971 typedef enum {
    972 	MSG_TYPE_NONE			= 0x00,
    973 	MSG_TYPE_INITIATOR_MSGOUT	= 0x01,
    974 	MSG_TYPE_INITIATOR_MSGIN	= 0x02,
    975 	MSG_TYPE_TARGET_MSGOUT		= 0x03,
    976 	MSG_TYPE_TARGET_MSGIN		= 0x04
    977 } ahc_msg_type;
    978 
    979 typedef enum {
    980 	MSGLOOP_IN_PROG,
    981 	MSGLOOP_MSGCOMPLETE,
    982 	MSGLOOP_TERMINATED
    983 } msg_loop_stat;
    984 
    985 /*********************** Software Configuration Structure *********************/
    986 TAILQ_HEAD(scb_tailq, scb);
    987 
    988 struct ahc_suspend_channel_state {
    989 	uint8_t	scsiseq;
    990 	uint8_t	sxfrctl0;
    991 	uint8_t	sxfrctl1;
    992 	uint8_t	simode0;
    993 	uint8_t	simode1;
    994 	uint8_t	seltimer;
    995 	uint8_t	seqctl;
    996 };
    997 
    998 struct ahc_suspend_state {
    999 	struct	ahc_suspend_channel_state channel[2];
   1000 	uint8_t	optionmode;
   1001 	uint8_t	dscommand0;
   1002 	uint8_t	dspcistatus;
   1003 	/* hsmailbox */
   1004 	uint8_t	crccontrol1;
   1005 	uint8_t	scbbaddr;
   1006 	/* Host and sequencer SCB counts */
   1007 	uint8_t	dff_thrsh;
   1008 	uint8_t	*scratch_ram;
   1009 	uint8_t	*btt;
   1010 };
   1011 
   1012 typedef void (*ahc_bus_intr_t)(struct ahc_softc *);
   1013 typedef void ahc_callback_t (void *);
   1014 
   1015 struct ahc_softc {
   1016 	struct device 		  sc_dev;
   1017 
   1018 	struct scsipi_channel	  sc_channel;
   1019 	struct scsipi_channel 	  sc_channel_b;
   1020 	struct device *		  sc_child;
   1021 	struct device *		  sc_child_b;
   1022 	struct scsipi_adapter	  sc_adapter;
   1023 
   1024 	bus_space_tag_t           tag;
   1025 	bus_space_handle_t        bsh;
   1026 
   1027 	scsipi_adapter_req_t	  sc_req;
   1028 
   1029 #ifndef __linux__
   1030 	bus_dma_tag_t		  buffer_dmat;   /* dmat for buffer I/O */
   1031 #endif
   1032 	struct scb_data		 *scb_data;
   1033 
   1034 	struct scb		 *next_queued_scb;
   1035 
   1036 	/*
   1037 	 * SCBs that have been sent to the controller
   1038 	 */
   1039 	LIST_HEAD(, scb)	  pending_scbs;
   1040 
   1041 	/*
   1042 	 * Counting lock for deferring the release of additional
   1043 	 * untagged transactions from the untagged_queues.  When
   1044 	 * the lock is decremented to 0, all queues in the
   1045 	 * untagged_queues array are run.
   1046 	 */
   1047 	u_int			  untagged_queue_lock;
   1048 
   1049 	/*
   1050 	 * Per-target queue of untagged-transactions.  The
   1051 	 * transaction at the head of the queue is the
   1052 	 * currently pending untagged transaction for the
   1053 	 * target.  The driver only allows a single untagged
   1054 	 * transaction per target.
   1055 	 */
   1056 	struct scb_tailq	  untagged_queues[AHC_NUM_TARGETS];
   1057 
   1058 	/*
   1059 	 * Platform specific data.
   1060 	 */
   1061 	struct ahc_platform_data *platform_data;
   1062 
   1063 	/*
   1064 	 * Platform specific device information.
   1065 	 */
   1066 	/* ahc_dev_softc_t		  dev_softc; */
   1067 
   1068 	/*
   1069 	 * Bus specific device information.
   1070 	 */
   1071 	ahc_bus_intr_t		  bus_intr;
   1072 
   1073 	/*
   1074 	 * Target mode related state kept on a per enabled lun basis.
   1075 	 * Targets that are not enabled will have null entries.
   1076 	 * As an initiator, we keep one target entry for our initiator
   1077 	 * ID to store our sync/wide transfer settings.
   1078 	 */
   1079 	struct ahc_tmode_tstate  *enabled_targets[AHC_NUM_TARGETS];
   1080 
   1081 	char inited_target[AHC_NUM_TARGETS];
   1082 
   1083 	/*
   1084 	 * The black hole device responsible for handling requests for
   1085 	 * disabled luns on enabled targets.
   1086 	 */
   1087 	struct ahc_tmode_lstate  *black_hole;
   1088 
   1089 	/*
   1090 	 * Device instance currently on the bus awaiting a continue TIO
   1091 	 * for a command that was not given the disconnect priveledge.
   1092 	 */
   1093 	struct ahc_tmode_lstate  *pending_device;
   1094 
   1095 	/*
   1096 	 * Card characteristics
   1097 	 */
   1098 	ahc_chip		  chip;
   1099 	ahc_feature		  features;
   1100 	ahc_bug			  bugs;
   1101 	ahc_flag		  flags;
   1102 	struct seeprom_config	 *seep_config;
   1103 
   1104 	/* Values to store in the SEQCTL register for pause and unpause */
   1105 	uint8_t			  unpause;
   1106 	uint8_t			  pause;
   1107 
   1108 	/* Command Queues */
   1109 	uint8_t			  qoutfifonext;
   1110 	uint8_t			  qinfifonext;
   1111 	uint8_t			 *qoutfifo;
   1112 	uint8_t			 *qinfifo;
   1113 
   1114 	/* Critical Section Data */
   1115 	struct cs		 *critical_sections;
   1116 	u_int			  num_critical_sections;
   1117 
   1118 	/* Links for chaining softcs */
   1119 	TAILQ_ENTRY(ahc_softc)	  links;
   1120 
   1121 	/* Channel Names ('A', 'B', etc.) */
   1122 	char			  channel;
   1123 
   1124 	/* Initiator Bus ID */
   1125 	uint8_t			  our_id;
   1126 	uint8_t			  our_id_b;
   1127 
   1128 	/*
   1129 	 * PCI error detection.
   1130 	 */
   1131 	int			  unsolicited_ints;
   1132 
   1133 	/*
   1134 	 * Target incoming command FIFO.
   1135 	 */
   1136 	struct target_cmd	 *targetcmds;
   1137 	uint8_t			  tqinfifonext;
   1138 
   1139 	/*
   1140 	 * Incoming and outgoing message handling.
   1141 	 */
   1142 	uint8_t			  send_msg_perror;
   1143 	ahc_msg_type		  msg_type;
   1144 	uint8_t			  msgout_buf[12];/* Message we are sending */
   1145 	uint8_t			  msgin_buf[12];/* Message we are receiving */
   1146 	u_int			  msgout_len;	/* Length of message to send */
   1147 	u_int			  msgout_index;	/* Current index in msgout */
   1148 	u_int			  msgin_index;	/* Current index in msgin */
   1149 
   1150 	/* Interrupt routine */
   1151 	void 			 *ih;
   1152 
   1153 	/*
   1154 	 * Mapping information for data structures shared
   1155 	 * between the sequencer and kernel.
   1156 	 */
   1157 	bus_dma_tag_t		  parent_dmat;
   1158 	bus_dmamap_t		  shared_data_dmamap;
   1159 	bus_addr_t		  shared_data_busaddr;
   1160 
   1161 	bus_dma_segment_t	  shared_data_seg;
   1162 	int			  shared_data_nseg;
   1163 	int			  shared_data_size;
   1164 	int			  sc_dmaflags;
   1165 
   1166 	/*
   1167 	 * Bus address of the one byte buffer used to
   1168 	 * work-around a DMA bug for chips <= aic7880
   1169 	 * in target mode.
   1170 	 */
   1171 	bus_addr_t		  dma_bug_buf;
   1172 
   1173 	/* Information saved through suspend/resume cycles */
   1174 	struct ahc_suspend_state  suspend_state;
   1175 
   1176 	/* Number of enabled target mode device on this card */
   1177 	u_int			  enabled_luns;
   1178 
   1179 	/* Initialization level of this data structure */
   1180 	u_int			  init_level;
   1181 
   1182 	/* PCI cacheline size. */
   1183 	u_int			  pci_cachesize;
   1184 
   1185 	u_int			  stack_size;
   1186 
   1187 	/* Per-Unit descriptive information */
   1188 	const char		 *description;
   1189 	char			 *name;
   1190 	int			  unit;
   1191 
   1192 	/* Selection Timer settings */
   1193 	int			  seltime;
   1194 	int			  seltime_b;
   1195 
   1196 	uint16_t	 	  user_discenable;/* Disconnection allowed  */
   1197 	uint16_t		  user_tagenable;/* Tagged Queuing allowed */
   1198 
   1199 	struct ahc_pci_busdata 	  *bd;
   1200 
   1201 	void			  *shutdown_hook;
   1202 };
   1203 
   1204 TAILQ_HEAD(ahc_softc_tailq, ahc_softc);
   1205 extern struct ahc_softc_tailq ahc_tailq;
   1206 
   1207 /************************ Active Device Information ***************************/
   1208 typedef enum {
   1209 	ROLE_UNKNOWN,
   1210 	ROLE_INITIATOR,
   1211 	ROLE_TARGET
   1212 } role_t;
   1213 
   1214 struct ahc_devinfo {
   1215 	int	 our_scsiid;
   1216 	int	 target_offset;
   1217 	uint16_t target_mask;
   1218 	u_int	 target;
   1219 	u_int	 lun;
   1220 	char	 channel;
   1221 	role_t	 role;		/*
   1222 				 * Only guaranteed to be correct if not
   1223 				 * in the busfree state.
   1224 				 */
   1225 };
   1226 
   1227 /****************************** PCI Structures ********************************/
   1228 typedef int (ahc_device_setup_t)(struct ahc_softc *);
   1229 
   1230 struct ahc_pci_identity {
   1231 	uint64_t		 full_id;
   1232 	uint64_t		 id_mask;
   1233 	char			*name;
   1234 	ahc_device_setup_t	*setup;
   1235 };
   1236 extern struct ahc_pci_identity ahc_pci_ident_table [];
   1237 extern const u_int ahc_num_pci_devs;
   1238 
   1239 /***************************** VL/EISA Declarations ***************************/
   1240 struct aic7770_identity {
   1241 	uint32_t		 full_id;
   1242 	uint32_t		 id_mask;
   1243 	char			*name;
   1244 	ahc_device_setup_t	*setup;
   1245 };
   1246 extern struct aic7770_identity aic7770_ident_table [];
   1247 extern const int ahc_num_aic7770_devs;
   1248 
   1249 #define AHC_EISA_SLOT_OFFSET	0xc00
   1250 #define AHC_EISA_IOSIZE		0x100
   1251 
   1252 /*************************** Function Declarations ****************************/
   1253 /******************************************************************************/
   1254 u_int			ahc_index_busy_tcl(struct ahc_softc *ahc, u_int tcl);
   1255 void			ahc_unbusy_tcl(struct ahc_softc *ahc, u_int tcl);
   1256 void			ahc_busy_tcl(struct ahc_softc *ahc,
   1257 				     u_int tcl, u_int busyid);
   1258 
   1259 /***************************** PCI Front End *********************************/
   1260 const struct ahc_pci_identity	*ahc_find_pci_device(pcireg_t id, pcireg_t subid, u_int func);
   1261 int			 ahc_pci_config(struct ahc_softc *,
   1262 					struct ahc_pci_identity *);
   1263 int			 ahc_pci_test_register_access(struct ahc_softc *);
   1264 
   1265 /*************************** EISA/VL Front End ********************************/
   1266 struct aic7770_identity *aic7770_find_device(uint32_t);
   1267 int			 aic7770_config(struct ahc_softc *ahc,
   1268 					struct aic7770_identity *,
   1269 					u_int port);
   1270 
   1271 /************************** SCB and SCB queue management **********************/
   1272 int		ahc_probe_scbs(struct ahc_softc *);
   1273 void		ahc_run_untagged_queues(struct ahc_softc *ahc);
   1274 void		ahc_run_untagged_queue(struct ahc_softc *ahc,
   1275 				       struct scb_tailq *queue);
   1276 void		ahc_qinfifo_requeue_tail(struct ahc_softc *ahc,
   1277 					 struct scb *scb);
   1278 int		ahc_match_scb(struct ahc_softc *ahc, struct scb *scb,
   1279 			      int target, char channel, int lun,
   1280 			      u_int tag, role_t role);
   1281 
   1282 /****************************** Initialization ********************************/
   1283 int			 ahc_softc_init(struct ahc_softc *);
   1284 void			 ahc_controller_info(struct ahc_softc *ahc, char *buf);
   1285 int			 ahc_init(struct ahc_softc *ahc);
   1286 void			 ahc_intr_enable(struct ahc_softc *ahc, int enable);
   1287 void			 ahc_pause_and_flushwork(struct ahc_softc *ahc);
   1288 int			 ahc_suspend(struct ahc_softc *ahc);
   1289 int			 ahc_resume(struct ahc_softc *ahc);
   1290 void			 ahc_softc_insert(struct ahc_softc *);
   1291 struct ahc_softc	*ahc_find_softc(struct ahc_softc *ahc);
   1292 void			 ahc_set_unit(struct ahc_softc *, int);
   1293 void			 ahc_set_name(struct ahc_softc *, char *);
   1294 void			 ahc_alloc_scbs(struct ahc_softc *ahc);
   1295 void			 ahc_free(struct ahc_softc *ahc);
   1296 int			 ahc_reset(struct ahc_softc *ahc);
   1297 void			 ahc_shutdown(void *arg);
   1298 
   1299 /*************************** Interrupt Services *******************************/
   1300 void			ahc_pci_intr(struct ahc_softc *ahc);
   1301 void			ahc_clear_intstat(struct ahc_softc *ahc);
   1302 void			ahc_run_qoutfifo(struct ahc_softc *ahc);
   1303 #ifdef AHC_TARGET_MODE
   1304 void			ahc_run_tqinfifo(struct ahc_softc *ahc, int paused);
   1305 #endif
   1306 void			ahc_handle_brkadrint(struct ahc_softc *ahc);
   1307 void			ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat);
   1308 void			ahc_handle_scsiint(struct ahc_softc *ahc,
   1309 					   u_int intstat);
   1310 void			ahc_clear_critical_section(struct ahc_softc *ahc);
   1311 
   1312 /***************************** Error Recovery *********************************/
   1313 typedef enum {
   1314 	SEARCH_COMPLETE,
   1315 	SEARCH_COUNT,
   1316 	SEARCH_REMOVE
   1317 } ahc_search_action;
   1318 int			ahc_search_qinfifo(struct ahc_softc *ahc, int target,
   1319 					   char channel, int lun, u_int tag,
   1320 					   role_t role, uint32_t status,
   1321 					   ahc_search_action action);
   1322 int			ahc_search_untagged_queues(struct ahc_softc *ahc,
   1323 						   struct scsipi_xfer *xs, /*ahc_io_ctx_t ctx*/
   1324 						   int target, char channel,
   1325 						   int lun, uint32_t status,
   1326 						   ahc_search_action action);
   1327 int			ahc_search_disc_list(struct ahc_softc *ahc, int target,
   1328 					     char channel, int lun, u_int tag,
   1329 					     int stop_on_first, int remove,
   1330 					     int save_state);
   1331 void			ahc_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
   1332 int			ahc_reset_channel(struct ahc_softc *ahc, char channel,
   1333 					  int initiate_reset);
   1334 int			ahc_abort_scbs(struct ahc_softc *ahc, int target,
   1335 				       char channel, int lun, u_int tag,
   1336 				       role_t role, uint32_t status);
   1337 void			ahc_restart(struct ahc_softc *ahc);
   1338 void			ahc_calc_residual(struct ahc_softc *ahc,
   1339 					  struct scb *scb);
   1340 /*************************** Utility Functions ********************************/
   1341 struct ahc_phase_table_entry*
   1342 			ahc_lookup_phase_entry(int phase);
   1343 void			ahc_compile_devinfo(struct ahc_devinfo *devinfo,
   1344 					    u_int our_id, u_int target,
   1345 					    u_int lun, char channel,
   1346 					    role_t role);
   1347 /************************** Transfer Negotiation ******************************/
   1348 struct ahc_syncrate*	ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
   1349 					  u_int *ppr_options, u_int maxsync);
   1350 u_int			ahc_find_period(struct ahc_softc *ahc,
   1351 					u_int scsirate, u_int maxsync);
   1352 void			ahc_validate_offset(struct ahc_softc *ahc,
   1353 					    struct ahc_initiator_tinfo *tinfo,
   1354 					    struct ahc_syncrate *syncrate,
   1355 					    u_int *offset, int wide,
   1356 					    role_t role);
   1357 void			ahc_validate_width(struct ahc_softc *ahc,
   1358 					   struct ahc_initiator_tinfo *tinfo,
   1359 					   u_int *bus_width,
   1360 					   role_t role);
   1361 /*
   1362  * Negotiation types.  These are used to qualify if we should renegotiate
   1363  * even if our goal and current transport parameters are identical.
   1364  */
   1365 typedef enum {
   1366 	AHC_NEG_TO_GOAL,	/* Renegotiate only if goal and curr differ. */
   1367 	AHC_NEG_IF_NON_ASYNC,	/* Renegotiate so long as goal is non-async. */
   1368 	AHC_NEG_ALWAYS		/* Renegotiat even if goal is async. */
   1369 } ahc_neg_type;
   1370 int			ahc_update_neg_request(struct ahc_softc*,
   1371 					       struct ahc_devinfo*,
   1372 					       struct ahc_tmode_tstate*,
   1373 					       struct ahc_initiator_tinfo*,
   1374 					       ahc_neg_type);
   1375 void			ahc_set_width(struct ahc_softc *ahc,
   1376 				      struct ahc_devinfo *devinfo,
   1377 				      u_int width, u_int type, int paused);
   1378 void			ahc_set_syncrate(struct ahc_softc *ahc,
   1379 					 struct ahc_devinfo *devinfo,
   1380 					 struct ahc_syncrate *syncrate,
   1381 					 u_int period, u_int offset,
   1382 					 u_int ppr_options,
   1383 					 u_int type, int paused);
   1384 typedef enum {
   1385 	AHC_QUEUE_NONE,
   1386 	AHC_QUEUE_BASIC,
   1387 	AHC_QUEUE_TAGGED
   1388 } ahc_queue_alg;
   1389 
   1390 void			ahc_set_tags(struct ahc_softc *ahc,
   1391 				     struct ahc_devinfo *devinfo,
   1392 				     ahc_queue_alg alg);
   1393 
   1394 /**************************** Target Mode *************************************/
   1395 #ifdef AHC_TARGET_MODE
   1396 void		ahc_send_lstate_events(struct ahc_softc *,
   1397 				       struct ahc_tmode_lstate *);
   1398 void		ahc_handle_en_lun(struct ahc_softc *ahc,
   1399 				  struct scsipi_xfer *xs
   1400 				  /*struct cam_sim *sim, union ccb *ccb*/);
   1401 cam_status	ahc_find_tmode_devs(struct ahc_softc *ahc,
   1402 				    /*struct cam_sim *sim, union ccb *ccb,*/
   1403 				    struct ahc_tmode_tstate **tstate,
   1404 				    struct ahc_tmode_lstate **lstate,
   1405 				    int notfound_failure);
   1406 #ifndef AHC_TMODE_ENABLE
   1407 #define AHC_TMODE_ENABLE 0
   1408 #endif
   1409 #endif
   1410 /******************************* Debug ***************************************/
   1411 #ifdef AHC_DEBUG
   1412 extern uint32_t ahc_debug;
   1413 #define	AHC_SHOW_MISC		0x0001
   1414 #define	AHC_SHOW_SENSE		0x0002
   1415 #define AHC_DUMP_SEEPROM	0x0004
   1416 #define AHC_SHOW_TERMCTL	0x0008
   1417 #define AHC_SHOW_MEMORY		0x0010
   1418 #define AHC_SHOW_MESSAGES	0x0020
   1419 #define	AHC_SHOW_DV		0x0040
   1420 #define AHC_SHOW_SELTO		0x0080
   1421 #define AHC_SHOW_QFULL		0x0200
   1422 #define AHC_SHOW_QUEUE		0x0400
   1423 #define AHC_SHOW_TQIN		0x0800
   1424 #define AHC_SHOW_MASKED_ERRORS	0x1000
   1425 #define AHC_DEBUG_SEQUENCER	0x2000
   1426 #endif
   1427 void			ahc_print_scb(struct scb *scb);
   1428 void			ahc_print_devinfo(struct ahc_softc *ahc,
   1429 					  struct ahc_devinfo *dev);
   1430 void			ahc_dump_card_state(struct ahc_softc *ahc);
   1431 int			ahc_print_register(ahc_reg_parse_entry_t *table,
   1432 					   u_int num_entries,
   1433 					   const char *name,
   1434 					   u_int address,
   1435 					   u_int value,
   1436 					   u_int *cur_column,
   1437 					   u_int wrap_point);
   1438 /******************************* SEEPROM *************************************/
   1439 int		ahc_acquire_seeprom(struct ahc_softc *ahc,
   1440 				    struct seeprom_descriptor *sd);
   1441 void		ahc_release_seeprom(struct seeprom_descriptor *sd);
   1442 
   1443 void		ahc_check_extport(struct ahc_softc *ahc, u_int *sxfrctl1);
   1444 #endif /* _AIC7XXXVAR_H_ */
   1445