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