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