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