Home | History | Annotate | Line # | Download | only in ic
osiop.c revision 1.3
      1 /*	$NetBSD: osiop.c,v 1.3 2001/05/17 10:35:30 scw 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("%s: NULL acb or scsipi_xfer in osiop_scsidone\n",
    611 		    sc->sc_dev.dv_xname);
    612 #if defined(OSIOP_DEBUG) && defined(DDB)
    613 		Debugger();
    614 #endif
    615 		return;
    616 	}
    617 #endif
    618 	xs = acb->xs;
    619 	sc = acb->sc;
    620 	periph = xs->xs_periph;
    621 
    622 #ifdef OSIOP_DEBUG
    623 	if (acb->status != ACB_S_DONE)
    624 		printf("%s: acb not done (status %d)\n",
    625 		    sc->sc_dev.dv_xname, acb->status);
    626 #endif
    627 
    628 	xs->status = status;
    629 
    630 	switch (status) {
    631 	case SCSI_OK:
    632 		xs->error = XS_NOERROR;
    633 		break;
    634 	case SCSI_BUSY:
    635 		xs->error = XS_BUSY;
    636 		break;
    637 	case SCSI_CHECK:
    638 		xs->error = XS_BUSY;
    639 		break;
    640 	case SCSI_OSIOP_NOCHECK:
    641 		/*
    642 		 * don't check status, xs->error is already valid
    643 		 */
    644 		break;
    645 	case SCSI_OSIOP_NOSTATUS:
    646 		/*
    647 		 * the status byte was not updated, cmd was
    648 		 * aborted
    649 		 */
    650 		xs->error = XS_SELTIMEOUT;
    651 		break;
    652 	default:
    653 #ifdef OSIOP_DEBUG
    654 		printf("%s: osiop_scsidone: unknown status code (0x%02x)\n",
    655 		    sc->sc_dev.dv_xname, status);
    656 #endif
    657 		xs->error = XS_DRIVER_STUFFUP;
    658 		break;
    659 	}
    660 
    661 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
    662 		bus_dmamap_sync(sc->sc_dmat, acb->datadma, 0, acb->datalen,
    663 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    664 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    665 		bus_dmamap_unload(sc->sc_dmat, acb->datadma);
    666 	}
    667 
    668 	bus_dmamap_sync(sc->sc_dmat, acb->cmddma, 0, acb->cmdlen,
    669 	    BUS_DMASYNC_POSTWRITE);
    670 	bus_dmamap_unload(sc->sc_dmat, acb->cmddma);
    671 
    672 	/*
    673 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    674 	 * a hack to figure out which queue it's on.  Note that it is *not*
    675 	 * necessary to cdr down the ready queue, but we must cdr down the
    676 	 * nexus queue and see if it's there, so we can mark the unit as no
    677 	 * longer busy.  This code is sickening, but it works.
    678 	 */
    679 	if (acb == sc->sc_nexus) {
    680 		sc->sc_nexus = NULL;
    681 		sc->sc_tinfo[periph->periph_target].lubusy &=
    682 		    ~(1 << periph->periph_lun);
    683 		if (!TAILQ_EMPTY(&sc->ready_list))
    684 			dosched = 1;	/* start next command */
    685 		sc->sc_active--;
    686 		OSIOP_TRACE('d', 'a', status, 0);
    687 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
    688 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    689 		OSIOP_TRACE('d', 'r', status, 0);
    690 	} else {
    691 		struct osiop_acb *acb2;
    692 		TAILQ_FOREACH(acb2, &sc->nexus_list, chain) {
    693 			if (acb2 == acb) {
    694 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    695 				sc->sc_tinfo[periph->periph_target].lubusy &=
    696 				    ~(1 << periph->periph_lun);
    697 				sc->sc_active--;
    698 				break;
    699 			}
    700 		}
    701 		if (acb2 == NULL) {
    702 			if (acb->chain.tqe_next != NULL) {
    703 				TAILQ_REMOVE(&sc->ready_list, acb, chain);
    704 				sc->sc_active--;
    705 			} else {
    706 				printf("%s: can't find matching acb\n",
    707 				    sc->sc_dev.dv_xname);
    708 #ifdef DDB
    709 #if 0
    710 				Debugger();
    711 #endif
    712 #endif
    713 			}
    714 		}
    715 		OSIOP_TRACE('d', 'n', status, 0);
    716 	}
    717 	/* Put it on the free list. */
    718 	acb->status = ACB_S_FREE;
    719 	TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    720 	sc->sc_tinfo[periph->periph_target].cmds++;
    721 
    722 	callout_stop(&xs->xs_callout);
    723 	xs->resid = 0;
    724 	scsipi_done(xs);
    725 
    726 	if (dosched && sc->sc_nexus == NULL)
    727 		osiop_sched(sc);
    728 }
    729 
    730 void
    731 osiop_abort(sc, where)
    732 	struct osiop_softc *sc;
    733 	const char *where;
    734 {
    735 
    736 	printf("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    737 	    sc->sc_dev.dv_xname, where,
    738 	    osiop_read_1(sc, OSIOP_DSTAT),
    739 	    osiop_read_1(sc, OSIOP_SSTAT0),
    740 	    osiop_read_1(sc, OSIOP_SBCL));
    741 
    742 	/* XXX XXX XXX */
    743 	if (sc->sc_active > 0) {
    744 		sc->sc_active = 0;
    745 	}
    746 }
    747 
    748 void
    749 osiop_init(sc)
    750 	struct osiop_softc *sc;
    751 {
    752 	int i, inhibit_sync, inhibit_disc;
    753 
    754 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
    755 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
    756 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
    757 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
    758 
    759 	if (sc->sc_minsync < 25)
    760 		sc->sc_minsync = 25;
    761 
    762 	if (sc->sc_clock_freq <= 25) {
    763 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1;	/* SCLK/1 */
    764 		sc->sc_tcp[0] = sc->sc_tcp[1];
    765 	} else if (sc->sc_clock_freq <= 37) {
    766 		sc->sc_dcntl |= OSIOP_DCNTL_CF_1_5;	/* SCLK/1.5 */
    767 		sc->sc_tcp[0] = sc->sc_tcp[2];
    768 	} else if (sc->sc_clock_freq <= 50) {
    769 		sc->sc_dcntl |= OSIOP_DCNTL_CF_2;	/* SCLK/2 */
    770 		sc->sc_tcp[0] = sc->sc_tcp[3];
    771 	} else {
    772 		sc->sc_dcntl |= OSIOP_DCNTL_CF_3;	/* SCLK/3 */
    773 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
    774 	}
    775 
    776 	if ((sc->sc_cfflags & 0x10000) != 0) {
    777 		sc->sc_flags |= OSIOP_NODMA;
    778 #ifdef OSIOP_DEBUG
    779 		printf("%s: DMA disabled; use polling\n",
    780 		    sc->sc_dev.dv_xname);
    781 #endif
    782 	}
    783 
    784 	inhibit_sync = (sc->sc_cfflags & 0xff00) >> 8;	/* XXX */
    785 	inhibit_disc =  sc->sc_cfflags & 0x00ff;	/* XXX */
    786 #ifdef OSIOP_DEBUG
    787 	if (inhibit_sync != 0)
    788 		printf("%s: Inhibiting synchronous transfer: 0x%02x\n",
    789 		    sc->sc_dev.dv_xname, inhibit_sync);
    790 	if (inhibit_disc != 0)
    791 		printf("%s: Inhibiting disconnect: 0x%02x\n",
    792 		    sc->sc_dev.dv_xname, inhibit_disc);
    793 #endif
    794 	for (i = 0; i < OSIOP_NTGT; i++) {
    795 		if (inhibit_sync & (1 << i))
    796 			sc->sc_tinfo[i].flags |= TI_NOSYNC;
    797 		if (inhibit_disc & (1 << i))
    798 			sc->sc_tinfo[i].flags |= TI_NODISC;
    799 	}
    800 
    801 	osiop_resetbus(sc);
    802 	osiop_reset(sc);
    803 }
    804 
    805 void
    806 osiop_reset(sc)
    807 	struct osiop_softc *sc;
    808 {
    809 	struct osiop_acb *acb;
    810 	int s;
    811 	u_int8_t stat;
    812 
    813 #ifdef OSIOP_DEBUG
    814 	printf("%s: resetting chip\n", sc->sc_dev.dv_xname);
    815 #endif
    816 	if (sc->sc_flags & OSIOP_ALIVE)
    817 		osiop_abort(sc, "reset");
    818 
    819 	s = splbio();
    820 
    821 	/*
    822 	 * Reset the chip
    823 	 * XXX - is this really needed?
    824 	 */
    825 
    826 	/* abort current script */
    827 	osiop_write_1(sc, OSIOP_ISTAT,
    828 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_ABRT);
    829 	/* reset chip */
    830 	osiop_write_1(sc, OSIOP_ISTAT,
    831 	    osiop_read_1(sc, OSIOP_ISTAT) | OSIOP_ISTAT_RST);
    832 	delay(100);
    833 	osiop_write_1(sc, OSIOP_ISTAT,
    834 	    osiop_read_1(sc, OSIOP_ISTAT) & ~OSIOP_ISTAT_RST);
    835 	delay(100);
    836 
    837 	/*
    838 	 * Set up various chip parameters
    839 	 */
    840 	osiop_write_1(sc, OSIOP_SCNTL0,
    841 	    OSIOP_ARB_FULL | OSIOP_SCNTL0_EPC | OSIOP_SCNTL0_EPG);
    842 	osiop_write_1(sc, OSIOP_SCNTL1, OSIOP_SCNTL1_ESR);
    843 	osiop_write_1(sc, OSIOP_DCNTL, sc->sc_dcntl);
    844 	osiop_write_1(sc, OSIOP_DMODE, OSIOP_DMODE_BL4);
    845 	/* don't enable interrupts yet */
    846 	osiop_write_1(sc, OSIOP_SIEN, 0x00);
    847 	osiop_write_1(sc, OSIOP_DIEN, 0x00);
    848 	osiop_write_1(sc, OSIOP_SCID, OSIOP_SCID_VALUE(sc->sc_id));
    849 	osiop_write_1(sc, OSIOP_DWT, 0x00);
    850 	osiop_write_1(sc, OSIOP_CTEST0, osiop_read_1(sc, OSIOP_CTEST0)
    851 	    | OSIOP_CTEST0_BTD | OSIOP_CTEST0_EAN);
    852 	osiop_write_1(sc, OSIOP_CTEST7,
    853 	    osiop_read_1(sc, OSIOP_CTEST7) | sc->sc_ctest7);
    854 
    855 	/* will need to re-negotiate sync xfers */
    856 	memset(&sc->sc_sync, 0, sizeof(sc->sc_sync));
    857 
    858 	stat = osiop_read_1(sc, OSIOP_ISTAT);
    859 	if (stat & OSIOP_ISTAT_SIP)
    860 		osiop_read_1(sc, OSIOP_SSTAT0);
    861 	if (stat & OSIOP_ISTAT_DIP)
    862 		osiop_read_1(sc, OSIOP_DSTAT);
    863 
    864 	splx(s);
    865 
    866 	delay(osiop_reset_delay * 1000);
    867 
    868 	if (sc->sc_nexus != NULL) {
    869 		sc->sc_nexus->xs->error =
    870 		    (sc->sc_nexus->flags & ACB_F_TIMEOUT) ?
    871 		    XS_TIMEOUT : XS_RESET;
    872 		sc->sc_nexus->status = ACB_S_DONE;
    873 		sc->sc_nexus->flags = 0;
    874 		osiop_scsidone(sc->sc_nexus, SCSI_OSIOP_NOCHECK);
    875 	}
    876 	while ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
    877 		acb->xs->error = (acb->flags & ACB_F_TIMEOUT) ?
    878 		    XS_TIMEOUT : XS_RESET;
    879 		acb->status = ACB_S_DONE;
    880 		acb->flags = 0;
    881 		osiop_scsidone(acb, SCSI_OSIOP_NOCHECK);
    882 	}
    883 
    884 	sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
    885 	/* enable SCSI and DMA interrupts */
    886 	sc->sc_sien = OSIOP_SIEN_M_A | OSIOP_SIEN_STO | /*OSIOP_SIEN_SEL |*/
    887 	    OSIOP_SIEN_SGE | OSIOP_SIEN_UDC | OSIOP_SIEN_RST | OSIOP_SIEN_PAR;
    888 	sc->sc_dien = OSIOP_DIEN_BF | OSIOP_DIEN_ABRT | OSIOP_DIEN_SIR |
    889 	    /*OSIOP_DIEN_WTD |*/ OSIOP_DIEN_IID;
    890 	osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
    891 	osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
    892 }
    893 
    894 void
    895 osiop_resetbus(sc)
    896 	struct osiop_softc *sc;
    897 {
    898 
    899 	osiop_write_1(sc, OSIOP_SIEN, 0);
    900 	osiop_write_1(sc, OSIOP_SCNTL1,
    901 	    osiop_read_1(sc, OSIOP_SCNTL1) | OSIOP_SCNTL1_RST);
    902 	delay(25);
    903 	osiop_write_1(sc, OSIOP_SCNTL1,
    904 	    osiop_read_1(sc, OSIOP_SCNTL1) & ~OSIOP_SCNTL1_RST);
    905 }
    906 
    907 /*
    908  * Setup Data Storage for 53C710 and start SCRIPTS processing
    909  */
    910 
    911 void
    912 osiop_start(sc)
    913 	struct osiop_softc *sc;
    914 {
    915 	struct osiop_acb *acb = sc->sc_nexus;
    916 	struct osiop_ds *ds = acb->ds;
    917 	struct scsipi_xfer *xs = acb->xs;
    918 	bus_dmamap_t dsdma = sc->sc_dsdma, datadma = acb->datadma;
    919 	int target = xs->xs_periph->periph_target;
    920 	int lun = xs->xs_periph->periph_lun;
    921 	int disconnect;
    922 	int i;
    923 
    924 #ifdef OSIOP_DEBUG
    925 	if (osiop_debug & DEBUG_DISC &&
    926 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
    927 		printf("ACK! osiop was busy: script %p dsa %p active %d\n",
    928 		    sc->sc_script, acb->ds, sc->sc_active);
    929 		printf("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
    930 		    osiop_read_1(sc, OSIOP_ISTAT),
    931 		    osiop_read_1(sc, OSIOP_SFBR),
    932 		    osiop_read_1(sc, OSIOP_LCRC),
    933 		    osiop_read_1(sc, OSIOP_SIEN),
    934 		    osiop_read_1(sc, OSIOP_DIEN));
    935 #ifdef DDB
    936 #if 0
    937 		Debugger();
    938 #endif
    939 #endif
    940 	}
    941 #endif
    942 
    943 #ifdef OSIOP_DEBUG
    944 	if (acb->status != ACB_S_READY)
    945 		panic("osiop_start: non-ready cmd in acb");
    946 #endif
    947 
    948 	acb->intstat = 0;
    949 
    950 	ds->cmd.count = acb->cmdlen;
    951 	ds->cmd.addr = acb->cmddma->dm_segs[0].ds_addr;
    952 
    953 	ds->scsi_addr = (1 << (16 + target)) | (sc->sc_sync[target].sxfer << 8);
    954 
    955 	disconnect = (xs->xs_control & XS_CTL_REQSENSE) == 0 &&
    956 	    (sc->sc_tinfo[target].flags & TI_NODISC) == 0;
    957 
    958 	ds->msgout[0] = MSG_IDENTIFY(lun, disconnect);
    959 	ds->id.count = 1;
    960 	ds->stat[0] = SCSI_OSIOP_NOSTATUS;	/* set invalid status */
    961 	ds->msgbuf[0] = ds->msgbuf[1] = MSG_INVALID;
    962 	memset(&ds->data, 0, sizeof(ds->data));
    963 
    964 	/*
    965 	 * Negotiate wide is the initial negotiation state;  since the 53c710
    966 	 * doesn't do wide transfers, just begin the synchronous transfer
    967 	 * negotation here.
    968 	 */
    969 	if (sc->sc_sync[target].state == NEG_INIT) {
    970 		if ((sc->sc_tinfo[target].flags & TI_NOSYNC) != 0) {
    971 			sc->sc_sync[target].state = NEG_DONE;
    972 			sc->sc_sync[target].sbcl = 0;
    973 			sc->sc_sync[target].sxfer = 0;
    974 #ifdef OSIOP_DEBUG
    975 			if (osiopsync_debug)
    976 				printf("Forcing target %d asynchronous\n",
    977 				    target);
    978 #endif
    979 		} else {
    980 			ds->msgbuf[2] = MSG_INVALID;
    981 			ds->msgout[1] = MSG_EXTENDED;
    982 			ds->msgout[2] = MSG_EXT_SDTR_LEN;
    983 			ds->msgout[3] = MSG_EXT_SDTR;
    984 			ds->msgout[4] = sc->sc_minsync;
    985 			ds->msgout[5] = OSIOP_MAX_OFFSET;
    986 			ds->id.count = 6;
    987 			sc->sc_sync[target].state = NEG_WAITS;
    988 #ifdef OSIOP_DEBUG
    989 			if (osiopsync_debug)
    990 				printf("Sending sync request to target %d\n",
    991 				    target);
    992 #endif
    993 		}
    994 	}
    995 
    996 	acb->curaddr = 0;
    997 	acb->curlen = 0;
    998 
    999 	/*
   1000 	 * Build physical DMA addresses for scatter/gather I/O
   1001 	 */
   1002 	if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   1003 		for (i = 0; i < datadma->dm_nsegs; i++) {
   1004 			ds->data[i].count = datadma->dm_segs[i].ds_len;
   1005 			ds->data[i].addr  = datadma->dm_segs[i].ds_addr;
   1006 		}
   1007 	}
   1008 
   1009 	/* sync script data structure */
   1010 	bus_dmamap_sync(sc->sc_dmat, dsdma,
   1011 	    acb->dsoffset, sizeof(struct osiop_ds),
   1012 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1013 
   1014 	acb->status = ACB_S_ACTIVE;
   1015 
   1016 	/* handle timeout */
   1017 	if ((xs->xs_control & XS_CTL_POLL) == 0) {
   1018 		int timeout = acb->xs->timeout;
   1019 		/* start expire timer */
   1020 		timeout = (timeout > 100000) ?
   1021 		    timeout / 1000 * hz : timeout * hz / 1000;
   1022 		if (timeout == 0)
   1023 			timeout = 1;
   1024 		callout_reset(&xs->xs_callout, timeout,
   1025 		    osiop_timeout, acb);
   1026 	}
   1027 #ifdef OSIOP_DEBUG
   1028 	if (osiop_debug & DEBUG_DISC &&
   1029 	    osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1030 		printf("ACK! osiop was busy at start: "
   1031 		    "script %p dsa %p active %d\n",
   1032 		    sc->sc_script, acb->ds, sc->sc_active);
   1033 #ifdef DDB
   1034 #if 0
   1035 		Debugger();
   1036 #endif
   1037 #endif
   1038 	}
   1039 #endif
   1040 	if (TAILQ_EMPTY(&sc->nexus_list)) {
   1041 		if (osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON)
   1042 			printf("%s: osiop_select while connected?\n",
   1043 			    sc->sc_dev.dv_xname);
   1044 		osiop_write_4(sc, OSIOP_TEMP, 0);
   1045 		osiop_write_1(sc, OSIOP_SBCL, sc->sc_sync[target].sbcl);
   1046 		osiop_write_4(sc, OSIOP_DSA,
   1047 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1048 		osiop_write_4(sc, OSIOP_DSP,
   1049 		    sc->sc_scrdma->dm_segs[0].ds_addr + Ent_scripts);
   1050 		OSIOP_TRACE('s', 1, 0, 0);
   1051 	} else {
   1052 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
   1053 			osiop_write_1(sc, OSIOP_ISTAT, OSIOP_ISTAT_SIGP);
   1054 			OSIOP_TRACE('s', 2, 0, 0);
   1055 		} else {
   1056 			OSIOP_TRACE('s', 3,
   1057 			    osiop_read_1(sc, OSIOP_ISTAT), 0);
   1058 		}
   1059 	}
   1060 #ifdef OSIOP_DEBUG
   1061 	osiopstarts++;
   1062 #endif
   1063 }
   1064 
   1065 /*
   1066  * Process a DMA or SCSI interrupt from the 53C710 SIOP
   1067  */
   1068 
   1069 int
   1070 osiop_checkintr(sc, istat, dstat, sstat0, status)
   1071 	struct	osiop_softc *sc;
   1072 	u_int8_t istat;
   1073 	u_int8_t dstat;
   1074 	u_int8_t sstat0;
   1075 	int *status;
   1076 {
   1077 	struct osiop_acb *acb = sc->sc_nexus;
   1078 	struct osiop_ds *ds;
   1079 	bus_dmamap_t dsdma = sc->sc_dsdma;
   1080 	bus_addr_t scraddr = sc->sc_scrdma->dm_segs[0].ds_addr;
   1081 	int target = 0;
   1082 	int dfifo, dbc, intcode, sstat1;
   1083 
   1084 	dfifo = osiop_read_1(sc, OSIOP_DFIFO);
   1085 	dbc = osiop_read_4(sc, OSIOP_DBC) & 0x00ffffff;
   1086 	sstat1 = osiop_read_1(sc, OSIOP_SSTAT1);
   1087 	osiop_write_1(sc, OSIOP_CTEST8,
   1088 	    osiop_read_1(sc, OSIOP_CTEST8) | OSIOP_CTEST8_CLF);
   1089 	while ((osiop_read_1(sc, OSIOP_CTEST1) & OSIOP_CTEST1_FMT) !=
   1090 	    OSIOP_CTEST1_FMT)
   1091 		;
   1092 	osiop_write_1(sc, OSIOP_CTEST8,
   1093 	    osiop_read_1(sc, OSIOP_CTEST8) & ~OSIOP_CTEST8_CLF);
   1094 	intcode = osiop_read_4(sc, OSIOP_DSPS);
   1095 #ifdef OSIOP_DEBUG
   1096 	osiopints++;
   1097 	if (osiop_read_4(sc, OSIOP_DSP) != 0 &&
   1098 	    (osiop_read_4(sc, OSIOP_DSP) < scraddr ||
   1099 	    osiop_read_4(sc, OSIOP_DSP) >= scraddr + sizeof(osiop_script))) {
   1100 		printf("%s: dsp not within script dsp %x scripts %lx:%lx",
   1101 		    sc->sc_dev.dv_xname,
   1102 		    osiop_read_4(sc, OSIOP_DSP),
   1103 		    scraddr, scraddr + sizeof(osiop_script));
   1104 		printf(" istat %x dstat %x sstat0 %x\n", istat, dstat, sstat0);
   1105 #ifdef DDB
   1106 		Debugger();
   1107 #endif
   1108 	}
   1109 #endif
   1110 	OSIOP_TRACE('i', dstat, istat, (istat & OSIOP_ISTAT_DIP) ?
   1111 	    intcode & 0xff : sstat0);
   1112 
   1113 	if (acb != NULL) { /* XXX */
   1114 		ds = acb->ds;
   1115 		bus_dmamap_sync(sc->sc_dmat, dsdma,
   1116 		    acb->dsoffset, sizeof(struct osiop_ds),
   1117 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1118 #ifdef OSIOP_DEBUG
   1119 		if (acb->status != ACB_S_ACTIVE)
   1120 			printf("osiop_checkintr: acb not active (status %d)\n",
   1121 			    acb->status);
   1122 #endif
   1123 	}
   1124 
   1125 
   1126 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_ok) {
   1127 		/* Normal completion status, or check condition */
   1128 #ifdef OSIOP_DEBUG
   1129 		if (osiop_read_4(sc, OSIOP_DSA) !=
   1130 		    dsdma->dm_segs[0].ds_addr + acb->dsoffset) {
   1131 			printf("osiop: invalid dsa: %x %lx\n",
   1132 			    osiop_read_4(sc, OSIOP_DSA),
   1133 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1134 			panic("*** osiop DSA invalid ***");
   1135 		}
   1136 #endif
   1137 		target = acb->xs->xs_periph->periph_target;
   1138 		if (sc->sc_sync[target].state == NEG_WAITS) {
   1139 			if (ds->msgbuf[1] == MSG_INVALID)
   1140 				printf("%s: target %d ignored sync request\n",
   1141 				    sc->sc_dev.dv_xname, target);
   1142 			else if (ds->msgbuf[1] == MSG_MESSAGE_REJECT)
   1143 				printf("%s: target %d rejected sync request\n",
   1144 				    sc->sc_dev.dv_xname, target);
   1145 			else
   1146 /* XXX - need to set sync transfer parameters */
   1147 				printf("%s: target %d (sync) %02x %02x %02x\n",
   1148 				    sc->sc_dev.dv_xname, target, ds->msgbuf[1],
   1149 				    ds->msgbuf[2], ds->msgbuf[3]);
   1150 			sc->sc_sync[target].state = NEG_DONE;
   1151 		}
   1152 #ifdef OSIOP_DEBUG
   1153 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1154 #if 0
   1155 			printf("ACK! osiop was busy at end: "
   1156 			    "script %p dsa %p\n", &osiop_script, ds);
   1157 #ifdef DDB
   1158 			Debugger();
   1159 #endif
   1160 #endif
   1161 		}
   1162 		if (ds->msgbuf[0] != MSG_CMDCOMPLETE)
   1163 			printf("%s: message was not COMMAND COMPLETE: %02x\n",
   1164 			    sc->sc_dev.dv_xname, ds->msgbuf[0]);
   1165 #endif
   1166 		if (!TAILQ_EMPTY(&sc->nexus_list))
   1167 			osiop_write_1(sc, OSIOP_DCNTL,
   1168 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1169 		*status = ds->stat[0];
   1170 		acb->status = ACB_S_DONE;
   1171 		return (1);
   1172 	}
   1173 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_syncmsg) {
   1174 		target = acb->xs->xs_periph->periph_target;
   1175 		if (ds->msgbuf[1] == MSG_EXTENDED &&
   1176 		    ds->msgbuf[2] == MSG_EXT_SDTR_LEN &&
   1177 		    ds->msgbuf[3] == MSG_EXT_SDTR) {
   1178 #ifdef OSIOP_DEBUG
   1179 			if (osiopsync_debug)
   1180 				printf("sync msg in: "
   1181 				    "%02x %02x %02x %02x %02x %02x\n",
   1182 				    ds->msgbuf[0], ds->msgbuf[1],
   1183 				    ds->msgbuf[2], ds->msgbuf[3],
   1184 				    ds->msgbuf[4], ds->msgbuf[5]);
   1185 #endif
   1186 			sc->sc_sync[target].sxfer = 0;
   1187 			sc->sc_sync[target].sbcl = 0;
   1188 			if (ds->msgbuf[2] == MSG_EXT_SDTR_LEN &&
   1189 			    ds->msgbuf[3] == MSG_EXT_SDTR &&
   1190 			    ds->msgbuf[5] != 0) {
   1191 				printf("%s: target %d now synchronous, "
   1192 				    "period=%d ns, offset=%d\n",
   1193 				    sc->sc_dev.dv_xname, target,
   1194 				    ds->msgbuf[4] * 4, ds->msgbuf[5]);
   1195 				    scsi_period_to_osiop(sc, target);
   1196 			}
   1197 			bus_dmamap_sync(sc->sc_dmat, dsdma,
   1198 			    acb->dsoffset, sizeof(struct osiop_ds),
   1199 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1200 			osiop_write_1(sc, OSIOP_SXFER,
   1201 			    sc->sc_sync[target].sxfer);
   1202 			osiop_write_1(sc, OSIOP_SBCL,
   1203 			    sc->sc_sync[target].sbcl);
   1204 			if (sc->sc_sync[target].state == NEG_WAITS) {
   1205 				sc->sc_sync[target].state = NEG_DONE;
   1206 				osiop_write_4(sc, OSIOP_DSP,
   1207 				    scraddr + Ent_clear_ack);
   1208 				return (0);
   1209 			}
   1210 			osiop_write_1(sc, OSIOP_DCNTL,
   1211 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1212 			sc->sc_sync[target].state = NEG_DONE;
   1213 			return (0);
   1214 		}
   1215 		/* XXX - not SDTR message */
   1216 	}
   1217 	if (sstat0 & OSIOP_SSTAT0_M_A) {
   1218 		/* Phase mismatch */
   1219 #ifdef OSIOP_DEBUG
   1220 		osiopphmm++;
   1221 		if (acb == NULL)
   1222 			printf("%s: Phase mismatch with no active command?\n",
   1223 			    sc->sc_dev.dv_xname);
   1224 #endif
   1225 		if (acb->datalen > 0) {
   1226 			int adjust = (dfifo - (dbc & 0x7f)) & 0x7f;
   1227 			if (sstat1 & OSIOP_SSTAT1_ORF)
   1228 				adjust++;
   1229 			if (sstat1 & OSIOP_SSTAT1_OLF)
   1230 				adjust++;
   1231 			acb->curaddr = osiop_read_4(sc, OSIOP_DNAD) - adjust;
   1232 			acb->curlen = dbc + adjust;
   1233 #ifdef OSIOP_DEBUG
   1234 			if (osiop_debug & DEBUG_DISC) {
   1235 				printf("Phase mismatch: curaddr %lx "
   1236 				    "curlen %lx dfifo %x dbc %x sstat1 %x "
   1237 				    "adjust %x sbcl %x starts %d acb %p\n",
   1238 				    acb->curaddr, acb->curlen, dfifo,
   1239 				    dbc, sstat1, adjust,
   1240 				    osiop_read_1(sc, OSIOP_SBCL),
   1241 				    osiopstarts, acb);
   1242 				if (ds->data[1].count != 0) {
   1243 					int i;
   1244 					for (i = 0; ds->data[i].count != 0; i++)
   1245 						printf("chain[%d] "
   1246 						    "addr %x len %x\n", i,
   1247 						    ds->data[i].addr,
   1248 						    ds->data[i].count);
   1249 				}
   1250 				bus_dmamap_sync(sc->sc_dmat, dsdma,
   1251 				    acb->dsoffset, sizeof(struct osiop_ds),
   1252 				    BUS_DMASYNC_PREREAD |
   1253 				    BUS_DMASYNC_PREWRITE);
   1254 			}
   1255 #endif
   1256 		}
   1257 #ifdef OSIOP_DEBUG
   1258 		OSIOP_TRACE('m', osiop_read_1(sc, OSIOP_SBCL),
   1259 		    osiop_read_4(sc, OSIOP_DSP) >> 8,
   1260 		    osiop_read_4(sc, OSIOP_DSP));
   1261 		if (osiop_debug & DEBUG_PHASE)
   1262 			printf("Phase mismatch: %x dsp +%lx dcmd %x\n",
   1263 			    osiop_read_1(sc, OSIOP_SBCL),
   1264 			    osiop_read_4(sc, OSIOP_DSP) - scraddr,
   1265 			    osiop_read_4(sc, OSIOP_DBC));
   1266 #endif
   1267 		if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_REQ) == 0) {
   1268 			printf("Phase mismatch: "
   1269 			    "REQ not asserted! %02x dsp %x\n",
   1270 			    osiop_read_1(sc, OSIOP_SBCL),
   1271 			    osiop_read_4(sc, OSIOP_DSP));
   1272 #if defined(OSIOP_DEBUG) && defined(DDB)
   1273 			/*Debugger(); XXX is*/
   1274 #endif
   1275 		}
   1276 		switch (OSIOP_PHASE(osiop_read_1(sc, OSIOP_SBCL))) {
   1277 		case DATA_OUT_PHASE:
   1278 		case DATA_IN_PHASE:
   1279 		case STATUS_PHASE:
   1280 		case COMMAND_PHASE:
   1281 		case MSG_IN_PHASE:
   1282 		case MSG_OUT_PHASE:
   1283 			osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
   1284 			break;
   1285 		default:
   1286 			printf("%s: invalid phase\n", sc->sc_dev.dv_xname);
   1287 			goto bad_phase;
   1288 		}
   1289 		return (0);
   1290 	}
   1291 	if (sstat0 & OSIOP_SSTAT0_STO) {
   1292 		/* Select timed out */
   1293 #ifdef OSIOP_DEBUG
   1294 		if (acb == NULL)
   1295 			printf("%s: Select timeout with no active command?\n",
   1296 			    sc->sc_dev.dv_xname);
   1297 		if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1298 			printf("ACK! osiop was busy at timeout: "
   1299 			    "script %p dsa %lx\n", sc->sc_script,
   1300 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1301 			printf(" sbcl %x sdid %x "
   1302 			    "istat %x dstat %x sstat0 %x\n",
   1303 			    osiop_read_1(sc, OSIOP_SBCL),
   1304 			    osiop_read_1(sc, OSIOP_SDID),
   1305 			    istat, dstat, sstat0);
   1306 			if ((osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) == 0) {
   1307 				printf("Yikes, it's not busy now!\n");
   1308 #if 0
   1309 				*status = SCSI_OSIOP_NOSTATUS;
   1310 				if (!TAILQ_EMPTY(&sc->nexus_list))
   1311 					osiop_write_4(sc, OSIOP_DSP,
   1312 					    scraddr + Ent_wait_reselect);
   1313 				return (1);
   1314 #endif
   1315 			}
   1316 #if 0
   1317 			osiop_write_1(sc, OSIOP_DCNTL,
   1318 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1319 #endif
   1320 #ifdef DDB
   1321 			Debugger();
   1322 #endif
   1323 			return (0);
   1324 		}
   1325 #endif
   1326 		acb->status = ACB_S_DONE;
   1327 		*status = SCSI_OSIOP_NOSTATUS;
   1328 		acb->xs->error = XS_SELTIMEOUT;
   1329 		if (!TAILQ_EMPTY(&sc->nexus_list))
   1330 			osiop_write_4(sc, OSIOP_DSP,
   1331 			    scraddr + Ent_wait_reselect);
   1332 		return (1);
   1333 	}
   1334 	if (acb != NULL)
   1335 		target = acb->xs->xs_periph->periph_target;
   1336 	else
   1337 		target = sc->sc_id;
   1338 	if (sstat0 & OSIOP_SSTAT0_UDC) {
   1339 #ifdef OSIOP_DEBUG
   1340 		if (acb == NULL)
   1341 			printf("%s: Unexpected disconnect "
   1342 			    "with no active command?\n", sc->sc_dev.dv_xname);
   1343 		printf("%s: target %d disconnected unexpectedly\n",
   1344 		    sc->sc_dev.dv_xname, target);
   1345 #endif
   1346 #if 0
   1347 		osiop_abort(sc, "osiop_chkintr");
   1348 #endif
   1349 		*status = SCSI_CHECK;
   1350 		if (!TAILQ_EMPTY(&sc->nexus_list))
   1351 			osiop_write_4(sc, OSIOP_DSP,
   1352 			    scraddr + Ent_wait_reselect);
   1353 		return (acb != NULL);
   1354 	}
   1355 	if (dstat & OSIOP_DSTAT_SIR &&
   1356 	    (intcode == A_int_disc || intcode == A_int_disc_wodp)) {
   1357 		/* Disconnect */
   1358 		if (acb == NULL) {
   1359 			printf("%s: Disconnect with no active command?\n",
   1360 			    sc->sc_dev.dv_xname);
   1361 			return (0);
   1362 		}
   1363 #ifdef OSIOP_DEBUG
   1364 		if (osiop_debug & DEBUG_DISC) {
   1365 			printf("%s: ID %02x disconnected TEMP %x (+%lx) "
   1366 			    "curaddr %lx curlen %lx buf %x len %x dfifo %x "
   1367 			    "dbc %x sstat1 %x starts %d acb %p\n",
   1368 			    sc->sc_dev.dv_xname, 1 << target,
   1369 			    osiop_read_4(sc, OSIOP_TEMP),
   1370 			    (osiop_read_4(sc, OSIOP_TEMP) != 0) ?
   1371 			        osiop_read_4(sc, OSIOP_TEMP) - scraddr : 0,
   1372 			    acb->curaddr, acb->curlen,
   1373 			    ds->data[0].addr, ds->data[0].count,
   1374 			    dfifo, dbc, sstat1, osiopstarts, acb);
   1375 			bus_dmamap_sync(sc->sc_dmat, dsdma,
   1376 			    acb->dsoffset, sizeof(struct osiop_ds),
   1377 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1378 		}
   1379 #endif
   1380 		/*
   1381 		 * XXXX need to update curaddr/curlen to reflect
   1382 		 * current data transferred.  If device disconnected in
   1383 		 * the middle of a DMA block, they should already be set
   1384 		 * by the phase change interrupt.  If the disconnect
   1385 		 * occurs on a DMA block boundary, we have to figure out
   1386 		 * which DMA block it was.
   1387 		 */
   1388 		if (acb->datalen > 0 &&
   1389 		    osiop_read_4(sc, OSIOP_TEMP) != 0) {
   1390 			long n = osiop_read_4(sc, OSIOP_TEMP) - scraddr;
   1391 
   1392 			if (acb->curlen != 0 &&
   1393 			    acb->curlen != ds->data[0].count)
   1394 				printf("%s: curaddr/curlen already set? "
   1395 				    "n %lx iob %lx/%lx chain[0] %x/%x\n",
   1396 				    sc->sc_dev.dv_xname, n,
   1397 				    acb->curaddr, acb->curlen,
   1398 				    ds->data[0].addr, ds->data[0].count);
   1399 			if (n < Ent_datain)
   1400 				n = (n - Ent_dataout) / 16;
   1401 			else
   1402 				n = (n - Ent_datain) / 16;
   1403 			if (n <= 0 && n > OSIOP_NSG)
   1404 				printf("TEMP invalid %ld\n", n);
   1405 			else {
   1406 				acb->curaddr = ds->data[n].addr;
   1407 				acb->curlen = ds->data[n].count;
   1408 			}
   1409 #ifdef OSIOP_DEBUG
   1410 			if (osiop_debug & DEBUG_DISC) {
   1411 				printf("%s: TEMP offset %ld",
   1412 				    sc->sc_dev.dv_xname, n);
   1413 				printf(" curaddr %lx curlen %lx\n",
   1414 				    acb->curaddr, acb->curlen);
   1415 			}
   1416 #endif
   1417 		}
   1418 		/*
   1419 		 * If data transfer was interrupted by disconnect, curaddr
   1420 		 * and curlen should reflect the point of interruption.
   1421 		 * Adjust the DMA chain so that the data transfer begins
   1422 		 * at the appropriate place upon reselection.
   1423 		 * XXX This should only be done on save data pointer message?
   1424 		 */
   1425 		if (acb->curlen > 0) {
   1426 			int i, j;
   1427 
   1428 #ifdef OSIOP_DEBUG
   1429 			if (osiop_debug & DEBUG_DISC)
   1430 				printf("%s: adjusting DMA chain\n",
   1431 				    sc->sc_dev.dv_xname);
   1432 			if (intcode == A_int_disc_wodp)
   1433 				printf("%s: ID %02x disconnected "
   1434 				    "without Save Data Pointers\n",
   1435 				    sc->sc_dev.dv_xname, 1 << target);
   1436 #endif
   1437 			for (i = 0; i < OSIOP_NSG; i++) {
   1438 				if (ds->data[i].count == 0)
   1439 					break;
   1440 				if (acb->curaddr >= ds->data[i].addr &&
   1441 				    acb->curaddr <
   1442 				    (ds->data[i].addr + ds->data[i].count))
   1443 					break;
   1444 			}
   1445 			if (i >= OSIOP_NSG || ds->data[i].count == 0) {
   1446 				printf("couldn't find saved data pointer: "
   1447 				    "curaddr %lx curlen %lx i %d\n",
   1448 				    acb->curaddr, acb->curlen, i);
   1449 #ifdef DDB
   1450 				Debugger();
   1451 #endif
   1452 			}
   1453 #ifdef OSIOP_DEBUG
   1454 			if (osiop_debug & DEBUG_DISC)
   1455 				printf(" chain[0]: %x/%x -> %lx/%lx\n",
   1456 				    ds->data[0].addr, ds->data[0].count,
   1457 				    acb->curaddr, acb->curlen);
   1458 #endif
   1459 			ds->data[0].addr = acb->curaddr;
   1460 			ds->data[0].count = acb->curlen;
   1461 			for (j = 1, i = i + 1;
   1462 			    i < OSIOP_NSG && ds->data[i].count > 0;
   1463 			    i++, j++) {
   1464 #ifdef OSIOP_DEBUG
   1465 			if (osiop_debug & DEBUG_DISC)
   1466 				printf("  chain[%d]: %x/%x -> %x/%x\n", j,
   1467 				    ds->data[j].addr, ds->data[j].count,
   1468 				    ds->data[i].addr, ds->data[i].count);
   1469 #endif
   1470 				ds->data[j].addr  = ds->data[i].addr;
   1471 				ds->data[j].count = ds->data[i].count;
   1472 			}
   1473 			if (j < OSIOP_NSG) {
   1474 				ds->data[j].addr  = 0;
   1475 				ds->data[j].count = 0;
   1476 			}
   1477 			bus_dmamap_sync(sc->sc_dmat, dsdma,
   1478 			    acb->dsoffset, sizeof(struct osiop_ds),
   1479 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1480 		}
   1481 		sc->sc_tinfo[target].dconns++;
   1482 		/*
   1483 		 * add nexus to waiting list
   1484 		 * clear nexus
   1485 		 * try to start another command for another target/lun
   1486 		 */
   1487 		acb->intstat = sc->sc_flags & OSIOP_INTSOFF;
   1488 		TAILQ_INSERT_TAIL(&sc->nexus_list, acb, chain);
   1489 		sc->sc_nexus = NULL;		/* no current device */
   1490 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_wait_reselect);
   1491 		/* XXXX start another command ? */
   1492 		if (!TAILQ_EMPTY(&sc->ready_list))
   1493 			osiop_sched(sc);
   1494 		return (0);
   1495 	}
   1496 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_reconnect) {
   1497 		int reselid = ffs(osiop_read_4(sc, OSIOP_SCRATCH) & 0xff) - 1;
   1498 		int reselun = osiop_read_1(sc, OSIOP_SFBR) & 0x07;
   1499 #ifdef OSIOP_DEBUG
   1500 		u_int8_t resmsg;
   1501 #endif
   1502 
   1503 		/* Reconnect */
   1504 		/* XXXX save current SBCL */
   1505 		sc->sc_sstat1 = osiop_read_1(sc, OSIOP_SBCL);
   1506 #ifdef OSIOP_DEBUG
   1507 		if (osiop_debug & DEBUG_DISC)
   1508 			printf("%s: target ID %02x reselected dsps %x\n",
   1509 			    sc->sc_dev.dv_xname, reselid, intcode);
   1510 		resmsg = osiop_read_1(sc, OSIOP_SFBR);
   1511 		if (!MSG_ISIDENTIFY(resmsg))
   1512 			printf("%s: Reselect message in was not identify: "
   1513 			    "%02x\n", sc->sc_dev.dv_xname, resmsg);
   1514 #endif
   1515 		if (sc->sc_nexus != NULL) {
   1516 			struct scsipi_periph *periph =
   1517 			    sc->sc_nexus->xs->xs_periph;
   1518 #ifdef OSIOP_DEBUG
   1519 			if (osiop_debug & DEBUG_DISC)
   1520 				printf("%s: reselect ID %02x w/active\n",
   1521 				    sc->sc_dev.dv_xname, reselid);
   1522 #endif
   1523 			TAILQ_INSERT_HEAD(&sc->ready_list,
   1524 			    sc->sc_nexus, chain);
   1525 			sc->sc_tinfo[periph->periph_target].lubusy
   1526 			    &= ~(1 << periph->periph_lun);
   1527 			sc->sc_active--;
   1528 		}
   1529 		/*
   1530 		 * locate acb of reselecting device
   1531 		 * set sc->sc_nexus to acb
   1532 		 */
   1533 		TAILQ_FOREACH(acb, &sc->nexus_list, chain) {
   1534 			struct scsipi_periph *periph = acb->xs->xs_periph;
   1535 			if (reselid != periph->periph_target ||
   1536 			    reselun != periph->periph_lun) {
   1537 				continue;
   1538 			}
   1539 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1540 			sc->sc_nexus = acb;
   1541 			sc->sc_flags |= acb->intstat;
   1542 			acb->intstat = 0;
   1543 			osiop_write_4(sc, OSIOP_DSA,
   1544 			    dsdma->dm_segs[0].ds_addr + acb->dsoffset);
   1545 			osiop_write_1(sc, OSIOP_SXFER,
   1546 			    sc->sc_sync[reselid].sxfer);
   1547 			osiop_write_1(sc, OSIOP_SBCL,
   1548 			    sc->sc_sync[reselid].sbcl);
   1549 			break;
   1550 		}
   1551 		if (acb == NULL) {
   1552 			printf("%s: target ID %02x reselect nexus_list %p\n",
   1553 			    sc->sc_dev.dv_xname, reselid,
   1554 			    TAILQ_FIRST(&sc->nexus_list));
   1555 			panic("unable to find reselecting device");
   1556 		}
   1557 
   1558 		osiop_write_4(sc, OSIOP_TEMP, 0);
   1559 		osiop_write_1(sc, OSIOP_DCNTL,
   1560 		    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1561 		return (0);
   1562 	}
   1563 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_connect) {
   1564 #ifdef OSIOP_DEBUG
   1565 		u_int8_t ctest2 = osiop_read_1(sc, OSIOP_CTEST2);
   1566 
   1567 		/* reselect was interrupted (by Sig_P or select) */
   1568 		if (osiop_debug & DEBUG_DISC ||
   1569 		    (ctest2 & OSIOP_CTEST2_SIGP) == 0)
   1570 			printf("%s: reselect interrupted (Sig_P?) "
   1571 			    "scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
   1572 			    sc->sc_dev.dv_xname,
   1573 			    osiop_read_1(sc, OSIOP_SCNTL1), ctest2,
   1574 			    osiop_read_1(sc, OSIOP_SFBR), istat,
   1575 			    osiop_read_1(sc, OSIOP_ISTAT));
   1576 #endif
   1577 		/* XXX assumes it was not select */
   1578 		if (sc->sc_nexus == NULL) {
   1579 #ifdef OSIOP_DEBUG
   1580 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
   1581 			    sc->sc_dev.dv_xname);
   1582 #if 0
   1583 			osiop_dump(sc);
   1584 #ifdef DDB
   1585 			Debugger();
   1586 #endif
   1587 #endif
   1588 #endif
   1589 			osiop_write_1(sc, OSIOP_DCNTL,
   1590 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1591 			return (0);
   1592 		}
   1593 		target = sc->sc_nexus->xs->xs_periph->periph_target;
   1594 		osiop_write_4(sc, OSIOP_TEMP, 0);
   1595 		osiop_write_4(sc, OSIOP_DSA,
   1596 		    dsdma->dm_segs[0].ds_addr + sc->sc_nexus->dsoffset);
   1597 		osiop_write_1(sc, OSIOP_SXFER, sc->sc_sync[target].sxfer);
   1598 		osiop_write_1(sc, OSIOP_SBCL, sc->sc_sync[target].sbcl);
   1599 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_scripts);
   1600 		return (0);
   1601 	}
   1602 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_msgin) {
   1603 		/* Unrecognized message in byte */
   1604 		if (acb == NULL)
   1605 			printf("%s: Bad message-in with no active command?\n",
   1606 			    sc->sc_dev.dv_xname);
   1607 		printf("%s: Unrecognized message in data "
   1608 		    "sfbr %x msg %x sbcl %x\n", sc->sc_dev.dv_xname,
   1609 		    osiop_read_1(sc, OSIOP_SFBR), ds->msgbuf[1],
   1610 		    osiop_read_1(sc, OSIOP_SBCL));
   1611 		/* what should be done here? */
   1612 		osiop_write_4(sc, OSIOP_DSP, scraddr + Ent_switch);
   1613 		bus_dmamap_sync(sc->sc_dmat, dsdma,
   1614 		    acb->dsoffset, sizeof(struct osiop_ds),
   1615 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1616 		return (0);
   1617 	}
   1618 	if (dstat & OSIOP_DSTAT_SIR && intcode == A_int_status) {
   1619 		/* Status phase wasn't followed by message in phase? */
   1620 		printf("%s: Status phase not followed by message in phase? "
   1621 		    "sbcl %x sbdl %x\n", sc->sc_dev.dv_xname,
   1622 		    osiop_read_1(sc, OSIOP_SBCL),
   1623 		    osiop_read_1(sc, OSIOP_SBDL));
   1624 		if (osiop_read_1(sc, OSIOP_SBCL) == 0xa7) {
   1625 			/* It is now, just continue the script? */
   1626 			osiop_write_1(sc, OSIOP_DCNTL,
   1627 			    osiop_read_1(sc, OSIOP_DCNTL) | OSIOP_DCNTL_STD);
   1628 			return (0);
   1629 		}
   1630 	}
   1631 	if (dstat & OSIOP_DSTAT_SIR && sstat0 == 0) {
   1632 		printf("OSIOP interrupt: %x sts %x msg %x %x sbcl %x\n",
   1633 		    intcode, ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
   1634 		    osiop_read_1(sc, OSIOP_SBCL));
   1635 		osiop_reset(sc);
   1636 		*status = SCSI_OSIOP_NOSTATUS;
   1637 		return (0);	/* osiop_reset has cleaned up */
   1638 	}
   1639 	if (sstat0 & OSIOP_SSTAT0_SGE)
   1640 		printf("%s: SCSI Gross Error\n", sc->sc_dev.dv_xname);
   1641 	if (sstat0 & OSIOP_SSTAT0_PAR)
   1642 		printf("%s: Parity Error\n", sc->sc_dev.dv_xname);
   1643 	if (dstat & OSIOP_DSTAT_IID)
   1644 		printf("%s: Invalid instruction detected\n",
   1645 		    sc->sc_dev.dv_xname);
   1646  bad_phase:
   1647 	/*
   1648 	 * temporary panic for unhandled conditions
   1649 	 * displays various things about the 53C710 status and registers
   1650 	 * then panics.
   1651 	 * XXXX need to clean this up to print out the info, reset, and continue
   1652 	 */
   1653 	printf("osiop_chkintr: target %x ds %p\n", target, ds);
   1654 	printf("scripts %lx ds %lx dsp %x dcmd %x\n", scraddr,
   1655 	    sc->sc_dsdma->dm_segs[0].ds_addr + acb->dsoffset,
   1656 	    osiop_read_4(sc, OSIOP_DSP),
   1657 	    osiop_read_4(sc, OSIOP_DBC));
   1658 	printf("osiop_chkintr: istat %x dstat %x sstat0 %x "
   1659 	    "dsps %x dsa %x sbcl %x sts %x msg %x %x sfbr %x\n",
   1660 	    istat, dstat, sstat0, intcode,
   1661 	    osiop_read_4(sc, OSIOP_DSA),
   1662 	    osiop_read_1(sc, OSIOP_SBCL),
   1663 	    ds->stat[0], ds->msgbuf[0], ds->msgbuf[1],
   1664 	    osiop_read_1(sc, OSIOP_SFBR));
   1665 #ifdef OSIOP_DEBUG
   1666 	if (osiop_debug & DEBUG_DMA)
   1667 		panic("osiop_chkintr: **** temp ****");
   1668 #endif
   1669 #ifdef DDB
   1670 	Debugger();
   1671 #endif
   1672 	osiop_reset(sc);	/* hard reset */
   1673 	*status = SCSI_OSIOP_NOSTATUS;
   1674 	acb->status = ACB_S_DONE;
   1675 	return (0);		/* osiop_reset cleaned up */
   1676 }
   1677 
   1678 void
   1679 osiop_select(sc)
   1680 	struct osiop_softc *sc;
   1681 {
   1682 	struct osiop_acb *acb = sc->sc_nexus;
   1683 
   1684 #ifdef OSIOP_DEBUG
   1685 	if (osiop_debug & DEBUG_CMD)
   1686 		printf("%s: select ", sc->sc_dev.dv_xname);
   1687 #endif
   1688 
   1689 	if (acb->xs->xs_control & XS_CTL_POLL || sc->sc_flags & OSIOP_NODMA) {
   1690 		sc->sc_flags |= OSIOP_INTSOFF;
   1691 		sc->sc_flags &= ~OSIOP_INTDEFER;
   1692 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
   1693 			osiop_write_1(sc, OSIOP_SIEN, 0);
   1694 			osiop_write_1(sc, OSIOP_DIEN, 0);
   1695 		}
   1696 #if 0
   1697 	} else if ((sc->sc_flags & OSIOP_INTDEFER) == 0) {
   1698 		sc->sc_flags &= ~OSIOP_INTSOFF;
   1699 		if ((osiop_read_1(sc, OSIOP_ISTAT) & OSIOP_ISTAT_CON) == 0) {
   1700 			osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
   1701 			osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
   1702 		}
   1703 #endif
   1704 	}
   1705 #ifdef OSIOP_DEBUG
   1706 	if (osiop_debug & DEBUG_CMD)
   1707 		printf("osiop_select: target %x cmd %02x ds %p\n",
   1708 		    acb->xs->xs_periph->periph_target,
   1709 		    acb->xs->cmd->opcode, sc->sc_nexus->ds);
   1710 #endif
   1711 
   1712 	osiop_start(sc);
   1713 
   1714 	return;
   1715 }
   1716 
   1717 /*
   1718  * 53C710 interrupt handler
   1719  */
   1720 
   1721 void
   1722 osiop_intr(sc)
   1723 	struct osiop_softc *sc;
   1724 {
   1725 	int status, s;
   1726 	u_int8_t istat, dstat, sstat0;
   1727 
   1728 	s = splbio();
   1729 
   1730 	istat = sc->sc_istat;
   1731 	if ((istat & (OSIOP_ISTAT_SIP | OSIOP_ISTAT_DIP)) == 0) {
   1732 		splx(s);
   1733 		return;
   1734 	}
   1735 
   1736 	/* Got a valid interrupt on this device; set by MD handler */
   1737 	dstat = sc->sc_dstat;
   1738 	sstat0 = sc->sc_sstat0;
   1739 	sc->sc_istat = 0;
   1740 #ifdef OSIOP_DEBUG
   1741 	if (!sc->sc_active) {
   1742 		/* XXX needs sync */
   1743 		printf("%s: spurious interrupt? "
   1744 		    "istat %x dstat %x sstat0 %x nexus %p status %x\n",
   1745 		    sc->sc_dev.dv_xname, istat, dstat, sstat0, sc->sc_nexus,
   1746 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0);
   1747 	}
   1748 #endif
   1749 
   1750 #ifdef OSIOP_DEBUG
   1751 	if (osiop_debug & (DEBUG_INT|DEBUG_CMD)) {
   1752 		/* XXX needs sync */
   1753 		printf("%s: intr istat %x dstat %x sstat0 %x dsps %x "
   1754 		    "sbcl %x dsp %x dcmd %x sts %x msg %x\n",
   1755 		    sc->sc_dev.dv_xname,
   1756 		    istat, dstat, sstat0,
   1757 		    osiop_read_4(sc, OSIOP_DSPS),
   1758 		    osiop_read_1(sc, OSIOP_SBCL),
   1759 		    osiop_read_4(sc, OSIOP_DSP),
   1760 		    osiop_read_4(sc, OSIOP_DBC),
   1761 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->stat[0] : 0,
   1762 		    (sc->sc_nexus != NULL) ? sc->sc_nexus->ds->msgbuf[0] : 0);
   1763 	}
   1764 #endif
   1765 	if (sc->sc_flags & OSIOP_INTDEFER) {
   1766 		sc->sc_flags &= ~(OSIOP_INTDEFER | OSIOP_INTSOFF);
   1767 		osiop_write_1(sc, OSIOP_SIEN, sc->sc_sien);
   1768 		osiop_write_1(sc, OSIOP_DIEN, sc->sc_dien);
   1769 	}
   1770 	if (osiop_checkintr(sc, istat, dstat, sstat0, &status)) {
   1771 #if 0
   1772 		if (status == SCSI_OSIOP_NOSTATUS)
   1773 			printf("osiop_intr: no valid status \n");
   1774 #endif
   1775 		if ((sc->sc_flags & (OSIOP_INTSOFF | OSIOP_INTDEFER)) !=
   1776 		    OSIOP_INTSOFF) {
   1777 #if 0
   1778 			if (osiop_read_1(sc, OSIOP_SBCL) & OSIOP_BSY) {
   1779 				struct scsipi_periph *periph;
   1780 
   1781 				periph = sc->sc_nexus->xs->xs_periph;
   1782 				printf("%s: SCSI bus busy at completion"
   1783 				    " targ %d sbcl %02x sfbr %x lcrc "
   1784 				    "%02x dsp +%x\n", sc->sc_dev.dv_xname,
   1785 				    periph->periphtarget,
   1786 				    osiop_read_1(sc, OSIOP_SBCL),
   1787 				    osiop_read_1(sc, OSIOP_SFBR),
   1788 				    osiop_read_1(sc, OSIOP_LCRC),
   1789 				    osiop_read_4(sc, OSIOP_DSP) -
   1790 				        sc->sc_scrdma->dm_segs[0].ds_addr);
   1791 			}
   1792 #endif
   1793 			osiop_scsidone(sc->sc_nexus, status);
   1794 		}
   1795 	}
   1796 	splx(s);
   1797 }
   1798 
   1799 /*
   1800  * This is based on the Progressive Peripherals 33Mhz Zeus driver and will
   1801  * not be correct for other 53c710 boards.
   1802  *
   1803  */
   1804 void
   1805 scsi_period_to_osiop(sc, target)
   1806 	struct osiop_softc *sc;
   1807 	int target;
   1808 {
   1809 	int period, offset, sxfer, sbcl = 0;
   1810 #ifdef DEBUG_SYNC
   1811 	int i;
   1812 #endif
   1813 
   1814 	period = sc->sc_nexus->ds->msgbuf[4];
   1815 	offset = sc->sc_nexus->ds->msgbuf[5];
   1816 #ifdef DEBUG_SYNC
   1817 	sxfer = 0;
   1818 	if (offset <= OSIOP_MAX_OFFSET)
   1819 		sxfer = offset;
   1820 	for (i = 0; i < sizeof(sync_tab) / sizeof(sync_tab[0]); i++) {
   1821 		if (period <= sync_tab[i].p) {
   1822 			sxfer |= sync_tab[i].r & 0x70;
   1823 			sbcl = sync_tab[i].r & 0x03;
   1824 			break;
   1825 		}
   1826 	}
   1827 	printf("osiop sync old: osiop_sxfr %02x, osiop_sbcl %02x\n",
   1828 	    sxfer, sbcl);
   1829 #endif
   1830 	for (sbcl = 1; sbcl < 4; sbcl++) {
   1831 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
   1832 		if (sxfer >= 0 && sxfer <= 7)
   1833 			break;
   1834 	}
   1835 	if (sbcl > 3) {
   1836 		printf("osiop sync: unable to compute sync params "
   1837 		    "for period %d ns\n", period * 4);
   1838 		/*
   1839 		 * XXX need to pick a value we can do and renegotiate
   1840 		 */
   1841 		sxfer = sbcl = 0;
   1842 	} else {
   1843 		sxfer = (sxfer << 4) | ((offset <= OSIOP_MAX_OFFSET) ?
   1844 		    offset : OSIOP_MAX_OFFSET);
   1845 #ifdef DEBUG_SYNC
   1846 		printf("osiop sync: params for period %dns: sxfer %x sbcl %x",
   1847 		    period * 4, sxfer, sbcl);
   1848 		printf(" actual period %dns\n",
   1849 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
   1850 #endif
   1851 	}
   1852 	sc->sc_sync[target].sxfer = sxfer;
   1853 	sc->sc_sync[target].sbcl = sbcl;
   1854 #ifdef DEBUG_SYNC
   1855 	printf("osiop sync: osiop_sxfr %02x, osiop_sbcl %02x\n", sxfer, sbcl);
   1856 #endif
   1857 }
   1858 
   1859 void
   1860 osiop_timeout(arg)
   1861 	void *arg;
   1862 {
   1863 	struct osiop_acb *acb = arg;
   1864 	struct scsipi_xfer *xs = acb->xs;
   1865 	struct osiop_softc *sc = acb->sc;
   1866 	int s;
   1867 
   1868 	scsipi_printaddr(xs->xs_periph);
   1869 	printf("command timeout\n");
   1870 
   1871 	s = splbio();
   1872 	/* reset the scsi bus */
   1873 	osiop_resetbus(sc);
   1874 
   1875 	/* deactivate callout */
   1876 	callout_stop(&xs->xs_callout);
   1877 	acb->flags |= ACB_F_TIMEOUT;
   1878 	osiop_reset(sc);
   1879 	splx(s);
   1880 	return;
   1881 }
   1882 
   1883 #ifdef OSIOP_DEBUG
   1884 
   1885 #if OSIOP_TRACE_SIZE
   1886 void
   1887 osiop_dump_trace()
   1888 {
   1889 	int i;
   1890 
   1891 	printf("osiop trace: next index %d\n", osiop_trix);
   1892 	i = osiop_trix;
   1893 	do {
   1894 		printf("%3d: '%c' %02x %02x %02x\n", i,
   1895 		    osiop_trbuf[i], osiop_trbuf[i + 1],
   1896 		    osiop_trbuf[i + 2], osiop_trbuf[i + 3]);
   1897 		i = (i + 4) & (OSIOP_TRACE_SIZE - 1);
   1898 	} while (i != osiop_trix);
   1899 }
   1900 #endif
   1901 
   1902 void
   1903 osiop_dump_acb(acb)
   1904 	struct osiop_acb *acb;
   1905 {
   1906 	u_int8_t *b;
   1907 	int i;
   1908 
   1909 	printf("acb@%p ", acb);
   1910 	if (acb->xs == NULL) {
   1911 		printf("<unused>\n");
   1912 		return;
   1913 	}
   1914 
   1915 	b = (u_int8_t *)&acb->xs->cmd;
   1916 	printf("(%d:%d) status %2x cmdlen %2ld cmd ",
   1917 	    acb->xs->xs_periph->periph_target,
   1918 	    acb->xs->xs_periph->periph_lun, acb->status, acb->cmdlen);
   1919 	for (i = acb->cmdlen; i > 0; i--)
   1920 		printf(" %02x", *b++);
   1921 	printf("\n");
   1922 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
   1923 	    acb->xs->datalen);
   1924 	printf("va %p:%lx ", acb->data, acb->datalen);
   1925 	printf("cur %lx:%lx\n", acb->curaddr, acb->curlen);
   1926 }
   1927 
   1928 void
   1929 osiop_dump(sc)
   1930 	struct osiop_softc *sc;
   1931 {
   1932 	struct osiop_acb *acb;
   1933 	int i, s;
   1934 
   1935 	s = splbio();
   1936 #if OSIOP_TRACE_SIZE
   1937 	osiop_dump_trace();
   1938 #endif
   1939 	printf("%s@%p istat %02x\n",
   1940 	    sc->sc_dev.dv_xname, sc, osiop_read_1(sc, OSIOP_ISTAT));
   1941 	if ((acb = TAILQ_FIRST(&sc->free_list)) != NULL) {
   1942 		printf("Free list:\n");
   1943 		while (acb) {
   1944 			osiop_dump_acb(acb);
   1945 			acb = TAILQ_NEXT(acb, chain);
   1946 		}
   1947 	}
   1948 	if ((acb = TAILQ_FIRST(&sc->ready_list)) != NULL) {
   1949 		printf("Ready list:\n");
   1950 		while (acb) {
   1951 			osiop_dump_acb(acb);
   1952 			acb = TAILQ_NEXT(acb, chain);
   1953 		}
   1954 	}
   1955 	if ((acb = TAILQ_FIRST(&sc->nexus_list)) != NULL) {
   1956 		printf("Nexus list:\n");
   1957 		while (acb) {
   1958 			osiop_dump_acb(acb);
   1959 			acb = TAILQ_NEXT(acb, chain);
   1960 		}
   1961 	}
   1962 	if (sc->sc_nexus) {
   1963 		printf("Nexus:\n");
   1964 		osiop_dump_acb(sc->sc_nexus);
   1965 	}
   1966 	for (i = 0; i < OSIOP_NTGT; i++) {
   1967 		if (sc->sc_tinfo[i].cmds > 2) {
   1968 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
   1969 			    i, sc->sc_tinfo[i].cmds,
   1970 			    sc->sc_tinfo[i].dconns,
   1971 			    sc->sc_tinfo[i].lubusy);
   1972 		}
   1973 	}
   1974 	splx(s);
   1975 }
   1976 #endif
   1977