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