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