2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <signal.h>
2N/A#include <dirent.h>
2N/A#include <limits.h>
2N/A#include <alloca.h>
2N/A#include <unistd.h>
2N/A#include <stdio.h>
2N/A#include <pthread.h>
2N/A#include <synch.h>
2N/A#include <errno.h>
2N/A#include <strings.h>
2N/A#include <assert.h>
2N/A#include <sys/nvpair.h>
2N/A
2N/A#include <topo_string.h>
2N/A#include <topo_alloc.h>
2N/A#include <topo_module.h>
2N/A#include <topo_error.h>
2N/A#include <topo_subr.h>
2N/A
2N/Avoid
2N/Atopo_mod_release(topo_mod_t *mod, tnode_t *node)
2N/A{
2N/A topo_mod_enter(mod);
2N/A
2N/A if (mod->tm_info->tmi_ops->tmo_release != NULL)
2N/A mod->tm_info->tmi_ops->tmo_release(mod, node);
2N/A
2N/A topo_mod_exit(mod);
2N/A}
2N/A
2N/Avoid
2N/Atopo_mod_hold(topo_mod_t *mod)
2N/A{
2N/A (void) pthread_mutex_lock(&mod->tm_lock);
2N/A mod->tm_refs++;
2N/A assert(mod->tm_refs != 0);
2N/A (void) pthread_mutex_unlock(&mod->tm_lock);
2N/A}
2N/A
2N/Avoid
2N/Atopo_mod_rele(topo_mod_t *mod)
2N/A{
2N/A assert(mod->tm_refs != 0);
2N/A
2N/A (void) pthread_mutex_lock(&mod->tm_lock);
2N/A
2N/A /*
2N/A * Lazy unload module
2N/A */
2N/A if (--mod->tm_refs == 0)
2N/A topo_modhash_unload(mod);
2N/A else
2N/A (void) pthread_mutex_unlock(&mod->tm_lock);
2N/A}
2N/A
2N/Avoid
2N/Atopo_mod_enter(topo_mod_t *mod)
2N/A{
2N/A (void) pthread_mutex_lock(&mod->tm_lock);
2N/A
2N/A while (mod->tm_busy != 0)
2N/A (void) pthread_cond_wait(&mod->tm_cv, &mod->tm_lock);
2N/A
2N/A ++mod->tm_busy;
2N/A
2N/A (void) pthread_mutex_unlock(&mod->tm_lock);
2N/A}
2N/A
2N/Avoid
2N/Atopo_mod_exit(topo_mod_t *mod)
2N/A{
2N/A (void) pthread_mutex_lock(&mod->tm_lock);
2N/A --mod->tm_busy;
2N/A
2N/A assert(mod->tm_busy == 0);
2N/A
2N/A (void) pthread_cond_broadcast(&mod->tm_cv);
2N/A (void) pthread_mutex_unlock(&mod->tm_lock);
2N/A}
2N/A
2N/Astatic void
2N/Atopo_modhash_lock(topo_modhash_t *mhp)
2N/A{
2N/A (void) pthread_mutex_lock(&mhp->mh_lock);
2N/A}
2N/A
2N/Astatic void
2N/Atopo_modhash_unlock(topo_modhash_t *mhp)
2N/A{
2N/A (void) pthread_mutex_unlock(&mhp->mh_lock);
2N/A}
2N/A
2N/Astatic void
2N/Atopo_mod_stop(topo_mod_t *mod)
2N/A{
2N/A if (mod->tm_flags & TOPO_MOD_INIT) {
2N/A mod->tm_mops->mop_fini(mod);
2N/A if (mod->tm_flags & TOPO_MOD_REG)
2N/A topo_mod_unregister(mod);
2N/A }
2N/A
2N/A mod->tm_flags = TOPO_MOD_FINI;
2N/A
2N/A topo_dprintf(mod->tm_hdl, TOPO_DBG_MODSVC,
2N/A "module %s stopped\n", mod->tm_name);
2N/A}
2N/A
2N/Astatic int
2N/Atopo_mod_start(topo_mod_t *mod, topo_version_t version)
2N/A{
2N/A topo_dprintf(mod->tm_hdl, TOPO_DBG_MODSVC,
2N/A "starting module %s\n", mod->tm_name);
2N/A
2N/A if (mod->tm_mops->mop_init(mod, version) != 0) {
2N/A if (mod->tm_errno == 0)
2N/A mod->tm_errno = ETOPO_MOD_INIT;
2N/A topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
2N/A "module %s failed to initialize: %s\n", mod->tm_name,
2N/A topo_strerror(mod->tm_errno));
2N/A return (-1);
2N/A }
2N/A
2N/A mod->tm_flags |= TOPO_MOD_INIT;
2N/A
2N/A if (!(mod->tm_flags & TOPO_MOD_REG)) {
2N/A topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
2N/A "module %s failed to register\n", mod->tm_name);
2N/A mod->tm_errno = ETOPO_MOD_NOREG;
2N/A topo_mod_stop(mod);
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Atopo_mod_t *
2N/Atopo_mod_lookup(topo_hdl_t *thp, const char *name, int bump)
2N/A{
2N/A topo_mod_t *mod;
2N/A topo_modhash_t *mhp = thp->th_modhash;
2N/A
2N/A topo_modhash_lock(mhp);
2N/A mod = topo_modhash_lookup(mhp, name);
2N/A if (mod != NULL && bump != 0)
2N/A topo_mod_hold(mod);
2N/A topo_modhash_unlock(mhp);
2N/A
2N/A return (mod);
2N/A}
2N/A
2N/Astatic void
2N/Atopo_mod_destroy(topo_mod_t *mod)
2N/A{
2N/A topo_hdl_t *thp = mod->tm_hdl;
2N/A
2N/A if (mod == NULL)
2N/A return;
2N/A
2N/A assert(mod->tm_refs == 0);
2N/A assert(!MUTEX_HELD(&mod->tm_lock));
2N/A
2N/A if (mod->tm_name != NULL)
2N/A topo_hdl_strfree(thp, mod->tm_name);
2N/A if (mod->tm_path != NULL)
2N/A topo_hdl_strfree(thp, mod->tm_path);
2N/A if (mod->tm_rootdir != NULL)
2N/A topo_hdl_strfree(thp, mod->tm_rootdir);
2N/A
2N/A topo_hdl_free(thp, mod, sizeof (topo_mod_t));
2N/A}
2N/A
2N/Astatic topo_mod_t *
2N/Aset_create_error(topo_hdl_t *thp, topo_mod_t *mod, const char *path, int err)
2N/A{
2N/A if (path != NULL)
2N/A topo_dprintf(thp, TOPO_DBG_ERR, "unable to load module %s: "
2N/A "%s\n", path, topo_strerror(err));
2N/A else
2N/A topo_dprintf(thp, TOPO_DBG_ERR, "unable to load module: "
2N/A "%s\n", topo_strerror(err));
2N/A
2N/A if (mod != NULL)
2N/A topo_mod_destroy(mod);
2N/A
2N/A (void) topo_hdl_seterrno(thp, err);
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic topo_mod_t *
2N/Atopo_mod_create(topo_hdl_t *thp, const char *name, const char *path,
2N/A const topo_imodops_t *ops, topo_version_t version)
2N/A{
2N/A topo_mod_t *mod;
2N/A
2N/A if (topo_modhash_lookup(thp->th_modhash, name) != NULL)
2N/A return (set_create_error(thp, NULL, path, ETOPO_MOD_LOADED));
2N/A
2N/A if ((mod = topo_hdl_zalloc(thp, sizeof (topo_mod_t))) == NULL)
2N/A return (set_create_error(thp, mod, path, ETOPO_NOMEM));
2N/A
2N/A mod->tm_hdl = thp;
2N/A
2N/A (void) pthread_mutex_init(&mod->tm_lock, NULL);
2N/A
2N/A mod->tm_name = topo_hdl_strdup(thp, name);
2N/A if (path != NULL)
2N/A mod->tm_path = topo_hdl_strdup(thp, path);
2N/A mod->tm_rootdir = topo_hdl_strdup(thp, thp->th_rootdir);
2N/A if (mod->tm_name == NULL || mod->tm_rootdir == NULL)
2N/A return (set_create_error(thp, mod, path, ETOPO_NOMEM));
2N/A
2N/A mod->tm_mops = (topo_imodops_t *)ops;
2N/A mod->tm_alloc = thp->th_alloc;
2N/A
2N/A /*
2N/A * Module will be held upon a successful return from topo_mod_start()
2N/A */
2N/A if ((topo_mod_start(mod, version)) < 0)
2N/A return (set_create_error(thp, mod, path, mod->tm_errno));
2N/A
2N/A topo_dprintf(thp, TOPO_DBG_MODSVC, "loaded module %s\n", mod->tm_name);
2N/A
2N/A return (mod);
2N/A}
2N/A
2N/Atopo_modhash_t *
2N/Atopo_modhash_create(topo_hdl_t *thp)
2N/A{
2N/A topo_modhash_t *mhp;
2N/A
2N/A if ((mhp = topo_hdl_zalloc(thp, sizeof (topo_modhash_t))) == NULL)
2N/A return (NULL);
2N/A
2N/A mhp->mh_hashlen = TOPO_HASH_BUCKETS;
2N/A if ((mhp->mh_hash = topo_hdl_zalloc(thp,
2N/A sizeof (void *) * mhp->mh_hashlen)) == NULL) {
2N/A topo_hdl_free(thp, mhp, sizeof (topo_modhash_t));
2N/A return (NULL);
2N/A }
2N/A mhp->mh_nelems = 0;
2N/A (void) pthread_mutex_init(&mhp->mh_lock, NULL);
2N/A
2N/A thp->th_modhash = mhp;
2N/A
2N/A return (mhp);
2N/A}
2N/A
2N/Avoid
2N/Atopo_modhash_destroy(topo_hdl_t *thp)
2N/A{
2N/A topo_modhash_t *mhp = thp->th_modhash;
2N/A
2N/A if (mhp == NULL)
2N/A return;
2N/A
2N/A assert(mhp->mh_nelems == 0);
2N/A
2N/A topo_hdl_free(thp, mhp->mh_hash, sizeof (void *) * mhp->mh_hashlen);
2N/A topo_hdl_free(thp, mhp, sizeof (topo_modhash_t));
2N/A thp->th_modhash = NULL;
2N/A}
2N/A
2N/Atopo_mod_t *
2N/Atopo_modhash_lookup(topo_modhash_t *mhp, const char *name)
2N/A{
2N/A topo_mod_t *mod = NULL;
2N/A uint_t h;
2N/A
2N/A h = topo_strhash(name) % mhp->mh_hashlen;
2N/A
2N/A for (mod = mhp->mh_hash[h]; mod != NULL; mod = mod->tm_next) {
2N/A if (strcmp(name, mod->tm_name) == 0)
2N/A break;
2N/A }
2N/A
2N/A return (mod);
2N/A}
2N/A
2N/Atopo_mod_t *
2N/Atopo_modhash_load(topo_hdl_t *thp, const char *name, const char *path,
2N/A const topo_imodops_t *ops, topo_version_t version)
2N/A{
2N/A topo_modhash_t *mhp = thp->th_modhash;
2N/A topo_mod_t *mod;
2N/A uint_t h;
2N/A
2N/A topo_modhash_lock(mhp);
2N/A
2N/A if ((mod = topo_mod_create(thp, name, path, ops, version)) == NULL) {
2N/A topo_modhash_unlock(mhp);
2N/A return (NULL); /* th_errno set */
2N/A }
2N/A
2N/A topo_mod_hold(mod);
2N/A
2N/A h = topo_strhash(name) % mhp->mh_hashlen;
2N/A mod->tm_next = mhp->mh_hash[h];
2N/A mhp->mh_hash[h] = mod;
2N/A mhp->mh_nelems++;
2N/A topo_modhash_unlock(mhp);
2N/A
2N/A return (mod);
2N/A}
2N/A
2N/Avoid
2N/Atopo_modhash_unload(topo_mod_t *mod)
2N/A{
2N/A uint_t h;
2N/A topo_mod_t **pp, *mp;
2N/A topo_hdl_t *thp = mod->tm_hdl;
2N/A topo_modhash_t *mhp;
2N/A
2N/A assert(MUTEX_HELD(&mod->tm_lock));
2N/A assert(mod->tm_busy == 0);
2N/A
2N/A mhp = thp->th_modhash;
2N/A topo_modhash_lock(mhp);
2N/A
2N/A assert(mhp != NULL);
2N/A
2N/A h = topo_strhash(mod->tm_name) % mhp->mh_hashlen;
2N/A pp = &mhp->mh_hash[h];
2N/A
2N/A for (mp = *pp; mp != NULL; mp = mp->tm_next) {
2N/A if (mp == mod)
2N/A break;
2N/A else
2N/A pp = &mp->tm_next;
2N/A }
2N/A
2N/A if (mp != NULL) {
2N/A *pp = mod->tm_next;
2N/A
2N/A assert(mhp->mh_nelems != 0);
2N/A
2N/A mhp->mh_nelems--;
2N/A
2N/A }
2N/A topo_modhash_unlock(mhp);
2N/A
2N/A (void) pthread_mutex_unlock(&mod->tm_lock);
2N/A
2N/A topo_mod_stop(mod);
2N/A topo_mod_destroy(mod);
2N/A
2N/A}
2N/A
2N/Avoid
2N/Atopo_modhash_unload_all(topo_hdl_t *thp)
2N/A{
2N/A int i;
2N/A topo_modhash_t *mhp = thp->th_modhash;
2N/A topo_mod_t *mp, **pp;
2N/A
2N/A if (mhp == NULL)
2N/A return;
2N/A
2N/A topo_modhash_lock(mhp);
2N/A for (i = 0; i < TOPO_HASH_BUCKETS; ++i) {
2N/A pp = &mhp->mh_hash[i];
2N/A mp = *pp;
2N/A while (mp != NULL) {
2N/A topo_mod_stop(mp);
2N/A
2N/A /*
2N/A * At this point we are forcing all modules to
2N/A * stop, ignore any remaining module reference counts.
2N/A */
2N/A mp->tm_refs = 0;
2N/A
2N/A *pp = mp->tm_next;
2N/A topo_mod_destroy(mp);
2N/A mp = *pp;
2N/A
2N/A --mhp->mh_nelems;
2N/A }
2N/A }
2N/A topo_modhash_unlock(mhp);
2N/A}