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 */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
c93c462eec9d46f84d567abf52eb29a27c2e134bCheng Sean Ye * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Topology Trees
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Toplogy trees are instantiated for each builtin (FMRI) scheme specified
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * in topo_builtin.c. Each ttree_t data structure contains the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * skeleton of the topology tree (scheme, root node, and file information).
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The root node of a topology does not represent any FMRI but rather serves
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * as the entry point for topology access interfaces. The file information
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * provides a handle to access static .xml files that seed scheme-specifc
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * topologies
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Topology trees will remain unpopulated until topo_snap_hold() is called.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * At that time, a ttree_t structure is allocated and added to the list
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * trees maintained in topo_hdl_t. Builtin scheme-specific enumerators are
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * called upon to create nodes that represent FMRIs for resources present in the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * system. If a <scheme>-topology.xml file exists in a standard file
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * location, the file is used to seed the topology while the rest is
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * dynamically created by the builtin or helper enumerator modules.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * For example, the 'hc' tree is enumerated by the hc enumerator (hc.c)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * after the hc-topology.xml is read from /usr/platform/`uname -i`/lib/fm/topo,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * /usr/platform/`uname -r`/lib/fm/topo, or /usr/lib/fm/topo. Each node
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * is created with a properly formatted hc FMRI resource.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Toplogy trees are released and deallocated when topo_snap_hold is called.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Upon return from topo_snap_rele(), all node resources are deallocated
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and all that remains is the ttree_t structure containing the root node.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <pthread.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <limits.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <assert.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <sys/param.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <sys/systeminfo.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <sys/utsname.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_alloc.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_error.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <topo_file.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_module.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_string.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_subr.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_tree.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic ttree_t *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiset_create_error(topo_hdl_t *thp, ttree_t *tp, int err)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (tp != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_tree_destroy(tp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (err != 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) topo_hdl_seterrno(thp, err);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (NULL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindittree_t *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_tree_create(topo_hdl_t *thp, topo_mod_t *mod, const char *scheme)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ttree_t *tp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tnode_t *rp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((tp = topo_mod_zalloc(mod, sizeof (ttree_t))) == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, NULL, ETOPO_NOMEM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi tp->tt_mod = mod;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((tp->tt_scheme = topo_mod_strdup(mod, scheme)) == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, tp, ETOPO_NOMEM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Initialize a private walker for internal use
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((tp->tt_walk = topo_mod_zalloc(mod, sizeof (topo_walk_t))) == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, tp, ETOPO_NOMEM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Create the root of this tree: LINKED but never BOUND
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((rp = topo_mod_zalloc(mod, sizeof (tnode_t))) == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (set_create_error(thp, tp, 0)); /* th_errno set */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->tn_state = TOPO_NODE_ROOT | TOPO_NODE_INIT;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->tn_name = tp->tt_scheme;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->tn_instance = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->tn_enum = mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rp->tn_hdl = thp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_node_hold(rp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tp->tt_walk->tw_root = rp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tp->tt_walk->tw_thp = thp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_hold(mod); /* released when root node destroyed */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tp->tt_root = rp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (tp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindivoid
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditopo_tree_destroy(ttree_t *tp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_t *mod;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (tp == NULL)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi mod = tp->tt_mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (tp->tt_walk != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_free(mod, tp->tt_walk, sizeof (topo_walk_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (tp->tt_root != NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi assert(tp->tt_root->tn_refs == 1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_node_rele(tp->tt_root);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
724365f7556fc4201fdb11766ebc6bd918523130sethg /*
724365f7556fc4201fdb11766ebc6bd918523130sethg * Deallocate this last, because a pointer alias for tt_scheme
724365f7556fc4201fdb11766ebc6bd918523130sethg * (stored in the root node's name field) may be used in
724365f7556fc4201fdb11766ebc6bd918523130sethg * topo_node_rele().
724365f7556fc4201fdb11766ebc6bd918523130sethg */
724365f7556fc4201fdb11766ebc6bd918523130sethg if (tp->tt_scheme != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_strfree(mod, tp->tt_scheme);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_free(mod, tp, sizeof (ttree_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_tree_enum(topo_hdl_t *thp, ttree_t *tp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
12cc75c814f0c017004a9bbc96429911e008601bcindi int rv = 0;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi char *pp;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi /*
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * Attempt to enumerate the tree from a topology map in the
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * following order:
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * <product-name>-<scheme>-topology
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * <platform-name>-<scheme>-topology (uname -i)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * <machine-name>-<scheme>-topology (uname -m)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * <scheme>-topology
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi *
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * Trim any SUNW, from the product or platform name
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * before loading file
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (thp->th_product == NULL ||
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (pp = strchr(thp->th_product, ',')) == NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi pp = thp->th_product;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi else
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi pp++;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj pp, tp->tt_scheme, 0) < 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((pp = strchr(thp->th_platform, ',')) == NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi pp = thp->th_platform;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi else
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi pp++;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj pp, tp->tt_scheme, 0) < 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj thp->th_machine, tp->tt_scheme, 0) < 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
12cc75c814f0c017004a9bbc96429911e008601bcindi if ((rv = topo_file_load(tp->tt_root->tn_enum,
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj tp->tt_root, NULL, tp->tt_scheme, 0)) < 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(thp, TOPO_DBG_ERR, "no "
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "topology map found for the %s "
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "FMRI set\n", tp->tt_scheme);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
12cc75c814f0c017004a9bbc96429911e008601bcindi if (rv != 0)
12cc75c814f0c017004a9bbc96429911e008601bcindi return (topo_hdl_seterrno(thp, ETOPO_ENUM_NOMAP));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_tree_enum_all(topo_hdl_t *thp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi int err = 0;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ttree_t *tp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (tp = topo_list_next(&thp->th_trees); tp != NULL;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tp = topo_list_next(tp)) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi err |= topo_tree_enum(thp, tp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (err != 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi else
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}