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