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