Home | History | Annotate | Line # | Download | only in obio
esp.c revision 1.5
      1  1.5  thorpej /*	$NetBSD: esp.c,v 1.5 1996/12/10 21:27:39 thorpej Exp $	*/
      2  1.1   briggs 
      3  1.1   briggs /*
      4  1.1   briggs  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
      5  1.1   briggs  *
      6  1.1   briggs  * Redistribution and use in source and binary forms, with or without
      7  1.1   briggs  * modification, are permitted provided that the following conditions
      8  1.1   briggs  * are met:
      9  1.1   briggs  * 1. Redistributions of source code must retain the above copyright
     10  1.1   briggs  *    notice, this list of conditions and the following disclaimer.
     11  1.1   briggs  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   briggs  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   briggs  *    documentation and/or other materials provided with the distribution.
     14  1.1   briggs  * 3. All advertising materials mentioning features or use of this software
     15  1.1   briggs  *    must display the following acknowledgement:
     16  1.1   briggs  *	This product includes software developed by Charles M. Hannum.
     17  1.1   briggs  * 4. The name of the author may not be used to endorse or promote products
     18  1.1   briggs  *    derived from this software without specific prior written permission.
     19  1.1   briggs  *
     20  1.1   briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1   briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1   briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1   briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1   briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1   briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1   briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1   briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1   briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1   briggs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1   briggs  */
     31  1.1   briggs 
     32  1.1   briggs /*
     33  1.1   briggs  * Copyright (c) 1994 Peter Galbavy
     34  1.1   briggs  * Copyright (c) 1995 Paul Kranenburg
     35  1.1   briggs  * All rights reserved.
     36  1.1   briggs  *
     37  1.1   briggs  * Redistribution and use in source and binary forms, with or without
     38  1.1   briggs  * modification, are permitted provided that the following conditions
     39  1.1   briggs  * are met:
     40  1.1   briggs  * 1. Redistributions of source code must retain the above copyright
     41  1.1   briggs  *    notice, this list of conditions and the following disclaimer.
     42  1.1   briggs  * 2. Redistributions in binary form must reproduce the above copyright
     43  1.1   briggs  *    notice, this list of conditions and the following disclaimer in the
     44  1.1   briggs  *    documentation and/or other materials provided with the distribution.
     45  1.1   briggs  * 3. All advertising materials mentioning features or use of this software
     46  1.1   briggs  *    must display the following acknowledgement:
     47  1.1   briggs  *	This product includes software developed by Peter Galbavy
     48  1.1   briggs  * 4. The name of the author may not be used to endorse or promote products
     49  1.1   briggs  *    derived from this software without specific prior written permission.
     50  1.1   briggs  *
     51  1.1   briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52  1.1   briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     53  1.1   briggs  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     54  1.1   briggs  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     55  1.1   briggs  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     56  1.1   briggs  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     57  1.1   briggs  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     58  1.1   briggs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     59  1.1   briggs  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     60  1.1   briggs  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61  1.1   briggs  * POSSIBILITY OF SUCH DAMAGE.
     62  1.1   briggs  */
     63  1.1   briggs 
     64  1.1   briggs /*
     65  1.1   briggs  * Based on aic6360 by Jarle Greipsland
     66  1.1   briggs  *
     67  1.1   briggs  * Acknowledgements: Many of the algorithms used in this driver are
     68  1.1   briggs  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     69  1.1   briggs  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     70  1.1   briggs  */
     71  1.1   briggs 
     72  1.1   briggs #include <sys/types.h>
     73  1.1   briggs #include <sys/param.h>
     74  1.1   briggs #include <sys/systm.h>
     75  1.1   briggs #include <sys/kernel.h>
     76  1.1   briggs #include <sys/errno.h>
     77  1.1   briggs #include <sys/ioctl.h>
     78  1.1   briggs #include <sys/device.h>
     79  1.1   briggs #include <sys/buf.h>
     80  1.1   briggs #include <sys/proc.h>
     81  1.1   briggs #include <sys/user.h>
     82  1.1   briggs #include <sys/queue.h>
     83  1.1   briggs 
     84  1.1   briggs #include <scsi/scsi_all.h>
     85  1.1   briggs #include <scsi/scsiconf.h>
     86  1.1   briggs #include <scsi/scsi_message.h>
     87  1.1   briggs 
     88  1.1   briggs #include <machine/cpu.h>
     89  1.1   briggs #include <machine/param.h>
     90  1.1   briggs 
     91  1.1   briggs #if defined(__sparc__)
     92  1.1   briggs #define	SPARC_DRIVER
     93  1.1   briggs #include <machine/autoconf.h>
     94  1.1   briggs #include <sparc/dev/sbusvar.h>
     95  1.1   briggs #include <sparc/dev/dmareg.h>
     96  1.1   briggs #include <sparc/dev/dmavar.h>
     97  1.1   briggs #include <sparc/dev/espreg.h>
     98  1.1   briggs #include <sparc/dev/espvar.h>
     99  1.1   briggs #else
    100  1.1   briggs #if (_MACHINE == mac68k)
    101  1.1   briggs #define MAC68K_DRIVER
    102  1.1   briggs #include <machine/viareg.h>
    103  1.1   briggs 
    104  1.1   briggs struct dma_softc {
    105  1.1   briggs 	struct esp_softc	*sc_esp;
    106  1.1   briggs 	int		sc_active;
    107  1.3   briggs 	int		sc_tc;
    108  1.1   briggs 	int		sc_datain;
    109  1.1   briggs 	size_t		sc_dmasize;
    110  1.1   briggs 	size_t		sc_dmatrans;
    111  1.1   briggs 	char		**sc_dmaaddr;
    112  1.1   briggs 	size_t		*sc_pdmalen;
    113  1.1   briggs };
    114  1.1   briggs 
    115  1.1   briggs #include <mac68k/dev/espreg.h>
    116  1.1   briggs #include <mac68k/dev/espvar.h>
    117  1.1   briggs #undef ESPCMD_DMA
    118  1.1   briggs #define ESPCMD_DMA	0	/* No DMA */
    119  1.1   briggs #undef ESPCMD_TRPAD
    120  1.1   briggs #define ESPCMD_TRPAD	0x98 	/* TRPAD needs DMA flag*/
    121  1.1   briggs 
    122  1.1   briggs static __inline__ void	dma_intr __P((struct dma_softc *sc));
    123  1.1   briggs 
    124  1.1   briggs static __inline__ void
    125  1.1   briggs dma_intr(sc)
    126  1.1   briggs 	struct dma_softc *sc;
    127  1.1   briggs {
    128  1.4   briggs 	register struct esp_softc	*sc_esp;
    129  1.3   briggs 	register u_char	*p;
    130  1.4   briggs 	register u_int	espphase, espstat, espintr;
    131  1.3   briggs 	register int	cnt;
    132  1.1   briggs 
    133  1.1   briggs 	if (sc->sc_active == 0) {
    134  1.4   briggs 		printf("dma_intr--inactive DMA\n");
    135  1.1   briggs 		return;
    136  1.1   briggs 	}
    137  1.1   briggs 
    138  1.3   briggs 	if ((sc->sc_esp->sc_espintr & ESPINTR_BS) == 0) {
    139  1.3   briggs 		sc->sc_active = 0;
    140  1.3   briggs 		return;
    141  1.3   briggs 	}
    142  1.3   briggs 
    143  1.4   briggs 	cnt = *sc->sc_pdmalen;
    144  1.4   briggs 	if (*sc->sc_pdmalen == 0) {
    145  1.4   briggs 		printf("data interrupt, but no count left.");
    146  1.4   briggs 	}
    147  1.4   briggs 
    148  1.1   briggs 	p = *sc->sc_dmaaddr;
    149  1.4   briggs 	sc_esp = sc->sc_esp;
    150  1.4   briggs 	espphase = sc_esp->sc_phase;
    151  1.4   briggs 	espstat = (u_int) sc_esp->sc_espstat;
    152  1.4   briggs 	espintr = (u_int) sc_esp->sc_espintr;
    153  1.4   briggs 	do {
    154  1.4   briggs 		if (sc->sc_datain) {
    155  1.4   briggs 			*p++ = ESP_READ_REG(sc_esp, ESP_FIFO);
    156  1.4   briggs 			cnt--;
    157  1.4   briggs 			if (espphase == DATA_IN_PHASE) {
    158  1.4   briggs 				ESPCMD(sc_esp, ESPCMD_TRANS);
    159  1.4   briggs 			} else {
    160  1.4   briggs 				sc->sc_active = 0;
    161  1.4   briggs 			}
    162  1.4   briggs 	 	} else {
    163  1.4   briggs 			if (   (espphase == DATA_OUT_PHASE)
    164  1.4   briggs 			    || (espphase == MESSAGE_OUT_PHASE)) {
    165  1.4   briggs 				ESP_WRITE_REG(sc_esp, ESP_FIFO, *p++);
    166  1.4   briggs 				cnt--;
    167  1.4   briggs 				ESPCMD(sc_esp, ESPCMD_TRANS);
    168  1.4   briggs 			} else {
    169  1.4   briggs 				sc->sc_active = 0;
    170  1.4   briggs 			}
    171  1.1   briggs 		}
    172  1.4   briggs 
    173  1.4   briggs 		if (sc->sc_active) {
    174  1.4   briggs 			while (!DMA_ISINTR(sc));
    175  1.4   briggs 			espstat = ESP_READ_REG(sc_esp, ESP_STAT);
    176  1.4   briggs 			espintr = ESP_READ_REG(sc_esp, ESP_INTR);
    177  1.4   briggs 			espphase = (espintr & ESPINTR_DIS)
    178  1.4   briggs 				    ? /* Disconnected */ BUSFREE_PHASE
    179  1.4   briggs 				    : espstat & ESPSTAT_PHASE;
    180  1.1   briggs 		}
    181  1.4   briggs 	} while (sc->sc_active && (espintr & ESPINTR_BS));
    182  1.4   briggs 	sc_esp->sc_phase = espphase;
    183  1.4   briggs 	sc_esp->sc_espstat = (u_char) espstat;
    184  1.4   briggs 	sc_esp->sc_espintr = (u_char) espintr;
    185  1.4   briggs 	*sc->sc_dmaaddr = p;
    186  1.4   briggs 	*sc->sc_pdmalen = cnt;
    187  1.4   briggs 
    188  1.3   briggs 	if (*sc->sc_pdmalen == 0) {
    189  1.3   briggs 		sc->sc_tc = ESPSTAT_TC;
    190  1.3   briggs 	}
    191  1.3   briggs 	sc->sc_esp->sc_espstat |= sc->sc_tc;
    192  1.1   briggs }
    193  1.1   briggs #else
    194  1.1   briggs #include <dev/tc/tcvar.h>
    195  1.1   briggs #include <alpha/tc/tcdsvar.h>
    196  1.1   briggs #include <alpha/tc/espreg.h>
    197  1.1   briggs #include <alpha/tc/espvar.h>
    198  1.1   briggs #endif
    199  1.1   briggs #endif
    200  1.1   briggs 
    201  1.1   briggs int esp_debug = 0; /*ESP_SHOWPHASE|ESP_SHOWMISC|ESP_SHOWTRAC|ESP_SHOWCMDS;*/
    202  1.1   briggs 
    203  1.1   briggs /*static*/ void	espattach	__P((struct device *, struct device *, void *));
    204  1.1   briggs /*static*/ int	espmatch	__P((struct device *, void *, void *));
    205  1.1   briggs /*static*/ u_int	esp_adapter_info __P((struct esp_softc *));
    206  1.1   briggs /*static*/ void	espreadregs	__P((struct esp_softc *));
    207  1.1   briggs /*static*/ void	esp_select	__P((struct esp_softc *, struct esp_ecb *));
    208  1.1   briggs /*static*/ int esp_reselect	__P((struct esp_softc *, int));
    209  1.1   briggs /*static*/ void	esp_scsi_reset	__P((struct esp_softc *));
    210  1.1   briggs /*static*/ void	esp_reset	__P((struct esp_softc *));
    211  1.1   briggs /*static*/ void	esp_init	__P((struct esp_softc *, int));
    212  1.1   briggs /*static*/ int	esp_scsi_cmd	__P((struct scsi_xfer *));
    213  1.1   briggs /*static*/ int	esp_poll	__P((struct esp_softc *, struct scsi_xfer *, int));
    214  1.1   briggs /*static*/ void	esp_sched	__P((struct esp_softc *));
    215  1.1   briggs /*static*/ void	esp_done	__P((struct esp_softc *, struct esp_ecb *));
    216  1.1   briggs /*static*/ void	esp_msgin	__P((struct esp_softc *));
    217  1.1   briggs /*static*/ void	esp_msgout	__P((struct esp_softc *));
    218  1.1   briggs /*static*/ int	espintr		__P((struct esp_softc *));
    219  1.1   briggs /*static*/ void	esp_timeout	__P((void *arg));
    220  1.1   briggs /*static*/ void	esp_abort	__P((struct esp_softc *, struct esp_ecb *));
    221  1.1   briggs /*static*/ void esp_dequeue	__P((struct esp_softc *, struct esp_ecb *));
    222  1.1   briggs void esp_sense __P((struct esp_softc *, struct esp_ecb *));
    223  1.1   briggs void esp_free_ecb __P((struct esp_softc *, struct esp_ecb *, int));
    224  1.1   briggs struct esp_ecb *esp_get_ecb __P((struct esp_softc *, int));
    225  1.1   briggs static inline int esp_stp2cpb __P((struct esp_softc *, int));
    226  1.1   briggs static inline int esp_cpb2stp __P((struct esp_softc *, int));
    227  1.1   briggs static inline void esp_setsync __P((struct esp_softc *, struct esp_tinfo *));
    228  1.1   briggs 
    229  1.1   briggs /* Linkup to the rest of the kernel */
    230  1.1   briggs struct cfattach esp_ca = {
    231  1.1   briggs 	sizeof(struct esp_softc), espmatch, espattach
    232  1.1   briggs };
    233  1.1   briggs 
    234  1.1   briggs struct cfdriver esp_cd = {
    235  1.1   briggs 	NULL, "esp", DV_DULL
    236  1.1   briggs };
    237  1.1   briggs 
    238  1.1   briggs struct scsi_adapter esp_switch = {
    239  1.1   briggs 	esp_scsi_cmd,
    240  1.1   briggs 	minphys,		/* no max at this level; handled by DMA code */
    241  1.1   briggs 	NULL,
    242  1.1   briggs 	NULL,
    243  1.1   briggs };
    244  1.1   briggs 
    245  1.1   briggs struct scsi_device esp_dev = {
    246  1.1   briggs 	NULL,			/* Use default error handler */
    247  1.1   briggs 	NULL,			/* have a queue, served by this */
    248  1.1   briggs 	NULL,			/* have no async handler */
    249  1.1   briggs 	NULL,			/* Use default 'done' routine */
    250  1.1   briggs };
    251  1.1   briggs 
    252  1.1   briggs int
    253  1.1   briggs espmatch(parent, vcf, aux)
    254  1.1   briggs 	struct device *parent;
    255  1.1   briggs 	void *vcf, *aux;
    256  1.1   briggs {
    257  1.1   briggs 	struct cfdata *cf = vcf;
    258  1.1   briggs #ifdef SPARC_DRIVER
    259  1.1   briggs 	register struct confargs *ca = aux;
    260  1.1   briggs 	register struct romaux *ra = &ca->ca_ra;
    261  1.1   briggs 
    262  1.1   briggs 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
    263  1.1   briggs 		return (0);
    264  1.1   briggs 	if (ca->ca_bustype == BUS_SBUS)
    265  1.1   briggs 		return (1);
    266  1.1   briggs 	ra->ra_len = NBPG;
    267  1.1   briggs 	return (probeget(ra->ra_vaddr, 1) != -1);
    268  1.1   briggs #else
    269  1.1   briggs #ifdef MAC68K_DRIVER
    270  1.1   briggs 	if ((cf->cf_unit == 0) && mac68k_machine.scsi96)
    271  1.1   briggs 		return (1);
    272  1.1   briggs 	if ((cf->cf_unit == 1) && mac68k_machine.scsi96_2)
    273  1.1   briggs 		return (1);
    274  1.1   briggs 	return (0);
    275  1.1   briggs #else
    276  1.1   briggs 	struct tcdsdev_attach_args *tcdsdev = aux;
    277  1.1   briggs 
    278  1.1   briggs 	if (strncmp(tcdsdev->tcdsda_modname, "PMAZ-AA ", TC_ROM_LLEN))
    279  1.1   briggs 		return (0);
    280  1.1   briggs 	return (!tc_badaddr(tcdsdev->tcdsda_addr));
    281  1.1   briggs #endif
    282  1.1   briggs #endif
    283  1.1   briggs }
    284  1.1   briggs 
    285  1.1   briggs /*
    286  1.1   briggs  * Attach this instance, and then all the sub-devices
    287  1.1   briggs  */
    288  1.1   briggs void
    289  1.1   briggs espattach(parent, self, aux)
    290  1.1   briggs 	struct device *parent, *self;
    291  1.1   briggs 	void *aux;
    292  1.1   briggs {
    293  1.1   briggs #ifdef SPARC_DRIVER
    294  1.1   briggs 	register struct confargs *ca = aux;
    295  1.1   briggs #else
    296  1.1   briggs #ifdef MAC68K_DRIVER
    297  1.1   briggs 	extern vm_offset_t	SCSIBase;
    298  1.1   briggs #else
    299  1.1   briggs 	register struct tcdsdev_attach_args *tcdsdev = aux;
    300  1.1   briggs #endif
    301  1.1   briggs #endif
    302  1.1   briggs 	struct esp_softc *sc = (void *)self;
    303  1.1   briggs #ifdef SPARC_DRIVER
    304  1.1   briggs 	struct bootpath *bp;
    305  1.1   briggs 	int dmachild = strncmp(parent->dv_xname, "dma", 3) == 0;
    306  1.1   briggs #endif
    307  1.1   briggs 
    308  1.1   briggs #ifdef SPARC_DRIVER
    309  1.1   briggs 	/*
    310  1.1   briggs 	 * Make sure things are sane. I don't know if this is ever
    311  1.1   briggs 	 * necessary, but it seem to be in all of Torek's code.
    312  1.1   briggs 	 */
    313  1.1   briggs 	if (ca->ca_ra.ra_nintr != 1) {
    314  1.1   briggs 		printf(": expected 1 interrupt, got %d\n", ca->ca_ra.ra_nintr);
    315  1.1   briggs 		return;
    316  1.1   briggs 	}
    317  1.1   briggs 
    318  1.1   briggs 	sc->sc_pri = ca->ca_ra.ra_intr[0].int_pri;
    319  1.1   briggs 	printf(" pri %d", sc->sc_pri);
    320  1.1   briggs 
    321  1.1   briggs 	/*
    322  1.1   briggs 	 * Map my registers in, if they aren't already in virtual
    323  1.1   briggs 	 * address space.
    324  1.1   briggs 	 */
    325  1.1   briggs 	if (ca->ca_ra.ra_vaddr)
    326  1.1   briggs 		sc->sc_reg = (volatile u_char *) ca->ca_ra.ra_vaddr;
    327  1.1   briggs 	else {
    328  1.1   briggs 		sc->sc_reg = (volatile u_char *)
    329  1.1   briggs 		    mapiodev(ca->ca_ra.ra_reg, 0, ca->ca_ra.ra_len, ca->ca_bustype);
    330  1.1   briggs 	}
    331  1.1   briggs #else
    332  1.1   briggs #ifdef MAC68K_DRIVER
    333  1.1   briggs 	if (sc->sc_dev.dv_unit == 0) {
    334  1.2   briggs 		unsigned long	reg_offset;
    335  1.2   briggs 
    336  1.1   briggs 		sc->sc_reg = (volatile u_char *) SCSIBase;
    337  1.1   briggs 		mac68k_register_scsi_irq((void (*)(void *)) espintr, sc);
    338  1.1   briggs 		sc->irq_mask = V2IF_SCSIIRQ;
    339  1.2   briggs 		reg_offset = SCSIBase - IOBase;
    340  1.2   briggs 		if (reg_offset == 0x10000) {
    341  1.2   briggs 			sc->sc_freq = 16500000;
    342  1.2   briggs 		} else {
    343  1.2   briggs 			sc->sc_freq = 25000000;
    344  1.2   briggs 		}
    345  1.1   briggs 	} else {
    346  1.4   briggs 		sc->sc_reg = (volatile u_char *) SCSIBase + 0x402;
    347  1.1   briggs 		mac68k_register_scsi_b_irq((void (*)(void *)) espintr, sc);
    348  1.4   briggs 		sc->irq_mask = V2IF_SCSIDRQ;
    349  1.2   briggs 		sc->sc_freq = 25000000;
    350  1.1   briggs 	}
    351  1.1   briggs 	sc->sc_dma = &sc->_sc_dma;
    352  1.1   briggs 	printf(": address %p", sc->sc_reg);
    353  1.1   briggs 
    354  1.1   briggs 	sc->sc_id = 7;
    355  1.1   briggs #else
    356  1.1   briggs 	sc->sc_reg = (volatile u_int32_t *)tcdsdev->tcdsda_addr;
    357  1.1   briggs 	sc->sc_cookie = tcdsdev->tcdsda_cookie;
    358  1.1   briggs 	sc->sc_dma = tcdsdev->tcdsda_sc;
    359  1.1   briggs 
    360  1.1   briggs 	printf(": address %x", sc->sc_reg);
    361  1.1   briggs 	tcds_intr_establish(parent, sc->sc_cookie, TC_IPL_BIO,
    362  1.1   briggs 	    (int (*)(void *))espintr, sc);
    363  1.1   briggs #endif
    364  1.1   briggs #endif
    365  1.1   briggs 
    366  1.1   briggs #ifdef SPARC_DRIVER
    367  1.1   briggs 	/* Other settings */
    368  1.1   briggs 	sc->sc_node = ca->ca_ra.ra_node;
    369  1.1   briggs 	if (ca->ca_bustype == BUS_SBUS) {
    370  1.1   briggs 		sc->sc_id = getpropint(sc->sc_node, "initiator-id", 7);
    371  1.1   briggs 		sc->sc_freq = getpropint(sc->sc_node, "clock-frequency", -1);
    372  1.1   briggs 	} else {
    373  1.1   briggs 		sc->sc_id = 7;
    374  1.1   briggs 		sc->sc_freq = 24000000;
    375  1.1   briggs 	}
    376  1.1   briggs 	if (sc->sc_freq < 0)
    377  1.1   briggs 		sc->sc_freq = ((struct sbus_softc *)
    378  1.1   briggs 		    sc->sc_dev.dv_parent)->sc_clockfreq;
    379  1.1   briggs #else
    380  1.1   briggs #ifdef MAC68K_DRIVER
    381  1.1   briggs #else
    382  1.1   briggs 	if (parent->dv_cfdata->cf_driver == &tcds_cd) {
    383  1.1   briggs 		sc->sc_id = tcdsdev->tcdsda_id;
    384  1.1   briggs 		sc->sc_freq = tcdsdev->tcdsda_freq;
    385  1.1   briggs 	} else {
    386  1.1   briggs 		/* XXX */
    387  1.1   briggs 		sc->sc_id = 7;
    388  1.1   briggs 		sc->sc_freq = 24000000;
    389  1.1   briggs 	}
    390  1.1   briggs #endif
    391  1.1   briggs #endif
    392  1.1   briggs 
    393  1.1   briggs 	/* gimme Mhz */
    394  1.1   briggs 	sc->sc_freq /= 1000000;
    395  1.1   briggs 
    396  1.1   briggs #ifdef SPARC_DRIVER
    397  1.1   briggs 	if (dmachild) {
    398  1.1   briggs 		sc->sc_dma = (struct dma_softc *)parent;
    399  1.1   briggs 		sc->sc_dma->sc_esp = sc;
    400  1.1   briggs 	} else {
    401  1.1   briggs 		/*
    402  1.1   briggs 		 * find the DMA by poking around the dma device structures
    403  1.1   briggs 		 *
    404  1.1   briggs 		 * What happens here is that if the dma driver has not been
    405  1.1   briggs 		 * configured, then this returns a NULL pointer. Then when the
    406  1.1   briggs 		 * dma actually gets configured, it does the opposing test, and
    407  1.1   briggs 		 * if the sc->sc_esp field in it's softc is NULL, then tries to
    408  1.1   briggs 		 * find the matching esp driver.
    409  1.1   briggs 		 *
    410  1.1   briggs 		 */
    411  1.1   briggs 		sc->sc_dma = (struct dma_softc *)
    412  1.1   briggs 			getdevunit("dma", sc->sc_dev.dv_unit);
    413  1.1   briggs 
    414  1.1   briggs 		/*
    415  1.1   briggs 		 * and a back pointer to us, for DMA
    416  1.1   briggs 		 */
    417  1.1   briggs 		if (sc->sc_dma)
    418  1.1   briggs 			sc->sc_dma->sc_esp = sc;
    419  1.1   briggs 		else
    420  1.1   briggs 			panic("espattach: no dma found");
    421  1.1   briggs 	}
    422  1.1   briggs #else
    423  1.1   briggs 	sc->sc_dma->sc_esp = sc;		/* XXX */
    424  1.1   briggs #endif
    425  1.1   briggs 
    426  1.1   briggs 	/*
    427  1.1   briggs 	 * It is necessary to try to load the 2nd config register here,
    428  1.1   briggs 	 * to find out what rev the esp chip is, else the esp_reset
    429  1.1   briggs 	 * will not set up the defaults correctly.
    430  1.1   briggs 	 */
    431  1.1   briggs 	sc->sc_cfg1 = sc->sc_id | ESPCFG1_PARENB;
    432  1.1   briggs #ifdef SPARC_DRIVER
    433  1.1   briggs 	sc->sc_cfg2 = ESPCFG2_SCSI2 | ESPCFG2_RPE;
    434  1.1   briggs 	sc->sc_cfg3 = ESPCFG3_CDB;
    435  1.1   briggs 	ESP_WRITE_REG(sc, ESP_CFG2, sc->sc_cfg2);
    436  1.1   briggs 
    437  1.1   briggs 	if ((ESP_READ_REG(sc, ESP_CFG2) & ~ESPCFG2_RSVD) != (ESPCFG2_SCSI2 | ESPCFG2_RPE)) {
    438  1.1   briggs 		printf(": ESP100");
    439  1.1   briggs 		sc->sc_rev = ESP100;
    440  1.1   briggs 	} else {
    441  1.1   briggs 		sc->sc_cfg2 = ESPCFG2_SCSI2;
    442  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG2, sc->sc_cfg2);
    443  1.1   briggs 		sc->sc_cfg3 = 0;
    444  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG3, sc->sc_cfg3);
    445  1.1   briggs 		sc->sc_cfg3 = (ESPCFG3_CDB | ESPCFG3_FCLK);
    446  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG3, sc->sc_cfg3);
    447  1.1   briggs 		if (ESP_READ_REG(sc, ESP_CFG3) != (ESPCFG3_CDB | ESPCFG3_FCLK)) {
    448  1.1   briggs 			printf(": ESP100A");
    449  1.1   briggs 			sc->sc_rev = ESP100A;
    450  1.1   briggs 		} else {
    451  1.1   briggs 			/* ESPCFG2_FE enables > 64K transfers */
    452  1.1   briggs 			sc->sc_cfg2 |= ESPCFG2_FE;
    453  1.1   briggs 			sc->sc_cfg3 = 0;
    454  1.1   briggs 			ESP_WRITE_REG(sc, ESP_CFG3, sc->sc_cfg3);
    455  1.1   briggs 			printf(": ESP200");
    456  1.1   briggs 			sc->sc_rev = ESP200;
    457  1.1   briggs 		}
    458  1.1   briggs 	}
    459  1.1   briggs #else
    460  1.1   briggs #ifdef MAC68K_DRIVER
    461  1.1   briggs 	sc->sc_cfg2 = ESPCFG2_SCSI2;
    462  1.3   briggs 	sc->sc_cfg3 = 0;
    463  1.1   briggs 	printf(": NCR53C96");
    464  1.1   briggs 	sc->sc_rev = NCR53C96;
    465  1.1   briggs #else
    466  1.1   briggs 	sc->sc_cfg2 = ESPCFG2_SCSI2;
    467  1.1   briggs 	sc->sc_cfg3 = 0x4;		/* Save residual byte. XXX??? */
    468  1.1   briggs 	printf(": NCR53C94");
    469  1.1   briggs 	sc->sc_rev = NCR53C94;
    470  1.1   briggs #endif
    471  1.1   briggs #endif
    472  1.1   briggs 
    473  1.1   briggs 	/*
    474  1.1   briggs 	 * This is the value used to start sync negotiations
    475  1.1   briggs 	 * Note that the ESP register "SYNCTP" is programmed
    476  1.1   briggs 	 * in "clocks per byte", and has a minimum value of 4.
    477  1.1   briggs 	 * The SCSI period used in negotiation is one-fourth
    478  1.1   briggs 	 * of the time (in nanoseconds) needed to transfer one byte.
    479  1.1   briggs 	 * Since the chip's clock is given in MHz, we have the following
    480  1.1   briggs 	 * formula: 4 * period = (1000 / freq) * 4
    481  1.1   briggs 	 */
    482  1.1   briggs 	sc->sc_minsync = 1000 / sc->sc_freq;
    483  1.1   briggs 
    484  1.1   briggs #ifdef SPARC_DRIVER
    485  1.1   briggs 	/*
    486  1.1   briggs 	 * Alas, we must now modify the value a bit, because it's
    487  1.1   briggs 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    488  1.1   briggs 	 * in config register 3...
    489  1.1   briggs 	 */
    490  1.1   briggs 	switch (sc->sc_rev) {
    491  1.1   briggs 	case ESP100:
    492  1.1   briggs 		sc->sc_maxxfer = 64 * 1024;
    493  1.1   briggs 		sc->sc_minsync = 0;	/* No synch on old chip? */
    494  1.1   briggs 		break;
    495  1.1   briggs 	case ESP100A:
    496  1.1   briggs 		sc->sc_maxxfer = 64 * 1024;
    497  1.1   briggs 		sc->sc_minsync = esp_cpb2stp(sc, 5); /* Min clocks/byte is 5 */
    498  1.1   briggs 		break;
    499  1.1   briggs 	case ESP200:
    500  1.1   briggs 		sc->sc_maxxfer = 16 * 1024 * 1024;
    501  1.1   briggs 		/* XXX - do actually set FAST* bits */
    502  1.1   briggs 	}
    503  1.1   briggs #else
    504  1.1   briggs #ifdef MAC68K_DRIVER
    505  1.1   briggs 	sc->sc_minsync = 0;	/* No synchronous xfers w/o DMA */
    506  1.1   briggs 	/* Really no limit, but since we want to fit into the TCR... */
    507  1.1   briggs 	sc->sc_maxxfer = 64 * 1024;
    508  1.1   briggs #else
    509  1.1   briggs 	sc->sc_maxxfer = 64 * 1024;
    510  1.1   briggs #endif
    511  1.1   briggs #endif
    512  1.1   briggs 
    513  1.1   briggs 	sc->sc_ccf = FREQTOCCF(sc->sc_freq);
    514  1.1   briggs 
    515  1.1   briggs 	/* The value *must not* be == 1. Make it 2 */
    516  1.1   briggs 	if (sc->sc_ccf == 1)
    517  1.1   briggs 		sc->sc_ccf = 2;
    518  1.1   briggs 
    519  1.1   briggs 	/*
    520  1.1   briggs 	 * The recommended timeout is 250ms. This register is loaded
    521  1.1   briggs 	 * with a value calculated as follows, from the docs:
    522  1.1   briggs 	 *
    523  1.1   briggs 	 *		(timout period) x (CLK frequency)
    524  1.1   briggs 	 *	reg = -------------------------------------
    525  1.1   briggs 	 *		 8192 x (Clock Conversion Factor)
    526  1.1   briggs 	 *
    527  1.1   briggs 	 * Since CCF has a linear relation to CLK, this generally computes
    528  1.1   briggs 	 * to the constant of 153.
    529  1.1   briggs 	 */
    530  1.1   briggs 	sc->sc_timeout = ((250 * 1000) * sc->sc_freq) / (8192 * sc->sc_ccf);
    531  1.1   briggs 
    532  1.1   briggs 	/* CCF register only has 3 bits; 0 is actually 8 */
    533  1.1   briggs 	sc->sc_ccf &= 7;
    534  1.1   briggs 
    535  1.1   briggs 	/* Reset state & bus */
    536  1.1   briggs 	sc->sc_state = 0;
    537  1.1   briggs 	esp_init(sc, 1);
    538  1.1   briggs 
    539  1.1   briggs 	printf(" %dMhz, target %d\n", sc->sc_freq, sc->sc_id);
    540  1.1   briggs 
    541  1.1   briggs #ifdef SPARC_DRIVER
    542  1.1   briggs 	/* add me to the sbus structures */
    543  1.1   briggs 	sc->sc_sd.sd_reset = (void *) esp_reset;
    544  1.1   briggs #if defined(SUN4C) || defined(SUN4M)
    545  1.1   briggs 	if (ca->ca_bustype == BUS_SBUS) {
    546  1.1   briggs 		if (dmachild)
    547  1.1   briggs 			sbus_establish(&sc->sc_sd, sc->sc_dev.dv_parent);
    548  1.1   briggs 		else
    549  1.1   briggs 			sbus_establish(&sc->sc_sd, &sc->sc_dev);
    550  1.1   briggs 	}
    551  1.1   briggs #endif /* SUN4C || SUN4M */
    552  1.1   briggs #endif
    553  1.1   briggs 
    554  1.1   briggs #ifdef SPARC_DRIVER
    555  1.1   briggs 	/* and the interuppts */
    556  1.1   briggs 	sc->sc_ih.ih_fun = (void *) espintr;
    557  1.1   briggs 	sc->sc_ih.ih_arg = sc;
    558  1.1   briggs 	intr_establish(sc->sc_pri, &sc->sc_ih);
    559  1.1   briggs 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    560  1.1   briggs #endif
    561  1.1   briggs 
    562  1.1   briggs 	/*
    563  1.1   briggs 	 * fill in the prototype scsi_link.
    564  1.1   briggs 	 */
    565  1.1   briggs 	sc->sc_link.channel = SCSI_CHANNEL_ONLY_ONE;
    566  1.1   briggs 	sc->sc_link.adapter_softc = sc;
    567  1.1   briggs 	sc->sc_link.adapter_target = sc->sc_id;
    568  1.1   briggs 	sc->sc_link.adapter = &esp_switch;
    569  1.1   briggs 	sc->sc_link.device = &esp_dev;
    570  1.1   briggs 	sc->sc_link.openings = 2;
    571  1.5  thorpej 	sc->sc_link.max_target = 7;
    572  1.1   briggs 
    573  1.1   briggs 	/*
    574  1.1   briggs 	 * If the boot path is "esp" at the moment and it's me, then
    575  1.1   briggs 	 * walk our pointer to the sub-device, ready for the config
    576  1.1   briggs 	 * below.
    577  1.1   briggs 	 */
    578  1.1   briggs #ifdef SPARC_DRIVER
    579  1.1   briggs 	bp = ca->ca_ra.ra_bp;
    580  1.1   briggs 	switch (ca->ca_bustype) {
    581  1.1   briggs 	case BUS_SBUS:
    582  1.1   briggs 		if (bp != NULL && strcmp(bp->name, "esp") == 0 &&
    583  1.1   briggs 		    SAME_ESP(sc, bp, ca))
    584  1.1   briggs 			bootpath_store(1, bp + 1);
    585  1.1   briggs 		break;
    586  1.1   briggs 	default:
    587  1.1   briggs 		if (bp != NULL && strcmp(bp->name, "esp") == 0 &&
    588  1.1   briggs 			bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit)
    589  1.1   briggs 			bootpath_store(1, bp + 1);
    590  1.1   briggs 		break;
    591  1.1   briggs 	}
    592  1.1   briggs #endif
    593  1.1   briggs 
    594  1.1   briggs 	/*
    595  1.1   briggs 	 * Now try to attach all the sub-devices
    596  1.1   briggs 	 */
    597  1.1   briggs 	config_found(self, &sc->sc_link, scsiprint);
    598  1.1   briggs 
    599  1.1   briggs #ifdef MAC68K_DRIVER
    600  1.1   briggs 	via2_reg(vPCR) = 0x22;
    601  1.1   briggs 	via2_reg(vIFR) = sc->irq_mask;
    602  1.1   briggs 	via2_reg(vIER) = 0x80 | sc->irq_mask;
    603  1.1   briggs #endif
    604  1.1   briggs #ifdef SPARC_DRIVER
    605  1.1   briggs 	bootpath_store(1, NULL);
    606  1.1   briggs #endif
    607  1.1   briggs }
    608  1.1   briggs 
    609  1.1   briggs /*
    610  1.1   briggs  * This is the generic esp reset function. It does not reset the SCSI bus,
    611  1.1   briggs  * only this controllers, but kills any on-going commands, and also stops
    612  1.1   briggs  * and resets the DMA.
    613  1.1   briggs  *
    614  1.1   briggs  * After reset, registers are loaded with the defaults from the attach
    615  1.1   briggs  * routine above.
    616  1.1   briggs  */
    617  1.1   briggs void
    618  1.1   briggs esp_reset(sc)
    619  1.1   briggs 	struct esp_softc *sc;
    620  1.1   briggs {
    621  1.1   briggs 
    622  1.1   briggs 	/* reset DMA first */
    623  1.1   briggs 	DMA_RESET(sc->sc_dma);
    624  1.1   briggs 
    625  1.1   briggs 	/* reset SCSI chip */
    626  1.1   briggs 	ESPCMD(sc, ESPCMD_RSTCHIP);
    627  1.1   briggs 	ESPCMD(sc, ESPCMD_NOP);
    628  1.1   briggs 	DELAY(500);
    629  1.1   briggs 
    630  1.1   briggs 	/* do these backwards, and fall through */
    631  1.1   briggs 	switch (sc->sc_rev) {
    632  1.1   briggs #ifndef SPARC_DRIVER
    633  1.1   briggs 	case NCR53C96:
    634  1.1   briggs 	case NCR53C94:
    635  1.1   briggs #endif
    636  1.1   briggs 	case ESP200:
    637  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG3, sc->sc_cfg3);
    638  1.1   briggs 	case ESP100A:
    639  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG2, sc->sc_cfg2);
    640  1.1   briggs 	case ESP100:
    641  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG1, sc->sc_cfg1);
    642  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CCF, sc->sc_ccf);
    643  1.1   briggs 		ESP_WRITE_REG(sc, ESP_SYNCOFF, 0);
    644  1.1   briggs 		ESP_WRITE_REG(sc, ESP_TIMEOUT, sc->sc_timeout);
    645  1.1   briggs 		break;
    646  1.1   briggs 	default:
    647  1.1   briggs 		printf("%s: unknown revision code, assuming ESP100\n",
    648  1.1   briggs 		    sc->sc_dev.dv_xname);
    649  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CFG1, sc->sc_cfg1);
    650  1.1   briggs 		ESP_WRITE_REG(sc, ESP_CCF, sc->sc_ccf);
    651  1.1   briggs 		ESP_WRITE_REG(sc, ESP_SYNCOFF, 0);
    652  1.1   briggs 		ESP_WRITE_REG(sc, ESP_TIMEOUT, sc->sc_timeout);
    653  1.1   briggs 	}
    654  1.1   briggs }
    655  1.1   briggs 
    656  1.1   briggs /*
    657  1.1   briggs  * Reset the SCSI bus, but not the chip
    658  1.1   briggs  */
    659  1.1   briggs void
    660  1.1   briggs esp_scsi_reset(sc)
    661  1.1   briggs 	struct esp_softc *sc;
    662  1.1   briggs {
    663  1.1   briggs #ifdef SPARC_DRIVER
    664  1.1   briggs 	/* stop DMA first, as the chip will return to Bus Free phase */
    665  1.1   briggs 	DMACSR(sc->sc_dma) &= ~D_EN_DMA;
    666  1.1   briggs #else
    667  1.1   briggs 	/*
    668  1.1   briggs 	 * XXX STOP DMA FIRST
    669  1.1   briggs 	 */
    670  1.1   briggs #endif
    671  1.1   briggs 
    672  1.1   briggs 	printf("esp: resetting SCSI bus\n");
    673  1.1   briggs 	ESPCMD(sc, ESPCMD_RSTSCSI);
    674  1.1   briggs }
    675  1.1   briggs 
    676  1.1   briggs /*
    677  1.1   briggs  * Initialize esp state machine
    678  1.1   briggs  */
    679  1.1   briggs void
    680  1.1   briggs esp_init(sc, doreset)
    681  1.1   briggs 	struct esp_softc *sc;
    682  1.1   briggs 	int doreset;
    683  1.1   briggs {
    684  1.1   briggs 	struct esp_ecb *ecb;
    685  1.1   briggs 	int r;
    686  1.1   briggs 
    687  1.1   briggs 	ESP_TRACE(("[ESP_INIT(%d)] ", doreset));
    688  1.1   briggs 
    689  1.1   briggs 	if (sc->sc_state == 0) {
    690  1.1   briggs 		/* First time through; initialize. */
    691  1.1   briggs 		TAILQ_INIT(&sc->ready_list);
    692  1.1   briggs 		TAILQ_INIT(&sc->nexus_list);
    693  1.1   briggs 		TAILQ_INIT(&sc->free_list);
    694  1.1   briggs 		sc->sc_nexus = NULL;
    695  1.1   briggs 		ecb = sc->sc_ecb;
    696  1.1   briggs 		bzero(ecb, sizeof(sc->sc_ecb));
    697  1.1   briggs 		for (r = 0; r < sizeof(sc->sc_ecb) / sizeof(*ecb); r++) {
    698  1.1   briggs 			TAILQ_INSERT_TAIL(&sc->free_list, ecb, chain);
    699  1.1   briggs 			ecb++;
    700  1.1   briggs 		}
    701  1.1   briggs 		bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
    702  1.1   briggs 	} else {
    703  1.1   briggs 		/* Cancel any active commands. */
    704  1.1   briggs 		sc->sc_state = ESP_CLEANING;
    705  1.1   briggs 		if ((ecb = sc->sc_nexus) != NULL) {
    706  1.1   briggs 			ecb->xs->error = XS_DRIVER_STUFFUP;
    707  1.1   briggs 			untimeout(esp_timeout, ecb);
    708  1.1   briggs 			esp_done(sc, ecb);
    709  1.1   briggs 		}
    710  1.1   briggs 		while ((ecb = sc->nexus_list.tqh_first) != NULL) {
    711  1.1   briggs 			ecb->xs->error = XS_DRIVER_STUFFUP;
    712  1.1   briggs 			untimeout(esp_timeout, ecb);
    713  1.1   briggs 			esp_done(sc, ecb);
    714  1.1   briggs 		}
    715  1.1   briggs 	}
    716  1.1   briggs 
    717  1.1   briggs 	/*
    718  1.1   briggs 	 * reset the chip to a known state
    719  1.1   briggs 	 */
    720  1.1   briggs 	esp_reset(sc);
    721  1.1   briggs 
    722  1.1   briggs 	sc->sc_phase = sc->sc_prevphase = INVALID_PHASE;
    723  1.1   briggs 	for (r = 0; r < 8; r++) {
    724  1.1   briggs 		struct esp_tinfo *ti = &sc->sc_tinfo[r];
    725  1.1   briggs /* XXX - config flags per target: low bits: no reselect; high bits: no synch */
    726  1.1   briggs 		int fl = sc->sc_dev.dv_cfdata->cf_flags;
    727  1.1   briggs 
    728  1.1   briggs 		ti->flags = ((sc->sc_minsync && !(fl & (1<<(r+8))))
    729  1.1   briggs 				? T_NEGOTIATE : 0) |
    730  1.1   briggs 				((fl & (1<<r)) ? T_RSELECTOFF : 0) |
    731  1.1   briggs 				T_NEED_TO_RESET;
    732  1.1   briggs 		ti->period = sc->sc_minsync;
    733  1.1   briggs 		ti->offset = 0;
    734  1.1   briggs 	}
    735  1.1   briggs 
    736  1.1   briggs 	if (doreset) {
    737  1.1   briggs 		sc->sc_state = ESP_SBR;
    738  1.1   briggs 		ESPCMD(sc, ESPCMD_RSTSCSI);
    739  1.1   briggs 	} else {
    740  1.1   briggs 		sc->sc_state = ESP_IDLE;
    741  1.1   briggs 	}
    742  1.1   briggs }
    743  1.1   briggs 
    744  1.1   briggs /*
    745  1.1   briggs  * Read the ESP registers, and save their contents for later use.
    746  1.1   briggs  * ESP_STAT, ESP_STEP & ESP_INTR are mostly zeroed out when reading
    747  1.1   briggs  * ESP_INTR - so make sure it is the last read.
    748  1.1   briggs  *
    749  1.1   briggs  * I think that (from reading the docs) most bits in these registers
    750  1.1   briggs  * only make sense when he DMA CSR has an interrupt showing. Call only
    751  1.1   briggs  * if an interrupt is pending.
    752  1.1   briggs  */
    753  1.1   briggs void
    754  1.1   briggs espreadregs(sc)
    755  1.1   briggs 	struct esp_softc *sc;
    756  1.1   briggs {
    757  1.1   briggs 
    758  1.1   briggs 	sc->sc_espstat = ESP_READ_REG(sc, ESP_STAT);
    759  1.1   briggs 	/* Only the stepo bits are of interest */
    760  1.1   briggs 	sc->sc_espstep = ESP_READ_REG(sc, ESP_STEP) & ESPSTEP_MASK;
    761  1.1   briggs 	sc->sc_espintr = ESP_READ_REG(sc, ESP_INTR);
    762  1.1   briggs 
    763  1.1   briggs #if !defined(SPARC_DRIVER) && !defined(MAC68K_DRIVER)
    764  1.1   briggs 	/* Clear the TCDS interrupt bit. */
    765  1.1   briggs 	(void)tcds_scsi_isintr(sc->sc_dma, 1);
    766  1.1   briggs #endif
    767  1.1   briggs 
    768  1.1   briggs 	/*
    769  1.1   briggs 	 * Determine the SCSI bus phase, return either a real SCSI bus phase
    770  1.1   briggs 	 * or some pseudo phase we use to detect certain exceptions.
    771  1.1   briggs 	 */
    772  1.1   briggs 
    773  1.1   briggs 	sc->sc_phase = (sc->sc_espintr & ESPINTR_DIS)
    774  1.1   briggs 			? /* Disconnected */ BUSFREE_PHASE
    775  1.1   briggs 			: sc->sc_espstat & ESPSTAT_PHASE;
    776  1.1   briggs 
    777  1.1   briggs 	ESP_MISC(("regs[intr=%02x,stat=%02x,step=%02x] ",
    778  1.1   briggs 		sc->sc_espintr, sc->sc_espstat, sc->sc_espstep));
    779  1.1   briggs }
    780  1.1   briggs 
    781  1.1   briggs /*
    782  1.1   briggs  * Convert chip register Clock Per Byte value to Synchronous Transfer Period.
    783  1.1   briggs  */
    784  1.1   briggs static inline int
    785  1.1   briggs esp_cpb2stp(sc, cpb)
    786  1.1   briggs 	struct esp_softc *sc;
    787  1.1   briggs 	int cpb;
    788  1.1   briggs {
    789  1.1   briggs 	return ((250 * cpb) / sc->sc_freq);
    790  1.1   briggs }
    791  1.1   briggs 
    792  1.1   briggs /*
    793  1.1   briggs  * Convert Synchronous Transfer Period to chip register Clock Per Byte value.
    794  1.1   briggs  */
    795  1.1   briggs static inline int
    796  1.1   briggs esp_stp2cpb(sc, period)
    797  1.1   briggs 	struct esp_softc *sc;
    798  1.1   briggs 	int period;
    799  1.1   briggs {
    800  1.1   briggs 	int v;
    801  1.1   briggs 	v = (sc->sc_freq * period) / 250;
    802  1.1   briggs 	if (esp_cpb2stp(sc, v) < period)
    803  1.1   briggs 		/* Correct round-down error */
    804  1.1   briggs 		v++;
    805  1.1   briggs 	return v;
    806  1.1   briggs }
    807  1.1   briggs 
    808  1.1   briggs static inline void
    809  1.1   briggs esp_setsync(sc, ti)
    810  1.1   briggs 	struct esp_softc *sc;
    811  1.1   briggs 	struct esp_tinfo *ti;
    812  1.1   briggs {
    813  1.1   briggs 
    814  1.1   briggs 	if (ti->flags & T_SYNCMODE) {
    815  1.1   briggs 		ESP_WRITE_REG(sc, ESP_SYNCOFF, ti->offset);
    816  1.1   briggs 		ESP_WRITE_REG(sc, ESP_SYNCTP, esp_stp2cpb(sc, ti->period));
    817  1.1   briggs 	} else {
    818  1.1   briggs 		ESP_WRITE_REG(sc, ESP_SYNCOFF, 0);
    819  1.1   briggs 		ESP_WRITE_REG(sc, ESP_SYNCTP, 0);
    820  1.1   briggs 	}
    821  1.1   briggs }
    822  1.1   briggs 
    823  1.1   briggs /*
    824  1.1   briggs  * Send a command to a target, set the driver state to ESP_SELECTING
    825  1.1   briggs  * and let the caller take care of the rest.
    826  1.1   briggs  *
    827  1.1   briggs  * Keeping this as a function allows me to say that this may be done
    828  1.1   briggs  * by DMA instead of programmed I/O soon.
    829  1.1   briggs  */
    830  1.1   briggs void
    831  1.1   briggs esp_select(sc, ecb)
    832  1.1   briggs 	struct esp_softc *sc;
    833  1.1   briggs 	struct esp_ecb *ecb;
    834  1.1   briggs {
    835  1.1   briggs 	struct scsi_link *sc_link = ecb->xs->sc_link;
    836  1.1   briggs 	int target = sc_link->target;
    837  1.1   briggs 	struct esp_tinfo *ti = &sc->sc_tinfo[target];
    838  1.1   briggs 	u_char *cmd;
    839  1.1   briggs 	int clen;
    840  1.1   briggs 
    841  1.1   briggs 	ESP_TRACE(("[esp_select(t%d,l%d,cmd:%x)] ", sc_link->target, sc_link->lun, ecb->cmd.opcode));
    842  1.1   briggs 
    843  1.1   briggs 	/* new state ESP_SELECTING */
    844  1.1   briggs 	sc->sc_state = ESP_SELECTING;
    845  1.1   briggs 
    846  1.1   briggs 	ESPCMD(sc, ESPCMD_FLUSH);
    847  1.1   briggs 
    848  1.1   briggs 	/*
    849  1.1   briggs 	 * The docs say the target register is never reset, and I
    850  1.1   briggs 	 * can't think of a better place to set it
    851  1.1   briggs 	 */
    852  1.1   briggs 	ESP_WRITE_REG(sc, ESP_SELID, target);
    853  1.1   briggs 	esp_setsync(sc, ti);
    854  1.1   briggs 
    855  1.1   briggs 	/*
    856  1.1   briggs 	 * Who am I. This is where we tell the target that we are
    857  1.1   briggs 	 * happy for it to disconnect etc.
    858  1.1   briggs 	 */
    859  1.1   briggs 	ESP_WRITE_REG(sc, ESP_FIFO,
    860  1.1   briggs 		MSG_IDENTIFY(sc_link->lun, (ti->flags & T_RSELECTOFF)?0:1));
    861  1.1   briggs 
    862  1.1   briggs 	if (ti->flags & T_NEGOTIATE) {
    863  1.1   briggs 		/* Arbitrate, select and stop after IDENTIFY message */
    864  1.1   briggs 		ESPCMD(sc, ESPCMD_SELATNS);
    865  1.1   briggs 		return;
    866  1.1   briggs 	}
    867  1.1   briggs 
    868  1.1   briggs 	/* Now the command into the FIFO */
    869  1.1   briggs 	cmd = (u_char *)&ecb->cmd;
    870  1.1   briggs 	clen = ecb->clen;
    871  1.1   briggs 	while (clen--)
    872  1.1   briggs 		ESP_WRITE_REG(sc, ESP_FIFO, *cmd++);
    873  1.1   briggs 
    874  1.1   briggs 	/* And get the targets attention */
    875  1.1   briggs 	ESPCMD(sc, ESPCMD_SELATN);
    876  1.1   briggs }
    877  1.1   briggs 
    878  1.1   briggs void
    879  1.1   briggs esp_free_ecb(sc, ecb, flags)
    880  1.1   briggs 	struct esp_softc *sc;
    881  1.1   briggs 	struct esp_ecb *ecb;
    882  1.1   briggs 	int flags;
    883  1.1   briggs {
    884  1.1   briggs 	int s;
    885  1.1   briggs 
    886  1.1   briggs 	s = splbio();
    887  1.1   briggs 
    888  1.1   briggs 	ecb->flags = 0;
    889  1.1   briggs 	TAILQ_INSERT_HEAD(&sc->free_list, ecb, chain);
    890  1.1   briggs 
    891  1.1   briggs 	/*
    892  1.1   briggs 	 * If there were none, wake anybody waiting for one to come free,
    893  1.1   briggs 	 * starting with queued entries.
    894  1.1   briggs 	 */
    895  1.1   briggs 	if (ecb->chain.tqe_next == 0)
    896  1.1   briggs 		wakeup(&sc->free_list);
    897  1.1   briggs 
    898  1.1   briggs 	splx(s);
    899  1.1   briggs }
    900  1.1   briggs 
    901  1.1   briggs struct esp_ecb *
    902  1.1   briggs esp_get_ecb(sc, flags)
    903  1.1   briggs 	struct esp_softc *sc;
    904  1.1   briggs 	int flags;
    905  1.1   briggs {
    906  1.1   briggs 	struct esp_ecb *ecb;
    907  1.1   briggs 	int s;
    908  1.1   briggs 
    909  1.1   briggs 	s = splbio();
    910  1.1   briggs 
    911  1.1   briggs 	while ((ecb = sc->free_list.tqh_first) == NULL &&
    912  1.1   briggs 	       (flags & SCSI_NOSLEEP) == 0)
    913  1.1   briggs 		tsleep(&sc->free_list, PRIBIO, "especb", 0);
    914  1.1   briggs 	if (ecb) {
    915  1.1   briggs 		TAILQ_REMOVE(&sc->free_list, ecb, chain);
    916  1.1   briggs 		ecb->flags |= ECB_ALLOC;
    917  1.1   briggs 	}
    918  1.1   briggs 
    919  1.1   briggs 	splx(s);
    920  1.1   briggs 	return ecb;
    921  1.1   briggs }
    922  1.1   briggs 
    923  1.1   briggs /*
    924  1.1   briggs  * DRIVER FUNCTIONS CALLABLE FROM HIGHER LEVEL DRIVERS
    925  1.1   briggs  */
    926  1.1   briggs 
    927  1.1   briggs /*
    928  1.1   briggs  * Start a SCSI-command
    929  1.1   briggs  * This function is called by the higher level SCSI-driver to queue/run
    930  1.1   briggs  * SCSI-commands.
    931  1.1   briggs  */
    932  1.1   briggs int
    933  1.1   briggs esp_scsi_cmd(xs)
    934  1.1   briggs 	struct scsi_xfer *xs;
    935  1.1   briggs {
    936  1.1   briggs 	struct scsi_link *sc_link = xs->sc_link;
    937  1.1   briggs 	struct esp_softc *sc = sc_link->adapter_softc;
    938  1.1   briggs 	struct esp_ecb *ecb;
    939  1.1   briggs 	int s, flags;
    940  1.1   briggs 
    941  1.1   briggs 	ESP_TRACE(("[esp_scsi_cmd] "));
    942  1.1   briggs 	ESP_CMDS(("[0x%x, %d]->%d ", (int)xs->cmd->opcode, xs->cmdlen,
    943  1.1   briggs 	    sc_link->target));
    944  1.1   briggs 
    945  1.1   briggs 	flags = xs->flags;
    946  1.1   briggs 	if ((ecb = esp_get_ecb(sc, flags)) == NULL) {
    947  1.1   briggs 		xs->error = XS_DRIVER_STUFFUP;
    948  1.1   briggs 		return TRY_AGAIN_LATER;
    949  1.1   briggs 	}
    950  1.1   briggs 
    951  1.1   briggs 	/* Initialize ecb */
    952  1.1   briggs 	ecb->xs = xs;
    953  1.1   briggs 	ecb->timeout = xs->timeout;
    954  1.1   briggs 
    955  1.1   briggs 	if (xs->flags & SCSI_RESET) {
    956  1.1   briggs 		ecb->flags |= ECB_RESET;
    957  1.1   briggs 		ecb->clen = 0;
    958  1.1   briggs 		ecb->dleft = 0;
    959  1.1   briggs 	} else {
    960  1.1   briggs 		bcopy(xs->cmd, &ecb->cmd, xs->cmdlen);
    961  1.1   briggs 		ecb->clen = xs->cmdlen;
    962  1.1   briggs 		ecb->daddr = xs->data;
    963  1.1   briggs 		ecb->dleft = xs->datalen;
    964  1.1   briggs 	}
    965  1.1   briggs 	ecb->stat = 0;
    966  1.1   briggs 
    967  1.1   briggs 	s = splbio();
    968  1.1   briggs 
    969  1.1   briggs 	TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
    970  1.1   briggs 	if (sc->sc_state == ESP_IDLE)
    971  1.1   briggs 		esp_sched(sc);
    972  1.1   briggs 
    973  1.1   briggs 	splx(s);
    974  1.1   briggs 
    975  1.1   briggs 	if ((flags & SCSI_POLL) == 0)
    976  1.1   briggs 		return SUCCESSFULLY_QUEUED;
    977  1.1   briggs 
    978  1.1   briggs 	/* Not allowed to use interrupts, use polling instead */
    979  1.1   briggs 	if (esp_poll(sc, xs, ecb->timeout)) {
    980  1.1   briggs 		esp_timeout(ecb);
    981  1.1   briggs 		if (esp_poll(sc, xs, ecb->timeout))
    982  1.1   briggs 			esp_timeout(ecb);
    983  1.1   briggs 	}
    984  1.1   briggs 	return COMPLETE;
    985  1.1   briggs }
    986  1.1   briggs 
    987  1.1   briggs /*
    988  1.1   briggs  * Used when interrupt driven I/O isn't allowed, e.g. during boot.
    989  1.1   briggs  */
    990  1.1   briggs int
    991  1.1   briggs esp_poll(sc, xs, count)
    992  1.1   briggs 	struct esp_softc *sc;
    993  1.1   briggs 	struct scsi_xfer *xs;
    994  1.1   briggs 	int count;
    995  1.1   briggs {
    996  1.1   briggs 
    997  1.1   briggs 	ESP_TRACE(("[esp_poll] "));
    998  1.1   briggs 	while (count) {
    999  1.1   briggs 		if (DMA_ISINTR(sc->sc_dma)) {
   1000  1.1   briggs 			espintr(sc);
   1001  1.1   briggs 		}
   1002  1.1   briggs #if alternatively
   1003  1.1   briggs 		if (ESP_READ_REG(sc, ESP_STAT) & ESPSTAT_INT)
   1004  1.1   briggs 			espintr(sc);
   1005  1.1   briggs #endif
   1006  1.1   briggs 		if ((xs->flags & ITSDONE) != 0)
   1007  1.1   briggs 			return 0;
   1008  1.1   briggs 		if (sc->sc_state == ESP_IDLE) {
   1009  1.1   briggs 			ESP_TRACE(("[esp_poll: rescheduling] "));
   1010  1.1   briggs 			esp_sched(sc);
   1011  1.1   briggs 		}
   1012  1.1   briggs 		DELAY(1000);
   1013  1.1   briggs 		count--;
   1014  1.1   briggs 	}
   1015  1.1   briggs 	return 1;
   1016  1.1   briggs }
   1017  1.1   briggs 
   1018  1.1   briggs 
   1019  1.1   briggs /*
   1020  1.1   briggs  * LOW LEVEL SCSI UTILITIES
   1021  1.1   briggs  */
   1022  1.1   briggs 
   1023  1.1   briggs /*
   1024  1.1   briggs  * Schedule a scsi operation.  This has now been pulled out of the interrupt
   1025  1.1   briggs  * handler so that we may call it from esp_scsi_cmd and esp_done.  This may
   1026  1.1   briggs  * save us an unecessary interrupt just to get things going.  Should only be
   1027  1.1   briggs  * called when state == ESP_IDLE and at bio pl.
   1028  1.1   briggs  */
   1029  1.1   briggs void
   1030  1.1   briggs esp_sched(sc)
   1031  1.1   briggs 	struct esp_softc *sc;
   1032  1.1   briggs {
   1033  1.1   briggs 	struct esp_ecb *ecb;
   1034  1.1   briggs 	struct scsi_link *sc_link;
   1035  1.1   briggs 	struct esp_tinfo *ti;
   1036  1.1   briggs 
   1037  1.1   briggs 	ESP_TRACE(("[esp_sched] "));
   1038  1.1   briggs 	if (sc->sc_state != ESP_IDLE)
   1039  1.1   briggs 		panic("esp_sched: not IDLE (state=%d)", sc->sc_state);
   1040  1.1   briggs 
   1041  1.1   briggs 	/*
   1042  1.1   briggs 	 * Find first ecb in ready queue that is for a target/lunit
   1043  1.1   briggs 	 * combinations that is not busy.
   1044  1.1   briggs 	 */
   1045  1.1   briggs 	for (ecb = sc->ready_list.tqh_first; ecb; ecb = ecb->chain.tqe_next) {
   1046  1.1   briggs 		sc_link = ecb->xs->sc_link;
   1047  1.1   briggs 		ti = &sc->sc_tinfo[sc_link->target];
   1048  1.1   briggs 		if ((ti->lubusy & (1 << sc_link->lun)) == 0) {
   1049  1.1   briggs 			TAILQ_REMOVE(&sc->ready_list, ecb, chain);
   1050  1.1   briggs 			sc->sc_nexus = ecb;
   1051  1.1   briggs 			esp_select(sc, ecb);
   1052  1.1   briggs 			break;
   1053  1.1   briggs 		} else
   1054  1.1   briggs 			ESP_MISC(("%d:%d busy\n",
   1055  1.1   briggs 			    sc_link->target, sc_link->lun));
   1056  1.1   briggs 	}
   1057  1.1   briggs }
   1058  1.1   briggs 
   1059  1.1   briggs void
   1060  1.1   briggs esp_sense(sc, ecb)
   1061  1.1   briggs 	struct esp_softc *sc;
   1062  1.1   briggs 	struct esp_ecb *ecb;
   1063  1.1   briggs {
   1064  1.1   briggs 	struct scsi_xfer *xs = ecb->xs;
   1065  1.1   briggs 	struct scsi_link *sc_link = xs->sc_link;
   1066  1.1   briggs 	struct esp_tinfo *ti = &sc->sc_tinfo[sc_link->target];
   1067  1.1   briggs 	struct scsi_sense *ss = (void *)&ecb->cmd;
   1068  1.1   briggs 
   1069  1.1   briggs 	ESP_MISC(("requesting sense "));
   1070  1.1   briggs 	/* Next, setup a request sense command block */
   1071  1.1   briggs 	bzero(ss, sizeof(*ss));
   1072  1.1   briggs 	ss->opcode = REQUEST_SENSE;
   1073  1.1   briggs 	ss->byte2 = sc_link->lun << 5;
   1074  1.1   briggs 	ss->length = sizeof(struct scsi_sense_data);
   1075  1.1   briggs 	ecb->clen = sizeof(*ss);
   1076  1.1   briggs 	ecb->daddr = (char *)&xs->sense;
   1077  1.1   briggs 	ecb->dleft = sizeof(struct scsi_sense_data);
   1078  1.1   briggs 	ecb->flags |= ECB_SENSE;
   1079  1.1   briggs 	ti->senses++;
   1080  1.1   briggs 	if (ecb->flags & ECB_NEXUS)
   1081  1.1   briggs 		ti->lubusy &= ~(1 << sc_link->lun);
   1082  1.1   briggs 	if (ecb == sc->sc_nexus) {
   1083  1.1   briggs 		esp_select(sc, ecb);
   1084  1.1   briggs 	} else {
   1085  1.1   briggs 		esp_dequeue(sc, ecb);
   1086  1.1   briggs 		TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
   1087  1.1   briggs 		if (sc->sc_state == ESP_IDLE)
   1088  1.1   briggs 			esp_sched(sc);
   1089  1.1   briggs 	}
   1090  1.1   briggs }
   1091  1.1   briggs 
   1092  1.1   briggs /*
   1093  1.1   briggs  * POST PROCESSING OF SCSI_CMD (usually current)
   1094  1.1   briggs  */
   1095  1.1   briggs void
   1096  1.1   briggs esp_done(sc, ecb)
   1097  1.1   briggs 	struct esp_softc *sc;
   1098  1.1   briggs 	struct esp_ecb *ecb;
   1099  1.1   briggs {
   1100  1.1   briggs 	struct scsi_xfer *xs = ecb->xs;
   1101  1.1   briggs 	struct scsi_link *sc_link = xs->sc_link;
   1102  1.1   briggs 	struct esp_tinfo *ti = &sc->sc_tinfo[sc_link->target];
   1103  1.1   briggs 
   1104  1.1   briggs 	ESP_TRACE(("[esp_done(error:%x)] ", xs->error));
   1105  1.1   briggs 
   1106  1.1   briggs 	/*
   1107  1.1   briggs 	 * Now, if we've come here with no error code, i.e. we've kept the
   1108  1.1   briggs 	 * initial XS_NOERROR, and the status code signals that we should
   1109  1.1   briggs 	 * check sense, we'll need to set up a request sense cmd block and
   1110  1.1   briggs 	 * push the command back into the ready queue *before* any other
   1111  1.1   briggs 	 * commands for this target/lunit, else we lose the sense info.
   1112  1.1   briggs 	 * We don't support chk sense conditions for the request sense cmd.
   1113  1.1   briggs 	 */
   1114  1.1   briggs 	if (xs->error == XS_NOERROR) {
   1115  1.1   briggs 		if ((ecb->flags & ECB_ABORT) != 0) {
   1116  1.1   briggs 			xs->error = XS_DRIVER_STUFFUP;
   1117  1.1   briggs 		} else if ((ecb->flags & ECB_SENSE) != 0) {
   1118  1.1   briggs 			xs->error = XS_SENSE;
   1119  1.1   briggs 		} else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
   1120  1.1   briggs 			/* First, save the return values */
   1121  1.1   briggs 			xs->resid = ecb->dleft;
   1122  1.1   briggs 			xs->status = ecb->stat;
   1123  1.1   briggs 			esp_sense(sc, ecb);
   1124  1.1   briggs 			return;
   1125  1.1   briggs 		} else {
   1126  1.1   briggs 			xs->resid = ecb->dleft;
   1127  1.1   briggs 		}
   1128  1.1   briggs 	}
   1129  1.1   briggs 
   1130  1.1   briggs 	xs->flags |= ITSDONE;
   1131  1.1   briggs 
   1132  1.3   briggs #if ESP_DEBUG > 0
   1133  1.1   briggs 	if (esp_debug & ESP_SHOWMISC) {
   1134  1.1   briggs 		if (xs->resid != 0)
   1135  1.1   briggs 			printf("resid=%d ", xs->resid);
   1136  1.1   briggs 		if (xs->error == XS_SENSE)
   1137  1.1   briggs 			printf("sense=0x%02x\n", xs->sense.error_code);
   1138  1.1   briggs 		else
   1139  1.1   briggs 			printf("error=%d\n", xs->error);
   1140  1.1   briggs 	}
   1141  1.1   briggs #endif
   1142  1.1   briggs 
   1143  1.1   briggs 	/*
   1144  1.1   briggs 	 * Remove the ECB from whatever queue it's on.
   1145  1.1   briggs 	 */
   1146  1.1   briggs 	if (ecb->flags & ECB_NEXUS)
   1147  1.1   briggs 		ti->lubusy &= ~(1 << sc_link->lun);
   1148  1.1   briggs 	if (ecb == sc->sc_nexus) {
   1149  1.1   briggs 		sc->sc_nexus = NULL;
   1150  1.1   briggs 		sc->sc_state = ESP_IDLE;
   1151  1.1   briggs 		esp_sched(sc);
   1152  1.1   briggs 	} else
   1153  1.1   briggs 		esp_dequeue(sc, ecb);
   1154  1.1   briggs 
   1155  1.1   briggs 	esp_free_ecb(sc, ecb, xs->flags);
   1156  1.1   briggs 	ti->cmds++;
   1157  1.1   briggs 	scsi_done(xs);
   1158  1.1   briggs }
   1159  1.1   briggs 
   1160  1.1   briggs void
   1161  1.1   briggs esp_dequeue(sc, ecb)
   1162  1.1   briggs 	struct esp_softc *sc;
   1163  1.1   briggs 	struct esp_ecb *ecb;
   1164  1.1   briggs {
   1165  1.1   briggs 
   1166  1.1   briggs 	if (ecb->flags & ECB_NEXUS) {
   1167  1.1   briggs 		TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
   1168  1.1   briggs 	} else {
   1169  1.1   briggs 		TAILQ_REMOVE(&sc->ready_list, ecb, chain);
   1170  1.1   briggs 	}
   1171  1.1   briggs }
   1172  1.1   briggs 
   1173  1.1   briggs /*
   1174  1.1   briggs  * INTERRUPT/PROTOCOL ENGINE
   1175  1.1   briggs  */
   1176  1.1   briggs 
   1177  1.1   briggs /*
   1178  1.1   briggs  * Schedule an outgoing message by prioritizing it, and asserting
   1179  1.1   briggs  * attention on the bus. We can only do this when we are the initiator
   1180  1.1   briggs  * else there will be an illegal command interrupt.
   1181  1.1   briggs  */
   1182  1.1   briggs #define esp_sched_msgout(m) \
   1183  1.1   briggs 	do {						\
   1184  1.1   briggs 		ESP_MISC(("esp_sched_msgout %d ", m));	\
   1185  1.1   briggs 		ESPCMD(sc, ESPCMD_SETATN);		\
   1186  1.1   briggs 		sc->sc_flags |= ESP_ATN;		\
   1187  1.1   briggs 		sc->sc_msgpriq |= (m);			\
   1188  1.1   briggs 	} while (0)
   1189  1.1   briggs 
   1190  1.1   briggs int
   1191  1.1   briggs esp_reselect(sc, message)
   1192  1.1   briggs 	struct esp_softc *sc;
   1193  1.1   briggs 	int message;
   1194  1.1   briggs {
   1195  1.1   briggs 	u_char selid, target, lun;
   1196  1.1   briggs 	struct esp_ecb *ecb;
   1197  1.1   briggs 	struct scsi_link *sc_link;
   1198  1.1   briggs 	struct esp_tinfo *ti;
   1199  1.1   briggs 
   1200  1.1   briggs 	/*
   1201  1.1   briggs 	 * The SCSI chip made a snapshot of the data bus while the reselection
   1202  1.1   briggs 	 * was being negotiated.  This enables us to determine which target did
   1203  1.1   briggs 	 * the reselect.
   1204  1.1   briggs 	 */
   1205  1.1   briggs 	selid = sc->sc_selid & ~(1 << sc->sc_id);
   1206  1.1   briggs 	if (selid & (selid - 1)) {
   1207  1.1   briggs 		printf("%s: reselect with invalid selid %02x; sending DEVICE RESET\n",
   1208  1.1   briggs 		    sc->sc_dev.dv_xname, selid);
   1209  1.1   briggs 		goto reset;
   1210  1.1   briggs 	}
   1211  1.1   briggs 
   1212  1.1   briggs 	/*
   1213  1.1   briggs 	 * Search wait queue for disconnected cmd
   1214  1.1   briggs 	 * The list should be short, so I haven't bothered with
   1215  1.1   briggs 	 * any more sophisticated structures than a simple
   1216  1.1   briggs 	 * singly linked list.
   1217  1.1   briggs 	 */
   1218  1.1   briggs 	target = ffs(selid) - 1;
   1219  1.1   briggs 	lun = message & 0x07;
   1220  1.1   briggs 	for (ecb = sc->nexus_list.tqh_first; ecb != NULL;
   1221  1.1   briggs 	     ecb = ecb->chain.tqe_next) {
   1222  1.1   briggs 		sc_link = ecb->xs->sc_link;
   1223  1.1   briggs 		if (sc_link->target == target && sc_link->lun == lun)
   1224  1.1   briggs 			break;
   1225  1.1   briggs 	}
   1226  1.1   briggs 	if (ecb == NULL) {
   1227  1.1   briggs 		printf("%s: reselect from target %d lun %d with no nexus; sending ABORT\n",
   1228  1.1   briggs 		    sc->sc_dev.dv_xname, target, lun);
   1229  1.1   briggs 		goto abort;
   1230  1.1   briggs 	}
   1231  1.1   briggs 
   1232  1.1   briggs 	/* Make this nexus active again. */
   1233  1.1   briggs 	TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
   1234  1.1   briggs 	sc->sc_state = ESP_CONNECTED;
   1235  1.1   briggs 	sc->sc_nexus = ecb;
   1236  1.1   briggs 	ti = &sc->sc_tinfo[target];
   1237  1.1   briggs 	ti->lubusy |= (1 << lun);
   1238  1.1   briggs 	esp_setsync(sc, ti);
   1239  1.1   briggs 
   1240  1.1   briggs 	if (ecb->flags & ECB_RESET)
   1241  1.1   briggs 		esp_sched_msgout(SEND_DEV_RESET);
   1242  1.1   briggs 	else if (ecb->flags & ECB_ABORT)
   1243  1.1   briggs 		esp_sched_msgout(SEND_ABORT);
   1244  1.1   briggs 
   1245  1.1   briggs 	/* Do an implicit RESTORE POINTERS. */
   1246  1.1   briggs 	sc->sc_dp = ecb->daddr;
   1247  1.1   briggs 	sc->sc_dleft = ecb->dleft;
   1248  1.1   briggs 
   1249  1.1   briggs 	return (0);
   1250  1.1   briggs 
   1251  1.1   briggs reset:
   1252  1.1   briggs 	esp_sched_msgout(SEND_DEV_RESET);
   1253  1.1   briggs 	return (1);
   1254  1.1   briggs 
   1255  1.1   briggs abort:
   1256  1.1   briggs 	esp_sched_msgout(SEND_ABORT);
   1257  1.1   briggs 	return (1);
   1258  1.1   briggs }
   1259  1.1   briggs 
   1260  1.1   briggs #define IS1BYTEMSG(m) (((m) != 1 && (m) < 0x20) || (m) & 0x80)
   1261  1.1   briggs #define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
   1262  1.1   briggs #define ISEXTMSG(m) ((m) == 1)
   1263  1.1   briggs 
   1264  1.1   briggs /*
   1265  1.1   briggs  * Get an incoming message as initiator.
   1266  1.1   briggs  *
   1267  1.1   briggs  * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
   1268  1.1   briggs  * byte in the FIFO
   1269  1.1   briggs  */
   1270  1.1   briggs void
   1271  1.1   briggs esp_msgin(sc)
   1272  1.1   briggs 	register struct esp_softc *sc;
   1273  1.1   briggs {
   1274  1.1   briggs 	register int v;
   1275  1.1   briggs 
   1276  1.1   briggs 	ESP_TRACE(("[esp_msgin(curmsglen:%d)] ", sc->sc_imlen));
   1277  1.1   briggs 
   1278  1.1   briggs 	if ((ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) == 0) {
   1279  1.1   briggs 		printf("%s: msgin: no msg byte available\n",
   1280  1.1   briggs 			sc->sc_dev.dv_xname);
   1281  1.1   briggs 		return;
   1282  1.1   briggs 	}
   1283  1.1   briggs 
   1284  1.1   briggs 	/*
   1285  1.1   briggs 	 * Prepare for a new message.  A message should (according
   1286  1.1   briggs 	 * to the SCSI standard) be transmitted in one single
   1287  1.1   briggs 	 * MESSAGE_IN_PHASE. If we have been in some other phase,
   1288  1.1   briggs 	 * then this is a new message.
   1289  1.1   briggs 	 */
   1290  1.1   briggs 	if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
   1291  1.1   briggs 		sc->sc_flags &= ~ESP_DROP_MSGI;
   1292  1.1   briggs 		sc->sc_imlen = 0;
   1293  1.1   briggs 	}
   1294  1.1   briggs 
   1295  1.1   briggs 	v = ESP_READ_REG(sc, ESP_FIFO);
   1296  1.1   briggs 	ESP_MISC(("<msgbyte:0x%02x>", v));
   1297  1.1   briggs 
   1298  1.1   briggs #if 0
   1299  1.1   briggs 	if (sc->sc_state == ESP_RESELECTED && sc->sc_imlen == 0) {
   1300  1.1   briggs 		/*
   1301  1.1   briggs 		 * Which target is reselecting us? (The ID bit really)
   1302  1.1   briggs 		 */
   1303  1.1   briggs 		sc->sc_selid = v;
   1304  1.1   briggs 		ESP_MISC(("selid=0x%2x ", sc->sc_selid));
   1305  1.1   briggs 		return;
   1306  1.1   briggs 	}
   1307  1.1   briggs #endif
   1308  1.1   briggs 
   1309  1.1   briggs 	sc->sc_imess[sc->sc_imlen] = v;
   1310  1.1   briggs 
   1311  1.1   briggs 	/*
   1312  1.1   briggs 	 * If we're going to reject the message, don't bother storing
   1313  1.1   briggs 	 * the incoming bytes.  But still, we need to ACK them.
   1314  1.1   briggs 	 */
   1315  1.1   briggs 
   1316  1.1   briggs 	if ((sc->sc_flags & ESP_DROP_MSGI)) {
   1317  1.1   briggs 		ESPCMD(sc, ESPCMD_MSGOK);
   1318  1.1   briggs 		printf("<dropping msg byte %x>",
   1319  1.1   briggs 			sc->sc_imess[sc->sc_imlen]);
   1320  1.1   briggs 		return;
   1321  1.1   briggs 	}
   1322  1.1   briggs 
   1323  1.1   briggs 	if (sc->sc_imlen >= ESP_MAX_MSG_LEN) {
   1324  1.1   briggs 		esp_sched_msgout(SEND_REJECT);
   1325  1.1   briggs 		sc->sc_flags |= ESP_DROP_MSGI;
   1326  1.1   briggs 	} else {
   1327  1.1   briggs 		sc->sc_imlen++;
   1328  1.1   briggs 		/*
   1329  1.1   briggs 		 * This testing is suboptimal, but most
   1330  1.1   briggs 		 * messages will be of the one byte variety, so
   1331  1.1   briggs 		 * it should not effect performance
   1332  1.1   briggs 		 * significantly.
   1333  1.1   briggs 		 */
   1334  1.1   briggs 		if (sc->sc_imlen == 1 && IS1BYTEMSG(sc->sc_imess[0]))
   1335  1.1   briggs 			goto gotit;
   1336  1.1   briggs 		if (sc->sc_imlen == 2 && IS2BYTEMSG(sc->sc_imess[0]))
   1337  1.1   briggs 			goto gotit;
   1338  1.1   briggs 		if (sc->sc_imlen >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
   1339  1.1   briggs 		    sc->sc_imlen == sc->sc_imess[1] + 2)
   1340  1.1   briggs 			goto gotit;
   1341  1.1   briggs 	}
   1342  1.1   briggs 	/* Ack what we have so far */
   1343  1.1   briggs 	ESPCMD(sc, ESPCMD_MSGOK);
   1344  1.1   briggs 	return;
   1345  1.1   briggs 
   1346  1.1   briggs gotit:
   1347  1.1   briggs 	ESP_MSGS(("gotmsg(%x)", sc->sc_imess[0]));
   1348  1.1   briggs 	/*
   1349  1.1   briggs 	 * Now we should have a complete message (1 byte, 2 byte
   1350  1.1   briggs 	 * and moderately long extended messages).  We only handle
   1351  1.1   briggs 	 * extended messages which total length is shorter than
   1352  1.1   briggs 	 * ESP_MAX_MSG_LEN.  Longer messages will be amputated.
   1353  1.1   briggs 	 */
   1354  1.1   briggs 	switch (sc->sc_state) {
   1355  1.1   briggs 		struct esp_ecb *ecb;
   1356  1.1   briggs 		struct esp_tinfo *ti;
   1357  1.1   briggs 
   1358  1.1   briggs 	case ESP_CONNECTED:
   1359  1.1   briggs 		ecb = sc->sc_nexus;
   1360  1.1   briggs 		ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
   1361  1.1   briggs 
   1362  1.1   briggs 		switch (sc->sc_imess[0]) {
   1363  1.1   briggs 		case MSG_CMDCOMPLETE:
   1364  1.1   briggs 			ESP_MSGS(("cmdcomplete "));
   1365  1.1   briggs 			if (sc->sc_dleft < 0) {
   1366  1.1   briggs 				struct scsi_link *sc_link = ecb->xs->sc_link;
   1367  1.1   briggs 				printf("%s: %d extra bytes from %d:%d\n",
   1368  1.1   briggs 				    sc->sc_dev.dv_xname, -sc->sc_dleft,
   1369  1.1   briggs 				    sc_link->target, sc_link->lun);
   1370  1.1   briggs 				sc->sc_dleft = 0;
   1371  1.1   briggs 			}
   1372  1.1   briggs 			ecb->xs->resid = ecb->dleft = sc->sc_dleft;
   1373  1.1   briggs 			sc->sc_state = ESP_CMDCOMPLETE;
   1374  1.1   briggs 			break;
   1375  1.1   briggs 
   1376  1.1   briggs 		case MSG_MESSAGE_REJECT:
   1377  1.1   briggs 			if (esp_debug & ESP_SHOWMSGS)
   1378  1.1   briggs 				printf("%s: our msg rejected by target\n",
   1379  1.1   briggs 				    sc->sc_dev.dv_xname);
   1380  1.1   briggs 			switch (sc->sc_msgout) {
   1381  1.1   briggs 			case SEND_SDTR:
   1382  1.1   briggs 				sc->sc_flags &= ~ESP_SYNCHNEGO;
   1383  1.1   briggs 				ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
   1384  1.1   briggs 				esp_setsync(sc, ti);
   1385  1.1   briggs 				break;
   1386  1.1   briggs 			case SEND_INIT_DET_ERR:
   1387  1.1   briggs 				goto abort;
   1388  1.1   briggs 			}
   1389  1.1   briggs 			break;
   1390  1.1   briggs 
   1391  1.1   briggs 		case MSG_NOOP:
   1392  1.1   briggs 			ESP_MSGS(("noop "));
   1393  1.1   briggs 			break;
   1394  1.1   briggs 
   1395  1.1   briggs 		case MSG_DISCONNECT:
   1396  1.1   briggs 			ESP_MSGS(("disconnect "));
   1397  1.1   briggs 			ti->dconns++;
   1398  1.1   briggs 			sc->sc_state = ESP_DISCONNECT;
   1399  1.1   briggs 			if ((ecb->xs->sc_link->quirks & SDEV_AUTOSAVE) == 0)
   1400  1.1   briggs 				break;
   1401  1.1   briggs 			/*FALLTHROUGH*/
   1402  1.1   briggs 
   1403  1.1   briggs 		case MSG_SAVEDATAPOINTER:
   1404  1.1   briggs 			ESP_MSGS(("save datapointer "));
   1405  1.1   briggs 			ecb->daddr = sc->sc_dp;
   1406  1.1   briggs 			ecb->dleft = sc->sc_dleft;
   1407  1.1   briggs 			break;
   1408  1.1   briggs 
   1409  1.1   briggs 		case MSG_RESTOREPOINTERS:
   1410  1.1   briggs 			ESP_MSGS(("restore datapointer "));
   1411  1.1   briggs 			sc->sc_dp = ecb->daddr;
   1412  1.1   briggs 			sc->sc_dleft = ecb->dleft;
   1413  1.1   briggs 			break;
   1414  1.1   briggs 
   1415  1.1   briggs 		case MSG_EXTENDED:
   1416  1.1   briggs 			ESP_MSGS(("extended(%x) ", sc->sc_imess[2]));
   1417  1.1   briggs 			switch (sc->sc_imess[2]) {
   1418  1.1   briggs 			case MSG_EXT_SDTR:
   1419  1.1   briggs 				ESP_MSGS(("SDTR period %d, offset %d ",
   1420  1.1   briggs 					sc->sc_imess[3], sc->sc_imess[4]));
   1421  1.1   briggs 				if (sc->sc_imess[1] != 3)
   1422  1.1   briggs 					goto reject;
   1423  1.1   briggs 				ti->period = sc->sc_imess[3];
   1424  1.1   briggs 				ti->offset = sc->sc_imess[4];
   1425  1.1   briggs 				ti->flags &= ~T_NEGOTIATE;
   1426  1.1   briggs 				if (sc->sc_minsync == 0 ||
   1427  1.1   briggs 				    ti->offset == 0 ||
   1428  1.1   briggs 				    ti->period > 124) {
   1429  1.1   briggs 					printf("%s:%d: async\n", "esp",
   1430  1.1   briggs 						ecb->xs->sc_link->target);
   1431  1.1   briggs 					if ((sc->sc_flags&ESP_SYNCHNEGO) == 0) {
   1432  1.1   briggs 						/* target initiated negotiation */
   1433  1.1   briggs 						ti->offset = 0;
   1434  1.1   briggs 						ti->flags &= ~T_SYNCMODE;
   1435  1.1   briggs 						esp_sched_msgout(SEND_SDTR);
   1436  1.1   briggs 					} else {
   1437  1.1   briggs 						/* we are async */
   1438  1.1   briggs 						ti->flags &= ~T_SYNCMODE;
   1439  1.1   briggs 					}
   1440  1.1   briggs 				} else {
   1441  1.1   briggs 					int r = 250/ti->period;
   1442  1.1   briggs 					int s = (100*250)/ti->period - 100*r;
   1443  1.1   briggs 					int p;
   1444  1.1   briggs 
   1445  1.1   briggs 					p =  esp_stp2cpb(sc, ti->period);
   1446  1.1   briggs 					ti->period = esp_cpb2stp(sc, p);
   1447  1.1   briggs #ifdef ESP_DEBUG
   1448  1.1   briggs 					sc_print_addr(ecb->xs->sc_link);
   1449  1.1   briggs 					printf("max sync rate %d.%02dMb/s\n",
   1450  1.1   briggs 						r, s);
   1451  1.1   briggs #endif
   1452  1.1   briggs 					if ((sc->sc_flags&ESP_SYNCHNEGO) == 0) {
   1453  1.1   briggs 						/* target initiated negotiation */
   1454  1.1   briggs 						if (ti->period < sc->sc_minsync)
   1455  1.1   briggs 							ti->period = sc->sc_minsync;
   1456  1.1   briggs 						if (ti->offset > 15)
   1457  1.1   briggs 							ti->offset = 15;
   1458  1.1   briggs 						ti->flags &= ~T_SYNCMODE;
   1459  1.1   briggs 						esp_sched_msgout(SEND_SDTR);
   1460  1.1   briggs 					} else {
   1461  1.1   briggs 						/* we are sync */
   1462  1.1   briggs 						ti->flags |= T_SYNCMODE;
   1463  1.1   briggs 					}
   1464  1.1   briggs 				}
   1465  1.1   briggs 				sc->sc_flags &= ~ESP_SYNCHNEGO;
   1466  1.1   briggs 				esp_setsync(sc, ti);
   1467  1.1   briggs 				break;
   1468  1.1   briggs 
   1469  1.1   briggs 			default:
   1470  1.1   briggs 				printf("%s: unrecognized MESSAGE EXTENDED; sending REJECT\n",
   1471  1.1   briggs 				    sc->sc_dev.dv_xname);
   1472  1.1   briggs 				goto reject;
   1473  1.1   briggs 			}
   1474  1.1   briggs 			break;
   1475  1.1   briggs 
   1476  1.1   briggs 		default:
   1477  1.1   briggs 			ESP_MSGS(("ident "));
   1478  1.1   briggs 			printf("%s: unrecognized MESSAGE; sending REJECT\n",
   1479  1.1   briggs 			    sc->sc_dev.dv_xname);
   1480  1.1   briggs 		reject:
   1481  1.1   briggs 			esp_sched_msgout(SEND_REJECT);
   1482  1.1   briggs 			break;
   1483  1.1   briggs 		}
   1484  1.1   briggs 		break;
   1485  1.1   briggs 
   1486  1.1   briggs 	case ESP_RESELECTED:
   1487  1.1   briggs 		if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
   1488  1.1   briggs 			printf("%s: reselect without IDENTIFY; sending DEVICE RESET\n",
   1489  1.1   briggs 			    sc->sc_dev.dv_xname);
   1490  1.1   briggs 			goto reset;
   1491  1.1   briggs 		}
   1492  1.1   briggs 
   1493  1.1   briggs 		(void) esp_reselect(sc, sc->sc_imess[0]);
   1494  1.1   briggs 		break;
   1495  1.1   briggs 
   1496  1.1   briggs 	default:
   1497  1.1   briggs 		printf("%s: unexpected MESSAGE IN; sending DEVICE RESET\n",
   1498  1.1   briggs 		    sc->sc_dev.dv_xname);
   1499  1.1   briggs 	reset:
   1500  1.1   briggs 		esp_sched_msgout(SEND_DEV_RESET);
   1501  1.1   briggs 		break;
   1502  1.1   briggs 
   1503  1.1   briggs 	abort:
   1504  1.1   briggs 		esp_sched_msgout(SEND_ABORT);
   1505  1.1   briggs 		break;
   1506  1.1   briggs 	}
   1507  1.1   briggs 
   1508  1.1   briggs 	/* Ack last message byte */
   1509  1.1   briggs 	ESPCMD(sc, ESPCMD_MSGOK);
   1510  1.1   briggs 
   1511  1.1   briggs 	/* Done, reset message pointer. */
   1512  1.1   briggs 	sc->sc_flags &= ~ESP_DROP_MSGI;
   1513  1.1   briggs 	sc->sc_imlen = 0;
   1514  1.1   briggs }
   1515  1.1   briggs 
   1516  1.1   briggs 
   1517  1.1   briggs /*
   1518  1.1   briggs  * Send the highest priority, scheduled message
   1519  1.1   briggs  */
   1520  1.1   briggs void
   1521  1.1   briggs esp_msgout(sc)
   1522  1.1   briggs 	register struct esp_softc *sc;
   1523  1.1   briggs {
   1524  1.1   briggs 	struct esp_tinfo *ti;
   1525  1.1   briggs 	struct esp_ecb *ecb;
   1526  1.1   briggs 	size_t size;
   1527  1.1   briggs 
   1528  1.1   briggs 	ESP_TRACE(("[esp_msgout(priq:%x, prevphase:%x)]", sc->sc_msgpriq, sc->sc_prevphase));
   1529  1.1   briggs 
   1530  1.1   briggs 	if (sc->sc_flags & ESP_ATN) {
   1531  1.1   briggs 		if (sc->sc_prevphase != MESSAGE_OUT_PHASE) {
   1532  1.1   briggs 		new:
   1533  1.1   briggs 			ESPCMD(sc, ESPCMD_FLUSH);
   1534  1.1   briggs 			DELAY(1);
   1535  1.1   briggs 			sc->sc_msgoutq = 0;
   1536  1.1   briggs 			sc->sc_omlen = 0;
   1537  1.1   briggs 		}
   1538  1.1   briggs 	} else {
   1539  1.1   briggs 		if (sc->sc_prevphase == MESSAGE_OUT_PHASE) {
   1540  1.1   briggs 			esp_sched_msgout(sc->sc_msgoutq);
   1541  1.1   briggs 			goto new;
   1542  1.1   briggs 		} else {
   1543  1.1   briggs 			printf("esp at line %d: unexpected MESSAGE OUT phase\n", __LINE__);
   1544  1.1   briggs 		}
   1545  1.1   briggs 	}
   1546  1.1   briggs 
   1547  1.1   briggs 	if (sc->sc_omlen == 0) {
   1548  1.1   briggs 		/* Pick up highest priority message */
   1549  1.1   briggs 		sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
   1550  1.1   briggs 		sc->sc_msgoutq |= sc->sc_msgout;
   1551  1.1   briggs 		sc->sc_msgpriq &= ~sc->sc_msgout;
   1552  1.1   briggs 		sc->sc_omlen = 1;		/* "Default" message len */
   1553  1.1   briggs 		switch (sc->sc_msgout) {
   1554  1.1   briggs 		case SEND_SDTR:
   1555  1.1   briggs 			ecb = sc->sc_nexus;
   1556  1.1   briggs 			ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
   1557  1.1   briggs 			sc->sc_omess[0] = MSG_EXTENDED;
   1558  1.1   briggs 			sc->sc_omess[1] = 3;
   1559  1.1   briggs 			sc->sc_omess[2] = MSG_EXT_SDTR;
   1560  1.1   briggs 			sc->sc_omess[3] = ti->period;
   1561  1.1   briggs 			sc->sc_omess[4] = ti->offset;
   1562  1.1   briggs 			sc->sc_omlen = 5;
   1563  1.1   briggs 			if ((sc->sc_flags & ESP_SYNCHNEGO) == 0) {
   1564  1.1   briggs 				ti->flags |= T_SYNCMODE;
   1565  1.1   briggs 				esp_setsync(sc, ti);
   1566  1.1   briggs 			}
   1567  1.1   briggs 			break;
   1568  1.1   briggs 		case SEND_IDENTIFY:
   1569  1.1   briggs 			if (sc->sc_state != ESP_CONNECTED) {
   1570  1.1   briggs 				printf("esp at line %d: no nexus\n", __LINE__);
   1571  1.1   briggs 			}
   1572  1.1   briggs 			ecb = sc->sc_nexus;
   1573  1.1   briggs 			sc->sc_omess[0] = MSG_IDENTIFY(ecb->xs->sc_link->lun,0);
   1574  1.1   briggs 			break;
   1575  1.1   briggs 		case SEND_DEV_RESET:
   1576  1.1   briggs 			sc->sc_flags |= ESP_ABORTING;
   1577  1.1   briggs 			sc->sc_omess[0] = MSG_BUS_DEV_RESET;
   1578  1.1   briggs 			ecb = sc->sc_nexus;
   1579  1.1   briggs 			ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
   1580  1.1   briggs 			ti->flags &= ~T_SYNCMODE;
   1581  1.1   briggs 			ti->flags |= T_NEGOTIATE;
   1582  1.1   briggs 			break;
   1583  1.1   briggs 		case SEND_PARITY_ERROR:
   1584  1.1   briggs 			sc->sc_omess[0] = MSG_PARITY_ERROR;
   1585  1.1   briggs 			break;
   1586  1.1   briggs 		case SEND_ABORT:
   1587  1.1   briggs 			sc->sc_flags |= ESP_ABORTING;
   1588  1.1   briggs 			sc->sc_omess[0] = MSG_ABORT;
   1589  1.1   briggs 			break;
   1590  1.1   briggs 		case SEND_INIT_DET_ERR:
   1591  1.1   briggs 			sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
   1592  1.1   briggs 			break;
   1593  1.1   briggs 		case SEND_REJECT:
   1594  1.1   briggs 			sc->sc_omess[0] = MSG_MESSAGE_REJECT;
   1595  1.1   briggs 			break;
   1596  1.1   briggs 		default:
   1597  1.1   briggs 			ESPCMD(sc, ESPCMD_RSTATN);
   1598  1.1   briggs 			sc->sc_flags &= ~ESP_ATN;
   1599  1.1   briggs 			sc->sc_omess[0] = MSG_NOOP;
   1600  1.1   briggs 			break;
   1601  1.1   briggs 		}
   1602  1.1   briggs 		sc->sc_omp = sc->sc_omess;
   1603  1.1   briggs 	}
   1604  1.1   briggs 
   1605  1.1   briggs #if 1
   1606  1.1   briggs 	/* (re)send the message */
   1607  1.1   briggs 	size = min(sc->sc_omlen, sc->sc_maxxfer);
   1608  1.1   briggs 	DMA_SETUP(sc->sc_dma, &sc->sc_omp, &sc->sc_omlen, 0, &size);
   1609  1.3   briggs #ifndef MAC68K_DRIVER
   1610  1.1   briggs 	/* Program the SCSI counter */
   1611  1.1   briggs 	ESP_WRITE_REG(sc, ESP_TCL, size);
   1612  1.1   briggs 	ESP_WRITE_REG(sc, ESP_TCM, size >> 8);
   1613  1.1   briggs 	if (sc->sc_cfg2 & ESPCFG2_FE) {
   1614  1.1   briggs 		ESP_WRITE_REG(sc, ESP_TCH, size >> 16);
   1615  1.1   briggs 	}
   1616  1.1   briggs 	/* load the count in */
   1617  1.1   briggs 	ESPCMD(sc, ESPCMD_NOP|ESPCMD_DMA);
   1618  1.3   briggs #endif
   1619  1.1   briggs 	ESPCMD(sc, ESPCMD_TRANS|ESPCMD_DMA);
   1620  1.1   briggs 	DMA_GO(sc->sc_dma);
   1621  1.1   briggs #else
   1622  1.1   briggs 	{	int i;
   1623  1.1   briggs 		for (i = 0; i < sc->sc_omlen; i++)
   1624  1.1   briggs 			ESP_WRITE_REG(sc, FIFO, sc->sc_omess[i]);
   1625  1.1   briggs 		ESPCMD(sc, ESPCMD_TRANS);
   1626  1.1   briggs 		sc->sc_omlen = 0;
   1627  1.1   briggs 	}
   1628  1.1   briggs #endif
   1629  1.1   briggs }
   1630  1.1   briggs 
   1631  1.1   briggs /*
   1632  1.1   briggs  * This is the most critical part of the driver, and has to know
   1633  1.1   briggs  * how to deal with *all* error conditions and phases from the SCSI
   1634  1.1   briggs  * bus. If there are no errors and the DMA was active, then call the
   1635  1.1   briggs  * DMA pseudo-interrupt handler. If this returns 1, then that was it
   1636  1.1   briggs  * and we can return from here without further processing.
   1637  1.1   briggs  *
   1638  1.1   briggs  * Most of this needs verifying.
   1639  1.1   briggs  */
   1640  1.1   briggs int
   1641  1.1   briggs espintr(sc)
   1642  1.1   briggs 	register struct esp_softc *sc;
   1643  1.1   briggs {
   1644  1.1   briggs 	register struct esp_ecb *ecb;
   1645  1.1   briggs 	register struct scsi_link *sc_link;
   1646  1.1   briggs 	struct esp_tinfo *ti;
   1647  1.1   briggs 	int loop;
   1648  1.1   briggs 	size_t size;
   1649  1.1   briggs 
   1650  1.1   briggs 	ESP_TRACE(("[espintr]"));
   1651  1.1   briggs 
   1652  1.1   briggs 	/*
   1653  1.1   briggs 	 * I have made some (maybe seriously flawed) assumptions here,
   1654  1.1   briggs 	 * but basic testing (uncomment the printf() below), show that
   1655  1.1   briggs 	 * certainly something happens when this loop is here.
   1656  1.1   briggs 	 *
   1657  1.1   briggs 	 * The idea is that many of the SCSI operations take very little
   1658  1.1   briggs 	 * time, and going away and getting interrupted is too high an
   1659  1.1   briggs 	 * overhead to pay. For example, selecting, sending a message
   1660  1.1   briggs 	 * and command and then doing some work can be done in one "pass".
   1661  1.1   briggs 	 *
   1662  1.1   briggs 	 * The DELAY is not variable because I do not understand that the
   1663  1.1   briggs 	 * DELAY loop should be fixed-time regardless of CPU speed, but
   1664  1.1   briggs 	 * I am *assuming* that the faster SCSI processors get things done
   1665  1.1   briggs 	 * quicker (sending a command byte etc), and so there is no
   1666  1.1   briggs 	 * need to be too slow.
   1667  1.1   briggs 	 *
   1668  1.1   briggs 	 * This is a heuristic. It is 2 when at 20Mhz, 2 at 25Mhz and 1
   1669  1.1   briggs 	 * at 40Mhz. This needs testing.
   1670  1.1   briggs 	 */
   1671  1.1   briggs 	for (loop = 0; 1;loop++, DELAY(50/sc->sc_freq)) {
   1672  1.1   briggs 		/* a feeling of deja-vu */
   1673  1.1   briggs 		if (!DMA_ISINTR(sc->sc_dma))
   1674  1.1   briggs 			return (loop != 0);
   1675  1.1   briggs #if 0
   1676  1.1   briggs 		if (loop)
   1677  1.1   briggs 			printf("*");
   1678  1.1   briggs #endif
   1679  1.1   briggs 
   1680  1.1   briggs 		/* and what do the registers say... */
   1681  1.1   briggs 		espreadregs(sc);
   1682  1.1   briggs 
   1683  1.4   briggs errintr:
   1684  1.1   briggs 		sc->sc_intrcnt.ev_count++;
   1685  1.1   briggs 
   1686  1.1   briggs 		/*
   1687  1.1   briggs 		 * At the moment, only a SCSI Bus Reset or Illegal
   1688  1.1   briggs 		 * Command are classed as errors. A disconnect is a
   1689  1.1   briggs 		 * valid condition, and we let the code check is the
   1690  1.1   briggs 		 * "ESP_BUSFREE_OK" flag was set before declaring it
   1691  1.1   briggs 		 * and error.
   1692  1.1   briggs 		 *
   1693  1.1   briggs 		 * Also, the status register tells us about "Gross
   1694  1.1   briggs 		 * Errors" and "Parity errors". Only the Gross Error
   1695  1.1   briggs 		 * is really bad, and the parity errors are dealt
   1696  1.1   briggs 		 * with later
   1697  1.1   briggs 		 *
   1698  1.1   briggs 		 * TODO
   1699  1.1   briggs 		 *	If there are too many parity error, go to slow
   1700  1.1   briggs 		 *	cable mode ?
   1701  1.1   briggs 		 */
   1702  1.1   briggs 
   1703  1.1   briggs 		/* SCSI Reset */
   1704  1.1   briggs 		if (sc->sc_espintr & ESPINTR_SBR) {
   1705  1.1   briggs 			if (ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) {
   1706  1.1   briggs 				ESPCMD(sc, ESPCMD_FLUSH);
   1707  1.1   briggs 				DELAY(1);
   1708  1.1   briggs 			}
   1709  1.1   briggs 			if (sc->sc_state != ESP_SBR) {
   1710  1.1   briggs 				printf("%s: SCSI bus reset\n",
   1711  1.1   briggs 					sc->sc_dev.dv_xname);
   1712  1.1   briggs 				esp_init(sc, 0); /* Restart everything */
   1713  1.1   briggs 				return 1;
   1714  1.1   briggs 			}
   1715  1.1   briggs #if 0
   1716  1.1   briggs 	/*XXX*/		printf("<expected bus reset: "
   1717  1.1   briggs 				"[intr %x, stat %x, step %d]>\n",
   1718  1.1   briggs 				sc->sc_espintr, sc->sc_espstat,
   1719  1.1   briggs 				sc->sc_espstep);
   1720  1.1   briggs #endif
   1721  1.1   briggs 			if (sc->sc_nexus)
   1722  1.1   briggs 				panic("%s: nexus in reset state",
   1723  1.1   briggs 				      sc->sc_dev.dv_xname);
   1724  1.1   briggs 			goto sched;
   1725  1.1   briggs 		}
   1726  1.1   briggs 
   1727  1.1   briggs 		ecb = sc->sc_nexus;
   1728  1.1   briggs 
   1729  1.1   briggs #define ESPINTR_ERR (ESPINTR_SBR|ESPINTR_ILL)
   1730  1.1   briggs 		if (sc->sc_espintr & ESPINTR_ERR ||
   1731  1.1   briggs 		    sc->sc_espstat & ESPSTAT_GE) {
   1732  1.1   briggs 
   1733  1.1   briggs 			if (sc->sc_espstat & ESPSTAT_GE) {
   1734  1.1   briggs 				/* no target ? */
   1735  1.1   briggs 				if (ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) {
   1736  1.1   briggs 					ESPCMD(sc, ESPCMD_FLUSH);
   1737  1.1   briggs 					DELAY(1);
   1738  1.1   briggs 				}
   1739  1.1   briggs 				if (sc->sc_state == ESP_CONNECTED ||
   1740  1.1   briggs 				    sc->sc_state == ESP_SELECTING) {
   1741  1.1   briggs 					ecb->xs->error = XS_DRIVER_STUFFUP;
   1742  1.1   briggs 					esp_done(sc, ecb);
   1743  1.1   briggs 				}
   1744  1.1   briggs 				return 1;
   1745  1.1   briggs 			}
   1746  1.1   briggs 
   1747  1.1   briggs 			if (sc->sc_espintr & ESPINTR_ILL) {
   1748  1.1   briggs 				/* illegal command, out of sync ? */
   1749  1.1   briggs 				printf("%s: illegal command: 0x%x (state %d, phase %x, prevphase %x)\n",
   1750  1.1   briggs 					sc->sc_dev.dv_xname, sc->sc_lastcmd,
   1751  1.1   briggs 					sc->sc_state, sc->sc_phase,
   1752  1.1   briggs 					sc->sc_prevphase);
   1753  1.1   briggs 				if (ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) {
   1754  1.1   briggs 					ESPCMD(sc, ESPCMD_FLUSH);
   1755  1.1   briggs 					DELAY(1);
   1756  1.1   briggs 				}
   1757  1.1   briggs 				esp_init(sc, 0); /* Restart everything */
   1758  1.1   briggs 				return 1;
   1759  1.1   briggs 			}
   1760  1.1   briggs 		}
   1761  1.1   briggs 
   1762  1.1   briggs 		/*
   1763  1.1   briggs 		 * Call if DMA is active.
   1764  1.1   briggs 		 *
   1765  1.1   briggs 		 * If DMA_INTR returns true, then maybe go 'round the loop
   1766  1.1   briggs 		 * again in case there is no more DMA queued, but a phase
   1767  1.1   briggs 		 * change is expected.
   1768  1.1   briggs 		 */
   1769  1.1   briggs 		if (DMA_ISACTIVE(sc->sc_dma)) {
   1770  1.1   briggs 			DMA_INTR(sc->sc_dma);
   1771  1.1   briggs 			/* If DMA active here, then go back to work... */
   1772  1.4   briggs 			if (   (sc->sc_espstat & ESPSTAT_GE)
   1773  1.4   briggs 			    || (sc->sc_espintr & ESPINTR_ERR))
   1774  1.4   briggs 				goto errintr;
   1775  1.1   briggs 			if (DMA_ISACTIVE(sc->sc_dma))
   1776  1.1   briggs 				return 1;
   1777  1.1   briggs 
   1778  1.1   briggs 			if (sc->sc_dleft == 0 &&
   1779  1.1   briggs 			    (sc->sc_espstat & ESPSTAT_TC) == 0)
   1780  1.1   briggs 				printf("%s: !TC [intr %x, stat %x, step %d]"
   1781  1.1   briggs 				       " prevphase %x, resid %x\n",
   1782  1.1   briggs 					sc->sc_dev.dv_xname,
   1783  1.1   briggs 					sc->sc_espintr,
   1784  1.1   briggs 					sc->sc_espstat,
   1785  1.1   briggs 					sc->sc_espstep,
   1786  1.1   briggs 					sc->sc_prevphase,
   1787  1.1   briggs 					ecb?ecb->dleft:-1);
   1788  1.1   briggs 		}
   1789  1.1   briggs 
   1790  1.1   briggs #if 0	/* Unreliable on some ESP revisions? */
   1791  1.1   briggs 		if ((sc->sc_espstat & ESPSTAT_INT) == 0) {
   1792  1.1   briggs 			printf("%s: spurious interrupt\n", sc->sc_dev.dv_xname);
   1793  1.1   briggs 			return 1;
   1794  1.1   briggs 		}
   1795  1.1   briggs #endif
   1796  1.1   briggs 
   1797  1.1   briggs 		/*
   1798  1.1   briggs 		 * check for less serious errors
   1799  1.1   briggs 		 */
   1800  1.1   briggs 		if (sc->sc_espstat & ESPSTAT_PE) {
   1801  1.1   briggs 			printf("%s: SCSI bus parity error\n",
   1802  1.1   briggs 				sc->sc_dev.dv_xname);
   1803  1.1   briggs 			if (sc->sc_prevphase == MESSAGE_IN_PHASE)
   1804  1.1   briggs 				esp_sched_msgout(SEND_PARITY_ERROR);
   1805  1.1   briggs 			else
   1806  1.1   briggs 				esp_sched_msgout(SEND_INIT_DET_ERR);
   1807  1.1   briggs 		}
   1808  1.1   briggs 
   1809  1.1   briggs 		if (sc->sc_espintr & ESPINTR_DIS) {
   1810  1.1   briggs 			ESP_MISC(("<DISC [intr %x, stat %x, step %d]>",
   1811  1.1   briggs 				sc->sc_espintr,sc->sc_espstat,sc->sc_espstep));
   1812  1.1   briggs 			if (ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) {
   1813  1.1   briggs 				ESPCMD(sc, ESPCMD_FLUSH);
   1814  1.1   briggs 				DELAY(1);
   1815  1.1   briggs 			}
   1816  1.1   briggs 			/*
   1817  1.1   briggs 			 * This command must (apparently) be issued within
   1818  1.1   briggs 			 * 250mS of a disconnect. So here you are...
   1819  1.1   briggs 			 */
   1820  1.1   briggs 			ESPCMD(sc, ESPCMD_ENSEL);
   1821  1.1   briggs 			switch (sc->sc_state) {
   1822  1.1   briggs 			case ESP_RESELECTED:
   1823  1.1   briggs 				goto sched;
   1824  1.1   briggs 
   1825  1.1   briggs 			case ESP_SELECTING:
   1826  1.1   briggs 				ecb->xs->error = XS_SELTIMEOUT;
   1827  1.1   briggs 				goto finish;
   1828  1.1   briggs 
   1829  1.1   briggs 			case ESP_CONNECTED:
   1830  1.1   briggs 				if ((sc->sc_flags & ESP_SYNCHNEGO)) {
   1831  1.1   briggs #ifdef ESP_DEBUG
   1832  1.1   briggs 					if (ecb)
   1833  1.1   briggs 						sc_print_addr(ecb->xs->sc_link);
   1834  1.1   briggs 					printf("sync nego not completed!\n");
   1835  1.1   briggs #endif
   1836  1.1   briggs 					ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
   1837  1.1   briggs 					sc->sc_flags &= ~ESP_SYNCHNEGO;
   1838  1.1   briggs 					ti->flags &= ~(T_NEGOTIATE | T_SYNCMODE);
   1839  1.1   briggs 				}
   1840  1.1   briggs 
   1841  1.1   briggs 				/* it may be OK to disconnect */
   1842  1.1   briggs 				if ((sc->sc_flags & ESP_ABORTING) == 0) {
   1843  1.1   briggs 					/*
   1844  1.1   briggs 					 * Section 5.1.1 of the SCSI 2 spec
   1845  1.1   briggs 					 * suggests issuing a REQUEST SENSE
   1846  1.1   briggs 					 * following an unexpected disconnect.
   1847  1.1   briggs 					 * Some devices go into a contingent
   1848  1.1   briggs 					 * allegiance condition when
   1849  1.1   briggs 					 * disconnecting, and this is necessary
   1850  1.1   briggs 					 * to clean up their state.
   1851  1.1   briggs 					 */
   1852  1.1   briggs 					printf("%s: unexpected disconnect; ",
   1853  1.1   briggs 					    sc->sc_dev.dv_xname);
   1854  1.1   briggs 					if (ecb->flags & ECB_SENSE) {
   1855  1.1   briggs 						printf("resetting\n");
   1856  1.1   briggs 						goto reset;
   1857  1.1   briggs 					}
   1858  1.1   briggs 					printf("sending REQUEST SENSE\n");
   1859  1.1   briggs 					esp_sense(sc, ecb);
   1860  1.1   briggs 					goto out;
   1861  1.1   briggs 				}
   1862  1.1   briggs 
   1863  1.1   briggs 				ecb->xs->error = XS_DRIVER_STUFFUP;
   1864  1.1   briggs 				goto finish;
   1865  1.1   briggs 
   1866  1.1   briggs 			case ESP_DISCONNECT:
   1867  1.1   briggs 				TAILQ_INSERT_HEAD(&sc->nexus_list, ecb, chain);
   1868  1.1   briggs 				sc->sc_nexus = NULL;
   1869  1.1   briggs 				goto sched;
   1870  1.1   briggs 
   1871  1.1   briggs 			case ESP_CMDCOMPLETE:
   1872  1.1   briggs 				goto finish;
   1873  1.1   briggs 			}
   1874  1.1   briggs 		}
   1875  1.1   briggs 
   1876  1.1   briggs 		switch (sc->sc_state) {
   1877  1.1   briggs 
   1878  1.1   briggs 		case ESP_SBR:
   1879  1.1   briggs 			printf("%s: waiting for SCSI Bus Reset to happen\n",
   1880  1.1   briggs 				sc->sc_dev.dv_xname);
   1881  1.1   briggs 			return 1;
   1882  1.1   briggs 
   1883  1.1   briggs 		case ESP_RESELECTED:
   1884  1.1   briggs 			/*
   1885  1.1   briggs 			 * we must be continuing a message ?
   1886  1.1   briggs 			 */
   1887  1.1   briggs 			if (sc->sc_phase != MESSAGE_IN_PHASE) {
   1888  1.1   briggs 				printf("%s: target didn't identify\n",
   1889  1.1   briggs 					sc->sc_dev.dv_xname);
   1890  1.1   briggs 				esp_init(sc, 1);
   1891  1.1   briggs 				return 1;
   1892  1.1   briggs 			}
   1893  1.1   briggs printf("<<RESELECT CONT'd>>");
   1894  1.1   briggs #if XXXX
   1895  1.1   briggs 			esp_msgin(sc);
   1896  1.1   briggs 			if (sc->sc_state != ESP_CONNECTED) {
   1897  1.1   briggs 				/* IDENTIFY fail?! */
   1898  1.1   briggs 				printf("%s: identify failed\n",
   1899  1.1   briggs 					sc->sc_dev.dv_xname);
   1900  1.1   briggs 				esp_init(sc, 1);
   1901  1.1   briggs 				return 1;
   1902  1.1   briggs 			}
   1903  1.1   briggs #endif
   1904  1.1   briggs 			break;
   1905  1.1   briggs 
   1906  1.1   briggs 		case ESP_IDLE:
   1907  1.1   briggs if (sc->sc_flags & ESP_ICCS) printf("[[esp: BUMMER]]");
   1908  1.1   briggs 		case ESP_SELECTING:
   1909  1.1   briggs 			sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
   1910  1.1   briggs 			sc->sc_flags = 0;
   1911  1.1   briggs 
   1912  1.1   briggs 			if (sc->sc_espintr & ESPINTR_RESEL) {
   1913  1.1   briggs 				/*
   1914  1.1   briggs 				 * If we're trying to select a
   1915  1.1   briggs 				 * target ourselves, push our command
   1916  1.1   briggs 				 * back into the ready list.
   1917  1.1   briggs 				 */
   1918  1.1   briggs 				if (sc->sc_state == ESP_SELECTING) {
   1919  1.1   briggs 					ESP_MISC(("backoff selector "));
   1920  1.1   briggs 					sc_link = sc->sc_nexus->xs->sc_link;
   1921  1.1   briggs 					ti = &sc->sc_tinfo[sc_link->target];
   1922  1.1   briggs 					TAILQ_INSERT_HEAD(&sc->ready_list,
   1923  1.1   briggs 					    sc->sc_nexus, chain);
   1924  1.1   briggs 					ecb = sc->sc_nexus = NULL;
   1925  1.1   briggs 				}
   1926  1.1   briggs 				sc->sc_state = ESP_RESELECTED;
   1927  1.1   briggs 				if (sc->sc_phase != MESSAGE_IN_PHASE) {
   1928  1.1   briggs 					/*
   1929  1.1   briggs 					 * Things are seriously fucked up.
   1930  1.1   briggs 					 * Pull the brakes, i.e. reset
   1931  1.1   briggs 					 */
   1932  1.1   briggs 					printf("%s: target didn't identify\n",
   1933  1.1   briggs 						sc->sc_dev.dv_xname);
   1934  1.1   briggs 					esp_init(sc, 1);
   1935  1.1   briggs 					return 1;
   1936  1.1   briggs 				}
   1937  1.1   briggs 				if ((ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) != 2) {
   1938  1.1   briggs 					printf("%s: RESELECT: %d bytes in FIFO!\n",
   1939  1.1   briggs 						sc->sc_dev.dv_xname,
   1940  1.1   briggs 						ESP_READ_REG(sc, ESP_FFLAG) &
   1941  1.1   briggs 						ESPFIFO_FF);
   1942  1.1   briggs 					esp_init(sc, 1);
   1943  1.1   briggs 					return 1;
   1944  1.1   briggs 				}
   1945  1.1   briggs 				sc->sc_selid = ESP_READ_REG(sc, ESP_FIFO);
   1946  1.1   briggs 				ESP_MISC(("selid=0x%2x ", sc->sc_selid));
   1947  1.1   briggs 				esp_msgin(sc);	/* Handle identify message */
   1948  1.1   briggs 				if (sc->sc_state != ESP_CONNECTED) {
   1949  1.1   briggs 					/* IDENTIFY fail?! */
   1950  1.1   briggs 					printf("%s: identify failed\n",
   1951  1.1   briggs 						sc->sc_dev.dv_xname);
   1952  1.1   briggs 					esp_init(sc, 1);
   1953  1.1   briggs 					return 1;
   1954  1.1   briggs 				}
   1955  1.1   briggs 				continue; /* ie. next phase expected soon */
   1956  1.1   briggs 			}
   1957  1.1   briggs 
   1958  1.1   briggs #define	ESPINTR_DONE	(ESPINTR_FC|ESPINTR_BS)
   1959  1.1   briggs 			if ((sc->sc_espintr & ESPINTR_DONE) == ESPINTR_DONE) {
   1960  1.1   briggs 				ecb = sc->sc_nexus;
   1961  1.1   briggs 				if (!ecb)
   1962  1.1   briggs 					panic("esp: not nexus at sc->sc_nexus");
   1963  1.1   briggs 
   1964  1.1   briggs 				sc_link = ecb->xs->sc_link;
   1965  1.1   briggs 				ti = &sc->sc_tinfo[sc_link->target];
   1966  1.1   briggs 
   1967  1.1   briggs 				switch (sc->sc_espstep) {
   1968  1.1   briggs 				case 0:
   1969  1.1   briggs 					printf("%s: select timeout/no disconnect\n",
   1970  1.1   briggs 						sc->sc_dev.dv_xname);
   1971  1.1   briggs 					ecb->xs->error = XS_SELTIMEOUT;
   1972  1.1   briggs 					goto finish;
   1973  1.1   briggs 				case 1:
   1974  1.1   briggs 					if ((ti->flags & T_NEGOTIATE) == 0) {
   1975  1.1   briggs 						printf("%s: step 1 & !NEG\n",
   1976  1.1   briggs 							sc->sc_dev.dv_xname);
   1977  1.1   briggs 						goto reset;
   1978  1.1   briggs 					}
   1979  1.1   briggs 					if (sc->sc_phase != MESSAGE_OUT_PHASE) {
   1980  1.1   briggs 						printf("%s: !MSGOUT\n",
   1981  1.1   briggs 							sc->sc_dev.dv_xname);
   1982  1.1   briggs 						goto reset;
   1983  1.1   briggs 					}
   1984  1.1   briggs 					/* Start negotiating */
   1985  1.1   briggs 					ti->period = sc->sc_minsync;
   1986  1.1   briggs 					ti->offset = 15;
   1987  1.1   briggs 					sc->sc_flags |= ESP_SYNCHNEGO;
   1988  1.1   briggs 					esp_sched_msgout(SEND_SDTR);
   1989  1.1   briggs 					break;
   1990  1.1   briggs 				case 3:
   1991  1.1   briggs 					/*
   1992  1.1   briggs 					 * Grr, this is supposed to mean
   1993  1.1   briggs 					 * "target left command phase
   1994  1.1   briggs 					 *  prematurely". It seems to happen
   1995  1.1   briggs 					 * regularly when sync mode is on.
   1996  1.1   briggs 					 * Look at FIFO to see if command
   1997  1.1   briggs 					 * went out.
   1998  1.1   briggs 					 * (Timing problems?)
   1999  1.1   briggs 					 */
   2000  1.1   briggs 					if ((ESP_READ_REG(sc, ESP_FFLAG)&ESPFIFO_FF) == 0) {
   2001  1.1   briggs 						/* Hope for the best.. */
   2002  1.1   briggs 						break;
   2003  1.1   briggs 					}
   2004  1.1   briggs 					printf("(%s:%d:%d): selection failed;"
   2005  1.1   briggs 						" %d left in FIFO "
   2006  1.1   briggs 						"[intr %x, stat %x, step %d]\n",
   2007  1.1   briggs 						sc->sc_dev.dv_xname,
   2008  1.1   briggs 						sc_link->target,
   2009  1.1   briggs 						sc_link->lun,
   2010  1.1   briggs 						ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF,
   2011  1.1   briggs 						sc->sc_espintr, sc->sc_espstat,
   2012  1.1   briggs 						sc->sc_espstep);
   2013  1.1   briggs 					ESPCMD(sc, ESPCMD_FLUSH);
   2014  1.1   briggs 					esp_sched_msgout(SEND_ABORT);
   2015  1.1   briggs 					return 1;
   2016  1.1   briggs 				case 2:
   2017  1.1   briggs 					/* Select stuck at Command Phase */
   2018  1.1   briggs 					ESPCMD(sc, ESPCMD_FLUSH);
   2019  1.1   briggs 				case 4:
   2020  1.1   briggs 					/* So far, everything went fine */
   2021  1.1   briggs 					break;
   2022  1.1   briggs 				}
   2023  1.1   briggs #if 0
   2024  1.1   briggs 				if (ecb->xs->flags & SCSI_RESET)
   2025  1.1   briggs 					esp_sched_msgout(SEND_DEV_RESET);
   2026  1.1   briggs 				else if (ti->flags & T_NEGOTIATE)
   2027  1.1   briggs 					esp_sched_msgout(
   2028  1.1   briggs 					    SEND_IDENTIFY | SEND_SDTR);
   2029  1.1   briggs 				else
   2030  1.1   briggs 					esp_sched_msgout(SEND_IDENTIFY);
   2031  1.1   briggs #endif
   2032  1.1   briggs 
   2033  1.1   briggs 				ecb->flags |= ECB_NEXUS;
   2034  1.1   briggs 				ti->lubusy |= (1 << sc_link->lun);
   2035  1.1   briggs 
   2036  1.1   briggs 				sc->sc_prevphase = INVALID_PHASE; /* ?? */
   2037  1.1   briggs 				/* Do an implicit RESTORE POINTERS. */
   2038  1.1   briggs 				sc->sc_dp = ecb->daddr;
   2039  1.1   briggs 				sc->sc_dleft = ecb->dleft;
   2040  1.1   briggs 
   2041  1.1   briggs 				/* On our first connection, schedule a timeout. */
   2042  1.1   briggs 				if ((ecb->xs->flags & SCSI_POLL) == 0)
   2043  1.1   briggs 					timeout(esp_timeout, ecb, (ecb->timeout * hz) / 1000);
   2044  1.1   briggs 
   2045  1.1   briggs 				sc->sc_state = ESP_CONNECTED;
   2046  1.1   briggs 				break;
   2047  1.1   briggs 			} else {
   2048  1.1   briggs 				printf("%s: unexpected status after select"
   2049  1.1   briggs 					": [intr %x, stat %x, step %x]\n",
   2050  1.1   briggs 					sc->sc_dev.dv_xname,
   2051  1.1   briggs 					sc->sc_espintr, sc->sc_espstat,
   2052  1.1   briggs 					sc->sc_espstep);
   2053  1.1   briggs 				ESPCMD(sc, ESPCMD_FLUSH);
   2054  1.1   briggs 				DELAY(1);
   2055  1.1   briggs 				goto reset;
   2056  1.1   briggs 			}
   2057  1.1   briggs 			if (sc->sc_state == ESP_IDLE) {
   2058  1.1   briggs 				printf("%s: stray interrupt\n", sc->sc_dev.dv_xname);
   2059  1.1   briggs 					return 0;
   2060  1.1   briggs 			}
   2061  1.1   briggs 			break;
   2062  1.1   briggs 
   2063  1.1   briggs 		case ESP_CONNECTED:
   2064  1.1   briggs 			if (sc->sc_flags & ESP_ICCS) {
   2065  1.1   briggs 				u_char msg;
   2066  1.1   briggs 
   2067  1.1   briggs 				sc->sc_flags &= ~ESP_ICCS;
   2068  1.1   briggs 
   2069  1.1   briggs 				if (!(sc->sc_espintr & ESPINTR_DONE)) {
   2070  1.1   briggs 					printf("%s: ICCS: "
   2071  1.1   briggs 					      ": [intr %x, stat %x, step %x]\n",
   2072  1.1   briggs 						sc->sc_dev.dv_xname,
   2073  1.1   briggs 						sc->sc_espintr, sc->sc_espstat,
   2074  1.1   briggs 						sc->sc_espstep);
   2075  1.1   briggs 				}
   2076  1.1   briggs 				if ((ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) != 2) {
   2077  1.1   briggs 					int i = (ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) - 2;
   2078  1.1   briggs 					while (i--)
   2079  1.1   briggs 						(void) ESP_READ_REG(sc, ESP_FIFO);
   2080  1.1   briggs 				}
   2081  1.1   briggs 				ecb->stat = ESP_READ_REG(sc, ESP_FIFO);
   2082  1.1   briggs 				msg = ESP_READ_REG(sc, ESP_FIFO);
   2083  1.1   briggs 				ESP_PHASE(("<stat:(%x,%x)>", ecb->stat, msg));
   2084  1.1   briggs 				if (msg == MSG_CMDCOMPLETE) {
   2085  1.1   briggs 					ecb->xs->resid = ecb->dleft = sc->sc_dleft;
   2086  1.1   briggs 					sc->sc_state = ESP_CMDCOMPLETE;
   2087  1.1   briggs 				} else
   2088  1.1   briggs 					printf("%s: STATUS_PHASE: msg %d\n",
   2089  1.1   briggs 						sc->sc_dev.dv_xname, msg);
   2090  1.1   briggs 				ESPCMD(sc, ESPCMD_MSGOK);
   2091  1.1   briggs 				continue; /* ie. wait for disconnect */
   2092  1.1   briggs 			}
   2093  1.1   briggs 			break;
   2094  1.1   briggs 		default:
   2095  1.1   briggs 			panic("%s: invalid state: %d",
   2096  1.1   briggs 			      sc->sc_dev.dv_xname,
   2097  1.1   briggs 			      sc->sc_state);
   2098  1.1   briggs 		}
   2099  1.1   briggs 
   2100  1.1   briggs 		/*
   2101  1.1   briggs 		 * Driver is now in state ESP_CONNECTED, i.e. we
   2102  1.1   briggs 		 * have a current command working the SCSI bus.
   2103  1.1   briggs 		 */
   2104  1.1   briggs 		if (sc->sc_state != ESP_CONNECTED || ecb == NULL) {
   2105  1.1   briggs 			panic("esp no nexus");
   2106  1.1   briggs 		}
   2107  1.1   briggs 
   2108  1.1   briggs 		switch (sc->sc_phase) {
   2109  1.1   briggs 		case MESSAGE_OUT_PHASE:
   2110  1.1   briggs 			ESP_PHASE(("MESSAGE_OUT_PHASE "));
   2111  1.1   briggs 			esp_msgout(sc);
   2112  1.1   briggs 			sc->sc_prevphase = MESSAGE_OUT_PHASE;
   2113  1.1   briggs 			break;
   2114  1.1   briggs 		case MESSAGE_IN_PHASE:
   2115  1.1   briggs 			ESP_PHASE(("MESSAGE_IN_PHASE "));
   2116  1.1   briggs 			if (sc->sc_espintr & ESPINTR_BS) {
   2117  1.1   briggs 				ESPCMD(sc, ESPCMD_FLUSH);
   2118  1.1   briggs 				sc->sc_flags |= ESP_WAITI;
   2119  1.1   briggs 				ESPCMD(sc, ESPCMD_TRANS);
   2120  1.1   briggs 			} else if (sc->sc_espintr & ESPINTR_FC) {
   2121  1.1   briggs 				if ((sc->sc_flags & ESP_WAITI) == 0) {
   2122  1.1   briggs 					printf("%s: MSGIN: unexpected FC bit: "
   2123  1.1   briggs 						"[intr %x, stat %x, step %x]\n",
   2124  1.1   briggs 					sc->sc_dev.dv_xname,
   2125  1.1   briggs 					sc->sc_espintr, sc->sc_espstat,
   2126  1.1   briggs 					sc->sc_espstep);
   2127  1.1   briggs 				}
   2128  1.1   briggs 				sc->sc_flags &= ~ESP_WAITI;
   2129  1.1   briggs 				esp_msgin(sc);
   2130  1.1   briggs 			} else {
   2131  1.1   briggs 				printf("%s: MSGIN: weird bits: "
   2132  1.1   briggs 					"[intr %x, stat %x, step %x]\n",
   2133  1.1   briggs 					sc->sc_dev.dv_xname,
   2134  1.1   briggs 					sc->sc_espintr, sc->sc_espstat,
   2135  1.1   briggs 					sc->sc_espstep);
   2136  1.1   briggs 			}
   2137  1.1   briggs 			sc->sc_prevphase = MESSAGE_IN_PHASE;
   2138  1.1   briggs 			break;
   2139  1.1   briggs 		case COMMAND_PHASE: {
   2140  1.1   briggs 			/* well, this means send the command again */
   2141  1.1   briggs 			u_char *cmd = (u_char *)&ecb->cmd;
   2142  1.1   briggs 			int i;
   2143  1.1   briggs 
   2144  1.1   briggs 			ESP_PHASE(("COMMAND_PHASE 0x%02x (%d) ",
   2145  1.1   briggs 				ecb->cmd.opcode, ecb->clen));
   2146  1.1   briggs 			if (ESP_READ_REG(sc, ESP_FFLAG) & ESPFIFO_FF) {
   2147  1.1   briggs 				ESPCMD(sc, ESPCMD_FLUSH);
   2148  1.1   briggs 				DELAY(1);
   2149  1.1   briggs 			}
   2150  1.1   briggs 			/* Now the command into the FIFO */
   2151  1.1   briggs 			for (i = 0; i < ecb->clen; i++)
   2152  1.1   briggs 				ESP_WRITE_REG(sc, ESP_FIFO, *cmd++);
   2153  1.1   briggs 			ESPCMD(sc, ESPCMD_TRANS);
   2154  1.1   briggs 			sc->sc_prevphase = COMMAND_PHASE;
   2155  1.1   briggs 			}
   2156  1.1   briggs 			break;
   2157  1.1   briggs 		case DATA_OUT_PHASE:
   2158  1.1   briggs 			ESP_PHASE(("DATA_OUT_PHASE [%d] ",  sc->sc_dleft));
   2159  1.1   briggs 			ESPCMD(sc, ESPCMD_FLUSH);
   2160  1.1   briggs 			size = min(sc->sc_dleft, sc->sc_maxxfer);
   2161  1.1   briggs 			DMA_SETUP(sc->sc_dma, &sc->sc_dp, &sc->sc_dleft,
   2162  1.1   briggs 				  0, &size);
   2163  1.1   briggs 			sc->sc_prevphase = DATA_OUT_PHASE;
   2164  1.1   briggs 			goto setup_xfer;
   2165  1.1   briggs 		case DATA_IN_PHASE:
   2166  1.1   briggs 			ESP_PHASE(("DATA_IN_PHASE "));
   2167  1.1   briggs 			if (sc->sc_rev == ESP100)
   2168  1.1   briggs 				ESPCMD(sc, ESPCMD_FLUSH);
   2169  1.1   briggs 			size = min(sc->sc_dleft, sc->sc_maxxfer);
   2170  1.1   briggs 			DMA_SETUP(sc->sc_dma, &sc->sc_dp, &sc->sc_dleft,
   2171  1.1   briggs 				  1, &size);
   2172  1.1   briggs 			sc->sc_prevphase = DATA_IN_PHASE;
   2173  1.1   briggs 		setup_xfer:
   2174  1.3   briggs #ifdef MAC68K_DRIVER
   2175  1.3   briggs 			if (!size) {
   2176  1.3   briggs #endif
   2177  1.1   briggs 			/* Program the SCSI counter */
   2178  1.1   briggs 			ESP_WRITE_REG(sc, ESP_TCL, size);
   2179  1.1   briggs 			ESP_WRITE_REG(sc, ESP_TCM, size >> 8);
   2180  1.1   briggs 			if (sc->sc_cfg2 & ESPCFG2_FE) {
   2181  1.1   briggs 				ESP_WRITE_REG(sc, ESP_TCH, size >> 16);
   2182  1.1   briggs 			}
   2183  1.1   briggs 			/* load the count in */
   2184  1.1   briggs 			ESPCMD(sc, ESPCMD_NOP|ESPCMD_DMA);
   2185  1.3   briggs #ifdef MAC68K_DRIVER
   2186  1.3   briggs 			}
   2187  1.3   briggs #endif
   2188  1.1   briggs 
   2189  1.1   briggs 			/*
   2190  1.1   briggs 			 * Note that if `size' is 0, we've already transceived
   2191  1.1   briggs 			 * all the bytes we want but we're still in DATA PHASE.
   2192  1.1   briggs 			 * Apparently, the device needs padding. Also, a
   2193  1.1   briggs 			 * transfer size of 0 means "maximum" to the chip
   2194  1.1   briggs 			 * DMA logic.
   2195  1.1   briggs 			 */
   2196  1.1   briggs 			ESPCMD(sc,
   2197  1.1   briggs 			       (size==0?ESPCMD_TRPAD:ESPCMD_TRANS)|ESPCMD_DMA);
   2198  1.1   briggs 			DMA_GO(sc->sc_dma);
   2199  1.1   briggs 			return 1;
   2200  1.1   briggs 		case STATUS_PHASE:
   2201  1.1   briggs 			ESP_PHASE(("STATUS_PHASE "));
   2202  1.1   briggs 			sc->sc_flags |= ESP_ICCS;
   2203  1.1   briggs 			ESPCMD(sc, ESPCMD_ICCS);
   2204  1.1   briggs 			sc->sc_prevphase = STATUS_PHASE;
   2205  1.1   briggs 			break;
   2206  1.1   briggs 		case INVALID_PHASE:
   2207  1.1   briggs 			break;
   2208  1.1   briggs 		default:
   2209  1.1   briggs 			printf("%s: unexpected bus phase; resetting\n",
   2210  1.1   briggs 			    sc->sc_dev.dv_xname);
   2211  1.1   briggs 			goto reset;
   2212  1.1   briggs 		}
   2213  1.1   briggs 	}
   2214  1.1   briggs 	panic("esp: should not get here..");
   2215  1.1   briggs 
   2216  1.1   briggs reset:
   2217  1.1   briggs 	esp_init(sc, 1);
   2218  1.1   briggs 	return 1;
   2219  1.1   briggs 
   2220  1.1   briggs finish:
   2221  1.1   briggs 	untimeout(esp_timeout, ecb);
   2222  1.1   briggs 	esp_done(sc, ecb);
   2223  1.1   briggs 	goto out;
   2224  1.1   briggs 
   2225  1.1   briggs sched:
   2226  1.1   briggs 	sc->sc_state = ESP_IDLE;
   2227  1.1   briggs 	esp_sched(sc);
   2228  1.1   briggs 	goto out;
   2229  1.1   briggs 
   2230  1.1   briggs out:
   2231  1.1   briggs 	return 1;
   2232  1.1   briggs }
   2233  1.1   briggs 
   2234  1.1   briggs void
   2235  1.1   briggs esp_abort(sc, ecb)
   2236  1.1   briggs 	struct esp_softc *sc;
   2237  1.1   briggs 	struct esp_ecb *ecb;
   2238  1.1   briggs {
   2239  1.1   briggs 
   2240  1.1   briggs 	/* 2 secs for the abort */
   2241  1.1   briggs 	ecb->timeout = ESP_ABORT_TIMEOUT;
   2242  1.1   briggs 	ecb->flags |= ECB_ABORT;
   2243  1.1   briggs 
   2244  1.1   briggs 	if (ecb == sc->sc_nexus) {
   2245  1.1   briggs 		/*
   2246  1.1   briggs 		 * If we're still selecting, the message will be scheduled
   2247  1.1   briggs 		 * after selection is complete.
   2248  1.1   briggs 		 */
   2249  1.1   briggs 		if (sc->sc_state == ESP_CONNECTED)
   2250  1.1   briggs 			esp_sched_msgout(SEND_ABORT);
   2251  1.1   briggs 	} else {
   2252  1.1   briggs 		esp_dequeue(sc, ecb);
   2253  1.1   briggs 		TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
   2254  1.1   briggs 		if (sc->sc_state == ESP_IDLE)
   2255  1.1   briggs 			esp_sched(sc);
   2256  1.1   briggs 	}
   2257  1.1   briggs }
   2258  1.1   briggs 
   2259  1.1   briggs void
   2260  1.1   briggs esp_timeout(arg)
   2261  1.1   briggs 	void *arg;
   2262  1.1   briggs {
   2263  1.1   briggs 	struct esp_ecb *ecb = arg;
   2264  1.1   briggs 	struct scsi_xfer *xs = ecb->xs;
   2265  1.1   briggs 	struct scsi_link *sc_link = xs->sc_link;
   2266  1.1   briggs 	struct esp_softc *sc = sc_link->adapter_softc;
   2267  1.1   briggs 	int s;
   2268  1.1   briggs 
   2269  1.1   briggs 	sc_print_addr(sc_link);
   2270  1.1   briggs 	printf("%s: timed out [ecb %p (flags 0x%x, dleft %x, stat %x)], "
   2271  1.1   briggs 	       "<state %d, nexus %p, phase(c %x, p %x), resid %x, msg(q %x,o %x) %s>",
   2272  1.1   briggs 		sc->sc_dev.dv_xname,
   2273  1.1   briggs 		ecb, ecb->flags, ecb->dleft, ecb->stat,
   2274  1.1   briggs 		sc->sc_state, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
   2275  1.1   briggs 		sc->sc_dleft, sc->sc_msgpriq, sc->sc_msgout,
   2276  1.1   briggs 		DMA_ISACTIVE(sc->sc_dma) ? "DMA active" : "");
   2277  1.1   briggs #if ESP_DEBUG > 0
   2278  1.1   briggs 	printf("TRACE: %s.", ecb->trace);
   2279  1.1   briggs #endif
   2280  1.1   briggs 
   2281  1.1   briggs 	s = splbio();
   2282  1.1   briggs 
   2283  1.1   briggs 	if (ecb->flags & ECB_ABORT) {
   2284  1.1   briggs 		/* abort timed out */
   2285  1.1   briggs 		printf(" AGAIN\n");
   2286  1.1   briggs 		esp_init(sc, 1);
   2287  1.1   briggs 	} else {
   2288  1.1   briggs 		/* abort the operation that has timed out */
   2289  1.1   briggs 		printf("\n");
   2290  1.1   briggs 		xs->error = XS_TIMEOUT;
   2291  1.1   briggs 		esp_abort(sc, ecb);
   2292  1.1   briggs 	}
   2293  1.1   briggs 
   2294  1.1   briggs 	splx(s);
   2295  1.1   briggs }
   2296