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