Home | History | Annotate | Line # | Download | only in dev
siop.c revision 1.68
      1 /*	$NetBSD: siop.c,v 1.67 2012/10/27 17:17:31 chs Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1990 The Regents of the University of California.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Van Jacobson of Lawrence Berkeley Laboratory.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     35  */
     36 
     37 /*
     38  * Copyright (c) 1994 Michael L. Hitch
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Van Jacobson of Lawrence Berkeley Laboratory.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  *
     63  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     64  */
     65 
     66 /*
     67  * AMIGA 53C710 scsi adaptor driver
     68  */
     69 
     70 #include "opt_ddb.h"
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: siop.c,v 1.67 2012/10/27 17:17:31 chs Exp $");
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/callout.h>
     78 #include <sys/kernel.h>
     79 #include <sys/device.h>
     80 #include <sys/disklabel.h>
     81 #include <sys/buf.h>
     82 #include <sys/malloc.h>
     83 
     84 #include <dev/scsipi/scsi_all.h>
     85 #include <dev/scsipi/scsipi_all.h>
     86 #include <dev/scsipi/scsiconf.h>
     87 #include <machine/cpu.h>
     88 #ifdef __m68k__
     89 #include <m68k/cacheops.h>
     90 #else
     91 #define DCIAS(pa)
     92 #endif
     93 #include <amiga/amiga/custom.h>
     94 #include <amiga/amiga/isr.h>
     95 #include <amiga/dev/siopreg.h>
     96 #include <amiga/dev/siopvar.h>
     97 
     98 /*
     99  * SCSI delays
    100  * In u-seconds, primarily for state changes on the SPC.
    101  */
    102 #define	SCSI_CMD_WAIT	500000	/* wait per step of 'immediate' cmds */
    103 #define	SCSI_DATA_WAIT	500000	/* wait per data in/out step */
    104 #define	SCSI_INIT_WAIT	500000	/* wait per step (both) during init */
    105 
    106 void siop_select(struct siop_softc *);
    107 void siopabort(struct siop_softc *, siop_regmap_p, const char *);
    108 void sioperror(struct siop_softc *, siop_regmap_p, u_char);
    109 void siopstart(struct siop_softc *);
    110 int  siop_checkintr(struct siop_softc *, u_char, u_char, u_char, int *);
    111 void siopreset(struct siop_softc *);
    112 void siopsetdelay(int);
    113 void siop_scsidone(struct siop_acb *, int);
    114 void siop_timeout(void *);
    115 void siop_sched(struct siop_softc *);
    116 void siop_poll(struct siop_softc *, struct siop_acb *);
    117 void siopintr(struct siop_softc *);
    118 void scsi_period_to_siop(struct siop_softc *, int);
    119 void siop_start(struct siop_softc *, int, int, u_char *, int, u_char *, int);
    120 void siop_dump_acb(struct siop_acb *);
    121 
    122 /* 53C710 script */
    123 const
    124 #include <amiga/dev/siop_script.out>
    125 
    126 /* default to not inhibit sync negotiation on any drive */
    127 u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
    128 u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3};
    129 int siop_no_dma = 0;
    130 
    131 int siop_reset_delay = 250;	/* delay after reset, in milleseconds */
    132 
    133 int siop_cmd_wait = SCSI_CMD_WAIT;
    134 int siop_data_wait = SCSI_DATA_WAIT;
    135 int siop_init_wait = SCSI_INIT_WAIT;
    136 
    137 #ifdef DEBUG_SYNC
    138 /*
    139  * sync period transfer lookup - only valid for 66 MHz clock
    140  */
    141 static struct {
    142 	unsigned char p;	/* period from sync request message */
    143 	unsigned char r;	/* siop_period << 4 | sbcl */
    144 } sync_tab[] = {
    145 	{ 60/4, 0<<4 | 1},
    146 	{ 76/4, 1<<4 | 1},
    147 	{ 92/4, 2<<4 | 1},
    148 	{ 92/4, 0<<4 | 2},
    149 	{108/4, 3<<4 | 1},
    150 	{116/4, 1<<4 | 2},
    151 	{120/4, 4<<4 | 1},
    152 	{120/4, 0<<4 | 3},
    153 	{136/4, 5<<4 | 1},
    154 	{140/4, 2<<4 | 2},
    155 	{152/4, 6<<4 | 1},
    156 	{152/4, 1<<4 | 3},
    157 	{164/4, 3<<4 | 2},
    158 	{168/4, 7<<4 | 1},
    159 	{180/4, 2<<4 | 3},
    160 	{184/4, 4<<4 | 2},
    161 	{208/4, 5<<4 | 2},
    162 	{212/4, 3<<4 | 3},
    163 	{232/4, 6<<4 | 2},
    164 	{240/4, 4<<4 | 3},
    165 	{256/4, 7<<4 | 2},
    166 	{272/4, 5<<4 | 3},
    167 	{300/4, 6<<4 | 3},
    168 	{332/4, 7<<4 | 3}
    169 };
    170 #endif
    171 
    172 #ifdef DEBUG
    173 /*
    174  *	0x01 - full debug
    175  *	0x02 - DMA chaining
    176  *	0x04 - siopintr
    177  *	0x08 - phase mismatch
    178  *	0x10 - <not used>
    179  *	0x20 - panic on unhandled exceptions
    180  *	0x100 - disconnect/reselect
    181  */
    182 int	siop_debug = 0;
    183 int	siopsync_debug = 0;
    184 int	siopdma_hits = 0;
    185 int	siopdma_misses = 0;
    186 int	siopchain_ints = 0;
    187 int	siopstarts = 0;
    188 int	siopints = 0;
    189 int	siopphmm = 0;
    190 #define SIOP_TRACE_SIZE	128
    191 #define SIOP_TRACE(a,b,c,d) \
    192 	siop_trbuf[siop_trix] = (a); \
    193 	siop_trbuf[siop_trix+1] = (b); \
    194 	siop_trbuf[siop_trix+2] = (c); \
    195 	siop_trbuf[siop_trix+3] = (d); \
    196 	siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1);
    197 u_char	siop_trbuf[SIOP_TRACE_SIZE];
    198 int	siop_trix;
    199 void siop_dump(struct siop_softc *);
    200 void siop_dump_trace(void);
    201 #else
    202 #define SIOP_TRACE(a,b,c,d)
    203 #endif
    204 
    205 
    206 /*
    207  * default minphys routine for siop based controllers
    208  */
    209 void
    210 siop_minphys(struct buf *bp)
    211 {
    212 
    213 	/*
    214 	 * No max transfer at this level.
    215 	 */
    216 	minphys(bp);
    217 }
    218 
    219 /*
    220  * used by specific siop controller
    221  *
    222  */
    223 void
    224 siop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    225                     void *arg)
    226 {
    227 	struct scsipi_xfer *xs;
    228 #ifdef DIAGNOSTIC
    229 	struct scsipi_periph *periph;
    230 #endif
    231 	struct siop_acb *acb;
    232 	struct siop_softc *sc = device_private(chan->chan_adapter->adapt_dev);
    233 	int flags, s;
    234 
    235 	switch (req) {
    236 	case ADAPTER_REQ_RUN_XFER:
    237 		xs = arg;
    238 #ifdef DIAGNOSTIC
    239 		periph = xs->xs_periph;
    240 #endif
    241 		flags = xs->xs_control;
    242 
    243 		/* XXXX ?? */
    244 		if (flags & XS_CTL_DATA_UIO)
    245 			panic("siop: scsi data uio requested");
    246 
    247 		/* XXXX ?? */
    248 		if (sc->sc_nexus && flags & XS_CTL_POLL)
    249 /*			panic("siop_scsicmd: busy");*/
    250 			printf("siop_scsicmd: busy\n");
    251 
    252 		s = splbio();
    253 		acb = sc->free_list.tqh_first;
    254 		if (acb) {
    255 			TAILQ_REMOVE(&sc->free_list, acb, chain);
    256 		}
    257 		splx(s);
    258 
    259 #ifdef DIAGNOSTIC
    260 		/*
    261 		 * This should never happen as we track the resources
    262 		 * in the mid-layer.
    263 		 */
    264 		if (acb == NULL) {
    265 			scsipi_printaddr(periph);
    266 			printf("unable to allocate acb\n");
    267 			panic("siop_scsipi_request");
    268 		}
    269 #endif
    270 
    271 		acb->flags = ACB_ACTIVE;
    272 		acb->xs = xs;
    273 		memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
    274 		acb->clen = xs->cmdlen;
    275 		acb->daddr = xs->data;
    276 		acb->dleft = xs->datalen;
    277 
    278 		s = splbio();
    279 		TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    280 
    281 		if (sc->sc_nexus == NULL)
    282 			siop_sched(sc);
    283 
    284 		splx(s);
    285 
    286 		if (flags & XS_CTL_POLL || siop_no_dma)
    287 			siop_poll(sc, acb);
    288 		return;
    289 
    290 	case ADAPTER_REQ_GROW_RESOURCES:
    291 		return;
    292 
    293 	case ADAPTER_REQ_SET_XFER_MODE:
    294 		return;
    295 	}
    296 }
    297 
    298 void
    299 siop_poll(struct siop_softc *sc, struct siop_acb *acb)
    300 {
    301 	siop_regmap_p rp = sc->sc_siopp;
    302 	struct scsipi_xfer *xs = acb->xs;
    303 	int i;
    304 	int status;
    305 	u_char istat;
    306 	u_char dstat;
    307 	u_char sstat0;
    308 	int s;
    309 	int to;
    310 
    311 	s = splbio();
    312 	to = xs->timeout / 1000;
    313 	if (sc->nexus_list.tqh_first)
    314 		printf("%s: siop_poll called with disconnected device\n",
    315 		    device_xname(sc->sc_dev));
    316 	for (;;) {
    317 		/* use cmd_wait values? */
    318 		i = 50000;
    319 		/* XXX spl0(); */
    320 		while (((istat = rp->siop_istat) &
    321 		    (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
    322 			if (--i <= 0) {
    323 #ifdef DEBUG
    324 				printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
    325 				    xs->xs_periph->periph_target, acb->cmd.opcode,
    326 				    rp->siop_sbcl, rp->siop_dsp,
    327 				    rp->siop_dsp - sc->sc_scriptspa,
    328 				    *((volatile long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
    329 #endif
    330 				i = 50000;
    331 				--to;
    332 				if (to <= 0) {
    333 					siopreset(sc);
    334 					return;
    335 				}
    336 			}
    337 			delay(20);
    338 		}
    339 		sstat0 = rp->siop_sstat0;
    340 		dstat = rp->siop_dstat;
    341 		if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
    342 			if (acb != sc->sc_nexus)
    343 				printf("%s: siop_poll disconnected device completed\n",
    344 				    device_xname(sc->sc_dev));
    345 			else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
    346 				sc->sc_flags &= ~SIOP_INTSOFF;
    347 				rp->siop_sien = sc->sc_sien;
    348 				rp->siop_dien = sc->sc_dien;
    349 			}
    350 			siop_scsidone(sc->sc_nexus, status);
    351 		}
    352 
    353 		if (xs->xs_status & XS_STS_DONE)
    354 			break;
    355 	}
    356 	splx(s);
    357 }
    358 
    359 /*
    360  * start next command that's ready
    361  */
    362 void
    363 siop_sched(struct siop_softc *sc)
    364 {
    365 	struct scsipi_periph *periph;
    366 	struct siop_acb *acb;
    367 	int i;
    368 
    369 #ifdef DEBUG
    370 	if (sc->sc_nexus) {
    371 		printf("%s: siop_sched- nexus %p/%d ready %p/%d\n",
    372 		    device_xname(sc->sc_dev), sc->sc_nexus,
    373 		    sc->sc_nexus->xs->xs_periph->periph_target,
    374 		    sc->ready_list.tqh_first,
    375 		    sc->ready_list.tqh_first->xs->xs_periph->periph_target);
    376 		return;
    377 	}
    378 #endif
    379 	for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
    380 		periph = acb->xs->xs_periph;
    381 		i = periph->periph_target;
    382 		if(!(sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
    383 			struct siop_tinfo *ti = &sc->sc_tinfo[i];
    384 
    385 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    386 			sc->sc_nexus = acb;
    387 			periph = acb->xs->xs_periph;
    388 			ti = &sc->sc_tinfo[periph->periph_target];
    389 			ti->lubusy |= (1 << periph->periph_lun);
    390 			break;
    391 		}
    392 	}
    393 
    394 	if (acb == NULL) {
    395 #ifdef DEBUGXXX
    396 		printf("%s: siop_sched didn't find ready command\n",
    397 		    device_xname(sc->sc_dev));
    398 #endif
    399 		return;
    400 	}
    401 
    402 	if (acb->xs->xs_control & XS_CTL_RESET)
    403 		siopreset(sc);
    404 
    405 #if 0
    406 	acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5;	/* XXXX */
    407 #endif
    408 	++sc->sc_active;
    409 	siop_select(sc);
    410 }
    411 
    412 void
    413 siop_scsidone(struct siop_acb *acb, int stat)
    414 {
    415 	struct scsipi_xfer *xs;
    416 	struct scsipi_periph *periph;
    417 	struct siop_softc *sc;
    418 	int dosched = 0;
    419 
    420 	if (acb == NULL || (xs = acb->xs) == NULL) {
    421 #ifdef DIAGNOSTIC
    422 		printf("siop_scsidone: NULL acb or scsipi_xfer\n");
    423 #if defined(DEBUG) && defined(DDB)
    424 		Debugger();
    425 #endif
    426 #endif
    427 		return;
    428 	}
    429 
    430 	callout_stop(&xs->xs_callout);
    431 
    432 	periph = xs->xs_periph;
    433 	sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
    434 
    435 	xs->status = stat;
    436 	xs->resid = 0;		/* XXXX */
    437 
    438 	if (xs->error == XS_NOERROR) {
    439 		if (stat == SCSI_CHECK || stat == SCSI_BUSY)
    440 			xs->error = XS_BUSY;
    441 	}
    442 
    443 	/*
    444 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    445 	 * a hack to figure out which queue it's on.  Note that it is *not*
    446 	 * necessary to cdr down the ready queue, but we must cdr down the
    447 	 * nexus queue and see if it's there, so we can mark the unit as no
    448 	 * longer busy.  This code is sickening, but it works.
    449 	 */
    450 	if (acb == sc->sc_nexus) {
    451 		sc->sc_nexus = NULL;
    452 		sc->sc_tinfo[periph->periph_target].lubusy &=
    453 			~(1<<periph->periph_lun);
    454 		if (sc->ready_list.tqh_first)
    455 			dosched = 1;	/* start next command */
    456 		--sc->sc_active;
    457 		SIOP_TRACE('d','a',stat,0)
    458 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
    459 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    460 		SIOP_TRACE('d','r',stat,0)
    461 	} else {
    462 		register struct siop_acb *acb2;
    463 		for (acb2 = sc->nexus_list.tqh_first; acb2;
    464 		    acb2 = acb2->chain.tqe_next)
    465 			if (acb2 == acb) {
    466 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    467 				sc->sc_tinfo[periph->periph_target].lubusy
    468 					&= ~(1<<periph->periph_lun);
    469 				--sc->sc_active;
    470 				break;
    471 			}
    472 		if (acb2)
    473 			;
    474 		else if (acb->chain.tqe_next) {
    475 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    476 			--sc->sc_active;
    477 		} else {
    478 			printf("%s: can't find matching acb\n",
    479 			    device_xname(sc->sc_dev));
    480 #ifdef DDB
    481 /*			Debugger(); */
    482 #endif
    483 		}
    484 		SIOP_TRACE('d','n',stat,0);
    485 	}
    486 	/* Put it on the free list. */
    487 	acb->flags = ACB_FREE;
    488 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    489 
    490 	sc->sc_tinfo[periph->periph_target].cmds++;
    491 
    492 	scsipi_done(xs);
    493 
    494 	if (dosched && sc->sc_nexus == NULL)
    495 		siop_sched(sc);
    496 }
    497 
    498 void
    499 siopabort(register struct siop_softc *sc, siop_regmap_p rp, const char *where)
    500 {
    501 #ifdef fix_this
    502 	int i;
    503 #endif
    504 
    505 	printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    506 	    device_xname(sc->sc_dev),
    507 	    where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
    508 
    509 	if (sc->sc_active > 0) {
    510 #ifdef TODO
    511       SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
    512       WAIT_CIP (rp);
    513 
    514       GET_SBIC_asr (rp, asr);
    515       if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
    516         {
    517           /* ok, get more drastic.. */
    518 
    519 	  SET_SBIC_cmd (rp, SBIC_CMD_RESET);
    520 	  delay(25);
    521 	  SBIC_WAIT(rp, SBIC_ASR_INT, 0);
    522 	  GET_SBIC_csr (rp, csr);       /* clears interrupt also */
    523 
    524           return;
    525         }
    526 
    527       do
    528         {
    529           SBIC_WAIT (rp, SBIC_ASR_INT, 0);
    530           GET_SBIC_csr (rp, csr);
    531         }
    532       while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    533 	      && (csr != SBIC_CSR_CMD_INVALID));
    534 #endif
    535 
    536 		/* lets just hope it worked.. */
    537 #ifdef fix_this
    538 		for (i = 0; i < 2; ++i) {
    539 			if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
    540 			    sc->sc_cur) {
    541 				printf ("siopabort: cleanup!\n");
    542 				sc->sc_iob[i].sc_xs = NULL;
    543 			}
    544 		}
    545 #endif	/* fix_this */
    546 /*		sc->sc_active = 0; */
    547 	}
    548 }
    549 
    550 void
    551 siopinitialize(struct siop_softc *sc)
    552 {
    553 	int i;
    554 	u_int inhibit_sync;
    555 	extern u_long scsi_nosync;
    556 	extern int shift_nosync;
    557 
    558 	/*
    559 	 * Need to check that scripts is on a long word boundary
    560 	 * Also should verify that dev doesn't span non-contiguous
    561 	 * physical pages.
    562 	 */
    563 	sc->sc_scriptspa = kvtop((void *)__UNCONST(scripts));
    564 
    565 	/*
    566 	 * malloc sc_acb to ensure that DS is on a long word boundary.
    567 	 */
    568 
    569 	sc->sc_acb = malloc(sizeof(struct siop_acb) * SIOP_NACB,
    570 		M_DEVBUF, M_NOWAIT);
    571 	if (sc->sc_acb == NULL)
    572 		panic("siopinitialize: ACB malloc failed!");
    573 
    574 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
    575 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
    576 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
    577 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
    578 	if (sc->sc_minsync < 25)
    579 		sc->sc_minsync = 25;
    580 	if (sc->sc_clock_freq <= 25) {
    581 		sc->sc_dcntl |= 0x80;		/* SCLK/1 */
    582 		sc->sc_tcp[0] = sc->sc_tcp[1];
    583 	} else if (sc->sc_clock_freq <= 37) {
    584 		sc->sc_dcntl |= 0x40;		/* SCLK/1.5 */
    585 		sc->sc_tcp[0] = sc->sc_tcp[2];
    586 	} else if (sc->sc_clock_freq <= 50) {
    587 		sc->sc_dcntl |= 0x00;		/* SCLK/2 */
    588 		sc->sc_tcp[0] = sc->sc_tcp[3];
    589 	} else {
    590 		sc->sc_dcntl |= 0xc0;		/* SCLK/3 */
    591 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
    592 	}
    593 
    594 	if (scsi_nosync) {
    595 		inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
    596 		shift_nosync += 8;
    597 #ifdef DEBUG
    598 		if (inhibit_sync)
    599 			printf("%s: Inhibiting synchronous transfer %02x\n",
    600 				device_xname(sc->sc_dev), inhibit_sync);
    601 #endif
    602 		for (i = 0; i < 8; ++i)
    603 			if (inhibit_sync & (1 << i))
    604 				siop_inhibit_sync[i] = 1;
    605 	}
    606 
    607 	siopreset (sc);
    608 }
    609 
    610 void
    611 siop_timeout(void *arg)
    612 {
    613 	struct siop_acb *acb;
    614 	struct scsipi_periph *periph;
    615 	struct siop_softc *sc;
    616 	int s;
    617 
    618 	acb = arg;
    619 	periph = acb->xs->xs_periph;
    620 	sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
    621 	scsipi_printaddr(periph);
    622 	printf("timed out\n");
    623 
    624 	s = splbio();
    625 
    626 	acb->xs->error = XS_TIMEOUT;
    627 	siopreset(sc);
    628 
    629 	splx(s);
    630 }
    631 
    632 void
    633 siopreset(struct siop_softc *sc)
    634 {
    635 	siop_regmap_p rp;
    636 	u_int i, s;
    637 	u_char  dummy;
    638 	struct siop_acb *acb;
    639 
    640 	rp = sc->sc_siopp;
    641 
    642 	if (sc->sc_flags & SIOP_ALIVE)
    643 		siopabort(sc, rp, "reset");
    644 
    645 	printf("%s: ", device_xname(sc->sc_dev));		/* XXXX */
    646 
    647 	s = splbio();
    648 
    649 	/*
    650 	 * Reset the chip
    651 	 * XXX - is this really needed?
    652 	 */
    653 	rp->siop_istat |= SIOP_ISTAT_ABRT;	/* abort current script */
    654 	rp->siop_istat |= SIOP_ISTAT_RST;		/* reset chip */
    655 	rp->siop_istat &= ~SIOP_ISTAT_RST;
    656 	/*
    657 	 * Reset SCSI bus (do we really want this?)
    658 	 */
    659 	rp->siop_sien = 0;
    660 	rp->siop_scntl1 |= SIOP_SCNTL1_RST;
    661 	delay(1);
    662 	rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
    663 
    664 	/*
    665 	 * Set up various chip parameters
    666 	 */
    667 	rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
    668 	rp->siop_scntl1 = SIOP_SCNTL1_ESR;
    669 	rp->siop_dcntl = sc->sc_dcntl;
    670 	rp->siop_dmode = 0x80;	/* burst length = 4 */
    671 	rp->siop_sien = 0x00;	/* don't enable interrupts yet */
    672 	rp->siop_dien = 0x00;	/* don't enable interrupts yet */
    673 	rp->siop_scid = 1 << sc->sc_channel.chan_id;
    674 	rp->siop_dwt = 0x00;
    675 	rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
    676 	rp->siop_ctest7 |= sc->sc_ctest7;
    677 
    678 	/* will need to re-negotiate sync xfers */
    679 	memset(&sc->sc_sync, 0, sizeof (sc->sc_sync));
    680 
    681 	i = rp->siop_istat;
    682 	if (i & SIOP_ISTAT_SIP)
    683 		dummy = rp->siop_sstat0;
    684 	if (i & SIOP_ISTAT_DIP)
    685 		dummy = rp->siop_dstat;
    686 
    687 	__USE(dummy);
    688 	splx (s);
    689 
    690 	delay (siop_reset_delay * 1000);
    691 	printf("siop id %d reset V%d\n", sc->sc_channel.chan_id,
    692 	    rp->siop_ctest8 >> 4);
    693 
    694 	if ((sc->sc_flags & SIOP_ALIVE) == 0) {
    695 		TAILQ_INIT(&sc->ready_list);
    696 		TAILQ_INIT(&sc->nexus_list);
    697 		TAILQ_INIT(&sc->free_list);
    698 		sc->sc_nexus = NULL;
    699 		acb = sc->sc_acb;
    700 		memset(acb, 0, sizeof(struct siop_acb) * SIOP_NACB);
    701 		for (i = 0; i < SIOP_NACB; i++) {
    702 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    703 			acb++;
    704 		}
    705 		memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
    706 	} else {
    707 		if (sc->sc_nexus != NULL) {
    708 			sc->sc_nexus->xs->error = XS_RESET;
    709 			siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
    710 		}
    711 		while ((acb = sc->nexus_list.tqh_first) > 0) {
    712 			acb->xs->error = XS_RESET;
    713 			siop_scsidone(acb, acb->stat[0]);
    714 		}
    715 	}
    716 
    717 	sc->sc_flags |= SIOP_ALIVE;
    718 	sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
    719 	/* enable SCSI and DMA interrupts */
    720 	sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
    721 	    SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
    722 	sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
    723 	    /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
    724 	rp->siop_sien = sc->sc_sien;
    725 	rp->siop_dien = sc->sc_dien;
    726 }
    727 
    728 /*
    729  * Setup Data Storage for 53C710 and start SCRIPTS processing
    730  */
    731 
    732 void
    733 siop_start(struct siop_softc *sc, int target, int lun, u_char *cbuf, int clen,
    734            u_char *buf, int len)
    735 {
    736 	siop_regmap_p rp = sc->sc_siopp;
    737 	int nchain;
    738 	int count, tcount;
    739 	char *addr, *dmaend;
    740 	struct siop_acb *acb = sc->sc_nexus;
    741 #ifdef DEBUG
    742 	int i;
    743 #endif
    744 
    745 #ifdef DEBUG
    746 	if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
    747 		printf ("ACK! siop was busy: rp %p script %p dsa %p active %ld\n",
    748 		    rp, &scripts, &acb->ds, sc->sc_active);
    749 		printf ("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
    750 		    rp->siop_istat, rp->siop_sfbr, rp->siop_lcrc,
    751 		    rp->siop_sien, rp->siop_dien);
    752 #ifdef DDB
    753 		/*Debugger();*/
    754 #endif
    755 	}
    756 #endif
    757 	acb->msgout[0] = MSG_IDENTIFY | lun;
    758 	if (siop_allow_disc[target] & 2 ||
    759 	    (siop_allow_disc[target] && len == 0))
    760 		acb->msgout[0] = MSG_IDENTIFY_DR | lun;
    761 	acb->status = 0;
    762 	acb->stat[0] = -1;
    763 	acb->msg[0] = -1;
    764 	acb->ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].sxfer << 8);
    765 	acb->ds.idlen = 1;
    766 	acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]);
    767 	acb->ds.cmdlen = clen;
    768 	acb->ds.cmdbuf = (char *) kvtop(cbuf);
    769 	acb->ds.stslen = 1;
    770 	acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]);
    771 	acb->ds.msglen = 1;
    772 	acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]);
    773 	acb->msg[1] = -1;
    774 	acb->ds.msginlen = 1;
    775 	acb->ds.extmsglen = 1;
    776 	acb->ds.synmsglen = 3;
    777 	acb->ds.msginbuf = (char *) kvtop(&acb->msg[1]);
    778 	acb->ds.extmsgbuf = (char *) kvtop(&acb->msg[2]);
    779 	acb->ds.synmsgbuf = (char *) kvtop(&acb->msg[3]);
    780 	memset(&acb->ds.chain, 0, sizeof (acb->ds.chain));
    781 
    782 	/*
    783 	 * Negotiate wide is the initial negotiation state;  since the 53c710
    784 	 * doesn't do wide transfers, just begin the synchronous transfer
    785 	 * negotation here.
    786 	 */
    787 	if (sc->sc_sync[target].state == NEG_WIDE) {
    788 		if (siop_inhibit_sync[target]) {
    789 			sc->sc_sync[target].state = NEG_DONE;
    790 			sc->sc_sync[target].sbcl = 0;
    791 			sc->sc_sync[target].sxfer = 0;
    792 #ifdef DEBUG
    793 			if (siopsync_debug)
    794 				printf ("Forcing target %d asynchronous\n", target);
    795 #endif
    796 		}
    797 		else {
    798 			acb->msg[2] = -1;
    799 			acb->msgout[1] = MSG_EXT_MESSAGE;
    800 			acb->msgout[2] = 3;
    801 			acb->msgout[3] = MSG_SYNC_REQ;
    802 #ifdef MAXTOR_SYNC_KLUDGE
    803 			acb->msgout[4] = 50 / 4;	/* ask for ridiculous period */
    804 #else
    805 			acb->msgout[4] = sc->sc_minsync;
    806 #endif
    807 			acb->msgout[5] = SIOP_MAX_OFFSET;
    808 			acb->ds.idlen = 6;
    809 			sc->sc_sync[target].state = NEG_WAITS;
    810 #ifdef DEBUG
    811 			if (siopsync_debug)
    812 				printf ("Sending sync request to target %d\n", target);
    813 #endif
    814 		}
    815 	}
    816 
    817 /*
    818  * Build physical DMA addresses for scatter/gather I/O
    819  */
    820 	acb->iob_buf = buf;
    821 	acb->iob_len = len;
    822 	acb->iob_curbuf = acb->iob_curlen = 0;
    823 	nchain = 0;
    824 	count = len;
    825 	addr = buf;
    826 	dmaend = NULL;
    827 	while (count > 0) {
    828 		acb->ds.chain[nchain].databuf = (char *) kvtop (addr);
    829 		if (count < (tcount = PAGE_SIZE - ((int) addr & PGOFSET)))
    830 			tcount = count;
    831 		acb->ds.chain[nchain].datalen = tcount;
    832 		addr += tcount;
    833 		count -= tcount;
    834 		if (acb->ds.chain[nchain].databuf == dmaend) {
    835 			dmaend += acb->ds.chain[nchain].datalen;
    836 			acb->ds.chain[nchain].datalen = 0;
    837 			acb->ds.chain[--nchain].datalen += tcount;
    838 #ifdef DEBUG
    839 			++siopdma_hits;
    840 #endif
    841 		}
    842 		else {
    843 			dmaend = acb->ds.chain[nchain].databuf +
    844 			    acb->ds.chain[nchain].datalen;
    845 			acb->ds.chain[nchain].datalen = tcount;
    846 #ifdef DEBUG
    847 			if (nchain)	/* Don't count miss on first one */
    848 				++siopdma_misses;
    849 #endif
    850 		}
    851 		++nchain;
    852 	}
    853 #ifdef DEBUG
    854 	if (nchain != 1 && len != 0 && siop_debug & 3) {
    855 		printf ("DMA chaining set: %d\n", nchain);
    856 		for (i = 0; i < nchain; ++i) {
    857 			printf ("  [%d] %8p %lx\n", i, acb->ds.chain[i].databuf,
    858 			    acb->ds.chain[i].datalen);
    859 		}
    860 	}
    861 #endif
    862 
    863 	/* push data cache for all data the 53c710 needs to access */
    864 	dma_cachectl ((void *)acb, sizeof (struct siop_acb));
    865 	dma_cachectl (cbuf, clen);
    866 	if (buf != NULL && len != 0)
    867 		dma_cachectl (buf, len);
    868 #ifdef DEBUG
    869 	if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
    870 		printf ("ACK! siop was busy at start: rp %p script %p dsa %p active %ld\n",
    871 		    rp, &scripts, &acb->ds, sc->sc_active);
    872 #ifdef DDB
    873 		/*Debugger();*/
    874 #endif
    875 	}
    876 #endif
    877 	if (sc->nexus_list.tqh_first == NULL) {
    878 		callout_reset(&acb->xs->xs_callout,
    879 		    mstohz(acb->xs->timeout) + 1, siop_timeout, acb);
    880 		if (rp->siop_istat & SIOP_ISTAT_CON)
    881 			printf("%s: siop_select while connected?\n",
    882 			    device_xname(sc->sc_dev));
    883 		rp->siop_temp = 0;
    884 		rp->siop_sbcl = sc->sc_sync[target].sbcl;
    885 		rp->siop_dsa = kvtop((void *)&acb->ds);
    886 		rp->siop_dsp = sc->sc_scriptspa;
    887 		SIOP_TRACE('s',1,0,0)
    888 	} else {
    889 		if ((rp->siop_istat & SIOP_ISTAT_CON) == 0) {
    890 			rp->siop_istat = SIOP_ISTAT_SIGP;
    891 			SIOP_TRACE('s',2,0,0);
    892 		}
    893 		else {
    894 			SIOP_TRACE('s',3,rp->siop_istat,0);
    895 		}
    896 	}
    897 #ifdef DEBUG
    898 	++siopstarts;
    899 #endif
    900 }
    901 
    902 /*
    903  * Process a DMA or SCSI interrupt from the 53C710 SIOP
    904  */
    905 
    906 int
    907 siop_checkintr(struct siop_softc *sc, u_char istat, u_char dstat,
    908                u_char sstat0, int *status)
    909 {
    910 	siop_regmap_p rp = sc->sc_siopp;
    911 	struct siop_acb *acb = sc->sc_nexus;
    912 	int	target = 0;
    913 	int	dfifo, dbc, sstat1;
    914 
    915 	dfifo = rp->siop_dfifo;
    916 	dbc = rp->siop_dbc0;
    917 	sstat1 = rp->siop_sstat1;
    918 	rp->siop_ctest8 |= SIOP_CTEST8_CLF;
    919 	while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT)
    920 		;
    921 	rp->siop_ctest8 &= ~SIOP_CTEST8_CLF;
    922 #ifdef DEBUG
    923 	++siopints;
    924 #if 0
    925 	if (siop_debug & 0x100) {
    926 		DCIAS(&acb->stat[0]);	/* XXX */
    927 		printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
    928 		    istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]);
    929 		printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    930 		    acb->msg[0], acb->msg[1], acb->msg[2],
    931 		    acb->msg[3], acb->msg[4], acb->msg[5]);
    932 	}
    933 #endif
    934 	if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa ||
    935 	    rp->siop_dsp >= sc->sc_scriptspa + sizeof(scripts))) {
    936 		printf ("%s: dsp not within script dsp %lx scripts %lx:%lx",
    937 		    device_xname(sc->sc_dev), rp->siop_dsp, sc->sc_scriptspa,
    938 		    sc->sc_scriptspa + sizeof(scripts));
    939 		printf(" istat %x dstat %x sstat0 %x\n",
    940 		    istat, dstat, sstat0);
    941 #ifdef DDB
    942 		Debugger();
    943 #endif
    944 	}
    945 #endif
    946 	SIOP_TRACE('i',dstat,istat,(istat&SIOP_ISTAT_DIP)?rp->siop_dsps&0xff:sstat0);
    947 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) {
    948 		/* Normal completion status, or check condition */
    949 #ifdef DEBUG
    950 		if (rp->siop_dsa != kvtop((void *)&acb->ds)) {
    951 			printf ("siop: invalid dsa: %lx %x\n", rp->siop_dsa,
    952 			    (unsigned)kvtop((void *)&acb->ds));
    953 			panic("*** siop DSA invalid ***");
    954 		}
    955 #endif
    956 		target = acb->xs->xs_periph->periph_target;
    957 		if (sc->sc_sync[target].state == NEG_WAITS) {
    958 			if (acb->msg[1] == 0xff)
    959 				printf ("%s: target %d ignored sync request\n",
    960 				    device_xname(sc->sc_dev), target);
    961 			else if (acb->msg[1] == MSG_REJECT)
    962 				printf ("%s: target %d rejected sync request\n",
    963 				    device_xname(sc->sc_dev), target);
    964 			else
    965 /* XXX - need to set sync transfer parameters */
    966 				printf("%s: target %d (sync) %02x %02x %02x\n",
    967 				    device_xname(sc->sc_dev), target, acb->msg[1],
    968 				    acb->msg[2], acb->msg[3]);
    969 			sc->sc_sync[target].state = NEG_DONE;
    970 		}
    971 		dma_cachectl(&acb->stat[0], 1);
    972 		*status = acb->stat[0];
    973 #ifdef DEBUG
    974 		if (rp->siop_sbcl & SIOP_BSY) {
    975 			/*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n",
    976 			    rp, &scripts, &acb->ds);*/
    977 #ifdef DDB
    978 			/*Debugger();*/
    979 #endif
    980 		}
    981 		if (acb->msg[0] != 0x00)
    982 			printf("%s: message was not COMMAND COMPLETE: %x\n",
    983 			    device_xname(sc->sc_dev), acb->msg[0]);
    984 #endif
    985 		if (sc->nexus_list.tqh_first)
    986 			rp->siop_dcntl |= SIOP_DCNTL_STD;
    987 		return 1;
    988 	}
    989 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0b) {
    990 		target = acb->xs->xs_periph->periph_target;
    991 		if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 3 &&
    992 		    acb->msg[3] == MSG_SYNC_REQ) {
    993 #ifdef DEBUG
    994 			if (siopsync_debug)
    995 				printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    996 				    acb->msg[0], acb->msg[1], acb->msg[2],
    997 				    acb->msg[3], acb->msg[4], acb->msg[5]);
    998 #endif
    999 			sc->sc_sync[target].sxfer = 0;
   1000 			sc->sc_sync[target].sbcl = 0;
   1001 			if (acb->msg[2] == 3 &&
   1002 			    acb->msg[3] == MSG_SYNC_REQ &&
   1003 			    acb->msg[5] != 0) {
   1004 #ifdef MAXTOR_KLUDGE
   1005 				/*
   1006 				 * Kludge for my Maxtor XT8580S
   1007 				 * It accepts whatever we request, even
   1008 				 * though it won't work.  So we ask for
   1009 				 * a short period than we can handle.  If
   1010 				 * the device says it can do it, use 208ns.
   1011 				 * If the device says it can do less than
   1012 				 * 100ns, then we limit it to 100ns.
   1013 				 */
   1014 				if (acb->msg[4] && acb->msg[4] < 100 / 4) {
   1015 #ifdef DEBUG
   1016 					printf ("%d: target %d wanted %dns period\n",
   1017 					    device_xname(sc->sc_dev), target,
   1018 					    acb->msg[4] * 4);
   1019 #endif
   1020 					if (acb->msg[4] == 50 / 4)
   1021 						acb->msg[4] = 208 / 4;
   1022 					else
   1023 						acb->msg[4] = 100 / 4;
   1024 				}
   1025 #endif /* MAXTOR_KLUDGE */
   1026 				printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
   1027 				    device_xname(sc->sc_dev), target,
   1028 				    acb->msg[4] * 4, acb->msg[5]);
   1029 				scsi_period_to_siop (sc, target);
   1030 			}
   1031 			rp->siop_sxfer = sc->sc_sync[target].sxfer;
   1032 			rp->siop_sbcl = sc->sc_sync[target].sbcl;
   1033 			if (sc->sc_sync[target].state == NEG_WAITS) {
   1034 				sc->sc_sync[target].state = NEG_DONE;
   1035 				rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
   1036 				return(0);
   1037 			}
   1038 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1039 			sc->sc_sync[target].state = NEG_DONE;
   1040 			return (0);
   1041 		}
   1042 		/* XXX - not SDTR message */
   1043 	}
   1044 	if (sstat0 & SIOP_SSTAT0_M_A) {		/* Phase mismatch */
   1045 #ifdef DEBUG
   1046 		++siopphmm;
   1047 		if (acb == NULL)
   1048 			printf("%s: Phase mismatch with no active command?\n",
   1049 			    device_xname(sc->sc_dev));
   1050 #endif
   1051 		if (acb->iob_len) {
   1052 			int adjust;
   1053 			adjust = ((dfifo - (dbc & 0x7f)) & 0x7f);
   1054 			if (sstat1 & SIOP_SSTAT1_ORF)
   1055 				++adjust;
   1056 			if (sstat1 & SIOP_SSTAT1_OLF)
   1057 				++adjust;
   1058 			acb->iob_curlen =
   1059 			    *((long *)__UNVOLATILE(&rp->siop_dcmd)) & 0xffffff;
   1060 			acb->iob_curlen += adjust;
   1061 			acb->iob_curbuf =
   1062 			    *((long *)__UNVOLATILE(&rp->siop_dnad)) - adjust;
   1063 #ifdef DEBUG
   1064 			if (siop_debug & 0x100) {
   1065 				int i;
   1066 				printf ("Phase mismatch: curbuf %lx curlen %lx dfifo %x dbc %x sstat1 %x adjust %x sbcl %x starts %d acb %p\n",
   1067 				    acb->iob_curbuf, acb->iob_curlen, dfifo,
   1068 				    dbc, sstat1, adjust, rp->siop_sbcl,
   1069 				    siopstarts, acb);
   1070 				if (acb->ds.chain[1].datalen) {
   1071 					for (i = 0; acb->ds.chain[i].datalen; ++i)
   1072 						printf("chain[%d] addr %p len %lx\n",
   1073 						    i, acb->ds.chain[i].databuf,
   1074 						    acb->ds.chain[i].datalen);
   1075 				}
   1076 			}
   1077 #endif
   1078 			dma_cachectl ((void *)acb, sizeof(*acb));
   1079 		}
   1080 #ifdef DEBUG
   1081 		SIOP_TRACE('m',rp->siop_sbcl,(rp->siop_dsp>>8),rp->siop_dsp);
   1082 		if (siop_debug & 9)
   1083 			printf ("Phase mismatch: %x dsp +%lx dcmd %lx\n",
   1084 			    rp->siop_sbcl,
   1085 			    rp->siop_dsp - sc->sc_scriptspa,
   1086 			    *((volatile long *)&rp->siop_dcmd));
   1087 #endif
   1088 		if ((rp->siop_sbcl & SIOP_REQ) == 0) {
   1089 			printf ("Phase mismatch: REQ not asserted! %02x dsp %lx\n",
   1090 			    rp->siop_sbcl, rp->siop_dsp);
   1091 #if defined(DEBUG) && defined(DDB)
   1092 			/*Debugger(); XXX is*/
   1093 #endif
   1094 		}
   1095 		switch (rp->siop_sbcl & 7) {
   1096 		case 0:		/* data out */
   1097 		case 1:		/* data in */
   1098 		case 2:		/* status */
   1099 		case 3:		/* command */
   1100 		case 6:		/* message in */
   1101 		case 7:		/* message out */
   1102 			rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
   1103 			break;
   1104 		default:
   1105 			goto bad_phase;
   1106 		}
   1107 		return 0;
   1108 	}
   1109 	if (sstat0 & SIOP_SSTAT0_STO) {		/* Select timed out */
   1110 #ifdef DEBUG
   1111 		if (acb == NULL)
   1112 			printf("%s: Select timeout with no active command?\n",
   1113 			    device_xname(sc->sc_dev));
   1114 		if (rp->siop_sbcl & SIOP_BSY) {
   1115 			printf ("ACK! siop was busy at timeout: rp %p script %p dsa %p\n",
   1116 			    rp, &scripts, &acb->ds);
   1117 			printf(" sbcl %x sdid %x istat %x dstat %x sstat0 %x\n",
   1118 			    rp->siop_sbcl, rp->siop_sdid, istat, dstat, sstat0);
   1119 			if (!(rp->siop_sbcl & SIOP_BSY)) {
   1120 				printf ("Yikes, it's not busy now!\n");
   1121 #if 0
   1122 				*status = -1;
   1123 				if (sc->nexus_list.tqh_first)
   1124 					rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1125 				return 1;
   1126 #endif
   1127 			}
   1128 /*			rp->siop_dcntl |= SIOP_DCNTL_STD;*/
   1129 			return (0);
   1130 #ifdef DDB
   1131 			Debugger();
   1132 #endif
   1133 		}
   1134 #endif
   1135 		*status = -1;
   1136 		acb->xs->error = XS_SELTIMEOUT;
   1137 		if (sc->nexus_list.tqh_first)
   1138 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1139 		return 1;
   1140 	}
   1141 	if (acb)
   1142 		target = acb->xs->xs_periph->periph_target;
   1143 	else
   1144 		target = 7;
   1145 	if (sstat0 & SIOP_SSTAT0_UDC) {
   1146 #ifdef DEBUG
   1147 		if (acb == NULL)
   1148 			printf("%s: Unexpected disconnect with no active command?\n",
   1149 			    device_xname(sc->sc_dev));
   1150 		printf ("%s: target %d disconnected unexpectedly\n",
   1151 		   device_xname(sc->sc_dev), target);
   1152 #endif
   1153 #if 0
   1154 		siopabort (sc, rp, "siopchkintr");
   1155 #endif
   1156 		*status = STS_BUSY;
   1157 		if (sc->nexus_list.tqh_first)
   1158 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1159 		return (acb != NULL);
   1160 	}
   1161 	if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff01 ||
   1162 	    rp->siop_dsps == 0xff02)) {
   1163 #ifdef DEBUG
   1164 		if (siop_debug & 0x100)
   1165 			printf ("%s: ID %02x disconnected TEMP %lx (+%lx) curbuf %lx curlen %lx buf %p len %lx dfifo %x dbc %x sstat1 %x starts %d acb %p\n",
   1166 			    device_xname(sc->sc_dev), 1 << target, rp->siop_temp,
   1167 			    rp->siop_temp ? rp->siop_temp - sc->sc_scriptspa : 0,
   1168 			    acb->iob_curbuf, acb->iob_curlen,
   1169 			    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen, dfifo, dbc, sstat1, siopstarts, acb);
   1170 #endif
   1171 		if (acb == NULL) {
   1172 			printf("%s: Disconnect with no active command?\n",
   1173 			    device_xname(sc->sc_dev));
   1174 			return (0);
   1175 		}
   1176 		/*
   1177 		 * XXXX need to update iob_curbuf/iob_curlen to reflect
   1178 		 * current data transferred.  If device disconnected in
   1179 		 * the middle of a DMA block, they should already be set
   1180 		 * by the phase change interrupt.  If the disconnect
   1181 		 * occurs on a DMA block boundary, we have to figure out
   1182 		 * which DMA block it was.
   1183 		 */
   1184 		if (acb->iob_len && rp->siop_temp) {
   1185 			int n = rp->siop_temp - sc->sc_scriptspa;
   1186 
   1187 			if (acb->iob_curlen && acb->iob_curlen != acb->ds.chain[0].datalen)
   1188 				printf("%s: iob_curbuf/len already set? n %x iob %lx/%lx chain[0] %p/%lx\n",
   1189 				    device_xname(sc->sc_dev), n, acb->iob_curbuf, acb->iob_curlen,
   1190 				    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen);
   1191 			if (n < Ent_datain)
   1192 				n = (n - Ent_dataout) / 16;
   1193 			else
   1194 				n = (n - Ent_datain) / 16;
   1195 			if (n <= 0 && n > DMAMAXIO)
   1196 				printf("TEMP invalid %d\n", n);
   1197 			else {
   1198 				acb->iob_curbuf = (u_long)acb->ds.chain[n].databuf;
   1199 				acb->iob_curlen = acb->ds.chain[n].datalen;
   1200 			}
   1201 #ifdef DEBUG
   1202 			if (siop_debug & 0x100) {
   1203 				printf("%s: TEMP offset %d", device_xname(sc->sc_dev), n);
   1204 				printf(" curbuf %lx curlen %lx\n", acb->iob_curbuf,
   1205 				    acb->iob_curlen);
   1206 			}
   1207 #endif
   1208 		}
   1209 		/*
   1210 		 * If data transfer was interrupted by disconnect, iob_curbuf
   1211 		 * and iob_curlen should reflect the point of interruption.
   1212 		 * Adjust the DMA chain so that the data transfer begins
   1213 		 * at the appropriate place upon reselection.
   1214 		 * XXX This should only be done on save data pointer message?
   1215 		 */
   1216 		if (acb->iob_curlen) {
   1217 			int i, j;
   1218 
   1219 #ifdef DEBUG
   1220 			if (siop_debug & 0x100)
   1221 				printf ("%s: adjusting DMA chain\n",
   1222 				    device_xname(sc->sc_dev));
   1223 			if (rp->siop_dsps == 0xff02)
   1224 				printf ("%s: ID %02x disconnected without Save Data Pointers\n",
   1225 				    device_xname(sc->sc_dev), 1 << target);
   1226 #endif
   1227 /* XXX is: 		if (rp->siop_dsps != 0xff02) { */
   1228 				/* not disconnected without save data ptr */
   1229 			for (i = 0; i < DMAMAXIO; ++i) {
   1230 				if (acb->ds.chain[i].datalen == 0)
   1231 					break;
   1232 				if (acb->iob_curbuf >= (long)acb->ds.chain[i].databuf &&
   1233 				    acb->iob_curbuf < (long)(acb->ds.chain[i].databuf +
   1234 				    acb->ds.chain[i].datalen))
   1235 					break;
   1236 			}
   1237 			if (i >= DMAMAXIO || acb->ds.chain[i].datalen == 0) {
   1238 				printf("couldn't find saved data pointer: ");
   1239 				printf("curbuf %lx curlen %lx i %d\n",
   1240 				    acb->iob_curbuf, acb->iob_curlen, i);
   1241 #ifdef DDB
   1242 				Debugger();
   1243 #endif
   1244 			}
   1245 /* XXX is: 		}			*/
   1246 #ifdef DEBUG
   1247 			if (siop_debug & 0x100)
   1248 				printf("  chain[0]: %p/%lx -> %lx/%lx\n",
   1249 				    acb->ds.chain[0].databuf,
   1250 				    acb->ds.chain[0].datalen,
   1251 				    acb->iob_curbuf,
   1252 				    acb->iob_curlen);
   1253 #endif
   1254 			acb->ds.chain[0].databuf = (char *)acb->iob_curbuf;
   1255 			acb->ds.chain[0].datalen = acb->iob_curlen;
   1256 			for (j = 1, ++i; i < DMAMAXIO && acb->ds.chain[i].datalen; ++i, ++j) {
   1257 #ifdef DEBUG
   1258 			if (siop_debug & 0x100)
   1259 				printf("  chain[%d]: %p/%lx -> %p/%lx\n", j,
   1260 				    acb->ds.chain[j].databuf,
   1261 				    acb->ds.chain[j].datalen,
   1262 				    acb->ds.chain[i].databuf,
   1263 				    acb->ds.chain[i].datalen);
   1264 #endif
   1265 				acb->ds.chain[j].databuf = acb->ds.chain[i].databuf;
   1266 				acb->ds.chain[j].datalen = acb->ds.chain[i].datalen;
   1267 			}
   1268 			if (j < DMAMAXIO)
   1269 				acb->ds.chain[j].datalen = 0;
   1270 			DCIAS(kvtop((void *)&acb->ds.chain));
   1271 		}
   1272 		++sc->sc_tinfo[target].dconns;
   1273 		/*
   1274 		 * add nexus to waiting list
   1275 		 * clear nexus
   1276 		 * try to start another command for another target/lun
   1277 		 */
   1278 		acb->status = sc->sc_flags & SIOP_INTSOFF;
   1279 		TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1280 		sc->sc_nexus = NULL;		/* no current device */
   1281 		/* start script to wait for reselect */
   1282 		if (sc->sc_nexus == NULL)
   1283 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1284 /* XXXX start another command ? */
   1285 		if (sc->ready_list.tqh_first)
   1286 			siop_sched(sc);
   1287 		return (0);
   1288 	}
   1289 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff03) {
   1290 		int reselid = rp->siop_scratch & 0x7f;
   1291 		int reselun = rp->siop_sfbr & 0x07;
   1292 
   1293 		sc->sc_sstat1 = rp->siop_sbcl;	/* XXXX save current SBCL */
   1294 #ifdef DEBUG
   1295 		if (siop_debug & 0x100)
   1296 			printf ("%s: target ID %02x reselected dsps %lx\n",
   1297 			     device_xname(sc->sc_dev), reselid,
   1298 			     rp->siop_dsps);
   1299 		if ((rp->siop_sfbr & 0x80) == 0)
   1300 			printf("%s: Reselect message in was not identify: %x\n",
   1301 			    device_xname(sc->sc_dev), rp->siop_sfbr);
   1302 #endif
   1303 		if (sc->sc_nexus) {
   1304 #ifdef DEBUG
   1305 			if (siop_debug & 0x100)
   1306 				printf ("%s: reselect ID %02x w/active\n",
   1307 				    device_xname(sc->sc_dev), reselid);
   1308 #endif
   1309 			TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain);
   1310 			sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target].lubusy
   1311 			    &= ~(1 << sc->sc_nexus->xs->xs_periph->periph_lun);
   1312 			--sc->sc_active;
   1313 		}
   1314 		/*
   1315 		 * locate acb of reselecting device
   1316 		 * set sc->sc_nexus to acb
   1317 		 */
   1318 		for (acb = sc->nexus_list.tqh_first; acb;
   1319 		    acb = acb->chain.tqe_next) {
   1320 			if (reselid != (acb->ds.scsi_addr >> 16) ||
   1321 			    reselun != (acb->msgout[0] & 0x07))
   1322 				continue;
   1323 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1324 			sc->sc_nexus = acb;
   1325 			sc->sc_flags |= acb->status;
   1326 			acb->status = 0;
   1327 			DCIAS(kvtop(&acb->stat[0]));
   1328 			rp->siop_dsa = kvtop((void *)&acb->ds);
   1329 			rp->siop_sxfer =
   1330 				sc->sc_sync[acb->xs->xs_periph->periph_target].sxfer;
   1331 			rp->siop_sbcl =
   1332 				sc->sc_sync[acb->xs->xs_periph->periph_target].sbcl;
   1333 			break;
   1334 		}
   1335 		if (acb == NULL) {
   1336 			printf("%s: target ID %02x reselect nexus_list %p\n",
   1337 			    device_xname(sc->sc_dev), reselid,
   1338 			    sc->nexus_list.tqh_first);
   1339 			panic("unable to find reselecting device");
   1340 		}
   1341 		dma_cachectl ((void *)acb, sizeof(*acb));
   1342 		rp->siop_temp = 0;
   1343 		rp->siop_dcntl |= SIOP_DCNTL_STD;
   1344 		return (0);
   1345 	}
   1346 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) {
   1347 #ifdef DEBUG
   1348 		u_short ctest2 = rp->siop_ctest2;
   1349 
   1350 		/* reselect was interrupted (by Sig_P or select) */
   1351 		if (siop_debug & 0x100 ||
   1352 		    (ctest2 & SIOP_CTEST2_SIGP) == 0)
   1353 			printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
   1354 			    device_xname(sc->sc_dev), rp->siop_scntl1,
   1355 			    ctest2, rp->siop_sfbr, istat, rp->siop_istat);
   1356 #endif
   1357 		/* XXX assumes it was not select */
   1358 		if (sc->sc_nexus == NULL) {
   1359 #ifdef DEBUG
   1360 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
   1361 			    device_xname(sc->sc_dev));
   1362 #if 0
   1363 			siop_dump(sc);
   1364 #ifdef DDB
   1365 			Debugger();
   1366 #endif
   1367 #endif
   1368 #endif
   1369 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1370 			return(0);
   1371 		}
   1372 		target = sc->sc_nexus->xs->xs_periph->periph_target;
   1373 		rp->siop_temp = 0;
   1374 		rp->siop_dsa = kvtop((void *)&sc->sc_nexus->ds);
   1375 		rp->siop_sxfer = sc->sc_sync[target].sxfer;
   1376 		rp->siop_sbcl = sc->sc_sync[target].sbcl;
   1377 		rp->siop_dsp = sc->sc_scriptspa;
   1378 		return (0);
   1379 	}
   1380 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) {
   1381 		if (acb == NULL)
   1382 			printf("%s: Bad message-in with no active command?\n",
   1383 			    device_xname(sc->sc_dev));
   1384 		/* Unrecognized message in byte */
   1385 		dma_cachectl (&acb->msg[1],1);
   1386 		printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n",
   1387 			device_xname(sc->sc_dev), rp->siop_sfbr, acb->msg[1], rp->siop_sbcl);
   1388 		/* what should be done here? */
   1389 		DCIAS(kvtop(&acb->msg[1]));
   1390 		rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
   1391 		return (0);
   1392 	}
   1393 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
   1394 		/* Status phase wasn't followed by message in phase? */
   1395 		printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n",
   1396 			device_xname(sc->sc_dev), rp->siop_sbcl, rp->siop_sbdl);
   1397 		if (rp->siop_sbcl == 0xa7) {
   1398 			/* It is now, just continue the script? */
   1399 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1400 			return (0);
   1401 		}
   1402 	}
   1403 	if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
   1404 		dma_cachectl (&acb->stat[0], 1);
   1405 		dma_cachectl (&acb->msg[0], 1);
   1406 		printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n",
   1407 		    rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1],
   1408 		    rp->siop_sbcl);
   1409 		siopreset (sc);
   1410 		*status = -1;
   1411 		return 0;	/* siopreset has cleaned up */
   1412 	}
   1413 	if (sstat0 & SIOP_SSTAT0_SGE)
   1414 		printf ("SIOP: SCSI Gross Error\n");
   1415 	if (sstat0 & SIOP_SSTAT0_PAR)
   1416 		printf ("SIOP: Parity Error\n");
   1417 	if (dstat & SIOP_DSTAT_IID)
   1418 		printf ("SIOP: Invalid instruction detected\n");
   1419 bad_phase:
   1420 	/*
   1421 	 * temporary panic for unhandled conditions
   1422 	 * displays various things about the 53C710 status and registers
   1423 	 * then panics.
   1424 	 * XXXX need to clean this up to print out the info, reset, and continue
   1425 	 */
   1426 	printf ("siopchkintr: target %x ds %p\n", target, &acb->ds);
   1427 	printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n",
   1428 	    sc->sc_scriptspa, (unsigned)kvtop((void *)&acb->ds),
   1429 	    (unsigned)kvtop((void *)__UNVOLATILE(rp)), rp->siop_dsp,
   1430 	    *((volatile long *)&rp->siop_dcmd));
   1431 	printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n",
   1432 	    istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa,
   1433 	     rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr);
   1434 #ifdef DEBUG
   1435 	if (siop_debug & 0x20)
   1436 		panic("siopchkintr: **** temp ****");
   1437 #endif
   1438 #ifdef DDB
   1439 	Debugger ();
   1440 #endif
   1441 	siopreset (sc);		/* hard reset */
   1442 	*status = -1;
   1443 	return 0;		/* siopreset cleaned up */
   1444 }
   1445 
   1446 void
   1447 siop_select(struct siop_softc *sc)
   1448 {
   1449 	siop_regmap_p rp;
   1450 	struct siop_acb *acb = sc->sc_nexus;
   1451 
   1452 #ifdef DEBUG
   1453 	if (siop_debug & 1)
   1454 		printf ("%s: select ", device_xname(sc->sc_dev));
   1455 #endif
   1456 
   1457 	rp = sc->sc_siopp;
   1458 	if (acb->xs->xs_control & XS_CTL_POLL || siop_no_dma) {
   1459 		sc->sc_flags |= SIOP_INTSOFF;
   1460 		sc->sc_flags &= ~SIOP_INTDEFER;
   1461 		if ((rp->siop_istat & 0x08) == 0) {
   1462 			rp->siop_sien = 0;
   1463 			rp->siop_dien = 0;
   1464 		}
   1465 #if 0
   1466 	} else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
   1467 		sc->sc_flags &= ~SIOP_INTSOFF;
   1468 		if ((rp->siop_istat & 0x08) == 0) {
   1469 			rp->siop_sien = sc->sc_sien;
   1470 			rp->siop_dien = sc->sc_dien;
   1471 		}
   1472 #endif
   1473 	}
   1474 #ifdef DEBUG
   1475 	if (siop_debug & 1)
   1476 		printf ("siop_select: target %x cmd %02x ds %p\n",
   1477 		    acb->xs->xs_periph->periph_target, acb->cmd.opcode,
   1478 		    &sc->sc_nexus->ds);
   1479 #endif
   1480 
   1481 	siop_start(sc, acb->xs->xs_periph->periph_target,
   1482 		acb->xs->xs_periph->periph_lun,
   1483 	    (u_char *)&acb->cmd, acb->clen, acb->daddr, acb->dleft);
   1484 
   1485 	return;
   1486 }
   1487 
   1488 /*
   1489  * 53C710 interrupt handler
   1490  */
   1491 
   1492 void
   1493 siopintr(register struct siop_softc *sc)
   1494 {
   1495 	siop_regmap_p rp;
   1496 	register u_char istat, dstat, sstat0;
   1497 	int status;
   1498 	int s = splbio();
   1499 
   1500 	istat = sc->sc_istat;
   1501 	if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
   1502 		splx(s);
   1503 		return;
   1504 	}
   1505 
   1506 	/* Got a valid interrupt on this device */
   1507 	rp = sc->sc_siopp;
   1508 	dstat = sc->sc_dstat;
   1509 	sstat0 = sc->sc_sstat0;
   1510 	if (dstat & SIOP_DSTAT_SIR)
   1511 		sc->sc_intcode = rp->siop_dsps;
   1512 	sc->sc_istat = 0;
   1513 #ifdef DEBUG
   1514 	if (siop_debug & 1)
   1515 		printf ("%s: intr istat %x dstat %x sstat0 %x\n",
   1516 		    device_xname(sc->sc_dev), istat, dstat, sstat0);
   1517 	if (!sc->sc_active) {
   1518 		printf ("%s: spurious interrupt? istat %x dstat %x sstat0 %x nexus %p status %x\n",
   1519 		    device_xname(sc->sc_dev), istat, dstat, sstat0,
   1520 		    sc->sc_nexus, sc->sc_nexus ? sc->sc_nexus->stat[0] : 0);
   1521 	}
   1522 #endif
   1523 
   1524 #ifdef DEBUG
   1525 	if (siop_debug & 5) {
   1526 		DCIAS(kvtop(&sc->sc_nexus->stat[0]));
   1527 		printf ("%s: intr istat %x dstat %x sstat0 %x dsps %lx sbcl %x sts %x msg %x\n",
   1528 		    device_xname(sc->sc_dev), istat, dstat, sstat0,
   1529 		    rp->siop_dsps,  rp->siop_sbcl,
   1530 		    sc->sc_nexus->stat[0], sc->sc_nexus->msg[0]);
   1531 	}
   1532 #endif
   1533 	if (sc->sc_flags & SIOP_INTDEFER) {
   1534 		sc->sc_flags &= ~(SIOP_INTDEFER | SIOP_INTSOFF);
   1535 		rp->siop_sien = sc->sc_sien;
   1536 		rp->siop_dien = sc->sc_dien;
   1537 	}
   1538 	if (siop_checkintr (sc, istat, dstat, sstat0, &status)) {
   1539 #if 1
   1540 		if (status == 0xff)
   1541 			printf ("siopintr: status == 0xff\n");
   1542 #endif
   1543 		if ((sc->sc_flags & (SIOP_INTSOFF | SIOP_INTDEFER)) != SIOP_INTSOFF) {
   1544 #if 0
   1545 			if (rp->siop_sbcl & SIOP_BSY) {
   1546 				printf ("%s: SCSI bus busy at completion",
   1547 					device_xname(sc->sc_dev));
   1548 				printf(" targ %d sbcl %02x sfbr %x lcrc %02x dsp +%x\n",
   1549 				    sc->sc_nexus->xs->xs_periph->periph_target,
   1550 				    rp->siop_sbcl, rp->siop_sfbr, rp->siop_lcrc,
   1551 				    rp->siop_dsp - sc->sc_scriptspa);
   1552 			}
   1553 #endif
   1554 			siop_scsidone(sc->sc_nexus, sc->sc_nexus ?
   1555 			    sc->sc_nexus->stat[0] : -1);
   1556 		}
   1557 	}
   1558 	splx(s);
   1559 }
   1560 
   1561 /*
   1562  * This is based on the Progressive Peripherals 33Mhz Zeus driver and will
   1563  * not be correct for other 53c710 boards.
   1564  *
   1565  */
   1566 void
   1567 scsi_period_to_siop(struct siop_softc *sc, int target)
   1568 {
   1569 	int period, offset, sxfer, sbcl = 0;
   1570 #ifdef DEBUG_SYNC
   1571 	int i;
   1572 #endif
   1573 
   1574 	period = sc->sc_nexus->msg[4];
   1575 	offset = sc->sc_nexus->msg[5];
   1576 #ifdef DEBUG_SYNC
   1577 	sxfer = 0;
   1578 	if (offset <= SIOP_MAX_OFFSET)
   1579 		sxfer = offset;
   1580 	for (i = 0; i < sizeof (sync_tab) / 2; ++i) {
   1581 		if (period <= sync_tab[i].p) {
   1582 			sxfer |= sync_tab[i].r & 0x70;
   1583 			sbcl = sync_tab[i].r & 0x03;
   1584 			break;
   1585 		}
   1586 	}
   1587 	printf ("siop sync old: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
   1588 #endif
   1589 	for (sbcl = 1; sbcl < 4; ++sbcl) {
   1590 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
   1591 		if (sxfer >= 0 && sxfer <= 7)
   1592 			break;
   1593 	}
   1594 	if (sbcl > 3) {
   1595 		printf("siop sync: unable to compute sync params for period %dns\n",
   1596 		    period * 4);
   1597 		/*
   1598 		 * XXX need to pick a value we can do and renegotiate
   1599 		 */
   1600 		sxfer = sbcl = 0;
   1601 	} else {
   1602 		sxfer = (sxfer << 4) | ((offset <= SIOP_MAX_OFFSET) ?
   1603 		    offset : SIOP_MAX_OFFSET);
   1604 #ifdef DEBUG_SYNC
   1605 		printf("siop sync: params for period %dns: sxfer %x sbcl %x",
   1606 		    period * 4, sxfer, sbcl);
   1607 		printf(" actual period %dns\n",
   1608 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
   1609 #endif
   1610 	}
   1611 	sc->sc_sync[target].sxfer = sxfer;
   1612 	sc->sc_sync[target].sbcl = sbcl;
   1613 #ifdef DEBUG_SYNC
   1614 	printf ("siop sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
   1615 #endif
   1616 }
   1617 
   1618 #ifdef DEBUG
   1619 
   1620 #if SIOP_TRACE_SIZE
   1621 void
   1622 siop_dump_trace(void)
   1623 {
   1624 	int i;
   1625 
   1626 	printf("siop trace: next index %d\n", siop_trix);
   1627 	i = siop_trix;
   1628 	do {
   1629 		printf("%3d: '%c' %02x %02x %02x\n", i, siop_trbuf[i],
   1630 		    siop_trbuf[i + 1], siop_trbuf[i + 2], siop_trbuf[i + 3]);
   1631 		i = (i + 4) & (SIOP_TRACE_SIZE - 1);
   1632 	} while (i != siop_trix);
   1633 }
   1634 #endif
   1635 
   1636 void
   1637 siop_dump_acb(struct siop_acb *acb)
   1638 {
   1639 	u_char *b = (u_char *) &acb->cmd;
   1640 	int i;
   1641 
   1642 	printf("acb@%p ", acb);
   1643 	if (acb->xs == NULL) {
   1644 		printf("<unused>\n");
   1645 		return;
   1646 	}
   1647 	printf("(%d:%d) flags %2x clen %2d cmd ",
   1648 		acb->xs->xs_periph->periph_target,
   1649 	    acb->xs->xs_periph->periph_lun, acb->flags, acb->clen);
   1650 	for (i = acb->clen; i; --i)
   1651 		printf(" %02x", *b++);
   1652 	printf("\n");
   1653 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
   1654 	    acb->xs->datalen);
   1655 	printf("va %p:%lx ", acb->iob_buf, acb->iob_len);
   1656 	printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen);
   1657 }
   1658 
   1659 void
   1660 siop_dump(struct siop_softc *sc)
   1661 {
   1662 	struct siop_acb *acb;
   1663 	siop_regmap_p rp = sc->sc_siopp;
   1664 	int s;
   1665 	int i;
   1666 
   1667 	s = splbio();
   1668 #if SIOP_TRACE_SIZE
   1669 	siop_dump_trace();
   1670 #endif
   1671 	printf("%s@%p regs %p istat %x\n",
   1672 	    device_xname(sc->sc_dev), sc, rp, rp->siop_istat);
   1673 	if ((acb = sc->free_list.tqh_first) > 0) {
   1674 		printf("Free list:\n");
   1675 		while (acb) {
   1676 			siop_dump_acb(acb);
   1677 			acb = acb->chain.tqe_next;
   1678 		}
   1679 	}
   1680 	if ((acb = sc->ready_list.tqh_first) > 0) {
   1681 		printf("Ready list:\n");
   1682 		while (acb) {
   1683 			siop_dump_acb(acb);
   1684 			acb = acb->chain.tqe_next;
   1685 		}
   1686 	}
   1687 	if ((acb = sc->nexus_list.tqh_first) > 0) {
   1688 		printf("Nexus list:\n");
   1689 		while (acb) {
   1690 			siop_dump_acb(acb);
   1691 			acb = acb->chain.tqe_next;
   1692 		}
   1693 	}
   1694 	if (sc->sc_nexus) {
   1695 		printf("Nexus:\n");
   1696 		siop_dump_acb(sc->sc_nexus);
   1697 	}
   1698 	for (i = 0; i < 8; ++i) {
   1699 		if (sc->sc_tinfo[i].cmds > 2) {
   1700 			printf("tgt %d: cmds %d disc %d lubusy %x\n",
   1701 			    i, sc->sc_tinfo[i].cmds,
   1702 			    sc->sc_tinfo[i].dconns,
   1703 			    sc->sc_tinfo[i].lubusy);
   1704 		}
   1705 	}
   1706 	splx(s);
   1707 }
   1708 #endif
   1709