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