Home | History | Annotate | Line # | Download | only in dev
sbic.c revision 1.15
      1  1.15   chopps /*	$NetBSD: sbic.c,v 1.15 1995/08/24 15:46:19 chopps Exp $	*/
      2   1.6      cgd 
      3   1.1   chopps /*
      4   1.1   chopps  * Copyright (c) 1994 Christian E. Hopps
      5   1.1   chopps  * Copyright (c) 1990 The Regents of the University of California.
      6   1.1   chopps  * All rights reserved.
      7   1.1   chopps  *
      8   1.1   chopps  * This code is derived from software contributed to Berkeley by
      9   1.1   chopps  * Van Jacobson of Lawrence Berkeley Laboratory.
     10   1.1   chopps  *
     11   1.1   chopps  * Redistribution and use in source and binary forms, with or without
     12   1.1   chopps  * modification, are permitted provided that the following conditions
     13   1.1   chopps  * are met:
     14   1.1   chopps  * 1. Redistributions of source code must retain the above copyright
     15   1.1   chopps  *    notice, this list of conditions and the following disclaimer.
     16   1.1   chopps  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1   chopps  *    notice, this list of conditions and the following disclaimer in the
     18   1.1   chopps  *    documentation and/or other materials provided with the distribution.
     19   1.1   chopps  * 3. All advertising materials mentioning features or use of this software
     20   1.1   chopps  *    must display the following acknowledgement:
     21   1.1   chopps  *	This product includes software developed by the University of
     22   1.1   chopps  *	California, Berkeley and its contributors.
     23   1.1   chopps  * 4. Neither the name of the University nor the names of its contributors
     24   1.1   chopps  *    may be used to endorse or promote products derived from this software
     25   1.1   chopps  *    without specific prior written permission.
     26   1.1   chopps  *
     27   1.1   chopps  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1   chopps  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1   chopps  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1   chopps  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1   chopps  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1   chopps  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1   chopps  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1   chopps  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1   chopps  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1   chopps  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1   chopps  * SUCH DAMAGE.
     38   1.1   chopps  *
     39   1.1   chopps  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     40   1.1   chopps  */
     41   1.1   chopps 
     42   1.1   chopps /*
     43   1.1   chopps  * AMIGA AMD 33C93 scsi adaptor driver
     44   1.1   chopps  */
     45   1.1   chopps 
     46   1.1   chopps #include <sys/param.h>
     47   1.1   chopps #include <sys/systm.h>
     48   1.1   chopps #include <sys/device.h>
     49  1.14   chopps #include <sys/kernel.h> /* For hz */
     50  1.10   chopps #include <sys/disklabel.h>
     51  1.10   chopps #include <sys/dkstat.h>
     52   1.1   chopps #include <sys/buf.h>
     53   1.1   chopps #include <scsi/scsi_all.h>
     54   1.1   chopps #include <scsi/scsiconf.h>
     55   1.1   chopps #include <vm/vm.h>
     56   1.1   chopps #include <vm/vm_kern.h>
     57   1.1   chopps #include <vm/vm_page.h>
     58   1.1   chopps #include <machine/pmap.h>
     59   1.1   chopps #include <machine/cpu.h>
     60   1.1   chopps #include <amiga/amiga/device.h>
     61   1.1   chopps #include <amiga/amiga/custom.h>
     62  1.10   chopps #include <amiga/amiga/isr.h>
     63   1.1   chopps #include <amiga/dev/dmavar.h>
     64   1.1   chopps #include <amiga/dev/sbicreg.h>
     65   1.1   chopps #include <amiga/dev/sbicvar.h>
     66   1.1   chopps 
     67  1.14   chopps /* These are for bounce buffers */
     68  1.14   chopps #include <amiga/amiga/cc.h>
     69  1.14   chopps #include <amiga/dev/zbusvar.h>
     70  1.14   chopps 
     71  1.14   chopps #include <vm/pmap.h>
     72  1.14   chopps 
     73  1.14   chopps /* Since I can't find this in any other header files */
     74  1.14   chopps #define SCSI_PHASE(reg)	(reg&0x07)
     75  1.14   chopps 
     76   1.1   chopps /*
     77   1.1   chopps  * SCSI delays
     78   1.1   chopps  * In u-seconds, primarily for state changes on the SPC.
     79   1.1   chopps  */
     80   1.1   chopps #define	SBIC_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
     81   1.1   chopps #define	SBIC_DATA_WAIT	50000	/* wait per data in/out step */
     82   1.1   chopps #define	SBIC_INIT_WAIT	50000	/* wait per step (both) during init */
     83   1.1   chopps 
     84   1.1   chopps #define	b_cylin		b_resid
     85   1.1   chopps #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
     86   1.1   chopps 
     87   1.1   chopps extern u_int kvtop();
     88   1.1   chopps 
     89  1.14   chopps int  sbicicmd __P((struct sbic_softc *, int, int, void *, int, void *, int));
     90   1.1   chopps int  sbicgo __P((struct sbic_softc *, struct scsi_xfer *));
     91   1.1   chopps int  sbicdmaok __P((struct sbic_softc *, struct scsi_xfer *));
     92   1.1   chopps int  sbicwait __P((sbic_regmap_p, char, int , int));
     93  1.13  mycroft int  sbiccheckdmap __P((void *, u_long, u_long));
     94   1.7   chopps int  sbicselectbus __P((struct sbic_softc *, sbic_regmap_p, u_char, u_char, u_char));
     95   1.1   chopps int  sbicxfstart __P((sbic_regmap_p, int, u_char, int));
     96   1.1   chopps int  sbicxfout __P((sbic_regmap_p regs, int, void *, int));
     97   1.1   chopps int  sbicfromscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
     98   1.1   chopps int  sbictoscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
     99   1.1   chopps int  sbicintr __P((struct sbic_softc *));
    100  1.14   chopps int  sbicpoll __P((struct sbic_softc *));
    101  1.14   chopps int  sbicnextstate __P((struct sbic_softc *, u_char, u_char));
    102  1.14   chopps int  sbicmsgin __P((struct sbic_softc *));
    103  1.14   chopps int  sbicxfin __P((sbic_regmap_p regs, int, void *));
    104  1.14   chopps int  sbicabort __P((struct sbic_softc *, sbic_regmap_p, char *));
    105   1.1   chopps void sbicxfdone __P((struct sbic_softc *, sbic_regmap_p, int));
    106   1.1   chopps void sbicerror __P((struct sbic_softc *, sbic_regmap_p, u_char));
    107   1.1   chopps void sbicstart __P((struct sbic_softc *));
    108   1.1   chopps void sbicreset __P((struct sbic_softc *));
    109  1.14   chopps void sbic_scsidone __P((struct sbic_acb *, int));
    110  1.14   chopps void sbic_sched __P((struct sbic_softc *));
    111  1.14   chopps void sbic_save_ptrs __P((struct sbic_softc *, sbic_regmap_p,int,int));
    112  1.14   chopps void sbic_load_ptrs __P((struct sbic_softc *, sbic_regmap_p,int,int));
    113   1.1   chopps 
    114   1.1   chopps /*
    115   1.1   chopps  * Synch xfer parameters, and timing conversions
    116   1.1   chopps  */
    117   1.1   chopps int sbic_min_period = SBIC_SYN_MIN_PERIOD;  /* in cycles = f(ICLK,FSn) */
    118   1.1   chopps int sbic_max_offset = SBIC_SYN_MAX_OFFSET;  /* pure number */
    119   1.1   chopps 
    120   1.1   chopps int sbic_cmd_wait = SBIC_CMD_WAIT;
    121   1.1   chopps int sbic_data_wait = SBIC_DATA_WAIT;
    122   1.1   chopps int sbic_init_wait = SBIC_INIT_WAIT;
    123   1.1   chopps 
    124   1.1   chopps /*
    125   1.1   chopps  * was broken before.. now if you want this you get it for all drives
    126   1.1   chopps  * on sbic controllers.
    127   1.1   chopps  */
    128   1.9   chopps int sbic_inhibit_sync = 0;
    129  1.14   chopps int sbic_enable_reselect = 1;
    130   1.1   chopps int sbic_clock_override = 0;
    131   1.1   chopps int sbic_no_dma = 0;
    132  1.14   chopps int sbic_parallel_operations = 1;
    133   1.1   chopps 
    134   1.1   chopps #ifdef DEBUG
    135  1.14   chopps sbic_regmap_p debug_sbic_regs;
    136  1.10   chopps int	sbicdma_ops = 0;	/* total DMA operations */
    137  1.10   chopps int	sbicdma_bounces = 0;	/* number operations using bounce buffer */
    138  1.10   chopps int	sbicdma_hits = 0;	/* number of DMA chains that were contiguous */
    139  1.10   chopps int	sbicdma_misses = 0;	/* number of DMA chains that were not contiguous */
    140  1.14   chopps int     sbicdma_saves = 0;
    141   1.1   chopps #define QPRINTF(a) if (sbic_debug > 1) printf a
    142   1.1   chopps int	sbic_debug = 0;
    143   1.1   chopps int	sync_debug = 0;
    144   1.1   chopps int	sbic_dma_debug = 0;
    145  1.14   chopps int	reselect_debug = 0;
    146  1.14   chopps int	report_sense = 0;
    147  1.14   chopps int	data_pointer_debug = 0;
    148  1.14   chopps int	sbic_timeout = 0;
    149  1.14   chopps u_char	debug_asr, debug_csr, timeout_active=0, routine;
    150  1.14   chopps void sbictimeout __P((struct sbic_softc *dev));
    151   1.1   chopps #else
    152   1.1   chopps #define QPRINTF
    153   1.1   chopps #endif
    154   1.1   chopps 
    155   1.1   chopps /*
    156   1.1   chopps  * default minphys routine for sbic based controllers
    157   1.1   chopps  */
    158  1.13  mycroft void
    159   1.1   chopps sbic_minphys(bp)
    160   1.1   chopps 	struct buf *bp;
    161   1.1   chopps {
    162  1.13  mycroft 
    163   1.1   chopps 	/*
    164  1.13  mycroft 	 * No max transfer at this level.
    165   1.1   chopps 	 */
    166  1.13  mycroft 	minphys(bp);
    167   1.1   chopps }
    168   1.1   chopps 
    169   1.1   chopps /*
    170  1.14   chopps  * Save DMA pointers.  Take into account partial transfer. Shut down DMA.
    171  1.14   chopps  */
    172  1.14   chopps void
    173  1.14   chopps sbic_save_ptrs(dev, regs, target, lun)
    174  1.14   chopps 	struct sbic_softc *dev;
    175  1.14   chopps 	sbic_regmap_p regs;
    176  1.14   chopps 	int target, lun;
    177  1.14   chopps {
    178  1.14   chopps 	int count, asr, csr, s;
    179  1.14   chopps 	unsigned long ptr;
    180  1.14   chopps 	char *vptr;
    181  1.14   chopps 	struct sbic_acb* acb;
    182  1.14   chopps 
    183  1.14   chopps 	extern vm_offset_t vm_first_phys;
    184  1.14   chopps 
    185  1.14   chopps 	if( !dev->sc_cur ) return;
    186  1.14   chopps 	if( !(dev->sc_flags & SBICF_INDMA) ) return; /* DMA not active */
    187  1.14   chopps 
    188  1.14   chopps 	s = splbio();
    189  1.14   chopps 
    190  1.14   chopps 	acb = dev->sc_nexus;
    191  1.14   chopps 	count = -1;
    192  1.14   chopps 	do {
    193  1.14   chopps 		GET_SBIC_asr(regs, asr);
    194  1.14   chopps 		if( asr & SBIC_ASR_DBR ) {
    195  1.14   chopps 			printf("sbic_save_ptrs: asr %02x canceled!\n", asr);
    196  1.14   chopps 			splx(s);
    197  1.14   chopps 			return;
    198  1.14   chopps 		}
    199  1.14   chopps 	} while( asr & (SBIC_ASR_BSY|SBIC_ASR_CIP) );
    200  1.14   chopps 
    201  1.14   chopps 	/* Save important state */
    202  1.14   chopps 	/* must be done before dmastop */
    203  1.14   chopps 	acb->sc_dmacmd = dev->sc_dmacmd;
    204  1.14   chopps 	SBIC_TC_GET(regs, count);
    205  1.14   chopps 
    206  1.14   chopps 	/* Shut down DMA ====CAREFUL==== */
    207  1.14   chopps 	dev->sc_dmastop(dev);
    208  1.14   chopps 	dev->sc_flags &= ~SBICF_INDMA;
    209  1.14   chopps 	SBIC_TC_PUT(regs, 0);
    210  1.14   chopps 
    211  1.14   chopps #ifdef DEBUG
    212  1.14   chopps 	if(!count && sbic_debug) printf("%dcount0",target);
    213  1.14   chopps 	if(data_pointer_debug == -1)
    214  1.14   chopps 		printf("SBIC saving target %d data pointers from (%x,%x)%xASR:%02x",
    215  1.14   chopps 		       target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
    216  1.14   chopps 		       acb->sc_dmacmd, asr);
    217  1.14   chopps #endif
    218  1.14   chopps 
    219  1.14   chopps 	/* Fixup partial xfers */
    220  1.14   chopps 	acb->sc_kv.dc_addr += (dev->sc_tcnt - count);
    221  1.14   chopps 	acb->sc_kv.dc_count -= (dev->sc_tcnt - count);
    222  1.14   chopps 	acb->sc_pa.dc_addr += (dev->sc_tcnt - count);
    223  1.14   chopps 	acb->sc_pa.dc_count -= ((dev->sc_tcnt - count)>>1);
    224  1.14   chopps 
    225  1.14   chopps 	acb->sc_tcnt = dev->sc_tcnt = count;
    226  1.14   chopps #ifdef DEBUG
    227  1.14   chopps 	if(data_pointer_debug)
    228  1.14   chopps 		printf(" at (%x,%x):%x\n",
    229  1.14   chopps 		       dev->sc_cur->dc_addr, dev->sc_cur->dc_count,count);
    230  1.14   chopps 	sbicdma_saves++;
    231  1.14   chopps #endif
    232  1.14   chopps 	splx(s);
    233  1.14   chopps }
    234  1.14   chopps 
    235  1.14   chopps 
    236  1.14   chopps /*
    237  1.14   chopps  * DOES NOT RESTART DMA!!!
    238  1.14   chopps  */
    239  1.14   chopps void sbic_load_ptrs(dev, regs, target, lun)
    240  1.14   chopps 	struct sbic_softc *dev;
    241  1.14   chopps 	sbic_regmap_p regs;
    242  1.14   chopps 	int target, lun;
    243  1.14   chopps {
    244  1.14   chopps 	int i, s, asr, count;
    245  1.14   chopps 	char* vaddr, * paddr;
    246  1.14   chopps 	struct sbic_acb *acb;
    247  1.14   chopps 
    248  1.14   chopps 	acb = dev->sc_nexus;
    249  1.14   chopps 	if( !acb->sc_kv.dc_count )
    250  1.14   chopps 		/* No data to xfer */
    251  1.14   chopps 		return;
    252  1.14   chopps 
    253  1.14   chopps 	s = splbio();
    254  1.14   chopps 
    255  1.14   chopps 	dev->sc_last = dev->sc_cur = &acb->sc_pa;
    256  1.14   chopps 	dev->sc_tcnt = acb->sc_tcnt;
    257  1.14   chopps 	dev->sc_dmacmd = acb->sc_dmacmd;
    258  1.14   chopps 
    259  1.14   chopps #ifdef DEBUG
    260  1.14   chopps 	sbicdma_ops++;
    261  1.14   chopps #endif
    262  1.14   chopps 	if( !dev->sc_tcnt ) {
    263  1.14   chopps 		/* sc_tcnt == 0 implies end of segment */
    264  1.14   chopps 
    265  1.14   chopps 		/* do kvm to pa mappings */
    266  1.14   chopps 		paddr = acb->sc_pa.dc_addr =
    267  1.14   chopps 			(char *) kvtop(acb->sc_kv.dc_addr);
    268  1.14   chopps 
    269  1.14   chopps 		vaddr = acb->sc_kv.dc_addr;
    270  1.14   chopps 		count = acb->sc_kv.dc_count;
    271  1.14   chopps 		for(count = (NBPG - ((int)vaddr & PGOFSET));
    272  1.14   chopps 		    count < acb->sc_kv.dc_count
    273  1.14   chopps 		    && (char*)kvtop(vaddr + count + 4) == paddr + count + 4;
    274  1.14   chopps 		    count += NBPG);
    275  1.14   chopps 		/* If it's all contiguous... */
    276  1.14   chopps 		if(count > acb->sc_kv.dc_count ) {
    277  1.14   chopps 			count = acb->sc_kv.dc_count;
    278  1.14   chopps #ifdef DEBUG
    279  1.14   chopps 			sbicdma_hits++;
    280  1.14   chopps #endif
    281  1.14   chopps 		} else {
    282  1.14   chopps #ifdef DEBUG
    283  1.14   chopps 			sbicdma_misses++;
    284  1.14   chopps #endif
    285  1.14   chopps 		}
    286  1.14   chopps 		acb->sc_tcnt = count;
    287  1.14   chopps 		acb->sc_pa.dc_count = count >> 1;
    288  1.14   chopps 
    289  1.14   chopps #ifdef DEBUG
    290  1.14   chopps 		if(data_pointer_debug)
    291  1.14   chopps 			printf("DMA recalc:kv(%x,%x)pa(%x,%x)\n",
    292  1.14   chopps 			       acb->sc_kv.dc_addr,
    293  1.14   chopps 			       acb->sc_kv.dc_count,
    294  1.14   chopps 			       acb->sc_pa.dc_addr,
    295  1.14   chopps 			       acb->sc_tcnt);
    296  1.14   chopps #endif
    297  1.14   chopps 	}
    298  1.14   chopps 	splx(s);
    299  1.14   chopps #ifdef DEBUG
    300  1.14   chopps 	if(data_pointer_debug)
    301  1.14   chopps 		printf("SBIC restoring target %d data pointers at (%x,%x)%x\n",
    302  1.14   chopps 		       target, dev->sc_cur->dc_addr, dev->sc_cur->dc_count,
    303  1.14   chopps 		       dev->sc_dmacmd);
    304  1.14   chopps #endif
    305  1.14   chopps }
    306  1.14   chopps 
    307  1.14   chopps /*
    308   1.1   chopps  * used by specific sbic controller
    309   1.1   chopps  *
    310   1.1   chopps  * it appears that the higher level code does nothing with LUN's
    311   1.1   chopps  * so I will too.  I could plug it in, however so could they
    312   1.1   chopps  * in scsi_scsi_cmd().
    313   1.1   chopps  */
    314   1.1   chopps int
    315   1.1   chopps sbic_scsicmd(xs)
    316   1.1   chopps 	struct scsi_xfer *xs;
    317   1.1   chopps {
    318  1.14   chopps 	struct sbic_acb *acb;
    319   1.1   chopps 	struct sbic_softc *dev;
    320   1.1   chopps 	struct scsi_link *slp;
    321  1.14   chopps 	int flags, s, stat;
    322   1.1   chopps 
    323   1.1   chopps 	slp = xs->sc_link;
    324   1.1   chopps 	dev = slp->adapter_softc;
    325   1.1   chopps 	flags = xs->flags;
    326   1.1   chopps 
    327   1.1   chopps 	if (flags & SCSI_DATA_UIO)
    328   1.1   chopps 		panic("sbic: scsi data uio requested");
    329  1.13  mycroft 
    330  1.14   chopps 	if (dev->sc_nexus && flags & SCSI_POLL)
    331   1.1   chopps 		panic("sbic_scsicmd: busy");
    332   1.1   chopps 
    333  1.14   chopps 	if (slp->target == slp->adapter_target)
    334  1.14   chopps 		return ESCAPE_NOT_SUPPORTED;
    335  1.14   chopps 
    336   1.1   chopps 	s = splbio();
    337  1.14   chopps 	acb = dev->free_list.tqh_first;
    338  1.14   chopps 	if (acb)
    339  1.14   chopps 		TAILQ_REMOVE(&dev->free_list, acb, chain);
    340  1.14   chopps 	splx(s);
    341  1.14   chopps 
    342  1.14   chopps 	if (acb == NULL) {
    343  1.14   chopps #ifdef DEBUG
    344  1.14   chopps 		printf("sbic_scsicmd: unable to queue request for target %d\n",
    345  1.14   chopps 		    slp->target);
    346  1.14   chopps #ifdef DDB
    347  1.14   chopps 		Debugger();
    348  1.14   chopps #endif
    349  1.14   chopps #endif
    350  1.14   chopps 		xs->error = XS_DRIVER_STUFFUP;
    351  1.14   chopps 		return(TRY_AGAIN_LATER);
    352  1.14   chopps 	}
    353  1.14   chopps 
    354  1.14   chopps 	acb->flags = ACB_ACTIVE;
    355  1.14   chopps 	if (flags & SCSI_DATA_IN)
    356  1.14   chopps 		acb->flags |= ACB_DATAIN;
    357  1.14   chopps 	acb->xs = xs;
    358  1.14   chopps 	bcopy(xs->cmd, &acb->cmd, xs->cmdlen);
    359  1.14   chopps 	acb->clen = xs->cmdlen;
    360  1.14   chopps 	acb->sc_kv.dc_addr = xs->data;
    361  1.14   chopps 	acb->sc_kv.dc_count = xs->datalen;
    362  1.14   chopps 	acb->pa_addr = xs->data ? (char *)kvtop(xs->data) : 0;	/* XXXX check */
    363  1.14   chopps 
    364  1.14   chopps 	if (flags & SCSI_POLL) {
    365  1.14   chopps 		s = splbio();
    366  1.14   chopps 		/*
    367  1.14   chopps 		 * This has major side effects -- it locks up the machine
    368  1.14   chopps 		 */
    369  1.14   chopps 
    370  1.14   chopps 		dev->sc_flags |= SBICF_ICMD;
    371  1.14   chopps 		do {
    372  1.14   chopps 			while(dev->sc_nexus)
    373  1.14   chopps 				sbicpoll(dev);
    374  1.14   chopps 			dev->sc_nexus = acb;
    375  1.14   chopps 			dev->sc_stat[0] = -1;
    376  1.14   chopps 			dev->sc_xs = xs;
    377  1.14   chopps 			dev->target = slp->target;
    378  1.14   chopps 			dev->lun = slp->lun;
    379  1.14   chopps 			stat = sbicicmd(dev, slp->target, slp->lun,
    380  1.14   chopps 					&acb->cmd, acb->clen,
    381  1.14   chopps 					acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
    382  1.14   chopps 		} while (dev->sc_nexus != acb);
    383  1.14   chopps 		sbic_scsidone(acb, stat);
    384  1.14   chopps 
    385   1.1   chopps 		splx(s);
    386  1.14   chopps 		return(COMPLETE);
    387   1.1   chopps 	}
    388   1.1   chopps 
    389  1.14   chopps 	s = splbio();
    390  1.14   chopps 	TAILQ_INSERT_TAIL(&dev->ready_list, acb, chain);
    391  1.14   chopps 
    392  1.14   chopps 	if (dev->sc_nexus) {
    393   1.1   chopps 		splx(s);
    394   1.1   chopps 		return(SUCCESSFULLY_QUEUED);
    395   1.1   chopps 	}
    396   1.1   chopps 
    397   1.1   chopps 	/*
    398  1.14   chopps 	 * nothing is active, try to start it now.
    399   1.1   chopps 	 */
    400  1.14   chopps 	sbic_sched(dev);
    401  1.14   chopps 	splx(s);
    402   1.1   chopps 
    403  1.14   chopps /* TODO:  add sbic_poll to do SCSI_POLL operations */
    404  1.14   chopps #if 0
    405   1.8   chopps 	if (flags & SCSI_POLL)
    406   1.1   chopps 		return(COMPLETE);
    407  1.14   chopps #endif
    408   1.1   chopps 	return(SUCCESSFULLY_QUEUED);
    409   1.1   chopps }
    410   1.1   chopps 
    411   1.1   chopps /*
    412  1.14   chopps  * attempt to start the next available command
    413   1.1   chopps  */
    414   1.1   chopps void
    415  1.14   chopps sbic_sched(dev)
    416   1.1   chopps 	struct sbic_softc *dev;
    417   1.1   chopps {
    418   1.1   chopps 	struct scsi_xfer *xs;
    419   1.1   chopps 	struct scsi_link *slp;
    420  1.14   chopps 	struct sbic_acb *acb;
    421  1.14   chopps 	int flags, /*phase,*/ stat, i;
    422  1.14   chopps 
    423  1.14   chopps 	if (dev->sc_nexus)
    424  1.14   chopps 		return;			/* a command is current active */
    425  1.14   chopps 
    426  1.14   chopps 	for (acb = dev->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
    427  1.14   chopps 		slp = acb->xs->sc_link;
    428  1.14   chopps 		i = slp->target;
    429  1.14   chopps 		if (!(dev->sc_tinfo[i].lubusy & (1 << slp->lun))) {
    430  1.14   chopps 			struct sbic_tinfo *ti = &dev->sc_tinfo[i];
    431  1.14   chopps 
    432  1.14   chopps 			TAILQ_REMOVE(&dev->ready_list, acb, chain);
    433  1.14   chopps 			dev->sc_nexus = acb;
    434  1.14   chopps 			slp = acb->xs->sc_link;
    435  1.14   chopps 			ti = &dev->sc_tinfo[slp->target];
    436  1.14   chopps 			ti->lubusy |= (1 << slp->lun);
    437  1.14   chopps 			acb->sc_pa.dc_addr = acb->pa_addr;	/* XXXX check */
    438  1.14   chopps 			break;
    439  1.14   chopps 		}
    440  1.14   chopps 	}
    441  1.14   chopps 
    442  1.14   chopps 	if (acb == NULL)
    443  1.14   chopps 		return;			/* did not find an available command */
    444   1.1   chopps 
    445  1.14   chopps 	dev->sc_xs = xs = acb->xs;
    446   1.1   chopps 	slp = xs->sc_link;
    447   1.1   chopps 	flags = xs->flags;
    448   1.1   chopps 
    449   1.1   chopps 	if (flags & SCSI_RESET)
    450   1.1   chopps 		sbicreset(dev);
    451   1.1   chopps 
    452  1.14   chopps #ifdef DEBUG
    453  1.14   chopps 	if( data_pointer_debug > 1 )
    454  1.14   chopps 		printf("sbic_sched(%d,%d)\n",slp->target,slp->lun);
    455  1.14   chopps #endif
    456   1.1   chopps 	dev->sc_stat[0] = -1;
    457  1.14   chopps 	dev->target = slp->target;
    458  1.14   chopps 	dev->lun = slp->lun;
    459  1.14   chopps 	if ( flags & SCSI_POLL || ( !sbic_parallel_operations
    460  1.14   chopps 				   && (/*phase == STATUS_PHASE ||*/
    461  1.14   chopps 				       sbicdmaok(dev, xs) == 0) ) )
    462  1.14   chopps 		stat = sbicicmd(dev, slp->target, slp->lun, &acb->cmd,
    463  1.14   chopps 		    acb->clen, acb->sc_kv.dc_addr, acb->sc_kv.dc_count);
    464   1.1   chopps 	else if (sbicgo(dev, xs) == 0)
    465   1.1   chopps 		return;
    466  1.13  mycroft 	else
    467   1.1   chopps 		stat = dev->sc_stat[0];
    468  1.13  mycroft 
    469  1.14   chopps 	sbic_scsidone(acb, stat);
    470   1.1   chopps }
    471   1.1   chopps 
    472   1.1   chopps void
    473  1.14   chopps sbic_scsidone(acb, stat)
    474  1.14   chopps 	struct sbic_acb *acb;
    475   1.1   chopps 	int stat;
    476   1.1   chopps {
    477   1.1   chopps 	struct scsi_xfer *xs;
    478  1.14   chopps 	struct scsi_link *slp;
    479  1.14   chopps 	struct sbic_softc *dev;
    480  1.14   chopps 	int s, dosched = 0;
    481   1.1   chopps 
    482  1.14   chopps 	xs = acb->xs;
    483  1.14   chopps 	slp = xs->sc_link;
    484  1.14   chopps 	dev = slp->adapter_softc;
    485   1.1   chopps #ifdef DIAGNOSTIC
    486  1.14   chopps 	if (acb == NULL || xs == NULL) {
    487  1.14   chopps 		printf("sbic_scsidone -- (%d,%d) no scsi_xfer\n",
    488  1.14   chopps 		       dev->target, dev->lun);
    489  1.14   chopps #ifdef DDB
    490  1.14   chopps 		Debugger();
    491  1.14   chopps #endif
    492  1.14   chopps 		return;
    493  1.14   chopps 	}
    494   1.1   chopps #endif
    495  1.10   chopps #if 1
    496  1.14   chopps 	if (((struct device *)(slp->device_softc))->dv_unit < dk_ndrive)
    497  1.14   chopps 		++dk_xfer[((struct device *)(slp->device_softc))->dv_unit];
    498  1.10   chopps #endif
    499   1.1   chopps 	/*
    500   1.1   chopps 	 * is this right?
    501   1.1   chopps 	 */
    502   1.1   chopps 	xs->status = stat;
    503   1.1   chopps 
    504  1.14   chopps #ifdef DEBUG
    505  1.14   chopps 	if( data_pointer_debug > 1 )
    506  1.14   chopps 		printf("scsidone: (%d,%d)->(%d,%d)%02x\n",
    507  1.14   chopps 		       slp->target, slp->lun,
    508  1.14   chopps 		       dev->target,  dev->lun,  stat);
    509  1.14   chopps 	if( xs->sc_link->target == dev->sc_link.adapter_target )
    510  1.14   chopps 		panic("target == hostid");
    511  1.14   chopps #endif
    512  1.14   chopps 
    513  1.14   chopps 	if (xs->error == XS_NOERROR && !(acb->flags & ACB_CHKSENSE)) {
    514  1.14   chopps 		if (stat == SCSI_CHECK) {
    515  1.14   chopps 			/* Schedule a REQUEST SENSE */
    516  1.14   chopps 			struct scsi_sense *ss = (void *)&acb->cmd;
    517  1.14   chopps #ifdef DEBUG
    518  1.14   chopps 			if (report_sense)
    519  1.14   chopps 				printf("sbic_scsidone: autosense %02x targ %d lun %d",
    520  1.14   chopps 				    acb->cmd.opcode, slp->target, slp->lun);
    521  1.14   chopps #endif
    522  1.14   chopps 			bzero(ss, sizeof(*ss));
    523  1.14   chopps 			ss->opcode = REQUEST_SENSE;
    524  1.14   chopps 			ss->byte2 = slp->lun << 5;
    525  1.14   chopps 			ss->length = sizeof(struct scsi_sense_data);
    526  1.14   chopps 			acb->clen = sizeof(*ss);
    527  1.14   chopps 			acb->sc_kv.dc_addr = (char *)&xs->sense;
    528  1.14   chopps 			acb->sc_kv.dc_count = sizeof(struct scsi_sense_data);
    529  1.14   chopps 			acb->pa_addr = (char *)kvtop(&xs->sense); /* XXX check */
    530  1.14   chopps 			acb->flags = ACB_ACTIVE | ACB_CHKSENSE | ACB_DATAIN;
    531  1.14   chopps 			TAILQ_INSERT_HEAD(&dev->ready_list, acb, chain);
    532  1.14   chopps 			dev->sc_tinfo[slp->target].lubusy &=
    533  1.14   chopps 			    ~(1 << slp->lun);
    534  1.14   chopps 			dev->sc_tinfo[slp->target].senses++;
    535  1.14   chopps 			if (dev->sc_nexus == acb) {
    536  1.14   chopps 				dev->sc_nexus = NULL;
    537  1.14   chopps 				sbic_sched(dev);
    538  1.14   chopps 			}
    539  1.14   chopps 			return;
    540  1.14   chopps 		}
    541  1.14   chopps 	}
    542  1.14   chopps 	if (xs->error == XS_NOERROR && (acb->flags & ACB_CHKSENSE)) {
    543  1.14   chopps 		xs->error = XS_SENSE;
    544  1.14   chopps #ifdef DEBUG
    545  1.14   chopps 		if (report_sense)
    546  1.14   chopps 			printf(" => %02x %02x\n", xs->sense.extended_flags,
    547  1.14   chopps 			    xs->sense.extended_extra_bytes[3]);
    548  1.14   chopps #endif
    549  1.14   chopps 	} else {
    550  1.14   chopps 		xs->resid = 0;		/* XXXX */
    551  1.14   chopps 	}
    552  1.14   chopps #if whataboutthisone
    553   1.1   chopps 		case SCSI_BUSY:
    554   1.1   chopps 			xs->error = XS_BUSY;
    555   1.1   chopps 			break;
    556  1.14   chopps #endif
    557   1.1   chopps 	xs->flags |= ITSDONE;
    558   1.1   chopps 
    559   1.1   chopps 	/*
    560  1.14   chopps 	 * Remove the ACB from whatever queue it's on.  We have to do a bit of
    561  1.14   chopps 	 * a hack to figure out which queue it's on.  Note that it is *not*
    562  1.14   chopps 	 * necessary to cdr down the ready queue, but we must cdr down the
    563  1.14   chopps 	 * nexus queue and see if it's there, so we can mark the unit as no
    564  1.14   chopps 	 * longer busy.  This code is sickening, but it works.
    565  1.14   chopps 	 */
    566  1.14   chopps 	if (acb == dev->sc_nexus) {
    567  1.14   chopps 		dev->sc_nexus = NULL;
    568  1.14   chopps 		dev->sc_tinfo[slp->target].lubusy &= ~(1<<slp->lun);
    569  1.14   chopps 		if (dev->ready_list.tqh_first)
    570  1.14   chopps 			dosched = 1;	/* start next command */
    571  1.14   chopps 	} else if (dev->ready_list.tqh_last == &acb->chain.tqe_next) {
    572  1.14   chopps 		TAILQ_REMOVE(&dev->ready_list, acb, chain);
    573   1.1   chopps 	} else {
    574  1.14   chopps 		register struct sbic_acb *acb2;
    575  1.14   chopps 		for (acb2 = dev->nexus_list.tqh_first; acb2;
    576  1.14   chopps 		    acb2 = acb2->chain.tqe_next)
    577  1.14   chopps 			if (acb2 == acb) {
    578  1.14   chopps 				TAILQ_REMOVE(&dev->nexus_list, acb, chain);
    579  1.14   chopps 				dev->sc_tinfo[slp->target].lubusy
    580  1.14   chopps 					&= ~(1<<slp->lun);
    581  1.14   chopps 				break;
    582  1.14   chopps 			}
    583  1.14   chopps 		if (acb2)
    584  1.14   chopps 			;
    585  1.14   chopps 		else if (acb->chain.tqe_next) {
    586  1.14   chopps 			TAILQ_REMOVE(&dev->ready_list, acb, chain);
    587  1.14   chopps 		} else {
    588  1.14   chopps 			printf("%s: can't find matching acb\n",
    589  1.14   chopps 			    dev->sc_dev.dv_xname);
    590  1.14   chopps #ifdef DDB
    591  1.14   chopps 			Debugger();
    592  1.14   chopps #endif
    593  1.14   chopps 		}
    594   1.1   chopps 	}
    595  1.14   chopps 	/* Put it on the free list. */
    596  1.14   chopps 	acb->flags = ACB_FREE;
    597  1.14   chopps 	TAILQ_INSERT_HEAD(&dev->free_list, acb, chain);
    598   1.1   chopps 
    599  1.14   chopps 	dev->sc_tinfo[slp->target].cmds++;
    600   1.1   chopps 
    601  1.14   chopps 	scsi_done(xs);
    602  1.13  mycroft 
    603  1.14   chopps 	if (dosched)
    604  1.14   chopps 		sbic_sched(dev);
    605   1.1   chopps }
    606   1.1   chopps 
    607   1.1   chopps int
    608   1.1   chopps sbicdmaok(dev, xs)
    609   1.1   chopps 	struct sbic_softc *dev;
    610   1.1   chopps 	struct scsi_xfer *xs;
    611   1.1   chopps {
    612   1.1   chopps 	if (sbic_no_dma || xs->datalen & 0x1 || (u_int)xs->data & 0x3)
    613   1.1   chopps 		return(0);
    614   1.1   chopps 	/*
    615   1.1   chopps 	 * controller supports dma to any addresses?
    616   1.1   chopps 	 */
    617  1.13  mycroft 	else if ((dev->sc_flags & SBICF_BADDMA) == 0)
    618   1.1   chopps 		return(1);
    619   1.1   chopps 	/*
    620   1.1   chopps 	 * this address is ok for dma?
    621   1.1   chopps 	 */
    622   1.1   chopps 	else if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
    623   1.1   chopps 		return(1);
    624   1.1   chopps 	/*
    625   1.1   chopps 	 * we have a bounce buffer?
    626   1.1   chopps 	 */
    627  1.14   chopps 	else if (dev->sc_tinfo[xs->sc_link->target].bounce)
    628  1.14   chopps 		return(1);
    629  1.14   chopps 	/*
    630  1.14   chopps 	 * try to get one
    631  1.14   chopps 	 */
    632  1.14   chopps 	else if (dev->sc_tinfo[xs->sc_link->target].bounce
    633  1.14   chopps 		 = (char *)alloc_z2mem(MAXPHYS)) {
    634  1.14   chopps 		if (isztwomem(dev->sc_tinfo[xs->sc_link->target].bounce))
    635  1.14   chopps 			printf("alloc ZII target %d bounce pa 0x%x\n",
    636  1.14   chopps 			       xs->sc_link->target,
    637  1.14   chopps 			       kvtop(dev->sc_tinfo[xs->sc_link->target].bounce));
    638  1.14   chopps 		else if (dev->sc_tinfo[xs->sc_link->target].bounce)
    639  1.14   chopps 			printf("alloc CHIP target %d bounce pa 0x%x\n",
    640  1.14   chopps 			       xs->sc_link->target,
    641  1.14   chopps 			       PREP_DMA_MEM(dev->sc_tinfo[xs->sc_link->target].bounce));
    642   1.1   chopps 		return(1);
    643  1.14   chopps 	}
    644  1.14   chopps 
    645   1.1   chopps 	return(0);
    646   1.1   chopps }
    647   1.1   chopps 
    648   1.1   chopps 
    649   1.1   chopps int
    650   1.1   chopps sbicwait(regs, until, timeo, line)
    651   1.1   chopps 	sbic_regmap_p regs;
    652   1.1   chopps 	char until;
    653   1.1   chopps 	int timeo;
    654   1.1   chopps 	int line;
    655   1.1   chopps {
    656   1.1   chopps 	u_char val;
    657   1.1   chopps 	int csr;
    658   1.1   chopps 
    659   1.1   chopps 	if (timeo == 0)
    660   1.1   chopps 		timeo = 1000000;	/* some large value.. */
    661   1.1   chopps 
    662   1.1   chopps 	GET_SBIC_asr(regs,val);
    663   1.1   chopps 	while ((val & until) == 0) {
    664   1.1   chopps 		if (timeo-- == 0) {
    665   1.1   chopps 			GET_SBIC_csr(regs, csr);
    666   1.1   chopps 			printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
    667   1.1   chopps 			    line, val, csr);
    668  1.14   chopps #if defined(DDB) && defined(DEBUG)
    669  1.14   chopps 			Debugger();
    670  1.14   chopps #endif
    671  1.14   chopps 			return(val); /* Maybe I should abort */
    672   1.1   chopps 			break;
    673   1.1   chopps 		}
    674   1.1   chopps 		DELAY(1);
    675   1.1   chopps 		GET_SBIC_asr(regs,val);
    676   1.1   chopps 	}
    677   1.1   chopps 	return(val);
    678   1.1   chopps }
    679   1.1   chopps 
    680  1.14   chopps int
    681   1.1   chopps sbicabort(dev, regs, where)
    682   1.1   chopps 	struct sbic_softc *dev;
    683   1.1   chopps 	sbic_regmap_p regs;
    684   1.1   chopps 	char *where;
    685   1.1   chopps {
    686   1.1   chopps 	u_char csr, asr;
    687  1.13  mycroft 
    688   1.1   chopps 	GET_SBIC_csr(regs, csr);
    689   1.1   chopps 	GET_SBIC_asr(regs, asr);
    690   1.1   chopps 
    691  1.13  mycroft 	printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
    692   1.1   chopps 	    dev->sc_dev.dv_xname, where, csr, asr);
    693   1.1   chopps 
    694  1.14   chopps 
    695  1.14   chopps #if 0
    696  1.14   chopps 	/* Clean up running command */
    697  1.14   chopps 	if (dev->sc_nexus != NULL) {
    698  1.14   chopps 		dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
    699  1.14   chopps 		sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
    700  1.14   chopps 	}
    701  1.14   chopps 	while (acb = dev->nexus_list.tqh_first) {
    702  1.14   chopps 		acb->xs->error = XS_DRIVER_STUFFUP;
    703  1.14   chopps 		sbic_scsidone(acb, -1 /*acb->stat[0]*/);
    704  1.14   chopps 	}
    705  1.14   chopps #endif
    706  1.14   chopps 
    707  1.14   chopps 	/* Clean up chip itself */
    708   1.1   chopps 	if (dev->sc_flags & SBICF_SELECTED) {
    709  1.14   chopps 		while( asr & SBIC_ASR_DBR ) {
    710  1.14   chopps 			/* sbic is jammed w/data. need to clear it */
    711  1.14   chopps 			/* But we don't know what direction it needs to go */
    712  1.14   chopps 			GET_SBIC_data(regs, asr);
    713  1.14   chopps 			printf("%s: abort %s: clearing data buffer 0x%02x\n",
    714  1.14   chopps 			       dev->sc_dev.dv_xname, where, asr);
    715  1.14   chopps 			GET_SBIC_asr(regs, asr);
    716  1.14   chopps 			if( asr & SBIC_ASR_DBR ) /* Not the read direction, then */
    717  1.14   chopps 				SET_SBIC_data(regs, asr);
    718  1.14   chopps 			GET_SBIC_asr(regs, asr);
    719  1.14   chopps 		}
    720  1.14   chopps 		WAIT_CIP(regs);
    721   1.1   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    722   1.1   chopps 		WAIT_CIP(regs);
    723   1.1   chopps 
    724   1.1   chopps 		GET_SBIC_asr(regs, asr);
    725   1.1   chopps 		if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
    726   1.1   chopps 			/* ok, get more drastic.. */
    727  1.13  mycroft 
    728  1.14   chopps 			sbicreset(dev);
    729   1.1   chopps 			dev->sc_flags &= ~SBICF_SELECTED;
    730  1.14   chopps 			return -1;
    731   1.1   chopps 		}
    732  1.14   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_DISC);
    733   1.1   chopps 
    734   1.1   chopps 		do {
    735   1.1   chopps 			SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    736   1.1   chopps 			GET_SBIC_csr (regs, csr);
    737   1.1   chopps 		} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    738   1.1   chopps 		    && (csr != SBIC_CSR_CMD_INVALID));
    739   1.1   chopps 
    740   1.1   chopps 		/* lets just hope it worked.. */
    741   1.1   chopps 		dev->sc_flags &= ~SBICF_SELECTED;
    742   1.1   chopps 	}
    743  1.14   chopps 	return -1;
    744   1.1   chopps }
    745   1.1   chopps 
    746  1.14   chopps 
    747   1.1   chopps /*
    748  1.14   chopps  * Initialize driver-private structures
    749   1.1   chopps  */
    750  1.14   chopps 
    751   1.1   chopps void
    752  1.14   chopps sbicinit(dev)
    753  1.14   chopps 	struct sbic_softc *dev;
    754   1.1   chopps {
    755  1.14   chopps 	sbic_regmap_p regs;
    756  1.14   chopps 	u_int my_id, i, s;
    757  1.14   chopps 	u_char csr;
    758  1.14   chopps 	struct sbic_acb *acb;
    759  1.14   chopps 
    760  1.14   chopps 	regs = dev->sc_sbicp;
    761   1.1   chopps 
    762  1.14   chopps 	if ((dev->sc_flags & SBICF_ALIVE) == 0) {
    763  1.14   chopps 		TAILQ_INIT(&dev->ready_list);
    764  1.14   chopps 		TAILQ_INIT(&dev->nexus_list);
    765  1.14   chopps 		TAILQ_INIT(&dev->free_list);
    766  1.14   chopps 		dev->sc_nexus = NULL;
    767  1.14   chopps 		dev->sc_xs = NULL;
    768  1.14   chopps 		acb = dev->sc_acb;
    769  1.14   chopps 		bzero(acb, sizeof(dev->sc_acb));
    770  1.14   chopps 		for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
    771  1.14   chopps 			TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
    772  1.14   chopps 			acb++;
    773  1.14   chopps 		}
    774  1.14   chopps 		bzero(dev->sc_tinfo, sizeof(dev->sc_tinfo));
    775  1.14   chopps 	} else panic("sbic: reinitializing driver!");
    776  1.14   chopps 
    777  1.14   chopps 	dev->sc_flags |= SBICF_ALIVE;
    778  1.14   chopps 	dev->sc_flags &= ~SBICF_SELECTED;
    779  1.14   chopps 
    780  1.14   chopps 	sbicreset(dev);
    781   1.1   chopps }
    782   1.1   chopps 
    783   1.1   chopps void
    784   1.1   chopps sbicreset(dev)
    785   1.1   chopps 	struct sbic_softc *dev;
    786   1.1   chopps {
    787   1.1   chopps 	sbic_regmap_p regs;
    788  1.14   chopps 	u_int my_id, i, s;
    789  1.14   chopps 	u_char csr;
    790  1.14   chopps 	struct sbic_acb *acb;
    791  1.13  mycroft 
    792   1.1   chopps 	regs = dev->sc_sbicp;
    793  1.14   chopps #if 0
    794  1.14   chopps 	if (dev->sc_flags & SBICF_ALIVE) {
    795  1.14   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    796  1.14   chopps 		WAIT_CIP(regs);
    797  1.14   chopps 	}
    798  1.14   chopps #else
    799  1.14   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    800  1.14   chopps 		WAIT_CIP(regs);
    801  1.14   chopps #endif
    802   1.1   chopps 	s = splbio();
    803  1.14   chopps 	my_id = dev->sc_link.adapter_target & SBIC_ID_MASK;
    804   1.1   chopps 
    805  1.14   chopps 	/* Enable advanced mode */
    806   1.1   chopps 	my_id |= SBIC_ID_EAF /*| SBIC_ID_EHP*/ ;
    807   1.1   chopps 	SET_SBIC_myid(regs, my_id);
    808   1.1   chopps 
    809   1.1   chopps 	/*
    810   1.1   chopps 	 * Disable interrupts (in dmainit) then reset the chip
    811   1.1   chopps 	 */
    812   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_RESET);
    813   1.1   chopps 	DELAY(25);
    814   1.1   chopps 	SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    815   1.1   chopps 	GET_SBIC_csr(regs, csr);       /* clears interrupt also */
    816   1.1   chopps 
    817  1.14   chopps 	if (dev->sc_clkfreq < 110)
    818  1.14   chopps 		my_id |= SBIC_ID_FS_8_10;
    819  1.14   chopps 	else if (dev->sc_clkfreq < 160)
    820  1.14   chopps 		my_id |= SBIC_ID_FS_12_15;
    821  1.14   chopps 	else if (dev->sc_clkfreq < 210)
    822  1.14   chopps 		my_id |= SBIC_ID_FS_16_20;
    823  1.14   chopps 
    824  1.14   chopps 	SET_SBIC_myid(regs, my_id);
    825  1.14   chopps 
    826   1.1   chopps 	/*
    827   1.1   chopps 	 * Set up various chip parameters
    828   1.1   chopps 	 */
    829  1.14   chopps 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /* | SBIC_CTL_HSP */
    830   1.1   chopps 	    | SBIC_MACHINE_DMA_MODE);
    831   1.1   chopps 	/*
    832   1.1   chopps 	 * don't allow (re)selection (SBIC_RID_ES)
    833   1.1   chopps 	 * until we can handle target mode!!
    834   1.1   chopps 	 */
    835  1.14   chopps 	SET_SBIC_rselid(regs, SBIC_RID_ER);
    836   1.1   chopps 	SET_SBIC_syn(regs, 0);     /* asynch for now */
    837   1.1   chopps 
    838   1.1   chopps 	/*
    839   1.1   chopps 	 * anything else was zeroed by reset
    840   1.1   chopps 	 */
    841   1.1   chopps 	splx(s);
    842   1.1   chopps 
    843  1.14   chopps #if 0
    844  1.14   chopps 	if ((dev->sc_flags & SBICF_ALIVE) == 0) {
    845  1.14   chopps 		TAILQ_INIT(&dev->ready_list);
    846  1.14   chopps 		TAILQ_INIT(&dev->nexus_list);
    847  1.14   chopps 		TAILQ_INIT(&dev->free_list);
    848  1.14   chopps 		dev->sc_nexus = NULL;
    849  1.14   chopps 		dev->sc_xs = NULL;
    850  1.14   chopps 		acb = dev->sc_acb;
    851  1.14   chopps 		bzero(acb, sizeof(dev->sc_acb));
    852  1.14   chopps 		for (i = 0; i < sizeof(dev->sc_acb) / sizeof(*acb); i++) {
    853  1.14   chopps 			TAILQ_INSERT_TAIL(&dev->free_list, acb, chain);
    854  1.14   chopps 			acb++;
    855  1.14   chopps 		}
    856  1.14   chopps 		bzero(dev->sc_tinfo, sizeof(dev->sc_tinfo));
    857  1.14   chopps 	} else {
    858  1.14   chopps 		if (dev->sc_nexus != NULL) {
    859  1.14   chopps 			dev->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
    860  1.14   chopps 			sbic_scsidone(dev->sc_nexus, dev->sc_stat[0]);
    861  1.14   chopps 		}
    862  1.14   chopps 		while (acb = dev->nexus_list.tqh_first) {
    863  1.14   chopps 			acb->xs->error = XS_DRIVER_STUFFUP;
    864  1.14   chopps 			sbic_scsidone(acb, -1 /*acb->stat[0]*/);
    865  1.14   chopps 		}
    866  1.14   chopps 	}
    867  1.14   chopps 
    868   1.1   chopps 	dev->sc_flags |= SBICF_ALIVE;
    869  1.14   chopps #endif
    870   1.1   chopps 	dev->sc_flags &= ~SBICF_SELECTED;
    871   1.1   chopps }
    872   1.1   chopps 
    873   1.1   chopps void
    874   1.1   chopps sbicerror(dev, regs, csr)
    875   1.1   chopps 	struct sbic_softc *dev;
    876   1.1   chopps 	sbic_regmap_p regs;
    877   1.1   chopps 	u_char csr;
    878   1.1   chopps {
    879   1.1   chopps 	struct scsi_xfer *xs;
    880   1.1   chopps 
    881   1.1   chopps 	xs = dev->sc_xs;
    882   1.1   chopps 
    883   1.1   chopps #ifdef DIAGNOSTIC
    884   1.1   chopps 	if (xs == NULL)
    885   1.1   chopps 		panic("sbicerror");
    886   1.1   chopps #endif
    887   1.1   chopps 	if (xs->flags & SCSI_SILENT)
    888   1.1   chopps 		return;
    889   1.1   chopps 
    890   1.1   chopps 	printf("%s: ", dev->sc_dev.dv_xname);
    891  1.14   chopps 	printf("csr == 0x%02x\n", csr);	/* XXX */
    892   1.1   chopps }
    893   1.1   chopps 
    894   1.1   chopps /*
    895   1.1   chopps  * select the bus, return when selected or error.
    896   1.1   chopps  */
    897   1.1   chopps int
    898   1.7   chopps sbicselectbus(dev, regs, target, lun, our_addr)
    899   1.1   chopps         struct sbic_softc *dev;
    900   1.1   chopps 	sbic_regmap_p regs;
    901   1.7   chopps 	u_char target, lun, our_addr;
    902   1.1   chopps {
    903   1.1   chopps 	u_char asr, csr, id;
    904   1.1   chopps 
    905   1.1   chopps 	QPRINTF(("sbicselectbus %d\n", target));
    906   1.1   chopps 
    907  1.13  mycroft 	/*
    908   1.1   chopps 	 * if we're already selected, return (XXXX panic maybe?)
    909   1.1   chopps 	 */
    910   1.1   chopps 	if (dev->sc_flags & SBICF_SELECTED)
    911   1.1   chopps 		return(1);
    912   1.1   chopps 
    913  1.14   chopps 	SET_SBIC_rselid (regs, 0);
    914  1.14   chopps #if 0
    915  1.14   chopps 	GET_SBIC_asr(regs, asr);
    916  1.14   chopps 	if( asr & (SBIC_ASR_INT|SBIC_ASR_BSY) ) {
    917  1.14   chopps 		/* This means we got ourselves reselected upon */
    918  1.14   chopps /*		printf("sbicselectbus: weird asr %02x\n", asr);*/
    919  1.14   chopps #ifdef DDB
    920  1.14   chopps /*		Debugger();*/
    921  1.14   chopps #endif
    922  1.14   chopps 		SET_SBIC_rselid (regs, SBIC_RID_ER);
    923  1.14   chopps 		return 1;
    924  1.14   chopps 	}
    925  1.14   chopps #endif
    926   1.1   chopps 	/*
    927   1.1   chopps 	 * issue select
    928   1.1   chopps 	 */
    929   1.1   chopps 	SBIC_TC_PUT(regs, 0);
    930   1.1   chopps 	SET_SBIC_selid(regs, target);
    931   1.1   chopps 	SET_SBIC_timeo(regs, SBIC_TIMEOUT(250,dev->sc_clkfreq));
    932   1.1   chopps 
    933   1.1   chopps 	/*
    934   1.1   chopps 	 * set sync or async
    935   1.1   chopps 	 */
    936   1.1   chopps 	if (dev->sc_sync[target].state == SYNC_DONE)
    937  1.13  mycroft 		SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[target].offset,
    938   1.1   chopps 		    dev->sc_sync[target].period));
    939   1.1   chopps 	else
    940   1.1   chopps 		SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
    941  1.13  mycroft 
    942  1.14   chopps 	GET_SBIC_asr(regs, asr);
    943  1.14   chopps 	if( asr & (SBIC_ASR_INT|SBIC_ASR_BSY) ) {
    944  1.14   chopps 		/* This means we got ourselves reselected upon */
    945  1.14   chopps /*		printf("sbicselectbus: INT/BSY asr %02x\n", asr);*/
    946  1.14   chopps #ifdef DDB
    947  1.14   chopps /*		Debugger();*/
    948  1.14   chopps #endif
    949  1.14   chopps 		return 1;
    950  1.14   chopps 	}
    951  1.14   chopps 
    952   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
    953   1.1   chopps 
    954   1.1   chopps 	/*
    955   1.1   chopps 	 * wait for select (merged from seperate function may need
    956   1.1   chopps 	 * cleanup)
    957   1.1   chopps 	 */
    958   1.1   chopps 	WAIT_CIP(regs);
    959  1.14   chopps #if 0
    960  1.14   chopps 	GET_SBIC_asr(regs, asr);
    961  1.14   chopps 	if( asr & SBIC_ASR_LCI ) {
    962  1.14   chopps 		/* This means we got ourselves reselected upon */
    963  1.14   chopps #ifdef DEBUG
    964  1.14   chopps 		if (reselect_debug)
    965  1.14   chopps 			printf("sbicselectbus: LCI asr %02x\n", asr);
    966  1.14   chopps /*		Debugger();*/
    967  1.14   chopps #endif
    968  1.14   chopps 		return 1;
    969  1.14   chopps 	}
    970  1.14   chopps #endif
    971   1.1   chopps 	do {
    972  1.14   chopps 		asr = SBIC_WAIT(regs, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
    973  1.14   chopps 		if (asr & SBIC_ASR_LCI) {
    974  1.14   chopps #ifdef DEBUG
    975  1.14   chopps 			if (reselect_debug)
    976  1.14   chopps 				printf("sbicselectbus: late LCI asr %02x\n", asr);
    977  1.14   chopps #endif
    978  1.14   chopps 			return 1;
    979  1.14   chopps 		}
    980   1.1   chopps 		GET_SBIC_csr (regs, csr);
    981   1.1   chopps 		QPRINTF(("%02x ", csr));
    982  1.14   chopps 		if( csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
    983  1.14   chopps #ifdef DEBUG
    984  1.14   chopps 			if( reselect_debug || 1 )
    985  1.14   chopps 				printf("sbicselectbus: reselected asr %02x\n", asr);
    986  1.14   chopps #endif
    987  1.15   chopps 			/* We need to handle this now so we don't lock up later */
    988  1.15   chopps 			sbicnextstate(dev, csr, asr);
    989  1.14   chopps 			return 1;
    990  1.14   chopps 		}
    991  1.14   chopps 		if( csr == SBIC_CSR_SLT || csr == SBIC_CSR_SLT_ATN) {
    992  1.14   chopps 			panic("sbicselectbus: target issued select!");
    993  1.14   chopps 			return 1;
    994  1.14   chopps 		}
    995   1.1   chopps 	} while (csr != (SBIC_CSR_MIS_2|MESG_OUT_PHASE)
    996   1.1   chopps 	    && csr != (SBIC_CSR_MIS_2|CMD_PHASE) && csr != SBIC_CSR_SEL_TIMEO);
    997   1.1   chopps 
    998  1.14   chopps 	/* Enable (or not) reselection */
    999  1.14   chopps 	if( (dev->sc_xs->flags & SCSI_POLL
   1000  1.14   chopps 	   || (dev->sc_flags & SBICF_ICMD)
   1001  1.14   chopps 	   || !sbic_enable_reselect)
   1002  1.14   chopps 	   && dev->nexus_list.tqh_first == NULL )
   1003  1.14   chopps 		SET_SBIC_rselid (regs, 0);
   1004  1.14   chopps 	else
   1005  1.14   chopps 		SET_SBIC_rselid (regs, SBIC_RID_ER);
   1006  1.14   chopps 
   1007  1.14   chopps 	if (csr == (SBIC_CSR_MIS_2|CMD_PHASE)) {
   1008   1.1   chopps 		dev->sc_flags |= SBICF_SELECTED;	/* device ignored ATN */
   1009  1.14   chopps 		GET_SBIC_selid(regs, id);
   1010  1.14   chopps 		dev->target = id;
   1011  1.14   chopps 		GET_SBIC_tlun(regs,dev->lun);
   1012  1.14   chopps 		if( dev->lun & SBIC_TLUN_VALID )
   1013  1.14   chopps 			dev->lun &= SBIC_TLUN_MASK;
   1014  1.14   chopps 		else
   1015  1.14   chopps 			dev->lun = lun;
   1016  1.14   chopps 	} else if (csr == (SBIC_CSR_MIS_2|MESG_OUT_PHASE)) {
   1017   1.1   chopps 		/*
   1018   1.1   chopps 		 * Send identify message
   1019   1.1   chopps 		 * (SCSI-2 requires an identify msg (?))
   1020   1.1   chopps 		 */
   1021   1.1   chopps 		GET_SBIC_selid(regs, id);
   1022  1.14   chopps 		dev->target = id;
   1023  1.14   chopps 		GET_SBIC_tlun(regs,dev->lun);
   1024  1.14   chopps 		if( dev->lun & SBIC_TLUN_VALID )
   1025  1.14   chopps 			dev->lun &= SBIC_TLUN_MASK;
   1026  1.14   chopps 		else
   1027  1.14   chopps 			dev->lun = lun;
   1028  1.13  mycroft 		/*
   1029  1.13  mycroft 		 * handle drives that don't want to be asked
   1030   1.1   chopps 		 * whether to go sync at all.
   1031   1.1   chopps 		 */
   1032   1.1   chopps 		if (sbic_inhibit_sync && dev->sc_sync[id].state == SYNC_START) {
   1033   1.1   chopps #ifdef DEBUG
   1034   1.1   chopps 			if (sync_debug)
   1035   1.1   chopps 				printf("Forcing target %d asynchronous.\n", id);
   1036   1.1   chopps #endif
   1037   1.1   chopps 			dev->sc_sync[id].offset = 0;
   1038   1.1   chopps 			dev->sc_sync[id].period = sbic_min_period;
   1039   1.1   chopps 			dev->sc_sync[id].state = SYNC_DONE;
   1040   1.1   chopps 		}
   1041  1.13  mycroft 
   1042   1.1   chopps 
   1043  1.14   chopps 		if (dev->sc_sync[id].state != SYNC_START){
   1044  1.14   chopps 			if( dev->sc_xs->flags & SCSI_POLL
   1045  1.14   chopps 			   || (dev->sc_flags & SBICF_ICMD)
   1046  1.14   chopps 			   || !sbic_enable_reselect )
   1047  1.14   chopps 				SEND_BYTE (regs, MSG_IDENTIFY | lun);
   1048  1.14   chopps 			else
   1049  1.14   chopps 				SEND_BYTE (regs, MSG_IDENTIFY_DR | lun);
   1050  1.14   chopps 		} else {
   1051   1.1   chopps 			/*
   1052   1.1   chopps 			 * try to initiate a sync transfer.
   1053  1.13  mycroft 			 * So compose the sync message we're going
   1054   1.1   chopps 			 * to send to the target
   1055   1.1   chopps 			 */
   1056   1.1   chopps 
   1057   1.1   chopps #ifdef DEBUG
   1058   1.1   chopps 			if (sync_debug)
   1059   1.1   chopps 				printf("Sending sync request to target %d ... ",
   1060   1.1   chopps 				    id);
   1061   1.1   chopps #endif
   1062   1.1   chopps 			/*
   1063   1.1   chopps 			 * setup scsi message sync message request
   1064   1.1   chopps 			 */
   1065   1.7   chopps 			dev->sc_msg[0] = MSG_IDENTIFY | lun;
   1066   1.1   chopps 			dev->sc_msg[1] = MSG_EXT_MESSAGE;
   1067   1.1   chopps 			dev->sc_msg[2] = 3;
   1068   1.1   chopps 			dev->sc_msg[3] = MSG_SYNC_REQ;
   1069   1.1   chopps 			dev->sc_msg[4] = sbictoscsiperiod(dev, regs,
   1070   1.1   chopps 			    sbic_min_period);
   1071   1.1   chopps 			dev->sc_msg[5] = sbic_max_offset;
   1072   1.1   chopps 
   1073   1.1   chopps 			if (sbicxfstart(regs, 6, MESG_OUT_PHASE, sbic_cmd_wait))
   1074   1.1   chopps 				sbicxfout(regs, 6, dev->sc_msg, MESG_OUT_PHASE);
   1075   1.1   chopps 
   1076   1.1   chopps 			dev->sc_sync[id].state = SYNC_SENT;
   1077   1.1   chopps #ifdef DEBUG
   1078   1.1   chopps 			if (sync_debug)
   1079   1.1   chopps 				printf ("sent\n");
   1080   1.1   chopps #endif
   1081   1.1   chopps 		}
   1082   1.1   chopps 
   1083   1.1   chopps 		SBIC_WAIT (regs, SBIC_ASR_INT, 0);
   1084   1.1   chopps 		GET_SBIC_csr (regs, csr);
   1085   1.1   chopps 		QPRINTF(("[%02x]", csr));
   1086   1.1   chopps #ifdef DEBUG
   1087   1.1   chopps 		if (sync_debug && dev->sc_sync[id].state == SYNC_SENT)
   1088   1.1   chopps 			printf("csr-result of last msgout: 0x%x\n", csr);
   1089   1.1   chopps #endif
   1090   1.1   chopps 
   1091   1.1   chopps 		if (csr != SBIC_CSR_SEL_TIMEO)
   1092   1.1   chopps 			dev->sc_flags |= SBICF_SELECTED;
   1093   1.1   chopps 	}
   1094  1.14   chopps 	if (csr == SBIC_CSR_SEL_TIMEO)
   1095  1.14   chopps 		dev->sc_xs->error = XS_SELTIMEOUT;
   1096  1.13  mycroft 
   1097   1.1   chopps 	QPRINTF(("\n"));
   1098   1.1   chopps 
   1099   1.1   chopps 	return(csr == SBIC_CSR_SEL_TIMEO);
   1100   1.1   chopps }
   1101   1.1   chopps 
   1102   1.1   chopps int
   1103   1.1   chopps sbicxfstart(regs, len, phase, wait)
   1104   1.1   chopps 	sbic_regmap_p regs;
   1105   1.1   chopps 	int len, wait;
   1106   1.1   chopps 	u_char phase;
   1107   1.1   chopps {
   1108   1.1   chopps 	u_char id;
   1109   1.1   chopps 
   1110  1.14   chopps 	switch (phase) {
   1111  1.14   chopps 	case DATA_IN_PHASE:
   1112  1.14   chopps 	case MESG_IN_PHASE:
   1113   1.1   chopps 		GET_SBIC_selid (regs, id);
   1114   1.1   chopps 		id |= SBIC_SID_FROM_SCSI;
   1115   1.1   chopps 		SET_SBIC_selid (regs, id);
   1116   1.1   chopps 		SBIC_TC_PUT (regs, (unsigned)len);
   1117  1.14   chopps 		break;
   1118  1.14   chopps 	case DATA_OUT_PHASE:
   1119  1.14   chopps 	case MESG_OUT_PHASE:
   1120  1.14   chopps 	case CMD_PHASE:
   1121  1.14   chopps 		GET_SBIC_selid (regs, id);
   1122  1.14   chopps 		id &= ~SBIC_SID_FROM_SCSI;
   1123  1.14   chopps 		SET_SBIC_selid (regs, id);
   1124   1.1   chopps 		SBIC_TC_PUT (regs, (unsigned)len);
   1125  1.14   chopps 		break;
   1126  1.14   chopps 	default:
   1127   1.1   chopps 		SBIC_TC_PUT (regs, 0);
   1128  1.14   chopps 	}
   1129   1.1   chopps 	QPRINTF(("sbicxfstart %d, %d, %d\n", len, phase, wait));
   1130   1.1   chopps 
   1131   1.1   chopps 	return(1);
   1132   1.1   chopps }
   1133   1.1   chopps 
   1134   1.1   chopps int
   1135   1.1   chopps sbicxfout(regs, len, bp, phase)
   1136   1.1   chopps 	sbic_regmap_p regs;
   1137   1.1   chopps 	int len;
   1138   1.1   chopps 	void *bp;
   1139   1.1   chopps 	int phase;
   1140   1.1   chopps {
   1141   1.1   chopps 	u_char orig_csr, csr, asr, *buf;
   1142   1.1   chopps 	int wait;
   1143  1.13  mycroft 
   1144   1.1   chopps 	buf = bp;
   1145   1.1   chopps 	wait = sbic_data_wait;
   1146   1.1   chopps 
   1147   1.1   chopps 	QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
   1148  1.13  mycroft 	    "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
   1149   1.1   chopps 	    buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
   1150   1.1   chopps 
   1151   1.1   chopps 	GET_SBIC_csr (regs, orig_csr);
   1152   1.1   chopps 
   1153  1.13  mycroft 	/*
   1154   1.1   chopps 	 * sigh.. WD-PROTO strikes again.. sending the command in one go
   1155   1.1   chopps 	 * causes the chip to lock up if talking to certain (misbehaving?)
   1156   1.1   chopps 	 * targets. Anyway, this procedure should work for all targets, but
   1157   1.1   chopps 	 * it's slightly slower due to the overhead
   1158   1.1   chopps 	 */
   1159   1.1   chopps 	WAIT_CIP (regs);
   1160   1.1   chopps 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
   1161   1.1   chopps 	for (;len > 0; len--) {
   1162   1.1   chopps 		GET_SBIC_asr (regs, asr);
   1163   1.1   chopps 		while ((asr & SBIC_ASR_DBR) == 0) {
   1164   1.1   chopps 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
   1165   1.1   chopps #ifdef DEBUG
   1166   1.1   chopps 				if (sbic_debug)
   1167   1.1   chopps 					printf("sbicxfout fail: l%d i%x w%d\n",
   1168   1.1   chopps 					    len, asr, wait);
   1169   1.1   chopps #endif
   1170   1.1   chopps 				return (len);
   1171   1.1   chopps 			}
   1172  1.14   chopps /*			DELAY(1);*/
   1173   1.1   chopps 			GET_SBIC_asr (regs, asr);
   1174   1.1   chopps 		}
   1175   1.1   chopps 
   1176   1.1   chopps 		SET_SBIC_data (regs, *buf);
   1177   1.1   chopps 		buf++;
   1178   1.1   chopps 	}
   1179  1.14   chopps 	SBIC_TC_GET(regs, len);
   1180  1.14   chopps 	QPRINTF(("sbicxfout done %d bytes\n", len));
   1181   1.1   chopps 	/*
   1182   1.1   chopps 	 * this leaves with one csr to be read
   1183   1.1   chopps 	 */
   1184   1.1   chopps 	return(0);
   1185   1.1   chopps }
   1186   1.1   chopps 
   1187  1.14   chopps /* returns # bytes left to read */
   1188  1.14   chopps int
   1189   1.1   chopps sbicxfin(regs, len, bp)
   1190   1.1   chopps 	sbic_regmap_p regs;
   1191   1.1   chopps 	int len;
   1192   1.1   chopps 	void *bp;
   1193   1.1   chopps {
   1194  1.14   chopps 	int wait, read;
   1195   1.1   chopps 	u_char *obp, *buf;
   1196   1.1   chopps 	u_char orig_csr, csr, asr;
   1197  1.13  mycroft 
   1198   1.1   chopps 	wait = sbic_data_wait;
   1199   1.1   chopps 	obp = bp;
   1200   1.1   chopps 	buf = bp;
   1201   1.1   chopps 
   1202   1.1   chopps 	GET_SBIC_csr (regs, orig_csr);
   1203   1.1   chopps 
   1204   1.1   chopps 	QPRINTF(("sbicxfin %d, csr=%02x\n", len, orig_csr));
   1205   1.1   chopps 
   1206   1.1   chopps 	WAIT_CIP (regs);
   1207   1.1   chopps 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
   1208   1.1   chopps 	for (;len > 0; len--) {
   1209   1.1   chopps 		GET_SBIC_asr (regs, asr);
   1210  1.14   chopps 		if((asr & SBIC_ASR_PE)) {
   1211  1.14   chopps #ifdef DEBUG
   1212  1.14   chopps 			printf("sbicxfin parity error: l%d i%x w%d\n",
   1213  1.14   chopps 			       len, asr, wait);
   1214  1.14   chopps /*			return ((unsigned long)buf - (unsigned long)bp); */
   1215  1.14   chopps #ifdef DDB
   1216  1.14   chopps 			Debugger();
   1217  1.14   chopps #endif
   1218  1.14   chopps #endif
   1219  1.14   chopps 		}
   1220   1.1   chopps 		while ((asr & SBIC_ASR_DBR) == 0) {
   1221   1.1   chopps 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
   1222   1.1   chopps #ifdef DEBUG
   1223  1.14   chopps 				if (sbic_debug) {
   1224  1.14   chopps 	QPRINTF(("sbicxfin fail:{%d} %02x %02x %02x %02x %02x %02x "
   1225  1.14   chopps 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
   1226  1.14   chopps 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
   1227   1.1   chopps 					printf("sbicxfin fail: l%d i%x w%d\n",
   1228   1.1   chopps 					    len, asr, wait);
   1229  1.14   chopps }
   1230   1.1   chopps #endif
   1231  1.14   chopps 				return len;
   1232  1.14   chopps 			}
   1233  1.14   chopps 
   1234  1.14   chopps 			if( ! asr & SBIC_ASR_BSY ) {
   1235  1.14   chopps 				GET_SBIC_csr(regs, csr);
   1236  1.14   chopps 				QPRINTF(("[CSR%02xASR%02x]", csr, asr));
   1237   1.1   chopps 			}
   1238   1.1   chopps 
   1239  1.14   chopps /*			DELAY(1);*/
   1240   1.1   chopps 			GET_SBIC_asr (regs, asr);
   1241   1.1   chopps 		}
   1242   1.1   chopps 
   1243   1.1   chopps 		GET_SBIC_data (regs, *buf);
   1244  1.14   chopps /*		QPRINTF(("asr=%02x, csr=%02x, data=%02x\n", asr, csr, *buf));*/
   1245   1.1   chopps 		buf++;
   1246   1.1   chopps 	}
   1247   1.1   chopps 
   1248   1.1   chopps 	QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
   1249  1.13  mycroft 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
   1250   1.1   chopps 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
   1251   1.1   chopps 
   1252   1.1   chopps 	/* this leaves with one csr to be read */
   1253  1.14   chopps 	return len;
   1254   1.1   chopps }
   1255   1.1   chopps 
   1256   1.1   chopps /*
   1257   1.1   chopps  * SCSI 'immediate' command:  issue a command to some SCSI device
   1258   1.1   chopps  * and get back an 'immediate' response (i.e., do programmed xfer
   1259   1.1   chopps  * to get the response data).  'cbuf' is a buffer containing a scsi
   1260   1.1   chopps  * command of length clen bytes.  'buf' is a buffer of length 'len'
   1261   1.1   chopps  * bytes for data.  The transfer direction is determined by the device
   1262   1.1   chopps  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
   1263  1.14   chopps  * command must supply no data.
   1264   1.1   chopps  */
   1265   1.1   chopps int
   1266  1.14   chopps sbicicmd(dev, target, lun, cbuf, clen, buf, len)
   1267   1.1   chopps 	struct sbic_softc *dev;
   1268   1.1   chopps 	void *cbuf, *buf;
   1269   1.1   chopps 	int clen, len;
   1270   1.1   chopps {
   1271   1.1   chopps 	sbic_regmap_p regs;
   1272   1.1   chopps 	u_char phase, csr, asr;
   1273  1.14   chopps 	int wait, newtarget, cmd_sent, parity_err;
   1274  1.14   chopps 	struct sbic_acb *acb;
   1275  1.14   chopps 
   1276  1.14   chopps 	int discon;
   1277  1.14   chopps 	int i;
   1278  1.14   chopps 
   1279  1.14   chopps #define CSR_LOG_BUF_SIZE 0
   1280  1.14   chopps #if CSR_LOG_BUF_SIZE
   1281  1.14   chopps 	int bufptr;
   1282  1.14   chopps 	int csrbuf[CSR_LOG_BUF_SIZE];
   1283  1.14   chopps 	bufptr=0;
   1284  1.14   chopps #endif
   1285   1.1   chopps 
   1286   1.1   chopps 	regs = dev->sc_sbicp;
   1287  1.14   chopps 	acb = dev->sc_nexus;
   1288  1.14   chopps 
   1289  1.14   chopps 	/* Make sure pointers are OK */
   1290  1.14   chopps 	dev->sc_last = dev->sc_cur = &acb->sc_pa;
   1291  1.14   chopps 	dev->sc_tcnt = acb->sc_tcnt = 0;
   1292  1.14   chopps 	acb->sc_pa.dc_count = 0; /* No DMA */
   1293  1.14   chopps 	acb->sc_kv.dc_addr = buf;
   1294  1.14   chopps 	acb->sc_kv.dc_count = len;
   1295  1.14   chopps 
   1296  1.14   chopps #ifdef DEBUG
   1297  1.14   chopps 	routine = 3;
   1298  1.14   chopps 	debug_sbic_regs = regs; /* store this to allow debug calls */
   1299  1.14   chopps 	if( data_pointer_debug > 1 )
   1300  1.14   chopps 		printf("sbicicmd(%d,%d):%d\n", target, lun,
   1301  1.14   chopps 		       acb->sc_kv.dc_count);
   1302  1.14   chopps #endif
   1303   1.1   chopps 
   1304  1.13  mycroft 	/*
   1305   1.1   chopps 	 * set the sbic into non-DMA mode
   1306   1.1   chopps 	 */
   1307  1.14   chopps 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI /*| SBIC_CTL_HSP*/);
   1308   1.1   chopps 
   1309   1.1   chopps 	dev->sc_stat[0] = 0xff;
   1310   1.1   chopps 	dev->sc_msg[0] = 0xff;
   1311  1.14   chopps 	i = 1; /* pre-load */
   1312   1.1   chopps 
   1313  1.14   chopps 	/* We're stealing the SCSI bus */
   1314  1.14   chopps 	dev->sc_flags |= SBICF_ICMD;
   1315   1.1   chopps 
   1316  1.14   chopps 	do {
   1317  1.14   chopps 		/*
   1318  1.14   chopps 		 * select the SCSI bus (it's an error if bus isn't free)
   1319  1.14   chopps 		 */
   1320  1.14   chopps 		if (!( dev->sc_flags & SBICF_SELECTED )
   1321  1.14   chopps 		    && sbicselectbus(dev, regs, target, lun, dev->sc_scsiaddr)) {
   1322  1.14   chopps 			/*printf("sbicicmd trying to select busy bus!\n");*/
   1323  1.14   chopps 			dev->sc_flags &= ~SBICF_ICMD;
   1324   1.1   chopps 			return(-1);
   1325  1.14   chopps 		}
   1326   1.1   chopps 
   1327   1.1   chopps 		/*
   1328  1.14   chopps 		 * Wait for a phase change (or error) then let the device sequence
   1329  1.14   chopps 		 * us through the various SCSI phases.
   1330   1.1   chopps 		 */
   1331  1.14   chopps 
   1332  1.14   chopps 		wait = sbic_cmd_wait;
   1333  1.14   chopps 
   1334  1.14   chopps 		GET_SBIC_asr (regs, asr);
   1335  1.14   chopps 		GET_SBIC_csr (regs, csr);
   1336  1.14   chopps 		QPRINTF((">ASR:%02xCSR:%02x<", asr, csr));
   1337  1.14   chopps 
   1338  1.14   chopps #if CSR_LOG_BUF_SIZE
   1339  1.14   chopps 		csrbuf[bufptr++] = csr;
   1340  1.14   chopps #endif
   1341  1.14   chopps 
   1342  1.14   chopps 
   1343  1.14   chopps 		switch (csr) {
   1344  1.14   chopps 		case SBIC_CSR_S_XFERRED:
   1345  1.14   chopps 		case SBIC_CSR_DISC:
   1346  1.14   chopps 		case SBIC_CSR_DISC_1:
   1347  1.14   chopps 			dev->sc_flags &= ~SBICF_SELECTED;
   1348  1.14   chopps 			GET_SBIC_cmd_phase (regs, phase);
   1349  1.14   chopps 			if (phase == 0x60) {
   1350  1.14   chopps 				GET_SBIC_tlun (regs, dev->sc_stat[0]);
   1351  1.14   chopps 				i = 0; /* done */
   1352  1.14   chopps /*				break; /* Bypass all the state gobldygook */
   1353  1.14   chopps 			} else {
   1354   1.1   chopps #ifdef DEBUG
   1355  1.14   chopps 				if(reselect_debug>1)
   1356  1.14   chopps 					printf("sbicicmd: handling disconnect\n");
   1357   1.1   chopps #endif
   1358  1.14   chopps 				i = SBIC_STATE_DISCONNECT;
   1359  1.14   chopps 			}
   1360  1.14   chopps 			break;
   1361   1.1   chopps 
   1362  1.14   chopps 		case SBIC_CSR_XFERRED|CMD_PHASE:
   1363  1.14   chopps 		case SBIC_CSR_MIS|CMD_PHASE:
   1364  1.14   chopps 		case SBIC_CSR_MIS_1|CMD_PHASE:
   1365  1.14   chopps 		case SBIC_CSR_MIS_2|CMD_PHASE:
   1366  1.14   chopps 			if (sbicxfstart(regs, clen, CMD_PHASE, sbic_cmd_wait))
   1367  1.14   chopps 				if (sbicxfout(regs, clen,
   1368  1.14   chopps 					      cbuf, CMD_PHASE))
   1369  1.14   chopps 					i = sbicabort(dev, regs,"icmd sending cmd");
   1370  1.14   chopps #if 0
   1371  1.14   chopps 			GET_SBIC_csr(regs, csr); /* Lets us reload tcount */
   1372   1.1   chopps 			WAIT_CIP(regs);
   1373  1.14   chopps 			GET_SBIC_asr(regs, asr);
   1374  1.14   chopps 			if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
   1375  1.14   chopps 				printf("next: cmd sent asr %02x, csr %02x\n",
   1376  1.14   chopps 				       asr, csr);
   1377  1.14   chopps #endif
   1378  1.14   chopps 			break;
   1379  1.14   chopps 
   1380  1.14   chopps #if 0
   1381  1.14   chopps 		case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
   1382  1.14   chopps 		case SBIC_CSR_XFERRED|DATA_IN_PHASE:
   1383  1.14   chopps 		case SBIC_CSR_MIS|DATA_OUT_PHASE:
   1384  1.14   chopps 		case SBIC_CSR_MIS|DATA_IN_PHASE:
   1385  1.14   chopps 		case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
   1386  1.14   chopps 		case SBIC_CSR_MIS_1|DATA_IN_PHASE:
   1387  1.14   chopps 		case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
   1388  1.14   chopps 		case SBIC_CSR_MIS_2|DATA_IN_PHASE:
   1389  1.14   chopps 			if (acb->sc_kv.dc_count <= 0)
   1390  1.14   chopps 				i = sbicabort(dev, regs, "icmd out of data");
   1391  1.14   chopps 			else {
   1392  1.14   chopps 			  wait = sbic_data_wait;
   1393  1.14   chopps 			  if (sbicxfstart(regs,
   1394  1.14   chopps 					  acb->sc_kv.dc_count,
   1395  1.14   chopps 					  SBIC_PHASE(csr), wait))
   1396  1.14   chopps 			    if (csr & 0x01)
   1397  1.14   chopps 			      /* data in? */
   1398  1.14   chopps 			      i=sbicxfin(regs,
   1399  1.14   chopps 					 acb->sc_kv.dc_count,
   1400  1.14   chopps 					 acb->sc_kv.dc_addr);
   1401  1.14   chopps 			    else
   1402  1.14   chopps 			      i=sbicxfout(regs,
   1403  1.14   chopps 					  acb->sc_kv.dc_count,
   1404  1.14   chopps 					  acb->sc_kv.dc_addr,
   1405  1.14   chopps 					     SBIC_PHASE(csr));
   1406  1.14   chopps 			  acb->sc_kv.dc_addr +=
   1407  1.14   chopps 				  (acb->sc_kv.dc_count - i);
   1408  1.14   chopps 			  acb->sc_kv.dc_count = i;
   1409  1.14   chopps 			  i = 1;
   1410  1.14   chopps 			}
   1411  1.14   chopps 			break;
   1412  1.14   chopps 
   1413   1.1   chopps #endif
   1414  1.14   chopps 		case SBIC_CSR_XFERRED|STATUS_PHASE:
   1415  1.14   chopps 		case SBIC_CSR_MIS|STATUS_PHASE:
   1416  1.14   chopps 		case SBIC_CSR_MIS_1|STATUS_PHASE:
   1417  1.14   chopps 		case SBIC_CSR_MIS_2|STATUS_PHASE:
   1418   1.1   chopps 			/*
   1419  1.14   chopps 			 * the sbic does the status/cmd-complete reading ok,
   1420  1.14   chopps 			 * so do this with its hi-level commands.
   1421   1.1   chopps 			 */
   1422  1.14   chopps #ifdef DEBUG
   1423  1.14   chopps 			if(sbic_debug)
   1424  1.14   chopps 				printf("SBICICMD status phase\n");
   1425  1.14   chopps #endif
   1426  1.14   chopps 			SBIC_TC_PUT(regs, 0);
   1427  1.14   chopps 			SET_SBIC_cmd_phase(regs, 0x46);
   1428  1.14   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1429  1.14   chopps 			break;
   1430   1.1   chopps 
   1431  1.14   chopps #if THIS_IS_A_RESERVED_STATE
   1432  1.14   chopps 		case BUS_FREE_PHASE:		/* This is not legal */
   1433  1.14   chopps 			if( dev->sc_stat[0] != 0xff )
   1434  1.14   chopps 				goto out;
   1435  1.14   chopps 			break;
   1436   1.1   chopps #endif
   1437   1.1   chopps 
   1438  1.14   chopps 		default:
   1439  1.14   chopps 			i = sbicnextstate(dev, csr, asr);
   1440  1.14   chopps 		}
   1441  1.14   chopps 
   1442  1.14   chopps 		/*
   1443  1.14   chopps 		 * make sure the last command was taken,
   1444  1.14   chopps 		 * ie. we're not hunting after an ignored command..
   1445  1.14   chopps 		 */
   1446  1.14   chopps 		GET_SBIC_asr(regs, asr);
   1447  1.14   chopps 
   1448  1.14   chopps 		/* tapes may take a loooong time.. */
   1449  1.14   chopps 		while (asr & SBIC_ASR_BSY){
   1450  1.14   chopps 			if(asr & SBIC_ASR_DBR) {
   1451  1.14   chopps 				printf("sbicicmd: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
   1452  1.14   chopps 				       csr,asr);
   1453  1.14   chopps #ifdef DDB
   1454  1.14   chopps 				Debugger();
   1455  1.14   chopps #endif
   1456  1.14   chopps 				/* SBIC is jammed */
   1457  1.14   chopps 				/* DUNNO which direction */
   1458  1.14   chopps 				/* Try old direction */
   1459  1.14   chopps 				GET_SBIC_data(regs,i);
   1460  1.14   chopps 				GET_SBIC_asr(regs, asr);
   1461  1.14   chopps 				if( asr & SBIC_ASR_DBR) /* Wants us to write */
   1462  1.14   chopps 					SET_SBIC_data(regs,i);
   1463   1.1   chopps 			}
   1464  1.14   chopps 			GET_SBIC_asr(regs, asr);
   1465   1.1   chopps 		}
   1466   1.1   chopps 
   1467   1.1   chopps 		/*
   1468  1.14   chopps 		 * wait for last command to complete
   1469   1.1   chopps 		 */
   1470  1.14   chopps 		if (asr & SBIC_ASR_LCI) {
   1471  1.14   chopps 			printf("sbicicmd: last command ignored\n");
   1472  1.14   chopps 		}
   1473  1.14   chopps 		else if( i == 1 ) /* Bsy */
   1474  1.14   chopps 			SBIC_WAIT (regs, SBIC_ASR_INT, wait);
   1475  1.14   chopps 
   1476  1.13  mycroft 		/*
   1477  1.14   chopps 		 * do it again
   1478   1.1   chopps 		 */
   1479  1.14   chopps 	} while ( i > 0 && dev->sc_stat[0] == 0xff);
   1480   1.1   chopps 
   1481  1.14   chopps 	/* Sometimes we need to do an extra read of the CSR */
   1482  1.14   chopps 	GET_SBIC_csr(regs, csr);
   1483   1.1   chopps 
   1484  1.14   chopps #if CSR_LOG_BUF_SIZE
   1485  1.14   chopps 	if(reselect_debug>1)
   1486  1.14   chopps 		for(i=0; i<bufptr; i++)
   1487  1.14   chopps 			printf("CSR:%02x", csrbuf[i]);
   1488  1.14   chopps #endif
   1489   1.1   chopps 
   1490  1.14   chopps #ifdef DEBUG
   1491  1.14   chopps 	if(data_pointer_debug > 1)
   1492  1.14   chopps 		printf("sbicicmd done(%d,%d):%d =%d=\n",
   1493  1.14   chopps 		       dev->target, lun,
   1494  1.14   chopps 		       acb->sc_kv.dc_count,
   1495  1.14   chopps 		       dev->sc_stat[0]);
   1496  1.14   chopps #endif
   1497   1.1   chopps 
   1498   1.1   chopps 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
   1499  1.14   chopps 	dev->sc_flags &= ~SBICF_ICMD;
   1500  1.14   chopps 
   1501   1.1   chopps 	return(dev->sc_stat[0]);
   1502   1.1   chopps }
   1503   1.1   chopps 
   1504   1.1   chopps /*
   1505   1.1   chopps  * Finish SCSI xfer command:  After the completion interrupt from
   1506   1.1   chopps  * a read/write operation, sequence through the final phases in
   1507   1.1   chopps  * programmed i/o.  This routine is a lot like sbicicmd except we
   1508   1.1   chopps  * skip (and don't allow) the select, cmd out and data in/out phases.
   1509   1.1   chopps  */
   1510   1.1   chopps void
   1511   1.1   chopps sbicxfdone(dev, regs, target)
   1512   1.1   chopps 	struct sbic_softc *dev;
   1513   1.1   chopps 	sbic_regmap_p regs;
   1514   1.1   chopps 	int target;
   1515   1.1   chopps {
   1516   1.1   chopps 	u_char phase, csr;
   1517   1.1   chopps 	int s;
   1518   1.1   chopps 
   1519   1.1   chopps 	QPRINTF(("{"));
   1520   1.1   chopps 	s = splbio();
   1521   1.1   chopps 
   1522   1.1   chopps 	/*
   1523   1.1   chopps 	 * have the sbic complete on its own
   1524   1.1   chopps 	 */
   1525   1.1   chopps 	SBIC_TC_PUT(regs, 0);
   1526   1.1   chopps 	SET_SBIC_cmd_phase(regs, 0x46);
   1527   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1528   1.1   chopps 
   1529   1.1   chopps 	do {
   1530   1.1   chopps 		SBIC_WAIT (regs, SBIC_ASR_INT, 0);
   1531   1.1   chopps 		GET_SBIC_csr (regs, csr);
   1532   1.1   chopps 		QPRINTF(("%02x:", csr));
   1533   1.1   chopps 	} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
   1534   1.1   chopps 	    && (csr != SBIC_CSR_S_XFERRED));
   1535   1.1   chopps 
   1536   1.1   chopps 	dev->sc_flags &= ~SBICF_SELECTED;
   1537   1.1   chopps 
   1538   1.1   chopps 	GET_SBIC_cmd_phase (regs, phase);
   1539   1.1   chopps 	QPRINTF(("}%02x", phase));
   1540   1.1   chopps 	if (phase == 0x60)
   1541   1.1   chopps 		GET_SBIC_tlun(regs, dev->sc_stat[0]);
   1542   1.1   chopps 	else
   1543   1.1   chopps 		sbicerror(dev, regs, csr);
   1544   1.1   chopps 
   1545   1.1   chopps 	QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
   1546   1.1   chopps 	splx(s);
   1547   1.1   chopps }
   1548   1.1   chopps 
   1549  1.14   chopps 	/*
   1550  1.14   chopps 	 * No DMA chains
   1551  1.14   chopps 	 */
   1552  1.14   chopps 
   1553   1.1   chopps int
   1554   1.1   chopps sbicgo(dev, xs)
   1555   1.1   chopps 	struct sbic_softc *dev;
   1556   1.1   chopps 	struct scsi_xfer *xs;
   1557   1.1   chopps {
   1558  1.14   chopps 	int i, dmaflags, count, wait, usedma;
   1559  1.14   chopps 	u_char csr, asr, cmd, *addr;
   1560   1.1   chopps 	sbic_regmap_p regs;
   1561  1.14   chopps 	struct sbic_acb *acb;
   1562   1.1   chopps 
   1563  1.14   chopps 	dev->target = xs->sc_link->target;
   1564  1.14   chopps 	dev->lun = xs->sc_link->lun;
   1565  1.14   chopps 	acb = dev->sc_nexus;
   1566   1.1   chopps 	regs = dev->sc_sbicp;
   1567  1.14   chopps 
   1568  1.14   chopps 	usedma = sbicdmaok(dev, xs);
   1569  1.14   chopps #ifdef DEBUG
   1570  1.14   chopps 	routine = 1;
   1571  1.14   chopps 	debug_sbic_regs = regs; /* store this to allow debug calls */
   1572  1.14   chopps 	if( data_pointer_debug > 1 )
   1573  1.14   chopps 		printf("sbicgo(%d,%d)\n", dev->target, dev->lun);
   1574  1.14   chopps #endif
   1575   1.1   chopps 
   1576   1.1   chopps 	/*
   1577   1.1   chopps 	 * set the sbic into DMA mode
   1578   1.1   chopps 	 */
   1579  1.14   chopps 	if( usedma )
   1580  1.14   chopps 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
   1581  1.14   chopps 				 SBIC_MACHINE_DMA_MODE);
   1582  1.14   chopps 	else
   1583  1.14   chopps 		SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
   1584  1.14   chopps 
   1585   1.1   chopps 
   1586   1.1   chopps 	/*
   1587   1.1   chopps 	 * select the SCSI bus (it's an error if bus isn't free)
   1588   1.1   chopps 	 */
   1589  1.14   chopps 	if (sbicselectbus(dev, regs, dev->target, dev->lun,
   1590   1.7   chopps 	    dev->sc_scsiaddr)) {
   1591  1.14   chopps /*		printf("sbicgo: Trying to select busy bus!\n"); */
   1592  1.14   chopps 		return(0); /* Not done: needs to be rescheduled */
   1593   1.1   chopps 	}
   1594  1.14   chopps 	dev->sc_stat[0] = 0xff;
   1595   1.1   chopps 
   1596   1.1   chopps 	/*
   1597  1.14   chopps 	 * Calculate DMA chains now
   1598   1.1   chopps 	 */
   1599   1.1   chopps 
   1600  1.14   chopps 	dmaflags = 0;
   1601  1.14   chopps 	if (acb->flags & ACB_DATAIN)
   1602  1.14   chopps 		dmaflags |= DMAGO_READ;
   1603   1.1   chopps 
   1604   1.1   chopps 
   1605   1.1   chopps 	/*
   1606  1.14   chopps 	 * Deal w/bounce buffers.
   1607   1.1   chopps 	 */
   1608   1.1   chopps 
   1609  1.14   chopps 	addr = acb->sc_kv.dc_addr;
   1610  1.14   chopps 	count = acb->sc_kv.dc_count;
   1611  1.14   chopps 	if (count && (char *)kvtop(addr) != acb->sc_pa.dc_addr)	{ /* XXXX check */
   1612  1.14   chopps 		printf("sbic: DMA buffer mapping changed %x->%x\n",
   1613  1.14   chopps 		    acb->sc_pa.dc_addr, kvtop(addr));
   1614  1.14   chopps #ifdef DDB
   1615  1.14   chopps 		Debugger();
   1616  1.14   chopps #endif
   1617   1.1   chopps 	}
   1618   1.1   chopps 
   1619  1.10   chopps #ifdef DEBUG
   1620  1.10   chopps 	++sbicdma_ops;			/* count total DMA operations */
   1621  1.10   chopps #endif
   1622  1.14   chopps 	if (count && usedma && dev->sc_flags & SBICF_BADDMA &&
   1623   1.1   chopps 	    sbiccheckdmap(addr, count, dev->sc_dmamask)) {
   1624   1.1   chopps 		/*
   1625   1.1   chopps 		 * need to bounce the dma.
   1626   1.1   chopps 		 */
   1627   1.1   chopps 		if (dmaflags & DMAGO_READ) {
   1628  1.14   chopps 			acb->flags |= ACB_BBUF;
   1629  1.14   chopps 			acb->sc_dmausrbuf = addr;
   1630  1.14   chopps 			acb->sc_dmausrlen = count;
   1631  1.14   chopps 			acb->sc_usrbufpa = (u_char *)kvtop(addr);
   1632  1.14   chopps 			if(!dev->sc_tinfo[dev->target].bounce) {
   1633  1.14   chopps 				printf("sbicgo: HELP! no bounce allocated for %d\n",
   1634  1.14   chopps 				       dev->target);
   1635  1.14   chopps 				printf("xfer: (%x->%x,%x)\n", acb->sc_dmausrbuf,
   1636  1.14   chopps 				       acb->sc_usrbufpa, acb->sc_dmausrlen);
   1637  1.14   chopps 				dev->sc_tinfo[xs->sc_link->target].bounce
   1638  1.14   chopps 					= (char *)alloc_z2mem(MAXPHYS);
   1639  1.14   chopps 				if (isztwomem(dev->sc_tinfo[xs->sc_link->target].bounce))
   1640  1.14   chopps 					printf("alloc ZII target %d bounce pa 0x%x\n",
   1641  1.14   chopps 					       xs->sc_link->target,
   1642  1.14   chopps 					       kvtop(dev->sc_tinfo[xs->sc_link->target].bounce));
   1643  1.14   chopps 				else if (dev->sc_tinfo[xs->sc_link->target].bounce)
   1644  1.14   chopps 					printf("alloc CHIP target %d bounce pa 0x%x\n",
   1645  1.14   chopps 					       xs->sc_link->target,
   1646  1.14   chopps 					       PREP_DMA_MEM(dev->sc_tinfo[xs->sc_link->target].bounce));
   1647  1.14   chopps 
   1648  1.14   chopps 				printf("Allocating %d bounce at %x\n",
   1649  1.14   chopps 				       dev->target,
   1650  1.14   chopps 				       kvtop(dev->sc_tinfo[dev->target].bounce));
   1651  1.14   chopps 			}
   1652   1.1   chopps 		} else {	/* write: copy to dma buffer */
   1653  1.14   chopps #ifdef DEBUG
   1654  1.14   chopps 			if(data_pointer_debug)
   1655  1.14   chopps 			printf("sbicgo: copying %x bytes to target %d bounce %x\n",
   1656  1.14   chopps 			       count, dev->target,
   1657  1.14   chopps 			       kvtop(dev->sc_tinfo[dev->target].bounce));
   1658  1.14   chopps #endif
   1659  1.14   chopps 			bcopy (addr, dev->sc_tinfo[dev->target].bounce, count);
   1660   1.1   chopps 		}
   1661  1.14   chopps 		addr = dev->sc_tinfo[dev->target].bounce;/* and use dma buffer */
   1662  1.14   chopps 		acb->sc_kv.dc_addr = addr;
   1663  1.10   chopps #ifdef DEBUG
   1664  1.10   chopps 		++sbicdma_bounces;		/* count number of bounced */
   1665  1.10   chopps #endif
   1666   1.1   chopps 	}
   1667   1.1   chopps 
   1668  1.14   chopps 	/*
   1669  1.14   chopps 	 * Allocate the DMA chain
   1670  1.14   chopps 	 */
   1671  1.14   chopps 
   1672  1.14   chopps 	/* Set start KVM addresses */
   1673  1.14   chopps #if 0
   1674  1.14   chopps 	acb->sc_kv.dc_addr = addr;
   1675  1.14   chopps 	acb->sc_kv.dc_count = count;
   1676  1.10   chopps #endif
   1677   1.1   chopps 
   1678  1.14   chopps 	/* Mark end of segment */
   1679  1.14   chopps 	acb->sc_tcnt = dev->sc_tcnt = 0;
   1680  1.14   chopps 	acb->sc_pa.dc_count = 0;
   1681  1.14   chopps 
   1682  1.14   chopps 	sbic_load_ptrs(dev, regs, dev->target, dev->lun);
   1683  1.14   chopps 	/* Enable interrupts but don't do any DMA */
   1684  1.14   chopps 	dev->sc_tcnt = dev->sc_dmago(dev, acb->sc_pa.dc_addr,
   1685  1.14   chopps 				     acb->sc_pa.dc_count,
   1686  1.14   chopps 				     dmaflags);
   1687  1.14   chopps 	dev->sc_flags |= SBICF_INDMA;
   1688  1.14   chopps         if( !usedma )
   1689  1.14   chopps 		dev->sc_dmacmd = 0; /* Don't use DMA */
   1690  1.14   chopps /*	SBIC_TC_PUT(regs, dev->sc_tcnt); /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
   1691  1.14   chopps 	sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   1692  1.14   chopps 
   1693  1.14   chopps 	/*
   1694  1.14   chopps 	 * push the data cache ( I think this won't work (EH))
   1695  1.14   chopps 	 */
   1696  1.14   chopps #if defined(M68040)
   1697  1.14   chopps 	if (mmutype == MMU_68040 && usedma && count) {
   1698  1.14   chopps 		dma_cachectl(addr, count);
   1699  1.14   chopps 		if (((u_int)addr & 0xF) || (((u_int)addr + count) & 0xF))
   1700  1.14   chopps 			dev->sc_flags |= SBICF_DCFLUSH;
   1701   1.1   chopps 	}
   1702   1.1   chopps #endif
   1703   1.1   chopps 
   1704   1.1   chopps 	/*
   1705  1.14   chopps 	 * dmago() also enables interrupts for the sbic
   1706   1.1   chopps 	 */
   1707  1.14   chopps #ifdef DEBUG
   1708  1.14   chopps 	if( data_pointer_debug > 1 )
   1709  1.14   chopps 		printf("sbicgo dmago:%d(%x:%x)\n",
   1710  1.14   chopps 		       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   1711  1.14   chopps 	if( sbic_timeout && !timeout_active ) {
   1712  1.14   chopps 		timeout((void *)sbictimeout, (void*)dev, sbic_timeout * hz);
   1713  1.14   chopps 		timeout_active = 1;
   1714   1.5   chopps 	}
   1715  1.14   chopps 	debug_asr = asr;
   1716  1.14   chopps 	debug_csr = csr;
   1717   1.3   chopps #endif
   1718   1.1   chopps 
   1719   1.1   chopps 	/*
   1720  1.14   chopps 	 * Lets cycle a while then let the interrupt handler take over
   1721   1.1   chopps 	 */
   1722   1.1   chopps 
   1723  1.14   chopps 	GET_SBIC_asr(regs, asr);
   1724  1.14   chopps 	do {
   1725  1.14   chopps 		GET_SBIC_csr(regs, csr);
   1726  1.14   chopps #ifdef DEBUG
   1727  1.14   chopps 		debug_csr = csr;
   1728  1.14   chopps 		routine = 1;
   1729  1.14   chopps #endif
   1730  1.14   chopps 		QPRINTF(("go[0x%x]", csr));
   1731   1.1   chopps 
   1732  1.14   chopps 		i = sbicnextstate(dev, csr, asr);
   1733   1.1   chopps 
   1734  1.14   chopps 		WAIT_CIP(regs);
   1735  1.14   chopps 		GET_SBIC_asr(regs, asr);
   1736  1.14   chopps #ifdef DEBUG
   1737  1.14   chopps 		debug_asr = asr;
   1738  1.14   chopps #endif
   1739  1.14   chopps 		if(asr & SBIC_ASR_LCI) printf("sbicgo: LCI asr:%02x csr:%02x\n",
   1740  1.14   chopps 					      asr,csr);
   1741  1.14   chopps 	} while( i == SBIC_STATE_RUNNING
   1742  1.14   chopps 		&& asr & (SBIC_ASR_INT|SBIC_ASR_LCI) );
   1743  1.14   chopps 
   1744  1.15   chopps 	if (i == SBIC_STATE_DONE && dev->sc_stat[0] != 0xff) {
   1745  1.14   chopps 		/* Did we really finish that fast? */
   1746  1.14   chopps 		return 1;
   1747  1.14   chopps 	}
   1748  1.14   chopps 	return 0;
   1749   1.1   chopps }
   1750   1.1   chopps 
   1751   1.1   chopps 
   1752   1.1   chopps int
   1753   1.1   chopps sbicintr(dev)
   1754   1.1   chopps 	struct sbic_softc *dev;
   1755   1.1   chopps {
   1756   1.1   chopps 	sbic_regmap_p regs;
   1757   1.3   chopps 	struct dma_chain *df, *dl;
   1758  1.14   chopps 	u_char asr, csr, *tmpaddr;
   1759  1.14   chopps 	struct sbic_acb *acb;
   1760  1.14   chopps 	int i, newtarget, newlun;
   1761  1.14   chopps 	unsigned tcnt;
   1762   1.1   chopps 
   1763   1.1   chopps 	regs = dev->sc_sbicp;
   1764   1.1   chopps 
   1765   1.1   chopps 	/*
   1766   1.1   chopps 	 * pending interrupt?
   1767   1.1   chopps 	 */
   1768   1.1   chopps 	GET_SBIC_asr (regs, asr);
   1769   1.1   chopps 	if ((asr & SBIC_ASR_INT) == 0)
   1770   1.1   chopps 		return(0);
   1771   1.1   chopps 
   1772  1.14   chopps 	do {
   1773  1.14   chopps 		GET_SBIC_csr(regs, csr);
   1774  1.14   chopps #ifdef DEBUG
   1775  1.14   chopps 		debug_csr = csr;
   1776  1.14   chopps 		routine = 2;
   1777  1.14   chopps #endif
   1778  1.14   chopps 		QPRINTF(("intr[0x%x]", csr));
   1779  1.14   chopps 
   1780  1.14   chopps 		i = sbicnextstate(dev, csr, asr);
   1781  1.14   chopps 
   1782  1.14   chopps 		WAIT_CIP(regs);
   1783  1.14   chopps 		GET_SBIC_asr(regs, asr);
   1784  1.14   chopps #ifdef DEBUG
   1785  1.14   chopps 		debug_asr = asr;
   1786  1.14   chopps #endif
   1787  1.14   chopps #if 0
   1788  1.14   chopps 		if(asr & SBIC_ASR_LCI) printf("sbicintr: LCI asr:%02x csr:%02x\n",
   1789  1.14   chopps 					      asr,csr);
   1790  1.14   chopps #endif
   1791  1.14   chopps 	} while(i == SBIC_STATE_RUNNING &&
   1792  1.14   chopps 		asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
   1793  1.14   chopps 	return(1);
   1794  1.14   chopps }
   1795  1.14   chopps 
   1796  1.14   chopps /*
   1797  1.14   chopps  * Run commands and wait for disconnect
   1798  1.14   chopps  */
   1799  1.14   chopps int
   1800  1.14   chopps sbicpoll(dev)
   1801  1.14   chopps 	struct sbic_softc *dev;
   1802  1.14   chopps {
   1803  1.14   chopps 	sbic_regmap_p regs;
   1804  1.14   chopps 	u_char asr, csr;
   1805  1.14   chopps 	struct sbic_pending* pendp;
   1806  1.14   chopps 	int i;
   1807  1.14   chopps 	unsigned tcnt;
   1808  1.14   chopps 
   1809  1.14   chopps 	regs = dev->sc_sbicp;
   1810  1.14   chopps 
   1811  1.14   chopps 	do {
   1812  1.14   chopps 		GET_SBIC_asr (regs, asr);
   1813  1.14   chopps #ifdef DEBUG
   1814  1.14   chopps 		debug_asr = asr;
   1815  1.14   chopps #endif
   1816  1.14   chopps 		GET_SBIC_csr(regs, csr);
   1817  1.14   chopps #ifdef DEBUG
   1818  1.14   chopps 		debug_csr = csr;
   1819  1.14   chopps 		routine = 2;
   1820  1.14   chopps #endif
   1821  1.14   chopps 		QPRINTF(("poll[0x%x]", csr));
   1822  1.14   chopps 
   1823  1.14   chopps 		i = sbicnextstate(dev, csr, asr);
   1824  1.14   chopps 
   1825  1.14   chopps 		WAIT_CIP(regs);
   1826  1.14   chopps 		GET_SBIC_asr(regs, asr);
   1827  1.14   chopps 		/* tapes may take a loooong time.. */
   1828  1.14   chopps 		while (asr & SBIC_ASR_BSY){
   1829  1.14   chopps 			if(asr & SBIC_ASR_DBR) {
   1830  1.14   chopps 				printf("sbipoll: Waiting while sbic is jammed, CSR:%02x,ASR:%02x\n",
   1831  1.14   chopps 				       csr,asr);
   1832  1.14   chopps #ifdef DDB
   1833  1.14   chopps 				Debugger();
   1834  1.14   chopps #endif
   1835  1.14   chopps 				/* SBIC is jammed */
   1836  1.14   chopps 				/* DUNNO which direction */
   1837  1.14   chopps 				/* Try old direction */
   1838  1.14   chopps 				GET_SBIC_data(regs,i);
   1839  1.14   chopps 				GET_SBIC_asr(regs, asr);
   1840  1.14   chopps 				if( asr & SBIC_ASR_DBR) /* Wants us to write */
   1841  1.14   chopps 					SET_SBIC_data(regs,i);
   1842  1.14   chopps 			}
   1843  1.14   chopps 			GET_SBIC_asr(regs, asr);
   1844  1.14   chopps 		}
   1845  1.14   chopps 
   1846  1.14   chopps 		if(asr & SBIC_ASR_LCI) printf("sbicpoll: LCI asr:%02x csr:%02x\n",
   1847  1.14   chopps 					      asr,csr);
   1848  1.14   chopps 		else if( i == 1 ) /* BSY */
   1849  1.14   chopps 			SBIC_WAIT(regs, SBIC_ASR_INT, sbic_cmd_wait);
   1850  1.14   chopps 	} while(i == SBIC_STATE_RUNNING);
   1851  1.14   chopps 	return(1);
   1852  1.14   chopps }
   1853  1.14   chopps 
   1854  1.14   chopps /*
   1855  1.14   chopps  * Handle a single msgin
   1856  1.14   chopps  */
   1857  1.14   chopps 
   1858  1.14   chopps int
   1859  1.14   chopps sbicmsgin(dev)
   1860  1.14   chopps 	struct sbic_softc *dev;
   1861  1.14   chopps {
   1862  1.14   chopps 	sbic_regmap_p regs;
   1863  1.14   chopps 	int recvlen;
   1864  1.14   chopps 	u_char asr, csr, *tmpaddr;
   1865  1.14   chopps 
   1866  1.14   chopps 	regs = dev->sc_sbicp;
   1867  1.14   chopps 
   1868  1.14   chopps 	dev->sc_msg[0] = 0xff;
   1869  1.14   chopps 	dev->sc_msg[1] = 0xff;
   1870  1.14   chopps 
   1871  1.14   chopps 	GET_SBIC_asr(regs, asr);
   1872  1.14   chopps #ifdef DEBUG
   1873  1.14   chopps 	if(reselect_debug>1)
   1874  1.14   chopps 		printf("sbicmsgin asr=%02x\n", asr);
   1875  1.14   chopps #endif
   1876  1.14   chopps 
   1877  1.14   chopps 	sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   1878  1.14   chopps 
   1879  1.14   chopps 	GET_SBIC_selid (regs, csr);
   1880  1.14   chopps 	SET_SBIC_selid (regs, csr | SBIC_SID_FROM_SCSI);
   1881  1.14   chopps 
   1882  1.14   chopps 	SBIC_TC_PUT(regs, 0);
   1883  1.14   chopps 	tmpaddr = dev->sc_msg;
   1884  1.14   chopps 	recvlen = 1;
   1885  1.14   chopps 	do {
   1886  1.14   chopps 		while( recvlen-- ) {
   1887  1.14   chopps 			GET_SBIC_asr(regs, asr);
   1888  1.14   chopps 			GET_SBIC_csr(regs, csr);
   1889  1.14   chopps 			QPRINTF(("sbicmsgin ready to go (csr,asr)=(%02x,%02x)\n",
   1890  1.14   chopps 				 csr, asr));
   1891  1.14   chopps 
   1892  1.14   chopps 			RECV_BYTE(regs, *tmpaddr);
   1893  1.14   chopps #if 1
   1894  1.14   chopps 			/*
   1895  1.14   chopps 			 * get the command completion interrupt, or we
   1896  1.14   chopps 			 * can't send a new command (LCI)
   1897  1.14   chopps 			 */
   1898  1.14   chopps 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   1899  1.14   chopps 			GET_SBIC_csr(regs, csr);
   1900  1.14   chopps #else
   1901  1.14   chopps 			WAIT_CIP(regs);
   1902  1.14   chopps 			do {
   1903  1.14   chopps 				GET_SBIC_asr(regs, asr);
   1904  1.14   chopps 				csr = 0xff;
   1905  1.14   chopps 				GET_SBIC_csr(regs, csr);
   1906  1.14   chopps 				if( csr == 0xff )
   1907  1.14   chopps 					printf("sbicmsgin waiting: csr %02x asr %02x\n", csr, asr);
   1908  1.14   chopps 			} while( csr == 0xff );
   1909  1.14   chopps #endif
   1910  1.14   chopps #ifdef DEBUG
   1911  1.14   chopps 			if(reselect_debug>1)
   1912  1.14   chopps 				printf("sbicmsgin: got %02x csr %02x asr %02x\n",
   1913  1.14   chopps 				       *tmpaddr, csr, asr);
   1914  1.14   chopps #endif
   1915  1.14   chopps #if do_parity_check
   1916  1.14   chopps 			if( asr & SBIC_ASR_PE ) {
   1917  1.14   chopps 				printf ("Parity error");
   1918  1.14   chopps 				/* This code simply does not work. */
   1919  1.14   chopps 				WAIT_CIP(regs);
   1920  1.14   chopps 				SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   1921  1.14   chopps 				WAIT_CIP(regs);
   1922  1.14   chopps 				GET_SBIC_asr(regs, asr);
   1923  1.14   chopps 				WAIT_CIP(regs);
   1924  1.14   chopps 				SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   1925  1.14   chopps 				WAIT_CIP(regs);
   1926  1.14   chopps 				if( !(asr & SBIC_ASR_LCI) )
   1927  1.14   chopps 					/* Target wants to send garbled msg*/
   1928  1.14   chopps 					continue;
   1929  1.14   chopps 				printf("--fixing\n");
   1930  1.14   chopps 				/* loop until a msgout phase occurs on target */
   1931  1.14   chopps 				while(csr & 0x07 != MESG_OUT_PHASE) {
   1932  1.14   chopps 					while( asr & SBIC_ASR_BSY &&
   1933  1.14   chopps 					      !(asr & SBIC_ASR_DBR|SBIC_ASR_INT) )
   1934  1.14   chopps 						GET_SBIC_asr(regs, asr);
   1935  1.14   chopps 					if( asr & SBIC_ASR_DBR )
   1936  1.14   chopps 						panic("msgin: jammed again!\n");
   1937  1.14   chopps 					GET_SBIC_csr(regs, csr);
   1938  1.14   chopps 					if( csr & 0x07 != MESG_OUT_PHASE ) {
   1939  1.14   chopps 						sbicnextstate(dev, csr, asr);
   1940  1.14   chopps 						sbic_save_ptrs(dev, regs,
   1941  1.14   chopps 							       dev->target,
   1942  1.14   chopps 							       dev->lun);
   1943  1.14   chopps 					}
   1944  1.14   chopps 				}
   1945  1.14   chopps 				/* Should be msg out by now */
   1946  1.14   chopps 				SEND_BYTE(regs, MSG_PARITY_ERROR);
   1947  1.14   chopps 			}
   1948  1.14   chopps 			else
   1949  1.14   chopps #endif
   1950  1.14   chopps 				tmpaddr++;
   1951  1.14   chopps 
   1952  1.14   chopps 			if(recvlen) {
   1953  1.14   chopps 				/* Clear ACK */
   1954  1.14   chopps 				WAIT_CIP(regs);
   1955  1.14   chopps 				GET_SBIC_asr(regs, asr);
   1956  1.14   chopps 				GET_SBIC_csr(regs, csr);
   1957  1.14   chopps 				QPRINTF(("sbicmsgin pre byte CLR_ACK (csr,asr)=(%02x,%02x)\n",
   1958  1.14   chopps 					 csr, asr));
   1959  1.14   chopps 				SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   1960  1.14   chopps 				SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   1961  1.14   chopps 			}
   1962  1.14   chopps 
   1963  1.14   chopps 		};
   1964  1.14   chopps 
   1965  1.14   chopps 		if(dev->sc_msg[0] == 0xff) {
   1966  1.14   chopps 			printf("sbicmsgin: sbic swallowed our message\n");
   1967  1.14   chopps 			break;
   1968  1.14   chopps 		}
   1969  1.14   chopps #ifdef DEBUG
   1970  1.14   chopps 		if (sync_debug)
   1971  1.14   chopps 			printf("msgin done csr 0x%x asr 0x%x msg 0x%x\n",
   1972  1.14   chopps 			       csr, asr, dev->sc_msg[0]);
   1973  1.14   chopps #endif
   1974  1.14   chopps 		/*
   1975  1.14   chopps 		 * test whether this is a reply to our sync
   1976  1.14   chopps 		 * request
   1977  1.14   chopps 		 */
   1978  1.14   chopps 		if (MSG_ISIDENTIFY(dev->sc_msg[0])) {
   1979  1.14   chopps 			QPRINTF(("IFFY"));
   1980  1.14   chopps #if 0
   1981  1.14   chopps 			/* There is an implied load-ptrs here */
   1982  1.14   chopps 			sbic_load_ptrs(dev, regs, dev->target, dev->lun);
   1983  1.14   chopps #endif
   1984  1.14   chopps 			/* Got IFFY msg -- ack it */
   1985  1.14   chopps 		} else if (dev->sc_msg[0] == MSG_REJECT
   1986  1.14   chopps 			   && dev->sc_sync[dev->target].state == SYNC_SENT) {
   1987  1.14   chopps 			QPRINTF(("REJECT of SYN"));
   1988  1.14   chopps #ifdef DEBUG
   1989  1.14   chopps 			if (sync_debug)
   1990  1.14   chopps 				printf("target %d rejected sync, going async\n",
   1991  1.14   chopps 				       dev->target);
   1992  1.14   chopps #endif
   1993  1.14   chopps 			dev->sc_sync[dev->target].period = sbic_min_period;
   1994  1.14   chopps 			dev->sc_sync[dev->target].offset = 0;
   1995  1.14   chopps 			dev->sc_sync[dev->target].state = SYNC_DONE;
   1996  1.14   chopps 			SET_SBIC_syn(regs,
   1997  1.14   chopps 				     SBIC_SYN(dev->sc_sync[dev->target].offset,
   1998  1.14   chopps 					      dev->sc_sync[dev->target].period));
   1999  1.14   chopps 		} else if ((dev->sc_msg[0] == MSG_REJECT)) {
   2000  1.14   chopps 			QPRINTF(("REJECT"));
   2001  1.14   chopps 			/*
   2002  1.14   chopps 			 * we'll never REJECt a REJECT message..
   2003  1.14   chopps 			 */
   2004  1.14   chopps 		} else if ((dev->sc_msg[0] == MSG_SAVE_DATA_PTR)) {
   2005  1.14   chopps 			QPRINTF(("MSG_SAVE_DATA_PTR"));
   2006  1.14   chopps 			/*
   2007  1.14   chopps 			 * don't reject this either.
   2008  1.14   chopps 			 */
   2009  1.14   chopps 		} else if ((dev->sc_msg[0] == MSG_DISCONNECT)) {
   2010  1.14   chopps 			QPRINTF(("DISCONNECT"));
   2011  1.14   chopps #ifdef DEBUG
   2012  1.14   chopps 			if( reselect_debug>1 && dev->sc_msg[0] == MSG_DISCONNECT )
   2013  1.14   chopps 				printf("sbicmsgin: got disconnect msg %s\n",
   2014  1.14   chopps 				       (dev->sc_flags & SBICF_ICMD)?"rejecting":"");
   2015  1.14   chopps #endif
   2016  1.14   chopps 			if( dev->sc_flags & SBICF_ICMD ) {
   2017  1.14   chopps 				/* We're in immediate mode. Prevent disconnects. */
   2018  1.14   chopps 				/* prepare to reject the message, NACK */
   2019  1.14   chopps 				SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   2020  1.14   chopps 				WAIT_CIP(regs);
   2021  1.14   chopps 			}
   2022  1.14   chopps 		} else if (dev->sc_msg[0] == MSG_CMD_COMPLETE ) {
   2023  1.14   chopps 			QPRINTF(("CMD_COMPLETE"));
   2024  1.14   chopps 			/* !! KLUDGE ALERT !! quite a few drives don't seem to
   2025  1.14   chopps 			 * really like the current way of sending the
   2026  1.14   chopps 			 * sync-handshake together with the ident-message, and
   2027  1.14   chopps 			 * they react by sending command-complete and
   2028  1.14   chopps 			 * disconnecting right after returning the valid sync
   2029  1.14   chopps 			 * handshake. So, all I can do is reselect the drive,
   2030  1.14   chopps 			 * and hope it won't disconnect again. I don't think
   2031  1.14   chopps 			 * this is valid behavior, but I can't help fixing a
   2032  1.14   chopps 			 * problem that apparently exists.
   2033  1.14   chopps 			 *
   2034  1.14   chopps 			 * Note: we should not get here on `normal' command
   2035  1.14   chopps 			 * completion, as that condition is handled by the
   2036  1.14   chopps 			 * high-level sel&xfer resume command used to walk
   2037  1.14   chopps 			 * thru status/cc-phase.
   2038  1.14   chopps 			 */
   2039  1.14   chopps 
   2040  1.14   chopps #ifdef DEBUG
   2041  1.14   chopps 			if (sync_debug)
   2042  1.14   chopps 				printf ("GOT MSG %d! target %d acting weird.."
   2043  1.14   chopps 					" waiting for disconnect...\n",
   2044  1.14   chopps 					dev->sc_msg[0], dev->target);
   2045  1.14   chopps #endif
   2046  1.14   chopps 			/* Check to see if sbic is handling this */
   2047  1.14   chopps 			GET_SBIC_asr(regs, asr);
   2048  1.14   chopps 			if(asr & SBIC_ASR_BSY)
   2049  1.14   chopps 				return SBIC_STATE_RUNNING;
   2050  1.14   chopps 
   2051  1.14   chopps 			/* Let's try this: Assume it works and set status to 00 */
   2052  1.14   chopps 			dev->sc_stat[0] = 0;
   2053  1.14   chopps 		} else if (dev->sc_msg[0] == MSG_EXT_MESSAGE
   2054  1.14   chopps 			   && tmpaddr == &dev->sc_msg[1]) {
   2055  1.14   chopps 			QPRINTF(("ExtMSG\n"));
   2056  1.14   chopps 			/* Read in whole extended message */
   2057  1.14   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2058  1.14   chopps 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2059  1.14   chopps 			GET_SBIC_asr(regs, asr);
   2060  1.14   chopps 			GET_SBIC_csr(regs, csr);
   2061  1.14   chopps 			QPRINTF(("CLR ACK asr %02x, csr %02x\n", asr, csr));
   2062  1.14   chopps 			RECV_BYTE(regs, *tmpaddr);
   2063  1.14   chopps 			/* Wait for command completion IRQ */
   2064  1.14   chopps 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2065  1.14   chopps 			recvlen = *tmpaddr++;
   2066  1.14   chopps 			QPRINTF(("Recving ext msg, asr %02x csr %02x len %02x\n",
   2067  1.14   chopps 			       asr, csr, recvlen));
   2068  1.14   chopps 		} else if (dev->sc_msg[0] == MSG_EXT_MESSAGE && dev->sc_msg[1] == 3
   2069  1.14   chopps 			   && dev->sc_msg[2] == MSG_SYNC_REQ) {
   2070  1.14   chopps 			QPRINTF(("SYN"));
   2071  1.14   chopps 			dev->sc_sync[dev->target].period =
   2072  1.14   chopps 				sbicfromscsiperiod(dev,
   2073  1.14   chopps 						   regs, dev->sc_msg[3]);
   2074  1.14   chopps 			dev->sc_sync[dev->target].offset = dev->sc_msg[4];
   2075  1.14   chopps 			dev->sc_sync[dev->target].state = SYNC_DONE;
   2076  1.14   chopps 			SET_SBIC_syn(regs,
   2077  1.14   chopps 				     SBIC_SYN(dev->sc_sync[dev->target].offset,
   2078  1.14   chopps 					      dev->sc_sync[dev->target].period));
   2079  1.14   chopps 			printf("%s: target %d now synchronous,"
   2080  1.14   chopps 			       " period=%dns, offset=%d.\n",
   2081  1.14   chopps 			       dev->sc_dev.dv_xname, dev->target,
   2082  1.14   chopps 			       dev->sc_msg[3] * 4, dev->sc_msg[4]);
   2083  1.14   chopps 		} else {
   2084  1.14   chopps #ifdef DEBUG
   2085  1.14   chopps 			if (sbic_debug || sync_debug)
   2086  1.14   chopps 				printf ("sbicmsgin: Rejecting message 0x%02x\n",
   2087  1.14   chopps 					dev->sc_msg[0]);
   2088  1.14   chopps #endif
   2089  1.14   chopps 			/* prepare to reject the message, NACK */
   2090  1.14   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   2091  1.14   chopps 			WAIT_CIP(regs);
   2092  1.14   chopps 		}
   2093  1.14   chopps 		/* Clear ACK */
   2094  1.14   chopps 		WAIT_CIP(regs);
   2095  1.14   chopps 		GET_SBIC_asr(regs, asr);
   2096  1.14   chopps 		GET_SBIC_csr(regs, csr);
   2097  1.14   chopps 		QPRINTF(("sbicmsgin pre CLR_ACK (csr,asr)=(%02x,%02x)%d\n",
   2098  1.14   chopps 			 csr, asr, recvlen));
   2099  1.14   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2100  1.14   chopps 		SBIC_WAIT(regs, SBIC_ASR_INT, 0);
   2101  1.14   chopps 	}
   2102  1.14   chopps #if 0
   2103  1.14   chopps 	while((csr == SBIC_CSR_MSGIN_W_ACK)
   2104  1.14   chopps 	      || (SBIC_PHASE(csr) == MESG_IN_PHASE));
   2105  1.14   chopps #else
   2106  1.14   chopps 	while (recvlen>0);
   2107  1.14   chopps #endif
   2108  1.14   chopps 
   2109  1.14   chopps 	QPRINTF(("sbicmsgin finished: csr %02x, asr %02x\n",csr, asr));
   2110  1.14   chopps 
   2111  1.14   chopps 	/* Should still have one CSR to read */
   2112  1.14   chopps 	return SBIC_STATE_RUNNING;
   2113  1.14   chopps }
   2114  1.14   chopps 
   2115  1.14   chopps 
   2116  1.14   chopps /*
   2117  1.14   chopps  * sbicnextstate()
   2118  1.14   chopps  * return:
   2119  1.14   chopps  *		0  == done
   2120  1.14   chopps  *		1  == working
   2121  1.14   chopps  *		2  == disconnected
   2122  1.14   chopps  *		-1 == error
   2123  1.14   chopps  */
   2124  1.14   chopps int
   2125  1.14   chopps sbicnextstate(dev, csr, asr)
   2126  1.14   chopps 	struct sbic_softc *dev;
   2127  1.14   chopps 	u_char csr, asr;
   2128  1.14   chopps {
   2129  1.14   chopps 	sbic_regmap_p regs;
   2130  1.14   chopps 	struct dma_chain *df, *dl;
   2131  1.14   chopps 	struct sbic_acb *acb;
   2132  1.14   chopps 	int i, newtarget, newlun, wait;
   2133  1.14   chopps 	unsigned tcnt;
   2134  1.14   chopps 
   2135  1.14   chopps 	regs = dev->sc_sbicp;
   2136  1.14   chopps 	acb = dev->sc_nexus;
   2137  1.14   chopps 
   2138  1.14   chopps 	QPRINTF(("next[%02x,%02x]",asr,csr));
   2139  1.14   chopps 
   2140  1.14   chopps 	switch (csr) {
   2141  1.14   chopps 	case SBIC_CSR_XFERRED|CMD_PHASE:
   2142  1.14   chopps 	case SBIC_CSR_MIS|CMD_PHASE:
   2143  1.14   chopps 	case SBIC_CSR_MIS_1|CMD_PHASE:
   2144  1.14   chopps 	case SBIC_CSR_MIS_2|CMD_PHASE:
   2145  1.14   chopps 		sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   2146  1.14   chopps 		if (sbicxfstart(regs, acb->clen, CMD_PHASE, sbic_cmd_wait))
   2147  1.14   chopps 			if (sbicxfout(regs, acb->clen,
   2148  1.14   chopps 				      &acb->cmd, CMD_PHASE))
   2149  1.14   chopps 				goto abort;
   2150  1.14   chopps 		break;
   2151   1.1   chopps 
   2152  1.14   chopps 	case SBIC_CSR_XFERRED|STATUS_PHASE:
   2153  1.14   chopps 	case SBIC_CSR_MIS|STATUS_PHASE:
   2154  1.14   chopps 	case SBIC_CSR_MIS_1|STATUS_PHASE:
   2155  1.14   chopps 	case SBIC_CSR_MIS_2|STATUS_PHASE:
   2156   1.1   chopps 		/*
   2157   1.1   chopps 		 * this should be the normal i/o completion case.
   2158   1.1   chopps 		 * get the status & cmd complete msg then let the
   2159   1.1   chopps 		 * device driver look at what happened.
   2160   1.1   chopps 		 */
   2161  1.14   chopps 		sbicxfdone(dev,regs,dev->target);
   2162   1.3   chopps 		/*
   2163   1.3   chopps 		 * check for overlapping cache line, flush if so
   2164   1.3   chopps 		 */
   2165   1.4   chopps #ifdef M68040
   2166   1.3   chopps 		if (dev->sc_flags & SBICF_DCFLUSH) {
   2167  1.14   chopps #if 0
   2168  1.14   chopps 			printf("sbic: 68040 DMA cache flush needs fixing? %x:%x\n",
   2169  1.14   chopps 			    dev->sc_xs->data, dev->sc_xs->datalen);
   2170  1.14   chopps #endif
   2171   1.3   chopps 		}
   2172   1.4   chopps #endif
   2173  1.14   chopps #ifdef DEBUG
   2174  1.14   chopps 		if( data_pointer_debug > 1 )
   2175  1.14   chopps 			printf("next dmastop: %d(%x:%x)\n",
   2176  1.14   chopps 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   2177  1.14   chopps #endif
   2178  1.14   chopps 		dev->sc_dmastop(dev); /* was dmafree */
   2179  1.14   chopps 		if (acb->flags & ACB_BBUF) {
   2180  1.14   chopps 			if ((u_char *)kvtop(acb->sc_dmausrbuf) != acb->sc_usrbufpa)
   2181  1.14   chopps 				printf("%s: WARNING - buffer mapping changed %x->%x\n",
   2182  1.14   chopps 				    dev->sc_dev.dv_xname, acb->sc_usrbufpa,
   2183  1.14   chopps 				    kvtop(acb->sc_dmausrbuf));
   2184  1.14   chopps #ifdef DEBUG
   2185  1.14   chopps 			if(data_pointer_debug)
   2186  1.14   chopps 			printf("sbicgo:copying %x bytes from target %d bounce %x\n",
   2187  1.14   chopps 			       acb->sc_dmausrlen,
   2188  1.14   chopps 			       dev->target,
   2189  1.14   chopps 			       kvtop(dev->sc_tinfo[dev->target].bounce));
   2190  1.14   chopps #endif
   2191  1.14   chopps 			bcopy(dev->sc_tinfo[dev->target].bounce,
   2192  1.14   chopps 			      acb->sc_dmausrbuf,
   2193  1.14   chopps 			      acb->sc_dmausrlen);
   2194  1.14   chopps 		}
   2195  1.14   chopps 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_DCFLUSH);
   2196  1.14   chopps 		sbic_scsidone(acb, dev->sc_stat[0]);
   2197  1.14   chopps 		return SBIC_STATE_DONE;
   2198  1.14   chopps 
   2199  1.14   chopps 	case SBIC_CSR_XFERRED|DATA_OUT_PHASE:
   2200  1.14   chopps 	case SBIC_CSR_XFERRED|DATA_IN_PHASE:
   2201  1.14   chopps 	case SBIC_CSR_MIS|DATA_OUT_PHASE:
   2202  1.14   chopps 	case SBIC_CSR_MIS|DATA_IN_PHASE:
   2203  1.14   chopps 	case SBIC_CSR_MIS_1|DATA_OUT_PHASE:
   2204  1.14   chopps 	case SBIC_CSR_MIS_1|DATA_IN_PHASE:
   2205  1.14   chopps 	case SBIC_CSR_MIS_2|DATA_OUT_PHASE:
   2206  1.14   chopps 	case SBIC_CSR_MIS_2|DATA_IN_PHASE:
   2207  1.14   chopps 		if( dev->sc_xs->flags & SCSI_POLL || dev->sc_flags & SBICF_ICMD
   2208  1.14   chopps 		   || acb->sc_dmacmd == 0 ) {
   2209  1.14   chopps 			/* Do PIO */
   2210  1.14   chopps 			SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
   2211  1.14   chopps 			if (acb->sc_kv.dc_count <= 0) {
   2212  1.14   chopps 				printf("sbicnextstate:xfer count %d asr%x csr%x\n",
   2213  1.14   chopps 				       acb->sc_kv.dc_count, asr, csr);
   2214  1.14   chopps 				goto abort;
   2215  1.11   chopps 			}
   2216  1.14   chopps 			wait = sbic_data_wait;
   2217  1.14   chopps 			if( sbicxfstart(regs,
   2218  1.14   chopps 					acb->sc_kv.dc_count,
   2219  1.14   chopps 					SBIC_PHASE(csr), wait))
   2220  1.14   chopps 				if( SBIC_PHASE(csr) == DATA_IN_PHASE )
   2221  1.14   chopps 					/* data in? */
   2222  1.14   chopps 					i=sbicxfin(regs,
   2223  1.14   chopps 						   acb->sc_kv.dc_count,
   2224  1.14   chopps 						   acb->sc_kv.dc_addr);
   2225  1.14   chopps 				else
   2226  1.14   chopps 					i=sbicxfout(regs,
   2227  1.14   chopps 						    acb->sc_kv.dc_count,
   2228  1.14   chopps 						    acb->sc_kv.dc_addr,
   2229  1.14   chopps 						    SBIC_PHASE(csr));
   2230  1.14   chopps 			acb->sc_kv.dc_addr +=
   2231  1.14   chopps 				(acb->sc_kv.dc_count - i);
   2232  1.14   chopps 			acb->sc_kv.dc_count = i;
   2233  1.14   chopps 		} else {
   2234  1.14   chopps 			/*
   2235  1.14   chopps 			 * do scatter-gather dma
   2236  1.14   chopps 			 * hacking the controller chip, ouch..
   2237  1.14   chopps 			 */
   2238  1.14   chopps 			SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
   2239  1.14   chopps 					 SBIC_MACHINE_DMA_MODE);
   2240  1.14   chopps 			/*
   2241  1.14   chopps 			 * set next dma addr and dec count
   2242  1.14   chopps 			 */
   2243  1.14   chopps #if 0
   2244  1.14   chopps 			SBIC_TC_GET(regs, tcnt);
   2245  1.14   chopps 			dev->sc_cur->dc_count -= ((dev->sc_tcnt - tcnt) >> 1);
   2246  1.14   chopps 			dev->sc_cur->dc_addr += (dev->sc_tcnt - tcnt);
   2247  1.14   chopps 			dev->sc_tcnt = acb->sc_tcnt = tcnt;
   2248  1.14   chopps #else
   2249  1.14   chopps 			sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   2250  1.14   chopps 			sbic_load_ptrs(dev, regs, dev->target, dev->lun);
   2251  1.14   chopps #endif
   2252  1.14   chopps #ifdef DEBUG
   2253  1.14   chopps 			if( data_pointer_debug > 1 )
   2254  1.14   chopps 				printf("next dmanext: %d(%x:%x)\n",
   2255  1.14   chopps 				       dev->target,dev->sc_cur->dc_addr,
   2256  1.14   chopps 				       dev->sc_tcnt);
   2257  1.14   chopps #endif
   2258  1.14   chopps 			dev->sc_tcnt = dev->sc_dmanext(dev);
   2259  1.14   chopps 			SBIC_TC_PUT(regs, (unsigned)dev->sc_tcnt);
   2260  1.14   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
   2261  1.14   chopps 			dev->sc_flags |= SBICF_INDMA;
   2262  1.14   chopps 		}
   2263  1.14   chopps 		break;
   2264  1.14   chopps 
   2265  1.14   chopps 	case SBIC_CSR_XFERRED|MESG_IN_PHASE:
   2266  1.14   chopps 	case SBIC_CSR_MIS|MESG_IN_PHASE:
   2267  1.14   chopps 	case SBIC_CSR_MIS_1|MESG_IN_PHASE:
   2268  1.14   chopps 	case SBIC_CSR_MIS_2|MESG_IN_PHASE:
   2269  1.14   chopps 		return sbicmsgin(dev);
   2270  1.14   chopps 
   2271  1.14   chopps 	case SBIC_CSR_MSGIN_W_ACK:
   2272  1.14   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK); /* Dunno what I'm ACKing */
   2273  1.14   chopps 		printf("Acking unknown msgin CSR:%02x",csr);
   2274  1.14   chopps 		break;
   2275  1.14   chopps 
   2276  1.14   chopps 	case SBIC_CSR_XFERRED|MESG_OUT_PHASE:
   2277  1.14   chopps 	case SBIC_CSR_MIS|MESG_OUT_PHASE:
   2278  1.14   chopps 	case SBIC_CSR_MIS_1|MESG_OUT_PHASE:
   2279  1.14   chopps 	case SBIC_CSR_MIS_2|MESG_OUT_PHASE:
   2280  1.14   chopps #ifdef DEBUG
   2281  1.14   chopps 		if (sync_debug)
   2282  1.14   chopps 			printf ("sending REJECT msg to last msg.\n");
   2283  1.14   chopps #endif
   2284  1.14   chopps 
   2285  1.14   chopps 		sbic_save_ptrs(dev, regs, dev->target, dev->lun);
   2286   1.1   chopps 		/*
   2287  1.14   chopps 		 * should only get here on reject,
   2288  1.14   chopps 		 * since it's always US that
   2289  1.14   chopps 		 * initiate a sync transfer
   2290   1.1   chopps 		 */
   2291  1.14   chopps 		SEND_BYTE(regs, MSG_REJECT);
   2292  1.14   chopps 		WAIT_CIP(regs);
   2293  1.14   chopps 		if( asr & (SBIC_ASR_BSY|SBIC_ASR_LCI|SBIC_ASR_CIP) )
   2294  1.14   chopps 			printf("next: REJECT sent asr %02x\n", asr);
   2295  1.14   chopps 		return SBIC_STATE_RUNNING;
   2296  1.14   chopps 
   2297  1.14   chopps 	case SBIC_CSR_DISC:
   2298  1.14   chopps 	case SBIC_CSR_DISC_1:
   2299  1.14   chopps 		dev->sc_flags &= ~(SBICF_INDMA|SBICF_SELECTED);
   2300  1.14   chopps 
   2301  1.14   chopps 		/* Try to schedule another target */
   2302  1.14   chopps #ifdef DEBUG
   2303  1.14   chopps 		if(reselect_debug>1)
   2304  1.14   chopps 			printf("sbicnext target %d disconnected\n", dev->target);
   2305  1.14   chopps #endif
   2306  1.14   chopps 		TAILQ_INSERT_HEAD(&dev->nexus_list, acb, chain);
   2307  1.14   chopps 		++dev->sc_tinfo[dev->target].dconns;
   2308  1.14   chopps 		dev->sc_nexus = NULL;
   2309  1.14   chopps 		dev->sc_xs = NULL;
   2310   1.1   chopps 
   2311  1.14   chopps 		if( acb->xs->flags & SCSI_POLL
   2312  1.14   chopps 		   || (dev->sc_flags & SBICF_ICMD)
   2313  1.14   chopps 		   || !sbic_parallel_operations )
   2314  1.14   chopps 			return SBIC_STATE_DISCONNECT;
   2315  1.14   chopps 		sbic_sched(dev);
   2316  1.14   chopps 		return SBIC_STATE_DISCONNECT;
   2317  1.14   chopps 
   2318  1.14   chopps 	case SBIC_CSR_RSLT_NI:
   2319  1.14   chopps 	case SBIC_CSR_RSLT_IFY:
   2320  1.14   chopps 		GET_SBIC_rselid(regs, newtarget);
   2321  1.14   chopps 		/* check SBIC_RID_SIV? */
   2322  1.14   chopps 		newtarget &= SBIC_RID_MASK;
   2323  1.14   chopps 		if (csr == SBIC_CSR_RSLT_IFY) {
   2324  1.14   chopps 			/* Read IFY msg to avoid lockup */
   2325  1.14   chopps 			GET_SBIC_data(regs, newlun);
   2326  1.14   chopps 			WAIT_CIP(regs);
   2327  1.14   chopps 			newlun &= SBIC_TLUN_MASK;
   2328  1.14   chopps 		} else {
   2329  1.14   chopps 			/* Need to get IFY message */
   2330  1.14   chopps 			for (newlun = 256; newlun; --newlun) {
   2331  1.14   chopps 				GET_SBIC_asr(regs, asr);
   2332  1.14   chopps 				if (asr & SBIC_ASR_INT)
   2333  1.14   chopps 					break;
   2334  1.14   chopps 				delay(1);
   2335  1.14   chopps 			}
   2336  1.14   chopps 			newlun = 0;	/* XXXX */
   2337  1.14   chopps 			if ((asr & SBIC_ASR_INT) == 0) {
   2338  1.14   chopps #ifdef DEBUG
   2339  1.14   chopps 				if (reselect_debug)
   2340  1.14   chopps 					printf("RSLT_NI - no IFFY message? asr %x\n", asr);
   2341  1.14   chopps #endif
   2342  1.14   chopps 			} else {
   2343  1.14   chopps 				GET_SBIC_csr(regs,csr);
   2344  1.14   chopps 				if (csr == SBIC_CSR_MIS|MESG_IN_PHASE ||
   2345  1.14   chopps 				    csr == SBIC_CSR_MIS_1|MESG_IN_PHASE ||
   2346  1.14   chopps 				    csr == SBIC_CSR_MIS_2|MESG_IN_PHASE) {
   2347  1.14   chopps 					sbicmsgin(dev);
   2348  1.14   chopps 					newlun = dev->sc_msg[0] & 7;
   2349  1.14   chopps 				} else {
   2350  1.14   chopps 					printf("RSLT_NI - not MESG_IN_PHASE %x\n",
   2351  1.14   chopps 					    csr);
   2352  1.14   chopps 				}
   2353  1.14   chopps 			}
   2354  1.14   chopps 		}
   2355  1.14   chopps #ifdef DEBUG
   2356  1.14   chopps 		if(reselect_debug>1 || (reselect_debug && csr==SBIC_CSR_RSLT_NI))
   2357  1.14   chopps 			printf("sbicnext: reselect %s from targ %d lun %d\n",
   2358  1.14   chopps 			    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY",
   2359  1.14   chopps 			    newtarget, newlun);
   2360  1.14   chopps #endif
   2361  1.14   chopps 		if (dev->sc_nexus) {
   2362  1.14   chopps #ifdef DEBUG
   2363  1.14   chopps 			if (reselect_debug > 1)
   2364  1.14   chopps 				printf("%s: reselect %s with active command\n",
   2365  1.14   chopps 				    dev->sc_dev.dv_xname,
   2366  1.14   chopps 				    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY");
   2367  1.14   chopps #ifdef DDB
   2368  1.14   chopps /*			Debugger();*/
   2369  1.14   chopps #endif
   2370  1.14   chopps #endif
   2371  1.14   chopps 			TAILQ_INSERT_HEAD(&dev->ready_list, dev->sc_nexus, chain);
   2372  1.14   chopps 			dev->sc_tinfo[dev->target].lubusy &= ~(1 << dev->lun);
   2373  1.14   chopps 		}
   2374  1.15   chopps 		/* Reload sync values for this target */
   2375  1.15   chopps 		if (dev->sc_sync[newtarget].state == SYNC_DONE)
   2376  1.15   chopps 			SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[newtarget].offset,
   2377  1.15   chopps 			    dev->sc_sync[newtarget].period));
   2378  1.15   chopps 		else
   2379  1.15   chopps 			SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
   2380  1.14   chopps 		for (acb = dev->nexus_list.tqh_first; acb;
   2381  1.14   chopps 		    acb = acb->chain.tqe_next) {
   2382  1.14   chopps 			if (acb->xs->sc_link->target != newtarget ||
   2383  1.14   chopps 			    acb->xs->sc_link->lun != newlun)
   2384  1.14   chopps 				continue;
   2385  1.14   chopps 			TAILQ_REMOVE(&dev->nexus_list, acb, chain);
   2386  1.14   chopps 			dev->sc_nexus = acb;
   2387  1.14   chopps 			dev->sc_xs = acb->xs;
   2388  1.14   chopps 			dev->sc_flags |= SBICF_SELECTED;
   2389  1.14   chopps 			dev->target = newtarget;
   2390  1.14   chopps 			dev->lun = newlun;
   2391  1.14   chopps 			break;
   2392  1.14   chopps 		}
   2393  1.14   chopps 		if (acb == NULL) {
   2394  1.14   chopps 			printf("%s: reselect %s targ %d not in nexus_list %x\n",
   2395  1.14   chopps 			    dev->sc_dev.dv_xname,
   2396  1.14   chopps 			    csr == SBIC_CSR_RSLT_NI ? "NI" : "IFY", newtarget,
   2397  1.14   chopps 			    &dev->nexus_list.tqh_first);
   2398  1.14   chopps 			panic("bad reselect in sbic");
   2399  1.14   chopps 		}
   2400  1.14   chopps 		if (csr == SBIC_CSR_RSLT_IFY)
   2401  1.14   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   2402  1.14   chopps 		break;
   2403   1.1   chopps 
   2404  1.14   chopps 	default:
   2405  1.14   chopps         abort:
   2406   1.1   chopps 		/*
   2407   1.1   chopps 		 * Something unexpected happened -- deal with it.
   2408   1.1   chopps 		 */
   2409  1.14   chopps 		printf("sbicnextstate: aborting csr %02x asr %02x\n", csr, asr);
   2410  1.14   chopps #ifdef DDB
   2411  1.14   chopps 		Debugger();
   2412  1.14   chopps #endif
   2413  1.14   chopps #ifdef DEBUG
   2414  1.14   chopps 		if( data_pointer_debug > 1 )
   2415  1.14   chopps 			printf("next dmastop: %d(%x:%x)\n",
   2416  1.14   chopps 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   2417  1.14   chopps #endif
   2418   1.1   chopps 		dev->sc_dmastop(dev);
   2419   1.1   chopps 		sbicerror(dev, regs, csr);
   2420  1.14   chopps 		sbicabort(dev, regs, "next");
   2421   1.1   chopps 		if (dev->sc_flags & SBICF_INDMA) {
   2422   1.3   chopps 			/*
   2423   1.3   chopps 			 * check for overlapping cache line, flush if so
   2424   1.3   chopps 			 */
   2425   1.4   chopps #ifdef M68040
   2426   1.3   chopps 			if (dev->sc_flags & SBICF_DCFLUSH) {
   2427  1.14   chopps #if 0
   2428  1.14   chopps 				printf("sibc: 68040 DMA cache flush needs fixing? %x:%x\n",
   2429  1.14   chopps 				    dev->sc_xs->data, dev->sc_xs->datalen);
   2430  1.14   chopps #endif
   2431   1.3   chopps 			}
   2432   1.4   chopps #endif
   2433  1.13  mycroft 			dev->sc_flags &=
   2434  1.14   chopps 				~(SBICF_INDMA | SBICF_DCFLUSH);
   2435  1.14   chopps #ifdef DEBUG
   2436  1.14   chopps 		if( data_pointer_debug > 1 )
   2437  1.14   chopps 			printf("next dmastop: %d(%x:%x)\n",
   2438  1.14   chopps 			       dev->target,dev->sc_cur->dc_addr,dev->sc_tcnt);
   2439  1.14   chopps #endif
   2440  1.14   chopps 			dev->sc_dmastop(dev);
   2441  1.14   chopps 			sbic_scsidone(acb, -1);
   2442   1.1   chopps 		}
   2443  1.14   chopps                 return SBIC_STATE_ERROR;
   2444   1.1   chopps 	}
   2445  1.14   chopps 
   2446  1.14   chopps 	return(SBIC_STATE_RUNNING);
   2447   1.1   chopps }
   2448   1.1   chopps 
   2449  1.14   chopps 
   2450   1.1   chopps /*
   2451   1.1   chopps  * Check if DMA can not be used with specified buffer
   2452   1.1   chopps  */
   2453   1.1   chopps 
   2454   1.1   chopps int
   2455   1.1   chopps sbiccheckdmap(bp, len, mask)
   2456   1.1   chopps 	void *bp;
   2457   1.1   chopps 	u_long len, mask;
   2458   1.1   chopps {
   2459   1.1   chopps 	u_char *buffer;
   2460   1.1   chopps 	u_long phy_buf;
   2461   1.1   chopps 	u_long phy_len;
   2462   1.1   chopps 
   2463   1.1   chopps 	buffer = bp;
   2464   1.1   chopps 
   2465   1.1   chopps 	if (len == 0)
   2466   1.1   chopps 		return(0);
   2467   1.1   chopps 
   2468   1.1   chopps 	while (len) {
   2469   1.1   chopps 		phy_buf = kvtop(buffer);
   2470   1.1   chopps 		if (len < (phy_len = NBPG - ((int) buffer & PGOFSET)))
   2471   1.1   chopps 			phy_len = len;
   2472   1.1   chopps 		if (phy_buf & mask)
   2473   1.1   chopps 			return(1);
   2474   1.1   chopps 		buffer += phy_len;
   2475   1.1   chopps 		len -= phy_len;
   2476   1.1   chopps 	}
   2477   1.1   chopps 	return(0);
   2478   1.1   chopps }
   2479   1.1   chopps 
   2480  1.13  mycroft int
   2481   1.1   chopps sbictoscsiperiod(dev, regs, a)
   2482   1.1   chopps 	struct sbic_softc *dev;
   2483   1.1   chopps 	sbic_regmap_p regs;
   2484   1.1   chopps 	int a;
   2485   1.1   chopps {
   2486   1.1   chopps 	unsigned int fs;
   2487  1.13  mycroft 
   2488   1.1   chopps 	/*
   2489   1.1   chopps 	 * cycle = DIV / (2*CLK)
   2490   1.1   chopps 	 * DIV = FS+2
   2491   1.1   chopps 	 * best we can do is 200ns at 20Mhz, 2 cycles
   2492   1.1   chopps 	 */
   2493  1.13  mycroft 
   2494   1.1   chopps 	GET_SBIC_myid(regs,fs);
   2495   1.1   chopps 	fs = (fs >>6) + 2;		/* DIV */
   2496   1.1   chopps 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);	/* Cycle, in ns */
   2497   1.1   chopps 	if (a < 2) a = 8;		/* map to Cycles */
   2498   1.1   chopps 	return ((fs*a)>>2);		/* in 4 ns units */
   2499   1.1   chopps }
   2500   1.1   chopps 
   2501  1.13  mycroft int
   2502   1.1   chopps sbicfromscsiperiod(dev, regs, p)
   2503   1.1   chopps 	struct sbic_softc *dev;
   2504   1.1   chopps 	sbic_regmap_p regs;
   2505   1.1   chopps 	int p;
   2506   1.1   chopps {
   2507   1.1   chopps 	register unsigned int fs, ret;
   2508  1.13  mycroft 
   2509   1.1   chopps 	/* Just the inverse of the above */
   2510  1.13  mycroft 
   2511   1.1   chopps 	GET_SBIC_myid(regs,fs);
   2512   1.1   chopps 	fs = (fs >>6) + 2;		/* DIV */
   2513   1.1   chopps 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);   /* Cycle, in ns */
   2514  1.13  mycroft 
   2515   1.1   chopps 	ret = p << 2;			/* in ns units */
   2516   1.1   chopps 	ret = ret / fs;			/* in Cycles */
   2517   1.1   chopps 	if (ret < sbic_min_period)
   2518   1.1   chopps 		return(sbic_min_period);
   2519   1.1   chopps 
   2520   1.1   chopps 	/* verify rounding */
   2521   1.1   chopps 	if (sbictoscsiperiod(dev, regs, ret) < p)
   2522   1.1   chopps 		ret++;
   2523   1.1   chopps 	return (ret >= 8) ? 0 : ret;
   2524   1.1   chopps }
   2525   1.1   chopps 
   2526  1.14   chopps #ifdef DEBUG
   2527  1.14   chopps 
   2528  1.14   chopps void sbicdumpstate()
   2529  1.14   chopps {
   2530  1.14   chopps 	u_char csr, asr;
   2531  1.14   chopps 
   2532  1.14   chopps 	GET_SBIC_asr(debug_sbic_regs,asr);
   2533  1.14   chopps 	GET_SBIC_csr(debug_sbic_regs,csr);
   2534  1.14   chopps 	printf("%s: asr:csr(%02x:%02x)->(%02x:%02x)\n",
   2535  1.14   chopps 	       (routine==1)?"sbicgo":
   2536  1.14   chopps 	       (routine==2)?"sbicintr":
   2537  1.14   chopps 	       (routine==3)?"sbicicmd":
   2538  1.14   chopps 	       (routine==4)?"sbicnext":"unknown",
   2539  1.14   chopps 	       debug_asr, debug_csr, asr, csr);
   2540  1.14   chopps 
   2541  1.14   chopps }
   2542  1.14   chopps 
   2543  1.14   chopps void sbictimeout(dev)
   2544  1.14   chopps 	struct sbic_softc *dev;
   2545  1.14   chopps {
   2546  1.14   chopps 	int s, asr;
   2547  1.14   chopps 
   2548  1.14   chopps 	/* Dump this every second while there's an active command */
   2549  1.14   chopps 	if( dev->sc_nexus ) {
   2550  1.14   chopps 		s = splbio();
   2551  1.14   chopps 		GET_SBIC_asr(dev->sc_sbicp, asr);
   2552  1.14   chopps 		if( asr & SBIC_ASR_INT ) {
   2553  1.14   chopps 			/* We need to service a missed IRQ */
   2554  1.14   chopps 			printf("Servicing a missed int:(%02x,%02x)->(%02x,??)\n",
   2555  1.14   chopps 			       debug_asr, debug_csr, asr);
   2556  1.14   chopps 			sbicintr(dev);
   2557  1.14   chopps 		}
   2558  1.14   chopps 		splx(s);
   2559  1.14   chopps 		sbicdumpstate();
   2560  1.14   chopps 		timeout((void *)sbictimeout, (void*)dev, sbic_timeout * hz);
   2561  1.14   chopps 	} else timeout_active = 0;
   2562  1.14   chopps }
   2563  1.14   chopps 
   2564  1.14   chopps #endif
   2565