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