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