Use device_t, device_xname(), device_private(). Add atabus_childdetached()

to clean up pointers to detached children.
This commit is contained in:
dyoung 2008-01-10 07:41:20 +00:00
parent 3e5bb8f3c8
commit 1415c1d7aa

View File

@ -1,4 +1,4 @@
/* $NetBSD: ata.c,v 1.94 2008/01/04 21:17:50 ad Exp $ */ /* $NetBSD: ata.c,v 1.95 2008/01/10 07:41:20 dyoung Exp $ */
/* /*
* Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved. * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
@ -30,7 +30,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.94 2008/01/04 21:17:50 ad Exp $"); __KERNEL_RCSID(0, "$NetBSD: ata.c,v 1.95 2008/01/10 07:41:20 dyoung Exp $");
#include "opt_ata.h" #include "opt_ata.h"
@ -107,6 +107,7 @@ const struct cdevsw atabus_cdevsw = {
extern struct cfdriver atabus_cd; extern struct cfdriver atabus_cd;
static void atabus_childdetached(device_t, device_t);
static bool atabus_resume(device_t); static bool atabus_resume(device_t);
static bool atabus_suspend(device_t); static bool atabus_suspend(device_t);
@ -218,7 +219,7 @@ atabusconfig(struct atabus_softc *atabus_sc)
* Fake the autoconfig "not configured" message * Fake the autoconfig "not configured" message
*/ */
aprint_normal("atapibus at %s not configured\n", aprint_normal("atapibus at %s not configured\n",
atac->atac_dev.dv_xname); device_xname(&atac->atac_dev));
chp->atapibus = NULL; chp->atapibus = NULL;
s = splbio(); s = splbio();
for (i = 0; i < chp->ch_ndrive; i++) for (i = 0; i < chp->ch_ndrive; i++)
@ -373,7 +374,7 @@ atabus_thread(void *arg)
* Autoconfiguration match routine. * Autoconfiguration match routine.
*/ */
static int static int
atabus_match(struct device *parent, struct cfdata *cf, void *aux) atabus_match(device_t parent, struct cfdata *cf, void *aux)
{ {
struct ata_channel *chp = aux; struct ata_channel *chp = aux;
@ -393,9 +394,9 @@ atabus_match(struct device *parent, struct cfdata *cf, void *aux)
* Autoconfiguration attach routine. * Autoconfiguration attach routine.
*/ */
static void static void
atabus_attach(struct device *parent, struct device *self, void *aux) atabus_attach(device_t parent, device_t self, void *aux)
{ {
struct atabus_softc *sc = (void *) self; struct atabus_softc *sc = device_private(self);
struct ata_channel *chp = aux; struct ata_channel *chp = aux;
struct atabus_initq *initq; struct atabus_initq *initq;
int error; int error;
@ -414,9 +415,9 @@ atabus_attach(struct device *parent, struct device *self, void *aux)
config_pending_incr(); config_pending_incr();
if ((error = kthread_create(PRI_NONE, 0, NULL, atabus_thread, sc, if ((error = kthread_create(PRI_NONE, 0, NULL, atabus_thread, sc,
&chp->ch_thread, "%s", sc->sc_dev.dv_xname)) != 0) &chp->ch_thread, "%s", device_xname(self))) != 0)
aprint_error("%s: unable to create kernel thread: error %d\n", aprint_error_dev(self,
sc->sc_dev.dv_xname, error); "unable to create kernel thread: error %d\n", error);
if (!pmf_device_register(self, atabus_suspend, atabus_resume)) if (!pmf_device_register(self, atabus_suspend, atabus_resume))
aprint_error_dev(self, "couldn't establish power handler\n"); aprint_error_dev(self, "couldn't establish power handler\n");
@ -428,11 +429,11 @@ atabus_attach(struct device *parent, struct device *self, void *aux)
* Autoconfiguration activation routine. * Autoconfiguration activation routine.
*/ */
static int static int
atabus_activate(struct device *self, enum devact act) atabus_activate(device_t self, enum devact act)
{ {
struct atabus_softc *sc = (void *) self; struct atabus_softc *sc = device_private(self);
struct ata_channel *chp = sc->sc_chan; struct ata_channel *chp = sc->sc_chan;
struct device *dev = NULL; device_t dev = NULL;
int s, i, error = 0; int s, i, error = 0;
s = splbio(); s = splbio();
@ -457,8 +458,8 @@ atabus_activate(struct device *self, enum devact act)
for (i = 0; i < chp->ch_ndrive; i++) { for (i = 0; i < chp->ch_ndrive; i++) {
if ((dev = chp->ch_drive[i].drv_softc) != NULL) { if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
ATADEBUG_PRINT(("atabus_activate: %s: " ATADEBUG_PRINT(("atabus_activate: %s: "
"deactivating %s\n", sc->sc_dev.dv_xname, "deactivating %s\n", device_xname(self),
dev->dv_xname), device_xname(dev)),
DEBUG_DETACH); DEBUG_DETACH);
error = config_deactivate(dev); error = config_deactivate(dev);
if (error) if (error)
@ -473,8 +474,8 @@ atabus_activate(struct device *self, enum devact act)
#ifdef ATADEBUG #ifdef ATADEBUG
if (dev != NULL && error != 0) if (dev != NULL && error != 0)
ATADEBUG_PRINT(("atabus_activate: %s: " ATADEBUG_PRINT(("atabus_activate: %s: "
"error %d deactivating %s\n", sc->sc_dev.dv_xname, "error %d deactivating %s\n", device_xname(self),
error, dev->dv_xname), DEBUG_DETACH); error, device_xname(dev)), DEBUG_DETACH);
#endif /* ATADEBUG */ #endif /* ATADEBUG */
return (error); return (error);
@ -486,61 +487,108 @@ atabus_activate(struct device *self, enum devact act)
* Autoconfiguration detach routine. * Autoconfiguration detach routine.
*/ */
static int static int
atabus_detach(struct device *self, int flags) atabus_detach(device_t self, int flags)
{ {
struct atabus_softc *sc = (void *) self; struct atabus_softc *sc = device_private(self);
struct ata_channel *chp = sc->sc_chan; struct ata_channel *chp = sc->sc_chan;
struct device *dev = NULL; device_t dev = NULL;
int s, i, error = 0; int s, i, error = 0;
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
/* Shutdown the channel. */ /* Shutdown the channel. */
s = splbio(); /* XXX ALSO NEED AN INTERLOCK HERE. */ s = splbio(); /* XXX ALSO NEED AN INTERLOCK HERE. */
chp->ch_flags |= ATACH_SHUTDOWN; chp->ch_flags |= ATACH_SHUTDOWN;
splx(s); splx(s);
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
wakeup(&chp->ch_thread); wakeup(&chp->ch_thread);
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
while (chp->ch_thread != NULL) while (chp->ch_thread != NULL)
(void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0); (void) tsleep(&chp->ch_flags, PRIBIO, "atadown", 0);
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
/* /*
* Detach atapibus and its children. * Detach atapibus and its children.
*/ */
if ((dev = chp->atapibus) != NULL) { if ((dev = chp->atapibus) != NULL) {
ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n", ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
sc->sc_dev.dv_xname, dev->dv_xname), DEBUG_DETACH); device_xname(self), device_xname(dev)), DEBUG_DETACH);
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
error = config_detach(dev, flags); error = config_detach(dev, flags);
if (error) if (error)
goto out; goto out;
} }
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
/*
* Detach our other children.
*/
for (i = 0; i < chp->ch_ndrive; i++) {
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
continue;
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
if ((dev = chp->ch_drive[i].drv_softc) != NULL) {
ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n",
device_xname(self), device_xname(dev)),
DEBUG_DETACH);
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
error = config_detach(dev, flags);
if (error)
goto out;
}
}
out:
aprint_error_dev(self, "%s.%d\n", __func__, __LINE__);
#ifdef ATADEBUG
if (dev != NULL && error != 0)
ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
device_xname(self), error, device_xname(dev)),
DEBUG_DETACH);
#endif /* ATADEBUG */
return (error);
}
void
atabus_childdetached(device_t self, device_t child)
{
struct atabus_softc *sc = device_private(self);
struct ata_channel *chp = sc->sc_chan;
int i;
/*
* atapibus detached.
*/
if (child == chp->atapibus) {
chp->atapibus = NULL;
return;
}
/* /*
* Detach our other children. * Detach our other children.
*/ */
for (i = 0; i < chp->ch_ndrive; i++) { for (i = 0; i < chp->ch_ndrive; i++) {
if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI) if (chp->ch_drive[i].drive_flags & DRIVE_ATAPI)
continue; continue;
if ((dev = chp->ch_drive[i].drv_softc) != NULL) { if (child == chp->ch_drive[i].drv_softc) {
ATADEBUG_PRINT(("atabus_detach: %s: detaching %s\n", chp->ch_drive[i].drv_softc = NULL;
sc->sc_dev.dv_xname, dev->dv_xname), chp->ch_drive[i].drive_flags = 0;
DEBUG_DETACH); return;
error = config_detach(dev, flags);
if (error)
goto out;
} }
} }
out: aprint_error_dev(self, "unknown child %p", (const void *)child);
#ifdef ATADEBUG
if (dev != NULL && error != 0)
ATADEBUG_PRINT(("atabus_detach: %s: error %d detaching %s\n",
sc->sc_dev.dv_xname, error, dev->dv_xname),
DEBUG_DETACH);
#endif /* ATADEBUG */
return (error);
} }
CFATTACH_DECL(atabus, sizeof(struct atabus_softc), CFATTACH_DECL2(atabus, sizeof(struct atabus_softc),
atabus_match, atabus_attach, atabus_detach, atabus_activate); atabus_match, atabus_attach, atabus_detach, atabus_activate, NULL,
atabus_childdetached);
/***************************************************************************** /*****************************************************************************
* Common ATA bus operations. * Common ATA bus operations.
@ -1015,8 +1063,8 @@ ata_print_modes(struct ata_channel *chp)
if ((drvp->drive_flags & DRIVE) == 0 || drvp->drv_softc == NULL) if ((drvp->drive_flags & DRIVE) == 0 || drvp->drv_softc == NULL)
continue; continue;
aprint_verbose("%s(%s:%d:%d): using PIO mode %d", aprint_verbose("%s(%s:%d:%d): using PIO mode %d",
drvp->drv_softc->dv_xname, device_xname(drvp->drv_softc),
atac->atac_dev.dv_xname, device_xname(&atac->atac_dev),
chp->ch_channel, drvp->drive, drvp->PIO_mode); chp->ch_channel, drvp->drive, drvp->PIO_mode);
#if NATA_DMA #if NATA_DMA
if (drvp->drive_flags & DRIVE_DMA) if (drvp->drive_flags & DRIVE_DMA)
@ -1063,7 +1111,7 @@ ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
{ {
struct ata_channel *chp = drvp->chnl_softc; struct ata_channel *chp = drvp->chnl_softc;
struct atac_softc *atac = chp->ch_atac; struct atac_softc *atac = chp->ch_atac;
struct device *drv_dev = drvp->drv_softc; device_t drv_dev = drvp->drv_softc;
int cf_flags = device_cfdata(drv_dev)->cf_flags; int cf_flags = device_cfdata(drv_dev)->cf_flags;
/* if drive or controller don't know its mode, we can't do much */ /* if drive or controller don't know its mode, we can't do much */
@ -1083,7 +1131,7 @@ ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) { if ((drvp->drive_flags & DRIVE_UDMA) && drvp->UDMA_mode >= 2) {
drvp->UDMA_mode--; drvp->UDMA_mode--;
printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n", printf("%s: transfer error, downgrading to Ultra-DMA mode %d\n",
drv_dev->dv_xname, drvp->UDMA_mode); device_xname(drv_dev), drvp->UDMA_mode);
} }
#endif #endif
@ -1094,7 +1142,7 @@ ata_downgrade_mode(struct ata_drive_datas *drvp, int flags)
drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA); drvp->drive_flags &= ~(DRIVE_DMA | DRIVE_UDMA);
drvp->PIO_mode = drvp->PIO_cap; drvp->PIO_mode = drvp->PIO_cap;
printf("%s: transfer error, downgrading to PIO mode %d\n", printf("%s: transfer error, downgrading to PIO mode %d\n",
drv_dev->dv_xname, drvp->PIO_mode); device_xname(drv_dev), drvp->PIO_mode);
} else /* already using PIO, can't downgrade */ } else /* already using PIO, can't downgrade */
return 0; return 0;
@ -1116,7 +1164,7 @@ ata_probe_caps(struct ata_drive_datas *drvp)
struct ataparams params, params2; struct ataparams params, params2;
struct ata_channel *chp = drvp->chnl_softc; struct ata_channel *chp = drvp->chnl_softc;
struct atac_softc *atac = chp->ch_atac; struct atac_softc *atac = chp->ch_atac;
struct device *drv_dev = drvp->drv_softc; device_t drv_dev = drvp->drv_softc;
int i, printed, s; int i, printed, s;
const char *sep = ""; const char *sep = "";
int cf_flags; int cf_flags;
@ -1143,7 +1191,7 @@ ata_probe_caps(struct ata_drive_datas *drvp)
splx(s); splx(s);
} else { } else {
aprint_verbose("%s: 32-bit data port\n", aprint_verbose("%s: 32-bit data port\n",
drv_dev->dv_xname); device_xname(drv_dev));
} }
} }
#if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */ #if 0 /* Some ultra-DMA drives claims to only support ATA-3. sigh */
@ -1152,7 +1200,7 @@ ata_probe_caps(struct ata_drive_datas *drvp)
for (i = 14; i > 0; i--) { for (i = 14; i > 0; i--) {
if (params.atap_ata_major & (1 << i)) { if (params.atap_ata_major & (1 << i)) {
aprint_verbose("%s: ATA version %d\n", aprint_verbose("%s: ATA version %d\n",
drv_dev->dv_xname, i); device_xname(drv_dev), i);
drvp->ata_vers = i; drvp->ata_vers = i;
break; break;
} }
@ -1198,7 +1246,7 @@ ata_probe_caps(struct ata_drive_datas *drvp)
continue; continue;
if (!printed) { if (!printed) {
aprint_verbose("%s: drive supports PIO mode %d", aprint_verbose("%s: drive supports PIO mode %d",
drv_dev->dv_xname, i + 3); device_xname(drv_dev), i + 3);
sep = ","; sep = ",";
printed = 1; printed = 1;
} }
@ -1468,7 +1516,7 @@ atabusioctl(dev_t dev, u_long cmd, void *addr, int flag,
chp->ch_drive[drive].drv_softc, 0); chp->ch_drive[drive].drv_softc, 0);
if (error) if (error)
return (error); return (error);
chp->ch_drive[drive].drv_softc = NULL; KASSERT(chp->ch_drive[drive].drv_softc == NULL);
} }
} }
error = 0; error = 0;