Home | History | Annotate | Line # | Download | only in ic
osiop.c revision 1.24
      1 /*	$NetBSD: osiop.c,v 1.24 2005/04/30 02:18:43 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * Copyright (c) 1990 The Regents of the University of California.
     31  * All rights reserved.
     32  *
     33  * This code is derived from software contributed to Berkeley by
     34  * Van Jacobson of Lawrence Berkeley Laboratory.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     61  */
     62 
     63 /*
     64  * Copyright (c) 1994 Michael L. Hitch
     65  *
     66  * This code is derived from software contributed to Berkeley by
     67  * Van Jacobson of Lawrence Berkeley Laboratory.
     68  *
     69  * Redistribution and use in source and binary forms, with or without
     70  * modification, are permitted provided that the following conditions
     71  * are met:
     72  * 1. Redistributions of source code must retain the above copyright
     73  *    notice, this list of conditions and the following disclaimer.
     74  * 2. Redistributions in binary form must reproduce the above copyright
     75  *    notice, this list of conditions and the following disclaimer in the
     76  *    documentation and/or other materials provided with the distribution.
     77  *
     78  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     79  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     80  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     81  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     82  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     83  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     87  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     88  *
     89  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     90  */
     91 
     92 /*
     93  * MI NCR53C710 scsi adaptor driver; based on arch/amiga/dev/siop.c:
     94  *	NetBSD: siop.c,v 1.43 1999/09/30 22:59:53 thorpej Exp
     95  *
     96  * bus_space/bus_dma'fied by Izumi Tsutsui <tsutsui (at) ceres.dti.ne.jp>
     97  *
     98  * The 53c710 datasheet is available at:
     99  * http://www.lsilogic.com/techlib/techdocs/storage_stand_prod/index.html
    100  */
    101 
    102 #include <sys/cdefs.h>
    103 __KERNEL_RCSID(0, "$NetBSD: osiop.c,v 1.24 2005/04/30 02:18:43 tsutsui Exp $");
    104 
    105 /* #define OSIOP_DEBUG */
    106 
    107 #include "opt_ddb.h"
    108 
    109 #include <sys/param.h>
    110 #include <sys/systm.h>
    111 #include <sys/device.h>
    112 #include <sys/malloc.h>
    113 #include <sys/buf.h>
    114 #include <sys/kernel.h>
    115 
    116 #include <uvm/uvm_extern.h>
    117 
    118 #include <dev/scsipi/scsi_all.h>
    119 #include <dev/scsipi/scsipi_all.h>
    120 #include <dev/scsipi/scsiconf.h>
    121 #include <dev/scsipi/scsi_message.h>
    122 
    123 #include <machine/cpu.h>
    124 #include <machine/bus.h>
    125 
    126 #include <dev/ic/osiopreg.h>
    127 #include <dev/ic/osiopvar.h>
    128 
    129 /* 53C710 script */
    130 #include <dev/microcode/siop/osiop.out>
    131 
    132 void osiop_attach(struct osiop_softc *);
    133 void osiop_minphys(struct buf *);
    134 void osiop_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
    135     void *);
    136 void osiop_poll(struct osiop_softc *, struct osiop_acb *);
    137 void osiop_sched(struct osiop_softc *);
    138 void osiop_scsidone(struct osiop_acb *, int);
    139 void osiop_abort(struct osiop_softc *, const char *);
    140 void osiop_init(struct osiop_softc *);
    141 void osiop_reset(struct osiop_softc *);
    142 void osiop_resetbus(struct osiop_softc *);
    143 void osiop_start(struct osiop_softc *);
    144 int osiop_checkintr(struct osiop_softc *, uint8_t, uint8_t, uint8_t, int *);
    145 void osiop_select(struct osiop_softc *);
    146 void osiop_update_xfer_mode(struct osiop_softc *, int);
    147 void scsi_period_to_osiop(struct osiop_softc *, int);
    148 void osiop_timeout(void *);
    149 
    150 int osiop_reset_delay = 250;	/* delay after reset, in milliseconds */
    151 
    152 #ifdef OSIOP_DEBUG
    153 #define DEBUG_DMA	0x01
    154 #define DEBUG_INT	0x02
    155 #define DEBUG_PHASE	0x04
    156 #define DEBUG_UNEXCEPT	0x08
    157 #define DEBUG_DISC	0x10
    158 #define DEBUG_CMD	0x20
    159 #define DEBUG_ALL	0xff
    160 int osiop_debug = 0; /*DEBUG_ALL;*/
    161 
    162 int osiopsync_debug = 0;
    163 int osiopdma_hits = 1;
    164 int osiopstarts = 0;
    165 int osiopints = 0;
    166 int osiopphmm = 0;
    167 int osiop_trix = 0;
    168 #define OSIOP_TRACE_SIZE	128
    169 #define OSIOP_TRACE(a,b,c,d)	do {				\
    170 	osiop_trbuf[osiop_trix + 0] = (a);			\
    171 	osiop_trbuf[osiop_trix + 1] = (b);			\
    172 	osiop_trbuf[osiop_trix + 2] = (c);			\
    173 	osiop_trbuf[osiop_trix + 3] = (d);			\
    174 	osiop_trix = (osiop_trix + 4) & (OSIOP_TRACE_SIZE - 1);	\
    175 } while (0)
    176 uint8_t osiop_trbuf[OSIOP_TRACE_SIZE];
    177 void osiop_dump_trace(void);
    178 void osiop_dump_acb(struct osiop_acb *);
    179 void osiop_dump(struct osiop_softc *);
    180 #else
    181 #define OSIOP_TRACE(a,b,c,d)
    182 #endif
    183 
    184 void
    185 osiop_attach(struct osiop_softc *sc)
    186 {
    187 	struct osiop_acb *acb;
    188 	bus_dma_segment_t seg;
    189 	int nseg;
    190 	int i, err;
    191 
    192 	/*
    193 	 * Allocate and map DMA-safe memory for the script.
    194 	 */
    195 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    196 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
    197 	if (err) {
    198 		printf(": failed to allocate script memory, err=%d\n", err);
    199 		return;
    200 	}
    201 	err = bus_dmamem_map(sc->sc_dmat, &seg, nseg, PAGE_SIZE,
    202 	    (caddr_t *)&sc->sc_script, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    203 	if (err) {
    204 		printf(": failed to map script memory, err=%d\n", err);
    205 		return;
    206 	}
    207 	err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
    208 	    BUS_DMA_NOWAIT, &sc->sc_scrdma);
    209 	if (err) {
    210 		printf(": failed to create script map, err=%d\n", err);
    211 		return;
    212 	}
    213 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_scrdma,
    214 	    sc->sc_script, PAGE_SIZE, NULL, BUS_DMA_NOWAIT);
    215 	if (err) {
    216 		printf(": failed to load script map, err=%d\n", err);
    217 		return;
    218 	}
    219 
    220 	/*
    221 	 * Copy and sync script
    222 	 */
    223 	memcpy(sc->sc_script, osiop_script, sizeof(osiop_script));
    224 	bus_dmamap_sync(sc->sc_dmat, sc->sc_scrdma, 0, sizeof(osiop_script),
    225 	    BUS_DMASYNC_PREWRITE);
    226 
    227 	/*
    228 	 * Allocate and map DMA-safe memory for the script data structure.
    229 	 */
    230 	err = bus_dmamem_alloc(sc->sc_dmat,
    231 	    sizeof(struct osiop_ds) * OSIOP_NACB, PAGE_SIZE, 0,
    232 	    &seg, 1, &nseg, BUS_DMA_NOWAIT);
    233 	if (err) {
    234 		printf(": failed to allocate ds memory, err=%d\n", err);
    235 		return;
    236 	}
    237 	err = bus_dmamem_map(sc->sc_dmat, &seg, nseg,
    238 	    sizeof(struct osiop_ds) * OSIOP_NACB, (caddr_t *)&sc->sc_ds,
    239 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    240 	if (err) {
    241 		printf(": failed to map ds memory, err=%d\n", err);
    242 		return;
    243 	}
    244 	err = bus_dmamap_create(sc->sc_dmat,
    245 	    sizeof(struct osiop_ds) * OSIOP_NACB, 1,
    246 	    sizeof(struct osiop_ds) * OSIOP_NACB, 0,
    247 	    BUS_DMA_NOWAIT, &sc->sc_dsdma);
    248 	if (err) {
    249 		printf(": failed to create ds map, err=%d\n", err);
    250 		return;
    251 	}
    252 	err = bus_dmamap_load(sc->sc_dmat, sc->sc_dsdma, sc->sc_ds,
    253 	    sizeof(struct osiop_ds) * OSIOP_NACB, NULL, BUS_DMA_NOWAIT);
    254 	if (err) {
    255 		printf(": failed to load ds map, err=%d\n", err);
    256 		return;
    257 	}
    258 
    259 	acb = malloc(sizeof(struct osiop_acb) * OSIOP_NACB,
    260 	    M_DEVBUF, M_NOWAIT|M_ZERO);
    261 	if (acb == NULL) {
    262 		printf(": can't allocate memory for acb\n");
    263 		return;
    264 	}
    265 	sc->sc_acb = acb;
    266 	sc->sc_cfflags = sc->sc_dev.dv_cfdata->cf_flags;
    267 	sc->sc_nexus = NULL;
    268 	sc->sc_active = 0;
    269 	memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
    270 
    271 	/* Initialize command block queue */
    272 	TAILQ_INIT(&sc->ready_list);
    273 	TAILQ_INIT(&sc->nexus_list);
    274 	TAILQ_INIT(&sc->free_list);
    275 
    276 	/* Initialize each command block */
    277 	for (i = 0; i < OSIOP_NACB; i++) {
    278 		bus_addr_t dsa;
    279 
    280 		/* XXX How much size is required for each command block? */
    281 		err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    282 		    0, BUS_DMA_NOWAIT, &acb->cmddma);
    283 		if (err) {
    284 			printf(": failed to create cmddma map, err=%d\n", err);
    285 			return;
    286 		}
    287 		err = bus_dmamap_create(sc->sc_dmat, OSIOP_MAX_XFER, OSIOP_NSG,
    288 		    OSIOP_MAX_XFER, 0, BUS_DMA_NOWAIT, &acb->datadma);
    289 		if (err) {
    290 			printf(": failed to create datadma map, err=%d\n",
    291 			    err);
    292 			return;
    293 		}
    294 
    295 		acb->sc = sc;
    296 		acb->ds = &sc->sc_ds[i];
    297 		acb->dsoffset = sizeof(struct osiop_ds) * i;
    298 
    299 		dsa = sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset;
    300 		acb->ds->id.addr = dsa + OSIOP_DSIDOFF;
    301 		acb->ds->status.count = 1;
    302 		acb->ds->status.addr = dsa + OSIOP_DSSTATOFF;
    303 		acb->ds->msg.count = 1;
    304 		acb->ds->msg.addr = dsa + OSIOP_DSMSGOFF;
    305 		acb->ds->msgin.count = 1;
    306 		acb->ds->msgin.addr = dsa + OSIOP_DSMSGINOFF;
    307 		acb->ds->extmsg.count = 1;
    308 		acb->ds->extmsg.addr = dsa + OSIOP_DSEXTMSGOFF;
    309 		acb->ds->synmsg.count = 3;
    310 		acb->ds->synmsg.addr = dsa + OSIOP_DSSYNMSGOFF;
    311 		TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    312 
    313 		acb++;
    314 	}
    315 
    316 	printf(": NCR53C710 rev %d, %dMHz, SCSI ID %d\n",
    317 	    osiop_read_1(sc, OSIOP_CTEST8) >> 4, sc->sc_clock_freq, sc->sc_id);
    318 
    319 	/*
    320 	 * Initialize all
    321 	 */
    322 	osiop_init(sc);
    323 
    324 	/*
    325 	 * Fill in the adapter.
    326 	 */
    327 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    328 	sc->sc_adapter.adapt_nchannels = 1;
    329 	sc->sc_adapter.adapt_openings = OSIOP_NACB;
    330 	sc->sc_adapter.adapt_max_periph = 1;
    331 	sc->sc_adapter.adapt_ioctl = NULL;
    332 	sc->sc_adapter.adapt_minphys = osiop_minphys;
    333 	sc->sc_adapter.adapt_request = osiop_scsipi_request;
    334 
    335 	/*
    336 	 * Fill in the channel.
    337 	 */
    338 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    339 	sc->sc_channel.chan_bustype = &scsi_bustype;
    340 	sc->sc_channel.chan_channel = 0;
    341 	sc->sc_channel.chan_ntargets = OSIOP_NTGT;
    342 	sc->sc_channel.chan_nluns = 8;
    343 	sc->sc_channel.chan_id = sc->sc_id;
    344 
    345 	/*
    346 	 * Now try to attach all the sub devices.
    347 	 */
    348 	config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    349 }
    350 
    351 /*
    352  * default minphys routine for osiop based controllers
    353  */
    354 void
    355 osiop_minphys(struct buf *bp)
    356 {
    357 
    358 	if (bp->b_bcount > OSIOP_MAX_XFER)
    359 		bp->b_bcount = OSIOP_MAX_XFER;
    360 	minphys(bp);
    361 }
    362 
    363 /*
    364  * used by specific osiop controller
    365  *
    366  */
    367 void
    368 osiop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    369     void *arg)
    370 {
    371 	struct scsipi_xfer *xs;
    372 	struct scsipi_periph *periph;
    373 	struct osiop_acb *acb;
    374 	struct osiop_softc *sc;
    375 	int err, flags, s;
    376 
    377 	sc = (struct osiop_softc *)chan->chan_adapter->adapt_dev;
    378 
    379 	switch (req) {
    380 	case ADAPTER_REQ_RUN_XFER:
    381 		xs = arg;
    382 		periph = xs->xs_periph;
    383 		flags = xs->xs_control;
    384 
    385 		/* XXXX ?? */
    386 		if (flags & XS_CTL_DATA_UIO)
    387 			panic("osiop: scsi data uio requested");
    388 
    389 		/* XXXX ?? */
    390 		if (sc->sc_nexus && flags & XS_CTL_POLL)
    391 #if 0
    392 			panic("osiop_scsicmd: busy");
    393 #else
    394 			printf("osiop_scsicmd: busy\n");
    395 #endif
    396 
    397 		s = splbio();
    398 		acb = TAILQ_FIRST(&sc->free_list);
    399 		if (acb != NULL) {
    400 			TAILQ_REMOVE(&sc->free_list, acb, chain);
    401 		}
    402 #ifdef DIAGNOSTIC
    403 		else {
    404 			scsipi_printaddr(periph);
    405 			printf("unable to allocate acb\n");
    406 			panic("osiop_scsipi_request");
    407 		}
    408 #endif
    409 
    410 		acb->status = ACB_S_READY;
    411 		acb->xs = xs;
    412 
    413 		/* Setup DMA map for SCSI command buffer */
    414 		err = bus_dmamap_load(sc->sc_dmat, acb->cmddma,
    415 		    xs->cmd, xs->cmdlen, NULL, BUS_DMA_NOWAIT);
    416 		if (err) {
    417 			printf("%s: unable to load cmd DMA map: %d\n",
    418 			    sc->sc_dev.dv_xname, err);
    419 			xs->error = XS_DRIVER_STUFFUP;
    420 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    421 			scsipi_done(xs);
    422 			splx(s);
    423 			return;
    424 		}
    425 
    426 		/* Setup DMA map for data buffer */
    427 		if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    428 			err = bus_dmamap_load(sc->sc_dmat, acb->datadma,
    429 			    xs->data, xs->datalen, NULL,
    430 			    BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
    431 			    ((xs->xs_control & XS_CTL_DATA_IN) ?
    432 			     BUS_DMA_READ : BUS_DMA_WRITE));
    433 			if (err) {
    434 				printf("%s: unable to load data DMA map: %d\n",
    435 				    sc->sc_dev.dv_xname, err);
    436 				xs->error = XS_DRIVER_STUFFUP;
    437 				scsipi_done(xs);
    438 				bus_dmamap_unload(sc->sc_dmat, acb->cmddma);
    439 				TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    440 				splx(s);
    441 				return;
    442 			}
    443 			bus_dmamap_sync(sc->sc_dmat, acb->datadma,
    444 			    0, xs->datalen, (xs->xs_control & XS_CTL_DATA_IN) ?
    445 			    BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    446 		}
    447 		bus_dmamap_sync(sc->sc_dmat, acb->cmddma, 0, xs->cmdlen,
    448 		    BUS_DMASYNC_PREWRITE);
    449 
    450 		acb->cmdlen = xs->cmdlen;
    451 		acb->datalen = xs->datalen;
    452 #ifdef OSIOP_DEBUG
    453 		acb->data = xs->data;
    454 #endif
    455 
    456 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    457 
    458 		if (sc->sc_nexus == NULL)
    459 			osiop_sched(sc);
    460 
    461 		splx(s);
    462 
    463 		if (flags & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA)
    464 			osiop_poll(sc, acb);
    465 		return;
    466 
    467 	case ADAPTER_REQ_GROW_RESOURCES:
    468 		return;
    469 
    470 	case ADAPTER_REQ_SET_XFER_MODE:
    471 		{
    472 			struct osiop_tinfo *ti;
    473 			struct scsipi_xfer_mode *xm = arg;
    474 
    475 			ti = &sc->sc_tinfo[xm->xm_target];
    476 
    477 			if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
    478 			    (ti->flags & TI_NOSYNC) == 0)
    479 				ti->state = NEG_INIT;
    480 
    481 			/*
    482 			 * If we're not going to negotiate, send the
    483 			 * notification now, since it won't happen later.
    484 			 */
    485 			if (ti->state == NEG_DONE)
    486 				osiop_update_xfer_mode(sc, xm->xm_target);
    487 
    488 			return;
    489 		}
    490 	}
    491 }
    492 
    493 void
    494 osiop_poll(struct osiop_softc *sc, struct osiop_acb *acb)
    495 {
    496 	struct scsipi_xfer *xs = acb->xs;
    497 	int status, i, s, to;
    498 	uint8_t istat, dstat, sstat0;
    499 
    500 	s = splbio();
    501 	to = xs->timeout / 1000;
    502 	if (!TAILQ_EMPTY(&sc->nexus_list))
    503 		printf("%s: osiop_poll called with disconnected device\n",
    504 		    sc->sc_dev.dv_xname);
    505 	for (;;) {
    506 		i = 1000;
    507 		while (((istat = osiop_read_1(sc, OSIOP_ISTAT)) &
    508 		    (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
    509 			if (i <= 0) {
    510 #ifdef OSIOP_DEBUG
    511 				printf("waiting: tgt %d cmd %02x sbcl %02x"
    512 				    " dsp %x (+%lx) dcmd %x"
    513 				    " ds %p timeout %d\n",
    514 				    xs->xs_periph->periph_target,
    515 				    xs->cmd->opcode,
    516 				    osiop_read_1(sc, OSIOP_SBCL),
    517 				    osiop_read_4(sc, OSIOP_DSP),
    518 				    osiop_read_4(sc, OSIOP_DSP) -
    519 				        sc->sc_scrdma->dm_segs[0].ds_addr,
    520 				    osiop_read_1(sc, OSIOP_DCMD),
    521 				    acb->ds, acb->xs->timeout);
    522 #endif
    523 				i = 1000;
    524 				to--;
    525 				if (to <= 0) {
    526 					osiop_reset(sc);
    527 					splx(s);
    528 					return;
    529 				}
    530 			}
    531 			delay(1000);
    532 			i--;
    533 		}
    534 		sstat0 = osiop_read_1(sc, OSIOP_SSTAT0);
    535 		delay(25);
    536 		dstat = osiop_read_1(sc, OSIOP_DSTAT);
    537 		if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
    538 			if (acb != sc->sc_nexus)
    539 				printf("%s: osiop_poll disconnected device"
    540 				    " completed\n", sc->sc_dev.dv_xname);
    541 			else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
    542 				sc->sc_flags &= ~OSIOP_INTSOFF;
    543 				osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
    544 				osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
    545 			}
    546 			osiop_scsidone(sc->sc_nexus, status);
    547 		}
    548 
    549 		if (xs->xs_status & XS_STS_DONE)
    550 			break;
    551 	}
    552 
    553 	splx(s);
    554 	return;
    555 }
    556 
    557 /*
    558  * start next command that's ready
    559  */
    560 void
    561 osiop_sched(struct osiop_softc *sc)
    562 {
    563 	struct scsipi_periph *periph;
    564 	struct osiop_acb *acb;
    565 	int i;
    566 
    567 #ifdef OSIOP_DEBUG
    568 	if (sc->sc_nexus != NULL) {
    569 		printf("%s: osiop_sched- nexus %p/%d ready %p/%d\n",
    570 		    sc->sc_dev.dv_xname, sc->sc_nexus,
    571 		    sc->sc_nexus->xs->xs_periph->periph_target,
    572 		    sc->ready_list.tqh_first,
    573 		    sc->ready_list.tqh_first->xs->xs_periph->periph_target);
    574 		return;
    575 	}
    576 #endif
    577 	TAILQ_FOREACH(acb, &sc->ready_list, chain) {
    578 		periph = acb->xs->xs_periph;
    579 		i = periph->periph_target;
    580 		if ((sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun)) == 0) {
    581 			struct osiop_tinfo *ti;
    582 
    583 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    584 			sc->sc_nexus = acb;
    585 			ti = &sc->sc_tinfo[i];
    586 			ti->lubusy |= (1 << periph->periph_lun);
    587 			break;
    588 		}
    589 	}
    590 
    591 	if (acb == NULL) {
    592 #ifdef OSIOP_DEBUG
    593 		printf("%s: osiop_sched didn't find ready command\n",
    594 		    sc->sc_dev.dv_xname);
    595 #endif
    596 		return;
    597 	}
    598 
    599 	if (acb->xs->xs_control & XS_CTL_RESET)
    600 		osiop_reset(sc);
    601 
    602 	sc->sc_active++;
    603 	osiop_select(sc);
    604 }
    605 
    606 void
    607 osiop_scsidone(struct osiop_acb *acb, int status)
    608 {
    609 	struct scsipi_xfer *xs;
    610 	struct scsipi_periph *periph;
    611 	struct osiop_softc *sc;
    612 	int dosched = 0;
    613 
    614 #ifdef DIAGNOSTIC
    615 	if (acb == NULL || acb->xs == NULL) {
    616 		printf("osiop_scsidone: NULL acb or scsipi_xfer\n");
    617 #if defined(OSIOP_DEBUG) && defined(DDB)
    618 		Debugger();
    619 #endif
    620 		return;
    621 	}
    622 #endif
    623 	xs = acb->xs;
    624 	sc = acb->sc;
    625 	periph = xs->xs_periph;
    626 
    627 #ifdef OSIOP_DEBUG
    628 	if (acb->status != ACB_S_DONE)
    629 		printf("%s: acb not done (status %d)\n",
    630 		    sc->sc_dev.dv_xname, acb->status);
    631 #endif
    632 
    633 	xs->status = status;
    634 
    635 	switch (status) {
    636 	case SCSI_OK:
    637 		xs->error = XS_NOERROR;
    638 		break;
    639 	case SCSI_BUSY:
    640 		xs->error = XS_BUSY;
    641 		break;
    642 	case SCSI_CHECK:
    643 		xs->error = XS_BUSY;
    644 		break;
    645 	case SCSI_OSIOP_NOCHECK:
    646 		/*
    647 		 * don't check status, xs->error is already valid
    648 		 */
    649 		break;
    650 	case SCSI_OSIOP_NOSTATUS:
    651 		/*
    652 		 * the status byte was not updated, cmd was
    653 		 * aborted
    654 		 */
    655 		xs->error = XS_SELTIMEOUT;
    656 		break;
    657 	default:
    658 #ifdef OSIOP_DEBUG
    659 		printf("%s: osiop_scsidone: unknown status code (0x%02x)\n",
    660 		    sc->sc_dev.dv_xname, status);
    661 #endif
    662 		xs->error = XS_DRIVER_STUFFUP;
    663 		break;
    664 	}
    665 
    666 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    667 		bus_dmamap_sync(sc->sc_dmat, acb->datadma, 0, acb->datalen,
    668 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    669 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    670 		bus_dmamap_unload(sc->sc_dmat, acb->datadma);
    671 	}
    672 
    673 	bus_dmamap_sync(sc->sc_dmat, acb->cmddma, 0, acb->cmdlen,
    674 	    BUS_DMASYNC_POSTWRITE);
    675 	bus_dmamap_unload(sc->sc_dmat, acb->cmddma);
    676 
    677 	/*
    678 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    679 	 * a hack to figure out which queue it's on.  Note that it is *not*
    680 	 * necessary to cdr down the ready queue, but we must cdr down the
    681 	 * nexus queue and see if it's there, so we can mark the unit as no
    682 	 * longer busy.  This code is sickening, but it works.
    683 	 */
    684 	if (acb == sc->sc_nexus) {
    685 		sc->sc_nexus = NULL;
    686 		sc->sc_tinfo[periph->periph_target].lubusy &=
    687 		    ~(1 << periph->periph_lun);
    688 		if (!TAILQ_EMPTY(&sc->ready_list))
    689 			dosched = 1;	/* start next command */
    690 		sc->sc_active--;
    691 		OSIOP_TRACE('d', 'a', status, 0);
    692 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
    693 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    694 		OSIOP_TRACE('d', 'r', status, 0);
    695 	} else {
    696 		struct osiop_acb *acb2;
    697 		TAILQ_FOREACH(acb2, &sc->nexus_list, chain) {
    698 			if (acb2 == acb) {
    699 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    700 				sc->sc_tinfo[periph->periph_target].lubusy &=
    701 				    ~(1 << periph->periph_lun);
    702 				sc->sc_active--;
    703 				break;
    704 			}
    705 		}
    706 		if (acb2 == NULL) {
    707 			if (acb->chain.tqe_next != NULL) {
    708 				TAILQ_REMOVE(&sc->ready_list, acb, chain);
    709 				sc->sc_active--;
    710 			} else {
    711 				printf("%s: can't find matching acb\n",
    712 				    sc->sc_dev.dv_xname);
    713 #ifdef DDB
    714 #if 0
    715 				Debugger();
    716 #endif
    717 #endif
    718 			}
    719 		}
    720 		OSIOP_TRACE('d', 'n', status, 0);
    721 	}
    722 	/* Put it on the free list. */
    723 	acb->status = ACB_S_FREE;
    724 	TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    725 	sc->sc_tinfo[periph->periph_target].cmds++;
    726 
    727 	callout_stop(&xs->xs_callout);
    728 	xs->resid = 0;
    729 	scsipi_done(xs);
    730 
    731 	if (dosched && sc->sc_nexus == NULL)
    732 		osiop_sched(sc);
    733 }
    734 
    735 void
    736 osiop_abort(struct osiop_softc *sc, const char *where)
    737 {
    738 
    739 	printf("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    740 	    sc->sc_dev.dv_xname, where,
    741 	    osiop_read_1(sc, OSIOP_DSTAT),
    742 	    osiop_read_1(sc, OSIOP_SSTAT0),
    743 	    osiop_read_1(sc, OSIOP_SBCL));
    744 
    745 	/* XXX XXX XXX */
    746 	if (sc->sc_active > 0) {
    747 		sc->sc_active = 0;
    748 	}
    749 }
    750 
    751 void
    752 osiop_init(struct osiop_softc *sc)
    753 {
    754 	int i, inhibit_sync, inhibit_disc;
    755 
    756 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
    757 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
    758 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
    759 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
    760 
    761 	if (sc->sc_minsync < 25)
    762 		sc->sc_minsync = 25;
    763 
    764 	if (sc->sc_clock_freq <= 25) {
    765 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1;	/* SCLK/1 */
    766 		sc->sc_tcp[0] = sc->sc_tcp[1];
    767 	} else if (sc->sc_clock_freq <= 37) {
    768 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1_5;	/* SCLK/1.5 */
    769 		sc->sc_tcp[0] = sc->sc_tcp[2];
    770 	} else if (sc->sc_clock_freq <= 50) {
    771 		sc->sc_dcntl |= OSIOP_DCNTL_CF_2;	/* SCLK/2 */
    772 		sc->sc_tcp[0] = sc->sc_tcp[3];
    773 	} else {
    774 		sc->sc_dcntl |= OSIOP_DCNTL_CF_3;	/* SCLK/3 */
    775 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
    776 	}
    777 
    778 	if ((sc->sc_cfflags & 0x10000) != 0) {
    779 		sc->sc_flags |= OSIOP_NODMA;
    780 #ifdef OSIOP_DEBUG
    781 		printf("%s: DMA disabled; use polling\n",
    782 		    sc->sc_dev.dv_xname);
    783 #endif
    784 	}
    785 
    786 	inhibit_sync = (sc->sc_cfflags & 0xff00) >> 8;	/* XXX */
    787 	inhibit_disc =  sc->sc_cfflags & 0x00ff;	/* XXX */
    788 #ifdef OSIOP_DEBUG
    789 	if (inhibit_sync != 0)
    790 		printf("%s: Inhibiting synchronous transfer: 0x%02x\n",
    791 		    sc->sc_dev.dv_xname, inhibit_sync);
    792 	if (inhibit_disc != 0)
    793 		printf("%s: Inhibiting disconnect: 0x%02x\n",
    794 		    sc->sc_dev.dv_xname, inhibit_disc);
    795 #endif
    796 	for (i = 0; i < OSIOP_NTGT; i++) {
    797 		if (inhibit_sync & (1 << i))
    798 			sc->sc_tinfo[i].flags |= TI_NOSYNC;
    799 		if (inhibit_disc & (1 << i))
    800 			sc->sc_tinfo[i].flags |= TI_NODISC;
    801 	}
    802 
    803 	osiop_resetbus(sc);
    804 	osiop_reset(sc);
    805 }
    806 
    807 void
    808 osiop_reset(struct osiop_softc *sc)
    809 {
    810 	struct osiop_acb *acb;
    811 	int i, s;
    812 	uint8_t stat;
    813 
    814 #ifdef OSIOP_DEBUG
    815 	printf("%s: resetting chip\n", sc->sc_dev.dv_xname);
    816 #endif
    817 	if (sc->sc_flags & OSIOP_ALIVE)
    818 		osiop_abort(sc, "reset");
    819 
    820 	s = splbio();
    821 
    822 	/*
    823 	 * Reset the chip
    824 	 * XXX - is this really needed?
    825 	 */
    826 
    827 	/* abort current script */
    828 	osiop_write_1(sc, OSIOP_ISTAT,
    829 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_ABRT);
    830 	/* reset chip */
    831 	osiop_write_1(sc, OSIOP_ISTAT,
    832 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_RST);
    833 	delay(100);
    834 	osiop_write_1(sc, OSIOP_ISTAT,
    835 	    osiop_read_1(sc, OSIOP_ISTAT) & ~OSIOP_ISTAT_RST);
    836 	delay(100);
    837 
    838 	/*
    839 	 * Set up various chip parameters
    840 	 */
    841 	osiop_write_1(sc, OSIOP_SCNTL0,
    842 	    OSIOP_ARB_FULL | OSIOP_SCNTL0_EPC | OSIOP_SCNTL0_EPG);
    843 	osiop_write_1(sc, OSIOP_SCNTL1, OSIOP_SCNTL1_ESR);
    844 	osiop_write_1(sc, OSIOP_DCNTL, sc->sc_dcntl);
    845 	osiop_write_1(sc, OSIOP_DMODE, OSIOP_DMODE_BL4);
    846 	/* don't enable interrupts yet */
    847 	osiop_write_1(sc, OSIOP_SIEN, 0x00);
    848 	osiop_write_1(sc, OSIOP_DIEN, 0x00);
    849 	osiop_write_1(sc, OSIOP_SCID, OSIOP_SCID_VALUE(sc->sc_id));
    850 	osiop_write_1(sc, OSIOP_DWT, 0x00);
    851 	osiop_write_1(sc, OSIOP_CTEST0, osiop_read_1(sc, OSIOP_CTEST0)
    852 	    | OSIOP_CTEST0_BTD | OSIOP_CTEST0_EAN);
    853 	osiop_write_1(sc, OSIOP_CTEST7,
    854 	    osiop_read_1(sc, OSIOP_CTEST7) | sc->sc_ctest7);
    855 	osiop_write_1(sc, OSIOP_CTEST4,
    856 	    osiop_read_1(sc, OSIOP_CTEST4) | sc->sc_ctest4);
    857 
    858 	/* will need to re-negotiate sync xfers */
    859 	for (i = 0; i < OSIOP_NTGT; i++) {
    860 		sc->sc_tinfo[i].state = NEG_INIT;
    861 		sc->sc_tinfo[i].period = 0;
    862 		sc->sc_tinfo[i].offset = 0;
    863 	}
    864 
    865 	stat = osiop_read_1(sc, OSIOP_ISTAT);
    866 	if (stat & OSIOP_ISTAT_SIP)
    867 		osiop_read_1(sc, OSIOP_SSTAT0);
    868 	delay(25);
    869 	if (stat & OSIOP_ISTAT_DIP)
    870 		osiop_read_1(sc, OSIOP_DSTAT);
    871 
    872 	splx(s);
    873 
    874 	delay(osiop_reset_delay * 1000);
    875 
    876 	if (sc->sc_nexus != NULL) {
    877 		sc->sc_nexus->xs->error =
    878 		    (sc->sc_nexus->flags & ACB_F_TIMEOUT) ?
    879 		    XS_TIMEOUT : XS_RESET;
    880 		sc->sc_nexus->status = ACB_S_DONE;
    881 		sc->sc_nexus->flags = 0;
    882 		osiop_scsidone(sc->sc_nexus, SCSI_OSIOP_NOCHECK);
    883 	}
    884 	while ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
    885 		acb->xs->error = (acb->flags & ACB_F_TIMEOUT) ?
    886 		    XS_TIMEOUT : XS_RESET;
    887 		acb->status = ACB_S_DONE;
    888 		acb->flags = 0;
    889 		osiop_scsidone(acb, SCSI_OSIOP_NOCHECK);
    890 	}
    891 
    892 	sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
    893 	/* enable SCSI and DMA interrupts */
    894 	sc->sc_sien = OSIOP_SIEN_M_A | OSIOP_SIEN_STO | /*OSIOP_SIEN_SEL |*/
    895 	    OSIOP_SIEN_SGE | OSIOP_SIEN_UDC | OSIOP_SIEN_RST | OSIOP_SIEN_PAR;
    896 	sc->sc_dien = OSIOP_DIEN_BF | OSIOP_DIEN_ABRT | OSIOP_DIEN_SIR |
    897 	    /*OSIOP_DIEN_WTD |*/ OSIOP_DIEN_IID;
    898 	osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
    899 	osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
    900 }
    901 
    902 void
    903 osiop_resetbus(struct osiop_softc *sc)
    904 {
    905 
    906 	osiop_write_1(sc, OSIOP_SIEN, 0);
    907 	osiop_write_1(sc, OSIOP_SCNTL1,
    908 	    osiop_read_1(sc, OSIOP_SCNTL1) | OSIOP_SCNTL1_RST);
    909 	delay(25);
    910 	osiop_write_1(sc, OSIOP_SCNTL1,
    911 	    osiop_read_1(sc, OSIOP_SCNTL1) & ~OSIOP_SCNTL1_RST);
    912 }
    913 
    914 /*
    915  * Setup Data Storage for 53C710 and start SCRIPTS processing
    916  */
    917 
    918 void
    919 osiop_start(struct osiop_softc *sc)
    920 {
    921 	struct osiop_acb *acb = sc->sc_nexus;
    922 	struct osiop_ds *ds = acb->ds;
    923 	struct scsipi_xfer *xs = acb->xs;
    924 	bus_dmamap_t dsdma = sc->sc_dsdma, datadma = acb->datadma;
    925 	struct osiop_tinfo *ti;
    926 	int target = xs->xs_periph->periph_target;
    927 	int lun = xs->xs_periph->periph_lun;
    928 	int disconnect, i;
    929 
    930 #ifdef OSIOP_DEBUG
    931 	if (osiop_debug & DEBUG_DISC &&
    932 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
    933 		printf("ACK! osiop was busy: script %p dsa %p active %d\n",
    934 		    sc->sc_script, acb->ds, sc->sc_active);
    935 		printf("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
    936 		    osiop_read_1(sc, OSIOP_ISTAT),
    937 		    osiop_read_1(sc, OSIOP_SFBR),
    938 		    osiop_read_1(sc, OSIOP_LCRC),
    939 		    osiop_read_1(sc, OSIOP_SIEN),
    940 		    osiop_read_1(sc, OSIOP_DIEN));
    941 #ifdef DDB
    942 #if 0
    943 		Debugger();
    944 #endif
    945 #endif
    946 	}
    947 #endif
    948 
    949 #ifdef OSIOP_DEBUG
    950 	if (acb->status != ACB_S_READY)
    951 		panic("osiop_start: non-ready cmd in acb");
    952 #endif
    953 
    954 	acb->intstat = 0;
    955 
    956 	ds->cmd.count = acb->cmdlen;
    957 	ds->cmd.addr = acb->cmddma->dm_segs[0].ds_addr;
    958 
    959 	ti = &sc->sc_tinfo[target];
    960 	ds->scsi_addr = ((1 << 16) << target) | (ti->sxfer << 8);
    961 
    962 	disconnect = (xs->xs_control & XS_CTL_REQSENSE) == 0 &&
    963 	    (ti->flags & TI_NODISC) == 0;
    964 
    965 	ds->msgout[0] = MSG_IDENTIFY(lun, disconnect);
    966 	ds->id.count = 1;
    967 	ds->stat[0] = SCSI_OSIOP_NOSTATUS;	/* set invalid status */
    968 	ds->msgbuf[0] = ds->msgbuf[1] = MSG_INVALID;
    969 	memset(&ds->data, 0, sizeof(ds->data));
    970 
    971 	/*
    972 	 * Negotiate wide is the initial negotiation state;  since the 53c710
    973 	 * doesn't do wide transfers, just begin the synchronous transfer
    974 	 * negotiation here.
    975 	 */
    976 	if (ti->state == NEG_INIT) {
    977 		if ((ti->flags & TI_NOSYNC) != 0) {
    978 			ti->state = NEG_DONE;
    979 			ti->sbcl = 0;
    980 			ti->sxfer = 0;
    981 			ti->period = 0;
    982 			ti->offset = 0;
    983 			osiop_update_xfer_mode(sc, target);
    984 #ifdef OSIOP_DEBUG
    985 			if (osiopsync_debug)
    986 				printf("Forcing target %d asynchronous\n",
    987 				    target);
    988 #endif
    989 		} else {
    990 			ds->msgbuf[2] = MSG_INVALID;
    991 			ds->msgout[1] = MSG_EXTENDED;
    992 			ds->msgout[2] = MSG_EXT_SDTR_LEN;
    993 			ds->msgout[3] = MSG_EXT_SDTR;
    994 			ds->msgout[4] = sc->sc_minsync;
    995 			ds->msgout[5] = OSIOP_MAX_OFFSET;
    996 			ds->id.count = MSG_EXT_SDTR_LEN + 3;
    997 			ti->state = NEG_WAITS;
    998 #ifdef OSIOP_DEBUG
    999 			if (osiopsync_debug)
   1000 				printf("Sending sync request to target %d\n",
   1001 				    target);
   1002 #endif
   1003 		}
   1004 	}
   1005 
   1006 	acb->curaddr = 0;
   1007 	acb->curlen = 0;
   1008 
   1009 	/*
   1010 	 * Build physical DMA addresses for scatter/gather I/O
   1011 	 */
   1012 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1013 		for (i = 0; i < datadma->dm_nsegs; i++) {
   1014 			ds->data[i].count = datadma->dm_segs[i].ds_len;
   1015 			ds->data[i].addr  = datadma->dm_segs[i].ds_addr;
   1016 		}
   1017 	}
   1018 
   1019 	/* sync script data structure */
   1020 	bus_dmamap_sync(sc->sc_dmat, dsdma,
   1021 	    acb->dsoffset, sizeof(struct osiop_ds),
   1022 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1023 
   1024 	acb->status = ACB_S_ACTIVE;
   1025 
   1026 	/* handle timeout */
   1027 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
   1028 		int timeout = mstohz(acb->xs->timeout);
   1029 		/* start expire timer */
   1030 		if (timeout == 0)
   1031 			timeout = 1;
   1032 		callout_reset(&xs->xs_callout, timeout,
   1033 		    osiop_timeout, acb);
   1034 	}
   1035 #ifdef OSIOP_DEBUG
   1036 	if (osiop_debug & DEBUG_DISC &&
   1037 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1038 		printf("ACK! osiop was busy at start: "
   1039 		    "script %p dsa %p active %d\n",
   1040 		    sc->sc_script, acb->ds, sc->sc_active);
   1041 #ifdef DDB
   1042 #if 0
   1043 		Debugger();
   1044 #endif
   1045 #endif
   1046 	}
   1047 #endif
   1048 	if (TAILQ_EMPTY(&sc->nexus_list)) {
   1049 		if (osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON)
   1050 			printf("%s: osiop_select while connected?\n",
   1051 			    sc->sc_dev.dv_xname);
   1052 		osiop_write_4(sc, OSIOP_TEMP, 0);
   1053 		osiop_write_1(sc, OSIOP_SBCL, ti->sbcl);
   1054 		osiop_write_4(sc, OSIOP_DSA,
   1055 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1056 		osiop_write_4(sc, OSIOP_DSP,
   1057 		    sc->sc_scrdma->dm_segs[0].ds_addr + Ent_scripts);
   1058 		OSIOP_TRACE('s', 1, 0, 0);
   1059 	} else {
   1060 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
   1061 			osiop_write_1(sc, OSIOP_ISTAT, OSIOP_ISTAT_SIGP);
   1062 			OSIOP_TRACE('s', 2, 0, 0);
   1063 		} else {
   1064 			OSIOP_TRACE('s', 3,
   1065 			    osiop_read_1(sc, OSIOP_ISTAT), 0);
   1066 		}
   1067 	}
   1068 #ifdef OSIOP_DEBUG
   1069 	osiopstarts++;
   1070 #endif
   1071 }
   1072 
   1073 /*
   1074  * Process a DMA or SCSI interrupt from the 53C710 SIOP
   1075  */
   1076 
   1077 int
   1078 osiop_checkintr(struct osiop_softc *sc, uint8_t istat, uint8_t dstat,
   1079     uint8_t sstat0, int *status)
   1080 {
   1081 	struct osiop_acb *acb = sc->sc_nexus;
   1082 	struct osiop_ds *ds = NULL;	/* XXX */
   1083 	bus_dmamap_t dsdma = sc->sc_dsdma;
   1084 	bus_addr_t scraddr = sc->sc_scrdma->dm_segs[0].ds_addr;
   1085 	int target = 0;
   1086 	int dfifo, dbc, intcode, sstat1;
   1087 
   1088 	dfifo = osiop_read_1(sc, OSIOP_DFIFO);
   1089 	dbc = osiop_read_4(sc, OSIOP_DBC) & 0x00ffffff;
   1090 	sstat1 = osiop_read_1(sc, OSIOP_SSTAT1);
   1091 	osiop_write_1(sc, OSIOP_CTEST8,
   1092 	    osiop_read_1(sc, OSIOP_CTEST8) | OSIOP_CTEST8_CLF);
   1093 	while ((osiop_read_1(sc, OSIOP_CTEST1) & OSIOP_CTEST1_FMT) !=
   1094 	    OSIOP_CTEST1_FMT)
   1095 		;
   1096 	osiop_write_1(sc, OSIOP_CTEST8,
   1097 	    osiop_read_1(sc, OSIOP_CTEST8) & ~OSIOP_CTEST8_CLF);
   1098 	intcode = osiop_read_4(sc, OSIOP_DSPS);
   1099 #ifdef OSIOP_DEBUG
   1100 	osiopints++;
   1101 	if (osiop_read_4(sc, OSIOP_DSP) != 0 &&
   1102 	    (osiop_read_4(sc, OSIOP_DSP) < scraddr ||
   1103 	    osiop_read_4(sc, OSIOP_DSP) >= scraddr + sizeof(osiop_script))) {
   1104 		printf("%s: dsp not within script dsp %x scripts %lx:%lx",
   1105 		    sc->sc_dev.dv_xname,
   1106 		    osiop_read_4(sc, OSIOP_DSP),
   1107 		    scraddr, scraddr + sizeof(osiop_script));
   1108 		printf(" istat %x dstat %x sstat0 %x\n", istat, dstat, sstat0);
   1109 #ifdef DDB
   1110 		Debugger();
   1111 #endif
   1112 	}
   1113 #endif
   1114 	OSIOP_TRACE('i', dstat, istat, (istat & OSIOP_ISTAT_DIP) ?
   1115 	    intcode & 0xff : sstat0);
   1116 
   1117 	if (acb != NULL) { /* XXX */
   1118 		ds = acb->ds;
   1119 		bus_dmamap_sync(sc->sc_dmat, dsdma,
   1120 		    acb->dsoffset, sizeof(struct osiop_ds),
   1121 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1122 #ifdef OSIOP_DEBUG
   1123 		if (acb->status != ACB_S_ACTIVE)
   1124 			printf("osiop_checkintr: acb not active (status %d)\n",
   1125 			    acb->status);
   1126 #endif
   1127 	}
   1128 
   1129 
   1130 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_ok) {
   1131 		/* Normal completion status, or check condition */
   1132 		struct osiop_tinfo *ti;
   1133 
   1134 		if (acb == NULL) {
   1135 			printf("%s: COMPLETE with no active command?\n",
   1136 			    sc->sc_dev.dv_xname);
   1137 			goto bad_phase;
   1138 		}
   1139 #ifdef OSIOP_DEBUG
   1140 		if (osiop_read_4(sc, OSIOP_DSA) !=
   1141 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset) {
   1142 			printf("osiop: invalid dsa: %x %lx\n",
   1143 			    osiop_read_4(sc, OSIOP_DSA),
   1144 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1145 			panic("*** osiop DSA invalid ***");
   1146 		}
   1147 #endif
   1148 		target = acb->xs->xs_periph->periph_target;
   1149 		ti = &sc->sc_tinfo[target];
   1150 		if (ti->state == NEG_WAITS) {
   1151 			if (ds->msgbuf[1] == MSG_INVALID)
   1152 				printf("%s: target %d ignored sync request\n",
   1153 				    sc->sc_dev.dv_xname, target);
   1154 			else if (ds->msgbuf[1] == MSG_MESSAGE_REJECT)
   1155 				printf("%s: target %d rejected sync request\n",
   1156 				    sc->sc_dev.dv_xname, target);
   1157 			ti->period = 0;
   1158 			ti->offset = 0;
   1159 			osiop_update_xfer_mode(sc, target);
   1160 			ti->state = NEG_DONE;
   1161 		}
   1162 #ifdef OSIOP_DEBUG
   1163 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1164 #if 0
   1165 			printf("ACK! osiop was busy at end: "
   1166 			    "script %p dsa %p\n", &osiop_script, ds);
   1167 #ifdef DDB
   1168 			Debugger();
   1169 #endif
   1170 #endif
   1171 		}
   1172 		if (ds->msgbuf[0] != MSG_CMDCOMPLETE)
   1173 			printf("%s: message was not COMMAND COMPLETE: %02x\n",
   1174 			    sc->sc_dev.dv_xname, ds->msgbuf[0]);
   1175 #endif
   1176 		if (!TAILQ_EMPTY(&sc->nexus_list))
   1177 			osiop_write_1(sc, OSIOP_DCNTL,
   1178 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1179 		*status = ds->stat[0];
   1180 		acb->status = ACB_S_DONE;
   1181 		return (1);
   1182 	}
   1183 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_syncmsg) {
   1184 		if (acb == NULL) {
   1185 			printf("%s: sync message with no active command?\n",
   1186 			    sc->sc_dev.dv_xname);
   1187 			goto bad_phase;
   1188 		}
   1189 		target = acb->xs->xs_periph->periph_target;
   1190 		if (ds->msgbuf[1] == MSG_EXTENDED &&
   1191 		    ds->msgbuf[2] == MSG_EXT_SDTR_LEN &&
   1192 		    ds->msgbuf[3] == MSG_EXT_SDTR) {
   1193 			struct osiop_tinfo *ti = &sc->sc_tinfo[target];
   1194 #ifdef OSIOP_DEBUG
   1195 			if (osiopsync_debug)
   1196 				printf("sync msg in: "
   1197 				    "%02x %02x %02x %02x %02x %02x\n",
   1198 				    ds->msgbuf[0], ds->msgbuf[1],
   1199 				    ds->msgbuf[2], ds->msgbuf[3],
   1200 				    ds->msgbuf[4], ds->msgbuf[5]);
   1201 #endif
   1202 			ti->period = ds->msgbuf[4];
   1203 			ti->offset = ds->msgbuf[5];
   1204 			ti->sxfer = 0;
   1205 			ti->sbcl = 0;
   1206 			if (ds->msgbuf[5] != 0)
   1207 				scsi_period_to_osiop(sc, target);
   1208 			osiop_update_xfer_mode(sc, target);
   1209 
   1210 			bus_dmamap_sync(sc->sc_dmat, dsdma,
   1211 			    acb->dsoffset, sizeof(struct osiop_ds),
   1212 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1213 			osiop_write_1(sc, OSIOP_SXFER, ti->sxfer);
   1214 			osiop_write_1(sc, OSIOP_SBCL, ti->sbcl);
   1215 			if (ti->state == NEG_WAITS) {
   1216 				ti->state = NEG_DONE;
   1217 				osiop_write_4(sc, OSIOP_DSP,
   1218 				    scraddr + Ent_clear_ack);
   1219 				return (0);
   1220 			}
   1221 			osiop_write_1(sc, OSIOP_DCNTL,
   1222 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1223 			ti->state = NEG_DONE;
   1224 			return (0);
   1225 		}
   1226 		/* XXX - not SDTR message */
   1227 	}
   1228 	if (sstat0 & OSIOP_SSTAT0_M_A) {
   1229 		/* Phase mismatch */
   1230 #ifdef OSIOP_DEBUG
   1231 		osiopphmm++;
   1232 #endif
   1233 		if (acb == NULL) {
   1234 			printf("%s: Phase mismatch with no active command?\n",
   1235 			    sc->sc_dev.dv_xname);
   1236 			goto bad_phase;
   1237 		}
   1238 		if (acb->datalen > 0) {
   1239 			int adjust = (dfifo - (dbc & 0x7f)) & 0x7f;
   1240 			if (sstat1 & OSIOP_SSTAT1_ORF)
   1241 				adjust++;
   1242 			if (sstat1 & OSIOP_SSTAT1_OLF)
   1243 				adjust++;
   1244 			acb->curaddr = osiop_read_4(sc, OSIOP_DNAD) - adjust;
   1245 			acb->curlen = dbc + adjust;
   1246 #ifdef OSIOP_DEBUG
   1247 			if (osiop_debug & DEBUG_DISC) {
   1248 				printf("Phase mismatch: curaddr %lx "
   1249 				    "curlen %lx dfifo %x dbc %x sstat1 %x "
   1250 				    "adjust %x sbcl %x starts %d acb %p\n",
   1251 				    acb->curaddr, acb->curlen, dfifo,
   1252 				    dbc, sstat1, adjust,
   1253 				    osiop_read_1(sc, OSIOP_SBCL),
   1254 				    osiopstarts, acb);
   1255 				if (ds->data[1].count != 0) {
   1256 					int i;
   1257 					for (i = 0; ds->data[i].count != 0; i++)
   1258 						printf("chain[%d] "
   1259 						    "addr %x len %x\n", i,
   1260 						    ds->data[i].addr,
   1261 						    ds->data[i].count);
   1262 				}
   1263 				bus_dmamap_sync(sc->sc_dmat, dsdma,
   1264 				    acb->dsoffset, sizeof(struct osiop_ds),
   1265 				    BUS_DMASYNC_PREREAD |
   1266 				    BUS_DMASYNC_PREWRITE);
   1267 			}
   1268 #endif
   1269 		}
   1270 #ifdef OSIOP_DEBUG
   1271 		OSIOP_TRACE('m', osiop_read_1(sc, OSIOP_SBCL),
   1272 		    osiop_read_4(sc, OSIOP_DSP) >> 8,
   1273 		    osiop_read_4(sc, OSIOP_DSP));
   1274 		if (osiop_debug & DEBUG_PHASE)
   1275 			printf("Phase mismatch: %x dsp +%lx dcmd %x\n",
   1276 			    osiop_read_1(sc, OSIOP_SBCL),
   1277 			    osiop_read_4(sc, OSIOP_DSP) - scraddr,
   1278 			    osiop_read_4(sc, OSIOP_DBC));
   1279 #endif
   1280 		if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_REQ) == 0) {
   1281 			printf("Phase mismatch: "
   1282 			    "REQ not asserted! %02x dsp %x\n",
   1283 			    osiop_read_1(sc, OSIOP_SBCL),
   1284 			    osiop_read_4(sc, OSIOP_DSP));
   1285 #if defined(OSIOP_DEBUG) && defined(DDB)
   1286 			/*Debugger(); XXX is*/
   1287 #endif
   1288 		}
   1289 		switch (OSIOP_PHASE(osiop_read_1(sc, OSIOP_SBCL))) {
   1290 		case DATA_OUT_PHASE:
   1291 		case DATA_IN_PHASE:
   1292 		case STATUS_PHASE:
   1293 		case COMMAND_PHASE:
   1294 		case MSG_IN_PHASE:
   1295 		case MSG_OUT_PHASE:
   1296 			osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
   1297 			break;
   1298 		default:
   1299 			printf("%s: invalid phase\n", sc->sc_dev.dv_xname);
   1300 			goto bad_phase;
   1301 		}
   1302 		return (0);
   1303 	}
   1304 	if (sstat0 & OSIOP_SSTAT0_STO) {
   1305 		/* Select timed out */
   1306 		if (acb == NULL) {
   1307 			printf("%s: Select timeout with no active command?\n",
   1308 			    sc->sc_dev.dv_xname);
   1309 			goto bad_phase;
   1310 		}
   1311 #ifdef OSIOP_DEBUG
   1312 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1313 			printf("ACK! osiop was busy at timeout: "
   1314 			    "script %p dsa %lx\n", sc->sc_script,
   1315 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1316 			printf(" sbcl %x sdid %x "
   1317 			    "istat %x dstat %x sstat0 %x\n",
   1318 			    osiop_read_1(sc, OSIOP_SBCL),
   1319 			    osiop_read_1(sc, OSIOP_SDID),
   1320 			    istat, dstat, sstat0);
   1321 			if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) == 0) {
   1322 				printf("Yikes, it's not busy now!\n");
   1323 #if 0
   1324 				*status = SCSI_OSIOP_NOSTATUS;
   1325 				if (!TAILQ_EMPTY(&sc->nexus_list))
   1326 					osiop_write_4(sc, OSIOP_DSP,
   1327 					    scraddr + Ent_wait_reselect);
   1328 				return (1);
   1329 #endif
   1330 			}
   1331 #if 0
   1332 			osiop_write_1(sc, OSIOP_DCNTL,
   1333 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1334 #endif
   1335 #ifdef DDB
   1336 			Debugger();
   1337 #endif
   1338 			return (0);
   1339 		}
   1340 #endif
   1341 		acb->status = ACB_S_DONE;
   1342 		*status = SCSI_OSIOP_NOSTATUS;
   1343 		acb->xs->error = XS_SELTIMEOUT;
   1344 		if (!TAILQ_EMPTY(&sc->nexus_list))
   1345 			osiop_write_4(sc, OSIOP_DSP,
   1346 			    scraddr + Ent_wait_reselect);
   1347 		return (1);
   1348 	}
   1349 	if (acb != NULL)
   1350 		target = acb->xs->xs_periph->periph_target;
   1351 	else
   1352 		target = sc->sc_id;
   1353 	if (sstat0 & OSIOP_SSTAT0_UDC) {
   1354 		printf("%s: target %d disconnected unexpectedly",
   1355 		    sc->sc_dev.dv_xname, target);
   1356 		if (acb == NULL)
   1357 			printf("with no active command?");
   1358 		printf("\n");
   1359 #if 0
   1360 		osiop_abort(sc, "osiop_chkintr");
   1361 #endif
   1362 		*status = SCSI_CHECK;
   1363 		if (!TAILQ_EMPTY(&sc->nexus_list))
   1364 			osiop_write_4(sc, OSIOP_DSP,
   1365 			    scraddr + Ent_wait_reselect);
   1366 		return (acb != NULL);
   1367 	}
   1368 	if (dstat & OSIOP_DSTAT_SIR &&
   1369 	    (intcode == A_int_disc || intcode == A_int_disc_wodp)) {
   1370 		/* Disconnect */
   1371 		if (acb == NULL) {
   1372 			printf("%s: Disconnect with no active command?\n",
   1373 			    sc->sc_dev.dv_xname);
   1374 			return (0);
   1375 		}
   1376 #ifdef OSIOP_DEBUG
   1377 		if (osiop_debug & DEBUG_DISC) {
   1378 			printf("%s: ID %02x disconnected TEMP %x (+%lx) "
   1379 			    "curaddr %lx curlen %lx buf %x len %x dfifo %x "
   1380 			    "dbc %x sstat1 %x starts %d acb %p\n",
   1381 			    sc->sc_dev.dv_xname, 1 << target,
   1382 			    osiop_read_4(sc, OSIOP_TEMP),
   1383 			    (osiop_read_4(sc, OSIOP_TEMP) != 0) ?
   1384 			        osiop_read_4(sc, OSIOP_TEMP) - scraddr : 0,
   1385 			    acb->curaddr, acb->curlen,
   1386 			    ds->data[0].addr, ds->data[0].count,
   1387 			    dfifo, dbc, sstat1, osiopstarts, acb);
   1388 			bus_dmamap_sync(sc->sc_dmat, dsdma,
   1389 			    acb->dsoffset, sizeof(struct osiop_ds),
   1390 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1391 		}
   1392 #endif
   1393 		/*
   1394 		 * XXXX need to update curaddr/curlen to reflect
   1395 		 * current data transferred.  If device disconnected in
   1396 		 * the middle of a DMA block, they should already be set
   1397 		 * by the phase change interrupt.  If the disconnect
   1398 		 * occurs on a DMA block boundary, we have to figure out
   1399 		 * which DMA block it was.
   1400 		 */
   1401 		if (acb->datalen > 0 &&
   1402 		    osiop_read_4(sc, OSIOP_TEMP) != 0) {
   1403 			long n = osiop_read_4(sc, OSIOP_TEMP) - scraddr;
   1404 
   1405 			if (acb->curlen != 0 &&
   1406 			    acb->curlen != ds->data[0].count)
   1407 				printf("%s: curaddr/curlen already set? "
   1408 				    "n %lx iob %lx/%lx chain[0] %x/%x\n",
   1409 				    sc->sc_dev.dv_xname, n,
   1410 				    acb->curaddr, acb->curlen,
   1411 				    ds->data[0].addr, ds->data[0].count);
   1412 			if (n < Ent_datain)
   1413 				n = (n - Ent_dataout) / 16;
   1414 			else
   1415 				n = (n - Ent_datain) / 16;
   1416 			if (n < 0 || n >= OSIOP_NSG)
   1417 				printf("TEMP invalid %ld\n", n);
   1418 			else {
   1419 				acb->curaddr = ds->data[n].addr;
   1420 				acb->curlen = ds->data[n].count;
   1421 			}
   1422 #ifdef OSIOP_DEBUG
   1423 			if (osiop_debug & DEBUG_DISC) {
   1424 				printf("%s: TEMP offset %ld",
   1425 				    sc->sc_dev.dv_xname, n);
   1426 				printf(" curaddr %lx curlen %lx\n",
   1427 				    acb->curaddr, acb->curlen);
   1428 			}
   1429 #endif
   1430 		}
   1431 		/*
   1432 		 * If data transfer was interrupted by disconnect, curaddr
   1433 		 * and curlen should reflect the point of interruption.
   1434 		 * Adjust the DMA chain so that the data transfer begins
   1435 		 * at the appropriate place upon reselection.
   1436 		 * XXX This should only be done on save data pointer message?
   1437 		 */
   1438 		if (acb->curlen > 0) {
   1439 			int i, j;
   1440 
   1441 #ifdef OSIOP_DEBUG
   1442 			if (osiop_debug & DEBUG_DISC)
   1443 				printf("%s: adjusting DMA chain\n",
   1444 				    sc->sc_dev.dv_xname);
   1445 			if (intcode == A_int_disc_wodp)
   1446 				printf("%s: ID %02x disconnected "
   1447 				    "without Save Data Pointers\n",
   1448 				    sc->sc_dev.dv_xname, 1 << target);
   1449 #endif
   1450 			for (i = 0; i < OSIOP_NSG; i++) {
   1451 				if (ds->data[i].count == 0)
   1452 					break;
   1453 				if (acb->curaddr >= ds->data[i].addr &&
   1454 				    acb->curaddr <
   1455 				    (ds->data[i].addr + ds->data[i].count))
   1456 					break;
   1457 			}
   1458 			if (i >= OSIOP_NSG || ds->data[i].count == 0) {
   1459 				printf("couldn't find saved data pointer: "
   1460 				    "curaddr %lx curlen %lx i %d\n",
   1461 				    acb->curaddr, acb->curlen, i);
   1462 #ifdef DDB
   1463 				Debugger();
   1464 #endif
   1465 			}
   1466 #ifdef OSIOP_DEBUG
   1467 			if (osiop_debug & DEBUG_DISC)
   1468 				printf(" chain[0]: %x/%x -> %lx/%lx\n",
   1469 				    ds->data[0].addr, ds->data[0].count,
   1470 				    acb->curaddr, acb->curlen);
   1471 #endif
   1472 			ds->data[0].addr = acb->curaddr;
   1473 			ds->data[0].count = acb->curlen;
   1474 			for (j = 1, i = i + 1;
   1475 			    i < OSIOP_NSG && ds->data[i].count > 0;
   1476 			    i++, j++) {
   1477 #ifdef OSIOP_DEBUG
   1478 				if (osiop_debug & DEBUG_DISC)
   1479 					printf("  chain[%d]: %x/%x -> %x/%x\n",
   1480 					    j, ds->data[j].addr,
   1481 					    ds->data[j].count,
   1482 					    ds->data[i].addr,
   1483 					    ds->data[i].count);
   1484 #endif
   1485 				ds->data[j].addr  = ds->data[i].addr;
   1486 				ds->data[j].count = ds->data[i].count;
   1487 			}
   1488 			if (j < OSIOP_NSG) {
   1489 				ds->data[j].addr  = 0;
   1490 				ds->data[j].count = 0;
   1491 			}
   1492 			bus_dmamap_sync(sc->sc_dmat, dsdma,
   1493 			    acb->dsoffset, sizeof(struct osiop_ds),
   1494 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1495 		}
   1496 		sc->sc_tinfo[target].dconns++;
   1497 		/*
   1498 		 * add nexus to waiting list
   1499 		 * clear nexus
   1500 		 * try to start another command for another target/lun
   1501 		 */
   1502 		acb->intstat = sc->sc_flags & OSIOP_INTSOFF;
   1503 		TAILQ_INSERT_TAIL(&sc->nexus_list, acb, chain);
   1504 		sc->sc_nexus = NULL;		/* no current device */
   1505 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_wait_reselect);
   1506 		/* XXXX start another command ? */
   1507 		if (!TAILQ_EMPTY(&sc->ready_list))
   1508 			osiop_sched(sc);
   1509 		return (0);
   1510 	}
   1511 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_reconnect) {
   1512 		int reselid = ffs(osiop_read_4(sc, OSIOP_SCRATCH) & 0xff) - 1;
   1513 		int reselun = osiop_read_1(sc, OSIOP_SFBR) & 0x07;
   1514 #ifdef OSIOP_DEBUG
   1515 		uint8_t resmsg;
   1516 #endif
   1517 
   1518 		/* Reconnect */
   1519 		/* XXXX save current SBCL */
   1520 		sc->sc_sstat1 = osiop_read_1(sc, OSIOP_SBCL);
   1521 #ifdef OSIOP_DEBUG
   1522 		if (osiop_debug & DEBUG_DISC)
   1523 			printf("%s: target ID %02x reselected dsps %x\n",
   1524 			    sc->sc_dev.dv_xname, reselid, intcode);
   1525 		resmsg = osiop_read_1(sc, OSIOP_SFBR);
   1526 		if (!MSG_ISIDENTIFY(resmsg))
   1527 			printf("%s: Reselect message in was not identify: "
   1528 			    "%02x\n", sc->sc_dev.dv_xname, resmsg);
   1529 #endif
   1530 		if (sc->sc_nexus != NULL) {
   1531 			struct scsipi_periph *periph =
   1532 			    sc->sc_nexus->xs->xs_periph;
   1533 #ifdef OSIOP_DEBUG
   1534 			if (osiop_debug & DEBUG_DISC)
   1535 				printf("%s: reselect ID %02x w/active\n",
   1536 				    sc->sc_dev.dv_xname, reselid);
   1537 #endif
   1538 			TAILQ_INSERT_HEAD(&sc->ready_list,
   1539 			    sc->sc_nexus, chain);
   1540 			sc->sc_tinfo[periph->periph_target].lubusy
   1541 			    &= ~(1 << periph->periph_lun);
   1542 			sc->sc_active--;
   1543 		}
   1544 		/*
   1545 		 * locate acb of reselecting device
   1546 		 * set sc->sc_nexus to acb
   1547 		 */
   1548 		TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
   1549 			struct scsipi_periph *periph = acb->xs->xs_periph;
   1550 			if (reselid != periph->periph_target ||
   1551 			    reselun != periph->periph_lun) {
   1552 				continue;
   1553 			}
   1554 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1555 			sc->sc_nexus = acb;
   1556 			sc->sc_flags |= acb->intstat;
   1557 			acb->intstat = 0;
   1558 			osiop_write_4(sc, OSIOP_DSA,
   1559 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1560 			osiop_write_1(sc, OSIOP_SXFER,
   1561 			    sc->sc_tinfo[reselid].sxfer);
   1562 			osiop_write_1(sc, OSIOP_SBCL,
   1563 			    sc->sc_tinfo[reselid].sbcl);
   1564 			break;
   1565 		}
   1566 		if (acb == NULL) {
   1567 			printf("%s: target ID %02x reselect nexus_list %p\n",
   1568 			    sc->sc_dev.dv_xname, reselid,
   1569 			    TAILQ_FIRST(&sc->nexus_list));
   1570 			panic("unable to find reselecting device");
   1571 		}
   1572 
   1573 		osiop_write_4(sc, OSIOP_TEMP, 0);
   1574 		osiop_write_1(sc, OSIOP_DCNTL,
   1575 		    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1576 		return (0);
   1577 	}
   1578 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_connect) {
   1579 #ifdef OSIOP_DEBUG
   1580 		uint8_t ctest2 = osiop_read_1(sc, OSIOP_CTEST2);
   1581 
   1582 		/* reselect was interrupted (by Sig_P or select) */
   1583 		if (osiop_debug & DEBUG_DISC ||
   1584 		    (ctest2 & OSIOP_CTEST2_SIGP) == 0)
   1585 			printf("%s: reselect interrupted (Sig_P?) "
   1586 			    "scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
   1587 			    sc->sc_dev.dv_xname,
   1588 			    osiop_read_1(sc, OSIOP_SCNTL1), ctest2,
   1589 			    osiop_read_1(sc, OSIOP_SFBR), istat,
   1590 			    osiop_read_1(sc, OSIOP_ISTAT));
   1591 #endif
   1592 		/* XXX assumes it was not select */
   1593 		if (sc->sc_nexus == NULL) {
   1594 #ifdef OSIOP_DEBUG
   1595 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
   1596 			    sc->sc_dev.dv_xname);
   1597 #if 0
   1598 			osiop_dump(sc);
   1599 #ifdef DDB
   1600 			Debugger();
   1601 #endif
   1602 #endif
   1603 #endif
   1604 			osiop_write_1(sc, OSIOP_DCNTL,
   1605 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1606 			return (0);
   1607 		}
   1608 		target = sc->sc_nexus->xs->xs_periph->periph_target;
   1609 		osiop_write_4(sc, OSIOP_TEMP, 0);
   1610 		osiop_write_4(sc, OSIOP_DSA,
   1611 		    dsdma->dm_segs[0].ds_addr + sc->sc_nexus->dsoffset);
   1612 		osiop_write_1(sc, OSIOP_SXFER, sc->sc_tinfo[target].sxfer);
   1613 		osiop_write_1(sc, OSIOP_SBCL, sc->sc_tinfo[target].sbcl);
   1614 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_scripts);
   1615 		return (0);
   1616 	}
   1617 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_msgin) {
   1618 		/* Unrecognized message in byte */
   1619 		if (acb == NULL) {
   1620 			printf("%s: Bad message-in with no active command?\n",
   1621 			    sc->sc_dev.dv_xname);
   1622 			goto bad_phase;
   1623 		}
   1624 		printf("%s: Unrecognized message in data "
   1625 		    "sfbr %x msg %x sbcl %x\n", sc->sc_dev.dv_xname,
   1626 		    osiop_read_1(sc, OSIOP_SFBR), ds->msgbuf[1],
   1627 		    osiop_read_1(sc, OSIOP_SBCL));
   1628 		/* what should be done here? */
   1629 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
   1630 		bus_dmamap_sync(sc->sc_dmat, dsdma,
   1631 		    acb->dsoffset, sizeof(struct osiop_ds),
   1632 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1633 		return (0);
   1634 	}
   1635 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_status) {
   1636 		/* Status phase wasn't followed by message in phase? */
   1637 		printf("%s: Status phase not followed by message in phase? "
   1638 		    "sbcl %x sbdl %x\n", sc->sc_dev.dv_xname,
   1639 		    osiop_read_1(sc, OSIOP_SBCL),
   1640 		    osiop_read_1(sc, OSIOP_SBDL));
   1641 		if (osiop_read_1(sc, OSIOP_SBCL) == 0xa7) {
   1642 			/* It is now, just continue the script? */
   1643 			osiop_write_1(sc, OSIOP_DCNTL,
   1644 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1645 			return (0);
   1646 		}
   1647 	}
   1648 	if (dstat & OSIOP_DSTAT_SIR && sstat0 == 0) {
   1649 		printf("OSIOP interrupt: %x sts %x msg %x %x sbcl %x\n",
   1650 		    intcode, ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
   1651 		    osiop_read_1(sc, OSIOP_SBCL));
   1652 		osiop_reset(sc);
   1653 		*status = SCSI_OSIOP_NOSTATUS;
   1654 		return (0);	/* osiop_reset has cleaned up */
   1655 	}
   1656 	if (sstat0 & OSIOP_SSTAT0_SGE)
   1657 		printf("%s: SCSI Gross Error\n", sc->sc_dev.dv_xname);
   1658 	if (sstat0 & OSIOP_SSTAT0_PAR)
   1659 		printf("%s: Parity Error\n", sc->sc_dev.dv_xname);
   1660 	if (dstat & OSIOP_DSTAT_IID)
   1661 		printf("%s: Invalid instruction detected\n",
   1662 		    sc->sc_dev.dv_xname);
   1663  bad_phase:
   1664 	/*
   1665 	 * temporary panic for unhandled conditions
   1666 	 * displays various things about the 53C710 status and registers
   1667 	 * then panics.
   1668 	 * XXXX need to clean this up to print out the info, reset, and continue
   1669 	 */
   1670 	printf("osiop_chkintr: target %x ds %p\n", target, ds);
   1671 	printf("scripts %lx ds %lx dsp %x dcmd %x\n", scraddr,
   1672 	    sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset,
   1673 	    osiop_read_4(sc, OSIOP_DSP),
   1674 	    osiop_read_4(sc, OSIOP_DBC));
   1675 	printf("osiop_chkintr: istat %x dstat %x sstat0 %x "
   1676 	    "dsps %x dsa %x sbcl %x sts %x msg %x %x sfbr %x\n",
   1677 	    istat, dstat, sstat0, intcode,
   1678 	    osiop_read_4(sc, OSIOP_DSA),
   1679 	    osiop_read_1(sc, OSIOP_SBCL),
   1680 	    ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
   1681 	    osiop_read_1(sc, OSIOP_SFBR));
   1682 #ifdef OSIOP_DEBUG
   1683 	if (osiop_debug & DEBUG_DMA)
   1684 		panic("osiop_chkintr: **** temp ****");
   1685 #endif
   1686 #ifdef DDB
   1687 	Debugger();
   1688 #endif
   1689 	osiop_reset(sc);	/* hard reset */
   1690 	*status = SCSI_OSIOP_NOSTATUS;
   1691 	acb->status = ACB_S_DONE;
   1692 	return (0);		/* osiop_reset cleaned up */
   1693 }
   1694 
   1695 void
   1696 osiop_select(struct osiop_softc *sc)
   1697 {
   1698 	struct osiop_acb *acb = sc->sc_nexus;
   1699 
   1700 #ifdef OSIOP_DEBUG
   1701 	if (osiop_debug & DEBUG_CMD)
   1702 		printf("%s: select ", sc->sc_dev.dv_xname);
   1703 #endif
   1704 
   1705 	if (acb->xs->xs_control & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA) {
   1706 		sc->sc_flags |= OSIOP_INTSOFF;
   1707 		sc->sc_flags &= ~OSIOP_INTDEFER;
   1708 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
   1709 			osiop_write_1(sc, OSIOP_SIEN, 0);
   1710 			osiop_write_1(sc, OSIOP_DIEN, 0);
   1711 		}
   1712 #if 0
   1713 	} else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
   1714 		sc->sc_flags &= ~OSIOP_INTSOFF;
   1715 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
   1716 			osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
   1717 			osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
   1718 		}
   1719 #endif
   1720 	}
   1721 #ifdef OSIOP_DEBUG
   1722 	if (osiop_debug & DEBUG_CMD)
   1723 		printf("osiop_select: target %x cmd %02x ds %p\n",
   1724 		    acb->xs->xs_periph->periph_target,
   1725 		    acb->xs->cmd->opcode, sc->sc_nexus->ds);
   1726 #endif
   1727 
   1728 	osiop_start(sc);
   1729 
   1730 	return;
   1731 }
   1732 
   1733 /*
   1734  * 53C710 interrupt handler
   1735  */
   1736 
   1737 void
   1738 osiop_intr(struct osiop_softc *sc)
   1739 {
   1740 	int status, s;
   1741 	uint8_t istat, dstat, sstat0;
   1742 
   1743 	s = splbio();
   1744 
   1745 	istat = sc->sc_istat;
   1746 	if ((istat & (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
   1747 		splx(s);
   1748 		return;
   1749 	}
   1750 
   1751 	/* Got a valid interrupt on this device; set by MD handler */
   1752 	dstat = sc->sc_dstat;
   1753 	sstat0 = sc->sc_sstat0;
   1754 	sc->sc_istat = 0;
   1755 #ifdef OSIOP_DEBUG
   1756 	if (!sc->sc_active) {
   1757 		/* XXX needs sync */
   1758 		printf("%s: spurious interrupt? "
   1759 		    "istat %x dstat %x sstat0 %x nexus %p status %x\n",
   1760 		    sc->sc_dev.dv_xname, istat, dstat, sstat0, sc->sc_nexus,
   1761 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0);
   1762 	}
   1763 #endif
   1764 
   1765 #ifdef OSIOP_DEBUG
   1766 	if (osiop_debug & (DEBUG_INT|DEBUG_CMD)) {
   1767 		/* XXX needs sync */
   1768 		printf("%s: intr istat %x dstat %x sstat0 %x dsps %x "
   1769 		    "sbcl %x dsp %x dcmd %x sts %x msg %x\n",
   1770 		    sc->sc_dev.dv_xname,
   1771 		    istat, dstat, sstat0,
   1772 		    osiop_read_4(sc, OSIOP_DSPS),
   1773 		    osiop_read_1(sc, OSIOP_SBCL),
   1774 		    osiop_read_4(sc, OSIOP_DSP),
   1775 		    osiop_read_4(sc, OSIOP_DBC),
   1776 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0,
   1777 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->msgbuf[0] : 0);
   1778 	}
   1779 #endif
   1780 	if (sc->sc_flags & OSIOP_INTDEFER) {
   1781 		sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
   1782 		osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
   1783 		osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
   1784 	}
   1785 	if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
   1786 #if 0
   1787 		if (status == SCSI_OSIOP_NOSTATUS)
   1788 			printf("osiop_intr: no valid status \n");
   1789 #endif
   1790 		if ((sc->sc_flags & (OSIOP_INTSOFF | OSIOP_INTDEFER)) !=
   1791 		    OSIOP_INTSOFF) {
   1792 #if 0
   1793 			if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1794 				struct scsipi_periph *periph;
   1795 
   1796 				periph = sc->sc_nexus->xs->xs_periph;
   1797 				printf("%s: SCSI bus busy at completion"
   1798 				    " targ %d sbcl %02x sfbr %x lcrc "
   1799 				    "%02x dsp +%x\n", sc->sc_dev.dv_xname,
   1800 				    periph->periphtarget,
   1801 				    osiop_read_1(sc, OSIOP_SBCL),
   1802 				    osiop_read_1(sc, OSIOP_SFBR),
   1803 				    osiop_read_1(sc, OSIOP_LCRC),
   1804 				    osiop_read_4(sc, OSIOP_DSP) -
   1805 				        sc->sc_scrdma->dm_segs[0].ds_addr);
   1806 			}
   1807 #endif
   1808 			osiop_scsidone(sc->sc_nexus, status);
   1809 		}
   1810 	}
   1811 	splx(s);
   1812 }
   1813 
   1814 void
   1815 osiop_update_xfer_mode(struct osiop_softc *sc, int target)
   1816 {
   1817 	struct osiop_tinfo *tinfo = &sc->sc_tinfo[target];
   1818 	struct scsipi_xfer_mode xm;
   1819 
   1820 	xm.xm_target = target;
   1821 	xm.xm_mode = 0;
   1822 	xm.xm_period = 0;
   1823 	xm.xm_offset = 0;
   1824 
   1825 	if (tinfo->period) {
   1826 		xm.xm_mode |= PERIPH_CAP_SYNC;
   1827 		xm.xm_period = tinfo->period;
   1828 		xm.xm_offset = tinfo->offset;
   1829 	}
   1830 
   1831 	scsipi_async_event(&sc->sc_channel, ASYNC_EVENT_XFER_MODE, &xm);
   1832 }
   1833 
   1834 void
   1835 scsi_period_to_osiop(struct osiop_softc *sc, int target)
   1836 {
   1837 	int period, offset, sxfer, sbcl;
   1838 
   1839 	period = sc->sc_tinfo[target].period;
   1840 	offset = sc->sc_tinfo[target].offset;
   1841 	for (sbcl = 1; sbcl < 4; sbcl++) {
   1842 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
   1843 		if (sxfer >= 0 && sxfer <= 7)
   1844 			break;
   1845 	}
   1846 	if (sbcl > 3) {
   1847 		printf("osiop sync: unable to compute sync params "
   1848 		    "for period %d ns\n", period * 4);
   1849 		/*
   1850 		 * XXX need to pick a value we can do and renegotiate
   1851 		 */
   1852 		sxfer = sbcl = 0;
   1853 	} else {
   1854 		sxfer = (sxfer << 4) | ((offset <= OSIOP_MAX_OFFSET) ?
   1855 		    offset : OSIOP_MAX_OFFSET);
   1856 #ifdef DEBUG_SYNC
   1857 		printf("osiop sync: params for period %dns: sxfer %x sbcl %x",
   1858 		    period * 4, sxfer, sbcl);
   1859 		printf(" actual period %dns\n",
   1860 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
   1861 #endif
   1862 	}
   1863 	sc->sc_tinfo[target].sxfer = sxfer;
   1864 	sc->sc_tinfo[target].sbcl = sbcl;
   1865 #ifdef DEBUG_SYNC
   1866 	printf("osiop sync: osiop_sxfr %02x, osiop_sbcl %02x\n", sxfer, sbcl);
   1867 #endif
   1868 }
   1869 
   1870 void
   1871 osiop_timeout(void *arg)
   1872 {
   1873 	struct osiop_acb *acb = arg;
   1874 	struct scsipi_xfer *xs = acb->xs;
   1875 	struct osiop_softc *sc = acb->sc;
   1876 	int s;
   1877 
   1878 	scsipi_printaddr(xs->xs_periph);
   1879 	printf("command timeout\n");
   1880 
   1881 	s = splbio();
   1882 	/* reset the scsi bus */
   1883 	osiop_resetbus(sc);
   1884 
   1885 	/* deactivate callout */
   1886 	callout_stop(&xs->xs_callout);
   1887 	acb->flags |= ACB_F_TIMEOUT;
   1888 	osiop_reset(sc);
   1889 	splx(s);
   1890 	return;
   1891 }
   1892 
   1893 #ifdef OSIOP_DEBUG
   1894 
   1895 #if OSIOP_TRACE_SIZE
   1896 void
   1897 osiop_dump_trace(void)
   1898 {
   1899 	int i;
   1900 
   1901 	printf("osiop trace: next index %d\n", osiop_trix);
   1902 	i = osiop_trix;
   1903 	do {
   1904 		printf("%3d: '%c' %02x %02x %02x\n", i,
   1905 		    osiop_trbuf[i], osiop_trbuf[i + 1],
   1906 		    osiop_trbuf[i + 2], osiop_trbuf[i + 3]);
   1907 		i = (i + 4) & (OSIOP_TRACE_SIZE - 1);
   1908 	} while (i != osiop_trix);
   1909 }
   1910 #endif
   1911 
   1912 void
   1913 osiop_dump_acb(struct osiop_acb *acb)
   1914 {
   1915 	uint8_t *b;
   1916 	int i;
   1917 
   1918 	printf("acb@%p ", acb);
   1919 	if (acb->xs == NULL) {
   1920 		printf("<unused>\n");
   1921 		return;
   1922 	}
   1923 
   1924 	b = (uint8_t *)&acb->xs->cmd;
   1925 	printf("(%d:%d) status %2x cmdlen %2ld cmd ",
   1926 	    acb->xs->xs_periph->periph_target,
   1927 	    acb->xs->xs_periph->periph_lun, acb->status, acb->cmdlen);
   1928 	for (i = acb->cmdlen; i > 0; i--)
   1929 		printf(" %02x", *b++);
   1930 	printf("\n");
   1931 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
   1932 	    acb->xs->datalen);
   1933 	printf("va %p:%lx ", acb->data, acb->datalen);
   1934 	printf("cur %lx:%lx\n", acb->curaddr, acb->curlen);
   1935 }
   1936 
   1937 void
   1938 osiop_dump(struct osiop_softc *sc)
   1939 {
   1940 	struct osiop_acb *acb;
   1941 	int i, s;
   1942 
   1943 	s = splbio();
   1944 #if OSIOP_TRACE_SIZE
   1945 	osiop_dump_trace();
   1946 #endif
   1947 	printf("%s@%p istat %02x\n",
   1948 	    sc->sc_dev.dv_xname, sc, osiop_read_1(sc, OSIOP_ISTAT));
   1949 	if ((acb = TAILQ_FIRST(&sc->free_list)) != NULL) {
   1950 		printf("Free list:\n");
   1951 		while (acb) {
   1952 			osiop_dump_acb(acb);
   1953 			acb = TAILQ_NEXT(acb, chain);
   1954 		}
   1955 	}
   1956 	if ((acb = TAILQ_FIRST(&sc->ready_list)) != NULL) {
   1957 		printf("Ready list:\n");
   1958 		while (acb) {
   1959 			osiop_dump_acb(acb);
   1960 			acb = TAILQ_NEXT(acb, chain);
   1961 		}
   1962 	}
   1963 	if ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
   1964 		printf("Nexus list:\n");
   1965 		while (acb) {
   1966 			osiop_dump_acb(acb);
   1967 			acb = TAILQ_NEXT(acb, chain);
   1968 		}
   1969 	}
   1970 	if (sc->sc_nexus) {
   1971 		printf("Nexus:\n");
   1972 		osiop_dump_acb(sc->sc_nexus);
   1973 	}
   1974 	for (i = 0; i < OSIOP_NTGT; i++) {
   1975 		if (sc->sc_tinfo[i].cmds > 2) {
   1976 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
   1977 			    i, sc->sc_tinfo[i].cmds,
   1978 			    sc->sc_tinfo[i].dconns,
   1979 			    sc->sc_tinfo[i].lubusy);
   1980 		}
   1981 	}
   1982 	splx(s);
   1983 }
   1984 #endif
   1985