xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_tree.c (revision 6a074c93c5dee390d8ca2377f42e55418f0a9eb3)
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 2007 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 	int rv = 0;
158 	char *pp;
159 
160 	/*
161 	 * Attempt to enumerate the tree from a topology map in the
162 	 * following order:
163 	 *	<product-name>-<scheme>-topology
164 	 *	<platform-name>-<scheme>-topology (uname -i)
165 	 *	<machine-name>-<scheme>-topology (uname -m)
166 	 *	<scheme>-topology
167 	 *
168 	 * Trim any SUNW, from the product or platform name
169 	 * before loading file
170 	 */
171 	if (thp->th_product == NULL ||
172 	    (pp = strchr(thp->th_product, ',')) == NULL)
173 		pp = thp->th_product;
174 	else
175 		pp++;
176 	if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
177 	    pp, tp->tt_scheme, 0) < 0) {
178 		if ((pp = strchr(thp->th_platform, ',')) == NULL)
179 			pp = thp->th_platform;
180 		else
181 			pp++;
182 
183 		if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
184 		    pp, tp->tt_scheme, 0) < 0) {
185 			if (topo_file_load(tp->tt_root->tn_enum, tp->tt_root,
186 			    thp->th_machine, tp->tt_scheme, 0) < 0) {
187 
188 				if ((rv = topo_file_load(tp->tt_root->tn_enum,
189 				    tp->tt_root, NULL, tp->tt_scheme, 0)) < 0) {
190 					topo_dprintf(thp, TOPO_DBG_ERR, "no "
191 					    "topology map found for the %s "
192 					    "FMRI set\n", tp->tt_scheme);
193 				}
194 			}
195 		}
196 	}
197 
198 	/*
199 	 * It would be nice to leave the devinfo and prominfo trees
200 	 * active but the interfaces consume copious amounts of memory
201 	 * while searching for property information
202 	 */
203 	if (thp->th_di != DI_NODE_NIL) {
204 		di_fini(thp->th_di);
205 		thp->th_di = DI_NODE_NIL;
206 	}
207 	if (thp->th_pi != DI_PROM_HANDLE_NIL) {
208 		di_prom_fini(thp->th_pi);
209 		thp->th_pi = DI_PROM_HANDLE_NIL;
210 	}
211 
212 	if (rv != 0)
213 		return (topo_hdl_seterrno(thp, ETOPO_ENUM_NOMAP));
214 
215 	return (0);
216 }
217 
218 int
219 topo_tree_enum_all(topo_hdl_t *thp)
220 {
221 	int err = 0;
222 	ttree_t *tp;
223 
224 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
225 	    tp = topo_list_next(tp)) {
226 		err |= topo_tree_enum(thp, tp);
227 	}
228 
229 	if (err != 0)
230 		return (-1);
231 	else
232 		return (0);
233 }
234