/*-
* Copyright (c) 2004, 2006, 2007 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by ITOH Yasufumi.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* DualEdge transfer support
*/
/* #define NJSC32_DUALEDGE */ /* XXX untested */
/*
* Auto param loading does not work properly (it partially works (works on
* start, doesn't on restart) on rev 0x54, it doesn't work at all on rev 0x51),
* and it doesn't improve the performance so much,
* forget about it.
*/
#undef NJSC32_AUTOPARAM
/*
* Check for termination power (32Bi and some versions of 32UDE).
*/
if (!nosleep || cold) {
DPRINTF(("%s: njsc32_init: checking TERMPWR\n",
device_xname(sc->sc_dev)));
/* First, turn termination power off */
njsc32_ireg_write_1(sc, NJSC32_IREG_TERM_PWR, 0);
/* give 0.5s to settle */
if (nosleep)
delay(500000);
else
tsleep(sc, PWAIT, "njs_t1", hz / 2);
}
/* supply termination power if not supplied by other devices */
if ((njsc32_ireg_read_1(sc, NJSC32_IREG_TERM_PWR) &
NJSC32_TERMPWR_SENSE) == 0) {
/* termination power is not present on the bus */
if (sc->sc_flags & NJSC32_CANNOT_SUPPLY_TERMPWR) {
/*
* CardBus device must not supply termination power
* to avoid excessive power consumption.
*/
printf("%s: no termination power present\n",
device_xname(sc->sc_dev));
} else {
/* supply termination power */
njsc32_ireg_write_1(sc, NJSC32_IREG_TERM_PWR,
NJSC32_TERMPWR_BPWR);
for (i = 0; i < NJSC32_NUM_CMD; i++) {
cmd = &sc->sc_cmds[i];
cmd->c_sc = sc;
cmd->c_sgt = sc->sc_cmdpg->dp_sg[i];
cmd->c_sgt_dma = dmaaddr +
offsetof(struct njsc32_dma_page, dp_sg[i]);
cmd->c_flags = 0;
error = bus_dmamap_create(sc->sc_dmat,
NJSC32_MAX_XFER, /* max total map size */
NJSC32_NUM_SG, /* max number of segments */
NJSC32_SGT_MAXSEGLEN, /* max size of a segment */
0, /* boundary */
BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &cmd->c_dmamap_xfer);
if (error) {
aprint_error_dev(sc->sc_dev,
"only %d cmd descs available (error = %d)\n",
i, error);
break;
}
TAILQ_INSERT_TAIL(&sc->sc_freecmd, cmd, c_q);
}
#if 1 /* test */
/*
* try to distinguish 32Bi and 32UDE
*/
/* try to set DualEdge bit (exists on 32UDE only) and read it back */
njsc32_write_2(sc, NJSC32_REG_TRANSFER, NJSC32_XFR_DUALEDGE_ENABLE);
if ((reg = njsc32_read_2(sc, NJSC32_REG_TRANSFER)) == 0xffff) {
/* device was removed? */
aprint_error_dev(sc->sc_dev, "attach failed\n");
return;
} else if (reg & NJSC32_XFR_DUALEDGE_ENABLE) {
detected_model = NJSC32_MODEL_32UDE | NJSC32_FLAG_DUALEDGE;
} else {
detected_model = NJSC32_MODEL_32BI;
}
njsc32_write_2(sc, NJSC32_REG_TRANSFER, 0); /* restore */
#if 1/*def DIAGNOSTIC*/
/* compare what is configured with what is detected */
if ((sc->sc_model & NJSC32_MODEL_MASK) !=
(detected_model & NJSC32_MODEL_MASK)) {
/*
* Please report this error if it happens.
*/
aprint_error_dev(sc->sc_dev, "model mismatch: %#x vs %#x\n",
sc->sc_model, detected_model);
return;
}
#endif
#endif
/* check model */
switch (sc->sc_model & NJSC32_MODEL_MASK) {
case NJSC32_MODEL_32BI:
str = "Bi";
/* 32Bi doesn't support DualEdge transfer */
KASSERT((sc->sc_model & NJSC32_FLAG_DUALEDGE) == 0);
break;
case NJSC32_MODEL_32UDE:
str = "UDE";
break;
default:
aprint_error_dev(sc->sc_dev, "unknown model!\n");
return;
}
aprint_normal_dev(sc->sc_dev, "NJSC-32%s", str);
val = 0;
p = sc->sc_msgout;
switch (sc->sc_msgoutlen) {
/* 31-24 23-16 15-8 7 ... 1 0 */
case 3: /* MSG3 MSG2 MSG1 V --- cnt */
val |= *p++ << NJSC32_MSGOUT_MSG1_SHIFT;
/* FALLTHROUGH */
case 2: /* MSG2 MSG1 --- V --- cnt */
val |= *p++ << NJSC32_MSGOUT_MSG2_SHIFT;
/* FALLTHROUGH */
case 1: /* MSG1 --- --- V --- cnt */
val |= *p++ << NJSC32_MSGOUT_MSG3_SHIFT;
val |= NJSC32_MSGOUT_VALID | sc->sc_msgoutlen;
break;
default:
break;
}
return val;
}
#ifdef NJSC32_DUALEDGE
/* add Wide Data Transfer Request to the next Message Out */
static void
njsc32_msgout_wdtr(struct njsc32_softc *sc, int width)
{
/* initial negotiation state */
if (target->t_state == NJSC32_TARST_INIT) {
#ifdef NJSC32_DUALEDGE
if (target->t_flags & NJSC32_TARF_DE)
target->t_state = NJSC32_TARST_DE;
else
#endif
if (target->t_flags & NJSC32_TARF_SYNC)
target->t_state = NJSC32_TARST_SDTR;
else
target->t_state = NJSC32_TARST_DONE;
}
switch (target->t_state) {
default:
case NJSC32_TARST_INIT:
#ifdef DIAGNOSTIC
panic("njsc32_negotiate_xfer");
/* NOTREACHED */
#endif
/* FALLTHROUGH */
case NJSC32_TARST_DONE:
/* no more work */
break;
#ifdef NJSC32_DUALEDGE
case NJSC32_TARST_DE:
njsc32_msgout_wdtr(sc, 0xde /* XXX? */);
break;
case NJSC32_TARST_WDTR:
njsc32_msgout_wdtr(sc, MSG_EXT_WDTR_BUS_8_BIT);
break;
#endif
case NJSC32_TARST_SDTR:
njsc32_msgout_sdtr(sc, sc->sc_synct[sc->sc_sync_max].sp_period,
NJSC32_SYNCOFFSET_MAX);
break;
case NJSC32_TARST_ASYNC:
njsc32_msgout_sdtr(sc, NJSC32_SYNCPERIOD_ASYNC,
NJSC32_SYNCOFFSET_ASYNC);
break;
}
}
/* turn LED on */
static inline void
njsc32_led_on(struct njsc32_softc *sc)
{
/* Note: must be called at splbio() */
static void
njsc32_start(struct njsc32_softc *sc)
{
struct njsc32_cmd *cmd;
/* get a command to issue */
TAILQ_FOREACH(cmd, &sc->sc_reqcmd, c_q) {
if (cmd->c_lu->lu_cmd == NULL &&
((cmd->c_flags & NJSC32_CMD_TAGGED) ||
TAILQ_EMPTY(&cmd->c_lu->lu_q)))
break; /* OK, the logical unit is free */
}
if (!cmd)
goto out; /* no work to do */
/* request will always fail if not in bus free phase */
if (njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR) !=
NJSC32_BUSMON_BUSFREE)
goto busy;
control = xs->xs_control;
lun = periph->periph_lun;
/*
* get a free cmd
* (scsipi layer knows the number of cmds, so this shall never fail)
*/
s = splbio();
cmd = TAILQ_FIRST(&sc->sc_freecmd);
KASSERT(cmd);
TAILQ_REMOVE(&sc->sc_freecmd, cmd, c_q);
splx(s);
switch (error) {
case 0:
break;
case ENOMEM:
case EAGAIN:
xs->error = XS_RESOURCE_SHORTAGE;
goto map_failed;
default:
xs->error = XS_DRIVER_STUFFUP;
map_failed:
printf("%s: njsc32_run_xfer: map failed, error %d\n",
device_xname(sc->sc_dev), error);
/* put it back to free command list */
s = splbio();
TAILQ_INSERT_HEAD(&sc->sc_freecmd, cmd, c_q);
splx(s);
/* abort this transfer */
scsipi_done(xs);
return;
}
for (i = 0; i < cmd->c_dmamap_xfer->dm_nsegs; i++) {
cmd->c_sgt[i].sg_addr =
htole32(cmd->c_dmamap_xfer->dm_segs[i].ds_addr);
cmd->c_sgt[i].sg_len =
htole32(cmd->c_dmamap_xfer->dm_segs[i].ds_len);
}
/* end mark */
cmd->c_sgt[i - 1].sg_len |= htole32(NJSC32_SGT_ENDMARK);
if (bp->b_bcount > NJSC32_MAX_XFER)
bp->b_bcount = NJSC32_MAX_XFER;
minphys(bp);
}
/*
* On some versions of 32UDE (probably the earlier ones), the controller
* detects continuous bus reset when the termination power is absent.
* Make sure the system won't hang on such situation.
*/
static void
njsc32_wait_reset_release(void *arg)
{
struct njsc32_softc *sc = arg;
struct njsc32_cmd *cmd;
/*
* set current data pointer
*/
static inline void
njsc32_set_cur_ptr(struct njsc32_cmd *cmd, u_int32_t pos)
{
/* new current data pointer */
cmd->c_dp_cur = pos;
/* update number of bytes transferred */
if (pos > cmd->c_dp_max)
cmd->c_dp_max = pos;
}
/*
* set data pointer for the next transfer
*/
static void
njsc32_set_ptr(struct njsc32_softc *sc, struct njsc32_cmd *cmd, u_int32_t pos)
{
struct njsc32_sgtable *sg;
unsigned sgte;
u_int32_t len;
/* set current pointer */
njsc32_set_cur_ptr(cmd, pos);
/* undo previous fix if any */
if (cmd->c_sgfixcnt != 0) {
sg = &cmd->c_sgt[cmd->c_sgoffset];
sg->sg_addr = htole32(le32toh(sg->sg_addr) - cmd->c_sgfixcnt);
sg->sg_len = htole32(le32toh(sg->sg_len) + cmd->c_sgfixcnt);
cmd->c_sgfixcnt = 0;
}
if (pos >= cmd->c_datacnt) {
/* transfer done */
#if 1 /*def DIAGNOSTIC*/
if (pos > cmd->c_datacnt)
printf("%s: pos %u too large\n",
device_xname(sc->sc_dev), pos - cmd->c_datacnt);
#endif
cmd->c_xferctl = 0; /* XXX correct? */
static enum njsc32_reselstat
njsc32_resel_identify(struct njsc32_softc *sc, int lun,
struct njsc32_cmd **pcmd)
{
int targetid;
struct njsc32_lu *plu;
struct njsc32_cmd *cmd;
switch (sc->sc_stat) {
case NJSC32_STAT_RESEL:
break; /* OK */
case NJSC32_STAT_RESEL_LUN:
case NJSC32_STAT_RECONNECT:
/*
* accept and ignore if the LUN is the same as the current one,
* reject otherwise.
*/
return sc->sc_resellun == lun ?
NJSC32_RESEL_THROUGH : NJSC32_RESEL_ERROR;
default:
printf("%s: njsc32_resel_identify: not in reselection\n",
device_xname(sc->sc_dev));
return NJSC32_RESEL_ERROR;
}
TPRINTF(("%s: njsc32_resel_tag: reselection tag %d\n",
device_xname(sc->sc_dev), tag));
if (sc->sc_stat != NJSC32_STAT_RESEL_LUN)
return NJSC32_RESEL_ERROR;
head = &sc->sc_targets[sc->sc_reselid].t_lus[sc->sc_resellun].lu_q;
/* XXX slow? */
/* search for the command of the tag */
TAILQ_FOREACH(cmd, head, c_q) {
if (cmd->c_xs->xs_tag_id == tag) {
sc->sc_stat = NJSC32_STAT_RECONNECT;
TAILQ_REMOVE(head, cmd, c_q);
*pcmd = cmd;
TPRINTC(cmd, ("njsc32_resel_tag: I_T_L_Q nexus\n"));
njsc32_reconnect(sc, cmd);
return NJSC32_RESEL_COMPLETE;
}
}
/* no disconnected commands */
return NJSC32_RESEL_ERROR;
}
/*
* Reload parameters and restart AutoSCSI.
*
* XXX autoparam doesn't work as expected and we can't use it here.
*/
static void
njsc32_cmd_reload(struct njsc32_softc *sc, struct njsc32_cmd *cmd, int cctl)
{
struct njsc32_target *target;
#define WAITNEXTMSG do { sc->sc_msgincnt++; goto restart; } while (0)
#define MSGCOMPLETE do { sc->sc_msgincnt = 0; goto restart; } while (0)
if (MSG_ISIDENTIFY(msg0)) {
/*
* Got Identify message from target.
*/
if ((msg0 & ~MSG_IDENTIFY_LUNMASK) != MSG_IDENTIFYFLAG ||
(rstat = njsc32_resel_identify(sc, msg0 &
MSG_IDENTIFY_LUNMASK, &cmd)) == NJSC32_RESEL_ERROR) {
/*
* invalid Identify -> Reject
*/
goto reject;
}
if (rstat == NJSC32_RESEL_COMPLETE)
reload_params = TRUE;
MSGCOMPLETE;
}
if (msg0 == MSG_SIMPLE_Q_TAG) {
if (msgcnt == 0)
WAITNEXTMSG;
/* got whole message */
sc->sc_msgincnt = 0;
if ((rstat = njsc32_resel_tag(sc, sc->sc_msginbuf[1], &cmd))
== NJSC32_RESEL_ERROR) {
/*
* invalid Simple Queue Tag -> Abort Tag
*/
printf("%s: msgin: invalid tag\n",
device_xname(sc->sc_dev));
njsc32_add_msgout(sc, MSG_ABORT_TAG);
goto reply;
}
if (rstat == NJSC32_RESEL_COMPLETE)
reload_params = TRUE;
MSGCOMPLETE;
}
/* I_T_L or I_T_L_Q nexus should be established now */
if (cmd == NULL) {
printf("%s: msgin %#x without nexus -- sending abort\n",
device_xname(sc->sc_dev), msg0);
njsc32_add_msgout(sc, MSG_ABORT);
goto reply;
}
case MSG_EXT_WDTR: /* Wide Data Transfer Request */
DPRINTC(cmd,
("WDTR %#x\n", sc->sc_msginbuf[EXTCODEOFF + 1]));
#ifdef NJSC32_DUALEDGE
if (msgcnt != MSG_EXT_WDTR_LEN + EXTCODEOFF-1)
break; /* reject */
/*
* T->I of this message is not used for
* DualEdge negotiation, so the device
* must not be a DualEdge device.
*
* XXX correct?
*/
target = cmd->c_target;
target->t_xferctl = 0;
switch (target->t_state) {
case NJSC32_TARST_DE:
if (sc->sc_msginbuf[EXTCODEOFF + 1] !=
MSG_EXT_WDTR_BUS_8_BIT) {
/*
* Oops, we got unexpected WDTR.
* Negotiate for 8bit.
*/
target->t_state = NJSC32_TARST_WDTR;
} else {
target->t_state = NJSC32_TARST_SDTR;
}
njsc32_negotiate_xfer(sc, target);
goto reply;
case NJSC32_TARST_WDTR:
if (sc->sc_msginbuf[EXTCODEOFF + 1] !=
MSG_EXT_WDTR_BUS_8_BIT) {
printf("%s: unexpected transfer width:"
" %#x\n", device_xname(sc->sc_dev),
sc->sc_msginbuf[EXTCODEOFF + 1]);
/* XXX what to do? */
}
target->t_state = NJSC32_TARST_SDTR;
njsc32_negotiate_xfer(sc, target);
goto reply;
default:
/* the target started WDTR exchange */
DPRINTC(cmd, ("WDTR from target\n"));
case MSG_NOOP: /* 0x08 */
#ifdef NJSC32_DUALEDGE
target = cmd->c_target;
if (target->t_state == NJSC32_TARST_DE) {
printf("%s: DualEdge transfer\n",
device_xname(sc->sc_dev));
target->t_xferctl = NJSC32_XFR_DUALEDGE_ENABLE;
/* go to next negotiation */
target->t_state = NJSC32_TARST_SDTR;
njsc32_negotiate_xfer(sc, target);
goto reply;
}
#endif
goto restart;
case MSG_INITIATOR_DET_ERR: /* 0x05 I->T only */
case MSG_ABORT: /* 0x06 I->T only */
case MSG_PARITY_ERROR: /* 0x09 I->T only */
case MSG_LINK_CMD_COMPLETE: /* 0x0a */
case MSG_LINK_CMD_COMPLETEF: /* 0x0b */
case MSG_BUS_DEV_RESET: /* 0x0c I->T only */
case MSG_ABORT_TAG: /* 0x0d I->T only */
case MSG_CLEAR_QUEUE: /* 0x0e I->T only */
#if 0 /* handled above */
case MSG_SIMPLE_Q_TAG: /* 0x20 */
#endif
case MSG_HEAD_OF_Q_TAG: /* 0x21 I->T only */
case MSG_ORDERED_Q_TAG: /* 0x22 I->T only */
case MSG_IGN_WIDE_RESIDUE: /* 0x23 */
/*
* Be careful the second and latter bytes of Message In
* shall not be absorbed by AutoSCSI.
*/
if (sc->sc_msgincnt == 0)
cctl |= NJSC32_CMD_AUTO_MSGIN_00_04 | NJSC32_CMD_AUTO_MSGIN_02;
if (sc->sc_msgoutlen != 0)
cctl |= NJSC32_CMD_AUTO_ATN;
/* make sure target is in Message Out phase */
bus = njsc32_read_1(sc, NJSC32_REG_SCSI_BUS_MONITOR);
if ((bus & NJSC32_BUSMON_PHASE_MASK) != NJSC32_PHASE_MESSAGE_OUT) {
/*
* Message Out is aborted by target.
*/
printf("%s: njsc32_msgout: phase change %#x\n",
device_xname(sc->sc_dev), bus);
} else {
/*
* target has come to Bus Free phase
* probably to notify an error
*/
PRINTC(cmd, ("njsc32_intr: unexpected bus free\n"));
/* try Request Sense */
xs->status = SCSI_CHECK;
njsc32_end_cmd(sc, cmd, XS_BUSY);
}
}
int
njsc32_intr(void *arg)
{
struct njsc32_softc *sc = arg;
u_int16_t intr;
u_int8_t arbstat, bus_phase;
int auto_phase;
int idbit;
struct njsc32_cmd *cmd;
intr = njsc32_read_2(sc, NJSC32_REG_IRQ);
if ((intr & NJSC32_IRQ_INTR_PENDING) == 0)
return 0; /* not mine */
#if 0 /* I don't think this is required */
/* mask interrupts */
njsc32_write_2(sc, NJSC32_REG_IRQ, NJSC32_IRQ_MASK_ALL);
#endif
/* we got an interrupt, so stop the timer */
njsc32_write_2(sc, NJSC32_REG_TIMER, NJSC32_TIMER_STOP);
if (intr & NJSC32_IRQ_SCSIRESET) {
printf("%s: detected bus reset\n", device_xname(sc->sc_dev));
/* make sure all devices on the bus are certainly reset */
njsc32_reset_bus(sc);
goto out;
}
if (auto_phase & NJSC32_XPHASE_BUS_FREE) {
/*
* XXX command has already finished
* -- what can we do?
*
* It is not clear current command
* caused the error -- reset everything.
*/
njsc32_init(sc, 1); /* XXX */
} else {
/* XXX does this case occur? */
#if 1
printf("%s: datain: parity error\n",
device_xname(sc->sc_dev));
#endif
/*
* Make attention condition and try
* to send Initiator Detected Error
* message.
*/
njsc32_init_msgout(sc);
njsc32_add_msgout(sc,
MSG_INITIATOR_DET_ERR);
njsc32_write_4(sc,
NJSC32_REG_SCSI_MSG_OUT,
njsc32_get_auto_msgout(sc));
/* restart autoscsi with ATN */
njsc32_write_2(sc,
NJSC32_REG_COMMAND_CONTROL,
NJSC32_CMD_CLEAR_CDB_FIFO_PTR |
NJSC32_CMD_AUTO_COMMAND_PHASE |
NJSC32_CMD_AUTO_SCSI_RESTART |
NJSC32_CMD_AUTO_MSGIN_00_04 |
NJSC32_CMD_AUTO_MSGIN_02 |
NJSC32_CMD_AUTO_ATN);
}
goto out;
}
/*
* data has been transferred, and current pointer
* is changed
*/
sackcnt = njsc32_read_4(sc, NJSC32_REG_SACK_CNT);
/*
* The controller returns extra ACK count
* if the DMA buffer is not 4byte aligned.
*/
cntoffset = le32toh(cmd->c_sgt[0].sg_addr) & 3;
#ifdef NJSC32_DEBUG
if (cntoffset != 0) {
printf("sackcnt %u, cntoffset %u\n",
sackcnt, cntoffset);
}
#endif
/* advance SCSI pointer */
njsc32_set_cur_ptr(cmd,
cmd->c_dp_cur + sackcnt - cntoffset);
}
if (auto_phase & NJSC32_XPHASE_MSGOUT) {
/* Message Out phase has been automatically processed */
TPRINTC(cmd, ("njsc32_intr: Message Out\n"));
if ((auto_phase & NJSC32_XPHASE_PAUSED_MSG_IN) == 0 &&
sc->sc_msgoutlen <= NJSC32_MSGOUT_MAX_AUTO) {
njsc32_init_msgout(sc);
}
}
if (auto_phase & NJSC32_XPHASE_STATUS) {
/* Status phase has been automatically processed */
cmd->c_xs->status =
njsc32_read_1(sc, NJSC32_REG_SCSI_CSB_IN);
TPRINTC(cmd, ("njsc32_intr: Status %#x\n",
cmd->c_xs->status));
}
if (auto_phase & NJSC32_XPHASE_BUS_FREE) {
/* AutoSCSI is finished */