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