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