xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_tree.c (revision ef8846857fcf954444cdc77e72249afef48377d2)
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_file.h>
66 #include <topo_module.h>
67 #include <topo_string.h>
68 #include <topo_subr.h>
69 #include <topo_tree.h>
70 
71 static ttree_t *
72 set_create_error(topo_hdl_t *thp, ttree_t *tp, int err)
73 {
74 	if (tp != NULL)
75 		topo_tree_destroy(tp);
76 
77 	if (err != 0)
78 		(void) topo_hdl_seterrno(thp, err);
79 
80 	return (NULL);
81 }
82 
83 ttree_t *
84 topo_tree_create(topo_hdl_t *thp, topo_mod_t *mod, const char *scheme)
85 {
86 	ttree_t *tp;
87 	tnode_t *rp;
88 
89 	if ((tp = topo_mod_zalloc(mod, sizeof (ttree_t))) == NULL)
90 		return (set_create_error(thp, NULL, ETOPO_NOMEM));
91 
92 	tp->tt_mod = mod;
93 
94 	if ((tp->tt_scheme = topo_mod_strdup(mod, scheme)) == NULL)
95 		return (set_create_error(thp, tp, ETOPO_NOMEM));
96 
97 	/*
98 	 * Initialize a private walker for internal use
99 	 */
100 	if ((tp->tt_walk = topo_mod_zalloc(mod, sizeof (topo_walk_t))) == NULL)
101 		return (set_create_error(thp, tp, ETOPO_NOMEM));
102 
103 	/*
104 	 * Create the root of this tree: LINKED but never BOUND
105 	 */
106 	if ((rp = topo_mod_zalloc(mod, sizeof (tnode_t))) == NULL)
107 		return (set_create_error(thp, tp, 0)); /* th_errno set */
108 
109 	rp->tn_state = TOPO_NODE_ROOT | TOPO_NODE_INIT;
110 	rp->tn_name = tp->tt_scheme;
111 	rp->tn_instance = 0;
112 	rp->tn_enum = mod;
113 	rp->tn_hdl = thp;
114 
115 	topo_node_hold(rp);
116 
117 	tp->tt_walk->tw_root = rp;
118 	tp->tt_walk->tw_thp = thp;
119 
120 	topo_mod_hold(mod); /* released when root node destroyed */
121 
122 	tp->tt_root = rp;
123 
124 	return (tp);
125 }
126 
127 void
128 topo_tree_destroy(ttree_t *tp)
129 {
130 	topo_mod_t *mod;
131 
132 	if (tp == NULL)
133 		return;
134 
135 	mod = tp->tt_mod;
136 	if (tp->tt_walk != NULL)
137 		topo_mod_free(mod, tp->tt_walk, sizeof (topo_walk_t));
138 
139 	if (tp->tt_root != NULL) {
140 		assert(tp->tt_root->tn_refs == 1);
141 		topo_node_rele(tp->tt_root);
142 	}
143 	/*
144 	 * Deallocate this last, because a pointer alias for tt_scheme
145 	 * (stored in the root node's name field) may be used in
146 	 * topo_node_rele().
147 	 */
148 	if (tp->tt_scheme != NULL)
149 		topo_mod_strfree(mod, tp->tt_scheme);
150 
151 	topo_mod_free(mod, tp, sizeof (ttree_t));
152 }
153 
154 static int
155 topo_tree_enum(topo_hdl_t *thp, ttree_t *tp)
156 {
157 	char *pp;
158 
159 	/*
160 	 * Attempt to enumerate the tree from a topology map in the
161 	 * following order:
162 	 *	<product-name>-<scheme>-topology
163 	 *	<platform-name>-<scheme>-topology (uname -i)
164 	 *	<machine-name>-<scheme>-topology (uname -m)
165 	 *	<scheme>-topology
166 	 *
167 	 * Trim any SUNW, from the product or platform name
168 	 * before loading file
169 	 */
170 	if (thp->th_product == NULL ||
171 	    (pp = strchr(thp->th_product, ',')) == NULL)
172 		pp = thp->th_product;
173 	else
174 		pp++;
175 	if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
176 	    pp, tp->tt_scheme) < 0) {
177 		if ((pp = strchr(thp->th_platform, ',')) == NULL)
178 			pp = thp->th_platform;
179 		else
180 			pp++;
181 
182 		if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
183 		    pp, tp->tt_scheme) < 0) {
184 			if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
185 			    thp->th_machine, tp->tt_scheme) < 0) {
186 
187 				if (topo_file_load(tp->tt_root->tn_enum,
188 				    tp->tt_root, NULL, tp->tt_scheme) < 0) {
189 					topo_dprintf(thp, TOPO_DBG_ERR, "no "
190 					    "topology map found for the %s "
191 					    "FMRI set\n", tp->tt_scheme);
192 					return (topo_hdl_seterrno(thp,
193 					    ETOPO_ENUM_NOMAP));
194 				}
195 			}
196 		}
197 	}
198 
199 	/*
200 	 * It would be nice to leave the devinfo and prominfo trees
201 	 * active but the interfaces consume copious amounts of memory
202 	 * while searching for property information
203 	 */
204 	if (thp->th_di != DI_NODE_NIL) {
205 		di_fini(thp->th_di);
206 		thp->th_di = DI_NODE_NIL;
207 	}
208 	if (thp->th_pi != DI_PROM_HANDLE_NIL) {
209 		di_prom_fini(thp->th_pi);
210 		thp->th_pi = DI_PROM_HANDLE_NIL;
211 	}
212 
213 
214 	return (0);
215 }
216 
217 int
218 topo_tree_enum_all(topo_hdl_t *thp)
219 {
220 	int err = 0;
221 	ttree_t *tp;
222 
223 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
224 	    tp = topo_list_next(tp)) {
225 		err |= topo_tree_enum(thp, tp);
226 	}
227 
228 	if (err != 0)
229 		return (-1);
230 	else
231 		return (0);
232 }
233