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