/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Van Jacobson of Lawrence Berkeley Laboratory.
*
* 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)scsi.c 7.5 (Berkeley) 5/4/91
*/
/*
* Changes Copyright (c) 2001 Wayne Knowles
* Changes Copyright (c) 1996 Steve Woodford
* Original Copyright (c) 1994 Christian E. Hopps
*
* This code is derived from software contributed to Berkeley by
* Van Jacobson of Lawrence Berkeley Laboratory.
*
* 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.
* 3. 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, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)scsi.c 7.5 (Berkeley) 5/4/91
*/
/*
* This version of the driver is pretty well generic, so should work with
* any flavour of WD33C93 chip.
*/
/*
* SCSI delays
* In u-seconds, primarily for state changes on the SPC.
*/
#define SBIC_CMD_WAIT 50000 /* wait per step of 'immediate' cmds */
#define SBIC_DATA_WAIT 50000 /* wait per data in/out step */
#define SBIC_INIT_WAIT 50000 /* wait per step (both) during init */
#define STATUS_UNKNOWN 0xff /* uninitialized status */
/*
* Convenience macro for waiting for a particular wd33c93 event
*/
#define SBIC_WAIT(regs, until, timeo) wd33c93_wait(regs, until, timeo, __LINE__)
/*
* Add reference to adapter so that we drop the reference after
* config_found() to make sure the adapter is disabled.
*/
if (scsipi_adapter_addref(&sc->sc_adapter) != 0) {
aprint_error_dev(sc->sc_dev, "unable to enable controller\n");
return;
}
if (!wd33c93_pool_initialized) {
/* All instances share the same pool */
pool_init(&wd33c93_pool, sizeof(struct wd33c93_acb), 0, 0, 0,
"wd33c93_acb", NULL, IPL_BIO);
++wd33c93_pool_initialized;
}
if (sc->sc_state == 0) {
TAILQ_INIT(&sc->ready_list);
/*
* Choose a suitable clock divisor and work out the resulting
* sync transfer periods in 4ns units.
*/
if (sc->sc_clkfreq < 110) {
my_id |= SBIC_ID_FS_8_10;
div = 2;
} else if (sc->sc_clkfreq < 160) {
my_id |= SBIC_ID_FS_12_15;
div = 3;
} else if (sc->sc_clkfreq < 210) {
my_id |= SBIC_ID_FS_16_20;
div = 4;
} else
panic("wd33c93: invalid clock speed %d", sc->sc_clkfreq);
for (i = 0; i < 7; i++)
sc->sc_syncperiods[i] =
(i + 2) * div * 1250 / sc->sc_clkfreq;
sc->sc_minsyncperiod = sc->sc_syncperiods[0];
SBIC_DEBUG(SYNC, ("available sync periods: %d %d %d %d %d %d %d\n",
sc->sc_syncperiods[0], sc->sc_syncperiods[1],
sc->sc_syncperiods[2], sc->sc_syncperiods[3],
sc->sc_syncperiods[4], sc->sc_syncperiods[5],
sc->sc_syncperiods[6]));
if (sc->sc_clkfreq >= 160 && sc->sc_chip == SBIC_CHIP_WD33C93B) {
for (i = 0; i < 3; i++)
sc->sc_fsyncperiods[i] =
(i + 2) * 2 * 1250 / sc->sc_clkfreq;
SBIC_DEBUG(SYNC, ("available fast sync periods: %d %d %d\n",
sc->sc_fsyncperiods[0], sc->sc_fsyncperiods[1],
sc->sc_fsyncperiods[2]));
sc->sc_minsyncperiod = sc->sc_fsyncperiods[0];
}
/* Max Sync Offset */
if (sc->sc_chip == SBIC_CHIP_WD33C93A ||
sc->sc_chip == SBIC_CHIP_WD33C93B)
sc->sc_maxoffset = SBIC_SYN_93AB_MAX_OFFSET;
else
sc->sc_maxoffset = SBIC_SYN_93_MAX_OFFSET;
/*
* don't allow Selection (SBIC_RID_ES)
* until we can handle target mode!!
*/
SET_SBIC_rselid(sc, SBIC_RID_ER);
/*
* Determine an appropriate value for the synchronous transfer register
* given the period and offset values in *ti.
*/
u_char
wd33c93_stp2syn(struct wd33c93_softc *sc, struct wd33c93_tinfo *ti)
{
unsigned i;
/* see if we can handle fast scsi (100-200ns) first */
if (ti->period < 50 && sc->sc_minsyncperiod < 50) {
for (i = 0; i < 3; i++)
if (sc->sc_fsyncperiods[i] >= ti->period)
return (SBIC_SYN(ti->offset, i + 2, 1));
}
for (i = 0; i < 7; i++) {
if (sc->sc_syncperiods[i] >= ti->period) {
if (i == 6)
return (SBIC_SYN(0, 0, 0));
else
return (SBIC_SYN(ti->offset, i + 2, 0));
}
}
/*
* Check if current operation can be done using DMA
*
* returns 1 if DMA OK, 0 for polled I/O transfer
*/
int
wd33c93_dmaok(struct wd33c93_softc *sc, struct scsipi_xfer *xs)
{
if (wd33c93_nodma || sc->sc_dmamode == SBIC_CTL_NO_DMA ||
(xs->xs_control & XS_CTL_POLL) || xs->datalen == 0)
return (0);
return(1);
}
/*
* Setup for DMA transfer
*/
void
wd33c93_dma_setup(struct wd33c93_softc *sc, int datain)
{
struct wd33c93_acb *acb = sc->sc_nexus;
int s;
if (flags & XS_CTL_POLL) {
/*
* Complete currently active command(s) before
* issuing an immediate command
*/
while (sc->sc_nexus)
wd33c93_poll(sc, sc->sc_nexus);
}
s = splbio();
TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
acb->flags |= ACB_READY;
/* If nothing is active, try to start it now. */
if (sc->sc_state == SBIC_IDLE)
wd33c93_sched(sc);
splx(s);
if ((flags & XS_CTL_POLL) == 0)
return;
if (wd33c93_poll(sc, acb)) {
wd33c93_timeout(acb);
if (wd33c93_poll(sc, acb)) /* 2nd retry for ABORT */
wd33c93_timeout(acb);
}
return;
case ADAPTER_REQ_GROW_RESOURCES:
/* XXX Not supported. */
return;
if ((xm->xm_mode & PERIPH_CAP_SYNC) != 0 &&
(ti->flags & T_NOSYNC) == 0)
ti->flags |= T_WANTSYNC;
/*
* If we're not going to negotiate, send the notification
* now, since it won't happen later.
*/
if (!(ti->flags & T_WANTSYNC) == !(ti->flags & T_SYNCMODE))
wd33c93_update_xfer_mode(sc, xm->xm_target);
else
ti->flags |= T_NEGOTIATE;
return;
}
}
}
/*
* attempt to start the next available command
*/
void
wd33c93_sched(struct wd33c93_softc *sc)
{
struct scsipi_periph *periph = NULL; /* Gag the compiler */
struct wd33c93_acb *acb;
struct wd33c93_tinfo *ti;
struct wd33c93_linfo *li;
int lun, tag, flags;
if (sc->sc_state != SBIC_IDLE)
return;
KASSERT(sc->sc_nexus == NULL);
/* Loop through the ready list looking for work to do... */
TAILQ_FOREACH(acb, &sc->ready_list, chain) {
periph = acb->xs->xs_periph;
lun = periph->periph_lun;
ti = &sc->sc_tinfo[periph->periph_target];
li = TINFO_LUN(ti, lun);
KASSERT(acb->flags & ACB_READY);
/* Select type of tag for this command */
if ((ti->flags & T_NODISC) != 0)
tag = 0;
else if ((ti->flags & T_TAG) == 0)
tag = 0;
else if ((acb->flags & ACB_SENSE) != 0)
tag = 0;
else if (acb->xs->xs_control & XS_CTL_POLL)
tag = 0; /* No tags for polled commands */
else
tag = acb->xs->xs_tag_type;
if (li == NULL) {
/* Initialize LUN info and add to list. */
li = malloc(sizeof(*li), M_DEVBUF, M_NOWAIT);
if (li == NULL)
continue;
memset(li, 0, sizeof(*li));
li->lun = lun;
if (lun < SBIC_NLUN)
ti->lun[lun] = li;
}
li->last_used = time_second;
/*
* We've found a potential command, but is the target/lun busy?
*/
xs->status = status & SCSI_STATUS_MASK;
xs->resid = acb->dleft;
if (xs->error == XS_NOERROR) {
switch (xs->status) {
case SCSI_CHECK:
case SCSI_TERMINATED:
/* XXX Need to read sense - return busy for now */
/*FALLTHROUGH*/
case SCSI_QUEUE_FULL:
case SCSI_BUSY:
xs->error = XS_BUSY;
break;
}
}
ti = &sc->sc_tinfo[sc->target];
li = TINFO_LUN(ti, sc->lun);
ti->cmds++;
if (xs->error == XS_SELTIMEOUT) {
/* Selection timeout -- discard this LUN if empty */
if (li->untagged == NULL && li->used == 0) {
if (sc->lun < SBIC_NLUN)
ti->lun[sc->lun] = NULL;
free(li, M_DEVBUF);
}
}
/*
* Clean up chip itself
*/
if (sc->sc_nexus == acb) {
/* Reschedule timeout. */
callout_reset(&acb->xs->xs_callout, mstohz(acb->timeout),
wd33c93_timeout, acb);
while (asr & SBIC_ASR_DBR) {
/*
* wd33c93 is jammed w/data. need to clear it
* But we don't know what direction it needs to go
*/
GET_SBIC_data(sc, asr);
printf("abort %s: clearing data buffer 0x%02x\n",
where, asr);
GET_SBIC_asr(sc, asr);
if (asr & SBIC_ASR_DBR) /* Not the read direction */
SET_SBIC_data(sc, asr);
GET_SBIC_asr(sc, asr);
}
/*
* select the bus, return when selected or error.
*
* Returns the current CSR following selection and optionally MSG out phase.
* i.e. the returned CSR *should* indicate CMD phase...
* If the return value is 0, some error happened.
*/
u_char
wd33c93_selectbus(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
{
struct scsipi_xfer *xs = acb->xs;
struct wd33c93_tinfo *ti;
u_char target, lun, asr, csr, id;
KASSERT(sc->sc_state == SBIC_IDLE);
target = xs->xs_periph->periph_target;
lun = xs->xs_periph->periph_lun;
ti = &sc->sc_tinfo[target];
GET_SBIC_asr(sc, asr);
if (asr & (SBIC_ASR_INT|SBIC_ASR_BSY)) {
/* This means we got ourselves reselected upon */
SBIC_DEBUG(PHASE, ("WD busy (reselect?) ASR=%02x\n", asr));
return 0;
}
SET_SBIC_cmd(sc, SBIC_CMD_SEL_ATN);
WAIT_CIP(sc);
/*
* wait for select (merged from separate function may need
* cleanup)
*/
do {
asr = SBIC_WAIT(sc, SBIC_ASR_INT | SBIC_ASR_LCI, 0);
if (asr & SBIC_ASR_LCI) {
QPRINTF(("late LCI: asr %02x\n", asr));
return 0;
}
/* Clear interrupt */
GET_SBIC_csr (sc, csr);
/* Reselected from under our feet? */
if (csr == SBIC_CSR_RSLT_NI || csr == SBIC_CSR_RSLT_IFY) {
SBIC_DEBUG(PHASE, ("got reselected, asr %02x\n", asr));
/*
* We need to handle this now so we don't lock up later
*/
wd33c93_nextstate(sc, acb, csr, asr);
return 0;
}
/* setup correct sync mode for this target */
wd33c93_setsync(sc, ti);
if (ti->flags & T_NODISC && sc->sc_disc == 0)
SET_SBIC_rselid (sc, 0); /* Not expecting a reselect */
else
SET_SBIC_rselid (sc, SBIC_RID_ER);
/*
* We only really need to do anything when the target goes to MSG out
* If the device ignored ATN, it's probably old and brain-dead,
* but we'll try to support it anyhow.
* If it doesn't support message out, it definitely doesn't
* support synchronous transfers, so no point in even asking...
*/
if (csr == (SBIC_CSR_MIS_2 | MESG_OUT_PHASE)) {
if (ti->flags & T_NEGOTIATE) {
/* Initiate a SDTR message */
SBIC_DEBUG(SYNC, ("Sending SDTR to target %d\n", id));
if (ti->flags & T_WANTSYNC) {
ti->period = sc->sc_minsyncperiod;
ti->offset = sc->sc_maxoffset;
} else {
ti->period = 0;
ti->offset = 0;
}
/* Send Sync negotiation message */
sc->sc_omsg[0] = MSG_IDENTIFY(lun, 0); /* No Disc */
sc->sc_omsg[1] = MSG_EXTENDED;
sc->sc_omsg[2] = MSG_EXT_SDTR_LEN;
sc->sc_omsg[3] = MSG_EXT_SDTR;
if (ti->flags & T_WANTSYNC) {
sc->sc_omsg[4] = sc->sc_minsyncperiod;
sc->sc_omsg[5] = sc->sc_maxoffset;
} else {
sc->sc_omsg[4] = 0;
sc->sc_omsg[5] = 0;
}
wd33c93_xfout(sc, 6, sc->sc_omsg);
sc->sc_msgout |= SEND_SDTR; /* may be rejected */
sc->sc_flags |= SBICF_SYNCNEGO;
} else {
if (sc->sc_nexus->tag_type != 0) {
/* Use TAGS */
SBIC_DEBUG(TAGS, ("<select %d:%d TAG=%x>\n",
sc->target, sc->lun,
sc->sc_nexus->tag_id));
sc->sc_omsg[0] = MSG_IDENTIFY(lun, 1);
sc->sc_omsg[1] = sc->sc_nexus->tag_type;
sc->sc_omsg[2] = sc->sc_nexus->tag_id;
wd33c93_xfout(sc, 3, sc->sc_omsg);
sc->sc_msgout |= SEND_TAG;
} else {
int no_disc;
/* Setup LUN nexus and disconnect privilege */
no_disc = xs->xs_control & XS_CTL_POLL ||
ti->flags & T_NODISC;
SEND_BYTE(sc, MSG_IDENTIFY(lun, !no_disc));
}
}
/*
* There's one interrupt still to come:
* the change to CMD phase...
*/
SBIC_WAIT(sc, SBIC_ASR_INT , 0);
GET_SBIC_csr(sc, csr);
}
return csr;
}
/*
* Information Transfer *to* a SCSI Target.
*
* Note: Don't expect there to be an interrupt immediately after all
* the data is transferred out. The WD spec sheet says that the Transfer-
* Info command for non-MSG_IN phases only completes when the target
* next asserts 'REQ'. That is, when the SCSI bus changes to a new state.
*
* This can have a nasty effect on commands which take a relatively long
* time to complete, for example a START/STOP unit command may remain in
* CMD phase until the disk has spun up. Only then will the target change
* to STATUS phase. This is really only a problem for immediate commands
* since we don't allow disconnection for them (yet).
*/
int
wd33c93_xfout(struct wd33c93_softc *sc, int len, void *bp)
{
int wait = wd33c93_data_wait;
u_char asr, *buf = bp;
/*
* sigh.. WD-PROTO strikes again.. sending the command in one go
* causes the chip to lock up if talking to certain (misbehaving?)
* targets. Anyway, this procedure should work for all targets, but
* it's slightly slower due to the overhead
*/
/*
* this leaves with one csr to be read
*/
return len;
}
/*
* Finish SCSI xfer command: After the completion interrupt from
* a read/write operation, sequence through the final phases in
* programmed i/o.
*/
void
wd33c93_xferdone(struct wd33c93_softc *sc)
{
u_char phase, csr;
int s;
QPRINTF(("{"));
s = splbio();
/*
* have the wd33c93 complete on its own
*/
SBIC_TC_PUT(sc, 0);
SET_SBIC_cmd_phase(sc, 0x46);
SET_SBIC_cmd(sc, SBIC_CMD_SEL_ATN_XFER);
/* select the SCSI bus (it's an error if bus isn't free) */
if ((csr = wd33c93_selectbus(sc, acb)) == 0)
return(0); /* Not done: needs to be rescheduled */
/*
* Lets cycle a while then let the interrupt handler take over.
*/
GET_SBIC_asr(sc, asr);
do {
QPRINTF(("go[0x%x] ", csr));
/* Handle the new phase */
i = wd33c93_nextstate(sc, acb, csr, asr);
WAIT_CIP(sc); /* XXX */
if (sc->sc_state == SBIC_CONNECTED) {
GET_SBIC_asr(sc, asr);
if (asr & SBIC_ASR_LCI)
printf("wd33c93_go: LCI asr:%02x csr:%02x\n", asr, csr);
if (asr & SBIC_ASR_INT)
GET_SBIC_csr(sc, csr);
}
} while (sc->sc_state == SBIC_CONNECTED &&
asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
QPRINTF(("> done i=%d stat=%02x\n", i, sc->sc_status));
if (i == SBIC_STATE_DONE) {
if (sc->sc_status == STATUS_UNKNOWN) {
printf("wd33c93_go: done & stat == UNKNOWN\n");
return 1; /* Did we really finish that fast? */
}
}
return 0;
}
int
wd33c93_intr(struct wd33c93_softc *sc)
{
u_char asr, csr;
if (asr & SBIC_ASR_LCI)
printf("wd33c93_intr: LCI asr:%02x csr:%02x\n",
asr, csr);
if (asr & SBIC_ASR_INT)
GET_SBIC_csr(sc, csr);
}
} while (sc->sc_state == SBIC_CONNECTED &&
asr & (SBIC_ASR_INT|SBIC_ASR_LCI));
SBIC_DEBUG(INTS, ("intr done. state=%d, asr=0x%02x\n", i, asr));
return(1);
}
/*
* Complete current command using polled I/O. Used when interrupt driven
* I/O is not allowed (ie. during boot and shutdown)
*
* Polled I/O is very processor intensive
*/
int
wd33c93_poll(struct wd33c93_softc *sc, struct wd33c93_acb *acb)
{
u_char asr, csr=0;
int count;
struct scsipi_xfer *xs = acb->xs;
case MSG_HEAD_OF_Q_TAG:
case MSG_ORDERED_Q_TAG:
case MSG_SIMPLE_Q_TAG:
printf("-- Out of phase TAG;"
"Nexus=%d:%d Tag=%02x/%02x\n",
sc->target, sc->lun, msgaddr[0], msgaddr[1]);
break;
case MSG_DISCONNECT:
SBIC_DEBUG(MSGS, ("msgin: DISCONNECT"));
/*
* Mark the fact that all bytes have moved. The
* target may not bother to do a SAVE POINTERS
* at this stage. This flag will set the residual
* count to zero on MSG COMPLETE.
*/
if (sc->sc_dleft == 0)
acb->flags |= ACB_COMPLETE;
case MSG_CMDCOMPLETE:
/*
* !! KLUDGE ALERT !! quite a few drives don't seem to
* really like the current way of sending the
* sync-handshake together with the ident-message, and
* they react by sending command-complete and
* disconnecting right after returning the valid sync
* handshake. So, all I can do is reselect the drive,
* and hope it won't disconnect again. I don't think
* this is valid behavior, but I can't help fixing a
* problem that apparently exists.
*
* Note: we should not get here on `normal' command
* completion, as that condition is handled by the
* high-level sel&xfer resume command used to walk
* thru status/cc-phase.
*/
SBIC_DEBUG(MSGS, ("msgin: CMD_COMPLETE"));
SBIC_DEBUG(SYNC, ("GOT MSG %d! target %d"
" acting weird.."
" waiting for disconnect...\n",
msgaddr[0], sc->target));
/* Check to see if wd33c93 is handling this */
GET_SBIC_asr(sc, asr);
if (asr & SBIC_ASR_BSY)
break;
/* XXX: Assume it works and set status to 00 */
sc->sc_status = 0;
sc->sc_state = SBIC_CMDCOMPLETE;
break;
case MSG_EXTENDED:
switch(msgaddr[2]) {
case MSG_EXT_SDTR: /* Sync negotiation */
SBIC_DEBUG(MSGS, ("msgin: EXT_SDTR; "
"period %d, offset %d",
msgaddr[3], msgaddr[4]));
if (msgaddr[1] != 3)
goto reject;
/*
* <SGI, IBM DORS-32160, WA6A> will do nothing
* but attempt sync negotiation until it gets
* what it wants. To avoid an infinite loop set
* off by the identify request, oblige them.
*/
if ((sc->sc_flags&SBICF_SYNCNEGO) == 0 &&
msgaddr[3] != 0)
ti->flags |= T_WANTSYNC;
if (!(ti->flags & T_WANTSYNC))
ti->period = ti->offset = 0;
reject:
/* We don't support whatever this message is... */
wd33c93_sched_msgout(sc, SEND_REJECT);
break;
}
break;
case SBIC_IDENTIFIED:
/*
* IDENTIFY message was received and queue tag is expected now
*/
if ((msgaddr[0]!=MSG_SIMPLE_Q_TAG) || (sc->sc_msgify==0)) {
printf("%s: TAG reselect without IDENTIFY;"
" MSG %x; sending DEVICE RESET\n",
device_xname(sc->sc_dev), msgaddr[0]);
goto reset;
}
SBIC_DEBUG(TAGS, ("TAG %x/%x\n", msgaddr[0], msgaddr[1]));
if (sc->sc_nexus)
printf("*TAG Recv with active nexus!!\n");
wd33c93_reselect(sc, sc->target, sc->lun,
msgaddr[0], msgaddr[1]);
break;
case SBIC_RESELECTED:
/*
* IDENTIFY message with target
*/
if (MSG_ISIDENTIFY(msgaddr[0])) {
SBIC_DEBUG(PHASE, ("IFFY[%x] ", msgaddr[0]));
sc->sc_msgify = msgaddr[0];
} else {
printf("%s: reselect without IDENTIFY;"
" MSG %x;"
" sending DEVICE RESET\n",
device_xname(sc->sc_dev), msgaddr[0]);
goto reset;
}
break;
/*
* this should be the normal i/o completion case.
* get the status & cmd complete msg then let the
* device driver look at what happened.
*/
wd33c93_xferdone(sc);
wd33c93_dma_stop(sc);
/* Fixup byte count to be passed to higher layer */
acb->dleft = (acb->flags & ACB_COMPLETE) ? 0 :
sc->sc_dleft;
/*
* Indicate to the upper layers that the command is done
*/
wd33c93_scsidone(sc, acb, sc->sc_status);
return SBIC_STATE_DONE;
case SBIC_CSR_XFERRED | DATA_IN_PHASE:
case SBIC_CSR_MIS | DATA_IN_PHASE:
case SBIC_CSR_MIS_1 | DATA_IN_PHASE:
case SBIC_CSR_MIS_2 | DATA_IN_PHASE:
case SBIC_CSR_XFERRED | DATA_OUT_PHASE:
case SBIC_CSR_MIS | DATA_OUT_PHASE:
case SBIC_CSR_MIS_1 | DATA_OUT_PHASE:
case SBIC_CSR_MIS_2 | DATA_OUT_PHASE:
/*
* Verify that we expected to transfer data...
*/
if (acb->dleft <= 0) {
printf("next: DATA phase with xfer count == %zd, asr:0x%02x csr:0x%02x\n",
acb->dleft, asr, csr);
goto abort;
}
/*
* Should we transfer using PIO or DMA ?
*/
if (acb->xs->xs_control & XS_CTL_POLL ||
sc->sc_flags & SBICF_NODMA) {
/* Perform transfer using PIO */
int resid;
if (SBIC_PHASE(csr) == DATA_IN_PHASE)
/* data in */
resid = wd33c93_xfin(sc, sc->sc_dleft,
sc->sc_daddr);
else /* data out */
resid = wd33c93_xfout(sc, sc->sc_dleft,
sc->sc_daddr);
/* Setup byte count for transfer */
SBIC_TC_PUT(sc, (unsigned)sc->sc_dleft);
/* Start the transfer */
SET_SBIC_cmd(sc, SBIC_CMD_XFER_INFO);
/* Start the DMA chip going */
sc->sc_tcnt = sc->sc_dmago(sc);
/* Indicate that we're in DMA mode */
sc->sc_flags |= SBICF_INDMA;
}
break;
case SBIC_CSR_XFERRED | MESG_IN_PHASE:
case SBIC_CSR_MIS | MESG_IN_PHASE:
case SBIC_CSR_MIS_1 | MESG_IN_PHASE:
case SBIC_CSR_MIS_2 | MESG_IN_PHASE:
wd33c93_dma_stop(sc);
/* Handle a single message in... */
return wd33c93_msgin_phase(sc, 0);
case SBIC_CSR_MSGIN_W_ACK:
/*
* We should never see this since it's handled in
* 'wd33c93_msgin_phase()' but just for the sake of paranoia...
*/
SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
case SBIC_CSR_XFERRED | MESG_OUT_PHASE:
case SBIC_CSR_MIS | MESG_OUT_PHASE:
case SBIC_CSR_MIS_1 | MESG_OUT_PHASE:
case SBIC_CSR_MIS_2 | MESG_OUT_PHASE:
/*
* Message out phase. ATN signal has been asserted
*/
wd33c93_dma_stop(sc);
wd33c93_msgout(sc);
return SBIC_STATE_RUNNING;
case SBIC_CSR_DISC:
case SBIC_CSR_DISC_1:
SBIC_DEBUG(RSEL, ("wd33c93next target %d disconnected\n",
sc->target));
wd33c93_dma_stop(sc);
if (acb->xs->xs_control & XS_CTL_POLL || wd33c93_nodisc)
return SBIC_STATE_DISCONNECT;
/* Try to schedule another target */
wd33c93_sched(sc);
return SBIC_STATE_DISCONNECT;
case SBIC_CSR_RSLT_NI:
case SBIC_CSR_RSLT_IFY:
{
/*
* A reselection.
* Note that since we don't enable Advanced Features (assuming
* the WD chip is at least the 'A' revision), we're only ever
* likely to see the 'SBIC_CSR_RSLT_NI' status. But for the
* hell of it, we'll handle it anyway, for all the extra code
* it needs...
*/
u_char newtarget, newlun;
if (sc->sc_flags & SBICF_INDMA) {
printf("**** RESELECT WHILE DMA ACTIVE!!! ***\n");
wd33c93_dma_stop(sc);
}
if (csr == SBIC_CSR_RSLT_IFY) {
/* Read Identify msg to avoid lockup */
GET_SBIC_data(sc, newlun);
WAIT_CIP(sc);
newlun &= SBIC_TLUN_MASK;
sc->sc_msgify = MSG_IDENTIFY(newlun, 0);
} else {
/*
* Need to read Identify message the hard way, assuming
* the target even sends us one...
*/
for (newlun = 255; newlun; --newlun) {
GET_SBIC_asr(sc, asr);
if (asr & SBIC_ASR_INT)
break;
DELAY(10);
}
/* If we didn't get an interrupt, something's up */
if ((asr & SBIC_ASR_INT) == 0) {
printf("%s: Reselect without identify? asr %x\n",
device_xname(sc->sc_dev), asr);
newlun = 0; /* XXXX */
} else {
/*
* We got an interrupt, verify that it's a
* change to message in phase, and if so
* read the message.
*/
GET_SBIC_csr(sc,csr);
/* Ok, we have the identity of the reselecting target. */
SBIC_DEBUG(RSEL, ("wd33c93next: reselect from targ %d lun %d",
newtarget, newlun));
wd33c93_reselect(sc, newtarget, newlun, 0, 0);
sc->sc_disc--;
if (csr == SBIC_CSR_RSLT_IFY)
SET_SBIC_cmd(sc, SBIC_CMD_CLR_ACK);
break;
}
default:
abort:
/* Something unexpected happened -- deal with it. */
printf("next: aborting asr 0x%02x csr 0x%02x\n", asr, csr);
if (sc->sc_nexus) {
/*
* Whoops! We've been reselected with a
* command in progress!
* The best we can do is to put the current
* command back on the ready list and hope
* for the best.
*/
SBIC_DEBUG(RSEL, ("%s: reselect with active command\n",
device_xname(sc->sc_dev)));
ti = &sc->sc_tinfo[sc->target];
li = TINFO_LUN(ti, sc->lun);
li->state = L_STATE_IDLE;
/* Setup state for new nexus */
acb = NULL;
sc->sc_flags = SBICF_SELECTED;
sc->sc_msgpriq = sc->sc_msgout = sc->sc_msgoutq = 0;
ti = &sc->sc_tinfo[target];
li = TINFO_LUN(ti, lun);
if (li != NULL) {
if (li->untagged != NULL && li->state)
acb = li->untagged;
else if (tag_type != MSG_SIMPLE_Q_TAG) {
/* Wait for tag to come by during MESG_IN Phase */
sc->target = target; /* setup I_T_L nexus */
sc->lun = lun;
sc->sc_state = SBIC_IDENTIFIED;
return;
} else if (tag_type)
acb = li->queued[tag_id];
}
if (acb == NULL) {
printf("%s: reselect from target %d lun %d tag %x:%x "
"with no nexus; sending ABORT\n",
device_xname(sc->sc_dev), target, lun, tag_type, tag_id);
goto abort;
}
if (asr & SBIC_ASR_INT) {
/* We need to service a missed IRQ */
wd33c93_intr(sc);
} else {
(void) wd33c93_abort(sc, sc->sc_nexus, "timeout");
}
splx(s);
}
void
wd33c93_watchdog(void *arg)
{
struct wd33c93_softc *sc = arg;
struct wd33c93_tinfo *ti;
struct wd33c93_linfo *li;
int t, s, l;
/* scrub LUN's that have not been used in the last 10min. */
time_t old = time_second - (10 * 60);
for (t = 0; t < SBIC_NTARG; t++) {
ti = &sc->sc_tinfo[t];
for (l = 0; l < SBIC_NLUN; l++) {
s = splbio();
li = TINFO_LUN(ti, l);
if (li && li->last_used < old &&
li->untagged == NULL && li->used == 0) {
ti->lun[li->lun] = NULL;
free(li, M_DEVBUF);
}
splx(s);
}
}
callout_reset(&sc->sc_watchdog, 60 * hz, wd33c93_watchdog, sc);
}
#ifdef DEBUG
void
wd33c93_hexdump(u_char *buf, int len)
{
printf("{%d}:", len);
while (len--)
printf(" %02x", *buf++);
printf("\n");
}