/*-
* Copyright (c) 1996,2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Adam Glass, David Jones, Gordon W. Ross, Jason R. Thorpe and
* Paul Kranenburg.
*
* 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.
*/
/*
* This file contains VME bus-dependent of the `si' SCSI adapter.
* This hardware is frequently found on Sun 3 and Sun 4 machines.
*
* The SCSI machinery on this adapter is implemented by an NCR5380,
* which is taken care of by the chipset driver in /sys/dev/ic/ncr5380sbc.c
*
* The logic has a bit to enable or disable the DMA engine,
* but that bit also gates the interrupt line from the NCR5380!
* Therefore, in order to get any interrupt from the 5380, (i.e.
* for reselect) one must clear the DMA engine transfer count and
* then enable DMA. This has the further complication that you
* CAN NOT touch the NCR5380 while the DMA enable bit is set, so
* we have to turn DMA back off before we even look at the 5380.
*
* What wonderfully whacky hardware this is!
*
*/
/*
* This driver originated as an MD implementation for the sun3 and sun4
* ports. The notes pertaining to that history are included below.
*
* David Jones wrote the initial version of this module for NetBSD/sun3,
* which included support for the VME adapter only. (no reselection).
*
* Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
* both the VME and OBIO code to support disconnect/reselect.
* (Required figuring out the hardware "features" noted above.)
*
* The autoconfiguration boilerplate came from Adam Glass.
*
* Jason R. Thorpe ported the autoconfiguration and VME portions to
* NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
* a wacky OBIO variant of the VME SCSI-3. Many thanks to Chuck Cranor
* for lots of helpful tips and suggestions. Thanks also to Paul Kranenburg
* and Chris Torek for bits of insight needed along the way. Thanks to
* David Gilbert and Andrew Gillham who risked filesystem life-and-limb
* for the sake of testing. Andrew Gillham helped work out the bugs
* the 4/100 DMA code.
*/
/*
* Transfers smaller than this are done using PIO
* (on assumption they're not worth DMA overhead)
*/
#define MIN_DMA_LEN 128
#ifdef DEBUG
int si_debug = 0;
#endif
/*
* This structure is used to keep track of mapped DMA requests.
*/
struct si_dma_handle {
int dh_flags;
#define SIDH_BUSY 0x01 /* This DH is in use */
#define SIDH_OUT 0x02 /* DMA does data out (write) */
int dh_maplen; /* Original data length */
bus_dmamap_t dh_dmamap;
#define dh_dvma dh_dmamap->dm_segs[0].ds_addr /* VA of buffer in DVMA space */
};
/*
* The first structure member has to be the ncr5380_softc
* so we can just cast to go back and fourth between them.
*/
struct si_softc {
struct ncr5380_softc ncr_sc;
bus_space_tag_t sc_bustag; /* bus tags */
bus_dma_tag_t sc_dmatag;
vme_chipset_tag_t sc_vctag;
int sc_adapter_iv_am; /* int. vec + address modifier */
struct si_dma_handle *sc_dma;
int sc_xlen; /* length of current DMA segment. */
int sc_options; /* options for this instance. */
};
/*
* Options. By default, DMA is enabled and DMA completion interrupts
* and reselect are disabled. You may enable additional features
* the `flags' directive in your kernel's configuration file.
*
* Alternatively, you can patch your kernel with DDB or some other
* mechanism. The sc_options member of the softc is OR'd with
* the value in si_options.
*
* Note, there's a separate sw_options to make life easier.
*/
#define SI_ENABLE_DMA 0x01 /* Use DMA (maybe polled) */
#define SI_DMA_INTR 0x02 /* DMA completion interrupts */
#define SI_DO_RESELECT 0x04 /* Allow disconnect/reselect */
#define SI_OPTIONS_MASK (SI_ENABLE_DMA|SI_DMA_INTR|SI_DO_RESELECT)
#define SI_OPTIONS_BITS "\10\3RESELECT\2DMA_INTR\1DMA"
int si_options = SI_ENABLE_DMA|SI_DMA_INTR|SI_DO_RESELECT;
/*
* If this is a VME SCSI board, we have to determine whether
* it is an "sc" (Sun2) or "si" (Sun3) SCSI board. This can
* be determined using the fact that the "sc" board occupies
* 4K bytes in VME space but the "si" board occupies 2K bytes.
*/
return vme_probe(ct, vme_addr + 0x801, 1, mod, VME_D8, NULL, 0) != 0;
}
/*
* Pull in the options flags. Allow the user to completely
* override the default values.
*/
if ((device_cfdata(self)->cf_flags & SI_OPTIONS_MASK) != 0)
sc->sc_options =
device_cfdata(self)->cf_flags & SI_OPTIONS_MASK;
/*
* Initialize si board itself.
*/
si_reset_adapter(ncr_sc);
ncr5380_attach(ncr_sc);
if (sc->sc_options & SI_DO_RESELECT) {
/*
* Need to enable interrupts (and DMA!)
* on this H/W for reselect to work.
*/
ncr_sc->sc_intr_on = si_intr_on;
ncr_sc->sc_intr_off = si_intr_off;
}
}
#ifdef DEBUG
if (si_debug) {
printf("%s\n", __func__);
}
#endif
/*
* The SCSI3 controller has an 8K FIFO to buffer data between the
* 5380 and the DMA. Make sure it starts out empty.
*
* The reset bits in the CSR are active low.
*/
SIREG_WRITE(ncr_sc, SIREG_CSR, 0);
delay(10);
SIREG_WRITE(ncr_sc, SIREG_CSR,
SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN);
delay(10);
/*****************************************************************
* Common functions for DMA
****************************************************************/
/*
* Allocate a DMA handle and put it in sc->sc_dma. Prepare
* for DMA transfer.
*/
void
si_dma_alloc(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct scsipi_xfer *xs = sr->sr_xs;
struct si_dma_handle *dh;
int i, xlen;
u_long addr;
#ifdef DIAGNOSTIC
if (sr->sr_dma_hand != NULL)
panic("%s: already have DMA handle", __func__);
#endif
#if 1 /* XXX - Temporary */
/* XXX - In case we think DMA is completely broken... */
if ((sc->sc_options & SI_ENABLE_DMA) == 0)
return;
#endif
/* If the DMA start addr is misaligned then do PIO */
if ((addr & 1) || (xlen & 1)) {
printf("%s: misaligned.\n", __func__);
return;
}
/* Make sure our caller checked sc_min_dma_len. */
if (xlen < MIN_DMA_LEN)
panic("%s: xlen=0x%x", __func__, xlen);
/* Find free DMA handle. Guaranteed to find one since we have
as many DMA handles as the driver has processes. */
for (i = 0; i < SCI_OPENINGS; i++) {
if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
goto found;
}
panic("si: no free DMA handles.");
/* Copy the "write" flag for convenience. */
if ((xs->xs_control & XS_CTL_DATA_OUT) != 0)
dh->dh_flags |= SIDH_OUT;
/*
* Double-map the buffer into DVMA space. If we can't re-map
* the buffer, we print a warning and fall back to PIO mode.
*
* NOTE: it is not safe to sleep here!
*/
if (bus_dmamap_load(sc->sc_dmatag, dh->dh_dmamap,
(void *)addr, xlen, NULL, BUS_DMA_NOWAIT) != 0) {
/* Can't remap segment */
printf("%s: can't remap 0x%lx/0x%x, doing PIO\n",
__func__, addr, dh->dh_maplen);
dh->dh_flags = 0;
return;
}
bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap, addr, xlen,
(dh->dh_flags & SIDH_OUT)
? BUS_DMASYNC_PREWRITE
: BUS_DMASYNC_PREREAD);
#ifdef DIAGNOSTIC
if (dh == NULL)
panic("%s: no DMA handle", __func__);
#endif
if (ncr_sc->sc_state & NCR_DOINGDMA)
panic("%s: free while in progress", __func__);
if (dh->dh_flags & SIDH_BUSY) {
/* Give back the DVMA space. */
bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap,
dh->dh_dvma, dh->dh_maplen,
(dh->dh_flags & SIDH_OUT)
? BUS_DMASYNC_POSTWRITE
: BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->sc_dmatag, dh->dh_dmamap);
dh->dh_flags = 0;
}
sr->sr_dma_hand = NULL;
}
/*
* Poll (spin-wait) for DMA completion.
* Called right after xx_dma_start(), and
* xx_dma_stop() will be called next.
* Same for either VME or OBIO.
*/
void
si_dma_poll(struct ncr5380_softc *ncr_sc)
{
struct sci_req *sr = ncr_sc->sc_current;
int tmo, csr_mask, csr;
/* Make sure DMA started successfully. */
if (ncr_sc->sc_state & NCR_ABORTING)
return;
/*****************************************************************
* VME functions for DMA
****************************************************************/
/*
* This is called when the bus is going idle,
* so we want to enable the SBC interrupts.
* That is controlled by the DMA enable!
* Who would have guessed!
* What a NASTY trick!
*/
void
si_intr_on(struct ncr5380_softc *ncr_sc)
{
uint16_t csr;
/*
* This is called when the bus is idle and we are
* about to start playing with the SBC chip.
*/
void
si_intr_off(struct ncr5380_softc *ncr_sc)
{
uint16_t csr;
/*
* This function is called during the COMMAND or MSG_IN phase
* that precedes a DATA_IN or DATA_OUT phase, in case we need
* to setup the DMA engine before the bus enters a DATA phase.
*
* XXX: The VME adapter appears to suppress SBC interrupts
* when the FIFO is not empty or the FIFO count is non-zero!
*
* On the VME version we just clear the DMA count and address
* here (to make sure it stays idle) and do the real setup
* later, in dma_start.
*/
void
si_dma_setup(struct ncr5380_softc *ncr_sc)
{
struct si_softc *sc = (struct si_softc *)ncr_sc;
struct sci_req *sr = ncr_sc->sc_current;
struct si_dma_handle *dh = sr->sr_dma_hand;
uint16_t csr;
u_long dva;
int xlen;
/*
* Set up the DMA controller.
* Note that (dh->dh_len < sc_datalen)
*/
csr = SIREG_READ(ncr_sc, SIREG_CSR);
/* Disable DMA while we're setting up the transfer */
csr &= ~SI_CSR_DMA_EN;
/*
* Get the DVMA mapping for this segment.
*/
dva = (u_long)(dh->dh_dvma);
if (dva & 1)
panic("%s: bad dmaaddr=0x%lx", __func__, dva);
xlen = ncr_sc->sc_datalen;
xlen &= ~1;
sc->sc_xlen = xlen; /* XXX: or less... */
#ifdef DEBUG
if (si_debug & 2) {
printf("%s: dh=%p, dmaaddr=0x%lx, xlen=%d\n",
__func__, dh, dva, xlen);
}
#endif
/* Set direction (send/recv) */
if (dh->dh_flags & SIDH_OUT) {
csr |= SI_CSR_SEND;
} else {
csr &= ~SI_CSR_SEND;
}
/* Set byte-packing control */
if (dva & 2) {
csr |= SI_CSR_BPCON;
} else {
csr &= ~SI_CSR_BPCON;
}
/*
* Acknowledge the phase change. (After DMA setup!)
* Put the SBIC into DMA mode, and start the transfer.
*/
if (dh->dh_flags & SIDH_OUT) {
NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
SCI_CLR_INTR(ncr_sc);
NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
/* Note that timeout may have set the error flag. */
if (ncr_sc->sc_state & NCR_ABORTING)
goto out;
/*
* Now try to figure out how much actually transferred
*
* The fifo_count does not reflect how many bytes were
* actually transferred for VME.
*
* SCSI-3 VME interface is a little funny on writes:
* if we have a disconnect, the DMA has overshot by
* one byte and the resid needs to be incremented.
* Only happens for partial transfers.
* (Thanks to Matt Jacob)
*/
/*
* After a read, we may need to clean-up
* "Left-over bytes" (yuck!)
*/
if (((dh->dh_flags & SIDH_OUT) == 0) &&
((csr & SI_CSR_LOB) != 0)) {
uint8_t *cp = ncr_sc->sc_dataptr;
uint16_t bprh, bprl;