Home | History | Annotate | Line # | Download | only in dev
siop.c revision 1.20
      1 /*	$NetBSD: siop.c,v 1.20 1995/02/12 19:19:28 chopps 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 /* need to know if any tapes have been configured */
     47 #include "st.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/disklabel.h>
     53 #include <sys/dkstat.h>
     54 #include <sys/buf.h>
     55 #include <scsi/scsi_all.h>
     56 #include <scsi/scsiconf.h>
     57 #include <machine/cpu.h>
     58 #include <amiga/amiga/custom.h>
     59 #include <amiga/amiga/isr.h>
     60 #include <amiga/dev/siopreg.h>
     61 #include <amiga/dev/siopvar.h>
     62 
     63 extern u_int	kvtop();
     64 
     65 /*
     66  * SCSI delays
     67  * In u-seconds, primarily for state changes on the SPC.
     68  */
     69 #define	SCSI_CMD_WAIT	500000	/* wait per step of 'immediate' cmds */
     70 #define	SCSI_DATA_WAIT	500000	/* wait per data in/out step */
     71 #define	SCSI_INIT_WAIT	500000	/* wait per step (both) during init */
     72 
     73 int  siopicmd __P((struct siop_softc *, int, int, void *, int, void *, int));
     74 int  siopgo __P((struct siop_softc *, struct scsi_xfer *));
     75 int  siopgetsense __P((struct siop_softc *, struct scsi_xfer *));
     76 void siopabort __P((struct siop_softc *, siop_regmap_p, char *));
     77 void sioperror __P((struct siop_softc *, siop_regmap_p, u_char));
     78 void siopstart __P((struct siop_softc *));
     79 void siopreset __P((struct siop_softc *));
     80 void siopsetdelay __P((int));
     81 void siop_scsidone __P((struct siop_softc *, int));
     82 void siop_donextcmd __P((struct siop_softc *));
     83 int  siopintr __P((struct siop_softc *));
     84 
     85 /* 53C710 script */
     86 #include <amiga/dev/siop_script.out>
     87 
     88 /* default to not inhibit sync negotiation on any drive */
     89 u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
     90 u_char siop_allow_disc[8] = {0, 0, 0, 0, 0, 0, 0, 0};
     91 int siop_no_dma = 0;
     92 
     93 int siop_reset_delay = 250;	/* delay after reset, in milleseconds */
     94 int siop_sync_period = 50;	/* synchronous transfer period, in nanoseconds */
     95 
     96 int siop_cmd_wait = SCSI_CMD_WAIT;
     97 int siop_data_wait = SCSI_DATA_WAIT;
     98 int siop_init_wait = SCSI_INIT_WAIT;
     99 
    100 /*
    101  * sync period transfer lookup - only valid for 66Mhz clock
    102  */
    103 static struct {
    104 	unsigned char x;	/* period from sync request message */
    105 	unsigned char y;	/* siop_period << 4 | sbcl */
    106 } xxx[] = {
    107 	{ 60/4, 0<<4 | 1},
    108 	{ 76/4, 1<<4 | 1},
    109 	{ 92/4, 2<<4 | 1},
    110 	{ 92/4, 0<<4 | 2},
    111 	{108/4, 3<<4 | 1},
    112 	{116/4, 1<<4 | 2},
    113 	{120/4, 4<<4 | 1},
    114 	{120/4, 0<<4 | 3},
    115 	{136/4, 5<<4 | 1},
    116 	{140/4, 2<<4 | 2},
    117 	{152/4, 6<<4 | 1},
    118 	{152/4, 1<<4 | 3},
    119 	{164/4, 3<<4 | 2},
    120 	{168/4, 7<<4 | 1},
    121 	{180/4, 2<<4 | 3},
    122 	{184/4, 4<<4 | 2},
    123 	{208/4, 5<<4 | 2},
    124 	{212/4, 3<<4 | 3},
    125 	{232/4, 6<<4 | 2},
    126 	{240/4, 4<<4 | 3},
    127 	{256/4, 7<<4 | 2},
    128 	{272/4, 5<<4 | 3},
    129 	{300/4, 6<<4 | 3},
    130 	{332/4, 7<<4 | 3}
    131 };
    132 
    133 #ifdef DEBUG
    134 #define QPRINTF(a) if (siop_debug > 1) printf a
    135 /*
    136  *	0x01 - full debug
    137  *	0x02 - DMA chaining
    138  *	0x04 - siopintr
    139  *	0x08 - phase mismatch
    140  *	0x10 - panic on phase mismatch
    141  *	0x20 - panic on unhandled exceptions
    142  *	0x100 - disconnect/reconnect
    143  */
    144 int	siop_debug = 0;
    145 int	siopsync_debug = 0;
    146 int	siopdma_hits = 0;
    147 int	siopdma_misses = 0;
    148 int	siopchain_ints = 0;
    149 int	siopstarts = 0;
    150 int	siopints = 0;
    151 int	siopphmm = 0;
    152 #else
    153 #define QPRINTF(a)
    154 #endif
    155 
    156 
    157 /*
    158  * default minphys routine for siop based controllers
    159  */
    160 void
    161 siop_minphys(bp)
    162 	struct buf *bp;
    163 {
    164 	/*
    165 	 * no max transfer at this level
    166 	 */
    167 }
    168 
    169 /*
    170  * used by specific siop controller
    171  *
    172  */
    173 int
    174 siop_scsicmd(xs)
    175 	struct scsi_xfer *xs;
    176 {
    177 	struct siop_pending *pendp;
    178 	struct siop_softc *sc;
    179 	struct scsi_link *slp;
    180 	int flags, s, i;
    181 
    182 	slp = xs->sc_link;
    183 	sc = slp->adapter_softc;
    184 	flags = xs->flags;
    185 
    186 	if (flags & SCSI_DATA_UIO)
    187 		panic("siop: scsi data uio requested");
    188 
    189 	if (sc->sc_xs && flags & SCSI_POLL)
    190 		panic("siop_scsicmd: busy");
    191 
    192 	s = splbio();
    193 	pendp = &sc->sc_xsstore[slp->target][slp->lun];
    194 	if (pendp->xs) {
    195 		splx(s);
    196 		return(TRY_AGAIN_LATER);
    197 	}
    198 
    199 	if (sc->sc_xs) {
    200 		pendp->xs = xs;
    201 		TAILQ_INSERT_TAIL(&sc->sc_xslist, pendp, link);
    202 		splx(s);
    203 		return(SUCCESSFULLY_QUEUED);
    204 	}
    205 	pendp->xs = NULL;
    206 	sc->sc_xs = xs;
    207 	splx(s);
    208 
    209 	/*
    210 	 * nothing is pending do it now.
    211 	 */
    212 	siop_donextcmd(sc);
    213 
    214 	if (flags & SCSI_POLL)
    215 		return(COMPLETE);
    216 	return(SUCCESSFULLY_QUEUED);
    217 }
    218 
    219 /*
    220  * entered with sc->sc_xs pointing to the next xfer to perform
    221  */
    222 void
    223 siop_donextcmd(sc)
    224 	struct siop_softc *sc;
    225 {
    226 	struct scsi_xfer *xs;
    227 	struct scsi_link *slp;
    228 	int flags, stat, i;
    229 
    230 	xs = sc->sc_xs;
    231 	slp = xs->sc_link;
    232 	flags = xs->flags;
    233 
    234 	if (flags & SCSI_RESET)
    235 		siopreset(sc);
    236 
    237 	xs->cmd->bytes[0] |= slp->lun << 5;
    238 	for (i = 0; i < 2; ++i) {
    239 		if (sc->sc_iob[i].sc_xs == NULL) {
    240 			sc->sc_iob[i].sc_xs = xs;
    241 			sc->sc_cur = & sc->sc_iob[i];
    242 			sc->sc_iob[i].sc_stat[0] = -1;
    243 			break;
    244 		}
    245 	}
    246 	if (sc->sc_cur == NULL)
    247 		panic ("siop: too many I/O's");
    248 	++sc->sc_active;
    249 #if 0
    250 if (sc->sc_active > 1) {
    251   printf ("active count %d\n", sc->sc_active);
    252 }
    253 #endif
    254 	if (flags & SCSI_POLL || siop_no_dma)
    255 		stat = siopicmd(sc, slp->target, slp->lun, xs->cmd,
    256 		    xs->cmdlen, xs->data, xs->datalen);
    257 	else if (siopgo(sc, xs) == 0)
    258 		return;
    259 	else
    260 		stat = sc->sc_cur->sc_stat[0];
    261 
    262 	siop_scsidone(sc, stat);
    263 }
    264 
    265 void
    266 siop_scsidone(sc, stat)
    267 	struct siop_softc *sc;
    268 	int stat;
    269 {
    270 	struct siop_pending *pendp;
    271 	struct scsi_xfer *xs;
    272 	int s, donext;
    273 
    274 	xs = sc->sc_xs;
    275 #ifdef DIAGNOSTIC
    276 	if (xs == NULL)
    277 		panic("siop_scsidone");
    278 #endif
    279 #if 1
    280 	if (((struct device *)(xs->sc_link->device_softc))->dv_unit < dk_ndrive)
    281 		++dk_xfer[((struct device *)(xs->sc_link->device_softc))->dv_unit];
    282 #endif
    283 	/*
    284 	 * is this right?
    285 	 */
    286 	xs->status = stat;
    287 
    288 	if (stat == 0)
    289 		xs->resid = 0;
    290 	else {
    291 		switch(stat) {
    292 		case SCSI_CHECK:
    293 			if (stat = siopgetsense(sc, xs))
    294 				goto bad_sense;
    295 			xs->error = XS_SENSE;
    296 if ((xs->sense.extended_flags & SSD_KEY) == 0x0b) { /* ABORTED COMMAND */
    297   printf ("%s: command %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    298     sc->sc_dev.dv_xname,
    299     xs->cmd->opcode, xs->cmd->bytes[0], xs->cmd->bytes[1], xs->cmd->bytes[1],
    300     xs->cmd->bytes[3], xs->cmd->bytes[4], xs->cmd->bytes[5],
    301     xs->cmd->bytes[6], xs->cmd->bytes[7], xs->cmd->bytes[8]);
    302   printf ("%s: sense %02x %02x %02x %02x %02x %02x %02 extlen %02x: %02x %02x\n",
    303     sc->sc_dev.dv_xname,
    304     xs->sense.error_code, xs->sense.extended_segment,
    305     xs->sense.extended_flags, xs->sense.extended_info[0],
    306     xs->sense.extended_info[1], xs->sense.extended_info[2],
    307     xs->sense.extended_info[3], xs->sense.extended_extra_len,
    308     xs->sense.extended_extra_bytes[0], xs->sense.extended_extra_bytes[1]);
    309 }
    310 			break;
    311 		case SCSI_BUSY:
    312 			xs->error = XS_BUSY;
    313 			break;
    314 		bad_sense:
    315 		default:
    316 			xs->error = XS_DRIVER_STUFFUP;
    317 			QPRINTF(("siop_scsicmd() bad %x\n", stat));
    318 			break;
    319 		}
    320 	}
    321 	xs->flags |= ITSDONE;
    322 
    323 	--sc->sc_active;
    324 	sc->sc_cur->sc_xs = NULL;
    325 	sc->sc_cur = NULL;
    326 	/*
    327 	 * if another command is already pending (i.e. it was pre-empted
    328 	 * by a reconnecting device), make it the current command and
    329 	 * continue
    330 	 */
    331 
    332 	/*
    333 	 * grab next command before scsi_done()
    334 	 * this way no single device can hog scsi resources.
    335 	 */
    336 	s = splbio();
    337 	pendp = sc->sc_xslist.tqh_first;
    338 	if (pendp == NULL) {
    339 		donext = 0;
    340 		sc->sc_xs = NULL;
    341 	} else {
    342 		donext = 1;
    343 		TAILQ_REMOVE(&sc->sc_xslist, pendp, link);
    344 		sc->sc_xs = pendp->xs;
    345 		pendp->xs = NULL;
    346 	}
    347 	splx(s);
    348 	scsi_done(xs);
    349 
    350 	if (donext)
    351 		siop_donextcmd(sc);
    352 	else if (sc->sc_active && sc->sc_xs == NULL) {
    353 /*		printf ("siop_scsidone: still active %d\n", sc->sc_active);*/
    354 /*		sc->sc_siopp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;*/
    355 	}
    356 }
    357 
    358 int
    359 siopgetsense(sc, xs)
    360 	struct siop_softc *sc;
    361 	struct scsi_xfer *xs;
    362 {
    363 	struct scsi_sense rqs;
    364 	struct scsi_link *slp;
    365 	int stat;
    366 
    367 	slp = xs->sc_link;
    368 
    369 	rqs.opcode = REQUEST_SENSE;
    370 	rqs.byte2 = slp->lun << 5;
    371 #ifdef not_yet
    372 	rqs.length = xs->req_sense_length ? xs->req_sense_length :
    373 	    sizeof(xs->sense);
    374 #else
    375 	rqs.length = sizeof(xs->sense);
    376 #endif
    377 	if (rqs.length > sizeof (xs->sense))
    378 		rqs.length = sizeof (xs->sense);
    379 	rqs.unused[0] = rqs.unused[1] = rqs.control = 0;
    380 
    381 	return(siopicmd(sc, slp->target, slp->lun, &rqs, sizeof(rqs),
    382 	    &xs->sense, rqs.length));
    383 }
    384 
    385 void
    386 siopabort(sc, rp, where)
    387 	register struct siop_softc *sc;
    388 	siop_regmap_p rp;
    389 	char *where;
    390 {
    391 	int i;
    392 
    393 	printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
    394 	    sc->sc_dev.dv_xname,
    395 	    where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
    396 
    397 	if (sc->sc_active > 0) {
    398 #ifdef TODO
    399       SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
    400       WAIT_CIP (rp);
    401 
    402       GET_SBIC_asr (rp, asr);
    403       if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
    404         {
    405           /* ok, get more drastic.. */
    406 
    407 	  SET_SBIC_cmd (rp, SBIC_CMD_RESET);
    408 	  delay(25);
    409 	  SBIC_WAIT(rp, SBIC_ASR_INT, 0);
    410 	  GET_SBIC_csr (rp, csr);       /* clears interrupt also */
    411 
    412           sc->sc_flags &= ~SIOP_SELECTED;
    413           return;
    414         }
    415 
    416       do
    417         {
    418           SBIC_WAIT (rp, SBIC_ASR_INT, 0);
    419           GET_SBIC_csr (rp, csr);
    420         }
    421       while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    422 	      && (csr != SBIC_CSR_CMD_INVALID));
    423 #endif
    424 
    425 		/* lets just hope it worked.. */
    426 		sc->sc_flags &= ~SIOP_SELECTED;
    427 		for (i = 0; i < 2; ++i) {
    428 			if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
    429 			    sc->sc_cur) {
    430 				printf ("siopabort: cleanup!\n");
    431 				sc->sc_iob[i].sc_xs = NULL;
    432 			}
    433 		}
    434 		sc->sc_active = 0;
    435 	}
    436 }
    437 
    438 void
    439 siopinitialize(sc)
    440 	struct siop_softc *sc;
    441 {
    442 	/*
    443 	 * Need to check that scripts is on a long word boundary
    444 	 * and that DS is on a long word boundary.
    445 	 * Also need to verify that dev doesn't span non-contiguous
    446 	 * physical pages.
    447 	 */
    448 	sc->sc_scriptspa = kvtop(scripts);
    449 #if 0
    450 	sc->sc_dspa = kvtop(&sc->sc_ds);
    451 	sc->sc_lunpa = kvtop(&sc->sc_msgout);
    452 	sc->sc_statuspa = kvtop(&sc->sc_stat[0]);
    453 	sc->sc_msgpa = kvtop(&sc->sc_msg[0]);
    454 #endif
    455 	siopreset (sc);
    456 }
    457 
    458 void
    459 siopreset(sc)
    460 	struct siop_softc *sc;
    461 {
    462 	siop_regmap_p rp;
    463 	u_int i, s;
    464 	u_char  my_id, dummy;
    465 
    466 	rp = sc->sc_siopp;
    467 
    468 	if (sc->sc_flags & SIOP_ALIVE)
    469 		siopabort(sc, rp, "reset");
    470 
    471 	printf("%s: ", sc->sc_dev.dv_xname);		/* XXXX */
    472 
    473 	s = splbio();
    474 	my_id = 7;
    475 
    476 	/*
    477 	 * Reset the chip
    478 	 * XXX - is this really needed?
    479 	 */
    480 	rp->siop_istat |= SIOP_ISTAT_ABRT;	/* abort current script */
    481 	rp->siop_istat |= SIOP_ISTAT_RST;		/* reset chip */
    482 	rp->siop_istat &= ~SIOP_ISTAT_RST;
    483 	/*
    484 	 * Reset SCSI bus (do we really want this?)
    485 	 */
    486 	rp->siop_sien &= ~SIOP_SIEN_RST;
    487 	rp->siop_scntl1 |= SIOP_SCNTL1_RST;
    488 	delay(1);
    489 	rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
    490 /*	rp->siop_sien |= SIOP_SIEN_RST;*/
    491 
    492 	/*
    493 	 * Set up various chip parameters
    494 	 */
    495 	rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
    496 	rp->siop_dcntl = sc->sc_clock_freq & 0xff;
    497 	rp->siop_dmode = 0x80;	/* burst length = 4 */
    498 	rp->siop_sien = 0x00;	/* don't enable interrupts yet */
    499 	rp->siop_dien = 0x00;	/* don't enable interrupts yet */
    500 	rp->siop_scid = 1 << my_id;
    501 	rp->siop_dwt = 0x00;
    502 	rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
    503 	rp->siop_ctest7 |= (sc->sc_clock_freq >> 8) & 0xff;
    504 
    505 	/* will need to re-negotiate sync xfers */
    506 	bzero(&sc->sc_sync, sizeof (sc->sc_sync));
    507 
    508 	i = rp->siop_istat;
    509 	if (i & SIOP_ISTAT_SIP)
    510 		dummy = rp->siop_sstat0;
    511 	if (i & SIOP_ISTAT_DIP)
    512 		dummy = rp->siop_dstat;
    513 
    514 	splx (s);
    515 
    516  	delay (siop_reset_delay * 1000);
    517 	printf("siop id %d reset V%d\n", my_id, rp->siop_ctest8 >> 4);
    518 	sc->sc_flags |= SIOP_ALIVE;
    519 	sc->sc_flags &= ~(SIOP_SELECTED | SIOP_DMA);
    520 }
    521 
    522 /*
    523  * Setup Data Storage for 53C710 and start SCRIPTS processing
    524  */
    525 
    526 void
    527 siop_setup (sc, target, lun, cbuf, clen, buf, len)
    528 	struct siop_softc *sc;
    529 	int target;
    530 	int lun;
    531 	u_char *cbuf;
    532 	int clen;
    533 	u_char *buf;
    534 	int len;
    535 {
    536 	siop_regmap_p rp = sc->sc_siopp;
    537 	int i;
    538 	int nchain;
    539 	int count, tcount;
    540 	char *addr, *dmaend;
    541 	struct siop_iob *iob = sc->sc_cur;
    542 
    543 #ifdef DEBUG
    544 	if (rp->siop_sbcl & SIOP_BSY) {
    545 		printf ("ACK! siop was busy: rp %x script %x dsa %x\n",
    546 		    rp, &scripts, &iob->sc_ds);
    547 		/*Debugger();*/
    548 	}
    549 	i = rp->siop_istat;
    550 	if (i & SIOP_ISTAT_DIP) {
    551 		int dstat;
    552 		dstat = rp->siop_dstat;
    553 	}
    554 	if (i & SIOP_ISTAT_SIP) {
    555 		int sstat;
    556 		sstat = rp->siop_sstat0;
    557 	}
    558 #endif
    559 	sc->sc_istat = 0;
    560 	iob->sc_msgout[0] = MSG_IDENTIFY | lun;
    561 /*
    562  * HACK - if no data transfer, allow disconnects
    563  */
    564 	if (/*len == 0 || */ siop_allow_disc[target]) {
    565 		iob->sc_msgout[0] = MSG_IDENTIFY_DR | lun;
    566 		rp->siop_scntl1 |= SIOP_SCNTL1_ESR;
    567 	}
    568 	iob->sc_status = 0;
    569 	iob->sc_stat[0] = -1;
    570 	iob->sc_msg[0] = -1;
    571 	iob->sc_ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].period << 8);
    572 	iob->sc_ds.idlen = 1;
    573 	iob->sc_ds.idbuf = (char *) kvtop(&iob->sc_msgout[0]);
    574 	iob->sc_ds.cmdlen = clen;
    575 	iob->sc_ds.cmdbuf = (char *) kvtop(cbuf);
    576 	iob->sc_ds.stslen = 1;
    577 	iob->sc_ds.stsbuf = (char *) kvtop(&iob->sc_stat[0]);
    578 	iob->sc_ds.msglen = 1;
    579 	iob->sc_ds.msgbuf = (char *) kvtop(&iob->sc_msg[0]);
    580 	iob->sc_msg[1] = -1;
    581 	iob->sc_ds.msginlen = 1;
    582 	iob->sc_ds.extmsglen = 1;
    583 	iob->sc_ds.synmsglen = 3;
    584 	iob->sc_ds.msginbuf = (char *) kvtop(&iob->sc_msg[1]);
    585 	iob->sc_ds.extmsgbuf = (char *) kvtop(&iob->sc_msg[2]);
    586 	iob->sc_ds.synmsgbuf = (char *) kvtop(&iob->sc_msg[3]);
    587 	bzero(&iob->sc_ds.chain, sizeof (iob->sc_ds.chain));
    588 
    589 	if (sc->sc_sync[target].state == SYNC_START) {
    590 		if (siop_inhibit_sync[target]) {
    591 			sc->sc_sync[target].state = SYNC_DONE;
    592 			sc->sc_sync[target].offset = 0;
    593 			sc->sc_sync[target].period = 0;
    594 #ifdef DEBUG
    595 			if (siopsync_debug)
    596 				printf ("Forcing target %d asynchronous\n", target);
    597 #endif
    598 		}
    599 		else {
    600 			iob->sc_msg[2] = -1;
    601 			iob->sc_msgout[1] = MSG_EXT_MESSAGE;
    602 			iob->sc_msgout[2] = 3;
    603 			iob->sc_msgout[3] = MSG_SYNC_REQ;
    604 			iob->sc_msgout[4] = siop_sync_period / 4;
    605 			iob->sc_msgout[5] = SIOP_MAX_OFFSET;
    606 			iob->sc_ds.idlen = 6;
    607 			sc->sc_sync[target].state = SYNC_SENT;
    608 #ifdef DEBUG
    609 			if (siopsync_debug)
    610 				printf ("Sending sync request to target %d\n", target);
    611 #endif
    612 		}
    613 	}
    614 
    615 /*
    616  * If length is > 1 page, check for consecutive physical pages
    617  * Need to set up chaining if not
    618  */
    619 	iob->iob_buf = buf;
    620 	iob->iob_len = len;
    621 	iob->iob_curbuf = iob->iob_curlen = 0;
    622 	nchain = 0;
    623 	count = len;
    624 	addr = buf;
    625 	dmaend = NULL;
    626 	while (count > 0) {
    627 		iob->sc_ds.chain[nchain].databuf = (char *) kvtop (addr);
    628 		if (count < (tcount = NBPG - ((int) addr & PGOFSET)))
    629 			tcount = count;
    630 		iob->sc_ds.chain[nchain].datalen = tcount;
    631 		addr += tcount;
    632 		count -= tcount;
    633 		if (iob->sc_ds.chain[nchain].databuf == dmaend) {
    634 			dmaend += iob->sc_ds.chain[nchain].datalen;
    635 			iob->sc_ds.chain[--nchain].datalen += tcount;
    636 #ifdef DEBUG
    637 			++siopdma_hits;
    638 #endif
    639 		}
    640 		else {
    641 			dmaend = iob->sc_ds.chain[nchain].databuf +
    642 			    iob->sc_ds.chain[nchain].datalen;
    643 			iob->sc_ds.chain[nchain].datalen = tcount;
    644 #ifdef DEBUG
    645 			if (nchain)	/* Don't count miss on first one */
    646 				++siopdma_misses;
    647 #endif
    648 		}
    649 		++nchain;
    650 	}
    651 #ifdef DEBUG
    652 	if (nchain != 1 && len != 0 && siop_debug & 3) {
    653 		printf ("DMA chaining set: %d\n", nchain);
    654 		for (i = 0; i < nchain; ++i) {
    655 			printf ("  [%d] %8x %4x\n", i, iob->sc_ds.chain[i].databuf,
    656 			    iob->sc_ds.chain[i].datalen);
    657 		}
    658 	}
    659 	if (rp->siop_sbcl & SIOP_BSY) {
    660 		printf ("ACK! siop was busy at start: rp %x script %x dsa %x\n",
    661 		    rp, &scripts, &iob->sc_ds);
    662 		/*Debugger();*/
    663 	}
    664 #endif
    665 
    666 	/* push data case on data the 53c710 needs to access */
    667 	dma_cachectl (sc, sizeof (struct siop_softc));
    668 	dma_cachectl (cbuf, clen);
    669 	if (buf != NULL && len != 0)
    670 		dma_cachectl (buf, len);
    671 	if (sc->sc_active <= 1) {
    672 		rp->siop_sbcl = sc->sc_sync[target].offset;
    673 		rp->siop_dsa = kvtop(&iob->sc_ds);
    674 /* XXX if disconnected devices pending, this may not work */
    675 		rp->siop_dsp = sc->sc_scriptspa;
    676 	} else {
    677 		rp->siop_istat = SIOP_ISTAT_SIGP;
    678 	}
    679 #ifdef DEBUG
    680 	++siopstarts;
    681 #endif
    682 }
    683 
    684 /*
    685  * Process a DMA or SCSI interrupt from the 53C710 SIOP
    686  */
    687 
    688 int
    689 siop_checkintr(sc, istat, dstat, sstat0, status)
    690 	struct	siop_softc *sc;
    691 	u_char	istat;
    692 	u_char	dstat;
    693 	u_char	sstat0;
    694 	int	*status;
    695 {
    696 	siop_regmap_p rp = sc->sc_siopp;
    697 	struct siop_iob *iob = sc->sc_cur;
    698 	int	target;
    699 	int	dfifo, dbc, sstat1;
    700 
    701 	dfifo = rp->siop_dfifo;
    702 	dbc = rp->siop_dbc0;
    703 	sstat1 = rp->siop_sstat1;
    704 	rp->siop_ctest8 |= SIOP_CTEST8_CLF;
    705 	while ((rp->siop_ctest1 & SIOP_CTEST1_FMT) == 0)
    706 		;
    707 	rp->siop_ctest8 &= ~SIOP_CTEST8_CLF;
    708 #ifdef DEBUG
    709 	++siopints;
    710 	if (siop_debug & 0x100) {
    711 		DCIAS(&iob->sc_stat[0]);	/* XXX */
    712 		printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
    713 		    istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl, iob->sc_stat[0], iob->sc_msg[0]);
    714 		printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    715 		    iob->sc_msg[0], iob->sc_msg[1], iob->sc_msg[2],
    716 		    iob->sc_msg[3], iob->sc_msg[4], iob->sc_msg[5]);
    717 	}
    718 #endif
    719 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff00) {
    720 		/* Normal completion status, or check condition */
    721 		if (rp->siop_dsa != kvtop(&iob->sc_ds)) {
    722 			printf ("siop: invalid dsa: %x %x\n", rp->siop_dsa,
    723 			    kvtop(&iob->sc_ds));
    724 			panic("*** siop DSA invalid ***");
    725 		}
    726 		target = sc->sc_slave;
    727 		if (sc->sc_sync[target].state == SYNC_SENT) {
    728 #ifdef DEBUG
    729 			if (siopsync_debug)
    730 				printf ("sync msg in: %02x %02x %02x %02x %02x %02x\n",
    731 				    iob->sc_msg[0], iob->sc_msg[1], iob->sc_msg[2],
    732 				    iob->sc_msg[3], iob->sc_msg[4], iob->sc_msg[5]);
    733 #endif
    734 			if (iob->sc_msg[1] == 0xff)
    735 				printf ("%s: target %d ignored sync request\n",
    736 				    sc->sc_dev.dv_xname, target);
    737 			else if (iob->sc_msg[1] == MSG_REJECT)
    738 				printf ("%s: target %d rejected sync request\n",
    739 				    sc->sc_dev.dv_xname, target);
    740 			sc->sc_sync[target].state = SYNC_DONE;
    741 			sc->sc_sync[target].period = 0;
    742 			sc->sc_sync[target].offset = 0;
    743 			if (iob->sc_msg[2] == 3 &&
    744 			    iob->sc_msg[3] == MSG_SYNC_REQ &&
    745 			    iob->sc_msg[5] != 0) {
    746 				if (iob->sc_msg[4] && iob->sc_msg[4] < 100 / 4) {
    747 #ifdef DEBUG
    748 					printf ("%d: target %d wanted %dns period\n",
    749 					    sc->sc_dev.dv_xname, target,
    750 					    iob->sc_msg[4] * 4);
    751 #endif
    752 					/*
    753 					 * Kludge for Maxtor XT8580S
    754 					 * It accepts whatever we request, even
    755 					 * though it won't work.  So we ask for
    756 					 * a short period than we can handle.  If
    757 					 * the device says it can do it, use 208ns.
    758 					 * If the device says it can do less than
    759 					 * 100ns, then we limit it to 100ns.
    760 					 */
    761 					if (iob->sc_msg[4] == siop_sync_period / 4)
    762 						iob->sc_msg[4] = 208 / 4;
    763 					else
    764 						iob->sc_msg[4] = 100 / 4;
    765 				}
    766 				printf ("%s: target %d now synchronous, period=%dns, offset=%d\n",
    767 				    sc->sc_dev.dv_xname, target,
    768 				    iob->sc_msg[4] * 4, iob->sc_msg[5]);
    769 				scsi_period_to_siop (sc, target);
    770 			}
    771 		}
    772 #if 0
    773 		DCIAS(sc->sc_statuspa);	/* XXX */
    774 #else
    775 		dma_cachectl(&iob->sc_stat[0], 1);
    776 #endif
    777 		*status = iob->sc_stat[0];
    778 #ifdef DEBUG
    779 		if (rp->siop_sbcl & SIOP_BSY) {
    780 			/*printf ("ACK! siop was busy at end: rp %x script %x dsa %x\n",
    781 			    rp, &scripts, &iob->sc_ds);*/
    782 			/*Debugger();*/
    783 		}
    784 #endif
    785 		if (sc->sc_active > 1)
    786 			rp->siop_dcntl |= SIOP_DCNTL_STD;
    787 		return 1;
    788 	}
    789 	if (sstat0 & SIOP_SSTAT0_M_A) {		/* Phase mismatch */
    790 #ifdef DEBUG
    791 		++siopphmm;
    792 		if (iob->iob_len) {
    793 			int adjust;
    794 			adjust = ((dfifo - (dbc & 0x7f)) & 0x7f);
    795 			if (sstat1 & SIOP_SSTAT1_ORF)
    796 				++adjust;
    797 			if (sstat1 & SIOP_SSTAT1_OLF)
    798 				++adjust;
    799 			iob->iob_curlen = *((long *)&rp->siop_dcmd) & 0xffffff;
    800 			iob->iob_curlen += adjust;
    801 			iob->iob_curbuf = *((long *)&rp->siop_dnad);
    802 #ifdef DEBUG
    803 			if (siop_debug & 0x100)
    804 				printf ("Phase mismatch: dnad %x dbc %x dfifo %x dbc %x sstat1 %x adjust %x sbcl %x\n",
    805 				    iob->iob_curbuf, iob->iob_curlen, dfifo, dbc, sstat1, adjust, rp->siop_sbcl);
    806 #endif
    807 		}
    808 		if (siop_debug & 9)
    809 			printf ("Phase mismatch: %x dsp +%x dcmd %x\n",
    810 			    rp->siop_sbcl,
    811 			    rp->siop_dsp - sc->sc_scriptspa,
    812 			    *((long *)&rp->siop_dcmd));
    813 		if (siop_debug & 0x10)
    814 			panic ("53c710 phase mismatch");
    815 #endif
    816 		if ((rp->siop_sbcl & SIOP_REQ) == 0)
    817 			printf ("Phase mismatch: REQ not asserted! %02x\n",
    818 			    rp->siop_sbcl);
    819 		switch (rp->siop_sbcl & 7) {
    820 /*
    821  * For data out and data in phase, check for DMA chaining
    822  */
    823 		case 0:		/* data out */
    824 		case 1:		/* data in */
    825 		case 2:		/* status */
    826 		case 3:		/* command */
    827 		case 6:		/* message in */
    828 		case 7:		/* message out */
    829 			rp->siop_dsp = sc->sc_scriptspa + Ent_switch;
    830 			break;
    831 		default:
    832 			goto bad_phase;
    833 		}
    834 		return 0;
    835 	}
    836 	if (sstat0 & SIOP_SSTAT0_STO) {		/* Select timed out */
    837 #ifdef DEBUG
    838 		if (rp->siop_sbcl & SIOP_BSY) {
    839 			printf ("ACK! siop was busy at timeout: rp %x script %x dsa %x\n",
    840 			    rp, &scripts, &iob->sc_ds);
    841 			printf(" sbcl %x sdid %x istat %x dstat %x sstat0 %x\n",
    842 			    rp->siop_sbcl, rp->siop_sdid, istat, dstat, sstat0);
    843 if (!(rp->siop_sbcl & SIOP_BSY)) {
    844 	printf ("Yikes, it's not busy now!\n");
    845 #if 0
    846 	*status = -1;
    847 	if (sc->sc_active > 1)
    848 		rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
    849 	return 1;
    850 #endif
    851 }
    852 /*			rp->siop_dcntl |= SIOP_DCNTL_STD;*/
    853 			return (0);
    854 			Debugger();
    855 		}
    856 #endif
    857 		*status = -1;
    858 		if (sc->sc_active > 1)
    859 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
    860 		return 1;
    861 	}
    862 	target = sc->sc_slave;
    863 	if (sstat0 & SIOP_SSTAT0_UDC) {
    864 #ifdef DEBUG
    865 		printf ("%s: target %d disconnected unexpectedly\n",
    866 		   sc->sc_dev.dv_xname, target);
    867 #endif
    868 #if 0
    869 		siopabort (sc, rp, "siopchkintr");
    870 #endif
    871 		*status = STS_BUSY;
    872 		if (sc->sc_active > 1)
    873 			rp->siop_dsp = sc->sc_scriptspa + Ent_wait_reselect;
    874 		return 1;
    875 	}
    876 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff0a) {
    877 #ifdef DEBUG
    878 		if (siop_debug & 0x100)
    879 			printf ("%s: ID %x disconnected TEMP %x (+%d) buf %x len %x buf %x len %x dfifo %x dbc %x sstat1 %x\n",
    880 			    sc->sc_dev.dv_xname, 1 << target, rp->siop_temp,
    881 			    rp->siop_temp - sc->sc_scriptspa,
    882 			    iob->iob_curbuf, iob->iob_curlen,
    883 			    iob->sc_ds.chain[0].databuf, iob->sc_ds.chain[0].datalen, dfifo, dbc, sstat1);
    884 #endif
    885 		/*
    886 		 * 	HACK!
    887 		 * set current entry in disconnect state
    888 		 * clear current
    889 		 * if another iob available and another request pending,
    890 		 * start another
    891 		 */
    892 		iob->sc_status = sc->sc_flags & (SIOP_SELECTED|SIOP_DMA);
    893 		sc->sc_flags &= ~(SIOP_SELECTED|SIOP_DMA);
    894 		sc->sc_cur = NULL;		/* no current device */
    895 		sc->sc_xs = NULL;
    896 		/* continue script to wait for reconnect */
    897 		rp->siop_dcntl |= SIOP_DCNTL_STD;
    898 		if (sc->sc_active < 2) {
    899 			struct siop_pending *pendp;
    900 			int s = splbio();
    901 			/*
    902 			 * can start another I/O, see if there are
    903 			 * any pending and start them
    904 			 */
    905 			if (pendp = sc->sc_xslist.tqh_first) {
    906 				TAILQ_REMOVE(&sc->sc_xslist, pendp, link);
    907 				sc->sc_xs = pendp->xs;
    908 				pendp->xs = NULL;
    909 				splx(s);
    910 #ifdef DEBUG
    911 				if (siop_debug & 0x100)
    912 					printf ("starting a pending command %x\n", sc->sc_xs);
    913 #endif
    914 				siop_donextcmd(sc);
    915 			}
    916 			splx(s);
    917 		}
    918 		return (0);
    919 	}
    920 	if (dstat & SIOP_DSTAT_SIR && (rp->siop_dsps == 0xff09 ||
    921 	    rp->siop_dsps == 0xff06)) {
    922 		int i;
    923 #ifdef DEBUG
    924 		if (siop_debug & 0x100)
    925 			printf ("%s: target reselected %x %x\n",
    926 			     sc->sc_dev.dv_xname, rp->siop_lcrc,
    927 			     rp->siop_dsps);
    928 #endif
    929 		if (sc->sc_cur) {
    930 			printf ("siop: oops - reconnect before current done\n");
    931 /*			panic ("siop: what now?");*/
    932 		}
    933 		/*
    934 		 * locate iob of reconnecting device
    935 		 * set sc->sc_cur to iob
    936 		 */
    937 		for (i = 0, iob = NULL; i < 2; ++i) {
    938 			if (sc->sc_iob[i].sc_xs == NULL)
    939 				continue;
    940 			if (!sc->sc_iob[i].sc_status)
    941 				continue;
    942 			if (!(rp->siop_lcrc & (sc->sc_iob[i].sc_ds.scsi_addr >> 16)))
    943 				continue;
    944 			iob = &sc->sc_iob[i];
    945 			sc->sc_cur = iob;
    946 			sc->sc_xs = iob->sc_xs;
    947 			sc->sc_flags |= iob->sc_status;
    948 			iob->sc_status = 0;
    949 			DCIAS(kvtop(&iob->sc_stat[0]));
    950 			rp->siop_dsa = kvtop(&iob->sc_ds);
    951 			if (iob->iob_curlen) {
    952 if (iob->iob_curbuf <= (u_long) iob->sc_ds.chain[0].databuf ||
    953     iob->iob_curbuf >= (u_long)(iob->sc_ds.chain[0].databuf + iob->sc_ds.chain[0].datalen))
    954 	printf ("%s: saved data pointer not in chain[0]\n");
    955 				iob->sc_ds.chain[0].databuf = (char *) iob->iob_curbuf;
    956 				iob->sc_ds.chain[0].datalen = iob->iob_curlen;
    957 #ifdef DEBUG
    958 				if (siop_debug & 0x100)
    959 					printf ("%s: reselect buf %x len %x\n",
    960 					    sc->sc_dev.dv_xname, iob->iob_curbuf,
    961 					    iob->iob_curlen);
    962 #endif
    963 				DCIAS(kvtop(&iob->sc_ds.chain));
    964 			}
    965 		}
    966 		if (iob == NULL)
    967 			panic("unable to find reconnecting device");
    968 		rp->siop_dcntl |= SIOP_DCNTL_STD;
    969 		return (0);
    970 	}
    971 	if (dstat & SIOP_DSTAT_SIR && rp->siop_dsps == 0xff07) {
    972 		/* reselect was interrupted */
    973 #ifdef DEBUG
    974 		if (siop_debug & 0x100 &&
    975 		    (rp->siop_sfbr & SIOP_CTEST2_SIGP) == 0)
    976 			printf ("%s: reselect interrupted (Sig_P?) scntl1 %x ctest2 %x sfbr %x\n",
    977 			    sc->sc_dev.dv_xname, rp->siop_scntl1, rp->siop_ctest2, rp->siop_sfbr);
    978 #endif
    979 		target = sc->sc_xs->sc_link->target;
    980 		rp->siop_dsa = kvtop(&sc->sc_cur->sc_ds);
    981 		rp->siop_sbcl = sc->sc_sync[target].offset;
    982 		rp->siop_dcntl |= SIOP_DCNTL_STD;
    983 		return (0);
    984 	}
    985 	if (sstat0 == 0 && dstat & SIOP_DSTAT_SIR) {
    986 #if 0
    987 		DCIAS(sc->sc_statuspa);
    988 #else
    989 		dma_cachectl (&iob->sc_stat[0], 1);
    990 #endif
    991 		printf ("SIOP interrupt: %x sts %x msg %x sbcl %x\n",
    992 		    rp->siop_dsps, iob->sc_stat[0], iob->sc_msg[0],
    993 		    rp->siop_sbcl);
    994 		siopreset (sc);
    995 		*status = -1;
    996 		return 1;
    997 	}
    998 	if (sstat0 & SIOP_SSTAT0_SGE)
    999 		printf ("SIOP: SCSI Gross Error\n");
   1000 	if (sstat0 & SIOP_SSTAT0_PAR)
   1001 		printf ("SIOP: Parity Error\n");
   1002 	if (dstat & SIOP_DSTAT_IID)
   1003 		printf ("SIOP: Invalid instruction detected\n");
   1004 bad_phase:
   1005 	/*
   1006 	 * temporary panic for unhandled conditions
   1007 	 * displays various things about the 53C710 status and registers
   1008 	 * then panics.
   1009 	 * XXXX need to clean this up to print out the info, reset, and continue
   1010 	 */
   1011 	printf ("siopchkintr: target %x ds %x\n", target, &iob->sc_ds);
   1012 	printf ("scripts %x ds %x rp %x dsp %x dcmd %x\n", sc->sc_scriptspa,
   1013 	    kvtop(&sc->sc_iob), kvtop(rp), rp->siop_dsp,
   1014 	    *((long *)&rp->siop_dcmd));
   1015 	printf ("siopchkintr: istat %x dstat %x sstat0 %x dsps %x dsa %x sbcl %x sts %x msg %x\n",
   1016 	    istat, dstat, sstat0, rp->siop_dsps, rp->siop_dsa,
   1017 	     rp->siop_sbcl, iob->sc_stat[0], iob->sc_msg[0]);
   1018 #ifdef DEBUG
   1019 	if (siop_debug & 0x20)
   1020 		panic("siopchkintr: **** temp ****");
   1021 #endif
   1022 	siopreset (sc);		/* hard reset */
   1023 	*status = -1;
   1024 	return 1;
   1025 }
   1026 
   1027 /*
   1028  * SCSI 'immediate' command:  issue a command to some SCSI device
   1029  * and get back an 'immediate' response (i.e., do programmed xfer
   1030  * to get the response data).  'cbuf' is a buffer containing a scsi
   1031  * command of length clen bytes.  'buf' is a buffer of length 'len'
   1032  * bytes for data.  The transfer direction is determined by the device
   1033  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
   1034  * command must supply no data.  'xferphase' is the bus phase the
   1035  * caller expects to happen after the command is issued.  It should
   1036  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
   1037  *
   1038  * XXX - 53C710 will use DMA, but no interrupts (it's a heck of a
   1039  * lot easier to do than to use programmed I/O).
   1040  *
   1041  */
   1042 int
   1043 siopicmd(sc, target, lun, cbuf, clen, buf, len)
   1044 	struct siop_softc *sc;
   1045 	int target;
   1046 	int lun;
   1047 	void *cbuf;
   1048 	int clen;
   1049 	void *buf;
   1050 	int len;
   1051 {
   1052 	siop_regmap_p rp = sc->sc_siopp;
   1053 	struct siop_iob *iob = sc->sc_cur;
   1054 	int i;
   1055 	int status;
   1056 	u_char istat;
   1057 	u_char dstat;
   1058 	u_char sstat0;
   1059 
   1060 	if (sc->sc_flags & SIOP_SELECTED) {
   1061 		printf ("siopicmd%d: bus busy\n", target);
   1062 		return -1;
   1063 	}
   1064 	rp->siop_sien = 0x00;		/* disable SCSI and DMA interrupts */
   1065 	rp->siop_dien = 0x00;
   1066 	sc->sc_flags |= SIOP_SELECTED;
   1067 	sc->sc_slave = target;
   1068 #ifdef DEBUG
   1069 	if (siop_debug & 1)
   1070 		printf ("siopicmd: target %x/%x cmd %02x ds %x\n", target,
   1071 		    lun, *((char *)cbuf), &iob->sc_ds);
   1072 #endif
   1073 	siop_setup (sc, target, lun, cbuf, clen, buf, len);
   1074 
   1075 	for (;;) {
   1076 		/* use cmd_wait values? */
   1077 		i = siop_cmd_wait << 1;
   1078 		while (((istat = rp->siop_istat) &
   1079 		    (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
   1080 			if (--i <= 0) {
   1081 				printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %x (+%x) dcmd %x ds %x\n",
   1082 				    target, *((char *)cbuf),
   1083 				    rp->siop_sbcl, rp->siop_dsp,
   1084 				    rp->siop_dsp - sc->sc_scriptspa,
   1085 				    *((long *)&rp->siop_dcmd), &iob->sc_ds);
   1086 				i = siop_cmd_wait << 2;
   1087 				/* XXXX need an upper limit and reset */
   1088 			}
   1089 			delay(10);
   1090 		}
   1091 		dstat = rp->siop_dstat;
   1092 		sstat0 = rp->siop_sstat0;
   1093 #ifdef DEBUG
   1094 		if (siop_debug & 1) {
   1095 			DCIAS(kvtop(iob->sc_stat[0]));	/* XXX should just invalidate sc->sc_stat */
   1096 			printf ("siopicmd: istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
   1097 			    istat, dstat, sstat0, rp->siop_dsps, rp->siop_sbcl,
   1098 			    iob->sc_stat[0], iob->sc_msg[0]);
   1099 		}
   1100 #endif
   1101 		if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
   1102 			sc->sc_flags &= ~SIOP_SELECTED;
   1103 			return (status);
   1104 		}
   1105 	}
   1106 }
   1107 
   1108 int
   1109 siopgo(sc, xs)
   1110 	struct siop_softc *sc;
   1111 	struct scsi_xfer *xs;
   1112 {
   1113 	siop_regmap_p rp;
   1114 	int i;
   1115 	int nchain;
   1116 	int count, tcount;
   1117 	char *addr, *dmaend;
   1118 
   1119 #ifdef DEBUG
   1120 	if (siop_debug & 1)
   1121 		printf ("%s: go ", sc->sc_dev.dv_xname);
   1122 #if 0
   1123 	if ((cdb->cdb[1] & 1) == 0 &&
   1124 	    ((cdb->cdb[0] == CMD_WRITE && cdb->cdb[2] == 0 && cdb->cdb[3] == 0) ||
   1125 	    (cdb->cdb[0] == CMD_WRITE_EXT && cdb->cdb[2] == 0 && cdb->cdb[3] == 0
   1126 	    && cdb->cdb[4] == 0)))
   1127 		panic ("siopgo: attempted write to block < 0x100");
   1128 #endif
   1129 #endif
   1130 #if 0
   1131 	cdb->cdb[1] |= unit << 5;
   1132 #endif
   1133 
   1134 	if (sc->sc_flags & SIOP_SELECTED) {
   1135 		printf ("%s: bus busy\n", sc->sc_dev.dv_xname);
   1136 		return 1;
   1137 	}
   1138 
   1139 	sc->sc_flags |= SIOP_SELECTED | SIOP_DMA;
   1140 	sc->sc_slave = xs->sc_link->target;
   1141 	rp = sc->sc_siopp;
   1142 	/* enable SCSI and DMA interrupts */
   1143 	rp->siop_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
   1144 	    SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
   1145 	rp->siop_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
   1146 	    /*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
   1147 #ifdef DEBUG
   1148 	if (siop_debug & 1)
   1149 		printf ("siopgo: target %x cmd %02x ds %x\n", sc->sc_slave,
   1150 		    xs->cmd->opcode, &sc->sc_cur->sc_ds);
   1151 #endif
   1152 
   1153 	siop_setup(sc, sc->sc_slave, xs->sc_link->lun, xs->cmd, xs->cmdlen,
   1154 	    xs->data, xs->datalen);
   1155 
   1156 	return (0);
   1157 }
   1158 
   1159 /*
   1160  * Check for 53C710 interrupts
   1161  */
   1162 
   1163 int
   1164 siopintr (sc)
   1165 	register struct siop_softc *sc;
   1166 {
   1167 	siop_regmap_p rp;
   1168 	register u_char istat, dstat, sstat0;
   1169 	int status;
   1170 
   1171 	rp = sc->sc_siopp;
   1172 	istat = sc->sc_istat;
   1173 	if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0)
   1174 		return;
   1175 	if ((rp->siop_sien | rp->siop_dien) == 0)
   1176 		return(0);	/* no interrupts enabled */
   1177 	/* Got a valid interrupt on this device */
   1178 	dstat = sc->sc_dstat;
   1179 	sstat0 = sc->sc_sstat0;
   1180 	sc->sc_istat = 0;
   1181 #ifdef DEBUG
   1182 	if (siop_debug & 1)
   1183 		printf ("%s: intr istat %x dstat %x sstat0 %x\n",
   1184 		    sc->sc_dev.dv_xname, istat, dstat, sstat0);
   1185 	if (!sc->sc_active) {
   1186 		printf ("%s: spurious interrupt? istat %x dstat %x sstat0 %x status %x\n",
   1187 		    sc->sc_dev.dv_xname, istat, dstat, sstat0, sc->sc_cur->sc_stat[0]);
   1188 	}
   1189 #endif
   1190 
   1191 #ifdef DEBUG
   1192 	if (siop_debug & 5) {
   1193 		DCIAS(kvtop(&sc->sc_cur->sc_stat[0]));
   1194 		printf ("%s: intr istat %x dstat %x sstat0 %x dsps %x sbcl %x sts %x msg %x\n",
   1195 		    sc->sc_dev.dv_xname, istat, dstat, sstat0,
   1196 		    rp->siop_dsps,  rp->siop_sbcl,
   1197 		    sc->sc_cur->sc_stat[0], sc->sc_cur->sc_msg[0]);
   1198 	}
   1199 #endif
   1200 	if (siop_checkintr (sc, istat, dstat, sstat0, &status)) {
   1201 #if 1
   1202 		if (sc->sc_active <= 1) {	/* more HACK */
   1203 			rp->siop_sien = 0;
   1204 			rp->siop_dien = 0;
   1205 		}
   1206 		if (status == 0xff)
   1207 			printf ("siopintr: status == 0xff\n");
   1208 #endif
   1209 		if (sc->sc_flags & SIOP_DMA) {
   1210 			sc->sc_flags &= ~(SIOP_DMA | SIOP_SELECTED);
   1211 			if (rp->siop_sbcl & SIOP_BSY)
   1212 				printf ("%s: SCSI bus busy at completion\n",
   1213 					sc->sc_dev.dv_xname);
   1214 			siop_scsidone(sc, sc->sc_cur->sc_stat[0]);
   1215 		}
   1216 	}
   1217 }
   1218 
   1219 /*
   1220  * This is based on the Progressive Peripherals Zeus driver and may
   1221  * not be correct for other 53c710 boards.
   1222  *
   1223  */
   1224 scsi_period_to_siop (sc, target)
   1225 	struct siop_softc *sc;
   1226 {
   1227 	int period, offset, i, sxfer;
   1228 
   1229 	period = sc->sc_cur->sc_msg[4];
   1230 	offset = sc->sc_cur->sc_msg[5];
   1231 	sxfer = 0;
   1232 	if (offset <= SIOP_MAX_OFFSET)
   1233 		sxfer = offset;
   1234 	for (i = 0; i < sizeof (xxx) / 2; ++i) {
   1235 		if (period <= xxx[i].x) {
   1236 			sxfer |= xxx[i].y & 0x70;
   1237 			offset = xxx[i].y & 0x03;
   1238 			break;
   1239 		}
   1240 	}
   1241 	sc->sc_sync[target].period = sxfer;
   1242 	sc->sc_sync[target].offset = offset;
   1243 #ifdef DEBUG
   1244 	printf ("sync: siop_sxfr %02x, siop_sbcl %02x\n", sxfer, offset);
   1245 #endif
   1246 }
   1247