/*
* Copyright (c) 2000, 2002 Manuel Bouyer.
*
* 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 AUTHOR ``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 AUTHOR 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.
*
*/
if (sc->features & SF_CHIP_FIFO)
bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5,
bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST5) |
CTEST5_DFS);
if (sc->features & SF_CHIP_LED0) {
/* Set GPIO0 as output if software LED control is required */
bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL,
bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_GPCNTL) & 0xfe);
}
if (sc->features & SF_BUS_ULTRA3) {
/* reset SCNTL4 */
bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_SCNTL4, 0);
}
sc->mode = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_STEST4) &
STEST4_MODE_MASK;
/*
* initialise the RAM. Without this we may get scsi gross errors on
* the 1010
*/
if (sc->features & SF_CHIP_RAM)
bus_space_set_region_4(sc->sc_ramt, sc->sc_ramh,
0, 0, sc->ram_size / 4);
sc->sc_reset(sc);
}
/* prepare tables before sending a cmd */
void
siop_setuptables(struct siop_common_cmd *siop_cmd)
{
int i;
struct siop_common_softc *sc = siop_cmd->siop_sc;
struct scsipi_xfer *xs = siop_cmd->xs;
int target = xs->xs_periph->periph_target;
int lun = xs->xs_periph->periph_lun;
int msgoffset = 1;
siop_cmd->siop_tables->id = siop_htoc32(sc, sc->targets[target]->id);
memset(siop_cmd->siop_tables->msg_out, 0,
sizeof(siop_cmd->siop_tables->msg_out));
/* request sense doesn't disconnect */
if (xs->xs_control & XS_CTL_REQSENSE)
siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
else if ((sc->features & SF_CHIP_GEBUG) &&
(sc->targets[target]->flags & TARF_ISWIDE) == 0)
/*
* 1010 bug: it seems that the 1010 has problems with reselect
* when not in wide mode (generate false SCSI gross error).
* The FreeBSD sym driver has comments about it but their
* workaround (disable SCSI gross error reporting) doesn't
* work with my adapter. So disable disconnect when not
* wide.
*/
siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 0);
else
siop_cmd->siop_tables->msg_out[0] = MSG_IDENTIFY(lun, 1);
if (xs->xs_tag_type != 0) {
if ((sc->targets[target]->flags & TARF_TAG) == 0) {
scsipi_printaddr(xs->xs_periph);
printf(": tagged command type %d id %d\n",
siop_cmd->xs->xs_tag_type, siop_cmd->xs->xs_tag_id);
panic("tagged command for non-tagging device");
}
siop_cmd->flags |= CMDFL_TAG;
siop_cmd->siop_tables->msg_out[1] = siop_cmd->xs->xs_tag_type;
/*
* use siop_cmd->tag not xs->xs_tag_id, caller may want a
* different one
*/
siop_cmd->siop_tables->msg_out[2] = siop_cmd->tag;
msgoffset = 3;
}
siop_cmd->siop_tables->t_msgout.count = siop_htoc32(sc, msgoffset);
if (sc->targets[target]->status == TARST_ASYNC) {
if ((sc->targets[target]->flags & TARF_DT) &&
(sc->mode == STEST4_MODE_LVD)) {
sc->targets[target]->status = TARST_PPR_NEG;
siop_ppr_msg(siop_cmd, msgoffset, sc->dt_minsync,
sc->maxoff);
} else if (sc->targets[target]->flags & TARF_WIDE) {
sc->targets[target]->status = TARST_WIDE_NEG;
siop_wdtr_msg(siop_cmd, msgoffset,
MSG_EXT_WDTR_BUS_16_BIT);
} else if (sc->targets[target]->flags & TARF_SYNC) {
sc->targets[target]->status = TARST_SYNC_NEG;
siop_sdtr_msg(siop_cmd, msgoffset, sc->st_minsync,
(sc->maxoff > 31) ? 31 : sc->maxoff);
} else {
sc->targets[target]->status = TARST_OK;
siop_update_xfer_mode(sc, target);
}
}
siop_cmd->siop_tables->status =
siop_htoc32(sc, SCSI_SIOP_NOSTATUS); /* set invalid status */
siop_cmd->siop_tables->cmd.count =
siop_htoc32(sc, siop_cmd->dmamap_cmd->dm_segs[0].ds_len);
siop_cmd->siop_tables->cmd.addr =
siop_htoc32(sc, siop_cmd->dmamap_cmd->dm_segs[0].ds_addr);
if (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
for (i = 0; i < siop_cmd->dmamap_data->dm_nsegs; i++) {
siop_cmd->siop_tables->data[i].count =
siop_htoc32(sc,
siop_cmd->dmamap_data->dm_segs[i].ds_len);
siop_cmd->siop_tables->data[i].addr =
siop_htoc32(sc,
siop_cmd->dmamap_data->dm_segs[i].ds_addr);
}
}
}
switch (cmd) {
case SCBUSIORESET:
/*
* abort the script. This will trigger an interrupt, which will
* trigger a bus reset.
* We can't safely trigger the reset here as we can't access
* the required register while the script is running.
*/
bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_ISTAT, ISTAT_ABRT);
return (0);
default:
return (ENOTTY);
}
}
/*
* compute how much of the current table didn't get handled when
* a phase mismatch occurs
*/
if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
== 0)
return; /* no valid data transfer */
if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
== 0)
return; /* no data pointers to save */
/*
* offset == SIOP_NSG may be a valid condition if we get a Save data
* pointer when the xfer is done. Just ignore the Save data pointer
* in this case
*/
if (offset == SIOP_NSG)
return;
#ifdef DIAGNOSTIC
if (offset > SIOP_NSG) {
scsipi_printaddr(siop_cmd->xs->xs_periph);
printf(": offset %d > %d\n", offset, SIOP_NSG);
panic("siop_sdp: offset");
}
#endif
/*
* Save data pointer. We do this by adjusting the tables to point
* at the beginning of the data not yet transferred.
* offset points to the first table with untransferred data.
*/
/*
* before doing that we decrease resid from the amount of data which
* has been transferred.
*/
siop_update_resid(siop_cmd, offset);
/*
* First let see if we have a resid from a phase mismatch. If so,
* we have to adjst the table at offset to remove transferred data.
*/
if (siop_cmd->flags & CMDFL_RESID) {
siop_cmd->flags &= ~CMDFL_RESID;
table = &siop_cmd->siop_tables->data[offset];
/* "cut" already transferred data from this table */
table->addr =
siop_htoc32(sc, siop_ctoh32(sc, table->addr) +
siop_ctoh32(sc, table->count) - siop_cmd->resid);
table->count = siop_htoc32(sc, siop_cmd->resid);
}
/*
* now we can remove entries which have been transferred.
* We just move the entries with data left at the beginning of the
* tables
*/
memmove(&siop_cmd->siop_tables->data[0],
&siop_cmd->siop_tables->data[offset],
(SIOP_NSG - offset) * sizeof(scr_table_t));
}
void
siop_update_resid(struct siop_common_cmd *siop_cmd, int offset)
{
struct siop_common_softc *sc = siop_cmd->siop_sc;
scr_table_t *table;
int i;
if ((siop_cmd->xs->xs_control & (XS_CTL_DATA_OUT | XS_CTL_DATA_IN))
== 0)
return; /* no data to transfer */
/*
* update resid. First account for the table entries which have
* been fully completed.
*/
for (i = 0; i < offset; i++)
siop_cmd->xs->resid -=
siop_ctoh32(sc, siop_cmd->siop_tables->data[i].count);
/*
* if CMDFL_RESID is set, the last table (pointed by offset) is a
* partial transfers. If not, offset points to the entry following
* the last full transfer.
*/
if (siop_cmd->flags & CMDFL_RESID) {
table = &siop_cmd->siop_tables->data[offset];
siop_cmd->xs->resid -=
siop_ctoh32(sc, table->count) - siop_cmd->resid;
}
}
int
siop_iwr(struct siop_common_cmd *siop_cmd)
{
int offset;
scr_table_t *table; /* table with IWR */
struct siop_common_softc *sc = siop_cmd->siop_sc;
/* handle ignore wide residue messages */
/* if target isn't wide, reject */
if ((siop_cmd->siop_target->flags & TARF_ISWIDE) == 0) {
siop_cmd->siop_tables->t_msgout.count = siop_htoc32(sc, 1);
siop_cmd->siop_tables->msg_out[0] = MSG_MESSAGE_REJECT;
return SIOP_NEG_MSGOUT;
}
/* get index of current command in table */
offset = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_SCRATCHA + 1);
/*
* if the current table did complete, we're now pointing at the
* next one. Go back one if we didn't see a phase mismatch.
*/
if ((siop_cmd->flags & CMDFL_RESID) == 0)
offset--;
table = &siop_cmd->siop_tables->data[offset];
if ((siop_cmd->flags & CMDFL_RESID) == 0) {
if (siop_ctoh32(sc, table->count) & 1) {
/* we really got the number of bytes we expected */
return SIOP_NEG_ACK;
} else {
/*
* now we really had a short xfer, by one byte.
* handle it just as if we had a phase mismatch
* (there is a resid of one for this table).
* Update scratcha1 to reflect the fact that
* this xfer isn't complete.
*/
siop_cmd->flags |= CMDFL_RESID;
siop_cmd->resid = 1;
bus_space_write_1(sc->sc_rt, sc->sc_rh,
SIOP_SCRATCHA + 1, offset);
return SIOP_NEG_ACK;
}
} else {
/*
* we already have a short xfer for this table; it's
* just one byte less than we though it was
*/
siop_cmd->resid--;
return SIOP_NEG_ACK;
}
}
void
siop_clearfifo(struct siop_common_softc *sc)
{
int timeout = 0;
int ctest3 = bus_space_read_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST3);