Home | History | Annotate | Line # | Download | only in dev
sbic.c revision 1.13
      1  1.13  mycroft /*	$NetBSD: sbic.c,v 1.13 1995/08/12 20:30:47 mycroft 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 /* need to know if any tapes have been configured */
     47   1.1   chopps #include "st.h"
     48   1.1   chopps 
     49   1.1   chopps #include <sys/param.h>
     50   1.1   chopps #include <sys/systm.h>
     51   1.1   chopps #include <sys/device.h>
     52  1.10   chopps #include <sys/disklabel.h>
     53  1.10   chopps #include <sys/dkstat.h>
     54   1.1   chopps #include <sys/buf.h>
     55   1.1   chopps #include <scsi/scsi_all.h>
     56   1.1   chopps #include <scsi/scsiconf.h>
     57   1.1   chopps #include <vm/vm.h>
     58   1.1   chopps #include <vm/vm_kern.h>
     59   1.1   chopps #include <vm/vm_page.h>
     60   1.1   chopps #include <machine/pmap.h>
     61   1.1   chopps #include <machine/cpu.h>
     62   1.1   chopps #include <amiga/amiga/device.h>
     63   1.1   chopps #include <amiga/amiga/custom.h>
     64  1.10   chopps #include <amiga/amiga/isr.h>
     65   1.1   chopps #include <amiga/dev/dmavar.h>
     66   1.1   chopps #include <amiga/dev/sbicreg.h>
     67   1.1   chopps #include <amiga/dev/sbicvar.h>
     68   1.1   chopps 
     69   1.1   chopps /*
     70   1.1   chopps  * SCSI delays
     71   1.1   chopps  * In u-seconds, primarily for state changes on the SPC.
     72   1.1   chopps  */
     73   1.1   chopps #define	SBIC_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
     74   1.1   chopps #define	SBIC_DATA_WAIT	50000	/* wait per data in/out step */
     75   1.1   chopps #define	SBIC_INIT_WAIT	50000	/* wait per step (both) during init */
     76   1.1   chopps 
     77   1.1   chopps #define	b_cylin		b_resid
     78   1.1   chopps #define SBIC_WAIT(regs, until, timeo) sbicwait(regs, until, timeo, __LINE__)
     79   1.1   chopps 
     80   1.1   chopps extern u_int kvtop();
     81   1.1   chopps 
     82   1.7   chopps int  sbicicmd __P((struct sbic_softc *, int, int, void *, int, void *, int,u_char));
     83   1.1   chopps int  sbicgo __P((struct sbic_softc *, struct scsi_xfer *));
     84   1.1   chopps int  sbicdmaok __P((struct sbic_softc *, struct scsi_xfer *));
     85   1.1   chopps int  sbicgetsense __P((struct sbic_softc *, struct scsi_xfer *));
     86   1.1   chopps int  sbicwait __P((sbic_regmap_p, char, int , int));
     87  1.13  mycroft int  sbiccheckdmap __P((void *, u_long, u_long));
     88   1.7   chopps int  sbicselectbus __P((struct sbic_softc *, sbic_regmap_p, u_char, u_char, u_char));
     89   1.1   chopps int  sbicxfstart __P((sbic_regmap_p, int, u_char, int));
     90   1.1   chopps int  sbicxfout __P((sbic_regmap_p regs, int, void *, int));
     91   1.1   chopps int  sbicfromscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
     92   1.1   chopps int  sbictoscsiperiod __P((struct sbic_softc *, sbic_regmap_p, int));
     93   1.1   chopps int  sbicintr __P((struct sbic_softc *));
     94   1.1   chopps void sbicxfin __P((sbic_regmap_p regs, int, void *));
     95   1.1   chopps void sbicxfdone __P((struct sbic_softc *, sbic_regmap_p, int));
     96   1.1   chopps void sbicabort __P((struct sbic_softc *, sbic_regmap_p, char *));
     97   1.1   chopps void sbicerror __P((struct sbic_softc *, sbic_regmap_p, u_char));
     98   1.1   chopps void sbicstart __P((struct sbic_softc *));
     99   1.1   chopps void sbicreset __P((struct sbic_softc *));
    100   1.1   chopps void sbicsetdelay __P((int));
    101   1.1   chopps void sbic_scsidone __P((struct sbic_softc *, int));
    102   1.1   chopps void sbic_donextcmd __P((struct sbic_softc *));
    103   1.1   chopps 
    104   1.1   chopps /*
    105   1.1   chopps  * Synch xfer parameters, and timing conversions
    106   1.1   chopps  */
    107   1.1   chopps int sbic_min_period = SBIC_SYN_MIN_PERIOD;  /* in cycles = f(ICLK,FSn) */
    108   1.1   chopps int sbic_max_offset = SBIC_SYN_MAX_OFFSET;  /* pure number */
    109   1.1   chopps 
    110   1.1   chopps int sbic_cmd_wait = SBIC_CMD_WAIT;
    111   1.1   chopps int sbic_data_wait = SBIC_DATA_WAIT;
    112   1.1   chopps int sbic_init_wait = SBIC_INIT_WAIT;
    113   1.1   chopps 
    114   1.1   chopps /*
    115   1.1   chopps  * was broken before.. now if you want this you get it for all drives
    116   1.1   chopps  * on sbic controllers.
    117   1.1   chopps  */
    118   1.9   chopps int sbic_inhibit_sync = 0;
    119   1.1   chopps int sbic_clock_override = 0;
    120   1.1   chopps int sbic_no_dma = 0;
    121   1.1   chopps 
    122   1.1   chopps #ifdef DEBUG
    123  1.10   chopps int	sbicdma_ops = 0;	/* total DMA operations */
    124  1.10   chopps int	sbicdma_bounces = 0;	/* number operations using bounce buffer */
    125  1.10   chopps int	sbicdma_hits = 0;	/* number of DMA chains that were contiguous */
    126  1.10   chopps int	sbicdma_misses = 0;	/* number of DMA chains that were not contiguous */
    127   1.1   chopps #define QPRINTF(a) if (sbic_debug > 1) printf a
    128   1.1   chopps int	sbic_debug = 0;
    129   1.1   chopps int	sync_debug = 0;
    130   1.1   chopps int	sbic_dma_debug = 0;
    131   1.1   chopps #else
    132   1.1   chopps #define QPRINTF
    133   1.1   chopps #endif
    134   1.1   chopps 
    135   1.1   chopps /*
    136   1.1   chopps  * default minphys routine for sbic based controllers
    137   1.1   chopps  */
    138  1.13  mycroft void
    139   1.1   chopps sbic_minphys(bp)
    140   1.1   chopps 	struct buf *bp;
    141   1.1   chopps {
    142  1.13  mycroft 
    143   1.1   chopps 	/*
    144  1.13  mycroft 	 * No max transfer at this level.
    145   1.1   chopps 	 */
    146  1.13  mycroft 	minphys(bp);
    147   1.1   chopps }
    148   1.1   chopps 
    149   1.1   chopps /*
    150   1.1   chopps  * used by specific sbic controller
    151   1.1   chopps  *
    152   1.1   chopps  * it appears that the higher level code does nothing with LUN's
    153   1.1   chopps  * so I will too.  I could plug it in, however so could they
    154   1.1   chopps  * in scsi_scsi_cmd().
    155   1.1   chopps  */
    156   1.1   chopps int
    157   1.1   chopps sbic_scsicmd(xs)
    158   1.1   chopps 	struct scsi_xfer *xs;
    159   1.1   chopps {
    160   1.1   chopps 	struct sbic_pending *pendp;
    161   1.1   chopps 	struct sbic_softc *dev;
    162   1.1   chopps 	struct scsi_link *slp;
    163   1.1   chopps 	int flags, s;
    164   1.1   chopps 
    165   1.1   chopps 	slp = xs->sc_link;
    166   1.1   chopps 	dev = slp->adapter_softc;
    167   1.1   chopps 	flags = xs->flags;
    168   1.1   chopps 
    169   1.1   chopps 	if (flags & SCSI_DATA_UIO)
    170   1.1   chopps 		panic("sbic: scsi data uio requested");
    171  1.13  mycroft 
    172   1.8   chopps 	if (dev->sc_xs && flags & SCSI_POLL)
    173   1.1   chopps 		panic("sbic_scsicmd: busy");
    174   1.1   chopps 
    175   1.1   chopps 	s = splbio();
    176   1.1   chopps 	pendp = &dev->sc_xsstore[slp->target][slp->lun];
    177   1.1   chopps 	if (pendp->xs) {
    178   1.1   chopps 		splx(s);
    179   1.1   chopps 		return(TRY_AGAIN_LATER);
    180   1.1   chopps 	}
    181   1.1   chopps 
    182   1.1   chopps 	if (dev->sc_xs) {
    183   1.1   chopps 		pendp->xs = xs;
    184   1.1   chopps 		TAILQ_INSERT_TAIL(&dev->sc_xslist, pendp, link);
    185   1.1   chopps 		splx(s);
    186   1.1   chopps 		return(SUCCESSFULLY_QUEUED);
    187   1.1   chopps 	}
    188   1.1   chopps 	pendp->xs = NULL;
    189   1.1   chopps 	dev->sc_xs = xs;
    190   1.1   chopps 	splx(s);
    191   1.1   chopps 
    192   1.1   chopps 	/*
    193   1.1   chopps 	 * nothing is pending do it now.
    194   1.1   chopps 	 */
    195   1.1   chopps 	sbic_donextcmd(dev);
    196   1.1   chopps 
    197   1.8   chopps 	if (flags & SCSI_POLL)
    198   1.1   chopps 		return(COMPLETE);
    199   1.1   chopps 	return(SUCCESSFULLY_QUEUED);
    200   1.1   chopps }
    201   1.1   chopps 
    202   1.1   chopps /*
    203   1.1   chopps  * entered with dev->sc_xs pointing to the next xfer to perform
    204   1.1   chopps  */
    205   1.1   chopps void
    206   1.1   chopps sbic_donextcmd(dev)
    207   1.1   chopps 	struct sbic_softc *dev;
    208   1.1   chopps {
    209   1.1   chopps 	struct scsi_xfer *xs;
    210   1.1   chopps 	struct scsi_link *slp;
    211   1.1   chopps 	int flags, phase, stat;
    212   1.1   chopps 
    213   1.1   chopps 	xs = dev->sc_xs;
    214   1.1   chopps 	slp = xs->sc_link;
    215   1.1   chopps 	flags = xs->flags;
    216   1.1   chopps 
    217   1.1   chopps 	if (flags & SCSI_DATA_IN)
    218   1.1   chopps 		phase = DATA_IN_PHASE;
    219   1.1   chopps 	else if (flags & SCSI_DATA_OUT)
    220   1.1   chopps 		phase = DATA_OUT_PHASE;
    221   1.1   chopps 	else
    222   1.1   chopps 		phase = STATUS_PHASE;
    223  1.13  mycroft 
    224   1.1   chopps 	if (flags & SCSI_RESET)
    225   1.1   chopps 		sbicreset(dev);
    226   1.1   chopps 
    227   1.1   chopps 	dev->sc_stat[0] = -1;
    228   1.7   chopps 	xs->cmd->bytes[0] |= slp->lun << 5;
    229   1.8   chopps 	if (phase == STATUS_PHASE || flags & SCSI_POLL ||
    230  1.13  mycroft 	    sbicdmaok(dev, xs) == 0)
    231   1.7   chopps 		stat = sbicicmd(dev, slp->target, slp->lun, xs->cmd,
    232   1.7   chopps 		    xs->cmdlen, xs->data, xs->datalen, phase);
    233   1.1   chopps 	else if (sbicgo(dev, xs) == 0)
    234   1.1   chopps 		return;
    235  1.13  mycroft 	else
    236   1.1   chopps 		stat = dev->sc_stat[0];
    237  1.13  mycroft 
    238   1.1   chopps 	sbic_scsidone(dev, stat);
    239   1.1   chopps }
    240   1.1   chopps 
    241   1.1   chopps void
    242   1.1   chopps sbic_scsidone(dev, stat)
    243   1.1   chopps 	struct sbic_softc *dev;
    244   1.1   chopps 	int stat;
    245   1.1   chopps {
    246   1.1   chopps 	struct sbic_pending *pendp;
    247   1.1   chopps 	struct scsi_xfer *xs;
    248   1.1   chopps 	int s, donext;
    249   1.1   chopps 
    250   1.1   chopps 	xs = dev->sc_xs;
    251   1.1   chopps #ifdef DIAGNOSTIC
    252   1.1   chopps 	if (xs == NULL)
    253   1.1   chopps 		panic("sbic_scsidone");
    254   1.1   chopps #endif
    255  1.10   chopps #if 1
    256  1.10   chopps 	if (((struct device *)(xs->sc_link->device_softc))->dv_unit < dk_ndrive)
    257  1.10   chopps 		++dk_xfer[((struct device *)(xs->sc_link->device_softc))->dv_unit];
    258  1.10   chopps #endif
    259   1.1   chopps 	/*
    260   1.1   chopps 	 * is this right?
    261   1.1   chopps 	 */
    262   1.1   chopps 	xs->status = stat;
    263   1.1   chopps 
    264   1.8   chopps 	if (stat == 0)
    265   1.1   chopps 		xs->resid = 0;
    266   1.1   chopps 	else {
    267   1.1   chopps 		switch(stat) {
    268   1.1   chopps 		case SCSI_CHECK:
    269   1.1   chopps 			if (stat = sbicgetsense(dev, xs))
    270   1.1   chopps 				goto bad_sense;
    271   1.1   chopps 			xs->error = XS_SENSE;
    272   1.1   chopps 			break;
    273   1.1   chopps 		case SCSI_BUSY:
    274   1.1   chopps 			xs->error = XS_BUSY;
    275   1.1   chopps 			break;
    276   1.1   chopps 		bad_sense:
    277   1.1   chopps 		default:
    278   1.1   chopps 			xs->error = XS_DRIVER_STUFFUP;
    279   1.1   chopps 			QPRINTF(("sbic_scsicmd() bad %x\n", stat));
    280   1.1   chopps 			break;
    281   1.1   chopps 		}
    282   1.1   chopps 	}
    283   1.1   chopps 	xs->flags |= ITSDONE;
    284   1.1   chopps 
    285   1.1   chopps 	/*
    286   1.1   chopps 	 * grab next command before scsi_done()
    287   1.1   chopps 	 * this way no single device can hog scsi resources.
    288   1.1   chopps 	 */
    289   1.1   chopps 	s = splbio();
    290   1.1   chopps 	pendp = dev->sc_xslist.tqh_first;
    291   1.1   chopps 	if (pendp == NULL) {
    292   1.1   chopps 		donext = 0;
    293   1.1   chopps 		dev->sc_xs = NULL;
    294   1.1   chopps 	} else {
    295   1.1   chopps 		donext = 1;
    296   1.1   chopps 		TAILQ_REMOVE(&dev->sc_xslist, pendp, link);
    297   1.1   chopps 		dev->sc_xs = pendp->xs;
    298   1.1   chopps 		pendp->xs = NULL;
    299   1.1   chopps 	}
    300   1.1   chopps 	splx(s);
    301   1.1   chopps 	scsi_done(xs);
    302   1.1   chopps 
    303   1.1   chopps 	if (donext)
    304   1.1   chopps 		sbic_donextcmd(dev);
    305   1.1   chopps }
    306   1.1   chopps 
    307   1.1   chopps int
    308   1.1   chopps sbicgetsense(dev, xs)
    309   1.1   chopps 	struct sbic_softc *dev;
    310   1.1   chopps 	struct scsi_xfer *xs;
    311   1.1   chopps {
    312   1.1   chopps 	struct scsi_sense rqs;
    313   1.1   chopps 	struct scsi_link *slp;
    314   1.1   chopps 	int stat;
    315   1.1   chopps 
    316   1.1   chopps 	slp = xs->sc_link;
    317  1.13  mycroft 
    318   1.8   chopps 	rqs.opcode = REQUEST_SENSE;
    319   1.1   chopps 	rqs.byte2 = slp->lun << 5;
    320   1.2   chopps #ifdef not_yet
    321  1.13  mycroft 	rqs.length = xs->req_sense_length ? xs->req_sense_length :
    322   1.1   chopps 	    sizeof(xs->sense);
    323   1.2   chopps #else
    324   1.2   chopps 	rqs.length = sizeof(xs->sense);
    325   1.2   chopps #endif
    326  1.13  mycroft 
    327   1.1   chopps 	rqs.unused[0] = rqs.unused[1] = rqs.control = 0;
    328  1.13  mycroft 
    329   1.7   chopps 	return(sbicicmd(dev, slp->target, slp->lun, &rqs, sizeof(rqs),
    330   1.7   chopps 	    &xs->sense, rqs.length, DATA_IN_PHASE));
    331   1.1   chopps }
    332   1.1   chopps 
    333   1.1   chopps int
    334   1.1   chopps sbicdmaok(dev, xs)
    335   1.1   chopps 	struct sbic_softc *dev;
    336   1.1   chopps 	struct scsi_xfer *xs;
    337   1.1   chopps {
    338   1.1   chopps 	if (sbic_no_dma || xs->datalen & 0x1 || (u_int)xs->data & 0x3)
    339   1.1   chopps 		return(0);
    340   1.1   chopps 	/*
    341   1.1   chopps 	 * controller supports dma to any addresses?
    342   1.1   chopps 	 */
    343  1.13  mycroft 	else if ((dev->sc_flags & SBICF_BADDMA) == 0)
    344   1.1   chopps 		return(1);
    345   1.1   chopps 	/*
    346   1.1   chopps 	 * this address is ok for dma?
    347   1.1   chopps 	 */
    348   1.1   chopps 	else if (sbiccheckdmap(xs->data, xs->datalen, dev->sc_dmamask) == 0)
    349   1.1   chopps 		return(1);
    350   1.1   chopps 	/*
    351   1.1   chopps 	 * we have a bounce buffer?
    352   1.1   chopps 	 */
    353   1.1   chopps 	else if (dev->sc_dmabuffer)
    354   1.1   chopps 		return(1);
    355   1.1   chopps 	return(0);
    356   1.1   chopps }
    357   1.1   chopps 
    358   1.1   chopps 
    359   1.1   chopps int
    360   1.1   chopps sbicwait(regs, until, timeo, line)
    361   1.1   chopps 	sbic_regmap_p regs;
    362   1.1   chopps 	char until;
    363   1.1   chopps 	int timeo;
    364   1.1   chopps 	int line;
    365   1.1   chopps {
    366   1.1   chopps 	u_char val;
    367   1.1   chopps 	int csr;
    368   1.1   chopps 
    369   1.1   chopps 	if (timeo == 0)
    370   1.1   chopps 		timeo = 1000000;	/* some large value.. */
    371   1.1   chopps 
    372   1.1   chopps 	GET_SBIC_asr(regs,val);
    373   1.1   chopps 	while ((val & until) == 0) {
    374   1.1   chopps 		if (timeo-- == 0) {
    375   1.1   chopps 			GET_SBIC_csr(regs, csr);
    376   1.1   chopps 			printf("sbicwait TIMEO @%d with asr=x%x csr=x%x\n",
    377   1.1   chopps 			    line, val, csr);
    378   1.1   chopps 			break;
    379   1.1   chopps 		}
    380   1.1   chopps 		DELAY(1);
    381   1.1   chopps 		GET_SBIC_asr(regs,val);
    382   1.1   chopps 	}
    383   1.1   chopps 	return(val);
    384   1.1   chopps }
    385   1.1   chopps 
    386   1.1   chopps void
    387   1.1   chopps sbicabort(dev, regs, where)
    388   1.1   chopps 	struct sbic_softc *dev;
    389   1.1   chopps 	sbic_regmap_p regs;
    390   1.1   chopps 	char *where;
    391   1.1   chopps {
    392   1.1   chopps 	u_char csr, asr;
    393  1.13  mycroft 
    394   1.1   chopps 	GET_SBIC_csr(regs, csr);
    395   1.1   chopps 	GET_SBIC_asr(regs, asr);
    396   1.1   chopps 
    397  1.13  mycroft 	printf ("%s: abort %s: csr = 0x%02x, asr = 0x%02x\n",
    398   1.1   chopps 	    dev->sc_dev.dv_xname, where, csr, asr);
    399   1.1   chopps 
    400   1.1   chopps 	if (dev->sc_flags & SBICF_SELECTED) {
    401   1.1   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_ABORT);
    402   1.1   chopps 		WAIT_CIP(regs);
    403   1.1   chopps 
    404   1.1   chopps 		GET_SBIC_asr(regs, asr);
    405   1.1   chopps 		if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI)) {
    406   1.1   chopps 			/* ok, get more drastic.. */
    407  1.13  mycroft 
    408   1.1   chopps 			SET_SBIC_cmd (regs, SBIC_CMD_RESET);
    409   1.1   chopps 			DELAY(25);
    410   1.1   chopps 			SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    411   1.1   chopps 			/* clears interrupt also */
    412   1.1   chopps 			GET_SBIC_csr (regs, csr);
    413   1.1   chopps 
    414   1.1   chopps 			dev->sc_flags &= ~SBICF_SELECTED;
    415   1.1   chopps 			return;
    416   1.1   chopps 		}
    417   1.1   chopps 
    418   1.1   chopps 		do {
    419   1.1   chopps 			SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    420   1.1   chopps 			GET_SBIC_csr (regs, csr);
    421   1.1   chopps 		} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
    422   1.1   chopps 		    && (csr != SBIC_CSR_CMD_INVALID));
    423   1.1   chopps 
    424   1.1   chopps 		/* lets just hope it worked.. */
    425   1.1   chopps 		dev->sc_flags &= ~SBICF_SELECTED;
    426   1.1   chopps 	}
    427   1.1   chopps }
    428   1.1   chopps 
    429   1.1   chopps /*
    430   1.1   chopps  * XXX Set/reset long delays.
    431   1.1   chopps  *
    432   1.1   chopps  * if delay == 0, reset default delays
    433   1.1   chopps  * if delay < 0,  set both delays to default long initialization values
    434   1.1   chopps  * if delay > 0,  set both delays to this value
    435   1.1   chopps  *
    436   1.1   chopps  * Used when a devices is expected to respond slowly (e.g. during
    437   1.1   chopps  * initialization).
    438   1.1   chopps  */
    439   1.1   chopps void
    440   1.1   chopps sbicsetdelay(del)
    441   1.1   chopps 	int del;
    442   1.1   chopps {
    443   1.1   chopps 	static int saved_cmd_wait, saved_data_wait;
    444   1.1   chopps 
    445   1.1   chopps 	if (del) {
    446   1.1   chopps 		saved_cmd_wait = sbic_cmd_wait;
    447   1.1   chopps 		saved_data_wait = sbic_data_wait;
    448   1.1   chopps 		if (del > 0)
    449   1.1   chopps 			sbic_cmd_wait = sbic_data_wait = del;
    450   1.1   chopps 		else
    451   1.1   chopps 			sbic_cmd_wait = sbic_data_wait = sbic_init_wait;
    452   1.1   chopps 	} else {
    453   1.1   chopps 		sbic_cmd_wait = saved_cmd_wait;
    454   1.1   chopps 		sbic_data_wait = saved_data_wait;
    455   1.1   chopps 	}
    456   1.1   chopps }
    457   1.1   chopps 
    458   1.1   chopps void
    459   1.1   chopps sbicreset(dev)
    460   1.1   chopps 	struct sbic_softc *dev;
    461   1.1   chopps {
    462   1.1   chopps 	sbic_regmap_p regs;
    463   1.1   chopps 	u_int i, s;
    464   1.1   chopps 	u_char my_id, csr;
    465  1.13  mycroft 
    466   1.1   chopps 	regs = dev->sc_sbicp;
    467   1.1   chopps 
    468   1.1   chopps 	if (dev->sc_flags & SBICF_ALIVE)
    469   1.1   chopps 		sbicabort(dev, regs, "reset");
    470  1.13  mycroft 
    471   1.1   chopps 	s = splbio();
    472   1.1   chopps 	/* preserve our ID for now */
    473   1.1   chopps 	GET_SBIC_myid (regs, my_id);
    474   1.1   chopps 	my_id &= SBIC_ID_MASK;
    475   1.1   chopps 
    476   1.1   chopps 	if (dev->sc_clkfreq < 110)
    477   1.1   chopps 		my_id |= SBIC_ID_FS_8_10;
    478   1.1   chopps 	else if (dev->sc_clkfreq < 160)
    479   1.1   chopps 		my_id |= SBIC_ID_FS_12_15;
    480   1.1   chopps 	else if (dev->sc_clkfreq < 210)
    481   1.1   chopps 		my_id |= SBIC_ID_FS_16_20;
    482   1.1   chopps 
    483   1.1   chopps 	my_id |= SBIC_ID_EAF /*| SBIC_ID_EHP*/ ;
    484   1.1   chopps 
    485   1.1   chopps 	SET_SBIC_myid(regs, my_id);
    486   1.1   chopps 
    487   1.1   chopps 	/*
    488   1.1   chopps 	 * Disable interrupts (in dmainit) then reset the chip
    489   1.1   chopps 	 */
    490   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_RESET);
    491   1.1   chopps 	DELAY(25);
    492   1.1   chopps 	SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    493   1.1   chopps 	GET_SBIC_csr(regs, csr);       /* clears interrupt also */
    494   1.1   chopps 
    495   1.1   chopps 	/*
    496   1.1   chopps 	 * Set up various chip parameters
    497   1.1   chopps 	 */
    498  1.13  mycroft 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI
    499   1.1   chopps 	    | SBIC_MACHINE_DMA_MODE);
    500   1.1   chopps 	/*
    501   1.1   chopps 	 * don't allow (re)selection (SBIC_RID_ES)
    502   1.1   chopps 	 * until we can handle target mode!!
    503   1.1   chopps 	 */
    504   1.1   chopps 	SET_SBIC_rselid(regs, 0);
    505   1.1   chopps 	SET_SBIC_syn(regs, 0);     /* asynch for now */
    506   1.1   chopps 
    507   1.1   chopps 	/*
    508   1.1   chopps 	 * anything else was zeroed by reset
    509   1.1   chopps 	 */
    510   1.1   chopps 	splx(s);
    511   1.1   chopps 
    512   1.1   chopps 	dev->sc_flags |= SBICF_ALIVE;
    513   1.1   chopps 	dev->sc_flags &= ~SBICF_SELECTED;
    514   1.1   chopps }
    515   1.1   chopps 
    516   1.1   chopps void
    517   1.1   chopps sbicerror(dev, regs, csr)
    518   1.1   chopps 	struct sbic_softc *dev;
    519   1.1   chopps 	sbic_regmap_p regs;
    520   1.1   chopps 	u_char csr;
    521   1.1   chopps {
    522   1.1   chopps 	struct scsi_xfer *xs;
    523   1.1   chopps 
    524   1.1   chopps 	xs = dev->sc_xs;
    525   1.1   chopps 
    526   1.1   chopps #ifdef DIAGNOSTIC
    527   1.1   chopps 	if (xs == NULL)
    528   1.1   chopps 		panic("sbicerror");
    529   1.1   chopps #endif
    530   1.1   chopps 	if (xs->flags & SCSI_SILENT)
    531   1.1   chopps 		return;
    532   1.1   chopps 
    533   1.1   chopps 	printf("%s: ", dev->sc_dev.dv_xname);
    534   1.1   chopps 	printf("csr == 0x%02i\n", csr);	/* XXX */
    535   1.1   chopps }
    536   1.1   chopps 
    537   1.1   chopps /*
    538   1.1   chopps  * select the bus, return when selected or error.
    539   1.1   chopps  */
    540   1.1   chopps int
    541   1.7   chopps sbicselectbus(dev, regs, target, lun, our_addr)
    542   1.1   chopps         struct sbic_softc *dev;
    543   1.1   chopps 	sbic_regmap_p regs;
    544   1.7   chopps 	u_char target, lun, our_addr;
    545   1.1   chopps {
    546   1.1   chopps 	u_char asr, csr, id;
    547   1.1   chopps 
    548   1.1   chopps 	QPRINTF(("sbicselectbus %d\n", target));
    549   1.1   chopps 
    550  1.13  mycroft 	/*
    551   1.1   chopps 	 * if we're already selected, return (XXXX panic maybe?)
    552   1.1   chopps 	 */
    553   1.1   chopps 	if (dev->sc_flags & SBICF_SELECTED)
    554   1.1   chopps 		return(1);
    555   1.1   chopps 
    556   1.1   chopps 	/*
    557   1.1   chopps 	 * issue select
    558   1.1   chopps 	 */
    559   1.1   chopps 	SBIC_TC_PUT(regs, 0);
    560   1.1   chopps 	SET_SBIC_selid(regs, target);
    561   1.1   chopps 	SET_SBIC_timeo(regs, SBIC_TIMEOUT(250,dev->sc_clkfreq));
    562   1.1   chopps 
    563   1.1   chopps 	/*
    564   1.1   chopps 	 * set sync or async
    565   1.1   chopps 	 */
    566   1.1   chopps 	if (dev->sc_sync[target].state == SYNC_DONE)
    567  1.13  mycroft 		SET_SBIC_syn(regs, SBIC_SYN (dev->sc_sync[target].offset,
    568   1.1   chopps 		    dev->sc_sync[target].period));
    569   1.1   chopps 	else
    570   1.1   chopps 		SET_SBIC_syn(regs, SBIC_SYN (0, sbic_min_period));
    571  1.13  mycroft 
    572   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN);
    573   1.1   chopps 
    574   1.1   chopps 	/*
    575   1.1   chopps 	 * wait for select (merged from seperate function may need
    576   1.1   chopps 	 * cleanup)
    577   1.1   chopps 	 */
    578   1.1   chopps 	WAIT_CIP(regs);
    579   1.1   chopps 	do {
    580   1.1   chopps 		SBIC_WAIT(regs, SBIC_ASR_INT, 0);
    581   1.1   chopps 		GET_SBIC_csr (regs, csr);
    582   1.1   chopps 		QPRINTF(("%02x ", csr));
    583   1.1   chopps 	} while (csr != (SBIC_CSR_MIS_2|MESG_OUT_PHASE)
    584   1.1   chopps 	    && csr != (SBIC_CSR_MIS_2|CMD_PHASE) && csr != SBIC_CSR_SEL_TIMEO);
    585   1.1   chopps 
    586   1.1   chopps 	if (csr == (SBIC_CSR_MIS_2|CMD_PHASE))
    587   1.1   chopps 		dev->sc_flags |= SBICF_SELECTED;	/* device ignored ATN */
    588   1.1   chopps 	else if (csr == (SBIC_CSR_MIS_2|MESG_OUT_PHASE)) {
    589   1.1   chopps 		/*
    590   1.1   chopps 		 * Send identify message
    591   1.1   chopps 		 * (SCSI-2 requires an identify msg (?))
    592   1.1   chopps 		 */
    593   1.1   chopps 		GET_SBIC_selid(regs, id);
    594   1.1   chopps 
    595  1.13  mycroft 		/*
    596  1.13  mycroft 		 * handle drives that don't want to be asked
    597   1.1   chopps 		 * whether to go sync at all.
    598   1.1   chopps 		 */
    599   1.1   chopps 		if (sbic_inhibit_sync && dev->sc_sync[id].state == SYNC_START) {
    600   1.1   chopps #ifdef DEBUG
    601   1.1   chopps 			if (sync_debug)
    602   1.1   chopps 				printf("Forcing target %d asynchronous.\n", id);
    603   1.1   chopps #endif
    604   1.1   chopps 			dev->sc_sync[id].offset = 0;
    605   1.1   chopps 			dev->sc_sync[id].period = sbic_min_period;
    606   1.1   chopps 			dev->sc_sync[id].state = SYNC_DONE;
    607   1.1   chopps 		}
    608  1.13  mycroft 
    609   1.1   chopps 
    610   1.1   chopps 		if (dev->sc_sync[id].state != SYNC_START)
    611   1.7   chopps 			SEND_BYTE (regs, MSG_IDENTIFY | lun);
    612   1.1   chopps 		else {
    613   1.1   chopps 			/*
    614   1.1   chopps 			 * try to initiate a sync transfer.
    615  1.13  mycroft 			 * So compose the sync message we're going
    616   1.1   chopps 			 * to send to the target
    617   1.1   chopps 			 */
    618   1.1   chopps 
    619   1.1   chopps #ifdef DEBUG
    620   1.1   chopps 			if (sync_debug)
    621   1.1   chopps 				printf("Sending sync request to target %d ... ",
    622   1.1   chopps 				    id);
    623   1.1   chopps #endif
    624   1.1   chopps 			/*
    625   1.1   chopps 			 * setup scsi message sync message request
    626   1.1   chopps 			 */
    627   1.7   chopps 			dev->sc_msg[0] = MSG_IDENTIFY | lun;
    628   1.1   chopps 			dev->sc_msg[1] = MSG_EXT_MESSAGE;
    629   1.1   chopps 			dev->sc_msg[2] = 3;
    630   1.1   chopps 			dev->sc_msg[3] = MSG_SYNC_REQ;
    631   1.1   chopps 			dev->sc_msg[4] = sbictoscsiperiod(dev, regs,
    632   1.1   chopps 			    sbic_min_period);
    633   1.1   chopps 			dev->sc_msg[5] = sbic_max_offset;
    634   1.1   chopps 
    635   1.1   chopps 			if (sbicxfstart(regs, 6, MESG_OUT_PHASE, sbic_cmd_wait))
    636   1.1   chopps 				sbicxfout(regs, 6, dev->sc_msg, MESG_OUT_PHASE);
    637   1.1   chopps 
    638   1.1   chopps 			dev->sc_sync[id].state = SYNC_SENT;
    639   1.1   chopps #ifdef DEBUG
    640   1.1   chopps 			if (sync_debug)
    641   1.1   chopps 				printf ("sent\n");
    642   1.1   chopps #endif
    643   1.1   chopps 		}
    644   1.1   chopps 
    645   1.1   chopps 		SBIC_WAIT (regs, SBIC_ASR_INT, 0);
    646   1.1   chopps 		GET_SBIC_csr (regs, csr);
    647   1.1   chopps 		QPRINTF(("[%02x]", csr));
    648   1.1   chopps #ifdef DEBUG
    649   1.1   chopps 		if (sync_debug && dev->sc_sync[id].state == SYNC_SENT)
    650   1.1   chopps 			printf("csr-result of last msgout: 0x%x\n", csr);
    651   1.1   chopps #endif
    652   1.1   chopps 
    653   1.1   chopps 		if (csr != SBIC_CSR_SEL_TIMEO)
    654   1.1   chopps 			dev->sc_flags |= SBICF_SELECTED;
    655   1.1   chopps 	}
    656  1.13  mycroft 
    657   1.1   chopps 	QPRINTF(("\n"));
    658   1.1   chopps 
    659   1.1   chopps 	return(csr == SBIC_CSR_SEL_TIMEO);
    660   1.1   chopps }
    661   1.1   chopps 
    662   1.1   chopps int
    663   1.1   chopps sbicxfstart(regs, len, phase, wait)
    664   1.1   chopps 	sbic_regmap_p regs;
    665   1.1   chopps 	int len, wait;
    666   1.1   chopps 	u_char phase;
    667   1.1   chopps {
    668   1.1   chopps 	u_char id;
    669   1.1   chopps 
    670   1.1   chopps 	if (phase == DATA_IN_PHASE || phase == MESG_IN_PHASE) {
    671   1.1   chopps 		GET_SBIC_selid (regs, id);
    672   1.1   chopps 		id |= SBIC_SID_FROM_SCSI;
    673   1.1   chopps 		SET_SBIC_selid (regs, id);
    674   1.1   chopps 		SBIC_TC_PUT (regs, (unsigned)len);
    675  1.13  mycroft 	} else if (phase == DATA_OUT_PHASE || phase == MESG_OUT_PHASE
    676   1.1   chopps 	    || phase == CMD_PHASE)
    677   1.1   chopps 		SBIC_TC_PUT (regs, (unsigned)len);
    678   1.1   chopps 	else
    679   1.1   chopps 		SBIC_TC_PUT (regs, 0);
    680   1.1   chopps 	QPRINTF(("sbicxfstart %d, %d, %d\n", len, phase, wait));
    681   1.1   chopps 
    682   1.1   chopps 	return(1);
    683   1.1   chopps }
    684   1.1   chopps 
    685   1.1   chopps int
    686   1.1   chopps sbicxfout(regs, len, bp, phase)
    687   1.1   chopps 	sbic_regmap_p regs;
    688   1.1   chopps 	int len;
    689   1.1   chopps 	void *bp;
    690   1.1   chopps 	int phase;
    691   1.1   chopps {
    692   1.1   chopps 	u_char orig_csr, csr, asr, *buf;
    693   1.1   chopps 	int wait;
    694  1.13  mycroft 
    695   1.1   chopps 	buf = bp;
    696   1.1   chopps 	wait = sbic_data_wait;
    697   1.1   chopps 
    698   1.1   chopps 	QPRINTF(("sbicxfout {%d} %02x %02x %02x %02x %02x "
    699  1.13  mycroft 	    "%02x %02x %02x %02x %02x\n", len, buf[0], buf[1], buf[2],
    700   1.1   chopps 	    buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]));
    701   1.1   chopps 
    702   1.1   chopps 	GET_SBIC_csr (regs, orig_csr);
    703   1.1   chopps 
    704  1.13  mycroft 	/*
    705   1.1   chopps 	 * sigh.. WD-PROTO strikes again.. sending the command in one go
    706   1.1   chopps 	 * causes the chip to lock up if talking to certain (misbehaving?)
    707   1.1   chopps 	 * targets. Anyway, this procedure should work for all targets, but
    708   1.1   chopps 	 * it's slightly slower due to the overhead
    709   1.1   chopps 	 */
    710   1.1   chopps 	WAIT_CIP (regs);
    711   1.1   chopps 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
    712   1.1   chopps 	for (;len > 0; len--) {
    713   1.1   chopps 		GET_SBIC_asr (regs, asr);
    714   1.1   chopps 		while ((asr & SBIC_ASR_DBR) == 0) {
    715   1.1   chopps 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
    716   1.1   chopps #ifdef DEBUG
    717   1.1   chopps 				if (sbic_debug)
    718   1.1   chopps 					printf("sbicxfout fail: l%d i%x w%d\n",
    719   1.1   chopps 					    len, asr, wait);
    720   1.1   chopps #endif
    721   1.1   chopps 				return (len);
    722   1.1   chopps 			}
    723   1.1   chopps 			DELAY(1);
    724   1.1   chopps 			GET_SBIC_asr (regs, asr);
    725   1.1   chopps 		}
    726   1.1   chopps 
    727   1.1   chopps 		SET_SBIC_data (regs, *buf);
    728   1.1   chopps 		buf++;
    729   1.1   chopps 	}
    730   1.1   chopps 
    731   1.1   chopps 	QPRINTF(("sbicxfout done\n"));
    732   1.1   chopps 	/*
    733   1.1   chopps 	 * this leaves with one csr to be read
    734   1.1   chopps 	 */
    735   1.1   chopps 	return(0);
    736   1.1   chopps }
    737   1.1   chopps 
    738   1.1   chopps void
    739   1.1   chopps sbicxfin(regs, len, bp)
    740   1.1   chopps 	sbic_regmap_p regs;
    741   1.1   chopps 	int len;
    742   1.1   chopps 	void *bp;
    743   1.1   chopps {
    744   1.1   chopps 	int wait;
    745   1.1   chopps 	u_char *obp, *buf;
    746   1.1   chopps 	u_char orig_csr, csr, asr;
    747  1.13  mycroft 
    748   1.1   chopps 	wait = sbic_data_wait;
    749   1.1   chopps 	obp = bp;
    750   1.1   chopps 	buf = bp;
    751   1.1   chopps 
    752   1.1   chopps 	GET_SBIC_csr (regs, orig_csr);
    753   1.1   chopps 
    754   1.1   chopps 	QPRINTF(("sbicxfin %d, csr=%02x\n", len, orig_csr));
    755   1.1   chopps 
    756   1.1   chopps 	WAIT_CIP (regs);
    757   1.1   chopps 	SET_SBIC_cmd (regs, SBIC_CMD_XFER_INFO);
    758   1.1   chopps 	for (;len > 0; len--) {
    759   1.1   chopps 		GET_SBIC_asr (regs, asr);
    760   1.1   chopps 		while ((asr & SBIC_ASR_DBR) == 0) {
    761   1.1   chopps 			if ((asr & SBIC_ASR_INT) || --wait < 0) {
    762   1.1   chopps #ifdef DEBUG
    763   1.1   chopps 				if (sbic_debug)
    764   1.1   chopps 					printf("sbicxfin fail: l%d i%x w%d\n",
    765   1.1   chopps 					    len, asr, wait);
    766   1.1   chopps #endif
    767   1.1   chopps 				return;
    768   1.1   chopps 			}
    769   1.1   chopps 
    770   1.1   chopps 			DELAY(1);
    771   1.1   chopps 			GET_SBIC_asr (regs, asr);
    772   1.1   chopps 		}
    773   1.1   chopps 
    774   1.1   chopps 		GET_SBIC_data (regs, *buf);
    775   1.1   chopps 		buf++;
    776   1.1   chopps 	}
    777   1.1   chopps 
    778   1.1   chopps 	QPRINTF(("sbicxfin {%d} %02x %02x %02x %02x %02x %02x "
    779  1.13  mycroft 	    "%02x %02x %02x %02x\n", len, obp[0], obp[1], obp[2],
    780   1.1   chopps 	    obp[3], obp[4], obp[5], obp[6], obp[7], obp[8], obp[9]));
    781   1.1   chopps 
    782   1.1   chopps 	/* this leaves with one csr to be read */
    783   1.1   chopps }
    784   1.1   chopps 
    785   1.1   chopps 
    786   1.1   chopps /*
    787   1.1   chopps  * SCSI 'immediate' command:  issue a command to some SCSI device
    788   1.1   chopps  * and get back an 'immediate' response (i.e., do programmed xfer
    789   1.1   chopps  * to get the response data).  'cbuf' is a buffer containing a scsi
    790   1.1   chopps  * command of length clen bytes.  'buf' is a buffer of length 'len'
    791   1.1   chopps  * bytes for data.  The transfer direction is determined by the device
    792   1.1   chopps  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
    793   1.1   chopps  * command must supply no data.  'xferphase' is the bus phase the
    794   1.1   chopps  * caller expects to happen after the command is issued.  It should
    795   1.1   chopps  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
    796   1.1   chopps  */
    797   1.1   chopps int
    798   1.7   chopps sbicicmd(dev, target, lun, cbuf, clen, buf, len, xferphase)
    799   1.1   chopps 	struct sbic_softc *dev;
    800   1.1   chopps 	void *cbuf, *buf;
    801   1.1   chopps 	int clen, len;
    802   1.1   chopps 	u_char xferphase;
    803   1.1   chopps {
    804   1.1   chopps 	sbic_regmap_p regs;
    805   1.1   chopps 	u_char phase, csr, asr;
    806   1.1   chopps 	int wait;
    807   1.1   chopps 
    808   1.1   chopps 	regs = dev->sc_sbicp;
    809   1.1   chopps 
    810  1.13  mycroft 	/*
    811   1.1   chopps 	 * set the sbic into non-DMA mode
    812   1.1   chopps 	 */
    813   1.1   chopps 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI);
    814   1.1   chopps 
    815   1.1   chopps retry_selection:
    816   1.1   chopps 	/*
    817   1.1   chopps 	 * select the SCSI bus (it's an error if bus isn't free)
    818   1.1   chopps 	 */
    819   1.7   chopps 	if (sbicselectbus(dev, regs, target, lun, dev->sc_scsiaddr))
    820   1.1   chopps 		return(-1);
    821  1.13  mycroft 	/*
    822  1.13  mycroft 	 * Wait for a phase change (or error) then let the device sequence
    823   1.1   chopps 	 * us through the various SCSI phases.
    824   1.1   chopps 	 */
    825   1.1   chopps 	dev->sc_stat[0] = 0xff;
    826   1.1   chopps 	dev->sc_msg[0] = 0xff;
    827   1.1   chopps 	phase = CMD_PHASE;
    828   1.1   chopps 
    829   1.1   chopps new_phase:
    830   1.1   chopps 	wait = sbic_cmd_wait;
    831   1.1   chopps 
    832   1.1   chopps 	GET_SBIC_csr (regs, csr);
    833   1.1   chopps 	QPRINTF((">CSR:%02x<", csr));
    834   1.1   chopps 
    835   1.1   chopps 	/*
    836   1.1   chopps 	 * requesting some new phase
    837   1.1   chopps 	 */
    838   1.1   chopps 	if ((csr != 0xff) && (csr & 0xf0) && (csr & 0x08))
    839   1.1   chopps 		phase = csr & PHASE;
    840   1.1   chopps 	else if ((csr == SBIC_CSR_DISC) || (csr == SBIC_CSR_DISC_1)
    841   1.1   chopps 	    || (csr == SBIC_CSR_S_XFERRED)) {
    842   1.1   chopps 		dev->sc_flags &= ~SBICF_SELECTED;
    843   1.1   chopps 		GET_SBIC_cmd_phase (regs, phase);
    844   1.1   chopps 		if (phase == 0x60)
    845   1.1   chopps 			GET_SBIC_tlun (regs, dev->sc_stat[0]);
    846   1.1   chopps 		else
    847   1.1   chopps 			return(-1);
    848   1.1   chopps 		goto out;
    849   1.1   chopps 	} else {
    850   1.1   chopps 		sbicerror(dev, regs, csr);
    851   1.1   chopps 		goto abort;
    852   1.1   chopps 	}
    853   1.1   chopps 
    854   1.1   chopps 	switch (phase) {
    855   1.1   chopps 	case CMD_PHASE:
    856   1.1   chopps 		if (sbicxfstart (regs, clen, phase, wait))
    857   1.1   chopps 			if (sbicxfout (regs, clen, cbuf, phase))
    858   1.1   chopps 				goto abort;
    859   1.1   chopps 		phase = xferphase;
    860   1.1   chopps 		break;
    861   1.1   chopps 	case DATA_IN_PHASE:
    862   1.1   chopps 		if (len <= 0)
    863   1.1   chopps 			goto abort;
    864   1.1   chopps 		wait = sbic_data_wait;
    865   1.1   chopps 		if (sbicxfstart(regs, len, phase, wait))
    866   1.1   chopps 			sbicxfin(regs, len, buf);
    867   1.1   chopps 		phase = STATUS_PHASE;
    868   1.1   chopps 		break;
    869   1.1   chopps 	case MESG_IN_PHASE:
    870   1.1   chopps 		if (sbicxfstart(regs, sizeof(dev->sc_msg), phase, wait) == 0)
    871   1.1   chopps 			break;
    872   1.1   chopps 		dev->sc_msg[0] = 0xff;
    873   1.1   chopps 		sbicxfin(regs, sizeof(dev->sc_msg), dev->sc_msg);
    874   1.1   chopps 		/*
    875  1.13  mycroft 		 * get the command completion interrupt, or we
    876   1.1   chopps 		 * can't send a new command (LCI)
    877   1.1   chopps 		 */
    878   1.1   chopps 		SBIC_WAIT(regs, SBIC_ASR_INT, wait);
    879   1.1   chopps 		GET_SBIC_csr(regs, csr);
    880   1.1   chopps #ifdef DEBUG
    881   1.1   chopps 		if (sync_debug)
    882   1.1   chopps 			printf("msgin done csr 0x%x\n", csr);
    883   1.1   chopps #endif
    884   1.1   chopps 		/*
    885   1.1   chopps 		 * test whether this is a reply to our sync
    886   1.1   chopps 		 * request
    887   1.1   chopps 		 */
    888   1.1   chopps 		if (dev->sc_msg[0] == MSG_EXT_MESSAGE && dev->sc_msg[1] == 3
    889   1.1   chopps 		    && dev->sc_msg[2] == MSG_SYNC_REQ) {
    890   1.1   chopps 
    891   1.1   chopps 			dev->sc_sync[target].period = sbicfromscsiperiod(dev,
    892   1.1   chopps 			    regs, dev->sc_msg[3]);
    893   1.1   chopps 			dev->sc_sync[target].offset = dev->sc_msg[4];
    894   1.1   chopps 			dev->sc_sync[target].state = SYNC_DONE;
    895   1.1   chopps 			SET_SBIC_syn(regs, SBIC_SYN(dev->sc_sync[target].offset,
    896   1.1   chopps 			    dev->sc_sync[target].period));
    897   1.1   chopps 			/* ACK the message */
    898   1.1   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    899   1.1   chopps 			WAIT_CIP(regs);
    900   1.1   chopps 			phase = CMD_PHASE;  /* or whatever */
    901   1.1   chopps 			printf("%s: target %d now synchronous,"
    902   1.1   chopps 			    " period=%dns, offset=%d.\n",
    903   1.1   chopps 			    dev->sc_dev.dv_xname, target, dev->sc_msg[3] * 4,
    904   1.1   chopps 			    dev->sc_msg[4]);
    905   1.1   chopps 		} else if (dev->sc_msg[0] == MSG_REJECT
    906   1.1   chopps 		    && dev->sc_sync[target].state == SYNC_SENT) {
    907   1.1   chopps #ifdef DEBUG
    908   1.1   chopps 			if (sync_debug)
    909   1.1   chopps 				printf("target %d rejected sync, going async\n",
    910   1.1   chopps 				    target);
    911   1.1   chopps #endif
    912   1.1   chopps 			dev->sc_sync[target].period = sbic_min_period;
    913   1.1   chopps 			dev->sc_sync[target].offset = 0;
    914   1.1   chopps 			dev->sc_sync[target].state = SYNC_DONE;
    915   1.1   chopps 			SET_SBIC_syn(regs, SBIC_SYN(dev->sc_sync[target].offset,
    916   1.1   chopps 			    dev->sc_sync[target].period));
    917   1.1   chopps 			/* ACK the message */
    918   1.1   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    919   1.1   chopps 			WAIT_CIP(regs);
    920   1.1   chopps 			phase = CMD_PHASE;  /* or whatever */
    921   1.1   chopps 		} else if (dev->sc_msg[0] == MSG_REJECT) {
    922   1.1   chopps 			/*
    923   1.1   chopps 			 * we'll never REJECt a REJECT message..
    924   1.1   chopps 			 */
    925   1.1   chopps 			/* ACK the message */
    926   1.1   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    927   1.1   chopps 			WAIT_CIP(regs);
    928   1.1   chopps 			phase = CMD_PHASE;  /* or whatever */
    929   1.9   chopps 		} else if (dev->sc_msg[0] == MSG_CMD_COMPLETE
    930   1.9   chopps 		    || dev->sc_msg[0] == 0xff) {
    931  1.13  mycroft 			/* !! KLUDGE ALERT !! quite a few drives don't seem to
    932   1.1   chopps 			 * really like the current way of sending the
    933  1.13  mycroft 			 * sync-handshake together with the ident-message, and
    934   1.1   chopps 			 * they react by sending command-complete and
    935   1.1   chopps 			 * disconnecting right after returning the valid sync
    936   1.1   chopps 			 * handshake. So, all I can do is reselect the drive,
    937   1.1   chopps 			 * and hope it won't disconnect again. I don't think
    938   1.1   chopps 			 * this is valid behavior, but I can't help fixing a
    939   1.1   chopps 			 * problem that apparently exists.
    940  1.13  mycroft 			 *
    941   1.1   chopps 			 * Note: we should not get here on `normal' command
    942   1.1   chopps 			 * completion, as that condition is handled by the
    943   1.1   chopps 			 * high-level sel&xfer resume command used to walk
    944   1.1   chopps 			 * thru status/cc-phase.
    945   1.1   chopps 			 */
    946   1.1   chopps 
    947   1.1   chopps #ifdef DEBUG
    948   1.1   chopps 			if (sync_debug)
    949   1.1   chopps 				printf ("GOT CMD-COMPLETE! %d acting weird.."
    950   1.1   chopps 				    " waiting for disconnect...\n", target);
    951   1.1   chopps #endif
    952   1.1   chopps 			/* ACK the message */
    953   1.1   chopps 			SET_SBIC_cmd (regs, SBIC_CMD_CLR_ACK);
    954   1.1   chopps 			WAIT_CIP(regs);
    955   1.1   chopps 
    956   1.1   chopps 			/* wait for disconnect */
    957  1.13  mycroft 			while (csr != SBIC_CSR_DISC &&
    958   1.1   chopps 			    csr != SBIC_CSR_DISC_1) {
    959   1.1   chopps 				DELAY(1);
    960   1.1   chopps 				GET_SBIC_csr(regs, csr);
    961   1.1   chopps 			}
    962   1.1   chopps #ifdef DEBUG
    963   1.1   chopps 			if (sync_debug)
    964   1.1   chopps 				printf ("ok.\nRetrying selection.\n");
    965   1.1   chopps #endif
    966   1.1   chopps 			dev->sc_flags &= ~SBICF_SELECTED;
    967   1.1   chopps 			goto retry_selection;
    968   1.1   chopps 		} else {
    969   1.1   chopps #ifdef DEBUG
    970   1.1   chopps 			if (sbic_debug || sync_debug)
    971   1.1   chopps 				printf ("Rejecting message 0x%02x\n",
    972   1.1   chopps 				    dev->sc_msg[0]);
    973   1.1   chopps #endif
    974   1.1   chopps 			/* prepare to reject the message, NACK */
    975   1.1   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
    976   1.1   chopps 			WAIT_CIP(regs);
    977   1.1   chopps 			SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
    978   1.1   chopps 			WAIT_CIP(regs);
    979   1.1   chopps 			phase = MESG_OUT_PHASE;
    980   1.1   chopps 		}
    981   1.1   chopps 		break;
    982   1.1   chopps 
    983   1.1   chopps 	case MESG_OUT_PHASE:
    984   1.1   chopps #ifdef DEBUG
    985   1.1   chopps 		if (sync_debug)
    986   1.1   chopps 			printf ("sending REJECT msg to last msg.\n");
    987   1.1   chopps #endif
    988   1.1   chopps 		/*
    989  1.13  mycroft 		 * should only get here on reject,
    990   1.1   chopps 		 * since it's always US that
    991   1.1   chopps 		 * initiate a sync transfer
    992   1.1   chopps 		 */
    993   1.1   chopps 		SEND_BYTE(regs, MSG_REJECT);
    994   1.1   chopps 		phase = STATUS_PHASE;
    995   1.1   chopps 		break;
    996   1.1   chopps 	case DATA_OUT_PHASE:
    997   1.1   chopps 		if (len <= 0)
    998   1.1   chopps 			goto abort;
    999   1.1   chopps 		wait = sbic_data_wait;
   1000   1.1   chopps 		if (sbicxfstart(regs, len, phase, wait))
   1001   1.1   chopps 			if (sbicxfout (regs, len, buf, phase))
   1002   1.1   chopps 				goto abort;
   1003   1.1   chopps 		phase = STATUS_PHASE;
   1004   1.1   chopps 		break;
   1005   1.1   chopps 	case STATUS_PHASE:
   1006  1.13  mycroft 		/*
   1007   1.1   chopps 		 * the sbic does the status/cmd-complete reading ok,
   1008   1.1   chopps 		 * so do this with its hi-level commands.
   1009   1.1   chopps 		 */
   1010   1.1   chopps 		SBIC_TC_PUT(regs, 0);
   1011   1.1   chopps 		SET_SBIC_cmd_phase(regs, 0x46);
   1012   1.1   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1013   1.1   chopps 		phase = BUS_FREE_PHASE;
   1014   1.1   chopps 		break;
   1015   1.1   chopps 	case BUS_FREE_PHASE:
   1016   1.1   chopps 		goto out;
   1017   1.1   chopps 	default:
   1018   1.1   chopps 		printf("%s: unexpected phase %d in icmd from %d\n",
   1019   1.1   chopps 		    dev->sc_dev.dv_xname, phase, target);
   1020   1.1   chopps 		goto abort;
   1021   1.1   chopps 	}
   1022   1.1   chopps 
   1023   1.1   chopps 	/*
   1024  1.13  mycroft 	 * make sure the last command was taken,
   1025   1.1   chopps 	 * ie. we're not hunting after an ignored command..
   1026   1.1   chopps 	 */
   1027   1.1   chopps 	GET_SBIC_asr(regs, asr);
   1028   1.1   chopps 	if (asr & SBIC_ASR_LCI)
   1029   1.1   chopps 		goto abort;
   1030   1.1   chopps 
   1031   1.1   chopps 	/* tapes may take a loooong time.. */
   1032   1.1   chopps 	while (asr & SBIC_ASR_BSY) {
   1033   1.1   chopps 		DELAY(1);
   1034   1.1   chopps 		GET_SBIC_asr(regs, asr);
   1035   1.1   chopps 	}
   1036   1.1   chopps 
   1037  1.13  mycroft 	/*
   1038   1.1   chopps 	 * wait for last command to complete
   1039   1.1   chopps 	 */
   1040   1.1   chopps 	SBIC_WAIT (regs, SBIC_ASR_INT, wait);
   1041   1.1   chopps 
   1042   1.1   chopps 	/*
   1043   1.1   chopps 	 * do it again
   1044   1.1   chopps 	 */
   1045   1.1   chopps 	goto new_phase;
   1046   1.1   chopps abort:
   1047   1.1   chopps 	sbicabort(dev, regs, "icmd");
   1048   1.1   chopps out:
   1049   1.1   chopps 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
   1050   1.1   chopps 	return(dev->sc_stat[0]);
   1051   1.1   chopps }
   1052   1.1   chopps 
   1053   1.1   chopps /*
   1054   1.1   chopps  * Finish SCSI xfer command:  After the completion interrupt from
   1055   1.1   chopps  * a read/write operation, sequence through the final phases in
   1056   1.1   chopps  * programmed i/o.  This routine is a lot like sbicicmd except we
   1057   1.1   chopps  * skip (and don't allow) the select, cmd out and data in/out phases.
   1058   1.1   chopps  */
   1059   1.1   chopps void
   1060   1.1   chopps sbicxfdone(dev, regs, target)
   1061   1.1   chopps 	struct sbic_softc *dev;
   1062   1.1   chopps 	sbic_regmap_p regs;
   1063   1.1   chopps 	int target;
   1064   1.1   chopps {
   1065   1.1   chopps 	u_char phase, csr;
   1066   1.1   chopps 	int s;
   1067   1.1   chopps 
   1068   1.1   chopps 	QPRINTF(("{"));
   1069   1.1   chopps 	s = splbio();
   1070   1.1   chopps 
   1071   1.1   chopps 	/*
   1072   1.1   chopps 	 * have the sbic complete on its own
   1073   1.1   chopps 	 */
   1074   1.1   chopps 	SBIC_TC_PUT(regs, 0);
   1075   1.1   chopps 	SET_SBIC_cmd_phase(regs, 0x46);
   1076   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_SEL_ATN_XFER);
   1077   1.1   chopps 
   1078   1.1   chopps 	do {
   1079   1.1   chopps 		SBIC_WAIT (regs, SBIC_ASR_INT, 0);
   1080   1.1   chopps 		GET_SBIC_csr (regs, csr);
   1081   1.1   chopps 		QPRINTF(("%02x:", csr));
   1082   1.1   chopps 	} while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
   1083   1.1   chopps 	    && (csr != SBIC_CSR_S_XFERRED));
   1084   1.1   chopps 
   1085   1.1   chopps 	dev->sc_flags &= ~SBICF_SELECTED;
   1086   1.1   chopps 
   1087   1.1   chopps 	GET_SBIC_cmd_phase (regs, phase);
   1088   1.1   chopps 	QPRINTF(("}%02x", phase));
   1089   1.1   chopps 	if (phase == 0x60)
   1090   1.1   chopps 		GET_SBIC_tlun(regs, dev->sc_stat[0]);
   1091   1.1   chopps 	else
   1092   1.1   chopps 		sbicerror(dev, regs, csr);
   1093   1.1   chopps 
   1094   1.1   chopps 	QPRINTF(("=STS:%02x=\n", dev->sc_stat[0]));
   1095   1.1   chopps 	splx(s);
   1096   1.1   chopps }
   1097   1.1   chopps 
   1098   1.1   chopps int
   1099   1.1   chopps sbicgo(dev, xs)
   1100   1.1   chopps 	struct sbic_softc *dev;
   1101   1.1   chopps 	struct scsi_xfer *xs;
   1102   1.1   chopps {
   1103   1.3   chopps 	int i, dmaflags, count, tcount, target, len, wait;
   1104   1.3   chopps 	u_char phase, csr, asr, cmd, *addr, *tmpaddr;
   1105   1.1   chopps 	sbic_regmap_p regs;
   1106   1.1   chopps 	struct dma_chain *dcp;
   1107   1.3   chopps 	u_int deoff, dspa;
   1108   1.1   chopps 	char *dmaend;
   1109   1.1   chopps 
   1110   1.1   chopps 	target = xs->sc_link->target;
   1111   1.1   chopps 	count = xs->datalen;
   1112   1.1   chopps 	addr = xs->data;
   1113   1.1   chopps 
   1114   1.1   chopps 	regs = dev->sc_sbicp;
   1115   1.1   chopps 	dmaend = NULL;
   1116   1.1   chopps 
   1117   1.1   chopps 	/*
   1118   1.1   chopps 	 * set the sbic into DMA mode
   1119   1.1   chopps 	 */
   1120   1.1   chopps 	SET_SBIC_control(regs, SBIC_CTL_EDI | SBIC_CTL_IDI |
   1121   1.1   chopps 	    SBIC_MACHINE_DMA_MODE);
   1122   1.1   chopps 
   1123   1.1   chopps 	/*
   1124   1.1   chopps 	 * select the SCSI bus (it's an error if bus isn't free)
   1125   1.1   chopps 	 */
   1126   1.7   chopps 	if (sbicselectbus(dev, regs, target, xs->sc_link->lun,
   1127   1.7   chopps 	    dev->sc_scsiaddr)) {
   1128   1.1   chopps 		dev->sc_dmafree(dev);
   1129   1.1   chopps 		return(-1);
   1130   1.1   chopps 	}
   1131   1.1   chopps 
   1132   1.1   chopps 	/*
   1133   1.1   chopps 	 * Wait for a phase change (or error) then let the device
   1134   1.1   chopps 	 * sequence us through command phase (we may have to take
   1135   1.1   chopps 	 * a msg in/out before doing the command).  If the disk has
   1136   1.1   chopps 	 * to do a seek, it may be a long time until we get a change
   1137   1.1   chopps 	 * to data phase so, in the absense of an explicit phase
   1138   1.1   chopps 	 * change, we assume data phase will be coming up and tell
   1139   1.1   chopps 	 * the SPC to start a transfer whenever it does.  We'll get
   1140   1.1   chopps 	 * a service required interrupt later if this assumption is
   1141   1.1   chopps 	 * wrong.  Otherwise we'll get a service required int when
   1142   1.1   chopps 	 * the transfer changes to status phase.
   1143   1.1   chopps 	 */
   1144   1.1   chopps 	phase = CMD_PHASE;
   1145   1.1   chopps 
   1146   1.1   chopps new_phase:
   1147   1.1   chopps 	wait = sbic_cmd_wait;
   1148   1.1   chopps 	switch (phase) {
   1149   1.1   chopps 	case CMD_PHASE:
   1150   1.1   chopps 		if (sbicxfstart(regs, xs->cmdlen, phase, wait))
   1151   1.1   chopps 			if (sbicxfout(regs, xs->cmdlen, xs->cmd, phase))
   1152   1.1   chopps 				goto abort;
   1153   1.1   chopps 		break;
   1154   1.1   chopps 	case MESG_IN_PHASE:
   1155   1.1   chopps 		if (sbicxfstart(regs, sizeof(dev->sc_msg), phase, wait) == 0)
   1156   1.1   chopps 			break;
   1157   1.1   chopps 
   1158   1.1   chopps 		sbicxfin(regs, sizeof(dev->sc_msg), dev->sc_msg);
   1159   1.1   chopps 		/*
   1160  1.13  mycroft 		 * prepare to reject any mesgin,
   1161   1.1   chopps 		 * no matter what it might be..
   1162   1.1   chopps 		 */
   1163   1.1   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_SET_ATN);
   1164   1.1   chopps 		WAIT_CIP(regs);
   1165   1.1   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_CLR_ACK);
   1166   1.1   chopps 		phase = MESG_OUT_PHASE;
   1167   1.1   chopps 		break;
   1168   1.1   chopps 	case MESG_OUT_PHASE:
   1169   1.1   chopps 		SEND_BYTE(regs, MSG_REJECT);
   1170   1.1   chopps 		phase = STATUS_PHASE;
   1171   1.1   chopps 		break;
   1172   1.1   chopps 	case DATA_IN_PHASE:
   1173   1.1   chopps 	case DATA_OUT_PHASE:
   1174   1.1   chopps 		goto out;
   1175   1.1   chopps 	/*
   1176   1.1   chopps 	 * status phase can happen, if the issued read/write command
   1177  1.13  mycroft 	 * is illegal (for example, reading after EOT on tape) and the
   1178  1.13  mycroft 	 * device doesn't even go to data in/out phase. So handle this
   1179   1.1   chopps 	 * here normally, instead of going thru abort-handling.
   1180   1.1   chopps 	 */
   1181   1.1   chopps 	case STATUS_PHASE:
   1182   1.1   chopps 		dev->sc_dmafree(dev);
   1183   1.1   chopps 		sbicxfdone(dev, regs, target);
   1184   1.1   chopps 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_BBUF);
   1185   1.1   chopps 		sbic_scsidone(dev, dev->sc_stat[0]);
   1186   1.1   chopps 		return(0);
   1187   1.1   chopps 	default:
   1188   1.1   chopps 		printf("%s: unexpected phase %d in go from %d\n", phase,
   1189   1.1   chopps 		    dev->sc_dev.dv_xname, target);
   1190   1.1   chopps 		goto abort;
   1191   1.1   chopps 	}
   1192   1.1   chopps 
   1193   1.1   chopps 	/*
   1194  1.13  mycroft 	 * make sure the last command was taken,
   1195   1.1   chopps 	 * ie. we're not hunting after an ignored command..
   1196   1.1   chopps 	 */
   1197   1.1   chopps 	GET_SBIC_asr(regs, asr);
   1198   1.1   chopps 	if (asr & SBIC_ASR_LCI)
   1199   1.1   chopps 		goto abort;
   1200   1.1   chopps 
   1201  1.13  mycroft 	/*
   1202   1.1   chopps 	 * tapes may take a loooong time..
   1203   1.1   chopps 	 */
   1204   1.1   chopps 	while (asr & SBIC_ASR_BSY) {
   1205   1.1   chopps 		DELAY(1);
   1206   1.1   chopps 		GET_SBIC_asr(regs, asr);
   1207   1.1   chopps 	}
   1208   1.1   chopps 
   1209  1.13  mycroft 	if (wait <= 0)
   1210   1.1   chopps 		goto abort;
   1211   1.1   chopps 
   1212   1.1   chopps 	/*
   1213   1.1   chopps 	 * wait for last command to complete
   1214   1.1   chopps 	 */
   1215   1.1   chopps 	SBIC_WAIT(regs, SBIC_ASR_INT, wait);
   1216   1.1   chopps 
   1217   1.1   chopps 	GET_SBIC_csr(regs, csr);
   1218   1.1   chopps 	QPRINTF((">CSR:%02x<", csr));
   1219   1.1   chopps 
   1220  1.13  mycroft 	/*
   1221   1.1   chopps 	 * requesting some new phase
   1222   1.1   chopps 	 */
   1223   1.1   chopps 	if ((csr != 0xff) && (csr & 0xf0) && (csr & 0x08))
   1224   1.1   chopps 		phase = csr & PHASE;
   1225   1.1   chopps 	else {
   1226   1.1   chopps 		sbicerror(dev, regs, csr);
   1227   1.1   chopps 		goto abort;
   1228   1.1   chopps 	}
   1229   1.1   chopps 	/*
   1230   1.1   chopps 	 * start again with for new phase
   1231   1.1   chopps 	 */
   1232   1.1   chopps 	goto new_phase;
   1233   1.1   chopps out:
   1234   1.1   chopps 	dmaflags = 0;
   1235   1.1   chopps 	if (xs->flags & SCSI_DATA_IN)
   1236   1.1   chopps 		dmaflags |= DMAGO_READ;
   1237   1.1   chopps 
   1238   1.1   chopps 	if (count > MAXPHYS)
   1239   1.1   chopps 		printf("sbicgo: bp->b_bcount > MAXPHYS %08x\n", count);
   1240   1.1   chopps 
   1241  1.10   chopps #ifdef DEBUG
   1242  1.10   chopps 	++sbicdma_ops;			/* count total DMA operations */
   1243  1.10   chopps #endif
   1244  1.13  mycroft 	if (dev->sc_flags & SBICF_BADDMA &&
   1245   1.1   chopps 	    sbiccheckdmap(addr, count, dev->sc_dmamask)) {
   1246   1.1   chopps 		/*
   1247   1.1   chopps 		 * need to bounce the dma.
   1248   1.1   chopps 		 */
   1249   1.1   chopps 		if (dmaflags & DMAGO_READ) {
   1250   1.1   chopps 			dev->sc_flags |= SBICF_BBUF;
   1251   1.1   chopps 			dev->sc_dmausrbuf = addr;
   1252   1.1   chopps 			dev->sc_dmausrlen = count;
   1253   1.1   chopps 		} else {	/* write: copy to dma buffer */
   1254   1.1   chopps 			bcopy (addr, dev->sc_dmabuffer, count);
   1255   1.1   chopps 		}
   1256   1.1   chopps 		addr = dev->sc_dmabuffer;	/* and use dma buffer */
   1257  1.10   chopps #ifdef DEBUG
   1258  1.10   chopps 		++sbicdma_bounces;		/* count number of bounced */
   1259  1.10   chopps #endif
   1260   1.1   chopps 	}
   1261   1.3   chopps 	tmpaddr = addr;
   1262   1.3   chopps 	len = count;
   1263   1.1   chopps #ifdef DEBUG
   1264   1.1   chopps 	if (sbic_dma_debug & DDB_FOLLOW)
   1265   1.1   chopps 		printf("sbicgo(%d, %x, %x, %x)\n", dev->sc_dev.dv_unit,
   1266   1.1   chopps 		    addr, count, dmaflags);
   1267   1.1   chopps #endif
   1268   1.1   chopps 	/*
   1269   1.1   chopps 	 * Build the DMA chain
   1270   1.1   chopps 	 */
   1271   1.1   chopps 	for (dcp = dev->sc_chain; count > 0; dcp++) {
   1272   1.1   chopps 		dcp->dc_addr = (char *) kvtop(addr);
   1273   1.1   chopps 		if (count < (tcount = NBPG - ((int)addr & PGOFSET)))
   1274   1.1   chopps 			tcount = count;
   1275   1.1   chopps 		addr += tcount;
   1276   1.1   chopps 		count -= tcount;
   1277   1.1   chopps 		dcp->dc_count = tcount >> 1;
   1278   1.1   chopps 
   1279   1.1   chopps 		/*
   1280  1.13  mycroft 		 * check if contigous, if not mark new end
   1281   1.1   chopps 		 * else increment end and count on previous.
   1282   1.1   chopps 		 */
   1283  1.10   chopps 		if (dcp->dc_addr != dmaend) {
   1284   1.1   chopps 			dmaend = dcp->dc_addr + tcount;
   1285  1.10   chopps #ifdef DEBUG
   1286  1.10   chopps 			if (dcp != dev->sc_chain)
   1287  1.10   chopps 				++sbicdma_misses;	/* count non-contiguous */
   1288  1.10   chopps #endif
   1289  1.10   chopps 		} else {
   1290   1.1   chopps 			dcp--;
   1291   1.1   chopps 			dmaend += tcount;
   1292   1.1   chopps 			dcp->dc_count += tcount >> 1;
   1293  1.10   chopps #ifdef DEBUG
   1294  1.10   chopps 			++sbicdma_hits;	/* count contiguous */
   1295  1.10   chopps #endif
   1296   1.1   chopps 		}
   1297   1.1   chopps 	}
   1298   1.1   chopps 
   1299   1.1   chopps 	dev->sc_cur = dev->sc_chain;
   1300   1.1   chopps 	dev->sc_last = --dcp;
   1301   1.1   chopps 	dev->sc_tcnt = dev->sc_cur->dc_count << 1;
   1302   1.1   chopps 
   1303   1.1   chopps #ifdef DEBUG
   1304   1.1   chopps 	if (sbic_dma_debug & DDB_IO) {
   1305   1.1   chopps 		for (dcp = dev->sc_chain; dcp <= dev->sc_last; dcp++)
   1306   1.1   chopps 			printf("  %d: %d@%x\n", dcp-dev->sc_chain,
   1307   1.1   chopps 			    dcp->dc_count, dcp->dc_addr);
   1308   1.1   chopps 	}
   1309   1.1   chopps #endif
   1310   1.1   chopps 
   1311   1.1   chopps 	/*
   1312   1.1   chopps 	 * push the data cash
   1313   1.1   chopps 	 */
   1314   1.3   chopps #if 0
   1315   1.1   chopps 	DCIS();
   1316   1.4   chopps #elif defined(M68040)
   1317   1.5   chopps 	if (cpu040) {
   1318   1.5   chopps 		dma_cachectl(tmpaddr, len);
   1319   1.3   chopps 
   1320   1.5   chopps 		dspa = (u_int)dev->sc_chain[0].dc_addr;
   1321   1.5   chopps 		deoff = (u_int)dev->sc_last->dc_addr
   1322   1.5   chopps 		    + (dev->sc_last->dc_count >> 1);
   1323   1.5   chopps 		if ((dspa & 0xF) || (deoff & 0xF))
   1324   1.5   chopps 			dev->sc_flags |= SBICF_DCFLUSH;
   1325   1.5   chopps 	}
   1326   1.3   chopps #endif
   1327   1.1   chopps 
   1328   1.1   chopps 	/*
   1329   1.1   chopps 	 * dmago() also enables interrupts for the sbic
   1330   1.1   chopps 	 */
   1331   1.1   chopps 	i = dev->sc_dmago(dev, addr, xs->datalen, dmaflags);
   1332   1.1   chopps 
   1333   1.1   chopps 	SBIC_TC_PUT(regs, (unsigned)i);
   1334   1.1   chopps 	SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
   1335   1.1   chopps 
   1336   1.1   chopps 	return(0);
   1337   1.1   chopps 
   1338   1.1   chopps abort:
   1339   1.1   chopps 	sbicabort(dev, regs, "go");
   1340   1.1   chopps 	dev->sc_dmafree(dev);
   1341   1.1   chopps 	return(-1);
   1342   1.1   chopps }
   1343   1.1   chopps 
   1344   1.1   chopps 
   1345   1.1   chopps int
   1346   1.1   chopps sbicintr(dev)
   1347   1.1   chopps 	struct sbic_softc *dev;
   1348   1.1   chopps {
   1349   1.1   chopps 	sbic_regmap_p regs;
   1350   1.3   chopps 	struct dma_chain *df, *dl;
   1351   1.1   chopps 	u_char asr, csr;
   1352   1.1   chopps 	int i;
   1353   1.1   chopps 
   1354   1.1   chopps 	regs = dev->sc_sbicp;
   1355   1.1   chopps 
   1356   1.1   chopps 	/*
   1357   1.1   chopps 	 * pending interrupt?
   1358   1.1   chopps 	 */
   1359   1.1   chopps 	GET_SBIC_asr (regs, asr);
   1360   1.1   chopps 	if ((asr & SBIC_ASR_INT) == 0)
   1361   1.1   chopps 		return(0);
   1362   1.1   chopps 
   1363   1.1   chopps 	GET_SBIC_csr(regs, csr);
   1364   1.1   chopps 	QPRINTF(("[0x%x]", csr));
   1365   1.1   chopps 
   1366   1.1   chopps 	if (csr == (SBIC_CSR_XFERRED|STATUS_PHASE)
   1367   1.1   chopps 	    || csr == (SBIC_CSR_MIS|STATUS_PHASE)
   1368   1.1   chopps 	    || csr == (SBIC_CSR_MIS_1|STATUS_PHASE)
   1369   1.1   chopps 	    || csr == (SBIC_CSR_MIS_2|STATUS_PHASE)) {
   1370   1.1   chopps 		/*
   1371   1.1   chopps 		 * this should be the normal i/o completion case.
   1372   1.1   chopps 		 * get the status & cmd complete msg then let the
   1373   1.1   chopps 		 * device driver look at what happened.
   1374   1.1   chopps 		 */
   1375   1.1   chopps 		sbicxfdone(dev, regs, dev->sc_xs->sc_link->target);
   1376   1.3   chopps 		/*
   1377   1.3   chopps 		 * check for overlapping cache line, flush if so
   1378   1.3   chopps 		 */
   1379   1.4   chopps #ifdef M68040
   1380   1.3   chopps 		if (dev->sc_flags & SBICF_DCFLUSH) {
   1381   1.3   chopps 			df = dev->sc_chain;
   1382   1.3   chopps 			dl = dev->sc_last;
   1383   1.3   chopps 			DCFL(df->dc_addr);
   1384   1.3   chopps 			DCFL(dl->dc_addr + (dl->dc_count >> 1));
   1385   1.3   chopps 		}
   1386   1.4   chopps #endif
   1387  1.11   chopps 		dev->sc_dmafree(dev);
   1388  1.11   chopps 		if (dev->sc_flags & SBICF_BBUF) {
   1389  1.11   chopps 			bcopy(dev->sc_dmabuffer, dev->sc_dmausrbuf,
   1390  1.11   chopps 			    dev->sc_dmausrlen);
   1391  1.11   chopps 			if (dev->sc_dmausrbuf >= (u_char *)0xff000000)
   1392  1.11   chopps 				printf("%s: WARNING - dmausrbuf = %x\n",
   1393  1.11   chopps 				    dev->sc_dev.dv_xname, dev->sc_dmausrbuf);
   1394  1.11   chopps 			}
   1395   1.3   chopps 		dev->sc_flags &= ~(SBICF_INDMA | SBICF_BBUF | SBICF_DCFLUSH);
   1396   1.1   chopps 		sbic_scsidone(dev, dev->sc_stat[0]);
   1397   1.1   chopps 	} else if (csr == (SBIC_CSR_XFERRED|DATA_OUT_PHASE)
   1398   1.1   chopps 	    || csr == (SBIC_CSR_XFERRED|DATA_IN_PHASE)
   1399   1.1   chopps 	    || csr == (SBIC_CSR_MIS|DATA_OUT_PHASE)
   1400   1.1   chopps 	    || csr == (SBIC_CSR_MIS|DATA_IN_PHASE)
   1401   1.1   chopps 	    || csr == (SBIC_CSR_MIS_1|DATA_OUT_PHASE)
   1402   1.1   chopps 	    || csr == (SBIC_CSR_MIS_1|DATA_IN_PHASE)
   1403   1.1   chopps 	    || csr == (SBIC_CSR_MIS_2|DATA_OUT_PHASE)
   1404   1.1   chopps 	    || csr == (SBIC_CSR_MIS_2|DATA_IN_PHASE)) {
   1405   1.1   chopps 		/*
   1406  1.13  mycroft 		 * do scatter-gather dma
   1407   1.1   chopps 		 * hacking the controller chip, ouch..
   1408   1.1   chopps 		 */
   1409   1.1   chopps 		/*
   1410   1.1   chopps 		 * set next dma addr and dec count
   1411   1.1   chopps 		 */
   1412   1.1   chopps 		dev->sc_cur->dc_addr += dev->sc_tcnt;
   1413   1.1   chopps 		dev->sc_cur->dc_count -= (dev->sc_tcnt >> 1);
   1414   1.1   chopps 
   1415   1.1   chopps 		if (dev->sc_cur->dc_count == 0)
   1416   1.1   chopps 			++dev->sc_cur;		/* advance to next segment */
   1417   1.1   chopps 
   1418   1.1   chopps 		i = dev->sc_dmanext(dev);
   1419   1.1   chopps 		SBIC_TC_PUT(regs, (unsigned)i);
   1420   1.1   chopps 		SET_SBIC_cmd(regs, SBIC_CMD_XFER_INFO);
   1421   1.1   chopps 	} else {
   1422   1.1   chopps 		/*
   1423   1.1   chopps 		 * Something unexpected happened -- deal with it.
   1424   1.1   chopps 		 */
   1425   1.1   chopps 		dev->sc_dmastop(dev);
   1426   1.1   chopps 		sbicerror(dev, regs, csr);
   1427   1.1   chopps 		sbicabort(dev, regs, "intr");
   1428   1.1   chopps 		if (dev->sc_flags & SBICF_INDMA) {
   1429   1.3   chopps 			/*
   1430   1.3   chopps 			 * check for overlapping cache line, flush if so
   1431   1.3   chopps 			 */
   1432   1.4   chopps #ifdef M68040
   1433   1.3   chopps 			if (dev->sc_flags & SBICF_DCFLUSH) {
   1434   1.3   chopps 				df = dev->sc_chain;
   1435   1.3   chopps 				dl = dev->sc_last;
   1436   1.3   chopps 				DCFL(df->dc_addr);
   1437   1.3   chopps 				DCFL(dl->dc_addr + (dl->dc_count >> 1));
   1438   1.3   chopps 			}
   1439   1.4   chopps #endif
   1440  1.13  mycroft 			dev->sc_flags &=
   1441   1.3   chopps 			    ~(SBICF_INDMA | SBICF_BBUF | SBICF_DCFLUSH);
   1442   1.1   chopps 			dev->sc_dmafree(dev);
   1443   1.1   chopps 			sbic_scsidone(dev, -1);
   1444   1.1   chopps 		}
   1445   1.1   chopps 	}
   1446   1.1   chopps 	return(1);
   1447   1.1   chopps }
   1448   1.1   chopps 
   1449   1.1   chopps /*
   1450   1.1   chopps  * Check if DMA can not be used with specified buffer
   1451   1.1   chopps  */
   1452   1.1   chopps 
   1453   1.1   chopps int
   1454   1.1   chopps sbiccheckdmap(bp, len, mask)
   1455   1.1   chopps 	void *bp;
   1456   1.1   chopps 	u_long len, mask;
   1457   1.1   chopps {
   1458   1.1   chopps 	u_char *buffer;
   1459   1.1   chopps 	u_long phy_buf;
   1460   1.1   chopps 	u_long phy_len;
   1461   1.1   chopps 
   1462   1.1   chopps 	buffer = bp;
   1463   1.1   chopps 
   1464   1.1   chopps 	if (len == 0)
   1465   1.1   chopps 		return(0);
   1466   1.1   chopps 
   1467   1.1   chopps 	while (len) {
   1468   1.1   chopps 		phy_buf = kvtop(buffer);
   1469   1.1   chopps 		if (len < (phy_len = NBPG - ((int) buffer & PGOFSET)))
   1470   1.1   chopps 			phy_len = len;
   1471   1.1   chopps 		if (phy_buf & mask)
   1472   1.1   chopps 			return(1);
   1473   1.1   chopps 		buffer += phy_len;
   1474   1.1   chopps 		len -= phy_len;
   1475   1.1   chopps 	}
   1476   1.1   chopps 	return(0);
   1477   1.1   chopps }
   1478   1.1   chopps 
   1479  1.13  mycroft int
   1480   1.1   chopps sbictoscsiperiod(dev, regs, a)
   1481   1.1   chopps 	struct sbic_softc *dev;
   1482   1.1   chopps 	sbic_regmap_p regs;
   1483   1.1   chopps 	int a;
   1484   1.1   chopps {
   1485   1.1   chopps 	unsigned int fs;
   1486  1.13  mycroft 
   1487   1.1   chopps 	/*
   1488   1.1   chopps 	 * cycle = DIV / (2*CLK)
   1489   1.1   chopps 	 * DIV = FS+2
   1490   1.1   chopps 	 * best we can do is 200ns at 20Mhz, 2 cycles
   1491   1.1   chopps 	 */
   1492  1.13  mycroft 
   1493   1.1   chopps 	GET_SBIC_myid(regs,fs);
   1494   1.1   chopps 	fs = (fs >>6) + 2;		/* DIV */
   1495   1.1   chopps 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);	/* Cycle, in ns */
   1496   1.1   chopps 	if (a < 2) a = 8;		/* map to Cycles */
   1497   1.1   chopps 	return ((fs*a)>>2);		/* in 4 ns units */
   1498   1.1   chopps }
   1499   1.1   chopps 
   1500  1.13  mycroft int
   1501   1.1   chopps sbicfromscsiperiod(dev, regs, p)
   1502   1.1   chopps 	struct sbic_softc *dev;
   1503   1.1   chopps 	sbic_regmap_p regs;
   1504   1.1   chopps 	int p;
   1505   1.1   chopps {
   1506   1.1   chopps 	register unsigned int fs, ret;
   1507  1.13  mycroft 
   1508   1.1   chopps 	/* Just the inverse of the above */
   1509  1.13  mycroft 
   1510   1.1   chopps 	GET_SBIC_myid(regs,fs);
   1511   1.1   chopps 	fs = (fs >>6) + 2;		/* DIV */
   1512   1.1   chopps 	fs = (fs * 10000) / (dev->sc_clkfreq<<1);   /* Cycle, in ns */
   1513  1.13  mycroft 
   1514   1.1   chopps 	ret = p << 2;			/* in ns units */
   1515   1.1   chopps 	ret = ret / fs;			/* in Cycles */
   1516   1.1   chopps 	if (ret < sbic_min_period)
   1517   1.1   chopps 		return(sbic_min_period);
   1518   1.1   chopps 
   1519   1.1   chopps 	/* verify rounding */
   1520   1.1   chopps 	if (sbictoscsiperiod(dev, regs, ret) < p)
   1521   1.1   chopps 		ret++;
   1522   1.1   chopps 	return (ret >= 8) ? 0 : ret;
   1523   1.1   chopps }
   1524   1.1   chopps 
   1525