* Copyright (c) 1988, 1992, 1993
* The Regents of the University of California. All rights reserved.
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
* %sccs.include.redist.c%
* @(#)esp.c 8.4 (Berkeley) %G%
* from: $Header: esp.c,v 1.28 93/04/27 14:40:44 torek Exp $ (LBL)
* Loosely derived from Mary Baker's devSCSIC90.c from the Berkeley
* Sprite project, which is:
* Copyright 1988 Regents of the University of California
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
* from /sprite/src/kernel/dev/sun4c.md/RCS/devSCSIC90.c,v 1.4
* 90/12/19 12:37:58 mgbaker Exp $ SPRITE (Berkeley)
* Sbus ESP/DMA driver. A single driver must be used for both devices
* as they are physically tied to each other: The DMA chip can only
* be used to assist ESP SCSI transactions; the ESP interrupt enable is
* Since DMA and SCSI interrupts are handled in the same routine, the
* DMA device does not declare itself as an sbus device. This saves
#include <dev/scsi/scsi.h>
#include <dev/scsi/scsivar.h>
#include <machine/autoconf.h>
#include <sparc/sbus/dmareg.h>
#include <sparc/sbus/espreg.h>
#include <sparc/sbus/sbusvar.h>
#include <libkern/libkern.h>
* This driver is largely a giant state machine:
* Given some previous SCSI state (as set up or tracked by us
* earlier) and the interrupt registers provided on the chips
* (dmacsr, espstat, espstep, and espintr), derive an action.
* In many cases this is just a matter of reading the target's
* phase and following its orders, which sets a new state.
* This sequencing is done in espact(); the state is primed in espselect().
* Data transfer is always done via DMA. Unfortunately, there are
* limits in the DMA and ESP chips on how much data can be moved
* in a single operation. The ESP chip has a 16-bit counter, so
* it is limited to 65536 bytes. More insidiously, while the DMA
* chip has a 32-bit address, this is composed of a 24-bit counter
* with an 8-bit latch, so it cannot cross a 16 MB boundary. To
* handle these, we program a smaller count than our caller requests;
* when this shorter transfer is done, if the target is still up
* for data transfer, we simply keep going (updating the DMA address)
struct device dc_dev
; /* base device */
volatile struct dmareg
*dc_dma
; /* register virtual address */
int dc_dmarev
; /* revision */
char *dc_dmafmt
; /* format for error messages */
void dmaattach(struct device
*, struct device
*, void *);
{ NULL
, "dma", matchbyname
, dmaattach
, DV_DULL
, sizeof(struct dma_softc
) };
struct hba_softc sc_hba
; /* base device + hba, must be first */
#define sc_dev sc_hba.hba_dev
struct sbusdev sc_sd
; /* sbus device */
struct intrhand sc_ih
; /* interrupt entry */
struct evcnt sc_intrcnt
; /* interrupt counter */
struct dma_softc
*sc_dc
; /* pointer to corresponding dma sc */
* Addresses mapped to hardware registers.
volatile struct espreg
*sc_esp
;
volatile struct dmareg
*sc_dma
;
* Copies of registers cleared/unlatched by reading.
* (FIFO flags is not cleared, but we want it for debugging.)
int sc_clockfreq
; /* clock frequency */
u_char sc_sel_timeout
; /* select timeout */
u_char sc_id
; /* initiator ID (default = 7) */
u_char sc_esptype
; /* 100, 100A, 2xx (see below) */
u_char sc_ccf
; /* clock conversion factor */
u_char sc_conf1
; /* value for config reg 1 */
u_char sc_conf2
; /* value for config reg 2 */
u_char sc_conf3
; /* value for config reg 3 */
struct bootpath
*sc_bp
; /* esp bootpath so far */
* Information pertaining to the current transfer,
* The size of sc_msg is the size of the ESP fifo,
* since we do message-in simply by allowing the fifo to fill.
char sc_probing
; /* used during autoconf; see below */
char sc_iwant
; /* true => icmd needs wakeup on idle */
char sc_state
; /* SCSI protocol state; see below */
char sc_sentcmd
; /* set once we get cmd out */
char sc_dmaactive
; /* true => doing dma */
u_char sc_sync
; /* synchronous transfer stuff (?) */
u_char sc_stat
[2]; /* status from last `status' phase */
u_char sc_msg
[16]; /* message from device */
u_short sc_dmactl
; /* control to load into dma csr */
u_long sc_dmaaddr
; /* address for next xfer */
int sc_dmasize
; /* size of current xfer */
int sc_resid
; /* count of bytes not yet xferred */
int sc_targ
; /* the target involved */
struct scsi_cdb
*sc_curcdb
; /* ptr to current command */
/* might cdbspace eventually be per-target? */
struct scsi_cdb sc_cdbspace
; /* space for one command */
* Values for sc_esptype (used to control configuration reset, and for
* workarounds for chip bugs). The order is important; see espreset().
* Probe state. 0 means not probing. While looking for each target
* we set this to PROBE_TESTING and do a TEST UNIT READY on unit 0.
* If selection fails, this is changed to PROBE_NO_TARGET; otherwise
* we assume the target exists, regardless of the result of the test.
#define PROBE_NO_TARGET 2
* Note that S_SVC is rare: normally we load the SCSI command into the
* ESP fifo and get interrupted only when the device has gone to data
* or status phase. If the device wants to play games, though, we end
* up doing things differently.
#define S_IDLE 0 /* not doing anything */
#define S_SEL 1 /* expecting select done interrupt */
#define S_SVC 2 /* expecting service req interrupt */
#define S_DI 3 /* expecting data-in done interrupt */
#define S_DO 4 /* expecting data-out done interrupt */
#define S_STAT 5 /* expecting status done interrupt */
#define S_MI 6 /* expecting message-in done interrupt */
#define S_FI 7 /* expecting final disconnect interrupt */
* Hardware limits on transfer sizes (see comments at top).
#define ESPMAX (64 * 1024)
#define DMAMAX(a) (0x01000000 - ((a) & 0x00ffffff))
* Return values from espact().
#define ACT_CONT 0 /* espact() handled everything */
#define ACT_IO 1 /* espact() is xferring data */
#define ACT_DONE 2 /* handled everything, and op is now done */
#define ACT_ERROR 3 /* an error occurred, op has been trashed */
#define ACT_RESET 4 /* please reset ESP, then do ACT_ERROR */
#define ACT_QUICKINTR 5 /* another interrupt is expected immediately */
/* autoconfiguration driver */
void espattach(struct device
*, struct device
*, void *);
{ NULL
, "esp", matchbyname
, espattach
, DV_DULL
, sizeof(struct esp_softc
) };
void espsbreset(struct device
*);
/* interrupt interface */
int espicmd(struct hba_softc
*, int, struct scsi_cdb
*, caddr_t
, int, int);
int espdump(struct hba_softc
*, int, struct scsi_cdb
*, caddr_t
, int);
void espstart(struct device
*, struct sq
*, struct buf
*,
scdgo_fn
, struct device
*);
int espgo(struct device
*, int, scintr_fn
, struct device
*,
void esprel(struct device
*);
void esphbareset(struct hba_softc
*, int);
static struct hbadriver esphbadriver
=
{ espicmd
, espdump
, espstart
, espgo
, esprel
, esphbareset
};
static void espdoattach(int);
static void dmareset(struct esp_softc
*);
static void espreset(struct esp_softc
*, int);
static void esperror(struct esp_softc
*, const char *);
static int espact(struct esp_softc
*);
void espselect(struct esp_softc
*, int, struct scsi_cdb
*);
/* second arg to espreset() */
#define RESET_ESPCHIP 0x1
#define RESET_SCSIBUS 0x2
#define RESET_BOTH (RESET_ESPCHIP | RESET_SCSIBUS)
* Attach a found DMA chip.
* The second argument is really a pointer to an sbus_attach_args.
dmaattach(parent
, dev
, args
)
register struct dma_softc
*dc
= (struct dma_softc
*)dev
;
register struct sbus_attach_args
*sa
= args
;
register volatile struct dmareg
*dma
;
dma
= (volatile struct dmareg
*)sa
->sa_ra
.ra_vaddr
;
dma
= (volatile struct dmareg
*)
mapiodev(sa
->sa_ra
.ra_paddr
, sizeof(struct dmareg
));
switch (rev
= DMA_REV(dma
->dma_csr
)) {
dc
->dc_dmafmt
= DMA_REV1_BITS
;
dc
->dc_dmafmt
= DMA_REV2_BITS
;
printf("WARNING: esp.c not yet updated for rev 3\n");
dc
->dc_dmafmt
= DMA_REV3_BITS
;
printf(": unknown revision code 0x%x\n", rev
);
dc
->dc_dmafmt
= DMA_REV3_BITS
; /* cross fingers */
espdoattach(dc
->dc_dev
.dv_unit
);
* Attach a found ESP chip. Search for targets; attach each one found.
* The latter must be deferred if the corresponding dma chip has not yet
espattach(parent
, self
, args
)
register struct esp_softc
*sc
= (struct esp_softc
*)self
;
register struct sbus_attach_args
*sa
= args
;
register volatile struct espreg
*esp
;
register struct bootpath
*bp
;
if (sa
->sa_ra
.ra_nintr
!= 1) {
printf(": expected 1 interrupt, got %d\n", sa
->sa_ra
.ra_nintr
);
pri
= sa
->sa_ra
.ra_intr
[0].int_pri
;
esp
= (volatile struct espreg
*)sa
->sa_ra
.ra_vaddr
;
esp
= (volatile struct espreg
*)
mapiodev(sa
->sa_ra
.ra_paddr
, sizeof(struct espreg
));
node
= sa
->sa_ra
.ra_node
;
sc
->sc_id
= getpropint(node
, "initiator-id", 7);
freq
= getpropint(node
, "clock-frequency", -1);
((struct sbus_softc
*)sc
->sc_dev
.dv_parent
)->sc_clockfreq
;
/* MIGHT NEED TO RESET ESP CHIP HERE ...? */
* Find out whether we have a -100, -100A, or -2xx,
* and what speed it runs at.
sc
->sc_conf1
= sc
->sc_id
| ESPCONF1_PARENB
;
esp
->esp_conf1
= sc
->sc_conf1
;
esp
->esp_conf2
= ESPCONF2_SCSI2
| ESPCONF2_RPE
;
if ((esp
->esp_conf2
& ~ESPCONF2_RSVD
) !=
(ESPCONF2_SCSI2
| ESPCONF2_RPE
)) {
if (esp
->esp_conf3
!= 5) { /* XXX def bits */
sc
->sc_esptype
= ESP100A
;
printf(", clock = %s MHz, ID = %d\n", clockfreq(freq
), sc
->sc_id
);
* Set clock conversion factor and select timeout.
* N.B.: clock frequency is not actually used in the rest
* of the driver; I calculate it here for completeness only
* (so I can see it when debugging).
freq
= howmany(freq
, 1000 * 1000); /* convert to MHz */
t
= ESPCCF_FROMMHZ(freq
);
t
= ESPTIMO_REGVAL(250, t
, freq
); /* timeout = 250 ms. */
* Link into sbus; set interrupt handler.
sc
->sc_sd
.sd_reset
= espsbreset
;
sbus_establish(&sc
->sc_sd
, &sc
->sc_dev
);
sc
->sc_ih
.ih_fun
= espintr
;
intr_establish(pri
, &sc
->sc_ih
);
evcnt_attach(&sc
->sc_dev
, "intr", &sc
->sc_intrcnt
);
#define SAME_ESP(bp, sa) \
((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
(bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
if (bp
!= NULL
&& strcmp(bp
->name
, "esp") == 0 && SAME_ESP(bp
, sa
))
espdoattach(sc
->sc_dev
.dv_unit
);
* `Final' attach of esp occurs once esp and dma chips have been found
* and assigned virtual addresses. Set up the ESP SCSI data structures
* and probe the SCSI bus.
register struct esp_softc
*sc
;
register struct dma_softc
*dc
;
register struct bootpath
*bp
;
/* make sure we have both */
if (espcd
.cd_ndevs
<= unit
||
dmacd
.cd_ndevs
<= unit
||
(sc
= espcd
.cd_devs
[unit
]) == NULL
||
(dc
= dmacd
.cd_devs
[unit
]) == NULL
)
sc
->sc_hba
.hba_driver
= &esphbadriver
;
sc
->sc_dma
->dma_csr
= 0; /* ??? */
espreset(sc
, RESET_ESPCHIP
);
/* MAYBE THIS SHOULD BE MOVED TO scsi_subr.c? */
for (targ
= 0; targ
< 8; targ
++) {
sc
->sc_probing
= PROBE_TESTING
;
(void)scsi_test_unit_ready(&sc
->sc_hba
, targ
, 0);
if (sc
->sc_probing
!= PROBE_NO_TARGET
) {
SCSI_FOUNDTARGET(&sc
->sc_hba
, targ
);
* See if we booted from a unit on this target. We could
* compare bp->name against the unit's name but there's no
* real need since a target and unit uniquely specify a
if ((bp
= sc
->sc_bp
) != NULL
&& (u_int
)(targ
= bp
->val
[0]) < 8 &&
(u_int
)(u
= bp
->val
[1]) < 8 &&
(t
= sc
->sc_hba
.hba_targets
[targ
]) != NULL
&& t
->t_units
[u
] != NULL
)
bootdv
= t
->t_units
[u
]->u_dev
;
* We are not allowed to touch the DMA "flush" and "drain" bits
* while it is still thinking about a request (DMA_RP).
#define DMAWAIT(dma) while ((dma)->dma_csr & DMA_RP) DELAY(1)
#define DMAWAIT1(dma) while ((dma)->dma_csr & DMA_PC) DELAY(1)
register volatile struct dmareg
*dma
= sc
->sc_dma
;
dma
->dma_csr
|= DMA_RESET
;
dma
->dma_csr
&= ~DMA_RESET
; /* ??? */
if (sc
->sc_dc
->dc_dmarev
== DMAREV_2
&& sc
->sc_esptype
!= ESP100
)
dma
->dma_csr
|= DMA_TURBO
;
dma
->dma_csr
|= DMA_IE
; /* enable interrupts */
* Reset the chip and/or SCSI bus (always resets DMA).
register struct esp_softc
*sc
;
register volatile struct espreg
*esp
= sc
->sc_esp
;
if (how
& RESET_ESPCHIP
) {
esp
->esp_cmd
= ESPCMD_RESET_CHIP
;
esp
->esp_cmd
= ESPCMD_NOP
;
* Reload configuration registers (cleared by
* RESET_CHIP command). Reloading conf2 on an
* ESP100 goofs it up, so out of paranoia we load
* only the registers that exist.
esp
->esp_conf1
= sc
->sc_conf1
;
if (sc
->sc_esptype
> ESP100
) { /* 100A, 2XX */
esp
->esp_conf2
= sc
->sc_conf2
;
if (sc
->sc_esptype
> ESP100A
) /* 2XX only */
esp
->esp_conf3
= sc
->sc_conf3
;
esp
->esp_ccf
= sc
->sc_ccf
;
esp
->esp_timeout
= sc
->sc_sel_timeout
;
/* We set synch offset later. */
if (how
& RESET_SCSIBUS
) {
* The chip should retain most of its parameters
* (including esp_ccf) across this kind of reset
* (see section 3.5 of Emulex documentation).
/* turn off scsi bus reset interrupts and reset scsi bus */
esp
->esp_conf1
= sc
->sc_conf1
| ESPCONF1_REPORT
;
esp
->esp_cmd
= ESPCMD_RESET_BUS
;
esp
->esp_cmd
= ESPCMD_NOP
;
esp
->esp_conf1
= sc
->sc_conf1
;
* Reset the SCSI bus and, optionally, all attached targets.
esphbareset(hba
, resetunits
)
register struct esp_softc
*sc
= (struct esp_softc
*)hba
;
espreset(sc
, RESET_SCSIBUS
);
scsi_reset_units(&sc
->sc_hba
);
* Reset the esp, after an Sbus reset.
* Also resets corresponding dma chip.
* THIS ROUTINE MIGHT GO AWAY
struct esp_softc
*sc
= (struct esp_softc
*)dev
;
printf(" %s %s", sc
->sc_dc
->dc_dev
.dv_xname
,
esphbareset(&sc
->sc_hba
, 1);
register struct esp_softc
*sc
;
"%s target %d cmd 0x%x (%s): %s:\n\
\tstat=%b (%s) step=%x dmacsr=%b fflags=%x intr=%b\n",
sc
->sc_dev
.dv_xname
, sc
->sc_targ
, sc
->sc_curcdb
->cdb_bytes
[0],
espstates
[sc
->sc_state
], err
,
stat
, ESPSTAT_BITS
, espphases
[stat
& ESPSTAT_PHASE
],
sc
->sc_espstep
, sc
->sc_dmacsr
, sc
->sc_dc
->dc_dmafmt
,
sc
->sc_espfflags
, sc
->sc_espintr
, ESPINTR_BITS
);
* Issue a select, loading command into the FIFO.
* Return nonzero on error, 0 if OK.
* Sets state to `selecting'; espact() will sequence state FSM.
register struct esp_softc
*sc
;
register struct scsi_cdb
*cdb
;
register volatile struct espreg
*esp
;
sc
->sc_stat
[0] = 0xff; /* ??? */
sc
->sc_msg
[0] = 0xff; /* ??? */
* Synch offset 0 => asynchronous transfer.
* Stuff the command bytes into the fifo.
* Select without attention since we do not do disconnect yet.
cmdlen
= SCSICMDLEN(cdb
->cdb_bytes
[0]);
for (i
= 0; i
< cmdlen
; i
++)
esp
->esp_fifo
= cdb
->cdb_bytes
[i
];
esp
->esp_cmd
= ESPCMD_SEL_NATN
;
/* the rest is done elsewhere */
* Sequence through the SCSI state machine. Return the action to take.
* Most of the work happens here.
* There are three interrupt sources:
* -- ESP interrupt request (typically, some device wants something).
* -- DMA byte count has reached 0 (we do not often want this one but
* can only turn it off in rev 2 DMA chips, it seems).
* DOES THIS OCCUR AT ALL HERE? THERE IS NOTHING TO HANDLE IT!
register struct esp_softc
*sc
;
register volatile struct espreg
*esp
;
register volatile struct dmareg
*dma
;
register int reg
, i
, resid
, newstate
;
register struct scsi_cdb
*cdb
;
/* check various error conditions, using as little code as possible */
if (sc
->sc_dmacsr
& DMA_EP
) {
esperror(sc
, "DMA error");
dma
->dma_csr
|= DMA_FLUSH
;
* This often occurs when there is no target.
if (sc
->sc_espintr
& ESPINTR_DSC
&&
sc
->sc_state
== S_SEL
&& sc
->sc_probing
) {
sc
->sc_probing
= PROBE_NO_TARGET
;
esperror(sc
, "DIAG: gross error (ignored)");
esperror(sc
, "parity error");
#define ERR (ESPINTR_SBR|ESPINTR_ILC|ESPINTR_RSL|ESPINTR_SAT|ESPINTR_SEL)
esperror(sc
, "scsi bus reset");
else if (reg
& ESPINTR_ILC
)
esperror(sc
, "illegal command (driver bug)");
printf("%s: target %d", sc
->sc_dev
.dv_xname
,
printf(" tried to reselect;");
printf(" selected with ATN;");
printf(" selected us as target;");
printf("we do not allow this yet\n");
* Disconnect currently only allowed in `final interrupt' states.
if (sc
->sc_state
== S_FI
)
* If we were doing a select just to test the existence
* of the target, note that it did not respond; otherwise
if (sc
->sc_state
== S_SEL
) {
sc
->sc_probing
= PROBE_NO_TARGET
;
/* flush fifo, in case we were selecting or sending data */
esp
->esp_cmd
= ESPCMD_FLUSH_FIFO
;
printf("%s: target %d not responding\n",
sc
->sc_dev
.dv_xname
, sc
->sc_targ
);
* Okay, things are moving along.
* What were we doing the last time we did something,
* and did it complete normally?
* We were selecting. Arbitration and select are
* complete (because ESPINTR_DSC was not set), but
* there is no guarantee the command went out.
if ((reg
& (ESPINTR_SVC
|ESPINTR_CMP
)) !=
(ESPINTR_SVC
|ESPINTR_CMP
)) {
esperror(sc
, "selection failed");
if (sc
->sc_espstep
== ESPSTEP_DONE
) {
if (sc
->sc_espstep
== 2) {
* We got something other than command phase.
* Just pretend things are normal; the
* device will ask for the command later.
esperror(sc
, "DIAG: esp step 2");
} else if (sc
->sc_espstep
== 3) {
* Device entered command phase and then exited it
* before we finished handing out the command.
* Do not know how to handle this.
esperror(sc
, "DIAG: esp step 3");
printf("%s: mysterious esp step %d\n",
sc
->sc_dev
.dv_xname
, sc
->sc_espstep
);
* Part of the command may still be lodged in the FIFO.
if (ESP_NFIFO(sc
->sc_espfflags
)) {
esp
->esp_cmd
= ESPCMD_FLUSH_FIFO
;
* We were waiting for phase change after stuffing the command
* into the FIFO. Make sure it got out.
if (ESP_NFIFO(sc
->sc_espfflags
)) {
esperror(sc
, "DIAG: CMDSVC, fifo not empty");
esp
->esp_cmd
= ESPCMD_FLUSH_FIFO
;
* We were doing DMA data in, and expecting a
* transfer-count-zero interrupt or a phase change.
* We got that; drain the pack register and handle
* as for data out -- but ignore FIFO (it should be
* empty, except for sync mode which we are not
dma
->dma_csr
|= DMA_DRAIN
;
* We were doing DMA data out. If there is data in the
* FIFO, it is stuff that got DMAed out but never made
* it to the device, so it counts as residual.
if ((resid
= ESP_NFIFO(sc
->sc_espfflags
)) != 0) {
esp
->esp_cmd
= ESPCMD_FLUSH_FIFO
;
if (sc
->sc_dmaactive
== 0) {
esperror(sc
, "dma done w/o dmaactive");
/* Finish computing residual count. */
reg
= esp
->esp_tcl
| (esp
->esp_tch
<< 8);
if (reg
== 0 && (sc
->sc_espstat
& ESPSTAT_TC
) == 0)
/* Compute xfer count (requested - resid). */
i
= sc
->sc_dmasize
- resid
;
printf("%s: xfer resid (%d) > xfer req (%d)\n",
sc
->sc_dev
.dv_xname
, resid
, sc
->sc_dmasize
);
i
= sc
->sc_dmasize
; /* forgiving... */
/* If data came in we must flush cache. */
if (sc
->sc_state
== S_DI
)
cache_flush(sc
->sc_dmaaddr
, i
);
if ((sc
->sc_espintr
& ESPINTR_SVC
) == 0) {
esperror(sc
, "no bus service req");
* The last thing we did was tell it `initiator complete'
* and so we expect to have gotten both the status byte
* and the final message byte. It is possible that we
* Apparently, BUS SERVICE is set if we got just status,
* while FUNCTION COMPLETE is set if we got both.
if ((reg
& (ESPINTR_SVC
|ESPINTR_CMP
)) != ESPINTR_CMP
) {
esperror(sc
, "bad status interrupt state");
reg
= ESP_NFIFO(sc
->sc_espfflags
);
"%s: command done but fifo count = %d; must be >= 2\n",
sc
->sc_dev
.dv_xname
, reg
);
* Read the status and the first msg byte.
* It should be CMD_COMPLETE. Eventually we
* may handle IDENTIFY, DISCONNECT, etc., as well.
sc
->sc_stat
[0] = esp
->esp_fifo
;
sc
->sc_msg
[0] = reg
= esp
->esp_fifo
;
esp
->esp_cmd
= ESPCMD_MSG_ACCEPT
;
if (reg
== MSG_CMD_COMPLETE
) {
if (SCSIMSGLEN(reg
) != 1) {
printf("%s: target %d is naughty\n",
sc
->sc_dev
.dv_xname
, sc
->sc_targ
);
printf("%s: warning: target %d returned msg 0x%x\n",
sc
->sc_dev
.dv_xname
, sc
->sc_targ
, reg
);
if ((reg
& ESPINTR_SVC
) == 0) {
esperror(sc
, "missing phase after msg in");
reg
= ESP_NFIFO(sc
->sc_espfflags
);
for (i
= 0; i
< reg
; i
++)
sc
->sc_msg
[i
] = esp
->esp_fifo
;
esperror(sc
, "target did not disconnect");
* Things are still moving along. The phase tells us
* what the device wants next. Do it.
switch (sc
->sc_espstat
& ESPSTAT_PHASE
) {
if (!sc
->sc_sentcmd
) esperror(sc
, "DIAG: data out without command");
if (sc
->sc_dmactl
& DMA_READ
) {
esperror(sc
, "wrong phase (want to read)");
if (!sc
->sc_sentcmd
) esperror(sc
, "DIAG: data in without command");
if (!(sc
->sc_dmactl
& DMA_READ
)) {
esperror(sc
, "wrong phase (want to write)");
esperror(sc
, "data count error");
* Compute DMA count based on chip limits.
* Set DMA address and load transfer count into
* ESP via DMA NOP, then set DMA control, and
* then we can start the DMA.
i
= min(sc
->sc_resid
, ESPMAX
);
i
= min(i
, DMAMAX(sc
->sc_dmaaddr
));
dma
->dma_addr
= sc
->sc_dmaaddr
;
esp
->esp_cmd
= ESPCMD_DMA
| ESPCMD_NOP
;
dma
->dma_csr
= sc
->sc_dmactl
;
esp
->esp_cmd
= ESPCMD_DMA
| ESPCMD_XFER_INFO
;
* Silly thing wants the command again.
* Load it into the FIFO and go to SVC state.
printf("%s: redoing command\n", sc
->sc_dev
.dv_xname
);
reg
= SCSICMDLEN(cdb
->cdb_bytes
[0]);
for (i
= 0; i
< reg
; i
++)
esp
->esp_fifo
= cdb
->cdb_bytes
[i
];
esp
->esp_cmd
= ESPCMD_XFER_INFO
;
esp
->esp_cmd
= ESPCMD_INIT_COMP
;
printf("%s: accepting (& ignoring) msg from target %d\n",
sc
->sc_dev
.dv_xname
, sc
->sc_targ
);
esp
->esp_cmd
= ESPCMD_MSG_ACCEPT
;
esperror(sc
, "bad phase");
* THIS SHOULD BE ADJUSTABLE
/* name howlong purpose */
#define SELECT_WAIT 300000 /* wait for select to complete */
#define CMD_WAIT 100000 /* wait for next phase, generic */
#define DATA_WAIT 100000 /* time to xfer data in/out */
* Send an `immediate' command, i.e., poll until the whole thing is done.
* Return the status byte from the device, or -1 if we timed out. We use
* DMA to transfer the data as the fifo only moves one byte at a time.
espicmd(hba
, targ
, cdb
, buf
, len
, rw
)
register struct esp_softc
*sc
= (struct esp_softc
*)hba
;
register volatile struct espreg
*esp
= sc
->sc_esp
;
register volatile struct dmareg
*dma
= sc
->sc_dma
;
* Wait for any ongoing operation to complete.
while (sc
->sc_state
!= S_IDLE
) {
tsleep((caddr_t
)&sc
->sc_iwant
, PRIBIO
, "espicmd", 0);
* Set up DMA transfer control (leaving interrupts disabled).
sc
->sc_dmactl
= rw
& B_READ
? DMA_ENA
| DMA_READ
: DMA_ENA
;
sc
->sc_dmaaddr
= (u_long
)buf
;
* Disable hardware interrupts and start select sequence,
* then loop, calling espact() after each ``interrupt''.
espselect(sc
, targ
, cdb
);
sc
->sc_espstat
= esp
->esp_stat
;
sc
->sc_espstep
= esp
->esp_step
& ESPSTEP_MASK
;
sc
->sc_espintr
= esp
->esp_intr
;
sc
->sc_espfflags
= esp
->esp_fflags
;
switch (r
= espact(sc
)) {
espreset(sc
, RESET_ESPCHIP
); /* ??? */
wakeup((caddr_t
)&sc
->sc_iwant
);
} else if ((sq
= sc
->sc_hba
.hba_head
) != NULL
) {
sc
->sc_hba
.hba_head
= sq
->sq_forw
;
(*sq
->sq_dgo
)(sq
->sq_dev
, &sc
->sc_cdbspace
);
* Dump (write memory, possibly physmem).
* SPARC higher-level dump code always provides virtual addresses,
* so we need not do any I/O mapping here.
espdump(hba
, targ
, cdb
, buf
, len
)
register struct hba_softc
*hba
;
register struct esp_softc
*sc
= (struct esp_softc
*)hba
;
* If we crashed in the middle of a bus transaction...
if (sc
->sc_state
!= S_IDLE
)
espreset(sc
, RESET_BOTH
); /* ??? */
return (espicmd(hba
, targ
, cdb
, buf
, len
, B_WRITE
));
* Allocate resources (SCSI bus and DVMA space) for the given transfer.
* Must be called at splbio().
* THIS SHOULD RETURN SUCCESS/FAIL INDICATION
espstart(self
, sq
, bp
, dgo
, dev
)
register struct esp_softc
*sc
= (struct esp_softc
*)self
;
if (sc
->sc_hba
.hba_busy
== 0) {
* Bus not busy, nothing to do here, just tell
* this target or unit that it has the SCSI bus.
(*dgo
)(dev
, &sc
->sc_cdbspace
);
* Bus is busy; just enqueue.
if (sc
->sc_hba
.hba_head
== NULL
)
sc
->sc_hba
.hba_head
= sq
;
sc
->sc_hba
.hba_tail
->sq_forw
= sq
;
sc
->sc_hba
.hba_tail
= sq
;
* Return 0 on success, 1 on failure.
espgo(self
, targ
, intr
, dev
, bp
, pad
)
register struct esp_softc
*sc
= (struct esp_softc
*)self
;
/* Set up dma control for espact(). */
sc
->sc_dmactl
= bp
->b_flags
& B_READ
?
DMA_ENA
| DMA_READ
| DMA_IE
: DMA_ENA
| DMA_IE
;
sc
->sc_dmaaddr
= (u_long
)bp
->b_un
.b_addr
;
sc
->sc_resid
= bp
->b_bcount
;
* Enable interrupts and start selection.
* The rest is done in espintr() and espact().
sc
->sc_hba
.hba_intr
= intr
; /* remember dev done function */
sc
->sc_hba
.hba_intrdev
= dev
; /* and its first arg */
sc
->sc_dma
->dma_csr
= DMA_IE
;
espselect(sc
, targ
, &sc
->sc_cdbspace
);
* Handle interrupt. Return 1 if taken.
register struct esp_softc
*sc
= (struct esp_softc
*)sc0
;
register volatile struct espreg
*esp
= sc
->sc_esp
;
register volatile struct dmareg
*dma
= sc
->sc_dma
;
return (0); /* not ours */
sc
->sc_intrcnt
.ev_count
++;
sc
->sc_espstat
= esp
->esp_stat
;
sc
->sc_espstep
= esp
->esp_step
& ESPSTEP_MASK
;
sc
->sc_espintr
= esp
->esp_intr
;
sc
->sc_espfflags
= esp
->esp_fflags
;
if (sc
->sc_state
== S_IDLE
) {
printf("%s: stray interrupt\n", sc
->sc_dev
.dv_xname
);
dma
->dma_csr
&= ~DMA_IE
; /* ??? */
switch (r
= espact(sc
)) {
case ACT_CONT
: /* just return */
case ACT_RESET
: /* please reset esp */
espreset(sc
, RESET_ESPCHIP
); /* ??? */
case ACT_DONE
: /* this one is done, successfully */
case ACT_ERROR
: /* this one is done due to `severe' error */
if (!sc
->sc_hba
.hba_busy
)
* This transaction is done. Call the driver's intr routine.
* If an immediate command is pending, let it run in front
* of us, otherwise start the next transation. Note that
* the interrupt routine may run its own immediate commands
* (`request sense' for errors, eg) before we get around to
* the process waiting to do immediate command, but that
* is OK; if we did not set S_IDLE here we could deadlock.
(*sc
->sc_hba
.hba_intr
)(sc
->sc_hba
.hba_intrdev
,
r
== ACT_DONE
? sc
->sc_stat
[0] : -1, sc
->sc_resid
);
wakeup((caddr_t
)&sc
->sc_iwant
);
} else if ((sq
= sc
->sc_hba
.hba_head
) != NULL
) {
sc
->sc_hba
.hba_head
= sq
->sq_forw
;
(*sq
->sq_dgo
)(sq
->sq_dev
, &sc
->sc_cdbspace
);
case ACT_QUICKINTR
: /* wait a short while for another interrupt */
printf("%s: quickintr: ", sc
->sc_dev
.dv_xname
);
printf("got one, wait=%d\n", wait
);
printf("did not get one\n");
* Target or unit decided to let go of the bus early.
register struct esp_softc
*sc
= (struct esp_softc
*)self
;
/* if there is someone else waiting, give them a crack at it */
wakeup((caddr_t
)&sc
->sc_iwant
);
} else if ((sq
= sc
->sc_hba
.hba_head
) != NULL
) {
sc
->sc_hba
.hba_head
= sq
->sq_forw
;
(*sq
->sq_dgo
)(sq
->sq_dev
, &sc
->sc_cdbspace
);