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