#include <sys/param.h>
#include <sys/device.h>
#include <sys/hotplug.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/mutex.h>
#include <sys/atomic.h>
#include <sys/reboot.h>
#include "hotplug.h"
#include "mpath.h"
extern short cfroots[];
#define ROOT ((struct device *)NULL)
struct matchinfo {
cfmatch_t fn;
struct device *parent;
void *match, *aux;
int indirect, pri;
};
#ifndef AUTOCONF_VERBOSE
#define AUTOCONF_VERBOSE 0
#endif
int autoconf_verbose = AUTOCONF_VERBOSE;
static void mapply(struct matchinfo *, struct cfdata *);
struct deferred_config {
TAILQ_ENTRY(deferred_config) dc_queue;
struct device *dc_dev;
void (*dc_func)(struct device *);
};
TAILQ_HEAD(, deferred_config) deferred_config_queue;
TAILQ_HEAD(, deferred_config) mountroot_config_queue;
void *config_rootsearch(cfmatch_t, char *, void *);
void config_process_deferred_children(struct device *);
struct devicelist alldevs;
volatile int config_pending;
struct mutex autoconf_attdet_mtx = MUTEX_INITIALIZER(IPL_HIGH);
int autoconf_attdet;
unsigned int autoconf_serial = 0;
void
config_init(void)
{
TAILQ_INIT(&deferred_config_queue);
TAILQ_INIT(&mountroot_config_queue);
TAILQ_INIT(&alldevs);
}
void
mapply(struct matchinfo *m, struct cfdata *cf)
{
int pri;
void *match;
if (m->indirect)
match = config_make_softc(m->parent, cf);
else
match = cf;
if (autoconf_verbose) {
printf(">>> probing for %s", cf->cf_driver->cd_name);
if (cf->cf_fstate == FSTATE_STAR)
printf("*\n");
else
printf("%d\n", cf->cf_unit);
}
if (m->fn != NULL)
pri = (*m->fn)(m->parent, match, m->aux);
else {
if (cf->cf_attach->ca_match == NULL) {
panic("mapply: no match function for '%s' device",
cf->cf_driver->cd_name);
}
pri = (*cf->cf_attach->ca_match)(m->parent, match, m->aux);
}
if (autoconf_verbose)
printf(">>> %s probe returned %d\n", cf->cf_driver->cd_name,
pri);
if (pri > m->pri) {
if (m->indirect && m->match) {
cf = ((struct device *)m->match)->dv_cfdata;
free(m->match, M_DEVBUF, cf->cf_attach->ca_devsize);
}
m->match = match;
m->pri = pri;
} else {
if (m->indirect)
free(match, M_DEVBUF, cf->cf_attach->ca_devsize);
}
}
void *
config_search(cfmatch_t fn, struct device *parent, void *aux)
{
struct cfdata *cf;
short *p;
struct matchinfo m;
m.fn = fn;
m.parent = parent;
m.match = NULL;
m.aux = aux;
m.indirect = parent && (parent->dv_cfdata->cf_driver->cd_mode & CD_INDIRECT);
m.pri = 0;
for (cf = cfdata; cf->cf_driver; cf++) {
if (cf->cf_fstate == FSTATE_FOUND)
continue;
if (cf->cf_fstate == FSTATE_DNOTFOUND ||
cf->cf_fstate == FSTATE_DSTAR)
continue;
if (boothowto & RB_UNHIBERNATE) {
if (cf->cf_driver->cd_mode & CD_SKIPHIBERNATE)
continue;
if (cf->cf_driver->cd_class == DV_IFNET)
continue;
if (cf->cf_driver->cd_class == DV_TAPE)
continue;
}
if (ISSET(boothowto, RB_COCOVM) &&
!ISSET(cf->cf_driver->cd_mode, CD_COCOVM))
continue;
for (p = cf->cf_parents; *p >= 0; p++)
if (parent->dv_cfdata == &cfdata[*p])
mapply(&m, cf);
}
if (autoconf_verbose) {
if (m.match) {
if (m.indirect)
cf = ((struct device *)m.match)->dv_cfdata;
else
cf = (struct cfdata *)m.match;
printf(">>> %s probe won\n",
cf->cf_driver->cd_name);
} else
printf(">>> no winning probe\n");
}
return (m.match);
}
void
config_scan(cfscan_t fn, struct device *parent)
{
struct cfdata *cf;
short *p;
void *match;
int indirect;
indirect = parent && (parent->dv_cfdata->cf_driver->cd_mode & CD_INDIRECT);
for (cf = cfdata; cf->cf_driver; cf++) {
if (cf->cf_fstate == FSTATE_FOUND)
continue;
if (cf->cf_fstate == FSTATE_DNOTFOUND ||
cf->cf_fstate == FSTATE_DSTAR)
continue;
for (p = cf->cf_parents; *p >= 0; p++)
if (parent->dv_cfdata == &cfdata[*p]) {
match = indirect?
config_make_softc(parent, cf) :
(void *)cf;
(*fn)(parent, match);
}
}
}
void *
config_rootsearch(cfmatch_t fn, char *rootname, void *aux)
{
struct cfdata *cf;
short *p;
struct matchinfo m;
m.fn = fn;
m.parent = ROOT;
m.match = NULL;
m.aux = aux;
m.indirect = 0;
m.pri = 0;
for (p = cfroots; *p >= 0; p++) {
cf = &cfdata[*p];
if (cf->cf_fstate == FSTATE_DNOTFOUND ||
cf->cf_fstate == FSTATE_DSTAR)
continue;
if (strcmp(cf->cf_driver->cd_name, rootname) == 0)
mapply(&m, cf);
}
return (m.match);
}
const char *msgs[3] = { "", " not configured\n", " unsupported\n" };
struct device *
config_found_sm(struct device *parent, void *aux, cfprint_t print,
cfmatch_t submatch)
{
void *match;
if ((match = config_search(submatch, parent, aux)) != NULL)
return (config_attach(parent, match, aux, print));
if (print)
printf("%s", msgs[(*print)(aux, parent->dv_xname)]);
return (NULL);
}
struct device *
config_rootfound(char *rootname, void *aux)
{
void *match;
if ((match = config_rootsearch((cfmatch_t)NULL, rootname, aux)) != NULL)
return (config_attach(ROOT, match, aux, (cfprint_t)NULL));
printf("root device %s not configured\n", rootname);
return (NULL);
}
struct device *
config_attach(struct device *parent, void *match, void *aux, cfprint_t print)
{
struct cfdata *cf;
struct device *dev;
struct cfdriver *cd;
const struct cfattach *ca;
mtx_enter(&autoconf_attdet_mtx);
while (autoconf_attdet < 0)
msleep_nsec(&autoconf_attdet, &autoconf_attdet_mtx,
PWAIT, "autoconf", INFSLP);
autoconf_attdet++;
mtx_leave(&autoconf_attdet_mtx);
if (parent && (parent->dv_cfdata->cf_driver->cd_mode & CD_INDIRECT)) {
dev = match;
cf = dev->dv_cfdata;
} else {
cf = match;
dev = config_make_softc(parent, cf);
}
cd = cf->cf_driver;
ca = cf->cf_attach;
KASSERT(cd->cd_devs != NULL);
KASSERT(dev->dv_unit < cd->cd_ndevs);
KASSERT(cd->cd_devs[dev->dv_unit] == NULL);
cd->cd_devs[dev->dv_unit] = dev;
if (cf->cf_fstate == FSTATE_STAR) {
if (dev->dv_unit == cf->cf_unit)
cf->cf_unit++;
} else
cf->cf_fstate = FSTATE_FOUND;
TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);
device_ref(dev);
if (parent == ROOT)
printf("%s at root", dev->dv_xname);
else {
printf("%s at %s", dev->dv_xname, parent->dv_xname);
if (print)
(void) (*print)(aux, NULL);
}
for (cf = cfdata; cf->cf_driver; cf++) {
if (cf->cf_driver == cd &&
cf->cf_unit == dev->dv_unit) {
if (cf->cf_fstate == FSTATE_NOTFOUND)
cf->cf_fstate = FSTATE_FOUND;
if (cf->cf_fstate == FSTATE_STAR)
cf->cf_unit++;
}
}
device_register(dev, aux);
(*ca->ca_attach)(parent, dev, aux);
config_process_deferred_children(dev);
#if NHOTPLUG > 0
if (!cold)
hotplug_device_attach(cd->cd_class, dev->dv_xname);
#endif
mtx_enter(&autoconf_attdet_mtx);
if (--autoconf_attdet == 0)
wakeup(&autoconf_attdet);
autoconf_serial++;
mtx_leave(&autoconf_attdet_mtx);
return (dev);
}
struct device *
config_make_softc(struct device *parent, struct cfdata *cf)
{
struct device *dev;
struct cfdriver *cd;
const struct cfattach *ca;
cd = cf->cf_driver;
ca = cf->cf_attach;
if (ca->ca_devsize < sizeof(struct device))
panic("config_make_softc");
dev = malloc(ca->ca_devsize, M_DEVBUF, M_NOWAIT|M_ZERO);
if (dev == NULL)
panic("config_make_softc: allocation for device softc failed");
dev->dv_class = cd->cd_class;
dev->dv_cfdata = cf;
dev->dv_flags = DVF_ACTIVE;
if (cf->cf_fstate == FSTATE_STAR) {
for (dev->dv_unit = cf->cf_starunit1;
dev->dv_unit < cf->cf_unit; dev->dv_unit++)
if (cd->cd_ndevs == 0 ||
dev->dv_unit >= cd->cd_ndevs ||
cd->cd_devs[dev->dv_unit] == NULL)
break;
} else
dev->dv_unit = cf->cf_unit;
if (snprintf(dev->dv_xname, sizeof(dev->dv_xname), "%s%d",
cd->cd_name, dev->dv_unit) >= sizeof(dev->dv_xname))
panic("config_make_softc: device name too long");
dev->dv_parent = parent;
if (dev->dv_unit >= cd->cd_ndevs) {
int old = cd->cd_ndevs, new;
void **nsp;
if (old == 0)
new = MINALLOCSIZE / sizeof(void *);
else
new = old * 2;
while (new <= dev->dv_unit)
new *= 2;
cd->cd_ndevs = new;
nsp = mallocarray(new, sizeof(void *), M_DEVBUF, M_NOWAIT|M_ZERO);
if (nsp == NULL)
panic("config_make_softc: %sing dev array",
old != 0 ? "expand" : "creat");
if (old != 0) {
bcopy(cd->cd_devs, nsp, old * sizeof(void *));
free(cd->cd_devs, M_DEVBUF, old * sizeof(void *));
}
cd->cd_devs = nsp;
}
if (cd->cd_devs[dev->dv_unit])
panic("config_make_softc: duplicate %s", dev->dv_xname);
dev->dv_ref = 1;
return (dev);
}
int
config_detach(struct device *dev, int flags)
{
struct cfdata *cf;
const struct cfattach *ca;
struct cfdriver *cd;
int rv = 0, i;
#ifdef DIAGNOSTIC
struct device *d;
#endif
#if NHOTPLUG > 0
char devname[16];
#endif
mtx_enter(&autoconf_attdet_mtx);
while (autoconf_attdet > 0)
msleep_nsec(&autoconf_attdet, &autoconf_attdet_mtx,
PWAIT, "autoconf", INFSLP);
autoconf_attdet--;
mtx_leave(&autoconf_attdet_mtx);
#if NHOTPLUG > 0
strlcpy(devname, dev->dv_xname, sizeof(devname));
#endif
cf = dev->dv_cfdata;
#ifdef DIAGNOSTIC
if (cf->cf_fstate != FSTATE_FOUND && cf->cf_fstate != FSTATE_STAR)
panic("config_detach: bad device fstate");
#endif
ca = cf->cf_attach;
cd = cf->cf_driver;
rv = config_deactivate(dev);
if (rv == 0) {
if (ca->ca_detach != NULL)
rv = (*ca->ca_detach)(dev, flags);
else
rv = EOPNOTSUPP;
}
if (rv != 0) {
if ((flags & DETACH_FORCE) == 0)
goto done;
else
panic("config_detach: forced detach of %s failed (%d)",
dev->dv_xname, rv);
}
#ifdef DIAGNOSTIC
i = 0;
for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
d = TAILQ_NEXT(d, dv_list)) {
if (d->dv_parent == dev) {
printf("config_detach: %s attached at %s\n",
d->dv_xname, dev->dv_xname);
i = 1;
}
}
if (i != 0)
panic("config_detach: detached device (%s) has children",
dev->dv_xname);
#endif
for (cf = cfdata; cf->cf_driver; cf++) {
if (cf->cf_driver == cd) {
if (cf->cf_fstate == FSTATE_FOUND &&
cf->cf_unit == dev->dv_unit)
cf->cf_fstate = FSTATE_NOTFOUND;
if (cf->cf_fstate == FSTATE_STAR &&
cf->cf_unit == dev->dv_unit + 1)
cf->cf_unit--;
}
}
TAILQ_REMOVE(&alldevs, dev, dv_list);
device_unref(dev);
cd->cd_devs[dev->dv_unit] = NULL;
if ((flags & DETACH_QUIET) == 0)
printf("%s detached\n", dev->dv_xname);
device_unref(dev);
for (i = 0; i < cd->cd_ndevs; i++)
if (cd->cd_devs[i] != NULL)
break;
if (i == cd->cd_ndevs) {
free(cd->cd_devs, M_DEVBUF, cd->cd_ndevs * sizeof(void *));
cd->cd_devs = NULL;
cd->cd_ndevs = 0;
cf->cf_unit = 0;
}
#if NHOTPLUG > 0
if (!cold)
hotplug_device_detach(cd->cd_class, devname);
#endif
done:
mtx_enter(&autoconf_attdet_mtx);
if (++autoconf_attdet == 0)
wakeup(&autoconf_attdet);
autoconf_serial++;
mtx_leave(&autoconf_attdet_mtx);
return (rv);
}
int
config_deactivate(struct device *dev)
{
int rv = 0, oflags = dev->dv_flags;
if (dev->dv_flags & DVF_ACTIVE) {
dev->dv_flags &= ~DVF_ACTIVE;
rv = config_suspend(dev, DVACT_DEACTIVATE);
if (rv)
dev->dv_flags = oflags;
}
return (rv);
}
void
config_defer(struct device *dev, void (*func)(struct device *))
{
struct deferred_config *dc;
if (dev->dv_parent == NULL)
panic("config_defer: can't defer config of a root device");
#ifdef DIAGNOSTIC
for (dc = TAILQ_FIRST(&deferred_config_queue); dc != NULL;
dc = TAILQ_NEXT(dc, dc_queue)) {
if (dc->dc_dev == dev)
panic("config_defer: deferred twice");
}
#endif
if ((dc = malloc(sizeof(*dc), M_DEVBUF, M_NOWAIT)) == NULL)
panic("config_defer: can't allocate defer structure");
dc->dc_dev = dev;
dc->dc_func = func;
TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
config_pending_incr();
}
void
config_mountroot(struct device *dev, void (*func)(struct device *))
{
struct deferred_config *dc;
if (rootvp != NULL) {
(*func)(dev);
return;
}
#ifdef DIAGNOSTIC
for (dc = TAILQ_FIRST(&mountroot_config_queue); dc != NULL;
dc = TAILQ_NEXT(dc, dc_queue)) {
if (dc->dc_dev == dev)
panic("config_mountroot: deferred twice");
}
#endif
if ((dc = malloc(sizeof(*dc), M_DEVBUF, M_NOWAIT)) == NULL)
panic("config_mountroot: can't allocate defer structure");
dc->dc_dev = dev;
dc->dc_func = func;
TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue);
}
void
config_process_deferred_children(struct device *parent)
{
struct deferred_config *dc, *ndc;
for (dc = TAILQ_FIRST(&deferred_config_queue);
dc != NULL; dc = ndc) {
ndc = TAILQ_NEXT(dc, dc_queue);
if (dc->dc_dev->dv_parent == parent) {
TAILQ_REMOVE(&deferred_config_queue, dc, dc_queue);
(*dc->dc_func)(dc->dc_dev);
free(dc, M_DEVBUF, sizeof(*dc));
config_pending_decr();
}
}
}
void
config_process_deferred_mountroot(void)
{
struct deferred_config *dc;
while ((dc = TAILQ_FIRST(&mountroot_config_queue)) != NULL) {
TAILQ_REMOVE(&mountroot_config_queue, dc, dc_queue);
(*dc->dc_func)(dc->dc_dev);
free(dc, M_DEVBUF, sizeof(*dc));
}
}
void
config_pending_incr(void)
{
config_pending++;
}
void
config_pending_decr(void)
{
#ifdef DIAGNOSTIC
if (config_pending == 0)
panic("config_pending_decr: config_pending == 0");
#endif
config_pending--;
if (config_pending == 0)
wakeup((void *)&config_pending);
}
int
config_detach_children(struct device *parent, int flags)
{
struct device *dev, *next_dev;
int rv = 0;
for (dev = TAILQ_LAST(&alldevs, devicelist);
dev != NULL; dev = next_dev) {
if (dev->dv_parent == parent) {
if ((rv = config_detach(dev, flags)) != 0)
return (rv);
next_dev = TAILQ_LAST(&alldevs, devicelist);
} else {
next_dev = TAILQ_PREV(dev, devicelist, dv_list);
}
}
return (0);
}
int
config_suspend(struct device *dev, int act)
{
const struct cfattach *ca = dev->dv_cfdata->cf_attach;
int r;
device_ref(dev);
if (ca->ca_activate)
r = (*ca->ca_activate)(dev, act);
else
r = config_activate_children(dev, act);
device_unref(dev);
return (r);
}
int
config_suspend_all(int act)
{
struct device *mainbus = device_mainbus();
struct device *mpath = device_mpath();
int rv = 0;
switch (act) {
case DVACT_QUIESCE:
case DVACT_SUSPEND:
case DVACT_POWERDOWN:
if (mpath) {
rv = config_suspend(mpath, act);
if (rv)
return rv;
}
if (mainbus)
rv = config_suspend(mainbus, act);
break;
case DVACT_RESUME:
case DVACT_WAKEUP:
if (mainbus) {
rv = config_suspend(mainbus, act);
if (rv)
return rv;
}
if (mpath)
rv = config_suspend(mpath, act);
break;
}
return (rv);
}
int
config_activate_children(struct device *parent, int act)
{
struct device *d;
int rv = 0;
for (d = TAILQ_NEXT(parent, dv_list); d != NULL;
d = TAILQ_NEXT(d, dv_list)) {
if (d->dv_parent != parent)
continue;
switch (act) {
case DVACT_QUIESCE:
case DVACT_SUSPEND:
case DVACT_RESUME:
case DVACT_WAKEUP:
case DVACT_POWERDOWN:
rv = config_suspend(d, act);
break;
case DVACT_DEACTIVATE:
rv = config_deactivate(d);
break;
}
if (rv == 0)
continue;
#ifdef DIAGNOSTIC
printf("config_activate_children: device %s failed %d\n",
d->dv_xname, act);
#endif
if (act == DVACT_RESUME)
printf("failing resume cannot be handled\n");
if (act == DVACT_POWERDOWN)
return (rv);
if (act != DVACT_SUSPEND)
return (rv);
d = TAILQ_PREV(d, devicelist, dv_list);
for (; d != NULL && d != parent;
d = TAILQ_PREV(d, devicelist, dv_list)) {
if (d->dv_parent != parent)
continue;
printf("resume %s\n", d->dv_xname);
config_suspend(d, DVACT_RESUME);
}
return (rv);
}
return (rv);
}
struct device *
device_lookup(struct cfdriver *cd, int unit)
{
struct device *dv = NULL;
if (unit >= 0 && unit < cd->cd_ndevs)
dv = (struct device *)(cd->cd_devs[unit]);
if (!dv)
return (NULL);
if (!(dv->dv_flags & DVF_ACTIVE))
dv = NULL;
if (dv != NULL)
device_ref(dv);
return (dv);
}
struct device *
device_mainbus(void)
{
extern struct cfdriver mainbus_cd;
if (mainbus_cd.cd_ndevs < 1)
return (NULL);
return (mainbus_cd.cd_devs[0]);
}
struct device *
device_mpath(void)
{
#if NMPATH > 0
extern struct cfdriver mpath_cd;
if (mpath_cd.cd_ndevs < 1)
return (NULL);
return (mpath_cd.cd_devs[0]);
#else
return (NULL);
#endif
}
void
device_ref(struct device *dv)
{
atomic_inc_int(&dv->dv_ref);
}
void
device_unref(struct device *dv)
{
const struct cfattach *ca;
if (atomic_dec_int_nv(&dv->dv_ref) == 0) {
ca = dv->dv_cfdata->cf_attach;
free(dv, M_DEVBUF, ca->ca_devsize);
}
}