Home | History | Annotate | Line # | Download | only in dev
si.c revision 1.21
      1  1.21      gwr /*	$NetBSD: si.c,v 1.21 1995/08/14 20:00:00 gwr Exp $	*/
      2   1.8      cgd 
      3   1.1    glass /*
      4   1.2      gwr  * Copyright (C) 1994 Adam Glass, Gordon W. Ross
      5   1.1    glass  * Copyright (C) 1993	Allen K. Briggs, Chris P. Caputo,
      6   1.1    glass  *			Michael L. Finch, Bradley A. Grantham, and
      7   1.1    glass  *			Lawrence A. Kesteloot
      8   1.1    glass  * All rights reserved.
      9   1.1    glass  *
     10   1.1    glass  * Redistribution and use in source and binary forms, with or without
     11   1.1    glass  * modification, are permitted provided that the following conditions
     12   1.1    glass  * are met:
     13   1.1    glass  * 1. Redistributions of source code must retain the above copyright
     14   1.1    glass  *    notice, this list of conditions and the following disclaimer.
     15   1.1    glass  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    glass  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    glass  *    documentation and/or other materials provided with the distribution.
     18   1.1    glass  * 3. All advertising materials mentioning features or use of this software
     19   1.1    glass  *    must display the following acknowledgement:
     20   1.1    glass  *	This product includes software developed by the Alice Group.
     21   1.1    glass  * 4. The names of the Alice Group or any of its members may not be used
     22   1.1    glass  *    to endorse or promote products derived from this software without
     23   1.1    glass  *    specific prior written permission.
     24   1.1    glass  *
     25   1.1    glass  * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
     26   1.1    glass  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27   1.1    glass  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28   1.1    glass  * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
     29   1.1    glass  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     30   1.1    glass  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     31   1.1    glass  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     32   1.1    glass  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     33   1.1    glass  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     34   1.1    glass  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.1    glass  */
     36   1.1    glass 
     37  1.14      gwr #define DEBUG 1
     38   1.2      gwr 
     39   1.2      gwr /* XXX - Need to add support for real DMA. -gwr */
     40   1.2      gwr /* #define PSEUDO_DMA 1 (broken) */
     41   1.1    glass 
     42   1.1    glass #include <sys/types.h>
     43   1.1    glass #include <sys/malloc.h>
     44   1.1    glass #include <sys/param.h>
     45   1.1    glass #include <sys/systm.h>
     46   1.1    glass #include <sys/errno.h>
     47   1.1    glass #include <sys/buf.h>
     48   1.1    glass #include <sys/proc.h>
     49   1.1    glass #include <sys/user.h>
     50   1.1    glass #include <sys/device.h>
     51   1.1    glass 
     52   1.1    glass #include <machine/autoconf.h>
     53   1.1    glass #include <machine/isr.h>
     54   1.1    glass #include <machine/obio.h>
     55   1.1    glass 
     56   1.2      gwr #include <scsi/scsi_all.h>
     57   1.2      gwr #include <scsi/scsi_debug.h>
     58   1.2      gwr #include <scsi/scsiconf.h>
     59   1.2      gwr 
     60   1.1    glass #include "scsi_defs.h"
     61   1.1    glass #include "scsi_5380.h"
     62  1.14      gwr #include "scsi_sunsi.h"
     63  1.14      gwr 
     64  1.14      gwr #ifdef	DEBUG
     65  1.14      gwr static int si_debug = 0;
     66  1.14      gwr static int si_flags = 0 /* | SDEV_DB2 */ ;
     67  1.14      gwr #endif
     68   1.1    glass 
     69   1.1    glass #define SCI_PHASE_DISC		0	/* sort of ... */
     70  1.10      gwr #define SCI_CLR_INTR(regs)	((volatile)(regs->sci_iack))
     71   1.1    glass #define SCI_ACK(ptr,phase)	(ptr)->sci_tcmd = (phase)
     72  1.17      gwr #define SCSI_TIMEOUT_VAL	1000000
     73   1.1    glass #define WAIT_FOR_NOT_REQ(ptr) {	\
     74   1.1    glass 	int scsi_timeout = SCSI_TIMEOUT_VAL; \
     75   1.1    glass 	while ( ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
     76   1.1    glass 		 ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
     77   1.1    glass 		 ((ptr)->sci_bus_csr & SCI_BUS_REQ) && \
     78   1.1    glass 		 (--scsi_timeout) ); \
     79   1.1    glass 	if (!scsi_timeout) { \
     80   1.2      gwr 		printf("scsi timeout--WAIT_FOR_NOT_REQ---%s, line %d.\n", \
     81   1.2      gwr 			__FILE__, __LINE__); \
     82   1.1    glass 		goto scsi_timeout_error; \
     83   1.1    glass 	} \
     84   1.1    glass 	}
     85   1.1    glass #define WAIT_FOR_REQ(ptr) {	\
     86   1.1    glass 	int scsi_timeout = SCSI_TIMEOUT_VAL; \
     87   1.1    glass 	while ( (((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
     88   1.1    glass 		(((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
     89   1.1    glass 		(((ptr)->sci_bus_csr & SCI_BUS_REQ) == 0) && \
     90   1.1    glass 		 (--scsi_timeout) ); \
     91   1.1    glass 	if (!scsi_timeout) { \
     92   1.2      gwr 		printf("scsi timeout--WAIT_FOR_REQ---%s, line %d.\n", \
     93   1.2      gwr 			__FILE__, __LINE__); \
     94   1.1    glass 		goto scsi_timeout_error; \
     95   1.1    glass 	} \
     96   1.1    glass 	}
     97   1.1    glass #define WAIT_FOR_BSY(ptr) {	\
     98   1.1    glass 	int scsi_timeout = SCSI_TIMEOUT_VAL; \
     99   1.1    glass 	while ( (((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
    100   1.1    glass 		(((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
    101   1.1    glass 		(((ptr)->sci_bus_csr & SCI_BUS_BSY) == 0) && \
    102   1.1    glass 		 (--scsi_timeout) ); \
    103   1.1    glass 	if (!scsi_timeout) { \
    104   1.2      gwr 		printf("scsi timeout--WAIT_FOR_BSY---%s, line %d.\n", \
    105   1.2      gwr 			__FILE__, __LINE__); \
    106   1.1    glass 		goto scsi_timeout_error; \
    107   1.1    glass 	} \
    108   1.1    glass 	}
    109   1.1    glass 
    110  1.14      gwr #define ARBITRATION_RETRIES 1000
    111  1.14      gwr 
    112   1.1    glass #ifdef DDB
    113   1.1    glass int Debugger();
    114   1.1    glass #else
    115   1.2      gwr #define Debugger() panic("Should call Debugger here %s:%d", \
    116   1.2      gwr 			 __FILE__, __LINE__)
    117   1.1    glass #endif
    118   1.1    glass 
    119   1.1    glass struct ncr5380_softc {
    120   1.1    glass     struct device sc_dev;
    121  1.10      gwr     volatile void *sc_regs;
    122  1.14      gwr     int sc_adapter_type;
    123  1.17      gwr     int sc_adapter_iv_am;	/* int. vec + address modifier */
    124   1.1    glass     struct scsi_link sc_link;
    125   1.1    glass };
    126   1.1    glass 
    127  1.20  mycroft static void		ncr5380_minphys(struct buf *bp);
    128   1.3      gwr static int		ncr5380_scsi_cmd(struct scsi_xfer *xs);
    129  1.14      gwr static int		ncr5380_reset_adapter(struct ncr5380_softc *);
    130  1.14      gwr static int		ncr5380_reset_scsibus(struct ncr5380_softc *);
    131   1.3      gwr static int		ncr5380_poll(int adapter, int timeout);
    132   1.3      gwr static int		ncr5380_send_cmd(struct scsi_xfer *xs);
    133   1.1    glass 
    134  1.14      gwr static int		ncr_intr(void *);
    135   1.1    glass 
    136   1.3      gwr static int	si_generic(int adapter, int id, int lun,
    137   1.1    glass 			 struct scsi_generic *cmd, int cmdlen,
    138   1.1    glass 			 void *databuf, int datalen);
    139   1.3      gwr static int	si_group0(int adapter, int id, int lun,
    140   1.1    glass 			    int opcode, int addr, int len,
    141   1.1    glass 			    int flags, caddr_t databuf, int datalen);
    142   1.1    glass 
    143   1.1    glass static char scsi_name[] = "si";
    144   1.1    glass 
    145   1.1    glass struct scsi_adapter	ncr5380_switch = {
    146   1.1    glass 	ncr5380_scsi_cmd,		/* scsi_cmd()		*/
    147   1.1    glass 	ncr5380_minphys,		/* scsi_minphys()	*/
    148  1.12      gwr 	NULL,				/* open_target_lu()	*/
    149  1.12      gwr 	NULL,				/* close_target_lu()	*/
    150   1.1    glass };
    151   1.1    glass 
    152   1.1    glass /* This is copied from julian's bt driver */
    153   1.1    glass /* "so we have a default dev struct for our link struct." */
    154   1.1    glass struct scsi_device ncr_dev = {
    155   1.1    glass 	NULL,		/* Use default error handler.	    */
    156   1.2      gwr 	NULL,		/* Use default start handler.		*/
    157   1.2      gwr 	NULL,		/* Use default async handler.	    */
    158   1.1    glass 	NULL,		/* Use default "done" routine.	    */
    159   1.1    glass };
    160   1.1    glass 
    161   1.3      gwr static int	si_match();
    162   1.3      gwr static void	si_attach();
    163   1.1    glass 
    164   1.2      gwr struct cfdriver sicd = {
    165   1.2      gwr 	NULL, "si", si_match, si_attach, DV_DULL,
    166   1.2      gwr 	sizeof(struct ncr5380_softc), NULL, 0,
    167   1.2      gwr };
    168   1.1    glass 
    169   1.3      gwr static int
    170   1.2      gwr si_print(aux, name)
    171   1.1    glass 	void *aux;
    172   1.1    glass 	char *name;
    173   1.1    glass {
    174   1.1    glass }
    175   1.1    glass 
    176   1.3      gwr static int
    177  1.10      gwr si_match(parent, vcf, args)
    178   1.1    glass 	struct device	*parent;
    179  1.10      gwr 	void		*vcf, *args;
    180   1.1    glass {
    181  1.10      gwr 	struct cfdata	*cf = vcf;
    182  1.10      gwr 	struct confargs *ca = args;
    183  1.16      gwr 	int x, probe_addr;
    184  1.10      gwr 
    185  1.16      gwr 	/* Default interrupt priority always splbio==2 */
    186  1.16      gwr 	if (ca->ca_intpri == -1)
    187  1.16      gwr 		ca->ca_intpri = 2;
    188  1.16      gwr 
    189  1.16      gwr 	if ((cpu_machine_id == SUN3_MACH_50) ||
    190  1.16      gwr 	    (cpu_machine_id == SUN3_MACH_60) )
    191  1.16      gwr 	{
    192  1.16      gwr 		/* Sun3/50 or Sun3/60 have only OBIO "si" */
    193  1.16      gwr 		if (ca->ca_bustype != BUS_OBIO)
    194  1.16      gwr 			return(0);
    195  1.13      gwr 		if (ca->ca_paddr == -1)
    196  1.13      gwr 			ca->ca_paddr = OBIO_NCR_SCSI;
    197  1.16      gwr 		/* OK... */
    198  1.16      gwr 	} else {
    199  1.16      gwr 		/* Other Sun3 models may have VME "si" or "sc" */
    200  1.16      gwr 		if (ca->ca_bustype != BUS_VME16)
    201  1.16      gwr 			return (0);
    202  1.13      gwr 		if (ca->ca_paddr == -1)
    203  1.13      gwr 			return (0);
    204  1.16      gwr 		/* OK... */
    205  1.16      gwr 	}
    206  1.16      gwr 
    207  1.16      gwr 	/* Make sure there is something there... */
    208  1.16      gwr 	x = bus_peek(ca->ca_bustype, ca->ca_paddr + 1, 1);
    209  1.16      gwr 	if (x == -1)
    210  1.13      gwr 		return (0);
    211  1.16      gwr 
    212  1.16      gwr 	/*
    213  1.16      gwr 	 * If this is a VME SCSI board, we have to determine whether
    214  1.16      gwr 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
    215  1.16      gwr 	 * be determined using the fact that the "sc" board occupies
    216  1.16      gwr 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
    217  1.16      gwr 	 */
    218  1.16      gwr 	if (ca->ca_bustype == BUS_VME16) {
    219  1.16      gwr 		/* Note, the "si" board should NOT respond here. */
    220  1.16      gwr 		x = bus_peek(ca->ca_bustype, ca->ca_paddr + 0x801, 1);
    221  1.16      gwr 		if (x != -1)
    222  1.16      gwr 			return(0);
    223  1.13      gwr 	}
    224  1.13      gwr 
    225  1.16      gwr     return (1);
    226   1.1    glass }
    227   1.1    glass 
    228   1.3      gwr static void
    229  1.10      gwr si_attach(parent, self, args)
    230   1.1    glass 	struct device	*parent, *self;
    231  1.10      gwr 	void		*args;
    232   1.1    glass {
    233  1.10      gwr 	struct ncr5380_softc *ncr5380 = (struct ncr5380_softc *) self;
    234  1.14      gwr 	volatile sci_regmap_t *regs;
    235  1.10      gwr 	struct confargs *ca = args;
    236   1.1    glass 
    237  1.12      gwr 	switch (ca->ca_bustype) {
    238  1.10      gwr 
    239  1.12      gwr 	case BUS_OBIO:
    240  1.12      gwr 		regs = (sci_regmap_t *)
    241  1.12      gwr 			obio_alloc(ca->ca_paddr, sizeof(*regs));
    242  1.14      gwr 		isr_add_autovect(ncr_intr, (void *)ncr5380,
    243  1.12      gwr 						 ca->ca_intpri);
    244  1.12      gwr 		break;
    245  1.12      gwr 
    246  1.12      gwr 	case BUS_VME16:
    247  1.12      gwr 		regs = (sci_regmap_t *)
    248  1.12      gwr 			bus_mapin(ca->ca_bustype, ca->ca_paddr, sizeof(*regs));
    249  1.14      gwr 		isr_add_vectored(ncr_intr, (void *)ncr5380,
    250  1.12      gwr 						 ca->ca_intpri, ca->ca_intvec);
    251  1.12      gwr 		break;
    252  1.12      gwr 
    253  1.12      gwr 	default:
    254  1.12      gwr 		printf("unknown\n");
    255  1.12      gwr 		return;
    256  1.12      gwr 	}
    257   1.1    glass 
    258  1.14      gwr 	ncr5380->sc_adapter_type = ca->ca_bustype;
    259  1.17      gwr 	ncr5380->sc_adapter_iv_am =
    260  1.17      gwr 		VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF);
    261  1.10      gwr 	ncr5380->sc_regs = regs;
    262  1.12      gwr 
    263  1.12      gwr 	/*
    264  1.12      gwr 	 * fill in the prototype scsi_link.
    265  1.12      gwr 	 */
    266   1.2      gwr     ncr5380->sc_link.adapter_softc = ncr5380;
    267  1.12      gwr     ncr5380->sc_link.adapter_target = 7;
    268   1.1    glass     ncr5380->sc_link.adapter = &ncr5380_switch;
    269   1.1    glass     ncr5380->sc_link.device = &ncr_dev;
    270  1.14      gwr     ncr5380->sc_link.openings = 2;
    271  1.13      gwr #ifdef	DEBUG
    272  1.14      gwr     ncr5380->sc_link.flags |= si_flags;
    273  1.13      gwr #endif
    274   1.1    glass 
    275  1.12      gwr     printf("\n");
    276  1.14      gwr 	ncr5380_reset_adapter(ncr5380);
    277  1.14      gwr 	ncr5380_reset_scsibus(ncr5380);
    278  1.10      gwr 	config_found(self, &(ncr5380->sc_link), si_print);
    279   1.1    glass }
    280   1.1    glass 
    281   1.1    glass #define MIN_PHYS	65536	/*BARF!!!!*/
    282  1.20  mycroft static void
    283   1.1    glass ncr5380_minphys(struct buf *bp)
    284   1.1    glass {
    285   1.1    glass 	if (bp->b_bcount > MIN_PHYS) {
    286   1.1    glass 		printf("Uh-oh...  ncr5380_minphys setting bp->b_bcount = %x.\n", MIN_PHYS);
    287   1.1    glass 		bp->b_bcount = MIN_PHYS;
    288   1.1    glass 	}
    289  1.20  mycroft 	minphys(bp);
    290   1.1    glass }
    291   1.1    glass #undef MIN_PHYS
    292   1.1    glass 
    293   1.5      gwr static int
    294   1.1    glass ncr5380_scsi_cmd(struct scsi_xfer *xs)
    295   1.1    glass {
    296   1.1    glass 	int flags, s, r;
    297   1.1    glass 
    298   1.1    glass 	flags = xs->flags;
    299   1.1    glass 	if (xs->bp) flags |= (SCSI_NOSLEEP);
    300   1.1    glass 	if ( flags & ITSDONE ) {
    301   1.1    glass 		printf("Already done?");
    302   1.1    glass 		xs->flags &= ~ITSDONE;
    303   1.1    glass 	}
    304   1.1    glass 	if ( ! ( flags & INUSE ) ) {
    305   1.1    glass 		printf("Not in use?");
    306   1.1    glass 		xs->flags |= INUSE;
    307   1.1    glass 	}
    308   1.1    glass 
    309  1.15      gwr 	s = splbio();
    310  1.14      gwr 
    311   1.1    glass 	if ( flags & SCSI_RESET ) {
    312   1.1    glass 		printf("flags & SCSIRESET.\n");
    313  1.14      gwr 		ncr5380_reset_scsibus(xs->sc_link->adapter_softc);
    314  1.14      gwr 		r = COMPLETE;
    315  1.14      gwr 	} else {
    316  1.14      gwr 		r = ncr5380_send_cmd(xs);
    317  1.14      gwr 		xs->flags |= ITSDONE;
    318  1.14      gwr 		scsi_done(xs);
    319   1.1    glass 	}
    320  1.14      gwr 
    321  1.15      gwr 	splx(s);
    322   1.1    glass 
    323   1.1    glass 	switch(r) {
    324  1.14      gwr 	case COMPLETE:
    325  1.14      gwr 	case SUCCESSFULLY_QUEUED:
    326  1.14      gwr 		r = SUCCESSFULLY_QUEUED;
    327  1.14      gwr 		if (xs->flags & SCSI_POLL)
    328  1.14      gwr 			r = COMPLETE;
    329  1.14      gwr 		break;
    330  1.14      gwr 	default:
    331  1.14      gwr 		break;
    332   1.1    glass 	}
    333   1.1    glass 	return r;
    334   1.1    glass }
    335   1.1    glass 
    336  1.14      gwr #ifdef	DEBUG
    337   1.3      gwr static int
    338   1.1    glass ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
    339   1.1    glass {
    340   1.1    glass 	u_char	*b = (u_char *) xs->cmd;
    341   1.1    glass 	int	i  = 0;
    342   1.1    glass 
    343   1.1    glass 	if ( ! ( xs->flags & SCSI_RESET ) ) {
    344   1.1    glass 		printf("si(%d:%d:%d)-",
    345   1.2      gwr 			   xs->sc_link->scsibus,
    346   1.2      gwr 			   xs->sc_link->target,
    347   1.2      gwr 			   xs->sc_link->lun);
    348   1.1    glass 		while (i < xs->cmdlen) {
    349   1.1    glass 			if (i) printf(",");
    350   1.1    glass 			printf("%x",b[i++]);
    351   1.1    glass 		}
    352   1.1    glass 		printf("-\n");
    353   1.1    glass 	} else {
    354   1.1    glass 		printf("si(%d:%d:%d)-RESET-\n",
    355   1.2      gwr 			   xs->sc_link->scsibus,
    356   1.2      gwr 			   xs->sc_link->target,
    357   1.2      gwr 			   xs->sc_link->lun);
    358   1.1    glass 	}
    359   1.1    glass }
    360  1.14      gwr #endif
    361   1.1    glass 
    362   1.1    glass /*
    363   1.1    glass  * Actual chip control.
    364   1.1    glass  */
    365   1.1    glass 
    366  1.14      gwr static void
    367  1.14      gwr ncr_sbc_intr(struct ncr5380_softc *ncr5380)
    368   1.1    glass {
    369  1.14      gwr 	volatile sci_regmap_t *regs = ncr5380->sc_regs;
    370   1.1    glass 
    371  1.14      gwr 	if ((regs->sci_csr & SCI_CSR_INT) == 0) {
    372   1.2      gwr #ifdef	DEBUG
    373  1.14      gwr 		printf (" ncr_sbc_intr: spurrious\n");
    374   1.2      gwr #endif
    375  1.14      gwr 		return;
    376  1.10      gwr 	}
    377  1.14      gwr 
    378  1.14      gwr 	SCI_CLR_INTR(regs);
    379  1.14      gwr #ifdef	DEBUG
    380  1.14      gwr 	printf (" ncr_sbc_intr\n");
    381  1.14      gwr #endif
    382   1.1    glass }
    383   1.1    glass 
    384  1.14      gwr static void
    385  1.14      gwr ncr_dma_intr(struct ncr5380_softc *ncr5380)
    386   1.1    glass {
    387  1.14      gwr 	volatile struct si_regs *regs = ncr5380->sc_regs;
    388  1.14      gwr 
    389  1.14      gwr #ifdef	DEBUG
    390  1.14      gwr 	printf (" ncr_dma_intr\n");
    391   1.2      gwr #endif
    392   1.1    glass }
    393   1.1    glass 
    394  1.14      gwr static int
    395  1.14      gwr ncr_intr(void *arg)
    396   1.1    glass {
    397  1.14      gwr 	struct ncr5380_softc *ncr5380 = arg;
    398  1.14      gwr 	volatile struct si_regs *si = ncr5380->sc_regs;
    399  1.14      gwr 	int rv = 0;
    400  1.14      gwr 
    401  1.14      gwr 	/* Interrupts not enabled?  Can not be for us. */
    402  1.14      gwr 	if ((si->si_csr & SI_CSR_INTR_EN) == 0)
    403  1.14      gwr 		return rv;
    404  1.14      gwr 
    405  1.14      gwr 	if (si->si_csr & SI_CSR_DMA_IP) {
    406  1.14      gwr 		ncr_dma_intr(ncr5380);
    407  1.14      gwr 		rv++;
    408  1.14      gwr 	}
    409  1.14      gwr 	if (si->si_csr & SI_CSR_SBC_IP) {
    410  1.14      gwr 		ncr_sbc_intr(ncr5380);
    411  1.14      gwr 		rv++;
    412  1.14      gwr 	}
    413  1.14      gwr 	return rv;
    414  1.14      gwr }
    415  1.14      gwr 
    416  1.14      gwr static int
    417  1.17      gwr ncr5380_reset_adapter(struct ncr5380_softc *sc)
    418  1.14      gwr {
    419  1.17      gwr 	volatile struct si_regs *si = sc->sc_regs;
    420  1.14      gwr 
    421  1.14      gwr #ifdef	DEBUG
    422  1.14      gwr 	if (si_debug) {
    423  1.14      gwr 		printf("si_reset_adapter\n");
    424  1.14      gwr 	}
    425   1.2      gwr #endif
    426  1.14      gwr 
    427  1.17      gwr 	/* The reset bits in the CSR are active low. */
    428  1.14      gwr 	si->si_csr = 0;
    429  1.17      gwr 	delay(20);
    430  1.14      gwr 	si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES;
    431  1.17      gwr 	si->fifo_count = 0;
    432  1.17      gwr 	if (sc->sc_adapter_type == BUS_VME16) {
    433  1.17      gwr 		si->dma_addrh = 0;
    434  1.17      gwr 		si->dma_addrl = 0;
    435  1.17      gwr 		si->dma_counth = 0;
    436  1.17      gwr 		si->dma_countl = 0;
    437  1.17      gwr 		si->iv_am = sc->sc_adapter_iv_am;
    438  1.17      gwr 	}
    439   1.1    glass }
    440   1.1    glass 
    441   1.3      gwr static int
    442  1.14      gwr ncr5380_reset_scsibus(struct ncr5380_softc *ncr5380)
    443   1.1    glass {
    444  1.14      gwr 	volatile sci_regmap_t *regs = ncr5380->sc_regs;
    445  1.14      gwr 
    446  1.14      gwr #ifdef	DEBUG
    447  1.14      gwr 	if (si_debug) {
    448  1.14      gwr 		printf("si_reset_scsibus\n");
    449  1.14      gwr 	}
    450  1.14      gwr #endif
    451   1.2      gwr 
    452  1.17      gwr 	regs->sci_icmd = SCI_ICMD_RST;
    453  1.17      gwr 	delay(100);
    454   1.2      gwr 	regs->sci_icmd = 0;
    455   1.1    glass 
    456   1.2      gwr 	regs->sci_mode = 0;
    457   1.2      gwr 	regs->sci_tcmd = SCI_PHASE_DISC;
    458   1.2      gwr 	regs->sci_sel_enb = 0;
    459   1.1    glass 
    460   1.2      gwr 	SCI_CLR_INTR(regs);
    461  1.17      gwr 	/* XXX - Need long delay here! */
    462   1.1    glass }
    463   1.1    glass 
    464   1.3      gwr static int
    465   1.1    glass ncr5380_poll(int adapter, int timeout)
    466   1.1    glass {
    467   1.1    glass }
    468   1.1    glass 
    469   1.3      gwr static int
    470   1.1    glass ncr5380_send_cmd(struct scsi_xfer *xs)
    471   1.1    glass {
    472   1.1    glass 	int	sense;
    473   1.1    glass 
    474  1.14      gwr #ifdef	DIAGNOSTIC
    475  1.14      gwr 	if ((getsr() & PSL_IPL) < PSL_IPL2)
    476  1.14      gwr 		panic("ncr_send_cmd: bad spl");
    477  1.14      gwr #endif
    478  1.14      gwr 
    479  1.14      gwr #ifdef	DEBUG
    480  1.14      gwr 	if (si_debug & 2)
    481  1.14      gwr 		ncr5380_show_scsi_cmd(xs);
    482  1.14      gwr #endif
    483  1.14      gwr 
    484   1.2      gwr 	sense = si_generic( xs->sc_link->scsibus, xs->sc_link->target,
    485   1.1    glass 			  xs->sc_link->lun, xs->cmd, xs->cmdlen,
    486   1.1    glass 			  xs->data, xs->datalen );
    487  1.14      gwr 
    488  1.13      gwr 	switch (sense) {
    489  1.13      gwr 	case 0:	/* success */
    490  1.13      gwr 		xs->resid = 0;
    491  1.13      gwr 		xs->error = XS_NOERROR;
    492  1.13      gwr 		break;
    493  1.13      gwr 
    494  1.13      gwr 	case 0x02:	/* Check condition */
    495   1.2      gwr #ifdef	DEBUG
    496  1.14      gwr 		if (si_debug)
    497  1.14      gwr 			printf("check cond. target %d.\n",
    498  1.14      gwr 				   xs->sc_link->target);
    499   1.2      gwr #endif
    500  1.13      gwr 		delay(10);	/* Phil's fix for slow devices. */
    501  1.13      gwr 		si_group0(xs->sc_link->scsibus,
    502  1.13      gwr 				  xs->sc_link->target,
    503  1.13      gwr 				  xs->sc_link->lun,
    504  1.13      gwr 				  0x3, 0x0,
    505  1.13      gwr 				  sizeof(struct scsi_sense_data),
    506  1.13      gwr 				  0, (caddr_t) &(xs->sense),
    507  1.13      gwr 				  sizeof(struct scsi_sense_data));
    508  1.13      gwr 		xs->error = XS_SENSE;
    509  1.13      gwr 		break;
    510  1.17      gwr 	case 0x08:	/* Busy - common code will delay, retry. */
    511  1.13      gwr 		xs->error = XS_BUSY;
    512  1.13      gwr 		break;
    513  1.17      gwr 	default:	/* Dead - tell common code to give up. */
    514  1.13      gwr 		xs->error = XS_DRIVER_STUFFUP;
    515  1.13      gwr 		break;
    516  1.13      gwr 
    517   1.1    glass 	}
    518   1.1    glass 	return (COMPLETE);
    519   1.1    glass }
    520   1.1    glass 
    521   1.3      gwr static int
    522   1.2      gwr si_select_target(register volatile sci_regmap_t *regs,
    523   1.1    glass 	      u_char myid, u_char tid, int with_atn)
    524   1.1    glass {
    525   1.1    glass 	register u_char	bid, icmd;
    526   1.1    glass 	int		ret = SCSI_RET_RETRY;
    527  1.14      gwr 	int 	arb_retries, arb_wait;
    528   1.1    glass 
    529   1.1    glass 	/* for our purposes.. */
    530   1.1    glass 	myid = 1 << myid;
    531   1.1    glass 	tid = 1 << tid;
    532   1.1    glass 
    533   1.2      gwr 	regs->sci_sel_enb = 0; /* we don't want any interrupts. */
    534   1.2      gwr 	regs->sci_tcmd = 0;	/* get into a harmless state */
    535  1.14      gwr 
    536  1.14      gwr 	arb_retries = ARBITRATION_RETRIES;
    537  1.14      gwr 
    538  1.14      gwr retry_arbitration:
    539   1.2      gwr 	regs->sci_mode = 0;	/* get into a harmless state */
    540  1.19      gwr wait_for_bus_free:
    541  1.14      gwr 	if (--arb_retries <= 0) {
    542  1.14      gwr #ifdef	DEBUG
    543  1.14      gwr 		if (si_debug) {
    544  1.17      gwr 			printf("si_select: arb_retries expended; resetting...\n");
    545  1.14      gwr 		}
    546  1.14      gwr #endif
    547  1.17      gwr 		ret = SCSI_RET_NEED_RESET;
    548  1.17      gwr 		goto nosel;
    549  1.14      gwr 	}
    550  1.14      gwr 
    551  1.17      gwr 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    552  1.17      gwr 
    553  1.19      gwr 	if (regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) {
    554  1.17      gwr 		/* Something is sitting on the SCSI bus... */
    555  1.14      gwr #ifdef	DEBUG
    556  1.19      gwr 		/* Only complain once (the last time through). */
    557  1.19      gwr 		if (si_debug && (arb_retries <= 1)) {
    558  1.19      gwr 			printf("si_select_target: still BSY+SEL\n");
    559  1.14      gwr 		}
    560  1.14      gwr #endif
    561  1.19      gwr 		/* Give it a little time, then try again. */
    562  1.19      gwr 		delay(10);
    563  1.19      gwr 		goto wait_for_bus_free;
    564  1.14      gwr 	}
    565   1.1    glass 
    566   1.1    glass 	regs->sci_odata = myid;
    567   1.1    glass 	regs->sci_mode = SCI_MODE_ARB;
    568   1.2      gwr /*	regs->sci_mode |= SCI_MODE_ARB;	XXX? */
    569  1.14      gwr 
    570   1.1    glass 	/* AIP might not set if BSY went true after we checked */
    571  1.14      gwr 	/* Wait up to about 100 usec. for it to appear. */
    572  1.14      gwr 	arb_wait = 50;	/* X2 */
    573  1.14      gwr 	do {
    574   1.1    glass 		if (regs->sci_icmd & SCI_ICMD_AIP)
    575  1.17      gwr 			goto got_aip;
    576  1.14      gwr 		delay2us();
    577  1.14      gwr 	} while (--arb_wait > 0);
    578  1.17      gwr 	/* XXX - Could have missed it? */
    579  1.17      gwr #ifdef	DEBUG
    580  1.17      gwr 	if (si_debug)
    581  1.17      gwr 		printf("si_select_target: API did not appear\n");
    582  1.17      gwr #endif
    583  1.17      gwr 	goto retry_arbitration;
    584  1.17      gwr 
    585  1.17      gwr 	got_aip:
    586  1.14      gwr #ifdef	DEBUG
    587  1.17      gwr 	if (si_debug & 4) {
    588  1.14      gwr 		printf("si_select_target: API after %d tries (last wait %d)\n",
    589  1.14      gwr 			   ARBITRATION_RETRIES - arb_retries,
    590  1.14      gwr 			   (50 - arb_wait));
    591   1.1    glass 	}
    592  1.14      gwr #endif
    593   1.1    glass 
    594  1.14      gwr 	delay(3);	/* 2.2 uSec. arbitration delay */
    595   1.1    glass 
    596   1.1    glass 	if (regs->sci_icmd & SCI_ICMD_LST) {
    597   1.2      gwr #ifdef	DEBUG
    598  1.14      gwr 		if (si_debug)
    599  1.14      gwr 			printf ("lost 1\n");
    600   1.2      gwr #endif
    601  1.14      gwr 		goto retry_arbitration;	/* XXX */
    602   1.1    glass 	}
    603   1.1    glass 
    604   1.1    glass 	regs->sci_mode &= ~SCI_MODE_PAR_CHK;
    605   1.1    glass 	bid = regs->sci_data;
    606   1.1    glass 
    607   1.1    glass 	if ((bid & ~myid) > myid) {
    608   1.2      gwr #ifdef	DEBUG
    609  1.14      gwr 		if (si_debug)
    610  1.14      gwr 			printf ("lost 2\n");
    611   1.2      gwr #endif
    612  1.14      gwr 		/* Trying again will not help. */
    613   1.1    glass 		goto lost;
    614   1.1    glass 	}
    615   1.1    glass 	if (regs->sci_icmd & SCI_ICMD_LST) {
    616   1.2      gwr #ifdef	DEBUG
    617  1.14      gwr 		if (si_debug)
    618  1.14      gwr 			printf ("lost 3\n");
    619   1.2      gwr #endif
    620   1.1    glass 		goto lost;
    621   1.1    glass 	}
    622   1.1    glass 
    623  1.20  mycroft 	/* Won arbitration, enter selection phase now */
    624   1.1    glass 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    625   1.1    glass 	icmd |= (with_atn ? (SCI_ICMD_SEL|SCI_ICMD_ATN) : SCI_ICMD_SEL);
    626   1.1    glass 	regs->sci_icmd = icmd;
    627   1.1    glass 
    628   1.1    glass 	if (regs->sci_icmd & SCI_ICMD_LST) {
    629   1.2      gwr #ifdef	DEBUG
    630  1.14      gwr 		if (si_debug)
    631  1.14      gwr 			printf ("nosel\n");
    632   1.2      gwr #endif
    633   1.1    glass 		goto nosel;
    634   1.1    glass 	}
    635   1.1    glass 
    636   1.1    glass 	/* XXX a target that violates specs might still drive the bus XXX */
    637   1.1    glass 	/* XXX should put our id out, and after the delay check nothi XXX */
    638   1.1    glass 	/* XXX ng else is out there.				      XXX */
    639   1.1    glass 
    640   1.2      gwr 	delay2us();
    641   1.1    glass 
    642  1.14      gwr 	regs->sci_sel_enb = 0;
    643  1.14      gwr 
    644   1.1    glass 	regs->sci_odata = myid | tid;
    645  1.14      gwr 
    646  1.14      gwr 	icmd |= SCI_ICMD_BSY|SCI_ICMD_DATA;
    647  1.14      gwr 	regs->sci_icmd = icmd;
    648   1.1    glass 
    649   1.1    glass /*	regs->sci_mode &= ~SCI_MODE_ARB;	 2 deskew delays, too */
    650   1.1    glass 	regs->sci_mode = 0;			/* 2 deskew delays, too */
    651  1.20  mycroft 
    652  1.14      gwr 	icmd &= ~SCI_ICMD_BSY;
    653   1.1    glass 	regs->sci_icmd = icmd;
    654   1.1    glass 
    655   1.1    glass 	/* bus settle delay, 400ns */
    656   1.2      gwr 	delay2us(); /* too much (was 2) ? */
    657   1.1    glass 
    658  1.14      gwr 	regs->sci_mode |= SCI_MODE_PAR_CHK;
    659   1.1    glass 
    660   1.1    glass 	{
    661   1.1    glass 		register int timeo  = 2500;/* 250 msecs in 100 usecs chunks */
    662   1.1    glass 		while ((regs->sci_bus_csr & SCI_BUS_BSY) == 0) {
    663   1.1    glass 			if (--timeo > 0) {
    664   1.1    glass 				delay(100);
    665   1.1    glass 			} else {
    666  1.14      gwr 				/* This is the "normal" no-such-device select error. */
    667  1.14      gwr #ifdef	DEBUG
    668  1.14      gwr 				if (si_debug)
    669  1.17      gwr 					printf("si_select: not BSY (nothing there)\n");
    670  1.14      gwr #endif
    671   1.1    glass 				goto nodev;
    672   1.1    glass 			}
    673   1.1    glass 		}
    674   1.1    glass 	}
    675   1.1    glass 
    676   1.1    glass 	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL);
    677   1.1    glass 	regs->sci_icmd = icmd;
    678   1.1    glass /*	regs->sci_sel_enb = myid;*/	/* looks like we should NOT have it */
    679  1.14      gwr 	/* XXX - SCI_MODE_PAR_CHK ? */
    680   1.1    glass 	return SCSI_RET_SUCCESS;
    681  1.14      gwr 
    682   1.1    glass nodev:
    683   1.1    glass 	ret = SCSI_RET_DEVICE_DOWN;
    684   1.1    glass 	regs->sci_sel_enb = myid;
    685   1.1    glass nosel:
    686  1.17      gwr 	regs->sci_icmd = 0;
    687  1.14      gwr 	regs->sci_mode = 0;
    688  1.14      gwr 	return ret;
    689  1.14      gwr 
    690   1.1    glass lost:
    691  1.14      gwr 	regs->sci_icmd = 0;
    692   1.1    glass 	regs->sci_mode = 0;
    693  1.14      gwr #ifdef	DEBUG
    694  1.14      gwr 	if (si_debug) {
    695  1.14      gwr 		printf("si_select: lost arbitration\n");
    696  1.14      gwr 	}
    697  1.14      gwr #endif
    698   1.1    glass 	return ret;
    699   1.1    glass }
    700   1.1    glass 
    701   1.1    glass sci_data_out(regs, phase, count, data)
    702   1.2      gwr 	register volatile sci_regmap_t	*regs;
    703   1.1    glass 	unsigned char		*data;
    704   1.1    glass {
    705   1.1    glass 	register unsigned char	icmd;
    706   1.1    glass 	register int		cnt=0;
    707   1.1    glass 
    708   1.1    glass 	/* ..checks.. */
    709   1.1    glass 
    710   1.1    glass 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    711   1.1    glass loop:
    712  1.21      gwr 	/* SCSI bus phase not valid until REQ is true. */
    713  1.21      gwr 	WAIT_FOR_REQ(regs);
    714   1.1    glass 	if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
    715   1.1    glass 		return cnt;
    716   1.1    glass 
    717   1.1    glass 	icmd |= SCI_ICMD_DATA;
    718   1.1    glass 	regs->sci_icmd = icmd;
    719   1.1    glass 	regs->sci_odata = *data++;
    720   1.1    glass 	icmd |= SCI_ICMD_ACK;
    721   1.1    glass 	regs->sci_icmd = icmd;
    722   1.1    glass 
    723   1.1    glass 	icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_ACK);
    724   1.1    glass 	WAIT_FOR_NOT_REQ(regs);
    725   1.1    glass 	regs->sci_icmd = icmd;
    726   1.1    glass 	++cnt;
    727   1.1    glass 	if (--count > 0)
    728   1.1    glass 		goto loop;
    729   1.1    glass scsi_timeout_error:
    730   1.1    glass 	return cnt;
    731   1.1    glass }
    732   1.1    glass 
    733   1.1    glass sci_data_in(regs, phase, count, data)
    734   1.2      gwr 	register volatile sci_regmap_t	*regs;
    735   1.1    glass 	unsigned char		*data;
    736   1.1    glass {
    737   1.1    glass 	register unsigned char	icmd;
    738   1.1    glass 	register int		cnt=0;
    739   1.1    glass 
    740   1.1    glass 	/* ..checks.. */
    741   1.1    glass 
    742   1.1    glass 	icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
    743   1.1    glass 
    744   1.1    glass loop:
    745  1.21      gwr 	/* SCSI bus phase not valid until REQ is true. */
    746  1.21      gwr 	WAIT_FOR_REQ(regs);
    747   1.1    glass 	if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
    748   1.1    glass 		return cnt;
    749   1.1    glass 
    750   1.1    glass 	*data++ = regs->sci_data;
    751   1.1    glass 	icmd |= SCI_ICMD_ACK;
    752   1.1    glass 	regs->sci_icmd = icmd;
    753   1.1    glass 
    754   1.1    glass 	icmd &= ~SCI_ICMD_ACK;
    755   1.1    glass 	WAIT_FOR_NOT_REQ(regs);
    756   1.1    glass 	regs->sci_icmd = icmd;
    757   1.1    glass 	++cnt;
    758   1.1    glass 	if (--count > 0)
    759   1.1    glass 		goto loop;
    760   1.1    glass 
    761   1.1    glass scsi_timeout_error:
    762   1.1    glass 	return cnt;
    763   1.1    glass }
    764   1.1    glass 
    765  1.17      gwr /* Return -1 (error) or number of bytes sent (>=0). */
    766   1.3      gwr static int
    767   1.2      gwr si_command_transfer(register volatile sci_regmap_t *regs,
    768   1.1    glass 		 int maxlen, u_char *data, u_char *status, u_char *msg)
    769   1.1    glass {
    770  1.17      gwr 	int	xfer, phase;
    771   1.1    glass 
    772  1.17      gwr 	xfer = 0;
    773   1.1    glass 	regs->sci_icmd = 0;
    774   1.1    glass 
    775   1.1    glass 	while (1) {
    776   1.1    glass 
    777   1.1    glass 		WAIT_FOR_REQ(regs);
    778   1.1    glass 
    779   1.1    glass 		phase = SCI_CUR_PHASE(regs->sci_bus_csr);
    780   1.1    glass 
    781   1.1    glass 		switch (phase) {
    782   1.1    glass 			case SCSI_PHASE_CMD:
    783   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_CMD);
    784   1.1    glass 				xfer += sci_data_out(regs, SCSI_PHASE_CMD,
    785   1.1    glass 						   	maxlen, data);
    786  1.17      gwr 				goto out;
    787  1.17      gwr 
    788   1.1    glass 			case SCSI_PHASE_DATA_IN:
    789  1.17      gwr 				printf("command_transfer: Data in phase?\n");
    790  1.17      gwr 				goto err;
    791  1.17      gwr 
    792   1.1    glass 			case SCSI_PHASE_DATA_OUT:
    793  1.17      gwr 				printf("command_transfer: Data out phase?\n");
    794  1.17      gwr 				goto err;
    795  1.17      gwr 
    796   1.1    glass 			case SCSI_PHASE_STATUS:
    797   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_STATUS);
    798  1.17      gwr 				printf("command_transfer: status in...\n");
    799   1.1    glass 				sci_data_in(regs, SCSI_PHASE_STATUS,
    800   1.1    glass 					  	1, status);
    801  1.17      gwr 				printf("command_transfer: status=0x%x\n", *status);
    802  1.17      gwr 				goto err;
    803  1.17      gwr 
    804   1.1    glass 			case SCSI_PHASE_MESSAGE_IN:
    805   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
    806  1.17      gwr 				printf("command_transfer: msg in?\n");
    807   1.1    glass 				sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
    808   1.1    glass 					  	1, msg);
    809   1.1    glass 				break;
    810  1.17      gwr 
    811   1.1    glass 			case SCSI_PHASE_MESSAGE_OUT:
    812   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
    813   1.1    glass 				sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
    814   1.1    glass 					  	1, msg);
    815   1.1    glass 				break;
    816  1.17      gwr 
    817   1.1    glass 			default:
    818  1.17      gwr 				printf("command_transfer: Unexpected phase 0x%x\n", phase);
    819  1.17      gwr 				goto err;
    820   1.1    glass 		}
    821   1.1    glass 	}
    822  1.17      gwr scsi_timeout_error:
    823  1.17      gwr  err:
    824  1.17      gwr 	xfer = -1;
    825  1.17      gwr  out:
    826  1.17      gwr 	return xfer;
    827   1.1    glass }
    828   1.1    glass 
    829   1.3      gwr static int
    830   1.2      gwr si_data_transfer(register volatile sci_regmap_t *regs,
    831   1.1    glass 	      int maxlen, u_char *data, u_char *status, u_char *msg)
    832   1.1    glass {
    833   1.1    glass 	int	retlen = 0, xfer, phase;
    834   1.1    glass 
    835   1.1    glass 	regs->sci_icmd = 0;
    836   1.1    glass 
    837   1.1    glass 	*status = 0;
    838   1.1    glass 
    839   1.1    glass 	while (1) {
    840   1.1    glass 
    841   1.1    glass 		WAIT_FOR_REQ(regs);
    842   1.1    glass 
    843   1.1    glass 		phase = SCI_CUR_PHASE(regs->sci_bus_csr);
    844   1.1    glass 
    845   1.1    glass 		switch (phase) {
    846   1.1    glass 			case SCSI_PHASE_CMD:
    847   1.1    glass 				printf("Command phase in data_transfer().\n");
    848   1.1    glass 				return retlen;
    849   1.1    glass 			case SCSI_PHASE_DATA_IN:
    850   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_DATA_IN);
    851   1.1    glass #if PSEUDO_DMA
    852   1.1    glass 				xfer = sci_pdma_in(regs, SCSI_PHASE_DATA_IN,
    853   1.1    glass 						  	maxlen, data);
    854   1.1    glass #else
    855   1.1    glass 				xfer = sci_data_in(regs, SCSI_PHASE_DATA_IN,
    856   1.1    glass 						  	maxlen, data);
    857   1.1    glass #endif
    858   1.1    glass 				retlen += xfer;
    859   1.1    glass 				maxlen -= xfer;
    860   1.1    glass 				break;
    861   1.1    glass 			case SCSI_PHASE_DATA_OUT:
    862   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_DATA_OUT);
    863   1.1    glass #if PSEUDO_DMA
    864   1.1    glass 				xfer = sci_pdma_out(regs, SCSI_PHASE_DATA_OUT,
    865   1.1    glass 						   	maxlen, data);
    866   1.1    glass #else
    867   1.1    glass 				xfer = sci_data_out(regs, SCSI_PHASE_DATA_OUT,
    868   1.1    glass 						   	maxlen, data);
    869   1.1    glass #endif
    870   1.1    glass 				retlen += xfer;
    871   1.1    glass 				maxlen -= xfer;
    872   1.1    glass 				break;
    873   1.1    glass 			case SCSI_PHASE_STATUS:
    874   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_STATUS);
    875   1.1    glass 				sci_data_in(regs, SCSI_PHASE_STATUS,
    876   1.1    glass 					  	1, status);
    877   1.1    glass 				break;
    878   1.1    glass 			case SCSI_PHASE_MESSAGE_IN:
    879   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_IN);
    880   1.1    glass 				sci_data_in(regs, SCSI_PHASE_MESSAGE_IN,
    881   1.1    glass 					  	1, msg);
    882   1.1    glass 				if (*msg == 0) {
    883   1.1    glass 					return retlen;
    884   1.1    glass 				} else {
    885   1.1    glass 					printf( "message 0x%x in "
    886   1.1    glass 						"data_transfer.\n", *msg);
    887   1.1    glass 				}
    888   1.1    glass 				break;
    889   1.1    glass 			case SCSI_PHASE_MESSAGE_OUT:
    890   1.1    glass 				SCI_ACK(regs,SCSI_PHASE_MESSAGE_OUT);
    891   1.1    glass 				sci_data_out(regs, SCSI_PHASE_MESSAGE_OUT,
    892   1.1    glass 					  	1, msg);
    893   1.1    glass 				break;
    894   1.1    glass 			default:
    895   1.1    glass 				printf( "Unexpected phase 0x%x in "
    896   1.1    glass 					"data_transfer().\n", phase);
    897   1.1    glass scsi_timeout_error:
    898   1.1    glass 				return retlen;
    899   1.1    glass 				break;
    900   1.1    glass 		}
    901   1.1    glass 	}
    902   1.1    glass }
    903   1.1    glass 
    904   1.3      gwr static int
    905  1.17      gwr si_dorequest(struct ncr5380_softc *sc,
    906  1.17      gwr 	int target, int lun, u_char *cmd, int cmdlen,
    907  1.17      gwr 	char *databuf, int datalen, int *sent)
    908  1.17      gwr 	/* Returns 0 on success, -1 on internal error, or the status byte */
    909   1.1    glass {
    910  1.17      gwr 	register volatile sci_regmap_t *regs = sc->sc_regs;
    911   1.1    glass 	int	cmd_bytes_sent, r;
    912   1.1    glass 	u_char	stat, msg, c;
    913   1.1    glass 
    914  1.17      gwr #ifdef	DEBUG
    915  1.17      gwr 	if (si_debug) {
    916  1.17      gwr 		printf("si_dorequest: target=%d, lun=%d\n", target, lun);
    917  1.17      gwr 	}
    918  1.17      gwr #endif
    919  1.17      gwr 
    920   1.1    glass 	*sent = 0;
    921   1.1    glass 
    922   1.2      gwr 	if ( ( r = si_select_target(regs, 7, target, 1) ) != SCSI_RET_SUCCESS) {
    923  1.14      gwr #ifdef	DEBUG
    924  1.14      gwr 		if (si_debug) {
    925  1.14      gwr 			printf("si_dorequest: select returned %d\n", r);
    926  1.14      gwr 		}
    927  1.14      gwr #endif
    928  1.17      gwr 
    929   1.1    glass 		SCI_CLR_INTR(regs);
    930   1.1    glass 		switch (r) {
    931  1.17      gwr 
    932  1.17      gwr 		case SCSI_RET_NEED_RESET:
    933  1.17      gwr 			printf("si_dorequest: target=%d, lun=%d, resetting...\n",
    934  1.17      gwr 				   target, lun, r);
    935  1.17      gwr 			ncr5380_reset_adapter(sc);
    936  1.17      gwr 			ncr5380_reset_scsibus(sc);
    937  1.17      gwr 			/* fall through */
    938   1.1    glass 		case SCSI_RET_RETRY:
    939  1.17      gwr 			return 0x08;	/* Busy - tell common code to retry. */
    940  1.17      gwr 
    941   1.1    glass 		default:
    942  1.17      gwr 			printf("si_dorequest: target=%d, lun=%d, error=%d.\n",
    943   1.1    glass 				target, lun, r);
    944  1.17      gwr 			/* fall through */
    945   1.1    glass 		case SCSI_RET_DEVICE_DOWN:
    946  1.17      gwr 			return -1;	/* Dead - tell common code to give up. */
    947   1.1    glass 		}
    948   1.1    glass 	}
    949   1.1    glass 
    950   1.1    glass 	c = 0x80 | lun;
    951   1.1    glass 
    952   1.2      gwr 	if ((cmd_bytes_sent = si_command_transfer(regs, cmdlen,
    953  1.14      gwr 				(u_char *) cmd, &stat, &c)) != cmdlen)
    954  1.14      gwr 	{
    955   1.1    glass 		SCI_CLR_INTR(regs);
    956  1.17      gwr 		if (cmd_bytes_sent >= 0) {
    957  1.17      gwr 			printf("Data underrun sending CCB (%d bytes of %d, sent).\n",
    958  1.17      gwr 				   cmd_bytes_sent, cmdlen);
    959  1.17      gwr 		}
    960   1.1    glass 		return -1;
    961   1.1    glass 	}
    962   1.1    glass 
    963  1.17      gwr 	*sent = si_data_transfer(regs, datalen, (u_char *)databuf,
    964   1.1    glass 				  &stat, &msg);
    965  1.14      gwr #ifdef	DEBUG
    966  1.14      gwr 	if (si_debug) {
    967  1.14      gwr 		printf("si_dorequest: data transfered = %d\n", *sent);
    968  1.14      gwr 	}
    969  1.14      gwr #endif
    970   1.1    glass 
    971   1.1    glass 	return stat;
    972   1.1    glass }
    973   1.1    glass 
    974   1.3      gwr static int
    975   1.2      gwr si_generic(int adapter, int id, int lun, struct scsi_generic *cmd,
    976   1.1    glass   	 int cmdlen, void *databuf, int datalen)
    977   1.1    glass {
    978  1.17      gwr 	register struct ncr5380_softc *sc = sicd.cd_devs[adapter];
    979  1.17      gwr 	int i, j, sent;
    980   1.1    glass 
    981   1.2      gwr 	if (cmd->opcode == TEST_UNIT_READY)	/* XXX */
    982   1.2      gwr 		cmd->bytes[0] = ((u_char) lun << 5);
    983   1.1    glass 
    984  1.17      gwr 	i = si_dorequest(sc, id, lun, (u_char *) cmd, cmdlen,
    985  1.17      gwr 					 databuf, datalen, &sent);
    986   1.1    glass 
    987   1.2      gwr 	return i;
    988   1.1    glass }
    989   1.1    glass 
    990   1.3      gwr static int
    991   1.2      gwr si_group0(int adapter, int id, int lun, int opcode, int addr, int len,
    992   1.1    glass 		int flags, caddr_t databuf, int datalen)
    993   1.1    glass {
    994  1.17      gwr 	register struct ncr5380_softc *sc = sicd.cd_devs[adapter];
    995   1.2      gwr 	unsigned char cmd[6];
    996  1.17      gwr 	int i, j, sent;
    997   1.2      gwr 
    998   1.2      gwr 	cmd[0] = opcode;		/* Operation code           		*/
    999   1.2      gwr 	cmd[1] = (lun << 5) | ((addr >> 16) & 0x1F);	/* Lun & MSB of addr	*/
   1000   1.2      gwr 	cmd[2] = (addr >> 8) & 0xFF;	/* addr					*/
   1001   1.2      gwr 	cmd[3] = addr & 0xFF;		/* LSB of addr				*/
   1002   1.2      gwr 	cmd[4] = len;			/* Allocation length			*/
   1003   1.2      gwr 	cmd[5] = flags;		/* Link/Flag				*/
   1004   1.1    glass 
   1005  1.17      gwr 	i = si_dorequest(sc, id, lun, cmd, 6, databuf, datalen, &sent);
   1006   1.1    glass 
   1007   1.2      gwr 	return i;
   1008   1.1    glass }
   1009