Home | History | Annotate | Line # | Download | only in ic
ncr53c9x.c revision 1.44
      1 /*	$NetBSD: ncr53c9x.c,v 1.44 2000/03/19 21:25:49 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1994 Peter Galbavy
     41  * Copyright (c) 1995 Paul Kranenburg
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Peter Galbavy
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     60  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     61  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     62  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     63  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     64  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     66  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     67  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68  * POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Based on aic6360 by Jarle Greipsland
     73  *
     74  * Acknowledgements: Many of the algorithms used in this driver are
     75  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     76  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     77  */
     78 
     79 #include <sys/types.h>
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/kernel.h>
     83 #include <sys/errno.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/device.h>
     86 #include <sys/buf.h>
     87 #include <sys/malloc.h>
     88 #include <sys/proc.h>
     89 #include <sys/user.h>
     90 #include <sys/queue.h>
     91 
     92 #include <dev/scsipi/scsi_all.h>
     93 #include <dev/scsipi/scsipi_all.h>
     94 #include <dev/scsipi/scsiconf.h>
     95 #include <dev/scsipi/scsi_message.h>
     96 
     97 #include <machine/cpu.h>
     98 
     99 #include <dev/ic/ncr53c9xreg.h>
    100 #include <dev/ic/ncr53c9xvar.h>
    101 
    102 int ncr53c9x_debug = 0; /*NCR_SHOWPHASE|NCR_SHOWMISC|NCR_SHOWTRAC|NCR_SHOWCMDS;*/
    103 
    104 /*static*/ void	ncr53c9x_readregs	__P((struct ncr53c9x_softc *));
    105 /*static*/ void	ncr53c9x_select		__P((struct ncr53c9x_softc *,
    106 					    struct ncr53c9x_ecb *));
    107 /*static*/ int ncr53c9x_reselect	__P((struct ncr53c9x_softc *, int));
    108 /*static*/ void	ncr53c9x_scsi_reset	__P((struct ncr53c9x_softc *));
    109 /*static*/ int	ncr53c9x_poll		__P((struct ncr53c9x_softc *,
    110 					    struct scsipi_xfer *, int));
    111 /*static*/ void	ncr53c9x_sched		__P((struct ncr53c9x_softc *));
    112 /*static*/ void	ncr53c9x_done		__P((struct ncr53c9x_softc *,
    113 					    struct ncr53c9x_ecb *));
    114 /*static*/ void	ncr53c9x_msgin		__P((struct ncr53c9x_softc *));
    115 /*static*/ void	ncr53c9x_msgout		__P((struct ncr53c9x_softc *));
    116 /*static*/ void	ncr53c9x_timeout	__P((void *arg));
    117 /*static*/ void	ncr53c9x_abort		__P((struct ncr53c9x_softc *,
    118 					    struct ncr53c9x_ecb *));
    119 /*static*/ void ncr53c9x_dequeue	__P((struct ncr53c9x_softc *,
    120 					    struct ncr53c9x_ecb *));
    121 
    122 void ncr53c9x_sense			__P((struct ncr53c9x_softc *,
    123 					    struct ncr53c9x_ecb *));
    124 void ncr53c9x_free_ecb			__P((struct ncr53c9x_softc *,
    125 					    struct ncr53c9x_ecb *, int));
    126 struct ncr53c9x_ecb *ncr53c9x_get_ecb	__P((struct ncr53c9x_softc *, int));
    127 
    128 static inline int ncr53c9x_stp2cpb	__P((struct ncr53c9x_softc *, int));
    129 static inline void ncr53c9x_setsync	__P((struct ncr53c9x_softc *,
    130 					    struct ncr53c9x_tinfo *));
    131 
    132 /*
    133  * Names for the NCR53c9x variants, correspnding to the variant tags
    134  * in ncr53c9xvar.h.
    135  */
    136 const char *ncr53c9x_variant_names[] = {
    137 	"ESP100",
    138 	"ESP100A",
    139 	"ESP200",
    140 	"NCR53C94",
    141 	"NCR53C96",
    142 	"ESP406",
    143 	"FAS408",
    144 	"FAS216",
    145 	"AM53C974",
    146 };
    147 
    148 /*
    149  * Attach this instance, and then all the sub-devices
    150  */
    151 void
    152 ncr53c9x_attach(sc, dev)
    153 	struct ncr53c9x_softc *sc;
    154 	struct scsipi_device *dev;
    155 {
    156 
    157 	/*
    158 	 * Allocate SCSI message buffers.
    159 	 * Front-ends can override allocation to avoid alignment
    160 	 * handling in the DMA engines. Note that that ncr53c9x_msgout()
    161 	 * can request a 1 byte DMA transfer.
    162 	 */
    163 	if (sc->sc_omess == NULL)
    164 		sc->sc_omess = malloc(NCR_MAX_MSG_LEN, M_DEVBUF, M_NOWAIT);
    165 
    166 	if (sc->sc_imess == NULL)
    167 		sc->sc_imess = malloc(NCR_MAX_MSG_LEN+1, M_DEVBUF, M_NOWAIT);
    168 
    169 	if (sc->sc_omess == NULL || sc->sc_imess == NULL) {
    170 		printf("out of memory\n");
    171 		return;
    172 	}
    173 
    174 	/*
    175 	 * Note, the front-end has set us up to print the chip variation.
    176 	 */
    177 	if (sc->sc_rev >= NCR_VARIANT_MAX) {
    178 		printf("\n%s: unknown variant %d, devices not attached\n",
    179 		    sc->sc_dev.dv_xname, sc->sc_rev);
    180 		return;
    181 	}
    182 
    183 	printf(": %s, %dMHz, SCSI ID %d\n",
    184 	    ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id);
    185 
    186 	sc->sc_ccf = FREQTOCCF(sc->sc_freq);
    187 
    188 	/* The value *must not* be == 1. Make it 2 */
    189 	if (sc->sc_ccf == 1)
    190 		sc->sc_ccf = 2;
    191 
    192 	/*
    193 	 * The recommended timeout is 250ms. This register is loaded
    194 	 * with a value calculated as follows, from the docs:
    195 	 *
    196 	 *		(timout period) x (CLK frequency)
    197 	 *	reg = -------------------------------------
    198 	 *		 8192 x (Clock Conversion Factor)
    199 	 *
    200 	 * Since CCF has a linear relation to CLK, this generally computes
    201 	 * to the constant of 153.
    202 	 */
    203 	sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
    204 
    205 	/* CCF register only has 3 bits; 0 is actually 8 */
    206 	sc->sc_ccf &= 7;
    207 
    208 	/*
    209 	 * fill in the prototype scsipi_link.
    210 	 */
    211 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    212 	sc->sc_link.adapter_softc = sc;
    213 	sc->sc_link.scsipi_scsi.adapter_target = sc->sc_id;
    214 	sc->sc_link.adapter = &sc->sc_adapter;
    215 	sc->sc_link.device = dev;
    216 	sc->sc_link.openings = 2;
    217 	sc->sc_link.scsipi_scsi.max_target = 7;
    218 	sc->sc_link.scsipi_scsi.max_lun = 7;
    219 	sc->sc_link.type = BUS_SCSI;
    220 
    221 	/* Initialize CFG4 and CFG5, and disable interrupts. */
    222 	if ((sc->sc_rev == NCR_VARIANT_ESP406) ||
    223 	    (sc->sc_rev == NCR_VARIANT_FAS408)) {
    224 		NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5);
    225 		NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4);
    226 	}
    227 
    228 	/*
    229 	 * Add reference to adapter so that we drop the reference after
    230 	 * config_found() to make sure the adatper is disabled.
    231 	 */
    232 	if (scsipi_adapter_addref(&sc->sc_link) != 0) {
    233 		printf("%s: unable to enable controller\n",
    234 		    sc->sc_dev.dv_xname);
    235 		return;
    236 	}
    237 
    238 	/* Reset state & bus */
    239 	sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
    240 	sc->sc_state = 0;
    241 	ncr53c9x_init(sc, 1);
    242 
    243 	/*
    244 	 * Now try to attach all the sub-devices
    245 	 */
    246 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    247 
    248 	scsipi_adapter_delref(&sc->sc_link);
    249 
    250 	/* Enable interrupts. */
    251 	if ((sc->sc_rev == NCR_VARIANT_ESP406) ||
    252 	    (sc->sc_rev == NCR_VARIANT_FAS408)) {
    253 		NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5 | NCRCFG5_SINT);
    254 		NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4);
    255 	}
    256 }
    257 
    258 int
    259 ncr53c9x_detach(sc, flags)
    260 	struct ncr53c9x_softc *sc;
    261 	int flags;
    262 {
    263 	int error;
    264 
    265 	if (sc->sc_child) {
    266 		error = config_detach(sc->sc_child, flags);
    267 		if (error)
    268 			return (error);
    269 	}
    270 
    271 	free(sc->sc_imess, M_DEVBUF);
    272 	free(sc->sc_omess, M_DEVBUF);
    273 
    274 	return (0);
    275 }
    276 
    277 /*
    278  * This is the generic ncr53c9x reset function. It does not reset the SCSI bus,
    279  * only this controller, but kills any on-going commands, and also stops
    280  * and resets the DMA.
    281  *
    282  * After reset, registers are loaded with the defaults from the attach
    283  * routine above.
    284  */
    285 void
    286 ncr53c9x_reset(sc)
    287 	struct ncr53c9x_softc *sc;
    288 {
    289 
    290 	/* reset DMA first */
    291 	NCRDMA_RESET(sc);
    292 
    293 	/* reset SCSI chip */
    294 	NCRCMD(sc, NCRCMD_RSTCHIP);
    295 	NCRCMD(sc, NCRCMD_NOP);
    296 	DELAY(500);
    297 
    298 	/* do these backwards, and fall through */
    299 	switch (sc->sc_rev) {
    300 	case NCR_VARIANT_ESP406:
    301 	case NCR_VARIANT_FAS408:
    302 		NCR_SCSIREGS(sc);
    303 	case NCR_VARIANT_AM53C974:
    304 	case NCR_VARIANT_FAS216:
    305 	case NCR_VARIANT_NCR53C94:
    306 	case NCR_VARIANT_NCR53C96:
    307 	case NCR_VARIANT_ESP200:
    308 		sc->sc_features |= NCR_F_HASCFG3;
    309 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    310 	case NCR_VARIANT_ESP100A:
    311 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    312 	case NCR_VARIANT_ESP100:
    313 		NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
    314 		NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
    315 		NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
    316 		NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
    317 		break;
    318 	default:
    319 		printf("%s: unknown revision code, assuming ESP100\n",
    320 		    sc->sc_dev.dv_xname);
    321 		NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
    322 		NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
    323 		NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
    324 		NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
    325 	}
    326 
    327 	if (sc->sc_rev == NCR_VARIANT_AM53C974)
    328 		NCR_WRITE_REG(sc, NCR_AMDCFG4,
    329 		    NCRAMDCFG4_GE12NS | NCRAMDCFG4_RADE);
    330 }
    331 
    332 /*
    333  * Reset the SCSI bus, but not the chip
    334  */
    335 void
    336 ncr53c9x_scsi_reset(sc)
    337 	struct ncr53c9x_softc *sc;
    338 {
    339 
    340 	(*sc->sc_glue->gl_dma_stop)(sc);
    341 
    342 	printf("%s: resetting SCSI bus\n", sc->sc_dev.dv_xname);
    343 	NCRCMD(sc, NCRCMD_RSTSCSI);
    344 }
    345 
    346 /*
    347  * Initialize ncr53c9x state machine
    348  */
    349 void
    350 ncr53c9x_init(sc, doreset)
    351 	struct ncr53c9x_softc *sc;
    352 	int doreset;
    353 {
    354 	struct ncr53c9x_ecb *ecb;
    355 	int r;
    356 
    357 	NCR_TRACE(("[NCR_INIT(%d)] ", doreset));
    358 
    359 	if (sc->sc_state == 0) {
    360 		/* First time through; initialize. */
    361 		TAILQ_INIT(&sc->ready_list);
    362 		TAILQ_INIT(&sc->nexus_list);
    363 		TAILQ_INIT(&sc->free_list);
    364 		sc->sc_nexus = NULL;
    365 		ecb = sc->sc_ecb;
    366 		bzero(ecb, sizeof(sc->sc_ecb));
    367 		for (r = 0; r < sizeof(sc->sc_ecb) / sizeof(*ecb); r++) {
    368 			TAILQ_INSERT_TAIL(&sc->free_list, ecb, chain);
    369 			ecb++;
    370 		}
    371 		bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
    372 	} else {
    373 		/* Cancel any active commands. */
    374 		sc->sc_state = NCR_CLEANING;
    375 		if ((ecb = sc->sc_nexus) != NULL) {
    376 			ecb->xs->error = XS_TIMEOUT;
    377 			ncr53c9x_done(sc, ecb);
    378 		}
    379 		while ((ecb = sc->nexus_list.tqh_first) != NULL) {
    380 			ecb->xs->error = XS_TIMEOUT;
    381 			ncr53c9x_done(sc, ecb);
    382 		}
    383 	}
    384 
    385 	/*
    386 	 * reset the chip to a known state
    387 	 */
    388 	ncr53c9x_reset(sc);
    389 
    390 	sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
    391 	for (r = 0; r < 8; r++) {
    392 		struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r];
    393 /* XXX - config flags per target: low bits: no reselect; high bits: no synch */
    394 
    395 		ti->flags = ((sc->sc_minsync && !(sc->sc_cfflags & (1<<(r+8))))
    396 				? T_NEGOTIATE : 0) |
    397 				((sc->sc_cfflags & (1<<r)) ? T_RSELECTOFF : 0) |
    398 				T_NEED_TO_RESET;
    399 		ti->period = sc->sc_minsync;
    400 		ti->offset = 0;
    401 	}
    402 
    403 	if (doreset) {
    404 		sc->sc_state = NCR_SBR;
    405 		NCRCMD(sc, NCRCMD_RSTSCSI);
    406 	} else {
    407 		sc->sc_state = NCR_IDLE;
    408 		ncr53c9x_sched(sc);
    409 	}
    410 }
    411 
    412 /*
    413  * Read the NCR registers, and save their contents for later use.
    414  * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
    415  * NCR_INTR - so make sure it is the last read.
    416  *
    417  * I think that (from reading the docs) most bits in these registers
    418  * only make sense when he DMA CSR has an interrupt showing. Call only
    419  * if an interrupt is pending.
    420  */
    421 __inline__ void
    422 ncr53c9x_readregs(sc)
    423 	struct ncr53c9x_softc *sc;
    424 {
    425 
    426 	sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
    427 	/* Only the stepo bits are of interest */
    428 	sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
    429 	sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
    430 
    431 	if (sc->sc_glue->gl_clear_latched_intr != NULL)
    432 		(*sc->sc_glue->gl_clear_latched_intr)(sc);
    433 
    434 	/*
    435 	 * Determine the SCSI bus phase, return either a real SCSI bus phase
    436 	 * or some pseudo phase we use to detect certain exceptions.
    437 	 */
    438 
    439 	sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS)
    440 			? /* Disconnected */ BUSFREE_PHASE
    441 			: sc->sc_espstat & NCRSTAT_PHASE;
    442 
    443 	NCR_MISC(("regs[intr=%02x,stat=%02x,step=%02x] ",
    444 		sc->sc_espintr, sc->sc_espstat, sc->sc_espstep));
    445 }
    446 
    447 /*
    448  * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
    449  */
    450 static inline int
    451 ncr53c9x_stp2cpb(sc, period)
    452 	struct ncr53c9x_softc *sc;
    453 	int period;
    454 {
    455 	int v;
    456 	v = (sc->sc_freq * period) / 250;
    457 	if (ncr53c9x_cpb2stp(sc, v) < period)
    458 		/* Correct round-down error */
    459 		v++;
    460 	return (v);
    461 }
    462 
    463 static inline void
    464 ncr53c9x_setsync(sc, ti)
    465 	struct ncr53c9x_softc *sc;
    466 	struct ncr53c9x_tinfo *ti;
    467 {
    468 	u_char syncoff, synctp, cfg3 = sc->sc_cfg3;
    469 
    470 	if (ti->flags & T_SYNCMODE) {
    471 		syncoff = ti->offset;
    472 		synctp = ncr53c9x_stp2cpb(sc, ti->period);
    473 		if (sc->sc_features & NCR_F_FASTSCSI) {
    474 			/*
    475 			 * If the period is 200ns or less (ti->period <= 50),
    476 			 * put the chip in Fast SCSI mode.
    477 			 */
    478 			if (ti->period <= 50)
    479 				/*
    480 				 * There are (at least) 4 variations of the
    481 				 * configuration 3 register.  The drive attach
    482 				 * routine sets the appropriate bit to put the
    483 				 * chip into Fast SCSI mode so that it doesn't
    484 				 * have to be figured out here each time.
    485 				 */
    486 				cfg3 |= sc->sc_cfg3_fscsi;
    487 		}
    488 
    489 		/*
    490 		 * Am53c974 requires different SYNCTP values when the
    491 		 * FSCSI bit is off.
    492 		 */
    493 		if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
    494 		    (cfg3 & NCRAMDCFG3_FSCSI) == 0)
    495 			synctp--;
    496 	} else {
    497 		syncoff = 0;
    498 		synctp = 0;
    499 	}
    500 
    501 	if (sc->sc_features & NCR_F_HASCFG3)
    502 		NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
    503 
    504 	NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
    505 	NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
    506 }
    507 
    508 int ncr53c9x_dmaselect = 0;
    509 /*
    510  * Send a command to a target, set the driver state to NCR_SELECTING
    511  * and let the caller take care of the rest.
    512  *
    513  * Keeping this as a function allows me to say that this may be done
    514  * by DMA instead of programmed I/O soon.
    515  */
    516 void
    517 ncr53c9x_select(sc, ecb)
    518 	struct ncr53c9x_softc *sc;
    519 	struct ncr53c9x_ecb *ecb;
    520 {
    521 	struct scsipi_link *sc_link = ecb->xs->sc_link;
    522 	int target = sc_link->scsipi_scsi.target;
    523 	int lun = sc_link->scsipi_scsi.lun;
    524 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
    525 	int tiflags = ti->flags;
    526 	u_char *cmd;
    527 	int clen;
    528 	size_t dmasize;
    529 
    530 	NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x)] ",
    531 		   target, lun, ecb->cmd.cmd.opcode));
    532 
    533 	sc->sc_state = NCR_SELECTING;
    534 
    535 	/*
    536 	 * Schedule the timeout now, the first time we will go away
    537 	 * expecting to come back due to an interrupt, because it is
    538 	 * always possible that the interrupt may never happen.
    539 	 */
    540 	if ((ecb->xs->xs_control & XS_CTL_POLL) == 0)
    541 		timeout(ncr53c9x_timeout, ecb,
    542 		    (ecb->timeout * hz) / 1000);
    543 
    544 	/*
    545 	 * The docs say the target register is never reset, and I
    546 	 * can't think of a better place to set it
    547 	 */
    548 	NCR_WRITE_REG(sc, NCR_SELID, target);
    549 	ncr53c9x_setsync(sc, ti);
    550 
    551 	if (ecb->flags & ECB_SENSE) {
    552 		/*
    553 		 * For REQUEST SENSE, we should not send an IDENTIFY or
    554 		 * otherwise mangle the target.  There should be no MESSAGE IN
    555 		 * phase.
    556 		 */
    557 		if (ncr53c9x_dmaselect) {
    558 			/* setup DMA transfer for command */
    559 			dmasize = clen = ecb->clen;
    560 			sc->sc_cmdlen = clen;
    561 			sc->sc_cmdp = (caddr_t)&ecb->cmd + 1;
    562 			NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
    563 
    564 			/* Program the SCSI counter */
    565 			NCR_WRITE_REG(sc, NCR_TCL, dmasize);
    566 			NCR_WRITE_REG(sc, NCR_TCM, dmasize >> 8);
    567 			if (sc->sc_cfg2 & NCRCFG2_FE) {
    568 				NCR_WRITE_REG(sc, NCR_TCH, dmasize >> 16);
    569 			}
    570 
    571 			/* load the count in */
    572 			NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
    573 
    574 			/* And get the targets attention */
    575 			NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
    576 			NCRDMA_GO(sc);
    577 		} else {
    578 			/* Now the command into the FIFO */
    579 			cmd = (u_char *)&ecb->cmd.cmd;
    580 			clen = ecb->clen;
    581 			while (clen--)
    582 				NCR_WRITE_REG(sc, NCR_FIFO, *cmd++);
    583 
    584 			NCRCMD(sc, NCRCMD_SELNATN);
    585 		}
    586 		return;
    587 	}
    588 
    589 	if (ncr53c9x_dmaselect && (tiflags & T_NEGOTIATE) == 0) {
    590 		ecb->cmd.id =
    591 		    MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF)?0:1);
    592 
    593 		/* setup DMA transfer for command */
    594 		dmasize = clen = ecb->clen + 1;
    595 		sc->sc_cmdlen = clen;
    596 		sc->sc_cmdp = (caddr_t)&ecb->cmd;
    597 		NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
    598 
    599 		/* Program the SCSI counter */
    600 		NCR_WRITE_REG(sc, NCR_TCL, dmasize);
    601 		NCR_WRITE_REG(sc, NCR_TCM, dmasize >> 8);
    602 		if (sc->sc_cfg2 & NCRCFG2_FE) {
    603 			NCR_WRITE_REG(sc, NCR_TCH, dmasize >> 16);
    604 		}
    605 
    606 		/* load the count in */
    607 		NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
    608 
    609 		/* And get the targets attention */
    610 		NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
    611 		NCRDMA_GO(sc);
    612 		return;
    613 	}
    614 
    615 	/*
    616 	 * Who am I. This is where we tell the target that we are
    617 	 * happy for it to disconnect etc.
    618 	 */
    619 	NCR_WRITE_REG(sc, NCR_FIFO,
    620 		      MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF)?0:1));
    621 
    622 	if (ti->flags & T_NEGOTIATE) {
    623 		/* Arbitrate, select and stop after IDENTIFY message */
    624 		NCRCMD(sc, NCRCMD_SELATNS);
    625 		return;
    626 	}
    627 
    628 	/* Now the command into the FIFO */
    629 	cmd = (u_char *)&ecb->cmd.cmd;
    630 	clen = ecb->clen;
    631 	while (clen--)
    632 		NCR_WRITE_REG(sc, NCR_FIFO, *cmd++);
    633 
    634 	/* And get the targets attention */
    635 	NCRCMD(sc, NCRCMD_SELATN);
    636 }
    637 
    638 void
    639 ncr53c9x_free_ecb(sc, ecb, flags)
    640 	struct ncr53c9x_softc *sc;
    641 	struct ncr53c9x_ecb *ecb;
    642 	int flags;
    643 {
    644 	int s;
    645 
    646 	s = splbio();
    647 
    648 	ecb->flags = 0;
    649 	TAILQ_INSERT_HEAD(&sc->free_list, ecb, chain);
    650 
    651 	/*
    652 	 * If there were none, wake anybody waiting for one to come free,
    653 	 * starting with queued entries.
    654 	 */
    655 	if (ecb->chain.tqe_next == 0)
    656 		wakeup(&sc->free_list);
    657 
    658 	splx(s);
    659 }
    660 
    661 struct ncr53c9x_ecb *
    662 ncr53c9x_get_ecb(sc, flags)
    663 	struct ncr53c9x_softc *sc;
    664 	int flags;
    665 {
    666 	struct ncr53c9x_ecb *ecb;
    667 	int s;
    668 
    669 	s = splbio();
    670 
    671 	while ((ecb = sc->free_list.tqh_first) == NULL &&
    672 	       (flags & XS_CTL_NOSLEEP) == 0)
    673 		tsleep(&sc->free_list, PRIBIO, "especb", 0);
    674 	if (ecb) {
    675 		TAILQ_REMOVE(&sc->free_list, ecb, chain);
    676 		ecb->flags |= ECB_ALLOC;
    677 	}
    678 
    679 	splx(s);
    680 	return (ecb);
    681 }
    682 
    683 /*
    684  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    685  */
    686 
    687 /*
    688  * Start a SCSI-command
    689  * This function is called by the higher level SCSI-driver to queue/run
    690  * SCSI-commands.
    691  */
    692 int
    693 ncr53c9x_scsi_cmd(xs)
    694 	struct scsipi_xfer *xs;
    695 {
    696 	struct scsipi_link *sc_link = xs->sc_link;
    697 	struct ncr53c9x_softc *sc = sc_link->adapter_softc;
    698 	struct ncr53c9x_ecb *ecb;
    699 	int s, flags;
    700 
    701 	NCR_TRACE(("[ncr53c9x_scsi_cmd] "));
    702 	NCR_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    703 	    sc_link->scsipi_scsi.target));
    704 
    705 	flags = xs->xs_control;
    706 	if ((ecb = ncr53c9x_get_ecb(sc, flags)) == NULL)
    707 		return (TRY_AGAIN_LATER);
    708 
    709 	/* Initialize ecb */
    710 	ecb->xs = xs;
    711 	ecb->timeout = xs->timeout;
    712 
    713 	if (flags & XS_CTL_RESET) {
    714 		ecb->flags |= ECB_RESET;
    715 		ecb->clen = 0;
    716 		ecb->dleft = 0;
    717 	} else {
    718 		bcopy(xs->cmd, &ecb->cmd.cmd, xs->cmdlen);
    719 		ecb->clen = xs->cmdlen;
    720 		ecb->daddr = xs->data;
    721 		ecb->dleft = xs->datalen;
    722 	}
    723 	ecb->stat = 0;
    724 
    725 	s = splbio();
    726 
    727 	TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
    728 	if (sc->sc_state == NCR_IDLE)
    729 		ncr53c9x_sched(sc);
    730 
    731 	splx(s);
    732 
    733 	if ((flags & XS_CTL_POLL) == 0)
    734 		return (SUCCESSFULLY_QUEUED);
    735 
    736 	/* Not allowed to use interrupts, use polling instead */
    737 	if (ncr53c9x_poll(sc, xs, ecb->timeout)) {
    738 		ncr53c9x_timeout(ecb);
    739 		if (ncr53c9x_poll(sc, xs, ecb->timeout))
    740 			ncr53c9x_timeout(ecb);
    741 	}
    742 	return (COMPLETE);
    743 }
    744 
    745 /*
    746  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    747  */
    748 int
    749 ncr53c9x_poll(sc, xs, count)
    750 	struct ncr53c9x_softc *sc;
    751 	struct scsipi_xfer *xs;
    752 	int count;
    753 {
    754 
    755 	NCR_TRACE(("[ncr53c9x_poll] "));
    756 	while (count) {
    757 		if (NCRDMA_ISINTR(sc)) {
    758 			ncr53c9x_intr(sc);
    759 		}
    760 #if alternatively
    761 		if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT)
    762 			ncr53c9x_intr(sc);
    763 #endif
    764 		if ((xs->xs_status & XS_STS_DONE) != 0)
    765 			return (0);
    766 		if (sc->sc_state == NCR_IDLE) {
    767 			NCR_TRACE(("[ncr53c9x_poll: rescheduling] "));
    768 			ncr53c9x_sched(sc);
    769 		}
    770 		DELAY(1000);
    771 		count--;
    772 	}
    773 	return (1);
    774 }
    775 
    776 
    777 /*
    778  * LOW LEVEL SCSI UTILITIES
    779  */
    780 
    781 /*
    782  * Schedule a scsi operation.  This has now been pulled out of the interrupt
    783  * handler so that we may call it from ncr53c9x_scsi_cmd and ncr53c9x_done.
    784  * This may save us an unecessary interrupt just to get things going.
    785  * Should only be called when state == NCR_IDLE and at bio pl.
    786  */
    787 void
    788 ncr53c9x_sched(sc)
    789 	struct ncr53c9x_softc *sc;
    790 {
    791 	struct ncr53c9x_ecb *ecb;
    792 	struct scsipi_link *sc_link;
    793 	struct ncr53c9x_tinfo *ti;
    794 
    795 	NCR_TRACE(("[ncr53c9x_sched] "));
    796 	if (sc->sc_state != NCR_IDLE)
    797 		panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
    798 
    799 	/*
    800 	 * Find first ecb in ready queue that is for a target/lunit
    801 	 * combinations that is not busy.
    802 	 */
    803 	for (ecb = sc->ready_list.tqh_first; ecb; ecb = ecb->chain.tqe_next) {
    804 		sc_link = ecb->xs->sc_link;
    805 		ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
    806 		if ((ti->lubusy & (1 << sc_link->scsipi_scsi.lun)) == 0) {
    807 			TAILQ_REMOVE(&sc->ready_list, ecb, chain);
    808 			sc->sc_nexus = ecb;
    809 			ncr53c9x_select(sc, ecb);
    810 			break;
    811 		} else
    812 			NCR_MISC(("%d:%d busy\n",
    813 				  sc_link->scsipi_scsi.target,
    814 				  sc_link->scsipi_scsi.lun));
    815 	}
    816 }
    817 
    818 void
    819 ncr53c9x_sense(sc, ecb)
    820 	struct ncr53c9x_softc *sc;
    821 	struct ncr53c9x_ecb *ecb;
    822 {
    823 	struct scsipi_xfer *xs = ecb->xs;
    824 	struct scsipi_link *sc_link = xs->sc_link;
    825 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
    826 	struct scsipi_sense *ss = (void *)&ecb->cmd.cmd;
    827 
    828 	NCR_MISC(("requesting sense "));
    829 	/* Next, setup a request sense command block */
    830 	bzero(ss, sizeof(*ss));
    831 	ss->opcode = REQUEST_SENSE;
    832 	ss->byte2 = sc_link->scsipi_scsi.lun << 5;
    833 	ss->length = sizeof(struct scsipi_sense_data);
    834 	ecb->clen = sizeof(*ss);
    835 	ecb->daddr = (char *)&xs->sense.scsi_sense;
    836 	ecb->dleft = sizeof(struct scsipi_sense_data);
    837 	ecb->flags |= ECB_SENSE;
    838 	ecb->timeout = NCR_SENSE_TIMEOUT;
    839 	ti->senses++;
    840 	if (ecb->flags & ECB_NEXUS)
    841 		ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
    842 	if (ecb == sc->sc_nexus) {
    843 		ecb->flags &= ~ECB_NEXUS;
    844 		ncr53c9x_select(sc, ecb);
    845 	} else {
    846 		ncr53c9x_dequeue(sc, ecb);
    847 		TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
    848 		if (sc->sc_state == NCR_IDLE)
    849 			ncr53c9x_sched(sc);
    850 	}
    851 }
    852 
    853 /*
    854  * POST PROCESSING OF SCSI_CMD (usually current)
    855  */
    856 void
    857 ncr53c9x_done(sc, ecb)
    858 	struct ncr53c9x_softc *sc;
    859 	struct ncr53c9x_ecb *ecb;
    860 {
    861 	struct scsipi_xfer *xs = ecb->xs;
    862 	struct scsipi_link *sc_link = xs->sc_link;
    863 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
    864 
    865 	NCR_TRACE(("[ncr53c9x_done(error:%x)] ", xs->error));
    866 
    867 	untimeout(ncr53c9x_timeout, ecb);
    868 
    869 	/*
    870 	 * Now, if we've come here with no error code, i.e. we've kept the
    871 	 * initial XS_NOERROR, and the status code signals that we should
    872 	 * check sense, we'll need to set up a request sense cmd block and
    873 	 * push the command back into the ready queue *before* any other
    874 	 * commands for this target/lunit, else we lose the sense info.
    875 	 * We don't support chk sense conditions for the request sense cmd.
    876 	 */
    877 	if (xs->error == XS_NOERROR) {
    878 		xs->status = ecb->stat;
    879 		if ((ecb->flags & ECB_ABORT) != 0) {
    880 			xs->error = XS_TIMEOUT;
    881 		} else if ((ecb->flags & ECB_SENSE) != 0) {
    882 			xs->error = XS_SENSE;
    883 		} else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
    884 			/* First, save the return values */
    885 			xs->resid = ecb->dleft;
    886 			ncr53c9x_sense(sc, ecb);
    887 			return;
    888 		} else {
    889 			xs->resid = ecb->dleft;
    890 		}
    891 	}
    892 
    893 	xs->xs_status |= XS_STS_DONE;
    894 
    895 #ifdef NCR53C9X_DEBUG
    896 	if (ncr53c9x_debug & NCR_SHOWMISC) {
    897 		if (xs->resid != 0)
    898 			printf("resid=%d ", xs->resid);
    899 		if (xs->error == XS_SENSE)
    900 			printf("sense=0x%02x\n", xs->sense.scsi_sense.error_code);
    901 		else
    902 			printf("error=%d\n", xs->error);
    903 	}
    904 #endif
    905 
    906 	/*
    907 	 * Remove the ECB from whatever queue it's on.
    908 	 */
    909 	if (ecb->flags & ECB_NEXUS)
    910 		ti->lubusy &= ~(1 << sc_link->scsipi_scsi.lun);
    911 	if (ecb == sc->sc_nexus) {
    912 		sc->sc_nexus = NULL;
    913 		if (sc->sc_state != NCR_CLEANING) {
    914 			sc->sc_state = NCR_IDLE;
    915 			ncr53c9x_sched(sc);
    916 		}
    917 	} else
    918 		ncr53c9x_dequeue(sc, ecb);
    919 
    920 	ncr53c9x_free_ecb(sc, ecb, xs->xs_control);
    921 	ti->cmds++;
    922 	scsipi_done(xs);
    923 }
    924 
    925 void
    926 ncr53c9x_dequeue(sc, ecb)
    927 	struct ncr53c9x_softc *sc;
    928 	struct ncr53c9x_ecb *ecb;
    929 {
    930 
    931 	if (ecb->flags & ECB_NEXUS) {
    932 		TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
    933 		ecb->flags &= ~ECB_NEXUS;
    934 	} else {
    935 		TAILQ_REMOVE(&sc->ready_list, ecb, chain);
    936 	}
    937 }
    938 
    939 /*
    940  * INTERRUPT/PROTOCOL ENGINE
    941  */
    942 
    943 /*
    944  * Schedule an outgoing message by prioritizing it, and asserting
    945  * attention on the bus. We can only do this when we are the initiator
    946  * else there will be an illegal command interrupt.
    947  */
    948 #define ncr53c9x_sched_msgout(m) \
    949 	do {							\
    950 		NCR_MISC(("ncr53c9x_sched_msgout %d ", m));	\
    951 		NCRCMD(sc, NCRCMD_SETATN);			\
    952 		sc->sc_flags |= NCR_ATN;			\
    953 		sc->sc_msgpriq |= (m);				\
    954 	} while (0)
    955 
    956 int
    957 ncr53c9x_reselect(sc, message)
    958 	struct ncr53c9x_softc *sc;
    959 	int message;
    960 {
    961 	u_char selid, target, lun;
    962 	struct ncr53c9x_ecb *ecb;
    963 	struct scsipi_link *sc_link;
    964 	struct ncr53c9x_tinfo *ti;
    965 
    966 	/*
    967 	 * The SCSI chip made a snapshot of the data bus while the reselection
    968 	 * was being negotiated.  This enables us to determine which target did
    969 	 * the reselect.
    970 	 */
    971 	selid = sc->sc_selid & ~(1 << sc->sc_id);
    972 	if (selid & (selid - 1)) {
    973 		printf("%s: reselect with invalid selid %02x;"
    974 		    " sending DEVICE RESET\n", sc->sc_dev.dv_xname, selid);
    975 		goto reset;
    976 	}
    977 
    978 	/*
    979 	 * Search wait queue for disconnected cmd
    980 	 * The list should be short, so I haven't bothered with
    981 	 * any more sophisticated structures than a simple
    982 	 * singly linked list.
    983 	 */
    984 	target = ffs(selid) - 1;
    985 	lun = message & 0x07;
    986 	for (ecb = sc->nexus_list.tqh_first; ecb != NULL;
    987 	     ecb = ecb->chain.tqe_next) {
    988 		sc_link = ecb->xs->sc_link;
    989 		if (sc_link->scsipi_scsi.target == target &&
    990 		    sc_link->scsipi_scsi.lun == lun)
    991 			break;
    992 	}
    993 	if (ecb == NULL) {
    994 		printf("%s: reselect from target %d lun %d with no nexus;"
    995 		    " sending ABORT\n", sc->sc_dev.dv_xname, target, lun);
    996 		goto abort;
    997 	}
    998 
    999 	/* Make this nexus active again. */
   1000 	TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
   1001 	sc->sc_state = NCR_CONNECTED;
   1002 	sc->sc_nexus = ecb;
   1003 	ti = &sc->sc_tinfo[target];
   1004 #ifdef NCR53C9X_DEBUG
   1005 	if ((ti->lubusy & (1 << lun)) == 0) {
   1006 		printf("%s: reselect: target %d, lun %d: should be busy\n",
   1007 			sc->sc_dev.dv_xname, target, lun);
   1008 		ti->lubusy |= (1 << lun);
   1009 	}
   1010 #endif
   1011 	ncr53c9x_setsync(sc, ti);
   1012 
   1013 	if (ecb->flags & ECB_RESET)
   1014 		ncr53c9x_sched_msgout(SEND_DEV_RESET);
   1015 	else if (ecb->flags & ECB_ABORT)
   1016 		ncr53c9x_sched_msgout(SEND_ABORT);
   1017 
   1018 	/* Do an implicit RESTORE POINTERS. */
   1019 	sc->sc_dp = ecb->daddr;
   1020 	sc->sc_dleft = ecb->dleft;
   1021 
   1022 	return (0);
   1023 
   1024 reset:
   1025 	ncr53c9x_sched_msgout(SEND_DEV_RESET);
   1026 	return (1);
   1027 
   1028 abort:
   1029 	ncr53c9x_sched_msgout(SEND_ABORT);
   1030 	return (1);
   1031 }
   1032 
   1033 #define IS1BYTEMSG(m) (((m) != 1 && (m) < 0x20) || (m) & 0x80)
   1034 #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1035 #define ISEXTMSG(m) ((m) == 1)
   1036 
   1037 /*
   1038  * Get an incoming message as initiator.
   1039  *
   1040  * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
   1041  * byte in the FIFO
   1042  */
   1043 void
   1044 ncr53c9x_msgin(sc)
   1045 	register struct ncr53c9x_softc *sc;
   1046 {
   1047 	register int v;
   1048 
   1049 	NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
   1050 
   1051 	if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) == 0) {
   1052 		printf("%s: msgin: no msg byte available\n",
   1053 			sc->sc_dev.dv_xname);
   1054 		return;
   1055 	}
   1056 
   1057 	/*
   1058 	 * Prepare for a new message.  A message should (according
   1059 	 * to the SCSI standard) be transmitted in one single
   1060 	 * MESSAGE_IN_PHASE. If we have been in some other phase,
   1061 	 * then this is a new message.
   1062 	 */
   1063 	if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
   1064 		sc->sc_flags &= ~NCR_DROP_MSGI;
   1065 		sc->sc_imlen = 0;
   1066 	}
   1067 
   1068 	v = NCR_READ_REG(sc, NCR_FIFO);
   1069 	NCR_MISC(("<msgbyte:0x%02x>", v));
   1070 
   1071 #if 0
   1072 	if (sc->sc_state == NCR_RESELECTED && sc->sc_imlen == 0) {
   1073 		/*
   1074 		 * Which target is reselecting us? (The ID bit really)
   1075 		 */
   1076 		sc->sc_selid = v;
   1077 		NCR_MISC(("selid=0x%2x ", sc->sc_selid));
   1078 		return;
   1079 	}
   1080 #endif
   1081 
   1082 	sc->sc_imess[sc->sc_imlen] = v;
   1083 
   1084 	/*
   1085 	 * If we're going to reject the message, don't bother storing
   1086 	 * the incoming bytes.  But still, we need to ACK them.
   1087 	 */
   1088 
   1089 	if ((sc->sc_flags & NCR_DROP_MSGI)) {
   1090 		NCRCMD(sc, NCRCMD_MSGOK);
   1091 		printf("<dropping msg byte %x>",
   1092 			sc->sc_imess[sc->sc_imlen]);
   1093 		return;
   1094 	}
   1095 
   1096 	if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
   1097 		ncr53c9x_sched_msgout(SEND_REJECT);
   1098 		sc->sc_flags |= NCR_DROP_MSGI;
   1099 	} else {
   1100 		sc->sc_imlen++;
   1101 		/*
   1102 		 * This testing is suboptimal, but most
   1103 		 * messages will be of the one byte variety, so
   1104 		 * it should not effect performance
   1105 		 * significantly.
   1106 		 */
   1107 		if (sc->sc_imlen == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1108 			goto gotit;
   1109 		if (sc->sc_imlen == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1110 			goto gotit;
   1111 		if (sc->sc_imlen >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1112 		    sc->sc_imlen == sc->sc_imess[1] + 2)
   1113 			goto gotit;
   1114 	}
   1115 	/* Ack what we have so far */
   1116 	NCRCMD(sc, NCRCMD_MSGOK);
   1117 	return;
   1118 
   1119 gotit:
   1120 	NCR_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
   1121 	/*
   1122 	 * Now we should have a complete message (1 byte, 2 byte
   1123 	 * and moderately long extended messages).  We only handle
   1124 	 * extended messages which total length is shorter than
   1125 	 * NCR_MAX_MSG_LEN.  Longer messages will be amputated.
   1126 	 */
   1127 	switch (sc->sc_state) {
   1128 		struct ncr53c9x_ecb *ecb;
   1129 		struct ncr53c9x_tinfo *ti;
   1130 
   1131 	case NCR_CONNECTED:
   1132 		ecb = sc->sc_nexus;
   1133 		ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
   1134 
   1135 		switch (sc->sc_imess[0]) {
   1136 		case MSG_CMDCOMPLETE:
   1137 			NCR_MSGS(("cmdcomplete "));
   1138 			if (sc->sc_dleft < 0) {
   1139 				scsi_print_addr(ecb->xs->sc_link);
   1140 				printf("got %ld extra bytes\n",
   1141 				       -(long)sc->sc_dleft);
   1142 				sc->sc_dleft = 0;
   1143 			}
   1144 			ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
   1145 				? 0
   1146 				: sc->sc_dleft;
   1147 			if ((ecb->flags & ECB_SENSE) == 0)
   1148 				ecb->xs->resid = ecb->dleft;
   1149 			sc->sc_state = NCR_CMDCOMPLETE;
   1150 			break;
   1151 
   1152 		case MSG_MESSAGE_REJECT:
   1153 			NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout));
   1154 			switch (sc->sc_msgout) {
   1155 			case SEND_SDTR:
   1156 				sc->sc_flags &= ~NCR_SYNCHNEGO;
   1157 				ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
   1158 				ncr53c9x_setsync(sc, ti);
   1159 				break;
   1160 			case SEND_INIT_DET_ERR:
   1161 				goto abort;
   1162 			}
   1163 			break;
   1164 
   1165 		case MSG_NOOP:
   1166 			NCR_MSGS(("noop "));
   1167 			break;
   1168 
   1169 		case MSG_DISCONNECT:
   1170 			NCR_MSGS(("disconnect "));
   1171 			ti->dconns++;
   1172 			sc->sc_state = NCR_DISCONNECT;
   1173 
   1174 			/*
   1175 			 * Mark the fact that all bytes have moved. The
   1176 			 * target may not bother to do a SAVE POINTERS
   1177 			 * at this stage. This flag will set the residual
   1178 			 * count to zero on MSG COMPLETE.
   1179 			 */
   1180 			if (sc->sc_dleft == 0)
   1181 				ecb->flags |= ECB_TENTATIVE_DONE;
   1182 
   1183 			break;
   1184 
   1185 		case MSG_SAVEDATAPOINTER:
   1186 			NCR_MSGS(("save datapointer "));
   1187 			ecb->daddr = sc->sc_dp;
   1188 			ecb->dleft = sc->sc_dleft;
   1189 			break;
   1190 
   1191 		case MSG_RESTOREPOINTERS:
   1192 			NCR_MSGS(("restore datapointer "));
   1193 			sc->sc_dp = ecb->daddr;
   1194 			sc->sc_dleft = ecb->dleft;
   1195 			break;
   1196 
   1197 		case MSG_EXTENDED:
   1198 			NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
   1199 			switch (sc->sc_imess[2]) {
   1200 			case MSG_EXT_SDTR:
   1201 				NCR_MSGS(("SDTR period %d, offset %d ",
   1202 					sc->sc_imess[3], sc->sc_imess[4]));
   1203 				if (sc->sc_imess[1] != 3)
   1204 					goto reject;
   1205 				ti->period = sc->sc_imess[3];
   1206 				ti->offset = sc->sc_imess[4];
   1207 				ti->flags &= ~T_NEGOTIATE;
   1208 				if (sc->sc_minsync == 0 ||
   1209 				    ti->offset == 0 ||
   1210 				    ti->period > 124) {
   1211 #ifdef NCR53C9X_DEBUG
   1212 					scsi_print_addr(ecb->xs->sc_link);
   1213 					printf("async mode\n");
   1214 #endif
   1215 					if ((sc->sc_flags&NCR_SYNCHNEGO)
   1216 					    == 0) {
   1217 						/*
   1218 						 * target initiated negotiation
   1219 						 */
   1220 						ti->offset = 0;
   1221 						ti->flags &= ~T_SYNCMODE;
   1222 						ncr53c9x_sched_msgout(
   1223 						    SEND_SDTR);
   1224 					} else {
   1225 						/* we are async */
   1226 						ti->flags &= ~T_SYNCMODE;
   1227 					}
   1228 				} else {
   1229 					int r = 250/ti->period;
   1230 					int s = (100*250)/ti->period - 100*r;
   1231 					int p;
   1232 
   1233 					p = ncr53c9x_stp2cpb(sc, ti->period);
   1234 					ti->period = ncr53c9x_cpb2stp(sc, p);
   1235 #ifdef NCR53C9X_DEBUG
   1236 					scsi_print_addr(ecb->xs->sc_link);
   1237 					printf("max sync rate %d.%02dMB/s\n",
   1238 						r, s);
   1239 #endif
   1240 					if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
   1241 						/*
   1242 						 * target initiated negotiation
   1243 						 */
   1244 						if (ti->period <
   1245 						    sc->sc_minsync)
   1246 							ti->period =
   1247 							    sc->sc_minsync;
   1248 						if (ti->offset > 15)
   1249 							ti->offset = 15;
   1250 						ti->flags &= ~T_SYNCMODE;
   1251 						ncr53c9x_sched_msgout(
   1252 						    SEND_SDTR);
   1253 					} else {
   1254 						/* we are sync */
   1255 						ti->flags |= T_SYNCMODE;
   1256 					}
   1257 				}
   1258 				sc->sc_flags &= ~NCR_SYNCHNEGO;
   1259 				ncr53c9x_setsync(sc, ti);
   1260 				break;
   1261 
   1262 			default:
   1263 				scsi_print_addr(ecb->xs->sc_link);
   1264 				printf("unrecognized MESSAGE EXTENDED;"
   1265 				       " sending REJECT\n");
   1266 				goto reject;
   1267 			}
   1268 			break;
   1269 
   1270 		default:
   1271 			NCR_MSGS(("ident "));
   1272 			scsi_print_addr(ecb->xs->sc_link);
   1273 			printf("unrecognized MESSAGE; sending REJECT\n");
   1274 		reject:
   1275 			ncr53c9x_sched_msgout(SEND_REJECT);
   1276 			break;
   1277 		}
   1278 		break;
   1279 
   1280 	case NCR_RESELECTED:
   1281 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1282 			printf("%s: reselect without IDENTIFY;"
   1283 			       " sending DEVICE RESET\n",
   1284 			       sc->sc_dev.dv_xname);
   1285 			goto reset;
   1286 		}
   1287 
   1288 		(void) ncr53c9x_reselect(sc, sc->sc_imess[0]);
   1289 		break;
   1290 
   1291 	default:
   1292 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1293 			sc->sc_dev.dv_xname);
   1294 	reset:
   1295 		ncr53c9x_sched_msgout(SEND_DEV_RESET);
   1296 		break;
   1297 
   1298 	abort:
   1299 		ncr53c9x_sched_msgout(SEND_ABORT);
   1300 		break;
   1301 	}
   1302 
   1303 	/* Ack last message byte */
   1304 	NCRCMD(sc, NCRCMD_MSGOK);
   1305 
   1306 	/* Done, reset message pointer. */
   1307 	sc->sc_flags &= ~NCR_DROP_MSGI;
   1308 	sc->sc_imlen = 0;
   1309 }
   1310 
   1311 
   1312 /*
   1313  * Send the highest priority, scheduled message
   1314  */
   1315 void
   1316 ncr53c9x_msgout(sc)
   1317 	register struct ncr53c9x_softc *sc;
   1318 {
   1319 	struct ncr53c9x_tinfo *ti;
   1320 	struct ncr53c9x_ecb *ecb;
   1321 	size_t size;
   1322 
   1323 	NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
   1324 	    sc->sc_msgpriq, sc->sc_prevphase));
   1325 
   1326 	/*
   1327 	 * XXX - the NCR_ATN flag is not in sync with the actual ATN
   1328 	 *	 condition on the SCSI bus. The 53c9x chip
   1329 	 *	 automatically turns off ATN before sending the
   1330 	 *	 message byte.  (see also the comment below in the
   1331 	 *	 default case when picking out a message to send)
   1332 	 */
   1333 	if (sc->sc_flags & NCR_ATN) {
   1334 		if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
   1335 		new:
   1336 			NCRCMD(sc, NCRCMD_FLUSH);
   1337 			DELAY(1);
   1338 			sc->sc_msgoutq = 0;
   1339 			sc->sc_omlen = 0;
   1340 		}
   1341 	} else {
   1342 		if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   1343 			ncr53c9x_sched_msgout(sc->sc_msgoutq);
   1344 			goto new;
   1345 		} else {
   1346 			printf("%s at line %d: unexpected MESSAGE OUT phase\n",
   1347 			    sc->sc_dev.dv_xname, __LINE__);
   1348 		}
   1349 	}
   1350 
   1351 	if (sc->sc_omlen == 0) {
   1352 		/* Pick up highest priority message */
   1353 		sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
   1354 		sc->sc_msgoutq |= sc->sc_msgout;
   1355 		sc->sc_msgpriq &= ~sc->sc_msgout;
   1356 		sc->sc_omlen = 1;		/* "Default" message len */
   1357 		switch (sc->sc_msgout) {
   1358 		case SEND_SDTR:
   1359 			ecb = sc->sc_nexus;
   1360 			ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
   1361 			sc->sc_omess[0] = MSG_EXTENDED;
   1362 			sc->sc_omess[1] = 3;
   1363 			sc->sc_omess[2] = MSG_EXT_SDTR;
   1364 			sc->sc_omess[3] = ti->period;
   1365 			sc->sc_omess[4] = ti->offset;
   1366 			sc->sc_omlen = 5;
   1367 			if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
   1368 				ti->flags |= T_SYNCMODE;
   1369 				ncr53c9x_setsync(sc, ti);
   1370 			}
   1371 			break;
   1372 		case SEND_IDENTIFY:
   1373 			if (sc->sc_state != NCR_CONNECTED) {
   1374 				printf("%s at line %d: no nexus\n",
   1375 				    sc->sc_dev.dv_xname, __LINE__);
   1376 			}
   1377 			ecb = sc->sc_nexus;
   1378 			sc->sc_omess[0] =
   1379 			    MSG_IDENTIFY(ecb->xs->sc_link->scsipi_scsi.lun, 0);
   1380 			break;
   1381 		case SEND_DEV_RESET:
   1382 			sc->sc_flags |= NCR_ABORTING;
   1383 			sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1384 			ecb = sc->sc_nexus;
   1385 			ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
   1386 			ti->flags &= ~T_SYNCMODE;
   1387 			ti->flags |= T_NEGOTIATE;
   1388 			break;
   1389 		case SEND_PARITY_ERROR:
   1390 			sc->sc_omess[0] = MSG_PARITY_ERROR;
   1391 			break;
   1392 		case SEND_ABORT:
   1393 			sc->sc_flags |= NCR_ABORTING;
   1394 			sc->sc_omess[0] = MSG_ABORT;
   1395 			break;
   1396 		case SEND_INIT_DET_ERR:
   1397 			sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1398 			break;
   1399 		case SEND_REJECT:
   1400 			sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1401 			break;
   1402 		default:
   1403 			/*
   1404 			 * We normally do not get here, since the chip
   1405 			 * automatically turns off ATN before the last
   1406 			 * byte of a message is sent to the target.
   1407 			 * However, if the target rejects our (multi-byte)
   1408 			 * message early by switching to MSG IN phase
   1409 			 * ATN remains on, so the target may return to
   1410 			 * MSG OUT phase. If there are no scheduled messages
   1411 			 * left we send a NO-OP.
   1412 			 *
   1413 			 * XXX - Note that this leaves no useful purpose for
   1414 			 * the NCR_ATN flag.
   1415 			 */
   1416 			sc->sc_flags &= ~NCR_ATN;
   1417 			sc->sc_omess[0] = MSG_NOOP;
   1418 			break;
   1419 		}
   1420 		sc->sc_omp = sc->sc_omess;
   1421 	}
   1422 
   1423 	/* (re)send the message */
   1424 	size = min(sc->sc_omlen, sc->sc_maxxfer);
   1425 	NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
   1426 	/* Program the SCSI counter */
   1427 	NCR_WRITE_REG(sc, NCR_TCL, size);
   1428 	NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
   1429 	if (sc->sc_cfg2 & NCRCFG2_FE) {
   1430 		NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
   1431 	}
   1432 	/* Load the count in and start the message-out transfer */
   1433 	NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
   1434 	NCRCMD(sc, NCRCMD_TRANS|NCRCMD_DMA);
   1435 	NCRDMA_GO(sc);
   1436 }
   1437 
   1438 /*
   1439  * This is the most critical part of the driver, and has to know
   1440  * how to deal with *all* error conditions and phases from the SCSI
   1441  * bus. If there are no errors and the DMA was active, then call the
   1442  * DMA pseudo-interrupt handler. If this returns 1, then that was it
   1443  * and we can return from here without further processing.
   1444  *
   1445  * Most of this needs verifying.
   1446  */
   1447 int sdebug = 0;
   1448 int
   1449 ncr53c9x_intr(arg)
   1450 	void *arg;
   1451 {
   1452 	register struct ncr53c9x_softc *sc = arg;
   1453 	register struct ncr53c9x_ecb *ecb;
   1454 	register struct scsipi_link *sc_link;
   1455 	struct ncr53c9x_tinfo *ti;
   1456 	size_t size;
   1457 	int nfifo;
   1458 
   1459 	NCR_TRACE(("[ncr53c9x_intr] "));
   1460 
   1461 	if (!NCRDMA_ISINTR(sc))
   1462 		return (0);
   1463 
   1464 again:
   1465 	/* and what do the registers say... */
   1466 	ncr53c9x_readregs(sc);
   1467 
   1468 	sc->sc_intrcnt.ev_count++;
   1469 
   1470 	/*
   1471 	 * At the moment, only a SCSI Bus Reset or Illegal
   1472 	 * Command are classed as errors. A disconnect is a
   1473 	 * valid condition, and we let the code check is the
   1474 	 * "NCR_BUSFREE_OK" flag was set before declaring it
   1475 	 * and error.
   1476 	 *
   1477 	 * Also, the status register tells us about "Gross
   1478 	 * Errors" and "Parity errors". Only the Gross Error
   1479 	 * is really bad, and the parity errors are dealt
   1480 	 * with later
   1481 	 *
   1482 	 * TODO
   1483 	 *	If there are too many parity error, go to slow
   1484 	 *	cable mode ?
   1485 	 */
   1486 
   1487 	/* SCSI Reset */
   1488 	if (sc->sc_espintr & NCRINTR_SBR) {
   1489 		if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   1490 			NCRCMD(sc, NCRCMD_FLUSH);
   1491 			DELAY(1);
   1492 		}
   1493 		if (sc->sc_state != NCR_SBR) {
   1494 			printf("%s: SCSI bus reset\n",
   1495 				sc->sc_dev.dv_xname);
   1496 			ncr53c9x_init(sc, 0); /* Restart everything */
   1497 			return (1);
   1498 		}
   1499 #if 0
   1500 /*XXX*/		printf("<expected bus reset: "
   1501 			"[intr %x, stat %x, step %d]>\n",
   1502 			sc->sc_espintr, sc->sc_espstat,
   1503 			sc->sc_espstep);
   1504 #endif
   1505 		if (sc->sc_nexus)
   1506 			panic("%s: nexus in reset state",
   1507 			      sc->sc_dev.dv_xname);
   1508 		goto sched;
   1509 	}
   1510 
   1511 	ecb = sc->sc_nexus;
   1512 
   1513 #define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
   1514 	if (sc->sc_espintr & NCRINTR_ERR ||
   1515 	    sc->sc_espstat & NCRSTAT_GE) {
   1516 
   1517 		if (sc->sc_espstat & NCRSTAT_GE) {
   1518 			/* Gross Error; no target ? */
   1519 			if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   1520 				NCRCMD(sc, NCRCMD_FLUSH);
   1521 				DELAY(1);
   1522 			}
   1523 			if (sc->sc_state == NCR_CONNECTED ||
   1524 			    sc->sc_state == NCR_SELECTING) {
   1525 				ecb->xs->error = XS_TIMEOUT;
   1526 				ncr53c9x_done(sc, ecb);
   1527 			}
   1528 			return (1);
   1529 		}
   1530 
   1531 		if (sc->sc_espintr & NCRINTR_ILL) {
   1532 			if (sc->sc_flags & NCR_EXPECT_ILLCMD) {
   1533 				/*
   1534 				 * Eat away "Illegal command" interrupt
   1535 				 * on a ESP100 caused by a re-selection
   1536 				 * while we were trying to select
   1537 				 * another target.
   1538 				 */
   1539 #ifdef DEBUG
   1540 				printf("%s: ESP100 work-around activated\n",
   1541 					sc->sc_dev.dv_xname);
   1542 #endif
   1543 				sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
   1544 				return (1);
   1545 			}
   1546 			/* illegal command, out of sync ? */
   1547 			printf("%s: illegal command: 0x%x "
   1548 			    "(state %d, phase %x, prevphase %x)\n",
   1549 				sc->sc_dev.dv_xname, sc->sc_lastcmd,
   1550 				sc->sc_state, sc->sc_phase,
   1551 				sc->sc_prevphase);
   1552 			if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   1553 				NCRCMD(sc, NCRCMD_FLUSH);
   1554 				DELAY(1);
   1555 			}
   1556 			ncr53c9x_init(sc, 1); /* Restart everything */
   1557 			return (1);
   1558 		}
   1559 	}
   1560 	sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
   1561 
   1562 	/*
   1563 	 * Call if DMA is active.
   1564 	 *
   1565 	 * If DMA_INTR returns true, then maybe go 'round the loop
   1566 	 * again in case there is no more DMA queued, but a phase
   1567 	 * change is expected.
   1568 	 */
   1569 	if (NCRDMA_ISACTIVE(sc)) {
   1570 		int r = NCRDMA_INTR(sc);
   1571 		if (r == -1) {
   1572 			printf("%s: DMA error; resetting\n",
   1573 				sc->sc_dev.dv_xname);
   1574 			ncr53c9x_init(sc, 1);
   1575 		}
   1576 		/* If DMA active here, then go back to work... */
   1577 		if (NCRDMA_ISACTIVE(sc))
   1578 			return (1);
   1579 
   1580 		if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
   1581 			/*
   1582 			 * DMA not completed.  If we can not find a
   1583 			 * acceptable explanation, print a diagnostic.
   1584 			 */
   1585 			if (sc->sc_state == NCR_SELECTING)
   1586 				/*
   1587 				 * This can happen if we are reselected
   1588 				 * while using DMA to select a target.
   1589 				 */
   1590 				/*void*/;
   1591 			else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   1592 				/*
   1593 				 * Our (multi-byte) message (eg SDTR) was
   1594 				 * interrupted by the target to send
   1595 				 * a MSG REJECT.
   1596 				 * Print diagnostic if current phase
   1597 				 * is not MESSAGE IN.
   1598 				 */
   1599 				if (sc->sc_phase != MESSAGE_IN_PHASE)
   1600 				    printf("%s: !TC on MSG OUT"
   1601 				       " [intr %x, stat %x, step %d]"
   1602 				       " prevphase %x, resid %lx\n",
   1603 					sc->sc_dev.dv_xname,
   1604 					sc->sc_espintr,
   1605 					sc->sc_espstat,
   1606 					sc->sc_espstep,
   1607 					sc->sc_prevphase,
   1608 					(u_long)sc->sc_omlen);
   1609 			} else if (sc->sc_dleft == 0) {
   1610 				/*
   1611 				 * The DMA operation was started for
   1612 				 * a DATA transfer. Print a diagnostic
   1613 				 * if the DMA counter and TC bit
   1614 				 * appear to be out of sync.
   1615 				 */
   1616 				printf("%s: !TC on DATA XFER"
   1617 				       " [intr %x, stat %x, step %d]"
   1618 				       " prevphase %x, resid %x\n",
   1619 					sc->sc_dev.dv_xname,
   1620 					sc->sc_espintr,
   1621 					sc->sc_espstat,
   1622 					sc->sc_espstep,
   1623 					sc->sc_prevphase,
   1624 					ecb?ecb->dleft:-1);
   1625 			}
   1626 		}
   1627 	}
   1628 
   1629 	/*
   1630 	 * Check for less serious errors.
   1631 	 */
   1632 	if (sc->sc_espstat & NCRSTAT_PE) {
   1633 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   1634 		if (sc->sc_prevphase == MESSAGE_IN_PHASE)
   1635 			ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
   1636 		else
   1637 			ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
   1638 	}
   1639 
   1640 	if (sc->sc_espintr & NCRINTR_DIS) {
   1641 		NCR_MISC(("<DISC [intr %x, stat %x, step %d]>",
   1642 			sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
   1643 		if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   1644 			NCRCMD(sc, NCRCMD_FLUSH);
   1645 			DELAY(1);
   1646 		}
   1647 		/*
   1648 		 * This command must (apparently) be issued within
   1649 		 * 250mS of a disconnect. So here you are...
   1650 		 */
   1651 		NCRCMD(sc, NCRCMD_ENSEL);
   1652 
   1653 		switch (sc->sc_state) {
   1654 		case NCR_RESELECTED:
   1655 			goto sched;
   1656 
   1657 		case NCR_SELECTING:
   1658 			ecb->xs->error = XS_SELTIMEOUT;
   1659 			goto finish;
   1660 
   1661 		case NCR_CONNECTED:
   1662 			if ((sc->sc_flags & NCR_SYNCHNEGO)) {
   1663 #ifdef NCR53C9X_DEBUG
   1664 				if (ecb)
   1665 					scsi_print_addr(ecb->xs->sc_link);
   1666 				printf("sync nego not completed!\n");
   1667 #endif
   1668 				ti = &sc->sc_tinfo[ecb->xs->sc_link->scsipi_scsi.target];
   1669 				sc->sc_flags &= ~NCR_SYNCHNEGO;
   1670 				ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
   1671 			}
   1672 
   1673 			/* it may be OK to disconnect */
   1674 			if ((sc->sc_flags & NCR_ABORTING) == 0) {
   1675 				/*
   1676 				 * Section 5.1.1 of the SCSI 2 spec
   1677 				 * suggests issuing a REQUEST SENSE
   1678 				 * following an unexpected disconnect.
   1679 				 * Some devices go into a contingent
   1680 				 * allegiance condition when
   1681 				 * disconnecting, and this is necessary
   1682 				 * to clean up their state.
   1683 				 */
   1684 				printf("%s: unexpected disconnect; ",
   1685 				    sc->sc_dev.dv_xname);
   1686 				if (ecb->flags & ECB_SENSE) {
   1687 					printf("resetting\n");
   1688 					goto reset;
   1689 				}
   1690 				printf("sending REQUEST SENSE\n");
   1691 				untimeout(ncr53c9x_timeout, ecb);
   1692 				ncr53c9x_sense(sc, ecb);
   1693 				goto out;
   1694 			}
   1695 
   1696 			ecb->xs->error = XS_TIMEOUT;
   1697 			goto finish;
   1698 
   1699 		case NCR_DISCONNECT:
   1700 			TAILQ_INSERT_HEAD(&sc->nexus_list, ecb, chain);
   1701 			sc->sc_nexus = NULL;
   1702 			goto sched;
   1703 
   1704 		case NCR_CMDCOMPLETE:
   1705 			goto finish;
   1706 		}
   1707 	}
   1708 
   1709 	switch (sc->sc_state) {
   1710 
   1711 	case NCR_SBR:
   1712 		printf("%s: waiting for SCSI Bus Reset to happen\n",
   1713 			sc->sc_dev.dv_xname);
   1714 		return (1);
   1715 
   1716 	case NCR_RESELECTED:
   1717 		/*
   1718 		 * we must be continuing a message ?
   1719 		 */
   1720 		if (sc->sc_phase != MESSAGE_IN_PHASE) {
   1721 			printf("%s: target didn't identify\n",
   1722 				sc->sc_dev.dv_xname);
   1723 			ncr53c9x_init(sc, 1);
   1724 			return (1);
   1725 		}
   1726 printf("<<RESELECT CONT'd>>");
   1727 #if XXXX
   1728 		ncr53c9x_msgin(sc);
   1729 		if (sc->sc_state != NCR_CONNECTED) {
   1730 			/* IDENTIFY fail?! */
   1731 			printf("%s: identify failed\n",
   1732 				sc->sc_dev.dv_xname);
   1733 			ncr53c9x_init(sc, 1);
   1734 			return (1);
   1735 		}
   1736 #endif
   1737 		break;
   1738 
   1739 	case NCR_IDLE:
   1740 	case NCR_SELECTING:
   1741 		sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
   1742 		sc->sc_flags = 0;
   1743 		ecb = sc->sc_nexus;
   1744 		if (ecb != NULL && (ecb->flags & ECB_NEXUS)) {
   1745 			scsi_print_addr(ecb->xs->sc_link);
   1746 			printf("ECB_NEXUS while in state %x\n", sc->sc_state);
   1747 		}
   1748 
   1749 		if (sc->sc_espintr & NCRINTR_RESEL) {
   1750 			/*
   1751 			 * If we're trying to select a
   1752 			 * target ourselves, push our command
   1753 			 * back into the ready list.
   1754 			 */
   1755 			if (sc->sc_state == NCR_SELECTING) {
   1756 				NCR_MISC(("backoff selector "));
   1757 				untimeout(ncr53c9x_timeout, ecb);
   1758 				sc_link = ecb->xs->sc_link;
   1759 				ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   1760 				TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
   1761 				ecb = sc->sc_nexus = NULL;
   1762 			}
   1763 			sc->sc_state = NCR_RESELECTED;
   1764 			if (sc->sc_phase != MESSAGE_IN_PHASE) {
   1765 				/*
   1766 				 * Things are seriously fucked up.
   1767 				 * Pull the brakes, i.e. reset
   1768 				 */
   1769 				printf("%s: target didn't identify\n",
   1770 					sc->sc_dev.dv_xname);
   1771 				ncr53c9x_init(sc, 1);
   1772 				return (1);
   1773 			}
   1774 			/*
   1775 			 * The C90 only inhibits FIFO writes until
   1776 			 * reselection is complete, instead of
   1777 			 * waiting until the interrupt status register
   1778 			 * has been read. So, if the reselect happens
   1779 			 * while we were entering a command bytes (for
   1780 			 * another target) some of those bytes can
   1781 			 * appear in the FIFO here, after the
   1782 			 * interrupt is taken.
   1783 			 */
   1784 			nfifo = NCR_READ_REG(sc,NCR_FFLAG) & NCRFIFO_FF;
   1785 			if (nfifo < 2 ||
   1786 			    (nfifo > 2 &&
   1787 			     sc->sc_rev != NCR_VARIANT_ESP100)) {
   1788 				printf("%s: RESELECT: %d bytes in FIFO! "
   1789 				    "[intr %x, stat %x, step %d, prevphase %x]\n",
   1790 					sc->sc_dev.dv_xname,
   1791 					nfifo,
   1792 					sc->sc_espintr,
   1793 					sc->sc_espstat,
   1794 					sc->sc_espstep,
   1795 					sc->sc_prevphase);
   1796 				ncr53c9x_init(sc, 1);
   1797 				return (1);
   1798 			}
   1799 			sc->sc_selid = NCR_READ_REG(sc, NCR_FIFO);
   1800 			NCR_MISC(("selid=0x%2x ", sc->sc_selid));
   1801 
   1802 			/* Handle identify message */
   1803 			ncr53c9x_msgin(sc);
   1804 			if (nfifo != 2) {
   1805 				/*
   1806 				 * Note: this should not happen
   1807 				 * with `dmaselect' on.
   1808 				 */
   1809 				sc->sc_flags |= NCR_EXPECT_ILLCMD;
   1810 				NCRCMD(sc, NCRCMD_FLUSH);
   1811 			} else if (ncr53c9x_dmaselect &&
   1812 				   sc->sc_rev == NCR_VARIANT_ESP100) {
   1813 				sc->sc_flags |= NCR_EXPECT_ILLCMD;
   1814 			}
   1815 
   1816 			if (sc->sc_state != NCR_CONNECTED) {
   1817 				/* IDENTIFY fail?! */
   1818 				printf("%s: identify failed\n",
   1819 					sc->sc_dev.dv_xname);
   1820 				ncr53c9x_init(sc, 1);
   1821 				return (1);
   1822 			}
   1823 			goto shortcut; /* ie. next phase expected soon */
   1824 		}
   1825 
   1826 #define	NCRINTR_DONE	(NCRINTR_FC|NCRINTR_BS)
   1827 		if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
   1828 			/*
   1829 			 * Arbitration won; examine the `step' register
   1830 			 * to determine how far the selection could progress.
   1831 			 */
   1832 			ecb = sc->sc_nexus;
   1833 			if (!ecb)
   1834 				panic("ncr53c9x: no nexus");
   1835 
   1836 			sc_link = ecb->xs->sc_link;
   1837 			ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   1838 
   1839 			switch (sc->sc_espstep) {
   1840 			case 0:
   1841 				/*
   1842 				 * The target did not respond with a
   1843 				 * message out phase - probably an old
   1844 				 * device that doesn't recognize ATN.
   1845 				 * Clear ATN and just continue, the
   1846 				 * target should be in the command
   1847 				 * phase.
   1848 				 * XXXX check for command phase?
   1849 				 */
   1850 				NCRCMD(sc, NCRCMD_RSTATN);
   1851 				break;
   1852 			case 1:
   1853 				if ((ti->flags & T_NEGOTIATE) == 0) {
   1854 					printf("%s: step 1 & !NEG\n",
   1855 						sc->sc_dev.dv_xname);
   1856 					goto reset;
   1857 				}
   1858 				if (sc->sc_phase != MESSAGE_OUT_PHASE) {
   1859 					printf("%s: !MSGOUT\n",
   1860 						sc->sc_dev.dv_xname);
   1861 					goto reset;
   1862 				}
   1863 				/* Start negotiating */
   1864 				ti->period = sc->sc_minsync;
   1865 				ti->offset = 15;
   1866 				sc->sc_flags |= NCR_SYNCHNEGO;
   1867 				ncr53c9x_sched_msgout(SEND_SDTR);
   1868 				break;
   1869 			case 3:
   1870 				/*
   1871 				 * Grr, this is supposed to mean
   1872 				 * "target left command phase  prematurely".
   1873 				 * It seems to happen regularly when
   1874 				 * sync mode is on.
   1875 				 * Look at FIFO to see if command went out.
   1876 				 * (Timing problems?)
   1877 				 */
   1878 				if (ncr53c9x_dmaselect) {
   1879 					if (sc->sc_cmdlen == 0)
   1880 						/* Hope for the best.. */
   1881 						break;
   1882 				} else if ((NCR_READ_REG(sc, NCR_FFLAG)
   1883 					    & NCRFIFO_FF) == 0) {
   1884 					/* Hope for the best.. */
   1885 					break;
   1886 				}
   1887 				printf("(%s:%d:%d): selection failed;"
   1888 					" %d left in FIFO "
   1889 					"[intr %x, stat %x, step %d]\n",
   1890 					sc->sc_dev.dv_xname,
   1891 					sc_link->scsipi_scsi.target,
   1892 					sc_link->scsipi_scsi.lun,
   1893 					NCR_READ_REG(sc, NCR_FFLAG)
   1894 					 & NCRFIFO_FF,
   1895 					sc->sc_espintr, sc->sc_espstat,
   1896 					sc->sc_espstep);
   1897 				NCRCMD(sc, NCRCMD_FLUSH);
   1898 				ncr53c9x_sched_msgout(SEND_ABORT);
   1899 				return (1);
   1900 			case 2:
   1901 				/* Select stuck at Command Phase */
   1902 				NCRCMD(sc, NCRCMD_FLUSH);
   1903 				break;
   1904 			case 4:
   1905 				if (ncr53c9x_dmaselect &&
   1906 				    sc->sc_cmdlen != 0)
   1907 					printf("(%s:%d:%d): select; "
   1908 					       "%lu left in DMA buffer "
   1909 					"[intr %x, stat %x, step %d]\n",
   1910 						sc->sc_dev.dv_xname,
   1911 						sc_link->scsipi_scsi.target,
   1912 						sc_link->scsipi_scsi.lun,
   1913 						(u_long)sc->sc_cmdlen,
   1914 						sc->sc_espintr,
   1915 						sc->sc_espstat,
   1916 						sc->sc_espstep);
   1917 				/* So far, everything went fine */
   1918 				break;
   1919 			}
   1920 
   1921 			ecb->flags |= ECB_NEXUS;
   1922 			ti->lubusy |= (1 << sc_link->scsipi_scsi.lun);
   1923 
   1924 			sc->sc_prevphase = INVALID_PHASE; /* ?? */
   1925 			/* Do an implicit RESTORE POINTERS. */
   1926 			sc->sc_dp = ecb->daddr;
   1927 			sc->sc_dleft = ecb->dleft;
   1928 			sc->sc_state = NCR_CONNECTED;
   1929 			break;
   1930 
   1931 		} else {
   1932 
   1933 			printf("%s: unexpected status after select"
   1934 				": [intr %x, stat %x, step %x]\n",
   1935 				sc->sc_dev.dv_xname,
   1936 				sc->sc_espintr, sc->sc_espstat,
   1937 				sc->sc_espstep);
   1938 			NCRCMD(sc, NCRCMD_FLUSH);
   1939 			DELAY(1);
   1940 			goto reset;
   1941 		}
   1942 		if (sc->sc_state == NCR_IDLE) {
   1943 			printf("%s: stray interrupt\n",
   1944 			    sc->sc_dev.dv_xname);
   1945 				return (0);
   1946 		}
   1947 		break;
   1948 
   1949 	case NCR_CONNECTED:
   1950 		if (sc->sc_flags & NCR_ICCS) {
   1951 			/* "Initiate Command Complete Steps" in progress */
   1952 			u_char msg;
   1953 
   1954 			sc->sc_flags &= ~NCR_ICCS;
   1955 
   1956 			if (!(sc->sc_espintr & NCRINTR_DONE)) {
   1957 				printf("%s: ICCS: "
   1958 				      ": [intr %x, stat %x, step %x]\n",
   1959 					sc->sc_dev.dv_xname,
   1960 					sc->sc_espintr, sc->sc_espstat,
   1961 					sc->sc_espstep);
   1962 			}
   1963 			if ((NCR_READ_REG(sc, NCR_FFLAG)
   1964 			    & NCRFIFO_FF) != 2) {
   1965 				/* Drop excess bytes from the queue */
   1966 				int i = (NCR_READ_REG(sc, NCR_FFLAG)
   1967 					    & NCRFIFO_FF) - 2;
   1968 				while (i-- > 0)
   1969 					(void) NCR_READ_REG(sc, NCR_FIFO);
   1970 			}
   1971 			ecb->stat = NCR_READ_REG(sc, NCR_FIFO);
   1972 			msg = NCR_READ_REG(sc, NCR_FIFO);
   1973 			NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
   1974 			if (msg == MSG_CMDCOMPLETE) {
   1975 				ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
   1976 					? 0
   1977 					: sc->sc_dleft;
   1978 				if ((ecb->flags & ECB_SENSE) == 0)
   1979 					ecb->xs->resid = ecb->dleft;
   1980 				sc->sc_state = NCR_CMDCOMPLETE;
   1981 			} else
   1982 				printf("%s: STATUS_PHASE: msg %d\n",
   1983 					sc->sc_dev.dv_xname, msg);
   1984 			NCRCMD(sc, NCRCMD_MSGOK);
   1985 			goto shortcut; /* ie. wait for disconnect */
   1986 		}
   1987 		break;
   1988 	default:
   1989 		panic("%s: invalid state: %d",
   1990 		      sc->sc_dev.dv_xname,
   1991 		      sc->sc_state);
   1992 	}
   1993 
   1994 	/*
   1995 	 * Driver is now in state NCR_CONNECTED, i.e. we
   1996 	 * have a current command working the SCSI bus.
   1997 	 */
   1998 	if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
   1999 		panic("ncr53c9x: no nexus");
   2000 	}
   2001 
   2002 	switch (sc->sc_phase) {
   2003 	case MESSAGE_OUT_PHASE:
   2004 		NCR_PHASE(("MESSAGE_OUT_PHASE "));
   2005 		ncr53c9x_msgout(sc);
   2006 		sc->sc_prevphase = MESSAGE_OUT_PHASE;
   2007 		break;
   2008 	case MESSAGE_IN_PHASE:
   2009 		NCR_PHASE(("MESSAGE_IN_PHASE "));
   2010 		sc->sc_prevphase = MESSAGE_IN_PHASE;
   2011 		if (sc->sc_espintr & NCRINTR_BS) {
   2012 			NCRCMD(sc, NCRCMD_FLUSH);
   2013 			sc->sc_flags |= NCR_WAITI;
   2014 			NCRCMD(sc, NCRCMD_TRANS);
   2015 		} else if (sc->sc_espintr & NCRINTR_FC) {
   2016 			if ((sc->sc_flags & NCR_WAITI) == 0) {
   2017 				printf("%s: MSGIN: unexpected FC bit: "
   2018 					"[intr %x, stat %x, step %x]\n",
   2019 				sc->sc_dev.dv_xname,
   2020 				sc->sc_espintr, sc->sc_espstat,
   2021 				sc->sc_espstep);
   2022 			}
   2023 			sc->sc_flags &= ~NCR_WAITI;
   2024 			ncr53c9x_msgin(sc);
   2025 		} else {
   2026 			printf("%s: MSGIN: weird bits: "
   2027 				"[intr %x, stat %x, step %x]\n",
   2028 				sc->sc_dev.dv_xname,
   2029 				sc->sc_espintr, sc->sc_espstat,
   2030 				sc->sc_espstep);
   2031 		}
   2032 		goto shortcut;	/* i.e. expect data to be ready */
   2033 		break;
   2034 	case COMMAND_PHASE:
   2035 		/*
   2036 		 * Send the command block. Normally we don't see this
   2037 		 * phase because the SEL_ATN command takes care of
   2038 		 * all this. However, we end up here if either the
   2039 		 * target or we wanted to exchange some more messages
   2040 		 * first (e.g. to start negotiations).
   2041 		 */
   2042 
   2043 		NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
   2044 			ecb->cmd.cmd.opcode, ecb->clen));
   2045 		if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   2046 			NCRCMD(sc, NCRCMD_FLUSH);
   2047 			DELAY(1);
   2048 		}
   2049 		if (ncr53c9x_dmaselect) {
   2050 			size_t size;
   2051 			/* setup DMA transfer for command */
   2052 			size = ecb->clen;
   2053 			sc->sc_cmdlen = size;
   2054 			sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
   2055 			NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
   2056 				     0, &size);
   2057 			/* Program the SCSI counter */
   2058 			NCR_WRITE_REG(sc, NCR_TCL, size);
   2059 			NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
   2060 			if (sc->sc_cfg2 & NCRCFG2_FE) {
   2061 				NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
   2062 			}
   2063 
   2064 			/* load the count in */
   2065 			NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
   2066 
   2067 			/* start the command transfer */
   2068 			NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
   2069 			NCRDMA_GO(sc);
   2070 		} else {
   2071 			u_char *cmd = (u_char *)&ecb->cmd.cmd;
   2072 			int i;
   2073 			/* Now the command into the FIFO */
   2074 			for (i = 0; i < ecb->clen; i++)
   2075 				NCR_WRITE_REG(sc, NCR_FIFO, *cmd++);
   2076 			NCRCMD(sc, NCRCMD_TRANS);
   2077 		}
   2078 		sc->sc_prevphase = COMMAND_PHASE;
   2079 		break;
   2080 	case DATA_OUT_PHASE:
   2081 		NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
   2082 		NCRCMD(sc, NCRCMD_FLUSH);
   2083 		size = min(sc->sc_dleft, sc->sc_maxxfer);
   2084 		NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft,
   2085 			  0, &size);
   2086 		sc->sc_prevphase = DATA_OUT_PHASE;
   2087 		goto setup_xfer;
   2088 	case DATA_IN_PHASE:
   2089 		NCR_PHASE(("DATA_IN_PHASE "));
   2090 		if (sc->sc_rev == NCR_VARIANT_ESP100)
   2091 			NCRCMD(sc, NCRCMD_FLUSH);
   2092 		size = min(sc->sc_dleft, sc->sc_maxxfer);
   2093 		NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft,
   2094 			  1, &size);
   2095 		sc->sc_prevphase = DATA_IN_PHASE;
   2096 	setup_xfer:
   2097 		/* Target returned to data phase: wipe "done" memory */
   2098 		ecb->flags &= ~ECB_TENTATIVE_DONE;
   2099 
   2100 		/* Program the SCSI counter */
   2101 		NCR_WRITE_REG(sc, NCR_TCL, size);
   2102 		NCR_WRITE_REG(sc, NCR_TCM, size >> 8);
   2103 		if (sc->sc_cfg2 & NCRCFG2_FE) {
   2104 			NCR_WRITE_REG(sc, NCR_TCH, size >> 16);
   2105 		}
   2106 		/* load the count in */
   2107 		NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
   2108 
   2109 		/*
   2110 		 * Note that if `size' is 0, we've already transceived
   2111 		 * all the bytes we want but we're still in DATA PHASE.
   2112 		 * Apparently, the device needs padding. Also, a
   2113 		 * transfer size of 0 means "maximum" to the chip
   2114 		 * DMA logic.
   2115 		 */
   2116 		NCRCMD(sc,
   2117 		       (size==0?NCRCMD_TRPAD:NCRCMD_TRANS)|NCRCMD_DMA);
   2118 		NCRDMA_GO(sc);
   2119 		return (1);
   2120 	case STATUS_PHASE:
   2121 		NCR_PHASE(("STATUS_PHASE "));
   2122 		sc->sc_flags |= NCR_ICCS;
   2123 		NCRCMD(sc, NCRCMD_ICCS);
   2124 		sc->sc_prevphase = STATUS_PHASE;
   2125 		goto shortcut;	/* i.e. expect status results soon */
   2126 		break;
   2127 	case INVALID_PHASE:
   2128 		break;
   2129 	default:
   2130 		printf("%s: unexpected bus phase; resetting\n",
   2131 		    sc->sc_dev.dv_xname);
   2132 		goto reset;
   2133 	}
   2134 
   2135 out:
   2136 	return (1);
   2137 
   2138 reset:
   2139 	ncr53c9x_init(sc, 1);
   2140 	goto out;
   2141 
   2142 finish:
   2143 	ncr53c9x_done(sc, ecb);
   2144 	goto out;
   2145 
   2146 sched:
   2147 	sc->sc_state = NCR_IDLE;
   2148 	ncr53c9x_sched(sc);
   2149 	goto out;
   2150 
   2151 shortcut:
   2152 	/*
   2153 	 * The idea is that many of the SCSI operations take very little
   2154 	 * time, and going away and getting interrupted is too high an
   2155 	 * overhead to pay. For example, selecting, sending a message
   2156 	 * and command and then doing some work can be done in one "pass".
   2157 	 *
   2158 	 * The delay is a heuristic. It is 2 when at 20Mhz, 2 at 25Mhz and 1
   2159 	 * at 40Mhz. This needs testing.
   2160 	 */
   2161 	DELAY(50/sc->sc_freq);
   2162 	if (NCRDMA_ISINTR(sc))
   2163 		goto again;
   2164 	goto out;
   2165 }
   2166 
   2167 void
   2168 ncr53c9x_abort(sc, ecb)
   2169 	struct ncr53c9x_softc *sc;
   2170 	struct ncr53c9x_ecb *ecb;
   2171 {
   2172 
   2173 	/* 2 secs for the abort */
   2174 	ecb->timeout = NCR_ABORT_TIMEOUT;
   2175 	ecb->flags |= ECB_ABORT;
   2176 
   2177 	if (ecb == sc->sc_nexus) {
   2178 		/*
   2179 		 * If we're still selecting, the message will be scheduled
   2180 		 * after selection is complete.
   2181 		 */
   2182 		if (sc->sc_state == NCR_CONNECTED)
   2183 			ncr53c9x_sched_msgout(SEND_ABORT);
   2184 
   2185 		/*
   2186 		 * Reschedule timeout. First, cancel a queued timeout (if any)
   2187 		 * in case someone decides to call ncr53c9x_abort() from
   2188 		 * elsewhere.
   2189 		 */
   2190 		untimeout(ncr53c9x_timeout, ecb);
   2191 		timeout(ncr53c9x_timeout, ecb, (ecb->timeout * hz) / 1000);
   2192 	} else {
   2193 		/* The command should be on the nexus list */
   2194 		if ((ecb->flags & ECB_NEXUS) == 0) {
   2195 			scsi_print_addr(ecb->xs->sc_link);
   2196 			printf("ncr53c9x_abort: not NEXUS\n");
   2197 			ncr53c9x_init(sc, 1);
   2198 		}
   2199 		/*
   2200 		 * Just leave the command on the nexus list.
   2201 		 * XXX - what choice do we have but to reset the SCSI
   2202 		 *	 eventually?
   2203 		 */
   2204 		if (sc->sc_state == NCR_IDLE)
   2205 			ncr53c9x_sched(sc);
   2206 	}
   2207 }
   2208 
   2209 void
   2210 ncr53c9x_timeout(arg)
   2211 	void *arg;
   2212 {
   2213 	struct ncr53c9x_ecb *ecb = arg;
   2214 	struct scsipi_xfer *xs = ecb->xs;
   2215 	struct scsipi_link *sc_link = xs->sc_link;
   2216 	struct ncr53c9x_softc *sc = sc_link->adapter_softc;
   2217 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[sc_link->scsipi_scsi.target];
   2218 	int s;
   2219 
   2220 	scsi_print_addr(sc_link);
   2221 	printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
   2222 	       "<state %d, nexus %p, phase(l %x, c %x, p %x), resid %lx, "
   2223 	       "msg(q %x,o %x) %s>",
   2224 		sc->sc_dev.dv_xname,
   2225 		ecb, ecb->flags, ecb->dleft, ecb->stat,
   2226 		sc->sc_state, sc->sc_nexus,
   2227 		NCR_READ_REG(sc, NCR_STAT),
   2228 		sc->sc_phase, sc->sc_prevphase,
   2229 		(long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
   2230 		NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
   2231 #if NCR53C9X_DEBUG > 1
   2232 	printf("TRACE: %s.", ecb->trace);
   2233 #endif
   2234 
   2235 	s = splbio();
   2236 
   2237 	if (ecb->flags & ECB_ABORT) {
   2238 		/* abort timed out */
   2239 		printf(" AGAIN\n");
   2240 
   2241 		ncr53c9x_init(sc, 1);
   2242 	} else {
   2243 		/* abort the operation that has timed out */
   2244 		printf("\n");
   2245 		xs->error = XS_TIMEOUT;
   2246 		ncr53c9x_abort(sc, ecb);
   2247 
   2248 		/* Disable sync mode if stuck in a data phase */
   2249 		if (ecb == sc->sc_nexus &&
   2250 		    (ti->flags & T_SYNCMODE) != 0 &&
   2251 		    (sc->sc_phase & (MSGI|CDI)) == 0) {
   2252 			scsi_print_addr(sc_link);
   2253 			printf("sync negotiation disabled\n");
   2254 			sc->sc_cfflags |= (1<<(sc_link->scsipi_scsi.target+8));
   2255 		}
   2256 	}
   2257 
   2258 	splx(s);
   2259 }
   2260