xref: /csrg-svn/sys/sparc/sbus/esp.c (revision 65145)
155138Storek /*
263322Sbostic  * Copyright (c) 1988, 1992, 1993
363322Sbostic  *	The Regents of the University of California.  All rights reserved.
455138Storek  *
555138Storek  * This software was developed by the Computer Systems Engineering group
655138Storek  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
755138Storek  * contributed to Berkeley.
855138Storek  *
955503Sbostic  * All advertising materials mentioning features or use of this software
1055503Sbostic  * must display the following acknowledgement:
1155503Sbostic  *	This product includes software developed by the University of
1259214Storek  *	California, Lawrence Berkeley Laboratory.
1355503Sbostic  *
1455138Storek  * %sccs.include.redist.c%
1555138Storek  *
16*65145Storek  *	@(#)esp.c	8.2 (Berkeley) 12/14/93
1755138Storek  *
1859327Storek  * from: $Header: esp.c,v 1.28 93/04/27 14:40:44 torek Exp $ (LBL)
1955138Storek  *
2055138Storek  * Loosely derived from Mary Baker's devSCSIC90.c from the Berkeley
2155138Storek  * Sprite project, which is:
2255138Storek  *
2355138Storek  * Copyright 1988 Regents of the University of California
2455138Storek  * Permission to use, copy, modify, and distribute this
2555138Storek  * software and its documentation for any purpose and without
2655138Storek  * fee is hereby granted, provided that the above copyright
2755138Storek  * notice appear in all copies.  The University of California
2855138Storek  * makes no representations about the suitability of this
2955138Storek  * software for any purpose.  It is provided "as is" without
3055138Storek  * express or implied warranty.
3155138Storek  *
3255138Storek  * from /sprite/src/kernel/dev/sun4c.md/RCS/devSCSIC90.c,v 1.4
3355138Storek  * 90/12/19 12:37:58 mgbaker Exp $ SPRITE (Berkeley)
3455138Storek  */
3555138Storek 
3655138Storek /*
3755138Storek  * Sbus ESP/DMA driver.  A single driver must be used for both devices
3855138Storek  * as they are physically tied to each other:  The DMA chip can only
3955138Storek  * be used to assist ESP SCSI transactions; the ESP interrupt enable is
4055138Storek  * in the DMA chip csr.
4155138Storek  *
4255138Storek  * Since DMA and SCSI interrupts are handled in the same routine, the
4355138Storek  * DMA device does not declare itself as an sbus device.  This saves
4455138Storek  * some space.
4555138Storek  */
4655138Storek 
4756540Sbostic #include <sys/param.h>
4856540Sbostic #include <sys/buf.h>
4956540Sbostic #include <sys/device.h>
5056540Sbostic #include <sys/malloc.h>
5155138Storek 
5256540Sbostic #include <dev/scsi/scsi.h>
5356540Sbostic #include <dev/scsi/scsivar.h>
5455138Storek 
5556540Sbostic #include <machine/autoconf.h>
5656540Sbostic #include <machine/cpu.h>
5755138Storek 
5856540Sbostic #include <sparc/sbus/dmareg.h>
5955138Storek #define ESP_PHASE_NAMES
6056540Sbostic #include <sparc/sbus/espreg.h>
6156540Sbostic #include <sparc/sbus/sbusvar.h>
6255138Storek 
63*65145Storek #include <libkern/libkern.h>
6455138Storek 
6555138Storek /*
66*65145Storek  * This driver is largely a giant state machine:
6755138Storek  *
68*65145Storek  *	Given some previous SCSI state (as set up or tracked by us
69*65145Storek  *	earlier) and the interrupt registers provided on the chips
70*65145Storek  *	(dmacsr, espstat, espstep, and espintr), derive an action.
71*65145Storek  *	In many cases this is just a matter of reading the target's
72*65145Storek  *	phase and following its orders, which sets a new state.
7355138Storek  *
7455138Storek  * This sequencing is done in espact(); the state is primed in espselect().
7555138Storek  *
76*65145Storek  * Data transfer is always done via DMA.  Unfortunately, there are
77*65145Storek  * limits in the DMA and ESP chips on how much data can be moved
78*65145Storek  * in a single operation.  The ESP chip has a 16-bit counter, so
79*65145Storek  * it is limited to 65536 bytes.  More insidiously, while the DMA
80*65145Storek  * chip has a 32-bit address, this is composed of a 24-bit counter
81*65145Storek  * with an 8-bit latch, so it cannot cross a 16 MB boundary.  To
82*65145Storek  * handle these, we program a smaller count than our caller requests;
83*65145Storek  * when this shorter transfer is done, if the target is still up
84*65145Storek  * for data transfer, we simply keep going (updating the DMA address)
85*65145Storek  * as needed.
8655138Storek  *
8755138Storek  * Another state bit is used to recover from bus resets:
8855138Storek  *
8955138Storek  *	A single TEST UNIT READY is attempted on each target before any
9055138Storek  *	real communication begins; this TEST UNIT READY is allowed to
9155138Storek  *	fail in any way.  This is required for the Quantum ProDrive 100
9255138Storek  *	MB disks, for instance, which respond to their first selection
9355138Storek  *	with status phase, and for anything that insists on implementing
9455138Storek  *	the broken SCSI-2 synch transfer initial message.
9555138Storek  *
9655138Storek  * This is done in espclear() (which calls espselect(); functions that
9755138Storek  * call espselect() must check for clearing first).
9855138Storek  *
9955138Storek  * The state machines actually intermingle, as some SCSI sequences are
10055138Storek  * only allowed during clearing.
10155138Storek  */
10255138Storek 
10355138Storek /* per-DMA variables */
10455138Storek struct dma_softc {
105*65145Storek 	struct	device dc_dev;		/* base device */
106*65145Storek 	volatile struct dmareg *dc_dma;	/* register virtual address */
107*65145Storek 	int	dc_dmarev;		/* revision */
108*65145Storek 	char	*dc_dmafmt;		/* format for error messages */
10955138Storek };
11055138Storek void	dmaattach(struct device *, struct device *, void *);
11155138Storek struct cfdriver dmacd =
11255138Storek     { NULL, "dma", matchbyname, dmaattach, DV_DULL, sizeof(struct dma_softc) };
11355138Storek 
11455138Storek /* per-ESP variables */
11555138Storek struct esp_softc {
11655138Storek 	/*
11755138Storek 	 * External interfaces.
11855138Storek 	 */
11955138Storek 	struct	hba_softc sc_hba;	/* base device + hba, must be first */
120*65145Storek #define	sc_dev	sc_hba.hba_dev
12155138Storek 	struct	sbusdev sc_sd;		/* sbus device */
12255138Storek 	struct	intrhand sc_ih;		/* interrupt entry */
12359214Storek 	struct	evcnt sc_intrcnt;	/* interrupt counter */
124*65145Storek 	struct	dma_softc *sc_dc;	/* pointer to corresponding dma sc */
12555138Storek 
12655138Storek 	/*
12755138Storek 	 * Addresses mapped to hardware registers.
12855138Storek 	 */
12955138Storek 	volatile struct espreg *sc_esp;
13055138Storek 	volatile struct dmareg *sc_dma;
13155138Storek 
13255138Storek 	/*
13355138Storek 	 * Copies of registers cleared/unlatched by reading.
134*65145Storek 	 * (FIFO flags is not cleared, but we want it for debugging.)
13555138Storek 	 */
13655138Storek 	u_long	sc_dmacsr;
13755138Storek 	u_char	sc_espstat;
13855138Storek 	u_char	sc_espstep;
13955138Storek 	u_char	sc_espintr;
140*65145Storek 	u_char	sc_espfflags;
14155138Storek 
14255138Storek 	/* miscellaneous */
14355138Storek 	int	sc_clockfreq;		/* clock frequency */
14455138Storek 	u_char	sc_sel_timeout;		/* select timeout */
14555138Storek 	u_char	sc_id;			/* initiator ID (default = 7) */
14655138Storek 	u_char	sc_needclear;		/* uncleared targets (1 bit each) */
14755138Storek 	u_char	sc_esptype;		/* 100, 100A, 2xx (see below) */
14855138Storek 	u_char	sc_ccf;			/* clock conversion factor */
14955138Storek 	u_char	sc_conf1;		/* value for config reg 1 */
15055138Storek 	u_char	sc_conf2;		/* value for config reg 2 */
15155138Storek 	u_char	sc_conf3;		/* value for config reg 3 */
15259214Storek 	struct	bootpath *sc_bp;	/* esp bootpath so far */
15355138Storek 
15455138Storek 	/*
15555138Storek 	 * Information pertaining to the current transfer,
15655138Storek 	 * including sequencing.
15755138Storek 	 *
15855138Storek 	 * The size of sc_msg is the size of the ESP fifo,
15955138Storek 	 * since we do message-in simply by allowing the fifo to fill.
16055138Storek 	 */
16155138Storek 	char	sc_probing;		/* used during autoconf; see below */
16255138Storek 	char	sc_clearing;		/* true => cmd is just to clear targ */
16355138Storek 	char	sc_state;		/* SCSI protocol state; see below */
16455138Storek 	char	sc_sentcmd;		/* set once we get cmd out */
16555138Storek 	char	sc_dmaactive;		/* true => doing dma */
16655138Storek #ifdef notyet
16755138Storek 	u_char	sc_sync;		/* synchronous transfer stuff (?) */
16855138Storek #endif
16955138Storek 	u_char	sc_stat[2];		/* status from last `status' phase */
17055138Storek 	u_char	sc_msg[16];		/* message from device */
17155138Storek 	u_short	sc_dmactl;		/* control to load into dma csr */
172*65145Storek 	u_long	sc_dmaaddr;		/* address for next xfer */
173*65145Storek 	int	sc_dmasize;		/* size of current xfer */
174*65145Storek 	int	sc_resid;		/* count of bytes not yet xferred */
17555138Storek 	int	sc_targ;		/* the target involved */
176*65145Storek 	struct	scsi_cdb *sc_curcdb;	/* ptr to current command */
177*65145Storek 	/* might cdbspace eventually be per-target? */
178*65145Storek 	struct	scsi_cdb sc_cdbspace;	/* space for one command */
17955138Storek };
18055138Storek 
18155138Storek /*
182*65145Storek  * Values for sc_esptype (used to control configuration reset, and for
183*65145Storek  * workarounds for chip bugs).  The order is important; see espreset().
18455138Storek  */
18555138Storek #define	ESP100	0
18655138Storek #define	ESP100A	1
18755138Storek #define	ESP2XX	2
18855138Storek 
18955138Storek /*
19055138Storek  * Probe state.  0 means not probing.  While looking for each target
19155138Storek  * we set this to PROBE_TESTING and do a TEST UNIT READY on unit 0.
19255138Storek  * If selection fails, this is changed to PROBE_NO_TARGET; otherwise
19355138Storek  * we assume the target exists, regardless of the result of the test.
19455138Storek  */
19555138Storek #define	PROBE_TESTING	1
19655138Storek #define	PROBE_NO_TARGET	2
19755138Storek 
19855138Storek /*
19955138Storek  * States in sc_state.
20055138Storek  *
201*65145Storek  * Note that S_SVC is rare: normally we load the SCSI command into the
20255138Storek  * ESP fifo and get interrupted only when the device has gone to data
20355138Storek  * or status phase.  If the device wants to play games, though, we end
20455138Storek  * up doing things differently.
20555138Storek  */
20655138Storek char *espstates[] = {
20755138Storek #define	S_IDLE		0	/* not doing anything */
20855138Storek 	"idle",
20955138Storek #define	S_SEL		1	/* expecting select done interrupt */
21055138Storek 	"selecting",
211*65145Storek #define	S_SVC		2	/* expecting service req interrupt */
212*65145Storek 	"waiting for svc req",
213*65145Storek #define	S_DI		3	/* expecting data-in done interrupt */
21455138Storek 	"receiving data",
215*65145Storek #define	S_DO		4	/* expecting data-out done interrupt */
21655138Storek 	"sending data",
217*65145Storek #define	S_STAT		5	/* expecting status done interrupt */
21855138Storek 	"receiving status",
219*65145Storek #define	S_MI		6	/* expecting message-in done interrupt */
22055138Storek 	"receiving message",
221*65145Storek #define	S_FI		7	/* expecting final disconnect interrupt */
22255138Storek 	"waiting for disconnect"
22355138Storek };
22455138Storek 
22555138Storek /*
226*65145Storek  * Hardware limits on transfer sizes (see comments at top).
227*65145Storek  */
228*65145Storek #define	ESPMAX		(64 * 1024)
229*65145Storek #define	DMAMAX(a)	(0x01000000 - ((a) & 0x00ffffff))
230*65145Storek 
231*65145Storek /*
23255138Storek  * Return values from espact().
23355138Storek  */
23455138Storek #define	ACT_CONT	0	/* espact() handled everything */
235*65145Storek #define	ACT_IO		1	/* espact() is xferring data */
236*65145Storek #define	ACT_DONE	2	/* handled everything, and op is now done */
237*65145Storek #define	ACT_ERROR	3	/* an error occurred, op has been trashed */
238*65145Storek #define	ACT_RESET	4	/* please reset ESP, then do ACT_ERROR */
239*65145Storek #define	ACT_QUICKINTR	5	/* another interrupt is expected immediately */
24055138Storek 
24155138Storek /* autoconfiguration driver */
24255138Storek void	espattach(struct device *, struct device *, void *);
24355138Storek struct cfdriver espcd =
24459214Storek     { NULL, "esp", matchbyname, espattach, DV_DULL, sizeof(struct esp_softc),
24559214Storek       "intr" };
24655138Storek 
24755138Storek /* Sbus driver */
24855138Storek void	espsbreset(struct device *);
24955138Storek 
25055138Storek /* interrupt interface */
25155138Storek int	espintr(void *);
25255138Storek 
25355138Storek /* SCSI HBA driver */
25455138Storek int	espicmd(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int, int);
25555138Storek int	espdump(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int);
25655138Storek void	espstart(struct device *, struct sq *, struct buf *,
25755138Storek 		scdgo_fn, struct device *);
25855138Storek int	espgo(struct device *, int, scintr_fn, struct device *,
25955138Storek 		struct buf *, int);
26055138Storek void	esprel(struct device *);
26155138Storek void	esphbareset(struct hba_softc *, int);
26255138Storek static struct hbadriver esphbadriver =
26355138Storek     { espicmd, espdump, espstart, espgo, esprel, esphbareset };
26455138Storek 
265*65145Storek /* other prototypes */
266*65145Storek static void espdoattach(int);
267*65145Storek static void dmareset(struct esp_softc *);
268*65145Storek static void espreset(struct esp_softc *, int);
269*65145Storek static void esperror(struct esp_softc *, const char *);
270*65145Storek static int espact(struct esp_softc *);
271*65145Storek void espselect(struct esp_softc *, int, struct scsi_cdb *);
27255138Storek 
273*65145Storek /* second arg to espreset() */
274*65145Storek #define	RESET_ESPCHIP	0x1
275*65145Storek #define	RESET_SCSIBUS	0x2
276*65145Storek #define	RESET_BOTH	(RESET_ESPCHIP | RESET_SCSIBUS)
27755138Storek 
27855138Storek /*
27955138Storek  * Attach a found DMA chip.
28055138Storek  * The second argument is really a pointer to an sbus_attach_args.
28155138Storek  */
28255138Storek void
28355138Storek dmaattach(parent, dev, args)
28455138Storek 	struct device *parent;
28555138Storek 	struct device *dev;
28655138Storek 	void *args;
28755138Storek {
288*65145Storek 	register struct dma_softc *dc = (struct dma_softc *)dev;
28955138Storek 	register struct sbus_attach_args *sa = args;
29055138Storek 	register volatile struct dmareg *dma;
29155138Storek 	register int rev;
29255138Storek 	struct esp_softc *esc;
29355138Storek 
29455138Storek 	if (sa->sa_ra.ra_vaddr)
29555138Storek 		dma = (volatile struct dmareg *)sa->sa_ra.ra_vaddr;
29655138Storek 	else
29755138Storek 		dma = (volatile struct dmareg *)
29855138Storek 		    mapiodev(sa->sa_ra.ra_paddr, sizeof(struct dmareg));
299*65145Storek 	dc->dc_dma = dma;
30055138Storek 
30155138Storek 	switch (rev = DMA_REV(dma->dma_csr)) {
30255138Storek 	case DMAREV_1:
30355138Storek 		printf(": rev 1\n");
304*65145Storek 		dc->dc_dmafmt = DMA_REV1_BITS;
30555138Storek 		break;
30655138Storek 	case DMAREV_2:
30755138Storek 		printf(": rev 2\n");
308*65145Storek 		dc->dc_dmafmt = DMA_REV2_BITS;
30955138Storek 		break;
31059327Storek 	case DMAREV_3:
31159327Storek 		printf(": rev 3\n");
31259327Storek 		printf("WARNING: esp.c not yet updated for rev 3\n");
313*65145Storek 		dc->dc_dmafmt = DMA_REV3_BITS;
31459327Storek 		break;
31555138Storek 	default:
31659327Storek 		printf(": unknown revision code 0x%x\n", rev);
317*65145Storek 		dc->dc_dmafmt = DMA_REV3_BITS;	/* cross fingers */
31855138Storek 		break;
31955138Storek 	}
320*65145Storek 	dc->dc_dmarev = rev;
321*65145Storek 	espdoattach(dc->dc_dev.dv_unit);
32255138Storek }
32355138Storek 
32455138Storek /*
32555138Storek  * Attach a found ESP chip.  Search for targets; attach each one found.
32655138Storek  * The latter must be deferred if the corresponding dma chip has not yet
32755138Storek  * been configured.
32855138Storek  */
32955138Storek void
33055138Storek espattach(parent, self, args)
33155138Storek 	struct device *parent;
33255138Storek 	struct device *self;
33355138Storek 	void *args;
33455138Storek {
33555138Storek 	register struct esp_softc *sc = (struct esp_softc *)self;
33655138Storek 	register struct sbus_attach_args *sa = args;
33755138Storek 	register volatile struct espreg *esp;
33859214Storek 	register struct bootpath *bp;
33955138Storek 	int node, pri, freq, t;
34055138Storek 
34155138Storek 	if (sa->sa_ra.ra_nintr != 1) {
34255138Storek 		printf(": expected 1 interrupt, got %d\n", sa->sa_ra.ra_nintr);
34355138Storek 		return;
34455138Storek 	}
34555138Storek 	pri = sa->sa_ra.ra_intr[0].int_pri;
34655138Storek 	printf(" pri %d", pri);
34755138Storek 	if (sa->sa_ra.ra_vaddr)
34855138Storek 		esp = (volatile struct espreg *)sa->sa_ra.ra_vaddr;
34955138Storek 	else
35055138Storek 		esp = (volatile struct espreg *)
35155138Storek 		    mapiodev(sa->sa_ra.ra_paddr, sizeof(struct espreg));
35255138Storek 	sc->sc_esp = esp;
35355138Storek 	node = sa->sa_ra.ra_node;
35455138Storek 	sc->sc_id = getpropint(node, "initiator-id", 7);
35555138Storek 	freq = getpropint(node, "clock-frequency", -1);
35655138Storek 	if (freq < 0)
357*65145Storek 		freq =
358*65145Storek 		    ((struct sbus_softc *)sc->sc_dev.dv_parent)->sc_clockfreq;
35955138Storek 
36055138Storek 	/* MIGHT NEED TO RESET ESP CHIP HERE ...? */
36155138Storek 
36255138Storek 	/*
36355138Storek 	 * Find out whether we have a -100, -100A, or -2xx,
36455138Storek 	 * and what speed it runs at.
36555138Storek 	 */
36655138Storek 	sc->sc_conf1 = sc->sc_id | ESPCONF1_PARENB;
36755138Storek 	/* sc->sc_conf2 = 0; */
36855138Storek 	/* sc->sc_conf3 = 0; */
36955138Storek 	esp->esp_conf1 = sc->sc_conf1;
37055138Storek 	esp->esp_conf2 = 0;
37155138Storek 	esp->esp_conf2 = ESPCONF2_SCSI2 | ESPCONF2_RPE;
37255138Storek 	if ((esp->esp_conf2 & ~ESPCONF2_RSVD) !=
37355138Storek 	    (ESPCONF2_SCSI2 | ESPCONF2_RPE)) {
37455138Storek 		printf(": ESP100");
37555138Storek 		sc->sc_esptype = ESP100;
37655138Storek 	} else {
37755138Storek 		esp->esp_conf2 = 0;
37855138Storek 		esp->esp_conf3 = 0;
37955138Storek 		esp->esp_conf3 = 5;
38055138Storek 		if (esp->esp_conf3 != 5) {	/* XXX def bits */
38155138Storek 			printf(": ESP100A");
38255138Storek 			sc->sc_esptype = ESP100A;
38355138Storek 		} else {
38455138Storek 			esp->esp_conf3 = 0;
38555138Storek 			printf(": ESP2XX");
38655138Storek 			sc->sc_esptype = ESP2XX;
38755138Storek 		}
38855138Storek 	}
38955138Storek 	printf(", clock = %s MHz, ID = %d\n", clockfreq(freq), sc->sc_id);
39055138Storek 
39155138Storek 	/*
39255138Storek 	 * Set clock conversion factor and select timeout.
39355138Storek 	 * N.B.: clock frequency is not actually used in the rest
39455138Storek 	 * of the driver; I calculate it here for completeness only
39555138Storek 	 * (so I can see it when debugging).
39655138Storek 	 */
39755138Storek 	sc->sc_clockfreq = freq;
39855138Storek 	freq = howmany(freq, 1000 * 1000);	/* convert to MHz */
39955138Storek 	t = ESPCCF_FROMMHZ(freq);
40055138Storek 	if (t < ESPCCF_MIN)
40155138Storek 		t = ESPCCF_MIN;
40255138Storek 	sc->sc_ccf = t;
40355138Storek 	t = ESPTIMO_REGVAL(250, t, freq);	/* timeout = 250 ms. */
40455138Storek 	if (t >= 256)
40555138Storek 		t = 0;
40655138Storek 	sc->sc_sel_timeout = t;
40755138Storek 
40855138Storek 	/*
40955138Storek 	 * Link into sbus; set interrupt handler.
41055138Storek 	 */
41155138Storek 	sc->sc_sd.sd_reset = espsbreset;
412*65145Storek 	sbus_establish(&sc->sc_sd, &sc->sc_dev);
41355138Storek 	sc->sc_ih.ih_fun = espintr;
41455138Storek 	sc->sc_ih.ih_arg = sc;
41555138Storek 	intr_establish(pri, &sc->sc_ih);
416*65145Storek 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
41759214Storek 
41859214Storek #define SAME_ESP(bp, sa) \
41959214Storek 	((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
420*65145Storek 	 (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
42159214Storek 
42259214Storek 	bp = sa->sa_ra.ra_bp;
42359214Storek 	if (bp != NULL && strcmp(bp->name, "esp") == 0 && SAME_ESP(bp, sa))
42459214Storek 		sc->sc_bp = bp + 1;
425*65145Storek 	espdoattach(sc->sc_dev.dv_unit);
42655138Storek }
42755138Storek 
42855138Storek /*
42955138Storek  * `Final' attach of esp occurs once esp and dma chips have been found
43055138Storek  * and assigned virtual addresses.  Set up the ESP SCSI data structures
43155138Storek  * and probe the SCSI bus.
43255138Storek  */
43355138Storek static void
43455138Storek espdoattach(unit)
43555138Storek 	int unit;
43655138Storek {
43755138Storek 	register struct esp_softc *sc;
438*65145Storek 	register struct dma_softc *dc;
43959214Storek 	register struct bootpath *bp;
44059214Storek 	register struct targ *t;
44159214Storek 	register int targ, u;
44255138Storek 
44355138Storek 	/* make sure we have both */
44455138Storek 	if (espcd.cd_ndevs <= unit ||
44555138Storek 	    dmacd.cd_ndevs <= unit ||
44655138Storek 	    (sc = espcd.cd_devs[unit]) == NULL ||
447*65145Storek 	    (dc = dmacd.cd_devs[unit]) == NULL)
44855138Storek 		return;
449*65145Storek 	sc->sc_dc = dc;
450*65145Storek 	sc->sc_dma = dc->dc_dma;
45155138Storek 	sc->sc_hba.hba_driver = &esphbadriver;
45255138Storek 
453*65145Storek 	sc->sc_dma->dma_csr = 0;	/* ??? */
454*65145Storek 	espreset(sc, RESET_ESPCHIP);
45555138Storek 
45655138Storek 	/* MAYBE THIS SHOULD BE MOVED TO scsi_subr.c? */
45755138Storek 	for (targ = 0; targ < 8; targ++) {
45855138Storek 		if (targ == sc->sc_id)
45955138Storek 			continue;
46055138Storek 		sc->sc_probing = PROBE_TESTING;
46155138Storek 		sc->sc_clearing = 1;
462*65145Storek 		(void)scsi_test_unit_ready(&sc->sc_hba, targ, 0);
46355138Storek 		if (sc->sc_probing != PROBE_NO_TARGET) {
46455138Storek 			sc->sc_probing = 0;
46555138Storek 			sc->sc_clearing = 0;
46655138Storek 			SCSI_FOUNDTARGET(&sc->sc_hba, targ);
46755138Storek 		}
46855138Storek 	}
46955138Storek 	sc->sc_probing = 0;
47055138Storek 	sc->sc_clearing = 0;
47159214Storek 
47259214Storek 	/*
473*65145Storek 	 * See if we booted from a unit on this target.  We could
474*65145Storek 	 * compare bp->name against the unit's name but there's no
475*65145Storek 	 * real need since a target and unit uniquely specify a
476*65145Storek 	 * scsi device.
47759214Storek 	 */
478*65145Storek 	if ((bp = sc->sc_bp) != NULL && (u_int)(targ = bp->val[0]) < 8 &&
479*65145Storek 	    (u_int)(u = bp->val[1]) < 8 &&
480*65145Storek 	    (t = sc->sc_hba.hba_targets[targ]) != NULL && t->t_units[u] != NULL)
48159214Storek 		bootdv = t->t_units[u]->u_dev;
48255138Storek }
48355138Storek 
48455138Storek /*
485*65145Storek  * We are not allowed to touch the DMA "flush" and "drain" bits
486*65145Storek  * while it is still thinking about a request (DMA_RP).
48755138Storek  */
488*65145Storek #define	DMAWAIT(dma)	while ((dma)->dma_csr & DMA_RP) DELAY(1)
489*65145Storek 
490*65145Storek /*
491*65145Storek  * Reset the DMA chip.
492*65145Storek  */
49355138Storek static void
49455138Storek dmareset(sc)
49555138Storek 	struct esp_softc *sc;
49655138Storek {
49755138Storek 	register volatile struct dmareg *dma = sc->sc_dma;
49855138Storek 
499*65145Storek 	DMAWAIT(dma);
50055138Storek 	dma->dma_csr |= DMA_RESET;
50155138Storek 	DELAY(200);
50255138Storek 	dma->dma_csr &= ~DMA_RESET;	/* ??? */
50355138Storek 	sc->sc_state = S_IDLE;
50455138Storek 	sc->sc_dmaactive = 0;
505*65145Storek 	if (sc->sc_dc->dc_dmarev == DMAREV_2 && sc->sc_esptype != ESP100)
50659327Storek 		dma->dma_csr |= DMA_TURBO;
50755138Storek 	dma->dma_csr |= DMA_IE;		/* enable interrupts */
50855138Storek 	DELAY(200);
50955138Storek }
51055138Storek 
51155138Storek /*
512*65145Storek  * Reset the chip and/or SCSI bus (always resets DMA).
51355138Storek  */
51455138Storek static void
515*65145Storek espreset(sc, how)
51655138Storek 	register struct esp_softc *sc;
517*65145Storek 	int how;
51855138Storek {
51955138Storek 	register volatile struct espreg *esp = sc->sc_esp;
52055138Storek 
52155138Storek 	dmareset(sc);
522*65145Storek 	if (how & RESET_ESPCHIP) {
523*65145Storek 		esp->esp_cmd = ESPCMD_RESET_CHIP;
524*65145Storek 		esp->esp_cmd = ESPCMD_NOP;
525*65145Storek 		/*
526*65145Storek 		 * Reload configuration registers (cleared by
527*65145Storek 		 * RESET_CHIP command).  Reloading conf2 on an
528*65145Storek 		 * ESP100 goofs it up, so out of paranoia we load
529*65145Storek 		 * only the registers that exist.
530*65145Storek 		 */
531*65145Storek 		esp->esp_conf1 = sc->sc_conf1;
532*65145Storek 		if (sc->sc_esptype > ESP100) {		/* 100A, 2XX */
533*65145Storek 			esp->esp_conf2 = sc->sc_conf2;
534*65145Storek 			if (sc->sc_esptype > ESP100A)	/* 2XX only */
535*65145Storek 				esp->esp_conf3 = sc->sc_conf3;
536*65145Storek 		}
537*65145Storek 		esp->esp_ccf = sc->sc_ccf;
538*65145Storek 		esp->esp_timeout = sc->sc_sel_timeout;
539*65145Storek 		/* We set synch offset later. */
54055138Storek 	}
541*65145Storek 	if (how & RESET_SCSIBUS) {
542*65145Storek 		/*
543*65145Storek 		 * The chip should retain most of its parameters
544*65145Storek 		 * (including esp_ccf) across this kind of reset
545*65145Storek 		 * (see section 3.5 of Emulex documentation).
546*65145Storek 		 */
547*65145Storek 		/* turn off scsi bus reset interrupts and reset scsi bus */
548*65145Storek 		esp->esp_conf1 = sc->sc_conf1 | ESPCONF1_REPORT;
549*65145Storek 		esp->esp_cmd = ESPCMD_RESET_BUS;
550*65145Storek 		esp->esp_cmd = ESPCMD_NOP;
551*65145Storek 		DELAY(100000);	/* ??? */
552*65145Storek 		(void)esp->esp_intr;
553*65145Storek 		esp->esp_conf1 = sc->sc_conf1;
554*65145Storek 	}
55555138Storek 
55655138Storek 	sc->sc_needclear = 0xff;
55755138Storek }
55855138Storek 
55955138Storek /*
56055138Storek  * Reset the SCSI bus and, optionally, all attached targets.
56155138Storek  */
56255138Storek void
56355138Storek esphbareset(hba, resetunits)
56455138Storek 	struct hba_softc *hba;
56555138Storek 	int resetunits;
56655138Storek {
56755138Storek 	register struct esp_softc *sc = (struct esp_softc *)hba;
56855138Storek 
569*65145Storek 	espreset(sc, RESET_SCSIBUS);
57055138Storek 	if (resetunits)
57155138Storek 		scsi_reset_units(&sc->sc_hba);
57255138Storek }
57355138Storek 
57455138Storek /*
57555138Storek  * Reset the esp, after an Sbus reset.
57655138Storek  * Also resets corresponding dma chip.
57755138Storek  *
57855138Storek  * THIS ROUTINE MIGHT GO AWAY
57955138Storek  */
58055138Storek void
58155138Storek espsbreset(dev)
58255138Storek 	struct device *dev;
58355138Storek {
58455138Storek 	struct esp_softc *sc = (struct esp_softc *)dev;
58555138Storek 
586*65145Storek 	if (sc->sc_dc) {
587*65145Storek 		printf(" %s %s", sc->sc_dc->dc_dev.dv_xname,
588*65145Storek 		    sc->sc_dev.dv_xname);
58955138Storek 		esphbareset(&sc->sc_hba, 1);
59055138Storek 	}
59155138Storek }
59255138Storek 
593*65145Storek /*
594*65145Storek  * Log an error.
595*65145Storek  */
59655138Storek static void
59755138Storek esperror(sc, err)
59855138Storek 	register struct esp_softc *sc;
599*65145Storek 	const char *err;
60055138Storek {
601*65145Storek 	int stat;
60255138Storek 
603*65145Storek 	stat = sc->sc_espstat;
604*65145Storek 	printf(
605*65145Storek "%s target %d cmd 0x%x (%s): %s:\n\
606*65145Storek \tstat=%b (%s) step=%x dmacsr=%b fflags=%x intr=%b\n",
607*65145Storek 	    sc->sc_dev.dv_xname, sc->sc_targ, sc->sc_curcdb->cdb_bytes[0],
608*65145Storek 	    espstates[sc->sc_state], err,
609*65145Storek 	    stat, ESPSTAT_BITS, espphases[stat & ESPSTAT_PHASE],
610*65145Storek 	    sc->sc_espstep, sc->sc_dmacsr, sc->sc_dc->dc_dmafmt,
611*65145Storek 	    sc->sc_espfflags, sc->sc_espintr, ESPINTR_BITS);
61255138Storek }
61355138Storek 
61455138Storek /*
615*65145Storek  * Issue a select, loading command into the FIFO.
616*65145Storek  * Return nonzero on error, 0 if OK.
617*65145Storek  * Sets state to `selecting'; espact() will sequence state FSM.
618*65145Storek  */
619*65145Storek void
620*65145Storek espselect(sc, targ, cdb)
621*65145Storek 	register struct esp_softc *sc;
622*65145Storek 	register int targ;
623*65145Storek 	register struct scsi_cdb *cdb;
624*65145Storek {
625*65145Storek 	register volatile struct espreg *esp;
626*65145Storek 	register int i, cmdlen;
627*65145Storek 
628*65145Storek 	sc->sc_targ = targ;
629*65145Storek 	sc->sc_state = S_SEL;
630*65145Storek 	sc->sc_curcdb = cdb;
631*65145Storek 	sc->sc_sentcmd = 0;
632*65145Storek 	sc->sc_stat[0] = 0xff;		/* ??? */
633*65145Storek 	sc->sc_msg[0] = 0xff;		/* ??? */
634*65145Storek 
635*65145Storek 	/*
636*65145Storek 	 * Try to talk to target.
637*65145Storek 	 * Synch offset 0 => asynchronous transfer.
638*65145Storek 	 */
639*65145Storek 	esp = sc->sc_esp;
640*65145Storek 	esp->esp_id = targ;
641*65145Storek 	esp->esp_syncoff = 0;
642*65145Storek 
643*65145Storek 	/*
644*65145Storek 	 * Stuff the command bytes into the fifo.
645*65145Storek 	 * Select without attention since we do not do disconnect yet.
646*65145Storek 	 */
647*65145Storek 	cmdlen = SCSICMDLEN(cdb->cdb_bytes[0]);
648*65145Storek 	for (i = 0; i < cmdlen; i++)
649*65145Storek 		esp->esp_fifo = cdb->cdb_bytes[i];
650*65145Storek 	esp->esp_cmd = ESPCMD_SEL_NATN;
651*65145Storek 	/* the rest is done elsewhere */
652*65145Storek }
653*65145Storek 
654*65145Storek /*
655*65145Storek  * Sequence through the SCSI state machine.  Return the action to take.
65655138Storek  *
65755138Storek  * Most of the work happens here.
65855138Storek  *
65955138Storek  * There are three interrupt sources:
66055138Storek  *   -- ESP interrupt request (typically, some device wants something).
66155138Storek  *   -- DMA memory error.
66255138Storek  *   -- DMA byte count has reached 0 (we do not often want this one but
66355138Storek  *	can only turn it off in rev 2 DMA chips, it seems).
66455138Storek  *	DOES THIS OCCUR AT ALL HERE?  THERE IS NOTHING TO HANDLE IT!
66555138Storek  */
66655138Storek static int
667*65145Storek espact(sc)
66855138Storek 	register struct esp_softc *sc;
669*65145Storek {
67055138Storek 	register volatile struct espreg *esp;
67155138Storek 	register volatile struct dmareg *dma;
672*65145Storek 	register int reg, i, resid, newstate;
67355138Storek 	register struct scsi_cdb *cdb;
67455138Storek 
675*65145Storek 	dma = sc->sc_dma;
67655138Storek 	/* check various error conditions, using as little code as possible */
67755138Storek 	if (sc->sc_dmacsr & DMA_EP) {
67855138Storek 		esperror(sc, "DMA error");
679*65145Storek 		DMAWAIT(dma);
68055138Storek 		dma->dma_csr |= DMA_FLUSH;
68155138Storek 		return (ACT_ERROR);
68255138Storek 	}
68355138Storek 	reg = sc->sc_espstat;
68455138Storek 	if (reg & ESPSTAT_GE) {
68555138Storek 		/*
68655138Storek 		 * This often occurs when there is no target.
68755138Storek 		 * (See DSC code below.)
68855138Storek 		 */
68955138Storek 		if (sc->sc_espintr & ESPINTR_DSC &&
69055138Storek 		    sc->sc_state == S_SEL && sc->sc_probing) {
69155138Storek 			sc->sc_probing = PROBE_NO_TARGET;
69255138Storek 			return (ACT_RESET);
69355138Storek 		}
694*65145Storek esperror(sc, "DIAG: gross error (ignored)");
69555138Storek 	}
69655138Storek 	if (reg & ESPSTAT_PE) {
69755138Storek 		esperror(sc, "parity error");
69855138Storek 		return (ACT_RESET);
69955138Storek 	}
70055138Storek 	reg = sc->sc_espintr;
70155138Storek #define ERR (ESPINTR_SBR|ESPINTR_ILC|ESPINTR_RSL|ESPINTR_SAT|ESPINTR_SEL)
70255138Storek 	if (reg & ERR) {
70355138Storek 		if (reg & ESPINTR_SBR)
70455138Storek 			esperror(sc, "scsi bus reset");
70555138Storek 		else if (reg & ESPINTR_ILC)
70655138Storek 			esperror(sc, "illegal command (driver bug)");
70755138Storek 		else {
708*65145Storek 			printf("%s: target %d", sc->sc_dev.dv_xname,
709*65145Storek 			    sc->sc_targ);
71055138Storek 			if (reg & ESPINTR_RSL)
71155138Storek 				printf(" tried to reselect;");
71255138Storek 			if (reg & ESPINTR_SAT)
71355138Storek 				printf(" selected with ATN;");
71455138Storek 			if (reg & ESPINTR_SEL)
71555138Storek 				printf(" selected us as target;");
71655138Storek 			printf("we do not allow this yet\n");
71755138Storek 		}
71855138Storek 		return (ACT_ERROR);
71955138Storek 	}
72055138Storek #undef ERR
72155138Storek 
722*65145Storek 	esp = sc->sc_esp;
723*65145Storek 
72455138Storek 	/*
72555138Storek 	 * Disconnect currently only allowed in `final interrupt' states.
72655138Storek 	 */
72755138Storek 	if (reg & ESPINTR_DSC) {
72855138Storek 		if (sc->sc_state == S_FI)
72955138Storek 			return (ACT_DONE);
73055138Storek 		/*
73155138Storek 		 * If we were doing a select just to test the existence
73255138Storek 		 * of the target, note that it did not respond; otherwise
73355138Storek 		 * gripe.
73455138Storek 		 */
73555138Storek 		if (sc->sc_state == S_SEL) {
73655138Storek 			if (sc->sc_probing) {
73755138Storek 				sc->sc_probing = PROBE_NO_TARGET;
73855138Storek 				return (ACT_RESET);
73955138Storek 			}
74055138Storek 		}
74155138Storek 		/* flush fifo, in case we were selecting or sending data */
74255138Storek 		esp->esp_cmd = ESPCMD_FLUSH_FIFO;
743*65145Storek 		DELAY(1);
74455138Storek 		printf("%s: target %d not responding\n",
745*65145Storek 		    sc->sc_dev.dv_xname, sc->sc_targ);
74655138Storek 		return (ACT_ERROR);
74755138Storek 	}
74855138Storek 
74955138Storek 	/*
75055138Storek 	 * Okay, things are moving along.
75155138Storek 	 * What were we doing the last time we did something,
75255138Storek 	 * and did it complete normally?
75355138Storek 	 */
75455138Storek 	switch (sc->sc_state) {
75555138Storek 
75655138Storek 	case S_SEL:
75755138Storek 		/*
75855138Storek 		 * We were selecting.  Arbitration and select are
75955138Storek 		 * complete (because ESPINTR_DSC was not set), but
76055138Storek 		 * there is no guarantee the command went out.
76155138Storek 		 */
76255138Storek 		if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) !=
76355138Storek 		    (ESPINTR_SVC|ESPINTR_CMP)) {
76455138Storek 			esperror(sc, "selection failed");
76555138Storek 			return (ACT_RESET);
76655138Storek 		}
76755138Storek 		if (sc->sc_espstep == ESPSTEP_DONE) {
76855138Storek 			sc->sc_sentcmd = 1;
76955138Storek 			break;
77055138Storek 		}
77155138Storek 		if (sc->sc_espstep == 2) {
77255138Storek 			/*
77355138Storek 			 * We got something other than command phase.
77455138Storek 			 * Just pretend things are normal; the
77555138Storek 			 * device will ask for the command later.
77655138Storek 			 */
777*65145Storek esperror(sc, "DIAG: esp step 2");
77855138Storek 		} else if (sc->sc_espstep == 3) {
77955138Storek 			/*
78055138Storek 			 * Device entered command phase and then exited it
78155138Storek 			 * before we finished handing out the command.
78255138Storek 			 * Let this happen iff we are trying to clear the
78355138Storek 			 * target state.
78455138Storek 			 */
785*65145Storek esperror(sc, "DIAG: esp step 3");
78655138Storek 			if (!sc->sc_clearing)
78755138Storek 				return (ACT_RESET);
78855138Storek 		} else {
78955138Storek 			printf("%s: mysterious esp step %d\n",
790*65145Storek 			    sc->sc_dev.dv_xname, sc->sc_espstep);
79155138Storek 			return (ACT_RESET);
79255138Storek 		}
793*65145Storek 
79455138Storek 		/*
79555138Storek 		 * Part of the command may still be lodged in the FIFO.
79655138Storek 		 */
797*65145Storek 		if (ESP_NFIFO(sc->sc_espfflags)) {
798*65145Storek 			esp->esp_cmd = ESPCMD_FLUSH_FIFO;
799*65145Storek 			DELAY(1);
800*65145Storek 		}
80155138Storek 		break;
80255138Storek 
803*65145Storek 	case S_SVC:
80455138Storek 		/*
80555138Storek 		 * We were waiting for phase change after stuffing the command
80655138Storek 		 * into the FIFO.  Make sure it got out.
80755138Storek 		 */
808*65145Storek 		if (ESP_NFIFO(sc->sc_espfflags)) {
809*65145Storek esperror(sc, "DIAG: CMDSVC, fifo not empty");
81055138Storek 			esp->esp_cmd = ESPCMD_FLUSH_FIFO;
811*65145Storek 			DELAY(1);
81255138Storek 		} else
81355138Storek 			sc->sc_sentcmd = 1;
81455138Storek 		break;
81555138Storek 
81655138Storek 	case S_DI:
81755138Storek 		/*
81855138Storek 		 * We were doing DMA data in, and expecting a
81955138Storek 		 * transfer-count-zero interrupt or a phase change.
820*65145Storek 		 * We got that; drain the pack register and handle
821*65145Storek 		 * as for data out -- but ignore FIFO (it should be
822*65145Storek 		 * empty, except for sync mode which we are not
823*65145Storek 		 * using anyway).
82455138Storek 		 */
825*65145Storek 		DMAWAIT(dma);
82655138Storek 		dma->dma_csr |= DMA_DRAIN;
827*65145Storek 		DELAY(1);
828*65145Storek 		resid = 0;
82955138Storek 		goto dma_data_done;
83055138Storek 
83155138Storek 	case S_DO:
83255138Storek 		/*
83355138Storek 		 * We were doing DMA data out.  If there is data in the
83455138Storek 		 * FIFO, it is stuff that got DMAed out but never made
83555138Storek 		 * it to the device, so it counts as residual.
83655138Storek 		 */
837*65145Storek 		if ((resid = ESP_NFIFO(sc->sc_espfflags)) != 0) {
83855138Storek 			esp->esp_cmd = ESPCMD_FLUSH_FIFO;
839*65145Storek 			DELAY(1);
840*65145Storek 		}
84155138Storek dma_data_done:
84255138Storek 		if (sc->sc_dmaactive == 0) {
843*65145Storek 			esperror(sc, "dma done w/o dmaactive");
84455138Storek 			panic("espact");
84555138Storek 		}
84655138Storek 		sc->sc_dmaactive = 0;
847*65145Storek 
848*65145Storek 		/* Finish computing residual count. */
849*65145Storek 		reg = esp->esp_tcl | (esp->esp_tch << 8);
85055138Storek 		if (reg == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0)
85155138Storek 			reg = 65536;
852*65145Storek 		resid += reg;
853*65145Storek 
854*65145Storek 		/* Compute xfer count (requested - resid). */
855*65145Storek 		i = sc->sc_dmasize - resid;
856*65145Storek 		if (i < 0) {
85755138Storek 			printf("%s: xfer resid (%d) > xfer req (%d)\n",
858*65145Storek 			    sc->sc_dev.dv_xname, resid, sc->sc_dmasize);
859*65145Storek 			i = sc->sc_dmasize;	/* forgiving... */
86055138Storek 		}
861*65145Storek 
862*65145Storek 		/* If data came in we must flush cache. */
86355138Storek 		if (sc->sc_state == S_DI)
864*65145Storek 			cache_flush(sc->sc_dmaaddr, i);
865*65145Storek 		sc->sc_dmaaddr += i;
866*65145Storek 		sc->sc_resid -= i;
86755138Storek 		if ((sc->sc_espintr & ESPINTR_SVC) == 0) {
868*65145Storek 			esperror(sc, "no bus service req");
86955138Storek 			return (ACT_RESET);
87055138Storek 		}
87155138Storek 		break;
87255138Storek 
87355138Storek 	case S_STAT:
87455138Storek 		/*
87555138Storek 		 * The last thing we did was tell it `initiator complete'
87655138Storek 		 * and so we expect to have gotten both the status byte
87755138Storek 		 * and the final message byte.  It is possible that we
87855138Storek 		 * got something else....
87955138Storek 		 *
88055138Storek 		 * Apparently, BUS SERVICE is set if we got just status,
88155138Storek 		 * while FUNCTION COMPLETE is set if we got both.
88255138Storek 		 */
88355138Storek 		if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != ESPINTR_CMP) {
88455138Storek 			esperror(sc, "bad status interrupt state");
88555138Storek 			return (ACT_RESET);
88655138Storek 		}
887*65145Storek 		reg = ESP_NFIFO(sc->sc_espfflags);
88855138Storek 		if (reg < 2) {
88955138Storek 			printf(
890*65145Storek 		"%s: command done but fifo count = %d; must be >= 2\n",
891*65145Storek 			    sc->sc_dev.dv_xname, reg);
89255138Storek 			return (ACT_RESET);
89355138Storek 		}
89455138Storek 		/*
89555138Storek 		 * Read the status and the first msg byte.
89655138Storek 		 * It should be CMD_COMPLETE.  Eventually we
89755138Storek 		 * may handle IDENTIFY, DISCONNECT, etc., as well.
89855138Storek 		 */
89955138Storek 		sc->sc_stat[0] = esp->esp_fifo;
90055138Storek 		sc->sc_msg[0] = reg = esp->esp_fifo;
90155138Storek 		esp->esp_cmd = ESPCMD_MSG_ACCEPT;
90255138Storek 		if (reg == MSG_CMD_COMPLETE) {
90355138Storek 			sc->sc_state = S_FI;
90455138Storek 			return (ACT_CONT);
90555138Storek 		}
90655138Storek 		if (SCSIMSGLEN(reg) != 1) {
90755138Storek 			printf("%s: target %d is naughty\n",
908*65145Storek 			    sc->sc_dev.dv_xname, sc->sc_targ);
90955138Storek 			return (ACT_RESET);
91055138Storek 		}
91155138Storek 		printf("%s: warning: target %d returned msg 0x%x\n",
912*65145Storek 		    sc->sc_dev.dv_xname, sc->sc_targ, reg);
91355138Storek 		sc->sc_state = S_FI;
91455138Storek 		return (ACT_CONT);
91555138Storek 
91655138Storek 	case S_MI:
91755138Storek 		if ((reg & ESPINTR_SVC) == 0) {
91855138Storek 			esperror(sc, "missing phase after msg in");
91955138Storek 			return (ACT_RESET);
92055138Storek 		}
921*65145Storek 		reg = ESP_NFIFO(sc->sc_espfflags);
92255138Storek 		for (i = 0; i < reg; i++)
92355138Storek 			sc->sc_msg[i] = esp->esp_fifo;
92455138Storek 		break;
92555138Storek 
92655138Storek 	case S_FI:
92755138Storek 		esperror(sc, "target did not disconnect");
92855138Storek 		return (ACT_RESET);
92955138Storek 	}
93055138Storek 
93155138Storek 	/*
93255138Storek 	 * Things are still moving along.  The phase tells us
93355138Storek 	 * what the device wants next.  Do it.
93455138Storek 	 */
935*65145Storek 	switch (sc->sc_espstat & ESPSTAT_PHASE) {
93655138Storek 
93755138Storek 	case ESPPHASE_DATA_OUT:
938*65145Storek if (!sc->sc_sentcmd) esperror(sc, "DIAG: data out without command");
939*65145Storek 		if (sc->sc_dmactl & DMA_READ) {
940*65145Storek 			esperror(sc, "wrong phase (want to read)");
941*65145Storek 			return (ACT_RESET);
942*65145Storek 		}
943*65145Storek 		newstate = S_DO;
944*65145Storek 		goto do_data_xfer;
94555138Storek 
94655138Storek 	case ESPPHASE_DATA_IN:
947*65145Storek if (!sc->sc_sentcmd) esperror(sc, "DIAG: data in without command");
948*65145Storek 		if (!(sc->sc_dmactl & DMA_READ)) {
949*65145Storek 			esperror(sc, "wrong phase (want to write)");
950*65145Storek 			return (ACT_RESET);
951*65145Storek 		}
952*65145Storek 		newstate = S_DI;
953*65145Storek do_data_xfer:
954*65145Storek 		if (sc->sc_resid == 0) {
955*65145Storek 			esperror(sc, "data count error");
956*65145Storek 			return (ACT_RESET);
957*65145Storek 		}
95855138Storek 
959*65145Storek 		/*
960*65145Storek 		 * Compute DMA count based on chip limits.
961*65145Storek 		 * Set DMA address and load transfer count into
962*65145Storek 		 * ESP via DMA NOP, then set DMA control, and
963*65145Storek 		 * then we can start the DMA.
964*65145Storek 		 */
965*65145Storek 		sc->sc_state = newstate;
966*65145Storek 		i = min(sc->sc_resid, ESPMAX);
967*65145Storek 		i = min(i, DMAMAX(sc->sc_dmaaddr));
968*65145Storek 		sc->sc_dmasize = i;
969*65145Storek 		dma->dma_addr = sc->sc_dmaaddr;
970*65145Storek 		esp->esp_tch = i >> 8;
971*65145Storek 		esp->esp_tcl = i;
972*65145Storek 		esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP;
973*65145Storek 		dma->dma_csr = sc->sc_dmactl;
974*65145Storek 		sc->sc_dmaactive = 1;
975*65145Storek 		esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO;
976*65145Storek 		return (ACT_IO);
977*65145Storek 
97855138Storek 	case ESPPHASE_CMD:
97955138Storek 		/*
98055138Storek 		 * Silly thing wants the command again.
981*65145Storek 		 * Load it into the FIFO and go to SVC state.
98255138Storek 		 */
983*65145Storek printf("%s: redoing command\n", sc->sc_dev.dv_xname);
984*65145Storek 		cdb = sc->sc_curcdb;
98555138Storek 		reg = SCSICMDLEN(cdb->cdb_bytes[0]);
98655138Storek 		for (i = 0; i < reg; i++)
98755138Storek 			esp->esp_fifo = cdb->cdb_bytes[i];
988*65145Storek 		sc->sc_state = S_SVC;
98955138Storek 		esp->esp_cmd = ESPCMD_XFER_INFO;
99055138Storek 		return (ACT_CONT);
99155138Storek 
99255138Storek 	case ESPPHASE_STATUS:
99355138Storek 		sc->sc_state = S_STAT;
99455138Storek 		esp->esp_cmd = ESPCMD_INIT_COMP;
99555138Storek 		return (ACT_CONT);
99655138Storek 
99755138Storek 	case ESPPHASE_MSG_IN:
998*65145Storek printf("%s: accepting (& ignoring) msg from target %d\n",
999*65145Storek     sc->sc_dev.dv_xname, sc->sc_targ);
100055138Storek 		sc->sc_state = S_MI;
100155138Storek 		esp->esp_cmd = ESPCMD_MSG_ACCEPT;
100255138Storek 		return (ACT_CONT);
100355138Storek 
100455138Storek 	default:
1005*65145Storek 		esperror(sc, "bad phase");
100655138Storek 		return (ACT_RESET);
100755138Storek 	}
100855138Storek 	/* NOTREACHED */
100955138Storek }
101055138Storek 
101155138Storek /*
101255138Storek  * Clear out target state by doing a special TEST UNIT READY.
101355138Storek  * Note that this calls espicmd (possibly recursively).
101455138Storek  */
101555138Storek void
101655138Storek espclear(sc, targ)
101755138Storek 	register struct esp_softc *sc;
101855138Storek 	register int targ;
101955138Storek {
102055138Storek 
102155138Storek 	/* turn off needclear immediately since this calls espicmd() again */
102255138Storek 	sc->sc_needclear &= ~(1 << targ);
102355138Storek 	sc->sc_clearing = 1;
102455138Storek 	(void) scsi_test_unit_ready(&sc->sc_hba, targ, 0);
102555138Storek 	sc->sc_clearing = 0;
102655138Storek }
102755138Storek 
102855138Storek /*
1029*65145Storek  * THIS SHOULD BE ADJUSTABLE
1030*65145Storek  */
1031*65145Storek 	/* name		howlong		purpose */
1032*65145Storek #define	SELECT_WAIT	300000		/* wait for select to complete */
1033*65145Storek #define	CMD_WAIT	100000		/* wait for next phase, generic */
1034*65145Storek #define	DATA_WAIT	100000		/* time to xfer data in/out */
1035*65145Storek 
1036*65145Storek /*
103755138Storek  * Send an `immediate' command, i.e., poll until the whole thing is done.
1038*65145Storek  * Return the status byte from the device, or -1 if we timed out.  We use
1039*65145Storek  * DMA to transfer the data as the fifo only moves one byte at a time.
104055138Storek  */
104155138Storek int
104255138Storek espicmd(hba, targ, cdb, buf, len, rw)
1043*65145Storek 	struct hba_softc *hba;
104455138Storek 	int targ;
1045*65145Storek 	struct scsi_cdb *cdb;
104655138Storek 	caddr_t buf;
1047*65145Storek 	int len, rw;
104855138Storek {
104955138Storek 	register struct esp_softc *sc = (struct esp_softc *)hba;
105055138Storek 	register volatile struct espreg *esp = sc->sc_esp;
105155138Storek 	register volatile struct dmareg *dma = sc->sc_dma;
105255138Storek 	register int r, wait;
105355138Storek 
105455138Storek 	/*
105555138Storek 	 * Clear the target if necessary.
105655138Storek 	 */
105755138Storek 	if (sc->sc_needclear & (1 << targ) && !sc->sc_probing)
105855138Storek 		espclear(sc, targ);
105955138Storek 
106055138Storek 	/*
1061*65145Storek 	 * Set up DMA transfer control (leaving interrupts disabled).
106255138Storek 	 */
1063*65145Storek 	sc->sc_dmactl = rw & B_READ ? DMA_ENA | DMA_READ : DMA_ENA;
1064*65145Storek 	sc->sc_dmaaddr = (u_long)buf;
1065*65145Storek 	sc->sc_resid = len;
1066*65145Storek 
1067*65145Storek 	/*
1068*65145Storek 	 * Disable hardware interrupts and start select sequence,
1069*65145Storek 	 * then loop, calling espact() after each ``interrupt''.
1070*65145Storek 	 */
1071*65145Storek 	DMAWAIT(dma);		/* ??? */
1072*65145Storek 	dma->dma_csr = 0;
1073*65145Storek 	espselect(sc, targ, cdb);
107455138Storek 	wait = SELECT_WAIT;
107555138Storek 	for (;;) {
107655138Storek 		r = dma->dma_csr;
107755138Storek 		if (!DMA_INTR(r)) {
107855138Storek 			if (--wait < 0) {
1079*65145Storek 				esperror(sc, "timeout");
1080*65145Storek 				goto reset;
108155138Storek 			}
108255138Storek 			DELAY(1);
108355138Storek 			continue;
108455138Storek 		}
1085*65145Storek 		sc->sc_espstat = esp->esp_stat;
1086*65145Storek 		sc->sc_espstep = esp->esp_step & ESPSTEP_MASK;
1087*65145Storek 		sc->sc_espintr = esp->esp_intr;
1088*65145Storek 		sc->sc_espfflags = esp->esp_fflags;
1089*65145Storek 		sc->sc_dmacsr = r;
1090*65145Storek 		switch (r = espact(sc)) {
109155138Storek 
109255138Storek 		case ACT_CONT:
109355138Storek 		case ACT_QUICKINTR:
1094*65145Storek 			wait = CMD_WAIT;
109555138Storek 			break;
109655138Storek 
1097*65145Storek 		case ACT_IO:
1098*65145Storek 			wait = DATA_WAIT;
109955138Storek 			break;
110055138Storek 
110155138Storek 		case ACT_RESET:
110255138Storek 			sc->sc_state = S_IDLE;
110355138Storek 			goto reset;
110455138Storek 
110555138Storek 		case ACT_DONE:
110655138Storek 			sc->sc_state = S_IDLE;
110755138Storek 			return (sc->sc_stat[0]);
110855138Storek 
110955138Storek 		case ACT_ERROR:
111055138Storek 			sc->sc_state = S_IDLE;
111155138Storek 			return (-1);
111255138Storek 
111355138Storek 		default:
111455138Storek 			panic("espicmd action");
111555138Storek 		}
111655138Storek 	}
111755138Storek reset:
1118*65145Storek 	espreset(sc, RESET_ESPCHIP);		/* ??? */
111955138Storek 	return (-1);
112055138Storek }
112155138Storek 
112255138Storek /*
112355138Storek  * Dump (write memory, possibly physmem).
112455138Storek  * SPARC higher-level dump code always provides virtual addresses,
112555138Storek  * so we need not do any I/O mapping here.
112655138Storek  */
112755138Storek int
112855138Storek espdump(hba, targ, cdb, buf, len)
112955138Storek 	register struct hba_softc *hba;
113055138Storek 	int targ;
1131*65145Storek 	struct scsi_cdb *cdb;
113255138Storek 	caddr_t buf;
113355138Storek 	register int len;
113455138Storek {
113555138Storek 
113655138Storek 	return (espicmd(hba, targ, cdb, buf, len, B_WRITE));
113755138Storek }
113855138Storek 
113955138Storek /*
114055138Storek  * Allocate resources (SCSI bus and DVMA space) for the given transfer.
114155138Storek  * Must be called at splbio().
114255138Storek  *
114355138Storek  * THIS SHOULD RETURN SUCCESS/FAIL INDICATION
114455138Storek  */
114555138Storek void
114655138Storek espstart(self, sq, bp, dgo, dev)
114755138Storek 	struct device *self;
114855138Storek 	register struct sq *sq;
114955138Storek 	struct buf *bp;
115055138Storek 	scdgo_fn dgo;
115155138Storek 	struct device *dev;
115255138Storek {
115355138Storek 	register struct esp_softc *sc = (struct esp_softc *)self;
115455138Storek 
115555138Storek 	if (sc->sc_hba.hba_busy == 0) {
115655138Storek 		/*
115755138Storek 		 * Bus not busy, nothing to do here, just tell
115855138Storek 		 * this target or unit that it has the SCSI bus.
115955138Storek 		 */
116055138Storek 		sc->sc_hba.hba_busy = 1;
1161*65145Storek 		(*dgo)(dev, &sc->sc_cdbspace);
116255138Storek 	} else {
116355138Storek 		/*
116455138Storek 		 * Bus is busy; just enqueue.
116555138Storek 		 */
116655138Storek 		sq->sq_dgo = dgo;
116755138Storek 		sq->sq_dev = dev;
116855138Storek 		sq->sq_forw = NULL;
116955138Storek 		if (sc->sc_hba.hba_head == NULL)
117055138Storek 			sc->sc_hba.hba_head = sq;
117155138Storek 		else
117255138Storek 			sc->sc_hba.hba_tail->sq_forw = sq;
117355138Storek 		sc->sc_hba.hba_tail = sq;
117455138Storek 	}
117555138Storek }
117655138Storek 
117755138Storek /*
1178*65145Storek  * Start buffered I/O.
117955138Storek  * Return 0 on success, 1 on failure.
118055138Storek  */
118155138Storek int
118255138Storek espgo(self, targ, intr, dev, bp, pad)
118355138Storek 	struct device *self;
118455138Storek 	int targ;
118555138Storek 	scintr_fn intr;
118655138Storek 	struct device *dev;
118755138Storek 	register struct buf *bp;
118855138Storek 	int pad;
118955138Storek {
119055138Storek 	register struct esp_softc *sc = (struct esp_softc *)self;
119155138Storek 
119255138Storek 	if (sc->sc_needclear & (1 << targ))
119355138Storek 		espclear(sc, targ);
119455138Storek 
1195*65145Storek 	/* Set up dma control for espact(). */
1196*65145Storek 	sc->sc_dmactl = bp->b_flags & B_READ ?
1197*65145Storek 	    DMA_ENA | DMA_READ | DMA_IE : DMA_ENA | DMA_IE;
1198*65145Storek 	sc->sc_dmaaddr = (u_long)bp->b_un.b_addr;
1199*65145Storek 	sc->sc_resid = bp->b_bcount;
120055138Storek 
120155138Storek 	/*
120255138Storek 	 * Enable interrupts and start selection.
1203*65145Storek 	 * The rest is done in espintr() and espact().
120455138Storek 	 */
120555138Storek 	sc->sc_hba.hba_intr = intr;	/* remember dev done function */
120655138Storek 	sc->sc_hba.hba_intrdev = dev;	/* and its first arg */
120755138Storek 	sc->sc_dma->dma_csr = DMA_IE;
1208*65145Storek 	espselect(sc, targ, &sc->sc_cdbspace);
120955138Storek 	return (0);
121055138Storek }
121155138Storek 
121255138Storek /*
121355138Storek  * Handle interrupt.  Return 1 if taken.
121455138Storek  */
121555138Storek int
121655138Storek espintr(sc0)
121755138Storek 	void *sc0;
121855138Storek {
121955138Storek 	register struct esp_softc *sc = (struct esp_softc *)sc0;
122055138Storek 	register volatile struct espreg *esp = sc->sc_esp;
122155138Storek 	register volatile struct dmareg *dma = sc->sc_dma;
122255138Storek 	register int r, wait;
122355138Storek 	register struct sq *sq;
122455138Storek 
122555138Storek 	r = dma->dma_csr;
122655138Storek 	if (!DMA_INTR(r))
122755138Storek 		return (0);		/* not ours */
122859214Storek 	sc->sc_intrcnt.ev_count++;
122955138Storek 
123055138Storek again:
123155138Storek 	sc->sc_espstat = esp->esp_stat;
123255138Storek 	sc->sc_espstep = esp->esp_step & ESPSTEP_MASK;
123355138Storek 	sc->sc_espintr = esp->esp_intr;
1234*65145Storek 	sc->sc_espfflags = esp->esp_fflags;
123555138Storek 	sc->sc_dmacsr = r;
123655138Storek 
123755138Storek 	if (sc->sc_state == S_IDLE) {
1238*65145Storek 		printf("%s: stray interrupt\n", sc->sc_dev.dv_xname);
123955138Storek 		dma->dma_csr &= ~DMA_IE;	/* ??? */
124055138Storek 		return (1);
124155138Storek 	}
1242*65145Storek 	switch (r = espact(sc)) {
124355138Storek 
124455138Storek 	case ACT_CONT:		/* just return */
1245*65145Storek 	case ACT_IO:
124655138Storek 		break;
124755138Storek 
124855138Storek 	case ACT_RESET:		/* please reset esp */
124955138Storek reset:
1250*65145Storek 		espreset(sc, RESET_ESPCHIP);	/* ??? */
125155138Storek 		/* FALLTHROUGH */
125255138Storek 
125355138Storek 	case ACT_DONE:		/* this one is done, successfully */
125455138Storek 	case ACT_ERROR:		/* this one is done due to `severe' error */
125555138Storek 		sc->sc_state = S_IDLE;
125655138Storek 		if (!sc->sc_hba.hba_busy)
125755138Storek 			panic("espintr sq");
125855138Storek 		/*
125955138Storek 		 * This transaction is done.
126055138Storek 		 * Call the driver's intr routine,
126155138Storek 		 * then start the next guy if any.
126255138Storek 		 */
126355138Storek 		(*sc->sc_hba.hba_intr)(sc->sc_hba.hba_intrdev,
126455138Storek 		    r == ACT_DONE ? sc->sc_stat[0] : -1, sc->sc_resid);
126555138Storek 		if ((sq = sc->sc_hba.hba_head) != NULL) {
126655138Storek 			sc->sc_hba.hba_head = sq->sq_forw;
1267*65145Storek 			(*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace);
126855138Storek 		} else
126955138Storek 			sc->sc_hba.hba_busy = 0;
127055138Storek 		break;
127155138Storek 
127255138Storek 	case ACT_QUICKINTR:	/* wait a short while for another interrupt */
1273*65145Storek printf("%s: quickintr: ", sc->sc_dev.dv_xname);
127455138Storek 		wait = 100;
127555138Storek 		do {
127655138Storek 			r = dma->dma_csr;
127755138Storek 			if (DMA_INTR(r)) {
127855138Storek printf("got one, wait=%d\n", wait);
127955138Storek 				goto again;
128055138Storek 			}
128155138Storek 		} while (--wait > 0);
128255138Storek printf("did not get one\n");
128355138Storek 		break;
128455138Storek 
128555138Storek 	default:
128655138Storek 		panic("espintr action");
128755138Storek 	}
128855138Storek 	return (1);
128955138Storek }
129055138Storek 
129155138Storek /*
129255138Storek  * Target or unit decided to let go of the bus early.
129355138Storek  */
129455138Storek void
129555138Storek esprel(self)
129655138Storek 	struct device *self;
129755138Storek {
129855138Storek 	register struct esp_softc *sc = (struct esp_softc *)self;
129955138Storek 	register struct sq *sq;
130055138Storek 
130155138Storek 	/* if there is someone else waiting, give them a crack at it */
130255138Storek 	if ((sq = sc->sc_hba.hba_head) != NULL)
1303*65145Storek 		(*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace);
130455138Storek 	else
130555138Storek 		sc->sc_hba.hba_busy = 0;
130655138Storek }
1307