155138Storek /* 255138Storek * Copyright (c) 1992 The Regents of the University of California. 355138Storek * 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*59327Storek * @(#)esp.c 7.6 (Berkeley) 04/27/93 1755138Storek * 18*59327Storek * 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 6355138Storek #ifdef DEBUG 6455138Storek int espdebug = 1; 6555138Storek #endif 6655138Storek 6755138Storek /* 6855138Storek * This driver is organized as a collection of state machines. The 6955138Storek * primary machine is the SCSI sequencer: 7055138Storek * 7155138Storek * Given some previous SCSI state (as set up or tracked by us earlier) 7255138Storek * and the interrupt registers provided on the chips (dmacsr, espstat, 7355138Storek * espstep, and espintr), derive an action. In many cases this is 7455138Storek * just a matter of reading the target's phase and following its orders, 7555138Storek * which sets a new state. 7655138Storek * 7755138Storek * This sequencing is done in espact(); the state is primed in espselect(). 7855138Storek * 7955138Storek * There will be (update this comment when there is) another state machine 8055138Storek * used to handle transfers that fall afoul of chip limits (16 bit DMA 8155138Storek * counter; 24 bit address counter in 32 bit address field). 8255138Storek * 8355138Storek * Another state bit is used to recover from bus resets: 8455138Storek * 8555138Storek * A single TEST UNIT READY is attempted on each target before any 8655138Storek * real communication begins; this TEST UNIT READY is allowed to 8755138Storek * fail in any way. This is required for the Quantum ProDrive 100 8855138Storek * MB disks, for instance, which respond to their first selection 8955138Storek * with status phase, and for anything that insists on implementing 9055138Storek * the broken SCSI-2 synch transfer initial message. 9155138Storek * 9255138Storek * This is done in espclear() (which calls espselect(); functions that 9355138Storek * call espselect() must check for clearing first). 9455138Storek * 9555138Storek * The state machines actually intermingle, as some SCSI sequences are 9655138Storek * only allowed during clearing. 9755138Storek */ 9855138Storek 9955138Storek /* per-DMA variables */ 10055138Storek struct dma_softc { 10155138Storek struct device sc_dev; /* base device */ 10255138Storek volatile struct dmareg *sc_dma; /* register virtual address */ 103*59327Storek int sc_dmarev; /* revision */ 104*59327Storek char *sc_dmafmt; /* format for error messages */ 105*59327Storek 10655138Storek }; 10755138Storek void dmaattach(struct device *, struct device *, void *); 10855138Storek struct cfdriver dmacd = 10955138Storek { NULL, "dma", matchbyname, dmaattach, DV_DULL, sizeof(struct dma_softc) }; 11055138Storek 11155138Storek /* per-ESP variables */ 11255138Storek struct esp_softc { 11355138Storek /* 11455138Storek * External interfaces. 11555138Storek */ 11655138Storek struct hba_softc sc_hba; /* base device + hba, must be first */ 11755138Storek struct sbusdev sc_sd; /* sbus device */ 11855138Storek struct intrhand sc_ih; /* interrupt entry */ 11959214Storek struct evcnt sc_intrcnt; /* interrupt counter */ 12055138Storek struct dma_softc *sc_dsc; /* pointer to corresponding dma sc */ 12155138Storek 12255138Storek /* 12355138Storek * Addresses mapped to hardware registers. 12455138Storek */ 12555138Storek volatile struct espreg *sc_esp; 12655138Storek volatile struct dmareg *sc_dma; 12755138Storek 12855138Storek /* 12955138Storek * Copies of registers cleared/unlatched by reading. 13055138Storek */ 13155138Storek u_long sc_dmacsr; 13255138Storek u_char sc_espstat; 13355138Storek u_char sc_espstep; 13455138Storek u_char sc_espintr; 13555138Storek 13655138Storek /* miscellaneous */ 13755138Storek int sc_clockfreq; /* clock frequency */ 13855138Storek u_char sc_sel_timeout; /* select timeout */ 13955138Storek u_char sc_id; /* initiator ID (default = 7) */ 14055138Storek u_char sc_needclear; /* uncleared targets (1 bit each) */ 14155138Storek u_char sc_esptype; /* 100, 100A, 2xx (see below) */ 14255138Storek u_char sc_ccf; /* clock conversion factor */ 14355138Storek u_char sc_conf1; /* value for config reg 1 */ 14455138Storek u_char sc_conf2; /* value for config reg 2 */ 14555138Storek u_char sc_conf3; /* value for config reg 3 */ 14659214Storek struct bootpath *sc_bp; /* esp bootpath so far */ 14755138Storek 14855138Storek /* 14955138Storek * Information pertaining to the current transfer, 15055138Storek * including sequencing. 15155138Storek * 15255138Storek * The size of sc_msg is the size of the ESP fifo, 15355138Storek * since we do message-in simply by allowing the fifo to fill. 15455138Storek */ 15555138Storek char sc_probing; /* used during autoconf; see below */ 15655138Storek char sc_clearing; /* true => cmd is just to clear targ */ 15755138Storek char sc_state; /* SCSI protocol state; see below */ 15855138Storek char sc_sentcmd; /* set once we get cmd out */ 15955138Storek char sc_dmaactive; /* true => doing dma */ 16055138Storek #ifdef notyet 16155138Storek u_char sc_sync; /* synchronous transfer stuff (?) */ 16255138Storek #endif 16355138Storek u_char sc_stat[2]; /* status from last `status' phase */ 16455138Storek u_char sc_msg[16]; /* message from device */ 16555138Storek u_short sc_dmactl; /* control to load into dma csr */ 16655138Storek u_long sc_dmaaddr; /* addr to load into dma addr */ 16755138Storek int sc_targ; /* the target involved */ 16855138Storek int sc_resid; /* count of bytes not xferred */ 16955138Storek struct scsi_cdb sc_cdb; /* current command (not in dvma) */ 17055138Storek }; 17155138Storek 17255138Storek /* 17355138Storek * Values for sc_esptype (used to control configuration reset). 17455138Storek * The order is important; see espreset(). 17555138Storek */ 17655138Storek #define ESP100 0 17755138Storek #define ESP100A 1 17855138Storek #define ESP2XX 2 17955138Storek 18055138Storek /* 18155138Storek * Probe state. 0 means not probing. While looking for each target 18255138Storek * we set this to PROBE_TESTING and do a TEST UNIT READY on unit 0. 18355138Storek * If selection fails, this is changed to PROBE_NO_TARGET; otherwise 18455138Storek * we assume the target exists, regardless of the result of the test. 18555138Storek */ 18655138Storek #define PROBE_TESTING 1 18755138Storek #define PROBE_NO_TARGET 2 18855138Storek 18955138Storek /* 19055138Storek * States in sc_state. 19155138Storek * 19255138Storek * Note that S_CMDSVC is rare: normally we load the SCSI command into the 19355138Storek * ESP fifo and get interrupted only when the device has gone to data 19455138Storek * or status phase. If the device wants to play games, though, we end 19555138Storek * up doing things differently. 19655138Storek */ 19755138Storek char *espstates[] = { 19855138Storek #define S_IDLE 0 /* not doing anything */ 19955138Storek "idle", 20055138Storek #define S_SEL 1 /* expecting select done interrupt */ 20155138Storek "selecting", 20255138Storek #define S_CMDSVC 2 /* expecting service req interrupt */ 20355138Storek "waiting for service request after command", 20455138Storek #define S_IOSVC 3 /* expecting service req interrupt */ 20555138Storek "waiting for service request after io", 20655138Storek #define S_DI 4 /* expecting data-in done interrupt */ 20755138Storek "receiving data", 20855138Storek #define S_DO 5 /* expecting data-out done interrupt */ 20955138Storek "sending data", 21055138Storek #define S_STAT 6 /* expecting status done interrupt */ 21155138Storek "receiving status", 21255138Storek #define S_MI 7 /* expecting message-in done interrupt */ 21355138Storek "receiving message", 21455138Storek #define S_FI 8 /* expecting final disconnect interrupt */ 21555138Storek "waiting for disconnect" 21655138Storek }; 21755138Storek 21855138Storek /* 21955138Storek * Return values from espact(). 22055138Storek */ 22155138Storek #define ACT_CONT 0 /* espact() handled everything */ 22255138Storek #define ACT_READ 1 /* target said it is sending us data */ 22355138Storek #define ACT_WRITE 2 /* target said it is expecting data */ 22455138Storek #define ACT_DONE 3 /* handled everything, and op is now done */ 22555138Storek #define ACT_ERROR 4 /* an error occurred, op has been trashed */ 22655138Storek #define ACT_RESET 5 /* please reset ESP, then do ACT_ERROR */ 22755138Storek #define ACT_QUICKINTR 6 /* another interrupt is expected immediately */ 22855138Storek 22955138Storek /* autoconfiguration driver */ 23055138Storek void espattach(struct device *, struct device *, void *); 23155138Storek struct cfdriver espcd = 23259214Storek { NULL, "esp", matchbyname, espattach, DV_DULL, sizeof(struct esp_softc), 23359214Storek "intr" }; 23455138Storek 23555138Storek /* Sbus driver */ 23655138Storek void espsbreset(struct device *); 23755138Storek 23855138Storek /* interrupt interface */ 23955138Storek int espintr(void *); 24055138Storek 24155138Storek /* SCSI HBA driver */ 24255138Storek int espicmd(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int, int); 24355138Storek int espdump(struct hba_softc *, int, struct scsi_cdb *, caddr_t, int); 24455138Storek void espstart(struct device *, struct sq *, struct buf *, 24555138Storek scdgo_fn, struct device *); 24655138Storek int espgo(struct device *, int, scintr_fn, struct device *, 24755138Storek struct buf *, int); 24855138Storek void esprel(struct device *); 24955138Storek void esphbareset(struct hba_softc *, int); 25055138Storek static struct hbadriver esphbadriver = 25155138Storek { espicmd, espdump, espstart, espgo, esprel, esphbareset }; 25255138Storek 25355138Storek /* forward declarations */ 25455138Storek static void espdoattach(int unit); 25555138Storek static void espreset(struct esp_softc *); 25655138Storek 25755138Storek /* 25855138Storek * The transfer size is limited to 16 bits since the scsi ctrl transfer 25955138Storek * counter is only 2 bytes. A 0 value means the biggest transfer size 26055138Storek * (2 ** 16) == 64k. 26155138Storek */ 26255138Storek #define MAX_TRANSFER_SIZE (64 * 1024) 26355138Storek 26455138Storek /* Return true if this transfer will cross a dma boundary */ 26555138Storek #define CROSS_DMA(addr, len) \ 26655138Storek (((int)(addr) & 0xff000000) != (((int)(addr) + (len) - 1) & 0xff000000)) 26755138Storek 26855138Storek /* 26955138Storek * Attach a found DMA chip. 27055138Storek * The second argument is really a pointer to an sbus_attach_args. 27155138Storek */ 27255138Storek void 27355138Storek dmaattach(parent, dev, args) 27455138Storek struct device *parent; 27555138Storek struct device *dev; 27655138Storek void *args; 27755138Storek { 27855138Storek register struct dma_softc *dsc = (struct dma_softc *)dev; 27955138Storek register struct sbus_attach_args *sa = args; 28055138Storek register volatile struct dmareg *dma; 28155138Storek register int rev; 28255138Storek struct esp_softc *esc; 28355138Storek 28455138Storek if (sa->sa_ra.ra_vaddr) 28555138Storek dma = (volatile struct dmareg *)sa->sa_ra.ra_vaddr; 28655138Storek else 28755138Storek dma = (volatile struct dmareg *) 28855138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct dmareg)); 28955138Storek dsc->sc_dma = dma; 29055138Storek 29155138Storek switch (rev = DMA_REV(dma->dma_csr)) { 29255138Storek case DMAREV_1: 29355138Storek printf(": rev 1\n"); 294*59327Storek dsc->sc_dmafmt = DMA_REV1_BITS; 29555138Storek break; 29655138Storek case DMAREV_2: 29755138Storek printf(": rev 2\n"); 298*59327Storek dsc->sc_dmafmt = DMA_REV2_BITS; 29955138Storek break; 300*59327Storek case DMAREV_3: 301*59327Storek printf(": rev 3\n"); 302*59327Storek printf("WARNING: esp.c not yet updated for rev 3\n"); 303*59327Storek dsc->sc_dmafmt = DMA_REV3_BITS; 304*59327Storek break; 30555138Storek default: 306*59327Storek printf(": unknown revision code 0x%x\n", rev); 307*59327Storek dsc->sc_dmafmt = DMA_REV3_BITS; /* cross fingers */ 30855138Storek break; 30955138Storek } 310*59327Storek dsc->sc_dmarev = rev; 31155138Storek espdoattach(dsc->sc_dev.dv_unit); 31255138Storek } 31355138Storek 31455138Storek /* 31555138Storek * Attach a found ESP chip. Search for targets; attach each one found. 31655138Storek * The latter must be deferred if the corresponding dma chip has not yet 31755138Storek * been configured. 31855138Storek */ 31955138Storek void 32055138Storek espattach(parent, self, args) 32155138Storek struct device *parent; 32255138Storek struct device *self; 32355138Storek void *args; 32455138Storek { 32555138Storek register struct esp_softc *sc = (struct esp_softc *)self; 32655138Storek register struct sbus_attach_args *sa = args; 32755138Storek register volatile struct espreg *esp; 32859214Storek register struct bootpath *bp; 32955138Storek struct dma_softc *dsc; 33055138Storek int node, pri, freq, t; 33155138Storek 33255138Storek if (sa->sa_ra.ra_nintr != 1) { 33355138Storek printf(": expected 1 interrupt, got %d\n", sa->sa_ra.ra_nintr); 33455138Storek return; 33555138Storek } 33655138Storek pri = sa->sa_ra.ra_intr[0].int_pri; 33755138Storek printf(" pri %d", pri); 33855138Storek if (sa->sa_ra.ra_vaddr) 33955138Storek esp = (volatile struct espreg *)sa->sa_ra.ra_vaddr; 34055138Storek else 34155138Storek esp = (volatile struct espreg *) 34255138Storek mapiodev(sa->sa_ra.ra_paddr, sizeof(struct espreg)); 34355138Storek sc->sc_esp = esp; 34455138Storek node = sa->sa_ra.ra_node; 34555138Storek sc->sc_id = getpropint(node, "initiator-id", 7); 34655138Storek freq = getpropint(node, "clock-frequency", -1); 34755138Storek if (freq < 0) 34855138Storek freq = ((struct sbus_softc *)sc->sc_hba.hba_dev.dv_parent)->sc_clockfreq; 34955138Storek 35055138Storek /* MIGHT NEED TO RESET ESP CHIP HERE ...? */ 35155138Storek 35255138Storek /* 35355138Storek * Find out whether we have a -100, -100A, or -2xx, 35455138Storek * and what speed it runs at. 35555138Storek */ 35655138Storek sc->sc_conf1 = sc->sc_id | ESPCONF1_PARENB; 35755138Storek /* sc->sc_conf2 = 0; */ 35855138Storek /* sc->sc_conf3 = 0; */ 35955138Storek esp->esp_conf1 = sc->sc_conf1; 36055138Storek esp->esp_conf2 = 0; 36155138Storek esp->esp_conf2 = ESPCONF2_SCSI2 | ESPCONF2_RPE; 36255138Storek if ((esp->esp_conf2 & ~ESPCONF2_RSVD) != 36355138Storek (ESPCONF2_SCSI2 | ESPCONF2_RPE)) { 36455138Storek printf(": ESP100"); 36555138Storek sc->sc_esptype = ESP100; 36655138Storek } else { 36755138Storek esp->esp_conf2 = 0; 36855138Storek esp->esp_conf3 = 0; 36955138Storek esp->esp_conf3 = 5; 37055138Storek if (esp->esp_conf3 != 5) { /* XXX def bits */ 37155138Storek printf(": ESP100A"); 37255138Storek sc->sc_esptype = ESP100A; 37355138Storek } else { 37455138Storek esp->esp_conf3 = 0; 37555138Storek printf(": ESP2XX"); 37655138Storek sc->sc_esptype = ESP2XX; 37755138Storek } 37855138Storek } 37955138Storek printf(", clock = %s MHz, ID = %d\n", clockfreq(freq), sc->sc_id); 38055138Storek 38155138Storek /* 38255138Storek * Set clock conversion factor and select timeout. 38355138Storek * N.B.: clock frequency is not actually used in the rest 38455138Storek * of the driver; I calculate it here for completeness only 38555138Storek * (so I can see it when debugging). 38655138Storek */ 38755138Storek sc->sc_clockfreq = freq; 38855138Storek freq = howmany(freq, 1000 * 1000); /* convert to MHz */ 38955138Storek t = ESPCCF_FROMMHZ(freq); 39055138Storek if (t < ESPCCF_MIN) 39155138Storek t = ESPCCF_MIN; 39255138Storek sc->sc_ccf = t; 39355138Storek t = ESPTIMO_REGVAL(250, t, freq); /* timeout = 250 ms. */ 39455138Storek if (t >= 256) 39555138Storek t = 0; 39655138Storek sc->sc_sel_timeout = t; 39755138Storek 39855138Storek /* 39955138Storek * Link into sbus; set interrupt handler. 40055138Storek */ 40155138Storek sc->sc_sd.sd_reset = espsbreset; 40255138Storek sbus_establish(&sc->sc_sd, &sc->sc_hba.hba_dev); 40355138Storek sc->sc_ih.ih_fun = espintr; 40455138Storek sc->sc_ih.ih_arg = sc; 40555138Storek intr_establish(pri, &sc->sc_ih); 40659214Storek evcnt_attach(&sc->sc_hba.hba_dev, "intr", &sc->sc_intrcnt); 40759214Storek 40859214Storek #define SAME_ESP(bp, sa) \ 40959214Storek ((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \ 41059214Storek (bp->val[0] == -1 && bp->val[1] == sc->sc_hba.hba_dev.dv_unit)) 41159214Storek 41259214Storek bp = sa->sa_ra.ra_bp; 41359214Storek if (bp != NULL && strcmp(bp->name, "esp") == 0 && SAME_ESP(bp, sa)) 41459214Storek sc->sc_bp = bp + 1; 41555138Storek espdoattach(sc->sc_hba.hba_dev.dv_unit); 41655138Storek } 41755138Storek 41855138Storek /* 41955138Storek * `Final' attach of esp occurs once esp and dma chips have been found 42055138Storek * and assigned virtual addresses. Set up the ESP SCSI data structures 42155138Storek * and probe the SCSI bus. 42255138Storek */ 42355138Storek static void 42455138Storek espdoattach(unit) 42555138Storek int unit; 42655138Storek { 42755138Storek register struct esp_softc *sc; 42855138Storek register struct dma_softc *dsc; 42959214Storek register struct bootpath *bp; 43059214Storek register struct targ *t; 43159214Storek register int targ, u; 43255138Storek 43355138Storek /* make sure we have both */ 43455138Storek if (espcd.cd_ndevs <= unit || 43555138Storek dmacd.cd_ndevs <= unit || 43655138Storek (sc = espcd.cd_devs[unit]) == NULL || 43755138Storek (dsc = dmacd.cd_devs[unit]) == NULL) 43855138Storek return; 43955138Storek sc->sc_dsc = dsc; 44055138Storek sc->sc_dma = dsc->sc_dma; 44155138Storek sc->sc_hba.hba_driver = &esphbadriver; 44255138Storek 44355138Storek espreset(sc); 44455138Storek 44555138Storek /* MAYBE THIS SHOULD BE MOVED TO scsi_subr.c? */ 44655138Storek for (targ = 0; targ < 8; targ++) { 44755138Storek if (targ == sc->sc_id) 44855138Storek continue; 44955138Storek sc->sc_probing = PROBE_TESTING; 45055138Storek sc->sc_clearing = 1; 45155138Storek (void) scsi_test_unit_ready(&sc->sc_hba, targ, 0); 45255138Storek if (sc->sc_probing != PROBE_NO_TARGET) { 45355138Storek sc->sc_probing = 0; 45455138Storek sc->sc_clearing = 0; 45555138Storek SCSI_FOUNDTARGET(&sc->sc_hba, targ); 45655138Storek } 45755138Storek } 45855138Storek sc->sc_probing = 0; 45955138Storek sc->sc_clearing = 0; 46059214Storek 46159214Storek if ((bp = sc->sc_bp) == NULL || (u_int)(targ = bp->val[0]) >= 8 || 46259214Storek (u_int)(u = bp->val[1]) >= 8) 46359214Storek return; 46459214Storek 46559214Storek /* 46659214Storek * Did we find it? We could compare bp->name against the unit's 46759214Storek * name but there's no real need since a target and unit 46859214Storek * uniquely specify a scsi device. 46959214Storek */ 47059214Storek if ((t = sc->sc_hba.hba_targets[targ]) != NULL && t->t_units[u] != NULL) 47159214Storek bootdv = t->t_units[u]->u_dev; 47255138Storek } 47355138Storek 47455138Storek /* 47555138Storek * Internal DMA reset. 47655138Storek */ 47755138Storek static void 47855138Storek dmareset(sc) 47955138Storek struct esp_softc *sc; 48055138Storek { 48155138Storek register volatile struct dmareg *dma = sc->sc_dma; 48255138Storek 48355138Storek /* reset DMA chip */ 48455138Storek dma->dma_csr |= DMA_RESET; 48555138Storek DELAY(200); 48655138Storek dma->dma_csr &= ~DMA_RESET; /* ??? */ 48755138Storek sc->sc_state = S_IDLE; 48855138Storek sc->sc_dmaactive = 0; 489*59327Storek if (sc->sc_dsc->sc_dmarev == DMAREV_2 && sc->sc_esptype != ESP100) 490*59327Storek dma->dma_csr |= DMA_TURBO; 49155138Storek dma->dma_csr |= DMA_IE; /* enable interrupts */ 49255138Storek DELAY(200); 49355138Storek } 49455138Storek 49555138Storek /* 49655138Storek * Reset the chip. N.B.: this causes a SCSI bus reset! 49755138Storek */ 49855138Storek static void 49955138Storek espreset(sc) 50055138Storek register struct esp_softc *sc; 50155138Storek { 50255138Storek register volatile struct espreg *esp = sc->sc_esp; 50355138Storek 50455138Storek dmareset(sc); 50555138Storek esp->esp_cmd = ESPCMD_RESET_CHIP; 50655138Storek DELAY(200); 50755138Storek esp->esp_cmd = ESPCMD_NOP; 50855138Storek DELAY(200); 50955138Storek 51055138Storek /* 51155138Storek * Reload configuration registers (cleared by RESET_CHIP command). 51255138Storek * Reloading conf2 on an ESP100 goofs it up, so out of paranoia 51355138Storek * we load only the registers that exist. 51455138Storek */ 51555138Storek esp->esp_conf1 = sc->sc_conf1; 51655138Storek if (sc->sc_esptype > ESP100) { /* 100A, 2XX */ 51755138Storek esp->esp_conf2 = sc->sc_conf2; 51855138Storek if (sc->sc_esptype > ESP100A) /* 2XX only */ 51955138Storek esp->esp_conf3 = sc->sc_conf3; 52055138Storek } 52155138Storek esp->esp_ccf = sc->sc_ccf; 52255138Storek esp->esp_timeout = sc->sc_sel_timeout; 52355138Storek /* We set synch offset later. */ 52455138Storek 52555138Storek sc->sc_needclear = 0xff; 52655138Storek } 52755138Storek 52855138Storek /* 52955138Storek * Reset the SCSI bus and, optionally, all attached targets. 53055138Storek * The chip should retain most of its parameters (including esp_ccf) 53155138Storek * across this kind of reset (see section 3.5 of Emulex documentation). 53255138Storek */ 53355138Storek void 53455138Storek esphbareset(hba, resetunits) 53555138Storek struct hba_softc *hba; 53655138Storek int resetunits; 53755138Storek { 53855138Storek register struct esp_softc *sc = (struct esp_softc *)hba; 53955138Storek register volatile struct espreg *esp = sc->sc_esp; 54055138Storek 54155138Storek dmareset(sc); 54255138Storek 54355138Storek /* BEGIN ??? */ 54455138Storek /* turn off scsi bus reset interrupts and reset scsi bus */ 54555138Storek esp->esp_conf1 = sc->sc_conf1 | ESPCONF1_REPORT; 54655138Storek DELAY(200); 54755138Storek esp->esp_cmd = ESPCMD_RESET_BUS; 54855138Storek DELAY(800); 54955138Storek esp->esp_cmd = ESPCMD_NOP; 55055138Storek DELAY(200); 55155138Storek esp->esp_conf1 = sc->sc_conf1; 55255138Storek /* END ??? */ 55355138Storek 55455138Storek sc->sc_needclear = 0xff; 55555138Storek 55655138Storek if (resetunits) 55755138Storek scsi_reset_units(&sc->sc_hba); 55855138Storek } 55955138Storek 56055138Storek /* 56155138Storek * Reset the esp, after an Sbus reset. 56255138Storek * Also resets corresponding dma chip. 56355138Storek * 56455138Storek * THIS ROUTINE MIGHT GO AWAY 56555138Storek */ 56655138Storek void 56755138Storek espsbreset(dev) 56855138Storek struct device *dev; 56955138Storek { 57055138Storek struct esp_softc *sc = (struct esp_softc *)dev; 57155138Storek 57255138Storek if (sc->sc_dsc) { 57355138Storek printf(" %s %s", sc->sc_dsc->sc_dev.dv_xname, 57455138Storek sc->sc_hba.hba_dev.dv_xname); 57555138Storek esphbareset(&sc->sc_hba, 1); 57655138Storek } 57755138Storek } 57855138Storek 57955138Storek static void 58055138Storek esperror(sc, err) 58155138Storek char *err; 58255138Storek register struct esp_softc *sc; 58355138Storek { 58455138Storek 58555138Storek printf("%s: %s (target=%d): stat=%b step=%x dmacsr=%b intr=%b\n", 58655138Storek sc->sc_hba.hba_dev.dv_xname, err, sc->sc_targ, 58755138Storek sc->sc_espstat, ESPSTAT_BITS, sc->sc_espstep, 588*59327Storek sc->sc_dmacsr, sc->sc_dsc->sc_dmafmt, 589*59327Storek sc->sc_espintr, ESPINTR_BITS); 59055138Storek } 59155138Storek 59255138Storek /* 59355138Storek * An interrupt has occurred. Sequence through the SCSI state machine. 59455138Storek * Return the action to take. 59555138Storek * 59655138Storek * Most of the work happens here. 59755138Storek * 59855138Storek * There are three interrupt sources: 59955138Storek * -- ESP interrupt request (typically, some device wants something). 60055138Storek * -- DMA memory error. 60155138Storek * -- DMA byte count has reached 0 (we do not often want this one but 60255138Storek * can only turn it off in rev 2 DMA chips, it seems). 60355138Storek * DOES THIS OCCUR AT ALL HERE? THERE IS NOTHING TO HANDLE IT! 60455138Storek */ 60555138Storek static int 60655138Storek espact(sc, esp, dma, cdb) 60755138Storek register struct esp_softc *sc; 60855138Storek register volatile struct espreg *esp; 60955138Storek register volatile struct dmareg *dma; 61055138Storek register struct scsi_cdb *cdb; 61155138Storek { 61255138Storek register char *xname = sc->sc_hba.hba_dev.dv_xname; 61355138Storek register int reg, phase, i; 61455138Storek 61555138Storek /* check various error conditions, using as little code as possible */ 61655138Storek if (sc->sc_dmacsr & DMA_EP) { 61755138Storek esperror(sc, "DMA error"); 61855138Storek dma->dma_csr |= DMA_FLUSH; 61955138Storek return (ACT_ERROR); 62055138Storek } 62155138Storek reg = sc->sc_espstat; 62255138Storek if (reg & ESPSTAT_GE) { 62355138Storek /* 62455138Storek * This often occurs when there is no target. 62555138Storek * (See DSC code below.) 62655138Storek */ 62755138Storek if (sc->sc_espintr & ESPINTR_DSC && 62855138Storek sc->sc_state == S_SEL && sc->sc_probing) { 62955138Storek sc->sc_probing = PROBE_NO_TARGET; 63055138Storek return (ACT_RESET); 63155138Storek } 63255138Storek esperror(sc, "DIAGNOSTIC: gross error (ignored)"); 63355138Storek } 63455138Storek if (reg & ESPSTAT_PE) { 63555138Storek esperror(sc, "parity error"); 63655138Storek return (ACT_RESET); 63755138Storek } 63855138Storek reg = sc->sc_espintr; 63955138Storek #define ERR (ESPINTR_SBR|ESPINTR_ILC|ESPINTR_RSL|ESPINTR_SAT|ESPINTR_SEL) 64055138Storek if (reg & ERR) { 64155138Storek if (reg & ESPINTR_SBR) 64255138Storek esperror(sc, "scsi bus reset"); 64355138Storek else if (reg & ESPINTR_ILC) 64455138Storek esperror(sc, "illegal command (driver bug)"); 64555138Storek else { 64655138Storek printf("%s: target %d", xname, sc->sc_targ); 64755138Storek if (reg & ESPINTR_RSL) 64855138Storek printf(" tried to reselect;"); 64955138Storek if (reg & ESPINTR_SAT) 65055138Storek printf(" selected with ATN;"); 65155138Storek if (reg & ESPINTR_SEL) 65255138Storek printf(" selected us as target;"); 65355138Storek printf("we do not allow this yet\n"); 65455138Storek } 65555138Storek return (ACT_ERROR); 65655138Storek } 65755138Storek #undef ERR 65855138Storek 65955138Storek /* 66055138Storek * Disconnect currently only allowed in `final interrupt' states. 66155138Storek */ 66255138Storek if (reg & ESPINTR_DSC) { 66355138Storek if (sc->sc_state == S_FI) 66455138Storek return (ACT_DONE); 66555138Storek /* 66655138Storek * If we were doing a select just to test the existence 66755138Storek * of the target, note that it did not respond; otherwise 66855138Storek * gripe. 66955138Storek */ 67055138Storek if (sc->sc_state == S_SEL) { 67155138Storek if (sc->sc_probing) { 67255138Storek sc->sc_probing = PROBE_NO_TARGET; 67355138Storek return (ACT_RESET); 67455138Storek } 67555138Storek } 67655138Storek /* flush fifo, in case we were selecting or sending data */ 67755138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 67855138Storek printf("%s: target %d not responding\n", 67955138Storek xname, sc->sc_targ); 68055138Storek return (ACT_ERROR); 68155138Storek } 68255138Storek 68355138Storek /* 68455138Storek * Okay, things are moving along. 68555138Storek * What were we doing the last time we did something, 68655138Storek * and did it complete normally? 68755138Storek */ 68855138Storek phase = sc->sc_espstat & ESPSTAT_PHASE; 68955138Storek switch (sc->sc_state) { 69055138Storek 69155138Storek case S_SEL: 69255138Storek /* 69355138Storek * We were selecting. Arbitration and select are 69455138Storek * complete (because ESPINTR_DSC was not set), but 69555138Storek * there is no guarantee the command went out. 69655138Storek */ 69755138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != 69855138Storek (ESPINTR_SVC|ESPINTR_CMP)) { 69955138Storek esperror(sc, "selection failed"); 70055138Storek return (ACT_RESET); 70155138Storek } 70255138Storek if (sc->sc_espstep == ESPSTEP_DONE) { 70355138Storek sc->sc_sentcmd = 1; 70455138Storek break; 70555138Storek } 70655138Storek if (sc->sc_espstep == 2) { 70755138Storek /* 70855138Storek * We got something other than command phase. 70955138Storek * Just pretend things are normal; the 71055138Storek * device will ask for the command later. 71155138Storek */ 71255138Storek esperror(sc, "DIAGNOSTIC: esp step 2"); 71355138Storek } else if (sc->sc_espstep == 3) { 71455138Storek /* 71555138Storek * Device entered command phase and then exited it 71655138Storek * before we finished handing out the command. 71755138Storek * Let this happen iff we are trying to clear the 71855138Storek * target state. 71955138Storek */ 72055138Storek esperror(sc, "DIAGNOSTIC: esp step 3"); 72155138Storek if (!sc->sc_clearing) 72255138Storek return (ACT_RESET); 72355138Storek } else { 72455138Storek printf("%s: mysterious esp step %d\n", 72555138Storek xname, sc->sc_espstep); 72655138Storek return (ACT_RESET); 72755138Storek } 72855138Storek /* 72955138Storek * Part of the command may still be lodged in the FIFO. 73055138Storek */ 73155138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 73255138Storek break; 73355138Storek 73455138Storek case S_CMDSVC: 73555138Storek /* 73655138Storek * We were waiting for phase change after stuffing the command 73755138Storek * into the FIFO. Make sure it got out. 73855138Storek */ 73955138Storek reg = ESP_NFIFO(esp); 74055138Storek if (reg) { 74155138Storek esperror(sc, "DIAGNOSTIC: CMDSVC, fifo not empty"); 74255138Storek printf("\tfifo count = %x\n", reg); 74355138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 74455138Storek } else 74555138Storek sc->sc_sentcmd = 1; 74655138Storek break; 74755138Storek 74855138Storek case S_IOSVC: 74955138Storek /* 75055138Storek * We were waiting for phase change after I/O. 75155138Storek */ 75255138Storek break; 75355138Storek 75455138Storek case S_DI: 75555138Storek /* 75655138Storek * We were doing DMA data in, and expecting a 75755138Storek * transfer-count-zero interrupt or a phase change. 75855138Storek * We got that; drain the pack register and 75955138Storek * handle as for data out. 76055138Storek */ 76155138Storek dma->dma_csr |= DMA_DRAIN; 76255138Storek reg = 0; /* FIFO auto flushed? */ 76355138Storek goto dma_data_done; 76455138Storek 76555138Storek case S_DO: 76655138Storek /* 76755138Storek * We were doing DMA data out. If there is data in the 76855138Storek * FIFO, it is stuff that got DMAed out but never made 76955138Storek * it to the device, so it counts as residual. 77055138Storek * 77155138Storek * XXX handle DMA IO with large count or address 77255138Storek * boundary condition by resuming here, or below? 77355138Storek */ 77455138Storek if ((reg = ESP_NFIFO(esp)) != 0) 77555138Storek esp->esp_cmd = ESPCMD_FLUSH_FIFO; 77655138Storek dma_data_done: 77755138Storek if (sc->sc_dmaactive == 0) { 77855138Storek printf("%s: dma done while %s, dmaactive==0\n", 77955138Storek xname, espstates[sc->sc_state]); 78055138Storek panic("espact"); 78155138Storek } 78255138Storek sc->sc_dmaactive = 0; 78355138Storek reg += esp->esp_tcl | (esp->esp_tch << 8); 78455138Storek if (reg == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 78555138Storek reg = 65536; 78655138Storek if (reg > sc->sc_resid) { 78755138Storek printf("%s: xfer resid (%d) > xfer req (%d)\n", 78855138Storek xname, reg, sc->sc_resid); 78955138Storek reg = sc->sc_resid; 79055138Storek } 79155138Storek /* 79255138Storek * If data came in we must flush cache. 79355138Storek */ 79455138Storek if (sc->sc_state == S_DI) 79555138Storek cache_flush(sc->sc_dmaaddr, sc->sc_resid - reg); 79655138Storek sc->sc_resid = reg; 79755138Storek if ((sc->sc_espintr & ESPINTR_SVC) == 0) { 79855138Storek printf("%s: no bus service req\n", xname); 79955138Storek return (ACT_RESET); 80055138Storek } 80155138Storek break; 80255138Storek 80355138Storek case S_STAT: 80455138Storek /* 80555138Storek * The last thing we did was tell it `initiator complete' 80655138Storek * and so we expect to have gotten both the status byte 80755138Storek * and the final message byte. It is possible that we 80855138Storek * got something else.... 80955138Storek * 81055138Storek * Apparently, BUS SERVICE is set if we got just status, 81155138Storek * while FUNCTION COMPLETE is set if we got both. 81255138Storek */ 81355138Storek if ((reg & (ESPINTR_SVC|ESPINTR_CMP)) != ESPINTR_CMP) { 81455138Storek esperror(sc, "bad status interrupt state"); 81555138Storek return (ACT_RESET); 81655138Storek } 81755138Storek reg = ESP_NFIFO(esp); 81855138Storek if (reg < 2) { 81955138Storek printf( 82055138Storek "%s: command done but fifo count = %d; must be >= 2\n", xname, 82155138Storek reg); 82255138Storek return (ACT_RESET); 82355138Storek } 82455138Storek /* 82555138Storek * Read the status and the first msg byte. 82655138Storek * It should be CMD_COMPLETE. Eventually we 82755138Storek * may handle IDENTIFY, DISCONNECT, etc., as well. 82855138Storek */ 82955138Storek sc->sc_stat[0] = esp->esp_fifo; 83055138Storek sc->sc_msg[0] = reg = esp->esp_fifo; 83155138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT; 83255138Storek if (reg == MSG_CMD_COMPLETE) { 83355138Storek sc->sc_state = S_FI; 83455138Storek return (ACT_CONT); 83555138Storek } 83655138Storek if (SCSIMSGLEN(reg) != 1) { 83755138Storek printf("%s: target %d is naughty\n", 83855138Storek xname, sc->sc_targ); 83955138Storek return (ACT_RESET); 84055138Storek } 84155138Storek printf("%s: warning: target %d returned msg 0x%x\n", 84255138Storek xname, sc->sc_targ, reg); 84355138Storek sc->sc_state = S_FI; 84455138Storek return (ACT_CONT); 84555138Storek 84655138Storek case S_MI: 84755138Storek if ((reg & ESPINTR_SVC) == 0) { 84855138Storek esperror(sc, "missing phase after msg in"); 84955138Storek return (ACT_RESET); 85055138Storek } 85155138Storek reg = ESP_NFIFO(esp); 85255138Storek for (i = 0; i < reg; i++) 85355138Storek sc->sc_msg[i] = esp->esp_fifo; 85455138Storek break; 85555138Storek 85655138Storek case S_FI: 85755138Storek esperror(sc, "target did not disconnect"); 85855138Storek return (ACT_RESET); 85955138Storek } 86055138Storek 86155138Storek /* 86255138Storek * Things are still moving along. The phase tells us 86355138Storek * what the device wants next. Do it. 86455138Storek */ 86555138Storek switch (phase) { 86655138Storek 86755138Storek case ESPPHASE_DATA_OUT: 86855138Storek if (!sc->sc_sentcmd) esperror(sc, "DIAGNOSTIC: data out without command"); 86955138Storek sc->sc_state = S_DO; 87055138Storek return (ACT_WRITE); 87155138Storek 87255138Storek case ESPPHASE_DATA_IN: 87355138Storek if (!sc->sc_sentcmd) esperror(sc, "DIAGNOSTIC: data in without command"); 87455138Storek sc->sc_state = S_DI; 87555138Storek return (ACT_READ); 87655138Storek 87755138Storek case ESPPHASE_CMD: 87855138Storek /* 87955138Storek * Silly thing wants the command again. 88055138Storek * Load it into the FIFO and go to CMDSVC state. 88155138Storek */ 88255138Storek printf("%s: redoing command\n", xname); 88355138Storek reg = SCSICMDLEN(cdb->cdb_bytes[0]); 88455138Storek for (i = 0; i < reg; i++) 88555138Storek esp->esp_fifo = cdb->cdb_bytes[i]; 88655138Storek sc->sc_state = S_CMDSVC; 88755138Storek esp->esp_cmd = ESPCMD_XFER_INFO; 88855138Storek return (ACT_CONT); 88955138Storek 89055138Storek case ESPPHASE_STATUS: 89155138Storek sc->sc_state = S_STAT; 89255138Storek esp->esp_cmd = ESPCMD_INIT_COMP; 89355138Storek return (ACT_CONT); 89455138Storek 89555138Storek case ESPPHASE_MSG_IN: 89655138Storek printf("%s: accepting (& ignoring) msg from target %d\n", xname, sc->sc_targ); 89755138Storek sc->sc_state = S_MI; 89855138Storek esp->esp_cmd = ESPCMD_MSG_ACCEPT; 89955138Storek return (ACT_CONT); 90055138Storek 90155138Storek default: 90255138Storek printf("%s: target %d asked for strange phase (%s)\n", 90355138Storek xname, sc->sc_targ, espphases[phase]); 90455138Storek return (ACT_RESET); 90555138Storek } 90655138Storek /* NOTREACHED */ 90755138Storek } 90855138Storek 90955138Storek /* 91055138Storek * Issue a select, loading command into the FIFO. 91155138Storek * Return nonzero on error, 0 if OK. 91255138Storek * Sets state to `selecting'; espact() will sequence state FSM. 91355138Storek */ 91455138Storek void 91555138Storek espselect(sc, esp, targ, cdb) 91655138Storek register struct esp_softc *sc; 91755138Storek register volatile struct espreg *esp; 91855138Storek register int targ; 91955138Storek register struct scsi_cdb *cdb; 92055138Storek { 92155138Storek register int i, cmdlen = SCSICMDLEN(cdb->cdb_bytes[0]); 92255138Storek 92355138Storek sc->sc_targ = targ; 92455138Storek sc->sc_state = S_SEL; 92555138Storek sc->sc_sentcmd = 0; 92655138Storek sc->sc_stat[0] = 0xff; /* ??? */ 92755138Storek sc->sc_msg[0] = 0xff; /* ??? */ 92855138Storek 92955138Storek /* 93055138Storek * Try to talk to target. 93155138Storek * Synch offset 0 => asynchronous transfer. 93255138Storek */ 93355138Storek esp->esp_id = targ; 93455138Storek esp->esp_syncoff = 0; 93555138Storek 93655138Storek /* 93755138Storek * Stuff the command bytes into the fifo. 93855138Storek * Select without attention since we do not do disconnect yet. 93955138Storek */ 94055138Storek for (i = 0; i < cmdlen; i++) 94155138Storek esp->esp_fifo = cdb->cdb_bytes[i]; 94255138Storek esp->esp_cmd = ESPCMD_SEL_NATN; 94355138Storek /* the rest is done elsewhere */ 94455138Storek } 94555138Storek 94655138Storek /* 94755138Storek * THIS SHOULD BE ADJUSTABLE 94855138Storek */ 94955138Storek /* name howlong purpose */ 95055138Storek #define SELECT_WAIT 300000 /* wait for select to complete */ 95155138Storek #define CMD_WAIT 1000 /* wait for next phase, generic */ 95255138Storek #define IO_WAIT 1000000 /* time to xfer data in/out */ 95355138Storek #define POSTDATA_WAIT 10000000 /* wait for next phase, after dataio */ 95455138Storek 95555138Storek /* 95655138Storek * Transfer data out via polling. Return success (0) iff all 95755138Storek * the bytes were sent and we got an interrupt. 95855138Storek * 95955138Storek * This returns -1 on timeout, resid count on early interrupt, 96055138Storek * but no one really cares.... 96155138Storek */ 96255138Storek static int 96355138Storek espixfer_out(sc, esp, dma, buf, len) 96455138Storek register struct esp_softc *sc; 96555138Storek register volatile struct espreg *esp; 96655138Storek register volatile struct dmareg *dma; 96755138Storek register caddr_t buf; 96855138Storek register int len; 96955138Storek { 97055138Storek register int wait, n; 97155138Storek 97255138Storek if (CROSS_DMA(buf, len)) 97355138Storek panic("espixfer_out: 16MB boundary"); 97455138Storek 97555138Storek /* set dma address and transfer count */ 97655138Storek dma->dma_addr = (int)buf; 97755138Storek esp->esp_tch = len >> 8; 97855138Storek esp->esp_tcl = len; 97955138Storek 98055138Storek /* load count into counter via DMA NOP */ 98155138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 98255138Storek 98355138Storek /* enable dma (but not interrupts) */ 98455138Storek dma->dma_csr = DMA_ENA; 98555138Storek 98655138Storek /* and go */ 98755138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 98855138Storek 98955138Storek /* wait for completion */ 99055138Storek for (wait = IO_WAIT; wait > 0; --wait) { 99155138Storek n = dma->dma_csr; 99255138Storek if (DMA_INTR(n)) { 99355138Storek sc->sc_espstat = esp->esp_stat; 99455138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 99555138Storek sc->sc_espintr = esp->esp_intr; 99655138Storek sc->sc_dmacsr = n; 99755138Storek n = esp->esp_tcl | (esp->esp_tch << 8); 99855138Storek if (n == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 99955138Storek n = 65536; 100055138Storek 100155138Storek return (n); 100255138Storek } 100355138Storek DELAY(1); 100455138Storek } 100555138Storek return (-1); 100655138Storek } 100755138Storek 100855138Storek /* 100955138Storek * Transfer data in via polling. 101055138Storek * Return resid count on interrupt, -1 if timed out. 101155138Storek */ 101255138Storek static int 101355138Storek espixfer_in(sc, esp, dma, buf, len) 101455138Storek register struct esp_softc *sc; 101555138Storek register volatile struct espreg *esp; 101655138Storek register volatile struct dmareg *dma; 101755138Storek register caddr_t buf; 101855138Storek register int len; 101955138Storek { 102055138Storek register int wait, n; 102155138Storek 102255138Storek if (CROSS_DMA(buf, len)) 102355138Storek panic("espixfer_in: 16MB boundary"); 102455138Storek 102555138Storek /* set dma address and transfer count */ 102655138Storek dma->dma_addr = (int)buf; 102755138Storek esp->esp_tch = len >> 8; 102855138Storek esp->esp_tcl = len; 102955138Storek 103055138Storek /* load count into counter via DMA NOP */ 103155138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 103255138Storek 103355138Storek /* enable dma (but not interrupts) */ 103455138Storek dma->dma_csr = DMA_ENA | DMA_READ; 103555138Storek 103655138Storek /* and go */ 103755138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 103855138Storek 103955138Storek /* wait for completion */ 104055138Storek for (wait = IO_WAIT; wait > 0; --wait) { 104155138Storek n = dma->dma_csr; 104255138Storek if (DMA_INTR(n)) { 104355138Storek sc->sc_espstat = esp->esp_stat; 104455138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 104555138Storek sc->sc_espintr = esp->esp_intr; 104655138Storek dma->dma_csr |= DMA_DRAIN; 104755138Storek sc->sc_dmacsr = n; 104855138Storek n = esp->esp_tcl | (esp->esp_tch << 8); 104955138Storek if (n == 0 && (sc->sc_espstat & ESPSTAT_TC) == 0) 105055138Storek n = 65536; 105155138Storek 105255138Storek cache_flush(buf, (u_int)len - n); 105355138Storek return (n); 105455138Storek } 105555138Storek DELAY(1); 105655138Storek } 105755138Storek return (-1); 105855138Storek } 105955138Storek 106055138Storek /* 106155138Storek * Clear out target state by doing a special TEST UNIT READY. 106255138Storek * Note that this calls espicmd (possibly recursively). 106355138Storek */ 106455138Storek void 106555138Storek espclear(sc, targ) 106655138Storek register struct esp_softc *sc; 106755138Storek register int targ; 106855138Storek { 106955138Storek 107055138Storek /* turn off needclear immediately since this calls espicmd() again */ 107155138Storek sc->sc_needclear &= ~(1 << targ); 107255138Storek sc->sc_clearing = 1; 107355138Storek (void) scsi_test_unit_ready(&sc->sc_hba, targ, 0); 107455138Storek sc->sc_clearing = 0; 107555138Storek } 107655138Storek 107755138Storek /* 107855138Storek * Send an `immediate' command, i.e., poll until the whole thing is done. 107955138Storek * Return the status byte from the device, or -1 if we timed out. 108055138Storek */ 108155138Storek int 108255138Storek espicmd(hba, targ, cdb, buf, len, rw) 108355138Storek register struct hba_softc *hba; 108455138Storek int targ; 108555138Storek register struct scsi_cdb *cdb; 108655138Storek caddr_t buf; 108755138Storek register int len; 108855138Storek int rw; 108955138Storek { 109055138Storek register struct esp_softc *sc = (struct esp_softc *)hba; 109155138Storek register volatile struct espreg *esp = sc->sc_esp; 109255138Storek register volatile struct dmareg *dma = sc->sc_dma; 109355138Storek register int r, wait; 109455138Storek char *msg; 109555138Storek 109655138Storek if ((unsigned)len > MAX_TRANSFER_SIZE) { 109755138Storek printf("%s: bad length %d\n", sc->sc_hba.hba_dev.dv_xname, len); 109855138Storek panic("espicmd"); 109955138Storek } 110055138Storek 110155138Storek /* 110255138Storek * Clear the target if necessary. 110355138Storek */ 110455138Storek if (sc->sc_needclear & (1 << targ) && !sc->sc_probing) 110555138Storek espclear(sc, targ); 110655138Storek 110755138Storek /* 110855138Storek * Disable hardware interrupts, start select sequence. 110955138Storek * Wait for interrupt-pending bit, then call espact() to 111055138Storek * sequence the state machine. When it tells us to do 111155138Storek * data transfer, we do programmed I/O. 111255138Storek * In any case, we loop calling espact() until done. 111355138Storek */ 111455138Storek dma->dma_csr = 0; /* disable hardware interrupts */ 111555138Storek espselect(sc, esp, targ, cdb); 111655138Storek wait = SELECT_WAIT; 111755138Storek loop: 111855138Storek for (;;) { 111955138Storek r = dma->dma_csr; 112055138Storek if (!DMA_INTR(r)) { 112155138Storek if (--wait < 0) { 112255138Storek msg = "timeout waiting for phase change"; 112355138Storek goto err; 112455138Storek } 112555138Storek DELAY(1); 112655138Storek continue; 112755138Storek } 112855138Storek break; 112955138Storek } 113055138Storek sc->sc_espstat = esp->esp_stat; 113155138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 113255138Storek sc->sc_espintr = esp->esp_intr; 113355138Storek sc->sc_dmacsr = r; 113455138Storek /* 113555138Storek * The action happens `twice around' for read and write. 113655138Storek * All the rest `goto loop' or return or some such. 113755138Storek */ 113855138Storek wait = CMD_WAIT; 113955138Storek for (;;) { 114055138Storek switch (r = espact(sc, esp, dma, cdb)) { 114155138Storek 114255138Storek case ACT_CONT: 114355138Storek case ACT_QUICKINTR: 114455138Storek goto loop; 114555138Storek 114655138Storek case ACT_READ: 114755138Storek if (len == 0 || (rw & B_READ) == 0) { 114855138Storek msg = "wrong phase"; 114955138Storek goto err; 115055138Storek } 115155138Storek r = espixfer_in(sc, esp, dma, buf, len); 115255138Storek if (r < 0) { 115355138Storek msg = "timeout reading from device"; 115455138Storek goto err; 115555138Storek } 115655138Storek buf += len - r; 115755138Storek len = r; 115855138Storek /* we did the io, expecting `generic service' */ 115955138Storek sc->sc_state = S_IOSVC; 116055138Storek wait = POSTDATA_WAIT; 116155138Storek break; 116255138Storek 116355138Storek case ACT_WRITE: 116455138Storek if (len == 0 || rw & B_READ) { 116555138Storek msg = "wrong phase"; 116655138Storek goto err; 116755138Storek } 116855138Storek if (espixfer_out(sc, esp, dma, buf, len)) { 116955138Storek msg = "timeout writing to device"; 117055138Storek goto err; 117155138Storek } 117255138Storek sc->sc_state = S_IOSVC; 117355138Storek wait = POSTDATA_WAIT; 117455138Storek break; 117555138Storek 117655138Storek case ACT_RESET: 117755138Storek sc->sc_state = S_IDLE; 117855138Storek goto reset; 117955138Storek 118055138Storek case ACT_DONE: 118155138Storek sc->sc_state = S_IDLE; 118255138Storek return (sc->sc_stat[0]); 118355138Storek 118455138Storek case ACT_ERROR: 118555138Storek sc->sc_state = S_IDLE; 118655138Storek return (-1); 118755138Storek 118855138Storek default: 118955138Storek panic("espicmd action"); 119055138Storek } 119155138Storek } 119255138Storek err: 119355138Storek printf("%s: target %d: %s (phase = %s)\n", 119455138Storek sc->sc_hba.hba_dev.dv_xname, targ, msg, 119555138Storek espphases[sc->sc_espstat & ESPSTAT_PHASE]); 119655138Storek reset: 119755138Storek espreset(sc); /* ??? */ 119855138Storek return (-1); 119955138Storek } 120055138Storek 120155138Storek /* 120255138Storek * Dump (write memory, possibly physmem). 120355138Storek * SPARC higher-level dump code always provides virtual addresses, 120455138Storek * so we need not do any I/O mapping here. 120555138Storek */ 120655138Storek int 120755138Storek espdump(hba, targ, cdb, buf, len) 120855138Storek register struct hba_softc *hba; 120955138Storek int targ; 121055138Storek register struct scsi_cdb *cdb; 121155138Storek caddr_t buf; 121255138Storek register int len; 121355138Storek { 121455138Storek 121555138Storek return (espicmd(hba, targ, cdb, buf, len, B_WRITE)); 121655138Storek } 121755138Storek 121855138Storek /* 121955138Storek * Allocate resources (SCSI bus and DVMA space) for the given transfer. 122055138Storek * Must be called at splbio(). 122155138Storek * 122255138Storek * THIS SHOULD RETURN SUCCESS/FAIL INDICATION 122355138Storek */ 122455138Storek void 122555138Storek espstart(self, sq, bp, dgo, dev) 122655138Storek struct device *self; 122755138Storek register struct sq *sq; 122855138Storek struct buf *bp; 122955138Storek scdgo_fn dgo; 123055138Storek struct device *dev; 123155138Storek { 123255138Storek register struct esp_softc *sc = (struct esp_softc *)self; 123355138Storek 123455138Storek if (sc->sc_hba.hba_busy == 0) { 123555138Storek /* 123655138Storek * Bus not busy, nothing to do here, just tell 123755138Storek * this target or unit that it has the SCSI bus. 123855138Storek */ 123955138Storek sc->sc_hba.hba_busy = 1; 124055138Storek (*dgo)(dev, &sc->sc_cdb); 124155138Storek } else { 124255138Storek /* 124355138Storek * Bus is busy; just enqueue. 124455138Storek */ 124555138Storek sq->sq_dgo = dgo; 124655138Storek sq->sq_dev = dev; 124755138Storek sq->sq_forw = NULL; 124855138Storek if (sc->sc_hba.hba_head == NULL) 124955138Storek sc->sc_hba.hba_head = sq; 125055138Storek else 125155138Storek sc->sc_hba.hba_tail->sq_forw = sq; 125255138Storek sc->sc_hba.hba_tail = sq; 125355138Storek } 125455138Storek } 125555138Storek 125655138Storek /* 125755138Storek * Send a `dma' command, i.e., send the cdb and use DMA to send the data. 125855138Storek * Return 0 on success, 1 on failure. 125955138Storek */ 126055138Storek int 126155138Storek espgo(self, targ, intr, dev, bp, pad) 126255138Storek struct device *self; 126355138Storek int targ; 126455138Storek scintr_fn intr; 126555138Storek struct device *dev; 126655138Storek register struct buf *bp; 126755138Storek int pad; 126855138Storek { 126955138Storek register struct esp_softc *sc = (struct esp_softc *)self; 127055138Storek register int len = bp->b_bcount; 127155138Storek register u_long addr; 127255138Storek 127355138Storek if ((unsigned)len > MAX_TRANSFER_SIZE) { 127455138Storek printf("%s: %s\n", sc->sc_hba.hba_dev.dv_xname, 127555138Storek len < 0 ? "negative length" : "transfer too big"); 127655138Storek return (1); 127755138Storek } 127855138Storek 127955138Storek if (sc->sc_needclear & (1 << targ)) 128055138Storek espclear(sc, targ); 128155138Storek 128255138Storek /* 128355138Storek * Set dma registers later, on data transfer, 128455138Storek * but compute the contents now. 128555138Storek * COULD JUST REMEMBER bp HERE...? 128655138Storek * 128755138Storek * The DMA chip cannot cross a 16 MB address boundary. 128855138Storek * We should do this as multiple DMA transactions on a 128955138Storek * single SCSI command, but I have not written that yet. 129055138Storek */ 129155138Storek sc->sc_dmactl = bp->b_flags & B_READ ? DMA_ENA | DMA_READ | DMA_IE : 129255138Storek DMA_ENA | DMA_IE; 129355138Storek addr = (u_long)bp->b_un.b_addr; 129455138Storek /* dma chip cannot cross 16MB boundary XXX */ 129555138Storek if (CROSS_DMA(addr, len)) 129655138Storek panic("dma crosses 16MB boundary: fix esp.c"); 129755138Storek sc->sc_dmaaddr = addr; 129855138Storek sc->sc_resid = len; 129955138Storek 130055138Storek /* 130155138Storek * Enable interrupts and start selection. 130255138Storek * The rest is done in our interrupt handler. 130355138Storek */ 130455138Storek sc->sc_hba.hba_intr = intr; /* remember dev done function */ 130555138Storek sc->sc_hba.hba_intrdev = dev; /* and its first arg */ 130655138Storek sc->sc_dma->dma_csr = DMA_IE; 130755138Storek espselect(sc, sc->sc_esp, targ, &sc->sc_cdb); 130855138Storek return (0); 130955138Storek } 131055138Storek 131155138Storek /* 131255138Storek * Handle interrupt. Return 1 if taken. 131355138Storek */ 131455138Storek int 131555138Storek espintr(sc0) 131655138Storek void *sc0; 131755138Storek { 131855138Storek register struct esp_softc *sc = (struct esp_softc *)sc0; 131955138Storek register volatile struct espreg *esp = sc->sc_esp; 132055138Storek register volatile struct dmareg *dma = sc->sc_dma; 132155138Storek register int r, wait; 132255138Storek register struct sq *sq; 132355138Storek 132455138Storek r = dma->dma_csr; 132555138Storek if (!DMA_INTR(r)) 132655138Storek return (0); /* not ours */ 132759214Storek sc->sc_intrcnt.ev_count++; 132855138Storek 132955138Storek again: 133055138Storek sc->sc_espstat = esp->esp_stat; 133155138Storek sc->sc_espstep = esp->esp_step & ESPSTEP_MASK; 133255138Storek sc->sc_espintr = esp->esp_intr; 133355138Storek sc->sc_dmacsr = r; 133455138Storek 133555138Storek if (sc->sc_state == S_IDLE) { 133655138Storek printf("%s: stray interrupt\n", sc->sc_hba.hba_dev.dv_xname); 133755138Storek dma->dma_csr &= ~DMA_IE; /* ??? */ 133855138Storek return (1); 133955138Storek } 134055138Storek switch (r = espact(sc, esp, dma, &sc->sc_cdb)) { 134155138Storek 134255138Storek case ACT_CONT: /* just return */ 134355138Storek break; 134455138Storek 134555138Storek case ACT_READ: 134655138Storek case ACT_WRITE: 134755138Storek /* 134855138Storek * We have to do this ourselves since another 134955138Storek * user of espact() wants to do programmed I/O. 135055138Storek * If we already did dma, and are done, stop. 135155138Storek */ 135255138Storek if (sc->sc_resid == 0) { 135355138Storek printf("%s: target %d sent too much data\n", 135455138Storek sc->sc_hba.hba_dev.dv_xname, sc->sc_targ); 135555138Storek goto reset; 135655138Storek } 135755138Storek sc->sc_dmaactive = 1; 135855138Storek dma->dma_addr = sc->sc_dmaaddr; 135955138Storek esp->esp_tch = sc->sc_resid >> 8; 136055138Storek esp->esp_tcl = sc->sc_resid; 136155138Storek /* load count into counter via DMA NOP */ 136255138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_NOP; 136355138Storek /* enable dma */ 136455138Storek dma->dma_csr = sc->sc_dmactl; 136555138Storek /* and go */ 136655138Storek esp->esp_cmd = ESPCMD_DMA | ESPCMD_XFER_INFO; 136755138Storek break; 136855138Storek 136955138Storek case ACT_RESET: /* please reset esp */ 137055138Storek reset: 137155138Storek espreset(sc); /* ??? */ 137255138Storek /* FALLTHROUGH */ 137355138Storek 137455138Storek case ACT_DONE: /* this one is done, successfully */ 137555138Storek case ACT_ERROR: /* this one is done due to `severe' error */ 137655138Storek sc->sc_state = S_IDLE; 137755138Storek if (!sc->sc_hba.hba_busy) 137855138Storek panic("espintr sq"); 137955138Storek /* 138055138Storek * This transaction is done. 138155138Storek * Call the driver's intr routine, 138255138Storek * then start the next guy if any. 138355138Storek */ 138455138Storek (*sc->sc_hba.hba_intr)(sc->sc_hba.hba_intrdev, 138555138Storek r == ACT_DONE ? sc->sc_stat[0] : -1, sc->sc_resid); 138655138Storek if ((sq = sc->sc_hba.hba_head) != NULL) { 138755138Storek sc->sc_hba.hba_head = sq->sq_forw; 138855138Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdb); 138955138Storek } else 139055138Storek sc->sc_hba.hba_busy = 0; 139155138Storek break; 139255138Storek 139355138Storek case ACT_QUICKINTR: /* wait a short while for another interrupt */ 139455138Storek printf("%s: quickintr: ", sc->sc_hba.hba_dev.dv_xname); 139555138Storek wait = 100; 139655138Storek do { 139755138Storek r = dma->dma_csr; 139855138Storek if (DMA_INTR(r)) { 139955138Storek printf("got one, wait=%d\n", wait); 140055138Storek goto again; 140155138Storek } 140255138Storek } while (--wait > 0); 140355138Storek printf("did not get one\n"); 140455138Storek break; 140555138Storek 140655138Storek default: 140755138Storek panic("espintr action"); 140855138Storek } 140955138Storek return (1); 141055138Storek } 141155138Storek 141255138Storek /* 141355138Storek * Target or unit decided to let go of the bus early. 141455138Storek */ 141555138Storek void 141655138Storek esprel(self) 141755138Storek struct device *self; 141855138Storek { 141955138Storek register struct esp_softc *sc = (struct esp_softc *)self; 142055138Storek register struct sq *sq; 142155138Storek 142255138Storek /* if there is someone else waiting, give them a crack at it */ 142355138Storek if ((sq = sc->sc_hba.hba_head) != NULL) 142455138Storek (*sq->sq_dgo)(sq->sq_dev, &sc->sc_cdb); 142555138Storek else 142655138Storek sc->sc_hba.hba_busy = 0; 142755138Storek } 1428