Home | History | Annotate | Line # | Download | only in dev
sci.c revision 1.30
      1  1.30    bouyer /*	$NetBSD: sci.c,v 1.30 2003/10/31 14:38:44 bouyer Exp $ */
      2   1.8       cgd 
      3   1.5    chopps /*
      4   1.1    chopps  * Copyright (c) 1990 The Regents of the University of California.
      5   1.1    chopps  * All rights reserved.
      6   1.1    chopps  *
      7   1.1    chopps  * This code is derived from software contributed to Berkeley by
      8   1.1    chopps  * Van Jacobson of Lawrence Berkeley Laboratory.
      9   1.1    chopps  *
     10   1.1    chopps  * Redistribution and use in source and binary forms, with or without
     11   1.1    chopps  * modification, are permitted provided that the following conditions
     12   1.1    chopps  * are met:
     13   1.1    chopps  * 1. Redistributions of source code must retain the above copyright
     14   1.1    chopps  *    notice, this list of conditions and the following disclaimer.
     15   1.1    chopps  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    chopps  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    chopps  *    documentation and/or other materials provided with the distribution.
     18  1.29       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.29       agc  *    may be used to endorse or promote products derived from this software
     20  1.29       agc  *    without specific prior written permission.
     21  1.29       agc  *
     22  1.29       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.29       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.29       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.29       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.29       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.29       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.29       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.29       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.29       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.29       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.29       agc  * SUCH DAMAGE.
     33  1.29       agc  *
     34  1.29       agc  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     35  1.29       agc  */
     36  1.29       agc 
     37  1.29       agc /*
     38  1.29       agc  * Copyright (c) 1994 Michael L. Hitch
     39  1.29       agc  *
     40  1.29       agc  * This code is derived from software contributed to Berkeley by
     41  1.29       agc  * Van Jacobson of Lawrence Berkeley Laboratory.
     42  1.29       agc  *
     43  1.29       agc  * Redistribution and use in source and binary forms, with or without
     44  1.29       agc  * modification, are permitted provided that the following conditions
     45  1.29       agc  * are met:
     46  1.29       agc  * 1. Redistributions of source code must retain the above copyright
     47  1.29       agc  *    notice, this list of conditions and the following disclaimer.
     48  1.29       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.29       agc  *    notice, this list of conditions and the following disclaimer in the
     50  1.29       agc  *    documentation and/or other materials provided with the distribution.
     51   1.1    chopps  * 3. All advertising materials mentioning features or use of this software
     52   1.1    chopps  *    must display the following acknowledgement:
     53   1.1    chopps  *	This product includes software developed by the University of
     54   1.1    chopps  *	California, Berkeley and its contributors.
     55   1.1    chopps  * 4. Neither the name of the University nor the names of its contributors
     56   1.1    chopps  *    may be used to endorse or promote products derived from this software
     57   1.1    chopps  *    without specific prior written permission.
     58   1.1    chopps  *
     59   1.1    chopps  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1    chopps  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1    chopps  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1    chopps  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1    chopps  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1    chopps  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1    chopps  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1    chopps  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1    chopps  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1    chopps  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1    chopps  * SUCH DAMAGE.
     70   1.1    chopps  *
     71   1.5    chopps  *	@(#)scsi.c	7.5 (Berkeley) 5/4/91
     72   1.1    chopps  */
     73   1.1    chopps 
     74   1.1    chopps /*
     75   1.1    chopps  * AMIGA NCR 5380 scsi adaptor driver
     76   1.1    chopps  */
     77  1.27   aymeric 
     78  1.27   aymeric #include <sys/cdefs.h>
     79  1.30    bouyer __KERNEL_RCSID(0, "$NetBSD: sci.c,v 1.30 2003/10/31 14:38:44 bouyer Exp $");
     80   1.1    chopps 
     81   1.1    chopps #include <sys/param.h>
     82   1.1    chopps #include <sys/systm.h>
     83   1.5    chopps #include <sys/device.h>
     84  1.11    chopps #include <sys/disklabel.h>
     85   1.1    chopps #include <sys/buf.h>
     86  1.20    bouyer #include <dev/scsipi/scsi_all.h>
     87  1.20    bouyer #include <dev/scsipi/scsipi_all.h>
     88  1.20    bouyer #include <dev/scsipi/scsiconf.h>
     89  1.23       mrg #include <uvm/uvm_extern.h>
     90   1.1    chopps #include <machine/pmap.h>
     91   1.5    chopps #include <machine/cpu.h>
     92   1.5    chopps #include <amiga/amiga/device.h>
     93   1.5    chopps #include <amiga/amiga/custom.h>
     94  1.11    chopps #include <amiga/amiga/isr.h>
     95   1.5    chopps #include <amiga/dev/scireg.h>
     96   1.1    chopps #include <amiga/dev/scivar.h>
     97   1.1    chopps 
     98   1.1    chopps /*
     99   1.1    chopps  * SCSI delays
    100   1.1    chopps  * In u-seconds, primarily for state changes on the SPC.
    101   1.1    chopps  */
    102   1.5    chopps #define	SCI_CMD_WAIT	50000	/* wait per step of 'immediate' cmds */
    103   1.5    chopps #define	SCI_DATA_WAIT	50000	/* wait per data in/out step */
    104   1.5    chopps #define	SCI_INIT_WAIT	50000	/* wait per step (both) during init */
    105   1.1    chopps 
    106  1.26   aymeric int  sciicmd(struct sci_softc *, int, void *, int, void *, int,u_char);
    107  1.26   aymeric int  scigo(struct sci_softc *, struct scsipi_xfer *);
    108  1.26   aymeric int  sciselectbus(struct sci_softc *, u_char, u_char);
    109  1.26   aymeric void sciabort(struct sci_softc *, char *);
    110  1.26   aymeric void scierror(struct sci_softc *, u_char);
    111  1.26   aymeric void scisetdelay(int);
    112  1.26   aymeric void sci_scsidone(struct sci_softc *, int);
    113  1.26   aymeric void sci_donextcmd(struct sci_softc *);
    114  1.26   aymeric int  sci_ixfer_out(struct sci_softc *, int, register u_char *, int);
    115  1.26   aymeric void sci_ixfer_in(struct sci_softc *, int, register u_char *, int);
    116   1.5    chopps 
    117   1.5    chopps int sci_cmd_wait = SCI_CMD_WAIT;
    118   1.5    chopps int sci_data_wait = SCI_DATA_WAIT;
    119   1.5    chopps int sci_init_wait = SCI_INIT_WAIT;
    120   1.1    chopps 
    121   1.5    chopps int sci_no_dma = 0;
    122   1.5    chopps 
    123   1.5    chopps #ifdef DEBUG
    124  1.19  christos #define QPRINTF(a) if (sci_debug > 1) printf a
    125   1.5    chopps int	sci_debug = 0;
    126   1.1    chopps #else
    127  1.16     veego #define QPRINTF(a)
    128   1.1    chopps #endif
    129   1.1    chopps 
    130   1.5    chopps /*
    131   1.5    chopps  * default minphys routine for sci based controllers
    132   1.5    chopps  */
    133  1.13   mycroft void
    134  1.26   aymeric sci_minphys(struct buf *bp)
    135   1.5    chopps {
    136  1.13   mycroft 
    137   1.5    chopps 	/*
    138  1.13   mycroft 	 * No max transfer at this level.
    139   1.5    chopps 	 */
    140  1.13   mycroft 	minphys(bp);
    141   1.5    chopps }
    142   1.5    chopps 
    143   1.5    chopps /*
    144   1.5    chopps  * used by specific sci controller
    145   1.5    chopps  *
    146   1.5    chopps  * it appears that the higher level code does nothing with LUN's
    147   1.5    chopps  * so I will too.  I could plug it in, however so could they
    148  1.20    bouyer  * in scsi_scsipi_cmd().
    149   1.5    chopps  */
    150  1.25    bouyer void
    151  1.26   aymeric sci_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
    152  1.26   aymeric                    void *arg)
    153  1.25    bouyer {
    154  1.20    bouyer 	struct scsipi_xfer *xs;
    155  1.25    bouyer 	struct scsipi_periph *periph;
    156  1.25    bouyer 	struct sci_softc *dev = (void *)chan->chan_adapter->adapt_dev;
    157   1.5    chopps 	int flags, s;
    158   1.1    chopps 
    159  1.25    bouyer 	switch (req) {
    160  1.25    bouyer 	case ADAPTER_REQ_RUN_XFER:
    161  1.25    bouyer 		xs = arg;
    162  1.25    bouyer 		periph = xs->xs_periph;
    163  1.25    bouyer 		flags = xs->xs_control;
    164   1.1    chopps 
    165  1.25    bouyer 		if (flags & XS_CTL_DATA_UIO)
    166   1.5    chopps 		panic("sci: scsi data uio requested");
    167  1.30    bouyer 
    168  1.30    bouyer 		s = splbio();
    169  1.13   mycroft 
    170  1.25    bouyer 		if (dev->sc_xs && flags & XS_CTL_POLL)
    171  1.25    bouyer 			panic("sci_scsicmd: busy");
    172   1.1    chopps 
    173  1.25    bouyer #ifdef DIAGNOSTIC
    174  1.25    bouyer 		/*
    175  1.25    bouyer 		 * This should never happen as we track the resources
    176  1.25    bouyer 		 * in the mid-layer.
    177  1.25    bouyer 		 */
    178  1.25    bouyer 		if (dev->sc_xs) {
    179  1.25    bouyer 			scsipi_printaddr(periph);
    180  1.25    bouyer 			printf("unable to allocate scb\n");
    181  1.25    bouyer 			panic("sea_scsipi_request");
    182  1.25    bouyer 		}
    183  1.25    bouyer #endif
    184  1.25    bouyer 
    185  1.25    bouyer 		dev->sc_xs = xs;
    186   1.5    chopps 		splx(s);
    187   1.1    chopps 
    188  1.25    bouyer 		/*
    189  1.25    bouyer 		 * nothing is pending do it now.
    190  1.25    bouyer 		 */
    191  1.25    bouyer 		sci_donextcmd(dev);
    192  1.25    bouyer 
    193  1.25    bouyer 		return;
    194   1.1    chopps 
    195  1.25    bouyer 	case ADAPTER_REQ_GROW_RESOURCES:
    196  1.25    bouyer 		return;
    197   1.1    chopps 
    198  1.25    bouyer 	case ADAPTER_REQ_SET_XFER_MODE:
    199  1.25    bouyer 		return;
    200  1.25    bouyer 	}
    201   1.5    chopps }
    202   1.1    chopps 
    203   1.5    chopps /*
    204   1.5    chopps  * entered with dev->sc_xs pointing to the next xfer to perform
    205   1.5    chopps  */
    206   1.5    chopps void
    207  1.26   aymeric sci_donextcmd(struct sci_softc *dev)
    208   1.5    chopps {
    209  1.20    bouyer 	struct scsipi_xfer *xs;
    210  1.25    bouyer 	struct scsipi_periph *periph;
    211   1.5    chopps 	int flags, phase, stat;
    212   1.5    chopps 
    213   1.5    chopps 	xs = dev->sc_xs;
    214  1.25    bouyer 	periph = xs->xs_periph;
    215  1.21   thorpej 	flags = xs->xs_control;
    216   1.5    chopps 
    217  1.21   thorpej 	if (flags & XS_CTL_DATA_IN)
    218   1.5    chopps 		phase = DATA_IN_PHASE;
    219  1.21   thorpej 	else if (flags & XS_CTL_DATA_OUT)
    220   1.5    chopps 		phase = DATA_OUT_PHASE;
    221   1.5    chopps 	else
    222   1.5    chopps 		phase = STATUS_PHASE;
    223  1.13   mycroft 
    224  1.21   thorpej 	if (flags & XS_CTL_RESET)
    225   1.5    chopps 		scireset(dev);
    226   1.1    chopps 
    227   1.5    chopps 	dev->sc_stat[0] = -1;
    228  1.25    bouyer 	xs->cmd->bytes[0] |= periph->periph_lun << 5;
    229  1.21   thorpej 	if (phase == STATUS_PHASE || flags & XS_CTL_POLL)
    230  1.25    bouyer 		stat = sciicmd(dev, periph->periph_target, xs->cmd, xs->cmdlen,
    231   1.5    chopps 		    xs->data, xs->datalen, phase);
    232   1.5    chopps 	else if (scigo(dev, xs) == 0)
    233   1.5    chopps 		return;
    234  1.13   mycroft 	else
    235   1.5    chopps 		stat = dev->sc_stat[0];
    236  1.13   mycroft 
    237   1.5    chopps 	sci_scsidone(dev, stat);
    238   1.5    chopps }
    239   1.1    chopps 
    240   1.5    chopps void
    241  1.26   aymeric sci_scsidone(struct sci_softc *dev, int stat)
    242   1.5    chopps {
    243  1.20    bouyer 	struct scsipi_xfer *xs;
    244   1.5    chopps 
    245   1.5    chopps 	xs = dev->sc_xs;
    246   1.5    chopps #ifdef DIAGNOSTIC
    247   1.5    chopps 	if (xs == NULL)
    248   1.5    chopps 		panic("sci_scsidone");
    249  1.11    chopps #endif
    250   1.5    chopps 	xs->status = stat;
    251  1.10    chopps 	if (stat == 0)
    252   1.5    chopps 		xs->resid = 0;
    253   1.5    chopps 	else {
    254   1.5    chopps 		switch(stat) {
    255   1.5    chopps 		case SCSI_CHECK:
    256  1.25    bouyer 			xs->resid = 0;
    257  1.25    bouyer 			/* FALLTHOUGH */
    258   1.5    chopps 		case SCSI_BUSY:
    259   1.5    chopps 			xs->error = XS_BUSY;
    260   1.5    chopps 			break;
    261   1.5    chopps 		default:
    262   1.5    chopps 			xs->error = XS_DRIVER_STUFFUP;
    263   1.5    chopps 			QPRINTF(("sci_scsicmd() bad %x\n", stat));
    264   1.5    chopps 			break;
    265   1.5    chopps 		}
    266   1.5    chopps 	}
    267  1.21   thorpej 
    268  1.20    bouyer 	scsipi_done(xs);
    269   1.1    chopps 
    270   1.1    chopps }
    271   1.1    chopps 
    272   1.5    chopps void
    273  1.26   aymeric sciabort(struct sci_softc *dev, char *where)
    274   1.1    chopps {
    275  1.19  christos 	printf ("%s: abort %s: csr = 0x%02x, bus = 0x%02x\n",
    276   1.5    chopps 	  dev->sc_dev.dv_xname, where, *dev->sci_csr, *dev->sci_bus_csr);
    277   1.1    chopps 
    278   1.1    chopps 	if (dev->sc_flags & SCI_SELECTED) {
    279   1.1    chopps 
    280   1.7    chopps 		/* lets just hope it worked.. */
    281   1.7    chopps 		dev->sc_flags &= ~SCI_SELECTED;
    282   1.1    chopps 		/* XXX */
    283   1.5    chopps 		scireset (dev);
    284   1.1    chopps 	}
    285   1.1    chopps }
    286   1.1    chopps 
    287   1.1    chopps /*
    288   1.1    chopps  * XXX Set/reset long delays.
    289   1.1    chopps  *
    290   1.1    chopps  * if delay == 0, reset default delays
    291   1.1    chopps  * if delay < 0,  set both delays to default long initialization values
    292   1.1    chopps  * if delay > 0,  set both delays to this value
    293   1.1    chopps  *
    294   1.1    chopps  * Used when a devices is expected to respond slowly (e.g. during
    295   1.1    chopps  * initialization).
    296   1.1    chopps  */
    297   1.1    chopps void
    298  1.26   aymeric scisetdelay(int del)
    299   1.1    chopps {
    300   1.1    chopps 	static int saved_cmd_wait, saved_data_wait;
    301   1.1    chopps 
    302   1.5    chopps 	if (del) {
    303   1.1    chopps 		saved_cmd_wait = sci_cmd_wait;
    304   1.1    chopps 		saved_data_wait = sci_data_wait;
    305   1.5    chopps 		if (del > 0)
    306   1.5    chopps 			sci_cmd_wait = sci_data_wait = del;
    307   1.1    chopps 		else
    308   1.1    chopps 			sci_cmd_wait = sci_data_wait = sci_init_wait;
    309   1.1    chopps 	} else {
    310   1.1    chopps 		sci_cmd_wait = saved_cmd_wait;
    311   1.1    chopps 		sci_data_wait = saved_data_wait;
    312   1.1    chopps 	}
    313   1.1    chopps }
    314   1.1    chopps 
    315   1.1    chopps void
    316  1.26   aymeric scireset(struct sci_softc *dev)
    317   1.1    chopps {
    318  1.16     veego 	u_int s;
    319  1.16     veego 	u_char my_id;
    320   1.1    chopps 
    321   1.7    chopps 	dev->sc_flags &= ~SCI_SELECTED;
    322   1.1    chopps 	if (dev->sc_flags & SCI_ALIVE)
    323   1.5    chopps 		sciabort(dev, "reset");
    324   1.1    chopps 
    325  1.19  christos 	printf("%s: ", dev->sc_dev.dv_xname);
    326   1.1    chopps 
    327   1.1    chopps 	s = splbio();
    328   1.1    chopps 	/* preserve our ID for now */
    329   1.1    chopps 	my_id = 7;
    330   1.1    chopps 
    331   1.1    chopps 	/*
    332   1.5    chopps 	 * Reset the chip
    333   1.1    chopps 	 */
    334   1.1    chopps 	*dev->sci_icmd = SCI_ICMD_TEST;
    335   1.1    chopps 	*dev->sci_icmd = SCI_ICMD_TEST | SCI_ICMD_RST;
    336   1.6    chopps 	delay (25);
    337   1.1    chopps 	*dev->sci_icmd = 0;
    338   1.1    chopps 
    339   1.1    chopps 	/*
    340   1.1    chopps 	 * Set up various chip parameters
    341   1.1    chopps 	 */
    342   1.1    chopps 	*dev->sci_icmd = 0;
    343   1.1    chopps 	*dev->sci_tcmd = 0;
    344   1.1    chopps 	*dev->sci_sel_enb = 0;
    345   1.1    chopps 
    346   1.1    chopps 	/* anything else was zeroed by reset */
    347   1.1    chopps 
    348   1.1    chopps 	splx (s);
    349   1.1    chopps 
    350  1.19  christos 	printf("sci id %d\n", my_id);
    351   1.1    chopps 	dev->sc_flags |= SCI_ALIVE;
    352   1.1    chopps }
    353   1.1    chopps 
    354   1.5    chopps void
    355  1.26   aymeric scierror(struct sci_softc *dev, u_char csr)
    356   1.1    chopps {
    357  1.20    bouyer 	struct scsipi_xfer *xs;
    358   1.1    chopps 
    359   1.5    chopps 	xs = dev->sc_xs;
    360   1.5    chopps 
    361   1.5    chopps #ifdef DIAGNOSTIC
    362   1.5    chopps 	if (xs == NULL)
    363   1.5    chopps 		panic("scierror");
    364   1.5    chopps #endif
    365  1.21   thorpej 	if (xs->xs_control & XS_CTL_SILENT)
    366   1.5    chopps 		return;
    367   1.5    chopps 
    368  1.19  christos 	printf("%s: ", dev->sc_dev.dv_xname);
    369  1.19  christos 	printf("csr == 0x%02i\n", csr);	/* XXX */
    370   1.1    chopps }
    371   1.1    chopps 
    372   1.5    chopps /*
    373   1.5    chopps  * select the bus, return when selected or error.
    374   1.5    chopps  */
    375   1.5    chopps int
    376  1.26   aymeric sciselectbus(struct sci_softc *dev, u_char target, u_char our_addr)
    377   1.1    chopps {
    378   1.1    chopps 	register int timeo = 2500;
    379   1.1    chopps 
    380   1.5    chopps 	QPRINTF (("sciselectbus %d\n", target));
    381   1.1    chopps 
    382   1.1    chopps 	/* if we're already selected, return */
    383   1.1    chopps 	if (dev->sc_flags & SCI_SELECTED)	/* XXXX */
    384   1.1    chopps 		return 1;
    385   1.1    chopps 
    386   1.1    chopps 	if ((*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    387   1.1    chopps 	    (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
    388   1.1    chopps 	    (*dev->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
    389   1.1    chopps 		return 1;
    390   1.1    chopps 
    391   1.1    chopps 	*dev->sci_tcmd = 0;
    392   1.1    chopps 	*dev->sci_odata = 0x80 + (1 << target);
    393   1.1    chopps 	*dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_SEL;
    394   1.1    chopps 	while ((*dev->sci_bus_csr & SCI_BUS_BSY) == 0) {
    395   1.1    chopps 		if (--timeo > 0) {
    396   1.6    chopps 			delay(100);
    397   1.1    chopps 		} else {
    398   1.1    chopps 			break;
    399   1.1    chopps 		}
    400   1.1    chopps 	}
    401   1.1    chopps 	if (timeo) {
    402   1.1    chopps 		*dev->sci_icmd = 0;
    403   1.1    chopps 		dev->sc_flags |= SCI_SELECTED;
    404   1.1    chopps 		return (0);
    405   1.1    chopps 	}
    406   1.1    chopps 	*dev->sci_icmd = 0;
    407   1.1    chopps 	return (1);
    408   1.1    chopps }
    409   1.1    chopps 
    410   1.5    chopps int
    411  1.26   aymeric sci_ixfer_out(register struct sci_softc *dev, int len, register u_char *buf,
    412  1.26   aymeric               int phase)
    413   1.1    chopps {
    414   1.1    chopps 	register int wait = sci_data_wait;
    415   1.1    chopps 	u_char csr;
    416   1.1    chopps 
    417   1.5    chopps 	QPRINTF(("sci_ixfer_out {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    418   1.1    chopps 	  len, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
    419   1.1    chopps 	  buf[6], buf[7], buf[8], buf[9]));
    420   1.1    chopps 
    421   1.1    chopps 	*dev->sci_tcmd = phase;
    422   1.1    chopps 	*dev->sci_icmd = SCI_ICMD_DATA;
    423   1.1    chopps 	for (;len > 0; len--) {
    424   1.1    chopps 		csr = *dev->sci_bus_csr;
    425   1.1    chopps 		while (!(csr & SCI_BUS_REQ)) {
    426   1.1    chopps 			if ((csr & SCI_BUS_BSY) == 0 || --wait < 0) {
    427   1.1    chopps #ifdef DEBUG
    428   1.1    chopps 				if (sci_debug)
    429  1.19  christos 					printf("sci_ixfer_out fail: l%d i%x w%d\n",
    430   1.1    chopps 					  len, csr, wait);
    431   1.1    chopps #endif
    432   1.1    chopps 				return (len);
    433   1.1    chopps 			}
    434   1.6    chopps 			delay(1);
    435   1.1    chopps 			csr = *dev->sci_bus_csr;
    436   1.1    chopps 		}
    437   1.1    chopps 
    438   1.1    chopps 		if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
    439   1.1    chopps 			break;
    440   1.1    chopps 		*dev->sci_odata = *buf;
    441   1.1    chopps 		*dev->sci_icmd = SCI_ICMD_DATA|SCI_ICMD_ACK;
    442   1.1    chopps 		buf++;
    443   1.1    chopps 		while (*dev->sci_bus_csr & SCI_BUS_REQ);
    444   1.1    chopps 		*dev->sci_icmd = SCI_ICMD_DATA;
    445   1.1    chopps 	}
    446   1.1    chopps 
    447   1.5    chopps 	QPRINTF(("sci_ixfer_out done\n"));
    448   1.1    chopps 	return (0);
    449   1.1    chopps }
    450   1.1    chopps 
    451   1.5    chopps void
    452  1.26   aymeric sci_ixfer_in(struct sci_softc *dev, int len, register u_char *buf, int phase)
    453   1.1    chopps {
    454   1.1    chopps 	int wait = sci_data_wait;
    455   1.1    chopps 	u_char csr;
    456   1.1    chopps 	volatile register u_char *sci_bus_csr = dev->sci_bus_csr;
    457   1.1    chopps 	volatile register u_char *sci_data = dev->sci_data;
    458   1.1    chopps 	volatile register u_char *sci_icmd = dev->sci_icmd;
    459  1.16     veego #ifdef DEBUG
    460  1.16     veego 	u_char *obp = buf;
    461  1.16     veego #endif
    462   1.1    chopps 
    463   1.1    chopps 	csr = *sci_bus_csr;
    464   1.1    chopps 
    465   1.5    chopps 	QPRINTF(("sci_ixfer_in %d, csr=%02x\n", len, csr));
    466   1.1    chopps 
    467   1.1    chopps 	*dev->sci_tcmd = phase;
    468   1.1    chopps 	*sci_icmd = 0;
    469   1.1    chopps 	for (;len > 0; len--) {
    470   1.1    chopps 		csr = *sci_bus_csr;
    471   1.1    chopps 		while (!(csr & SCI_BUS_REQ)) {
    472   1.1    chopps 			if (!(csr & SCI_BUS_BSY) || --wait < 0) {
    473   1.1    chopps #ifdef DEBUG
    474   1.1    chopps 				if (sci_debug)
    475  1.19  christos 					printf("sci_ixfer_in fail: l%d i%x w%d\n",
    476   1.1    chopps 					len, csr, wait);
    477   1.1    chopps #endif
    478   1.1    chopps 				return;
    479   1.1    chopps 			}
    480   1.1    chopps 
    481   1.6    chopps 			delay(1);
    482   1.1    chopps 			csr = *sci_bus_csr;
    483   1.1    chopps 		}
    484   1.1    chopps 
    485   1.1    chopps 		if (!(*dev->sci_csr & SCI_CSR_PHASE_MATCH))
    486   1.1    chopps 			break;
    487   1.1    chopps 		*buf = *sci_data;
    488   1.1    chopps 		*sci_icmd = SCI_ICMD_ACK;
    489   1.1    chopps 		buf++;
    490   1.1    chopps 		while (*sci_bus_csr & SCI_BUS_REQ);
    491   1.1    chopps 		*sci_icmd = 0;
    492   1.1    chopps 	}
    493   1.1    chopps 
    494   1.5    chopps 	QPRINTF(("sci_ixfer_in {%d} %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
    495   1.1    chopps 	  len, obp[0], obp[1], obp[2], obp[3], obp[4], obp[5],
    496   1.1    chopps 	  obp[6], obp[7], obp[8], obp[9]));
    497   1.1    chopps }
    498   1.1    chopps 
    499   1.1    chopps /*
    500   1.1    chopps  * SCSI 'immediate' command:  issue a command to some SCSI device
    501   1.1    chopps  * and get back an 'immediate' response (i.e., do programmed xfer
    502   1.1    chopps  * to get the response data).  'cbuf' is a buffer containing a scsi
    503   1.1    chopps  * command of length clen bytes.  'buf' is a buffer of length 'len'
    504   1.1    chopps  * bytes for data.  The transfer direction is determined by the device
    505   1.1    chopps  * (i.e., by the scsi bus data xfer phase).  If 'len' is zero, the
    506   1.1    chopps  * command must supply no data.  'xferphase' is the bus phase the
    507   1.1    chopps  * caller expects to happen after the command is issued.  It should
    508   1.1    chopps  * be one of DATA_IN_PHASE, DATA_OUT_PHASE or STATUS_PHASE.
    509   1.1    chopps  */
    510   1.5    chopps int
    511  1.26   aymeric sciicmd(struct sci_softc *dev, int target, void *cbuf, int clen, void *buf,
    512  1.26   aymeric         int len, u_char xferphase)
    513   1.1    chopps {
    514  1.16     veego 	u_char phase;
    515   1.1    chopps 	register int wait;
    516   1.1    chopps 
    517   1.1    chopps 	/* select the SCSI bus (it's an error if bus isn't free) */
    518   1.5    chopps 	if (sciselectbus (dev, target, dev->sc_scsi_addr))
    519   1.1    chopps 		return -1;
    520   1.1    chopps 	/*
    521   1.1    chopps 	 * Wait for a phase change (or error) then let the device
    522   1.1    chopps 	 * sequence us through the various SCSI phases.
    523   1.1    chopps 	 */
    524   1.1    chopps 	dev->sc_stat[0] = 0xff;
    525   1.1    chopps 	dev->sc_msg[0] = 0xff;
    526   1.1    chopps 	phase = CMD_PHASE;
    527   1.1    chopps 	while (1) {
    528   1.1    chopps 		wait = sci_cmd_wait;
    529   1.1    chopps 
    530   1.1    chopps 		while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) == SCI_BUS_BSY);
    531   1.1    chopps 
    532   1.1    chopps 		QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
    533   1.1    chopps 		if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
    534   1.1    chopps 			return -1;
    535   1.1    chopps 		}
    536   1.1    chopps 		phase = SCI_PHASE(*dev->sci_bus_csr);
    537   1.1    chopps 
    538   1.1    chopps 		switch (phase) {
    539   1.1    chopps 		case CMD_PHASE:
    540   1.5    chopps 			if (sci_ixfer_out (dev, clen, cbuf, phase))
    541   1.1    chopps 				goto abort;
    542   1.1    chopps 			phase = xferphase;
    543   1.1    chopps 			break;
    544   1.1    chopps 
    545   1.1    chopps 		case DATA_IN_PHASE:
    546   1.1    chopps 			if (len <= 0)
    547   1.1    chopps 				goto abort;
    548   1.1    chopps 			wait = sci_data_wait;
    549   1.5    chopps 			sci_ixfer_in (dev, len, buf, phase);
    550   1.1    chopps 			phase = STATUS_PHASE;
    551   1.1    chopps 			break;
    552   1.1    chopps 
    553   1.1    chopps 		case DATA_OUT_PHASE:
    554   1.1    chopps 			if (len <= 0)
    555   1.1    chopps 				goto abort;
    556   1.1    chopps 			wait = sci_data_wait;
    557   1.5    chopps 			if (sci_ixfer_out (dev, len, buf, phase))
    558   1.1    chopps 				goto abort;
    559   1.1    chopps 			phase = STATUS_PHASE;
    560   1.1    chopps 			break;
    561   1.1    chopps 
    562   1.1    chopps 		case MESG_IN_PHASE:
    563   1.1    chopps 			dev->sc_msg[0] = 0xff;
    564   1.5    chopps 			sci_ixfer_in (dev, 1, dev->sc_msg,phase);
    565   1.1    chopps 			dev->sc_flags &= ~SCI_SELECTED;
    566   1.1    chopps 			while (*dev->sci_bus_csr & SCI_BUS_BSY);
    567   1.1    chopps 			goto out;
    568   1.1    chopps 			break;
    569   1.1    chopps 
    570   1.1    chopps 		case MESG_OUT_PHASE:
    571   1.1    chopps 			phase = STATUS_PHASE;
    572   1.1    chopps 			break;
    573   1.1    chopps 
    574   1.1    chopps 		case STATUS_PHASE:
    575   1.5    chopps 			sci_ixfer_in (dev, 1, dev->sc_stat, phase);
    576   1.1    chopps 			phase = MESG_IN_PHASE;
    577   1.1    chopps 			break;
    578   1.1    chopps 
    579   1.1    chopps 		case BUS_FREE_PHASE:
    580   1.1    chopps 			goto out;
    581   1.1    chopps 
    582   1.1    chopps 		default:
    583  1.19  christos 		printf("sci: unexpected phase %d in icmd from %d\n",
    584   1.1    chopps 		  phase, target);
    585   1.1    chopps 		goto abort;
    586   1.1    chopps 		}
    587   1.1    chopps #if 0
    588   1.1    chopps 		if (wait <= 0)
    589   1.1    chopps 			goto abort;
    590   1.1    chopps #endif
    591   1.1    chopps 	}
    592   1.1    chopps 
    593   1.1    chopps abort:
    594   1.5    chopps 	sciabort(dev, "icmd");
    595   1.1    chopps out:
    596   1.1    chopps 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
    597   1.1    chopps 	return (dev->sc_stat[0]);
    598   1.1    chopps }
    599   1.1    chopps 
    600   1.1    chopps int
    601  1.26   aymeric scigo(struct sci_softc *dev, struct scsipi_xfer *xs)
    602   1.1    chopps {
    603  1.16     veego 	int count, target;
    604  1.16     veego 	u_char phase, *addr;
    605   1.5    chopps 
    606  1.25    bouyer 	target = xs->xs_periph->periph_target;
    607   1.5    chopps 	count = xs->datalen;
    608   1.5    chopps 	addr = xs->data;
    609   1.1    chopps 
    610   1.1    chopps 	if (sci_no_dma)	{
    611   1.5    chopps 		sciicmd (dev, target, (u_char *) xs->cmd, xs->cmdlen,
    612   1.1    chopps 		  addr, count,
    613  1.21   thorpej 		  xs->xs_control & XS_CTL_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE);
    614   1.1    chopps 
    615   1.5    chopps 		return (1);
    616   1.1    chopps 	}
    617   1.1    chopps 
    618   1.1    chopps 	/* select the SCSI bus (it's an error if bus isn't free) */
    619   1.5    chopps 	if (sciselectbus (dev, target, dev->sc_scsi_addr))
    620   1.1    chopps 		return -1;
    621   1.1    chopps 	/*
    622   1.1    chopps 	 * Wait for a phase change (or error) then let the device
    623   1.1    chopps 	 * sequence us through the various SCSI phases.
    624   1.1    chopps 	 */
    625   1.1    chopps 	dev->sc_stat[0] = 0xff;
    626   1.1    chopps 	dev->sc_msg[0] = 0xff;
    627   1.1    chopps 	phase = CMD_PHASE;
    628   1.1    chopps 	while (1) {
    629   1.1    chopps 		while ((*dev->sci_bus_csr & (SCI_BUS_REQ|SCI_BUS_BSY)) ==
    630   1.1    chopps 		  SCI_BUS_BSY);
    631   1.1    chopps 
    632   1.1    chopps 		QPRINTF((">CSR:%02x<", *dev->sci_bus_csr));
    633   1.1    chopps 		if ((*dev->sci_bus_csr & SCI_BUS_REQ) == 0) {
    634   1.1    chopps 			goto abort;
    635   1.1    chopps 		}
    636   1.1    chopps 		phase = SCI_PHASE(*dev->sci_bus_csr);
    637   1.1    chopps 
    638   1.1    chopps 		switch (phase) {
    639   1.1    chopps 		case CMD_PHASE:
    640  1.16     veego 			if (sci_ixfer_out (dev, xs->cmdlen, (u_char *) xs->cmd, phase))
    641   1.1    chopps 				goto abort;
    642  1.21   thorpej 			phase = xs->xs_control & XS_CTL_DATA_IN ? DATA_IN_PHASE : DATA_OUT_PHASE;
    643   1.1    chopps 			break;
    644   1.1    chopps 
    645   1.1    chopps 		case DATA_IN_PHASE:
    646   1.1    chopps 			if (count <= 0)
    647   1.1    chopps 				goto abort;
    648   1.1    chopps 			/* XXX use psuedo DMA if available */
    649   1.1    chopps 			if (count >= 128 && dev->dma_xfer_in)
    650   1.1    chopps 				(*dev->dma_xfer_in)(dev, count, addr, phase);
    651   1.1    chopps 			else
    652   1.5    chopps 				sci_ixfer_in (dev, count, addr, phase);
    653   1.1    chopps 			phase = STATUS_PHASE;
    654   1.1    chopps 			break;
    655   1.1    chopps 
    656   1.1    chopps 		case DATA_OUT_PHASE:
    657   1.1    chopps 			if (count <= 0)
    658   1.1    chopps 				goto abort;
    659   1.1    chopps 			/* XXX use psuedo DMA if available */
    660   1.1    chopps 			if (count >= 128 && dev->dma_xfer_out)
    661   1.1    chopps 				(*dev->dma_xfer_out)(dev, count, addr, phase);
    662   1.1    chopps 			else
    663   1.5    chopps 				if (sci_ixfer_out (dev, count, addr, phase))
    664   1.1    chopps 					goto abort;
    665   1.1    chopps 			phase = STATUS_PHASE;
    666   1.1    chopps 			break;
    667   1.1    chopps 
    668   1.1    chopps 		case MESG_IN_PHASE:
    669   1.1    chopps 			dev->sc_msg[0] = 0xff;
    670   1.5    chopps 			sci_ixfer_in (dev, 1, dev->sc_msg,phase);
    671   1.1    chopps 			dev->sc_flags &= ~SCI_SELECTED;
    672   1.1    chopps 			while (*dev->sci_bus_csr & SCI_BUS_BSY);
    673   1.1    chopps 			goto out;
    674   1.1    chopps 			break;
    675   1.1    chopps 
    676   1.1    chopps 		case MESG_OUT_PHASE:
    677   1.1    chopps 			phase = STATUS_PHASE;
    678   1.1    chopps 			break;
    679   1.1    chopps 
    680   1.1    chopps 		case STATUS_PHASE:
    681   1.5    chopps 			sci_ixfer_in (dev, 1, dev->sc_stat, phase);
    682   1.1    chopps 			phase = MESG_IN_PHASE;
    683   1.1    chopps 			break;
    684   1.1    chopps 
    685   1.1    chopps 		case BUS_FREE_PHASE:
    686   1.1    chopps 			goto out;
    687   1.1    chopps 
    688   1.1    chopps 		default:
    689  1.19  christos 		printf("sci: unexpected phase %d in icmd from %d\n",
    690   1.5    chopps 		  phase, target);
    691   1.1    chopps 		goto abort;
    692   1.1    chopps 		}
    693   1.1    chopps 	}
    694   1.1    chopps 
    695   1.1    chopps abort:
    696   1.5    chopps 	sciabort(dev, "go");
    697   1.1    chopps out:
    698   1.1    chopps 	QPRINTF(("=STS:%02x=", dev->sc_stat[0]));
    699   1.5    chopps 	return (1);
    700   1.1    chopps }
    701