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