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