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