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