Home | History | Annotate | Line # | Download | only in dev
siop.c revision 1.40
      1 /*	$NetBSD: siop.c,v 1.40 1999/03/26 22:50:22 mhitch Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Michael L. Hitch
      5  * Copyright (c) 1990 The Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Van Jacobson of Lawrence Berkeley Laboratory.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)siop.c	7.5 (Berkeley) 5/4/91
     40  */
     41 
     42 /*
     43  * AMIGA 53C710 scsi adaptor driver
     44  */
     45 
     46 #include "opt_ddb.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/device.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/dkstat.h>
     53 #include <sys/buf.h>
     54 #include <sys/malloc.h>
     55 #include <dev/scsipi/scsi_all.h>
     56 #include <dev/scsipi/scsipi_all.h>
     57 #include <dev/scsipi/scsiconf.h>
     58 #include <machine/cpu.h>
     59 #include <amiga/amiga/custom.h>
     60 #include <amiga/amiga/isr.h>
     61 #include <amiga/dev/siopreg.h>
     62 #include <amiga/dev/siopvar.h>
     63 
     64 /*
     65  * SCSI delays
     66  * In u-seconds, primarily for state changes on the SPC.
     67  */
     68 #define	SCSI_CMD_WAIT	500000	/* wait per step of 'immediate' cmds */
     69 #define	SCSI_DATA_WAIT	500000	/* wait per data in/out step */
     70 #define	SCSI_INIT_WAIT	500000	/* wait per step (both) during init */
     71 
     72 void siop_select __P((struct siop_softc *));
     73 void siopabort __P((struct siop_softc *, siop_regmap_p, char *));
     74 void sioperror __P((struct siop_softc *, siop_regmap_p, u_char));
     75 void siopstart __P((struct siop_softc *));
     76 int  siop_checkintr __P((struct siop_softc *, u_char, u_char, u_char, int *));
     77 void siopreset __P((struct siop_softc *));
     78 void siopsetdelay __P((int));
     79 void siop_scsidone __P((struct siop_acb *, int));
     80 void siop_sched __P((struct siop_softc *));
     81 int  siop_poll __P((struct siop_softc *, struct siop_acb *));
     82 void siopintr __P((struct siop_softc *));
     83 void scsi_period_to_siop __P((struct siop_softc *, int));
     84 void siop_start __P((struct siop_softc *, int, int, u_char *, int, u_char *, int));
     85 void siop_dump_acb __P((struct siop_acb *));
     86 
     87 /* 53C710 script */
     88 const
     89 #include <amiga/dev/siop_script.out>
     90 
     91 /* default to not inhibit sync negotiation on any drive */
     92 u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
     93 u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3};
     94 int siop_no_dma = 0;
     95 
     96 int siop_reset_delay = 250;	/* delay after reset, in milleseconds */
     97 
     98 int siop_cmd_wait = SCSI_CMD_WAIT;
     99 int siop_data_wait = SCSI_DATA_WAIT;
    100 int siop_init_wait = SCSI_INIT_WAIT;
    101 
    102 #ifdef DEBUG_SYNC
    103 /*
    104  * sync period transfer lookup - only valid for 66Mhz clock
    105  */
    106 static struct {
    107 	unsigned char p;	/* period from sync request message */
    108 	unsigned char r;	/* siop_period << 4 | sbcl */
    109 } sync_tab[] = {
    110 	{ 60/4, 0<<4 | 1},
    111 	{ 76/4, 1<<4 | 1},
    112 	{ 92/4, 2<<4 | 1},
    113 	{ 92/4, 0<<4 | 2},
    114 	{108/4, 3<<4 | 1},
    115 	{116/4, 1<<4 | 2},
    116 	{120/4, 4<<4 | 1},
    117 	{120/4, 0<<4 | 3},
    118 	{136/4, 5<<4 | 1},
    119 	{140/4, 2<<4 | 2},
    120 	{152/4, 6<<4 | 1},
    121 	{152/4, 1<<4 | 3},
    122 	{164/4, 3<<4 | 2},
    123 	{168/4, 7<<4 | 1},
    124 	{180/4, 2<<4 | 3},
    125 	{184/4, 4<<4 | 2},
    126 	{208/4, 5<<4 | 2},
    127 	{212/4, 3<<4 | 3},
    128 	{232/4, 6<<4 | 2},
    129 	{240/4, 4<<4 | 3},
    130 	{256/4, 7<<4 | 2},
    131 	{272/4, 5<<4 | 3},
    132 	{300/4, 6<<4 | 3},
    133 	{332/4, 7<<4 | 3}
    134 };
    135 #endif
    136 
    137 #ifdef DEBUG
    138 /*
    139  *	0x01 - full debug
    140  *	0x02 - DMA chaining
    141  *	0x04 - siopintr
    142  *	0x08 - phase mismatch
    143  *	0x10 - <not used>
    144  *	0x20 - panic on unhandled exceptions
    145  *	0x100 - disconnect/reselect
    146  */
    147 int	siop_debug = 0;
    148 int	siopsync_debug = 0;
    149 int	siopdma_hits = 0;
    150 int	siopdma_misses = 0;
    151 int	siopchain_ints = 0;
    152 int	siopstarts = 0;
    153 int	siopints = 0;
    154 int	siopphmm = 0;
    155 #define SIOP_TRACE_SIZE	128
    156 #define SIOP_TRACE(a,b,c,d) \
    157 	siop_trbuf[siop_trix] = (a); \
    158 	siop_trbuf[siop_trix+1] = (b); \
    159 	siop_trbuf[siop_trix+2] = (c); \
    160 	siop_trbuf[siop_trix+3] = (d); \
    161 	siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1);
    162 u_char	siop_trbuf[SIOP_TRACE_SIZE];
    163 int	siop_trix;
    164 void siop_dump __P((struct siop_softc *));
    165 void siop_dump_trace __P((void));
    166 #else
    167 #define SIOP_TRACE(a,b,c,d)
    168 #endif
    169 
    170 
    171 /*
    172  * default minphys routine for siop based controllers
    173  */
    174 void
    175 siop_minphys(bp)
    176 	struct buf *bp;
    177 {
    178 
    179 	/*
    180 	 * No max transfer at this level.
    181 	 */
    182 	minphys(bp);
    183 }
    184 
    185 /*
    186  * used by specific siop controller
    187  *
    188  */
    189 int
    190 siop_scsicmd(xs)
    191 	struct scsipi_xfer *xs;
    192 {
    193 	struct siop_acb *acb;
    194 	struct siop_softc *sc;
    195 	struct scsipi_link *slp;
    196 	int flags, s;
    197 
    198 	slp = xs->sc_link;
    199 	sc = slp->adapter_softc;
    200 	flags = xs->flags;
    201 
    202 	/* XXXX ?? */
    203 	if (flags & SCSI_DATA_UIO)
    204 		panic("siop: scsi data uio requested");
    205 
    206 	/* XXXX ?? */
    207 	if (sc->sc_nexus && flags & SCSI_POLL)
    208 /*		panic("siop_scsicmd: busy");*/
    209 		printf("siop_scsicmd: busy\n");
    210 
    211 	s = splbio();
    212 	acb = sc->free_list.tqh_first;
    213 	if (acb) {
    214 		TAILQ_REMOVE(&sc->free_list, acb, chain);
    215 	}
    216 	splx(s);
    217 
    218 	if (acb == NULL) {
    219 		xs->error = XS_DRIVER_STUFFUP;
    220 		return(TRY_AGAIN_LATER);
    221 	}
    222 
    223 	acb->flags = ACB_ACTIVE;
    224 	acb->xs = xs;
    225 	bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
    226 	acb->clen = xs->cmdlen;
    227 	acb->daddr = xs->data;
    228 	acb->dleft = xs->datalen;
    229 
    230 	s = splbio();
    231 	TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
    232 
    233 	if (sc->sc_nexus == NULL)
    234 		siop_sched(sc);
    235 
    236 	splx(s);
    237 
    238 	if (flags & SCSI_POLL || siop_no_dma)
    239 		return(siop_poll(sc, acb));
    240 	return(SUCCESSFULLY_QUEUED);
    241 }
    242 
    243 int
    244 siop_poll(sc, acb)
    245 	struct siop_softc *sc;
    246 	struct siop_acb *acb;
    247 {
    248 	siop_regmap_p rp = sc->sc_siopp;
    249 	struct scsipi_xfer *xs = acb->xs;
    250 	int i;
    251 	int status;
    252 	u_char istat;
    253 	u_char dstat;
    254 	u_char sstat0;
    255 	int s;
    256 	int to;
    257 
    258 	s = splbio();
    259 	to = xs->timeout / 1000;
    260 	if (sc->nexus_list.tqh_first)
    261 		printf("%s: siop_poll called with disconnected device\n",
    262 		    sc->sc_dev.dv_xname);
    263 	for (;;) {
    264 		/* use cmd_wait values? */
    265 		i = 50000;
    266 		/* XXX spl0(); */
    267 		while (((istat = rp->siop_istat) &
    268 		    (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
    269 			if (--i <= 0) {
    270 #ifdef DEBUG
    271 				printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
    272 				    xs->sc_link->scsipi_scsi.target, acb->cmd.opcode,
    273 				    rp->siop_sbcl, rp->siop_dsp,
    274 				    rp->siop_dsp - sc->sc_scriptspa,
    275 				    *((long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
    276 #endif
    277 				i = 50000;
    278 				--to;
    279 				if (to <= 0) {
    280 					siopreset(sc);
    281 					return(COMPLETE);
    282 				}
    283 			}
    284 			delay(20);
    285 		}
    286 		sstat0 = rp->siop_sstat0;
    287 		dstat = rp->siop_dstat;
    288 		if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
    289 			if (acb != sc->sc_nexus)
    290 				printf("%s: siop_poll disconnected device completed\n",
    291 				    sc->sc_dev.dv_xname);
    292 			else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
    293 				sc->sc_flags &= ~SIOP_INTSOFF;
    294 				rp->siop_sien = sc->sc_sien;
    295 				rp->siop_dien = sc->sc_dien;
    296 			}
    297 			siop_scsidone(sc->sc_nexus, status);
    298 		}
    299 		if (xs->flags & ITSDONE)
    300 			break;
    301 	}
    302 	splx(s);
    303 	return (COMPLETE);
    304 }
    305 
    306 /*
    307  * start next command that's ready
    308  */
    309 void
    310 siop_sched(sc)
    311 	struct siop_softc *sc;
    312 {
    313 	struct scsipi_link *slp;
    314 	struct siop_acb *acb;
    315 	int i;
    316 
    317 #ifdef DEBUG
    318 	if (sc->sc_nexus) {
    319 		printf("%s: siop_sched- nexus %p/%d ready %p/%d\n",
    320 		    sc->sc_dev.dv_xname, sc->sc_nexus,
    321 		    sc->sc_nexus->xs->sc_link->scsipi_scsi.target,
    322 		    sc->ready_list.tqh_first,
    323 		    sc->ready_list.tqh_first->xs->sc_link->scsipi_scsi.target);
    324 		return;
    325 	}
    326 #endif
    327 	for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
    328 		slp = acb->xs->sc_link;
    329 		i = slp->scsipi_scsi.target;
    330 		if(!(sc->sc_tinfo[i].lubusy & (1 << slp->scsipi_scsi.lun))) {
    331 			struct siop_tinfo *ti = &sc->sc_tinfo[i];
    332 
    333 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    334 			sc->sc_nexus = acb;
    335 			slp = acb->xs->sc_link;
    336 			ti = &sc->sc_tinfo[slp->scsipi_scsi.target];
    337 			ti->lubusy |= (1 << slp->scsipi_scsi.lun);
    338 			break;
    339 		}
    340 	}
    341 
    342 	if (acb == NULL) {
    343 #ifdef DEBUGXXX
    344 		printf("%s: siop_sched didn't find ready command\n",
    345 		    sc->sc_dev.dv_xname);
    346 #endif
    347 		return;
    348 	}
    349 
    350 	if (acb->xs->flags & SCSI_RESET)
    351 		siopreset(sc);
    352 
    353 #if 0
    354 	acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5;	/* XXXX */
    355 #endif
    356 	++sc->sc_active;
    357 	siop_select(sc);
    358 }
    359 
    360 void
    361 siop_scsidone(acb, stat)
    362 	struct siop_acb *acb;
    363 	int stat;
    364 {
    365 	struct scsipi_xfer *xs;
    366 	struct scsipi_link *slp;
    367 	struct siop_softc *sc;
    368 	int dosched = 0;
    369 
    370 	if (acb == NULL || (xs = acb->xs) == NULL) {
    371 #ifdef DIAGNOSTIC
    372 		printf("siop_scsidone: NULL acb or scsipi_xfer\n");
    373 #if defined(DEBUG) && defined(DDB)
    374 		Debugger();
    375 #endif
    376 #endif
    377 		return;
    378 	}
    379 	slp = xs->sc_link;
    380 	sc = slp->adapter_softc;
    381 	/*
    382 	 * is this right?
    383 	 */
    384 	xs->status = stat;
    385 
    386 	if (xs->error == XS_NOERROR && !(acb->flags & ACB_CHKSENSE)) {
    387 		if (stat == SCSI_CHECK) {
    388 			struct scsipi_sense *ss = (void *)&acb->cmd;
    389 			bzero(ss, sizeof(*ss));
    390 			ss->opcode = REQUEST_SENSE;
    391 			ss->byte2 = slp->scsipi_scsi.lun << 5;
    392 			ss->length = sizeof(struct scsipi_sense_data);
    393 			acb->clen = sizeof(*ss);
    394 			acb->daddr = (char *)&xs->sense.scsi_sense;
    395 			acb->dleft = sizeof(struct scsipi_sense_data);
    396 			acb->flags = ACB_ACTIVE | ACB_CHKSENSE;
    397 			TAILQ_INSERT_HEAD(&sc->ready_list, acb, chain);
    398 			--sc->sc_active;
    399 			sc->sc_tinfo[slp->scsipi_scsi.target].lubusy &=
    400 			    ~(1 << slp->scsipi_scsi.lun);
    401 			sc->sc_tinfo[slp->scsipi_scsi.target].senses++;
    402 			if (sc->sc_nexus == acb) {
    403 				sc->sc_nexus = NULL;
    404 				siop_sched(sc);
    405 			}
    406 			SIOP_TRACE('d','s',0,0)
    407 			return;
    408 		}
    409 	}
    410 	if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE)) {
    411 		xs->error = XS_SENSE;
    412 	} else {
    413 		xs->resid = 0;		/* XXXX */
    414 	}
    415 #if whataboutthisone
    416 		case SCSI_BUSY:
    417 			xs->error = XS_BUSY;
    418 			break;
    419 #endif
    420 	xs->flags |= ITSDONE;
    421 
    422 	/*
    423 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    424 	 * a hack to figure out which queue it's on.  Note that it is *not*
    425 	 * necessary to cdr down the ready queue, but we must cdr down the
    426 	 * nexus queue and see if it's there, so we can mark the unit as no
    427 	 * longer busy.  This code is sickening, but it works.
    428 	 */
    429 	if (acb == sc->sc_nexus) {
    430 		sc->sc_nexus = NULL;
    431 		sc->sc_tinfo[slp->scsipi_scsi.target].lubusy &=
    432 			~(1<<slp->scsipi_scsi.lun);
    433 		if (sc->ready_list.tqh_first)
    434 			dosched = 1;	/* start next command */
    435 		--sc->sc_active;
    436 		SIOP_TRACE('d','a',stat,0)
    437 	} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
    438 		TAILQ_REMOVE(&sc->ready_list, acb, chain);
    439 		SIOP_TRACE('d','r',stat,0)
    440 	} else {
    441 		register struct siop_acb *acb2;
    442 		for (acb2 = sc->nexus_list.tqh_first; acb2;
    443 		    acb2 = acb2->chain.tqe_next)
    444 			if (acb2 == acb) {
    445 				TAILQ_REMOVE(&sc->nexus_list, acb, chain);
    446 				sc->sc_tinfo[slp->scsipi_scsi.target].lubusy
    447 					&= ~(1<<slp->scsipi_scsi.lun);
    448 				--sc->sc_active;
    449 				break;
    450 			}
    451 		if (acb2)
    452 			;
    453 		else if (acb->chain.tqe_next) {
    454 			TAILQ_REMOVE(&sc->ready_list, acb, chain);
    455 			--sc->sc_active;
    456 		} else {
    457 			printf("%s: can't find matching acb\n",
    458 			    sc->sc_dev.dv_xname);
    459 #ifdef DDB
    460 /*			Debugger(); */
    461 #endif
    462 		}
    463 		SIOP_TRACE('d','n',stat,0);
    464 	}
    465 	/* Put it on the free list. */
    466 	acb->flags = ACB_FREE;
    467 	TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
    468 
    469 	sc->sc_tinfo[slp->scsipi_scsi.target].cmds++;
    470 
    471 	scsipi_done(xs);
    472 
    473 	if (dosched && sc->sc_nexus == NULL)
    474 		siop_sched(sc);
    475 }
    476 
    477 void
    478 siopabort(sc, rp, where)
    479 	register struct siop_softc *sc;
    480 	siop_regmap_p rp;
    481 	char *where;
    482 {
    483 #ifdef fix_this
    484 	int i;
    485 #endif
    486 
    487 	printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    488 	    sc->sc_dev.dv_xname,
    489 	    where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
    490 
    491 	if (sc->sc_active > 0) {
    492 #ifdef TODO
    493       SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
    494       WAIT_CIP (rp);
    495 
    496       GET_SBIC_asr (rp, asr);
    497       if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
    498         {
    499           /* ok, get more drastic.. */
    500 
    501 	  SET_SBIC_cmd (rp, SBIC_CMD_RESET);
    502 	  delay(25);
    503 	  SBIC_WAIT(rp, SBIC_ASR_INT, 0);
    504 	  GET_SBIC_csr (rp, csr);       /* clears interrupt also */
    505 
    506           return;
    507         }
    508 
    509       do
    510         {
    511           SBIC_WAIT (rp, SBIC_ASR_INT, 0);
    512           GET_SBIC_csr (rp, csr);
    513         }
    514       while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    515 	      && (csr != SBIC_CSR_CMD_INVALID));
    516 #endif
    517 
    518 		/* lets just hope it worked.. */
    519 #ifdef fix_this
    520 		for (i = 0; i < 2; ++i) {
    521 			if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
    522 			    sc->sc_cur) {
    523 				printf ("siopabort: cleanup!\n");
    524 				sc->sc_iob[i].sc_xs = NULL;
    525 			}
    526 		}
    527 #endif	/* fix_this */
    528 /*		sc->sc_active = 0; */
    529 	}
    530 }
    531 
    532 void
    533 siopinitialize(sc)
    534 	struct siop_softc *sc;
    535 {
    536 	int i;
    537 	u_int inhibit_sync;
    538 	extern u_long scsi_nosync;
    539 	extern int shift_nosync;
    540 
    541 	/*
    542 	 * Need to check that scripts is on a long word boundary
    543 	 * Also should verify that dev doesn't span non-contiguous
    544 	 * physical pages.
    545 	 */
    546 	sc->sc_scriptspa = kvtop((caddr_t)scripts);
    547 
    548 	/*
    549 	 * malloc sc_acb to ensure that DS is on a long word boundary.
    550 	 */
    551 
    552 	MALLOC(sc->sc_acb, struct siop_acb *,
    553 		sizeof(struct siop_acb) * SIOP_NACB, M_DEVBUF, M_NOWAIT);
    554 	if (sc->sc_acb == NULL)
    555 		panic("siopinitialize: ACB malloc failed!");
    556 
    557 	sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
    558 	sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
    559 	sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
    560 	sc->sc_minsync = sc->sc_tcp[1];		/* in 4ns units */
    561 	if (sc->sc_minsync < 25)
    562 		sc->sc_minsync = 25;
    563 	if (sc->sc_clock_freq <= 25) {
    564 		sc->sc_dcntl |= 0x80;		/* SCLK/1 */
    565 		sc->sc_tcp[0] = sc->sc_tcp[1];
    566 	} else if (sc->sc_clock_freq <= 37) {
    567 		sc->sc_dcntl |= 0x40;		/* SCLK/1.5 */
    568 		sc->sc_tcp[0] = sc->sc_tcp[2];
    569 	} else if (sc->sc_clock_freq <= 50) {
    570 		sc->sc_dcntl |= 0x00;		/* SCLK/2 */
    571 		sc->sc_tcp[0] = sc->sc_tcp[3];
    572 	} else {
    573 		sc->sc_dcntl |= 0xc0;		/* SCLK/3 */
    574 		sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
    575 	}
    576 
    577 	if (scsi_nosync) {
    578 		inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
    579 		shift_nosync += 8;
    580 #ifdef DEBUG
    581 		if (inhibit_sync)
    582 			printf("%s: Inhibiting synchronous transfer %02x\n",
    583 				sc->sc_dev.dv_xname, inhibit_sync);
    584 #endif
    585 		for (i = 0; i < 8; ++i)
    586 			if (inhibit_sync & (1 << i))
    587 				siop_inhibit_sync[i] = 1;
    588 	}
    589 
    590 	siopreset (sc);
    591 }
    592 
    593 void
    594 siopreset(sc)
    595 	struct siop_softc *sc;
    596 {
    597 	siop_regmap_p rp;
    598 	u_int i, s;
    599 	u_char  dummy;
    600 	struct siop_acb *acb;
    601 
    602 	rp = sc->sc_siopp;
    603 
    604 	if (sc->sc_flags & SIOP_ALIVE)
    605 		siopabort(sc, rp, "reset");
    606 
    607 	printf("%s: ", sc->sc_dev.dv_xname);		/* XXXX */
    608 
    609 	s = splbio();
    610 
    611 	/*
    612 	 * Reset the chip
    613 	 * XXX - is this really needed?
    614 	 */
    615 	rp->siop_istat |= SIOP_ISTAT_ABRT;	/* abort current script */
    616 	rp->siop_istat |= SIOP_ISTAT_RST;		/* reset chip */
    617 	rp->siop_istat &= ~SIOP_ISTAT_RST;
    618 	/*
    619 	 * Reset SCSI bus (do we really want this?)
    620 	 */
    621 	rp->siop_sien = 0;
    622 	rp->siop_scntl1 |= SIOP_SCNTL1_RST;
    623 	delay(1);
    624 	rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
    625 
    626 	/*
    627 	 * Set up various chip parameters
    628 	 */
    629 	rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
    630 	rp->siop_scntl1 = SIOP_SCNTL1_ESR;
    631 	rp->siop_dcntl = sc->sc_dcntl;
    632 	rp->siop_dmode = 0x80;	/* burst length = 4 */
    633 	rp->siop_sien = 0x00;	/* don't enable interrupts yet */
    634 	rp->siop_dien = 0x00;	/* don't enable interrupts yet */
    635 	rp->siop_scid = 1 << sc->sc_link.scsipi_scsi.adapter_target;
    636 	rp->siop_dwt = 0x00;
    637 	rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
    638 	rp->siop_ctest7 |= sc->sc_ctest7;
    639 
    640 	/* will need to re-negotiate sync xfers */
    641 	bzero(&sc->sc_sync, sizeof (sc->sc_sync));
    642 
    643 	i = rp->siop_istat;
    644 	if (i & SIOP_ISTAT_SIP)
    645 		dummy = rp->siop_sstat0;
    646 	if (i & SIOP_ISTAT_DIP)
    647 		dummy = rp->siop_dstat;
    648 
    649 	splx (s);
    650 
    651 	delay (siop_reset_delay * 1000);
    652 	printf("siop id %d reset V%d\n", sc->sc_link.scsipi_scsi.adapter_target,
    653 	    rp->siop_ctest8 >> 4);
    654 
    655 	if ((sc->sc_flags & SIOP_ALIVE) == 0) {
    656 		TAILQ_INIT(&sc->ready_list);
    657 		TAILQ_INIT(&sc->nexus_list);
    658 		TAILQ_INIT(&sc->free_list);
    659 		sc->sc_nexus = NULL;
    660 		acb = sc->sc_acb;
    661 		bzero(acb, sizeof(struct siop_acb) * SIOP_NACB);
    662 		for (i = 0; i < SIOP_NACB; i++) {
    663 			TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
    664 			acb++;
    665 		}
    666 		bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
    667 	} else {
    668 		if (sc->sc_nexus != NULL) {
    669 			sc->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
    670 			siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
    671 		}
    672 		while ((acb = sc->nexus_list.tqh_first) > 0) {
    673 			acb->xs->error = XS_DRIVER_STUFFUP;
    674 			siop_scsidone(acb, acb->stat[0]);
    675 		}
    676 	}
    677 
    678 	sc->sc_flags |= SIOP_ALIVE;
    679 	sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
    680 	/* enable SCSI and DMA interrupts */
    681 	sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
    682 	    SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
    683 	sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
    684 	    /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
    685 	rp->siop_sien = sc->sc_sien;
    686 	rp->siop_dien = sc->sc_dien;
    687 }
    688 
    689 /*
    690  * Setup Data Storage for 53C710 and start SCRIPTS processing
    691  */
    692 
    693 void
    694 siop_start (sc, target, lun, cbuf, clen, buf, len)
    695 	struct siop_softc *sc;
    696 	int target;
    697 	int lun;
    698 	u_char *cbuf;
    699 	int clen;
    700 	u_char *buf;
    701 	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 = NBPG - ((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 ((caddr_t)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((caddr_t)&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(sc, istat, dstat, sstat0, status)
    873 	struct	siop_softc *sc;
    874 	u_char	istat;
    875 	u_char	dstat;
    876 	u_char	sstat0;
    877 	int	*status;
    878 {
    879 	siop_regmap_p rp = sc->sc_siopp;
    880 	struct siop_acb *acb = sc->sc_nexus;
    881 	int	target = 0;
    882 	int	dfifo, dbc, sstat1;
    883 
    884 	dfifo = rp->siop_dfifo;
    885 	dbc = rp->siop_dbc0;
    886 	sstat1 = rp->siop_sstat1;
    887 	rp->siop_ctest8 |= SIOP_CTEST8_CLF;
    888 	while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) != SIOP_CTEST1_FMT)
    889 		;
    890 	rp->siop_ctest8 &= ~SIOP_CTEST8_CLF;
    891 #ifdef DEBUG
    892 	++siopints;
    893 #if 0
    894 	if (siop_debug & 0x100) {
    895 		DCIAS(&acb->stat[0]);	/* XXX */
    896 		printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
    897 		    istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, acb->stat[0], acb->msg[0]);
    898 		printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    899 		    acb->msg[0], acb->msg[1], acb->msg[2],
    900 		    acb->msg[3], acb->msg[4], acb->msg[5]);
    901 	}
    902 #endif
    903 	if (rp->siop_dsp && (rp->siop_dsp < sc->sc_scriptspa ||
    904 	    rp->siop_dsp >= sc->sc_scriptspa + sizeof(scripts))) {
    905 		printf ("%s: dsp not within script dsp %lx scripts %lx:%lx",
    906 		    sc->sc_dev.dv_xname, rp->siop_dsp, sc->sc_scriptspa,
    907 		    sc->sc_scriptspa + sizeof(scripts));
    908 		printf(" istat %x dstat %x sstat0 %x\n",
    909 		    istat, dstat, sstat0);
    910 #ifdef DDB
    911 		Debugger();
    912 #endif
    913 	}
    914 #endif
    915 	SIOP_TRACE('i',dstat,istat,(istat&SIOP_ISTAT_DIP)?rp->siop_dsps&0xff:sstat0);
    916 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) {
    917 		/* Normal completion status, or check condition */
    918 #ifdef DEBUG
    919 		if (rp->siop_dsa != kvtop((caddr_t)&acb->ds)) {
    920 			printf ("siop: invalid dsa: %lx %x\n", rp->siop_dsa,
    921 			    kvtop((caddr_t)&acb->ds));
    922 			panic("*** siop DSA invalid ***");
    923 		}
    924 #endif
    925 		target = acb->xs->sc_link->scsipi_scsi.target;
    926 		if (sc->sc_sync[target].state == NEG_WAITS) {
    927 			if (acb->msg[1] == 0xff)
    928 				printf ("%s: target %d ignored sync request\n",
    929 				    sc->sc_dev.dv_xname, target);
    930 			else if (acb->msg[1] == MSG_REJECT)
    931 				printf ("%s: target %d rejected sync request\n",
    932 				    sc->sc_dev.dv_xname, target);
    933 			else
    934 /* XXX - need to set sync transfer parameters */
    935 				printf("%s: target %d (sync) %02x %02x %02x\n",
    936 				    sc->sc_dev.dv_xname, target, acb->msg[1],
    937 				    acb->msg[2], acb->msg[3]);
    938 			sc->sc_sync[target].state = NEG_DONE;
    939 		}
    940 		dma_cachectl(&acb->stat[0], 1);
    941 		*status = acb->stat[0];
    942 #ifdef DEBUG
    943 		if (rp->siop_sbcl & SIOP_BSY) {
    944 			/*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n",
    945 			    rp, &scripts, &acb->ds);*/
    946 #ifdef DDB
    947 			/*Debugger();*/
    948 #endif
    949 		}
    950 		if (acb->msg[0] != 0x00)
    951 			printf("%s: message was not COMMAND COMPLETE: %x\n",
    952 			    sc->sc_dev.dv_xname, acb->msg[0]);
    953 #endif
    954 		if (sc->nexus_list.tqh_first)
    955 			rp->siop_dcntl |= SIOP_DCNTL_STD;
    956 		return 1;
    957 	}
    958 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0b) {
    959 		target = acb->xs->sc_link->scsipi_scsi.target;
    960 		if (acb->msg[1] == MSG_EXT_MESSAGE && acb->msg[2] == 3 &&
    961 		    acb->msg[3] == MSG_SYNC_REQ) {
    962 #ifdef DEBUG
    963 			if (siopsync_debug)
    964 				printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    965 				    acb->msg[0], acb->msg[1], acb->msg[2],
    966 				    acb->msg[3], acb->msg[4], acb->msg[5]);
    967 #endif
    968 			sc->sc_sync[target].sxfer = 0;
    969 			sc->sc_sync[target].sbcl = 0;
    970 			if (acb->msg[2] == 3 &&
    971 			    acb->msg[3] == MSG_SYNC_REQ &&
    972 			    acb->msg[5] != 0) {
    973 #ifdef MAXTOR_KLUDGE
    974 				/*
    975 				 * Kludge for my Maxtor XT8580S
    976 				 * It accepts whatever we request, even
    977 				 * though it won't work.  So we ask for
    978 				 * a short period than we can handle.  If
    979 				 * the device says it can do it, use 208ns.
    980 				 * If the device says it can do less than
    981 				 * 100ns, then we limit it to 100ns.
    982 				 */
    983 				if (acb->msg[4] && acb->msg[4] < 100 / 4) {
    984 #ifdef DEBUG
    985 					printf ("%d: target %d wanted %dns period\n",
    986 					    sc->sc_dev.dv_xname, target,
    987 					    acb->msg[4] * 4);
    988 #endif
    989 					if (acb->msg[4] == 50 / 4)
    990 						acb->msg[4] = 208 / 4;
    991 					else
    992 						acb->msg[4] = 100 / 4;
    993 				}
    994 #endif /* MAXTOR_KLUDGE */
    995 				printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
    996 				    sc->sc_dev.dv_xname, target,
    997 				    acb->msg[4] * 4, acb->msg[5]);
    998 				scsi_period_to_siop (sc, target);
    999 			}
   1000 			rp->siop_sxfer = sc->sc_sync[target].sxfer;
   1001 			rp->siop_sbcl = sc->sc_sync[target].sbcl;
   1002 			if (sc->sc_sync[target].state == NEG_WAITS) {
   1003 				sc->sc_sync[target].state = NEG_DONE;
   1004 				rp->siop_dsp = sc->sc_scriptspa + Ent_clear_ack;
   1005 				return(0);
   1006 			}
   1007 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1008 			sc->sc_sync[target].state = NEG_DONE;
   1009 			return (0);
   1010 		}
   1011 		/* XXX - not SDTR message */
   1012 	}
   1013 	if (sstat0 & SIOP_SSTAT0_M_A) {		/* Phase mismatch */
   1014 #ifdef DEBUG
   1015 		++siopphmm;
   1016 		if (acb == NULL)
   1017 			printf("%s: Phase mismatch with no active command?\n",
   1018 			    sc->sc_dev.dv_xname);
   1019 #endif
   1020 		if (acb->iob_len) {
   1021 			int adjust;
   1022 			adjust = ((dfifo - (dbc & 0x7f)) & 0x7f);
   1023 			if (sstat1 & SIOP_SSTAT1_ORF)
   1024 				++adjust;
   1025 			if (sstat1 & SIOP_SSTAT1_OLF)
   1026 				++adjust;
   1027 			acb->iob_curlen = *((long *)&rp->siop_dcmd) & 0xffffff;
   1028 			acb->iob_curlen += adjust;
   1029 			acb->iob_curbuf = *((long *)&rp->siop_dnad) - adjust;
   1030 #ifdef DEBUG
   1031 			if (siop_debug & 0x100) {
   1032 				int i;
   1033 				printf ("Phase mismatch: curbuf %lx curlen %lx dfifo %x dbc %x sstat1 %x adjust %x sbcl %x starts %d acb %p\n",
   1034 				    acb->iob_curbuf, acb->iob_curlen, dfifo,
   1035 				    dbc, sstat1, adjust, rp->siop_sbcl, siopstarts, acb);
   1036 				if (acb->ds.chain[1].datalen) {
   1037 					for (i = 0; acb->ds.chain[i].datalen; ++i)
   1038 						printf("chain[%d] addr %p len %lx\n",
   1039 						    i, acb->ds.chain[i].databuf,
   1040 						    acb->ds.chain[i].datalen);
   1041 				}
   1042 			}
   1043 #endif
   1044 			dma_cachectl ((caddr_t)acb, sizeof(*acb));
   1045 		}
   1046 #ifdef DEBUG
   1047 		SIOP_TRACE('m',rp->siop_sbcl,(rp->siop_dsp>>8),rp->siop_dsp);
   1048 		if (siop_debug & 9)
   1049 			printf ("Phase mismatch: %x dsp +%lx dcmd %lx\n",
   1050 			    rp->siop_sbcl,
   1051 			    rp->siop_dsp - sc->sc_scriptspa,
   1052 			    *((long *)&rp->siop_dcmd));
   1053 #endif
   1054 		if ((rp->siop_sbcl & SIOP_REQ) == 0) {
   1055 			printf ("Phase mismatch: REQ not asserted! %02x dsp %lx\n",
   1056 			    rp->siop_sbcl, rp->siop_dsp);
   1057 #if defined(DEBUG) && defined(DDB)
   1058 			Debugger();
   1059 #endif
   1060 		}
   1061 		switch (rp->siop_sbcl & 7) {
   1062 		case 0:		/* data out */
   1063 		case 1:		/* data in */
   1064 		case 2:		/* status */
   1065 		case 3:		/* command */
   1066 		case 6:		/* message in */
   1067 		case 7:		/* message out */
   1068 			rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
   1069 			break;
   1070 		default:
   1071 			goto bad_phase;
   1072 		}
   1073 		return 0;
   1074 	}
   1075 	if (sstat0 & SIOP_SSTAT0_STO) {		/* Select timed out */
   1076 #ifdef DEBUG
   1077 		if (acb == NULL)
   1078 			printf("%s: Select timeout with no active command?\n",
   1079 			    sc->sc_dev.dv_xname);
   1080 		if (rp->siop_sbcl & SIOP_BSY) {
   1081 			printf ("ACK! siop was busy at timeout: rp %p script %p dsa %p\n",
   1082 			    rp, &scripts, &acb->ds);
   1083 			printf(" sbcl %x sdid %x istat %x dstat %x sstat0 %x\n",
   1084 			    rp->siop_sbcl, rp->siop_sdid, istat, dstat, sstat0);
   1085 			if (!(rp->siop_sbcl & SIOP_BSY)) {
   1086 				printf ("Yikes, it's not busy now!\n");
   1087 #if 0
   1088 				*status = -1;
   1089 				if (sc->nexus_list.tqh_first)
   1090 					rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1091 				return 1;
   1092 #endif
   1093 			}
   1094 /*			rp->siop_dcntl |= SIOP_DCNTL_STD;*/
   1095 			return (0);
   1096 #ifdef DDB
   1097 			Debugger();
   1098 #endif
   1099 		}
   1100 #endif
   1101 		*status = -1;
   1102 		acb->xs->error = XS_SELTIMEOUT;
   1103 		if (sc->nexus_list.tqh_first)
   1104 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1105 		return 1;
   1106 	}
   1107 	if (acb)
   1108 		target = acb->xs->sc_link->scsipi_scsi.target;
   1109 	else
   1110 		target = 7;
   1111 	if (sstat0 & SIOP_SSTAT0_UDC) {
   1112 #ifdef DEBUG
   1113 		if (acb == NULL)
   1114 			printf("%s: Unexpected disconnect with no active command?\n",
   1115 			    sc->sc_dev.dv_xname);
   1116 		printf ("%s: target %d disconnected unexpectedly\n",
   1117 		   sc->sc_dev.dv_xname, target);
   1118 #endif
   1119 #if 0
   1120 		siopabort (sc, rp, "siopchkintr");
   1121 #endif
   1122 		*status = STS_BUSY;
   1123 		if (sc->nexus_list.tqh_first)
   1124 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1125 		return (acb != NULL);
   1126 	}
   1127 	if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff01 ||
   1128 	    rp->siop_dsps == 0xff02)) {
   1129 #ifdef DEBUG
   1130 		if (siop_debug & 0x100)
   1131 			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",
   1132 			    sc->sc_dev.dv_xname, 1 << target, rp->siop_temp,
   1133 			    rp->siop_temp ? rp->siop_temp - sc->sc_scriptspa : 0,
   1134 			    acb->iob_curbuf, acb->iob_curlen,
   1135 			    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen, dfifo, dbc, sstat1, siopstarts, acb);
   1136 #endif
   1137 		if (acb == NULL) {
   1138 			printf("%s: Disconnect with no active command?\n",
   1139 			    sc->sc_dev.dv_xname);
   1140 			return (0);
   1141 		}
   1142 		/*
   1143 		 * XXXX need to update iob_curbuf/iob_curlen to reflect
   1144 		 * current data transferred.  If device disconnected in
   1145 		 * the middle of a DMA block, they should already be set
   1146 		 * by the phase change interrupt.  If the disconnect
   1147 		 * occurs on a DMA block boundary, we have to figure out
   1148 		 * which DMA block it was.
   1149 		 */
   1150 		if (acb->iob_len && rp->siop_temp) {
   1151 			int n = rp->siop_temp - sc->sc_scriptspa;
   1152 
   1153 			if (acb->iob_curlen && acb->iob_curlen != acb->ds.chain[0].datalen)
   1154 				printf("%s: iob_curbuf/len already set? n %x iob %lx/%lx chain[0] %p/%lx\n",
   1155 				    sc->sc_dev.dv_xname, n, acb->iob_curbuf, acb->iob_curlen,
   1156 				    acb->ds.chain[0].databuf, acb->ds.chain[0].datalen);
   1157 			if (n < Ent_datain)
   1158 				n = (n - Ent_dataout) / 16;
   1159 			else
   1160 				n = (n - Ent_datain) / 16;
   1161 			if (n <= 0 && n > DMAMAXIO)
   1162 				printf("TEMP invalid %d\n", n);
   1163 			else {
   1164 				acb->iob_curbuf = (u_long)acb->ds.chain[n].databuf;
   1165 				acb->iob_curlen = acb->ds.chain[n].datalen;
   1166 			}
   1167 #ifdef DEBUG
   1168 			if (siop_debug & 0x100) {
   1169 				printf("%s: TEMP offset %d", sc->sc_dev.dv_xname, n);
   1170 				printf(" curbuf %lx curlen %lx\n", acb->iob_curbuf,
   1171 				    acb->iob_curlen);
   1172 			}
   1173 #endif
   1174 		}
   1175 		/*
   1176 		 * If data transfer was interrupted by disconnect, iob_curbuf
   1177 		 * and iob_curlen should reflect the point of interruption.
   1178 		 * Adjust the DMA chain so that the data transfer begins
   1179 		 * at the appropriate place upon reselection.
   1180 		 * XXX This should only be done on save data pointer message?
   1181 		 */
   1182 		if (acb->iob_curlen) {
   1183 			int i, j;
   1184 
   1185 #ifdef DEBUG
   1186 			if (siop_debug & 0x100)
   1187 				printf ("%s: adjusting DMA chain\n",
   1188 				    sc->sc_dev.dv_xname);
   1189 			if (rp->siop_dsps == 0xff02)
   1190 				printf ("%s: ID %02x disconnected without Save Data Pointers\n",
   1191 				    sc->sc_dev.dv_xname, 1 << target);
   1192 #endif
   1193 			for (i = 0; i < DMAMAXIO; ++i) {
   1194 				if (acb->ds.chain[i].datalen == 0)
   1195 					break;
   1196 				if (acb->iob_curbuf >= (long)acb->ds.chain[i].databuf &&
   1197 				    acb->iob_curbuf < (long)(acb->ds.chain[i].databuf +
   1198 				    acb->ds.chain[i].datalen))
   1199 					break;
   1200 			}
   1201 			if (i >= DMAMAXIO || acb->ds.chain[i].datalen == 0) {
   1202 				printf("couldn't find saved data pointer: ");
   1203 				printf("curbuf %lx curlen %lx i %d\n",
   1204 				    acb->iob_curbuf, acb->iob_curlen, i);
   1205 #ifdef DDB
   1206 				Debugger();
   1207 #endif
   1208 			}
   1209 #ifdef DEBUG
   1210 			if (siop_debug & 0x100)
   1211 				printf("  chain[0]: %p/%lx -> %lx/%lx\n",
   1212 				    acb->ds.chain[0].databuf,
   1213 				    acb->ds.chain[0].datalen,
   1214 				    acb->iob_curbuf,
   1215 				    acb->iob_curlen);
   1216 #endif
   1217 			acb->ds.chain[0].databuf = (char *)acb->iob_curbuf;
   1218 			acb->ds.chain[0].datalen = acb->iob_curlen;
   1219 			for (j = 1, ++i; i < DMAMAXIO && acb->ds.chain[i].datalen; ++i, ++j) {
   1220 #ifdef DEBUG
   1221 			if (siop_debug & 0x100)
   1222 				printf("  chain[%d]: %p/%lx -> %p/%lx\n", j,
   1223 				    acb->ds.chain[j].databuf,
   1224 				    acb->ds.chain[j].datalen,
   1225 				    acb->ds.chain[i].databuf,
   1226 				    acb->ds.chain[i].datalen);
   1227 #endif
   1228 				acb->ds.chain[j].databuf = acb->ds.chain[i].databuf;
   1229 				acb->ds.chain[j].datalen = acb->ds.chain[i].datalen;
   1230 			}
   1231 			if (j < DMAMAXIO)
   1232 				acb->ds.chain[j].datalen = 0;
   1233 			DCIAS(kvtop((caddr_t)&acb->ds.chain));
   1234 		}
   1235 		++sc->sc_tinfo[target].dconns;
   1236 		/*
   1237 		 * add nexus to waiting list
   1238 		 * clear nexus
   1239 		 * try to start another command for another target/lun
   1240 		 */
   1241 		acb->status = sc->sc_flags & SIOP_INTSOFF;
   1242 		TAILQ_INSERT_HEAD(&sc->nexus_list, acb, chain);
   1243 		sc->sc_nexus = NULL;		/* no current device */
   1244 		/* start script to wait for reselect */
   1245 		if (sc->sc_nexus == NULL)
   1246 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
   1247 /* XXXX start another command ? */
   1248 		if (sc->ready_list.tqh_first)
   1249 			siop_sched(sc);
   1250 		return (0);
   1251 	}
   1252 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff03) {
   1253 		int reselid = rp->siop_scratch & 0x7f;
   1254 		int reselun = rp->siop_sfbr & 0x07;
   1255 
   1256 		sc->sc_sstat1 = rp->siop_sbcl;	/* XXXX save current SBCL */
   1257 #ifdef DEBUG
   1258 		if (siop_debug & 0x100)
   1259 			printf ("%s: target ID %02x reselected dsps %lx\n",
   1260 			     sc->sc_dev.dv_xname, reselid,
   1261 			     rp->siop_dsps);
   1262 		if ((rp->siop_sfbr & 0x80) == 0)
   1263 			printf("%s: Reselect message in was not identify: %x\n",
   1264 			    sc->sc_dev.dv_xname, rp->siop_sfbr);
   1265 #endif
   1266 		if (sc->sc_nexus) {
   1267 #ifdef DEBUG
   1268 			if (siop_debug & 0x100)
   1269 				printf ("%s: reselect ID %02x w/active\n",
   1270 				    sc->sc_dev.dv_xname, reselid);
   1271 #endif
   1272 			TAILQ_INSERT_HEAD(&sc->ready_list, sc->sc_nexus, chain);
   1273 			sc->sc_tinfo[sc->sc_nexus->xs->sc_link->scsipi_scsi.target].lubusy
   1274 			    &= ~(1 << sc->sc_nexus->xs->sc_link->scsipi_scsi.lun);
   1275 			--sc->sc_active;
   1276 		}
   1277 		/*
   1278 		 * locate acb of reselecting device
   1279 		 * set sc->sc_nexus to acb
   1280 		 */
   1281 		for (acb = sc->nexus_list.tqh_first; acb;
   1282 		    acb = acb->chain.tqe_next) {
   1283 			if (reselid != (acb->ds.scsi_addr >> 16) ||
   1284 			    reselun != (acb->msgout[0] & 0x07))
   1285 				continue;
   1286 			TAILQ_REMOVE(&sc->nexus_list, acb, chain);
   1287 			sc->sc_nexus = acb;
   1288 			sc->sc_flags |= acb->status;
   1289 			acb->status = 0;
   1290 			DCIAS(kvtop(&acb->stat[0]));
   1291 			rp->siop_dsa = kvtop((caddr_t)&acb->ds);
   1292 			rp->siop_sxfer =
   1293 				sc->sc_sync[acb->xs->sc_link->scsipi_scsi.target].sxfer;
   1294 			rp->siop_sbcl =
   1295 				sc->sc_sync[acb->xs->sc_link->scsipi_scsi.target].sbcl;
   1296 			break;
   1297 		}
   1298 		if (acb == NULL) {
   1299 			printf("%s: target ID %02x reselect nexus_list %p\n",
   1300 			    sc->sc_dev.dv_xname, reselid,
   1301 			    sc->nexus_list.tqh_first);
   1302 			panic("unable to find reselecting device");
   1303 		}
   1304 		dma_cachectl ((caddr_t)acb, sizeof(*acb));
   1305 		rp->siop_temp = 0;
   1306 		rp->siop_dcntl |= SIOP_DCNTL_STD;
   1307 		return (0);
   1308 	}
   1309 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff04) {
   1310 #ifdef DEBUG
   1311 		u_short ctest2 = rp->siop_ctest2;
   1312 
   1313 		/* reselect was interrupted (by Sig_P or select) */
   1314 		if (siop_debug & 0x100 ||
   1315 		    (ctest2 & SIOP_CTEST2_SIGP) == 0)
   1316 			printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x istat %x/%x\n",
   1317 			    sc->sc_dev.dv_xname, rp->siop_scntl1,
   1318 			    ctest2, rp->siop_sfbr, istat, rp->siop_istat);
   1319 #endif
   1320 		/* XXX assumes it was not select */
   1321 		if (sc->sc_nexus == NULL) {
   1322 #ifdef DEBUG
   1323 			printf("%s: reselect interrupted, sc_nexus == NULL\n",
   1324 			    sc->sc_dev.dv_xname);
   1325 #if 0
   1326 			siop_dump(sc);
   1327 #ifdef DDB
   1328 			Debugger();
   1329 #endif
   1330 #endif
   1331 #endif
   1332 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1333 			return(0);
   1334 		}
   1335 		target = sc->sc_nexus->xs->sc_link->scsipi_scsi.target;
   1336 		rp->siop_temp = 0;
   1337 		rp->siop_dsa = kvtop((caddr_t)&sc->sc_nexus->ds);
   1338 		rp->siop_sxfer = sc->sc_sync[target].sxfer;
   1339 		rp->siop_sbcl = sc->sc_sync[target].sbcl;
   1340 		rp->siop_dsp = sc->sc_scriptspa;
   1341 		return (0);
   1342 	}
   1343 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff06) {
   1344 		if (acb == NULL)
   1345 			printf("%s: Bad message-in with no active command?\n",
   1346 			    sc->sc_dev.dv_xname);
   1347 		/* Unrecognized message in byte */
   1348 		dma_cachectl (&acb->msg[1],1);
   1349 		printf ("%s: Unrecognized message in data sfbr %x msg %x sbcl %x\n",
   1350 			sc->sc_dev.dv_xname, rp->siop_sfbr, acb->msg[1], rp->siop_sbcl);
   1351 		/* what should be done here? */
   1352 		DCIAS(kvtop(&acb->msg[1]));
   1353 		rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
   1354 		return (0);
   1355 	}
   1356 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
   1357 		/* Status phase wasn't followed by message in phase? */
   1358 		printf ("%s: Status phase not followed by message in phase? sbcl %x sbdl %x\n",
   1359 			sc->sc_dev.dv_xname, rp->siop_sbcl, rp->siop_sbdl);
   1360 		if (rp->siop_sbcl == 0xa7) {
   1361 			/* It is now, just continue the script? */
   1362 			rp->siop_dcntl |= SIOP_DCNTL_STD;
   1363 			return (0);
   1364 		}
   1365 	}
   1366 	if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
   1367 		dma_cachectl (&acb->stat[0], 1);
   1368 		dma_cachectl (&acb->msg[0], 1);
   1369 		printf ("SIOP interrupt: %lx sts %x msg %x %x sbcl %x\n",
   1370 		    rp->siop_dsps, acb->stat[0], acb->msg[0], acb->msg[1],
   1371 		    rp->siop_sbcl);
   1372 		siopreset (sc);
   1373 		*status = -1;
   1374 		return 0;	/* siopreset has cleaned up */
   1375 	}
   1376 	if (sstat0 & SIOP_SSTAT0_SGE)
   1377 		printf ("SIOP: SCSI Gross Error\n");
   1378 	if (sstat0 & SIOP_SSTAT0_PAR)
   1379 		printf ("SIOP: Parity Error\n");
   1380 	if (dstat & SIOP_DSTAT_IID)
   1381 		printf ("SIOP: Invalid instruction detected\n");
   1382 bad_phase:
   1383 	/*
   1384 	 * temporary panic for unhandled conditions
   1385 	 * displays various things about the 53C710 status and registers
   1386 	 * then panics.
   1387 	 * XXXX need to clean this up to print out the info, reset, and continue
   1388 	 */
   1389 	printf ("siopchkintr: target %x ds %p\n", target, &acb->ds);
   1390 	printf ("scripts %lx ds %x rp %x dsp %lx dcmd %lx\n", sc->sc_scriptspa,
   1391 	    kvtop((caddr_t)&acb->ds), kvtop((caddr_t)rp), rp->siop_dsp,
   1392 	    *((long *)&rp->siop_dcmd));
   1393 	printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %lx dsa %lx sbcl %x sts %x msg %x %x sfbr %x\n",
   1394 	    istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa,
   1395 	     rp->siop_sbcl, acb->stat[0], acb->msg[0], acb->msg[1], rp->siop_sfbr);
   1396 #ifdef DEBUG
   1397 	if (siop_debug & 0x20)
   1398 		panic("siopchkintr: **** temp ****");
   1399 #endif
   1400 #ifdef DDB
   1401 	Debugger ();
   1402 #endif
   1403 	siopreset (sc);		/* hard reset */
   1404 	*status = -1;
   1405 	return 0;		/* siopreset cleaned up */
   1406 }
   1407 
   1408 void
   1409 siop_select(sc)
   1410 	struct siop_softc *sc;
   1411 {
   1412 	siop_regmap_p rp;
   1413 	struct siop_acb *acb = sc->sc_nexus;
   1414 
   1415 #ifdef DEBUG
   1416 	if (siop_debug & 1)
   1417 		printf ("%s: select ", sc->sc_dev.dv_xname);
   1418 #endif
   1419 
   1420 	rp = sc->sc_siopp;
   1421 	if (acb->xs->flags & SCSI_POLL || siop_no_dma) {
   1422 		sc->sc_flags |= SIOP_INTSOFF;
   1423 		sc->sc_flags &= ~SIOP_INTDEFER;
   1424 		if ((rp->siop_istat & 0x08) == 0) {
   1425 			rp->siop_sien = 0;
   1426 			rp->siop_dien = 0;
   1427 		}
   1428 #if 0
   1429 	} else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
   1430 		sc->sc_flags &= ~SIOP_INTSOFF;
   1431 		if ((rp->siop_istat & 0x08) == 0) {
   1432 			rp->siop_sien = sc->sc_sien;
   1433 			rp->siop_dien = sc->sc_dien;
   1434 		}
   1435 #endif
   1436 	}
   1437 #ifdef DEBUG
   1438 	if (siop_debug & 1)
   1439 		printf ("siop_select: target %x cmd %02x ds %p\n",
   1440 		    acb->xs->sc_link->scsipi_scsi.target, acb->cmd.opcode,
   1441 		    &sc->sc_nexus->ds);
   1442 #endif
   1443 
   1444 	siop_start(sc, acb->xs->sc_link->scsipi_scsi.target,
   1445 		acb->xs->sc_link->scsipi_scsi.lun,
   1446 	    (u_char *)&acb->cmd, acb->clen, acb->daddr, acb->dleft);
   1447 
   1448 	return;
   1449 }
   1450 
   1451 /*
   1452  * 53C710 interrupt handler
   1453  */
   1454 
   1455 void
   1456 siopintr (sc)
   1457 	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->sc_link->scsipi_scsi.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 (sc, target)
   1532 	struct siop_softc *sc;
   1533 	int target;
   1534 {
   1535 	int period, offset, sxfer, sbcl = 0;
   1536 #ifdef DEBUG_SYNC
   1537 	int i;
   1538 #endif
   1539 
   1540 	period = sc->sc_nexus->msg[4];
   1541 	offset = sc->sc_nexus->msg[5];
   1542 #ifdef DEBUG_SYNC
   1543 	sxfer = 0;
   1544 	if (offset <= SIOP_MAX_OFFSET)
   1545 		sxfer = offset;
   1546 	for (i = 0; i < sizeof (sync_tab) / 2; ++i) {
   1547 		if (period <= sync_tab[i].p) {
   1548 			sxfer |= sync_tab[i].r & 0x70;
   1549 			sbcl = sync_tab[i].r & 0x03;
   1550 			break;
   1551 		}
   1552 	}
   1553 	printf ("siop sync old: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
   1554 #endif
   1555 	for (sbcl = 1; sbcl < 4; ++sbcl) {
   1556 		sxfer = (period * 4 - 1) / sc->sc_tcp[sbcl] - 3;
   1557 		if (sxfer >= 0 && sxfer <= 7)
   1558 			break;
   1559 	}
   1560 	if (sbcl > 3) {
   1561 		printf("siop sync: unable to compute sync params for period %dns\n",
   1562 		    period * 4);
   1563 		/*
   1564 		 * XXX need to pick a value we can do and renegotiate
   1565 		 */
   1566 		sxfer = sbcl = 0;
   1567 	} else {
   1568 		sxfer = (sxfer << 4) | ((offset <= SIOP_MAX_OFFSET) ?
   1569 		    offset : SIOP_MAX_OFFSET);
   1570 #ifdef DEBUG_SYNC
   1571 		printf("siop sync: params for period %dns: sxfer %x sbcl %x",
   1572 		    period * 4, sxfer, sbcl);
   1573 		printf(" actual period %dns\n",
   1574 		    sc->sc_tcp[sbcl] * ((sxfer >> 4) + 4));
   1575 #endif
   1576 	}
   1577 	sc->sc_sync[target].sxfer = sxfer;
   1578 	sc->sc_sync[target].sbcl = sbcl;
   1579 #ifdef DEBUG_SYNC
   1580 	printf ("siop sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, sbcl);
   1581 #endif
   1582 }
   1583 
   1584 #ifdef DEBUG
   1585 
   1586 #if SIOP_TRACE_SIZE
   1587 void
   1588 siop_dump_trace()
   1589 {
   1590 	int i;
   1591 
   1592 	printf("siop trace: next index %d\n", siop_trix);
   1593 	i = siop_trix;
   1594 	do {
   1595 		printf("%3d: '%c' %02x %02x %02x\n", i, siop_trbuf[i],
   1596 		    siop_trbuf[i + 1], siop_trbuf[i + 2], siop_trbuf[i + 3]);
   1597 		i = (i + 4) & (SIOP_TRACE_SIZE - 1);
   1598 	} while (i != siop_trix);
   1599 }
   1600 #endif
   1601 
   1602 void
   1603 siop_dump_acb(acb)
   1604 	struct siop_acb *acb;
   1605 {
   1606 	u_char *b = (u_char *) &acb->cmd;
   1607 	int i;
   1608 
   1609 	printf("acb@%p ", acb);
   1610 	if (acb->xs == NULL) {
   1611 		printf("<unused>\n");
   1612 		return;
   1613 	}
   1614 	printf("(%d:%d) flags %2x clen %2d cmd ",
   1615 		acb->xs->sc_link->scsipi_scsi.target,
   1616 	    acb->xs->sc_link->scsipi_scsi.lun, acb->flags, acb->clen);
   1617 	for (i = acb->clen; i; --i)
   1618 		printf(" %02x", *b++);
   1619 	printf("\n");
   1620 	printf("  xs: %p data %p:%04x ", acb->xs, acb->xs->data,
   1621 	    acb->xs->datalen);
   1622 	printf("va %p:%lx ", acb->iob_buf, acb->iob_len);
   1623 	printf("cur %lx:%lx\n", acb->iob_curbuf, acb->iob_curlen);
   1624 }
   1625 
   1626 void
   1627 siop_dump(sc)
   1628 	struct siop_softc *sc;
   1629 {
   1630 	struct siop_acb *acb;
   1631 	siop_regmap_p rp = sc->sc_siopp;
   1632 	int s;
   1633 	int i;
   1634 
   1635 	s = splbio();
   1636 #if SIOP_TRACE_SIZE
   1637 	siop_dump_trace();
   1638 #endif
   1639 	printf("%s@%p regs %p istat %x\n",
   1640 	    sc->sc_dev.dv_xname, sc, rp, rp->siop_istat);
   1641 	if ((acb = sc->free_list.tqh_first) > 0) {
   1642 		printf("Free list:\n");
   1643 		while (acb) {
   1644 			siop_dump_acb(acb);
   1645 			acb = acb->chain.tqe_next;
   1646 		}
   1647 	}
   1648 	if ((acb = sc->ready_list.tqh_first) > 0) {
   1649 		printf("Ready list:\n");
   1650 		while (acb) {
   1651 			siop_dump_acb(acb);
   1652 			acb = acb->chain.tqe_next;
   1653 		}
   1654 	}
   1655 	if ((acb = sc->nexus_list.tqh_first) > 0) {
   1656 		printf("Nexus list:\n");
   1657 		while (acb) {
   1658 			siop_dump_acb(acb);
   1659 			acb = acb->chain.tqe_next;
   1660 		}
   1661 	}
   1662 	if (sc->sc_nexus) {
   1663 		printf("Nexus:\n");
   1664 		siop_dump_acb(sc->sc_nexus);
   1665 	}
   1666 	for (i = 0; i < 8; ++i) {
   1667 		if (sc->sc_tinfo[i].cmds > 2) {
   1668 			printf("tgt %d: cmds %d disc %d senses %d lubusy %x\n",
   1669 			    i, sc->sc_tinfo[i].cmds,
   1670 			    sc->sc_tinfo[i].dconns,
   1671 			    sc->sc_tinfo[i].senses,
   1672 			    sc->sc_tinfo[i].lubusy);
   1673 		}
   1674 	}
   1675 	splx(s);
   1676 }
   1677 #endif
   1678