#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: esc.c,v 1.34 2021/08/21 11:55:24 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <uvm/uvm_extern.h>
#include <machine/pmap.h>
#include <machine/cpu.h>
#include <machine/io.h>
#include <machine/intr.h>
#include <acorn32/podulebus/podulebus.h>
#include <acorn32/podulebus/escreg.h>
#include <acorn32/podulebus/escvar.h>
void escinitialize(struct esc_softc *);
void esc_minphys(struct buf *bp);
void esc_scsi_request(struct scsipi_channel *,
scsipi_adapter_req_t, void *);
void esc_donextcmd(struct esc_softc *dev, struct esc_pending *pendp);
void esc_scsidone(struct esc_softc *dev, struct scsipi_xfer *xs,
int stat);
void escintr(struct esc_softc *dev);
void esciwait(struct esc_softc *dev);
void escreset(struct esc_softc *dev, int how);
int escselect(struct esc_softc *dev, struct esc_pending *pendp,
unsigned char *cbuf, int clen,
unsigned char *buf, int len, int mode);
void escicmd(struct esc_softc *dev, struct esc_pending *pendp);
int escgo(struct esc_softc *dev, struct esc_pending *pendp);
void esc_init_nexus(struct esc_softc *, struct nexus *);
void esc_save_pointers(struct esc_softc *);
void esc_restore_pointers(struct esc_softc *);
void esc_ixfer(struct esc_softc *);
void esc_build_sdtrm(struct esc_softc *, int, int);
int esc_select_unit(struct esc_softc *, short);
struct nexus *esc_arbitate_target(struct esc_softc *, int);
void esc_setup_nexus(struct esc_softc *, struct nexus *, struct esc_pending *,
unsigned char *, int, unsigned char *, int, int);
int esc_pretests(struct esc_softc *, esc_regmap_p);
int esc_midaction(struct esc_softc *, esc_regmap_p, struct nexus *);
int esc_postaction(struct esc_softc *, esc_regmap_p, struct nexus *);
u_char esc_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
u_char esc_inhibit_disc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
#undef DEBUG
#define DEBUG
#ifdef DEBUG
#define QPRINTF(a) if (esc_debug > 1) printf a
int esc_debug = 2;
#else
#define QPRINTF
#endif
void
esc_minphys(struct buf *bp)
{
minphys(bp);
}
void
esc_init_nexus(struct esc_softc *dev, struct nexus *nexus)
{
memset(nexus, 0, sizeof(struct nexus));
nexus->state = ESC_NS_IDLE;
nexus->period = 200;
nexus->offset = 0;
nexus->syncper = 5;
nexus->syncoff = 0;
nexus->config3 = dev->sc_config3 & ~ESC_CFG3_FASTSCSI;
}
void
escinitialize(struct esc_softc *dev)
{
int i;
dev->sc_led_status = 0;
TAILQ_INIT(&dev->sc_xs_pending);
TAILQ_INIT(&dev->sc_xs_free);
for(i=0; i<MAXPENDING; i++) {
TAILQ_INSERT_TAIL(&dev->sc_xs_free, &dev->sc_xs_store[i],
link);
}
if (dev->sc_clock_freq <= 10)
dev->sc_clock_conv_fact = 2;
if (dev->sc_clock_freq <= 40)
dev->sc_clock_conv_fact = 2+((dev->sc_clock_freq-10)/5);
else
panic("escinitialize: Clock frequency too high");
dev->sc_config1 = (dev->sc_host_id & ESC_CFG1_BUS_ID_MASK);
dev->sc_config2 = ESC_CFG2_FEATURES_ENABLE;
dev->sc_config3 = (dev->sc_clock_freq > 25 ? ESC_CFG3_FASTCLK : 0);
dev->sc_timeout_val = 1+dev->sc_timeout*dev->sc_clock_freq/
((7682*dev->sc_clock_conv_fact)/1000);
dev->sc_clock_period = 1000/dev->sc_clock_freq;
escreset(dev, 1 | 2);
dev->sc_units_disconnected = 0;
dev->sc_msg_in_len = 0;
dev->sc_msg_out_len = 0;
dev->sc_flags = 0;
for(i=0; i<8; i++)
esc_init_nexus(dev, &dev->sc_nexus[i]);
dev->sc_bump_va = (u_char *)uvm_km_alloc(kernel_map, dev->sc_bump_sz, 0,
UVM_KMF_WIRED | UVM_KMF_ZERO);
(void) pmap_extract(pmap_kernel(), (vaddr_t)dev->sc_bump_va,
(paddr_t *)&dev->sc_bump_pa);
pt_entry_t * const ptep = vtopte((vaddr_t) dev->sc_bump_va);
const pt_entry_t opte = *ptep;
const pt_entry_t npte = opte & ~L2_C;
l2pte_set(ptep, npte, opte);
PTE_SYNC(ptep);
cpu_tlb_flushD();
cpu_dcache_wbinv_range((vaddr_t)dev->sc_bump_va, PAGE_SIZE);
printf(" dmabuf V0x%08x P0x%08x", (u_int)dev->sc_bump_va, (u_int)dev->sc_bump_pa);
}
void
esc_scsi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
void *arg)
{
struct scsipi_xfer *xs;
struct esc_softc *dev = device_private(chan->chan_adapter->adapt_dev);
struct scsipi_periph *periph;
struct esc_pending *pendp;
int flags, s, target;
switch (req) {
case ADAPTER_REQ_RUN_XFER:
xs = arg;
periph = xs->xs_periph;
flags = xs->xs_control;
target = periph->periph_target;
if (flags & XS_CTL_DATA_UIO)
panic("esc: scsi data uio requested");
if ((flags & XS_CTL_POLL) && (dev->sc_flags & ESC_ACTIVE))
panic("esc_scsicmd: busy");
s = splbio();
pendp = dev->sc_xs_free.tqh_first;
if (pendp == NULL) {
splx(s);
xs->error = XS_RESOURCE_SHORTAGE;
scsipi_done(xs);
return;
}
TAILQ_REMOVE(&dev->sc_xs_free, pendp, link);
pendp->xs = xs;
splx(s);
if ((dev->sc_flags & ESC_ACTIVE) ||
(dev->sc_nexus[target].flags & ESC_NF_UNIT_BUSY)) {
s = splbio();
TAILQ_INSERT_TAIL(&dev->sc_xs_pending, pendp, link);
splx(s);
} else
esc_donextcmd(dev, pendp);
return;
case ADAPTER_REQ_GROW_RESOURCES:
case ADAPTER_REQ_SET_XFER_MODE:
return;
}
}
void
esc_donextcmd(struct esc_softc *dev, struct esc_pending *pendp)
{
int s;
if (pendp->xs->xs_control & XS_CTL_RESET) {
struct nexus *nexus;
s = splbio();
while(!escselect(dev, pendp, 0, 0, 0, 0, ESC_SELECT_K)) {
splx(s);
delay(10);
s = splbio();
}
nexus = dev->sc_cur_nexus;
while(nexus->flags & ESC_NF_UNIT_BUSY) {
esciwait(dev);
escintr(dev);
}
nexus->flags |= ESC_NF_UNIT_BUSY;
splx(s);
escreset(dev, 0);
s = splbio();
nexus->flags &= ~ESC_NF_UNIT_BUSY;
splx(s);
}
if (pendp->xs->xs_control & XS_CTL_POLL) {
s = splbio();
escicmd(dev, pendp);
TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
splx(s);
} else {
escgo(dev, pendp);
return;
}
}
void
esc_scsidone(struct esc_softc *dev, struct scsipi_xfer *xs, int stat)
{
struct esc_pending *pendp;
int s;
xs->status = stat;
if (stat == 0)
xs->resid = 0;
else {
switch(stat) {
case SCSI_CHECK:
case SCSI_BUSY:
xs->error = XS_BUSY;
break;
case -1:
xs->error = XS_DRIVER_STUFFUP;
QPRINTF(("esc_scsicmd() bad %x\n", stat));
break;
default:
xs->error = XS_TIMEOUT;
break;
}
}
s = splbio();
pendp = dev->sc_xs_pending.tqh_first;
while(pendp) {
if (!(dev->sc_nexus[pendp->xs->xs_periph->periph_target].flags &
ESC_NF_UNIT_BUSY))
break;
pendp = pendp->link.tqe_next;
}
if (pendp != NULL) {
TAILQ_REMOVE(&dev->sc_xs_pending, pendp, link);
}
splx(s);
scsipi_done(xs);
if (pendp)
esc_donextcmd(dev, pendp);
}
void
escreset(struct esc_softc *dev, int how)
{
esc_regmap_p rp;
int i, s;
rp = dev->sc_esc;
if (how & 1) {
for(i=0; i<8; i++)
esc_init_nexus(dev, &dev->sc_nexus[i]);
*rp->esc_command = ESC_CMD_RESET_CHIP;
delay(1);
*rp->esc_command = ESC_CMD_NOP;
*rp->esc_config1 = dev->sc_config1;
*rp->esc_config2 = dev->sc_config2;
*rp->esc_config3 = dev->sc_config3;
*rp->esc_config4 = dev->sc_config4;
*rp->esc_timeout = dev->sc_timeout_val;
*rp->esc_clkconv = dev->sc_clock_conv_fact &
ESC_CLOCK_CONVERSION_MASK;
}
if (how & 2) {
for(i=0; i<8; i++)
esc_init_nexus(dev, &dev->sc_nexus[i]);
s = splbio();
*rp->esc_command = ESC_CMD_RESET_SCSI_BUS;
delay(100);
while(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING) {
dev->sc_status = *rp->esc_status;
dev->sc_interrupt = *rp->esc_interrupt;
delay(100);
}
dev->sc_status = *rp->esc_status;
dev->sc_interrupt = *rp->esc_interrupt;
splx(s);
}
if (dev->sc_config_flags & ESC_SLOW_START)
delay(4*250000);
else
delay(250000);
}
void
esc_save_pointers(struct esc_softc *dev)
{
struct nexus *nx;
nx = dev->sc_cur_nexus;
if (nx) {
nx->cur_link = dev->sc_cur_link;
nx->max_link = dev->sc_max_link;
nx->buf = dev->sc_buf;
nx->len = dev->sc_len;
nx->dma_len = dev->sc_dma_len;
nx->dma_buf = dev->sc_dma_buf;
nx->dma_blk_flg = dev->sc_dma_blk_flg;
nx->dma_blk_len = dev->sc_dma_blk_len;
nx->dma_blk_ptr = dev->sc_dma_blk_ptr;
}
}
void
esc_restore_pointers(struct esc_softc *dev)
{
struct nexus *nx;
nx = dev->sc_cur_nexus;
if (nx) {
dev->sc_cur_link = nx->cur_link;
dev->sc_max_link = nx->max_link;
dev->sc_buf = nx->buf;
dev->sc_len = nx->len;
dev->sc_dma_len = nx->dma_len;
dev->sc_dma_buf = nx->dma_buf;
dev->sc_dma_blk_flg = nx->dma_blk_flg;
dev->sc_dma_blk_len = nx->dma_blk_len;
dev->sc_dma_blk_ptr = nx->dma_blk_ptr;
dev->sc_chain = nx->dma;
dev->sc_unit = (nx->lun_unit & 0x0F);
dev->sc_lun = (nx->lun_unit & 0xF0) >> 4;
}
}
void
esciwait(struct esc_softc *dev)
{
esc_regmap_p rp;
if (dev->sc_flags & ESC_DONT_WAIT) {
dev->sc_flags &= ~ESC_DONT_WAIT;
return;
}
rp = dev->sc_esc;
while(!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING));
dev->sc_status = *rp->esc_status;
dev->sc_interrupt = *rp->esc_interrupt;
if (dev->sc_interrupt & ESC_INT_RESELECTED) {
dev->sc_resel[0] = *rp->esc_fifo;
dev->sc_resel[1] = *rp->esc_fifo;
}
}
#if 0
void
esc_ixfer(struct esc_softc *dev)
{
esc_regmap_p rp;
u_char *buf;
int len, mode, phase;
rp = dev->sc_esc;
buf = dev->sc_buf;
len = dev->sc_len;
phase = dev->sc_status & ESC_STAT_PHASE_MASK;
mode = (phase == ESC_PHASE_DATA_IN);
while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase))
if (mode) {
*rp->esc_command = ESC_CMD_TRANSFER_INFO;
esciwait(dev);
*buf++ = *rp->esc_fifo;
len--;
} else {
len--;
*rp->esc_fifo = *buf++;
*rp->esc_command = ESC_CMD_TRANSFER_INFO;
esciwait(dev);
}
dev->sc_buf = buf;
dev->sc_len = len;
dev->sc_flags |= ESC_DONT_WAIT;
}
#else
void
esc_ixfer(struct esc_softc *dev)
{
esc_regmap_p rp;
vu_char *esc_status;
vu_char *esc_command;
vu_char *esc_interrupt;
vu_char *esc_fifo;
u_char *buf;
int len, mode, phase;
rp = dev->sc_esc;
buf = dev->sc_buf;
len = dev->sc_len;
esc_status = rp->esc_status;
esc_command = rp->esc_command;
esc_interrupt = rp->esc_interrupt;
esc_fifo = rp->esc_fifo;
phase = dev->sc_status & ESC_STAT_PHASE_MASK;
mode = (phase == ESC_PHASE_DATA_IN);
if (mode) {
while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) {
*esc_command = ESC_CMD_TRANSFER_INFO;
while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING));
dev->sc_status = *esc_status;
dev->sc_interrupt = *esc_interrupt;
*buf++ = *esc_fifo;
len--;
}
} else {
while(len && ((dev->sc_status & ESC_STAT_PHASE_MASK) == phase)) {
len--;
*esc_fifo = *buf++;
*esc_command = ESC_CMD_TRANSFER_INFO;
while(!(*esc_status & ESC_STAT_INTERRUPT_PENDING));
dev->sc_status = *esc_status;
dev->sc_interrupt = *esc_interrupt;
}
}
dev->sc_buf = buf;
dev->sc_len = len;
dev->sc_flags |= ESC_DONT_WAIT;
}
#endif
void
esc_build_sdtrm(struct esc_softc *dev, int period, int offset)
{
dev->sc_msg_out[0] = 0x01;
dev->sc_msg_out[1] = 0x03;
dev->sc_msg_out[2] = 0x01;
dev->sc_msg_out[3] = period/4;
dev->sc_msg_out[4] = offset;
dev->sc_msg_out_len= 5;
}
int
esc_select_unit(struct esc_softc *dev, short target)
{
esc_regmap_p rp;
struct nexus *nexus;
int s, retcode, i;
u_char cmd;
s = splbio();
retcode = 0;
nexus = &dev->sc_nexus[target];
if (!(dev->sc_flags & ESC_ACTIVE) && !dev->sc_sel_nexus) {
dev->sc_flags |= ESC_ACTIVE;
rp = dev->sc_esc;
*rp->esc_syncper = nexus->syncper;
*rp->esc_syncoff = nexus->syncoff;
*rp->esc_config3 = nexus->config3;
*rp->esc_config1 = dev->sc_config1;
*rp->esc_timeout = dev->sc_timeout_val;
*rp->esc_dest_id = target;
if (!(*rp->esc_status & ESC_STAT_INTERRUPT_PENDING)) {
*rp->esc_fifo = nexus->ID;
if ((nexus->flags & (ESC_NF_DO_SDTR | ESC_NF_RESET))
|| (dev->sc_msg_out_len != 0))
cmd = ESC_CMD_SEL_ATN_STOP;
else {
for(i=0; i<nexus->clen; i++)
*rp->esc_fifo = nexus->cbuf[i];
cmd = ESC_CMD_SEL_ATN;
}
dev->sc_sel_nexus = nexus;
*rp->esc_command = cmd;
retcode = 1;
nexus->flags &= ~ESC_NF_RETRY_SELECT;
} else
nexus->flags |= ESC_NF_RETRY_SELECT;
} else
nexus->flags |= ESC_NF_RETRY_SELECT;
splx(s);
return(retcode);
}
struct nexus *
esc_arbitate_target(struct esc_softc *dev, int target)
{
struct nexus *nexus;
int s;
nexus = &dev->sc_nexus[target];
s = splbio();
if (nexus->flags & ESC_NF_UNIT_BUSY)
nexus = 0;
else
nexus->flags |= ESC_NF_UNIT_BUSY;
splx(s);
return(nexus);
}
void
esc_setup_nexus(struct esc_softc *dev, struct nexus *nexus, struct esc_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
{
int sync, target, lun;
target = pendp->xs->xs_periph->periph_target;
lun = pendp->xs->xs_periph->periph_lun;
if ((dev->sc_config_flags & (ESC_NO_SYNCH | ESC_NO_DMA)) ||
esc_inhibit_sync[(int)target])
mode &= ~ESC_SELECT_S;
if ((dev->sc_config_flags & ESC_NO_RESELECT) ||
esc_inhibit_disc[(int)target])
mode &= ~ESC_SELECT_R;
nexus->xs = pendp->xs;
nexus->ID = ((mode & ESC_SELECT_R) ? 0xC0 : 0x80) | lun;
nexus->clen = clen;
memcpy(nexus->cbuf, cbuf, nexus->clen);
nexus->cbuf[1] |= lun << 5;
nexus->cur_link = 0;
nexus->dma_len = 0;
nexus->dma_buf = 0;
nexus->dma_blk_len = 0;
nexus->dma_blk_ptr = 0;
nexus->len = len;
nexus->buf = buf;
nexus->lun_unit = (lun << 4) | target;
nexus->state = ESC_NS_SELECTED;
nexus->flags &= ESC_NF_UNIT_BUSY
| ESC_NF_SYNC_TESTED | ESC_NF_SELECT_ME;
if (mode & ESC_SELECT_I)
nexus->flags |= ESC_NF_IMMEDIATE;
if (mode & ESC_SELECT_K)
nexus->flags |= ESC_NF_RESET;
sync = ((mode & ESC_SELECT_S) ? 1 : 0);
if (sync && (mode & ESC_SELECT_I))
sync = 0;
if (!sync &&
((nexus->flags & ESC_NF_SYNC_TESTED) && (nexus->offset != 0))) {
nexus->flags |= ESC_NF_DO_SDTR;
nexus->period = 200;
nexus->offset = 0;
} else if (sync && !(nexus->flags & ESC_NF_SYNC_TESTED)) {
nexus->flags |= ESC_NF_DO_SDTR;
nexus->period = ((dev->sc_clock_freq>25) ? 100 : 200);
nexus->offset = 8;
if ((nexus->period == 100) &&
(dev->sc_config_flags & ESC_SLOW_CABLE))
nexus->period = 200;
}
if ((mode & ESC_SELECT_I) || (dev->sc_config_flags & ESC_NO_DMA)) {
nexus->dma[0].ptr = buf;
nexus->dma[0].len = len;
nexus->dma[0].flg = ESC_CHAIN_PRG;
nexus->max_link = 1;
} else {
nexus->max_link = dev->sc_build_dma_chain(dev, nexus->dma,
buf, len);
}
if (len && !(mode & ESC_SELECT_I))
cpu_dcache_wbinv_range((vaddr_t)buf, len);
}
int
escselect(struct esc_softc *dev, struct esc_pending *pendp, unsigned char *cbuf, int clen, unsigned char *buf, int len, int mode)
{
struct nexus *nexus;
nexus = esc_arbitate_target(dev, pendp->xs->xs_periph->periph_target);
if (nexus == NULL)
return(0);
esc_setup_nexus(dev, nexus, pendp, cbuf, clen, buf, len, mode);
esc_select_unit(dev, pendp->xs->xs_periph->periph_target);
return(1);
}
int
escgo(struct esc_softc *dev, struct esc_pending *pendp)
{
int s;
char *buf;
buf = pendp->xs->data;
if (escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
buf, pendp->xs->datalen, ESC_SELECT_RS)) {
s = splbio();
TAILQ_INSERT_TAIL(&dev->sc_xs_free, pendp, link);
splx(s);
} else {
s = splbio();
TAILQ_INSERT_HEAD(&dev->sc_xs_pending, pendp, link);
splx(s);
}
return(0);
}
int
esc_pretests(struct esc_softc *dev, esc_regmap_p rp)
{
struct nexus *nexus;
int i, s;
if (dev->sc_interrupt & ESC_INT_SCSI_RESET_DETECTED) {
for(i=0; i<8; i++) {
if (dev->sc_nexus[i].xs)
esc_scsidone(dev, dev->sc_nexus[i].xs, -2);
esc_init_nexus(dev, &dev->sc_nexus[i]);
}
printf("escintr: SCSI-RESET detected!");
return(-1);
}
if (dev->sc_interrupt & ESC_INT_ILLEGAL_COMMAND) {
printf("FIFO:");
while(*rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK)
printf(" %x", *rp->esc_fifo);
printf("\n");
printf("CMD: %x\n", *rp->esc_command);
panic("escintr: ILLEGAL COMMAND!");
}
if (dev->sc_interrupt & ESC_INT_RESELECTED) {
s = splbio();
dev->sc_flags |= ESC_ACTIVE;
if (dev->sc_sel_nexus) {
dev->sc_sel_nexus->flags |= ESC_NF_SELECT_ME;
dev->sc_sel_nexus = 0;
}
splx(s);
if (dev->sc_units_disconnected) {
dev->sc_resel[0] &= ~(1<<dev->sc_host_id);
for(i=0; i<8; i++)
if (dev->sc_resel[0] & (1<<i))
break;
if (i == 8)
panic("Illegal reselection!");
if (dev->sc_nexus[i].state == ESC_NS_DISCONNECTED) {
dev->sc_cur_nexus = &dev->sc_nexus[i];
nexus = dev->sc_cur_nexus;
*rp->esc_syncper = nexus->syncper;
*rp->esc_syncoff = nexus->syncoff;
*rp->esc_config3 = nexus->config3;
*rp->esc_dest_id = i & 7;
dev->sc_units_disconnected--;
dev->sc_msg_in_len= 0;
esc_restore_pointers(dev);
nexus->state = ESC_NS_RESELECTED;
*rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED;
return(1);
}
}
printf("escintr: resel[0] %x resel[1] %x disconnected %d\n",
dev->sc_resel[0], dev->sc_resel[1],
dev->sc_units_disconnected);
panic("escintr: Unexpected reselection!");
}
return(0);
}
int
esc_midaction(struct esc_softc *dev, esc_regmap_p rp, struct nexus *nexus)
{
int i, left, len, s;
u_char status, msg;
if (dev->sc_interrupt & ESC_INT_DISCONNECT) {
s = splbio();
dev->sc_cur_nexus = 0;
dev->sc_flags &= ~ESC_ACTIVE;
*rp->esc_command = ESC_CMD_FLUSH_FIFO;
switch(nexus->state) {
case ESC_NS_SELECTED:
dev->sc_sel_nexus = 0;
nexus->flags &= ~ESC_NF_SELECT_ME;
nexus->xs->resid = dev->sc_len;
nexus->status = -2;
nexus->flags &= ~ESC_NF_UNIT_BUSY;
nexus->state = ESC_NS_FINISHED;
break;
case ESC_NS_DONE:
nexus->xs->resid = dev->sc_len;
nexus->flags &= ~ESC_NF_UNIT_BUSY;
nexus->state = ESC_NS_FINISHED;
dev->sc_led(dev, 0);
break;
case ESC_NS_DISCONNECTING:
nexus->state = ESC_NS_DISCONNECTED;
dev->sc_units_disconnected++;
break;
case ESC_NS_RESET:
dev->sc_led(dev, 0);
esc_init_nexus(dev, nexus);
break;
default:
printf("escintr: Unexpected disconnection\n");
printf("escintr: u %x s %d p %d f %x c %x\n",
nexus->lun_unit, nexus->state,
dev->sc_status & ESC_STAT_PHASE_MASK,
nexus->flags, nexus->cbuf[0]);
nexus->xs->resid = dev->sc_len;
nexus->flags &= ~ESC_NF_UNIT_BUSY;
nexus->state = ESC_NS_FINISHED;
nexus->status = -3;
dev->sc_led(dev, 0);
break;
}
if (dev->sc_units_disconnected &&
!(dev->sc_flags & ESC_ACTIVE))
*rp->esc_command = ESC_CMD_ENABLE_RESEL;
splx(s);
for(i=0; i<8; i++)
if (dev->sc_nexus[i].flags & (ESC_NF_SELECT_ME | ESC_NF_RETRY_SELECT))
if (esc_select_unit(dev, i) == 2)
break;
if (nexus->state == ESC_NS_FINISHED)
esc_scsidone(dev, nexus->xs, nexus->status);
return(1);
}
switch(nexus->state) {
case ESC_NS_SELECTED:
dev->sc_cur_nexus = nexus;
dev->sc_sel_nexus = 0;
nexus->flags &= ~ESC_NF_SELECT_ME;
*rp->esc_syncper = nexus->syncper;
*rp->esc_syncoff = nexus->syncoff;
*rp->esc_config3 = nexus->config3;
esc_restore_pointers(dev);
nexus->status = 0xFF;
dev->sc_msg_in[0] = 0xFF;
dev->sc_msg_in_len= 0;
dev->sc_led(dev, 1);
break;
case ESC_NS_DATA_IN:
case ESC_NS_DATA_OUT:
if (dev->sc_dma_len)
if (dev->sc_cur_link < dev->sc_max_link) {
left = dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR);
len = dev->sc_dma_len;
if (nexus->state == ESC_NS_DATA_IN) {
if (dev->sc_dma_buf == dev->sc_bump_pa)
{
while((*rp->esc_fifo_flags&ESC_FIFO_COUNT_MASK)
&& left)
dev->sc_bump_va[len-(left--)] = *rp->esc_fifo;
memcpy(dev->sc_buf, dev->sc_bump_va, len-left);
}
} else {
left+= *rp->esc_fifo_flags & ESC_FIFO_COUNT_MASK;
*rp->esc_command = ESC_CMD_FLUSH_FIFO;
}
dev->sc_len -= len-left;
dev->sc_buf += len-left;
dev->sc_dma_buf = (char *)dev->sc_dma_buf + len-left;
dev->sc_dma_len = left;
dev->sc_dma_blk_ptr = (char *)dev->sc_dma_blk_ptr +
len-left;
dev->sc_dma_blk_len -= len-left;
if (!dev->sc_dma_blk_len)
dev->sc_cur_link++;
}
break;
case ESC_NS_STATUS:
status = *rp->esc_fifo;
msg = *rp->esc_fifo;
nexus->status = status;
if (status != 0)
nexus->status = -1;
dev->sc_msg_in[0] = msg;
dev->sc_msg_in_len = 1;
nexus->flags |= ESC_NF_HAS_MSG;
break;
default:
break;
}
return(0);
}
int
esc_postaction(struct esc_softc *dev, esc_regmap_p rp, struct nexus *nexus)
{
int i, len;
u_char cmd;
short offset, period;
cmd = 0;
switch(dev->sc_status & ESC_STAT_PHASE_MASK) {
case ESC_PHASE_DATA_OUT:
case ESC_PHASE_DATA_IN:
if ((dev->sc_status & ESC_STAT_PHASE_MASK) ==
ESC_PHASE_DATA_OUT)
nexus->state = ESC_NS_DATA_OUT;
else
nexus->state = ESC_NS_DATA_IN;
dev->sc_setup_dma(dev, 0, 0, ESC_DMA_CLEAR);
if (dev->sc_cur_link < dev->sc_max_link) {
if (!dev->sc_dma_blk_len) {
dev->sc_dma_blk_ptr = dev->sc_chain[dev->sc_cur_link].ptr;
dev->sc_dma_blk_len = dev->sc_chain[dev->sc_cur_link].len;
dev->sc_dma_blk_flg = dev->sc_chain[dev->sc_cur_link].flg;
}
if (dev->sc_dma_blk_flg == ESC_CHAIN_PRG) {
esc_ixfer(dev);
dev->sc_cur_link++;
dev->sc_dma_len = 0;
break;
}
else if (dev->sc_dma_blk_flg == ESC_CHAIN_BUMP)
len = dev->sc_dma_blk_len;
else
len = dev->sc_need_bump(dev,
(void *)dev->sc_dma_blk_ptr,
dev->sc_dma_blk_len);
if (len) {
dev->sc_dma_buf = dev->sc_bump_pa;
dev->sc_dma_len = len;
if (nexus->state == ESC_NS_DATA_OUT)
memcpy(dev->sc_bump_va, dev->sc_buf, dev->sc_dma_len);
} else {
dev->sc_dma_buf = dev->sc_dma_blk_ptr;
dev->sc_dma_len = dev->sc_dma_blk_len;
}
dev->sc_setup_dma(dev, (void *)dev->sc_dma_buf,
dev->sc_dma_len,
((nexus->state == ESC_NS_DATA_OUT) ?
ESC_DMA_WRITE : ESC_DMA_READ));
printf("Using DMA !!!!\n");
cmd = ESC_CMD_TRANSFER_INFO | ESC_CMD_DMA;
} else {
*rp->esc_tc_low = 0;
*rp->esc_tc_mid = 1;
*rp->esc_tc_high = 0;
cmd = ESC_CMD_TRANSFER_PAD;
}
break;
case ESC_PHASE_COMMAND:
nexus->state = ESC_NS_SVC;
*rp->esc_command = ESC_CMD_FLUSH_FIFO;
for(i=0; i<5; i++);
for(i=0; i<nexus->clen; i++)
*rp->esc_fifo = nexus->cbuf[i];
cmd = ESC_CMD_TRANSFER_INFO;
break;
case ESC_PHASE_STATUS:
nexus->state = ESC_NS_STATUS;
cmd = ESC_CMD_COMMAND_COMPLETE;
break;
case ESC_PHASE_MESSAGE_OUT:
nexus->state = ESC_NS_MSG_OUT;
*rp->esc_command = ESC_CMD_FLUSH_FIFO;
if (nexus->flags & ESC_NF_DO_SDTR) {
esc_build_sdtrm(dev, nexus->period, nexus->offset);
nexus->flags |= ESC_NF_SDTR_SENT;
nexus->flags &= ~ESC_NF_DO_SDTR;
} else if (nexus->flags & ESC_NF_RESET) {
dev->sc_msg_out[0] = 0x0C;
dev->sc_msg_out_len = 1;
nexus->state = ESC_NS_RESET;
nexus->flags &= ~ESC_NF_RESET;
} else if (dev->sc_msg_out_len == 0) {
dev->sc_msg_out[0] = 0x08;
dev->sc_msg_out_len = 1;
}
cmd = ESC_CMD_TRANSFER_INFO;
for(i=0; i<dev->sc_msg_out_len; i++)
*rp->esc_fifo = dev->sc_msg_out[i];
dev->sc_msg_out_len = 0;
break;
case ESC_PHASE_MESSAGE_IN:
nexus->state = ESC_NS_MSG_IN;
while(!(nexus->flags & ESC_NF_HAS_MSG)) {
*rp->esc_command = ESC_CMD_TRANSFER_INFO;
esciwait(dev);
dev->sc_msg_in[dev->sc_msg_in_len++] = *rp->esc_fifo;
if (dev->sc_msg_in[0] >= 0x80) ;
else if (dev->sc_msg_in[0] >= 0x30) ;
else if (((dev->sc_msg_in[0] >= 0x20) &&
(dev->sc_msg_in_len == 2)) ||
((dev->sc_msg_in[0] != 0x01) &&
(dev->sc_msg_in_len == 1))) {
nexus->flags |= ESC_NF_HAS_MSG;
break;
} else {
if (dev->sc_msg_in_len >= 2)
if ((dev->sc_msg_in[1]+2) == dev->sc_msg_in_len) {
nexus->flags |= ESC_NF_HAS_MSG;
break;
}
}
*rp->esc_command = ESC_CMD_MESSAGE_ACCEPTED;
esciwait(dev);
if ((dev->sc_status & ESC_STAT_PHASE_MASK) !=
ESC_PHASE_MESSAGE_IN)
break;
}
cmd = ESC_CMD_MESSAGE_ACCEPTED;
if (nexus->flags & ESC_NF_HAS_MSG) {
switch(dev->sc_msg_in[0]) {
case 0x00:
nexus->state = ESC_NS_DONE;
break;
case 0x04:
nexus->state = ESC_NS_DISCONNECTING;
break;
case 0x02:
esc_save_pointers(dev);
break;
case 0x03:
esc_restore_pointers(dev);
break;
case 0x07:
if (nexus->flags & ESC_NF_SDTR_SENT) {
nexus->flags &= ~ESC_NF_SDTR_SENT;
nexus->config3 &= ~ESC_CFG3_FASTSCSI;
nexus->syncper = 5;
nexus->syncoff = 0;
*rp->esc_syncper = nexus->syncper;
*rp->esc_syncoff = nexus->syncoff;
*rp->esc_config3 = nexus->config3;
} else
panic("escintr: Unknown message rejected!");
break;
case 0x08:
break;
case 0x01:
switch(dev->sc_msg_in[2]) {
case 0x01:
period = 4*dev->sc_msg_in[3];
offset = dev->sc_msg_in[4];
if (offset > 16)
offset = 16;
if ((period < 200) &&
(dev->sc_clock_freq <= 25))
period = 200;
if (offset == 0)
period = 5*dev->sc_clock_period;
nexus->syncper = period/
dev->sc_clock_period;
nexus->syncoff = offset;
if (period < 200)
nexus->config3 |= ESC_CFG3_FASTSCSI;
else
nexus->config3 &=~ESC_CFG3_FASTSCSI;
nexus->flags |= ESC_NF_SYNC_TESTED;
*rp->esc_syncper = nexus->syncper;
*rp->esc_syncoff = nexus->syncoff;
*rp->esc_config3 = nexus->config3;
if (!(nexus->flags& ESC_NF_SDTR_SENT))
{
if ((dev->sc_config_flags &
ESC_NO_SYNCH) ||
(dev->sc_config_flags &
ESC_NO_DMA) ||
esc_inhibit_sync[
nexus->lun_unit & 7]) {
period = 200;
offset = 0;
}
nexus->offset = offset;
nexus->period = period;
nexus->flags |= ESC_NF_DO_SDTR;
*rp->esc_command = ESC_CMD_SET_ATN;
}
nexus->flags &= ~ESC_NF_SDTR_SENT;
break;
case 0x00:
case 0x02:
case 0x03:
default:
dev->sc_msg_out[0] = 0x07;
dev->sc_msg_out_len = 1;
*rp->esc_command = ESC_CMD_SET_ATN;
cmd = ESC_CMD_MESSAGE_ACCEPTED;
break;
}
break;
default:
dev->sc_msg_out[0] = 0x07;
dev->sc_msg_out_len = 1;
*rp->esc_command = ESC_CMD_SET_ATN;
cmd = ESC_CMD_MESSAGE_ACCEPTED;
break;
}
nexus->flags &= ~ESC_NF_HAS_MSG;
dev->sc_msg_in_len = 0;
}
break;
default:
printf("ESCINTR: UNKNOWN PHASE! phase: %d\n",
dev->sc_status & ESC_STAT_PHASE_MASK);
dev->sc_led(dev, 0);
esc_scsidone(dev, nexus->xs, -4);
return(-1);
}
if (cmd)
*rp->esc_command = cmd;
return(0);
}
void
escintr(struct esc_softc *dev)
{
esc_regmap_p rp;
struct nexus *nexus;
rp = dev->sc_esc;
if (!esc_pretests(dev, rp)) {
nexus = dev->sc_cur_nexus;
if (nexus == NULL)
nexus = dev->sc_sel_nexus;
if (nexus)
if (!esc_midaction(dev, rp, nexus))
esc_postaction(dev, rp, nexus);
}
}
void
escicmd(struct esc_softc *dev, struct esc_pending *pendp)
{
struct nexus *nexus;
nexus = &dev->sc_nexus[pendp->xs->xs_periph->periph_target];
if (!escselect(dev, pendp, (char *)pendp->xs->cmd, pendp->xs->cmdlen,
(char *)pendp->xs->data, pendp->xs->datalen,
ESC_SELECT_I))
panic("escicmd: Couldn't select unit");
while(nexus->state != ESC_NS_FINISHED) {
esciwait(dev);
escintr(dev);
}
nexus->flags &= ~ESC_NF_SYNC_TESTED;
}