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