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