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