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