Home | History | Annotate | Line # | Download | only in ic
advlib.c revision 1.7
      1 /*      $NetBSD: advlib.c,v 1.7 1998/10/28 20:39:46 dante Exp $        */
      2 
      3 /*
      4  * Low level routines for the Advanced Systems Inc. SCSI controllers chips
      5  *
      6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * Author: Baldassare Dante Profeta <dante (at) mclink.it>
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 /*
     40  * Ported from:
     41  */
     42 /*
     43  * advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
     44  *
     45  * Copyright (c) 1995-1998 Advanced System Products, Inc.
     46  * All Rights Reserved.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that redistributions of source
     50  * code retain the above copyright notice and this comment without
     51  * modification.
     52  *
     53  */
     54 
     55 #include <sys/types.h>
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/malloc.h>
     59 #include <sys/kernel.h>
     60 #include <sys/queue.h>
     61 #include <sys/device.h>
     62 
     63 #include <machine/bus.h>
     64 #include <machine/intr.h>
     65 
     66 #include <dev/scsipi/scsi_all.h>
     67 #include <dev/scsipi/scsipi_all.h>
     68 #include <dev/scsipi/scsiconf.h>
     69 
     70 #include <vm/vm.h>
     71 #include <vm/vm_param.h>
     72 #include <vm/pmap.h>
     73 
     74 #include <dev/ic/adv.h>
     75 #include <dev/ic/advlib.h>
     76 #include <dev/ic/advmcode.h>
     77 
     78 
     79 /* #define ASC_DEBUG */
     80 
     81 /******************************************************************************/
     82 /*                                Static functions                            */
     83 /******************************************************************************/
     84 
     85 /* Initializzation routines */
     86 static u_int32_t AscLoadMicroCode __P((bus_space_tag_t, bus_space_handle_t,
     87 					u_int16_t, u_int16_t *, u_int16_t));
     88 static void AscInitLram __P((ASC_SOFTC *));
     89 static void AscInitQLinkVar __P((ASC_SOFTC *));
     90 static int AscResetChipAndScsiBus __P((bus_space_tag_t, bus_space_handle_t));
     91 static u_int16_t AscGetChipBusType __P((bus_space_tag_t, bus_space_handle_t));
     92 
     93 /* Chip register routines */
     94 static void AscSetBank __P((bus_space_tag_t, bus_space_handle_t, u_int8_t));
     95 
     96 /* RISC Chip routines */
     97 static int AscStartChip __P((bus_space_tag_t, bus_space_handle_t));
     98 static int AscStopChip __P((bus_space_tag_t, bus_space_handle_t));
     99 static u_int8_t AscSetChipScsiID __P((bus_space_tag_t, bus_space_handle_t,
    100 					u_int8_t));
    101 static u_int8_t AscGetChipScsiCtrl __P((bus_space_tag_t, bus_space_handle_t));
    102 static u_int8_t AscGetChipVersion __P((bus_space_tag_t, bus_space_handle_t,
    103 					u_int16_t));
    104 static int AscSetRunChipSynRegAtID __P((bus_space_tag_t, bus_space_handle_t,
    105 					u_int8_t, u_int8_t));
    106 static int AscSetChipSynRegAtID __P((bus_space_tag_t, bus_space_handle_t,
    107 					u_int8_t, u_int8_t));
    108 static int AscHostReqRiscHalt __P((bus_space_tag_t, bus_space_handle_t));
    109 static int AscIsChipHalted __P((bus_space_tag_t, bus_space_handle_t));
    110 static void AscSetChipIH __P((bus_space_tag_t, bus_space_handle_t, u_int16_t));
    111 
    112 /* Lram routines */
    113 static u_int8_t AscReadLramByte __P((bus_space_tag_t, bus_space_handle_t,
    114 					u_int16_t));
    115 static void AscWriteLramByte __P((bus_space_tag_t, bus_space_handle_t,
    116 					u_int16_t, u_int8_t));
    117 static u_int16_t AscReadLramWord __P((bus_space_tag_t, bus_space_handle_t,
    118 					u_int16_t));
    119 static void AscWriteLramWord __P((bus_space_tag_t, bus_space_handle_t,
    120 					u_int16_t, u_int16_t));
    121 static u_int32_t AscReadLramDWord __P((bus_space_tag_t, bus_space_handle_t,
    122 					u_int16_t));
    123 static void AscWriteLramDWord __P((bus_space_tag_t, bus_space_handle_t,
    124 					u_int16_t, u_int32_t));
    125 static void AscMemWordSetLram __P((bus_space_tag_t, bus_space_handle_t,
    126 					u_int16_t, u_int16_t, int));
    127 static void AscMemWordCopyToLram __P((bus_space_tag_t, bus_space_handle_t,
    128 					u_int16_t, u_int16_t *, int));
    129 static void AscMemWordCopyFromLram __P((bus_space_tag_t, bus_space_handle_t,
    130 					u_int16_t, u_int16_t *, int));
    131 static void AscMemDWordCopyToLram __P((bus_space_tag_t, bus_space_handle_t,
    132 					u_int16_t, u_int32_t *, int));
    133 static u_int32_t AscMemSumLramWord __P((bus_space_tag_t, bus_space_handle_t,
    134 					u_int16_t, int));
    135 static int AscTestExternalLram __P((bus_space_tag_t, bus_space_handle_t));
    136 
    137 /* MicroCode routines */
    138 static u_int16_t AscInitMicroCodeVar __P((ASC_SOFTC *));
    139 static u_int32_t AscGetOnePhyAddr __P((ASC_SOFTC *, u_int8_t *, u_int32_t));
    140 static u_int32_t AscGetSGList __P((ASC_SOFTC *, u_int8_t *, u_int32_t,
    141 					ASC_SG_HEAD *));
    142 
    143 /* EEProm routines */
    144 static int AscWriteEEPCmdReg __P((bus_space_tag_t, bus_space_handle_t,
    145 					u_int8_t));
    146 static int AscWriteEEPDataReg __P((bus_space_tag_t, bus_space_handle_t,
    147 					u_int16_t));
    148 static void AscWaitEEPRead __P((void));
    149 static void AscWaitEEPWrite __P((void));
    150 static u_int16_t AscReadEEPWord __P((bus_space_tag_t, bus_space_handle_t,
    151 					u_int8_t));
    152 static u_int16_t AscWriteEEPWord __P((bus_space_tag_t, bus_space_handle_t,
    153 					u_int8_t, u_int16_t));
    154 static u_int16_t AscGetEEPConfig __P((bus_space_tag_t, bus_space_handle_t,
    155 					ASCEEP_CONFIG *, u_int16_t));
    156 static int AscSetEEPConfig __P((bus_space_tag_t, bus_space_handle_t,
    157 					ASCEEP_CONFIG *, u_int16_t));
    158 static int AscSetEEPConfigOnce __P((bus_space_tag_t, bus_space_handle_t,
    159 					ASCEEP_CONFIG *, u_int16_t));
    160 #ifdef ASC_DEBUG
    161 static void AscPrintEEPConfig __P((ASCEEP_CONFIG *, u_int16_t));
    162 #endif
    163 
    164 /* Interrupt routines */
    165 static void AscIsrChipHalted __P((ASC_SOFTC *));
    166 static int AscIsrQDone __P((ASC_SOFTC *));
    167 static int AscWaitTixISRDone __P((ASC_SOFTC *, u_int8_t));
    168 static int AscWaitISRDone __P((ASC_SOFTC *));
    169 static u_int8_t _AscCopyLramScsiDoneQ __P((bus_space_tag_t, bus_space_handle_t,
    170 					u_int16_t, ASC_QDONE_INFO *,
    171 					u_int32_t));
    172 static void AscGetQDoneInfo __P((bus_space_tag_t, bus_space_handle_t, u_int16_t,
    173 					ASC_QDONE_INFO *));
    174 static void AscToggleIRQAct __P((bus_space_tag_t, bus_space_handle_t));
    175 static void AscDisableInterrupt __P((bus_space_tag_t, bus_space_handle_t));
    176 static void AscEnableInterrupt __P((bus_space_tag_t, bus_space_handle_t));
    177 static u_int8_t AscGetChipIRQ __P((bus_space_tag_t, bus_space_handle_t,
    178 					u_int16_t));
    179 static u_int8_t AscSetChipIRQ __P((bus_space_tag_t, bus_space_handle_t,
    180 					u_int8_t, u_int16_t));
    181 static void AscAckInterrupt __P((bus_space_tag_t, bus_space_handle_t));
    182 static u_int32_t AscGetMaxDmaCount __P((u_int16_t));
    183 static u_int16_t AscGetIsaDmaChannel __P((bus_space_tag_t, bus_space_handle_t));
    184 static u_int16_t AscSetIsaDmaChannel __P((bus_space_tag_t, bus_space_handle_t,
    185 					u_int16_t));
    186 static u_int8_t AscGetIsaDmaSpeed __P((bus_space_tag_t, bus_space_handle_t));
    187 static u_int8_t AscSetIsaDmaSpeed __P((bus_space_tag_t, bus_space_handle_t,
    188 					u_int8_t));
    189 
    190 /* Messages routines */
    191 static void AscHandleExtMsgIn __P((ASC_SOFTC *, u_int16_t, u_int8_t,
    192 					ASC_SCSI_BIT_ID_TYPE, int, u_int8_t));
    193 static u_int8_t AscMsgOutSDTR __P((ASC_SOFTC *, u_int8_t, u_int8_t));
    194 
    195 /* SDTR routines */
    196 static void AscSetChipSDTR __P((bus_space_tag_t, bus_space_handle_t,
    197 					u_int8_t, u_int8_t));
    198 static u_int8_t AscCalSDTRData __P((ASC_SOFTC *, u_int8_t, u_int8_t));
    199 static u_int8_t AscGetSynPeriodIndex __P((ASC_SOFTC *, u_int8_t));
    200 
    201 /* Queue routines */
    202 static int AscSendScsiQueue __P((ASC_SOFTC *, ASC_SCSI_Q *, u_int8_t));
    203 static int AscSgListToQueue __P((int));
    204 static u_int AscGetNumOfFreeQueue __P((ASC_SOFTC *, u_int8_t, u_int8_t));
    205 static int AscPutReadyQueue __P((ASC_SOFTC *, ASC_SCSI_Q *, u_int8_t));
    206 static void AscPutSCSIQ __P((bus_space_tag_t, bus_space_handle_t,
    207 					 u_int16_t, ASC_SCSI_Q *));
    208 static int AscPutReadySgListQueue __P((ASC_SOFTC *, ASC_SCSI_Q *, u_int8_t));
    209 static u_int8_t AscAllocFreeQueue __P((bus_space_tag_t, bus_space_handle_t,
    210 					u_int8_t));
    211 static u_int8_t AscAllocMultipleFreeQueue __P((bus_space_tag_t,
    212 					bus_space_handle_t,
    213 					u_int8_t, u_int8_t));
    214 static int AscStopQueueExe __P((bus_space_tag_t, bus_space_handle_t));
    215 static void AscStartQueueExe __P((bus_space_tag_t, bus_space_handle_t));
    216 static void AscCleanUpBusyQueue __P((bus_space_tag_t, bus_space_handle_t));
    217 static int _AscWaitQDone __P((bus_space_tag_t, bus_space_handle_t,
    218 					ASC_SCSI_Q *));
    219 static int AscCleanUpDiscQueue __P((bus_space_tag_t, bus_space_handle_t));
    220 
    221 /* Abort and Reset CCB routines */
    222 static int AscRiscHaltedAbortCCB __P((ASC_SOFTC *, u_int32_t));
    223 static int AscRiscHaltedAbortTIX __P((ASC_SOFTC *, u_int8_t));
    224 
    225 /* Error Handling routines */
    226 static int AscSetLibErrorCode __P((ASC_SOFTC *, u_int16_t));
    227 
    228 /* Handle bugged borads routines */
    229 static int AscTagQueuingSafe __P((ASC_SCSI_INQUIRY *));
    230 static void AscAsyncFix __P((ASC_SOFTC *, u_int8_t, ASC_SCSI_INQUIRY *));
    231 
    232 /* Miscellaneous routines */
    233 static int AscCompareString __P((u_char *, u_char *, int));
    234 
    235 /* Device oriented routines */
    236 static int DvcEnterCritical __P((void));
    237 static void DvcLeaveCritical __P((int));
    238 static void DvcSleepMilliSecond __P((u_int32_t));
    239 //static void DvcDelayMicroSecond __P((u_int32_t));
    240 static void DvcDelayNanoSecond __P((u_int32_t));
    241 
    242 
    243 /******************************************************************************/
    244 /*                            Initializzation routines                        */
    245 /******************************************************************************/
    246 
    247 /*
    248  * This function perform the following steps:
    249  * - initialize ASC_SOFTC structure with defaults values.
    250  * - inquire board registers to know what kind of board it is.
    251  * - keep track of bugged borads.
    252  */
    253 void
    254 AscInitASC_SOFTC(sc)
    255 	ASC_SOFTC      *sc;
    256 {
    257 	bus_space_tag_t iot = sc->sc_iot;
    258 	bus_space_handle_t ioh = sc->sc_ioh;
    259 	int             i;
    260 	u_int8_t        chip_version;
    261 
    262 
    263 	ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_HALT);
    264 	ASC_SET_CHIP_STATUS(iot, ioh, 0);
    265 
    266 	sc->bug_fix_cntl = 0;
    267 	sc->pci_fix_asyn_xfer = 0;
    268 	sc->pci_fix_asyn_xfer_always = 0;
    269 	sc->sdtr_done = 0;
    270 	sc->cur_total_qng = 0;
    271 	sc->last_q_shortage = 0;
    272 	sc->use_tagged_qng = 0;
    273 	sc->unit_not_ready = 0;
    274 	sc->queue_full_or_busy = 0;
    275 	sc->host_init_sdtr_index = 0;
    276 	sc->can_tagged_qng = 0;
    277 	sc->cmd_qng_enabled = 0;
    278 	sc->dvc_cntl = ASC_DEF_DVC_CNTL;
    279 	sc->init_sdtr = 0;
    280 	sc->max_total_qng = ASC_DEF_MAX_TOTAL_QNG;
    281 	sc->scsi_reset_wait = 3;
    282 	sc->start_motor = ASC_SCSI_WIDTH_BIT_SET;
    283 	sc->max_dma_count = AscGetMaxDmaCount(sc->bus_type);
    284 	sc->sdtr_enable = ASC_SCSI_WIDTH_BIT_SET;
    285 	sc->disc_enable = ASC_SCSI_WIDTH_BIT_SET;
    286 	sc->chip_scsi_id = ASC_DEF_CHIP_SCSI_ID;
    287 	sc->lib_serial_no = ASC_LIB_SERIAL_NUMBER;
    288 	sc->lib_version = (ASC_LIB_VERSION_MAJOR << 8) | ASC_LIB_VERSION_MINOR;
    289 	chip_version = AscGetChipVersion(iot, ioh, sc->bus_type);
    290 	sc->chip_version = chip_version;
    291 	if ((sc->bus_type & ASC_IS_PCI) &&
    292 	    (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3150)) {
    293 		sc->bus_type = ASC_IS_PCI_ULTRA;
    294 		sc->sdtr_period_tbl[0] = SYN_ULTRA_XFER_NS_0;
    295 		sc->sdtr_period_tbl[1] = SYN_ULTRA_XFER_NS_1;
    296 		sc->sdtr_period_tbl[2] = SYN_ULTRA_XFER_NS_2;
    297 		sc->sdtr_period_tbl[3] = SYN_ULTRA_XFER_NS_3;
    298 		sc->sdtr_period_tbl[4] = SYN_ULTRA_XFER_NS_4;
    299 		sc->sdtr_period_tbl[5] = SYN_ULTRA_XFER_NS_5;
    300 		sc->sdtr_period_tbl[6] = SYN_ULTRA_XFER_NS_6;
    301 		sc->sdtr_period_tbl[7] = SYN_ULTRA_XFER_NS_7;
    302 		sc->sdtr_period_tbl[8] = SYN_ULTRA_XFER_NS_8;
    303 		sc->sdtr_period_tbl[9] = SYN_ULTRA_XFER_NS_9;
    304 		sc->sdtr_period_tbl[10] = SYN_ULTRA_XFER_NS_10;
    305 		sc->sdtr_period_tbl[11] = SYN_ULTRA_XFER_NS_11;
    306 		sc->sdtr_period_tbl[12] = SYN_ULTRA_XFER_NS_12;
    307 		sc->sdtr_period_tbl[13] = SYN_ULTRA_XFER_NS_13;
    308 		sc->sdtr_period_tbl[14] = SYN_ULTRA_XFER_NS_14;
    309 		sc->sdtr_period_tbl[15] = SYN_ULTRA_XFER_NS_15;
    310 		sc->max_sdtr_index = 15;
    311 		if (chip_version == ASC_CHIP_VER_PCI_ULTRA_3150)
    312 			ASC_SET_EXTRA_CONTROL(iot, ioh,
    313 				       (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
    314 		else if (chip_version >= ASC_CHIP_VER_PCI_ULTRA_3050)
    315 			ASC_SET_EXTRA_CONTROL(iot, ioh,
    316 				   (SEC_ACTIVE_NEGATE | SEC_ENABLE_FILTER));
    317 	} else {
    318 		sc->sdtr_period_tbl[0] = SYN_XFER_NS_0;
    319 		sc->sdtr_period_tbl[1] = SYN_XFER_NS_1;
    320 		sc->sdtr_period_tbl[2] = SYN_XFER_NS_2;
    321 		sc->sdtr_period_tbl[3] = SYN_XFER_NS_3;
    322 		sc->sdtr_period_tbl[4] = SYN_XFER_NS_4;
    323 		sc->sdtr_period_tbl[5] = SYN_XFER_NS_5;
    324 		sc->sdtr_period_tbl[6] = SYN_XFER_NS_6;
    325 		sc->sdtr_period_tbl[7] = SYN_XFER_NS_7;
    326 		sc->max_sdtr_index = 7;
    327 	}
    328 
    329 	if (sc->bus_type == ASC_IS_PCI)
    330 		ASC_SET_EXTRA_CONTROL(iot, ioh,
    331 				      (SEC_ACTIVE_NEGATE | SEC_SLEW_RATE));
    332 
    333 	sc->isa_dma_speed = ASC_DEF_ISA_DMA_SPEED;
    334 	if (AscGetChipBusType(iot, ioh) == ASC_IS_ISAPNP) {
    335 		ASC_SET_CHIP_IFC(iot, ioh, ASC_IFC_INIT_DEFAULT);
    336 		sc->bus_type = ASC_IS_ISAPNP;
    337 	}
    338 	if ((sc->bus_type & ASC_IS_ISA) != 0)
    339 		sc->isa_dma_channel = AscGetIsaDmaChannel(iot, ioh);
    340 
    341 	for (i = 0; i <= ASC_MAX_TID; i++) {
    342 		sc->cur_dvc_qng[i] = 0;
    343 		sc->max_dvc_qng[i] = ASC_MAX_SCSI1_QNG;
    344 		sc->max_tag_qng[i] = ASC_MAX_INRAM_TAG_QNG;
    345 	}
    346 }
    347 
    348 
    349 /*
    350  * This function initialize some ASC_SOFTC fields with values read from
    351  * on-board EEProm.
    352  */
    353 u_int16_t
    354 AscInitFromEEP(sc)
    355 	ASC_SOFTC      *sc;
    356 {
    357 	bus_space_tag_t iot = sc->sc_iot;
    358 	bus_space_handle_t ioh = sc->sc_ioh;
    359 	ASCEEP_CONFIG   eep_config_buf;
    360 	ASCEEP_CONFIG  *eep_config;
    361 	u_int16_t       chksum;
    362 	u_int16_t       warn_code;
    363 	u_int16_t       cfg_msw, cfg_lsw;
    364 	int             i;
    365 	int             write_eep = 0;
    366 
    367 
    368 	warn_code = 0;
    369 	AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0x00FE);
    370 	AscStopQueueExe(iot, ioh);
    371 	if ((AscStopChip(iot, ioh) == FALSE) ||
    372 	    (AscGetChipScsiCtrl(iot, ioh) != 0)) {
    373 		AscResetChipAndScsiBus(iot, ioh);
    374 		DvcSleepMilliSecond(sc->scsi_reset_wait * 1000);
    375 	}
    376 	if (AscIsChipHalted(iot, ioh) == FALSE)
    377 		return (-1);
    378 
    379 	ASC_SET_PC_ADDR(iot, ioh, ASC_MCODE_START_ADDR);
    380 	if (ASC_GET_PC_ADDR(iot, ioh) != ASC_MCODE_START_ADDR)
    381 		return (-2);
    382 
    383 	eep_config = &eep_config_buf;
    384 	cfg_msw = ASC_GET_CHIP_CFG_MSW(iot, ioh);
    385 	cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh);
    386 	if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
    387 		cfg_msw &= (~(ASC_CFG_MSW_CLR_MASK));
    388 		warn_code |= ASC_WARN_CFG_MSW_RECOVER;
    389 		ASC_SET_CHIP_CFG_MSW(iot, ioh, cfg_msw);
    390 	}
    391 	chksum = AscGetEEPConfig(iot, ioh, eep_config, sc->bus_type);
    392 #ifdef ASC_DEBUG
    393 	AscPrintEEPConfig(eep_config, chksum);
    394 #endif
    395 	if (chksum == 0)
    396 		chksum = 0xAA55;
    397 
    398 	if (ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_AUTO_CONFIG) {
    399 		warn_code |= ASC_WARN_AUTO_CONFIG;
    400 		if (sc->chip_version == 3) {
    401 			if (eep_config->cfg_lsw != cfg_lsw) {
    402 				warn_code |= ASC_WARN_EEPROM_RECOVER;
    403 				eep_config->cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh);
    404 			}
    405 			if (eep_config->cfg_msw != cfg_msw) {
    406 				warn_code |= ASC_WARN_EEPROM_RECOVER;
    407 				eep_config->cfg_msw = ASC_GET_CHIP_CFG_MSW(iot, ioh);
    408 			}
    409 		}
    410 	}
    411 	eep_config->cfg_msw &= ~ASC_CFG_MSW_CLR_MASK;
    412 	eep_config->cfg_lsw |= ASC_CFG0_HOST_INT_ON;
    413 
    414 	if (chksum != eep_config->chksum) {
    415 		if (AscGetChipVersion(iot, ioh, sc->bus_type) ==
    416 		    ASC_CHIP_VER_PCI_ULTRA_3050) {
    417 			eep_config->init_sdtr = 0xFF;
    418 			eep_config->disc_enable = 0xFF;
    419 			eep_config->start_motor = 0xFF;
    420 			eep_config->use_cmd_qng = 0;
    421 			eep_config->max_total_qng = 0xF0;
    422 			eep_config->max_tag_qng = 0x20;
    423 			eep_config->cntl = 0xBFFF;
    424 			eep_config->chip_scsi_id = 7;
    425 			eep_config->no_scam = 0;
    426 			eep_config->adapter_info[0] = 0;
    427 			eep_config->adapter_info[1] = 0;
    428 			eep_config->adapter_info[2] = 0;
    429 			eep_config->adapter_info[3] = 0;
    430 #if BYTE_ORDER == BIG_ENDIAN
    431 			eep_config->adapter_info[5] = 0;
    432 			/* Indicate EEPROM-less board. */
    433 			eep_config->adapter_info[4] = 0xBB;
    434 #else
    435 			eep_config->adapter_info[4] = 0;
    436 			/* Indicate EEPROM-less board. */
    437 			eep_config->adapter_info[5] = 0xBB;
    438 #endif
    439 		} else {
    440 			write_eep = 1;
    441 			warn_code |= ASC_WARN_EEPROM_CHKSUM;
    442 		}
    443 	}
    444 	sc->sdtr_enable = eep_config->init_sdtr;
    445 	sc->disc_enable = eep_config->disc_enable;
    446 	sc->cmd_qng_enabled = eep_config->use_cmd_qng;
    447 	sc->isa_dma_speed = eep_config->isa_dma_speed;
    448 	sc->start_motor = eep_config->start_motor;
    449 	sc->dvc_cntl = eep_config->cntl;
    450 #if BYTE_ORDER == BIG_ENDIAN
    451 	sc->adapter_info[0] = eep_config->adapter_info[1];
    452 	sc->adapter_info[1] = eep_config->adapter_info[0];
    453 	sc->adapter_info[2] = eep_config->adapter_info[3];
    454 	sc->adapter_info[3] = eep_config->adapter_info[2];
    455 	sc->adapter_info[4] = eep_config->adapter_info[5];
    456 	sc->adapter_info[5] = eep_config->adapter_info[4];
    457 #else
    458 	sc->adapter_info[0] = eep_config->adapter_info[0];
    459 	sc->adapter_info[1] = eep_config->adapter_info[1];
    460 	sc->adapter_info[2] = eep_config->adapter_info[2];
    461 	sc->adapter_info[3] = eep_config->adapter_info[3];
    462 	sc->adapter_info[4] = eep_config->adapter_info[4];
    463 	sc->adapter_info[5] = eep_config->adapter_info[5];
    464 #endif
    465 
    466 	if (!AscTestExternalLram(iot, ioh)) {
    467 		if (((sc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA)) {
    468 			eep_config->max_total_qng = ASC_MAX_PCI_ULTRA_INRAM_TOTAL_QNG;
    469 			eep_config->max_tag_qng = ASC_MAX_PCI_ULTRA_INRAM_TAG_QNG;
    470 		} else {
    471 			eep_config->cfg_msw |= 0x0800;
    472 			cfg_msw |= 0x0800;
    473 			ASC_SET_CHIP_CFG_MSW(iot, ioh, cfg_msw);
    474 			eep_config->max_total_qng = ASC_MAX_PCI_INRAM_TOTAL_QNG;
    475 			eep_config->max_tag_qng = ASC_MAX_INRAM_TAG_QNG;
    476 		}
    477 	}
    478 	if (eep_config->max_total_qng < ASC_MIN_TOTAL_QNG)
    479 		eep_config->max_total_qng = ASC_MIN_TOTAL_QNG;
    480 
    481 	if (eep_config->max_total_qng > ASC_MAX_TOTAL_QNG)
    482 		eep_config->max_total_qng = ASC_MAX_TOTAL_QNG;
    483 
    484 	if (eep_config->max_tag_qng > eep_config->max_total_qng)
    485 		eep_config->max_tag_qng = eep_config->max_total_qng;
    486 
    487 	if (eep_config->max_tag_qng < ASC_MIN_TAG_Q_PER_DVC)
    488 		eep_config->max_tag_qng = ASC_MIN_TAG_Q_PER_DVC;
    489 
    490 	sc->max_total_qng = eep_config->max_total_qng;
    491 	if ((eep_config->use_cmd_qng & eep_config->disc_enable) !=
    492 	    eep_config->use_cmd_qng) {
    493 		eep_config->disc_enable = eep_config->use_cmd_qng;
    494 		warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
    495 	}
    496 	if (sc->bus_type & (ASC_IS_ISA | ASC_IS_VL | ASC_IS_EISA))
    497 		sc->irq_no = AscGetChipIRQ(iot, ioh, sc->bus_type);
    498 
    499 	eep_config->chip_scsi_id &= ASC_MAX_TID;
    500 	sc->chip_scsi_id = eep_config->chip_scsi_id;
    501 	if (((sc->bus_type & ASC_IS_PCI_ULTRA) == ASC_IS_PCI_ULTRA) &&
    502 	    !(sc->dvc_cntl & ASC_CNTL_SDTR_ENABLE_ULTRA)) {
    503 		sc->host_init_sdtr_index = ASC_SDTR_ULTRA_PCI_10MB_INDEX;
    504 	}
    505 	for (i = 0; i <= ASC_MAX_TID; i++) {
    506 		sc->max_tag_qng[i] = eep_config->max_tag_qng;
    507 		sc->sdtr_period_offset[i] = ASC_DEF_SDTR_OFFSET |
    508 			(sc->host_init_sdtr_index << 4);
    509 	}
    510 
    511 	eep_config->cfg_msw = ASC_GET_CHIP_CFG_MSW(iot, ioh);
    512 	if (write_eep) {
    513 		AscSetEEPConfig(iot, ioh, eep_config, sc->bus_type);
    514 #ifdef ASC_DEBUG
    515 		AscPrintEEPConfig(eep_config, 0);
    516 #endif
    517 	}
    518 
    519 	return (warn_code);
    520 }
    521 
    522 
    523 u_int16_t
    524 AscInitFromASC_SOFTC(sc)
    525 	ASC_SOFTC      *sc;
    526 {
    527 	bus_space_tag_t iot = sc->sc_iot;
    528 	bus_space_handle_t ioh = sc->sc_ioh;
    529 	u_int16_t       cfg_msw;
    530 	u_int16_t       warn_code;
    531 	u_int16_t       pci_device_id = sc->pci_device_id;
    532 
    533 
    534 	warn_code = 0;
    535 	cfg_msw = ASC_GET_CHIP_CFG_MSW(iot, ioh);
    536 
    537 	if ((cfg_msw & ASC_CFG_MSW_CLR_MASK) != 0) {
    538 		cfg_msw &= (~(ASC_CFG_MSW_CLR_MASK));
    539 		warn_code |= ASC_WARN_CFG_MSW_RECOVER;
    540 		ASC_SET_CHIP_CFG_MSW(iot, ioh, cfg_msw);
    541 	}
    542 	if ((sc->cmd_qng_enabled & sc->disc_enable) != sc->cmd_qng_enabled) {
    543 		sc->disc_enable = sc->cmd_qng_enabled;
    544 		warn_code |= ASC_WARN_CMD_QNG_CONFLICT;
    545 	}
    546 	if (ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_AUTO_CONFIG) {
    547 		warn_code |= ASC_WARN_AUTO_CONFIG;
    548 	}
    549 	if ((sc->bus_type & (ASC_IS_ISA | ASC_IS_VL)) != 0) {
    550 		AscSetChipIRQ(iot, ioh, sc->irq_no, sc->bus_type);
    551 	}
    552 	if (sc->bus_type & ASC_IS_PCI) {
    553 		cfg_msw &= 0xFFC0;
    554 		ASC_SET_CHIP_CFG_MSW(iot, ioh, cfg_msw);
    555 
    556 		if ((sc->bus_type & ASC_IS_PCI_ULTRA) != ASC_IS_PCI_ULTRA) {
    557 			if ((pci_device_id == ASC_PCI_DEVICE_ID_REV_A) ||
    558 			    (pci_device_id == ASC_PCI_DEVICE_ID_REV_B)) {
    559 				sc->bug_fix_cntl |= ASC_BUG_FIX_IF_NOT_DWB;
    560 				sc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
    561 			}
    562 		}
    563 	} else if (sc->bus_type == ASC_IS_ISAPNP) {
    564 		if (AscGetChipVersion(iot, ioh, sc->bus_type) ==
    565 		    ASC_CHIP_VER_ASYN_BUG) {
    566 			sc->bug_fix_cntl |= ASC_BUG_FIX_ASYN_USE_SYN;
    567 		}
    568 	}
    569 	AscSetChipScsiID(iot, ioh, sc->chip_scsi_id);
    570 
    571 	if (sc->bus_type & ASC_IS_ISA) {
    572 		AscSetIsaDmaChannel(iot, ioh, sc->isa_dma_channel);
    573 		AscSetIsaDmaSpeed(iot, ioh, sc->isa_dma_speed);
    574 	}
    575 	return (warn_code);
    576 }
    577 
    578 
    579 /*
    580  * - Initialize RISC chip
    581  * - Intialize Lram
    582  * - Load uCode into Lram
    583  * - Enable Interrupts
    584  */
    585 int
    586 AscInitDriver(sc)
    587 	ASC_SOFTC      *sc;
    588 {
    589 	bus_space_tag_t iot = sc->sc_iot;
    590 	bus_space_handle_t ioh = sc->sc_ioh;
    591 	u_int32_t       chksum;
    592 
    593 
    594 	if (!AscFindSignature(iot, ioh))
    595 		return (1);
    596 
    597 	AscDisableInterrupt(iot, ioh);
    598 
    599 	AscInitLram(sc);
    600 	chksum = AscLoadMicroCode(iot, ioh, 0, (u_int16_t *) asc_mcode,
    601 				  asc_mcode_size);
    602 	if (chksum != asc_mcode_chksum)
    603 		return (2);
    604 
    605 	if (AscInitMicroCodeVar(sc) == 0)
    606 		return (3);
    607 
    608 	AscEnableInterrupt(iot, ioh);
    609 
    610 	return (0);
    611 }
    612 
    613 
    614 int
    615 AscFindSignature(iot, ioh)
    616 	bus_space_tag_t iot;
    617 	bus_space_handle_t ioh;
    618 {
    619 	u_int16_t       sig_word;
    620 
    621 	if (ASC_GET_CHIP_SIGNATURE_BYTE(iot, ioh) == ASC_1000_ID1B) {
    622 		sig_word = ASC_GET_CHIP_SIGNATURE_WORD(iot, ioh);
    623 		if (sig_word == ASC_1000_ID0W ||
    624 		    sig_word == ASC_1000_ID0W_FIX)
    625 			return (1);
    626 	}
    627 	return (0);
    628 }
    629 
    630 
    631 static void
    632 AscInitLram(sc)
    633 	ASC_SOFTC      *sc;
    634 {
    635 	bus_space_tag_t iot = sc->sc_iot;
    636 	bus_space_handle_t ioh = sc->sc_ioh;
    637 	u_int8_t        i;
    638 	u_int16_t       s_addr;
    639 
    640 
    641 	AscMemWordSetLram(iot, ioh, ASC_QADR_BEG, 0,
    642 			  (((sc->max_total_qng + 2 + 1) * 64) >> 1));
    643 
    644 	i = ASC_MIN_ACTIVE_QNO;
    645 	s_addr = ASC_QADR_BEG + ASC_QBLK_SIZE;
    646 	AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_FWD, i + 1);
    647 	AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_BWD, sc->max_total_qng);
    648 	AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_QNO, i);
    649 	i++;
    650 	s_addr += ASC_QBLK_SIZE;
    651 	for (; i < sc->max_total_qng; i++, s_addr += ASC_QBLK_SIZE) {
    652 		AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_FWD, i + 1);
    653 		AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_BWD, i - 1);
    654 		AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_QNO, i);
    655 	}
    656 	AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_FWD, ASC_QLINK_END);
    657 	AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_BWD, sc->max_total_qng - 1);
    658 	AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_QNO, sc->max_total_qng);
    659 	i++;
    660 	s_addr += ASC_QBLK_SIZE;
    661 	for (; i <= (u_int8_t) (sc->max_total_qng + 3); i++, s_addr += ASC_QBLK_SIZE) {
    662 		AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_FWD, i);
    663 		AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_BWD, i);
    664 		AscWriteLramByte(iot, ioh, s_addr + ASC_SCSIQ_B_QNO, i);
    665 	}
    666 }
    667 
    668 
    669 void
    670 AscReInitLram(sc)
    671 	ASC_SOFTC      *sc;
    672 {
    673 
    674 	AscInitLram(sc);
    675 	AscInitQLinkVar(sc);
    676 }
    677 
    678 
    679 static void
    680 AscInitQLinkVar(sc)
    681 	ASC_SOFTC      *sc;
    682 {
    683 	bus_space_tag_t iot = sc->sc_iot;
    684 	bus_space_handle_t ioh = sc->sc_ioh;
    685 	u_int8_t        i;
    686 	u_int16_t       lram_addr;
    687 
    688 
    689 	ASC_PUT_RISC_VAR_FREE_QHEAD(iot, ioh, 1);
    690 	ASC_PUT_RISC_VAR_DONE_QTAIL(iot, ioh, sc->max_total_qng);
    691 	ASC_PUT_VAR_FREE_QHEAD(iot, ioh, 1);
    692 	ASC_PUT_VAR_DONE_QTAIL(iot, ioh, sc->max_total_qng);
    693 	AscWriteLramByte(iot, ioh, ASCV_BUSY_QHEAD_B, sc->max_total_qng + 1);
    694 	AscWriteLramByte(iot, ioh, ASCV_DISC1_QHEAD_B, sc->max_total_qng + 2);
    695 	AscWriteLramByte(iot, ioh, ASCV_TOTAL_READY_Q_B, sc->max_total_qng);
    696 	AscWriteLramWord(iot, ioh, ASCV_ASCDVC_ERR_CODE_W, 0);
    697 	AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
    698 	AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B, 0);
    699 	AscWriteLramByte(iot, ioh, ASCV_SCSIBUSY_B, 0);
    700 	AscWriteLramByte(iot, ioh, ASCV_WTM_FLAG_B, 0);
    701 	ASC_PUT_QDONE_IN_PROGRESS(iot, ioh, 0);
    702 	lram_addr = ASC_QADR_BEG;
    703 	for (i = 0; i < 32; i++, lram_addr += 2)
    704 		AscWriteLramWord(iot, ioh, lram_addr, 0);
    705 }
    706 
    707 
    708 static int
    709 AscResetChipAndScsiBus(bus_space_tag_t iot,
    710 		       bus_space_handle_t ioh)
    711 {
    712 	while (ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_SCSI_RESET_ACTIVE);
    713 
    714 	AscStopChip(iot, ioh);
    715 	ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_CHIP_RESET | ASC_CC_SCSI_RESET | ASC_CC_HALT);
    716 
    717 	DvcDelayNanoSecond(60000);
    718 
    719 	AscSetChipIH(iot, ioh, ASC_INS_RFLAG_WTM);
    720 	AscSetChipIH(iot, ioh, ASC_INS_HALT);
    721 	ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_CHIP_RESET | ASC_CC_HALT);
    722 	ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_HALT);
    723 
    724 	DvcSleepMilliSecond(200);
    725 
    726 	ASC_SET_CHIP_STATUS(iot, ioh, ASC_CIW_CLR_SCSI_RESET_INT);
    727 	AscStartChip(iot, ioh);
    728 
    729 	DvcSleepMilliSecond(200);
    730 
    731 	return (AscIsChipHalted(iot, ioh));
    732 }
    733 
    734 
    735 static u_int16_t
    736 AscGetChipBusType(iot, ioh)
    737 	bus_space_tag_t iot;
    738 	bus_space_handle_t ioh;
    739 {
    740 	u_int16_t       chip_ver;
    741 
    742 	chip_ver = ASC_GET_CHIP_VER_NO(iot, ioh);
    743 	if ((chip_ver >= ASC_CHIP_MIN_VER_VL) &&
    744 	    (chip_ver <= ASC_CHIP_MAX_VER_VL)) {
    745 		/*
    746 		 * if(((iop_base & 0x0C30) == 0x0C30) || ((iop_base & 0x0C50)
    747 		 * == 0x0C50)) return (ASC_IS_EISA);
    748 		 */
    749 		return (ASC_IS_VL);
    750 	}
    751 	if ((chip_ver >= ASC_CHIP_MIN_VER_ISA) &&
    752 	    (chip_ver <= ASC_CHIP_MAX_VER_ISA)) {
    753 		if (chip_ver >= ASC_CHIP_MIN_VER_ISA_PNP)
    754 			return (ASC_IS_ISAPNP);
    755 
    756 		return (ASC_IS_ISA);
    757 	} else if ((chip_ver >= ASC_CHIP_MIN_VER_PCI) &&
    758 		   (chip_ver <= ASC_CHIP_MAX_VER_PCI))
    759 		return (ASC_IS_PCI);
    760 
    761 	return (0);
    762 }
    763 
    764 
    765 /******************************************************************************/
    766 /*                             Chip register routines                         */
    767 /******************************************************************************/
    768 
    769 
    770 static void
    771 AscSetBank(iot, ioh, bank)
    772 	bus_space_tag_t iot;
    773 	bus_space_handle_t ioh;
    774 	u_int8_t        bank;
    775 {
    776 	u_int8_t        val;
    777 
    778 	val = ASC_GET_CHIP_CONTROL(iot, ioh) &
    779 		(~(ASC_CC_SINGLE_STEP | ASC_CC_TEST |
    780 		   ASC_CC_DIAG | ASC_CC_SCSI_RESET |
    781 		   ASC_CC_CHIP_RESET));
    782 
    783 	switch (bank) {
    784 	case 1:
    785 		val |= ASC_CC_BANK_ONE;
    786 		break;
    787 
    788 	case 2:
    789 		val |= ASC_CC_DIAG | ASC_CC_BANK_ONE;
    790 		break;
    791 
    792 	default:
    793 		val &= ~ASC_CC_BANK_ONE;
    794 	}
    795 
    796 	ASC_SET_CHIP_CONTROL(iot, ioh, val);
    797 	return;
    798 }
    799 
    800 
    801 /******************************************************************************/
    802 /*                                 Chip routines                              */
    803 /******************************************************************************/
    804 
    805 
    806 static int
    807 AscStartChip(iot, ioh)
    808 	bus_space_tag_t iot;
    809 	bus_space_handle_t ioh;
    810 {
    811 	ASC_SET_CHIP_CONTROL(iot, ioh, 0);
    812 	if ((ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_HALTED) != 0)
    813 		return (0);
    814 
    815 	return (1);
    816 }
    817 
    818 
    819 static int
    820 AscStopChip(iot, ioh)
    821 	bus_space_tag_t iot;
    822 	bus_space_handle_t ioh;
    823 {
    824 	u_int8_t        cc_val;
    825 
    826 	cc_val = ASC_GET_CHIP_CONTROL(iot, ioh) &
    827 		(~(ASC_CC_SINGLE_STEP | ASC_CC_TEST | ASC_CC_DIAG));
    828 	ASC_SET_CHIP_CONTROL(iot, ioh, cc_val | ASC_CC_HALT);
    829 	AscSetChipIH(iot, ioh, ASC_INS_HALT);
    830 	AscSetChipIH(iot, ioh, ASC_INS_RFLAG_WTM);
    831 	if ((ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_HALTED) == 0)
    832 		return (0);
    833 
    834 	return (1);
    835 }
    836 
    837 
    838 static u_int8_t
    839 AscGetChipVersion(iot, ioh, bus_type)
    840 	bus_space_tag_t iot;
    841 	bus_space_handle_t ioh;
    842 	u_int16_t       bus_type;
    843 {
    844 	if (bus_type & ASC_IS_EISA) {
    845 		/*
    846 		 * u_int16_t	eisa_iop; u_int8_t	revision;
    847 		 *
    848 		 * eisa_iop = ASC_GET_EISA_SLOT(iop_base) |
    849 		 * ASC_EISA_REV_IOP_MASK; revision = inp(eisa_iop);
    850 		 * return((ASC_CHIP_MIN_VER_EISA - 1) + revision);
    851 		 */
    852 	}
    853 	return (ASC_GET_CHIP_VER_NO(iot, ioh));
    854 }
    855 
    856 
    857 static u_int8_t
    858 AscSetChipScsiID(iot, ioh, new_id)
    859 	bus_space_tag_t iot;
    860 	bus_space_handle_t ioh;
    861 	u_int8_t        new_id;
    862 {
    863 	u_int16_t       cfg_lsw;
    864 
    865 	if (ASC_GET_CHIP_SCSI_ID(iot, ioh) == new_id)
    866 		return (new_id);
    867 
    868 	cfg_lsw = ASC_GET_CHIP_SCSI_ID(iot, ioh);
    869 	cfg_lsw &= 0xF8FF;
    870 	cfg_lsw |= (new_id & ASC_MAX_TID) << 8;
    871 	ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg_lsw);
    872 	return (ASC_GET_CHIP_SCSI_ID(iot, ioh));
    873 }
    874 
    875 
    876 static u_int8_t
    877 AscGetChipScsiCtrl(iot, ioh)
    878 	bus_space_tag_t iot;
    879 	bus_space_handle_t ioh;
    880 {
    881 	u_int8_t        scsi_ctrl;
    882 
    883 	AscSetBank(iot, ioh, 1);
    884 	scsi_ctrl = bus_space_read_1(iot, ioh, ASC_IOP_REG_SC);
    885 	AscSetBank(iot, ioh, 0);
    886 	return (scsi_ctrl);
    887 }
    888 
    889 
    890 static int
    891 AscSetRunChipSynRegAtID(iot, ioh, tid_no, sdtr_data)
    892 	bus_space_tag_t iot;
    893 	bus_space_handle_t ioh;
    894 	u_int8_t        tid_no;
    895 	u_int8_t        sdtr_data;
    896 {
    897 	int             retval = FALSE;
    898 
    899 	if (AscHostReqRiscHalt(iot, ioh)) {
    900 		retval = AscSetChipSynRegAtID(iot, ioh, tid_no, sdtr_data);
    901 		AscStartChip(iot, ioh);
    902 	}
    903 	return (retval);
    904 }
    905 
    906 
    907 static int
    908 AscSetChipSynRegAtID(iot, ioh, id, sdtr_data)
    909 	bus_space_tag_t iot;
    910 	bus_space_handle_t ioh;
    911 	u_int8_t        id;
    912 	u_int8_t        sdtr_data;
    913 {
    914 	ASC_SCSI_BIT_ID_TYPE org_id;
    915 	int             i;
    916 	int             sta = TRUE;
    917 
    918 	AscSetBank(iot, ioh, 1);
    919 	org_id = ASC_READ_CHIP_DVC_ID(iot, ioh);
    920 	for (i = 0; i <= ASC_MAX_TID; i++)
    921 		if (org_id == (0x01 << i))
    922 			break;
    923 
    924 	org_id = i;
    925 	ASC_WRITE_CHIP_DVC_ID(iot, ioh, id);
    926 	if (ASC_READ_CHIP_DVC_ID(iot, ioh) == (0x01 << id)) {
    927 		AscSetBank(iot, ioh, 0);
    928 		ASC_SET_CHIP_SYN(iot, ioh, sdtr_data);
    929 		if (ASC_GET_CHIP_SYN(iot, ioh) != sdtr_data)
    930 			sta = FALSE;
    931 	} else
    932 		sta = FALSE;
    933 
    934 	AscSetBank(iot, ioh, 1);
    935 	ASC_WRITE_CHIP_DVC_ID(iot, ioh, org_id);
    936 	AscSetBank(iot, ioh, 0);
    937 	return (sta);
    938 }
    939 
    940 
    941 static int
    942 AscHostReqRiscHalt(iot, ioh)
    943 	bus_space_tag_t iot;
    944 	bus_space_handle_t ioh;
    945 {
    946 	int             count = 0;
    947 	int             retval = 0;
    948 	u_int8_t        saved_stop_code;
    949 
    950 
    951 	if (AscIsChipHalted(iot, ioh))
    952 		return (1);
    953 	saved_stop_code = AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B);
    954 	AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B,
    955 		      ASC_STOP_HOST_REQ_RISC_HALT | ASC_STOP_REQ_RISC_STOP);
    956 
    957 	do {
    958 		if (AscIsChipHalted(iot, ioh)) {
    959 			retval = 1;
    960 			break;
    961 		}
    962 		DvcSleepMilliSecond(100);
    963 	} while (count++ < 20);
    964 
    965 	AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B, saved_stop_code);
    966 
    967 	return (retval);
    968 }
    969 
    970 
    971 static int
    972 AscIsChipHalted(iot, ioh)
    973 	bus_space_tag_t iot;
    974 	bus_space_handle_t ioh;
    975 {
    976 	if ((ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_HALTED) != 0)
    977 		if ((ASC_GET_CHIP_CONTROL(iot, ioh) & ASC_CC_HALT) != 0)
    978 			return (1);
    979 
    980 	return (0);
    981 }
    982 
    983 
    984 static void
    985 AscSetChipIH(iot, ioh, ins_code)
    986 	bus_space_tag_t iot;
    987 	bus_space_handle_t ioh;
    988 	u_int16_t       ins_code;
    989 {
    990 	AscSetBank(iot, ioh, 1);
    991 	ASC_WRITE_CHIP_IH(iot, ioh, ins_code);
    992 	AscSetBank(iot, ioh, 0);
    993 
    994 	return;
    995 }
    996 
    997 
    998 /******************************************************************************/
    999 /*                                 Lram routines                              */
   1000 /******************************************************************************/
   1001 
   1002 
   1003 static u_int8_t
   1004 AscReadLramByte(iot, ioh, addr)
   1005 	bus_space_tag_t iot;
   1006 	bus_space_handle_t ioh;
   1007 	u_int16_t       addr;
   1008 {
   1009 	u_int8_t        byte_data;
   1010 	u_int16_t       word_data;
   1011 
   1012 
   1013 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr & 0xFFFE);
   1014 	word_data = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   1015 
   1016 	if (addr & 1) {
   1017 		/* odd address */
   1018 		byte_data = (u_int8_t) ((word_data >> 8) & 0xFF);
   1019 	} else {
   1020 		/* even address */
   1021 		byte_data = (u_int8_t) (word_data & 0xFF);
   1022 	}
   1023 
   1024 	return (byte_data);
   1025 }
   1026 
   1027 
   1028 static void
   1029 AscWriteLramByte(iot, ioh, addr, data)
   1030 	bus_space_tag_t iot;
   1031 	bus_space_handle_t ioh;
   1032 	u_int16_t       addr;
   1033 	u_int8_t        data;
   1034 {
   1035 	u_int16_t       word_data;
   1036 
   1037 
   1038 	word_data = AscReadLramWord(iot, ioh, addr & 0xFFFE);
   1039 
   1040 	if (addr & 1) {
   1041 		/* odd address */
   1042 		word_data &= 0x00FF;
   1043 		word_data |= (((u_int16_t) data) << 8) & 0xFF00;
   1044 	} else {
   1045 		/* even address */
   1046 		word_data &= 0xFF00;
   1047 		word_data |= ((u_int16_t) data) & 0x00FF;
   1048 	}
   1049 
   1050 	AscWriteLramWord(iot, ioh, addr, word_data);
   1051 }
   1052 
   1053 
   1054 static u_int16_t
   1055 AscReadLramWord(iot, ioh, addr)
   1056 	bus_space_tag_t iot;
   1057 	bus_space_handle_t ioh;
   1058 	u_int16_t       addr;
   1059 {
   1060 
   1061 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr);
   1062 	return (ASC_GET_CHIP_LRAM_DATA(iot, ioh));
   1063 }
   1064 
   1065 
   1066 static void
   1067 AscWriteLramWord(iot, ioh, addr, data)
   1068 	bus_space_tag_t iot;
   1069 	bus_space_handle_t ioh;
   1070 	u_int16_t       addr;
   1071 	u_int16_t       data;
   1072 {
   1073 
   1074 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr);
   1075 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, data);
   1076 }
   1077 
   1078 
   1079 static u_int32_t
   1080 AscReadLramDWord(iot, ioh, addr)
   1081 	bus_space_tag_t iot;
   1082 	bus_space_handle_t ioh;
   1083 	u_int16_t       addr;
   1084 {
   1085 	u_int16_t       low_word, hi_word;
   1086 
   1087 
   1088 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr);
   1089 	low_word = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   1090 	hi_word = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   1091 
   1092 	return ((((u_int32_t) hi_word) << 16) | (u_int32_t) low_word);
   1093 }
   1094 
   1095 
   1096 static void
   1097 AscWriteLramDWord(iot, ioh, addr, data)
   1098 	bus_space_tag_t iot;
   1099 	bus_space_handle_t ioh;
   1100 	u_int16_t       addr;
   1101 	u_int32_t       data;
   1102 {
   1103 
   1104 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr);
   1105 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, (u_int16_t) (data & 0x0000FFFF));
   1106 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, (u_int16_t) (data >> 16));
   1107 }
   1108 
   1109 
   1110 static void
   1111 AscMemWordSetLram(iot, ioh, s_addr, s_words, count)
   1112 	bus_space_tag_t iot;
   1113 	bus_space_handle_t ioh;
   1114 	u_int16_t       s_addr;
   1115 	u_int16_t       s_words;
   1116 	int             count;
   1117 {
   1118 	int             i;
   1119 
   1120 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, s_addr);
   1121 	for (i = 0; i < count; i++)
   1122 		ASC_SET_CHIP_LRAM_DATA(iot, ioh, s_words);
   1123 }
   1124 
   1125 
   1126 static void
   1127 AscMemWordCopyToLram(iot, ioh, s_addr, s_buffer, words)
   1128 	bus_space_tag_t iot;
   1129 	bus_space_handle_t ioh;
   1130 	u_int16_t       s_addr;
   1131 	u_int16_t      *s_buffer;
   1132 	int             words;
   1133 {
   1134 	int             i;
   1135 
   1136 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, s_addr);
   1137 	for (i = 0; i < words; i++, s_buffer++)
   1138 		ASC_SET_CHIP_LRAM_DATA_NO_SWAP(iot, ioh, *s_buffer);
   1139 }
   1140 
   1141 
   1142 static void
   1143 AscMemWordCopyFromLram(iot, ioh, s_addr, s_buffer, words)
   1144 	bus_space_tag_t iot;
   1145 	bus_space_handle_t ioh;
   1146 	u_int16_t       s_addr;
   1147 	u_int16_t      *s_buffer;
   1148 	int             words;
   1149 {
   1150 	int             i;
   1151 
   1152 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, s_addr);
   1153 	for (i = 0; i < words; i++, s_buffer++)
   1154 		*s_buffer = ASC_GET_CHIP_LRAM_DATA_NO_SWAP(iot, ioh);
   1155 }
   1156 
   1157 
   1158 static void
   1159 AscMemDWordCopyToLram(iot, ioh, s_addr, s_buffer, dwords)
   1160 	bus_space_tag_t iot;
   1161 	bus_space_handle_t ioh;
   1162 	u_int16_t       s_addr;
   1163 	u_int32_t      *s_buffer;
   1164 	int             dwords;
   1165 {
   1166 	int             i;
   1167 	u_int32_t      *pw;
   1168 
   1169 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, s_addr);
   1170 
   1171 	pw = s_buffer;
   1172 	for (i = 0; i < dwords; i++, pw++) {
   1173 		ASC_SET_CHIP_LRAM_DATA(iot, ioh, LO_WORD(*pw));
   1174 		DELAY(1);
   1175 		ASC_SET_CHIP_LRAM_DATA(iot, ioh, HI_WORD(*pw));
   1176 	}
   1177 }
   1178 
   1179 
   1180 static u_int32_t
   1181 AscMemSumLramWord(iot, ioh, s_addr, words)
   1182 	bus_space_tag_t iot;
   1183 	bus_space_handle_t ioh;
   1184 	u_int16_t       s_addr;
   1185 	int             words;
   1186 {
   1187 	u_int32_t       sum = 0L;
   1188 	u_int16_t       i;
   1189 
   1190 
   1191 	for (i = 0; i < words; i++, s_addr += 2)
   1192 		sum += AscReadLramWord(iot, ioh, s_addr);
   1193 
   1194 	return (sum);
   1195 }
   1196 
   1197 
   1198 static int
   1199 AscTestExternalLram(iot, ioh)
   1200 	bus_space_tag_t iot;
   1201 	bus_space_handle_t ioh;
   1202 {
   1203 	u_int16_t       q_addr;
   1204 	u_int16_t       saved_word;
   1205 	int             retval;
   1206 
   1207 
   1208 	retval = 0;
   1209 	q_addr = ASC_QNO_TO_QADDR(241);
   1210 	saved_word = AscReadLramWord(iot, ioh, q_addr);
   1211 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, q_addr);
   1212 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, 0x55AA);
   1213 	DvcSleepMilliSecond(10);
   1214 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, q_addr);
   1215 
   1216 	if (ASC_GET_CHIP_LRAM_DATA(iot, ioh) == 0x55AA) {
   1217 		retval = 1;
   1218 		AscWriteLramWord(iot, ioh, q_addr, saved_word);
   1219 	}
   1220 	return (retval);
   1221 }
   1222 
   1223 
   1224 /******************************************************************************/
   1225 /*                               MicroCode routines                           */
   1226 /******************************************************************************/
   1227 
   1228 
   1229 static u_int16_t
   1230 AscInitMicroCodeVar(sc)
   1231 	ASC_SOFTC      *sc;
   1232 {
   1233 	bus_space_tag_t iot = sc->sc_iot;
   1234 	bus_space_handle_t ioh = sc->sc_ioh;
   1235 	u_int32_t       phy_addr;
   1236 	int             i;
   1237 
   1238 
   1239 	for (i = 0; i <= ASC_MAX_TID; i++)
   1240 		ASC_PUT_MCODE_INIT_SDTR_AT_ID(iot, ioh, i,
   1241 					      sc->sdtr_period_offset[i]);
   1242 
   1243 	AscInitQLinkVar(sc);
   1244 	AscWriteLramByte(iot, ioh, ASCV_DISC_ENABLE_B, sc->disc_enable);
   1245 	AscWriteLramByte(iot, ioh, ASCV_HOSTSCSI_ID_B,
   1246 			 ASC_TID_TO_TARGET_ID(sc->chip_scsi_id));
   1247 
   1248 	if ((phy_addr = AscGetOnePhyAddr(sc, sc->overrun_buf,
   1249 					 ASC_OVERRUN_BSIZE)) == 0L) {
   1250 		return (0);
   1251 	} else {
   1252 		phy_addr = (phy_addr & 0xFFFFFFF8ul) + 8;
   1253 		AscWriteLramDWord(iot, ioh, ASCV_OVERRUN_PADDR_D, phy_addr);
   1254 		AscWriteLramDWord(iot, ioh, ASCV_OVERRUN_BSIZE_D,
   1255 				  ASC_OVERRUN_BSIZE - 8);
   1256 	}
   1257 
   1258 	sc->mcode_date = AscReadLramWord(iot, ioh, ASCV_MC_DATE_W);
   1259 	sc->mcode_version = AscReadLramWord(iot, ioh, ASCV_MC_VER_W);
   1260 	ASC_SET_PC_ADDR(iot, ioh, ASC_MCODE_START_ADDR);
   1261 
   1262 	if (ASC_GET_PC_ADDR(iot, ioh) != ASC_MCODE_START_ADDR) {
   1263 		return (0);
   1264 	}
   1265 	if (AscStartChip(iot, ioh) != 1) {
   1266 		return (0);
   1267 	}
   1268 	return (1);
   1269 }
   1270 
   1271 
   1272 static u_int32_t
   1273 AscLoadMicroCode(iot, ioh, s_addr, mcode_buf, mcode_size)
   1274 	bus_space_tag_t iot;
   1275 	bus_space_handle_t ioh;
   1276 	u_int16_t       s_addr;
   1277 	u_int16_t      *mcode_buf;
   1278 	u_int16_t       mcode_size;
   1279 {
   1280 	u_int32_t       chksum;
   1281 	u_int16_t       mcode_word_size;
   1282 	u_int16_t       mcode_chksum;
   1283 
   1284 	mcode_word_size = mcode_size >> 1;
   1285 	/* clear board memory */
   1286 	AscMemWordSetLram(iot, ioh, s_addr, 0, mcode_word_size);
   1287 	/* copy uCode to board memory */
   1288 	AscMemWordCopyToLram(iot, ioh, s_addr, mcode_buf, mcode_word_size);
   1289 	chksum = AscMemSumLramWord(iot, ioh, s_addr, mcode_word_size);
   1290 	mcode_chksum = AscMemSumLramWord(iot, ioh, ASC_CODE_SEC_BEG,
   1291 			   ((mcode_size - s_addr - ASC_CODE_SEC_BEG) >> 1));
   1292 	AscWriteLramWord(iot, ioh, ASCV_MCODE_CHKSUM_W, mcode_chksum);
   1293 	AscWriteLramWord(iot, ioh, ASCV_MCODE_SIZE_W, mcode_size);
   1294 
   1295 	return (chksum);
   1296 }
   1297 
   1298 
   1299 static u_int32_t
   1300 AscGetOnePhyAddr(sc, buf_addr, buf_size)
   1301 	ASC_SOFTC      *sc;
   1302 	u_int8_t       *buf_addr;
   1303 	u_int32_t       buf_size;
   1304 {
   1305 	ASC_MIN_SG_HEAD sg_head;
   1306 
   1307 	sg_head.entry_cnt = ASC_MIN_SG_LIST;
   1308 	if (AscGetSGList(sc, buf_addr, buf_size, (ASC_SG_HEAD *) & sg_head) !=
   1309 	    buf_size) {
   1310 		return (0L);
   1311 	}
   1312 	if (sg_head.entry_cnt > 1) {
   1313 		return (0L);
   1314 	}
   1315 	return (sg_head.sg_list[0].addr);
   1316 }
   1317 
   1318 
   1319 static u_int32_t
   1320 AscGetSGList(sc, buf_addr, buf_len, asc_sg_head_ptr)
   1321 	ASC_SOFTC      *sc;
   1322 	u_int8_t       *buf_addr;
   1323 	u_int32_t       buf_len;
   1324 	ASC_SG_HEAD    *asc_sg_head_ptr;
   1325 {
   1326 	u_int32_t       buf_size;
   1327 
   1328 	buf_size = buf_len;
   1329 	asc_sg_head_ptr->entry_cnt = 1;
   1330 	asc_sg_head_ptr->sg_list[0].addr = (u_int32_t) buf_addr;
   1331 	asc_sg_head_ptr->sg_list[0].bytes = buf_size;
   1332 
   1333 	return (buf_size);
   1334 }
   1335 
   1336 
   1337 /******************************************************************************/
   1338 /*                                 EEProm routines                            */
   1339 /******************************************************************************/
   1340 
   1341 
   1342 static int
   1343 AscWriteEEPCmdReg(iot, ioh, cmd_reg)
   1344 	bus_space_tag_t iot;
   1345 	bus_space_handle_t ioh;
   1346 	u_int8_t        cmd_reg;
   1347 {
   1348 	u_int8_t        read_back;
   1349 	int             retry;
   1350 
   1351 	retry = 0;
   1352 
   1353 	while (TRUE) {
   1354 		ASC_SET_CHIP_EEP_CMD(iot, ioh, cmd_reg);
   1355 		DvcSleepMilliSecond(1);
   1356 		read_back = ASC_GET_CHIP_EEP_CMD(iot, ioh);
   1357 		if (read_back == cmd_reg)
   1358 			return (1);
   1359 
   1360 		if (retry++ > ASC_EEP_MAX_RETRY)
   1361 			return (0);
   1362 	}
   1363 }
   1364 
   1365 
   1366 static int
   1367 AscWriteEEPDataReg(iot, ioh, data_reg)
   1368 	bus_space_tag_t iot;
   1369 	bus_space_handle_t ioh;
   1370 	u_int16_t       data_reg;
   1371 {
   1372 	u_int16_t       read_back;
   1373 	int             retry;
   1374 
   1375 	retry = 0;
   1376 	while (TRUE) {
   1377 		ASC_SET_CHIP_EEP_DATA(iot, ioh, data_reg);
   1378 		DvcSleepMilliSecond(1);
   1379 		read_back = ASC_GET_CHIP_EEP_DATA(iot, ioh);
   1380 		if (read_back == data_reg)
   1381 			return (1);
   1382 
   1383 		if (retry++ > ASC_EEP_MAX_RETRY)
   1384 			return (0);
   1385 	}
   1386 }
   1387 
   1388 
   1389 static void
   1390 AscWaitEEPRead(void)
   1391 {
   1392 
   1393 	DvcSleepMilliSecond(1);
   1394 }
   1395 
   1396 
   1397 static void
   1398 AscWaitEEPWrite(void)
   1399 {
   1400 
   1401 	DvcSleepMilliSecond(1);
   1402 }
   1403 
   1404 
   1405 static u_int16_t
   1406 AscReadEEPWord(iot, ioh, addr)
   1407 	bus_space_tag_t iot;
   1408 	bus_space_handle_t ioh;
   1409 	u_int8_t        addr;
   1410 {
   1411 	u_int16_t       read_wval;
   1412 	u_int8_t        cmd_reg;
   1413 
   1414 	AscWriteEEPCmdReg(iot, ioh, ASC_EEP_CMD_WRITE_DISABLE);
   1415 	AscWaitEEPRead();
   1416 	cmd_reg = addr | ASC_EEP_CMD_READ;
   1417 	AscWriteEEPCmdReg(iot, ioh, cmd_reg);
   1418 	AscWaitEEPRead();
   1419 	read_wval = ASC_GET_CHIP_EEP_DATA(iot, ioh);
   1420 	AscWaitEEPRead();
   1421 
   1422 	return (read_wval);
   1423 }
   1424 
   1425 
   1426 static u_int16_t
   1427 AscWriteEEPWord(iot, ioh, addr, word_val)
   1428 	bus_space_tag_t iot;
   1429 	bus_space_handle_t ioh;
   1430 	u_int8_t        addr;
   1431 	u_int16_t       word_val;
   1432 {
   1433 	u_int16_t       read_wval;
   1434 
   1435 	read_wval = AscReadEEPWord(iot, ioh, addr);
   1436 	if (read_wval != word_val) {
   1437 		AscWriteEEPCmdReg(iot, ioh, ASC_EEP_CMD_WRITE_ABLE);
   1438 		AscWaitEEPRead();
   1439 		AscWriteEEPDataReg(iot, ioh, word_val);
   1440 		AscWaitEEPRead();
   1441 		AscWriteEEPCmdReg(iot, ioh, ASC_EEP_CMD_WRITE | addr);
   1442 		AscWaitEEPWrite();
   1443 		AscWriteEEPCmdReg(iot, ioh, ASC_EEP_CMD_WRITE_DISABLE);
   1444 		AscWaitEEPRead();
   1445 		return (AscReadEEPWord(iot, ioh, addr));
   1446 	}
   1447 	return (read_wval);
   1448 }
   1449 
   1450 
   1451 static u_int16_t
   1452 AscGetEEPConfig(iot, ioh, cfg_buf, bus_type)
   1453 	bus_space_tag_t iot;
   1454 	bus_space_handle_t ioh;
   1455 	ASCEEP_CONFIG  *cfg_buf;
   1456 	u_int16_t       bus_type;
   1457 {
   1458 	u_int16_t       wval;
   1459 	u_int16_t       sum;
   1460 	u_int16_t      *wbuf;
   1461 	int             cfg_beg;
   1462 	int             cfg_end;
   1463 	int             s_addr;
   1464 	int             isa_pnp_wsize;
   1465 
   1466 
   1467 	wbuf = (u_int16_t *) cfg_buf;
   1468 	sum = 0;
   1469 	isa_pnp_wsize = 0;
   1470 
   1471 	for (s_addr = 0; s_addr < (2 + isa_pnp_wsize); s_addr++, wbuf++) {
   1472 		wval = AscReadEEPWord(iot, ioh, s_addr);
   1473 		sum += wval;
   1474 		*wbuf = wval;
   1475 	}
   1476 
   1477 	if (bus_type & ASC_IS_VL) {
   1478 		cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
   1479 		cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
   1480 	} else {
   1481 		cfg_beg = ASC_EEP_DVC_CFG_BEG;
   1482 		cfg_end = ASC_EEP_MAX_DVC_ADDR;
   1483 	}
   1484 
   1485 	for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
   1486 		wval = AscReadEEPWord(iot, ioh, s_addr);
   1487 		sum += wval;
   1488 		*wbuf = wval;
   1489 	}
   1490 
   1491 	*wbuf = AscReadEEPWord(iot, ioh, s_addr);
   1492 
   1493 	return (sum);
   1494 }
   1495 
   1496 
   1497 static int
   1498 AscSetEEPConfig(iot, ioh, cfg_buf, bus_type)
   1499 	bus_space_tag_t iot;
   1500 	bus_space_handle_t ioh;
   1501 	ASCEEP_CONFIG  *cfg_buf;
   1502 	u_int16_t       bus_type;
   1503 {
   1504 	int             retry;
   1505 	int             n_error;
   1506 
   1507 	retry = 0;
   1508 	while (TRUE) {
   1509 		if ((n_error = AscSetEEPConfigOnce(iot, ioh, cfg_buf, bus_type)) == 0)
   1510 			break;
   1511 
   1512 		if (++retry > ASC_EEP_MAX_RETRY)
   1513 			break;
   1514 	}
   1515 
   1516 	return (n_error);
   1517 }
   1518 
   1519 
   1520 static int
   1521 AscSetEEPConfigOnce(iot, ioh, cfg_buf, bus_type)
   1522 	bus_space_tag_t iot;
   1523 	bus_space_handle_t ioh;
   1524 	ASCEEP_CONFIG  *cfg_buf;
   1525 	u_int16_t       bus_type;
   1526 {
   1527 	int             n_error;
   1528 	u_int16_t      *wbuf;
   1529 	u_int16_t       sum;
   1530 	int             s_addr;
   1531 	int             cfg_beg;
   1532 	int             cfg_end;
   1533 
   1534 	wbuf = (u_int16_t *) cfg_buf;
   1535 	n_error = 0;
   1536 	sum = 0;
   1537 
   1538 	for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
   1539 		sum += *wbuf;
   1540 		if (*wbuf != AscWriteEEPWord(iot, ioh, s_addr, *wbuf))
   1541 			n_error++;
   1542 	}
   1543 
   1544 	if (bus_type & ASC_IS_VL) {
   1545 		cfg_beg = ASC_EEP_DVC_CFG_BEG_VL;
   1546 		cfg_end = ASC_EEP_MAX_DVC_ADDR_VL;
   1547 	} else {
   1548 		cfg_beg = ASC_EEP_DVC_CFG_BEG;
   1549 		cfg_end = ASC_EEP_MAX_DVC_ADDR;
   1550 	}
   1551 
   1552 	for (s_addr = cfg_beg; s_addr <= (cfg_end - 1); s_addr++, wbuf++) {
   1553 		sum += *wbuf;
   1554 		if (*wbuf != AscWriteEEPWord(iot, ioh, s_addr, *wbuf))
   1555 			n_error++;
   1556 	}
   1557 
   1558 	*wbuf = sum;
   1559 	if (sum != AscWriteEEPWord(iot, ioh, s_addr, sum))
   1560 		n_error++;
   1561 
   1562 	wbuf = (u_int16_t *) cfg_buf;
   1563 	for (s_addr = 0; s_addr < 2; s_addr++, wbuf++) {
   1564 		if (*wbuf != AscReadEEPWord(iot, ioh, s_addr))
   1565 			n_error++;
   1566 	}
   1567 
   1568 	for (s_addr = cfg_beg; s_addr <= cfg_end; s_addr++, wbuf++) {
   1569 		if (*wbuf != AscReadEEPWord(iot, ioh, s_addr))
   1570 			n_error++;
   1571 	}
   1572 
   1573 	return (n_error);
   1574 }
   1575 
   1576 
   1577 #ifdef ASC_DEBUG
   1578 static void
   1579 AscPrintEEPConfig(eep_config, chksum)
   1580 	ASCEEP_CONFIG	*eep_config;
   1581 	u_int16_t	chksum;
   1582 {
   1583 	printf("---- ASC EEprom settings ----\n");
   1584 	printf("cfg_lsw = 0x%x\n", eep_config->cfg_lsw);
   1585 	printf("cfg_msw = 0x%x\n", eep_config->cfg_msw);
   1586 	printf("init_sdtr = 0x%x\n", eep_config->init_sdtr);
   1587 	printf("disc_enable = 0x%x\n", eep_config->disc_enable);
   1588 	printf("use_cmd_qng = %d\n", eep_config->use_cmd_qng);
   1589 	printf("start_motor = 0x%x\n", eep_config->start_motor);
   1590 	printf("max_total_qng = 0x%x\n", eep_config->max_total_qng);
   1591 	printf("max_tag_qng = 0x%x\n", eep_config->max_tag_qng);
   1592 	printf("bios_scan = 0x%x\n", eep_config->bios_scan);
   1593 	printf("power_up_wait = 0x%x\n", eep_config->power_up_wait);
   1594 	printf("no_scam = %d\n", eep_config->no_scam);
   1595 	printf("chip_scsi_id = %d\n", eep_config->chip_scsi_id);
   1596 	printf("isa_dma_speed = %d\n", eep_config->isa_dma_speed);
   1597 	printf("cntl = 0x%x\n", eep_config->cntl);
   1598 #if BYTE_ORDER == BIG_ENDIAN
   1599 	printf("adapter_info[0] = 0x%x\n", eep_config->adapter_info[1]);
   1600 	printf("adapter_info[1] = 0x%x\n", eep_config->adapter_info[0]);
   1601 	printf("adapter_info[2] = 0x%x\n", eep_config->adapter_info[3]);
   1602 	printf("adapter_info[3] = 0x%x\n", eep_config->adapter_info[2]);
   1603 	printf("adapter_info[4] = 0x%x\n", eep_config->adapter_info[5]);
   1604 	printf("adapter_info[5] = 0x%x\n", eep_config->adapter_info[4]);
   1605 #else
   1606 	printf("adapter_info[0] = 0x%x\n", eep_config->adapter_info[0]);
   1607 	printf("adapter_info[1] = 0x%x\n", eep_config->adapter_info[1]);
   1608 	printf("adapter_info[2] = 0x%x\n", eep_config->adapter_info[2]);
   1609 	printf("adapter_info[3] = 0x%x\n", eep_config->adapter_info[3]);
   1610 	printf("adapter_info[4] = 0x%x\n", eep_config->adapter_info[4]);
   1611 	printf("adapter_info[5] = 0x%x\n", eep_config->adapter_info[5]);
   1612 #endif
   1613 	printf("checksum = 0x%x\n", eep_config->chksum);
   1614 	printf("calculated checksum = 0x%x\n", chksum);
   1615 	printf("-----------------------------\n");
   1616 }
   1617 #endif
   1618 
   1619 
   1620 /******************************************************************************/
   1621 /*                               Interrupt routines                           */
   1622 /******************************************************************************/
   1623 
   1624 
   1625 int
   1626 AscISR(sc)
   1627 	ASC_SOFTC      *sc;
   1628 {
   1629 	bus_space_tag_t iot = sc->sc_iot;
   1630 	bus_space_handle_t ioh = sc->sc_ioh;
   1631 	u_int16_t       chipstat;
   1632 	u_int16_t       saved_ram_addr;
   1633 	u_int8_t        ctrl_reg;
   1634 	u_int8_t        saved_ctrl_reg;
   1635 	int             int_pending;
   1636 	int             status;
   1637 	u_int8_t        host_flag;
   1638 
   1639 
   1640 	int_pending = FALSE;
   1641 
   1642 	ctrl_reg = ASC_GET_CHIP_CONTROL(iot, ioh);
   1643 	saved_ctrl_reg = ctrl_reg & (~(ASC_CC_SCSI_RESET | ASC_CC_CHIP_RESET |
   1644 			   ASC_CC_SINGLE_STEP | ASC_CC_DIAG | ASC_CC_TEST));
   1645 	chipstat = ASC_GET_CHIP_STATUS(iot, ioh);
   1646 	if (chipstat & ASC_CSW_SCSI_RESET_LATCH) {
   1647 		if (!(sc->bus_type & (ASC_IS_VL | ASC_IS_EISA))) {
   1648 			int_pending = TRUE;
   1649 			sc->sdtr_done = 0;
   1650 			saved_ctrl_reg &= (u_int8_t) (~ASC_CC_HALT);
   1651 
   1652 			while (ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_SCSI_RESET_ACTIVE);
   1653 
   1654 			ASC_SET_CHIP_CONTROL(iot, ioh, (ASC_CC_CHIP_RESET | ASC_CC_HALT));
   1655 			ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_HALT);
   1656 			ASC_SET_CHIP_STATUS(iot, ioh, ASC_CIW_CLR_SCSI_RESET_INT);
   1657 			ASC_SET_CHIP_STATUS(iot, ioh, 0);
   1658 			chipstat = ASC_GET_CHIP_STATUS(iot, ioh);
   1659 		}
   1660 	}
   1661 	saved_ram_addr = ASC_GET_CHIP_LRAM_ADDR(iot, ioh);
   1662 	host_flag = AscReadLramByte(iot, ioh, ASCV_HOST_FLAG_B) &
   1663 		(u_int8_t) (~ASC_HOST_FLAG_IN_ISR);
   1664 	AscWriteLramByte(iot, ioh, ASCV_HOST_FLAG_B,
   1665 			 (host_flag | ASC_HOST_FLAG_IN_ISR));
   1666 
   1667 	if ((chipstat & ASC_CSW_INT_PENDING) || (int_pending)) {
   1668 		AscAckInterrupt(iot, ioh);
   1669 		int_pending = TRUE;
   1670 
   1671 		if ((chipstat & ASC_CSW_HALTED) &&
   1672 		    (ctrl_reg & ASC_CC_SINGLE_STEP)) {
   1673 			AscIsrChipHalted(sc);
   1674 			saved_ctrl_reg &= ~ASC_CC_HALT;
   1675 		} else {
   1676 			if (sc->dvc_cntl & ASC_CNTL_INT_MULTI_Q) {
   1677 				while (((status = AscIsrQDone(sc)) & 0x01) != 0);
   1678 			} else {
   1679 				do {
   1680 					if ((status = AscIsrQDone(sc)) == 1)
   1681 						break;
   1682 				} while (status == 0x11);
   1683 			}
   1684 
   1685 			if (status & 0x80)
   1686 				int_pending = -1;
   1687 		}
   1688 	}
   1689 	AscWriteLramByte(iot, ioh, ASCV_HOST_FLAG_B, host_flag);
   1690 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, saved_ram_addr);
   1691 	ASC_SET_CHIP_CONTROL(iot, ioh, saved_ctrl_reg);
   1692 
   1693 	return (1);
   1694 	/* return(int_pending); */
   1695 }
   1696 
   1697 
   1698 static int
   1699 AscIsrQDone(sc)
   1700 	ASC_SOFTC      *sc;
   1701 {
   1702 	u_int8_t        next_qp;
   1703 	u_int8_t        n_q_used;
   1704 	u_int8_t        sg_list_qp;
   1705 	u_int8_t        sg_queue_cnt;
   1706 	u_int8_t        q_cnt;
   1707 	u_int8_t        done_q_tail;
   1708 	u_int8_t        tid_no;
   1709 	ASC_SCSI_BIT_ID_TYPE scsi_busy;
   1710 	ASC_SCSI_BIT_ID_TYPE target_id;
   1711 	bus_space_tag_t iot = sc->sc_iot;
   1712 	bus_space_handle_t ioh = sc->sc_ioh;
   1713 	u_int16_t       q_addr;
   1714 	u_int16_t       sg_q_addr;
   1715 	u_int8_t        cur_target_qng;
   1716 	ASC_QDONE_INFO  scsiq_buf;
   1717 	ASC_QDONE_INFO *scsiq;
   1718 	ASC_ISR_CALLBACK asc_isr_callback;
   1719 
   1720 
   1721 	asc_isr_callback = (ASC_ISR_CALLBACK) sc->isr_callback;
   1722 	n_q_used = 1;
   1723 	scsiq = (ASC_QDONE_INFO *) & scsiq_buf;
   1724 	done_q_tail = ASC_GET_VAR_DONE_QTAIL(iot, ioh);
   1725 	q_addr = ASC_QNO_TO_QADDR(done_q_tail);
   1726 	next_qp = AscReadLramByte(iot, ioh, (q_addr + ASC_SCSIQ_B_FWD));
   1727 
   1728 	if (next_qp != ASC_QLINK_END) {
   1729 		ASC_PUT_VAR_DONE_QTAIL(iot, ioh, next_qp);
   1730 		q_addr = ASC_QNO_TO_QADDR(next_qp);
   1731 		sg_queue_cnt = _AscCopyLramScsiDoneQ(iot, ioh, q_addr, scsiq,
   1732 						     sc->max_dma_count);
   1733 		AscWriteLramByte(iot, ioh, (q_addr + ASC_SCSIQ_B_STATUS),
   1734 		      (scsiq->q_status & ~(ASC_QS_READY | ASC_QS_ABORTED)));
   1735 		tid_no = ASC_TIX_TO_TID(scsiq->d2.target_ix);
   1736 		target_id = ASC_TIX_TO_TARGET_ID(scsiq->d2.target_ix);
   1737 		if ((scsiq->cntl & ASC_QC_SG_HEAD) != 0) {
   1738 			sg_q_addr = q_addr;
   1739 			sg_list_qp = next_qp;
   1740 			for (q_cnt = 0; q_cnt < sg_queue_cnt; q_cnt++) {
   1741 				sg_list_qp = AscReadLramByte(iot, ioh,
   1742 					       sg_q_addr + ASC_SCSIQ_B_FWD);
   1743 				sg_q_addr = ASC_QNO_TO_QADDR(sg_list_qp);
   1744 				if (sg_list_qp == ASC_QLINK_END) {
   1745 					AscSetLibErrorCode(sc, ASCQ_ERR_SG_Q_LINKS);
   1746 					scsiq->d3.done_stat = ASC_QD_WITH_ERROR;
   1747 					scsiq->d3.host_stat = ASC_QHSTA_D_QDONE_SG_LIST_CORRUPTED;
   1748 					panic("AscIsrQDone: Corrupted SG list encountered");
   1749 				}
   1750 				AscWriteLramByte(iot, ioh,
   1751 				sg_q_addr + ASC_SCSIQ_B_STATUS, ASC_QS_FREE);
   1752 			}
   1753 			n_q_used = sg_queue_cnt + 1;
   1754 			ASC_PUT_VAR_DONE_QTAIL(iot, ioh, sg_list_qp);
   1755 		}
   1756 		if (sc->queue_full_or_busy & target_id) {
   1757 			cur_target_qng = AscReadLramByte(iot, ioh,
   1758 					ASC_QADR_BEG + scsiq->d2.target_ix);
   1759 
   1760 			if (cur_target_qng < sc->max_dvc_qng[tid_no]) {
   1761 				scsi_busy = AscReadLramByte(iot, ioh, ASCV_SCSIBUSY_B);
   1762 				scsi_busy &= ~target_id;
   1763 				AscWriteLramByte(iot, ioh, ASCV_SCSIBUSY_B, scsi_busy);
   1764 				sc->queue_full_or_busy &= ~target_id;
   1765 			}
   1766 		}
   1767 		if (sc->cur_total_qng >= n_q_used) {
   1768 			sc->cur_total_qng -= n_q_used;
   1769 			if (sc->cur_dvc_qng[tid_no] != 0) {
   1770 				sc->cur_dvc_qng[tid_no]--;
   1771 			}
   1772 		} else {
   1773 			AscSetLibErrorCode(sc, ASCQ_ERR_CUR_QNG);
   1774 			scsiq->d3.done_stat = ASC_QD_WITH_ERROR;
   1775 			panic("AscIsrQDone: Attempting to free more queues than are active");
   1776 		}
   1777 
   1778 		if ((scsiq->d2.ccb_ptr == 0UL) || ((scsiq->q_status & ASC_QS_ABORTED) != 0)) {
   1779 			return (0x11);
   1780 		} else if (scsiq->q_status == ASC_QS_DONE) {
   1781 			scsiq->remain_bytes += scsiq->extra_bytes;
   1782 
   1783 			if (scsiq->d3.done_stat == ASC_QD_WITH_ERROR) {
   1784 				if (scsiq->d3.host_stat == ASC_QHSTA_M_DATA_OVER_RUN) {
   1785 					if ((scsiq->cntl & (ASC_QC_DATA_IN | ASC_QC_DATA_OUT)) == 0) {
   1786 						scsiq->d3.done_stat = ASC_QD_NO_ERROR;
   1787 						scsiq->d3.host_stat = ASC_QHSTA_NO_ERROR;
   1788 					}
   1789 				} else if (scsiq->d3.host_stat == ASC_QHSTA_M_HUNG_REQ_SCSI_BUS_RESET) {
   1790 					AscStopChip(iot, ioh);
   1791 					ASC_SET_CHIP_CONTROL(iot, ioh, (ASC_CC_SCSI_RESET | ASC_CC_HALT));
   1792 					DvcDelayNanoSecond(60000);
   1793 					ASC_SET_CHIP_CONTROL(iot, ioh, ASC_CC_HALT);
   1794 					ASC_SET_CHIP_STATUS(iot, ioh, ASC_CIW_CLR_SCSI_RESET_INT);
   1795 					ASC_SET_CHIP_STATUS(iot, ioh, 0);
   1796 					ASC_SET_CHIP_CONTROL(iot, ioh, 0);
   1797 				}
   1798 			}
   1799 			(*asc_isr_callback) (sc, scsiq);
   1800 
   1801 			return (1);
   1802 		} else {
   1803 			AscSetLibErrorCode(sc, ASCQ_ERR_Q_STATUS);
   1804 			panic("AscIsrQDone: completed scsiq with unknown status");
   1805 
   1806 			return (0x80);
   1807 		}
   1808 	}
   1809 	return (0);
   1810 }
   1811 
   1812 
   1813 /*
   1814  * handle all the conditions that may halt the board
   1815  * waiting us to intervene
   1816  */
   1817 static void
   1818 AscIsrChipHalted(sc)
   1819 	ASC_SOFTC      *sc;
   1820 {
   1821 	bus_space_tag_t iot = sc->sc_iot;
   1822 	bus_space_handle_t ioh = sc->sc_ioh;
   1823 	EXT_MSG         out_msg;
   1824 	u_int16_t       int_halt_code;
   1825 	u_int16_t       halt_q_addr;
   1826 	u_int8_t        halt_qp;
   1827 	u_int8_t        target_ix;
   1828 	u_int8_t        tag_code;
   1829 	u_int8_t        q_status;
   1830 	u_int8_t        q_cntl;
   1831 	u_int8_t        tid_no;
   1832 	u_int8_t        cur_dvc_qng;
   1833 	u_int8_t        asyn_sdtr;
   1834 	u_int8_t        scsi_status;
   1835 	u_int8_t        sdtr_data;
   1836 	ASC_SCSI_BIT_ID_TYPE scsi_busy;
   1837 	ASC_SCSI_BIT_ID_TYPE target_id;
   1838 
   1839 
   1840 	int_halt_code = AscReadLramWord(iot, ioh, ASCV_HALTCODE_W);
   1841 
   1842 	halt_qp = AscReadLramByte(iot, ioh, ASCV_CURCDB_B);
   1843 	halt_q_addr = ASC_QNO_TO_QADDR(halt_qp);
   1844 	target_ix = AscReadLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_TARGET_IX);
   1845 	q_cntl = AscReadLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_CNTL);
   1846 	tid_no = ASC_TIX_TO_TID(target_ix);
   1847 	target_id = ASC_TID_TO_TARGET_ID(tid_no);
   1848 
   1849 	if (sc->pci_fix_asyn_xfer & target_id) {
   1850 		asyn_sdtr = ASYN_SDTR_DATA_FIX_PCI_REV_AB;
   1851 	} else {
   1852 		asyn_sdtr = 0;
   1853 	}
   1854 
   1855 	if (int_halt_code == ASC_HALT_DISABLE_ASYN_USE_SYN_FIX) {
   1856 		if (sc->pci_fix_asyn_xfer & target_id) {
   1857 			AscSetChipSDTR(iot, ioh, 0, tid_no);
   1858 			sc->sdtr_data[tid_no] = 0;
   1859 		}
   1860 		AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
   1861 	} else if (int_halt_code == ASC_HALT_ENABLE_ASYN_USE_SYN_FIX) {
   1862 		if (sc->pci_fix_asyn_xfer & target_id) {
   1863 			AscSetChipSDTR(iot, ioh, asyn_sdtr, tid_no);
   1864 			sc->sdtr_data[tid_no] = asyn_sdtr;
   1865 		}
   1866 		AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
   1867 	} else if (int_halt_code == ASC_HALT_EXTMSG_IN) {
   1868 		AscHandleExtMsgIn(sc, halt_q_addr, q_cntl, target_id,
   1869 				  tid_no, asyn_sdtr);
   1870 		AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
   1871 	} else if (int_halt_code == ASC_HALT_CHK_CONDITION) {
   1872 		q_cntl |= ASC_QC_REQ_SENSE;
   1873 
   1874 		if (sc->init_sdtr & target_id) {
   1875 			sc->sdtr_done &= ~target_id;
   1876 
   1877 			sdtr_data = ASC_GET_MCODE_INIT_SDTR_AT_ID(iot, ioh, tid_no);
   1878 			q_cntl |= ASC_QC_MSG_OUT;
   1879 			AscMsgOutSDTR(sc, sc->sdtr_period_tbl[(sdtr_data >> 4) &
   1880 						  (sc->max_sdtr_index - 1)],
   1881 				      (sdtr_data & ASC_SYN_MAX_OFFSET));
   1882 		}
   1883 		AscWriteLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_CNTL, q_cntl);
   1884 
   1885 		tag_code = AscReadLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_TAG_CODE);
   1886 		tag_code &= 0xDC;
   1887 
   1888 		if ((sc->pci_fix_asyn_xfer & target_id) &&
   1889 		    !(sc->pci_fix_asyn_xfer_always & target_id)) {
   1890 			tag_code |= (ASC_TAG_FLAG_DISABLE_DISCONNECT |
   1891 				     ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX);
   1892 		}
   1893 		AscWriteLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_TAG_CODE, tag_code);
   1894 
   1895 		q_status = AscReadLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_STATUS);
   1896 		q_status |= ASC_QS_READY | ASC_QS_BUSY;
   1897 
   1898 		AscWriteLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_STATUS, q_status);
   1899 
   1900 		scsi_busy = AscReadLramByte(iot, ioh, ASCV_SCSIBUSY_B);
   1901 		scsi_busy &= ~target_id;
   1902 		AscWriteLramByte(iot, ioh, ASCV_SCSIBUSY_B, scsi_busy);
   1903 
   1904 		AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
   1905 	} else if (int_halt_code == ASC_HALT_SDTR_REJECTED) {
   1906 		AscMemWordCopyFromLram(iot, ioh, ASCV_MSGOUT_BEG,
   1907 			     (u_int16_t *) & out_msg, sizeof(EXT_MSG) >> 1);
   1908 
   1909 		if ((out_msg.msg_type == MS_EXTEND) &&
   1910 		    (out_msg.msg_len == MS_SDTR_LEN) &&
   1911 		    (out_msg.msg_req == MS_SDTR_CODE)) {
   1912 			sc->init_sdtr &= ~target_id;
   1913 			sc->sdtr_done &= ~target_id;
   1914 			AscSetChipSDTR(iot, ioh, asyn_sdtr, tid_no);
   1915 			sc->sdtr_data[tid_no] = asyn_sdtr;
   1916 		}
   1917 		q_cntl &= ~ASC_QC_MSG_OUT;
   1918 		AscWriteLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_CNTL, q_cntl);
   1919 		AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
   1920 	} else if (int_halt_code == ASC_HALT_SS_QUEUE_FULL) {
   1921 		scsi_status = AscReadLramByte(iot, ioh,
   1922 				       halt_q_addr + ASC_SCSIQ_SCSI_STATUS);
   1923 		cur_dvc_qng = AscReadLramByte(iot, ioh, target_ix + ASC_QADR_BEG);
   1924 
   1925 		if ((cur_dvc_qng > 0) && (sc->cur_dvc_qng[tid_no] > 0)) {
   1926 			scsi_busy = AscReadLramByte(iot, ioh, ASCV_SCSIBUSY_B);
   1927 			scsi_busy |= target_id;
   1928 			AscWriteLramByte(iot, ioh, ASCV_SCSIBUSY_B, scsi_busy);
   1929 			sc->queue_full_or_busy |= target_id;
   1930 
   1931 			if (scsi_status == SS_QUEUE_FULL) {
   1932 				if (cur_dvc_qng > ASC_MIN_TAGGED_CMD) {
   1933 					cur_dvc_qng -= 1;
   1934 					sc->max_dvc_qng[tid_no] = cur_dvc_qng;
   1935 
   1936 					AscWriteLramByte(iot, ioh,
   1937 							 tid_no + ASCV_MAX_DVC_QNG_BEG, cur_dvc_qng);
   1938 
   1939 #if ASC_QUEUE_FLOW_CONTROL
   1940 					if ((sc->device[tid_no] != NULL) &&
   1941 					    (sc->device[tid_no]->queue_curr_depth > cur_dvc_qng)) {
   1942 						sc->device[tid_no]->queue_curr_depth = cur_dvc_qng;
   1943 					}
   1944 #endif				/* ASC_QUEUE_FLOW_CONTROL */
   1945 				}
   1946 			}
   1947 		}
   1948 		AscWriteLramWord(iot, ioh, ASCV_HALTCODE_W, 0);
   1949 	}
   1950 	return;
   1951 }
   1952 
   1953 
   1954 static int
   1955 AscWaitTixISRDone(sc, target_ix)
   1956 	ASC_SOFTC      *sc;
   1957 	u_int8_t        target_ix;
   1958 {
   1959 	u_int8_t        cur_req;
   1960 	u_int8_t        tid_no;
   1961 	int             i = 0;
   1962 
   1963 	tid_no = ASC_TIX_TO_TID(target_ix);
   1964 	while (i++ < 10) {
   1965 		if ((cur_req = sc->cur_dvc_qng[tid_no]) == 0)
   1966 			break;
   1967 
   1968 		DvcSleepMilliSecond(1000L);
   1969 		if (sc->cur_dvc_qng[tid_no] == cur_req)
   1970 			break;
   1971 	}
   1972 	return (1);
   1973 }
   1974 
   1975 static int
   1976 AscWaitISRDone(sc)
   1977 	ASC_SOFTC      *sc;
   1978 {
   1979 	int             tid;
   1980 
   1981 	for (tid = 0; tid <= ASC_MAX_TID; tid++)
   1982 		AscWaitTixISRDone(sc, ASC_TID_TO_TIX(tid));
   1983 
   1984 	return (1);
   1985 }
   1986 
   1987 
   1988 static u_int8_t
   1989 _AscCopyLramScsiDoneQ(iot, ioh, q_addr, scsiq, max_dma_count)
   1990 	bus_space_tag_t iot;
   1991 	bus_space_handle_t ioh;
   1992 	u_int16_t       q_addr;
   1993 	ASC_QDONE_INFO *scsiq;
   1994 	u_int32_t       max_dma_count;
   1995 {
   1996 	u_int16_t       _val;
   1997 	u_int8_t        sg_queue_cnt;
   1998 
   1999 	AscGetQDoneInfo(iot, ioh, q_addr + ASC_SCSIQ_DONE_INFO_BEG, scsiq);
   2000 
   2001 	_val = AscReadLramWord(iot, ioh, q_addr + ASC_SCSIQ_B_STATUS);
   2002 	scsiq->q_status = LO_BYTE(_val);
   2003 	scsiq->q_no = HI_BYTE(_val);
   2004 	_val = AscReadLramWord(iot, ioh, q_addr + ASC_SCSIQ_B_CNTL);
   2005 	scsiq->cntl = LO_BYTE(_val);
   2006 	sg_queue_cnt = HI_BYTE(_val);
   2007 	_val = AscReadLramWord(iot, ioh, q_addr + ASC_SCSIQ_B_SENSE_LEN);
   2008 	scsiq->sense_len = LO_BYTE(_val);
   2009 	scsiq->extra_bytes = HI_BYTE(_val);
   2010 	scsiq->remain_bytes = AscReadLramWord(iot, ioh,
   2011 				     q_addr + ASC_SCSIQ_DW_REMAIN_XFER_CNT);
   2012 	scsiq->remain_bytes &= max_dma_count;
   2013 
   2014 	return (sg_queue_cnt);
   2015 }
   2016 
   2017 
   2018 static void
   2019 AscGetQDoneInfo(iot, ioh, addr, scsiq)
   2020 	bus_space_tag_t iot;
   2021 	bus_space_handle_t ioh;
   2022 	u_int16_t	addr;
   2023 	ASC_QDONE_INFO	*scsiq;
   2024 {
   2025 	u_int16_t	val;
   2026 
   2027 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr);
   2028 
   2029 	val = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   2030 	scsiq->d2.ccb_ptr = MAKELONG(val, ASC_GET_CHIP_LRAM_DATA(iot, ioh));
   2031 	val = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   2032 	scsiq->d2.target_ix = LO_BYTE(val);
   2033 	scsiq->d2.flag = HI_BYTE(val);
   2034 	val = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   2035 	scsiq->d2.cdb_len = LO_BYTE(val);
   2036 	scsiq->d2.tag_code = HI_BYTE(val);
   2037 	scsiq->d2.vm_id = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   2038 
   2039 	val = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   2040 	scsiq->d3.done_stat = LO_BYTE(val);
   2041 	scsiq->d3.host_stat = HI_BYTE(val);
   2042 	val = ASC_GET_CHIP_LRAM_DATA(iot, ioh);
   2043 	scsiq->d3.scsi_stat = LO_BYTE(val);
   2044 	scsiq->d3.scsi_msg = HI_BYTE(val);
   2045 }
   2046 
   2047 
   2048 static void
   2049 AscToggleIRQAct(iot, ioh)
   2050 	bus_space_tag_t iot;
   2051 	bus_space_handle_t ioh;
   2052 {
   2053 
   2054 	ASC_SET_CHIP_STATUS(iot, ioh, ASC_CIW_IRQ_ACT);
   2055 	ASC_SET_CHIP_STATUS(iot, ioh, 0);
   2056 }
   2057 
   2058 
   2059 static void
   2060 AscDisableInterrupt(iot, ioh)
   2061 	bus_space_tag_t iot;
   2062 	bus_space_handle_t ioh;
   2063 {
   2064 	u_int16_t       cfg;
   2065 
   2066 	cfg = ASC_GET_CHIP_CFG_LSW(iot, ioh);
   2067 	ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg & (~ASC_CFG0_HOST_INT_ON));
   2068 }
   2069 
   2070 
   2071 static void
   2072 AscEnableInterrupt(iot, ioh)
   2073 	bus_space_tag_t iot;
   2074 	bus_space_handle_t ioh;
   2075 {
   2076 	u_int16_t       cfg;
   2077 
   2078 	cfg = ASC_GET_CHIP_CFG_LSW(iot, ioh);
   2079 	ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg | ASC_CFG0_HOST_INT_ON);
   2080 }
   2081 
   2082 
   2083 static u_int8_t
   2084 AscGetChipIRQ(iot, ioh, bus_type)
   2085 	bus_space_tag_t iot;
   2086 	bus_space_handle_t ioh;
   2087 	u_int16_t       bus_type;
   2088 {
   2089 	u_int16_t       cfg_lsw;
   2090 	u_int8_t        chip_irq;
   2091 
   2092 
   2093 	if (bus_type & ASC_IS_EISA) {
   2094 		/*
   2095 		 * cfg_lsw = AscGetEisaChipCfg(iot, ioh); chip_irq =
   2096 		 * ((cfg_lsw >> 8) & 0x07) + 10; if((chip_irq == 13) ||
   2097 		 * (chip_irq > 15)) return (0); return(chip_irq);
   2098 		 */
   2099 	}
   2100 	if ((bus_type & ASC_IS_VL) != 0) {
   2101 		cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh);
   2102 		chip_irq = (cfg_lsw >> 2) & 0x07;
   2103 		if ((chip_irq == 0) ||
   2104 		    (chip_irq == 4) ||
   2105 		    (chip_irq == 7)) {
   2106 			return (0);
   2107 		}
   2108 		return (chip_irq + (ASC_MIN_IRQ_NO - 1));
   2109 	}
   2110 	cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh);
   2111 	chip_irq = (cfg_lsw >> 2) & 0x03;
   2112 	if (chip_irq == 3)
   2113 		chip_irq += 2;
   2114 	return (chip_irq + ASC_MIN_IRQ_NO);
   2115 }
   2116 
   2117 
   2118 static u_int8_t
   2119 AscSetChipIRQ(iot, ioh, irq_no, bus_type)
   2120 	bus_space_tag_t iot;
   2121 	bus_space_handle_t ioh;
   2122 	u_int8_t        irq_no;
   2123 	u_int16_t       bus_type;
   2124 {
   2125 	u_int16_t       cfg_lsw;
   2126 
   2127 
   2128 	if (bus_type & ASC_IS_VL) {
   2129 		if (irq_no) {
   2130 			if ((irq_no < ASC_MIN_IRQ_NO) || (irq_no > ASC_MAX_IRQ_NO))
   2131 				irq_no = 0;
   2132 			else
   2133 				irq_no -= ASC_MIN_IRQ_NO - 1;
   2134 		}
   2135 
   2136 		cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh) & 0xFFE3;
   2137 		cfg_lsw |= 0x0010;
   2138 		ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg_lsw);
   2139 		AscToggleIRQAct(iot, ioh);
   2140 		cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh) & 0xFFE0;
   2141 		cfg_lsw |= (irq_no & 0x07) << 2;
   2142 		ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg_lsw);
   2143 		AscToggleIRQAct(iot, ioh);
   2144 
   2145 		return (AscGetChipIRQ(iot, ioh, bus_type));
   2146 	}
   2147 	if (bus_type & ASC_IS_ISA) {
   2148 		if (irq_no == 15)
   2149 			irq_no -= 2;
   2150 		irq_no -= ASC_MIN_IRQ_NO;
   2151 		cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh) & 0xFFF3;
   2152 		cfg_lsw |= (irq_no & 0x03) << 2;
   2153 		ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg_lsw);
   2154 
   2155 		return (AscGetChipIRQ(iot, ioh, bus_type));
   2156 	}
   2157 	return (0);
   2158 }
   2159 
   2160 
   2161 static void
   2162 AscAckInterrupt(iot, ioh)
   2163 	bus_space_tag_t iot;
   2164 	bus_space_handle_t ioh;
   2165 {
   2166 	u_int8_t        host_flag;
   2167 	u_int8_t        risc_flag;
   2168 	u_int16_t       loop;
   2169 
   2170 
   2171 	loop = 0;
   2172 	do {
   2173 		risc_flag = AscReadLramByte(iot, ioh, ASCV_RISC_FLAG_B);
   2174 		if (loop++ > 0x7FFF)
   2175 			break;
   2176 	} while ((risc_flag & ASC_RISC_FLAG_GEN_INT) != 0);
   2177 
   2178 	host_flag = AscReadLramByte(iot, ioh, ASCV_HOST_FLAG_B) &
   2179 		(~ASC_HOST_FLAG_ACK_INT);
   2180 	AscWriteLramByte(iot, ioh, ASCV_HOST_FLAG_B,
   2181 			 host_flag | ASC_HOST_FLAG_ACK_INT);
   2182 	ASC_SET_CHIP_STATUS(iot, ioh, ASC_CIW_INT_ACK);
   2183 
   2184 	loop = 0;
   2185 	while (ASC_GET_CHIP_STATUS(iot, ioh) & ASC_CSW_INT_PENDING) {
   2186 		ASC_SET_CHIP_STATUS(iot, ioh, ASC_CIW_INT_ACK);
   2187 		if (loop++ > 3)
   2188 			break;
   2189 	}
   2190 
   2191 	AscWriteLramByte(iot, ioh, ASCV_HOST_FLAG_B, host_flag);
   2192 }
   2193 
   2194 
   2195 static u_int32_t
   2196 AscGetMaxDmaCount(bus_type)
   2197 	u_int16_t       bus_type;
   2198 {
   2199 	if (bus_type & ASC_IS_ISA)
   2200 		return (ASC_MAX_ISA_DMA_COUNT);
   2201 	else if (bus_type & (ASC_IS_EISA | ASC_IS_VL))
   2202 		return (ASC_MAX_VL_DMA_COUNT);
   2203 	return (ASC_MAX_PCI_DMA_COUNT);
   2204 }
   2205 
   2206 
   2207 static u_int16_t
   2208 AscGetIsaDmaChannel(iot, ioh)
   2209 	bus_space_tag_t iot;
   2210 	bus_space_handle_t ioh;
   2211 {
   2212 	u_int16_t       channel;
   2213 
   2214 	channel = ASC_GET_CHIP_CFG_LSW(iot, ioh) & 0x0003;
   2215 	if (channel == 0x03)
   2216 		return (0);
   2217 	else if (channel == 0x00)
   2218 		return (7);
   2219 	return (channel + 4);
   2220 }
   2221 
   2222 
   2223 static u_int16_t
   2224 AscSetIsaDmaChannel(iot, ioh, dma_channel)
   2225 	bus_space_tag_t iot;
   2226 	bus_space_handle_t ioh;
   2227 	u_int16_t       dma_channel;
   2228 {
   2229 	u_int16_t       cfg_lsw;
   2230 	u_int8_t        value;
   2231 
   2232 	if ((dma_channel >= 5) && (dma_channel <= 7)) {
   2233 		if (dma_channel == 7)
   2234 			value = 0x00;
   2235 		else
   2236 			value = dma_channel - 4;
   2237 		cfg_lsw = ASC_GET_CHIP_CFG_LSW(iot, ioh) & 0xFFFC;
   2238 		cfg_lsw |= value;
   2239 		ASC_SET_CHIP_CFG_LSW(iot, ioh, cfg_lsw);
   2240 		return (AscGetIsaDmaChannel(iot, ioh));
   2241 	}
   2242 	return (0);
   2243 }
   2244 
   2245 
   2246 static u_int8_t
   2247 AscGetIsaDmaSpeed(iot, ioh)
   2248 	bus_space_tag_t iot;
   2249 	bus_space_handle_t ioh;
   2250 {
   2251 	u_int8_t        speed_value;
   2252 
   2253 	AscSetBank(iot, ioh, 1);
   2254 	speed_value = ASC_READ_CHIP_DMA_SPEED(iot, ioh);
   2255 	speed_value &= 0x07;
   2256 	AscSetBank(iot, ioh, 0);
   2257 	return (speed_value);
   2258 }
   2259 
   2260 
   2261 static u_int8_t
   2262 AscSetIsaDmaSpeed(iot, ioh, speed_value)
   2263 	bus_space_tag_t iot;
   2264 	bus_space_handle_t ioh;
   2265 	u_int8_t        speed_value;
   2266 {
   2267 	speed_value &= 0x07;
   2268 	AscSetBank(iot, ioh, 1);
   2269 	ASC_WRITE_CHIP_DMA_SPEED(iot, ioh, speed_value);
   2270 	AscSetBank(iot, ioh, 0);
   2271 	return (AscGetIsaDmaSpeed(iot, ioh));
   2272 }
   2273 
   2274 
   2275 /******************************************************************************/
   2276 /*                              Messages routines                             */
   2277 /******************************************************************************/
   2278 
   2279 
   2280 static void
   2281 AscHandleExtMsgIn(sc, halt_q_addr, q_cntl, target_id, tid_no, asyn_sdtr)
   2282 	ASC_SOFTC      *sc;
   2283 	u_int16_t       halt_q_addr;
   2284 	u_int8_t        q_cntl;
   2285 	ASC_SCSI_BIT_ID_TYPE target_id;
   2286 	int             tid_no;
   2287 	u_int8_t        asyn_sdtr;
   2288 {
   2289 	bus_space_tag_t iot = sc->sc_iot;
   2290 	bus_space_handle_t ioh = sc->sc_ioh;
   2291 	EXT_MSG         ext_msg;
   2292 	u_int8_t        sdtr_data;
   2293 	int             sdtr_accept;
   2294 
   2295 
   2296 	AscMemWordCopyFromLram(iot, ioh, ASCV_MSGIN_BEG,
   2297 			     (u_int16_t *) & ext_msg, sizeof(EXT_MSG) >> 1);
   2298 
   2299 	if (ext_msg.msg_type == MS_EXTEND &&
   2300 	    ext_msg.msg_req == MS_SDTR_CODE &&
   2301 	    ext_msg.msg_len == MS_SDTR_LEN) {
   2302 		sdtr_accept = TRUE;
   2303 
   2304 		if (ext_msg.req_ack_offset > ASC_SYN_MAX_OFFSET) {
   2305 			sdtr_accept = FALSE;
   2306 			ext_msg.req_ack_offset = ASC_SYN_MAX_OFFSET;
   2307 		}
   2308 		if ((ext_msg.xfer_period <
   2309 		     sc->sdtr_period_tbl[sc->host_init_sdtr_index]) ||
   2310 		    (ext_msg.xfer_period >
   2311 		     sc->sdtr_period_tbl[sc->max_sdtr_index])) {
   2312 			sdtr_accept = FALSE;
   2313 			ext_msg.xfer_period = sc->sdtr_period_tbl[sc->host_init_sdtr_index];
   2314 		}
   2315 		if (sdtr_accept) {
   2316 			sdtr_data = AscCalSDTRData(sc, ext_msg.xfer_period,
   2317 						   ext_msg.req_ack_offset);
   2318 			if (sdtr_data == 0xFF) {
   2319 				q_cntl |= ASC_QC_MSG_OUT;
   2320 				sc->init_sdtr &= ~target_id;
   2321 				sc->sdtr_done &= ~target_id;
   2322 				AscSetChipSDTR(iot, ioh, asyn_sdtr, tid_no);
   2323 				sc->sdtr_data[tid_no] = asyn_sdtr;
   2324 			}
   2325 		}
   2326 		if (ext_msg.req_ack_offset == 0) {
   2327 			q_cntl &= ~ASC_QC_MSG_OUT;
   2328 			sc->init_sdtr &= ~target_id;
   2329 			sc->sdtr_done &= ~target_id;
   2330 			AscSetChipSDTR(iot, ioh, asyn_sdtr, tid_no);
   2331 		} else {
   2332 			if (sdtr_accept && (q_cntl & ASC_QC_MSG_OUT)) {
   2333 				q_cntl &= ~ASC_QC_MSG_OUT;
   2334 				sc->sdtr_done |= target_id;
   2335 				sc->init_sdtr |= target_id;
   2336 				sc->pci_fix_asyn_xfer &= ~target_id;
   2337 				sdtr_data = AscCalSDTRData(sc, ext_msg.xfer_period,
   2338 						    ext_msg.req_ack_offset);
   2339 				AscSetChipSDTR(iot, ioh, sdtr_data, tid_no);
   2340 				sc->sdtr_data[tid_no] = sdtr_data;
   2341 			} else {
   2342 				q_cntl |= ASC_QC_MSG_OUT;
   2343 				AscMsgOutSDTR(sc, ext_msg.xfer_period,
   2344 					      ext_msg.req_ack_offset);
   2345 				sc->pci_fix_asyn_xfer &= ~target_id;
   2346 				sdtr_data = AscCalSDTRData(sc, ext_msg.xfer_period,
   2347 						    ext_msg.req_ack_offset);
   2348 				AscSetChipSDTR(iot, ioh, sdtr_data, tid_no);
   2349 				sc->sdtr_data[tid_no] = sdtr_data;
   2350 				sc->sdtr_done |= target_id;
   2351 				sc->init_sdtr |= target_id;
   2352 			}
   2353 		}
   2354 	} else if (ext_msg.msg_type == MS_EXTEND &&
   2355 		   ext_msg.msg_req == MS_WDTR_CODE &&
   2356 		   ext_msg.msg_len == MS_WDTR_LEN) {
   2357 		ext_msg.wdtr_width = 0;
   2358 		AscMemWordCopyToLram(iot, ioh, ASCV_MSGOUT_BEG,
   2359 			     (u_int16_t *) & ext_msg, sizeof(EXT_MSG) >> 1);
   2360 		q_cntl |= ASC_QC_MSG_OUT;
   2361 	} else {
   2362 		ext_msg.msg_type = M1_MSG_REJECT;
   2363 		AscMemWordCopyToLram(iot, ioh, ASCV_MSGOUT_BEG,
   2364 			     (u_int16_t *) & ext_msg, sizeof(EXT_MSG) >> 1);
   2365 		q_cntl |= ASC_QC_MSG_OUT;
   2366 	}
   2367 
   2368 	AscWriteLramByte(iot, ioh, halt_q_addr + ASC_SCSIQ_B_CNTL, q_cntl);
   2369 }
   2370 
   2371 
   2372 static u_int8_t
   2373 AscMsgOutSDTR(sc, sdtr_period, sdtr_offset)
   2374 	ASC_SOFTC      *sc;
   2375 	u_int8_t        sdtr_period;
   2376 	u_int8_t        sdtr_offset;
   2377 {
   2378 	bus_space_tag_t iot = sc->sc_iot;
   2379 	bus_space_handle_t ioh = sc->sc_ioh;
   2380 	EXT_MSG         sdtr_buf;
   2381 	u_int8_t        sdtr_period_index;
   2382 
   2383 
   2384 	sdtr_buf.msg_type = MS_EXTEND;
   2385 	sdtr_buf.msg_len = MS_SDTR_LEN;
   2386 	sdtr_buf.msg_req = MS_SDTR_CODE;
   2387 	sdtr_buf.xfer_period = sdtr_period;
   2388 	sdtr_offset &= ASC_SYN_MAX_OFFSET;
   2389 	sdtr_buf.req_ack_offset = sdtr_offset;
   2390 	if ((sdtr_period_index = AscGetSynPeriodIndex(sc, sdtr_period)) <=
   2391 	    sc->max_sdtr_index) {
   2392 		AscMemWordCopyToLram(iot, ioh, ASCV_MSGOUT_BEG,
   2393 			    (u_int16_t *) & sdtr_buf, sizeof(EXT_MSG) >> 1);
   2394 		return ((sdtr_period_index << 4) | sdtr_offset);
   2395 	} else {
   2396 		sdtr_buf.req_ack_offset = 0;
   2397 		AscMemWordCopyToLram(iot, ioh, ASCV_MSGOUT_BEG,
   2398 			    (u_int16_t *) & sdtr_buf, sizeof(EXT_MSG) >> 1);
   2399 		return (0);
   2400 	}
   2401 }
   2402 
   2403 
   2404 /******************************************************************************/
   2405 /*                                  SDTR routines                             */
   2406 /******************************************************************************/
   2407 
   2408 
   2409 static void
   2410 AscSetChipSDTR(iot, ioh, sdtr_data, tid_no)
   2411 	bus_space_tag_t iot;
   2412 	bus_space_handle_t ioh;
   2413 	u_int8_t        sdtr_data;
   2414 	u_int8_t        tid_no;
   2415 {
   2416 	AscSetChipSynRegAtID(iot, ioh, tid_no, sdtr_data);
   2417 	AscWriteLramByte(iot, ioh, tid_no + ASCV_SDTR_DONE_BEG, sdtr_data);
   2418 }
   2419 
   2420 
   2421 static u_int8_t
   2422 AscCalSDTRData(sc, sdtr_period, syn_offset)
   2423 	ASC_SOFTC      *sc;
   2424 	u_int8_t        sdtr_period;
   2425 	u_int8_t        syn_offset;
   2426 {
   2427 	u_int8_t        byte;
   2428 	u_int8_t        sdtr_period_ix;
   2429 
   2430 	sdtr_period_ix = AscGetSynPeriodIndex(sc, sdtr_period);
   2431 	if (sdtr_period_ix > sc->max_sdtr_index)
   2432 		return (0xFF);
   2433 
   2434 	byte = (sdtr_period_ix << 4) | (syn_offset & ASC_SYN_MAX_OFFSET);
   2435 	return (byte);
   2436 }
   2437 
   2438 
   2439 static u_int8_t
   2440 AscGetSynPeriodIndex(sc, syn_time)
   2441 	ASC_SOFTC      *sc;
   2442 	u_int8_t        syn_time;
   2443 {
   2444 	u_int8_t       *period_table;
   2445 	int             max_index;
   2446 	int             min_index;
   2447 	int             i;
   2448 
   2449 	period_table = sc->sdtr_period_tbl;
   2450 	max_index = sc->max_sdtr_index;
   2451 	min_index = sc->host_init_sdtr_index;
   2452 	if ((syn_time <= period_table[max_index])) {
   2453 		for (i = min_index; i < (max_index - 1); i++) {
   2454 			if (syn_time <= period_table[i])
   2455 				return (i);
   2456 		}
   2457 
   2458 		return (max_index);
   2459 	} else
   2460 		return (max_index + 1);
   2461 }
   2462 
   2463 
   2464 /******************************************************************************/
   2465 /*                                 Queue routines                             */
   2466 /******************************************************************************/
   2467 
   2468 /*
   2469  * Send a command to the board
   2470  */
   2471 int
   2472 AscExeScsiQueue(sc, scsiq)
   2473 	ASC_SOFTC      *sc;
   2474 	ASC_SCSI_Q     *scsiq;
   2475 {
   2476 	bus_space_tag_t iot = sc->sc_iot;
   2477 	bus_space_handle_t ioh = sc->sc_ioh;
   2478 	ASC_SG_HEAD    *sg_head = scsiq->sg_head;
   2479 	int             retval;
   2480 	int             n_q_required;
   2481 	int             disable_syn_offset_one_fix;
   2482 	int             i;
   2483 	u_int32_t       addr;
   2484 	u_int16_t       sg_entry_cnt = 0;
   2485 	u_int16_t       sg_entry_cnt_minus_one = 0;
   2486 	u_int8_t        target_ix;
   2487 	u_int8_t        tid_no;
   2488 	u_int8_t        sdtr_data;
   2489 	u_int8_t        extra_bytes;
   2490 	u_int8_t        scsi_cmd;
   2491 	u_int32_t       data_cnt;
   2492 
   2493 
   2494 	scsiq->q1.q_no = 0;
   2495 	if ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0)
   2496 		scsiq->q1.extra_bytes = 0;
   2497 
   2498 	retval = ASC_BUSY;
   2499 	target_ix = scsiq->q2.target_ix;
   2500 	tid_no = ASC_TIX_TO_TID(target_ix);
   2501 	n_q_required = 1;
   2502 
   2503 	if (scsiq->cdbptr[0] == SCSICMD_RequestSense)
   2504 		if ((sc->init_sdtr & scsiq->q1.target_id) != 0) {
   2505 			sc->sdtr_done &= ~scsiq->q1.target_id;
   2506 			sdtr_data = ASC_GET_MCODE_INIT_SDTR_AT_ID(iot, ioh, tid_no);
   2507 			AscMsgOutSDTR(sc, sc->sdtr_period_tbl[(sdtr_data >> 4) &
   2508 						  (sc->max_sdtr_index - 1)],
   2509 				      sdtr_data & ASC_SYN_MAX_OFFSET);
   2510 			scsiq->q1.cntl |= (ASC_QC_MSG_OUT | ASC_QC_URGENT);
   2511 		}
   2512 	/*
   2513 	 * if there is just one segment into S/G list then
   2514 	 * map it as it was a single request, filling
   2515 	 * data_addr and data_cnt of ASC_SCSIQ structure.
   2516 	 */
   2517 	if ((scsiq->q1.cntl & ASC_QC_SG_HEAD) != 0) {
   2518 		sg_entry_cnt = sg_head->entry_cnt;
   2519 
   2520 		if (sg_entry_cnt < 1)
   2521 			panic("AscExeScsiQueue: Queue with QC_SG_HEAD set but %d segs.",
   2522 			      sg_entry_cnt);
   2523 
   2524 		if (sg_entry_cnt > ASC_MAX_SG_LIST)
   2525 			panic("AscExeScsiQueue: Queue with too many segs.");
   2526 
   2527 		if (sg_entry_cnt == 1) {
   2528 			scsiq->q1.data_addr = sg_head->sg_list[0].addr;
   2529 			scsiq->q1.data_cnt = sg_head->sg_list[0].bytes;
   2530 			scsiq->q1.cntl &= ~(ASC_QC_SG_HEAD | ASC_QC_SG_SWAP_QUEUE);
   2531 		}
   2532 		sg_entry_cnt_minus_one = sg_entry_cnt - 1;
   2533 	}
   2534 	scsi_cmd = scsiq->cdbptr[0];
   2535 	disable_syn_offset_one_fix = FALSE;
   2536 	if ((sc->pci_fix_asyn_xfer & scsiq->q1.target_id) &&
   2537 	    !(sc->pci_fix_asyn_xfer_always & scsiq->q1.target_id)) {
   2538 		if (scsiq->q1.cntl & ASC_QC_SG_HEAD) {
   2539 			data_cnt = 0;
   2540 			for (i = 0; i < sg_entry_cnt; i++)
   2541 				data_cnt += sg_head->sg_list[i].bytes;
   2542 		} else {
   2543 			data_cnt = scsiq->q1.data_cnt;
   2544 		}
   2545 
   2546 		if (data_cnt != 0ul) {
   2547 			if (data_cnt < 512ul) {
   2548 				disable_syn_offset_one_fix = TRUE;
   2549 			} else {
   2550 				if (scsi_cmd == SCSICMD_Inquiry ||
   2551 				    scsi_cmd == SCSICMD_RequestSense ||
   2552 				    scsi_cmd == SCSICMD_ReadCapacity ||
   2553 				    scsi_cmd == SCSICMD_ReadTOC ||
   2554 				    scsi_cmd == SCSICMD_ModeSelect6 ||
   2555 				    scsi_cmd == SCSICMD_ModeSense6 ||
   2556 				    scsi_cmd == SCSICMD_ModeSelect10 ||
   2557 				    scsi_cmd == SCSICMD_ModeSense10) {
   2558 					disable_syn_offset_one_fix = TRUE;
   2559 				}
   2560 			}
   2561 		}
   2562 	}
   2563 	if (disable_syn_offset_one_fix) {
   2564 		scsiq->q2.tag_code &= ~M2_QTAG_MSG_SIMPLE;
   2565 		scsiq->q2.tag_code |= (ASC_TAG_FLAG_DISABLE_ASYN_USE_SYN_FIX |
   2566 				       ASC_TAG_FLAG_DISABLE_DISCONNECT);
   2567 	} else {
   2568 		scsiq->q2.tag_code &= 0x23;
   2569 	}
   2570 
   2571 	if ((scsiq->q1.cntl & ASC_QC_SG_HEAD) != 0) {
   2572 		if (sc->bug_fix_cntl) {
   2573 			if (sc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
   2574 				if ((scsi_cmd == SCSICMD_Read6) || (scsi_cmd == SCSICMD_Read10)) {
   2575 					addr = sg_head->sg_list[sg_entry_cnt_minus_one].addr +
   2576 						sg_head->sg_list[sg_entry_cnt_minus_one].bytes;
   2577 					extra_bytes = addr & 0x0003;
   2578 					if ((extra_bytes != 0) &&
   2579 					    ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0)) {
   2580 						scsiq->q2.tag_code |= ASC_TAG_FLAG_EXTRA_BYTES;
   2581 						scsiq->q1.extra_bytes = extra_bytes;
   2582 						sg_head->sg_list[sg_entry_cnt_minus_one].bytes -=
   2583 							extra_bytes;
   2584 					}
   2585 				}
   2586 			}
   2587 		}
   2588 		sg_head->entry_to_copy = sg_head->entry_cnt;
   2589 		n_q_required = AscSgListToQueue(sg_entry_cnt);
   2590 		if ((AscGetNumOfFreeQueue(sc, target_ix, n_q_required) >= n_q_required)
   2591 		    || ((scsiq->q1.cntl & ASC_QC_URGENT) != 0)) {
   2592 			retval = AscSendScsiQueue(sc, scsiq, n_q_required);
   2593 		}
   2594 	} else {
   2595 		if (sc->bug_fix_cntl) {
   2596 			if (sc->bug_fix_cntl & ASC_BUG_FIX_IF_NOT_DWB) {
   2597 				if ((scsi_cmd == SCSICMD_Read6) || (scsi_cmd == SCSICMD_Read10)) {
   2598 					addr = scsiq->q1.data_addr + scsiq->q1.data_cnt;
   2599 					extra_bytes = addr & 0x0003;
   2600 					if ((extra_bytes != 0) &&
   2601 					    ((scsiq->q2.tag_code & ASC_TAG_FLAG_EXTRA_BYTES) == 0)) {
   2602 						if ((scsiq->q1.data_cnt & 0x01FF) == 0) {
   2603 							scsiq->q2.tag_code |= ASC_TAG_FLAG_EXTRA_BYTES;
   2604 							scsiq->q1.data_cnt -= extra_bytes;
   2605 							scsiq->q1.extra_bytes = extra_bytes;
   2606 						}
   2607 					}
   2608 				}
   2609 			}
   2610 		}
   2611 		n_q_required = 1;
   2612 		if ((AscGetNumOfFreeQueue(sc, target_ix, 1) >= 1) ||
   2613 		    ((scsiq->q1.cntl & ASC_QC_URGENT) != 0)) {
   2614 			retval = AscSendScsiQueue(sc, scsiq, n_q_required);
   2615 		}
   2616 	}
   2617 
   2618 	return (retval);
   2619 }
   2620 
   2621 
   2622 static int
   2623 AscSendScsiQueue(sc, scsiq, n_q_required)
   2624 	ASC_SOFTC      *sc;
   2625 	ASC_SCSI_Q     *scsiq;
   2626 	u_int8_t        n_q_required;
   2627 {
   2628 	bus_space_tag_t iot = sc->sc_iot;
   2629 	bus_space_handle_t ioh = sc->sc_ioh;
   2630 	u_int8_t        free_q_head;
   2631 	u_int8_t        next_qp;
   2632 	u_int8_t        tid_no;
   2633 	u_int8_t        target_ix;
   2634 	int             retval;
   2635 
   2636 
   2637 	target_ix = scsiq->q2.target_ix;
   2638 	tid_no = ASC_TIX_TO_TID(target_ix);
   2639 	retval = ASC_BUSY;
   2640 	free_q_head = ASC_GET_VAR_FREE_QHEAD(iot, ioh);
   2641 
   2642 	if ((next_qp = AscAllocMultipleFreeQueue(iot, ioh, free_q_head, n_q_required))
   2643 	    != ASC_QLINK_END) {
   2644 		if (n_q_required > 1) {
   2645 			sc->last_q_shortage = 0;
   2646 			scsiq->sg_head->queue_cnt = n_q_required - 1;
   2647 		}
   2648 		scsiq->q1.q_no = free_q_head;
   2649 
   2650 		if ((retval = AscPutReadySgListQueue(sc, scsiq, free_q_head)) == ASC_NOERROR) {
   2651 			ASC_PUT_VAR_FREE_QHEAD(iot, ioh, next_qp);
   2652 			sc->cur_total_qng += n_q_required;
   2653 			sc->cur_dvc_qng[tid_no]++;
   2654 		}
   2655 	}
   2656 	return (retval);
   2657 }
   2658 
   2659 
   2660 static int
   2661 AscPutReadySgListQueue(sc, scsiq, q_no)
   2662 	ASC_SOFTC      *sc;
   2663 	ASC_SCSI_Q     *scsiq;
   2664 	u_int8_t        q_no;
   2665 {
   2666 	bus_space_tag_t iot = sc->sc_iot;
   2667 	bus_space_handle_t ioh = sc->sc_ioh;
   2668 	int             retval;
   2669 	int             i;
   2670 	ASC_SG_HEAD    *sg_head;
   2671 	ASC_SG_LIST_Q   scsi_sg_q;
   2672 	u_int32_t       saved_data_addr;
   2673 	u_int32_t       saved_data_cnt;
   2674 	u_int16_t       sg_list_dwords;
   2675 	u_int16_t       sg_index;
   2676 	u_int16_t       sg_entry_cnt;
   2677 	u_int16_t       q_addr;
   2678 	u_int8_t        next_qp;
   2679 
   2680 
   2681 	saved_data_addr = scsiq->q1.data_addr;
   2682 	saved_data_cnt = scsiq->q1.data_cnt;
   2683 
   2684 	if ((sg_head = scsiq->sg_head) != 0) {
   2685 		scsiq->q1.data_addr = sg_head->sg_list[0].addr;
   2686 		scsiq->q1.data_cnt = sg_head->sg_list[0].bytes;
   2687 		sg_entry_cnt = sg_head->entry_cnt - 1;
   2688 		if (sg_entry_cnt != 0) {
   2689 			q_addr = ASC_QNO_TO_QADDR(q_no);
   2690 			sg_index = 1;
   2691 			scsiq->q1.sg_queue_cnt = sg_head->queue_cnt;
   2692 			scsi_sg_q.sg_head_qp = q_no;
   2693 			scsi_sg_q.cntl = ASC_QCSG_SG_XFER_LIST;
   2694 
   2695 			for (i = 0; i < sg_head->queue_cnt; i++) {
   2696 				scsi_sg_q.seq_no = i + 1;
   2697 				if (sg_entry_cnt > ASC_SG_LIST_PER_Q) {
   2698 					sg_list_dwords = ASC_SG_LIST_PER_Q * 2;
   2699 					sg_entry_cnt -= ASC_SG_LIST_PER_Q;
   2700 					if (i == 0) {
   2701 						scsi_sg_q.sg_list_cnt = ASC_SG_LIST_PER_Q;
   2702 						scsi_sg_q.sg_cur_list_cnt = ASC_SG_LIST_PER_Q;
   2703 					} else {
   2704 						scsi_sg_q.sg_list_cnt = ASC_SG_LIST_PER_Q - 1;
   2705 						scsi_sg_q.sg_cur_list_cnt = ASC_SG_LIST_PER_Q - 1;
   2706 					}
   2707 				} else {
   2708 					scsi_sg_q.cntl |= ASC_QCSG_SG_XFER_END;
   2709 					sg_list_dwords = sg_entry_cnt << 1;
   2710 					if (i == 0) {
   2711 						scsi_sg_q.sg_list_cnt = sg_entry_cnt;
   2712 						scsi_sg_q.sg_cur_list_cnt = sg_entry_cnt;
   2713 					} else {
   2714 						scsi_sg_q.sg_list_cnt = sg_entry_cnt - 1;
   2715 						scsi_sg_q.sg_cur_list_cnt = sg_entry_cnt - 1;
   2716 					}
   2717 
   2718 					sg_entry_cnt = 0;
   2719 				}
   2720 
   2721 				next_qp = AscReadLramByte(iot, ioh, q_addr + ASC_SCSIQ_B_FWD);
   2722 				scsi_sg_q.q_no = next_qp;
   2723 				q_addr = ASC_QNO_TO_QADDR(next_qp);
   2724 
   2725 				/*
   2726 				 * Tell the board how many entries are in the S/G list
   2727 				 */
   2728 				AscMemWordCopyToLram(iot, ioh, q_addr + ASC_SCSIQ_SGHD_CPY_BEG,
   2729 							(u_int16_t *) & scsi_sg_q,
   2730 							sizeof(ASC_SG_LIST_Q) >> 1);
   2731 				/*
   2732 				 * Tell the board the addresses of the S/G list segments
   2733 				 */
   2734 				AscMemDWordCopyToLram(iot, ioh, q_addr + ASC_SGQ_LIST_BEG,
   2735 							(u_int32_t *) & sg_head->sg_list[sg_index],
   2736 							sg_list_dwords);
   2737 				sg_index += ASC_SG_LIST_PER_Q;
   2738 			}
   2739 		}
   2740 	}
   2741 	retval = AscPutReadyQueue(sc, scsiq, q_no);
   2742 	scsiq->q1.data_addr = saved_data_addr;
   2743 	scsiq->q1.data_cnt = saved_data_cnt;
   2744 	return (retval);
   2745 }
   2746 
   2747 
   2748 static int
   2749 AscPutReadyQueue(sc, scsiq, q_no)
   2750 	ASC_SOFTC      *sc;
   2751 	ASC_SCSI_Q     *scsiq;
   2752 	u_int8_t        q_no;
   2753 {
   2754 	bus_space_tag_t iot = sc->sc_iot;
   2755 	bus_space_handle_t ioh = sc->sc_ioh;
   2756 	u_int16_t       q_addr;
   2757 	u_int8_t        tid_no;
   2758 	u_int8_t        sdtr_data;
   2759 	u_int8_t        syn_period_ix;
   2760 	u_int8_t        syn_offset;
   2761 
   2762 
   2763 	if (((sc->init_sdtr & scsiq->q1.target_id) != 0) &&
   2764 	    ((sc->sdtr_done & scsiq->q1.target_id) == 0)) {
   2765 		tid_no = ASC_TIX_TO_TID(scsiq->q2.target_ix);
   2766 		sdtr_data = ASC_GET_MCODE_INIT_SDTR_AT_ID(iot, ioh, tid_no);
   2767 		syn_period_ix = (sdtr_data >> 4) & (sc->max_sdtr_index - 1);
   2768 		syn_offset = sdtr_data & ASC_SYN_MAX_OFFSET;
   2769 		AscMsgOutSDTR(sc, sc->sdtr_period_tbl[syn_period_ix], syn_offset);
   2770 		scsiq->q1.cntl |= ASC_QC_MSG_OUT;
   2771 	}
   2772 	q_addr = ASC_QNO_TO_QADDR(q_no);
   2773 
   2774 	if ((scsiq->q1.target_id & sc->use_tagged_qng) == 0) {
   2775 		scsiq->q2.tag_code &= ~M2_QTAG_MSG_SIMPLE;
   2776 	}
   2777 	scsiq->q1.status = ASC_QS_FREE;
   2778 	AscMemWordCopyToLram(iot, ioh, q_addr + ASC_SCSIQ_CDB_BEG,
   2779 		       (u_int16_t *) scsiq->cdbptr, scsiq->q2.cdb_len >> 1);
   2780 
   2781 	AscPutSCSIQ(iot, ioh, q_addr + ASC_SCSIQ_CPY_BEG, scsiq);
   2782 
   2783 	/*
   2784 	 * Let's start the command
   2785 	 */
   2786 	AscWriteLramWord(iot, ioh, q_addr + ASC_SCSIQ_B_STATUS,
   2787 			 (scsiq->q1.q_no << 8) | ASC_QS_READY);
   2788 
   2789 	return (ASC_NOERROR);
   2790 }
   2791 
   2792 
   2793 static void
   2794 AscPutSCSIQ(iot, ioh, addr, scsiq)
   2795 	bus_space_tag_t		iot;
   2796 	bus_space_handle_t	ioh;
   2797 	u_int16_t		addr;
   2798 	ASC_SCSI_Q 		*scsiq;
   2799 {
   2800 	u_int16_t	val;
   2801 
   2802 
   2803 	ASC_SET_CHIP_LRAM_ADDR(iot, ioh, addr);
   2804 
   2805 	/* ASC_SCSIQ_1 */
   2806 	val = MAKEWORD(scsiq->q1.cntl, scsiq->q1.sg_queue_cnt);
   2807 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2808 	val = MAKEWORD(scsiq->q1.target_id, scsiq->q1.target_lun);
   2809 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2810 	val = LO_WORD(scsiq->q1.data_addr);
   2811 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2812 	val = HI_WORD(scsiq->q1.data_addr);
   2813 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2814 	val = LO_WORD(scsiq->q1.data_cnt);
   2815 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2816 	val = HI_WORD(scsiq->q1.data_cnt);
   2817 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2818 	val = LO_WORD(scsiq->q1.sense_addr);
   2819 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2820 	val = HI_WORD(scsiq->q1.sense_addr);
   2821 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2822 	val = MAKEWORD(scsiq->q1.sense_len, scsiq->q1.extra_bytes);
   2823 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2824 
   2825 	/* ASC_SCSIQ_2 */
   2826 	val = LO_WORD(scsiq->q2.ccb_ptr);
   2827 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2828 	val = HI_WORD(scsiq->q2.ccb_ptr);
   2829 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2830 	val = MAKEWORD(scsiq->q2.target_ix, scsiq->q2.flag);
   2831 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2832 	val = MAKEWORD(scsiq->q2.cdb_len, scsiq->q2.tag_code);
   2833 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, val);
   2834 	ASC_SET_CHIP_LRAM_DATA(iot, ioh, scsiq->q2.vm_id);
   2835 }
   2836 
   2837 
   2838 static int
   2839 AscSgListToQueue(sg_list)
   2840 	int             sg_list;
   2841 {
   2842 	int             n_sg_list_qs;
   2843 
   2844 	n_sg_list_qs = ((sg_list - 1) / ASC_SG_LIST_PER_Q);
   2845 	if (((sg_list - 1) % ASC_SG_LIST_PER_Q) != 0)
   2846 		n_sg_list_qs++;
   2847 
   2848 	return (n_sg_list_qs + 1);
   2849 }
   2850 
   2851 
   2852 static u_int
   2853 AscGetNumOfFreeQueue(sc, target_ix, n_qs)
   2854 	ASC_SOFTC      *sc;
   2855 	u_int8_t        target_ix;
   2856 	u_int8_t        n_qs;
   2857 {
   2858 	u_int           cur_used_qs;
   2859 	u_int           cur_free_qs;
   2860 
   2861 
   2862 	if (n_qs == 1) {
   2863 		cur_used_qs = sc->cur_total_qng +
   2864 			sc->last_q_shortage +
   2865 			ASC_MIN_FREE_Q;
   2866 	} else {
   2867 		cur_used_qs = sc->cur_total_qng + ASC_MIN_FREE_Q;
   2868 	}
   2869 
   2870 	if ((cur_used_qs + n_qs) <= sc->max_total_qng) {
   2871 		cur_free_qs = sc->max_total_qng - cur_used_qs;
   2872 		return (cur_free_qs);
   2873 	}
   2874 	if (n_qs > 1)
   2875 		if ((n_qs > sc->last_q_shortage) &&
   2876 		    (n_qs <= (sc->max_total_qng - ASC_MIN_FREE_Q))) {
   2877 			sc->last_q_shortage = n_qs;
   2878 		}
   2879 	return (0);
   2880 }
   2881 
   2882 
   2883 static u_int8_t
   2884 AscAllocFreeQueue(iot, ioh, free_q_head)
   2885 	bus_space_tag_t iot;
   2886 	bus_space_handle_t ioh;
   2887 	u_int8_t        free_q_head;
   2888 {
   2889 	u_int16_t       q_addr;
   2890 	u_int8_t        next_qp;
   2891 	u_int8_t        q_status;
   2892 
   2893 
   2894 	q_addr = ASC_QNO_TO_QADDR(free_q_head);
   2895 	q_status = AscReadLramByte(iot, ioh, q_addr + ASC_SCSIQ_B_STATUS);
   2896 	next_qp = AscReadLramByte(iot, ioh, q_addr + ASC_SCSIQ_B_FWD);
   2897 	if (((q_status & ASC_QS_READY) == 0) && (next_qp != ASC_QLINK_END))
   2898 		return (next_qp);
   2899 
   2900 	return (ASC_QLINK_END);
   2901 }
   2902 
   2903 
   2904 static u_int8_t
   2905 AscAllocMultipleFreeQueue(iot, ioh, free_q_head, n_free_q)
   2906 	bus_space_tag_t iot;
   2907 	bus_space_handle_t ioh;
   2908 	u_int8_t        free_q_head;
   2909 	u_int8_t        n_free_q;
   2910 {
   2911 	u_int8_t        i;
   2912 
   2913 	for (i = 0; i < n_free_q; i++) {
   2914 		free_q_head = AscAllocFreeQueue(iot, ioh, free_q_head);
   2915 		if (free_q_head == ASC_QLINK_END)
   2916 			break;
   2917 	}
   2918 
   2919 	return (free_q_head);
   2920 }
   2921 
   2922 
   2923 static int
   2924 AscStopQueueExe(iot, ioh)
   2925 	bus_space_tag_t iot;
   2926 	bus_space_handle_t ioh;
   2927 {
   2928 	int             count = 0;
   2929 
   2930 	if (AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B) == 0) {
   2931 		AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B, ASC_STOP_REQ_RISC_STOP);
   2932 		do {
   2933 			if (AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B) &
   2934 			    ASC_STOP_ACK_RISC_STOP)
   2935 				return (1);
   2936 
   2937 			DvcSleepMilliSecond(100);
   2938 		} while (count++ < 20);
   2939 	}
   2940 	return (0);
   2941 }
   2942 
   2943 
   2944 static void
   2945 AscStartQueueExe(iot, ioh)
   2946 	bus_space_tag_t iot;
   2947 	bus_space_handle_t ioh;
   2948 {
   2949 
   2950 	if (AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B) != 0)
   2951 		AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B, 0);
   2952 }
   2953 
   2954 
   2955 static void
   2956 AscCleanUpBusyQueue(iot, ioh)
   2957 	bus_space_tag_t iot;
   2958 	bus_space_handle_t ioh;
   2959 {
   2960 	int             count = 0;
   2961 	u_int8_t        stop_code;
   2962 
   2963 
   2964 	if (AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B) != 0) {
   2965 		AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B, ASC_STOP_CLEAN_UP_BUSY_Q);
   2966 		do {
   2967 			stop_code = AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B);
   2968 			if ((stop_code & ASC_STOP_CLEAN_UP_BUSY_Q) == 0)
   2969 				break;
   2970 
   2971 			DvcSleepMilliSecond(100);
   2972 		} while (count++ < 20);
   2973 	}
   2974 }
   2975 
   2976 
   2977 static int
   2978 _AscWaitQDone(iot, ioh, scsiq)
   2979 	bus_space_tag_t iot;
   2980 	bus_space_handle_t ioh;
   2981 	ASC_SCSI_Q     *scsiq;
   2982 {
   2983 	u_int16_t       q_addr;
   2984 	u_int8_t        q_status;
   2985 	int             count = 0;
   2986 
   2987 	while (scsiq->q1.q_no == 0);
   2988 
   2989 	q_addr = ASC_QNO_TO_QADDR(scsiq->q1.q_no);
   2990 	do {
   2991 		q_status = AscReadLramByte(iot, ioh, q_addr + ASC_SCSIQ_B_STATUS);
   2992 		DvcSleepMilliSecond(100L);
   2993 		if (count++ > 30)
   2994 			return (0);
   2995 
   2996 	} while ((q_status & ASC_QS_READY) != 0);
   2997 
   2998 	return (1);
   2999 }
   3000 
   3001 
   3002 static int
   3003 AscCleanUpDiscQueue(iot, ioh)
   3004 	bus_space_tag_t iot;
   3005 	bus_space_handle_t ioh;
   3006 {
   3007 	int             count;
   3008 	u_int8_t        stop_code;
   3009 
   3010 	count = 0;
   3011 	if (AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B) != 0) {
   3012 		AscWriteLramByte(iot, ioh, ASCV_STOP_CODE_B, ASC_STOP_CLEAN_UP_DISC_Q);
   3013 		do {
   3014 			stop_code = AscReadLramByte(iot, ioh, ASCV_STOP_CODE_B);
   3015 			if ((stop_code & ASC_STOP_CLEAN_UP_DISC_Q) == 0)
   3016 				break;
   3017 
   3018 			DvcSleepMilliSecond(100);
   3019 		} while (count++ < 20);
   3020 	}
   3021 	return (1);
   3022 }
   3023 
   3024 
   3025 /******************************************************************************/
   3026 /*                           Abort and Reset CCB routines                     */
   3027 /******************************************************************************/
   3028 
   3029 
   3030 int
   3031 AscAbortCCB(sc, ccb)
   3032 	ASC_SOFTC      *sc;
   3033 	u_int32_t       ccb;
   3034 {
   3035 	bus_space_tag_t iot = sc->sc_iot;
   3036 	bus_space_handle_t ioh = sc->sc_ioh;
   3037 	int             retval;
   3038 	ASC_SCSI_BIT_ID_TYPE saved_unit_not_ready;
   3039 
   3040 
   3041 	retval = -1;
   3042 	saved_unit_not_ready = sc->unit_not_ready;
   3043 	sc->unit_not_ready = 0xFF;
   3044 	AscWaitISRDone(sc);
   3045 	if (AscStopQueueExe(iot, ioh) == 1) {
   3046 		if (AscRiscHaltedAbortCCB(sc, ccb) == 1) {
   3047 			retval = 1;
   3048 			AscCleanUpBusyQueue(iot, ioh);
   3049 			AscStartQueueExe(iot, ioh);
   3050 		} else {
   3051 			retval = 0;
   3052 			AscStartQueueExe(iot, ioh);
   3053 		}
   3054 	}
   3055 	sc->unit_not_ready = saved_unit_not_ready;
   3056 
   3057 	return (retval);
   3058 }
   3059 
   3060 
   3061 static int
   3062 AscRiscHaltedAbortCCB(sc, ccb)
   3063 	ASC_SOFTC      *sc;
   3064 	u_int32_t       ccb;
   3065 {
   3066 	bus_space_tag_t iot = sc->sc_iot;
   3067 	bus_space_handle_t ioh = sc->sc_ioh;
   3068 	u_int16_t       q_addr;
   3069 	u_int8_t        q_no;
   3070 	ASC_QDONE_INFO  scsiq_buf;
   3071 	ASC_QDONE_INFO *scsiq;
   3072 	ASC_ISR_CALLBACK asc_isr_callback;
   3073 	int             last_int_level;
   3074 
   3075 
   3076 	asc_isr_callback = (ASC_ISR_CALLBACK) sc->isr_callback;
   3077 	last_int_level = DvcEnterCritical();
   3078 	scsiq = (ASC_QDONE_INFO *) & scsiq_buf;
   3079 
   3080 	for (q_no = ASC_MIN_ACTIVE_QNO; q_no <= sc->max_total_qng; q_no++) {
   3081 		q_addr = ASC_QNO_TO_QADDR(q_no);
   3082 		scsiq->d2.ccb_ptr = AscReadLramDWord(iot, ioh,
   3083 					       q_addr + ASC_SCSIQ_D_CCBPTR);
   3084 		if (scsiq->d2.ccb_ptr == ccb) {
   3085 			_AscCopyLramScsiDoneQ(iot, ioh, q_addr, scsiq, sc->max_dma_count);
   3086 			if (((scsiq->q_status & ASC_QS_READY) != 0)
   3087 			    && ((scsiq->q_status & ASC_QS_ABORTED) == 0)
   3088 			  && ((scsiq->cntl & ASC_QCSG_SG_XFER_LIST) == 0)) {
   3089 				scsiq->q_status |= ASC_QS_ABORTED;
   3090 				scsiq->d3.done_stat = ASC_QD_ABORTED_BY_HOST;
   3091 				AscWriteLramDWord(iot, ioh, q_addr + ASC_SCSIQ_D_CCBPTR, 0L);
   3092 				AscWriteLramByte(iot, ioh, q_addr + ASC_SCSIQ_B_STATUS,
   3093 						 scsiq->q_status);
   3094 				(*asc_isr_callback) (sc, scsiq);
   3095 				return (1);
   3096 			}
   3097 		}
   3098 	}
   3099 
   3100 	DvcLeaveCritical(last_int_level);
   3101 	return (0);
   3102 }
   3103 
   3104 
   3105 static int
   3106 AscRiscHaltedAbortTIX(sc, target_ix)
   3107 	ASC_SOFTC      *sc;
   3108 	u_int8_t        target_ix;
   3109 {
   3110 	bus_space_tag_t iot = sc->sc_iot;
   3111 	bus_space_handle_t ioh = sc->sc_ioh;
   3112 	u_int16_t       q_addr;
   3113 	u_int8_t        q_no;
   3114 	ASC_QDONE_INFO  scsiq_buf;
   3115 	ASC_QDONE_INFO *scsiq;
   3116 	ASC_ISR_CALLBACK asc_isr_callback;
   3117 	int             last_int_level;
   3118 
   3119 
   3120 	asc_isr_callback = (ASC_ISR_CALLBACK) sc->isr_callback;
   3121 	last_int_level = DvcEnterCritical();
   3122 	scsiq = (ASC_QDONE_INFO *) & scsiq_buf;
   3123 	for (q_no = ASC_MIN_ACTIVE_QNO; q_no <= sc->max_total_qng; q_no++) {
   3124 		q_addr = ASC_QNO_TO_QADDR(q_no);
   3125 		_AscCopyLramScsiDoneQ(iot, ioh, q_addr, scsiq, sc->max_dma_count);
   3126 		if (((scsiq->q_status & ASC_QS_READY) != 0) &&
   3127 		    ((scsiq->q_status & ASC_QS_ABORTED) == 0) &&
   3128 		    ((scsiq->cntl & ASC_QCSG_SG_XFER_LIST) == 0)) {
   3129 			if (scsiq->d2.target_ix == target_ix) {
   3130 				scsiq->q_status |= ASC_QS_ABORTED;
   3131 				scsiq->d3.done_stat = ASC_QD_ABORTED_BY_HOST;
   3132 				AscWriteLramDWord(iot, ioh, q_addr + ASC_SCSIQ_D_CCBPTR, 0L);
   3133 				AscWriteLramByte(iot, ioh, q_addr + ASC_SCSIQ_B_STATUS,
   3134 						 scsiq->q_status);
   3135 				(*asc_isr_callback) (sc, scsiq);
   3136 			}
   3137 		}
   3138 	}
   3139 	DvcLeaveCritical(last_int_level);
   3140 	return (1);
   3141 }
   3142 
   3143 
   3144 /*
   3145  * AscResetDevice calls _AscWaitQDone which requires interrupt enabled,
   3146  * so we cannot use this function with the actual NetBSD SCSI layer
   3147  * because at boot time interrupts are disabled.
   3148  */
   3149 int
   3150 AscResetDevice(sc, target_ix)
   3151 	ASC_SOFTC      *sc;
   3152 	u_char          target_ix;
   3153 {
   3154 	bus_space_tag_t iot = sc->sc_iot;
   3155 	bus_space_handle_t ioh = sc->sc_ioh;
   3156 	int             retval;
   3157 	u_int8_t        tid_no;
   3158 	ASC_SCSI_BIT_ID_TYPE target_id;
   3159 	int             i;
   3160 	ASC_SCSI_REQ_Q  scsiq_buf;
   3161 	ASC_SCSI_REQ_Q *scsiq;
   3162 	u_int8_t       *buf;
   3163 	ASC_SCSI_BIT_ID_TYPE saved_unit_not_ready;
   3164 
   3165 
   3166 	tid_no = ASC_TIX_TO_TID(target_ix);
   3167 	target_id = ASC_TID_TO_TARGET_ID(tid_no);
   3168 	saved_unit_not_ready = sc->unit_not_ready;
   3169 	sc->unit_not_ready = target_id;
   3170 	retval = ASC_ERROR;
   3171 
   3172 	AscWaitTixISRDone(sc, target_ix);
   3173 
   3174 	if (AscStopQueueExe(iot, ioh) == 1) {
   3175 		if (AscRiscHaltedAbortTIX(sc, target_ix) == 1) {
   3176 			AscCleanUpBusyQueue(iot, ioh);
   3177 			AscStartQueueExe(iot, ioh);
   3178 			AscWaitTixISRDone(sc, target_ix);
   3179 			retval = ASC_NOERROR;
   3180 			scsiq = (ASC_SCSI_REQ_Q *) & scsiq_buf;
   3181 			buf = (u_char *) & scsiq_buf;
   3182 			for (i = 0; i < sizeof(ASC_SCSI_REQ_Q); i++)
   3183 				*buf++ = 0x00;
   3184 			scsiq->q1.status = (u_char) ASC_QS_READY;
   3185 			scsiq->q2.cdb_len = 6;
   3186 			scsiq->q2.tag_code = M2_QTAG_MSG_SIMPLE;
   3187 			scsiq->q1.target_id = target_id;
   3188 			scsiq->q2.target_ix = ASC_TIDLUN_TO_IX(tid_no, 0);
   3189 			scsiq->cdbptr = (u_int8_t *) scsiq->cdb;
   3190 			scsiq->q1.cntl = ASC_QC_NO_CALLBACK | ASC_QC_MSG_OUT | ASC_QC_URGENT;
   3191 			AscWriteLramByte(iot, ioh, ASCV_MSGOUT_BEG, M1_BUS_DVC_RESET);
   3192 			sc->unit_not_ready &= ~target_id;
   3193 			sc->sdtr_done |= target_id;
   3194 			if (AscExeScsiQueue(sc, (ASC_SCSI_Q *) scsiq) == ASC_NOERROR) {
   3195 				sc->unit_not_ready = target_id;
   3196 				DvcSleepMilliSecond(1000);
   3197 				_AscWaitQDone(iot, ioh, (ASC_SCSI_Q *) scsiq);
   3198 				if (AscStopQueueExe(iot, ioh) == ASC_NOERROR) {
   3199 					AscCleanUpDiscQueue(iot, ioh);
   3200 					AscStartQueueExe(iot, ioh);
   3201 					if (sc->pci_fix_asyn_xfer & target_id)
   3202 						AscSetRunChipSynRegAtID(iot, ioh, tid_no,
   3203 								ASYN_SDTR_DATA_FIX_PCI_REV_AB);
   3204 					AscWaitTixISRDone(sc, target_ix);
   3205 				}
   3206 			} else
   3207 				retval = ASC_BUSY;
   3208 			sc->sdtr_done &= ~target_id;
   3209 		} else {
   3210 			retval = ASC_ERROR;
   3211 			AscStartQueueExe(iot, ioh);
   3212 		}
   3213 	}
   3214 	sc->unit_not_ready = saved_unit_not_ready;
   3215 	return (retval);
   3216 }
   3217 
   3218 
   3219 int
   3220 AscResetBus(sc)
   3221 	ASC_SOFTC      *sc;
   3222 {
   3223 	bus_space_tag_t iot = sc->sc_iot;
   3224 	bus_space_handle_t ioh = sc->sc_ioh;
   3225 	int             retval;
   3226 	int             i;
   3227 
   3228 
   3229 	sc->unit_not_ready = 0xFF;
   3230 	retval = ASC_NOERROR;
   3231 
   3232 	AscWaitISRDone(sc);
   3233 	AscStopQueueExe(iot, ioh);
   3234 	sc->sdtr_done = 0;
   3235 	AscResetChipAndScsiBus(iot, ioh);
   3236 	DvcSleepMilliSecond((u_long) ((u_int16_t) sc->scsi_reset_wait * 1000));
   3237 	AscReInitLram(sc);
   3238 	for (i = 0; i <= ASC_MAX_TID; i++) {
   3239 		sc->cur_dvc_qng[i] = 0;
   3240 		if (sc->pci_fix_asyn_xfer & (ASC_SCSI_BIT_ID_TYPE) (0x01 << i))
   3241 			AscSetChipSynRegAtID(iot, ioh, i, ASYN_SDTR_DATA_FIX_PCI_REV_AB);
   3242 	}
   3243 
   3244 	ASC_SET_PC_ADDR(iot, ioh, ASC_MCODE_START_ADDR);
   3245 	if (ASC_GET_PC_ADDR(iot, ioh) != ASC_MCODE_START_ADDR)
   3246 		retval = ASC_ERROR;
   3247 
   3248 	if (AscStartChip(iot, ioh) == 0)
   3249 		retval = ASC_ERROR;
   3250 
   3251 	AscStartQueueExe(iot, ioh);
   3252 	sc->unit_not_ready = 0;
   3253 	sc->queue_full_or_busy = 0;
   3254 	return (retval);
   3255 }
   3256 
   3257 
   3258 /******************************************************************************/
   3259 /*                            Error Handling routines                         */
   3260 /******************************************************************************/
   3261 
   3262 
   3263 static int
   3264 AscSetLibErrorCode(sc, err_code)
   3265 	ASC_SOFTC      *sc;
   3266 	u_int16_t       err_code;
   3267 {
   3268 	/*
   3269 	 * if(sc->err_code == 0) { sc->err_code = err_code;
   3270 	 */ AscWriteLramWord(sc->sc_iot, sc->sc_ioh, ASCV_ASCDVC_ERR_CODE_W,
   3271 			       err_code);
   3272 	/*
   3273 	 * }
   3274 	 */
   3275 	return (err_code);
   3276 }
   3277 
   3278 
   3279 /******************************************************************************/
   3280 /*                            Handle bugged borads routines                   */
   3281 /******************************************************************************/
   3282 
   3283 
   3284 void
   3285 AscInquiryHandling(sc, tid_no, inq)
   3286 	ASC_SOFTC      *sc;
   3287 	u_int8_t        tid_no;
   3288 	ASC_SCSI_INQUIRY *inq;
   3289 {
   3290 	bus_space_tag_t iot = sc->sc_iot;
   3291 	bus_space_handle_t ioh = sc->sc_ioh;
   3292 	ASC_SCSI_BIT_ID_TYPE tid_bit = ASC_TIX_TO_TARGET_ID(tid_no);
   3293 	ASC_SCSI_BIT_ID_TYPE orig_init_sdtr, orig_use_tagged_qng;
   3294 
   3295 
   3296 	orig_init_sdtr = sc->init_sdtr;
   3297 	orig_use_tagged_qng = sc->use_tagged_qng;
   3298 
   3299 	sc->init_sdtr &= ~tid_bit;
   3300 	sc->can_tagged_qng &= ~tid_bit;
   3301 	sc->use_tagged_qng &= ~tid_bit;
   3302 
   3303 	if (inq->byte3.rsp_data_fmt >= 2 || inq->byte2.ansi_apr_ver >= 2) {
   3304 		if ((sc->sdtr_enable & tid_bit) && inq->byte7.Sync)
   3305 			sc->init_sdtr |= tid_bit;
   3306 
   3307 		if ((sc->cmd_qng_enabled & tid_bit) && inq->byte7.CmdQue)
   3308 			if (AscTagQueuingSafe(inq)) {
   3309 				sc->use_tagged_qng |= tid_bit;
   3310 				sc->can_tagged_qng |= tid_bit;
   3311 			}
   3312 	}
   3313 	if (orig_use_tagged_qng != sc->use_tagged_qng) {
   3314 		AscWriteLramByte(iot, ioh, ASCV_DISC_ENABLE_B,
   3315 				 sc->disc_enable);
   3316 		AscWriteLramByte(iot, ioh, ASCV_USE_TAGGED_QNG_B,
   3317 				 sc->use_tagged_qng);
   3318 		AscWriteLramByte(iot, ioh, ASCV_CAN_TAGGED_QNG_B,
   3319 				 sc->can_tagged_qng);
   3320 
   3321 		sc->max_dvc_qng[tid_no] =
   3322 			sc->max_tag_qng[tid_no];
   3323 		AscWriteLramByte(iot, ioh, ASCV_MAX_DVC_QNG_BEG + tid_no,
   3324 				 sc->max_dvc_qng[tid_no]);
   3325 	}
   3326 	if (orig_init_sdtr != sc->init_sdtr)
   3327 		AscAsyncFix(sc, tid_no, inq);
   3328 }
   3329 
   3330 
   3331 static int
   3332 AscTagQueuingSafe(inq)
   3333 	ASC_SCSI_INQUIRY *inq;
   3334 {
   3335 	if ((inq->add_len >= 32) &&
   3336 	    (AscCompareString(inq->vendor_id, "QUANTUM XP34301", 15) == 0) &&
   3337 	    (AscCompareString(inq->product_rev_level, "1071", 4) == 0)) {
   3338 		return 0;
   3339 	}
   3340 	return 1;
   3341 }
   3342 
   3343 
   3344 static void
   3345 AscAsyncFix(sc, tid_no, inq)
   3346 	ASC_SOFTC      *sc;
   3347 	u_int8_t        tid_no;
   3348 	ASC_SCSI_INQUIRY *inq;
   3349 {
   3350 	u_int8_t        dvc_type;
   3351 	ASC_SCSI_BIT_ID_TYPE tid_bits;
   3352 
   3353 
   3354 	dvc_type = inq->byte0.peri_dvc_type;
   3355 	tid_bits = ASC_TIX_TO_TARGET_ID(tid_no);
   3356 
   3357 	if (sc->bug_fix_cntl & ASC_BUG_FIX_ASYN_USE_SYN) {
   3358 		if (!(sc->init_sdtr & tid_bits)) {
   3359 			if ((dvc_type == SCSI_TYPE_CDROM) &&
   3360 			(AscCompareString(inq->vendor_id, "HP ", 3) == 0)) {
   3361 				sc->pci_fix_asyn_xfer_always |= tid_bits;
   3362 			}
   3363 			sc->pci_fix_asyn_xfer |= tid_bits;
   3364 			if ((dvc_type == SCSI_TYPE_PROC) ||
   3365 			    (dvc_type == SCSI_TYPE_SCANNER)) {
   3366 				sc->pci_fix_asyn_xfer &= ~tid_bits;
   3367 			}
   3368 			if ((dvc_type == SCSI_TYPE_SASD) &&
   3369 			    (AscCompareString(inq->vendor_id, "TANDBERG", 8) == 0) &&
   3370 			    (AscCompareString(inq->product_id, " TDC 36", 7) == 0)) {
   3371 				sc->pci_fix_asyn_xfer &= ~tid_bits;
   3372 			}
   3373 			if ((dvc_type == SCSI_TYPE_SASD) &&
   3374 			    (AscCompareString(inq->vendor_id, "WANGTEK ", 8) == 0)) {
   3375 				sc->pci_fix_asyn_xfer &= ~tid_bits;
   3376 			}
   3377 			if ((dvc_type == SCSI_TYPE_CDROM) &&
   3378 			    (AscCompareString(inq->vendor_id, "NEC	 ", 8) == 0) &&
   3379 			    (AscCompareString(inq->product_id, "CD-ROM DRIVE	", 16) == 0)) {
   3380 				sc->pci_fix_asyn_xfer &= ~tid_bits;
   3381 			}
   3382 			if ((dvc_type == SCSI_TYPE_CDROM) &&
   3383 			    (AscCompareString(inq->vendor_id, "YAMAHA", 6) == 0) &&
   3384 			    (AscCompareString(inq->product_id, "CDR400", 6) == 0)) {
   3385 				sc->pci_fix_asyn_xfer &= ~tid_bits;
   3386 			}
   3387 			if (sc->pci_fix_asyn_xfer & tid_bits) {
   3388 				AscSetRunChipSynRegAtID(sc->sc_iot, sc->sc_ioh, tid_no,
   3389 					     ASYN_SDTR_DATA_FIX_PCI_REV_AB);
   3390 			}
   3391 		}
   3392 	}
   3393 }
   3394 
   3395 
   3396 /******************************************************************************/
   3397 /*                              Miscellaneous routines                        */
   3398 /******************************************************************************/
   3399 
   3400 
   3401 static int
   3402 AscCompareString(str1, str2, len)
   3403 	u_char         *str1;
   3404 	u_char         *str2;
   3405 	int             len;
   3406 {
   3407 	int             i;
   3408 	int             diff;
   3409 
   3410 	for (i = 0; i < len; i++) {
   3411 		diff = (int) (str1[i] - str2[i]);
   3412 		if (diff != 0)
   3413 			return (diff);
   3414 	}
   3415 
   3416 	return (0);
   3417 }
   3418 
   3419 
   3420 /******************************************************************************/
   3421 /*                            Device oriented routines                        */
   3422 /******************************************************************************/
   3423 
   3424 
   3425 static int
   3426 DvcEnterCritical(void)
   3427 {
   3428 	int             s;
   3429 
   3430 	s = splbio();
   3431 	return (s);
   3432 }
   3433 
   3434 
   3435 static void
   3436 DvcLeaveCritical(s)
   3437 	int             s;
   3438 {
   3439 
   3440 	splx(s);
   3441 }
   3442 
   3443 
   3444 static void
   3445 DvcSleepMilliSecond(n)
   3446 	u_int32_t       n;
   3447 {
   3448 
   3449 	DELAY(n * 1000);
   3450 }
   3451 
   3452 #ifdef UNUSED
   3453 static void
   3454 DvcDelayMicroSecond(n)
   3455 	u_int32_t       n;
   3456 {
   3457 
   3458 	DELAY(n);
   3459 }
   3460 #endif
   3461 
   3462 static void
   3463 DvcDelayNanoSecond(n)
   3464 	u_int32_t       n;
   3465 {
   3466 
   3467 	DELAY((n + 999) / 1000);
   3468 }
   3469