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