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*67100Storek * @(#)esp.c 8.4 (Berkeley) 05/06/94
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
6365145Storek #include <libkern/libkern.h>
6455138Storek
6555138Storek /*
6665145Storek * This driver is largely a giant state machine:
6755138Storek *
6865145Storek * Given some previous SCSI state (as set up or tracked by us
6965145Storek * earlier) and the interrupt registers provided on the chips
7065145Storek * (dmacsr, espstat, espstep, and espintr), derive an action.
7165145Storek * In many cases this is just a matter of reading the target's
7265145Storek * 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 *
7665145Storek * Data transfer is always done via DMA. Unfortunately, there are
7765145Storek * limits in the DMA and ESP chips on how much data can be moved
7865145Storek * in a single operation. The ESP chip has a 16-bit counter, so
7965145Storek * it is limited to 65536 bytes. More insidiously, while the DMA
8065145Storek * chip has a 32-bit address, this is composed of a 24-bit counter
8165145Storek * with an 8-bit latch, so it cannot cross a 16 MB boundary. To
8265145Storek * handle these, we program a smaller count than our caller requests;
8365145Storek * when this shorter transfer is done, if the target is still up
8465145Storek * for data transfer, we simply keep going (updating the DMA address)
8565145Storek * as needed.
8655138Storek */
8755138Storek
8855138Storek /* per-DMA variables */
8955138Storek struct dma_softc {
9065145Storek struct device dc_dev; /* base device */
9165145Storek volatile struct dmareg *dc_dma; /* register virtual address */
9265145Storek int dc_dmarev; /* revision */
9365145Storek char *dc_dmafmt; /* format for error messages */
9455138Storek };
9555138Storek void dmaattach(struct device *, struct device *, void *);
9655138Storek struct cfdriver dmacd =
9755138Storek { NULL, "dma", matchbyname, dmaattach, DV_DULL, sizeof(struct dma_softc) };
9855138Storek
9955138Storek /* per-ESP variables */
10055138Storek struct esp_softc {
10155138Storek /*
10255138Storek * External interfaces.
10355138Storek */
10455138Storek struct hba_softc sc_hba; /* base device + hba, must be first */
10565145Storek #define sc_dev sc_hba.hba_dev
10655138Storek struct sbusdev sc_sd; /* sbus device */
10755138Storek struct intrhand sc_ih; /* interrupt entry */
10859214Storek struct evcnt sc_intrcnt; /* interrupt counter */
10965145Storek struct dma_softc *sc_dc; /* pointer to corresponding dma sc */
11055138Storek
11155138Storek /*
11255138Storek * Addresses mapped to hardware registers.
11355138Storek */
11455138Storek volatile struct espreg *sc_esp;
11555138Storek volatile struct dmareg *sc_dma;
11655138Storek
11755138Storek /*
11855138Storek * Copies of registers cleared/unlatched by reading.
11965145Storek * (FIFO flags is not cleared, but we want it for debugging.)
12055138Storek */
12155138Storek u_long sc_dmacsr;
12255138Storek u_char sc_espstat;
12355138Storek u_char sc_espstep;
12455138Storek u_char sc_espintr;
12565145Storek u_char sc_espfflags;
12655138Storek
12755138Storek /* miscellaneous */
12855138Storek int sc_clockfreq; /* clock frequency */
12955138Storek u_char sc_sel_timeout; /* select timeout */
13055138Storek u_char sc_id; /* initiator ID (default = 7) */
13155138Storek u_char sc_esptype; /* 100, 100A, 2xx (see below) */
13255138Storek u_char sc_ccf; /* clock conversion factor */
13355138Storek u_char sc_conf1; /* value for config reg 1 */
13455138Storek u_char sc_conf2; /* value for config reg 2 */
13555138Storek u_char sc_conf3; /* value for config reg 3 */
13659214Storek struct bootpath *sc_bp; /* esp bootpath so far */
13755138Storek
13855138Storek /*
13955138Storek * Information pertaining to the current transfer,
14055138Storek * including sequencing.
14155138Storek *
14255138Storek * The size of sc_msg is the size of the ESP fifo,
14355138Storek * since we do message-in simply by allowing the fifo to fill.
14455138Storek */
14555138Storek char sc_probing; /* used during autoconf; see below */
14667079Storek char sc_iwant; /* true => icmd needs wakeup on idle */
14755138Storek char sc_state; /* SCSI protocol state; see below */
14855138Storek char sc_sentcmd; /* set once we get cmd out */
14955138Storek char sc_dmaactive; /* true => doing dma */
15055138Storek #ifdef notyet
15155138Storek u_char sc_sync; /* synchronous transfer stuff (?) */
15255138Storek #endif
15355138Storek u_char sc_stat[2]; /* status from last `status' phase */
15455138Storek u_char sc_msg[16]; /* message from device */
15555138Storek u_short sc_dmactl; /* control to load into dma csr */
15665145Storek u_long sc_dmaaddr; /* address for next xfer */
15765145Storek int sc_dmasize; /* size of current xfer */
15865145Storek int sc_resid; /* count of bytes not yet xferred */
15955138Storek int sc_targ; /* the target involved */
16065145Storek struct scsi_cdb *sc_curcdb; /* ptr to current command */
16165145Storek /* might cdbspace eventually be per-target? */
16265145Storek struct scsi_cdb sc_cdbspace; /* space for one command */
16355138Storek };
16455138Storek
16555138Storek /*
16665145Storek * Values for sc_esptype (used to control configuration reset, and for
16765145Storek * workarounds for chip bugs). The order is important; see espreset().
16855138Storek */
16955138Storek #define ESP100 0
17055138Storek #define ESP100A 1
17155138Storek #define ESP2XX 2
17255138Storek
17355138Storek /*
17455138Storek * Probe state. 0 means not probing. While looking for each target
17555138Storek * we set this to PROBE_TESTING and do a TEST UNIT READY on unit 0.
17655138Storek * If selection fails, this is changed to PROBE_NO_TARGET; otherwise
17755138Storek * we assume the target exists, regardless of the result of the test.
17855138Storek */
17955138Storek #define PROBE_TESTING 1
18055138Storek #define PROBE_NO_TARGET 2
18155138Storek
18255138Storek /*
18355138Storek * States in sc_state.
18455138Storek *
18565145Storek * Note that S_SVC is rare: normally we load the SCSI command into the
18655138Storek * ESP fifo and get interrupted only when the device has gone to data
18755138Storek * or status phase. If the device wants to play games, though, we end
18855138Storek * up doing things differently.
18955138Storek */
19055138Storek char *espstates[] = {
19155138Storek #define S_IDLE 0 /* not doing anything */
19255138Storek "idle",
19355138Storek #define S_SEL 1 /* expecting select done interrupt */
19455138Storek "selecting",
19565145Storek #define S_SVC 2 /* expecting service req interrupt */
19665145Storek "waiting for svc req",
19765145Storek #define S_DI 3 /* expecting data-in done interrupt */
19855138Storek "receiving data",
19965145Storek #define S_DO 4 /* expecting data-out done interrupt */
20055138Storek "sending data",
20165145Storek #define S_STAT 5 /* expecting status done interrupt */
20255138Storek "receiving status",
20365145Storek #define S_MI 6 /* expecting message-in done interrupt */
20455138Storek "receiving message",
20565145Storek #define S_FI 7 /* expecting final disconnect interrupt */
20655138Storek "waiting for disconnect"
20755138Storek };
20855138Storek
20955138Storek /*
21065145Storek * Hardware limits on transfer sizes (see comments at top).
21165145Storek */
21265145Storek #define ESPMAX (64 * 1024)
21365145Storek #define DMAMAX(a) (0x01000000 - ((a) & 0x00ffffff))
21465145Storek
21565145Storek /*
21655138Storek * Return values from espact().
21755138Storek */
21855138Storek #define ACT_CONT 0 /* espact() handled everything */
21965145Storek #define ACT_IO 1 /* espact() is xferring data */
22065145Storek #define ACT_DONE 2 /* handled everything, and op is now done */
22165145Storek #define ACT_ERROR 3 /* an error occurred, op has been trashed */
22265145Storek #define ACT_RESET 4 /* please reset ESP, then do ACT_ERROR */
22365145Storek #define ACT_QUICKINTR 5 /* another interrupt is expected immediately */
22455138Storek
22555138Storek /* autoconfiguration driver */
22655138Storek void espattach(struct device *, struct device *, void *);
22755138Storek struct cfdriver espcd =
228*67100Storek { NULL, "esp", matchbyname, espattach, DV_DULL, sizeof(struct esp_softc) };
22955138Storek
23055138Storek /* Sbus driver */
23155138Storek void espsbreset(struct device *);
23255138Storek
23355138Storek /* interrupt interface */
23455138Storek int espintr(void *);
23555138Storek
23655138Storek /* SCSI HBA driver */
23755138Storek int espicmd(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int, int);
23855138Storek int espdump(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int);
23955138Storek void espstart(struct device *, struct sq *, struct buf *,
24055138Storek scdgo_fn, struct device *);
24155138Storek int espgo(struct device *, int, scintr_fn, struct device *,
24255138Storek struct buf *, int);
24355138Storek void esprel(struct device *);
24455138Storek void esphbareset(struct hba_softc *, int);
24555138Storek static struct hbadriver esphbadriver =
24655138Storek { espicmd, espdump, espstart, espgo, esprel, esphbareset };
24755138Storek
24865145Storek /* other prototypes */
24965145Storek static void espdoattach(int);
25065145Storek static void dmareset(struct esp_softc *);
25165145Storek static void espreset(struct esp_softc *, int);
25265145Storek static void esperror(struct esp_softc *, const char *);
25365145Storek static int espact(struct esp_softc *);
25465145Storek void espselect(struct esp_softc *, int, struct scsi_cdb *);
25555138Storek
25665145Storek /* second arg to espreset() */
25765145Storek #define RESET_ESPCHIP 0x1
25865145Storek #define RESET_SCSIBUS 0x2
25965145Storek #define RESET_BOTH (RESET_ESPCHIP | RESET_SCSIBUS)
26055138Storek
26155138Storek /*
26255138Storek * Attach a found DMA chip.
26355138Storek * The second argument is really a pointer to an sbus_attach_args.
26455138Storek */
26555138Storek void
dmaattach(parent,dev,args)26655138Storek dmaattach(parent, dev, args)
26755138Storek struct device *parent;
26855138Storek struct device *dev;
26955138Storek void *args;
27055138Storek {
27165145Storek register struct dma_softc *dc = (struct dma_softc *)dev;
27255138Storek register struct sbus_attach_args *sa = args;
27355138Storek register volatile struct dmareg *dma;
27455138Storek register int rev;
27555138Storek struct esp_softc *esc;
27655138Storek
27755138Storek if (sa->sa_ra.ra_vaddr)
27855138Storek dma = (volatile struct dmareg *)sa->sa_ra.ra_vaddr;
27955138Storek else
28055138Storek dma = (volatile struct dmareg *)
28155138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct dmareg));
28265145Storek dc->dc_dma = dma;
28355138Storek
28455138Storek switch (rev = DMA_REV(dma->dma_csr)) {
28555138Storek case DMAREV_1:
28655138Storek printf(": rev 1\n");
28765145Storek dc->dc_dmafmt = DMA_REV1_BITS;
28855138Storek break;
28955138Storek case DMAREV_2:
29055138Storek printf(": rev 2\n");
29165145Storek dc->dc_dmafmt = DMA_REV2_BITS;
29255138Storek break;
29359327Storek case DMAREV_3:
29459327Storek printf(": rev 3\n");
29559327Storek printf("WARNING: esp.c not yet updated for rev 3\n");
29665145Storek dc->dc_dmafmt = DMA_REV3_BITS;
29759327Storek break;
29855138Storek default:
29959327Storek printf(": unknown revision code 0x%x\n", rev);
30065145Storek dc->dc_dmafmt = DMA_REV3_BITS; /* cross fingers */
30155138Storek break;
30255138Storek }
30365145Storek dc->dc_dmarev = rev;
30465145Storek espdoattach(dc->dc_dev.dv_unit);
30555138Storek }
30655138Storek
30755138Storek /*
30855138Storek * Attach a found ESP chip. Search for targets; attach each one found.
30955138Storek * The latter must be deferred if the corresponding dma chip has not yet
31055138Storek * been configured.
31155138Storek */
31255138Storek void
espattach(parent,self,args)31355138Storek espattach(parent, self, args)
31455138Storek struct device *parent;
31555138Storek struct device *self;
31655138Storek void *args;
31755138Storek {
31855138Storek register struct esp_softc *sc = (struct esp_softc *)self;
31955138Storek register struct sbus_attach_args *sa = args;
32055138Storek register volatile struct espreg *esp;
32159214Storek register struct bootpath *bp;
32255138Storek int node, pri, freq, t;
32355138Storek
32455138Storek if (sa->sa_ra.ra_nintr != 1) {
32555138Storek printf(": expected 1 interrupt, got %d\n", sa->sa_ra.ra_nintr);
32655138Storek return;
32755138Storek }
32855138Storek pri = sa->sa_ra.ra_intr[0].int_pri;
32955138Storek printf(" pri %d", pri);
33055138Storek if (sa->sa_ra.ra_vaddr)
33155138Storek esp = (volatile struct espreg *)sa->sa_ra.ra_vaddr;
33255138Storek else
33355138Storek esp = (volatile struct espreg *)
33455138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct espreg));
33555138Storek sc->sc_esp = esp;
33655138Storek node = sa->sa_ra.ra_node;
33755138Storek sc->sc_id = getpropint(node, "initiator-id", 7);
33855138Storek freq = getpropint(node, "clock-frequency", -1);
33955138Storek if (freq < 0)
34065145Storek freq =
34165145Storek ((struct sbus_softc *)sc->sc_dev.dv_parent)->sc_clockfreq;
34255138Storek
34355138Storek /* MIGHT NEED TO RESET ESP CHIP HERE ...? */
34455138Storek
34555138Storek /*
34655138Storek * Find out whether we have a -100, -100A, or -2xx,
34755138Storek * and what speed it runs at.
34855138Storek */
34955138Storek sc->sc_conf1 = sc->sc_id | ESPCONF1_PARENB;
35055138Storek /* sc->sc_conf2 = 0; */
35155138Storek /* sc->sc_conf3 = 0; */
35255138Storek esp->esp_conf1 = sc->sc_conf1;
35355138Storek esp->esp_conf2 = 0;
35455138Storek esp->esp_conf2 = ESPCONF2_SCSI2 | ESPCONF2_RPE;
35555138Storek if ((esp->esp_conf2 & ~ESPCONF2_RSVD) !=
35655138Storek (ESPCONF2_SCSI2 | ESPCONF2_RPE)) {
35755138Storek printf(": ESP100");
35855138Storek sc->sc_esptype = ESP100;
35955138Storek } else {
36055138Storek esp->esp_conf2 = 0;
36155138Storek esp->esp_conf3 = 0;
36255138Storek esp->esp_conf3 = 5;
36355138Storek if (esp->esp_conf3 != 5) { /* XXX def bits */
36455138Storek printf(": ESP100A");
36555138Storek sc->sc_esptype = ESP100A;
36655138Storek } else {
36755138Storek esp->esp_conf3 = 0;
36855138Storek printf(": ESP2XX");
36955138Storek sc->sc_esptype = ESP2XX;
37055138Storek }
37155138Storek }
37255138Storek printf(", clock = %s MHz, ID = %d\n", clockfreq(freq), sc->sc_id);
37355138Storek
37455138Storek /*
37555138Storek * Set clock conversion factor and select timeout.
37655138Storek * N.B.: clock frequency is not actually used in the rest
37755138Storek * of the driver; I calculate it here for completeness only
37855138Storek * (so I can see it when debugging).
37955138Storek */
38055138Storek sc->sc_clockfreq = freq;
38155138Storek freq = howmany(freq, 1000 * 1000); /* convert to MHz */
38255138Storek t = ESPCCF_FROMMHZ(freq);
38355138Storek if (t < ESPCCF_MIN)
38455138Storek t = ESPCCF_MIN;
38555138Storek sc->sc_ccf = t;
38655138Storek t = ESPTIMO_REGVAL(250, t, freq); /* timeout = 250 ms. */
38755138Storek if (t >= 256)
38855138Storek t = 0;
38955138Storek sc->sc_sel_timeout = t;
39055138Storek
39155138Storek /*
39255138Storek * Link into sbus; set interrupt handler.
39355138Storek */
39455138Storek sc->sc_sd.sd_reset = espsbreset;
39565145Storek sbus_establish(&sc->sc_sd, &sc->sc_dev);
39655138Storek sc->sc_ih.ih_fun = espintr;
39755138Storek sc->sc_ih.ih_arg = sc;
39855138Storek intr_establish(pri, &sc->sc_ih);
39965145Storek evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
40059214Storek
40159214Storek #define SAME_ESP(bp, sa) \
40259214Storek ((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
40365145Storek (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
40459214Storek
40559214Storek bp = sa->sa_ra.ra_bp;
40659214Storek if (bp != NULL && strcmp(bp->name, "esp") == 0 && SAME_ESP(bp, sa))
40759214Storek sc->sc_bp = bp + 1;
40865145Storek espdoattach(sc->sc_dev.dv_unit);
40955138Storek }
41055138Storek
41155138Storek /*
41255138Storek * `Final' attach of esp occurs once esp and dma chips have been found
41355138Storek * and assigned virtual addresses. Set up the ESP SCSI data structures
41455138Storek * and probe the SCSI bus.
41555138Storek */
41655138Storek static void
espdoattach(unit)41755138Storek espdoattach(unit)
41855138Storek int unit;
41955138Storek {
42055138Storek register struct esp_softc *sc;
42165145Storek register struct dma_softc *dc;
42259214Storek register struct bootpath *bp;
42359214Storek register struct targ *t;
42459214Storek register int targ, u;
42555138Storek
42655138Storek /* make sure we have both */
42755138Storek if (espcd.cd_ndevs <= unit ||
42855138Storek dmacd.cd_ndevs <= unit ||
42955138Storek (sc = espcd.cd_devs[unit]) == NULL ||
43065145Storek (dc = dmacd.cd_devs[unit]) == NULL)
43155138Storek return;
43265145Storek sc->sc_dc = dc;
43365145Storek sc->sc_dma = dc->dc_dma;
43455138Storek sc->sc_hba.hba_driver = &esphbadriver;
43555138Storek
43665145Storek sc->sc_dma->dma_csr = 0; /* ??? */
43765145Storek espreset(sc, RESET_ESPCHIP);
43855138Storek
43955138Storek /* MAYBE THIS SHOULD BE MOVED TO scsi_subr.c? */
44055138Storek for (targ = 0; targ < 8; targ++) {
44155138Storek if (targ == sc->sc_id)
44255138Storek continue;
44355138Storek sc->sc_probing = PROBE_TESTING;
44465145Storek (void)scsi_test_unit_ready(&sc->sc_hba, targ, 0);
44555138Storek if (sc->sc_probing != PROBE_NO_TARGET) {
44655138Storek sc->sc_probing = 0;
44755138Storek SCSI_FOUNDTARGET(&sc->sc_hba, targ);
44855138Storek }
44955138Storek }
45055138Storek sc->sc_probing = 0;
45159214Storek
45259214Storek /*
45365145Storek * See if we booted from a unit on this target. We could
45465145Storek * compare bp->name against the unit's name but there's no
45565145Storek * real need since a target and unit uniquely specify a
45665145Storek * scsi device.
45759214Storek */
45865145Storek if ((bp = sc->sc_bp) != NULL && (u_int)(targ = bp->val[0]) < 8 &&
45965145Storek (u_int)(u = bp->val[1]) < 8 &&
46065145Storek (t = sc->sc_hba.hba_targets[targ]) != NULL && t->t_units[u] != NULL)
46159214Storek bootdv = t->t_units[u]->u_dev;
46255138Storek }
46355138Storek
46455138Storek /*
46565145Storek * We are not allowed to touch the DMA "flush" and "drain" bits
46665145Storek * while it is still thinking about a request (DMA_RP).
46755138Storek */
46865145Storek #define DMAWAIT(dma) while ((dma)->dma_csr & DMA_RP) DELAY(1)
46967079Storek #define DMAWAIT1(dma) while ((dma)->dma_csr & DMA_PC) DELAY(1)
47065145Storek
47165145Storek /*
47265145Storek * Reset the DMA chip.
47365145Storek */
47455138Storek static void
dmareset(sc)47555138Storek dmareset(sc)
47655138Storek struct esp_softc *sc;
47755138Storek {
47855138Storek register volatile struct dmareg *dma = sc->sc_dma;
47955138Storek
48065145Storek DMAWAIT(dma);
48155138Storek dma->dma_csr |= DMA_RESET;
48255138Storek DELAY(200);
48355138Storek dma->dma_csr &= ~DMA_RESET; /* ??? */
48455138Storek sc->sc_dmaactive = 0;
48565145Storek if (sc->sc_dc->dc_dmarev == DMAREV_2 && sc->sc_esptype != ESP100)
48659327Storek dma->dma_csr |= DMA_TURBO;
48755138Storek dma->dma_csr |= DMA_IE; /* enable interrupts */
48855138Storek DELAY(200);
48955138Storek }
49055138Storek
49155138Storek /*
49265145Storek * Reset the chip and/or SCSI bus (always resets DMA).
49355138Storek */
49455138Storek static void
espreset(sc,how)49565145Storek espreset(sc, how)
49655138Storek register struct esp_softc *sc;
49765145Storek int how;
49855138Storek {
49955138Storek register volatile struct espreg *esp = sc->sc_esp;
50055138Storek
50155138Storek dmareset(sc);
50265145Storek if (how & RESET_ESPCHIP) {
50365145Storek esp->esp_cmd = ESPCMD_RESET_CHIP;
50465145Storek esp->esp_cmd = ESPCMD_NOP;
50565145Storek /*
50665145Storek * Reload configuration registers (cleared by
50765145Storek * RESET_CHIP command). Reloading conf2 on an
50865145Storek * ESP100 goofs it up, so out of paranoia we load
50965145Storek * only the registers that exist.
51065145Storek */
51165145Storek esp->esp_conf1 = sc->sc_conf1;
51265145Storek if (sc->sc_esptype > ESP100) { /* 100A, 2XX */
51365145Storek esp->esp_conf2 = sc->sc_conf2;
51465145Storek if (sc->sc_esptype > ESP100A) /* 2XX only */
51565145Storek esp->esp_conf3 = sc->sc_conf3;
51665145Storek }
51765145Storek esp->esp_ccf = sc->sc_ccf;
51865145Storek esp->esp_timeout = sc->sc_sel_timeout;
51965145Storek /* We set synch offset later. */
52055138Storek }
52165145Storek if (how & RESET_SCSIBUS) {
52265145Storek /*
52365145Storek * The chip should retain most of its parameters
52465145Storek * (including esp_ccf) across this kind of reset
52565145Storek * (see section 3.5 of Emulex documentation).
52665145Storek */
52765145Storek /* turn off scsi bus reset interrupts and reset scsi bus */
52865145Storek esp->esp_conf1 = sc->sc_conf1 | ESPCONF1_REPORT;
52965145Storek esp->esp_cmd = ESPCMD_RESET_BUS;
53065145Storek esp->esp_cmd = ESPCMD_NOP;
53165145Storek DELAY(100000); /* ??? */
53265145Storek (void)esp->esp_intr;
53365145Storek esp->esp_conf1 = sc->sc_conf1;
53465145Storek }
53567079Storek sc->sc_state = S_IDLE;
53655138Storek }
53755138Storek
53855138Storek /*
53955138Storek * Reset the SCSI bus and, optionally, all attached targets.
54055138Storek */
54155138Storek void
esphbareset(hba,resetunits)54255138Storek esphbareset(hba, resetunits)
54355138Storek struct hba_softc *hba;
54455138Storek int resetunits;
54555138Storek {
54655138Storek register struct esp_softc *sc = (struct esp_softc *)hba;
54755138Storek
54865145Storek espreset(sc, RESET_SCSIBUS);
54955138Storek if (resetunits)
55055138Storek scsi_reset_units(&sc->sc_hba);
55155138Storek }
55255138Storek
55355138Storek /*
55455138Storek * Reset the esp, after an Sbus reset.
55555138Storek * Also resets corresponding dma chip.
55655138Storek *
55755138Storek * THIS ROUTINE MIGHT GO AWAY
55855138Storek */
55955138Storek void
espsbreset(dev)56055138Storek espsbreset(dev)
56155138Storek struct device *dev;
56255138Storek {
56355138Storek struct esp_softc *sc = (struct esp_softc *)dev;
56455138Storek
56565145Storek if (sc->sc_dc) {
56665145Storek printf(" %s %s", sc->sc_dc->dc_dev.dv_xname,
56765145Storek sc->sc_dev.dv_xname);
56855138Storek esphbareset(&sc->sc_hba, 1);
56955138Storek }
57055138Storek }
57155138Storek
57265145Storek /*
57365145Storek * Log an error.
57465145Storek */
57555138Storek static void
esperror(sc,err)57655138Storek esperror(sc, err)
57755138Storek register struct esp_softc *sc;
57865145Storek const char *err;
57955138Storek {
58065145Storek int stat;
58155138Storek
58265145Storek stat = sc->sc_espstat;
58365145Storek printf(
58465145Storek "%s target %d cmd 0x%x (%s): %s:\n\
58565145Storek \tstat=%b (%s) step=%x dmacsr=%b fflags=%x intr=%b\n",
58665145Storek sc->sc_dev.dv_xname, sc->sc_targ, sc->sc_curcdb->cdb_bytes[0],
58765145Storek espstates[sc->sc_state], err,
58865145Storek stat, ESPSTAT_BITS, espphases[stat & ESPSTAT_PHASE],
58965145Storek sc->sc_espstep, sc->sc_dmacsr, sc->sc_dc->dc_dmafmt,
59065145Storek sc->sc_espfflags, sc->sc_espintr, ESPINTR_BITS);
59155138Storek }
59255138Storek
59355138Storek /*
59465145Storek * Issue a select, loading command into the FIFO.
59565145Storek * Return nonzero on error, 0 if OK.
59665145Storek * Sets state to `selecting'; espact() will sequence state FSM.
59765145Storek */
59865145Storek void
espselect(sc,targ,cdb)59965145Storek espselect(sc, targ, cdb)
60065145Storek register struct esp_softc *sc;
60165145Storek register int targ;
60265145Storek register struct scsi_cdb *cdb;
60365145Storek {
60465145Storek register volatile struct espreg *esp;
60565145Storek register int i, cmdlen;
60665145Storek
60765145Storek sc->sc_targ = targ;
60865145Storek sc->sc_state = S_SEL;
60965145Storek sc->sc_curcdb = cdb;
61065145Storek sc->sc_sentcmd = 0;
61165145Storek sc->sc_stat[0] = 0xff; /* ??? */
61265145Storek sc->sc_msg[0] = 0xff; /* ??? */
61365145Storek
61465145Storek /*
61565145Storek * Try to talk to target.
61665145Storek * Synch offset 0 => asynchronous transfer.
61765145Storek */
61865145Storek esp = sc->sc_esp;
61965145Storek esp->esp_id = targ;
62065145Storek esp->esp_syncoff = 0;
62165145Storek
62265145Storek /*
62365145Storek * Stuff the command bytes into the fifo.
62465145Storek * Select without attention since we do not do disconnect yet.
62565145Storek */
62665145Storek cmdlen = SCSICMDLEN(cdb->cdb_bytes[0]);
62765145Storek for (i = 0; i < cmdlen; i++)
62865145Storek esp->esp_fifo = cdb->cdb_bytes[i];
62965145Storek esp->esp_cmd = ESPCMD_SEL_NATN;
63065145Storek /* the rest is done elsewhere */
63165145Storek }
63265145Storek
63365145Storek /*
63465145Storek * Sequence through the SCSI state machine. Return the action to take.
63555138Storek *
63655138Storek * Most of the work happens here.
63755138Storek *
63855138Storek * There are three interrupt sources:
63955138Storek * -- ESP interrupt request (typically, some device wants something).
64055138Storek * -- DMA memory error.
64155138Storek * -- DMA byte count has reached 0 (we do not often want this one but
64255138Storek * can only turn it off in rev 2 DMA chips, it seems).
64355138Storek * DOES THIS OCCUR AT ALL HERE? THERE IS NOTHING TO HANDLE IT!
64455138Storek */
64555138Storek static int
espact(sc)64665145Storek espact(sc)
64755138Storek register struct esp_softc *sc;
64865145Storek {
64955138Storek register volatile struct espreg *esp;
65055138Storek register volatile struct dmareg *dma;
65165145Storek register int reg, i, resid, newstate;
65255138Storek register struct scsi_cdb *cdb;
65355138Storek
65465145Storek dma = sc->sc_dma;
65555138Storek /* check various error conditions, using as little code as possible */
65655138Storek if (sc->sc_dmacsr & DMA_EP) {
65755138Storek esperror(sc, "DMA error");
65865145Storek DMAWAIT(dma);
65955138Storek dma->dma_csr |= DMA_FLUSH;
66067079Storek DMAWAIT1(dma);
66155138Storek return (ACT_ERROR);
66255138Storek }
66355138Storek reg = sc->sc_espstat;
66455138Storek if (reg & ESPSTAT_GE) {
66555138Storek /*
66655138Storek * This often occurs when there is no target.
66755138Storek * (See DSC code below.)
66855138Storek */
66955138Storek if (sc->sc_espintr & ESPINTR_DSC &&
67055138Storek sc->sc_state == S_SEL && sc->sc_probing) {
67155138Storek sc->sc_probing = PROBE_NO_TARGET;
67255138Storek return (ACT_RESET);
67355138Storek }
67465145Storek esperror(sc, "DIAG: gross error (ignored)");
67555138Storek }
67655138Storek if (reg & ESPSTAT_PE) {
67755138Storek esperror(sc, "parity error");
67855138Storek return (ACT_RESET);
67955138Storek }
68055138Storek reg = sc->sc_espintr;
68155138Storek #define ERR (ESPINTR_SBR|ESPINTR_ILC|ESPINTR_RSL|ESPINTR_SAT|ESPINTR_SEL)
68255138Storek if (reg & ERR) {
68355138Storek if (reg & ESPINTR_SBR)
68455138Storek esperror(sc, "scsi bus reset");
68555138Storek else if (reg & ESPINTR_ILC)
68655138Storek esperror(sc, "illegal command (driver bug)");
68755138Storek else {
68865145Storek printf("%s: target %d", sc->sc_dev.dv_xname,
68965145Storek sc->sc_targ);
69055138Storek if (reg & ESPINTR_RSL)
69155138Storek printf(" tried to reselect;");
69255138Storek if (reg & ESPINTR_SAT)
69355138Storek printf(" selected with ATN;");
69455138Storek if (reg & ESPINTR_SEL)
69555138Storek printf(" selected us as target;");
69655138Storek printf("we do not allow this yet\n");
69755138Storek }
69855138Storek return (ACT_ERROR);
69955138Storek }
70055138Storek #undef ERR
70155138Storek
70265145Storek esp = sc->sc_esp;
70365145Storek
70455138Storek /*
70555138Storek * Disconnect currently only allowed in `final interrupt' states.
70655138Storek */
70755138Storek if (reg & ESPINTR_DSC) {
70855138Storek if (sc->sc_state == S_FI)
70955138Storek return (ACT_DONE);
71055138Storek /*
71155138Storek * If we were doing a select just to test the existence
71255138Storek * of the target, note that it did not respond; otherwise
71355138Storek * gripe.
71455138Storek */
71555138Storek if (sc->sc_state == S_SEL) {
71655138Storek if (sc->sc_probing) {
71755138Storek sc->sc_probing = PROBE_NO_TARGET;
71855138Storek return (ACT_RESET);
71955138Storek }
72055138Storek }
72155138Storek /* flush fifo, in case we were selecting or sending data */
72255138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO;
72365145Storek DELAY(1);
72455138Storek printf("%s: target %d not responding\n",
72565145Storek sc->sc_dev.dv_xname, sc->sc_targ);
72655138Storek return (ACT_ERROR);
72755138Storek }
72855138Storek
72955138Storek /*
73055138Storek * Okay, things are moving along.
73155138Storek * What were we doing the last time we did something,
73255138Storek * and did it complete normally?
73355138Storek */
73455138Storek switch (sc->sc_state) {
73555138Storek
73655138Storek case S_SEL:
73755138Storek /*
73855138Storek * We were selecting. Arbitration and select are
73955138Storek * complete (because ESPINTR_DSC was not set), but
74055138Storek * there is no guarantee the command went out.
74155138Storek */
74255138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) !=
74355138Storek (ESPINTR_SVC|ESPINTR_CMP)) {
74455138Storek esperror(sc, "selection failed");
74555138Storek return (ACT_RESET);
74655138Storek }
74755138Storek if (sc->sc_espstep == ESPSTEP_DONE) {
74855138Storek sc->sc_sentcmd = 1;
74955138Storek break;
75055138Storek }
75155138Storek if (sc->sc_espstep == 2) {
75255138Storek /*
75355138Storek * We got something other than command phase.
75455138Storek * Just pretend things are normal; the
75555138Storek * device will ask for the command later.
75655138Storek */
75765145Storek esperror(sc, "DIAG: esp step 2");
75855138Storek } else if (sc->sc_espstep == 3) {
75955138Storek /*
76055138Storek * Device entered command phase and then exited it
76155138Storek * before we finished handing out the command.
762*67100Storek * Do not know how to handle this.
76355138Storek */
76465145Storek esperror(sc, "DIAG: esp step 3");
76555138Storek } else {
76655138Storek printf("%s: mysterious esp step %d\n",
76765145Storek sc->sc_dev.dv_xname, sc->sc_espstep);
76855138Storek return (ACT_RESET);
76955138Storek }
77065145Storek
77155138Storek /*
77255138Storek * Part of the command may still be lodged in the FIFO.
77355138Storek */
77465145Storek if (ESP_NFIFO(sc->sc_espfflags)) {
77565145Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO;
77665145Storek DELAY(1);
77765145Storek }
77855138Storek break;
77955138Storek
78065145Storek case S_SVC:
78155138Storek /*
78255138Storek * We were waiting for phase change after stuffing the command
78355138Storek * into the FIFO. Make sure it got out.
78455138Storek */
78565145Storek if (ESP_NFIFO(sc->sc_espfflags)) {
78665145Storek esperror(sc, "DIAG: CMDSVC, fifo not empty");
78755138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO;
78865145Storek DELAY(1);
78955138Storek } else
79055138Storek sc->sc_sentcmd = 1;
79155138Storek break;
79255138Storek
79355138Storek case S_DI:
79455138Storek /*
79555138Storek * We were doing DMA data in, and expecting a
79655138Storek * transfer-count-zero interrupt or a phase change.
79765145Storek * We got that; drain the pack register and handle
79865145Storek * as for data out -- but ignore FIFO (it should be
79965145Storek * empty, except for sync mode which we are not
80065145Storek * using anyway).
80155138Storek */
80265145Storek DMAWAIT(dma);
80355138Storek dma->dma_csr |= DMA_DRAIN;
80467079Storek DMAWAIT1(dma);
80565145Storek resid = 0;
80655138Storek goto dma_data_done;
80755138Storek
80855138Storek case S_DO:
80955138Storek /*
81055138Storek * We were doing DMA data out. If there is data in the
81155138Storek * FIFO, it is stuff that got DMAed out but never made
81255138Storek * it to the device, so it counts as residual.
81355138Storek */
81465145Storek if ((resid = ESP_NFIFO(sc->sc_espfflags)) != 0) {
81555138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO;
81665145Storek DELAY(1);
81765145Storek }
81855138Storek dma_data_done:
81955138Storek if (sc->sc_dmaactive == 0) {
82065145Storek esperror(sc, "dma done w/o dmaactive");
82155138Storek panic("espact");
82255138Storek }
82355138Storek sc->sc_dmaactive = 0;
82465145Storek
82565145Storek /* Finish computing residual count. */
82665145Storek reg = esp->esp_tcl | (esp->esp_tch << 8);
82755138Storek if (reg == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0)
82855138Storek reg = 65536;
82965145Storek resid += reg;
83065145Storek
83165145Storek /* Compute xfer count (requested - resid). */
83265145Storek i = sc->sc_dmasize - resid;
83365145Storek if (i < 0) {
83455138Storek printf("%s: xfer resid (%d) > xfer req (%d)\n",
83565145Storek sc->sc_dev.dv_xname, resid, sc->sc_dmasize);
83665145Storek i = sc->sc_dmasize; /* forgiving... */
83755138Storek }
83865145Storek
83965145Storek /* If data came in we must flush cache. */
84055138Storek if (sc->sc_state == S_DI)
84165145Storek cache_flush(sc->sc_dmaaddr, i);
84265145Storek sc->sc_dmaaddr += i;
84365145Storek sc->sc_resid -= i;
84455138Storek if ((sc->sc_espintr & ESPINTR_SVC) == 0) {
84565145Storek esperror(sc, "no bus service req");
84655138Storek return (ACT_RESET);
84755138Storek }
84855138Storek break;
84955138Storek
85055138Storek case S_STAT:
85155138Storek /*
85255138Storek * The last thing we did was tell it `initiator complete'
85355138Storek * and so we expect to have gotten both the status byte
85455138Storek * and the final message byte. It is possible that we
85555138Storek * got something else....
85655138Storek *
85755138Storek * Apparently, BUS SERVICE is set if we got just status,
85855138Storek * while FUNCTION COMPLETE is set if we got both.
85955138Storek */
86055138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != ESPINTR_CMP) {
86155138Storek esperror(sc, "bad status interrupt state");
86255138Storek return (ACT_RESET);
86355138Storek }
86465145Storek reg = ESP_NFIFO(sc->sc_espfflags);
86555138Storek if (reg < 2) {
86655138Storek printf(
86765145Storek "%s: command done but fifo count = %d; must be >= 2\n",
86865145Storek sc->sc_dev.dv_xname, reg);
86955138Storek return (ACT_RESET);
87055138Storek }
87155138Storek /*
87255138Storek * Read the status and the first msg byte.
87355138Storek * It should be CMD_COMPLETE. Eventually we
87455138Storek * may handle IDENTIFY, DISCONNECT, etc., as well.
87555138Storek */
87655138Storek sc->sc_stat[0] = esp->esp_fifo;
87755138Storek sc->sc_msg[0] = reg = esp->esp_fifo;
87855138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT;
87955138Storek if (reg == MSG_CMD_COMPLETE) {
88055138Storek sc->sc_state = S_FI;
88155138Storek return (ACT_CONT);
88255138Storek }
88355138Storek if (SCSIMSGLEN(reg) != 1) {
88455138Storek printf("%s: target %d is naughty\n",
88565145Storek sc->sc_dev.dv_xname, sc->sc_targ);
88655138Storek return (ACT_RESET);
88755138Storek }
88855138Storek printf("%s: warning: target %d returned msg 0x%x\n",
88965145Storek sc->sc_dev.dv_xname, sc->sc_targ, reg);
89055138Storek sc->sc_state = S_FI;
89155138Storek return (ACT_CONT);
89255138Storek
89355138Storek case S_MI:
89455138Storek if ((reg & ESPINTR_SVC) == 0) {
89555138Storek esperror(sc, "missing phase after msg in");
89655138Storek return (ACT_RESET);
89755138Storek }
89865145Storek reg = ESP_NFIFO(sc->sc_espfflags);
89955138Storek for (i = 0; i < reg; i++)
90055138Storek sc->sc_msg[i] = esp->esp_fifo;
90155138Storek break;
90255138Storek
90355138Storek case S_FI:
90455138Storek esperror(sc, "target did not disconnect");
90555138Storek return (ACT_RESET);
90655138Storek }
90755138Storek
90855138Storek /*
90955138Storek * Things are still moving along. The phase tells us
91055138Storek * what the device wants next. Do it.
91155138Storek */
91265145Storek switch (sc->sc_espstat & ESPSTAT_PHASE) {
91355138Storek
91455138Storek case ESPPHASE_DATA_OUT:
91565145Storek if (!sc->sc_sentcmd) esperror(sc, "DIAG: data out without command");
91665145Storek if (sc->sc_dmactl & DMA_READ) {
91765145Storek esperror(sc, "wrong phase (want to read)");
91865145Storek return (ACT_RESET);
91965145Storek }
92065145Storek newstate = S_DO;
92165145Storek goto do_data_xfer;
92255138Storek
92355138Storek case ESPPHASE_DATA_IN:
92465145Storek if (!sc->sc_sentcmd) esperror(sc, "DIAG: data in without command");
92565145Storek if (!(sc->sc_dmactl & DMA_READ)) {
92665145Storek esperror(sc, "wrong phase (want to write)");
92765145Storek return (ACT_RESET);
92865145Storek }
92965145Storek newstate = S_DI;
93065145Storek do_data_xfer:
93165145Storek if (sc->sc_resid == 0) {
93265145Storek esperror(sc, "data count error");
93365145Storek return (ACT_RESET);
93465145Storek }
93555138Storek
93665145Storek /*
93765145Storek * Compute DMA count based on chip limits.
93865145Storek * Set DMA address and load transfer count into
93965145Storek * ESP via DMA NOP, then set DMA control, and
94065145Storek * then we can start the DMA.
94165145Storek */
94265145Storek sc->sc_state = newstate;
94365145Storek i = min(sc->sc_resid, ESPMAX);
94465145Storek i = min(i, DMAMAX(sc->sc_dmaaddr));
94565145Storek sc->sc_dmasize = i;
94665145Storek dma->dma_addr = sc->sc_dmaaddr;
94765145Storek esp->esp_tch = i >> 8;
94865145Storek esp->esp_tcl = i;
94965145Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP;
95065145Storek dma->dma_csr = sc->sc_dmactl;
95165145Storek sc->sc_dmaactive = 1;
95265145Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO;
95365145Storek return (ACT_IO);
95465145Storek
95555138Storek case ESPPHASE_CMD:
95655138Storek /*
95755138Storek * Silly thing wants the command again.
95865145Storek * Load it into the FIFO and go to SVC state.
95955138Storek */
96065145Storek printf("%s: redoing command\n", sc->sc_dev.dv_xname);
96165145Storek cdb = sc->sc_curcdb;
96255138Storek reg = SCSICMDLEN(cdb->cdb_bytes[0]);
96355138Storek for (i = 0; i < reg; i++)
96455138Storek esp->esp_fifo = cdb->cdb_bytes[i];
96565145Storek sc->sc_state = S_SVC;
96655138Storek esp->esp_cmd = ESPCMD_XFER_INFO;
96755138Storek return (ACT_CONT);
96855138Storek
96955138Storek case ESPPHASE_STATUS:
97055138Storek sc->sc_state = S_STAT;
97155138Storek esp->esp_cmd = ESPCMD_INIT_COMP;
97255138Storek return (ACT_CONT);
97355138Storek
97455138Storek case ESPPHASE_MSG_IN:
97565145Storek printf("%s: accepting (& ignoring) msg from target %d\n",
97665145Storek sc->sc_dev.dv_xname, sc->sc_targ);
97755138Storek sc->sc_state = S_MI;
97855138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT;
97955138Storek return (ACT_CONT);
98055138Storek
98155138Storek default:
98265145Storek esperror(sc, "bad phase");
98355138Storek return (ACT_RESET);
98455138Storek }
98555138Storek /* NOTREACHED */
98655138Storek }
98755138Storek
98855138Storek /*
98965145Storek * THIS SHOULD BE ADJUSTABLE
99065145Storek */
99165145Storek /* name howlong purpose */
99265145Storek #define SELECT_WAIT 300000 /* wait for select to complete */
99365145Storek #define CMD_WAIT 100000 /* wait for next phase, generic */
99465145Storek #define DATA_WAIT 100000 /* time to xfer data in/out */
99565145Storek
99665145Storek /*
99755138Storek * Send an `immediate' command, i.e., poll until the whole thing is done.
99865145Storek * Return the status byte from the device, or -1 if we timed out. We use
99965145Storek * DMA to transfer the data as the fifo only moves one byte at a time.
100055138Storek */
100155138Storek int
espicmd(hba,targ,cdb,buf,len,rw)100255138Storek espicmd(hba, targ, cdb, buf, len, rw)
100365145Storek struct hba_softc *hba;
100455138Storek int targ;
100565145Storek struct scsi_cdb *cdb;
100655138Storek caddr_t buf;
100765145Storek int len, rw;
100855138Storek {
100955138Storek register struct esp_softc *sc = (struct esp_softc *)hba;
101055138Storek register volatile struct espreg *esp = sc->sc_esp;
101155138Storek register volatile struct dmareg *dma = sc->sc_dma;
101267079Storek register int r, s, wait;
101367079Storek register struct sq *sq;
101455138Storek
101555138Storek /*
101667079Storek * Wait for any ongoing operation to complete.
101767079Storek */
101867079Storek s = splbio();
101967079Storek while (sc->sc_state != S_IDLE) {
102067079Storek sc->sc_iwant = 1;
102167079Storek tsleep((caddr_t)&sc->sc_iwant, PRIBIO, "espicmd", 0);
102267079Storek }
102367079Storek sc->sc_hba.hba_busy = 1;
102467079Storek splx(s);
102567079Storek
102667079Storek /*
102765145Storek * Set up DMA transfer control (leaving interrupts disabled).
102855138Storek */
102965145Storek sc->sc_dmactl = rw & B_READ ? DMA_ENA | DMA_READ : DMA_ENA;
103065145Storek sc->sc_dmaaddr = (u_long)buf;
103165145Storek sc->sc_resid = len;
103265145Storek
103365145Storek /*
103465145Storek * Disable hardware interrupts and start select sequence,
103565145Storek * then loop, calling espact() after each ``interrupt''.
103665145Storek */
103765145Storek DMAWAIT(dma); /* ??? */
103865145Storek dma->dma_csr = 0;
103965145Storek espselect(sc, targ, cdb);
104055138Storek wait = SELECT_WAIT;
104155138Storek for (;;) {
104255138Storek r = dma->dma_csr;
104355138Storek if (!DMA_INTR(r)) {
104455138Storek if (--wait < 0) {
104565145Storek esperror(sc, "timeout");
104665145Storek goto reset;
104755138Storek }
104855138Storek DELAY(1);
104955138Storek continue;
105055138Storek }
105165145Storek sc->sc_espstat = esp->esp_stat;
105265145Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK;
105365145Storek sc->sc_espintr = esp->esp_intr;
105465145Storek sc->sc_espfflags = esp->esp_fflags;
105565145Storek sc->sc_dmacsr = r;
105665145Storek switch (r = espact(sc)) {
105755138Storek
105855138Storek case ACT_CONT:
105955138Storek case ACT_QUICKINTR:
106065145Storek wait = CMD_WAIT;
106155138Storek break;
106255138Storek
106365145Storek case ACT_IO:
106465145Storek wait = DATA_WAIT;
106555138Storek break;
106655138Storek
106755138Storek case ACT_RESET:
106855138Storek goto reset;
106955138Storek
107055138Storek case ACT_DONE:
107167079Storek r = sc->sc_stat[0];
107267079Storek goto done;
107355138Storek
107455138Storek case ACT_ERROR:
107567079Storek r = -1;
107667079Storek goto done;
107755138Storek
107855138Storek default:
107955138Storek panic("espicmd action");
108055138Storek }
108155138Storek }
108255138Storek reset:
108365145Storek espreset(sc, RESET_ESPCHIP); /* ??? */
108467079Storek r = -1;
108567079Storek done:
108667079Storek sc->sc_state = S_IDLE;
108767079Storek s = splbio();
108867079Storek if (sc->sc_iwant) {
108967079Storek sc->sc_iwant = 0;
109067079Storek wakeup((caddr_t)&sc->sc_iwant);
109167079Storek } else if ((sq = sc->sc_hba.hba_head) != NULL) {
109267079Storek sc->sc_hba.hba_head = sq->sq_forw;
109367079Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace);
109467079Storek } else
109567079Storek sc->sc_hba.hba_busy = 0;
109667079Storek splx(s);
109767079Storek return (r);
109855138Storek }
109955138Storek
110055138Storek /*
110155138Storek * Dump (write memory, possibly physmem).
110255138Storek * SPARC higher-level dump code always provides virtual addresses,
110355138Storek * so we need not do any I/O mapping here.
110455138Storek */
110555138Storek int
espdump(hba,targ,cdb,buf,len)110655138Storek espdump(hba, targ, cdb, buf, len)
110755138Storek register struct hba_softc *hba;
110855138Storek int targ;
110965145Storek struct scsi_cdb *cdb;
111055138Storek caddr_t buf;
111155138Storek register int len;
111255138Storek {
111367079Storek register struct esp_softc *sc = (struct esp_softc *)hba;
111455138Storek
111567079Storek /*
111667079Storek * If we crashed in the middle of a bus transaction...
111767079Storek */
111867079Storek if (sc->sc_state != S_IDLE)
111967079Storek espreset(sc, RESET_BOTH); /* ??? */
112055138Storek return (espicmd(hba, targ, cdb, buf, len, B_WRITE));
112155138Storek }
112255138Storek
112355138Storek /*
112455138Storek * Allocate resources (SCSI bus and DVMA space) for the given transfer.
112555138Storek * Must be called at splbio().
112655138Storek *
112755138Storek * THIS SHOULD RETURN SUCCESS/FAIL INDICATION
112855138Storek */
112955138Storek void
espstart(self,sq,bp,dgo,dev)113055138Storek espstart(self, sq, bp, dgo, dev)
113155138Storek struct device *self;
113255138Storek register struct sq *sq;
113355138Storek struct buf *bp;
113455138Storek scdgo_fn dgo;
113555138Storek struct device *dev;
113655138Storek {
113755138Storek register struct esp_softc *sc = (struct esp_softc *)self;
113855138Storek
113955138Storek if (sc->sc_hba.hba_busy == 0) {
114055138Storek /*
114155138Storek * Bus not busy, nothing to do here, just tell
114255138Storek * this target or unit that it has the SCSI bus.
114355138Storek */
114455138Storek sc->sc_hba.hba_busy = 1;
114565145Storek (*dgo)(dev, &sc->sc_cdbspace);
114655138Storek } else {
114755138Storek /*
114855138Storek * Bus is busy; just enqueue.
114955138Storek */
115055138Storek sq->sq_dgo = dgo;
115155138Storek sq->sq_dev = dev;
115255138Storek sq->sq_forw = NULL;
115355138Storek if (sc->sc_hba.hba_head == NULL)
115455138Storek sc->sc_hba.hba_head = sq;
115555138Storek else
115655138Storek sc->sc_hba.hba_tail->sq_forw = sq;
115755138Storek sc->sc_hba.hba_tail = sq;
115855138Storek }
115955138Storek }
116055138Storek
116155138Storek /*
116265145Storek * Start buffered I/O.
116355138Storek * Return 0 on success, 1 on failure.
116455138Storek */
116555138Storek int
espgo(self,targ,intr,dev,bp,pad)116655138Storek espgo(self, targ, intr, dev, bp, pad)
116755138Storek struct device *self;
116855138Storek int targ;
116955138Storek scintr_fn intr;
117055138Storek struct device *dev;
117155138Storek register struct buf *bp;
117255138Storek int pad;
117355138Storek {
117455138Storek register struct esp_softc *sc = (struct esp_softc *)self;
117555138Storek
117665145Storek /* Set up dma control for espact(). */
117765145Storek sc->sc_dmactl = bp->b_flags & B_READ ?
117865145Storek DMA_ENA | DMA_READ | DMA_IE : DMA_ENA | DMA_IE;
117965145Storek sc->sc_dmaaddr = (u_long)bp->b_un.b_addr;
118065145Storek sc->sc_resid = bp->b_bcount;
118155138Storek
118255138Storek /*
118355138Storek * Enable interrupts and start selection.
118465145Storek * The rest is done in espintr() and espact().
118555138Storek */
118655138Storek sc->sc_hba.hba_intr = intr; /* remember dev done function */
118755138Storek sc->sc_hba.hba_intrdev = dev; /* and its first arg */
118855138Storek sc->sc_dma->dma_csr = DMA_IE;
118965145Storek espselect(sc, targ, &sc->sc_cdbspace);
119055138Storek return (0);
119155138Storek }
119255138Storek
119355138Storek /*
119455138Storek * Handle interrupt. Return 1 if taken.
119555138Storek */
119655138Storek int
espintr(sc0)119755138Storek espintr(sc0)
119855138Storek void *sc0;
119955138Storek {
120055138Storek register struct esp_softc *sc = (struct esp_softc *)sc0;
120155138Storek register volatile struct espreg *esp = sc->sc_esp;
120255138Storek register volatile struct dmareg *dma = sc->sc_dma;
120355138Storek register int r, wait;
120455138Storek register struct sq *sq;
120555138Storek
120655138Storek r = dma->dma_csr;
120755138Storek if (!DMA_INTR(r))
120855138Storek return (0); /* not ours */
120959214Storek sc->sc_intrcnt.ev_count++;
121055138Storek
121155138Storek again:
121255138Storek sc->sc_espstat = esp->esp_stat;
121355138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK;
121455138Storek sc->sc_espintr = esp->esp_intr;
121565145Storek sc->sc_espfflags = esp->esp_fflags;
121655138Storek sc->sc_dmacsr = r;
121755138Storek
121855138Storek if (sc->sc_state == S_IDLE) {
121965145Storek printf("%s: stray interrupt\n", sc->sc_dev.dv_xname);
122055138Storek dma->dma_csr &= ~DMA_IE; /* ??? */
122155138Storek return (1);
122255138Storek }
122365145Storek switch (r = espact(sc)) {
122455138Storek
122555138Storek case ACT_CONT: /* just return */
122665145Storek case ACT_IO:
122755138Storek break;
122855138Storek
122955138Storek case ACT_RESET: /* please reset esp */
123055138Storek reset:
123165145Storek espreset(sc, RESET_ESPCHIP); /* ??? */
123255138Storek /* FALLTHROUGH */
123355138Storek
123455138Storek case ACT_DONE: /* this one is done, successfully */
123555138Storek case ACT_ERROR: /* this one is done due to `severe' error */
123655138Storek if (!sc->sc_hba.hba_busy)
123755138Storek panic("espintr sq");
123855138Storek /*
123967079Storek * This transaction is done. Call the driver's intr routine.
124067079Storek * If an immediate command is pending, let it run in front
124167079Storek * of us, otherwise start the next transation. Note that
124267079Storek * the interrupt routine may run its own immediate commands
124367079Storek * (`request sense' for errors, eg) before we get around to
124467079Storek * the process waiting to do immediate command, but that
124567079Storek * is OK; if we did not set S_IDLE here we could deadlock.
124655138Storek */
124767079Storek sc->sc_state = S_IDLE;
124855138Storek (*sc->sc_hba.hba_intr)(sc->sc_hba.hba_intrdev,
124955138Storek r == ACT_DONE ? sc->sc_stat[0] : -1, sc->sc_resid);
125067079Storek if (sc->sc_iwant) {
125167079Storek wakeup((caddr_t)&sc->sc_iwant);
125267079Storek sc->sc_iwant = 0;
125367079Storek } else if ((sq = sc->sc_hba.hba_head) != NULL) {
125455138Storek sc->sc_hba.hba_head = sq->sq_forw;
125565145Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace);
125655138Storek } else
125755138Storek sc->sc_hba.hba_busy = 0;
125855138Storek break;
125955138Storek
126055138Storek case ACT_QUICKINTR: /* wait a short while for another interrupt */
126165145Storek printf("%s: quickintr: ", sc->sc_dev.dv_xname);
126255138Storek wait = 100;
126355138Storek do {
126455138Storek r = dma->dma_csr;
126555138Storek if (DMA_INTR(r)) {
126655138Storek printf("got one, wait=%d\n", wait);
126755138Storek goto again;
126855138Storek }
126955138Storek } while (--wait > 0);
127055138Storek printf("did not get one\n");
127155138Storek break;
127255138Storek
127355138Storek default:
127455138Storek panic("espintr action");
127555138Storek }
127655138Storek return (1);
127755138Storek }
127855138Storek
127955138Storek /*
128055138Storek * Target or unit decided to let go of the bus early.
128155138Storek */
128255138Storek void
esprel(self)128355138Storek esprel(self)
128455138Storek struct device *self;
128555138Storek {
128655138Storek register struct esp_softc *sc = (struct esp_softc *)self;
128755138Storek register struct sq *sq;
128855138Storek
128955138Storek /* if there is someone else waiting, give them a crack at it */
1290*67100Storek if (sc->sc_iwant) {
1291*67100Storek wakeup((caddr_t)&sc->sc_iwant);
1292*67100Storek sc->sc_iwant = 0;
1293*67100Storek } else if ((sq = sc->sc_hba.hba_head) != NULL) {
1294*67100Storek sc->sc_hba.hba_head = sq->sq_forw;
129565145Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace);
1296*67100Storek } else
129755138Storek sc->sc_hba.hba_busy = 0;
129855138Storek }
1299