Home | History | Annotate | Line # | Download | only in dev
sci.c revision 1.16
      1  1.16    veego /*	$NetBSD: sci.c,v 1.16 1996/04/21 21:12:24 veego Exp $	*/
      2   1.8      cgd 
      3   1.5   chopps /*
      4   1.5   chopps  * Copyright (c) 1994 Michael L. Hitch
      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.5   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 NCR 5380 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.5   chopps #include <sys/device.h>
     49  1.11   chopps #include <sys/disklabel.h>
     50  1.11   chopps #include <sys/dkstat.h>
     51   1.1   chopps #include <sys/buf.h>
     52   1.5   chopps #include <scsi/scsi_all.h>
     53   1.5   chopps #include <scsi/scsiconf.h>
     54   1.1   chopps #include <vm/vm.h>
     55   1.1   chopps #include <vm/vm_kern.h>
     56   1.1   chopps #include <vm/vm_page.h>
     57   1.1   chopps #include <machine/pmap.h>
     58   1.5   chopps #include <machine/cpu.h>
     59   1.5   chopps #include <amiga/amiga/device.h>
     60   1.5   chopps #include <amiga/amiga/custom.h>
     61  1.11   chopps #include <amiga/amiga/isr.h>
     62   1.5   chopps #include <amiga/dev/scireg.h>
     63   1.1   chopps #include <amiga/dev/scivar.h>
     64   1.1   chopps 
     65   1.1   chopps /*
     66   1.1   chopps  * SCSI delays
     67   1.1   chopps  * In u-seconds, primarily for state changes on the SPC.
     68   1.1   chopps  */
     69   1.5   chopps #define	SCI_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
     70   1.5   chopps #define	SCI_DATA_WAIT	50000	/* wait per data in/out step */
     71   1.5   chopps #define	SCI_INIT_WAIT	50000	/* wait per step (both) during init */
     72   1.1   chopps 
     73   1.5   chopps #define	b_cylin		b_resid
     74   1.1   chopps 
     75   1.5   chopps int  sciicmd __P((struct sci_softc *, int, void *, int, void *, int,u_char));
     76   1.5   chopps int  scigo __P((struct sci_softc *, struct scsi_xfer *));
     77   1.5   chopps int  scigetsense __P((struct sci_softc *, struct scsi_xfer *));
     78   1.5   chopps int  sciselectbus __P((struct sci_softc *, u_char, u_char));
     79   1.5   chopps void sciabort __P((struct sci_softc *, char *));
     80   1.5   chopps void scierror __P((struct sci_softc *, u_char));
     81   1.5   chopps void scisetdelay __P((int));
     82   1.5   chopps void sci_scsidone __P((struct sci_softc *, int));
     83   1.5   chopps void sci_donextcmd __P((struct sci_softc *));
     84  1.16    veego int  sci_ixfer_out __P((struct sci_softc *, int, register u_char *, int));
     85  1.16    veego void sci_ixfer_in __P((struct sci_softc *, int, register u_char *, int));
     86   1.5   chopps 
     87   1.5   chopps int sci_cmd_wait = SCI_CMD_WAIT;
     88   1.5   chopps int sci_data_wait = SCI_DATA_WAIT;
     89   1.5   chopps int sci_init_wait = SCI_INIT_WAIT;
     90   1.1   chopps 
     91   1.5   chopps int sci_no_dma = 0;
     92   1.5   chopps 
     93   1.5   chopps #ifdef DEBUG
     94   1.5   chopps #define QPRINTF(a) if (sci_debug > 1) printf a
     95   1.5   chopps int	sci_debug = 0;
     96   1.1   chopps #else
     97  1.16    veego #define QPRINTF(a)
     98   1.1   chopps #endif
     99   1.1   chopps 
    100   1.5   chopps /*
    101   1.5   chopps  * default minphys routine for sci based controllers
    102   1.5   chopps  */
    103  1.13  mycroft void
    104   1.5   chopps sci_minphys(bp)
    105   1.5   chopps 	struct buf *bp;
    106   1.5   chopps {
    107  1.13  mycroft 
    108   1.5   chopps 	/*
    109  1.13  mycroft 	 * No max transfer at this level.
    110   1.5   chopps 	 */
    111  1.13  mycroft 	minphys(bp);
    112   1.5   chopps }
    113   1.5   chopps 
    114   1.5   chopps /*
    115   1.5   chopps  * used by specific sci controller
    116   1.5   chopps  *
    117   1.5   chopps  * it appears that the higher level code does nothing with LUN's
    118   1.5   chopps  * so I will too.  I could plug it in, however so could they
    119   1.5   chopps  * in scsi_scsi_cmd().
    120   1.5   chopps  */
    121   1.5   chopps int
    122   1.5   chopps sci_scsicmd(xs)
    123   1.5   chopps 	struct scsi_xfer *xs;
    124   1.5   chopps {
    125   1.5   chopps 	struct sci_pending *pendp;
    126   1.5   chopps 	struct sci_softc *dev;
    127   1.5   chopps 	struct scsi_link *slp;
    128   1.5   chopps 	int flags, s;
    129   1.1   chopps 
    130   1.5   chopps 	slp = xs->sc_link;
    131   1.5   chopps 	dev = slp->adapter_softc;
    132   1.5   chopps 	flags = xs->flags;
    133   1.1   chopps 
    134   1.5   chopps 	if (flags & SCSI_DATA_UIO)
    135   1.5   chopps 		panic("sci: scsi data uio requested");
    136  1.13  mycroft 
    137  1.10   chopps 	if (dev->sc_xs && flags & SCSI_POLL)
    138   1.5   chopps 		panic("sci_scsicmd: busy");
    139   1.1   chopps 
    140   1.5   chopps 	s = splbio();
    141   1.5   chopps 	pendp = &dev->sc_xsstore[slp->target][slp->lun];
    142   1.5   chopps 	if (pendp->xs) {
    143   1.5   chopps 		splx(s);
    144   1.5   chopps 		return(TRY_AGAIN_LATER);
    145   1.5   chopps 	}
    146   1.1   chopps 
    147   1.5   chopps 	if (dev->sc_xs) {
    148   1.5   chopps 		pendp->xs = xs;
    149   1.5   chopps 		TAILQ_INSERT_TAIL(&dev->sc_xslist, pendp, link);
    150   1.5   chopps 		splx(s);
    151   1.5   chopps 		return(SUCCESSFULLY_QUEUED);
    152   1.5   chopps 	}
    153   1.5   chopps 	pendp->xs = NULL;
    154   1.5   chopps 	dev->sc_xs = xs;
    155   1.5   chopps 	splx(s);
    156   1.1   chopps 
    157   1.5   chopps 	/*
    158   1.5   chopps 	 * nothing is pending do it now.
    159   1.5   chopps 	 */
    160   1.5   chopps 	sci_donextcmd(dev);
    161   1.1   chopps 
    162  1.10   chopps 	if (flags & SCSI_POLL)
    163   1.5   chopps 		return(COMPLETE);
    164   1.5   chopps 	return(SUCCESSFULLY_QUEUED);
    165   1.5   chopps }
    166   1.1   chopps 
    167   1.5   chopps /*
    168   1.5   chopps  * entered with dev->sc_xs pointing to the next xfer to perform
    169   1.5   chopps  */
    170   1.5   chopps void
    171   1.5   chopps sci_donextcmd(dev)
    172   1.5   chopps 	struct sci_softc *dev;
    173   1.5   chopps {
    174   1.5   chopps 	struct scsi_xfer *xs;
    175   1.5   chopps 	struct scsi_link *slp;
    176   1.5   chopps 	int flags, phase, stat;
    177   1.5   chopps 
    178   1.5   chopps 	xs = dev->sc_xs;
    179   1.5   chopps 	slp = xs->sc_link;
    180   1.5   chopps 	flags = xs->flags;
    181   1.5   chopps 
    182   1.5   chopps 	if (flags & SCSI_DATA_IN)
    183   1.5   chopps 		phase = DATA_IN_PHASE;
    184   1.5   chopps 	else if (flags & SCSI_DATA_OUT)
    185   1.5   chopps 		phase = DATA_OUT_PHASE;
    186   1.5   chopps 	else
    187   1.5   chopps 		phase = STATUS_PHASE;
    188  1.13  mycroft 
    189   1.5   chopps 	if (flags & SCSI_RESET)
    190   1.5   chopps 		scireset(dev);
    191   1.1   chopps 
    192   1.5   chopps 	dev->sc_stat[0] = -1;
    193   1.9   chopps 	xs->cmd->bytes[0] |= slp->lun << 5;
    194  1.13  mycroft 	if (phase == STATUS_PHASE || flags & SCSI_POLL)
    195  1.13  mycroft 		stat = sciicmd(dev, slp->target, xs->cmd, xs->cmdlen,
    196   1.5   chopps 		    xs->data, xs->datalen, phase);
    197   1.5   chopps 	else if (scigo(dev, xs) == 0)
    198   1.5   chopps 		return;
    199  1.13  mycroft 	else
    200   1.5   chopps 		stat = dev->sc_stat[0];
    201  1.13  mycroft 
    202   1.5   chopps 	sci_scsidone(dev, stat);
    203   1.5   chopps }
    204   1.1   chopps 
    205   1.5   chopps void
    206   1.5   chopps sci_scsidone(dev, stat)
    207   1.5   chopps 	struct sci_softc *dev;
    208   1.5   chopps 	int stat;
    209   1.5   chopps {
    210   1.5   chopps 	struct sci_pending *pendp;
    211   1.5   chopps 	struct scsi_xfer *xs;
    212   1.5   chopps 	int s, donext;
    213   1.5   chopps 
    214   1.5   chopps 	xs = dev->sc_xs;
    215   1.5   chopps #ifdef DIAGNOSTIC
    216   1.5   chopps 	if (xs == NULL)
    217   1.5   chopps 		panic("sci_scsidone");
    218  1.11   chopps #endif
    219  1.15  thorpej 	/*
    220  1.15  thorpej 	 * XXX Support old-style instrumentation for now.
    221  1.15  thorpej 	 * IS THIS REALLY THE RIGHT PLACE FOR THIS?  --thorpej
    222  1.15  thorpej 	 */
    223  1.14   chopps 	if (xs->sc_link->device_softc &&
    224  1.14   chopps 	    ((struct device *)(xs->sc_link->device_softc))->dv_unit < dk_ndrive)
    225  1.11   chopps 		++dk_xfer[((struct device *)(xs->sc_link->device_softc))->dv_unit];
    226   1.5   chopps 	/*
    227   1.5   chopps 	 * is this right?
    228   1.5   chopps 	 */
    229   1.5   chopps 	xs->status = stat;
    230   1.1   chopps 
    231  1.10   chopps 	if (stat == 0)
    232   1.5   chopps 		xs->resid = 0;
    233   1.5   chopps 	else {
    234   1.5   chopps 		switch(stat) {
    235   1.5   chopps 		case SCSI_CHECK:
    236  1.16    veego 			stat = scigetsense(dev, xs);
    237  1.16    veego 			if (stat != 0)
    238   1.5   chopps 				goto bad_sense;
    239   1.5   chopps 			xs->error = XS_SENSE;
    240   1.5   chopps 			break;
    241   1.5   chopps 		case SCSI_BUSY:
    242   1.5   chopps 			xs->error = XS_BUSY;
    243   1.5   chopps 			break;
    244   1.5   chopps 		bad_sense:
    245   1.5   chopps 		default:
    246   1.5   chopps 			xs->error = XS_DRIVER_STUFFUP;
    247   1.5   chopps 			QPRINTF(("sci_scsicmd() bad %x\n", stat));
    248   1.5   chopps 			break;
    249   1.5   chopps 		}
    250   1.5   chopps 	}
    251   1.5   chopps 	xs->flags |= ITSDONE;
    252   1.1   chopps 
    253   1.5   chopps 	/*
    254   1.5   chopps 	 * grab next command before scsi_done()
    255   1.5   chopps 	 * this way no single device can hog scsi resources.
    256   1.5   chopps 	 */
    257   1.5   chopps 	s = splbio();
    258   1.5   chopps 	pendp = dev->sc_xslist.tqh_first;
    259   1.5   chopps 	if (pendp == NULL) {
    260   1.5   chopps 		donext = 0;
    261   1.5   chopps 		dev->sc_xs = NULL;
    262   1.5   chopps 	} else {
    263   1.5   chopps 		donext = 1;
    264   1.5   chopps 		TAILQ_REMOVE(&dev->sc_xslist, pendp, link);
    265   1.5   chopps 		dev->sc_xs = pendp->xs;
    266   1.5   chopps 		pendp->xs = NULL;
    267   1.5   chopps 	}
    268   1.5   chopps 	splx(s);
    269   1.5   chopps 	scsi_done(xs);
    270   1.1   chopps 
    271   1.5   chopps 	if (donext)
    272   1.5   chopps 		sci_donextcmd(dev);
    273   1.5   chopps }
    274   1.1   chopps 
    275   1.5   chopps int
    276   1.5   chopps scigetsense(dev, xs)
    277   1.5   chopps 	struct sci_softc *dev;
    278   1.5   chopps 	struct scsi_xfer *xs;
    279   1.1   chopps {
    280   1.5   chopps 	struct scsi_sense rqs;
    281   1.5   chopps 	struct scsi_link *slp;
    282   1.1   chopps 
    283   1.5   chopps 	slp = xs->sc_link;
    284  1.13  mycroft 
    285  1.10   chopps 	rqs.opcode = REQUEST_SENSE;
    286   1.5   chopps 	rqs.byte2 = slp->lun << 5;
    287   1.5   chopps #ifdef not_yet
    288  1.13  mycroft 	rqs.length = xs->req_sense_length ? xs->req_sense_length :
    289   1.5   chopps 	    sizeof(xs->sense);
    290   1.5   chopps #else
    291   1.5   chopps 	rqs.length = sizeof(xs->sense);
    292   1.5   chopps #endif
    293  1.13  mycroft 
    294   1.5   chopps 	rqs.unused[0] = rqs.unused[1] = rqs.control = 0;
    295  1.13  mycroft 
    296   1.5   chopps 	return(sciicmd(dev, slp->target, &rqs, sizeof(rqs), &xs->sense,
    297   1.5   chopps 	    rqs.length, DATA_IN_PHASE));
    298   1.1   chopps }
    299   1.1   chopps 
    300   1.5   chopps void
    301   1.5   chopps sciabort(dev, where)
    302   1.5   chopps 	struct sci_softc *dev;
    303   1.1   chopps 	char *where;
    304   1.1   chopps {
    305   1.5   chopps 	printf ("%s: abort %s: csr = 0x%02x, bus = 0x%02x\n",
    306   1.5   chopps 	  dev->sc_dev.dv_xname, where, *dev->sci_csr, *dev->sci_bus_csr);
    307   1.1   chopps 
    308   1.1   chopps 	if (dev->sc_flags & SCI_SELECTED) {
    309   1.1   chopps 
    310   1.7   chopps 		/* lets just hope it worked.. */
    311   1.7   chopps 		dev->sc_flags &= ~SCI_SELECTED;
    312   1.1   chopps 		/* XXX */
    313   1.5   chopps 		scireset (dev);
    314   1.1   chopps 	}
    315   1.1   chopps }
    316   1.1   chopps 
    317   1.1   chopps /*
    318   1.1   chopps  * XXX Set/reset long delays.
    319   1.1   chopps  *
    320   1.1   chopps  * if delay == 0, reset default delays
    321   1.1   chopps  * if delay < 0,  set both delays to default long initialization values
    322   1.1   chopps  * if delay > 0,  set both delays to this value
    323   1.1   chopps  *
    324   1.1   chopps  * Used when a devices is expected to respond slowly (e.g. during
    325   1.1   chopps  * initialization).
    326   1.1   chopps  */
    327   1.1   chopps void
    328   1.5   chopps scisetdelay(del)
    329   1.5   chopps 	int del;
    330   1.1   chopps {
    331   1.1   chopps 	static int saved_cmd_wait, saved_data_wait;
    332   1.1   chopps 
    333   1.5   chopps 	if (del) {
    334   1.1   chopps 		saved_cmd_wait = sci_cmd_wait;
    335   1.1   chopps 		saved_data_wait = sci_data_wait;
    336   1.5   chopps 		if (del > 0)
    337   1.5   chopps 			sci_cmd_wait = sci_data_wait = del;
    338   1.1   chopps 		else
    339   1.1   chopps 			sci_cmd_wait = sci_data_wait = sci_init_wait;
    340   1.1   chopps 	} else {
    341   1.1   chopps 		sci_cmd_wait = saved_cmd_wait;
    342   1.1   chopps 		sci_data_wait = saved_data_wait;
    343   1.1   chopps 	}
    344   1.1   chopps }
    345   1.1   chopps 
    346   1.1   chopps void
    347   1.5   chopps scireset(dev)
    348   1.5   chopps 	struct sci_softc *dev;
    349   1.1   chopps {
    350  1.16    veego 	u_int s;
    351  1.16    veego 	u_char my_id;
    352   1.1   chopps 
    353   1.7   chopps 	dev->sc_flags &= ~SCI_SELECTED;
    354   1.1   chopps 	if (dev->sc_flags & SCI_ALIVE)
    355   1.5   chopps 		sciabort(dev, "reset");
    356   1.1   chopps 
    357   1.5   chopps 	printf("%s: ", dev->sc_dev.dv_xname);
    358   1.1   chopps 
    359   1.1   chopps 	s = splbio();
    360   1.1   chopps 	/* preserve our ID for now */
    361   1.1   chopps 	my_id = 7;
    362   1.1   chopps 
    363   1.1   chopps 	/*
    364   1.5   chopps 	 * Reset the chip
    365   1.1   chopps 	 */
    366   1.1   chopps 	*dev->sci_icmd = SCI_ICMD_TEST;
    367   1.1   chopps 	*dev->sci_icmd = SCI_ICMD_TEST | SCI_ICMD_RST;
    368   1.6   chopps 	delay (25);
    369   1.1   chopps 	*dev->sci_icmd = 0;
    370   1.1   chopps 
    371   1.1   chopps 	/*
    372   1.1   chopps 	 * Set up various chip parameters
    373   1.1   chopps 	 */
    374   1.1   chopps 	*dev->sci_icmd = 0;
    375   1.1   chopps 	*dev->sci_tcmd = 0;
    376   1.1   chopps 	*dev->sci_sel_enb = 0;
    377   1.1   chopps 
    378   1.1   chopps 	/* anything else was zeroed by reset */
    379   1.1   chopps 
    380   1.1   chopps 	splx (s);
    381   1.1   chopps 
    382   1.1   chopps 	printf("sci id %d\n", my_id);
    383   1.1   chopps 	dev->sc_flags |= SCI_ALIVE;
    384   1.1   chopps }
    385   1.1   chopps 
    386   1.5   chopps void
    387   1.5   chopps scierror(dev, csr)
    388   1.5   chopps 	struct sci_softc *dev;
    389   1.1   chopps 	u_char csr;
    390   1.1   chopps {
    391   1.5   chopps 	struct scsi_xfer *xs;
    392   1.1   chopps 
    393   1.5   chopps 	xs = dev->sc_xs;
    394   1.5   chopps 
    395   1.5   chopps #ifdef DIAGNOSTIC
    396   1.5   chopps 	if (xs == NULL)
    397   1.5   chopps 		panic("scierror");
    398   1.5   chopps #endif
    399   1.5   chopps 	if (xs->flags & SCSI_SILENT)
    400   1.5   chopps 		return;
    401   1.5   chopps 
    402   1.5   chopps 	printf("%s: ", dev->sc_dev.dv_xname);
    403   1.5   chopps 	printf("csr == 0x%02i\n", csr);	/* XXX */
    404   1.1   chopps }
    405   1.1   chopps 
    406   1.5   chopps /*
    407   1.5   chopps  * select the bus, return when selected or error.
    408   1.5   chopps  */
    409   1.5   chopps int
    410   1.5   chopps sciselectbus(dev, target, our_addr)
    411   1.5   chopps         struct sci_softc *dev;
    412   1.1   chopps 	u_char target, our_addr;
    413   1.1   chopps {
    414   1.1   chopps 	register int timeo = 2500;
    415   1.1   chopps 
    416   1.5   chopps 	QPRINTF (("sciselectbus %d\n", target));
    417   1.1   chopps 
    418   1.1   chopps 	/* if we're already selected, return */
    419   1.1   chopps 	if (dev->sc_flags & SCI_SELECTED)	/* XXXX */
    420   1.1   chopps 		return 1;
    421   1.1   chopps 
    422   1.1   chopps 	if ((*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    423   1.1   chopps 	    (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    424   1.1   chopps 	    (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
    425   1.1   chopps 		return 1;
    426   1.1   chopps 
    427   1.1   chopps 	*dev->sci_tcmd = 0;
    428   1.1   chopps 	*dev->sci_odata = 0x80 + (1 << target);
    429   1.1   chopps 	*dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_SEL;
    430   1.1   chopps 	while ((*dev->sci_bus_csr & SCI_BUS_BSY) == 0) {
    431   1.1   chopps 		if (--timeo > 0) {
    432   1.6   chopps 			delay(100);
    433   1.1   chopps 		} else {
    434   1.1   chopps 			break;
    435   1.1   chopps 		}
    436   1.1   chopps 	}
    437   1.1   chopps 	if (timeo) {
    438   1.1   chopps 		*dev->sci_icmd = 0;
    439   1.1   chopps 		dev->sc_flags |= SCI_SELECTED;
    440   1.1   chopps 		return (0);
    441   1.1   chopps 	}
    442   1.1   chopps 	*dev->sci_icmd = 0;
    443   1.1   chopps 	return (1);
    444   1.1   chopps }
    445   1.1   chopps 
    446   1.5   chopps int
    447   1.5   chopps sci_ixfer_out(dev, len, buf, phase)
    448   1.1   chopps 	register struct sci_softc *dev;
    449   1.1   chopps 	int len;
    450   1.1   chopps 	register u_char *buf;
    451   1.1   chopps 	int phase;
    452   1.1   chopps {
    453   1.1   chopps 	register int wait = sci_data_wait;
    454   1.1   chopps 	u_char csr;
    455   1.1   chopps 
    456   1.5   chopps 	QPRINTF(("sci_ixfer_out {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    457   1.1   chopps 	  len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
    458   1.1   chopps 	  buf[6], buf[7], buf[8], buf[9]));
    459   1.1   chopps 
    460   1.1   chopps 	*dev->sci_tcmd = phase;
    461   1.1   chopps 	*dev->sci_icmd = SCI_ICMD_DATA;
    462   1.1   chopps 	for (;len > 0; len--) {
    463   1.1   chopps 		csr = *dev->sci_bus_csr;
    464   1.1   chopps 		while (!(csr & SCI_BUS_REQ)) {
    465   1.1   chopps 			if ((csr & SCI_BUS_BSY) == 0 || --wait < 0) {
    466   1.1   chopps #ifdef DEBUG
    467   1.1   chopps 				if (sci_debug)
    468   1.5   chopps 					printf("sci_ixfer_out fail: l%d i%x w%d\n",
    469   1.1   chopps 					  len, csr, wait);
    470   1.1   chopps #endif
    471   1.1   chopps 				return (len);
    472   1.1   chopps 			}
    473   1.6   chopps 			delay(1);
    474   1.1   chopps 			csr = *dev->sci_bus_csr;
    475   1.1   chopps 		}
    476   1.1   chopps 
    477   1.1   chopps 		if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
    478   1.1   chopps 			break;
    479   1.1   chopps 		*dev->sci_odata = *buf;
    480   1.1   chopps 		*dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_ACK;
    481   1.1   chopps 		buf++;
    482   1.1   chopps 		while (*dev->sci_bus_csr & SCI_BUS_REQ);
    483   1.1   chopps 		*dev->sci_icmd = SCI_ICMD_DATA;
    484   1.1   chopps 	}
    485   1.1   chopps 
    486   1.5   chopps 	QPRINTF(("sci_ixfer_out done\n"));
    487   1.1   chopps 	return (0);
    488   1.1   chopps }
    489   1.1   chopps 
    490   1.5   chopps void
    491   1.5   chopps sci_ixfer_in(dev, len, buf, phase)
    492   1.1   chopps 	struct sci_softc *dev;
    493   1.1   chopps 	int len;
    494   1.1   chopps 	register u_char *buf;
    495   1.1   chopps 	int phase;
    496   1.1   chopps {
    497   1.1   chopps 	int wait = sci_data_wait;
    498   1.1   chopps 	u_char csr;
    499   1.1   chopps 	volatile register u_char *sci_bus_csr = dev->sci_bus_csr;
    500   1.1   chopps 	volatile register u_char *sci_data = dev->sci_data;
    501   1.1   chopps 	volatile register u_char *sci_icmd = dev->sci_icmd;
    502  1.16    veego #ifdef DEBUG
    503  1.16    veego 	u_char *obp = buf;
    504  1.16    veego #endif
    505   1.1   chopps 
    506   1.1   chopps 	csr = *sci_bus_csr;
    507   1.1   chopps 
    508   1.5   chopps 	QPRINTF(("sci_ixfer_in %d, csr=%02x\n", len, csr));
    509   1.1   chopps 
    510   1.1   chopps 	*dev->sci_tcmd = phase;
    511   1.1   chopps 	*sci_icmd = 0;
    512   1.1   chopps 	for (;len > 0; len--) {
    513   1.1   chopps 		csr = *sci_bus_csr;
    514   1.1   chopps 		while (!(csr & SCI_BUS_REQ)) {
    515   1.1   chopps 			if (!(csr & SCI_BUS_BSY) || --wait < 0) {
    516   1.1   chopps #ifdef DEBUG
    517   1.1   chopps 				if (sci_debug)
    518   1.5   chopps 					printf("sci_ixfer_in fail: l%d i%x w%d\n",
    519   1.1   chopps 					len, csr, wait);
    520   1.1   chopps #endif
    521   1.1   chopps 				return;
    522   1.1   chopps 			}
    523   1.1   chopps 
    524   1.6   chopps 			delay(1);
    525   1.1   chopps 			csr = *sci_bus_csr;
    526   1.1   chopps 		}
    527   1.1   chopps 
    528   1.1   chopps 		if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
    529   1.1   chopps 			break;
    530   1.1   chopps 		*buf = *sci_data;
    531   1.1   chopps 		*sci_icmd = SCI_ICMD_ACK;
    532   1.1   chopps 		buf++;
    533   1.1   chopps 		while (*sci_bus_csr & SCI_BUS_REQ);
    534   1.1   chopps 		*sci_icmd = 0;
    535   1.1   chopps 	}
    536   1.1   chopps 
    537   1.5   chopps 	QPRINTF(("sci_ixfer_in {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    538   1.1   chopps 	  len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
    539   1.1   chopps 	  obp[6], obp[7], obp[8], obp[9]));
    540   1.1   chopps }
    541   1.1   chopps 
    542   1.1   chopps /*
    543   1.1   chopps  * SCSI 'immediate' command:  issue a command to some SCSI device
    544   1.1   chopps  * and get back an 'immediate' response (i.e., do programmed xfer
    545   1.1   chopps  * to get the response data).  'cbuf' is a buffer containing a scsi
    546   1.1   chopps  * command of length clen bytes.  'buf' is a buffer of length 'len'
    547   1.1   chopps  * bytes for data.  The transfer direction is determined by the device
    548   1.1   chopps  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
    549   1.1   chopps  * command must supply no data.  'xferphase' is the bus phase the
    550   1.1   chopps  * caller expects to happen after the command is issued.  It should
    551   1.1   chopps  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
    552   1.1   chopps  */
    553   1.5   chopps int
    554   1.5   chopps sciicmd(dev, target, cbuf, clen, buf, len, xferphase)
    555   1.1   chopps 	struct sci_softc *dev;
    556   1.5   chopps 	void *cbuf, *buf;
    557   1.5   chopps 	int clen, len;
    558   1.1   chopps 	u_char xferphase;
    559   1.1   chopps {
    560  1.16    veego 	u_char phase;
    561   1.1   chopps 	register int wait;
    562   1.1   chopps 
    563   1.1   chopps 	/* select the SCSI bus (it's an error if bus isn't free) */
    564   1.5   chopps 	if (sciselectbus (dev, target, dev->sc_scsi_addr))
    565   1.1   chopps 		return -1;
    566   1.1   chopps 	/*
    567   1.1   chopps 	 * Wait for a phase change (or error) then let the device
    568   1.1   chopps 	 * sequence us through the various SCSI phases.
    569   1.1   chopps 	 */
    570   1.1   chopps 	dev->sc_stat[0] = 0xff;
    571   1.1   chopps 	dev->sc_msg[0] = 0xff;
    572   1.1   chopps 	phase = CMD_PHASE;
    573   1.1   chopps 	while (1) {
    574   1.1   chopps 		wait = sci_cmd_wait;
    575   1.1   chopps 
    576   1.1   chopps 		while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) == SCI_BUS_BSY);
    577   1.1   chopps 
    578   1.1   chopps 		QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
    579   1.1   chopps 		if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
    580   1.1   chopps 			return -1;
    581   1.1   chopps 		}
    582   1.1   chopps 		phase = SCI_PHASE(*dev->sci_bus_csr);
    583   1.1   chopps 
    584   1.1   chopps 		switch (phase) {
    585   1.1   chopps 		case CMD_PHASE:
    586   1.5   chopps 			if (sci_ixfer_out (dev, clen, cbuf, phase))
    587   1.1   chopps 				goto abort;
    588   1.1   chopps 			phase = xferphase;
    589   1.1   chopps 			break;
    590   1.1   chopps 
    591   1.1   chopps 		case DATA_IN_PHASE:
    592   1.1   chopps 			if (len <= 0)
    593   1.1   chopps 				goto abort;
    594   1.1   chopps 			wait = sci_data_wait;
    595   1.5   chopps 			sci_ixfer_in (dev, len, buf, phase);
    596   1.1   chopps 			phase = STATUS_PHASE;
    597   1.1   chopps 			break;
    598   1.1   chopps 
    599   1.1   chopps 		case DATA_OUT_PHASE:
    600   1.1   chopps 			if (len <= 0)
    601   1.1   chopps 				goto abort;
    602   1.1   chopps 			wait = sci_data_wait;
    603   1.5   chopps 			if (sci_ixfer_out (dev, len, buf, phase))
    604   1.1   chopps 				goto abort;
    605   1.1   chopps 			phase = STATUS_PHASE;
    606   1.1   chopps 			break;
    607   1.1   chopps 
    608   1.1   chopps 		case MESG_IN_PHASE:
    609   1.1   chopps 			dev->sc_msg[0] = 0xff;
    610   1.5   chopps 			sci_ixfer_in (dev, 1, dev->sc_msg,phase);
    611   1.1   chopps 			dev->sc_flags &= ~SCI_SELECTED;
    612   1.1   chopps 			while (*dev->sci_bus_csr & SCI_BUS_BSY);
    613   1.1   chopps 			goto out;
    614   1.1   chopps 			break;
    615   1.1   chopps 
    616   1.1   chopps 		case MESG_OUT_PHASE:
    617   1.1   chopps 			phase = STATUS_PHASE;
    618   1.1   chopps 			break;
    619   1.1   chopps 
    620   1.1   chopps 		case STATUS_PHASE:
    621   1.5   chopps 			sci_ixfer_in (dev, 1, dev->sc_stat, phase);
    622   1.1   chopps 			phase = MESG_IN_PHASE;
    623   1.1   chopps 			break;
    624   1.1   chopps 
    625   1.1   chopps 		case BUS_FREE_PHASE:
    626   1.1   chopps 			goto out;
    627   1.1   chopps 
    628   1.1   chopps 		default:
    629   1.1   chopps 		printf("sci: unexpected phase %d in icmd from %d\n",
    630   1.1   chopps 		  phase, target);
    631   1.1   chopps 		goto abort;
    632   1.1   chopps 		}
    633   1.1   chopps #if 0
    634   1.1   chopps 		if (wait <= 0)
    635   1.1   chopps 			goto abort;
    636   1.1   chopps #endif
    637   1.1   chopps 	}
    638   1.1   chopps 
    639   1.1   chopps abort:
    640   1.5   chopps 	sciabort(dev, "icmd");
    641   1.1   chopps out:
    642   1.1   chopps 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
    643   1.1   chopps 	return (dev->sc_stat[0]);
    644   1.1   chopps }
    645   1.1   chopps 
    646   1.1   chopps int
    647   1.5   chopps scigo(dev, xs)
    648   1.5   chopps 	struct sci_softc *dev;
    649   1.5   chopps 	struct scsi_xfer *xs;
    650   1.1   chopps {
    651  1.16    veego 	int count, target;
    652  1.16    veego 	u_char phase, *addr;
    653   1.5   chopps 
    654   1.5   chopps 	target = xs->sc_link->target;
    655   1.5   chopps 	count = xs->datalen;
    656   1.5   chopps 	addr = xs->data;
    657   1.1   chopps 
    658   1.1   chopps 	if (sci_no_dma)	{
    659   1.5   chopps 		sciicmd (dev, target, (u_char *) xs->cmd, xs->cmdlen,
    660   1.1   chopps 		  addr, count,
    661   1.5   chopps 		  xs->flags & SCSI_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE);
    662   1.1   chopps 
    663   1.5   chopps 		return (1);
    664   1.1   chopps 	}
    665   1.1   chopps 
    666   1.1   chopps 	/* select the SCSI bus (it's an error if bus isn't free) */
    667   1.5   chopps 	if (sciselectbus (dev, target, dev->sc_scsi_addr))
    668   1.1   chopps 		return -1;
    669   1.1   chopps 	/*
    670   1.1   chopps 	 * Wait for a phase change (or error) then let the device
    671   1.1   chopps 	 * sequence us through the various SCSI phases.
    672   1.1   chopps 	 */
    673   1.1   chopps 	dev->sc_stat[0] = 0xff;
    674   1.1   chopps 	dev->sc_msg[0] = 0xff;
    675   1.1   chopps 	phase = CMD_PHASE;
    676   1.1   chopps 	while (1) {
    677   1.1   chopps 		while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) ==
    678   1.1   chopps 		  SCI_BUS_BSY);
    679   1.1   chopps 
    680   1.1   chopps 		QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
    681   1.1   chopps 		if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
    682   1.1   chopps 			goto abort;
    683   1.1   chopps 		}
    684   1.1   chopps 		phase = SCI_PHASE(*dev->sci_bus_csr);
    685   1.1   chopps 
    686   1.1   chopps 		switch (phase) {
    687   1.1   chopps 		case CMD_PHASE:
    688  1.16    veego 			if (sci_ixfer_out (dev, xs->cmdlen, (u_char *) xs->cmd, phase))
    689   1.1   chopps 				goto abort;
    690   1.5   chopps 			phase = xs->flags & SCSI_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE;
    691   1.1   chopps 			break;
    692   1.1   chopps 
    693   1.1   chopps 		case DATA_IN_PHASE:
    694   1.1   chopps 			if (count <= 0)
    695   1.1   chopps 				goto abort;
    696   1.1   chopps 			/* XXX use psuedo DMA if available */
    697   1.1   chopps 			if (count >= 128 && dev->dma_xfer_in)
    698   1.1   chopps 				(*dev->dma_xfer_in)(dev, count, addr, phase);
    699   1.1   chopps 			else
    700   1.5   chopps 				sci_ixfer_in (dev, count, addr, phase);
    701   1.1   chopps 			phase = STATUS_PHASE;
    702   1.1   chopps 			break;
    703   1.1   chopps 
    704   1.1   chopps 		case DATA_OUT_PHASE:
    705   1.1   chopps 			if (count <= 0)
    706   1.1   chopps 				goto abort;
    707   1.1   chopps 			/* XXX use psuedo DMA if available */
    708   1.1   chopps 			if (count >= 128 && dev->dma_xfer_out)
    709   1.1   chopps 				(*dev->dma_xfer_out)(dev, count, addr, phase);
    710   1.1   chopps 			else
    711   1.5   chopps 				if (sci_ixfer_out (dev, count, addr, phase))
    712   1.1   chopps 					goto abort;
    713   1.1   chopps 			phase = STATUS_PHASE;
    714   1.1   chopps 			break;
    715   1.1   chopps 
    716   1.1   chopps 		case MESG_IN_PHASE:
    717   1.1   chopps 			dev->sc_msg[0] = 0xff;
    718   1.5   chopps 			sci_ixfer_in (dev, 1, dev->sc_msg,phase);
    719   1.1   chopps 			dev->sc_flags &= ~SCI_SELECTED;
    720   1.1   chopps 			while (*dev->sci_bus_csr & SCI_BUS_BSY);
    721   1.1   chopps 			goto out;
    722   1.1   chopps 			break;
    723   1.1   chopps 
    724   1.1   chopps 		case MESG_OUT_PHASE:
    725   1.1   chopps 			phase = STATUS_PHASE;
    726   1.1   chopps 			break;
    727   1.1   chopps 
    728   1.1   chopps 		case STATUS_PHASE:
    729   1.5   chopps 			sci_ixfer_in (dev, 1, dev->sc_stat, phase);
    730   1.1   chopps 			phase = MESG_IN_PHASE;
    731   1.1   chopps 			break;
    732   1.1   chopps 
    733   1.1   chopps 		case BUS_FREE_PHASE:
    734   1.1   chopps 			goto out;
    735   1.1   chopps 
    736   1.1   chopps 		default:
    737   1.1   chopps 		printf("sci: unexpected phase %d in icmd from %d\n",
    738   1.5   chopps 		  phase, target);
    739   1.1   chopps 		goto abort;
    740   1.1   chopps 		}
    741   1.1   chopps 	}
    742   1.1   chopps 
    743   1.1   chopps abort:
    744   1.5   chopps 	sciabort(dev, "go");
    745   1.1   chopps out:
    746   1.1   chopps 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
    747   1.5   chopps 	return (1);
    748   1.1   chopps }
    749