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