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