/*-
* Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* 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.
*/
/*
* Copyright (c) 1994, 1995 Julian Highfield. All rights reserved.
* Portions copyright (c) 1994, 1996, 1997
* Charles M. Hannum. All rights reserved.
*
* 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 Julian Highfield.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 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.
*/
/*
* This driver is for the WD7000 family of SCSI controllers:
* the WD7000-ASC, a bus-mastering DMA controller,
* the WD7000-FASST2, an -ASC with new firmware and scatter-gather,
* and the WD7000-ASE, which was custom manufactured for Apollo
* workstations and seems to include an -ASC as well as floppy
* and ESDI interfaces.
*
* Loosely based on Theo Deraadt's unfinished attempt.
*/
if (wmbi->stat == WDS_MBI_FREE) {
for (i = 0; i < WDS_MBX_SIZE; i++) {
if (wmbi->stat != WDS_MBI_FREE) {
printf("%s: mbi not in round-robin order\n",
device_xname(sc->sc_dev));
goto AGAIN;
}
wds_nextmbx(wmbi, wmbx, mbi);
}
#ifdef WDSDIAGnot
printf("%s: mbi interrupt with no full mailboxes\n",
device_xname(sc->sc_dev));
#endif
return;
}
AGAIN:
do {
scb = wds_scb_phys_kv(sc, phystol(wmbi->scb_addr));
if (!scb) {
printf("%s: bad mbi scb pointer; skipping\n",
device_xname(sc->sc_dev));
goto next;
}
/*
* put in the phystokv hash table
* Never gets taken out.
*/
scb->hashkey = scb->dmamap_self->dm_segs[0].ds_addr;
hashnum = SCB_HASH(scb->hashkey);
scb->nexthash = sc->sc_scbhash[hashnum];
sc->sc_scbhash[hashnum] = scb;
wds_reset_scb(sc, scb);
return (0);
}
/*
* Create a set of scbs and add them to the free list.
*/
int
wds_create_scbs(struct wds_softc *sc, void *mem, size_t size)
{
bus_dma_segment_t seg;
struct wds_scb *scb;
int rseg, error;
/*
* Get a free scb
*
* If there are none, see if we can allocate a new one. If so, put it in
* the hash table too otherwise either return an error or sleep.
*/
struct wds_scb *
wds_get_scb(struct wds_softc *sc)
{
struct wds_scb *scb;
int s;
while (scb) {
if (scb->hashkey == scb_phys)
break;
/* XXX Check to see if it matches the sense command block. */
if (scb->hashkey == (scb_phys - sizeof(struct wds_cmd)))
break;
scb = scb->nexthash;
}
return (scb);
}
/*
* Queue a SCB to be sent to the controller, and send it if possible.
*/
void
wds_queue_scb(struct wds_softc *sc, struct wds_scb *scb)
{
/* Link scb to mbo. */
ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
offsetof(struct wds_scb, cmd), wmbo->scb_addr);
/* XXX What about aborts? */
wmbo->cmd = WDS_MBO_START;
/* Tell the card to poll immediately. */
c = WDSC_MSTART(wmbo - wmbx->mbo);
wds_cmd(sc->sc_iot, sc->sc_ioh, &c, sizeof c);
if ((scb->flags & SCB_POLLED) == 0)
callout_reset(&scb->xs->xs_callout,
mstohz(scb->timeout), wds_timeout, scb);
++sc->sc_mbofull;
wds_nextmbx(wmbo, wmbx, mbo);
}
wmbx->tmbo = wmbo;
}
/*
* Process the result of a SCSI command.
*/
void
wds_done(struct wds_softc *sc, struct wds_scb *scb, u_char stat)
{
bus_dma_tag_t dmat = sc->sc_dmat;
struct scsipi_xfer *xs = scb->xs;
/* XXXXX */
/* Don't release the SCB if it was an internal command. */
if (xs == 0) {
scb->flags |= SCB_DONE;
return;
}
/*
* If we were a data transfer, unload the map that described
* the data buffer.
*/
if (xs->datalen) {
bus_dmamap_sync(dmat, scb->dmamap_xfer, 0,
scb->dmamap_xfer->dm_mapsize,
(xs->xs_control & XS_CTL_DATA_IN) ?
BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(dmat, scb->dmamap_xfer);
}
if (xs->error == XS_NOERROR) {
/* If all went well, or an error is acceptable. */
if (stat == WDS_MBI_OK) {
/* OK, set the result */
xs->resid = 0;
} else {
/* Check the mailbox status. */
switch (stat) {
case WDS_MBI_OKERR:
/*
* SCSI error recorded in scb,
* counts as WDS_MBI_OK
*/
switch (scb->cmd.venderr) {
case 0x00:
aprint_error_dev(sc->sc_dev,
"Is this an error?\n");
/* Experiment. */
xs->error = XS_DRIVER_STUFFUP;
break;
case 0x01:
#if 0
aprint_error_dev(sc->sc_dev,
"OK, see SCSI error field.\n");
#endif
if (scb->cmd.stat == SCSI_CHECK ||
scb->cmd.stat == SCSI_BUSY) {
xs->status = scb->cmd.stat;
xs->error = XS_BUSY;
}
break;
case 0x40:
#if 0
printf("%s: DMA underrun!\n",
device_xname(sc->sc_dev));
#endif
/*
* Hits this if the target
* returns fewer that datalen
* bytes (eg my CD-ROM, which
* returns a short version
* string, or if DMA is
* turned off etc.
*/
xs->resid = 0;
break;
default:
printf("%s: VENDOR ERROR "
"%02x, scsi %02x\n",
device_xname(sc->sc_dev),
scb->cmd.venderr,
scb->cmd.stat);
/* Experiment. */
xs->error = XS_DRIVER_STUFFUP;
break;
}
break;
case WDS_MBI_ETIME:
/*
* The documentation isn't clear on
* what conditions might generate this,
* but selection timeouts are the only
* one I can think of.
*/
xs->error = XS_SELTIMEOUT;
break;
case WDS_MBI_ERESET:
case WDS_MBI_ETARCMD:
case WDS_MBI_ERESEL:
case WDS_MBI_ESEL:
case WDS_MBI_EABORT:
case WDS_MBI_ESRESET:
case WDS_MBI_EHRESET:
xs->error = XS_DRIVER_STUFFUP;
break;
}
}
} /* XS_NOERROR */
wds_free_scb(sc, scb);
scsipi_done(xs);
}
int
wds_find(bus_space_tag_t iot, bus_space_handle_t ioh,
struct wds_probe_data *sc)
{
int i;
/* XXXXX */
/*
* Sending a command causes the CMDRDY bit to clear.
*/
for (i = 5; i; i--) {
if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
break;
delay(100);
}
if (!i)
return 0;
for (i = 2000; i; i--) {
if ((bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_RDY) != 0)
break;
delay(100);
}
if (!i)
return 0;
if (sc) {
#ifdef notyet
sc->sc_irq = ...;
sc->sc_drq = ...;
#endif
/* XXX Can we do this better? */
sc->sc_scsi_dev = 7;
}
return 1;
}
/*
* Initialise the board and driver.
*/
void
wds_init(struct wds_softc *sc, int isreset)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
bus_dma_segment_t seg;
struct wds_setup init;
u_char c;
int i, rseg;
if (isreset)
goto doinit;
/*
* Allocate the mailbox.
*/
if (bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE, 0, &seg, 1,
&rseg, BUS_DMA_NOWAIT) ||
bus_dmamem_map(sc->sc_dmat, &seg, rseg, PAGE_SIZE,
(void **)&wmbx, BUS_DMA_NOWAIT|BUS_DMA_COHERENT))
panic("wds_init: can't create or map mailbox");
/*
* Since DMA memory allocation is always rounded up to a
* page size, create some scbs from the leftovers.
*/
if (wds_create_scbs(sc, ((char *)wmbx) +
ALIGN(sizeof(struct wds_mbx)),
PAGE_SIZE - ALIGN(sizeof(struct wds_mbx))))
panic("wds_init: can't create scbs");
/*
* Create and load the mailbox DMA map.
*/
if (bus_dmamap_create(sc->sc_dmat, sizeof(struct wds_mbx), 1,
sizeof(struct wds_mbx), 0, BUS_DMA_NOWAIT, &sc->sc_dmamap_mbox) ||
bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_mbox, wmbx,
sizeof(struct wds_mbx), NULL, BUS_DMA_NOWAIT))
panic("wds_ionit: can't create or load mailbox DMA map");
doinit:
/*
* Set up initial mail box for round-robin operation.
*/
for (i = 0; i < WDS_MBX_SIZE; i++) {
wmbx->mbo[i].cmd = WDS_MBO_FREE;
wmbx->mbi[i].stat = WDS_MBI_FREE;
}
wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
wmbx->tmbi = &wmbx->mbi[0];
sc->sc_mbofull = 0;
if (xs->xs_control & XS_CTL_DATA_UIO) {
/* XXX Fix me! */
/*
* Let's not worry about UIO. There isn't any code
* for the non-SG boards anyway!
*/
aprint_error_dev(sc->sc_dev,
"UIO is untested and disabled!\n");
xs->error = XS_DRIVER_STUFFUP;
scsipi_done(xs);
return;
}
flags = xs->xs_control;
/* Get an SCB to use. */
scb = wds_get_scb(sc);
#ifdef DIAGNOSTIC
/*
* This should never happen as we track the resources
* in the mid-layer.
*/
if (scb == NULL) {
scsipi_printaddr(periph);
printf("unable to allocate scb\n");
panic("wds_scsipi_request");
}
#endif
scb->xs = xs;
scb->timeout = xs->timeout;
/* Zero out the command structure. */
if (xs->cmdlen > sizeof(scb->cmd.scb)) {
aprint_error_dev(sc->sc_dev,
"cmdlen %d too large for SCB\n", xs->cmdlen);
xs->error = XS_DRIVER_STUFFUP;
goto out_bad;
}
memset(&scb->cmd, 0, sizeof scb->cmd);
memcpy(&scb->cmd.scb, xs->cmd, xs->cmdlen);
/* Set up some of the command fields. */
scb->cmd.targ = (periph->periph_target << 5) |
periph->periph_lun;
/*
* NOTE: cmd.write may be OK as 0x40 (disable direction
* checking) on boards other than the WD-7000V-ASE. Need
* this for the ASE:
*/
scb->cmd.write = (xs->xs_control & XS_CTL_DATA_IN) ?
0x80 : 0x00;
if (sc->sc_maxsegs > 1) {
/*
* Load the hardware scatter/gather map with the
* contents of the DMA map.
*/
for (seg = 0;
seg < scb->dmamap_xfer->dm_nsegs; seg++) {
ltophys(scb->dmamap_xfer->dm_segs[seg].ds_addr,
scb->scat_gath[seg].seg_addr);
ltophys(scb->dmamap_xfer->dm_segs[seg].ds_len,
scb->scat_gath[seg].seg_len);
}
/*
* Set up for scatter/gather transfer.
*/
scb->cmd.opcode = WDSX_SCSISG;
ltophys(scb->dmamap_self->dm_segs[0].ds_addr +
offsetof(struct wds_scb, scat_gath),
scb->cmd.data);
ltophys(scb->dmamap_self->dm_nsegs *
sizeof(struct wds_scat_gath), scb->cmd.len);
} else {
/*
* This board is an ASC or an ASE, and the
* transfer has been mapped contig for us.
*/
scb->cmd.opcode = WDSX_SCSICMD;
ltophys(scb->dmamap_xfer->dm_segs[0].ds_addr,
scb->cmd.data);
ltophys(scb->dmamap_xfer->dm_segs[0].ds_len,
scb->cmd.len);
}
} else {
scb->cmd.opcode = WDSX_SCSICMD;
ltophys(0, scb->cmd.data);
ltophys(0, scb->cmd.len);
}
/* XXX Do we really want to do this? */
if (flags & XS_CTL_POLL) {
/* Will poll card, await result. */
bus_space_write_1(sc->sc_iot, sc->sc_ioh,
WDS_HCR, WDSH_DRQEN);
scb->flags |= SCB_POLLED;
} else {
/*
* Will send command, let interrupt routine
* handle result.
*/
bus_space_write_1(sc->sc_iot, sc->sc_ioh, WDS_HCR,
WDSH_IRQEN | WDSH_DRQEN);
}
s = splbio();
wds_queue_scb(sc, scb);
splx(s);
if ((flags & XS_CTL_POLL) == 0)
return;
if (wds_poll(sc, xs, scb->timeout)) {
wds_timeout(scb);
if (wds_poll(sc, xs, scb->timeout))
wds_timeout(scb);
}
return;
case ADAPTER_REQ_GROW_RESOURCES:
/* XXX Not supported. */
return;
case ADAPTER_REQ_SET_XFER_MODE:
/* XXX How do we do this? */
return;
}
}
/*
* Poll a particular unit, looking for a particular scb
*/
int
wds_poll(struct wds_softc *sc, struct scsipi_xfer *xs, int count)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
/* timeouts are in msec, so we loop in 1000 usec cycles */
while (count) {
/*
* If we had interrupts enabled, would we
* have got an interrupt?
*/
if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
wdsintr(sc);
if (xs->xs_status & XS_STS_DONE)
return 0;
delay(1000); /* only happens in boot so ok */
count--;
}
return 1;
}
/*
* Poll a particular unit, looking for a particular scb
*/
int
wds_ipoll(struct wds_softc *sc, struct wds_scb *scb, int count)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
/* timeouts are in msec, so we loop in 1000 usec cycles */
while (count) {
/*
* If we had interrupts enabled, would we
* have got an interrupt?
*/
if (bus_space_read_1(iot, ioh, WDS_STAT) & WDSS_IRQ)
wdsintr(sc);
if (scb->flags & SCB_DONE)
return 0;
delay(1000); /* only happens in boot so ok */
count--;
}
return 1;
}
#ifdef WDSDIAG
/*
* If The scb's mbx is not free, then the board has gone south?
*/
wds_collect_mbo(sc);
if (scb->flags & SCB_SENDING) {
aprint_error_dev(sc->sc_dev, "not taking commands!\n");
Debugger();
}
#endif
/*
* If it has been through before, then
* a previous abort has failed, don't
* try abort again
*/
if (scb->flags & SCB_ABORT) {
/* abort timed out */
printf(" AGAIN\n");
/* XXX Must reset! */
} else {
/* abort the operation that has timed out */
printf("\n");
scb->xs->error = XS_TIMEOUT;
scb->timeout = WDS_ABORT_TIMEOUT;
scb->flags |= SCB_ABORT;
wds_queue_scb(sc, scb);
}