7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
724365f7556fc4201fdb11766ebc6bd918523130sethg * Common Development and Distribution License (the "License").
724365f7556fc4201fdb11766ebc6bd918523130sethg * You may not use this file except in compliance with the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * or http://www.opensolaris.org/os/licensing.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * See the License for the specific language governing permissions
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and limitations under the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * When distributing Covered Code, include this CDDL HEADER in each
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If applicable, add the following below this CDDL HEADER, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * fields enclosed by brackets "[]" replaced with your own identifying
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * information: Portions Copyright [yyyy] [name of copyright owner]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER END
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
53f3aea0943e36e5fed2615ad5f9fd1f17de51d2Roger A. Faulkner
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
53f3aea0943e36e5fed2615ad5f9fd1f17de51d2Roger A. Faulkner * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <signal.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <dirent.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <limits.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <alloca.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <unistd.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <stdio.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <pthread.h>
53f3aea0943e36e5fed2615ad5f9fd1f17de51d2Roger A. Faulkner#include <synch.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <errno.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <strings.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <assert.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <sys/nvpair.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_string.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_alloc.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_module.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_error.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_subr.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiextern nv_alloc_ops_t topo_nv_alloc_ops;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_release(topo_mod_t *mod, tnode_t *node)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_enter(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (mod->tm_info->tmi_ops->tmo_release != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mod->tm_info->tmi_ops->tmo_release(mod, node);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_exit(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_hold(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_lock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_refs++;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mod->tm_refs != 0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_unlock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_rele(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mod->tm_refs != 0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_lock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Lazy unload module
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (--mod->tm_refs == 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_unload(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi else
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_unlock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_enter(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_lock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (mod->tm_busy != 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_cond_wait(&mod->tm_cv, &mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ++mod->tm_busy;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_unlock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_exit(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_lock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi --mod->tm_busy;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mod->tm_busy == 0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_cond_broadcast(&mod->tm_cv);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_unlock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic void
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_lock(topo_modhash_t *mhp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_lock(&mhp->mh_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic void
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_unlock(topo_modhash_t *mhp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_unlock(&mhp->mh_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic void
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_stop(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_flags & TOPO_MOD_INIT) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_mops->mop_fini(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_flags & TOPO_MOD_REG)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_unregister(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_flags = TOPO_MOD_FINI;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_MODSVC,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "module %s stopped\n", mod->tm_name);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditopo_mod_start(topo_mod_t *mod, topo_version_t version)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_MODSVC,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "starting module %s\n", mod->tm_name);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (mod->tm_mops->mop_init(mod, version) != 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (mod->tm_errno == 0)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mod->tm_errno = ETOPO_MOD_INIT;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "module %s failed to initialize: %s\n", mod->tm_name,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_strerror(mod->tm_errno));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_flags |= TOPO_MOD_INIT;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (!(mod->tm_flags & TOPO_MOD_REG)) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "module %s failed to register\n", mod->tm_name);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_errno = ETOPO_MOD_NOREG;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_stop(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_t *
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditopo_mod_lookup(topo_hdl_t *thp, const char *name, int bump)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t *mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_t *mhp = thp->th_modhash;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_lock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod = topo_modhash_lookup(mhp, name);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (mod != NULL && bump != 0)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_hold(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_unlock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic void
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_destroy(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_t *thp = mod->tm_hdl;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mod->tm_refs == 0);
53f3aea0943e36e5fed2615ad5f9fd1f17de51d2Roger A. Faulkner assert(!MUTEX_HELD(&mod->tm_lock));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_name != NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_strfree(thp, mod->tm_name);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_path != NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_strfree(thp, mod->tm_path);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod->tm_rootdir != NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_strfree(thp, mod->tm_rootdir);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_free(thp, mod, sizeof (topo_mod_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic topo_mod_t *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiset_create_error(topo_hdl_t *thp, topo_mod_t *mod, const char *path, int err)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
12cc75c814f0c017004a9bbc96429911e008601bcindi if (path != NULL)
12cc75c814f0c017004a9bbc96429911e008601bcindi topo_dprintf(thp, TOPO_DBG_ERR, "unable to load module %s: "
12cc75c814f0c017004a9bbc96429911e008601bcindi "%s\n", path, topo_strerror(err));
12cc75c814f0c017004a9bbc96429911e008601bcindi else
12cc75c814f0c017004a9bbc96429911e008601bcindi topo_dprintf(thp, TOPO_DBG_ERR, "unable to load module: "
12cc75c814f0c017004a9bbc96429911e008601bcindi "%s\n", topo_strerror(err));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mod != NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_destroy(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) topo_hdl_seterrno(thp, err);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic topo_mod_t *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_create(topo_hdl_t *thp, const char *name, const char *path,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi const topo_imodops_t *ops, topo_version_t version)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t *mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (topo_modhash_lookup(thp->th_modhash, name) != NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, NULL, path, ETOPO_MOD_LOADED));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((mod = topo_hdl_zalloc(thp, sizeof (topo_mod_t))) == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, mod, path, ETOPO_NOMEM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
c40d7343efa60b18ad1ceb316eb337caeea79046cindi mod->tm_hdl = thp;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_init(&mod->tm_lock, NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_name = topo_hdl_strdup(thp, name);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (path != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mod->tm_path = topo_hdl_strdup(thp, path);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_rootdir = topo_hdl_strdup(thp, thp->th_rootdir);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (mod->tm_name == NULL || mod->tm_rootdir == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, mod, path, ETOPO_NOMEM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mod->tm_mops = (topo_imodops_t *)ops;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_alloc = thp->th_alloc;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Module will be held upon a successful return from topo_mod_start()
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((topo_mod_start(mod, version)) < 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, mod, path, mod->tm_errno));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(thp, TOPO_DBG_MODSVC, "loaded module %s\n", mod->tm_name);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_t *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_create(topo_hdl_t *thp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_t *mhp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((mhp = topo_hdl_zalloc(thp, sizeof (topo_modhash_t))) == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mhp->mh_hashlen = TOPO_HASH_BUCKETS;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((mhp->mh_hash = topo_hdl_zalloc(thp,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi sizeof (void *) * mhp->mh_hashlen)) == NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_free(thp, mhp, sizeof (topo_modhash_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mhp->mh_nelems = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_init(&mhp->mh_lock, NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi thp->th_modhash = mhp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_destroy(topo_hdl_t *thp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_t *mhp = thp->th_modhash;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mhp == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mhp->mh_nelems == 0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_free(thp, mhp->mh_hash, sizeof (void *) * mhp->mh_hashlen);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_free(thp, mhp, sizeof (topo_modhash_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi thp->th_modhash = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_t *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_lookup(topo_modhash_t *mhp, const char *name)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t *mod = NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi uint_t h;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi h = topo_strhash(name) % mhp->mh_hashlen;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (mod = mhp->mh_hash[h]; mod != NULL; mod = mod->tm_next) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (strcmp(name, mod->tm_name) == 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi break;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_mod_t *
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditopo_modhash_load(topo_hdl_t *thp, const char *name, const char *path,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi const topo_imodops_t *ops, topo_version_t version)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_t *mhp = thp->th_modhash;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t *mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi uint_t h;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_lock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((mod = topo_mod_create(thp, name, path, ops, version)) == NULL) {
724365f7556fc4201fdb11766ebc6bd918523130sethg topo_modhash_unlock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (NULL); /* th_errno set */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_hold(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi h = topo_strhash(name) % mhp->mh_hashlen;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mod->tm_next = mhp->mh_hash[h];
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mhp->mh_hash[h] = mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mhp->mh_nelems++;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_unlock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_unload(topo_mod_t *mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi uint_t h;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t **pp, *mp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_t *thp = mod->tm_hdl;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_t *mhp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
53f3aea0943e36e5fed2615ad5f9fd1f17de51d2Roger A. Faulkner assert(MUTEX_HELD(&mod->tm_lock));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mod->tm_busy == 0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mhp = thp->th_modhash;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_lock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mhp != NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi h = topo_strhash(mod->tm_name) % mhp->mh_hashlen;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pp = &mhp->mh_hash[h];
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (mp = *pp; mp != NULL; mp = mp->tm_next) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mp == mod)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi break;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi else
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pp = &mp->tm_next;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mp != NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *pp = mod->tm_next;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(mhp->mh_nelems != 0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mhp->mh_nelems--;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_unlock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) pthread_mutex_unlock(&mod->tm_lock);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_stop(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_destroy(mod);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_modhash_unload_all(topo_hdl_t *thp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int i;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_t *mhp = thp->th_modhash;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t *mp, **pp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
c40d7343efa60b18ad1ceb316eb337caeea79046cindi if (mhp == NULL)
c40d7343efa60b18ad1ceb316eb337caeea79046cindi return;
c40d7343efa60b18ad1ceb316eb337caeea79046cindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_lock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (i = 0; i < TOPO_HASH_BUCKETS; ++i) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi pp = &mhp->mh_hash[i];
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mp = *pp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi while (mp != NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_stop(mp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi /*
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * At this point we are forcing all modules to
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * stop, ignore any remaining module reference counts.
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mp->tm_refs = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *pp = mp->tm_next;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_destroy(mp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mp = *pp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi --mhp->mh_nelems;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_modhash_unlock(mhp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}