/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code was written by Alessandro Forin and Neil Pittman
* at Microsoft Research and contributed to The NetBSD Foundation
* by Microsoft Corporation.
*
* 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.
*/
/*
* Handle the close request for the device.
*/
static int
icapclose(dev_t device, int flags, int fmt, struct lwp *process)
{
DEBUG_PRINT(("icapclose\n"), DEBUG_FUNCS);
return 0; /* this always succeeds */
}
/*
* Handle the read request for the device.
*/
static int
icapread(dev_t dev, struct uio *uio, int flags)
{
DEBUG_PRINT(("icapread\n"), DEBUG_READS);
return (physio(icapstrategy, NULL, dev, B_READ, minphys, uio));
}
/*
* Handle the write request for the device.
*/
static int
icapwrite(dev_t dev, struct uio *uio, int flags)
{
DEBUG_PRINT(("icapwrite\n"), DEBUG_WRITES);
return (physio(icapstrategy, NULL, dev, B_WRITE, minphys, uio));
}
/*
* Handle the ioctl request for the device.
*/
static int
icapioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
{
return ENOTTY;
}
/*
* Strategy function for the device.
*/
static void
icapstrategy(struct buf *bp)
{
struct icap_softc *sc;
int s;
DEBUG_PRINT(("icapstrategy\n"), DEBUG_FUNCS);
/* We did nothing lest we did */
bp->b_resid = bp->b_bcount;
/* Do we know you. */
sc = device_lookup_private(&icap_cd, minor(bp->b_dev));
if (sc == NULL) {
DEBUG_PRINT(("icapstrategy: nodev %" PRIx64 "\n",bp->b_dev),
DEBUG_ERRORS);
bp->b_error = ENXIO;
biodone(bp);
return;
}
/* Add to Q. If Q was empty get it started */
s = splbio();
bufq_put(sc->sc_buflist, bp);
if (bufq_peek(sc->sc_buflist) == bp) {
icapstart(sc);
}
splx(s);
}
/*
* Get the next I/O request started
*/
static void
icapstart(struct icap_softc *sc)
{
paddr_t phys, phys2;
vaddr_t virt;
size_t count;
uint32_t fl;
struct buf *bp = sc->sc_bp;
/* Yes, get the next request if any
*/
bp = bufq_get(sc->sc_buflist);
DEBUG_PRINT(("icapnext: %p\n",bp), DEBUG_XFERS);
if (bp == NULL)
return;
}
/* Done with this request?
*/
if ((bp->b_resid == 0) || bp->b_error) {
/* Yes, complete and move to next, if any
*/
sc->sc_bp = NULL;
biodone(bp);
DEBUG_PRINT(("icapdone %p\n",bp), DEBUG_XFERS);
bp = NULL;
goto recheck;
}
/* If new request init the xfer info
*/
if (sc->sc_bp == NULL) {
sc->sc_bp = bp;
sc->sc_data = bp->b_data;
sc->sc_count = bp->b_resid;
}
/* Loop filling as many buffers as will fit in the FIFO
*/
fl = (bp->b_flags & B_READ) ? ICAPS_F_RECV : ICAPS_F_XMIT;
for (;;) {
/* Make sure there's still room in the FIFO, no errors.
*/
if (sc->sc_dp->Control & (ICAPC_IF_FULL|ICAPC_ERROR))
break;
/* How much data do we xfer and where
*/
virt = (vaddr_t)sc->sc_data;
phys = kvtophys(virt);
count = round_page(virt) - virt;
if (count == 0) count = PAGE_SIZE;/* could(will) be aligned */
/* How much of it is contiguous
*/
while (count < sc->sc_count) {
phys2 = kvtophys(virt + count);
if (phys2 != (phys + count)) {
/* No longer contig, ship it
*/
break;
}
count += PAGE_SIZE;
}
/* Trim if we went too far
*/
if (count > sc->sc_count)
count = sc->sc_count;
/* Beware, order matters */
saf = sc->sc_dp->SizeAndFlags;
hi = sc->sc_dp->BufferAddressHi32; /* BUGBUG 64bit */
lo = sc->sc_dp->BufferAddressLo32; /* this pops the fifo */
__USE(hi);
__USE(lo);
/* Say its done that much (and sanity)
*/
if (bp) {
size_t count = saf & ICAPS_S_MASK;
/* more sanity */
if (count > bp->b_resid)
count = bp->b_resid;
bp->b_resid -= count;
}
/* More? */
isr = sc->sc_dp->Control;
}
/* Did we pop at least one */
if (saf)
icapstart(sc);