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