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