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