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*67079Storek * @(#)esp.c 8.3 (Berkeley) 05/03/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 * 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 { 10565145Storek struct device dc_dev; /* base device */ 10665145Storek volatile struct dmareg *dc_dma; /* register virtual address */ 10765145Storek int dc_dmarev; /* revision */ 10865145Storek 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 */ 12065145Storek #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 */ 12465145Storek 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. 13465145Storek * (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; 14065145Storek 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 */ 163*67079Storek char sc_iwant; /* true => icmd needs wakeup on idle */ 16455138Storek char sc_state; /* SCSI protocol state; see below */ 16555138Storek char sc_sentcmd; /* set once we get cmd out */ 16655138Storek char sc_dmaactive; /* true => doing dma */ 16755138Storek #ifdef notyet 16855138Storek u_char sc_sync; /* synchronous transfer stuff (?) */ 16955138Storek #endif 17055138Storek u_char sc_stat[2]; /* status from last `status' phase */ 17155138Storek u_char sc_msg[16]; /* message from device */ 17255138Storek u_short sc_dmactl; /* control to load into dma csr */ 17365145Storek u_long sc_dmaaddr; /* address for next xfer */ 17465145Storek int sc_dmasize; /* size of current xfer */ 17565145Storek int sc_resid; /* count of bytes not yet xferred */ 17655138Storek int sc_targ; /* the target involved */ 17765145Storek struct scsi_cdb *sc_curcdb; /* ptr to current command */ 17865145Storek /* might cdbspace eventually be per-target? */ 17965145Storek struct scsi_cdb sc_cdbspace; /* space for one command */ 18055138Storek }; 18155138Storek 18255138Storek /* 18365145Storek * Values for sc_esptype (used to control configuration reset, and for 18465145Storek * workarounds for chip bugs). The order is important; see espreset(). 18555138Storek */ 18655138Storek #define ESP100 0 18755138Storek #define ESP100A 1 18855138Storek #define ESP2XX 2 18955138Storek 19055138Storek /* 19155138Storek * Probe state. 0 means not probing. While looking for each target 19255138Storek * we set this to PROBE_TESTING and do a TEST UNIT READY on unit 0. 19355138Storek * If selection fails, this is changed to PROBE_NO_TARGET; otherwise 19455138Storek * we assume the target exists, regardless of the result of the test. 19555138Storek */ 19655138Storek #define PROBE_TESTING 1 19755138Storek #define PROBE_NO_TARGET 2 19855138Storek 19955138Storek /* 20055138Storek * States in sc_state. 20155138Storek * 20265145Storek * Note that S_SVC is rare: normally we load the SCSI command into the 20355138Storek * ESP fifo and get interrupted only when the device has gone to data 20455138Storek * or status phase. If the device wants to play games, though, we end 20555138Storek * up doing things differently. 20655138Storek */ 20755138Storek char *espstates[] = { 20855138Storek #define S_IDLE 0 /* not doing anything */ 20955138Storek "idle", 21055138Storek #define S_SEL 1 /* expecting select done interrupt */ 21155138Storek "selecting", 21265145Storek #define S_SVC 2 /* expecting service req interrupt */ 21365145Storek "waiting for svc req", 21465145Storek #define S_DI 3 /* expecting data-in done interrupt */ 21555138Storek "receiving data", 21665145Storek #define S_DO 4 /* expecting data-out done interrupt */ 21755138Storek "sending data", 21865145Storek #define S_STAT 5 /* expecting status done interrupt */ 21955138Storek "receiving status", 22065145Storek #define S_MI 6 /* expecting message-in done interrupt */ 22155138Storek "receiving message", 22265145Storek #define S_FI 7 /* expecting final disconnect interrupt */ 22355138Storek "waiting for disconnect" 22455138Storek }; 22555138Storek 22655138Storek /* 22765145Storek * Hardware limits on transfer sizes (see comments at top). 22865145Storek */ 22965145Storek #define ESPMAX (64 * 1024) 23065145Storek #define DMAMAX(a) (0x01000000 - ((a) & 0x00ffffff)) 23165145Storek 23265145Storek /* 23355138Storek * Return values from espact(). 23455138Storek */ 23555138Storek #define ACT_CONT 0 /* espact() handled everything */ 23665145Storek #define ACT_IO 1 /* espact() is xferring data */ 23765145Storek #define ACT_DONE 2 /* handled everything, and op is now done */ 23865145Storek #define ACT_ERROR 3 /* an error occurred, op has been trashed */ 23965145Storek #define ACT_RESET 4 /* please reset ESP, then do ACT_ERROR */ 24065145Storek #define ACT_QUICKINTR 5 /* another interrupt is expected immediately */ 24155138Storek 24255138Storek /* autoconfiguration driver */ 24355138Storek void espattach(struct device *, struct device *, void *); 24455138Storek struct cfdriver espcd = 24559214Storek { NULL, "esp", matchbyname, espattach, DV_DULL, sizeof(struct esp_softc), 24659214Storek "intr" }; 24755138Storek 24855138Storek /* Sbus driver */ 24955138Storek void espsbreset(struct device *); 25055138Storek 25155138Storek /* interrupt interface */ 25255138Storek int espintr(void *); 25355138Storek 25455138Storek /* SCSI HBA driver */ 25555138Storek int espicmd(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int, int); 25655138Storek int espdump(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int); 25755138Storek void espstart(struct device *, struct sq *, struct buf *, 25855138Storek scdgo_fn, struct device *); 25955138Storek int espgo(struct device *, int, scintr_fn, struct device *, 26055138Storek struct buf *, int); 26155138Storek void esprel(struct device *); 26255138Storek void esphbareset(struct hba_softc *, int); 26355138Storek static struct hbadriver esphbadriver = 26455138Storek { espicmd, espdump, espstart, espgo, esprel, esphbareset }; 26555138Storek 26665145Storek /* other prototypes */ 26765145Storek static void espdoattach(int); 26865145Storek static void dmareset(struct esp_softc *); 26965145Storek static void espreset(struct esp_softc *, int); 27065145Storek static void esperror(struct esp_softc *, const char *); 27165145Storek static int espact(struct esp_softc *); 27265145Storek void espselect(struct esp_softc *, int, struct scsi_cdb *); 27355138Storek 27465145Storek /* second arg to espreset() */ 27565145Storek #define RESET_ESPCHIP 0x1 27665145Storek #define RESET_SCSIBUS 0x2 27765145Storek #define RESET_BOTH (RESET_ESPCHIP | RESET_SCSIBUS) 27855138Storek 27955138Storek /* 28055138Storek * Attach a found DMA chip. 28155138Storek * The second argument is really a pointer to an sbus_attach_args. 28255138Storek */ 28355138Storek void 28455138Storek dmaattach(parent, dev, args) 28555138Storek struct device *parent; 28655138Storek struct device *dev; 28755138Storek void *args; 28855138Storek { 28965145Storek register struct dma_softc *dc = (struct dma_softc *)dev; 29055138Storek register struct sbus_attach_args *sa = args; 29155138Storek register volatile struct dmareg *dma; 29255138Storek register int rev; 29355138Storek struct esp_softc *esc; 29455138Storek 29555138Storek if (sa->sa_ra.ra_vaddr) 29655138Storek dma = (volatile struct dmareg *)sa->sa_ra.ra_vaddr; 29755138Storek else 29855138Storek dma = (volatile struct dmareg *) 29955138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct dmareg)); 30065145Storek dc->dc_dma = dma; 30155138Storek 30255138Storek switch (rev = DMA_REV(dma->dma_csr)) { 30355138Storek case DMAREV_1: 30455138Storek printf(": rev 1\n"); 30565145Storek dc->dc_dmafmt = DMA_REV1_BITS; 30655138Storek break; 30755138Storek case DMAREV_2: 30855138Storek printf(": rev 2\n"); 30965145Storek dc->dc_dmafmt = DMA_REV2_BITS; 31055138Storek break; 31159327Storek case DMAREV_3: 31259327Storek printf(": rev 3\n"); 31359327Storek printf("WARNING: esp.c not yet updated for rev 3\n"); 31465145Storek dc->dc_dmafmt = DMA_REV3_BITS; 31559327Storek break; 31655138Storek default: 31759327Storek printf(": unknown revision code 0x%x\n", rev); 31865145Storek dc->dc_dmafmt = DMA_REV3_BITS; /* cross fingers */ 31955138Storek break; 32055138Storek } 32165145Storek dc->dc_dmarev = rev; 32265145Storek espdoattach(dc->dc_dev.dv_unit); 32355138Storek } 32455138Storek 32555138Storek /* 32655138Storek * Attach a found ESP chip. Search for targets; attach each one found. 32755138Storek * The latter must be deferred if the corresponding dma chip has not yet 32855138Storek * been configured. 32955138Storek */ 33055138Storek void 33155138Storek espattach(parent, self, args) 33255138Storek struct device *parent; 33355138Storek struct device *self; 33455138Storek void *args; 33555138Storek { 33655138Storek register struct esp_softc *sc = (struct esp_softc *)self; 33755138Storek register struct sbus_attach_args *sa = args; 33855138Storek register volatile struct espreg *esp; 33959214Storek register struct bootpath *bp; 34055138Storek int node, pri, freq, t; 34155138Storek 34255138Storek if (sa->sa_ra.ra_nintr != 1) { 34355138Storek printf(": expected 1 interrupt, got %d\n", sa->sa_ra.ra_nintr); 34455138Storek return; 34555138Storek } 34655138Storek pri = sa->sa_ra.ra_intr[0].int_pri; 34755138Storek printf(" pri %d", pri); 34855138Storek if (sa->sa_ra.ra_vaddr) 34955138Storek esp = (volatile struct espreg *)sa->sa_ra.ra_vaddr; 35055138Storek else 35155138Storek esp = (volatile struct espreg *) 35255138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct espreg)); 35355138Storek sc->sc_esp = esp; 35455138Storek node = sa->sa_ra.ra_node; 35555138Storek sc->sc_id = getpropint(node, "initiator-id", 7); 35655138Storek freq = getpropint(node, "clock-frequency", -1); 35755138Storek if (freq < 0) 35865145Storek freq = 35965145Storek ((struct sbus_softc *)sc->sc_dev.dv_parent)->sc_clockfreq; 36055138Storek 36155138Storek /* MIGHT NEED TO RESET ESP CHIP HERE ...? */ 36255138Storek 36355138Storek /* 36455138Storek * Find out whether we have a -100, -100A, or -2xx, 36555138Storek * and what speed it runs at. 36655138Storek */ 36755138Storek sc->sc_conf1 = sc->sc_id | ESPCONF1_PARENB; 36855138Storek /* sc->sc_conf2 = 0; */ 36955138Storek /* sc->sc_conf3 = 0; */ 37055138Storek esp->esp_conf1 = sc->sc_conf1; 37155138Storek esp->esp_conf2 = 0; 37255138Storek esp->esp_conf2 = ESPCONF2_SCSI2 | ESPCONF2_RPE; 37355138Storek if ((esp->esp_conf2 & ~ESPCONF2_RSVD) != 37455138Storek (ESPCONF2_SCSI2 | ESPCONF2_RPE)) { 37555138Storek printf(": ESP100"); 37655138Storek sc->sc_esptype = ESP100; 37755138Storek } else { 37855138Storek esp->esp_conf2 = 0; 37955138Storek esp->esp_conf3 = 0; 38055138Storek esp->esp_conf3 = 5; 38155138Storek if (esp->esp_conf3 != 5) { /* XXX def bits */ 38255138Storek printf(": ESP100A"); 38355138Storek sc->sc_esptype = ESP100A; 38455138Storek } else { 38555138Storek esp->esp_conf3 = 0; 38655138Storek printf(": ESP2XX"); 38755138Storek sc->sc_esptype = ESP2XX; 38855138Storek } 38955138Storek } 39055138Storek printf(", clock = %s MHz, ID = %d\n", clockfreq(freq), sc->sc_id); 39155138Storek 39255138Storek /* 39355138Storek * Set clock conversion factor and select timeout. 39455138Storek * N.B.: clock frequency is not actually used in the rest 39555138Storek * of the driver; I calculate it here for completeness only 39655138Storek * (so I can see it when debugging). 39755138Storek */ 39855138Storek sc->sc_clockfreq = freq; 39955138Storek freq = howmany(freq, 1000 * 1000); /* convert to MHz */ 40055138Storek t = ESPCCF_FROMMHZ(freq); 40155138Storek if (t < ESPCCF_MIN) 40255138Storek t = ESPCCF_MIN; 40355138Storek sc->sc_ccf = t; 40455138Storek t = ESPTIMO_REGVAL(250, t, freq); /* timeout = 250 ms. */ 40555138Storek if (t >= 256) 40655138Storek t = 0; 40755138Storek sc->sc_sel_timeout = t; 40855138Storek 40955138Storek /* 41055138Storek * Link into sbus; set interrupt handler. 41155138Storek */ 41255138Storek sc->sc_sd.sd_reset = espsbreset; 41365145Storek sbus_establish(&sc->sc_sd, &sc->sc_dev); 41455138Storek sc->sc_ih.ih_fun = espintr; 41555138Storek sc->sc_ih.ih_arg = sc; 41655138Storek intr_establish(pri, &sc->sc_ih); 41765145Storek evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt); 41859214Storek 41959214Storek #define SAME_ESP(bp, sa) \ 42059214Storek ((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \ 42165145Storek (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit)) 42259214Storek 42359214Storek bp = sa->sa_ra.ra_bp; 42459214Storek if (bp != NULL && strcmp(bp->name, "esp") == 0 && SAME_ESP(bp, sa)) 42559214Storek sc->sc_bp = bp + 1; 42665145Storek espdoattach(sc->sc_dev.dv_unit); 42755138Storek } 42855138Storek 42955138Storek /* 43055138Storek * `Final' attach of esp occurs once esp and dma chips have been found 43155138Storek * and assigned virtual addresses. Set up the ESP SCSI data structures 43255138Storek * and probe the SCSI bus. 43355138Storek */ 43455138Storek static void 43555138Storek espdoattach(unit) 43655138Storek int unit; 43755138Storek { 43855138Storek register struct esp_softc *sc; 43965145Storek register struct dma_softc *dc; 44059214Storek register struct bootpath *bp; 44159214Storek register struct targ *t; 44259214Storek register int targ, u; 44355138Storek 44455138Storek /* make sure we have both */ 44555138Storek if (espcd.cd_ndevs <= unit || 44655138Storek dmacd.cd_ndevs <= unit || 44755138Storek (sc = espcd.cd_devs[unit]) == NULL || 44865145Storek (dc = dmacd.cd_devs[unit]) == NULL) 44955138Storek return; 45065145Storek sc->sc_dc = dc; 45165145Storek sc->sc_dma = dc->dc_dma; 45255138Storek sc->sc_hba.hba_driver = &esphbadriver; 45355138Storek 45465145Storek sc->sc_dma->dma_csr = 0; /* ??? */ 45565145Storek espreset(sc, RESET_ESPCHIP); 45655138Storek 45755138Storek /* MAYBE THIS SHOULD BE MOVED TO scsi_subr.c? */ 45855138Storek for (targ = 0; targ < 8; targ++) { 45955138Storek if (targ == sc->sc_id) 46055138Storek continue; 46155138Storek sc->sc_probing = PROBE_TESTING; 46255138Storek sc->sc_clearing = 1; 46365145Storek (void)scsi_test_unit_ready(&sc->sc_hba, targ, 0); 46455138Storek if (sc->sc_probing != PROBE_NO_TARGET) { 46555138Storek sc->sc_probing = 0; 46655138Storek sc->sc_clearing = 0; 46755138Storek SCSI_FOUNDTARGET(&sc->sc_hba, targ); 46855138Storek } 46955138Storek } 47055138Storek sc->sc_probing = 0; 47155138Storek sc->sc_clearing = 0; 47259214Storek 47359214Storek /* 47465145Storek * See if we booted from a unit on this target. We could 47565145Storek * compare bp->name against the unit's name but there's no 47665145Storek * real need since a target and unit uniquely specify a 47765145Storek * scsi device. 47859214Storek */ 47965145Storek if ((bp = sc->sc_bp) != NULL && (u_int)(targ = bp->val[0]) < 8 && 48065145Storek (u_int)(u = bp->val[1]) < 8 && 48165145Storek (t = sc->sc_hba.hba_targets[targ]) != NULL && t->t_units[u] != NULL) 48259214Storek bootdv = t->t_units[u]->u_dev; 48355138Storek } 48455138Storek 48555138Storek /* 48665145Storek * We are not allowed to touch the DMA "flush" and "drain" bits 48765145Storek * while it is still thinking about a request (DMA_RP). 48855138Storek */ 48965145Storek #define DMAWAIT(dma) while ((dma)->dma_csr & DMA_RP) DELAY(1) 490*67079Storek #define DMAWAIT1(dma) while ((dma)->dma_csr & DMA_PC) DELAY(1) 49165145Storek 49265145Storek /* 49365145Storek * Reset the DMA chip. 49465145Storek */ 49555138Storek static void 49655138Storek dmareset(sc) 49755138Storek struct esp_softc *sc; 49855138Storek { 49955138Storek register volatile struct dmareg *dma = sc->sc_dma; 50055138Storek 50165145Storek DMAWAIT(dma); 50255138Storek dma->dma_csr |= DMA_RESET; 50355138Storek DELAY(200); 50455138Storek dma->dma_csr &= ~DMA_RESET; /* ??? */ 50555138Storek sc->sc_dmaactive = 0; 50665145Storek if (sc->sc_dc->dc_dmarev == DMAREV_2 && sc->sc_esptype != ESP100) 50759327Storek dma->dma_csr |= DMA_TURBO; 50855138Storek dma->dma_csr |= DMA_IE; /* enable interrupts */ 50955138Storek DELAY(200); 51055138Storek } 51155138Storek 51255138Storek /* 51365145Storek * Reset the chip and/or SCSI bus (always resets DMA). 51455138Storek */ 51555138Storek static void 51665145Storek espreset(sc, how) 51755138Storek register struct esp_softc *sc; 51865145Storek int how; 51955138Storek { 52055138Storek register volatile struct espreg *esp = sc->sc_esp; 52155138Storek 52255138Storek dmareset(sc); 52365145Storek if (how & RESET_ESPCHIP) { 52465145Storek esp->esp_cmd = ESPCMD_RESET_CHIP; 52565145Storek esp->esp_cmd = ESPCMD_NOP; 52665145Storek /* 52765145Storek * Reload configuration registers (cleared by 52865145Storek * RESET_CHIP command). Reloading conf2 on an 52965145Storek * ESP100 goofs it up, so out of paranoia we load 53065145Storek * only the registers that exist. 53165145Storek */ 53265145Storek esp->esp_conf1 = sc->sc_conf1; 53365145Storek if (sc->sc_esptype > ESP100) { /* 100A, 2XX */ 53465145Storek esp->esp_conf2 = sc->sc_conf2; 53565145Storek if (sc->sc_esptype > ESP100A) /* 2XX only */ 53665145Storek esp->esp_conf3 = sc->sc_conf3; 53765145Storek } 53865145Storek esp->esp_ccf = sc->sc_ccf; 53965145Storek esp->esp_timeout = sc->sc_sel_timeout; 54065145Storek /* We set synch offset later. */ 54155138Storek } 54265145Storek if (how & RESET_SCSIBUS) { 54365145Storek /* 54465145Storek * The chip should retain most of its parameters 54565145Storek * (including esp_ccf) across this kind of reset 54665145Storek * (see section 3.5 of Emulex documentation). 54765145Storek */ 54865145Storek /* turn off scsi bus reset interrupts and reset scsi bus */ 54965145Storek esp->esp_conf1 = sc->sc_conf1 | ESPCONF1_REPORT; 55065145Storek esp->esp_cmd = ESPCMD_RESET_BUS; 55165145Storek esp->esp_cmd = ESPCMD_NOP; 55265145Storek DELAY(100000); /* ??? */ 55365145Storek (void)esp->esp_intr; 55465145Storek esp->esp_conf1 = sc->sc_conf1; 55565145Storek } 556*67079Storek sc->sc_state = S_IDLE; 557*67079Storek if (sc->sc_iwant) { 558*67079Storek wakeup((caddr_t)&sc->sc_iwant); 559*67079Storek sc->sc_iwant = 0; 560*67079Storek } 56155138Storek sc->sc_needclear = 0xff; 56255138Storek } 56355138Storek 56455138Storek /* 56555138Storek * Reset the SCSI bus and, optionally, all attached targets. 56655138Storek */ 56755138Storek void 56855138Storek esphbareset(hba, resetunits) 56955138Storek struct hba_softc *hba; 57055138Storek int resetunits; 57155138Storek { 57255138Storek register struct esp_softc *sc = (struct esp_softc *)hba; 57355138Storek 57465145Storek espreset(sc, RESET_SCSIBUS); 57555138Storek if (resetunits) 57655138Storek scsi_reset_units(&sc->sc_hba); 57755138Storek } 57855138Storek 57955138Storek /* 58055138Storek * Reset the esp, after an Sbus reset. 58155138Storek * Also resets corresponding dma chip. 58255138Storek * 58355138Storek * THIS ROUTINE MIGHT GO AWAY 58455138Storek */ 58555138Storek void 58655138Storek espsbreset(dev) 58755138Storek struct device *dev; 58855138Storek { 58955138Storek struct esp_softc *sc = (struct esp_softc *)dev; 59055138Storek 59165145Storek if (sc->sc_dc) { 59265145Storek printf(" %s %s", sc->sc_dc->dc_dev.dv_xname, 59365145Storek sc->sc_dev.dv_xname); 59455138Storek esphbareset(&sc->sc_hba, 1); 59555138Storek } 59655138Storek } 59755138Storek 59865145Storek /* 59965145Storek * Log an error. 60065145Storek */ 60155138Storek static void 60255138Storek esperror(sc, err) 60355138Storek register struct esp_softc *sc; 60465145Storek const char *err; 60555138Storek { 60665145Storek int stat; 60755138Storek 60865145Storek stat = sc->sc_espstat; 60965145Storek printf( 61065145Storek "%s target %d cmd 0x%x (%s): %s:\n\ 61165145Storek \tstat=%b (%s) step=%x dmacsr=%b fflags=%x intr=%b\n", 61265145Storek sc->sc_dev.dv_xname, sc->sc_targ, sc->sc_curcdb->cdb_bytes[0], 61365145Storek espstates[sc->sc_state], err, 61465145Storek stat, ESPSTAT_BITS, espphases[stat & ESPSTAT_PHASE], 61565145Storek sc->sc_espstep, sc->sc_dmacsr, sc->sc_dc->dc_dmafmt, 61665145Storek sc->sc_espfflags, sc->sc_espintr, ESPINTR_BITS); 61755138Storek } 61855138Storek 61955138Storek /* 62065145Storek * Issue a select, loading command into the FIFO. 62165145Storek * Return nonzero on error, 0 if OK. 62265145Storek * Sets state to `selecting'; espact() will sequence state FSM. 62365145Storek */ 62465145Storek void 62565145Storek espselect(sc, targ, cdb) 62665145Storek register struct esp_softc *sc; 62765145Storek register int targ; 62865145Storek register struct scsi_cdb *cdb; 62965145Storek { 63065145Storek register volatile struct espreg *esp; 63165145Storek register int i, cmdlen; 63265145Storek 63365145Storek sc->sc_targ = targ; 63465145Storek sc->sc_state = S_SEL; 63565145Storek sc->sc_curcdb = cdb; 63665145Storek sc->sc_sentcmd = 0; 63765145Storek sc->sc_stat[0] = 0xff; /* ??? */ 63865145Storek sc->sc_msg[0] = 0xff; /* ??? */ 63965145Storek 64065145Storek /* 64165145Storek * Try to talk to target. 64265145Storek * Synch offset 0 => asynchronous transfer. 64365145Storek */ 64465145Storek esp = sc->sc_esp; 64565145Storek esp->esp_id = targ; 64665145Storek esp->esp_syncoff = 0; 64765145Storek 64865145Storek /* 64965145Storek * Stuff the command bytes into the fifo. 65065145Storek * Select without attention since we do not do disconnect yet. 65165145Storek */ 65265145Storek cmdlen = SCSICMDLEN(cdb->cdb_bytes[0]); 65365145Storek for (i = 0; i < cmdlen; i++) 65465145Storek esp->esp_fifo = cdb->cdb_bytes[i]; 65565145Storek esp->esp_cmd = ESPCMD_SEL_NATN; 65665145Storek /* the rest is done elsewhere */ 65765145Storek } 65865145Storek 65965145Storek /* 66065145Storek * Sequence through the SCSI state machine. Return the action to take. 66155138Storek * 66255138Storek * Most of the work happens here. 66355138Storek * 66455138Storek * There are three interrupt sources: 66555138Storek * -- ESP interrupt request (typically, some device wants something). 66655138Storek * -- DMA memory error. 66755138Storek * -- DMA byte count has reached 0 (we do not often want this one but 66855138Storek * can only turn it off in rev 2 DMA chips, it seems). 66955138Storek * DOES THIS OCCUR AT ALL HERE? THERE IS NOTHING TO HANDLE IT! 67055138Storek */ 67155138Storek static int 67265145Storek espact(sc) 67355138Storek register struct esp_softc *sc; 67465145Storek { 67555138Storek register volatile struct espreg *esp; 67655138Storek register volatile struct dmareg *dma; 67765145Storek register int reg, i, resid, newstate; 67855138Storek register struct scsi_cdb *cdb; 67955138Storek 68065145Storek dma = sc->sc_dma; 68155138Storek /* check various error conditions, using as little code as possible */ 68255138Storek if (sc->sc_dmacsr & DMA_EP) { 68355138Storek esperror(sc, "DMA error"); 68465145Storek DMAWAIT(dma); 68555138Storek dma->dma_csr |= DMA_FLUSH; 686*67079Storek DMAWAIT1(dma); 68755138Storek return (ACT_ERROR); 68855138Storek } 68955138Storek reg = sc->sc_espstat; 69055138Storek if (reg & ESPSTAT_GE) { 69155138Storek /* 69255138Storek * This often occurs when there is no target. 69355138Storek * (See DSC code below.) 69455138Storek */ 69555138Storek if (sc->sc_espintr & ESPINTR_DSC && 69655138Storek sc->sc_state == S_SEL && sc->sc_probing) { 69755138Storek sc->sc_probing = PROBE_NO_TARGET; 69855138Storek return (ACT_RESET); 69955138Storek } 70065145Storek esperror(sc, "DIAG: gross error (ignored)"); 70155138Storek } 70255138Storek if (reg & ESPSTAT_PE) { 70355138Storek esperror(sc, "parity error"); 70455138Storek return (ACT_RESET); 70555138Storek } 70655138Storek reg = sc->sc_espintr; 70755138Storek #define ERR (ESPINTR_SBR|ESPINTR_ILC|ESPINTR_RSL|ESPINTR_SAT|ESPINTR_SEL) 70855138Storek if (reg & ERR) { 70955138Storek if (reg & ESPINTR_SBR) 71055138Storek esperror(sc, "scsi bus reset"); 71155138Storek else if (reg & ESPINTR_ILC) 71255138Storek esperror(sc, "illegal command (driver bug)"); 71355138Storek else { 71465145Storek printf("%s: target %d", sc->sc_dev.dv_xname, 71565145Storek sc->sc_targ); 71655138Storek if (reg & ESPINTR_RSL) 71755138Storek printf(" tried to reselect;"); 71855138Storek if (reg & ESPINTR_SAT) 71955138Storek printf(" selected with ATN;"); 72055138Storek if (reg & ESPINTR_SEL) 72155138Storek printf(" selected us as target;"); 72255138Storek printf("we do not allow this yet\n"); 72355138Storek } 72455138Storek return (ACT_ERROR); 72555138Storek } 72655138Storek #undef ERR 72755138Storek 72865145Storek esp = sc->sc_esp; 72965145Storek 73055138Storek /* 73155138Storek * Disconnect currently only allowed in `final interrupt' states. 73255138Storek */ 73355138Storek if (reg & ESPINTR_DSC) { 73455138Storek if (sc->sc_state == S_FI) 73555138Storek return (ACT_DONE); 73655138Storek /* 73755138Storek * If we were doing a select just to test the existence 73855138Storek * of the target, note that it did not respond; otherwise 73955138Storek * gripe. 74055138Storek */ 74155138Storek if (sc->sc_state == S_SEL) { 74255138Storek if (sc->sc_probing) { 74355138Storek sc->sc_probing = PROBE_NO_TARGET; 74455138Storek return (ACT_RESET); 74555138Storek } 74655138Storek } 74755138Storek /* flush fifo, in case we were selecting or sending data */ 74855138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 74965145Storek DELAY(1); 75055138Storek printf("%s: target %d not responding\n", 75165145Storek sc->sc_dev.dv_xname, sc->sc_targ); 75255138Storek return (ACT_ERROR); 75355138Storek } 75455138Storek 75555138Storek /* 75655138Storek * Okay, things are moving along. 75755138Storek * What were we doing the last time we did something, 75855138Storek * and did it complete normally? 75955138Storek */ 76055138Storek switch (sc->sc_state) { 76155138Storek 76255138Storek case S_SEL: 76355138Storek /* 76455138Storek * We were selecting. Arbitration and select are 76555138Storek * complete (because ESPINTR_DSC was not set), but 76655138Storek * there is no guarantee the command went out. 76755138Storek */ 76855138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != 76955138Storek (ESPINTR_SVC|ESPINTR_CMP)) { 77055138Storek esperror(sc, "selection failed"); 77155138Storek return (ACT_RESET); 77255138Storek } 77355138Storek if (sc->sc_espstep == ESPSTEP_DONE) { 77455138Storek sc->sc_sentcmd = 1; 77555138Storek break; 77655138Storek } 77755138Storek if (sc->sc_espstep == 2) { 77855138Storek /* 77955138Storek * We got something other than command phase. 78055138Storek * Just pretend things are normal; the 78155138Storek * device will ask for the command later. 78255138Storek */ 78365145Storek esperror(sc, "DIAG: esp step 2"); 78455138Storek } else if (sc->sc_espstep == 3) { 78555138Storek /* 78655138Storek * Device entered command phase and then exited it 78755138Storek * before we finished handing out the command. 78855138Storek * Let this happen iff we are trying to clear the 78955138Storek * target state. 79055138Storek */ 79165145Storek esperror(sc, "DIAG: esp step 3"); 79255138Storek if (!sc->sc_clearing) 79355138Storek return (ACT_RESET); 79455138Storek } else { 79555138Storek printf("%s: mysterious esp step %d\n", 79665145Storek sc->sc_dev.dv_xname, sc->sc_espstep); 79755138Storek return (ACT_RESET); 79855138Storek } 79965145Storek 80055138Storek /* 80155138Storek * Part of the command may still be lodged in the FIFO. 80255138Storek */ 80365145Storek if (ESP_NFIFO(sc->sc_espfflags)) { 80465145Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 80565145Storek DELAY(1); 80665145Storek } 80755138Storek break; 80855138Storek 80965145Storek case S_SVC: 81055138Storek /* 81155138Storek * We were waiting for phase change after stuffing the command 81255138Storek * into the FIFO. Make sure it got out. 81355138Storek */ 81465145Storek if (ESP_NFIFO(sc->sc_espfflags)) { 81565145Storek esperror(sc, "DIAG: CMDSVC, fifo not empty"); 81655138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 81765145Storek DELAY(1); 81855138Storek } else 81955138Storek sc->sc_sentcmd = 1; 82055138Storek break; 82155138Storek 82255138Storek case S_DI: 82355138Storek /* 82455138Storek * We were doing DMA data in, and expecting a 82555138Storek * transfer-count-zero interrupt or a phase change. 82665145Storek * We got that; drain the pack register and handle 82765145Storek * as for data out -- but ignore FIFO (it should be 82865145Storek * empty, except for sync mode which we are not 82965145Storek * using anyway). 83055138Storek */ 83165145Storek DMAWAIT(dma); 83255138Storek dma->dma_csr |= DMA_DRAIN; 833*67079Storek DMAWAIT1(dma); 83465145Storek resid = 0; 83555138Storek goto dma_data_done; 83655138Storek 83755138Storek case S_DO: 83855138Storek /* 83955138Storek * We were doing DMA data out. If there is data in the 84055138Storek * FIFO, it is stuff that got DMAed out but never made 84155138Storek * it to the device, so it counts as residual. 84255138Storek */ 84365145Storek if ((resid = ESP_NFIFO(sc->sc_espfflags)) != 0) { 84455138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 84565145Storek DELAY(1); 84665145Storek } 84755138Storek dma_data_done: 84855138Storek if (sc->sc_dmaactive == 0) { 84965145Storek esperror(sc, "dma done w/o dmaactive"); 85055138Storek panic("espact"); 85155138Storek } 85255138Storek sc->sc_dmaactive = 0; 85365145Storek 85465145Storek /* Finish computing residual count. */ 85565145Storek reg = esp->esp_tcl | (esp->esp_tch << 8); 85655138Storek if (reg == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 85755138Storek reg = 65536; 85865145Storek resid += reg; 85965145Storek 86065145Storek /* Compute xfer count (requested - resid). */ 86165145Storek i = sc->sc_dmasize - resid; 86265145Storek if (i < 0) { 86355138Storek printf("%s: xfer resid (%d) > xfer req (%d)\n", 86465145Storek sc->sc_dev.dv_xname, resid, sc->sc_dmasize); 86565145Storek i = sc->sc_dmasize; /* forgiving... */ 86655138Storek } 86765145Storek 86865145Storek /* If data came in we must flush cache. */ 86955138Storek if (sc->sc_state == S_DI) 87065145Storek cache_flush(sc->sc_dmaaddr, i); 87165145Storek sc->sc_dmaaddr += i; 87265145Storek sc->sc_resid -= i; 87355138Storek if ((sc->sc_espintr & ESPINTR_SVC) == 0) { 87465145Storek esperror(sc, "no bus service req"); 87555138Storek return (ACT_RESET); 87655138Storek } 87755138Storek break; 87855138Storek 87955138Storek case S_STAT: 88055138Storek /* 88155138Storek * The last thing we did was tell it `initiator complete' 88255138Storek * and so we expect to have gotten both the status byte 88355138Storek * and the final message byte. It is possible that we 88455138Storek * got something else.... 88555138Storek * 88655138Storek * Apparently, BUS SERVICE is set if we got just status, 88755138Storek * while FUNCTION COMPLETE is set if we got both. 88855138Storek */ 88955138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != ESPINTR_CMP) { 89055138Storek esperror(sc, "bad status interrupt state"); 89155138Storek return (ACT_RESET); 89255138Storek } 89365145Storek reg = ESP_NFIFO(sc->sc_espfflags); 89455138Storek if (reg < 2) { 89555138Storek printf( 89665145Storek "%s: command done but fifo count = %d; must be >= 2\n", 89765145Storek sc->sc_dev.dv_xname, reg); 89855138Storek return (ACT_RESET); 89955138Storek } 90055138Storek /* 90155138Storek * Read the status and the first msg byte. 90255138Storek * It should be CMD_COMPLETE. Eventually we 90355138Storek * may handle IDENTIFY, DISCONNECT, etc., as well. 90455138Storek */ 90555138Storek sc->sc_stat[0] = esp->esp_fifo; 90655138Storek sc->sc_msg[0] = reg = esp->esp_fifo; 90755138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT; 90855138Storek if (reg == MSG_CMD_COMPLETE) { 90955138Storek sc->sc_state = S_FI; 91055138Storek return (ACT_CONT); 91155138Storek } 91255138Storek if (SCSIMSGLEN(reg) != 1) { 91355138Storek printf("%s: target %d is naughty\n", 91465145Storek sc->sc_dev.dv_xname, sc->sc_targ); 91555138Storek return (ACT_RESET); 91655138Storek } 91755138Storek printf("%s: warning: target %d returned msg 0x%x\n", 91865145Storek sc->sc_dev.dv_xname, sc->sc_targ, reg); 91955138Storek sc->sc_state = S_FI; 92055138Storek return (ACT_CONT); 92155138Storek 92255138Storek case S_MI: 92355138Storek if ((reg & ESPINTR_SVC) == 0) { 92455138Storek esperror(sc, "missing phase after msg in"); 92555138Storek return (ACT_RESET); 92655138Storek } 92765145Storek reg = ESP_NFIFO(sc->sc_espfflags); 92855138Storek for (i = 0; i < reg; i++) 92955138Storek sc->sc_msg[i] = esp->esp_fifo; 93055138Storek break; 93155138Storek 93255138Storek case S_FI: 93355138Storek esperror(sc, "target did not disconnect"); 93455138Storek return (ACT_RESET); 93555138Storek } 93655138Storek 93755138Storek /* 93855138Storek * Things are still moving along. The phase tells us 93955138Storek * what the device wants next. Do it. 94055138Storek */ 94165145Storek switch (sc->sc_espstat & ESPSTAT_PHASE) { 94255138Storek 94355138Storek case ESPPHASE_DATA_OUT: 94465145Storek if (!sc->sc_sentcmd) esperror(sc, "DIAG: data out without command"); 94565145Storek if (sc->sc_dmactl & DMA_READ) { 94665145Storek esperror(sc, "wrong phase (want to read)"); 94765145Storek return (ACT_RESET); 94865145Storek } 94965145Storek newstate = S_DO; 95065145Storek goto do_data_xfer; 95155138Storek 95255138Storek case ESPPHASE_DATA_IN: 95365145Storek if (!sc->sc_sentcmd) esperror(sc, "DIAG: data in without command"); 95465145Storek if (!(sc->sc_dmactl & DMA_READ)) { 95565145Storek esperror(sc, "wrong phase (want to write)"); 95665145Storek return (ACT_RESET); 95765145Storek } 95865145Storek newstate = S_DI; 95965145Storek do_data_xfer: 96065145Storek if (sc->sc_resid == 0) { 96165145Storek esperror(sc, "data count error"); 96265145Storek return (ACT_RESET); 96365145Storek } 96455138Storek 96565145Storek /* 96665145Storek * Compute DMA count based on chip limits. 96765145Storek * Set DMA address and load transfer count into 96865145Storek * ESP via DMA NOP, then set DMA control, and 96965145Storek * then we can start the DMA. 97065145Storek */ 97165145Storek sc->sc_state = newstate; 97265145Storek i = min(sc->sc_resid, ESPMAX); 97365145Storek i = min(i, DMAMAX(sc->sc_dmaaddr)); 97465145Storek sc->sc_dmasize = i; 97565145Storek dma->dma_addr = sc->sc_dmaaddr; 97665145Storek esp->esp_tch = i >> 8; 97765145Storek esp->esp_tcl = i; 97865145Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 97965145Storek dma->dma_csr = sc->sc_dmactl; 98065145Storek sc->sc_dmaactive = 1; 98165145Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 98265145Storek return (ACT_IO); 98365145Storek 98455138Storek case ESPPHASE_CMD: 98555138Storek /* 98655138Storek * Silly thing wants the command again. 98765145Storek * Load it into the FIFO and go to SVC state. 98855138Storek */ 98965145Storek printf("%s: redoing command\n", sc->sc_dev.dv_xname); 99065145Storek cdb = sc->sc_curcdb; 99155138Storek reg = SCSICMDLEN(cdb->cdb_bytes[0]); 99255138Storek for (i = 0; i < reg; i++) 99355138Storek esp->esp_fifo = cdb->cdb_bytes[i]; 99465145Storek sc->sc_state = S_SVC; 99555138Storek esp->esp_cmd = ESPCMD_XFER_INFO; 99655138Storek return (ACT_CONT); 99755138Storek 99855138Storek case ESPPHASE_STATUS: 99955138Storek sc->sc_state = S_STAT; 100055138Storek esp->esp_cmd = ESPCMD_INIT_COMP; 100155138Storek return (ACT_CONT); 100255138Storek 100355138Storek case ESPPHASE_MSG_IN: 100465145Storek printf("%s: accepting (& ignoring) msg from target %d\n", 100565145Storek sc->sc_dev.dv_xname, sc->sc_targ); 100655138Storek sc->sc_state = S_MI; 100755138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT; 100855138Storek return (ACT_CONT); 100955138Storek 101055138Storek default: 101165145Storek esperror(sc, "bad phase"); 101255138Storek return (ACT_RESET); 101355138Storek } 101455138Storek /* NOTREACHED */ 101555138Storek } 101655138Storek 101755138Storek /* 101855138Storek * Clear out target state by doing a special TEST UNIT READY. 101955138Storek * Note that this calls espicmd (possibly recursively). 102055138Storek */ 102155138Storek void 102255138Storek espclear(sc, targ) 102355138Storek register struct esp_softc *sc; 102455138Storek register int targ; 102555138Storek { 102655138Storek 102755138Storek /* turn off needclear immediately since this calls espicmd() again */ 102855138Storek sc->sc_needclear &= ~(1 << targ); 102955138Storek sc->sc_clearing = 1; 103055138Storek (void) scsi_test_unit_ready(&sc->sc_hba, targ, 0); 103155138Storek sc->sc_clearing = 0; 103255138Storek } 103355138Storek 103455138Storek /* 103565145Storek * THIS SHOULD BE ADJUSTABLE 103665145Storek */ 103765145Storek /* name howlong purpose */ 103865145Storek #define SELECT_WAIT 300000 /* wait for select to complete */ 103965145Storek #define CMD_WAIT 100000 /* wait for next phase, generic */ 104065145Storek #define DATA_WAIT 100000 /* time to xfer data in/out */ 104165145Storek 104265145Storek /* 104355138Storek * Send an `immediate' command, i.e., poll until the whole thing is done. 104465145Storek * Return the status byte from the device, or -1 if we timed out. We use 104565145Storek * DMA to transfer the data as the fifo only moves one byte at a time. 104655138Storek */ 104755138Storek int 104855138Storek espicmd(hba, targ, cdb, buf, len, rw) 104965145Storek struct hba_softc *hba; 105055138Storek int targ; 105165145Storek struct scsi_cdb *cdb; 105255138Storek caddr_t buf; 105365145Storek int len, rw; 105455138Storek { 105555138Storek register struct esp_softc *sc = (struct esp_softc *)hba; 105655138Storek register volatile struct espreg *esp = sc->sc_esp; 105755138Storek register volatile struct dmareg *dma = sc->sc_dma; 1058*67079Storek register int r, s, wait; 1059*67079Storek register struct sq *sq; 106055138Storek 106155138Storek /* 1062*67079Storek * Wait for any ongoing operation to complete. 1063*67079Storek */ 1064*67079Storek s = splbio(); 1065*67079Storek while (sc->sc_state != S_IDLE) { 1066*67079Storek sc->sc_iwant = 1; 1067*67079Storek tsleep((caddr_t)&sc->sc_iwant, PRIBIO, "espicmd", 0); 1068*67079Storek } 1069*67079Storek sc->sc_hba.hba_busy = 1; 1070*67079Storek splx(s); 1071*67079Storek 1072*67079Storek /* 107355138Storek * Clear the target if necessary. 107455138Storek */ 107555138Storek if (sc->sc_needclear & (1 << targ) && !sc->sc_probing) 107655138Storek espclear(sc, targ); 107755138Storek 107855138Storek /* 107965145Storek * Set up DMA transfer control (leaving interrupts disabled). 108055138Storek */ 108165145Storek sc->sc_dmactl = rw & B_READ ? DMA_ENA | DMA_READ : DMA_ENA; 108265145Storek sc->sc_dmaaddr = (u_long)buf; 108365145Storek sc->sc_resid = len; 108465145Storek 108565145Storek /* 108665145Storek * Disable hardware interrupts and start select sequence, 108765145Storek * then loop, calling espact() after each ``interrupt''. 108865145Storek */ 108965145Storek DMAWAIT(dma); /* ??? */ 109065145Storek dma->dma_csr = 0; 109165145Storek espselect(sc, targ, cdb); 109255138Storek wait = SELECT_WAIT; 109355138Storek for (;;) { 109455138Storek r = dma->dma_csr; 109555138Storek if (!DMA_INTR(r)) { 109655138Storek if (--wait < 0) { 109765145Storek esperror(sc, "timeout"); 109865145Storek goto reset; 109955138Storek } 110055138Storek DELAY(1); 110155138Storek continue; 110255138Storek } 110365145Storek sc->sc_espstat = esp->esp_stat; 110465145Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 110565145Storek sc->sc_espintr = esp->esp_intr; 110665145Storek sc->sc_espfflags = esp->esp_fflags; 110765145Storek sc->sc_dmacsr = r; 110865145Storek switch (r = espact(sc)) { 110955138Storek 111055138Storek case ACT_CONT: 111155138Storek case ACT_QUICKINTR: 111265145Storek wait = CMD_WAIT; 111355138Storek break; 111455138Storek 111565145Storek case ACT_IO: 111665145Storek wait = DATA_WAIT; 111755138Storek break; 111855138Storek 111955138Storek case ACT_RESET: 112055138Storek goto reset; 112155138Storek 112255138Storek case ACT_DONE: 1123*67079Storek r = sc->sc_stat[0]; 1124*67079Storek goto done; 112555138Storek 112655138Storek case ACT_ERROR: 1127*67079Storek r = -1; 1128*67079Storek goto done; 112955138Storek 113055138Storek default: 113155138Storek panic("espicmd action"); 113255138Storek } 113355138Storek } 113455138Storek reset: 113565145Storek espreset(sc, RESET_ESPCHIP); /* ??? */ 1136*67079Storek r = -1; 1137*67079Storek done: 1138*67079Storek sc->sc_state = S_IDLE; 1139*67079Storek s = splbio(); 1140*67079Storek if (sc->sc_iwant) { 1141*67079Storek sc->sc_iwant = 0; 1142*67079Storek wakeup((caddr_t)&sc->sc_iwant); 1143*67079Storek } else if ((sq = sc->sc_hba.hba_head) != NULL) { 1144*67079Storek sc->sc_hba.hba_head = sq->sq_forw; 1145*67079Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace); 1146*67079Storek } else 1147*67079Storek sc->sc_hba.hba_busy = 0; 1148*67079Storek splx(s); 1149*67079Storek return (r); 115055138Storek } 115155138Storek 115255138Storek /* 115355138Storek * Dump (write memory, possibly physmem). 115455138Storek * SPARC higher-level dump code always provides virtual addresses, 115555138Storek * so we need not do any I/O mapping here. 115655138Storek */ 115755138Storek int 115855138Storek espdump(hba, targ, cdb, buf, len) 115955138Storek register struct hba_softc *hba; 116055138Storek int targ; 116165145Storek struct scsi_cdb *cdb; 116255138Storek caddr_t buf; 116355138Storek register int len; 116455138Storek { 1165*67079Storek register struct esp_softc *sc = (struct esp_softc *)hba; 116655138Storek 1167*67079Storek /* 1168*67079Storek * If we crashed in the middle of a bus transaction... 1169*67079Storek */ 1170*67079Storek if (sc->sc_state != S_IDLE) 1171*67079Storek espreset(sc, RESET_BOTH); /* ??? */ 117255138Storek return (espicmd(hba, targ, cdb, buf, len, B_WRITE)); 117355138Storek } 117455138Storek 117555138Storek /* 117655138Storek * Allocate resources (SCSI bus and DVMA space) for the given transfer. 117755138Storek * Must be called at splbio(). 117855138Storek * 117955138Storek * THIS SHOULD RETURN SUCCESS/FAIL INDICATION 118055138Storek */ 118155138Storek void 118255138Storek espstart(self, sq, bp, dgo, dev) 118355138Storek struct device *self; 118455138Storek register struct sq *sq; 118555138Storek struct buf *bp; 118655138Storek scdgo_fn dgo; 118755138Storek struct device *dev; 118855138Storek { 118955138Storek register struct esp_softc *sc = (struct esp_softc *)self; 119055138Storek 119155138Storek if (sc->sc_hba.hba_busy == 0) { 119255138Storek /* 119355138Storek * Bus not busy, nothing to do here, just tell 119455138Storek * this target or unit that it has the SCSI bus. 119555138Storek */ 119655138Storek sc->sc_hba.hba_busy = 1; 119765145Storek (*dgo)(dev, &sc->sc_cdbspace); 119855138Storek } else { 119955138Storek /* 120055138Storek * Bus is busy; just enqueue. 120155138Storek */ 120255138Storek sq->sq_dgo = dgo; 120355138Storek sq->sq_dev = dev; 120455138Storek sq->sq_forw = NULL; 120555138Storek if (sc->sc_hba.hba_head == NULL) 120655138Storek sc->sc_hba.hba_head = sq; 120755138Storek else 120855138Storek sc->sc_hba.hba_tail->sq_forw = sq; 120955138Storek sc->sc_hba.hba_tail = sq; 121055138Storek } 121155138Storek } 121255138Storek 121355138Storek /* 121465145Storek * Start buffered I/O. 121555138Storek * Return 0 on success, 1 on failure. 121655138Storek */ 121755138Storek int 121855138Storek espgo(self, targ, intr, dev, bp, pad) 121955138Storek struct device *self; 122055138Storek int targ; 122155138Storek scintr_fn intr; 122255138Storek struct device *dev; 122355138Storek register struct buf *bp; 122455138Storek int pad; 122555138Storek { 122655138Storek register struct esp_softc *sc = (struct esp_softc *)self; 122755138Storek 122855138Storek if (sc->sc_needclear & (1 << targ)) 122955138Storek espclear(sc, targ); 123055138Storek 123165145Storek /* Set up dma control for espact(). */ 123265145Storek sc->sc_dmactl = bp->b_flags & B_READ ? 123365145Storek DMA_ENA | DMA_READ | DMA_IE : DMA_ENA | DMA_IE; 123465145Storek sc->sc_dmaaddr = (u_long)bp->b_un.b_addr; 123565145Storek sc->sc_resid = bp->b_bcount; 123655138Storek 123755138Storek /* 123855138Storek * Enable interrupts and start selection. 123965145Storek * The rest is done in espintr() and espact(). 124055138Storek */ 124155138Storek sc->sc_hba.hba_intr = intr; /* remember dev done function */ 124255138Storek sc->sc_hba.hba_intrdev = dev; /* and its first arg */ 124355138Storek sc->sc_dma->dma_csr = DMA_IE; 124465145Storek espselect(sc, targ, &sc->sc_cdbspace); 124555138Storek return (0); 124655138Storek } 124755138Storek 124855138Storek /* 124955138Storek * Handle interrupt. Return 1 if taken. 125055138Storek */ 125155138Storek int 125255138Storek espintr(sc0) 125355138Storek void *sc0; 125455138Storek { 125555138Storek register struct esp_softc *sc = (struct esp_softc *)sc0; 125655138Storek register volatile struct espreg *esp = sc->sc_esp; 125755138Storek register volatile struct dmareg *dma = sc->sc_dma; 125855138Storek register int r, wait; 125955138Storek register struct sq *sq; 126055138Storek 126155138Storek r = dma->dma_csr; 126255138Storek if (!DMA_INTR(r)) 126355138Storek return (0); /* not ours */ 126459214Storek sc->sc_intrcnt.ev_count++; 126555138Storek 126655138Storek again: 126755138Storek sc->sc_espstat = esp->esp_stat; 126855138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 126955138Storek sc->sc_espintr = esp->esp_intr; 127065145Storek sc->sc_espfflags = esp->esp_fflags; 127155138Storek sc->sc_dmacsr = r; 127255138Storek 127355138Storek if (sc->sc_state == S_IDLE) { 127465145Storek printf("%s: stray interrupt\n", sc->sc_dev.dv_xname); 127555138Storek dma->dma_csr &= ~DMA_IE; /* ??? */ 127655138Storek return (1); 127755138Storek } 127865145Storek switch (r = espact(sc)) { 127955138Storek 128055138Storek case ACT_CONT: /* just return */ 128165145Storek case ACT_IO: 128255138Storek break; 128355138Storek 128455138Storek case ACT_RESET: /* please reset esp */ 128555138Storek reset: 128665145Storek espreset(sc, RESET_ESPCHIP); /* ??? */ 128755138Storek /* FALLTHROUGH */ 128855138Storek 128955138Storek case ACT_DONE: /* this one is done, successfully */ 129055138Storek case ACT_ERROR: /* this one is done due to `severe' error */ 129155138Storek if (!sc->sc_hba.hba_busy) 129255138Storek panic("espintr sq"); 129355138Storek /* 1294*67079Storek * This transaction is done. Call the driver's intr routine. 1295*67079Storek * If an immediate command is pending, let it run in front 1296*67079Storek * of us, otherwise start the next transation. Note that 1297*67079Storek * the interrupt routine may run its own immediate commands 1298*67079Storek * (`request sense' for errors, eg) before we get around to 1299*67079Storek * the process waiting to do immediate command, but that 1300*67079Storek * is OK; if we did not set S_IDLE here we could deadlock. 130155138Storek */ 1302*67079Storek sc->sc_state = S_IDLE; 130355138Storek (*sc->sc_hba.hba_intr)(sc->sc_hba.hba_intrdev, 130455138Storek r == ACT_DONE ? sc->sc_stat[0] : -1, sc->sc_resid); 1305*67079Storek if (sc->sc_iwant) { 1306*67079Storek wakeup((caddr_t)&sc->sc_iwant); 1307*67079Storek sc->sc_iwant = 0; 1308*67079Storek } else if ((sq = sc->sc_hba.hba_head) != NULL) { 130955138Storek sc->sc_hba.hba_head = sq->sq_forw; 131065145Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace); 131155138Storek } else 131255138Storek sc->sc_hba.hba_busy = 0; 131355138Storek break; 131455138Storek 131555138Storek case ACT_QUICKINTR: /* wait a short while for another interrupt */ 131665145Storek printf("%s: quickintr: ", sc->sc_dev.dv_xname); 131755138Storek wait = 100; 131855138Storek do { 131955138Storek r = dma->dma_csr; 132055138Storek if (DMA_INTR(r)) { 132155138Storek printf("got one, wait=%d\n", wait); 132255138Storek goto again; 132355138Storek } 132455138Storek } while (--wait > 0); 132555138Storek printf("did not get one\n"); 132655138Storek break; 132755138Storek 132855138Storek default: 132955138Storek panic("espintr action"); 133055138Storek } 133155138Storek return (1); 133255138Storek } 133355138Storek 133455138Storek /* 133555138Storek * Target or unit decided to let go of the bus early. 133655138Storek */ 133755138Storek void 133855138Storek esprel(self) 133955138Storek struct device *self; 134055138Storek { 134155138Storek register struct esp_softc *sc = (struct esp_softc *)self; 134255138Storek register struct sq *sq; 134355138Storek 134455138Storek /* if there is someone else waiting, give them a crack at it */ 134555138Storek if ((sq = sc->sc_hba.hba_head) != NULL) 134665145Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdbspace); 134755138Storek else 134855138Storek sc->sc_hba.hba_busy = 0; 134955138Storek } 1350