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