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