/*-
* Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran and Charles M. Hannum.
*
* 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.
*/
/* Register with PMF */
if (!pmf_device_register1(dksc->sc_dev, ld_suspend, ld_resume,
ld_shutdown))
aprint_error_dev(dksc->sc_dev,
"couldn't establish power handler\n");
/* Discover wedges on this disk. */
config_interrupts(sc->sc_dv, ld_config_interrupts);
}
int
ldbegindetach(struct ld_softc *sc, int flags)
{
struct dk_softc *dksc = &sc->sc_dksc;
int error;
/* If we never attached properly, no problem with detaching. */
if ((sc->sc_flags & LDF_ENABLED) == 0)
return 0;
/*
* If the disk is still open, back out before we commit to
* detaching.
*/
error = disk_begindetach(&dksc->sc_dkdev, ld_lastclose, dksc->sc_dev,
flags);
if (error)
return error;
/* We are now committed to detaching. Prevent new xfers. */
ldadjqparam(sc, 0);
return 0;
}
void
ldenddetach(struct ld_softc *sc)
{
struct dk_softc *dksc = &sc->sc_dksc;
int bmaj, cmaj, i, mn;
if ((sc->sc_flags & LDF_ENABLED) == 0)
return;
/* Wait for commands queued with the hardware to complete. */
mutex_enter(&sc->sc_mutex);
while (sc->sc_queuecnt > 0) {
if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz)) {
/*
* XXX This seems like a recipe for crashing on
* use after free...
*/
printf("%s: not drained\n", dksc->sc_xname);
break;
}
}
mutex_exit(&sc->sc_mutex);
/* Kill off any queued buffers. */
dk_drain(dksc);
bufq_free(dksc->sc_bufq);
/* Locate the major numbers. */
bmaj = bdevsw_lookup_major(&ld_bdevsw);
cmaj = cdevsw_lookup_major(&ld_cdevsw);
/* Nuke the vnodes for any open instances. */
for (i = 0; i < MAXPARTITIONS; i++) {
mn = DISKMINOR(device_unit(dksc->sc_dev), i);
vdevgone(bmaj, mn, mn, VBLK);
vdevgone(cmaj, mn, mn, VCHR);
}
/* Delete all of our wedges. */
dkwedge_delall(&dksc->sc_dkdev);
/* Detach from the disk list. */
disk_detach(&dksc->sc_dkdev);
disk_destroy(&dksc->sc_dkdev);
dk_detach(dksc);
/* Deregister with PMF */
pmf_device_deregister(dksc->sc_dev);
/*
* XXX We can't really flush the cache here, because the
* XXX device may already be non-existent from the controller's
* XXX perspective.
*/
#if 0
ld_flush(dksc->sc_dev, false);
#endif
cv_destroy(&sc->sc_drain);
mutex_destroy(&sc->sc_mutex);
}
/* Block new requests and wait for outstanding requests to drain. */
mutex_enter(&sc->sc_mutex);
KASSERT((sc->sc_flags & LDF_SUSPEND) == 0);
sc->sc_flags |= LDF_SUSPEND;
while ((queuecnt = sc->sc_queuecnt) > 0) {
if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
break;
}
mutex_exit(&sc->sc_mutex);
/* Block suspend if we couldn't drain everything in 30sec. */
if (queuecnt > 0) {
device_printf(dev, "timeout draining buffers\n");
goto out;
}
/* Flush cache before we lose power. If we can't, block suspend. */
if (ld_flush(dev, /*poll*/false) != 0) {
device_printf(dev, "failed to flush cache\n");
goto out;
}
/* Success! */
ok = true;
out: if (!ok)
(void)ld_resume(dev, qual);
return ok;
}
/* Allow new requests to come in. */
mutex_enter(&sc->sc_mutex);
KASSERT(sc->sc_flags & LDF_SUSPEND);
sc->sc_flags &= ~LDF_SUSPEND;
mutex_exit(&sc->sc_mutex);
/* Restart any pending queued requests. */
dk_start(&sc->sc_dksc, NULL);
/* ARGSUSED */
static int
ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
{
struct ld_softc *sc;
struct dk_softc *dksc;
int unit, error;
unit = DISKUNIT(dev);
sc = device_lookup_private(&ld_cd, unit);
dksc = &sc->sc_dksc;
error = 0;
/*
* Some common checks so that individual attachments wouldn't need
* to duplicate them.
*/
switch (cmd) {
case DIOCCACHESYNC:
/*
* XXX Do we really need to care about having a writable
* file descriptor here?
*/
if ((flag & FWRITE) == 0)
error = EBADF;
else
error = 0;
break;
}
if (error != 0)
return (error);
if (sc->sc_ioctl) {
if ((sc->sc_flags & LDF_MPSAFE) == 0)
KERNEL_LOCK(1, curlwp);
error = (*sc->sc_ioctl)(sc, cmd, addr, flag, 0);
if ((sc->sc_flags & LDF_MPSAFE) == 0)
KERNEL_UNLOCK_ONE(curlwp);
if (error != EPASSTHROUGH)
return (error);
}
/* something not handled by the attachment */
return dk_ioctl(dksc, dev, cmd, addr, flag, l);
}
/*
* Flush the device's cache.
*/
static int
ld_flush(device_t self, bool poll)
{
int error = 0;
struct ld_softc *sc = device_private(self);
if (sc->sc_ioctl) {
if ((sc->sc_flags & LDF_MPSAFE) == 0)
KERNEL_LOCK(1, curlwp);
error = (*sc->sc_ioctl)(sc, DIOCCACHESYNC, NULL, 0, poll);
if ((sc->sc_flags & LDF_MPSAFE) == 0)
KERNEL_UNLOCK_ONE(curlwp);
if (error != 0)
device_printf(self, "unable to flush cache\n");
}