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