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