xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_tree.c (revision 1a220b56b93ff1dc80855691548503117af4cc10)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Topology Trees
30  *
31  * Toplogy trees are instantiated for each builtin (FMRI) scheme specified
32  * in topo_builtin.c.  Each ttree_t data structure contains the
33  * skeleton of the topology tree (scheme, root node, and file information).
34  * The root node of a topology does not represent any FMRI but rather serves
35  * as the entry point for topology access interfaces.  The file information
36  * provides a handle to access static .xml files that seed scheme-specifc
37  * topologies
38  *
39  * Topology trees will remain unpopulated until topo_snap_hold() is called.
40  * At that time, a ttree_t structure is allocated and added to the list
41  * trees maintained in topo_hdl_t.  Builtin scheme-specific enumerators are
42  * called upon to create nodes that represent FMRIs for resources present in the
43  * system.  If a <scheme>-topology.xml file exists in a standard file
44  * location, the file is used to seed the topology while the rest is
45  * dynamically created by the builtin or helper enumerator modules.
46  * For example, the 'hc' tree is enumerated by the hc enumerator (hc.c)
47  * after the hc-topology.xml is read from /usr/platform/`uname -i`/lib/fm/topo,
48  * /usr/platform/`uname -r`/lib/fm/topo, or /usr/lib/fm/topo.  Each node
49  * is created with a properly formatted hc FMRI resource.
50  *
51  * Toplogy trees are released and deallocated when topo_snap_hold is called.
52  * Upon return from topo_snap_rele(), all node resources are deallocated
53  * and all that remains is the ttree_t structure containing the root node.
54  */
55 
56 #include <pthread.h>
57 #include <limits.h>
58 #include <assert.h>
59 #include <sys/param.h>
60 #include <sys/systeminfo.h>
61 #include <sys/utsname.h>
62 
63 #include <topo_alloc.h>
64 #include <topo_error.h>
65 #include <topo_module.h>
66 #include <topo_string.h>
67 #include <topo_subr.h>
68 #include <topo_tree.h>
69 
70 static ttree_t *
71 set_create_error(topo_hdl_t *thp, ttree_t *tp, int err)
72 {
73 	if (tp != NULL)
74 		topo_tree_destroy(thp, tp);
75 
76 	if (err != 0)
77 		(void) topo_hdl_seterrno(thp, err);
78 
79 	return (NULL);
80 }
81 
82 static void
83 set_system_props(tnode_t *node)
84 {
85 	int err;
86 	char platform[MAXNAMELEN];
87 	char isa[MAXNAMELEN];
88 	struct utsname uts;
89 
90 	platform[0] = '\0';
91 	isa[0] = '\0';
92 	(void) sysinfo(SI_PLATFORM, platform, sizeof (platform));
93 	(void) sysinfo(SI_ARCHITECTURE, isa, sizeof (isa));
94 	(void) uname(&uts);
95 
96 	(void) topo_pgroup_create(node, TOPO_PGROUP_SYSTEM,
97 	    TOPO_STABILITY_PRIVATE, &err);
98 	(void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM,
99 	    TOPO_PROP_PLATFORM, TOPO_PROP_SET_ONCE, platform, &err);
100 	(void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM,
101 	    TOPO_PROP_ISA, TOPO_PROP_SET_ONCE, isa, &err);
102 	(void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM,
103 	    TOPO_PROP_MACHINE, TOPO_PROP_SET_ONCE, uts.machine, &err);
104 }
105 
106 ttree_t *
107 topo_tree_create(topo_hdl_t *thp, topo_mod_t *mod, const char *scheme)
108 {
109 	ttree_t *tp;
110 	tnode_t *rp;
111 
112 	if ((tp = topo_hdl_zalloc(thp, sizeof (ttree_t))) == NULL)
113 		return (set_create_error(thp, NULL, ETOPO_NOMEM));
114 
115 	if ((tp->tt_scheme = topo_hdl_strdup(thp, scheme)) == NULL)
116 		return (set_create_error(thp, tp, ETOPO_NOMEM));
117 
118 	/*
119 	 * Initialize a private walker for internal use
120 	 */
121 	if ((tp->tt_walk = topo_hdl_zalloc(thp, sizeof (topo_walk_t))) == NULL)
122 		return (set_create_error(thp, tp, ETOPO_NOMEM));
123 
124 	/*
125 	 * Create the root of this tree: LINKED but never BOUND
126 	 */
127 	if ((rp = topo_mod_zalloc(mod, sizeof (tnode_t))) == NULL)
128 		return (set_create_error(thp, tp, 0)); /* th_errno set */
129 
130 	rp->tn_state = TOPO_NODE_ROOT | TOPO_NODE_INIT;
131 	rp->tn_name = tp->tt_scheme;
132 	rp->tn_instance = 0;
133 	rp->tn_enum = mod;
134 	rp->tn_hdl = thp;
135 
136 	set_system_props(rp);
137 	topo_node_hold(rp);
138 
139 	tp->tt_walk->tw_root = rp;
140 	tp->tt_walk->tw_thp = thp;
141 
142 	topo_mod_hold(mod); /* released when root node destroyed */
143 
144 	tp->tt_root = rp;
145 
146 	return (tp);
147 }
148 
149 void
150 topo_tree_destroy(topo_hdl_t *thp, ttree_t *tp)
151 {
152 	if (tp == NULL)
153 		return;
154 
155 	if (tp->tt_walk != NULL)
156 		topo_hdl_free(thp, tp->tt_walk, sizeof (topo_walk_t));
157 
158 	if (tp->tt_file != NULL)
159 		topo_file_unload(thp, tp);
160 
161 	if (tp->tt_root != NULL) {
162 		assert(tp->tt_root->tn_refs == 1);
163 		topo_node_rele(tp->tt_root);
164 	}
165 	/*
166 	 * Deallocate this last, because a pointer alias for tt_scheme
167 	 * (stored in the root node's name field) may be used in
168 	 * topo_node_rele().
169 	 */
170 	if (tp->tt_scheme != NULL)
171 		topo_hdl_strfree(thp, tp->tt_scheme);
172 
173 	topo_hdl_free(thp, tp, sizeof (ttree_t));
174 }
175 
176 static int
177 topo_tree_enum(topo_hdl_t *thp, ttree_t *tp)
178 {
179 	tnode_t *rnode;
180 
181 	rnode = tp->tt_root;
182 	/*
183 	 * Attempt to populate the tree from a topology file
184 	 */
185 	if (topo_file_load(thp, rnode->tn_enum, tp) < 0) {
186 		/*
187 		 * If this tree does not have a matching static topology file,
188 		 * continue on.
189 		 */
190 		if (topo_hdl_errno(thp) != ETOPO_FILE_NOENT)
191 			return (topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL));
192 	}
193 	return (0);
194 }
195 
196 int
197 topo_tree_enum_all(topo_hdl_t *thp)
198 {
199 	int err = 0;
200 	ttree_t *tp;
201 
202 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
203 	    tp = topo_list_next(tp)) {
204 		err |= topo_tree_enum(thp, tp);
205 	}
206 
207 	if (err != 0)
208 		return (-1);
209 	else
210 		return (0);
211 }
212