xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_snap.c (revision c7402f0767d7a0360fabd0bd449c6baf9b282074)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Snapshot Library Interfaces
28  *
29  * Consumers of topology data may use the interfaces in this file to open,
30  * snapshot and close a topology exported by FMRI scheme (hc, mem and cpu)
31  * builtin plugins and their helper modules.  A topology handle is obtained
32  * by calling topo_open().  Upon a successful return, the caller may use this
33  * handle to open a new snapshot.  Each snapshot is assigned a Universally
34  * Unique Identifier that in a future enchancement to the libtopo API will be
35  * used as the file locator in /var/fm/topo to persist new snapshots or lookup
36  * a previously captured snapshot.  topo_snap_hold() will capture the current
37  * system topology.  All consumers of the topo_hdl_t argument will be
38  * blocked from accessing the topology trees until the snapshot completes.
39  *
40  * A snapshot may be cleared by calling topo_snap_rele().  As with
41  * topo_snap_hold(), all topology accesses are blocked until the topology
42  * trees have been released and deallocated.
43  *
44  * Walker Library Interfaces
45  *
46  * Once a snapshot has been taken with topo_snap_hold(), topo_hdl_t holders
47  * may initiate topology tree walks on a scheme-tree basis.  topo_walk_init()
48  * will initiate the data structures required to walk any one one of the
49  * FMRI scheme trees.  The walker data structure, topo_walk_t, is an opaque
50  * handle passed to topo_walk_step to begin the walk.  At each node in the
51  * topology tree, a callback function is called with access to the node at
52  * which our current walk falls.  The callback function is passed in during
53  * calls to topo_walk_init() and used throughout the walk_step of the
54  * scheme tree.  At any time, the callback may terminate the walk by returning
55  * TOPO_WALK_TERMINATE or TOPO_WALK_ERR.  TOPO_WALK_NEXT will continue the walk.
56  *
57  * The type of walk through the tree may be sibling first or child first by
58  * respectively passing in TOPO_WALK_SIBLING or TOPO_WALK_CHILD to
59  * the topo_walk_step() function.  Topology nodes
60  * associated with an outstanding walk are held in place and will not be
61  * deallocated until the walk through that node completes.
62  *
63  * Once the walk has terminated, the walking process should call
64  * topo_walk_fini() to clean-up resources created in topo_walk_init()
65  * and release nodes that may be still held.
66  */
67 
68 #include <alloca.h>
69 #include <ctype.h>
70 #include <pthread.h>
71 #include <limits.h>
72 #include <assert.h>
73 #include <fcntl.h>
74 #include <smbios.h>
75 #include <sys/param.h>
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #include <sys/systeminfo.h>
79 #include <sys/utsname.h>
80 #include <uuid/uuid.h>
81 
82 #include <fm/libtopo.h>
83 #include <sys/fm/protocol.h>
84 
85 #include <topo_alloc.h>
86 #include <topo_builtin.h>
87 #include <topo_string.h>
88 #include <topo_error.h>
89 #include <topo_subr.h>
90 
91 static void topo_snap_destroy(topo_hdl_t *);
92 
93 static topo_hdl_t *
94 set_open_errno(topo_hdl_t *thp, int *errp, int err)
95 {
96 	if (thp != NULL) {
97 		topo_close(thp);
98 	}
99 	if (errp != NULL)
100 		*errp = err;
101 	return (NULL);
102 }
103 
104 topo_hdl_t *
105 topo_open(int version, const char *rootdir, int *errp)
106 {
107 	topo_hdl_t *thp = NULL;
108 	topo_alloc_t *tap;
109 
110 	char platform[MAXNAMELEN];
111 	char isa[MAXNAMELEN];
112 	struct utsname uts;
113 	struct stat st;
114 
115 	smbios_hdl_t *shp;
116 	smbios_system_t s1;
117 	smbios_info_t s2;
118 	id_t id;
119 
120 	char *dbflags, *dbout;
121 
122 	if (version != TOPO_VERSION)
123 		return (set_open_errno(thp, errp, ETOPO_HDL_ABIVER));
124 
125 	if (rootdir != NULL && stat(rootdir, &st) < 0)
126 		return (set_open_errno(thp, errp, ETOPO_HDL_INVAL));
127 
128 	if ((thp = topo_zalloc(sizeof (topo_hdl_t), 0)) == NULL)
129 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
130 
131 	(void) pthread_mutex_init(&thp->th_lock, NULL);
132 
133 	if ((tap = topo_zalloc(sizeof (topo_alloc_t), 0)) == NULL)
134 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
135 
136 	/*
137 	 * Install default allocators
138 	 */
139 	tap->ta_flags = 0;
140 	tap->ta_alloc = topo_alloc;
141 	tap->ta_zalloc = topo_zalloc;
142 	tap->ta_free = topo_free;
143 	tap->ta_nvops.nv_ao_alloc = topo_nv_alloc;
144 	tap->ta_nvops.nv_ao_free = topo_nv_free;
145 	(void) nv_alloc_init(&tap->ta_nva, &tap->ta_nvops);
146 	thp->th_alloc = tap;
147 
148 	if ((thp->th_modhash = topo_modhash_create(thp)) == NULL)
149 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
150 
151 	/*
152 	 * Set-up system information and search paths for modules
153 	 * and topology map files
154 	 */
155 	if (rootdir == NULL) {
156 		rootdir = topo_hdl_strdup(thp, "/");
157 		thp->th_rootdir = (char *)rootdir;
158 	} else {
159 		int len;
160 		char *rpath;
161 
162 		len = strlen(rootdir);
163 		if (len >= PATH_MAX)
164 			return (set_open_errno(thp, errp, EINVAL));
165 
166 		if (rootdir[len - 1] != '/') {
167 			rpath = alloca(len + 2);
168 			(void) snprintf(rpath, len + 2, "%s/", rootdir);
169 		} else {
170 			rpath = (char *)rootdir;
171 		}
172 		thp->th_rootdir = topo_hdl_strdup(thp, rpath);
173 	}
174 
175 	platform[0] = '\0';
176 	isa[0] = '\0';
177 	(void) sysinfo(SI_PLATFORM, platform, sizeof (platform));
178 	(void) sysinfo(SI_ARCHITECTURE, isa, sizeof (isa));
179 	(void) uname(&uts);
180 	thp->th_platform = topo_hdl_strdup(thp, platform);
181 	thp->th_isa = topo_hdl_strdup(thp, isa);
182 	thp->th_machine = topo_hdl_strdup(thp, uts.machine);
183 	if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) {
184 		if ((id = smbios_info_system(shp, &s1)) != SMB_ERR &&
185 		    smbios_info_common(shp, id, &s2) != SMB_ERR) {
186 
187 			if (strcmp(s2.smbi_product, SMB_DEFAULT1) != 0 &&
188 			    strcmp(s2.smbi_product, SMB_DEFAULT2) != 0) {
189 				thp->th_product = topo_cleanup_auth_str(thp,
190 				    (char *)s2.smbi_product);
191 			}
192 		}
193 		smbios_close(shp);
194 	} else {
195 		thp->th_product = topo_hdl_strdup(thp, thp->th_platform);
196 	}
197 
198 	if (thp->th_rootdir == NULL || thp->th_platform == NULL ||
199 	    thp->th_machine == NULL)
200 		return (set_open_errno(thp, errp, ETOPO_NOMEM));
201 
202 	dbflags	 = getenv("TOPO_DEBUG");
203 	dbout = getenv("TOPO_DEBUG_OUT");
204 	if (dbflags != NULL)
205 		topo_debug_set(thp, dbflags, dbout);
206 
207 	if (topo_builtin_create(thp, thp->th_rootdir) != 0) {
208 		topo_dprintf(thp, TOPO_DBG_ERR,
209 		    "failed to load builtin modules: %s\n",
210 		    topo_hdl_errmsg(thp));
211 		topo_close(thp);
212 		return (NULL);
213 	}
214 
215 	return (thp);
216 }
217 
218 void
219 topo_close(topo_hdl_t *thp)
220 {
221 	ttree_t *tp;
222 
223 	topo_hdl_lock(thp);
224 	if (thp->th_platform != NULL)
225 		topo_hdl_strfree(thp, thp->th_platform);
226 	if (thp->th_isa != NULL)
227 		topo_hdl_strfree(thp, thp->th_isa);
228 	if (thp->th_machine != NULL)
229 		topo_hdl_strfree(thp, thp->th_machine);
230 	if (thp->th_product != NULL)
231 		topo_hdl_strfree(thp, thp->th_product);
232 	if (thp->th_rootdir != NULL)
233 		topo_hdl_strfree(thp, thp->th_rootdir);
234 	if (thp->th_ipmi != NULL)
235 		ipmi_close(thp->th_ipmi);
236 	if (thp->th_smbios != NULL)
237 		smbios_close(thp->th_smbios);
238 
239 	/*
240 	 * Clean-up snapshot
241 	 */
242 	topo_snap_destroy(thp);
243 
244 	/*
245 	 * Clean-up trees
246 	 */
247 	while ((tp = topo_list_next(&thp->th_trees)) != NULL) {
248 		topo_list_delete(&thp->th_trees, tp);
249 		topo_tree_destroy(tp);
250 	}
251 
252 	/*
253 	 * Unload all plugins
254 	 */
255 	topo_modhash_unload_all(thp);
256 
257 	if (thp->th_modhash != NULL)
258 		topo_modhash_destroy(thp);
259 	if (thp->th_alloc != NULL)
260 		topo_free(thp->th_alloc, sizeof (topo_alloc_t));
261 
262 	topo_hdl_unlock(thp);
263 
264 	topo_free(thp, sizeof (topo_hdl_t));
265 }
266 
267 static char *
268 topo_snap_create(topo_hdl_t *thp, int *errp)
269 {
270 	uuid_t uuid;
271 	char *ustr = NULL;
272 
273 	topo_hdl_lock(thp);
274 	if (thp->th_uuid != NULL) {
275 		*errp = ETOPO_HDL_UUID;
276 		topo_hdl_unlock(thp);
277 		return (NULL);
278 	}
279 
280 	if ((thp->th_uuid = topo_hdl_zalloc(thp, TOPO_UUID_SIZE)) == NULL) {
281 		*errp = ETOPO_NOMEM;
282 		topo_dprintf(thp, TOPO_DBG_ERR, "unable to allocate uuid: %s\n",
283 		    topo_strerror(*errp));
284 		topo_hdl_unlock(thp);
285 		return (NULL);
286 	}
287 
288 	uuid_generate(uuid);
289 	uuid_unparse(uuid, thp->th_uuid);
290 	if ((ustr = topo_hdl_strdup(thp, thp->th_uuid)) == NULL) {
291 		*errp = ETOPO_NOMEM;
292 		topo_hdl_unlock(thp);
293 		return (NULL);
294 	}
295 
296 	thp->th_di = di_init("/",
297 	    DINFOFORCE | DINFOSUBTREE | DINFOMINOR | DINFOPROP | DINFOPATH);
298 	thp->th_pi = di_prom_init();
299 
300 	if (topo_tree_enum_all(thp) < 0) {
301 		topo_dprintf(thp, TOPO_DBG_ERR, "enumeration failure: %s\n",
302 		    topo_hdl_errmsg(thp));
303 		if (topo_hdl_errno(thp) == ETOPO_ENUM_FATAL) {
304 			*errp = thp->th_errno;
305 
306 			if (thp->th_di != DI_NODE_NIL) {
307 				di_fini(thp->th_di);
308 				thp->th_di = DI_NODE_NIL;
309 			}
310 			if (thp->th_pi != DI_PROM_HANDLE_NIL) {
311 				di_prom_fini(thp->th_pi);
312 				thp->th_pi = DI_PROM_HANDLE_NIL;
313 			}
314 
315 			topo_hdl_strfree(thp, ustr);
316 			topo_hdl_unlock(thp);
317 			return (NULL);
318 		}
319 	}
320 
321 	if (thp->th_ipmi != NULL &&
322 	    ipmi_sdr_changed(thp->th_ipmi) &&
323 	    ipmi_sdr_refresh(thp->th_ipmi) != 0) {
324 		topo_dprintf(thp, TOPO_DBG_ERR,
325 		    "failed to refresh IPMI sdr repository: %s\n",
326 		    ipmi_errmsg(thp->th_ipmi));
327 	}
328 
329 	topo_hdl_unlock(thp);
330 
331 	return (ustr);
332 }
333 
334 /*ARGSUSED*/
335 static char *
336 topo_snap_log_create(topo_hdl_t *thp, const char *uuid, int *errp)
337 {
338 	return ((char *)uuid);
339 }
340 
341 /*ARGSUSED*/
342 static int
343 fac_walker(topo_hdl_t *thp, tnode_t *node, void *arg)
344 {
345 	int err;
346 	nvlist_t *out;
347 
348 	if (topo_method_supported(node, TOPO_METH_FAC_ENUM, 0)) {
349 		/*
350 		 * If the facility enumeration method fails, note the failure,
351 		 * but continue on with the walk.
352 		 */
353 		if (topo_method_invoke(node, TOPO_METH_FAC_ENUM, 0, NULL, &out,
354 		    &err) != 0) {
355 			topo_dprintf(thp, TOPO_DBG_ERR,
356 			    "facility enumeration method failed on node %s=%d "
357 			    "(%s)\n", topo_node_name(node),
358 			    topo_node_instance(node), topo_strerror(err));
359 		}
360 	}
361 	return (TOPO_WALK_NEXT);
362 }
363 
364 /*
365  * Return snapshot id
366  */
367 char *
368 topo_snap_hold(topo_hdl_t *thp, const char *uuid, int *errp)
369 {
370 	topo_walk_t *twp;
371 
372 	if (thp == NULL)
373 		return (NULL);
374 
375 	if (uuid == NULL) {
376 		char *ret;
377 
378 		ret = topo_snap_create(thp, errp);
379 
380 		/*
381 		 * Now walk the tree and invoke any facility enumeration methods
382 		 */
383 		if (ret != NULL) {
384 			if ((twp = topo_walk_init(thp, FM_FMRI_SCHEME_HC,
385 			    fac_walker, (void *)0, errp)) == NULL) {
386 				return (ret);
387 			}
388 			(void) topo_walk_step(twp, TOPO_WALK_CHILD);
389 			topo_walk_fini(twp);
390 		}
391 		return (ret);
392 	}
393 	return (topo_snap_log_create(thp, uuid, errp));
394 }
395 
396 /*ARGSUSED*/
397 static int
398 topo_walk_destroy(topo_hdl_t *thp, tnode_t *node, void *notused)
399 {
400 	tnode_t *cnode;
401 
402 	cnode = topo_child_first(node);
403 
404 	if (cnode != NULL)
405 		return (TOPO_WALK_NEXT);
406 
407 	topo_node_unbind(node);
408 
409 	return (TOPO_WALK_NEXT);
410 }
411 
412 static void
413 topo_snap_destroy(topo_hdl_t *thp)
414 {
415 	int i;
416 	ttree_t *tp;
417 	topo_walk_t *twp;
418 	tnode_t *root;
419 	topo_nodehash_t *nhp;
420 	topo_mod_t *mod;
421 
422 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
423 	    tp = topo_list_next(tp)) {
424 
425 		root = tp->tt_root;
426 		twp = tp->tt_walk;
427 		/*
428 		 * Clean-up tree nodes from the bottom-up
429 		 */
430 		if ((twp->tw_node = topo_child_first(root)) != NULL) {
431 			twp->tw_cb = topo_walk_destroy;
432 			topo_node_hold(root);
433 			topo_node_hold(twp->tw_node); /* released at walk end */
434 			(void) topo_walk_bottomup(twp, TOPO_WALK_CHILD);
435 			topo_node_rele(root);
436 		}
437 
438 		/*
439 		 * Tidy-up the root node
440 		 */
441 		while ((nhp = topo_list_next(&root->tn_children)) != NULL) {
442 			for (i = 0; i < nhp->th_arrlen; i++) {
443 				assert(nhp->th_nodearr[i] == NULL);
444 			}
445 			mod = nhp->th_enum;
446 			topo_mod_strfree(mod, nhp->th_name);
447 			topo_mod_free(mod, nhp->th_nodearr,
448 			    nhp->th_arrlen * sizeof (tnode_t *));
449 			topo_list_delete(&root->tn_children, nhp);
450 			topo_mod_free(mod, nhp, sizeof (topo_nodehash_t));
451 			topo_mod_rele(mod);
452 		}
453 
454 	}
455 
456 	/*
457 	 * Clean-up our cached devinfo and prom tree handles.
458 	 */
459 	if (thp->th_di != DI_NODE_NIL) {
460 		di_fini(thp->th_di);
461 		thp->th_di = DI_NODE_NIL;
462 	}
463 	if (thp->th_pi != DI_PROM_HANDLE_NIL) {
464 		di_prom_fini(thp->th_pi);
465 		thp->th_pi = DI_PROM_HANDLE_NIL;
466 	}
467 
468 
469 	if (thp->th_uuid != NULL) {
470 		topo_hdl_free(thp, thp->th_uuid, TOPO_UUID_SIZE);
471 		thp->th_uuid = NULL;
472 	}
473 }
474 
475 void
476 topo_snap_release(topo_hdl_t *thp)
477 {
478 	if (thp == NULL)
479 		return;
480 
481 	topo_hdl_lock(thp);
482 	topo_snap_destroy(thp);
483 	topo_hdl_unlock(thp);
484 }
485 
486 topo_walk_t *
487 topo_walk_init(topo_hdl_t *thp, const char *scheme, topo_walk_cb_t cb_f,
488     void *pdata, int *errp)
489 {
490 	ttree_t *tp;
491 	topo_walk_t *wp;
492 
493 	for (tp = topo_list_next(&thp->th_trees); tp != NULL;
494 	    tp = topo_list_next(tp)) {
495 		if (strcmp(scheme, tp->tt_scheme) == 0) {
496 
497 			/*
498 			 * Hold the root node and start walk at the first
499 			 * child node
500 			 */
501 			assert(tp->tt_root != NULL);
502 
503 			if ((wp = topo_node_walk_init(thp, NULL, tp->tt_root,
504 			    cb_f, pdata, errp)) == NULL) /* errp set */
505 				return (NULL);
506 
507 			return (wp);
508 		}
509 	}
510 
511 	*errp = ETOPO_WALK_NOTFOUND;
512 	return (NULL);
513 }
514 
515 static int
516 step_child(tnode_t *cnp, topo_walk_t *wp, int flag, int bottomup)
517 {
518 	int status;
519 	tnode_t *nnp;
520 
521 	nnp = topo_child_first(cnp);
522 
523 	if (nnp == NULL) {
524 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
525 		    "step_child: TOPO_WALK_TERMINATE for %s=%d\n",
526 		    cnp->tn_name, cnp->tn_instance);
527 		return (TOPO_WALK_TERMINATE);
528 	}
529 
530 	topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
531 	    "step_child: walk through node %s=%d to %s=%d\n",
532 	    cnp->tn_name, cnp->tn_instance, nnp->tn_name, nnp->tn_instance);
533 
534 	topo_node_hold(nnp); /* released on return from walk_step */
535 	wp->tw_node = nnp;
536 	if (bottomup == 1)
537 		status = topo_walk_bottomup(wp, flag);
538 	else
539 		status = topo_walk_step(wp, flag);
540 
541 	return (status);
542 }
543 
544 static int
545 step_sibling(tnode_t *cnp, topo_walk_t *wp, int flag, int bottomup)
546 {
547 	int status;
548 	tnode_t *nnp;
549 
550 	nnp = topo_child_next(cnp->tn_parent, cnp);
551 
552 	if (nnp == NULL) {
553 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
554 		    "step_sibling: TOPO_WALK_TERMINATE for %s=%d\n",
555 		    cnp->tn_name, cnp->tn_instance);
556 		return (TOPO_WALK_TERMINATE);
557 	}
558 
559 	topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
560 	    "step_sibling: through sibling node %s=%d to %s=%d\n",
561 	    cnp->tn_name, cnp->tn_instance, nnp->tn_name, nnp->tn_instance);
562 
563 	topo_node_hold(nnp); /* released on return from walk_step */
564 	wp->tw_node = nnp;
565 	if (bottomup == 1)
566 		status = topo_walk_bottomup(wp, flag);
567 	else
568 		status = topo_walk_step(wp, flag);
569 
570 	return (status);
571 }
572 
573 int
574 topo_walk_byid(topo_walk_t *wp, const char *name, topo_instance_t inst)
575 {
576 	int status;
577 	tnode_t *nnp, *cnp;
578 
579 	cnp = wp->tw_node;
580 	nnp = topo_node_lookup(cnp, name, inst);
581 	if (nnp == NULL)
582 		return (TOPO_WALK_TERMINATE);
583 
584 	topo_node_hold(nnp);
585 	wp->tw_node = nnp;
586 	if (wp->tw_mod != NULL)
587 		status = wp->tw_cb(wp->tw_mod, nnp, wp->tw_pdata);
588 	else
589 		status = wp->tw_cb(wp->tw_thp, nnp, wp->tw_pdata);
590 	topo_node_rele(nnp);
591 	wp->tw_node = cnp;
592 
593 	return (status);
594 }
595 
596 int
597 topo_walk_bysibling(topo_walk_t *wp, const char *name, topo_instance_t inst)
598 {
599 	int status;
600 	tnode_t *cnp, *pnp;
601 
602 	cnp = wp->tw_node;
603 	pnp = topo_node_parent(cnp);
604 	assert(pnp != NULL);
605 
606 	topo_node_hold(pnp);
607 	wp->tw_node = pnp;
608 	status = topo_walk_byid(wp, name, inst);
609 	topo_node_rele(pnp);
610 	wp->tw_node = cnp;
611 
612 	return (status);
613 }
614 
615 int
616 topo_walk_step(topo_walk_t *wp, int flag)
617 {
618 	int status;
619 	tnode_t *cnp = wp->tw_node;
620 
621 	if (flag != TOPO_WALK_CHILD && flag != TOPO_WALK_SIBLING) {
622 		topo_node_rele(cnp);
623 		return (TOPO_WALK_ERR);
624 	}
625 
626 	/*
627 	 * No more nodes to walk
628 	 */
629 	if (cnp == NULL) {
630 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
631 		    "walk_step terminated\n");
632 		topo_node_rele(cnp);
633 		return (TOPO_WALK_TERMINATE);
634 	}
635 
636 
637 	if (wp->tw_mod != NULL)
638 		status = wp->tw_cb(wp->tw_mod, cnp, wp->tw_pdata);
639 	else
640 		status = wp->tw_cb(wp->tw_thp, cnp, wp->tw_pdata);
641 
642 	/*
643 	 * Walker callback says we're done
644 	 */
645 	if (status != TOPO_WALK_NEXT) {
646 		topo_node_rele(cnp);
647 		return (status);
648 	}
649 
650 	if (flag == TOPO_WALK_CHILD)
651 		status = step_child(cnp, wp, flag, 0);
652 	else
653 		status = step_sibling(cnp, wp, flag, 0);
654 
655 	/*
656 	 * No more nodes in this hash, skip to next node hash by stepping
657 	 * to next sibling (child-first walk) or next child (sibling-first
658 	 * walk).
659 	 */
660 	if (status == TOPO_WALK_TERMINATE) {
661 		if (flag == TOPO_WALK_CHILD)
662 			status = step_sibling(cnp, wp, flag, 0);
663 		else
664 			status = step_child(cnp, wp, flag, 0);
665 	}
666 
667 	topo_node_rele(cnp); /* done with current node */
668 
669 	return (status);
670 }
671 
672 void
673 topo_walk_fini(topo_walk_t *wp)
674 {
675 	if (wp == NULL)
676 		return;
677 
678 	topo_node_rele(wp->tw_root);
679 
680 	topo_hdl_free(wp->tw_thp, wp, sizeof (topo_walk_t));
681 }
682 
683 int
684 topo_walk_bottomup(topo_walk_t *wp, int flag)
685 {
686 	int status;
687 	tnode_t *cnp;
688 
689 	if (wp == NULL)
690 		return (TOPO_WALK_ERR);
691 
692 	cnp = wp->tw_node;
693 	if (flag != TOPO_WALK_CHILD && flag != TOPO_WALK_SIBLING) {
694 		topo_node_rele(cnp);
695 		return (TOPO_WALK_ERR);
696 	}
697 
698 	/*
699 	 * End of the line
700 	 */
701 	if (cnp == NULL) {
702 		topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
703 		    "walk_bottomup terminated\n");
704 		topo_node_rele(cnp);
705 		return (TOPO_WALK_TERMINATE);
706 	}
707 
708 	topo_dprintf(wp->tw_thp, TOPO_DBG_WALK,
709 	    "%s walk_bottomup through node %s=%d\n",
710 	    (flag == TOPO_WALK_CHILD ? "TOPO_WALK_CHILD" : "TOPO_WALK_SIBLING"),
711 	    cnp->tn_name, cnp->tn_instance);
712 
713 	if (flag == TOPO_WALK_CHILD)
714 		status = step_child(cnp, wp, flag, 1);
715 	else
716 		status = step_sibling(cnp, wp, flag, 1);
717 
718 	/*
719 	 * At a leaf, run the callback
720 	 */
721 	if (status == TOPO_WALK_TERMINATE) {
722 		if ((status = wp->tw_cb(wp->tw_thp, cnp, wp->tw_pdata))
723 		    != TOPO_WALK_NEXT) {
724 			topo_node_rele(cnp);
725 			return (status);
726 		}
727 	}
728 
729 	/*
730 	 * Try next child or sibling
731 	 */
732 	if (status == TOPO_WALK_NEXT) {
733 		if (flag == TOPO_WALK_CHILD)
734 			status = step_sibling(cnp, wp, flag, 1);
735 		else
736 			status = step_child(cnp, wp, flag, 1);
737 	}
738 
739 	topo_node_rele(cnp); /* done with current node */
740 
741 	return (status);
742 }
743 
744 di_node_t
745 topo_hdl_devinfo(topo_hdl_t *thp)
746 {
747 	return (thp == NULL ? DI_NODE_NIL : thp->th_di);
748 }
749 
750 di_prom_handle_t
751 topo_hdl_prominfo(topo_hdl_t *thp)
752 {
753 	return (thp == NULL ? DI_PROM_HANDLE_NIL : thp->th_pi);
754 }
755