Home | History | Annotate | Line # | Download | only in ic
ncr53c9x.c revision 1.95
      1 /*	$NetBSD: ncr53c9x.c,v 1.95 2002/08/26 05:17:48 petrov 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/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: ncr53c9x.c,v 1.95 2002/08/26 05:17:48 petrov Exp $");
     81 
     82 #include <sys/param.h>
     83 #include <sys/systm.h>
     84 #include <sys/callout.h>
     85 #include <sys/kernel.h>
     86 #include <sys/errno.h>
     87 #include <sys/ioctl.h>
     88 #include <sys/device.h>
     89 #include <sys/buf.h>
     90 #include <sys/malloc.h>
     91 #include <sys/proc.h>
     92 #include <sys/queue.h>
     93 #include <sys/pool.h>
     94 #include <sys/scsiio.h>
     95 
     96 #include <dev/scsipi/scsi_all.h>
     97 #include <dev/scsipi/scsipi_all.h>
     98 #include <dev/scsipi/scsiconf.h>
     99 #include <dev/scsipi/scsi_message.h>
    100 
    101 #include <dev/ic/ncr53c9xreg.h>
    102 #include <dev/ic/ncr53c9xvar.h>
    103 
    104 int ncr53c9x_debug = NCR_SHOWMISC; /*NCR_SHOWPHASE|NCR_SHOWMISC|NCR_SHOWTRAC|NCR_SHOWCMDS;*/
    105 #ifdef DEBUG
    106 int ncr53c9x_notag = 0;
    107 #endif
    108 
    109 /*static*/ void	ncr53c9x_readregs(struct ncr53c9x_softc *);
    110 /*static*/ void	ncr53c9x_select(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
    111 /*static*/ int ncr53c9x_reselect(struct ncr53c9x_softc *, int, int, int);
    112 /*static*/ void	ncr53c9x_scsi_reset(struct ncr53c9x_softc *);
    113 /*static*/ int	ncr53c9x_poll(struct ncr53c9x_softc *,
    114 			      struct scsipi_xfer *, int);
    115 /*static*/ void	ncr53c9x_sched(struct ncr53c9x_softc *);
    116 /*static*/ void	ncr53c9x_done(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
    117 /*static*/ void	ncr53c9x_msgin(struct ncr53c9x_softc *);
    118 /*static*/ void	ncr53c9x_msgout(struct ncr53c9x_softc *);
    119 /*static*/ void	ncr53c9x_timeout(void *arg);
    120 /*static*/ void	ncr53c9x_watch(void *arg);
    121 /*static*/ void	ncr53c9x_abort(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
    122 /*static*/ void ncr53c9x_dequeue(struct ncr53c9x_softc *,
    123 				struct ncr53c9x_ecb *);
    124 /*static*/ int	ncr53c9x_ioctl(struct scsipi_channel *, u_long,
    125 			       caddr_t, int, struct proc *);
    126 
    127 void ncr53c9x_sense(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
    128 void ncr53c9x_free_ecb(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
    129 struct ncr53c9x_ecb *ncr53c9x_get_ecb(struct ncr53c9x_softc *, int);
    130 
    131 static inline int ncr53c9x_stp2cpb(struct ncr53c9x_softc *, int);
    132 static inline void ncr53c9x_setsync(struct ncr53c9x_softc *,
    133 				    struct ncr53c9x_tinfo *);
    134 void   ncr53c9x_update_xfer_mode (struct ncr53c9x_softc *, int);
    135 static struct ncr53c9x_linfo *ncr53c9x_lunsearch(struct ncr53c9x_tinfo *,
    136 						 int64_t lun);
    137 
    138 static void ncr53c9x_wrfifo(struct ncr53c9x_softc *, u_char *, int);
    139 
    140 static int  ncr53c9x_rdfifo(struct ncr53c9x_softc *, int);
    141 #define NCR_RDFIFO_START   0
    142 #define NCR_RDFIFO_CONTINUE 1
    143 
    144 
    145 #define NCR_SET_COUNT(sc, size) do { \
    146 		NCR_WRITE_REG((sc), NCR_TCL, (size)); 			\
    147 		NCR_WRITE_REG((sc), NCR_TCM, (size) >> 8);		\
    148 		if ((sc->sc_cfg2 & NCRCFG2_FE) || 			\
    149 		    (sc->sc_rev == NCR_VARIANT_FAS366)) {		\
    150 			NCR_WRITE_REG((sc), NCR_TCH, (size) >> 16);	\
    151 		}							\
    152 		if (sc->sc_rev == NCR_VARIANT_FAS366) {			\
    153 			NCR_WRITE_REG(sc, NCR_RCH, 0);			\
    154 		}							\
    155 } while (0)
    156 
    157 static int ecb_pool_initialized = 0;
    158 static struct pool ecb_pool;
    159 
    160 /*
    161  * Names for the NCR53c9x variants, correspnding to the variant tags
    162  * in ncr53c9xvar.h.
    163  */
    164 static const char *ncr53c9x_variant_names[] = {
    165 	"ESP100",
    166 	"ESP100A",
    167 	"ESP200",
    168 	"NCR53C94",
    169 	"NCR53C96",
    170 	"ESP406",
    171 	"FAS408",
    172 	"FAS216",
    173 	"AM53C974",
    174 	"FAS366/HME",
    175 	"NCR53C90 (86C01)",
    176 };
    177 
    178 /*
    179  * Search linked list for LUN info by LUN id.
    180  */
    181 static struct ncr53c9x_linfo *
    182 ncr53c9x_lunsearch(ti, lun)
    183 	struct ncr53c9x_tinfo *ti;
    184 	int64_t lun;
    185 {
    186 	struct ncr53c9x_linfo *li;
    187 	LIST_FOREACH(li, &ti->luns, link)
    188 		if (li->lun == lun)
    189 			return (li);
    190 	return (NULL);
    191 }
    192 
    193 /*
    194  * Attach this instance, and then all the sub-devices
    195  */
    196 void
    197 ncr53c9x_attach(sc)
    198 	struct ncr53c9x_softc *sc;
    199 {
    200 	struct scsipi_adapter *adapt = &sc->sc_adapter;
    201 	struct scsipi_channel *chan = &sc->sc_channel;
    202 
    203 	callout_init(&sc->sc_watchdog);
    204 	/*
    205 	 * Allocate SCSI message buffers.
    206 	 * Front-ends can override allocation to avoid alignment
    207 	 * handling in the DMA engines. Note that that ncr53c9x_msgout()
    208 	 * can request a 1 byte DMA transfer.
    209 	 */
    210 	if (sc->sc_omess == NULL)
    211 		sc->sc_omess = malloc(NCR_MAX_MSG_LEN, M_DEVBUF, M_NOWAIT);
    212 
    213 	if (sc->sc_imess == NULL)
    214 		sc->sc_imess = malloc(NCR_MAX_MSG_LEN + 1, M_DEVBUF, M_NOWAIT);
    215 
    216 	if (sc->sc_omess == NULL || sc->sc_imess == NULL) {
    217 		printf("out of memory\n");
    218 		return;
    219 	}
    220 
    221 	/*
    222 	 * Note, the front-end has set us up to print the chip variation.
    223 	 */
    224 	if (sc->sc_rev >= NCR_VARIANT_MAX) {
    225 		printf("\n%s: unknown variant %d, devices not attached\n",
    226 		    sc->sc_dev.dv_xname, sc->sc_rev);
    227 		return;
    228 	}
    229 
    230 	printf(": %s, %dMHz, SCSI ID %d\n",
    231 	    ncr53c9x_variant_names[sc->sc_rev], sc->sc_freq, sc->sc_id);
    232 
    233 	/*
    234 	 * Treat NCR53C90 with the 86C01 DMA chip exactly as ESP100
    235 	 * from now on.
    236 	 */
    237 	if (sc->sc_rev == NCR_VARIANT_NCR53C90_86C01)
    238 		sc->sc_rev = NCR_VARIANT_ESP100;
    239 
    240 	sc->sc_ccf = FREQTOCCF(sc->sc_freq);
    241 
    242 	/* The value *must not* be == 1. Make it 2 */
    243 	if (sc->sc_ccf == 1)
    244 		sc->sc_ccf = 2;
    245 
    246 	/*
    247 	 * The recommended timeout is 250ms. This register is loaded
    248 	 * with a value calculated as follows, from the docs:
    249 	 *
    250 	 *		(timout period) x (CLK frequency)
    251 	 *	reg = -------------------------------------
    252 	 *		 8192 x (Clock Conversion Factor)
    253 	 *
    254 	 * Since CCF has a linear relation to CLK, this generally computes
    255 	 * to the constant of 153.
    256 	 */
    257 	sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
    258 
    259 	/* CCF register only has 3 bits; 0 is actually 8 */
    260 	sc->sc_ccf &= 7;
    261 
    262 	/*
    263 	 * Fill in the scsipi_adapter.
    264 	 */
    265 	adapt->adapt_dev = &sc->sc_dev;
    266 	adapt->adapt_nchannels = 1;
    267 	adapt->adapt_openings = 256;
    268 	adapt->adapt_max_periph = 256;
    269 	adapt->adapt_ioctl = ncr53c9x_ioctl;
    270 	/* adapt_request initialized by front-end */
    271 	/* adapt_minphys initialized by front-end */
    272 
    273 	/*
    274 	 * Fill in the scsipi_channel.
    275 	 */
    276 	memset(chan, 0, sizeof(*chan));
    277 	chan->chan_adapter = adapt;
    278 	chan->chan_bustype = &scsi_bustype;
    279 	chan->chan_channel = 0;
    280 	chan->chan_ntargets = 8; /* XXX fas has 16(not supported) */
    281 	chan->chan_nluns = 8;
    282 	chan->chan_id = sc->sc_id;
    283 
    284 	/*
    285 	 * Add reference to adapter so that we drop the reference after
    286 	 * config_found() to make sure the adatper is disabled.
    287 	 */
    288 	if (scsipi_adapter_addref(adapt) != 0) {
    289 		printf("%s: unable to enable controller\n",
    290 		    sc->sc_dev.dv_xname);
    291 		return;
    292 	}
    293 
    294 	/* Reset state & bus */
    295 	sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
    296 	sc->sc_state = 0;
    297 	ncr53c9x_init(sc, 1);
    298 
    299 	/*
    300 	 * Now try to attach all the sub-devices
    301 	 */
    302 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    303 
    304 	scsipi_adapter_delref(adapt);
    305 	callout_reset(&sc->sc_watchdog, 60*hz, ncr53c9x_watch, sc);
    306 }
    307 
    308 int
    309 ncr53c9x_detach(sc, flags)
    310 	struct ncr53c9x_softc *sc;
    311 	int flags;
    312 {
    313 	int error;
    314 
    315 	if (sc->sc_child) {
    316 		error = config_detach(sc->sc_child, flags);
    317 		if (error)
    318 			return (error);
    319 	}
    320 
    321 	free(sc->sc_imess, M_DEVBUF);
    322 	free(sc->sc_omess, M_DEVBUF);
    323 
    324 	return (0);
    325 }
    326 
    327 /*
    328  * This is the generic ncr53c9x reset function. It does not reset the SCSI bus,
    329  * only this controller, but kills any on-going commands, and also stops
    330  * and resets the DMA.
    331  *
    332  * After reset, registers are loaded with the defaults from the attach
    333  * routine above.
    334  */
    335 void
    336 ncr53c9x_reset(sc)
    337 	struct ncr53c9x_softc *sc;
    338 {
    339 
    340 	/* reset DMA first */
    341 	NCRDMA_RESET(sc);
    342 
    343 	/* reset SCSI chip */
    344 	NCRCMD(sc, NCRCMD_RSTCHIP);
    345 	NCRCMD(sc, NCRCMD_NOP);
    346 	DELAY(500);
    347 
    348 	/* do these backwards, and fall through */
    349 	switch (sc->sc_rev) {
    350 	case NCR_VARIANT_ESP406:
    351 	case NCR_VARIANT_FAS408:
    352 		NCR_WRITE_REG(sc, NCR_CFG5, sc->sc_cfg5 | NCRCFG5_SINT);
    353 		NCR_WRITE_REG(sc, NCR_CFG4, sc->sc_cfg4);
    354 	case NCR_VARIANT_AM53C974:
    355 	case NCR_VARIANT_FAS216:
    356 	case NCR_VARIANT_NCR53C94:
    357 	case NCR_VARIANT_NCR53C96:
    358 	case NCR_VARIANT_ESP200:
    359 		sc->sc_features |= NCR_F_HASCFG3;
    360 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    361 	case NCR_VARIANT_ESP100A:
    362 		sc->sc_features |= NCR_F_SELATN3;
    363 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    364 	case NCR_VARIANT_ESP100:
    365 		NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
    366 		NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
    367 		NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
    368 		NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
    369 		break;
    370 
    371 	case NCR_VARIANT_FAS366:
    372 		sc->sc_features |=
    373 		    NCR_F_HASCFG3 | NCR_F_FASTSCSI | NCR_F_SELATN3;
    374 		sc->sc_cfg3 = NCRFASCFG3_FASTCLK | NCRFASCFG3_OBAUTO;
    375 		sc->sc_cfg3_fscsi = NCRFASCFG3_FASTSCSI;
    376 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    377 		sc->sc_cfg2 = 0; /* NCRCFG2_HMEFE| NCRCFG2_HME32 */
    378 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    379 		NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
    380 		NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
    381 		NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
    382 		NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
    383 		break;
    384 
    385 	default:
    386 		printf("%s: unknown revision code, assuming ESP100\n",
    387 		    sc->sc_dev.dv_xname);
    388 		NCR_WRITE_REG(sc, NCR_CFG1, sc->sc_cfg1);
    389 		NCR_WRITE_REG(sc, NCR_CCF, sc->sc_ccf);
    390 		NCR_WRITE_REG(sc, NCR_SYNCOFF, 0);
    391 		NCR_WRITE_REG(sc, NCR_TIMEOUT, sc->sc_timeout);
    392 	}
    393 
    394 	if (sc->sc_rev == NCR_VARIANT_AM53C974)
    395 		NCR_WRITE_REG(sc, NCR_AMDCFG4, sc->sc_cfg4);
    396 
    397 #if 0
    398 	printf("%s: ncr53c9x_reset: revision %d\n",
    399 	       sc->sc_dev.dv_xname, sc->sc_rev);
    400 	printf("%s: ncr53c9x_reset: cfg1 0x%x, cfg2 0x%x, cfg3 0x%x, "
    401 	    "ccf 0x%x, timeout 0x%x\n",
    402 	    sc->sc_dev.dv_xname, sc->sc_cfg1, sc->sc_cfg2, sc->sc_cfg3,
    403 	    sc->sc_ccf, sc->sc_timeout);
    404 #endif
    405 }
    406 
    407 /*
    408  * Reset the SCSI bus, but not the chip
    409  */
    410 void
    411 ncr53c9x_scsi_reset(sc)
    412 	struct ncr53c9x_softc *sc;
    413 {
    414 
    415 	(*sc->sc_glue->gl_dma_stop)(sc);
    416 
    417 	printf("%s: resetting SCSI bus\n", sc->sc_dev.dv_xname);
    418 	NCRCMD(sc, NCRCMD_RSTSCSI);
    419 }
    420 
    421 /*
    422  * Initialize ncr53c9x state machine
    423  */
    424 void
    425 ncr53c9x_init(sc, doreset)
    426 	struct ncr53c9x_softc *sc;
    427 	int doreset;
    428 {
    429 	struct ncr53c9x_ecb *ecb;
    430 	struct ncr53c9x_linfo *li;
    431 	int i, r;
    432 
    433 	NCR_MISC(("[NCR_INIT(%d) %d] ", doreset, sc->sc_state));
    434 
    435 	if (!ecb_pool_initialized) {
    436 		/* All instances share this pool */
    437 		pool_init(&ecb_pool, sizeof(struct ncr53c9x_ecb), 0, 0, 0,
    438 		    "ncr53c9x_ecb", NULL);
    439 		ecb_pool_initialized = 1;
    440 	}
    441 
    442 	if (sc->sc_state == 0) {
    443 		/* First time through; initialize. */
    444 
    445 		TAILQ_INIT(&sc->ready_list);
    446 		sc->sc_nexus = NULL;
    447 		memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
    448 		for (r = 0; r < NCR_NTARG; r++) {
    449 			LIST_INIT(&sc->sc_tinfo[r].luns);
    450 		}
    451 	} else {
    452 		/* Cancel any active commands. */
    453 		sc->sc_state = NCR_CLEANING;
    454 		sc->sc_msgify = 0;
    455 		if ((ecb = sc->sc_nexus) != NULL) {
    456 			ecb->xs->error = XS_TIMEOUT;
    457 			ncr53c9x_done(sc, ecb);
    458 		}
    459 		/* Cancel outstanding disconnected commands on each LUN */
    460 		for (r = 0; r < 8; r++) {
    461 			LIST_FOREACH(li, &sc->sc_tinfo[r].luns, link) {
    462 				if ((ecb = li->untagged) != NULL) {
    463 					li->untagged = NULL;
    464 					/*
    465 					 * XXXXXXX
    466 					 *
    467 					 * Should we terminate a command
    468 					 * that never reached the disk?
    469 					 */
    470 					li->busy = 0;
    471 					ecb->xs->error = XS_TIMEOUT;
    472 					ncr53c9x_done(sc, ecb);
    473 				}
    474 				for (i = 0; i < 256; i++)
    475 					if ((ecb = li->queued[i])) {
    476 						li->queued[i] = NULL;
    477 						ecb->xs->error = XS_TIMEOUT;
    478 						ncr53c9x_done(sc, ecb);
    479 					}
    480 				li->used = 0;
    481 			}
    482 		}
    483 	}
    484 
    485 	/*
    486 	 * reset the chip to a known state
    487 	 */
    488 	ncr53c9x_reset(sc);
    489 
    490 	sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
    491 	for (r = 0; r < 8; r++) {
    492 		struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[r];
    493 /* XXX - config flags per target: low bits: no reselect; high bits: no synch */
    494 
    495 		ti->flags = ((sc->sc_minsync && !(sc->sc_cfflags & (1<<(r+8))))
    496 		    ? 0 : T_SYNCHOFF) |
    497 		    ((sc->sc_cfflags & (1<<r)) ? T_RSELECTOFF : 0);
    498 #ifdef DEBUG
    499 		if (ncr53c9x_notag)
    500 			ti->flags &= ~T_TAG;
    501 #endif
    502 		ti->period = sc->sc_minsync;
    503 		ti->offset = 0;
    504 		ti->cfg3   = 0;
    505 
    506 		ncr53c9x_update_xfer_mode(sc, r);
    507 	}
    508 
    509 	if (doreset) {
    510 		sc->sc_state = NCR_SBR;
    511 		NCRCMD(sc, NCRCMD_RSTSCSI);
    512 	} else {
    513 		sc->sc_state = NCR_IDLE;
    514 		ncr53c9x_sched(sc);
    515 	}
    516 }
    517 
    518 /*
    519  * Read the NCR registers, and save their contents for later use.
    520  * NCR_STAT, NCR_STEP & NCR_INTR are mostly zeroed out when reading
    521  * NCR_INTR - so make sure it is the last read.
    522  *
    523  * I think that (from reading the docs) most bits in these registers
    524  * only make sense when he DMA CSR has an interrupt showing. Call only
    525  * if an interrupt is pending.
    526  */
    527 __inline__ void
    528 ncr53c9x_readregs(sc)
    529 	struct ncr53c9x_softc *sc;
    530 {
    531 
    532 	sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT);
    533 	/* Only the stepo bits are of interest */
    534 	sc->sc_espstep = NCR_READ_REG(sc, NCR_STEP) & NCRSTEP_MASK;
    535 
    536 	if (sc->sc_rev == NCR_VARIANT_FAS366)
    537 		sc->sc_espstat2 = NCR_READ_REG(sc, NCR_STAT2);
    538 
    539 	sc->sc_espintr = NCR_READ_REG(sc, NCR_INTR);
    540 
    541 	if (sc->sc_glue->gl_clear_latched_intr != NULL)
    542 		(*sc->sc_glue->gl_clear_latched_intr)(sc);
    543 
    544 	/*
    545 	 * Determine the SCSI bus phase, return either a real SCSI bus phase
    546 	 * or some pseudo phase we use to detect certain exceptions.
    547 	 */
    548 
    549 	sc->sc_phase = (sc->sc_espintr & NCRINTR_DIS) ?
    550 	    /* Disconnected */ BUSFREE_PHASE : sc->sc_espstat & NCRSTAT_PHASE;
    551 
    552 	NCR_INTS(("regs[intr=%02x,stat=%02x,step=%02x,stat2=%02x] ",
    553 	    sc->sc_espintr, sc->sc_espstat, sc->sc_espstep, sc->sc_espstat2));
    554 }
    555 
    556 /*
    557  * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
    558  */
    559 static inline int
    560 ncr53c9x_stp2cpb(sc, period)
    561 	struct ncr53c9x_softc *sc;
    562 	int period;
    563 {
    564 	int v;
    565 	v = (sc->sc_freq * period) / 250;
    566 	if (ncr53c9x_cpb2stp(sc, v) < period)
    567 		/* Correct round-down error */
    568 		v++;
    569 	return (v);
    570 }
    571 
    572 static inline void
    573 ncr53c9x_setsync(sc, ti)
    574 	struct ncr53c9x_softc *sc;
    575 	struct ncr53c9x_tinfo *ti;
    576 {
    577 	u_char syncoff, synctp;
    578 	u_char cfg3 = sc->sc_cfg3 | ti->cfg3;
    579 
    580 	if (ti->flags & T_SYNCMODE) {
    581 		syncoff = ti->offset;
    582 		synctp = ncr53c9x_stp2cpb(sc, ti->period);
    583 		if (sc->sc_features & NCR_F_FASTSCSI) {
    584 			/*
    585 			 * If the period is 200ns or less (ti->period <= 50),
    586 			 * put the chip in Fast SCSI mode.
    587 			 */
    588 			if (ti->period <= 50)
    589 				/*
    590 				 * There are (at least) 4 variations of the
    591 				 * configuration 3 register.  The drive attach
    592 				 * routine sets the appropriate bit to put the
    593 				 * chip into Fast SCSI mode so that it doesn't
    594 				 * have to be figured out here each time.
    595 				 */
    596 				cfg3 |= sc->sc_cfg3_fscsi;
    597 		}
    598 
    599 		/*
    600 		 * Am53c974 requires different SYNCTP values when the
    601 		 * FSCSI bit is off.
    602 		 */
    603 		if (sc->sc_rev == NCR_VARIANT_AM53C974 &&
    604 		    (cfg3 & NCRAMDCFG3_FSCSI) == 0)
    605 			synctp--;
    606 	} else {
    607 		syncoff = 0;
    608 		synctp = 0;
    609 	}
    610 
    611 	if (sc->sc_features & NCR_F_HASCFG3)
    612 		NCR_WRITE_REG(sc, NCR_CFG3, cfg3);
    613 
    614 	NCR_WRITE_REG(sc, NCR_SYNCOFF, syncoff);
    615 	NCR_WRITE_REG(sc, NCR_SYNCTP, synctp);
    616 }
    617 
    618 /*
    619  * Send a command to a target, set the driver state to NCR_SELECTING
    620  * and let the caller take care of the rest.
    621  *
    622  * Keeping this as a function allows me to say that this may be done
    623  * by DMA instead of programmed I/O soon.
    624  */
    625 void
    626 ncr53c9x_select(sc, ecb)
    627 	struct ncr53c9x_softc *sc;
    628 	struct ncr53c9x_ecb *ecb;
    629 {
    630 	struct scsipi_periph *periph = ecb->xs->xs_periph;
    631 	int target = periph->periph_target;
    632 	int lun = periph->periph_lun;
    633 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
    634 	int tiflags = ti->flags;
    635 	u_char *cmd;
    636 	int clen;
    637 	int selatn3, selatns;
    638 	size_t dmasize;
    639 
    640 	NCR_TRACE(("[ncr53c9x_select(t%d,l%d,cmd:%x,tag:%x,%x)] ",
    641 	    target, lun, ecb->cmd.cmd.opcode, ecb->tag[0], ecb->tag[1]));
    642 
    643 	sc->sc_state = NCR_SELECTING;
    644 	/*
    645 	 * Schedule the timeout now, the first time we will go away
    646 	 * expecting to come back due to an interrupt, because it is
    647 	 * always possible that the interrupt may never happen.
    648 	 */
    649 	if ((ecb->xs->xs_control & XS_CTL_POLL) == 0) {
    650 		callout_reset(&ecb->xs->xs_callout, mstohz(ecb->timeout),
    651 		    ncr53c9x_timeout, ecb);
    652 	}
    653 
    654 	/*
    655 	 * The docs say the target register is never reset, and I
    656 	 * can't think of a better place to set it
    657 	 */
    658 	if (sc->sc_rev == NCR_VARIANT_FAS366) {
    659 		NCRCMD(sc, NCRCMD_FLUSH);
    660 		NCR_WRITE_REG(sc, NCR_SELID, target | NCR_BUSID_HME);
    661 	} else {
    662 		NCR_WRITE_REG(sc, NCR_SELID, target);
    663 	}
    664 	ncr53c9x_setsync(sc, ti);
    665 
    666 	if ((ecb->flags & ECB_SENSE) != 0) {
    667 		/*
    668 		 * For REQUEST SENSE, we should not send an IDENTIFY or
    669 		 * otherwise mangle the target.  There should be no MESSAGE IN
    670 		 * phase.
    671 		 */
    672 		if (sc->sc_features & NCR_F_DMASELECT) {
    673 			/* setup DMA transfer for command */
    674 			dmasize = clen = ecb->clen;
    675 			sc->sc_cmdlen = clen;
    676 			sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
    677 
    678 			/* Program the SCSI counter */
    679 			NCR_SET_COUNT(sc, dmasize);
    680 
    681 			if (sc->sc_rev != NCR_VARIANT_FAS366)
    682 				NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
    683 
    684 			/* And get the targets attention */
    685 			NCRCMD(sc, NCRCMD_SELNATN | NCRCMD_DMA);
    686 			NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0,
    687 			    &dmasize);
    688 			NCRDMA_GO(sc);
    689 		} else {
    690 			ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
    691 			NCRCMD(sc, NCRCMD_SELNATN);
    692 		}
    693 		return;
    694 	}
    695 
    696 	selatn3 = selatns = 0;
    697 	if (ecb->tag[0] != 0) {
    698 		if (sc->sc_features & NCR_F_SELATN3)
    699 			/* use SELATN3 to send tag messages */
    700 			selatn3 = 1;
    701 		else
    702 			/* We don't have SELATN3; use SELATNS to send tags */
    703 			selatns = 1;
    704 	}
    705 
    706 	if (ti->flags & T_NEGOTIATE) {
    707 		/* We have to use SELATNS to send sync/wide messages */
    708 		selatn3 = 0;
    709 		selatns = 1;
    710 	}
    711 
    712 	cmd = (u_char *)&ecb->cmd.cmd;
    713 
    714 	if (selatn3) {
    715 		/* We'll use tags with SELATN3 */
    716 		clen = ecb->clen + 3;
    717 		cmd -= 3;
    718 		cmd[0] = MSG_IDENTIFY(lun, 1);	/* msg[0] */
    719 		cmd[1] = ecb->tag[0];		/* msg[1] */
    720 		cmd[2] = ecb->tag[1];		/* msg[2] */
    721 	} else {
    722 		/* We don't have tags, or will send messages with SELATNS */
    723 		clen = ecb->clen + 1;
    724 		cmd -= 1;
    725 		cmd[0] = MSG_IDENTIFY(lun, (tiflags & T_RSELECTOFF) == 0);
    726 	}
    727 
    728 	if ((sc->sc_features & NCR_F_DMASELECT) && !selatns) {
    729 
    730 		/* setup DMA transfer for command */
    731 		dmasize = clen;
    732 		sc->sc_cmdlen = clen;
    733 		sc->sc_cmdp = cmd;
    734 
    735 		/* Program the SCSI counter */
    736 		NCR_SET_COUNT(sc, dmasize);
    737 
    738 		/* load the count in */
    739 		/* if (sc->sc_rev != NCR_VARIANT_FAS366) */
    740 			NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
    741 
    742 		/* And get the targets attention */
    743 		if (selatn3) {
    744 			sc->sc_msgout = SEND_TAG;
    745 			sc->sc_flags |= NCR_ATN;
    746 			NCRCMD(sc, NCRCMD_SELATN3 | NCRCMD_DMA);
    747 		} else
    748 			NCRCMD(sc, NCRCMD_SELATN | NCRCMD_DMA);
    749 		NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen, 0, &dmasize);
    750 		NCRDMA_GO(sc);
    751 		return;
    752 	}
    753 
    754 	/*
    755 	 * Who am I. This is where we tell the target that we are
    756 	 * happy for it to disconnect etc.
    757 	 */
    758 
    759 	/* Now get the command into the FIFO */
    760 	ncr53c9x_wrfifo(sc, cmd, clen);
    761 
    762 	/* And get the targets attention */
    763 	if (selatns) {
    764 		NCR_MSGS(("SELATNS \n"));
    765 		/* Arbitrate, select and stop after IDENTIFY message */
    766 		NCRCMD(sc, NCRCMD_SELATNS);
    767 	} else if (selatn3) {
    768 		sc->sc_msgout = SEND_TAG;
    769 		sc->sc_flags |= NCR_ATN;
    770 		NCRCMD(sc, NCRCMD_SELATN3);
    771 	} else
    772 		NCRCMD(sc, NCRCMD_SELATN);
    773 }
    774 
    775 void
    776 ncr53c9x_free_ecb(sc, ecb)
    777 	struct ncr53c9x_softc *sc;
    778 	struct ncr53c9x_ecb *ecb;
    779 {
    780 	int s;
    781 
    782 	s = splbio();
    783 	ecb->flags = 0;
    784 	pool_put(&ecb_pool, (void *)ecb);
    785 	splx(s);
    786 	return;
    787 }
    788 
    789 struct ncr53c9x_ecb *
    790 ncr53c9x_get_ecb(sc, flags)
    791 	struct ncr53c9x_softc *sc;
    792 	int flags;
    793 {
    794 	struct ncr53c9x_ecb *ecb;
    795 	int s;
    796 
    797 	s = splbio();
    798 	ecb = (struct ncr53c9x_ecb *)pool_get(&ecb_pool, PR_NOWAIT);
    799 	splx(s);
    800 	if (ecb) {
    801 		memset(ecb, 0, sizeof(*ecb));
    802 		ecb->flags |= ECB_ALLOC;
    803 	}
    804 	return (ecb);
    805 }
    806 
    807 /*
    808  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    809  */
    810 
    811 /*
    812  * Start a SCSI-command
    813  * This function is called by the higher level SCSI-driver to queue/run
    814  * SCSI-commands.
    815  */
    816 
    817 void
    818 ncr53c9x_scsipi_request(chan, req, arg)
    819 	struct scsipi_channel *chan;
    820 	scsipi_adapter_req_t req;
    821 	void *arg;
    822 {
    823 	struct scsipi_xfer *xs;
    824 	struct scsipi_periph *periph;
    825 	struct ncr53c9x_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    826 	struct ncr53c9x_ecb *ecb;
    827 	int s, flags;
    828 
    829 	NCR_TRACE(("[ncr53c9x_scsipi_request] "));
    830 
    831 	switch (req) {
    832 	case ADAPTER_REQ_RUN_XFER:
    833 		xs = arg;
    834 		periph = xs->xs_periph;
    835 		flags = xs->xs_control;
    836 
    837 		NCR_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    838 		    periph->periph_target));
    839 
    840 		/* Get an ECB to use. */
    841 		ecb = ncr53c9x_get_ecb(sc, xs->xs_control);
    842 		/*
    843 		 * This should never happen as we track resources
    844 		 * in the mid-layer.
    845 		 */
    846 		if (ecb == NULL) {
    847 			scsipi_printaddr(periph);
    848 			printf("unable to allocate ecb\n");
    849 			xs->error = XS_RESOURCE_SHORTAGE;
    850 			scsipi_done(xs);
    851 			return;
    852 		}
    853 
    854 		/* Initialize ecb */
    855 		ecb->xs = xs;
    856 		ecb->timeout = xs->timeout;
    857 
    858 		if (flags & XS_CTL_RESET) {
    859 			ecb->flags |= ECB_RESET;
    860 			ecb->clen = 0;
    861 			ecb->dleft = 0;
    862 		} else {
    863 			memcpy(&ecb->cmd.cmd, xs->cmd, xs->cmdlen);
    864 			ecb->clen = xs->cmdlen;
    865 			ecb->daddr = xs->data;
    866 			ecb->dleft = xs->datalen;
    867 		}
    868 		ecb->stat = 0;
    869 
    870 		s = splbio();
    871 
    872 		TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
    873 		ecb->flags |= ECB_READY;
    874 		if (sc->sc_state == NCR_IDLE)
    875 			ncr53c9x_sched(sc);
    876 
    877 		splx(s);
    878 
    879 		if ((flags & XS_CTL_POLL) == 0)
    880 			return;
    881 
    882 		/* Not allowed to use interrupts, use polling instead */
    883 		if (ncr53c9x_poll(sc, xs, ecb->timeout)) {
    884 			ncr53c9x_timeout(ecb);
    885 			if (ncr53c9x_poll(sc, xs, ecb->timeout))
    886 				ncr53c9x_timeout(ecb);
    887 		}
    888 		return;
    889 
    890 	case ADAPTER_REQ_GROW_RESOURCES:
    891 		/* XXX Not supported. */
    892 		return;
    893 
    894 	case ADAPTER_REQ_SET_XFER_MODE:
    895 	    {
    896 		struct ncr53c9x_tinfo *ti;
    897 		struct scsipi_xfer_mode *xm = arg;
    898 
    899 		ti = &sc->sc_tinfo[xm->xm_target];
    900 		ti->flags &= ~(T_NEGOTIATE|T_SYNCMODE);
    901 		ti->period = 0;
    902 		ti->offset = 0;
    903 
    904 		if ((sc->sc_cfflags & (1<<(xm->xm_target+16))) == 0 &&
    905 		    (xm->xm_mode & PERIPH_CAP_TQING)) {
    906 			NCR_MISC(("%s: target %d: tagged queuing\n",
    907 			    sc->sc_dev.dv_xname, xm->xm_target));
    908 			ti->flags |= T_TAG;
    909 		} else
    910 			ti->flags &= ~T_TAG;
    911 
    912 		if ((xm->xm_mode & PERIPH_CAP_WIDE16) != 0) {
    913 			NCR_MISC(("%s: target %d: wide scsi negotiation\n",
    914 			    sc->sc_dev.dv_xname, xm->xm_target));
    915 			if (sc->sc_rev == NCR_VARIANT_FAS366) {
    916 				ti->flags |= T_WIDE;
    917 				ti->width = 1;
    918 			}
    919 		}
    920 
    921 		if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
    922 		    (ti->flags & T_SYNCHOFF) == 0 && sc->sc_minsync != 0) {
    923 			NCR_MISC(("%s: target %d: sync negotiation\n",
    924 			    sc->sc_dev.dv_xname, xm->xm_target));
    925 			ti->flags |= T_NEGOTIATE;
    926 			ti->period = sc->sc_minsync;
    927 		}
    928 		/*
    929 		 * If we're not going to negotiate, send the notification
    930 		 * now, since it won't happen later.
    931 		 */
    932 		if ((ti->flags & T_NEGOTIATE) == 0)
    933 			ncr53c9x_update_xfer_mode(sc, xm->xm_target);
    934 		return;
    935 	    }
    936 	}
    937 }
    938 
    939 void
    940 ncr53c9x_update_xfer_mode(sc, target)
    941 	struct ncr53c9x_softc *sc;
    942 	int target;
    943 {
    944 	struct scsipi_xfer_mode xm;
    945 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[target];
    946 
    947 	xm.xm_target = target;
    948 	xm.xm_mode = 0;
    949 	xm.xm_period = 0;
    950 	xm.xm_offset = 0;
    951 
    952 	if (ti->flags & T_SYNCMODE) {
    953 		xm.xm_mode |= PERIPH_CAP_SYNC;
    954 		xm.xm_period = ti->period;
    955 		xm.xm_offset = ti->offset;
    956 	}
    957 	if (ti->width)
    958 		xm.xm_mode |= PERIPH_CAP_WIDE16;
    959 
    960 	if ((ti->flags & (T_RSELECTOFF|T_TAG)) == T_TAG)
    961 		xm.xm_mode |= PERIPH_CAP_TQING;
    962 
    963 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
    964 }
    965 
    966 /*
    967  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    968  */
    969 int
    970 ncr53c9x_poll(sc, xs, count)
    971 	struct ncr53c9x_softc *sc;
    972 	struct scsipi_xfer *xs;
    973 	int count;
    974 {
    975 
    976 	NCR_TRACE(("[ncr53c9x_poll] "));
    977 	while (count) {
    978 		if (NCRDMA_ISINTR(sc)) {
    979 			ncr53c9x_intr(sc);
    980 		}
    981 #if alternatively
    982 		if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT)
    983 			ncr53c9x_intr(sc);
    984 #endif
    985 		if ((xs->xs_status & XS_STS_DONE) != 0)
    986 			return (0);
    987 		if (sc->sc_state == NCR_IDLE) {
    988 			NCR_TRACE(("[ncr53c9x_poll: rescheduling] "));
    989 			ncr53c9x_sched(sc);
    990 		}
    991 		DELAY(1000);
    992 		count--;
    993 	}
    994 	return (1);
    995 }
    996 
    997 int
    998 ncr53c9x_ioctl(chan, cmd, arg, flag, p)
    999 	struct scsipi_channel *chan;
   1000 	u_long cmd;
   1001 	caddr_t arg;
   1002 	int flag;
   1003 	struct proc *p;
   1004 {
   1005 	struct ncr53c9x_softc *sc = (void *)chan->chan_adapter->adapt_dev;
   1006 	int s, error = 0;
   1007 
   1008 	switch (cmd) {
   1009 	case SCBUSIORESET:
   1010 		s = splbio();
   1011 		ncr53c9x_scsi_reset(sc);
   1012 		splx(s);
   1013 		break;
   1014 	default:
   1015 		error = ENOTTY;
   1016 		break;
   1017 	}
   1018 	return (error);
   1019 }
   1020 
   1021 
   1022 /*
   1023  * LOW LEVEL SCSI UTILITIES
   1024  */
   1025 
   1026 /*
   1027  * Schedule a scsi operation.  This has now been pulled out of the interrupt
   1028  * handler so that we may call it from ncr53c9x_scsipi_request and
   1029  * ncr53c9x_done.  This may save us an unecessary interrupt just to get
   1030  * things going.  Should only be called when state == NCR_IDLE and at bio pl.
   1031  */
   1032 void
   1033 ncr53c9x_sched(sc)
   1034 	struct ncr53c9x_softc *sc;
   1035 {
   1036 	struct ncr53c9x_ecb *ecb;
   1037 	struct scsipi_periph *periph;
   1038 	struct ncr53c9x_tinfo *ti;
   1039 	int lun;
   1040 	struct ncr53c9x_linfo *li;
   1041 	int s, tag;
   1042 
   1043 	NCR_TRACE(("[ncr53c9x_sched] "));
   1044 	if (sc->sc_state != NCR_IDLE)
   1045 		panic("ncr53c9x_sched: not IDLE (state=%d)", sc->sc_state);
   1046 
   1047 	/*
   1048 	 * Find first ecb in ready queue that is for a target/lunit
   1049 	 * combinations that is not busy.
   1050 	 */
   1051 	for (ecb = TAILQ_FIRST(&sc->ready_list); ecb != NULL;
   1052 	    ecb = TAILQ_NEXT(ecb, chain)) {
   1053 		periph = ecb->xs->xs_periph;
   1054 		ti = &sc->sc_tinfo[periph->periph_target];
   1055 		lun = periph->periph_lun;
   1056 
   1057 		/* Select type of tag for this command */
   1058 		if ((ti->flags & (T_RSELECTOFF)) != 0)
   1059 			tag = 0;
   1060 		else if ((ti->flags & (T_TAG)) == 0)
   1061 			tag = 0;
   1062 		else if ((ecb->flags & ECB_SENSE) != 0)
   1063 			tag = 0;
   1064 		else
   1065 			tag = ecb->xs->xs_tag_type;
   1066 #if 0
   1067 		/* XXXX Use tags for polled commands? */
   1068 		if (ecb->xs->xs_control & XS_CTL_POLL)
   1069 			tag = 0;
   1070 #endif
   1071 
   1072 		s = splbio();
   1073 		li = TINFO_LUN(ti, lun);
   1074 		if (li == NULL) {
   1075 			/* Initialize LUN info and add to list. */
   1076 			if ((li = malloc(sizeof(*li),
   1077 			    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) {
   1078 				splx(s);
   1079 				continue;
   1080 			}
   1081 			li->lun = lun;
   1082 
   1083 			LIST_INSERT_HEAD(&ti->luns, li, link);
   1084 			if (lun < NCR_NLUN)
   1085 				ti->lun[lun] = li;
   1086 		}
   1087 		li->last_used = time.tv_sec;
   1088 		if (tag == 0) {
   1089 			/* Try to issue this as an un-tagged command */
   1090 			if (li->untagged == NULL)
   1091 				li->untagged = ecb;
   1092 		}
   1093 		if (li->untagged != NULL) {
   1094 			tag = 0;
   1095 			if ((li->busy != 1) && li->used == 0) {
   1096 				/* We need to issue this untagged command now */
   1097 				ecb = li->untagged;
   1098 				periph = ecb->xs->xs_periph;
   1099 			} else {
   1100 				/* Not ready yet */
   1101 				splx(s);
   1102 				continue;
   1103 			}
   1104 		}
   1105 		ecb->tag[0] = tag;
   1106 		if (tag != 0) {
   1107 			li->queued[ecb->xs->xs_tag_id] = ecb;
   1108 			ecb->tag[1] = ecb->xs->xs_tag_id;
   1109 			li->used++;
   1110 		}
   1111 		splx(s);
   1112 		if (li->untagged != NULL && (li->busy != 1)) {
   1113 			li->busy = 1;
   1114 			TAILQ_REMOVE(&sc->ready_list, ecb, chain);
   1115 			ecb->flags &= ~ECB_READY;
   1116 			sc->sc_nexus = ecb;
   1117 			ncr53c9x_select(sc, ecb);
   1118 			break;
   1119 		}
   1120 		if (li->untagged == NULL && tag != 0) {
   1121 			TAILQ_REMOVE(&sc->ready_list, ecb, chain);
   1122 			ecb->flags &= ~ECB_READY;
   1123 			sc->sc_nexus = ecb;
   1124 			ncr53c9x_select(sc, ecb);
   1125 			break;
   1126 		} else
   1127 			NCR_TRACE(("%d:%d busy\n",
   1128 			    periph->periph_target,
   1129 			    periph->periph_lun));
   1130 	}
   1131 }
   1132 
   1133 void
   1134 ncr53c9x_sense(sc, ecb)
   1135 	struct ncr53c9x_softc *sc;
   1136 	struct ncr53c9x_ecb *ecb;
   1137 {
   1138 	struct scsipi_xfer *xs = ecb->xs;
   1139 	struct scsipi_periph *periph = xs->xs_periph;
   1140 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
   1141 	struct scsipi_sense *ss = (void *)&ecb->cmd.cmd;
   1142 	struct ncr53c9x_linfo *li;
   1143 	int lun = periph->periph_lun;
   1144 
   1145 	NCR_TRACE(("requesting sense "));
   1146 	/* Next, setup a request sense command block */
   1147 	memset(ss, 0, sizeof(*ss));
   1148 	ss->opcode = REQUEST_SENSE;
   1149 	ss->byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
   1150 	ss->length = sizeof(struct scsipi_sense_data);
   1151 	ecb->clen = sizeof(*ss);
   1152 	ecb->daddr = (char *)&xs->sense.scsi_sense;
   1153 	ecb->dleft = sizeof(struct scsipi_sense_data);
   1154 	ecb->flags |= ECB_SENSE;
   1155 	ecb->timeout = NCR_SENSE_TIMEOUT;
   1156 	ti->senses++;
   1157 	li = TINFO_LUN(ti, lun);
   1158 	if (li->busy)
   1159 		li->busy = 0;
   1160 	ncr53c9x_dequeue(sc, ecb);
   1161 	li->untagged = ecb; /* must be executed first to fix C/A */
   1162 	li->busy = 2;
   1163 	if (ecb == sc->sc_nexus) {
   1164 		ncr53c9x_select(sc, ecb);
   1165 	} else {
   1166 		TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
   1167 		ecb->flags |= ECB_READY;
   1168 		if (sc->sc_state == NCR_IDLE)
   1169 			ncr53c9x_sched(sc);
   1170 	}
   1171 }
   1172 
   1173 /*
   1174  * POST PROCESSING OF SCSI_CMD (usually current)
   1175  */
   1176 void
   1177 ncr53c9x_done(sc, ecb)
   1178 	struct ncr53c9x_softc *sc;
   1179 	struct ncr53c9x_ecb *ecb;
   1180 {
   1181 	struct scsipi_xfer *xs = ecb->xs;
   1182 	struct scsipi_periph *periph = xs->xs_periph;
   1183 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
   1184 	int lun = periph->periph_lun;
   1185 	struct ncr53c9x_linfo *li = TINFO_LUN(ti, lun);
   1186 
   1187 	NCR_TRACE(("[ncr53c9x_done(error:%x)] ", xs->error));
   1188 
   1189 	callout_stop(&ecb->xs->xs_callout);
   1190 
   1191 	/*
   1192 	 * Now, if we've come here with no error code, i.e. we've kept the
   1193 	 * initial XS_NOERROR, and the status code signals that we should
   1194 	 * check sense, we'll need to set up a request sense cmd block and
   1195 	 * push the command back into the ready queue *before* any other
   1196 	 * commands for this target/lunit, else we lose the sense info.
   1197 	 * We don't support chk sense conditions for the request sense cmd.
   1198 	 */
   1199 	if (xs->error == XS_NOERROR) {
   1200 		xs->status = ecb->stat;
   1201 		if ((ecb->flags & ECB_ABORT) != 0) {
   1202 			xs->error = XS_TIMEOUT;
   1203 		} else if ((ecb->flags & ECB_SENSE) != 0) {
   1204 			xs->error = XS_SENSE;
   1205 		} else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
   1206 			/* First, save the return values */
   1207 			xs->resid = ecb->dleft;
   1208 			ncr53c9x_sense(sc, ecb);
   1209 			return;
   1210 		} else {
   1211 			xs->resid = ecb->dleft;
   1212 		}
   1213 		if (xs->status == SCSI_QUEUE_FULL || xs->status == XS_BUSY)
   1214 			xs->error = XS_BUSY;
   1215 	}
   1216 
   1217 #ifdef NCR53C9X_DEBUG
   1218 	if (ncr53c9x_debug & NCR_SHOWTRAC) {
   1219 		if (xs->resid != 0)
   1220 			printf("resid=%d ", xs->resid);
   1221 		if (xs->error == XS_SENSE)
   1222 			printf("sense=0x%02x\n",
   1223 			    xs->sense.scsi_sense.error_code);
   1224 		else
   1225 			printf("error=%d\n", xs->error);
   1226 	}
   1227 #endif
   1228 
   1229 	/*
   1230 	 * Remove the ECB from whatever queue it's on.
   1231 	 */
   1232 	ncr53c9x_dequeue(sc, ecb);
   1233 	if (ecb == sc->sc_nexus) {
   1234 		sc->sc_nexus = NULL;
   1235 		if (sc->sc_state != NCR_CLEANING) {
   1236 			sc->sc_state = NCR_IDLE;
   1237 			ncr53c9x_sched(sc);
   1238 		}
   1239 	}
   1240 
   1241 	if (xs->error == XS_SELTIMEOUT) {
   1242 		/* Selection timeout -- discard this LUN if empty */
   1243 		if (li->untagged == NULL && li->used == 0) {
   1244 			if (lun < NCR_NLUN)
   1245 				ti->lun[lun] = NULL;
   1246 			LIST_REMOVE(li, link);
   1247 			free(li, M_DEVBUF);
   1248 		}
   1249 	}
   1250 
   1251 	ncr53c9x_free_ecb(sc, ecb);
   1252 	ti->cmds++;
   1253 	scsipi_done(xs);
   1254 }
   1255 
   1256 void
   1257 ncr53c9x_dequeue(sc, ecb)
   1258 	struct ncr53c9x_softc *sc;
   1259 	struct ncr53c9x_ecb *ecb;
   1260 {
   1261 	struct ncr53c9x_tinfo *ti =
   1262 	    &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
   1263 	struct ncr53c9x_linfo *li;
   1264 	int64_t lun = ecb->xs->xs_periph->periph_lun;
   1265 
   1266 	li = TINFO_LUN(ti, lun);
   1267 #ifdef DIAGNOSTIC
   1268 	if (li == NULL || li->lun != lun)
   1269 		panic("ncr53c9x_dequeue: lun %qx for ecb %p does not exist\n",
   1270 		      (long long) lun, ecb);
   1271 #endif
   1272 	if (li->untagged == ecb) {
   1273 		li->busy = 0;
   1274 		li->untagged = NULL;
   1275 	}
   1276 	if (ecb->tag[0] && li->queued[ecb->tag[1]] != NULL) {
   1277 #ifdef DIAGNOSTIC
   1278 		if (li->queued[ecb->tag[1]] != NULL &&
   1279 		    (li->queued[ecb->tag[1]] != ecb))
   1280 			panic("ncr53c9x_dequeue: slot %d for lun %qx has %p "
   1281 			    "instead of ecb %p\n", ecb->tag[1],
   1282 			    (long long) lun,
   1283 			    li->queued[ecb->tag[1]], ecb);
   1284 #endif
   1285 		li->queued[ecb->tag[1]] = NULL;
   1286 		li->used--;
   1287 	}
   1288 
   1289 	if ((ecb->flags & ECB_READY) != 0) {
   1290 		ecb->flags &= ~ECB_READY;
   1291 		TAILQ_REMOVE(&sc->ready_list, ecb, chain);
   1292 	}
   1293 }
   1294 
   1295 /*
   1296  * INTERRUPT/PROTOCOL ENGINE
   1297  */
   1298 
   1299 /*
   1300  * Schedule an outgoing message by prioritizing it, and asserting
   1301  * attention on the bus. We can only do this when we are the initiator
   1302  * else there will be an illegal command interrupt.
   1303  */
   1304 #define ncr53c9x_sched_msgout(m) \
   1305 	do {							\
   1306 		NCR_MSGS(("ncr53c9x_sched_msgout %x %d", m, __LINE__));	\
   1307 		NCRCMD(sc, NCRCMD_SETATN);			\
   1308 		sc->sc_flags |= NCR_ATN;			\
   1309 		sc->sc_msgpriq |= (m);				\
   1310 	} while (0)
   1311 
   1312 static void
   1313 ncr53c9x_flushfifo(struct ncr53c9x_softc *sc)
   1314 {
   1315 	NCR_TRACE(("[flushfifo] "));
   1316 
   1317 	NCRCMD(sc, NCRCMD_FLUSH);
   1318 
   1319 	if (sc->sc_phase == COMMAND_PHASE ||
   1320 	    sc->sc_phase == MESSAGE_OUT_PHASE)
   1321 		DELAY(2);
   1322 }
   1323 
   1324 static int
   1325 ncr53c9x_rdfifo(struct ncr53c9x_softc *sc, int how)
   1326 {
   1327 	int i, n;
   1328 	u_char *buf;
   1329 
   1330 	switch(how) {
   1331 	case NCR_RDFIFO_START:
   1332 		buf = sc->sc_imess;
   1333 		sc->sc_imlen = 0;
   1334 		break;
   1335 	case NCR_RDFIFO_CONTINUE:
   1336 		buf = sc->sc_imess + sc->sc_imlen;
   1337 		break;
   1338 	default:
   1339 		panic("ncr53c9x_rdfifo: bad flag\n");
   1340 		break;
   1341 	}
   1342 
   1343 	/*
   1344 	 * XXX buffer (sc_imess) size for message
   1345 	 */
   1346 
   1347 	n = NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF;
   1348 
   1349 	if (sc->sc_rev == NCR_VARIANT_FAS366) {
   1350 		n *= 2;
   1351 
   1352 		for (i = 0; i < n; i++)
   1353 			buf[i] = NCR_READ_REG(sc, NCR_FIFO);
   1354 
   1355 		if (sc->sc_espstat2 & NCRFAS_STAT2_ISHUTTLE) {
   1356 
   1357 			NCR_WRITE_REG(sc, NCR_FIFO, 0);
   1358 			buf[i++] = NCR_READ_REG(sc, NCR_FIFO);
   1359 
   1360 			NCR_READ_REG(sc, NCR_FIFO);
   1361 
   1362 			ncr53c9x_flushfifo(sc);
   1363 		}
   1364 	} else {
   1365 		for (i = 0; i < n; i++)
   1366 			buf[i] = NCR_READ_REG(sc, NCR_FIFO);
   1367 	}
   1368 
   1369 	sc->sc_imlen += i;
   1370 
   1371 #if 0
   1372 #ifdef NCR53C9X_DEBUG
   1373  	{
   1374 		int j;
   1375 
   1376 		NCR_TRACE(("\n[rdfifo %s (%d):",
   1377 		    (how == NCR_RDFIFO_START) ? "start" : "cont",
   1378 		    (int)sc->sc_imlen));
   1379 		if (ncr53c9x_debug & NCR_SHOWTRAC) {
   1380 			for (j = 0; j < sc->sc_imlen; j++)
   1381 				printf(" %02x", sc->sc_imess[j]);
   1382 			printf("]\n");
   1383 		}
   1384 	}
   1385 #endif
   1386 #endif
   1387 	return sc->sc_imlen;
   1388 }
   1389 
   1390 static void
   1391 ncr53c9x_wrfifo(struct ncr53c9x_softc *sc, u_char *p, int len)
   1392 {
   1393 	int i;
   1394 
   1395 #ifdef NCR53C9X_DEBUG
   1396 	NCR_MSGS(("[wrfifo(%d):", len));
   1397 	if (ncr53c9x_debug & NCR_SHOWMSGS) {
   1398 		for (i = 0; i < len; i++)
   1399 			printf(" %02x", p[i]);
   1400 		printf("]\n");
   1401 	}
   1402 #endif
   1403 
   1404 	for (i = 0; i < len; i++) {
   1405 		NCR_WRITE_REG(sc, NCR_FIFO, p[i]);
   1406 
   1407 		if (sc->sc_rev == NCR_VARIANT_FAS366)
   1408 			NCR_WRITE_REG(sc, NCR_FIFO, 0);
   1409 	}
   1410 }
   1411 
   1412 int
   1413 ncr53c9x_reselect(sc, message, tagtype, tagid)
   1414 	struct ncr53c9x_softc *sc;
   1415 	int message;
   1416 	int tagtype, tagid;
   1417 {
   1418 	u_char selid, target, lun;
   1419 	struct ncr53c9x_ecb *ecb = NULL;
   1420 	struct ncr53c9x_tinfo *ti;
   1421 	struct ncr53c9x_linfo *li;
   1422 
   1423 
   1424 	if (sc->sc_rev == NCR_VARIANT_FAS366) {
   1425 		target = sc->sc_selid;
   1426 	} else {
   1427 		/*
   1428 		 * The SCSI chip made a snapshot of the data bus
   1429 		 * while the reselection was being negotiated.
   1430 		 * This enables us to determine which target did
   1431 		 * the reselect.
   1432 		 */
   1433 		selid = sc->sc_selid & ~(1 << sc->sc_id);
   1434 		if (selid & (selid - 1)) {
   1435 			printf("%s: reselect with invalid selid %02x;"
   1436 			    " sending DEVICE RESET\n",
   1437 			    sc->sc_dev.dv_xname, selid);
   1438 			goto reset;
   1439 		}
   1440 
   1441 		target = ffs(selid) - 1;
   1442 	}
   1443 	lun = message & 0x07;
   1444 
   1445 	/*
   1446 	 * Search wait queue for disconnected cmd
   1447 	 * The list should be short, so I haven't bothered with
   1448 	 * any more sophisticated structures than a simple
   1449 	 * singly linked list.
   1450 	 */
   1451 	ti = &sc->sc_tinfo[target];
   1452 	li = TINFO_LUN(ti, lun);
   1453 
   1454 	/*
   1455 	 * We can get as far as the LUN with the IDENTIFY
   1456 	 * message.  Check to see if we're running an
   1457 	 * un-tagged command.  Otherwise ack the IDENTIFY
   1458 	 * and wait for a tag message.
   1459 	 */
   1460 	if (li != NULL) {
   1461 		if (li->untagged != NULL && li->busy)
   1462 			ecb = li->untagged;
   1463 		else if (tagtype != MSG_SIMPLE_Q_TAG) {
   1464 			/* Wait for tag to come by */
   1465 			sc->sc_state = NCR_IDENTIFIED;
   1466 			return (0);
   1467 		} else if (tagtype)
   1468 			ecb = li->queued[tagid];
   1469 	}
   1470 	if (ecb == NULL) {
   1471 		printf("%s: reselect from target %d lun %d tag %x:%x "
   1472 		    "with no nexus; sending ABORT\n",
   1473 		    sc->sc_dev.dv_xname, target, lun, tagtype, tagid);
   1474 		goto abort;
   1475 	}
   1476 
   1477 	/* Make this nexus active again. */
   1478 	sc->sc_state = NCR_CONNECTED;
   1479 	sc->sc_nexus = ecb;
   1480 	ncr53c9x_setsync(sc, ti);
   1481 
   1482 	if (ecb->flags & ECB_RESET)
   1483 		ncr53c9x_sched_msgout(SEND_DEV_RESET);
   1484 	else if (ecb->flags & ECB_ABORT)
   1485 		ncr53c9x_sched_msgout(SEND_ABORT);
   1486 
   1487 	/* Do an implicit RESTORE POINTERS. */
   1488 	sc->sc_dp = ecb->daddr;
   1489 	sc->sc_dleft = ecb->dleft;
   1490 
   1491 	return (0);
   1492 
   1493 reset:
   1494 	ncr53c9x_sched_msgout(SEND_DEV_RESET);
   1495 	return (1);
   1496 
   1497 abort:
   1498 	ncr53c9x_sched_msgout(SEND_ABORT);
   1499 	return (1);
   1500 }
   1501 
   1502 static inline int
   1503 __verify_msg_format(u_char *p, int len)
   1504 {
   1505 
   1506 	if (len == 1 && MSG_IS1BYTE(p[0]))
   1507 		return 1;
   1508 	if (len == 2 && MSG_IS2BYTE(p[0]))
   1509 		return 1;
   1510 	if (len >= 3 && MSG_ISEXTENDED(p[0]) &&
   1511 	    len == p[1] + 2)
   1512 		return 1;
   1513 
   1514 	return 0;
   1515 }
   1516 
   1517 /*
   1518  * Get an incoming message as initiator.
   1519  *
   1520  * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
   1521  * byte in the FIFO
   1522  */
   1523 void
   1524 ncr53c9x_msgin(sc)
   1525 	struct ncr53c9x_softc *sc;
   1526 {
   1527 
   1528 	NCR_TRACE(("[ncr53c9x_msgin(curmsglen:%ld)] ", (long)sc->sc_imlen));
   1529 
   1530 	if (sc->sc_imlen == 0) {
   1531 		printf("%s: msgin: no msg byte available\n",
   1532 		    sc->sc_dev.dv_xname);
   1533 		return;
   1534 	}
   1535 
   1536 	/*
   1537 	 * Prepare for a new message.  A message should (according
   1538 	 * to the SCSI standard) be transmitted in one single
   1539 	 * MESSAGE_IN_PHASE. If we have been in some other phase,
   1540 	 * then this is a new message.
   1541 	 */
   1542 	if (sc->sc_prevphase != MESSAGE_IN_PHASE &&
   1543 	    sc->sc_state != NCR_RESELECTED) {
   1544 		printf("%s: phase change, dropping message, "
   1545 		    "prev %d, state %d\n",
   1546 		    sc->sc_dev.dv_xname, sc->sc_prevphase, sc->sc_state);
   1547 		sc->sc_flags &= ~NCR_DROP_MSGI;
   1548 		sc->sc_imlen = 0;
   1549 	}
   1550 
   1551 	NCR_TRACE(("<msgbyte:0x%02x>", sc->sc_imess[0]));
   1552 
   1553 	/*
   1554 	 * If we're going to reject the message, don't bother storing
   1555 	 * the incoming bytes.  But still, we need to ACK them.
   1556 	 */
   1557 	if ((sc->sc_flags & NCR_DROP_MSGI) != 0) {
   1558 		NCRCMD(sc, NCRCMD_MSGOK);
   1559 		printf("<dropping msg byte %x>", sc->sc_imess[sc->sc_imlen]);
   1560 		return;
   1561 	}
   1562 
   1563 	if (sc->sc_imlen >= NCR_MAX_MSG_LEN) {
   1564 		ncr53c9x_sched_msgout(SEND_REJECT);
   1565 		sc->sc_flags |= NCR_DROP_MSGI;
   1566 	} else {
   1567 		u_char *pb;
   1568 		int plen;
   1569 
   1570 		switch (sc->sc_state) {
   1571 		/*
   1572 		 * if received message is the first of reselection
   1573 		 * then first byte is selid, and then message
   1574 		 */
   1575 		case NCR_RESELECTED:
   1576 			pb = sc->sc_imess + 1;
   1577 			plen = sc->sc_imlen - 1;
   1578 			break;
   1579 		default:
   1580 			pb = sc->sc_imess;
   1581 			plen = sc->sc_imlen;
   1582 			break;
   1583 		}
   1584 
   1585 		if (__verify_msg_format(pb, plen))
   1586 			goto gotit;
   1587 	}
   1588 
   1589 	/* Ack what we have so far */
   1590 	NCRCMD(sc, NCRCMD_MSGOK);
   1591 	return;
   1592 
   1593 gotit:
   1594 	NCR_MSGS(("gotmsg(%x) state %d", sc->sc_imess[0], sc->sc_state));
   1595 	/* we got complete message, flush the imess, */
   1596 	/* XXX nobody uses imlen below */
   1597 	sc->sc_imlen = 0;
   1598 	/*
   1599 	 * Now we should have a complete message (1 byte, 2 byte
   1600 	 * and moderately long extended messages).  We only handle
   1601 	 * extended messages which total length is shorter than
   1602 	 * NCR_MAX_MSG_LEN.  Longer messages will be amputated.
   1603 	 */
   1604 	switch (sc->sc_state) {
   1605 		struct ncr53c9x_ecb *ecb;
   1606 		struct ncr53c9x_tinfo *ti;
   1607 		struct ncr53c9x_linfo *li;
   1608 		int lun;
   1609 
   1610 	case NCR_CONNECTED:
   1611 		ecb = sc->sc_nexus;
   1612 		ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
   1613 
   1614 		switch (sc->sc_imess[0]) {
   1615 		case MSG_CMDCOMPLETE:
   1616 			NCR_MSGS(("cmdcomplete "));
   1617 			if (sc->sc_dleft < 0) {
   1618 				scsipi_printaddr(ecb->xs->xs_periph);
   1619 				printf("got %ld extra bytes\n",
   1620 				    -(long)sc->sc_dleft);
   1621 				sc->sc_dleft = 0;
   1622 			}
   1623 			ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE) ?
   1624 			    0 : sc->sc_dleft;
   1625 			if ((ecb->flags & ECB_SENSE) == 0)
   1626 				ecb->xs->resid = ecb->dleft;
   1627 			sc->sc_state = NCR_CMDCOMPLETE;
   1628 			break;
   1629 
   1630 		case MSG_MESSAGE_REJECT:
   1631 			NCR_MSGS(("msg reject (msgout=%x) ", sc->sc_msgout));
   1632 			switch (sc->sc_msgout) {
   1633 			case SEND_TAG:
   1634 				/*
   1635 				 * Target does not like tagged queuing.
   1636 				 *  - Flush the command queue
   1637 				 *  - Disable tagged queuing for the target
   1638 				 *  - Dequeue ecb from the queued array.
   1639 				 */
   1640 				printf("%s: tagged queuing rejected: "
   1641 				    "target %d\n",
   1642 				    sc->sc_dev.dv_xname,
   1643 				    ecb->xs->xs_periph->periph_target);
   1644 
   1645 				NCR_MSGS(("(rejected sent tag)"));
   1646 				NCRCMD(sc, NCRCMD_FLUSH);
   1647 				DELAY(1);
   1648 				ti->flags &= ~T_TAG;
   1649 				lun = ecb->xs->xs_periph->periph_lun;
   1650 				li = TINFO_LUN(ti, lun);
   1651 				if (ecb->tag[0] &&
   1652 				    li->queued[ecb->tag[1]] != NULL) {
   1653 					li->queued[ecb->tag[1]] = NULL;
   1654 					li->used--;
   1655 				}
   1656 				ecb->tag[0] = ecb->tag[1] = 0;
   1657 				li->untagged = ecb;
   1658 				li->busy = 1;
   1659 				break;
   1660 
   1661 			case SEND_SDTR:
   1662 				printf("%s: sync transfer rejected: "
   1663 				    "target %d\n",
   1664 				    sc->sc_dev.dv_xname,
   1665 				    ecb->xs->xs_periph->periph_target);
   1666 
   1667 				sc->sc_flags &= ~NCR_SYNCHNEGO;
   1668 				ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
   1669 				ncr53c9x_setsync(sc, ti);
   1670 				ncr53c9x_update_xfer_mode(sc,
   1671 				    ecb->xs->xs_periph->periph_target);
   1672 				break;
   1673 
   1674 			case SEND_WDTR:
   1675 				printf("%s: wide transfer rejected: "
   1676 				    "target %d\n",
   1677 				    sc->sc_dev.dv_xname,
   1678 				    ecb->xs->xs_periph->periph_target);
   1679 				ti->flags &= ~T_WIDE;
   1680 				ti->width = 0;
   1681 				break;
   1682 
   1683 			case SEND_INIT_DET_ERR:
   1684 				goto abort;
   1685 			}
   1686 			break;
   1687 
   1688 		case MSG_NOOP:
   1689 			NCR_MSGS(("noop "));
   1690 			break;
   1691 
   1692 		case MSG_HEAD_OF_Q_TAG:
   1693 		case MSG_SIMPLE_Q_TAG:
   1694 		case MSG_ORDERED_Q_TAG:
   1695 			NCR_MSGS(("TAG %x:%x",
   1696 			    sc->sc_imess[0], sc->sc_imess[1]));
   1697 			break;
   1698 
   1699 		case MSG_DISCONNECT:
   1700 			NCR_MSGS(("disconnect "));
   1701 			ti->dconns++;
   1702 			sc->sc_state = NCR_DISCONNECT;
   1703 
   1704 			/*
   1705 			 * Mark the fact that all bytes have moved. The
   1706 			 * target may not bother to do a SAVE POINTERS
   1707 			 * at this stage. This flag will set the residual
   1708 			 * count to zero on MSG COMPLETE.
   1709 			 */
   1710 			if (sc->sc_dleft == 0)
   1711 				ecb->flags |= ECB_TENTATIVE_DONE;
   1712 
   1713 			break;
   1714 
   1715 		case MSG_SAVEDATAPOINTER:
   1716 			NCR_MSGS(("save datapointer "));
   1717 			ecb->daddr = sc->sc_dp;
   1718 			ecb->dleft = sc->sc_dleft;
   1719 			break;
   1720 
   1721 		case MSG_RESTOREPOINTERS:
   1722 			NCR_MSGS(("restore datapointer "));
   1723 			sc->sc_dp = ecb->daddr;
   1724 			sc->sc_dleft = ecb->dleft;
   1725 			break;
   1726 
   1727 		case MSG_EXTENDED:
   1728 			NCR_MSGS(("extended(%x) ", sc->sc_imess[2]));
   1729 			switch (sc->sc_imess[2]) {
   1730 			case MSG_EXT_SDTR:
   1731 				NCR_MSGS(("SDTR period %d, offset %d ",
   1732 				    sc->sc_imess[3], sc->sc_imess[4]));
   1733 				if (sc->sc_imess[1] != 3)
   1734 					goto reject;
   1735 				ti->period = sc->sc_imess[3];
   1736 				ti->offset = sc->sc_imess[4];
   1737 				ti->flags &= ~T_NEGOTIATE;
   1738 				if (sc->sc_minsync == 0 ||
   1739 				    ti->offset == 0 ||
   1740 				    ti->period > 124) {
   1741 #if 0
   1742 #ifdef NCR53C9X_DEBUG
   1743 					scsipi_printaddr(ecb->xs->xs_periph);
   1744 					printf("async mode\n");
   1745 #endif
   1746 #endif
   1747 					ti->flags &= ~T_SYNCMODE;
   1748 					if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
   1749 						/*
   1750 						 * target initiated negotiation
   1751 						 */
   1752 						ti->offset = 0;
   1753 						ncr53c9x_sched_msgout(
   1754 						    SEND_SDTR);
   1755 					}
   1756 				} else {
   1757 #if 0
   1758 					int r = 250/ti->period;
   1759 					int s = (100*250)/ti->period - 100*r;
   1760 #endif
   1761 					int p;
   1762 
   1763 					p = ncr53c9x_stp2cpb(sc, ti->period);
   1764 					ti->period = ncr53c9x_cpb2stp(sc, p);
   1765 #if 0
   1766 #ifdef NCR53C9X_DEBUG
   1767 					scsipi_printaddr(ecb->xs->xs_periph);
   1768 					printf("max sync rate %d.%02dMB/s\n",
   1769 					    r, s);
   1770 #endif
   1771 #endif
   1772 					if ((sc->sc_flags&NCR_SYNCHNEGO) == 0) {
   1773 						/*
   1774 						 * target initiated negotiation
   1775 						 */
   1776 						if (ti->period <
   1777 						    sc->sc_minsync)
   1778 							ti->period =
   1779 							    sc->sc_minsync;
   1780 						if (ti->offset > 15)
   1781 							ti->offset = 15;
   1782 						ti->flags &= ~T_SYNCMODE;
   1783 						ncr53c9x_sched_msgout(
   1784 						    SEND_SDTR);
   1785 					} else {
   1786 						/* we are sync */
   1787 						ti->flags |= T_SYNCMODE;
   1788 					}
   1789 				}
   1790 				ncr53c9x_update_xfer_mode(sc,
   1791 				    ecb->xs->xs_periph->periph_target);
   1792 				sc->sc_flags &= ~NCR_SYNCHNEGO;
   1793 				ncr53c9x_setsync(sc, ti);
   1794 				break;
   1795 
   1796 			case MSG_EXT_WDTR:
   1797 				printf("%s: wide mode %d\n",
   1798 				       sc->sc_dev.dv_xname, sc->sc_imess[3]);
   1799 				if (sc->sc_imess[3] == 1) {
   1800 					ti->cfg3 |= NCRFASCFG3_EWIDE;
   1801 					ncr53c9x_setsync(sc, ti);
   1802 				} else
   1803 					ti->width = 0;
   1804 				ti->flags &= ~T_WIDE;
   1805 				break;
   1806 			default:
   1807 				scsipi_printaddr(ecb->xs->xs_periph);
   1808 				printf("unrecognized MESSAGE EXTENDED;"
   1809 				    " sending REJECT\n");
   1810 				goto reject;
   1811 			}
   1812 			break;
   1813 
   1814 		default:
   1815 			NCR_MSGS(("ident "));
   1816 			scsipi_printaddr(ecb->xs->xs_periph);
   1817 			printf("unrecognized MESSAGE; sending REJECT\n");
   1818 		reject:
   1819 			ncr53c9x_sched_msgout(SEND_REJECT);
   1820 			break;
   1821 		}
   1822 		break;
   1823 
   1824 	case NCR_IDENTIFIED:
   1825 		/*
   1826 		 * IDENTIFY message was received and queue tag is expected now
   1827 		 */
   1828 		if ((sc->sc_imess[0] != MSG_SIMPLE_Q_TAG) ||
   1829 		    (sc->sc_msgify == 0)) {
   1830 			printf("%s: TAG reselect without IDENTIFY;"
   1831 			    " MSG %x;"
   1832 			    " sending DEVICE RESET\n",
   1833 			    sc->sc_dev.dv_xname,
   1834 			    sc->sc_imess[0]);
   1835 			goto reset;
   1836 		}
   1837 		(void) ncr53c9x_reselect(sc, sc->sc_msgify,
   1838 		    sc->sc_imess[0], sc->sc_imess[1]);
   1839 		break;
   1840 
   1841 	case NCR_RESELECTED:
   1842 		if (MSG_ISIDENTIFY(sc->sc_imess[1])) {
   1843 			sc->sc_msgify = sc->sc_imess[1];
   1844 		} else {
   1845 			printf("%s: reselect without IDENTIFY;"
   1846 			    " MSG %x;"
   1847 			    " sending DEVICE RESET\n",
   1848 			    sc->sc_dev.dv_xname,
   1849 			    sc->sc_imess[1]);
   1850 			goto reset;
   1851 		}
   1852 		(void) ncr53c9x_reselect(sc, sc->sc_msgify, 0, 0);
   1853 		break;
   1854 
   1855 	default:
   1856 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1857 		    sc->sc_dev.dv_xname);
   1858 	reset:
   1859 		ncr53c9x_sched_msgout(SEND_DEV_RESET);
   1860 		break;
   1861 
   1862 	abort:
   1863 		ncr53c9x_sched_msgout(SEND_ABORT);
   1864 		break;
   1865 	}
   1866 
   1867 	/* if we have more messages to send set ATN */
   1868 	if (sc->sc_msgpriq)
   1869 		NCRCMD(sc, NCRCMD_SETATN);
   1870 
   1871 	/* Ack last message byte */
   1872 	NCRCMD(sc, NCRCMD_MSGOK);
   1873 
   1874 	/* Done, reset message pointer. */
   1875 	sc->sc_flags &= ~NCR_DROP_MSGI;
   1876 	sc->sc_imlen = 0;
   1877 }
   1878 
   1879 
   1880 /*
   1881  * Send the highest priority, scheduled message
   1882  */
   1883 void
   1884 ncr53c9x_msgout(sc)
   1885 	struct ncr53c9x_softc *sc;
   1886 {
   1887 	struct ncr53c9x_tinfo *ti;
   1888 	struct ncr53c9x_ecb *ecb;
   1889 	size_t size;
   1890 
   1891 	NCR_TRACE(("[ncr53c9x_msgout(priq:%x, prevphase:%x)]",
   1892 	    sc->sc_msgpriq, sc->sc_prevphase));
   1893 
   1894 	/*
   1895 	 * XXX - the NCR_ATN flag is not in sync with the actual ATN
   1896 	 *	 condition on the SCSI bus. The 53c9x chip
   1897 	 *	 automatically turns off ATN before sending the
   1898 	 *	 message byte.  (see also the comment below in the
   1899 	 *	 default case when picking out a message to send)
   1900 	 */
   1901 	if (sc->sc_flags & NCR_ATN) {
   1902 		if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
   1903 		new:
   1904 			NCRCMD(sc, NCRCMD_FLUSH);
   1905 /*			DELAY(1); */
   1906 			sc->sc_msgoutq = 0;
   1907 			sc->sc_omlen = 0;
   1908 		}
   1909 	} else {
   1910 		if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   1911 			ncr53c9x_sched_msgout(sc->sc_msgoutq);
   1912 			goto new;
   1913 		} else {
   1914 			printf("%s at line %d: unexpected MESSAGE OUT phase\n",
   1915 			    sc->sc_dev.dv_xname, __LINE__);
   1916 		}
   1917 	}
   1918 
   1919 	if (sc->sc_omlen == 0) {
   1920 		/* Pick up highest priority message */
   1921 		sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
   1922 		sc->sc_msgoutq |= sc->sc_msgout;
   1923 		sc->sc_msgpriq &= ~sc->sc_msgout;
   1924 		sc->sc_omlen = 1;		/* "Default" message len */
   1925 		switch (sc->sc_msgout) {
   1926 		case SEND_SDTR:
   1927 			ecb = sc->sc_nexus;
   1928 			ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
   1929 			sc->sc_omess[0] = MSG_EXTENDED;
   1930 			sc->sc_omess[1] = MSG_EXT_SDTR_LEN;
   1931 			sc->sc_omess[2] = MSG_EXT_SDTR;
   1932 			sc->sc_omess[3] = ti->period;
   1933 			sc->sc_omess[4] = ti->offset;
   1934 			sc->sc_omlen = 5;
   1935 			if ((sc->sc_flags & NCR_SYNCHNEGO) == 0) {
   1936 				ti->flags |= T_SYNCMODE;
   1937 				ncr53c9x_setsync(sc, ti);
   1938 			}
   1939 			break;
   1940 		case SEND_WDTR:
   1941 			ecb = sc->sc_nexus;
   1942 			ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
   1943 			sc->sc_omess[0] = MSG_EXTENDED;
   1944 			sc->sc_omess[1] = MSG_EXT_WDTR_LEN;
   1945 			sc->sc_omess[2] = MSG_EXT_WDTR;
   1946 			sc->sc_omess[3] = ti->width;
   1947 			sc->sc_omlen = 4;
   1948 			break;
   1949                 case SEND_IDENTIFY:
   1950                         if (sc->sc_state != NCR_CONNECTED) {
   1951                                 printf("%s at line %d: no nexus\n",
   1952                                     sc->sc_dev.dv_xname, __LINE__);
   1953                         }
   1954                         ecb = sc->sc_nexus;
   1955                         sc->sc_omess[0] =
   1956                             MSG_IDENTIFY(ecb->xs->xs_periph->periph_lun, 0);
   1957                         break;
   1958 		case SEND_TAG:
   1959 			if (sc->sc_state != NCR_CONNECTED) {
   1960 				printf("%s at line %d: no nexus\n",
   1961 				    sc->sc_dev.dv_xname, __LINE__);
   1962 			}
   1963 			ecb = sc->sc_nexus;
   1964 			sc->sc_omess[0] = ecb->tag[0];
   1965 			sc->sc_omess[1] = ecb->tag[1];
   1966 			sc->sc_omlen = 2;
   1967 			break;
   1968 		case SEND_DEV_RESET:
   1969 			sc->sc_flags |= NCR_ABORTING;
   1970 			sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1971 			ecb = sc->sc_nexus;
   1972 			ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
   1973 			ti->flags &= ~T_SYNCMODE;
   1974 			ncr53c9x_update_xfer_mode(sc,
   1975 			    ecb->xs->xs_periph->periph_target);
   1976 			if ((ti->flags & T_SYNCHOFF) == 0)
   1977 				/* We can re-start sync negotiation */
   1978 				ti->flags |= T_NEGOTIATE;
   1979 			break;
   1980 		case SEND_PARITY_ERROR:
   1981 			sc->sc_omess[0] = MSG_PARITY_ERROR;
   1982 			break;
   1983 		case SEND_ABORT:
   1984 			sc->sc_flags |= NCR_ABORTING;
   1985 			sc->sc_omess[0] = MSG_ABORT;
   1986 			break;
   1987 		case SEND_INIT_DET_ERR:
   1988 			sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1989 			break;
   1990 		case SEND_REJECT:
   1991 			sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1992 			break;
   1993 		default:
   1994 			/*
   1995 			 * We normally do not get here, since the chip
   1996 			 * automatically turns off ATN before the last
   1997 			 * byte of a message is sent to the target.
   1998 			 * However, if the target rejects our (multi-byte)
   1999 			 * message early by switching to MSG IN phase
   2000 			 * ATN remains on, so the target may return to
   2001 			 * MSG OUT phase. If there are no scheduled messages
   2002 			 * left we send a NO-OP.
   2003 			 *
   2004 			 * XXX - Note that this leaves no useful purpose for
   2005 			 * the NCR_ATN flag.
   2006 			 */
   2007 			sc->sc_flags &= ~NCR_ATN;
   2008 			sc->sc_omess[0] = MSG_NOOP;
   2009 			break;
   2010 		}
   2011 		sc->sc_omp = sc->sc_omess;
   2012 	}
   2013 
   2014 #ifdef DEBUG
   2015 	{
   2016 		int i;
   2017 
   2018 		NCR_MSGS(("<msgout:"));
   2019 		for (i = 0; i < sc->sc_omlen; i++)
   2020 			NCR_MSGS((" %02x", sc->sc_omess[i]));
   2021 		NCR_MSGS(("> "));
   2022 	}
   2023 #endif
   2024 	if (sc->sc_rev == NCR_VARIANT_FAS366) {
   2025 		/*
   2026 		 * XXX fifo size
   2027 		 */
   2028 		ncr53c9x_flushfifo(sc);
   2029 		ncr53c9x_wrfifo(sc, sc->sc_omp, sc->sc_omlen);
   2030 		NCRCMD(sc, NCRCMD_TRANS);
   2031 	} else {
   2032 		/* (re)send the message */
   2033 		size = min(sc->sc_omlen, sc->sc_maxxfer);
   2034 		NCRDMA_SETUP(sc, &sc->sc_omp, &sc->sc_omlen, 0, &size);
   2035 		/* Program the SCSI counter */
   2036 		NCR_SET_COUNT(sc, size);
   2037 
   2038 		/* Load the count in and start the message-out transfer */
   2039 		NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
   2040 		NCRCMD(sc, NCRCMD_TRANS|NCRCMD_DMA);
   2041 		NCRDMA_GO(sc);
   2042 	}
   2043 }
   2044 
   2045 /*
   2046  * This is the most critical part of the driver, and has to know
   2047  * how to deal with *all* error conditions and phases from the SCSI
   2048  * bus. If there are no errors and the DMA was active, then call the
   2049  * DMA pseudo-interrupt handler. If this returns 1, then that was it
   2050  * and we can return from here without further processing.
   2051  *
   2052  * Most of this needs verifying.
   2053  */
   2054 int
   2055 ncr53c9x_intr(arg)
   2056 	void *arg;
   2057 {
   2058 	struct ncr53c9x_softc *sc = arg;
   2059 	struct ncr53c9x_ecb *ecb;
   2060 	struct scsipi_periph *periph;
   2061 	struct ncr53c9x_tinfo *ti;
   2062 	size_t size;
   2063 	int nfifo;
   2064 
   2065 	NCR_INTS(("[ncr53c9x_intr: state %d]", sc->sc_state));
   2066 
   2067 	if (!NCRDMA_ISINTR(sc))
   2068 		return (0);
   2069 
   2070 again:
   2071 	/* and what do the registers say... */
   2072 	ncr53c9x_readregs(sc);
   2073 
   2074 	sc->sc_intrcnt.ev_count++;
   2075 
   2076 	/*
   2077 	 * At the moment, only a SCSI Bus Reset or Illegal
   2078 	 * Command are classed as errors. A disconnect is a
   2079 	 * valid condition, and we let the code check is the
   2080 	 * "NCR_BUSFREE_OK" flag was set before declaring it
   2081 	 * and error.
   2082 	 *
   2083 	 * Also, the status register tells us about "Gross
   2084 	 * Errors" and "Parity errors". Only the Gross Error
   2085 	 * is really bad, and the parity errors are dealt
   2086 	 * with later
   2087 	 *
   2088 	 * TODO
   2089 	 *	If there are too many parity error, go to slow
   2090 	 *	cable mode ?
   2091 	 */
   2092 
   2093 	/* SCSI Reset */
   2094 	if ((sc->sc_espintr & NCRINTR_SBR) != 0) {
   2095 		if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 0) {
   2096 			NCRCMD(sc, NCRCMD_FLUSH);
   2097 			DELAY(1);
   2098 		}
   2099 		if (sc->sc_state != NCR_SBR) {
   2100 			printf("%s: SCSI bus reset\n", sc->sc_dev.dv_xname);
   2101 			ncr53c9x_init(sc, 0); /* Restart everything */
   2102 			return (1);
   2103 		}
   2104 #if 0
   2105 /*XXX*/		printf("<expected bus reset: "
   2106 		    "[intr %x, stat %x, step %d]>\n",
   2107 		    sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
   2108 #endif
   2109 		if (sc->sc_nexus != NULL)
   2110 			panic("%s: nexus in reset state",
   2111 			    sc->sc_dev.dv_xname);
   2112 		goto sched;
   2113 	}
   2114 
   2115 	ecb = sc->sc_nexus;
   2116 
   2117 #define NCRINTR_ERR (NCRINTR_SBR|NCRINTR_ILL)
   2118 	if (sc->sc_espintr & NCRINTR_ERR ||
   2119 	    sc->sc_espstat & NCRSTAT_GE) {
   2120 
   2121 		if ((sc->sc_espstat & NCRSTAT_GE) != 0) {
   2122 			/* Gross Error; no target ? */
   2123 			if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   2124 				NCRCMD(sc, NCRCMD_FLUSH);
   2125 				DELAY(1);
   2126 			}
   2127 			if (sc->sc_state == NCR_CONNECTED ||
   2128 			    sc->sc_state == NCR_SELECTING) {
   2129 				ecb->xs->error = XS_TIMEOUT;
   2130 				ncr53c9x_done(sc, ecb);
   2131 			}
   2132 			return (1);
   2133 		}
   2134 
   2135 		if ((sc->sc_espintr & NCRINTR_ILL) != 0) {
   2136 			if ((sc->sc_flags & NCR_EXPECT_ILLCMD) != 0) {
   2137 				/*
   2138 				 * Eat away "Illegal command" interrupt
   2139 				 * on a ESP100 caused by a re-selection
   2140 				 * while we were trying to select
   2141 				 * another target.
   2142 				 */
   2143 #ifdef DEBUG
   2144 				printf("%s: ESP100 work-around activated\n",
   2145 					sc->sc_dev.dv_xname);
   2146 #endif
   2147 				sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
   2148 				return (1);
   2149 			}
   2150 			/* illegal command, out of sync ? */
   2151 			printf("%s: illegal command: 0x%x "
   2152 			    "(state %d, phase %x, prevphase %x)\n",
   2153 			    sc->sc_dev.dv_xname, sc->sc_lastcmd,
   2154 			    sc->sc_state, sc->sc_phase, sc->sc_prevphase);
   2155 			if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   2156 				NCRCMD(sc, NCRCMD_FLUSH);
   2157 				DELAY(1);
   2158 			}
   2159 			ncr53c9x_init(sc, 1); /* Restart everything */
   2160 			return (1);
   2161 		}
   2162 	}
   2163 	sc->sc_flags &= ~NCR_EXPECT_ILLCMD;
   2164 
   2165 	/*
   2166 	 * Call if DMA is active.
   2167 	 *
   2168 	 * If DMA_INTR returns true, then maybe go 'round the loop
   2169 	 * again in case there is no more DMA queued, but a phase
   2170 	 * change is expected.
   2171 	 */
   2172 	if (NCRDMA_ISACTIVE(sc)) {
   2173 		int r = NCRDMA_INTR(sc);
   2174 		if (r == -1) {
   2175 			printf("%s: DMA error; resetting\n",
   2176 			    sc->sc_dev.dv_xname);
   2177 			ncr53c9x_init(sc, 1);
   2178 			return 1;
   2179 		}
   2180 		/* If DMA active here, then go back to work... */
   2181 		if (NCRDMA_ISACTIVE(sc))
   2182 			return (1);
   2183 
   2184 		if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
   2185 			/*
   2186 			 * DMA not completed.  If we can not find a
   2187 			 * acceptable explanation, print a diagnostic.
   2188 			 */
   2189 			if (sc->sc_state == NCR_SELECTING)
   2190 				/*
   2191 				 * This can happen if we are reselected
   2192 				 * while using DMA to select a target.
   2193 				 */
   2194 				/*void*/;
   2195 			else if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   2196 				/*
   2197 				 * Our (multi-byte) message (eg SDTR) was
   2198 				 * interrupted by the target to send
   2199 				 * a MSG REJECT.
   2200 				 * Print diagnostic if current phase
   2201 				 * is not MESSAGE IN.
   2202 				 */
   2203 				if (sc->sc_phase != MESSAGE_IN_PHASE)
   2204 					printf("%s: !TC on MSG OUT"
   2205 					    " [intr %x, stat %x, step %d]"
   2206 					    " prevphase %x, resid %lx\n",
   2207 					    sc->sc_dev.dv_xname,
   2208 					    sc->sc_espintr,
   2209 					    sc->sc_espstat,
   2210 					    sc->sc_espstep,
   2211 					    sc->sc_prevphase,
   2212 					    (u_long)sc->sc_omlen);
   2213 			} else if (sc->sc_dleft == 0) {
   2214 				/*
   2215 				 * The DMA operation was started for
   2216 				 * a DATA transfer. Print a diagnostic
   2217 				 * if the DMA counter and TC bit
   2218 				 * appear to be out of sync.
   2219 				 */
   2220 				printf("%s: !TC on DATA XFER"
   2221 				    " [intr %x, stat %x, step %d]"
   2222 				    " prevphase %x, resid %x\n",
   2223 				    sc->sc_dev.dv_xname,
   2224 				    sc->sc_espintr,
   2225 				    sc->sc_espstat,
   2226 				    sc->sc_espstep,
   2227 				    sc->sc_prevphase,
   2228 				    ecb ? ecb->dleft : -1);
   2229 			}
   2230 		}
   2231 	}
   2232 
   2233 	/*
   2234 	 * Check for less serious errors.
   2235 	 */
   2236 	if ((sc->sc_espstat & NCRSTAT_PE) != 0) {
   2237 		printf("%s: SCSI bus parity error\n", sc->sc_dev.dv_xname);
   2238 		if (sc->sc_prevphase == MESSAGE_IN_PHASE)
   2239 			ncr53c9x_sched_msgout(SEND_PARITY_ERROR);
   2240 		else
   2241 			ncr53c9x_sched_msgout(SEND_INIT_DET_ERR);
   2242 	}
   2243 
   2244 	if ((sc->sc_espintr & NCRINTR_DIS) != 0) {
   2245 		sc->sc_msgify = 0;
   2246 		NCR_INTS(("<DISC [intr %x, stat %x, step %d]>",
   2247 		    sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
   2248 		if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   2249 			NCRCMD(sc, NCRCMD_FLUSH);
   2250 /*			DELAY(1); */
   2251 		}
   2252 		/*
   2253 		 * This command must (apparently) be issued within
   2254 		 * 250mS of a disconnect. So here you are...
   2255 		 */
   2256 		NCRCMD(sc, NCRCMD_ENSEL);
   2257 
   2258 		switch (sc->sc_state) {
   2259 		case NCR_RESELECTED:
   2260 			goto sched;
   2261 
   2262 		case NCR_SELECTING:
   2263 		{
   2264 			struct ncr53c9x_linfo *li;
   2265 
   2266 			ecb->xs->error = XS_SELTIMEOUT;
   2267 
   2268 			/* Selection timeout -- discard all LUNs if empty */
   2269 			periph = ecb->xs->xs_periph;
   2270 			ti = &sc->sc_tinfo[periph->periph_target];
   2271 			li = LIST_FIRST(&ti->luns);
   2272 			while (li != NULL) {
   2273 				if (li->untagged == NULL && li->used == 0) {
   2274 					if (li->lun < NCR_NLUN)
   2275 						ti->lun[li->lun] = NULL;
   2276 					LIST_REMOVE(li, link);
   2277 					free(li, M_DEVBUF);
   2278 					/*
   2279 					 * Restart the search at the beginning
   2280 					 */
   2281 					li = LIST_FIRST(&ti->luns);
   2282 					continue;
   2283 				}
   2284 				li = LIST_NEXT(li, link);
   2285 			}
   2286 			goto finish;
   2287 		}
   2288 		case NCR_CONNECTED:
   2289 			if ((sc->sc_flags & NCR_SYNCHNEGO) != 0) {
   2290 #ifdef NCR53C9X_DEBUG
   2291 				if (ecb != NULL)
   2292 					scsipi_printaddr(ecb->xs->xs_periph);
   2293 				printf("sync nego not completed!\n");
   2294 #endif
   2295 				ti = &sc->sc_tinfo[ecb->xs->xs_periph->periph_target];
   2296 				sc->sc_flags &= ~NCR_SYNCHNEGO;
   2297 				ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
   2298 			}
   2299 
   2300 			/* it may be OK to disconnect */
   2301 			if ((sc->sc_flags & NCR_ABORTING) == 0) {
   2302 				/*
   2303 				 * Section 5.1.1 of the SCSI 2 spec
   2304 				 * suggests issuing a REQUEST SENSE
   2305 				 * following an unexpected disconnect.
   2306 				 * Some devices go into a contingent
   2307 				 * allegiance condition when
   2308 				 * disconnecting, and this is necessary
   2309 				 * to clean up their state.
   2310 				 */
   2311 				printf("%s: unexpected disconnect; ",
   2312 				    sc->sc_dev.dv_xname);
   2313 				if ((ecb->flags & ECB_SENSE) != 0) {
   2314 					printf("resetting\n");
   2315 					goto reset;
   2316 				}
   2317 				printf("sending REQUEST SENSE\n");
   2318 				callout_stop(&ecb->xs->xs_callout);
   2319 				ncr53c9x_sense(sc, ecb);
   2320 				goto out;
   2321 			}
   2322 
   2323 			ecb->xs->error = XS_TIMEOUT;
   2324 			goto finish;
   2325 
   2326 		case NCR_DISCONNECT:
   2327 			sc->sc_nexus = NULL;
   2328 			goto sched;
   2329 
   2330 		case NCR_CMDCOMPLETE:
   2331 			goto finish;
   2332 		}
   2333 	}
   2334 
   2335 	switch (sc->sc_state) {
   2336 
   2337 	case NCR_SBR:
   2338 		printf("%s: waiting for SCSI Bus Reset to happen\n",
   2339 		    sc->sc_dev.dv_xname);
   2340 		return (1);
   2341 
   2342 	case NCR_RESELECTED:
   2343 		/*
   2344 		 * we must be continuing a message ?
   2345 		 */
   2346 		if (sc->sc_phase != MESSAGE_IN_PHASE) {
   2347 			printf("%s: target didn't identify\n",
   2348 			    sc->sc_dev.dv_xname);
   2349 			ncr53c9x_init(sc, 1);
   2350 			return (1);
   2351 		}
   2352 printf("<<RESELECT CONT'd>>");
   2353 #if XXXX
   2354 		ncr53c9x_msgin(sc);
   2355 		if (sc->sc_state != NCR_CONNECTED) {
   2356 			/* IDENTIFY fail?! */
   2357 			printf("%s: identify failed\n",
   2358 			    sc->sc_dev.dv_xname, sc->sc_state);
   2359 			ncr53c9x_init(sc, 1);
   2360 			return (1);
   2361 		}
   2362 #endif
   2363 		break;
   2364 
   2365 	case NCR_IDENTIFIED:
   2366 		ecb = sc->sc_nexus;
   2367 		if (sc->sc_phase != MESSAGE_IN_PHASE) {
   2368 			int i = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
   2369  			/*
   2370 			 * Things are seriously fucked up.
   2371 			 * Pull the brakes, i.e. reset
   2372 			 */
   2373 			printf("%s: target didn't send tag: %d bytes in fifo\n",
   2374 			    sc->sc_dev.dv_xname, i);
   2375 			/* Drain and display fifo */
   2376 			while (i-- > 0)
   2377 				printf("[%d] ", NCR_READ_REG(sc, NCR_FIFO));
   2378 
   2379 			ncr53c9x_init(sc, 1);
   2380 			return (1);
   2381 		} else
   2382 			goto msgin;
   2383 
   2384 		break;
   2385 
   2386 	case NCR_IDLE:
   2387 	case NCR_SELECTING:
   2388 		ecb = sc->sc_nexus;
   2389 		if (sc->sc_espintr & NCRINTR_RESEL) {
   2390 			sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
   2391 			sc->sc_flags = 0;
   2392 			/*
   2393 			 * If we're trying to select a
   2394 			 * target ourselves, push our command
   2395 			 * back into the ready list.
   2396 			 */
   2397 			if (sc->sc_state == NCR_SELECTING) {
   2398 				NCR_INTS(("backoff selector "));
   2399 				callout_stop(&ecb->xs->xs_callout);
   2400 				ncr53c9x_dequeue(sc, ecb);
   2401 				TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
   2402 				ecb->flags |= ECB_READY;
   2403 				ecb = sc->sc_nexus = NULL;
   2404 			}
   2405 			sc->sc_state = NCR_RESELECTED;
   2406 			if (sc->sc_phase != MESSAGE_IN_PHASE) {
   2407 				/*
   2408 				 * Things are seriously fucked up.
   2409 				 * Pull the brakes, i.e. reset
   2410 				 */
   2411 				printf("%s: target didn't identify\n",
   2412 				    sc->sc_dev.dv_xname);
   2413 				ncr53c9x_init(sc, 1);
   2414 				return (1);
   2415 			}
   2416 			/*
   2417 			 * The C90 only inhibits FIFO writes until
   2418 			 * reselection is complete, instead of
   2419 			 * waiting until the interrupt status register
   2420 			 * has been read. So, if the reselect happens
   2421 			 * while we were entering a command bytes (for
   2422 			 * another target) some of those bytes can
   2423 			 * appear in the FIFO here, after the
   2424 			 * interrupt is taken.
   2425 			 */
   2426 			nfifo = ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
   2427 
   2428 			if (nfifo < 2 ||
   2429 			    (nfifo > 2 && sc->sc_rev != NCR_VARIANT_ESP100)) {
   2430 				printf("%s: RESELECT: %d bytes in FIFO! "
   2431 				    "[intr %x, stat %x, step %d, "
   2432 				    "prevphase %x]\n",
   2433 				    sc->sc_dev.dv_xname,
   2434 				    nfifo,
   2435 				    sc->sc_espintr,
   2436 				    sc->sc_espstat,
   2437 				    sc->sc_espstep,
   2438 				    sc->sc_prevphase);
   2439 				ncr53c9x_init(sc, 1);
   2440 				return (1);
   2441 			}
   2442 			sc->sc_selid = sc->sc_imess[0];
   2443 			NCR_INTS(("selid=%02x ", sc->sc_selid));
   2444 
   2445 			/* Handle identify message */
   2446 			ncr53c9x_msgin(sc);
   2447 			if (nfifo != 2) {
   2448 				/*
   2449 				 * Note: this should not happen
   2450 				 * with `dmaselect' on.
   2451 				 */
   2452 				sc->sc_flags |= NCR_EXPECT_ILLCMD;
   2453 				NCRCMD(sc, NCRCMD_FLUSH);
   2454 			} else if (sc->sc_features & NCR_F_DMASELECT &&
   2455 			    sc->sc_rev == NCR_VARIANT_ESP100) {
   2456 				sc->sc_flags |= NCR_EXPECT_ILLCMD;
   2457 			}
   2458 
   2459 			if (sc->sc_state != NCR_CONNECTED &&
   2460 			    sc->sc_state != NCR_IDENTIFIED) {
   2461 				/* IDENTIFY fail?! */
   2462 				printf("%s: identify failed, "
   2463 				    "state %d, intr %02x\n",
   2464 				    sc->sc_dev.dv_xname, sc->sc_state,
   2465 				    sc->sc_espintr);
   2466 				ncr53c9x_init(sc, 1);
   2467 				return (1);
   2468 			}
   2469 			goto shortcut; /* ie. next phase expected soon */
   2470 		}
   2471 
   2472 #define	NCRINTR_DONE	(NCRINTR_FC|NCRINTR_BS)
   2473 		if ((sc->sc_espintr & NCRINTR_DONE) == NCRINTR_DONE) {
   2474 			/*
   2475 			 * Arbitration won; examine the `step' register
   2476 			 * to determine how far the selection could progress.
   2477 			 */
   2478 			ecb = sc->sc_nexus;
   2479 			if (ecb == NULL)
   2480 				panic("ncr53c9x: no nexus");
   2481 
   2482 			periph = ecb->xs->xs_periph;
   2483 			ti = &sc->sc_tinfo[periph->periph_target];
   2484 
   2485 			switch (sc->sc_espstep) {
   2486 			case 0:
   2487 				/*
   2488 				 * The target did not respond with a
   2489 				 * message out phase - probably an old
   2490 				 * device that doesn't recognize ATN.
   2491 				 * Clear ATN and just continue, the
   2492 				 * target should be in the command
   2493 				 * phase.
   2494 				 * XXXX check for command phase?
   2495 				 */
   2496 				NCRCMD(sc, NCRCMD_RSTATN);
   2497 				break;
   2498 			case 1:
   2499 				if ((ti->flags & T_NEGOTIATE) == 0 &&
   2500 				    ecb->tag[0] == 0) {
   2501 					printf("%s: step 1 & !NEG\n",
   2502 					    sc->sc_dev.dv_xname);
   2503 					goto reset;
   2504 				}
   2505 				if (sc->sc_phase != MESSAGE_OUT_PHASE) {
   2506 					printf("%s: !MSGOUT\n",
   2507 					    sc->sc_dev.dv_xname);
   2508 					goto reset;
   2509 				}
   2510 				if (ti->flags & T_WIDE) {
   2511 					ncr53c9x_sched_msgout(SEND_WDTR);
   2512 				}
   2513 				if (ti->flags & T_NEGOTIATE) {
   2514 					/* Start negotiating */
   2515 					ti->period = sc->sc_minsync;
   2516 					ti->offset = 15;
   2517 					sc->sc_flags |= NCR_SYNCHNEGO;
   2518 					if (ecb->tag[0])
   2519 						ncr53c9x_sched_msgout(
   2520 						    SEND_TAG|SEND_SDTR);
   2521 					else
   2522 						ncr53c9x_sched_msgout(
   2523 						    SEND_SDTR);
   2524 				} else {
   2525 					/* Could not do ATN3 so send TAG */
   2526 					ncr53c9x_sched_msgout(SEND_TAG);
   2527 				}
   2528 				sc->sc_prevphase = MESSAGE_OUT_PHASE; /* XXXX */
   2529 				break;
   2530 			case 3:
   2531 				/*
   2532 				 * Grr, this is supposed to mean
   2533 				 * "target left command phase  prematurely".
   2534 				 * It seems to happen regularly when
   2535 				 * sync mode is on.
   2536 				 * Look at FIFO to see if command went out.
   2537 				 * (Timing problems?)
   2538 				 */
   2539 				if (sc->sc_features & NCR_F_DMASELECT) {
   2540 					if (sc->sc_cmdlen == 0)
   2541 						/* Hope for the best.. */
   2542 						break;
   2543 				} else if ((NCR_READ_REG(sc, NCR_FFLAG)
   2544 				    & NCRFIFO_FF) == 0) {
   2545 					/* Hope for the best.. */
   2546 					break;
   2547 				}
   2548 				printf("(%s:%d:%d): selection failed;"
   2549 				    " %d left in FIFO "
   2550 				    "[intr %x, stat %x, step %d]\n",
   2551 				    sc->sc_dev.dv_xname,
   2552 				    periph->periph_target,
   2553 				    periph->periph_lun,
   2554 				    NCR_READ_REG(sc, NCR_FFLAG)
   2555 				     & NCRFIFO_FF,
   2556 				    sc->sc_espintr, sc->sc_espstat,
   2557 				    sc->sc_espstep);
   2558 				NCRCMD(sc, NCRCMD_FLUSH);
   2559 				ncr53c9x_sched_msgout(SEND_ABORT);
   2560 				return (1);
   2561 			case 2:
   2562 				/* Select stuck at Command Phase */
   2563 				NCRCMD(sc, NCRCMD_FLUSH);
   2564 				break;
   2565 			case 4:
   2566 				if (sc->sc_features & NCR_F_DMASELECT &&
   2567 				    sc->sc_cmdlen != 0)
   2568 					printf("(%s:%d:%d): select; "
   2569 					    "%lu left in DMA buffer "
   2570 					    "[intr %x, stat %x, step %d]\n",
   2571 					    sc->sc_dev.dv_xname,
   2572 					    periph->periph_target,
   2573 					    periph->periph_lun,
   2574 					    (u_long)sc->sc_cmdlen,
   2575 					    sc->sc_espintr,
   2576 					    sc->sc_espstat,
   2577 					    sc->sc_espstep);
   2578 				/* So far, everything went fine */
   2579 				break;
   2580 			}
   2581 
   2582 			sc->sc_prevphase = INVALID_PHASE; /* ?? */
   2583 			/* Do an implicit RESTORE POINTERS. */
   2584 			sc->sc_dp = ecb->daddr;
   2585 			sc->sc_dleft = ecb->dleft;
   2586 			sc->sc_state = NCR_CONNECTED;
   2587 			break;
   2588 
   2589 		} else {
   2590 
   2591 			printf("%s: unexpected status after select"
   2592 			    ": [intr %x, stat %x, step %x]\n",
   2593 			    sc->sc_dev.dv_xname,
   2594 			    sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
   2595 			NCRCMD(sc, NCRCMD_FLUSH);
   2596 			DELAY(1);
   2597 			goto reset;
   2598 		}
   2599 		if (sc->sc_state == NCR_IDLE) {
   2600 			printf("%s: stray interrupt\n", sc->sc_dev.dv_xname);
   2601 			return (0);
   2602 		}
   2603 		break;
   2604 
   2605 	case NCR_CONNECTED:
   2606 		if ((sc->sc_flags & NCR_ICCS) != 0) {
   2607 			/* "Initiate Command Complete Steps" in progress */
   2608 			u_char msg;
   2609 
   2610 			sc->sc_flags &= ~NCR_ICCS;
   2611 
   2612 			if (!(sc->sc_espintr & NCRINTR_DONE)) {
   2613 				printf("%s: ICCS: "
   2614 				    ": [intr %x, stat %x, step %x]\n",
   2615 				    sc->sc_dev.dv_xname,
   2616 				    sc->sc_espintr, sc->sc_espstat,
   2617 				    sc->sc_espstep);
   2618 			}
   2619 			ncr53c9x_rdfifo(sc, NCR_RDFIFO_START);
   2620 			if (sc->sc_imlen < 2)
   2621 				printf("%s: can't get status, only %d bytes\n",
   2622 				    sc->sc_dev.dv_xname, (int)sc->sc_imlen);
   2623 			ecb->stat = sc->sc_imess[sc->sc_imlen - 2];
   2624 			msg = sc->sc_imess[sc->sc_imlen - 1];
   2625 			NCR_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
   2626 			if (msg == MSG_CMDCOMPLETE) {
   2627 				ecb->dleft = (ecb->flags & ECB_TENTATIVE_DONE)
   2628 					? 0 : sc->sc_dleft;
   2629 				if ((ecb->flags & ECB_SENSE) == 0)
   2630 					ecb->xs->resid = ecb->dleft;
   2631 				sc->sc_state = NCR_CMDCOMPLETE;
   2632 			} else
   2633 				printf("%s: STATUS_PHASE: msg %d\n",
   2634 				    sc->sc_dev.dv_xname, msg);
   2635 			sc->sc_imlen = 0;
   2636 			NCRCMD(sc, NCRCMD_MSGOK);
   2637 			goto shortcut; /* ie. wait for disconnect */
   2638 		}
   2639 		break;
   2640 
   2641 	default:
   2642 		/* Don't panic: reset. */
   2643 		printf("%s: invalid state: %d\n",
   2644 		    sc->sc_dev.dv_xname, sc->sc_state);
   2645 		ncr53c9x_scsi_reset(sc);
   2646 		goto out;
   2647 	}
   2648 
   2649 	/*
   2650 	 * Driver is now in state NCR_CONNECTED, i.e. we
   2651 	 * have a current command working the SCSI bus.
   2652 	 */
   2653 	if (sc->sc_state != NCR_CONNECTED || ecb == NULL) {
   2654 		panic("ncr53c9x: no nexus");
   2655 	}
   2656 
   2657 	switch (sc->sc_phase) {
   2658 	case MESSAGE_OUT_PHASE:
   2659 		NCR_PHASE(("MESSAGE_OUT_PHASE "));
   2660 		ncr53c9x_msgout(sc);
   2661 		sc->sc_prevphase = MESSAGE_OUT_PHASE;
   2662 		break;
   2663 
   2664 	case MESSAGE_IN_PHASE:
   2665 msgin:
   2666 		NCR_PHASE(("MESSAGE_IN_PHASE "));
   2667 		if ((sc->sc_espintr & NCRINTR_BS) != 0) {
   2668 			if ((sc->sc_rev != NCR_VARIANT_FAS366) ||
   2669 			    !(sc->sc_espstat2 & NCRFAS_STAT2_EMPTY)) {
   2670 				NCRCMD(sc, NCRCMD_FLUSH);
   2671 			}
   2672 			sc->sc_flags |= NCR_WAITI;
   2673 			NCRCMD(sc, NCRCMD_TRANS);
   2674 		} else if ((sc->sc_espintr & NCRINTR_FC) != 0) {
   2675 			if ((sc->sc_flags & NCR_WAITI) == 0) {
   2676 				printf("%s: MSGIN: unexpected FC bit: "
   2677 				    "[intr %x, stat %x, step %x]\n",
   2678 				    sc->sc_dev.dv_xname,
   2679 				    sc->sc_espintr, sc->sc_espstat,
   2680 				    sc->sc_espstep);
   2681 			}
   2682 			sc->sc_flags &= ~NCR_WAITI;
   2683 			ncr53c9x_rdfifo(sc,
   2684 			    (sc->sc_prevphase == sc->sc_phase) ?
   2685 			    NCR_RDFIFO_CONTINUE : NCR_RDFIFO_START);
   2686 			ncr53c9x_msgin(sc);
   2687 		} else {
   2688 			printf("%s: MSGIN: weird bits: "
   2689 			    "[intr %x, stat %x, step %x]\n",
   2690 			    sc->sc_dev.dv_xname,
   2691 			    sc->sc_espintr, sc->sc_espstat, sc->sc_espstep);
   2692 		}
   2693 		sc->sc_prevphase = MESSAGE_IN_PHASE;
   2694 		goto shortcut;	/* i.e. expect data to be ready */
   2695 		break;
   2696 
   2697 	case COMMAND_PHASE:
   2698 		/*
   2699 		 * Send the command block. Normally we don't see this
   2700 		 * phase because the SEL_ATN command takes care of
   2701 		 * all this. However, we end up here if either the
   2702 		 * target or we wanted to exchange some more messages
   2703 		 * first (e.g. to start negotiations).
   2704 		 */
   2705 
   2706 		NCR_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
   2707 		    ecb->cmd.cmd.opcode, ecb->clen));
   2708 		if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) {
   2709 			NCRCMD(sc, NCRCMD_FLUSH);
   2710 /*			DELAY(1);*/
   2711 		}
   2712 		if (sc->sc_features & NCR_F_DMASELECT) {
   2713 			size_t size;
   2714 			/* setup DMA transfer for command */
   2715 			size = ecb->clen;
   2716 			sc->sc_cmdlen = size;
   2717 			sc->sc_cmdp = (caddr_t)&ecb->cmd.cmd;
   2718 			NCRDMA_SETUP(sc, &sc->sc_cmdp, &sc->sc_cmdlen,
   2719 			    0, &size);
   2720 			/* Program the SCSI counter */
   2721 			NCR_SET_COUNT(sc, size);
   2722 
   2723 			/* load the count in */
   2724 			NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
   2725 
   2726 			/* start the command transfer */
   2727 			NCRCMD(sc, NCRCMD_TRANS | NCRCMD_DMA);
   2728 			NCRDMA_GO(sc);
   2729 		} else {
   2730 			ncr53c9x_wrfifo(sc, (u_char *)&ecb->cmd.cmd, ecb->clen);
   2731 			NCRCMD(sc, NCRCMD_TRANS);
   2732 		}
   2733 		sc->sc_prevphase = COMMAND_PHASE;
   2734 		break;
   2735 
   2736 	case DATA_OUT_PHASE:
   2737 		NCR_PHASE(("DATA_OUT_PHASE [%ld] ",(long)sc->sc_dleft));
   2738 		NCRCMD(sc, NCRCMD_FLUSH);
   2739 		size = min(sc->sc_dleft, sc->sc_maxxfer);
   2740 		NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 0, &size);
   2741 		sc->sc_prevphase = DATA_OUT_PHASE;
   2742 		goto setup_xfer;
   2743 
   2744 	case DATA_IN_PHASE:
   2745 		NCR_PHASE(("DATA_IN_PHASE "));
   2746 		if (sc->sc_rev == NCR_VARIANT_ESP100)
   2747 			NCRCMD(sc, NCRCMD_FLUSH);
   2748 		size = min(sc->sc_dleft, sc->sc_maxxfer);
   2749 		NCRDMA_SETUP(sc, &sc->sc_dp, &sc->sc_dleft, 1, &size);
   2750 		sc->sc_prevphase = DATA_IN_PHASE;
   2751 	setup_xfer:
   2752 		/* Target returned to data phase: wipe "done" memory */
   2753 		ecb->flags &= ~ECB_TENTATIVE_DONE;
   2754 
   2755 		/* Program the SCSI counter */
   2756 		NCR_SET_COUNT(sc, size);
   2757 
   2758 		/* load the count in */
   2759 		NCRCMD(sc, NCRCMD_NOP|NCRCMD_DMA);
   2760 
   2761 		/*
   2762 		 * Note that if `size' is 0, we've already transceived
   2763 		 * all the bytes we want but we're still in DATA PHASE.
   2764 		 * Apparently, the device needs padding. Also, a
   2765 		 * transfer size of 0 means "maximum" to the chip
   2766 		 * DMA logic.
   2767 		 */
   2768 		NCRCMD(sc,
   2769 		    (size == 0 ? NCRCMD_TRPAD : NCRCMD_TRANS) | NCRCMD_DMA);
   2770 		NCRDMA_GO(sc);
   2771 		return (1);
   2772 
   2773 	case STATUS_PHASE:
   2774 		NCR_PHASE(("STATUS_PHASE "));
   2775 		sc->sc_flags |= NCR_ICCS;
   2776 		NCRCMD(sc, NCRCMD_ICCS);
   2777 		sc->sc_prevphase = STATUS_PHASE;
   2778 		goto shortcut;	/* i.e. expect status results soon */
   2779 		break;
   2780 
   2781 	case INVALID_PHASE:
   2782 		break;
   2783 
   2784 	default:
   2785 		printf("%s: unexpected bus phase; resetting\n",
   2786 		    sc->sc_dev.dv_xname);
   2787 		goto reset;
   2788 	}
   2789 
   2790 out:
   2791 	return (1);
   2792 
   2793 reset:
   2794 	ncr53c9x_init(sc, 1);
   2795 	goto out;
   2796 
   2797 finish:
   2798 	ncr53c9x_done(sc, ecb);
   2799 	goto out;
   2800 
   2801 sched:
   2802 	sc->sc_state = NCR_IDLE;
   2803 	ncr53c9x_sched(sc);
   2804 	goto out;
   2805 
   2806 shortcut:
   2807 	/*
   2808 	 * The idea is that many of the SCSI operations take very little
   2809 	 * time, and going away and getting interrupted is too high an
   2810 	 * overhead to pay. For example, selecting, sending a message
   2811 	 * and command and then doing some work can be done in one "pass".
   2812 	 *
   2813 	 * The delay is a heuristic. It is 2 when at 20MHz, 2 at 25MHz and 1
   2814 	 * at 40MHz. This needs testing.
   2815 	 */
   2816 	{
   2817 		struct timeval wait, cur;
   2818 
   2819 		microtime(&wait);
   2820 		wait.tv_usec += 50 / sc->sc_freq;
   2821 		if (wait.tv_usec > 1000000) {
   2822 			wait.tv_sec++;
   2823 			wait.tv_usec -= 1000000;
   2824 		}
   2825 		do {
   2826 			if (NCRDMA_ISINTR(sc))
   2827 				goto again;
   2828 			microtime(&cur);
   2829 		} while (cur.tv_sec <= wait.tv_sec &&
   2830 			 cur.tv_usec <= wait.tv_usec);
   2831 	}
   2832 	goto out;
   2833 }
   2834 
   2835 void
   2836 ncr53c9x_abort(sc, ecb)
   2837 	struct ncr53c9x_softc *sc;
   2838 	struct ncr53c9x_ecb *ecb;
   2839 {
   2840 
   2841 	/* 2 secs for the abort */
   2842 	ecb->timeout = NCR_ABORT_TIMEOUT;
   2843 	ecb->flags |= ECB_ABORT;
   2844 
   2845 	if (ecb == sc->sc_nexus) {
   2846 		/*
   2847 		 * If we're still selecting, the message will be scheduled
   2848 		 * after selection is complete.
   2849 		 */
   2850 		if (sc->sc_state == NCR_CONNECTED)
   2851 			ncr53c9x_sched_msgout(SEND_ABORT);
   2852 
   2853 		/*
   2854 		 * Reschedule timeout.
   2855 		 */
   2856 		callout_reset(&ecb->xs->xs_callout, mstohz(ecb->timeout),
   2857 		    ncr53c9x_timeout, ecb);
   2858 	} else {
   2859 		/*
   2860 		 * Just leave the command where it is.
   2861 		 * XXX - what choice do we have but to reset the SCSI
   2862 		 *	 eventually?
   2863 		 */
   2864 		if (sc->sc_state == NCR_IDLE)
   2865 			ncr53c9x_sched(sc);
   2866 	}
   2867 }
   2868 
   2869 void
   2870 ncr53c9x_timeout(arg)
   2871 	void *arg;
   2872 {
   2873 	struct ncr53c9x_ecb *ecb = arg;
   2874 	struct scsipi_xfer *xs = ecb->xs;
   2875 	struct scsipi_periph *periph = xs->xs_periph;
   2876 	struct ncr53c9x_softc *sc =
   2877 	    (void *)periph->periph_channel->chan_adapter->adapt_dev;
   2878 	struct ncr53c9x_tinfo *ti = &sc->sc_tinfo[periph->periph_target];
   2879 	int s;
   2880 
   2881 	scsipi_printaddr(periph);
   2882 	printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
   2883 	    "<state %d, nexus %p, phase(l %x, c %x, p %x), resid %lx, "
   2884 	    "msg(q %x,o %x) %s>",
   2885 	    sc->sc_dev.dv_xname,
   2886 	    ecb, ecb->flags, ecb->dleft, ecb->stat,
   2887 	    sc->sc_state, sc->sc_nexus,
   2888 	    NCR_READ_REG(sc, NCR_STAT),
   2889 	    sc->sc_phase, sc->sc_prevphase,
   2890 	    (long)sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
   2891 	    NCRDMA_ISACTIVE(sc) ? "DMA active" : "");
   2892 #if NCR53C9X_DEBUG > 1
   2893 	printf("TRACE: %s.", ecb->trace);
   2894 #endif
   2895 
   2896 	s = splbio();
   2897 
   2898 	if (ecb->flags & ECB_ABORT) {
   2899 		/* abort timed out */
   2900 		printf(" AGAIN\n");
   2901 
   2902 		ncr53c9x_init(sc, 1);
   2903 	} else {
   2904 		/* abort the operation that has timed out */
   2905 		printf("\n");
   2906 		xs->error = XS_TIMEOUT;
   2907 		ncr53c9x_abort(sc, ecb);
   2908 
   2909 		/* Disable sync mode if stuck in a data phase */
   2910 		if (ecb == sc->sc_nexus &&
   2911 		    (ti->flags & T_SYNCMODE) != 0 &&
   2912 		    (sc->sc_phase & (MSGI|CDI)) == 0) {
   2913 			/* XXX ASYNC CALLBACK! */
   2914 			scsipi_printaddr(periph);
   2915 			printf("sync negotiation disabled\n");
   2916 			sc->sc_cfflags |= (1 << (periph->periph_target + 8));
   2917 			ncr53c9x_update_xfer_mode(sc, periph->periph_target);
   2918 		}
   2919 	}
   2920 
   2921 	splx(s);
   2922 }
   2923 
   2924 void
   2925 ncr53c9x_watch(arg)
   2926 	void *arg;
   2927 {
   2928 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   2929 	struct ncr53c9x_tinfo *ti;
   2930 	struct ncr53c9x_linfo *li;
   2931 	int t, s;
   2932 	/* Delete any structures that have not been used in 10min. */
   2933 	time_t old = time.tv_sec - (10 * 60);
   2934 
   2935 	s = splbio();
   2936 	for (t = 0; t < NCR_NTARG; t++) {
   2937 		ti = &sc->sc_tinfo[t];
   2938 		li = LIST_FIRST(&ti->luns);
   2939 		while (li) {
   2940 			if (li->last_used < old &&
   2941 			    li->untagged == NULL &&
   2942 			    li->used == 0) {
   2943 				if (li->lun < NCR_NLUN)
   2944 					ti->lun[li->lun] = NULL;
   2945 				LIST_REMOVE(li, link);
   2946 				free(li, M_DEVBUF);
   2947 				/* Restart the search at the beginning */
   2948 				li = LIST_FIRST(&ti->luns);
   2949 				continue;
   2950 			}
   2951 			li = LIST_NEXT(li, link);
   2952 		}
   2953 	}
   2954 	splx(s);
   2955 	callout_reset(&sc->sc_watchdog, 60 * hz, ncr53c9x_watch, sc);
   2956 }
   2957 
   2958