#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ncr5380sbc.c,v 1.74 2024/10/29 15:58:14 nat Exp $");
#include "opt_ddb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/errno.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/scsipi_debug.h>
#include <dev/scsipi/scsi_message.h>
#include <dev/scsipi/scsiconf.h>
#ifdef DDB
#include <ddb/db_output.h>
#endif
#include <dev/ic/ncr5380reg.h>
#include <dev/ic/ncr5380var.h>
#include <dev/ic/ncr53c400reg.h>
static void ncr5380_reset_scsibus(struct ncr5380_softc *);
static void ncr5380_sched(struct ncr5380_softc *);
static void ncr5380_done(struct ncr5380_softc *);
static int ncr5380_select(struct ncr5380_softc *, struct sci_req *);
static void ncr5380_reselect(struct ncr5380_softc *);
static int ncr5380_msg_in(struct ncr5380_softc *);
static int ncr5380_msg_out(struct ncr5380_softc *);
static int ncr5380_data_xfer(struct ncr5380_softc *, int);
static int ncr5380_command(struct ncr5380_softc *);
static int ncr5380_status(struct ncr5380_softc *);
static void ncr5380_machine(struct ncr5380_softc *);
void ncr5380_abort(struct ncr5380_softc *);
void ncr5380_cmd_timeout(void *);
#define ACT_CONTINUE 0x00
#define ACT_DISCONNECT 0x01
#define ACT_CMD_DONE 0x02
#define ACT_RESET_BUS 0x04
#define ACT_WAIT_DMA 0x10
#ifndef DDB
#ifndef Debugger
#define Debugger() printf("Debug: ncr5380.c:%d\n", __LINE__)
#endif
#endif
#ifdef NCR5380_DEBUG
struct ncr5380_softc *ncr5380_debug_sc;
#define NCR_DBG_BREAK 1
#define NCR_DBG_CMDS 2
int ncr5380_debug = 0;
#define NCR_BREAK() \
do { if (ncr5380_debug & NCR_DBG_BREAK) Debugger(); } while (0)
static void ncr5380_show_scsi_cmd(struct scsipi_xfer *);
#ifdef DDB
void ncr5380_clear_trace(void);
void ncr5380_show_trace(void);
void ncr5380_show_req(struct sci_req *);
void ncr5380_show_state(void);
#endif
#else
#define NCR_BREAK()
#define ncr5380_show_scsi_cmd(xs)
#endif
static const char *
phase_names[8] = {
"DATA_OUT",
"DATA_IN",
"COMMAND",
"STATUS",
"UNSPEC1",
"UNSPEC2",
"MSG_OUT",
"MSG_IN",
};
int ncr5380_wait_phase_timo = 1000 * 10 * 300;
int ncr5380_wait_req_timo = 1000 * 50;
int ncr5380_wait_nrq_timo = 1000 * 25;
static inline int ncr5380_wait_req(struct ncr5380_softc *);
static inline int ncr5380_wait_not_req(struct ncr5380_softc *);
static inline void ncr_sched_msgout(struct ncr5380_softc *, int);
static inline int
ncr5380_wait_req(struct ncr5380_softc *sc)
{
int timo = ncr5380_wait_req_timo;
for (;;) {
if (NCR5380_READ(sc, sci_bus_csr) & SCI_BUS_REQ) {
timo = 0;
break;
}
if (--timo < 0)
break;
delay(2);
}
return timo;
}
static inline int
ncr5380_wait_not_req(struct ncr5380_softc *sc)
{
int timo = ncr5380_wait_nrq_timo;
for (;;) {
if ((NCR5380_READ(sc, sci_bus_csr) & SCI_BUS_REQ) == 0) {
timo = 0;
break;
}
if (--timo < 0)
break;
delay(2);
}
return timo;
}
static inline void
ncr_sched_msgout(struct ncr5380_softc *sc, int msg_code)
{
if (sc->sc_msgpriq == 0) {
uint8_t icmd;
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
NCR5380_WRITE(sc, sci_icmd, (icmd | SCI_ICMD_ATN));
delay(2);
}
sc->sc_msgpriq |= msg_code;
}
int
ncr5380_pio_out(struct ncr5380_softc *sc, int phase, int count, uint8_t *data)
{
uint8_t icmd;
int resid;
int error;
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
icmd |= SCI_ICMD_DATA;
NCR5380_WRITE(sc, sci_icmd, icmd);
resid = count;
while (resid > 0) {
if (!SCI_BUSY(sc)) {
NCR_TRACE("pio_out: lost BSY, resid=%d\n", resid);
break;
}
if (ncr5380_wait_req(sc)) {
NCR_TRACE("pio_out: no REQ, resid=%d\n", resid);
break;
}
if (SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr)) != phase)
break;
if (data)
NCR5380_WRITE(sc, sci_odata, *data++);
else
NCR5380_WRITE(sc, sci_odata, 0);
icmd |= SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
error = ncr5380_wait_not_req(sc);
icmd &= ~SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (error) {
NCR_TRACE("pio_out: stuck REQ, resid=%d\n", resid);
break;
}
--resid;
}
icmd &= ~SCI_ICMD_DATA;
NCR5380_WRITE(sc, sci_icmd, icmd);
return count - resid;
}
int
ncr5380_pio_in(struct ncr5380_softc *sc, int phase, int count, uint8_t *data)
{
uint8_t icmd;
int resid;
int error;
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
resid = count;
while (resid > 0) {
if (!SCI_BUSY(sc)) {
NCR_TRACE("pio_in: lost BSY, resid=%d\n", resid);
break;
}
if (ncr5380_wait_req(sc)) {
NCR_TRACE("pio_in: no REQ, resid=%d\n", resid);
break;
}
if (SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr)) != phase)
break;
if (data)
*data++ = NCR5380_READ(sc, sci_data);
else
(void) NCR5380_READ(sc, sci_data);
icmd |= SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
error = ncr5380_wait_not_req(sc);
icmd &= ~SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (error) {
NCR_TRACE("pio_in: stuck REQ, resid=%d\n", resid);
break;
}
--resid;
}
return count - resid;
}
void
ncr5380_init(struct ncr5380_softc *sc)
{
int i, j;
#ifdef NCR5380_DEBUG
ncr5380_debug_sc = sc;
#endif
for (i = 0; i < SCI_OPENINGS; i++)
sc->sc_ring[i].sr_xs = NULL;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
sc->sc_matrix[i][j] = NULL;
sc->sc_prevphase = PHASE_INVALID;
sc->sc_state = NCR_IDLE;
#ifdef NCR5380_USE_BUS_SPACE
if (sc->sc_rev == NCR_VARIANT_NCR53C400)
bus_space_write_1(sc->sc_regt, sc->sc_regh, C400_CSR,
C400_CSR_5380_ENABLE);
#endif
NCR5380_WRITE(sc, sci_tcmd, PHASE_INVALID);
NCR5380_WRITE(sc, sci_icmd, 0);
NCR5380_WRITE(sc, sci_mode, 0);
NCR5380_WRITE(sc, sci_sel_enb, 0);
SCI_CLR_INTR(sc);
NCR5380_WRITE(sc, sci_sel_enb, 0x80);
if (sc->sc_intr_on) {
NCR_TRACE("init: intr ON\n", 0);
sc->sc_intr_on(sc);
}
}
static void
ncr5380_reset_scsibus(struct ncr5380_softc *sc)
{
struct sci_req *sr;
NCR_TRACE("reset_scsibus, cur=0x%x\n",
(long) sc->sc_current);
NCR5380_WRITE(sc, sci_icmd, SCI_ICMD_RST);
delay(500);
NCR5380_WRITE(sc, sci_icmd, 0);
NCR5380_WRITE(sc, sci_mode, 0);
NCR5380_WRITE(sc, sci_tcmd, PHASE_INVALID);
SCI_CLR_INTR(sc);
delay(100000);
sr = sc->sc_current;
if (sr && (sc->sc_state & NCR_ABORTING) == 0)
ncr5380_abort(sc);
}
int
ncr5380_intr(void *arg)
{
struct ncr5380_softc *sc = arg;
int claimed = 0;
NCR_TRACE("intr: top, state=%d\n", sc->sc_state);
if (sc->sc_state == NCR_IDLE) {
if (sc->sc_intr_off) {
NCR_TRACE("intr: for reselect, intr off\n", 0);
sc->sc_intr_off(sc);
}
ncr5380_reselect(sc);
}
if (sc->sc_state & NCR_WORKING) {
NCR_TRACE("intr: call machine, cur=0x%x\n",
(long) sc->sc_current);
ncr5380_machine(sc);
NCR_TRACE("intr: machine done, cur=0x%x\n",
(long) sc->sc_current);
claimed = 1;
}
if (sc->sc_state == NCR_IDLE) {
NCR_TRACE("intr: call sched, cur=0x%x\n",
(long) sc->sc_current);
ncr5380_sched(sc);
NCR_TRACE("intr: sched done, cur=0x%x\n",
(long) sc->sc_current);
}
return claimed;
}
void
ncr5380_abort(struct ncr5380_softc *sc)
{
if (sc->sc_intr_off) {
NCR_TRACE("abort: intr off\n", 0);
sc->sc_intr_off(sc);
}
sc->sc_state |= NCR_ABORTING;
if ((sc->sc_state & NCR_DOINGDMA) == 0) {
ncr_sched_msgout(sc, SEND_ABORT);
}
NCR_TRACE("abort: call machine, cur=0x%x\n",
(long) sc->sc_current);
ncr5380_machine(sc);
NCR_TRACE("abort: machine done, cur=0x%x\n",
(long) sc->sc_current);
if (sc->sc_intr_on) {
NCR_TRACE("abort: intr ON\n", 0);
sc->sc_intr_on(sc);
}
}
void
ncr5380_cmd_timeout(void *arg)
{
struct sci_req *sr = arg;
struct scsipi_xfer *xs;
struct scsipi_periph *periph;
struct ncr5380_softc *sc;
int s;
s = splbio();
xs = sr->sr_xs;
if (xs == NULL) {
printf("ncr5380_cmd_timeout: no scsipi_xfer\n");
goto out;
}
periph = xs->xs_periph;
sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
printf("%s: cmd timeout, targ=%d, lun=%d\n",
device_xname(sc->sc_dev),
sr->sr_target, sr->sr_lun);
sr->sr_flags |= SR_OVERDUE;
if (sc->sc_current == sr) {
NCR_TRACE("cmd_tmo: call abort, sr=0x%x\n", (long) sr);
ncr5380_abort(sc);
} else {
NCR_TRACE("cmd_tmo: clear matrix, t/l=0x%02x\n",
(sr->sr_target << 4) | sr->sr_lun);
sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
}
if (sc->sc_state == NCR_IDLE) {
NCR_TRACE("cmd_tmo: call sched, cur=0x%lx\n",
(long)sc->sc_current);
ncr5380_sched(sc);
NCR_TRACE("cmd_tmo: sched done, cur=0x%lx\n",
(long)sc->sc_current);
}
out:
splx(s);
}
void
ncr5380_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
void *arg)
{
struct scsipi_xfer *xs;
struct scsipi_periph *periph;
struct ncr5380_softc *sc;
struct sci_req *sr;
int s, i, flags;
sc = device_private(chan->chan_adapter->adapt_dev);
switch (req) {
case ADAPTER_REQ_RUN_XFER:
xs = arg;
periph = xs->xs_periph;
flags = xs->xs_control;
if (flags & XS_CTL_DATA_UIO)
panic("%s: scsi data uio requested", __func__);
s = splbio();
if (flags & XS_CTL_POLL) {
sr = sc->sc_current;
if (sr) {
#ifdef NCR5380_DEBUG
printf("%s: polled request aborting %d/%d\n",
device_xname(sc->sc_dev),
sr->sr_target, sr->sr_lun);
#endif
ncr5380_abort(sc);
}
if (sc->sc_state != NCR_IDLE) {
panic("%s: polled request, abort failed",
__func__);
}
}
for (i = 0; i < SCI_OPENINGS; i++)
if (sc->sc_ring[i].sr_xs == NULL)
goto new;
scsipi_printaddr(periph);
printf("unable to allocate ring slot\n");
panic("%s", __func__);
new:
sr = &sc->sc_ring[i];
sr->sr_xs = xs;
sr->sr_target = periph->periph_target;
sr->sr_lun = periph->periph_lun;
sr->sr_dma_hand = NULL;
sr->sr_dataptr = xs->data;
sr->sr_datalen = xs->datalen;
sr->sr_flags = (flags & XS_CTL_POLL) ? SR_IMMED : 0;
if (xs->xs_control & XS_CTL_REQSENSE)
sr->sr_flags |= SR_IMMED;
sr->sr_status = -1;
sc->sc_ncmds++;
NCR_TRACE("scsipi_cmd: new sr=0x0\n", (long)sr);
if (flags & XS_CTL_POLL) {
sc->sc_rr = i;
}
if (sc->sc_state == NCR_IDLE) {
NCR_TRACE("scsipi_cmd: call sched, cur=0x%x\n",
(long) sc->sc_current);
ncr5380_sched(sc);
NCR_TRACE("scsipi_cmd: sched done, cur=0x%x\n",
(long) sc->sc_current);
}
if (flags & XS_CTL_POLL) {
if ((xs->xs_status & XS_STS_DONE) == 0)
panic("%s: poll didn't finish", __func__);
}
splx(s);
return;
case ADAPTER_REQ_GROW_RESOURCES:
return;
case ADAPTER_REQ_SET_XFER_MODE:
{
struct scsipi_xfer_mode *xm = arg;
xm->xm_mode = 0;
xm->xm_period = 0;
xm->xm_offset = 0;
scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
return;
}
}
}
static void
ncr5380_done(struct ncr5380_softc *sc)
{
struct sci_req *sr;
struct scsipi_xfer *xs;
#ifdef DIAGNOSTIC
if (sc->sc_state == NCR_IDLE)
panic("%s: state=idle", __func__);
if (sc->sc_current == NULL)
panic("%s: current=0", __func__);
#endif
sr = sc->sc_current;
xs = sr->sr_xs;
NCR_TRACE("done: top, cur=0x%x\n", (long) sc->sc_current);
if (sr->sr_dma_hand) {
NCR_TRACE("done: dma_free, dh=0x%x\n",
(long) sr->sr_dma_hand);
(*sc->sc_dma_free)(sc);
}
#ifdef DIAGNOSTIC
if (sr->sr_dma_hand)
panic("%s: DMA free did not", __func__);
#endif
if (sc->sc_state & NCR_ABORTING) {
NCR_TRACE("done: aborting, error=%d\n", xs->error);
if (xs->error == XS_NOERROR)
xs->error = XS_TIMEOUT;
}
NCR_TRACE("done: check error=%d\n", (long) xs->error);
if (xs->error != XS_NOERROR)
goto finish;
NCR_TRACE("done: check status=%d\n", sr->sr_status);
xs->status = sr->sr_status;
switch (sr->sr_status) {
case SCSI_OK:
xs->error = XS_NOERROR;
break;
case SCSI_CHECK:
case SCSI_BUSY:
xs->error = XS_BUSY;
break;
case -1:
default:
printf("%s: target %d, bad status=%d\n",
device_xname(sc->sc_dev), sr->sr_target, sr->sr_status);
xs->error = XS_DRIVER_STUFFUP;
break;
}
finish:
NCR_TRACE("done: finish, error=%d\n", xs->error);
#ifdef DIAGNOSTIC
if ((sc->sc_state & NCR_WORKING) == 0)
panic("%s: bad state", __func__);
#endif
sc->sc_current = NULL;
sc->sc_matrix[sr->sr_target][sr->sr_lun] = NULL;
callout_stop(&sr->sr_xs->xs_callout);
sr->sr_xs = NULL;
sc->sc_ncmds--;
const bool aborting = sc->sc_state & NCR_ABORTING;
if (aborting)
scsipi_channel_freeze(&sc->sc_channel, 1);
scsipi_done(xs);
sc->sc_state = NCR_IDLE;
if (aborting)
scsipi_channel_thaw(&sc->sc_channel, 1);
}
static void
ncr5380_sched(struct ncr5380_softc *sc)
{
struct sci_req *sr;
struct scsipi_xfer *xs;
int target = 0, lun = 0;
int error, i;
if (sc->sc_intr_off) {
NCR_TRACE("sched: top, intr off\n", 0);
sc->sc_intr_off(sc);
}
next_job:
#ifdef DIAGNOSTIC
if (sc->sc_state != NCR_IDLE)
panic("%s: not idle", __func__);
if (sc->sc_current)
panic("%s: current set", __func__);
#endif
i = sc->sc_rr;
sr = NULL;
do {
if (sc->sc_ring[i].sr_xs) {
target = sc->sc_ring[i].sr_target;
lun = sc->sc_ring[i].sr_lun;
if (sc->sc_matrix[target][lun] == NULL) {
sc->sc_rr = i;
sr = &sc->sc_ring[i];
break;
}
}
i++;
if (i == SCI_OPENINGS)
i = 0;
} while (i != sc->sc_rr);
if (sr == NULL) {
NCR_TRACE("sched: no work, cur=0x%x\n",
(long) sc->sc_current);
if (sc->sc_intr_on) {
NCR_TRACE("sched: ret, intr ON\n", 0);
sc->sc_intr_on(sc);
}
return;
}
NCR_TRACE("sched: select for t/l=0x%02x\n",
(sr->sr_target << 4) | sr->sr_lun);
sc->sc_state = NCR_WORKING;
error = ncr5380_select(sc, sr);
if (sc->sc_current) {
if (sr->sr_flags & SR_IMMED) {
printf("%s: reselected while polling (abort)\n",
device_xname(sc->sc_dev));
sc->sc_state |= NCR_ABORTING;
sc->sc_msgpriq |= SEND_ABORT;
}
sr = sc->sc_current;
xs = sr->sr_xs;
NCR_TRACE("sched: reselect, new sr=0x%x\n", (long)sr);
goto have_nexus;
}
sc->sc_matrix[target][lun] = sr;
sc->sc_current = sr;
xs = sr->sr_xs;
sc->sc_dataptr = sr->sr_dataptr;
sc->sc_datalen = sr->sr_datalen;
sc->sc_prevphase = PHASE_INVALID;
sc->sc_msgpriq = SEND_IDENTIFY;
sc->sc_msgoutq = 0;
sc->sc_msgout = 0;
NCR_TRACE("sched: select rv=%d\n", error);
switch (error) {
case XS_NOERROR:
break;
case XS_BUSY:
printf("%s: select found SCSI bus busy, resetting...\n",
device_xname(sc->sc_dev));
ncr5380_reset_scsibus(sc);
case XS_SELTIMEOUT:
default:
xs->error = error;
NCR_TRACE("sched: call done, sr=0x%x\n", (long)sr);
ncr5380_done(sc);
sc->sc_dataptr = NULL;
sc->sc_datalen = 0;
sc->sc_prevphase = PHASE_INVALID;
sc->sc_msgpriq = 0;
sc->sc_msgoutq = 0;
sc->sc_msgout = 0;
goto next_job;
}
if (sr->sr_flags & SR_OVERDUE) {
NCR_TRACE("sched: overdue, sr=0x%x\n", (long)sr);
sc->sc_state |= NCR_ABORTING;
sc->sc_msgpriq |= SEND_ABORT;
goto have_nexus;
}
#ifdef NCR5380_DEBUG
if (ncr5380_debug & NCR_DBG_CMDS) {
printf("ncr5380_sched: begin, target=%d, LUN=%d\n",
xs->xs_periph->periph_target, xs->xs_periph->periph_lun);
ncr5380_show_scsi_cmd(xs);
}
#endif
if (xs->xs_control & XS_CTL_RESET) {
NCR_TRACE("sched: cmd=reset, sr=0x%x\n", (long)sr);
sc->sc_msgpriq |= SEND_DEV_RESET;
goto have_nexus;
}
#ifdef DIAGNOSTIC
if ((xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) == 0) {
if (sc->sc_dataptr) {
printf("%s: ptr but no data in/out flags?\n",
device_xname(sc->sc_dev));
NCR_BREAK();
sc->sc_dataptr = NULL;
}
}
#endif
if (sc->sc_dataptr && sc->sc_dma_alloc &&
(sc->sc_datalen >= sc->sc_min_dma_len))
{
NCR_TRACE("sched: dma_alloc, len=%d\n", sc->sc_datalen);
(*sc->sc_dma_alloc)(sc);
}
if (sr->sr_dma_hand && sc->sc_dma_setup) {
NCR_TRACE("sched: dma_setup, dh=0x%x\n",
(long) sr->sr_dma_hand);
sc->sc_dma_setup(sc);
}
if ((sr->sr_flags & SR_IMMED) == 0) {
i = mstohz(xs->timeout);
NCR_TRACE("sched: set timeout=%d\n", i);
callout_reset(&sr->sr_xs->xs_callout, i,
ncr5380_cmd_timeout, sr);
}
have_nexus:
NCR_TRACE("sched: call machine, cur=0x%x\n",
(long) sc->sc_current);
ncr5380_machine(sc);
NCR_TRACE("sched: machine done, cur=0x%x\n",
(long) sc->sc_current);
if (sc->sc_state == NCR_IDLE)
goto next_job;
return;
}
void
ncr5380_reselect(struct ncr5380_softc *sc)
{
struct sci_req *sr;
int target, lun, phase, timo;
int target_mask = 0;
uint8_t bus, data, icmd, mode, msg;
#ifdef DIAGNOSTIC
if (sc->sc_current)
panic("%s: current set", __func__);
#endif
bus = NCR5380_READ(sc, sci_bus_csr);
if ((bus & SCI_BUS_SEL) == 0) {
return;
}
timo = ncr5380_wait_nrq_timo;
for (;;) {
if ((bus & SCI_BUS_BSY) == 0)
break;
if (--timo <= 0) {
printf("%s: reselect, BSY stuck, bus=0x%x\n",
device_xname(sc->sc_dev), bus);
ncr5380_reset_scsibus(sc);
return;
}
delay(2);
bus = NCR5380_READ(sc, sci_bus_csr);
if ((bus & SCI_BUS_SEL) == 0)
return;
}
NCR_TRACE("reselect, valid data after %d loops\n",
ncr5380_wait_nrq_timo - timo);
delay(2);
data = NCR5380_READ(sc, sci_data) & 0xFF;
if ((bus & SCI_BUS_IO) == 0) {
printf("%s: selected as target, data=0x%x\n",
device_xname(sc->sc_dev), data);
ncr5380_reset_scsibus(sc);
return;
}
for (target = 0; target < 7; target++) {
target_mask = (1 << target);
if (data & target_mask)
break;
}
if ((data & 0x7F) != target_mask) {
printf("%s: bad reselect, data=0x%x\n",
device_xname(sc->sc_dev), data);
return;
}
NCR_TRACE("reselect: target=0x%x\n", target);
NCR5380_WRITE(sc, sci_icmd, SCI_ICMD_BSY);
timo = ncr5380_wait_nrq_timo;
for (;;) {
bus = NCR5380_READ(sc, sci_bus_csr);
if ((bus & SCI_BUS_SEL) == 0)
break;
if (--timo <= 0) {
printf("%s: reselect, SEL stuck, bus=0x%x\n",
device_xname(sc->sc_dev), bus);
NCR_BREAK();
break;
}
delay(2);
}
NCR5380_WRITE(sc, sci_icmd, 0);
NCR5380_WRITE(sc, sci_sel_enb, 0);
SCI_CLR_INTR(sc);
lun = 0;
if (ncr5380_wait_req(sc)) {
printf("%s: reselect, no REQ\n",
device_xname(sc->sc_dev));
goto abort;
}
phase = SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr));
if (phase != PHASE_MSG_IN) {
printf("%s: reselect, phase=%d\n",
device_xname(sc->sc_dev), phase);
goto abort;
}
NCR5380_WRITE(sc, sci_tcmd, PHASE_MSG_IN);
msg = NCR5380_READ(sc, sci_data);
if ((msg & 0x80) == 0) {
printf("%s: reselect, not identify, msg=%d\n",
device_xname(sc->sc_dev), msg);
goto abort;
}
lun = msg & 7;
sr = sc->sc_matrix[target][lun];
if (sr) {
sc->sc_state |= NCR_WORKING;
sc->sc_current = sr;
NCR_TRACE("reselect: resume sr=0x%x\n", (long)sr);
sc->sc_dataptr = sr->sr_dataptr;
sc->sc_datalen = sr->sr_datalen;
sc->sc_prevphase = PHASE_INVALID;
sc->sc_msgpriq = 0;
sc->sc_msgoutq = 0;
sc->sc_msgout = 0;
if (sc->sc_parity_disable & target_mask)
mode = SCI_MODE_MONBSY;
else
mode = SCI_MODE_MONBSY | SCI_MODE_PAR_CHK;
if (sc->sc_rev == NCR_VARIANT_CXD1180)
mode &= ~SCI_MODE_MONBSY;
NCR5380_WRITE(sc, sci_mode, mode);
if (sr->sr_dma_hand && sc->sc_dma_setup) {
NCR_TRACE("reselect: call DMA setup, dh=0x%x\n",
(long) sr->sr_dma_hand);
sc->sc_dma_setup(sc);
}
ncr5380_pio_in(sc, PHASE_MSG_IN, 1, &msg);
return;
}
printf("%s: phantom reselect: target=%d, LUN=%d\n",
device_xname(sc->sc_dev), target, lun);
abort:
sc->sc_state |= NCR_ABORTING;
icmd = SCI_ICMD_ATN;
NCR5380_WRITE(sc, sci_icmd, icmd);
delay(2);
ncr5380_pio_in(sc, PHASE_MSG_IN, 1, &msg);
sc->sc_prevphase = PHASE_INVALID;
sc->sc_msgpriq = SEND_ABORT;
ncr5380_msg_out(sc);
NCR5380_WRITE(sc, sci_tcmd, PHASE_INVALID);
NCR5380_WRITE(sc, sci_sel_enb, 0);
SCI_CLR_INTR(sc);
NCR5380_WRITE(sc, sci_sel_enb, 0x80);
sc->sc_state &= ~NCR_ABORTING;
}
static int
ncr5380_select(struct ncr5380_softc *sc, struct sci_req *sr)
{
int timo, s, target_mask;
uint8_t data, icmd, mode;
ncr5380_reselect(sc);
if (sc->sc_current) {
NCR_TRACE("select: reselect, cur=0x%x\n",
(long) sc->sc_current);
return XS_BUSY;
}
NCR5380_WRITE(sc, sci_tcmd, PHASE_DATA_OUT);
NCR5380_WRITE(sc, sci_icmd, 0);
icmd = 0;
NCR5380_WRITE(sc, sci_mode, 0);
s = splvm();
NCR5380_WRITE(sc, sci_odata, 0x80);
NCR5380_WRITE(sc, sci_mode, SCI_MODE_ARB);
#define WAIT_AIP_USEC 20
timo = WAIT_AIP_USEC;
for (;;) {
if (NCR5380_READ(sc, sci_icmd) & SCI_ICMD_AIP)
break;
if (timo <= 0) {
NCR_TRACE("select: bus busy, rc=%d\n", XS_BUSY);
goto lost_arb;
}
timo -= 2;
delay(2);
}
NCR_TRACE("select: have AIP after %d uSec.\n",
WAIT_AIP_USEC - timo);
delay(3);
if (NCR5380_READ(sc, sci_icmd) & SCI_ICMD_LST) {
NCR_TRACE("select: lost one, rc=%d\n", XS_BUSY);
goto lost_arb;
}
icmd = (SCI_ICMD_BSY | SCI_ICMD_SEL);
NCR5380_WRITE(sc, sci_icmd, icmd);
delay(2);
if ((sc->sc_rev != NCR_VARIANT_CXD1180) &&
(NCR5380_READ(sc, sci_icmd) & SCI_ICMD_LST)) {
NCR_TRACE("select: lost two, rc=%d\n", XS_BUSY);
lost_arb:
NCR5380_WRITE(sc, sci_icmd, 0);
NCR5380_WRITE(sc, sci_mode, 0);
splx(s);
ncr5380_reselect(sc);
return XS_BUSY;
}
NCR5380_WRITE(sc, sci_mode, 0);
NCR5380_WRITE(sc, sci_sel_enb, 0);
splx(s);
target_mask = (1 << sr->sr_target);
data = 0x80 | target_mask;
NCR5380_WRITE(sc, sci_odata, data);
icmd |= (SCI_ICMD_DATA | SCI_ICMD_ATN);
NCR5380_WRITE(sc, sci_icmd, icmd);
delay(2);
icmd &= ~SCI_ICMD_BSY;
NCR5380_WRITE(sc, sci_icmd, icmd);
delay(3);
for (timo = 25000;;) {
if (NCR5380_READ(sc, sci_bus_csr) & SCI_BUS_BSY)
goto success;
if (--timo <= 0)
break;
delay(10);
}
icmd &= ~SCI_ICMD_DATA;
NCR5380_WRITE(sc, sci_icmd, icmd);
delay(201);
if ((NCR5380_READ(sc, sci_bus_csr) & SCI_BUS_BSY) == 0) {
NCR5380_WRITE(sc, sci_tcmd, PHASE_INVALID);
NCR5380_WRITE(sc, sci_icmd, 0);
NCR5380_WRITE(sc, sci_mode, 0);
NCR5380_WRITE(sc, sci_sel_enb, 0);
SCI_CLR_INTR(sc);
NCR5380_WRITE(sc, sci_sel_enb, 0x80);
NCR_TRACE("select: device down, rc=%d\n", XS_SELTIMEOUT);
return XS_SELTIMEOUT;
}
success:
icmd &= ~(SCI_ICMD_DATA | SCI_ICMD_SEL);
NCR5380_WRITE(sc, sci_icmd, icmd);
if (sc->sc_parity_disable & target_mask)
mode = SCI_MODE_MONBSY;
else
mode = SCI_MODE_MONBSY | SCI_MODE_PAR_CHK;
if (sc->sc_rev == NCR_VARIANT_CXD1180)
mode &= ~SCI_MODE_MONBSY;
NCR5380_WRITE(sc, sci_mode, mode);
return XS_NOERROR;
}
static int
ncr5380_msg_in(struct ncr5380_softc *sc)
{
struct sci_req *sr = sc->sc_current;
struct scsipi_xfer *xs = sr->sr_xs;
int n, phase;
int act_flags;
uint8_t icmd;
NCR5380_WRITE(sc, sci_tcmd, PHASE_MSG_IN);
act_flags = ACT_CONTINUE;
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
if (sc->sc_prevphase == PHASE_MSG_IN) {
n = sc->sc_imp - sc->sc_imess;
NCR_TRACE("msg_in: continuation, n=%d\n", n);
goto nextbyte;
}
sc->sc_state &= ~NCR_DROP_MSGIN;
nextmsg:
n = 0;
sc->sc_imp = &sc->sc_imess[n];
nextbyte:
for (;;) {
if (!SCI_BUSY(sc)) {
NCR_TRACE("msg_in: lost BSY, n=%d\n", n);
act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
return act_flags;
}
if (ncr5380_wait_req(sc)) {
NCR_TRACE("msg_in: BSY but no REQ, n=%d\n", n);
return act_flags;
}
phase = SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr));
if (phase != PHASE_MSG_IN) {
return act_flags;
}
if (NCR5380_READ(sc, sci_csr) & SCI_CSR_PERR) {
ncr_sched_msgout(sc, SEND_PARITY_ERROR);
sc->sc_state |= NCR_DROP_MSGIN;
}
if ((sc->sc_state & NCR_DROP_MSGIN) == 0) {
if (n >= NCR_MAX_MSG_LEN) {
ncr_sched_msgout(sc, SEND_REJECT);
sc->sc_state |= NCR_DROP_MSGIN;
} else {
*sc->sc_imp++ = NCR5380_READ(sc, sci_data);
n++;
if (n == 1 && MSG_IS1BYTE(sc->sc_imess[0]))
goto have_msg;
if (n == 2 && MSG_IS2BYTE(sc->sc_imess[0]))
goto have_msg;
if (n >= 3 && MSG_ISEXTENDED(sc->sc_imess[0]) &&
n == sc->sc_imess[1] + 2)
goto have_msg;
}
}
icmd |= SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (ncr5380_wait_not_req(sc)) {
NCR_TRACE("msg_in: drop, stuck REQ, n=%d\n", n);
act_flags |= ACT_RESET_BUS;
}
icmd &= ~SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (act_flags != ACT_CONTINUE)
return act_flags;
}
have_msg:
switch (sc->sc_imess[0]) {
case MSG_CMDCOMPLETE:
NCR_TRACE("msg_in: CMDCOMPLETE\n", 0);
act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
break;
case MSG_PARITY_ERROR:
NCR_TRACE("msg_in: PARITY_ERROR\n", 0);
ncr_sched_msgout(sc, sc->sc_msgout);
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
break;
case MSG_MESSAGE_REJECT:
NCR_TRACE("msg_in: got reject for 0x%x\n", sc->sc_msgout);
switch (sc->sc_msgout) {
case SEND_IDENTIFY:
break;
case SEND_INIT_DET_ERR:
goto abort;
}
break;
case MSG_NOOP:
NCR_TRACE("msg_in: NOOP\n", 0);
break;
case MSG_DISCONNECT:
NCR_TRACE("msg_in: DISCONNECT\n", 0);
act_flags |= ACT_DISCONNECT;
if ((xs->xs_periph->periph_quirks & PQUIRK_AUTOSAVE) == 0)
break;
case MSG_SAVEDATAPOINTER:
NCR_TRACE("msg_in: SAVE_PTRS\n", 0);
sr->sr_dataptr = sc->sc_dataptr;
sr->sr_datalen = sc->sc_datalen;
break;
case MSG_RESTOREPOINTERS:
NCR_TRACE("msg_in: RESTORE_PTRS\n", 0);
sc->sc_dataptr = sr->sr_dataptr;
sc->sc_datalen = sr->sr_datalen;
break;
case MSG_EXTENDED:
switch (sc->sc_imess[2]) {
case MSG_EXT_SDTR:
case MSG_EXT_WDTR:
goto reject;
default:
printf("%s: unrecognized MESSAGE EXTENDED; "
"sending REJECT\n",
device_xname(sc->sc_dev));
NCR_BREAK();
goto reject;
}
break;
default:
NCR_TRACE("msg_in: eh? imsg=0x%x\n", sc->sc_imess[0]);
printf("%s: unrecognized MESSAGE; sending REJECT\n",
device_xname(sc->sc_dev));
NCR_BREAK();
reject:
ncr_sched_msgout(sc, SEND_REJECT);
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
break;
abort:
sc->sc_state |= NCR_ABORTING;
ncr_sched_msgout(sc, SEND_ABORT);
break;
}
icmd |= SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (ncr5380_wait_not_req(sc)) {
NCR_TRACE("msg_in: last, stuck REQ, n=%d\n", n);
act_flags |= ACT_RESET_BUS;
}
icmd &= ~SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (act_flags == ACT_CONTINUE)
goto nextmsg;
return act_flags;
}
static int
ncr5380_msg_out(struct ncr5380_softc *sc)
{
struct sci_req *sr = sc->sc_current;
int act_flags, n, phase, progress;
uint8_t icmd, msg;
NCR5380_WRITE(sc, sci_tcmd, PHASE_MSG_OUT);
progress = 0;
act_flags = ACT_CONTINUE;
icmd = NCR5380_READ(sc, sci_icmd) & SCI_ICMD_RMASK;
icmd |= (SCI_ICMD_ATN | SCI_ICMD_DATA);
NCR5380_WRITE(sc, sci_icmd, icmd);
if (sc->sc_prevphase == PHASE_MSG_OUT) {
if (sc->sc_omp == sc->sc_omess) {
sc->sc_msgpriq |= sc->sc_msgoutq;
NCR_TRACE("msg_out: retrans priq=0x%x\n",
sc->sc_msgpriq);
} else {
n = sc->sc_omp - sc->sc_omess;
NCR_TRACE("msg_out: continuation, n=%d\n", n);
goto nextbyte;
}
}
sc->sc_msgoutq = 0;
nextmsg:
sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
sc->sc_msgpriq &= ~sc->sc_msgout;
sc->sc_msgoutq |= sc->sc_msgout;
switch (sc->sc_msgout) {
case SEND_IDENTIFY:
NCR_TRACE("msg_out: SEND_IDENTIFY\n", 0);
if (sr == NULL) {
printf("%s: SEND_IDENTIFY while not connected; "
"sending NOOP\n",
device_xname(sc->sc_dev));
NCR_BREAK();
goto noop;
}
msg = 0xc0;
if (sc->sc_no_disconnect & (1 << sr->sr_target))
msg = 0x80;
if (sr->sr_flags & (SR_IMMED))
msg = 0x80;
sc->sc_omess[0] = msg | sr->sr_lun;
n = 1;
break;
case SEND_DEV_RESET:
NCR_TRACE("msg_out: SEND_DEV_RESET\n", 0);
act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
sc->sc_omess[0] = MSG_BUS_DEV_RESET;
n = 1;
break;
case SEND_REJECT:
NCR_TRACE("msg_out: SEND_REJECT\n", 0);
sc->sc_omess[0] = MSG_MESSAGE_REJECT;
n = 1;
break;
case SEND_PARITY_ERROR:
NCR_TRACE("msg_out: SEND_PARITY_ERROR\n", 0);
sc->sc_omess[0] = MSG_PARITY_ERROR;
n = 1;
break;
case SEND_INIT_DET_ERR:
NCR_TRACE("msg_out: SEND_INIT_DET_ERR\n", 0);
sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
n = 1;
break;
case SEND_ABORT:
NCR_TRACE("msg_out: SEND_ABORT\n", 0);
act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
sc->sc_omess[0] = MSG_ABORT;
n = 1;
break;
case 0:
printf("%s: unexpected MESSAGE OUT; sending NOOP\n",
device_xname(sc->sc_dev));
NCR_BREAK();
noop:
NCR_TRACE("msg_out: send NOOP\n", 0);
sc->sc_omess[0] = MSG_NOOP;
n = 1;
break;
default:
printf("%s: weird MESSAGE OUT; sending NOOP\n",
device_xname(sc->sc_dev));
NCR_BREAK();
goto noop;
}
sc->sc_omp = &sc->sc_omess[n];
nextbyte:
while (n > 0) {
if (!SCI_BUSY(sc)) {
NCR_TRACE("msg_out: lost BSY, n=%d\n", n);
goto out;
}
if (ncr5380_wait_req(sc)) {
NCR_TRACE("msg_out: no REQ, n=%d\n", n);
goto out;
}
phase = SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr));
if (phase != PHASE_MSG_OUT) {
NCR_TRACE("msg_out: new phase=%d\n", phase);
goto out;
}
--n;
if (n == 0 && sc->sc_msgpriq == 0) {
icmd &= ~SCI_ICMD_ATN;
NCR5380_WRITE(sc, sci_icmd, icmd);
delay(2);
}
NCR5380_WRITE(sc, sci_odata, *--sc->sc_omp);
icmd |= SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (ncr5380_wait_not_req(sc)) {
NCR_TRACE("msg_out: stuck REQ, n=%d\n", n);
act_flags |= ACT_RESET_BUS;
}
icmd &= ~SCI_ICMD_ACK;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (act_flags & ACT_RESET_BUS)
goto out;
}
progress++;
if (sc->sc_msgpriq != 0) {
goto nextmsg;
}
out:
icmd &= ~SCI_ICMD_DATA;
NCR5380_WRITE(sc, sci_icmd, icmd);
if (!progress)
act_flags |= ACT_RESET_BUS;
return act_flags;
}
static int
ncr5380_command(struct ncr5380_softc *sc)
{
struct sci_req *sr = sc->sc_current;
struct scsipi_xfer *xs = sr->sr_xs;
int len;
NCR5380_WRITE(sc, sci_tcmd, PHASE_COMMAND);
len = ncr5380_pio_out(sc, PHASE_COMMAND, xs->cmdlen,
(uint8_t *)xs->cmd);
if (len != xs->cmdlen) {
#ifdef NCR5380_DEBUG
printf("%s: short transfer: wanted %d got %d.\n",
__func__, xs->cmdlen, len);
ncr5380_show_scsi_cmd(xs);
NCR_BREAK();
#endif
if (len < 6) {
xs->error = XS_DRIVER_STUFFUP;
sc->sc_state |= NCR_ABORTING;
ncr_sched_msgout(sc, SEND_ABORT);
}
}
return ACT_CONTINUE;
}
static int
ncr5380_data_xfer(struct ncr5380_softc *sc, int phase)
{
struct sci_req *sr = sc->sc_current;
struct scsipi_xfer *xs = sr->sr_xs;
int expected_phase;
int len;
if (sc->sc_state & NCR_ABORTING) {
printf("%s: aborting, but phase=%s (reset)\n",
device_xname(sc->sc_dev), phase_names[phase & 7]);
return ACT_RESET_BUS;
}
expected_phase = (xs->xs_control & XS_CTL_DATA_OUT) ?
PHASE_DATA_OUT : PHASE_DATA_IN;
if (phase != expected_phase) {
printf("%s: data phase error\n", device_xname(sc->sc_dev));
goto abort;
}
if (sc->sc_datalen <= 0) {
if (phase == PHASE_DATA_IN)
ncr5380_pio_in(sc, phase, 4096, NULL);
else
ncr5380_pio_out(sc, phase, 4096, NULL);
if (SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr)) == phase) {
printf("%s: too much data padding\n",
device_xname(sc->sc_dev));
goto abort;
}
return ACT_CONTINUE;
}
if (sr->sr_dma_hand &&
(sc->sc_datalen >= sc->sc_min_dma_len))
{
NCR_TRACE("data_xfer: dma_start, dh=0x%x\n",
(long) sr->sr_dma_hand);
(*sc->sc_dma_start)(sc);
return ACT_WAIT_DMA;
}
NCR_TRACE("data_xfer: doing PIO, len=%d\n", sc->sc_datalen);
NCR5380_WRITE(sc, sci_tcmd, phase);
if (phase == PHASE_DATA_OUT) {
len = (*sc->sc_pio_out)(sc, phase, sc->sc_datalen,
sc->sc_dataptr);
} else {
len = (*sc->sc_pio_in)(sc, phase, sc->sc_datalen,
sc->sc_dataptr);
}
sc->sc_dataptr += len;
sc->sc_datalen -= len;
NCR_TRACE("data_xfer: did PIO, resid=%d\n", sc->sc_datalen);
return ACT_CONTINUE;
abort:
sc->sc_state |= NCR_ABORTING;
ncr_sched_msgout(sc, SEND_ABORT);
return ACT_CONTINUE;
}
static int
ncr5380_status(struct ncr5380_softc *sc)
{
int len;
uint8_t status;
struct sci_req *sr = sc->sc_current;
NCR5380_WRITE(sc, sci_tcmd, PHASE_STATUS);
len = ncr5380_pio_in(sc, PHASE_STATUS, 1, &status);
if (len) {
sr->sr_status = status;
} else {
printf("%s: none?\n", __func__);
}
return ACT_CONTINUE;
}
static void
ncr5380_machine(struct ncr5380_softc *sc)
{
struct sci_req *sr;
struct scsipi_xfer *xs;
int act_flags, phase, timo;
#ifdef DIAGNOSTIC
if (sc->sc_state == NCR_IDLE)
panic("%s: state=idle", __func__);
if (sc->sc_current == NULL)
panic("%s: no current cmd", __func__);
#endif
sr = sc->sc_current;
xs = sr->sr_xs;
act_flags = ACT_CONTINUE;
if (sc->sc_state & NCR_DOINGDMA) {
goto dma_done;
}
next_phase:
if (!SCI_BUSY(sc)) {
printf("%s: unexpected disconnect.\n", __func__);
xs->error = XS_DRIVER_STUFFUP;
act_flags |= (ACT_DISCONNECT | ACT_CMD_DONE);
goto do_actions;
}
timo = ncr5380_wait_phase_timo;
for (;;) {
if (NCR5380_READ(sc, sci_bus_csr) & SCI_BUS_REQ)
break;
if (--timo <= 0) {
if (sc->sc_state & NCR_ABORTING) {
printf("%s: no REQ while aborting, reset\n",
device_xname(sc->sc_dev));
act_flags |= ACT_RESET_BUS;
goto do_actions;
}
printf("%s: no REQ for next phase, abort\n",
device_xname(sc->sc_dev));
sc->sc_state |= NCR_ABORTING;
ncr_sched_msgout(sc, SEND_ABORT);
goto next_phase;
}
delay(100);
}
phase = SCI_BUS_PHASE(NCR5380_READ(sc, sci_bus_csr));
NCR_TRACE("machine: phase=%s\n",
(long) phase_names[phase & 7]);
#if 0
NCR5380_WRITE(sc, sci_tcmd, phase);
#endif
switch (phase) {
case PHASE_DATA_OUT:
case PHASE_DATA_IN:
act_flags = ncr5380_data_xfer(sc, phase);
break;
case PHASE_COMMAND:
act_flags = ncr5380_command(sc);
break;
case PHASE_STATUS:
act_flags = ncr5380_status(sc);
break;
case PHASE_MSG_OUT:
act_flags = ncr5380_msg_out(sc);
break;
case PHASE_MSG_IN:
act_flags = ncr5380_msg_in(sc);
break;
default:
printf("%s: Unexpected phase 0x%x\n", __func__, phase);
sc->sc_state |= NCR_ABORTING;
ncr_sched_msgout(sc, SEND_ABORT);
goto next_phase;
}
sc->sc_prevphase = phase;
do_actions:
if (act_flags & ACT_WAIT_DMA) {
act_flags &= ~ACT_WAIT_DMA;
if ((sr->sr_flags & SR_IMMED) == 0) {
NCR_TRACE("machine: wait for DMA intr.\n", 0);
return;
}
NCR_TRACE("machine: dma_poll, dh=0x%x\n",
(long) sr->sr_dma_hand);
(*sc->sc_dma_poll)(sc);
dma_done:
if (sr->sr_flags & SR_OVERDUE)
sc->sc_state |= NCR_ABORTING;
NCR_TRACE("machine: dma_stop, dh=0x%x\n",
(long) sr->sr_dma_hand);
(*sc->sc_dma_stop)(sc);
SCI_CLR_INTR(sc);
if (sc->sc_state & NCR_ABORTING) {
ncr_sched_msgout(sc, SEND_ABORT);
}
}
if (NCR5380_READ(sc, sci_csr) & SCI_CSR_PERR) {
printf("%s: parity error!\n", device_xname(sc->sc_dev));
ncr_sched_msgout(sc, SEND_PARITY_ERROR);
}
if (act_flags == ACT_CONTINUE)
goto next_phase;
NCR_TRACE("machine: act_flags=0x%x\n", act_flags);
if (act_flags & ACT_RESET_BUS) {
act_flags |= ACT_CMD_DONE;
sc->sc_state |= NCR_ABORTING;
printf("%s: reset SCSI bus for TID=%d LUN=%d\n",
device_xname(sc->sc_dev), sr->sr_target, sr->sr_lun);
ncr5380_reset_scsibus(sc);
}
if (act_flags & ACT_CMD_DONE) {
act_flags |= ACT_DISCONNECT;
if (sc->sc_datalen < 0) {
printf("%s: %d extra bytes from %d:%d\n",
device_xname(sc->sc_dev), -sc->sc_datalen,
sr->sr_target, sr->sr_lun);
sc->sc_datalen = 0;
}
xs->resid = sc->sc_datalen;
NCR_TRACE("machine: call done, cur=0x%x\n", (long)sr);
ncr5380_done(sc);
}
if (act_flags & ACT_DISCONNECT) {
timo = ncr5380_wait_req_timo;
for (;;) {
if (!SCI_BUSY(sc))
goto busfree;
if (--timo <= 0)
break;
delay(2);
}
printf("%s: Target %d LUN %d stuck busy, resetting...\n",
device_xname(sc->sc_dev), sr->sr_target, sr->sr_lun);
ncr5380_reset_scsibus(sc);
busfree:
NCR_TRACE("machine: discon, waited %d\n",
ncr5380_wait_req_timo - timo);
NCR5380_WRITE(sc, sci_icmd, 0);
NCR5380_WRITE(sc, sci_mode, 0);
NCR5380_WRITE(sc, sci_tcmd, PHASE_INVALID);
NCR5380_WRITE(sc, sci_sel_enb, 0);
SCI_CLR_INTR(sc);
NCR5380_WRITE(sc, sci_sel_enb, 0x80);
if ((act_flags & ACT_CMD_DONE) == 0) {
NCR_TRACE("machine: discon, cur=0x%x\n", (long)sr);
}
sc->sc_state = NCR_IDLE;
sc->sc_current = NULL;
sc->sc_dataptr = NULL;
sc->sc_datalen = 0;
sc->sc_prevphase = PHASE_INVALID;
sc->sc_msgpriq = 0;
sc->sc_msgoutq = 0;
sc->sc_msgout = 0;
}
}
#ifdef NCR5380_DEBUG
static void
ncr5380_show_scsi_cmd(struct scsipi_xfer *xs)
{
uint8_t *b = (uint8_t *)xs->cmd;
int i = 0;
scsipi_printaddr(xs->xs_periph);
if ((xs->xs_control & XS_CTL_RESET) == 0) {
while (i < xs->cmdlen) {
if (i)
printf(",");
printf("%x",b[i++]);
}
printf("\n");
} else {
printf("RESET\n");
}
}
int ncr5380_traceidx = 0;
#define TRACE_MAX 1024
struct trace_ent {
const char *msg;
long val;
} ncr5380_tracebuf[TRACE_MAX];
void
ncr5380_trace(const char *msg, long val)
{
struct trace_ent *tr;
int s;
s = splbio();
tr = &ncr5380_tracebuf[ncr5380_traceidx];
ncr5380_traceidx++;
if (ncr5380_traceidx >= TRACE_MAX)
ncr5380_traceidx = 0;
tr->msg = msg;
tr->val = val;
splx(s);
}
#ifdef DDB
void
ncr5380_clear_trace(void)
{
ncr5380_traceidx = 0;
memset((char *)ncr5380_tracebuf, 0, sizeof(ncr5380_tracebuf));
}
void
ncr5380_show_trace(void)
{
struct trace_ent *tr;
int idx;
idx = ncr5380_traceidx;
do {
tr = &ncr5380_tracebuf[idx];
idx++;
if (idx >= TRACE_MAX)
idx = 0;
if (tr->msg)
db_printf(tr->msg, tr->val);
} while (idx != ncr5380_traceidx);
}
void
ncr5380_show_req(struct sci_req *sr)
{
struct scsipi_xfer *xs = sr->sr_xs;
db_printf("TID=%d ", sr->sr_target);
db_printf("LUN=%d ", sr->sr_lun);
db_printf("dh=%p ", sr->sr_dma_hand);
db_printf("dptr=%p ", sr->sr_dataptr);
db_printf("dlen=0x%x ", sr->sr_datalen);
db_printf("flags=%d ", sr->sr_flags);
db_printf("stat=%d ", sr->sr_status);
if (xs == NULL) {
db_printf("(xs=NULL)\n");
return;
}
db_printf("\n");
#ifdef SCSIPI_DEBUG
show_scsipi_xs(xs);
#else
db_printf("xs=%p\n", xs);
#endif
}
void
ncr5380_show_state(void)
{
struct ncr5380_softc *sc;
struct sci_req *sr;
int i, j, k;
sc = ncr5380_debug_sc;
if (sc == NULL) {
db_printf("ncr5380_debug_sc == NULL\n");
return;
}
db_printf("sc_ncmds=%d\n", sc->sc_ncmds);
k = -1;
for (i = 0; i < SCI_OPENINGS; i++) {
sr = &sc->sc_ring[i];
if (sr->sr_xs) {
if (sr == sc->sc_current)
k = i;
db_printf("req %d: (sr=%p)", i, sr);
ncr5380_show_req(sr);
}
}
db_printf("sc_rr=%d, current=%d\n", sc->sc_rr, k);
db_printf("Active request matrix:\n");
for(i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
sr = sc->sc_matrix[i][j];
if (sr) {
db_printf("TID=%d LUN=%d sr=%p\n", i, j, sr);
}
}
}
db_printf("sc_state=0x%x\n", sc->sc_state);
db_printf("sc_current=%p\n", sc->sc_current);
db_printf("sc_dataptr=%p\n", sc->sc_dataptr);
db_printf("sc_datalen=0x%x\n", sc->sc_datalen);
db_printf("sc_prevphase=%d\n", sc->sc_prevphase);
db_printf("sc_msgpriq=0x%x\n", sc->sc_msgpriq);
}
#endif
#endif
void
ncr5380_attach(struct ncr5380_softc *sc)
{
struct scsipi_adapter *adapt = &sc->sc_adapter;
struct scsipi_channel *chan = &sc->sc_channel;
adapt->adapt_request = ncr5380_scsipi_request;
adapt->adapt_dev = sc->sc_dev;
adapt->adapt_nchannels = 1;
adapt->adapt_openings = SCI_OPENINGS;
adapt->adapt_max_periph = 1;
if (sc->sc_flags & NCR5380_FORCE_POLLING)
adapt->adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
chan->chan_adapter = adapt;
chan->chan_bustype = &scsi_bustype;
chan->chan_channel = 0;
chan->chan_ntargets = 8;
chan->chan_nluns = 8;
if (scsipi_adapter_addref(adapt) != 0) {
aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
return;
}
ncr5380_init(sc);
ncr5380_reset_scsibus(sc);
(void)config_found(sc->sc_dev, chan, scsiprint, CFARGS_NONE);
scsipi_adapter_delref(adapt);
}
int
ncr5380_detach(struct ncr5380_softc *sc, int flags)
{
return EOPNOTSUPP;
}