xref: /illumos-gate/usr/src/lib/libdevinfo/devinfo.c (revision 602ca9ea)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53ebafc43Sjveta  * Common Development and Distribution License (the "License").
63ebafc43Sjveta  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*602ca9eaScth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Interfaces for getting device configuration data from kernel
307c478bd9Sstevel@tonic-gate  * through the devinfo driver.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <poll.h>
407c478bd9Sstevel@tonic-gate #include <synch.h>
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
437c478bd9Sstevel@tonic-gate #include <sys/obpdefs.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/time.h>
477c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
487c478bd9Sstevel@tonic-gate #include <stdarg.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	NDEBUG 1
517c478bd9Sstevel@tonic-gate #include <assert.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "libdevinfo.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Debug message levels
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate typedef enum {
597c478bd9Sstevel@tonic-gate 	DI_QUIET = 0,	/* No debug messages - the default */
607c478bd9Sstevel@tonic-gate 	DI_ERR = 1,
617c478bd9Sstevel@tonic-gate 	DI_INFO,
627c478bd9Sstevel@tonic-gate 	DI_TRACE,
637c478bd9Sstevel@tonic-gate 	DI_TRACE1,
647c478bd9Sstevel@tonic-gate 	DI_TRACE2
657c478bd9Sstevel@tonic-gate } di_debug_t;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate int di_debug = DI_QUIET;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	DPRINTF(args)	{ if (di_debug != DI_QUIET) dprint args; }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void dprint(di_debug_t msglevel, const char *fmt, ...);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #pragma init(_libdevinfo_init)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate void
777c478bd9Sstevel@tonic-gate _libdevinfo_init()
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	char	*debug_str = getenv("_LIBDEVINFO_DEBUG");
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (debug_str) {
827c478bd9Sstevel@tonic-gate 		errno = 0;
837c478bd9Sstevel@tonic-gate 		di_debug = atoi(debug_str);
847c478bd9Sstevel@tonic-gate 		if (errno || di_debug < DI_QUIET)
857c478bd9Sstevel@tonic-gate 			di_debug = DI_QUIET;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate di_node_t
907c478bd9Sstevel@tonic-gate di_init(const char *phys_path, uint_t flag)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	return (di_init_impl(phys_path, flag, NULL));
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * We use blocking_open() to guarantee access to the devinfo device, if open()
977c478bd9Sstevel@tonic-gate  * is failing with EAGAIN.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static int
1007c478bd9Sstevel@tonic-gate blocking_open(const char *path, int oflag)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	int fd;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	while ((fd = open(path, oflag)) == -1 && errno == EAGAIN)
1057c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 1 * MILLISEC);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	return (fd);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* private interface */
1117c478bd9Sstevel@tonic-gate di_node_t
1127c478bd9Sstevel@tonic-gate di_init_driver(const char *drv_name, uint_t flag)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	int fd;
1157c478bd9Sstevel@tonic-gate 	char driver[MAXPATHLEN];
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/*
1187c478bd9Sstevel@tonic-gate 	 * Don't allow drv_name to exceed MAXPATHLEN - 1, or 1023,
1197c478bd9Sstevel@tonic-gate 	 * which should be sufficient for any sensible programmer.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	if ((drv_name == NULL) || (strlen(drv_name) >= MAXPATHLEN)) {
1227c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1237c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	(void) strcpy(driver, drv_name);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/*
1287c478bd9Sstevel@tonic-gate 	 * open the devinfo driver
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo",
1317c478bd9Sstevel@tonic-gate 	    O_RDONLY)) == -1) {
1327c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n", errno));
1337c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DINFOLODRV, driver) != 0) {
1377c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "failed to load driver %s\n", driver));
1387c478bd9Sstevel@tonic-gate 		(void) close(fd);
1397c478bd9Sstevel@tonic-gate 		errno = ENXIO;
1407c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	(void) close(fd);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * Driver load succeeded, return a snapshot
1467c478bd9Sstevel@tonic-gate 	 */
1477c478bd9Sstevel@tonic-gate 	return (di_init("/", flag));
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate di_node_t
1517c478bd9Sstevel@tonic-gate di_init_impl(const char *phys_path, uint_t flag,
1527c478bd9Sstevel@tonic-gate 	struct di_priv_data *priv)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	caddr_t pa;
1557c478bd9Sstevel@tonic-gate 	int fd, map_size;
1567c478bd9Sstevel@tonic-gate 	struct di_all *dap;
1577c478bd9Sstevel@tonic-gate 	struct dinfo_io dinfo_io;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	uint_t pageoffset = sysconf(_SC_PAGESIZE) - 1;
1607c478bd9Sstevel@tonic-gate 	uint_t pagemask = ~pageoffset;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_init: taking a snapshot\n"));
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/*
1657c478bd9Sstevel@tonic-gate 	 * Make sure there is no minor name in the path
1667c478bd9Sstevel@tonic-gate 	 * and the path do not start with /devices....
1677c478bd9Sstevel@tonic-gate 	 */
1687c478bd9Sstevel@tonic-gate 	if (strchr(phys_path, ':') ||
1697c478bd9Sstevel@tonic-gate 	    (strncmp(phys_path, "/devices", 8) == 0) ||
1707c478bd9Sstevel@tonic-gate 	    (strlen(phys_path) > MAXPATHLEN)) {
1717c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1727c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	if (strlen(phys_path) == 0)
1767c478bd9Sstevel@tonic-gate 		(void) sprintf(dinfo_io.root_path, "/");
1777c478bd9Sstevel@tonic-gate 	else if (*phys_path != '/')
1787c478bd9Sstevel@tonic-gate 		(void) snprintf(dinfo_io.root_path, sizeof (dinfo_io.root_path),
1797c478bd9Sstevel@tonic-gate 		    "/%s", phys_path);
1807c478bd9Sstevel@tonic-gate 	else
1817c478bd9Sstevel@tonic-gate 		(void) snprintf(dinfo_io.root_path, sizeof (dinfo_io.root_path),
1827c478bd9Sstevel@tonic-gate 		    "%s", phys_path);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/*
1857c478bd9Sstevel@tonic-gate 	 * If private data is requested, copy the format specification
1867c478bd9Sstevel@tonic-gate 	 */
1877c478bd9Sstevel@tonic-gate 	if (flag & DINFOPRIVDATA & 0xff) {
1887c478bd9Sstevel@tonic-gate 		if (priv)
1897c478bd9Sstevel@tonic-gate 			bcopy(priv, &dinfo_io.priv,
1907c478bd9Sstevel@tonic-gate 			    sizeof (struct di_priv_data));
1917c478bd9Sstevel@tonic-gate 		else {
1927c478bd9Sstevel@tonic-gate 			errno = EINVAL;
1937c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/*
1987c478bd9Sstevel@tonic-gate 	 * Attempt to open the devinfo driver.  Make a second attempt at the
1997c478bd9Sstevel@tonic-gate 	 * read-only minor node if we don't have privileges to open the full
2007c478bd9Sstevel@tonic-gate 	 * version _and_ if we're not requesting operations that the read-only
2017c478bd9Sstevel@tonic-gate 	 * node can't perform.  (Setgid processes would fail an access() test,
2027c478bd9Sstevel@tonic-gate 	 * of course.)
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo",
2057c478bd9Sstevel@tonic-gate 	    O_RDONLY)) == -1) {
2067c478bd9Sstevel@tonic-gate 		if ((flag & DINFOFORCE) == DINFOFORCE ||
2077c478bd9Sstevel@tonic-gate 		    (flag & DINFOPRIVDATA) == DINFOPRIVDATA) {
2087c478bd9Sstevel@tonic-gate 			/*
2097c478bd9Sstevel@tonic-gate 			 * We wanted to perform a privileged operation, but the
2107c478bd9Sstevel@tonic-gate 			 * privileged node isn't available.  Don't modify errno
2117c478bd9Sstevel@tonic-gate 			 * on our way out (but display it if we're running with
2127c478bd9Sstevel@tonic-gate 			 * di_debug set).
2137c478bd9Sstevel@tonic-gate 			 */
2147c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n",
2157c478bd9Sstevel@tonic-gate 			    errno));
2167c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo,ro",
2207c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == -1) {
2217c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n",
2227c478bd9Sstevel@tonic-gate 			    errno));
2237c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * Verify that there is no major conflict, i.e., we are indeed opening
2297c478bd9Sstevel@tonic-gate 	 * the devinfo driver.
2307c478bd9Sstevel@tonic-gate 	 */
2317c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DINFOIDENT, NULL) != DI_MAGIC) {
2327c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR,
2337c478bd9Sstevel@tonic-gate 		    "driver ID failed; check for major conflict\n"));
2347c478bd9Sstevel@tonic-gate 		(void) close(fd);
2357c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	/*
2397c478bd9Sstevel@tonic-gate 	 * create snapshot
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	if ((map_size = ioctl(fd, flag, &dinfo_io)) < 0) {
2427c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "devinfo ioctl failed with "
2437c478bd9Sstevel@tonic-gate 		    "error: %d\n", errno));
2447c478bd9Sstevel@tonic-gate 		(void) close(fd);
2457c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2467c478bd9Sstevel@tonic-gate 	} else if (map_size == 0) {
2477c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "%s not found\n", phys_path));
2487c478bd9Sstevel@tonic-gate 		errno = ENXIO;
2497c478bd9Sstevel@tonic-gate 		(void) close(fd);
2507c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/*
2547c478bd9Sstevel@tonic-gate 	 * copy snapshot to userland
2557c478bd9Sstevel@tonic-gate 	 */
2567c478bd9Sstevel@tonic-gate 	map_size = (map_size + pageoffset) & pagemask;
2577c478bd9Sstevel@tonic-gate 	if ((pa = valloc(map_size)) == NULL) {
2587c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "valloc failed for snapshot\n"));
2597c478bd9Sstevel@tonic-gate 		(void) close(fd);
2607c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DINFOUSRLD, pa) != map_size) {
2647c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "failed to copy snapshot to usrld\n"));
2657c478bd9Sstevel@tonic-gate 		(void) close(fd);
2667c478bd9Sstevel@tonic-gate 		free(pa);
2677c478bd9Sstevel@tonic-gate 		errno = EFAULT;
2687c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	(void) close(fd);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	dap = DI_ALL(pa);
2743e2676e0Svikram 	if (dap->version != DI_SNAPSHOT_VERSION) {
2753e2676e0Svikram 		DPRINTF((DI_ERR, "wrong snapshot version "
2763e2676e0Svikram 		    "(expected=%d, actual=%d)\n",
2773e2676e0Svikram 		    DI_SNAPSHOT_VERSION, dap->version));
2783e2676e0Svikram 		free(pa);
2793e2676e0Svikram 		errno = ESTALE;
2803e2676e0Svikram 		return (DI_NODE_NIL);
2813e2676e0Svikram 	}
2827c478bd9Sstevel@tonic-gate 	if (dap->top_devinfo == 0) {	/* phys_path not found */
2837c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "%s not found\n", phys_path));
2847c478bd9Sstevel@tonic-gate 		free(pa);
2857c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2867c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	return (DI_NODE(pa + dap->top_devinfo));
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate void
2937c478bd9Sstevel@tonic-gate di_fini(di_node_t root)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_fini: freeing a snapshot\n"));
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * paranoid checking
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
3037c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "di_fini called with NIL arg\n"));
3047c478bd9Sstevel@tonic-gate 		return;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * The root contains its own offset--self.
3097c478bd9Sstevel@tonic-gate 	 * Subtracting it from root address, we get the starting addr.
3107c478bd9Sstevel@tonic-gate 	 * The map_size is stored at the beginning of snapshot.
3117c478bd9Sstevel@tonic-gate 	 * Once we have starting address and size, we can free().
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	pa = (caddr_t)root - DI_NODE(root)->self;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	free(pa);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate di_node_t
3197c478bd9Sstevel@tonic-gate di_parent_node(di_node_t node)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3247c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3257c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get parent of node %s\n", di_node_name(node)));
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent) {
3337c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_NODE(node)->parent));
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
3387c478bd9Sstevel@tonic-gate 	 *   If parent doesn't exist and node is not the root,
3397c478bd9Sstevel@tonic-gate 	 *   set errno to ENOTSUP. Otherwise, set errno to ENXIO.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	if (strcmp(DI_ALL(pa)->root_path, "/") != 0)
3427c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
3437c478bd9Sstevel@tonic-gate 	else
3447c478bd9Sstevel@tonic-gate 		errno = ENXIO;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate di_node_t
3507c478bd9Sstevel@tonic-gate di_sibling_node(di_node_t node)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3557c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3567c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get sibling of node %s\n", di_node_name(node)));
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->sibling) {
3647c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_NODE(node)->sibling));
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
3697c478bd9Sstevel@tonic-gate 	 *   Sibling doesn't exist, figure out if ioctl command
3707c478bd9Sstevel@tonic-gate 	 *   has DINFOSUBTREE set. If it doesn't, set errno to
3717c478bd9Sstevel@tonic-gate 	 *   ENOTSUP.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	if (!(DI_ALL(pa)->command & DINFOSUBTREE))
3747c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
3757c478bd9Sstevel@tonic-gate 	else
3767c478bd9Sstevel@tonic-gate 		errno = ENXIO;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate di_node_t
3827c478bd9Sstevel@tonic-gate di_child_node(di_node_t node)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get child of node %s\n", di_node_name(node)));
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3897c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3907c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->child) {
3967c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_NODE(node)->child));
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/*
4007c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
4017c478bd9Sstevel@tonic-gate 	 *   Child doesn't exist, figure out if DINFOSUBTREE is set.
4027c478bd9Sstevel@tonic-gate 	 *   If it isn't, set errno to ENOTSUP.
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 	if (!(DI_ALL(pa)->command & DINFOSUBTREE))
4057c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
4067c478bd9Sstevel@tonic-gate 	else
4077c478bd9Sstevel@tonic-gate 		errno = ENXIO;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate di_node_t
4137c478bd9Sstevel@tonic-gate di_drv_first_node(const char *drv_name, di_node_t root)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	caddr_t		pa;		/* starting address of map */
4167c478bd9Sstevel@tonic-gate 	int		major, devcnt;
4177c478bd9Sstevel@tonic-gate 	struct di_devnm	*devnm;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Get first node of driver %s\n", drv_name));
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
4227c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4237c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * get major number of driver
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	pa = (caddr_t)root - DI_NODE(root)->self;
4307c478bd9Sstevel@tonic-gate 	devcnt = DI_ALL(pa)->devcnt;
4317c478bd9Sstevel@tonic-gate 	devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	for (major = 0; major < devcnt; major++)
4347c478bd9Sstevel@tonic-gate 		if (devnm[major].name && (strcmp(drv_name,
4357c478bd9Sstevel@tonic-gate 		    (char *)(pa + devnm[major].name)) == 0))
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (major >= devcnt) {
4397c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4407c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if (!(devnm[major].head)) {
4447c478bd9Sstevel@tonic-gate 		errno = ENXIO;
4457c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	return (DI_NODE(pa + devnm[major].head));
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate di_node_t
4527c478bd9Sstevel@tonic-gate di_drv_next_node(di_node_t node)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	caddr_t		pa;		/* starting address of map */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
4577c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4587c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "next node on per driver list:"
4627c478bd9Sstevel@tonic-gate 	    " current=%s, driver=%s\n",
4637c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_driver_name(node)));
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->next == (di_off_t)-1) {
4667c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
4677c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->next == NULL) {
4737c478bd9Sstevel@tonic-gate 		errno = ENXIO;
4747c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	return (DI_NODE(pa + DI_NODE(node)->next));
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Internal library interfaces:
4827c478bd9Sstevel@tonic-gate  *   node_list etc. for node walking
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate struct node_list {
4857c478bd9Sstevel@tonic-gate 	struct node_list *next;
4867c478bd9Sstevel@tonic-gate 	di_node_t node;
4877c478bd9Sstevel@tonic-gate };
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate static void
4907c478bd9Sstevel@tonic-gate free_node_list(struct node_list **headp)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	struct node_list *tmp;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	while (*headp) {
4957c478bd9Sstevel@tonic-gate 		tmp = *headp;
4967c478bd9Sstevel@tonic-gate 		*headp = (*headp)->next;
4977c478bd9Sstevel@tonic-gate 		free(tmp);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate static void
5027c478bd9Sstevel@tonic-gate append_node_list(struct node_list **headp, struct node_list *list)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	struct node_list *tmp;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (*headp == NULL) {
5077c478bd9Sstevel@tonic-gate 		*headp = list;
5087c478bd9Sstevel@tonic-gate 		return;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (list == NULL)	/* a minor optimization */
5127c478bd9Sstevel@tonic-gate 		return;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	tmp = *headp;
5157c478bd9Sstevel@tonic-gate 	while (tmp->next)
5167c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	tmp->next = list;
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate static void
5227c478bd9Sstevel@tonic-gate prepend_node_list(struct node_list **headp, struct node_list *list)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	struct node_list *tmp;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (list == NULL)
5277c478bd9Sstevel@tonic-gate 		return;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	tmp = *headp;
5307c478bd9Sstevel@tonic-gate 	*headp = list;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (tmp == NULL)	/* a minor optimization */
5337c478bd9Sstevel@tonic-gate 		return;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	while (list->next)
5367c478bd9Sstevel@tonic-gate 		list = list->next;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	list->next = tmp;
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate  * returns 1 if node is a descendant of parent, 0 otherwise
5437c478bd9Sstevel@tonic-gate  */
5447c478bd9Sstevel@tonic-gate static int
5457c478bd9Sstevel@tonic-gate is_descendant(di_node_t node, di_node_t parent)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * DI_NODE_NIL is parent of root, so it is
5497c478bd9Sstevel@tonic-gate 	 * the parent of all nodes.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	if (parent == DI_NODE_NIL) {
5527c478bd9Sstevel@tonic-gate 		return (1);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	do {
5567c478bd9Sstevel@tonic-gate 		node = di_parent_node(node);
5577c478bd9Sstevel@tonic-gate 	} while ((node != DI_NODE_NIL) && (node != parent));
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	return (node != DI_NODE_NIL);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate  * Insert list before the first node which is NOT a descendent of parent.
5647c478bd9Sstevel@tonic-gate  * This is needed to reproduce the exact walking order of link generators.
5657c478bd9Sstevel@tonic-gate  */
5667c478bd9Sstevel@tonic-gate static void
5677c478bd9Sstevel@tonic-gate insert_node_list(struct node_list **headp, struct node_list *list,
5687c478bd9Sstevel@tonic-gate     di_node_t parent)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	struct node_list *tmp, *tmp1;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (list == NULL)
5737c478bd9Sstevel@tonic-gate 		return;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	tmp = *headp;
5767c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {	/* a minor optimization */
5777c478bd9Sstevel@tonic-gate 		*headp = list;
5787c478bd9Sstevel@tonic-gate 		return;
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (!is_descendant(tmp->node, parent)) {
5827c478bd9Sstevel@tonic-gate 		prepend_node_list(headp, list);
5837c478bd9Sstevel@tonic-gate 		return;
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * Find first node which is not a descendant
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 	while (tmp->next && is_descendant(tmp->next->node, parent)) {
5907c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	tmp1 = tmp->next;
5947c478bd9Sstevel@tonic-gate 	tmp->next = list;
5957c478bd9Sstevel@tonic-gate 	append_node_list(headp, tmp1);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  *   Get a linked list of handles of all children
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate static struct node_list *
6027c478bd9Sstevel@tonic-gate get_children(di_node_t node)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	di_node_t child;
6057c478bd9Sstevel@tonic-gate 	struct node_list *result, *tmp;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE1, "Get children of node %s\n", di_node_name(node)));
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if ((child = di_child_node(node)) == DI_NODE_NIL) {
6107c478bd9Sstevel@tonic-gate 		return (NULL);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if ((result = malloc(sizeof (struct node_list))) == NULL) {
6147c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
6157c478bd9Sstevel@tonic-gate 		return (NULL);
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	result->node = child;
6197c478bd9Sstevel@tonic-gate 	tmp = result;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	while ((child = di_sibling_node(tmp->node)) != DI_NODE_NIL) {
6227c478bd9Sstevel@tonic-gate 		if ((tmp->next = malloc(sizeof (struct node_list))) == NULL) {
6237c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "malloc of node_list failed\n"));
6247c478bd9Sstevel@tonic-gate 			free_node_list(&result);
6257c478bd9Sstevel@tonic-gate 			return (NULL);
6267c478bd9Sstevel@tonic-gate 		}
6277c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
6287c478bd9Sstevel@tonic-gate 		tmp->node = child;
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	tmp->next = NULL;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	return (result);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * Internal library interface:
6387c478bd9Sstevel@tonic-gate  *   Delete all siblings of the first node from the node_list, along with
6397c478bd9Sstevel@tonic-gate  *   the first node itself.
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate static void
6427c478bd9Sstevel@tonic-gate prune_sib(struct node_list **headp)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	di_node_t parent, curr_par, curr_gpar;
6457c478bd9Sstevel@tonic-gate 	struct node_list *curr, *prev;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * get handle to parent of first node
6497c478bd9Sstevel@tonic-gate 	 */
6507c478bd9Sstevel@tonic-gate 	if ((parent = di_parent_node((*headp)->node)) == DI_NODE_NIL) {
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * This must be the root of the snapshot, so can't
6537c478bd9Sstevel@tonic-gate 		 * have any siblings.
6547c478bd9Sstevel@tonic-gate 		 *
6557c478bd9Sstevel@tonic-gate 		 * XXX Put a check here just in case.
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		if ((*headp)->next)
6587c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "Unexpected err in di_walk_node.\n"));
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		free(*headp);
6617c478bd9Sstevel@tonic-gate 		*headp = NULL;
6627c478bd9Sstevel@tonic-gate 		return;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/*
6667c478bd9Sstevel@tonic-gate 	 * To be complete, we should also delete the children
6677c478bd9Sstevel@tonic-gate 	 * of siblings that have already been visited.
6687c478bd9Sstevel@tonic-gate 	 * This happens for DI_WALK_SIBFIRST when the first node
6697c478bd9Sstevel@tonic-gate 	 * is NOT the first in the linked list of siblings.
6707c478bd9Sstevel@tonic-gate 	 *
6717c478bd9Sstevel@tonic-gate 	 * Hence, we compare parent with BOTH the parent and grandparent
6727c478bd9Sstevel@tonic-gate 	 * of nodes, and delete node is a match is found.
6737c478bd9Sstevel@tonic-gate 	 */
6747c478bd9Sstevel@tonic-gate 	prev = *headp;
6757c478bd9Sstevel@tonic-gate 	curr = prev->next;
6767c478bd9Sstevel@tonic-gate 	while (curr) {
6777c478bd9Sstevel@tonic-gate 		if (((curr_par = di_parent_node(curr->node)) != DI_NODE_NIL) &&
6787c478bd9Sstevel@tonic-gate 		    ((curr_par == parent) || ((curr_gpar =
6797c478bd9Sstevel@tonic-gate 		    di_parent_node(curr_par)) != DI_NODE_NIL) &&
6807c478bd9Sstevel@tonic-gate 		    (curr_gpar == parent))) {
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * match parent/grandparent: delete curr
6837c478bd9Sstevel@tonic-gate 			 */
6847c478bd9Sstevel@tonic-gate 			prev->next = curr->next;
6857c478bd9Sstevel@tonic-gate 			free(curr);
6867c478bd9Sstevel@tonic-gate 			curr = prev->next;
6877c478bd9Sstevel@tonic-gate 		} else
6887c478bd9Sstevel@tonic-gate 			curr = curr->next;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * delete the first node
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	curr = *headp;
6957c478bd9Sstevel@tonic-gate 	*headp = curr->next;
6967c478bd9Sstevel@tonic-gate 	free(curr);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * Internal library function:
7017c478bd9Sstevel@tonic-gate  *	Update node list based on action (return code from callback)
7027c478bd9Sstevel@tonic-gate  *	and flag specifying walking behavior.
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate static void
7057c478bd9Sstevel@tonic-gate update_node_list(int action, uint_t flag, struct node_list **headp)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	struct node_list *children, *tmp;
7087c478bd9Sstevel@tonic-gate 	di_node_t parent = di_parent_node((*headp)->node);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	switch (action) {
7117c478bd9Sstevel@tonic-gate 	case DI_WALK_TERMINATE:
7127c478bd9Sstevel@tonic-gate 		/*
7137c478bd9Sstevel@tonic-gate 		 * free the node list and be done
7147c478bd9Sstevel@tonic-gate 		 */
7157c478bd9Sstevel@tonic-gate 		children = NULL;
7167c478bd9Sstevel@tonic-gate 		free_node_list(headp);
7177c478bd9Sstevel@tonic-gate 		break;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	case DI_WALK_PRUNESIB:
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * Get list of children and prune siblings
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		children = get_children((*headp)->node);
7247c478bd9Sstevel@tonic-gate 		prune_sib(headp);
7257c478bd9Sstevel@tonic-gate 		break;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	case DI_WALK_PRUNECHILD:
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * Set children to NULL and pop first node
7307c478bd9Sstevel@tonic-gate 		 */
7317c478bd9Sstevel@tonic-gate 		children = NULL;
7327c478bd9Sstevel@tonic-gate 		tmp = *headp;
7337c478bd9Sstevel@tonic-gate 		*headp = tmp->next;
7347c478bd9Sstevel@tonic-gate 		free(tmp);
7357c478bd9Sstevel@tonic-gate 		break;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	case DI_WALK_CONTINUE:
7387c478bd9Sstevel@tonic-gate 	default:
7397c478bd9Sstevel@tonic-gate 		/*
7407c478bd9Sstevel@tonic-gate 		 * Get list of children and pop first node
7417c478bd9Sstevel@tonic-gate 		 */
7427c478bd9Sstevel@tonic-gate 		children = get_children((*headp)->node);
7437c478bd9Sstevel@tonic-gate 		tmp = *headp;
7447c478bd9Sstevel@tonic-gate 		*headp = tmp->next;
7457c478bd9Sstevel@tonic-gate 		free(tmp);
7467c478bd9Sstevel@tonic-gate 		break;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * insert the list of children
7517c478bd9Sstevel@tonic-gate 	 */
7527c478bd9Sstevel@tonic-gate 	switch (flag) {
7537c478bd9Sstevel@tonic-gate 	case DI_WALK_CLDFIRST:
7547c478bd9Sstevel@tonic-gate 		prepend_node_list(headp, children);
7557c478bd9Sstevel@tonic-gate 		break;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	case DI_WALK_SIBFIRST:
7587c478bd9Sstevel@tonic-gate 		append_node_list(headp, children);
7597c478bd9Sstevel@tonic-gate 		break;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	case DI_WALK_LINKGEN:
7627c478bd9Sstevel@tonic-gate 	default:
7637c478bd9Sstevel@tonic-gate 		insert_node_list(headp, children, parent);
7647c478bd9Sstevel@tonic-gate 		break;
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate  * Internal library function:
7707c478bd9Sstevel@tonic-gate  *   Invoke callback on one node and update the list of nodes to be walked
7717c478bd9Sstevel@tonic-gate  *   based on the flag and return code.
7727c478bd9Sstevel@tonic-gate  */
7737c478bd9Sstevel@tonic-gate static void
7747c478bd9Sstevel@tonic-gate walk_one_node(struct node_list **headp, uint_t flag, void *arg,
7757c478bd9Sstevel@tonic-gate 	int (*callback)(di_node_t, void *))
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Walking node %s\n", di_node_name((*headp)->node)));
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	update_node_list(callback((*headp)->node, arg),
7807c478bd9Sstevel@tonic-gate 	    flag & DI_WALK_MASK, headp);
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate int
7847c478bd9Sstevel@tonic-gate di_walk_node(di_node_t root, uint_t flag, void *arg,
7857c478bd9Sstevel@tonic-gate 	int (*node_callback)(di_node_t, void *))
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (root == NULL) {
7907c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7917c478bd9Sstevel@tonic-gate 		return (-1);
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
7957c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
7967c478bd9Sstevel@tonic-gate 		return (-1);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	head->next = NULL;
8007c478bd9Sstevel@tonic-gate 	head->node = root;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start node walking from node %s\n",
8037c478bd9Sstevel@tonic-gate 	    di_node_name(root)));
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	while (head != NULL)
8067c478bd9Sstevel@tonic-gate 		walk_one_node(&head, flag, arg, node_callback);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	return (0);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * Internal library function:
8137c478bd9Sstevel@tonic-gate  *   Invoke callback for each minor on the minor list of first node
8147c478bd9Sstevel@tonic-gate  *   on node_list headp, and place children of first node on the list.
8157c478bd9Sstevel@tonic-gate  *
8167c478bd9Sstevel@tonic-gate  *   This is similar to walk_one_node, except we only walk in child
8177c478bd9Sstevel@tonic-gate  *   first mode.
8187c478bd9Sstevel@tonic-gate  */
8197c478bd9Sstevel@tonic-gate static void
8207c478bd9Sstevel@tonic-gate walk_one_minor_list(struct node_list **headp, const char *desired_type,
8217c478bd9Sstevel@tonic-gate 	uint_t flag, void *arg, int (*callback)(di_node_t, di_minor_t, void *))
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	int ddm_type;
8247c478bd9Sstevel@tonic-gate 	int action = DI_WALK_CONTINUE;
8257c478bd9Sstevel@tonic-gate 	char *node_type;
8267c478bd9Sstevel@tonic-gate 	di_minor_t minor = DI_MINOR_NIL;
8277c478bd9Sstevel@tonic-gate 	di_node_t node = (*headp)->node;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
8307c478bd9Sstevel@tonic-gate 		ddm_type = di_minor_type(minor);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		if ((ddm_type == DDM_ALIAS) && !(flag & DI_CHECK_ALIAS))
8337c478bd9Sstevel@tonic-gate 			continue;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		if ((ddm_type == DDM_INTERNAL_PATH) &&
8367c478bd9Sstevel@tonic-gate 		    !(flag & DI_CHECK_INTERNAL_PATH))
8377c478bd9Sstevel@tonic-gate 			continue;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		node_type = di_minor_nodetype(minor);
8407c478bd9Sstevel@tonic-gate 		if ((desired_type != NULL) && ((node_type == NULL) ||
8417c478bd9Sstevel@tonic-gate 		    strncmp(desired_type, node_type, strlen(desired_type))
8427c478bd9Sstevel@tonic-gate 		    != 0))
8437c478bd9Sstevel@tonic-gate 			continue;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		if ((action = callback(node, minor, arg)) ==
8467c478bd9Sstevel@tonic-gate 		    DI_WALK_TERMINATE) {
8477c478bd9Sstevel@tonic-gate 			break;
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	update_node_list(action, DI_WALK_LINKGEN, headp);
8527c478bd9Sstevel@tonic-gate }
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate int
8557c478bd9Sstevel@tonic-gate di_walk_minor(di_node_t root, const char *minor_type, uint_t flag, void *arg,
8567c478bd9Sstevel@tonic-gate 	int (*minor_callback)(di_node_t, di_minor_t, void *))
8577c478bd9Sstevel@tonic-gate {
858*602ca9eaScth 	struct node_list	*head;	/* node_list for tree walk */
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate #ifdef DEBUG
861*602ca9eaScth 	char	*devfspath = di_devfs_path(root);
862*602ca9eaScth 	DPRINTF((DI_INFO, "walking minor nodes under %s\n", devfspath));
863*602ca9eaScth 	di_devfs_path_free(devfspath);
8647c478bd9Sstevel@tonic-gate #endif
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if (root == NULL) {
8677c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8687c478bd9Sstevel@tonic-gate 		return (-1);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
8727c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
8737c478bd9Sstevel@tonic-gate 		return (-1);
8747c478bd9Sstevel@tonic-gate 	}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	head->next = NULL;
8777c478bd9Sstevel@tonic-gate 	head->node = root;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start minor walking from node %s\n",
880*602ca9eaScth 	    di_node_name(root)));
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	while (head != NULL)
8837c478bd9Sstevel@tonic-gate 		walk_one_minor_list(&head, minor_type, flag, arg,
8847c478bd9Sstevel@tonic-gate 		    minor_callback);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	return (0);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * generic node parameters
8917c478bd9Sstevel@tonic-gate  *   Calling these routines always succeeds.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate char *
8947c478bd9Sstevel@tonic-gate di_node_name(di_node_t node)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	return ((caddr_t)node + DI_NODE(node)->node_name - DI_NODE(node)->self);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate /* returns NULL ptr or a valid ptr to non-NULL string */
9007c478bd9Sstevel@tonic-gate char *
9017c478bd9Sstevel@tonic-gate di_bus_addr(di_node_t node)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	caddr_t pa = (caddr_t)node - DI_NODE(node)->self;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->address == 0)
9067c478bd9Sstevel@tonic-gate 		return (NULL);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	return ((char *)(pa + DI_NODE(node)->address));
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate char *
9127c478bd9Sstevel@tonic-gate di_binding_name(di_node_t node)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	caddr_t pa = (caddr_t)node - DI_NODE(node)->self;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->bind_name == 0)
9177c478bd9Sstevel@tonic-gate 		return (NULL);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	return ((char *)(pa + DI_NODE(node)->bind_name));
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate int
9237c478bd9Sstevel@tonic-gate di_compatible_names(di_node_t node, char **names)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	char *c;
9267c478bd9Sstevel@tonic-gate 	int len, size, entries = 0;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->compat_names == 0) {
9297c478bd9Sstevel@tonic-gate 		*names = NULL;
9307c478bd9Sstevel@tonic-gate 		return (0);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	*names = (caddr_t)node +
934*602ca9eaScth 	    DI_NODE(node)->compat_names - DI_NODE(node)->self;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	c = *names;
9377c478bd9Sstevel@tonic-gate 	len = DI_NODE(node)->compat_length;
9387c478bd9Sstevel@tonic-gate 	while (len > 0) {
9397c478bd9Sstevel@tonic-gate 		entries++;
9407c478bd9Sstevel@tonic-gate 		size = strlen(c) + 1;
9417c478bd9Sstevel@tonic-gate 		len -= size;
9427c478bd9Sstevel@tonic-gate 		c += size;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	return (entries);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate int
9497c478bd9Sstevel@tonic-gate di_instance(di_node_t node)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	return (DI_NODE(node)->instance);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate /*
9557c478bd9Sstevel@tonic-gate  * XXX: emulate the return value of the old implementation
9567c478bd9Sstevel@tonic-gate  * using info from devi_node_class and devi_node_attributes.
9577c478bd9Sstevel@tonic-gate  */
9587c478bd9Sstevel@tonic-gate int
9597c478bd9Sstevel@tonic-gate di_nodeid(di_node_t node)
9607c478bd9Sstevel@tonic-gate {
9617c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->node_class == DDI_NC_PROM)
9627c478bd9Sstevel@tonic-gate 		return (DI_PROM_NODEID);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->attributes & DDI_PERSISTENT)
9657c478bd9Sstevel@tonic-gate 		return (DI_SID_NODEID);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	return (DI_PSEUDO_NODEID);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate uint_t
9717c478bd9Sstevel@tonic-gate di_state(di_node_t node)
9727c478bd9Sstevel@tonic-gate {
9737c478bd9Sstevel@tonic-gate 	uint_t result = 0;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	if (di_node_state(node) < DS_ATTACHED)
9767c478bd9Sstevel@tonic-gate 		result |= DI_DRIVER_DETACHED;
9777c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_DEVICE_OFFLINE)
9787c478bd9Sstevel@tonic-gate 		result |= DI_DEVICE_OFFLINE;
9797c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_DEVICE_DOWN)
9807c478bd9Sstevel@tonic-gate 		result |= DI_DEVICE_OFFLINE;
9817c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_BUS_QUIESCED)
9827c478bd9Sstevel@tonic-gate 		result |= DI_BUS_QUIESCED;
9837c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_BUS_DOWN)
9847c478bd9Sstevel@tonic-gate 		result |= DI_BUS_DOWN;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	return (result);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate ddi_node_state_t
9907c478bd9Sstevel@tonic-gate di_node_state(di_node_t node)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	return (DI_NODE(node)->node_state);
9937c478bd9Sstevel@tonic-gate }
9947c478bd9Sstevel@tonic-gate 
9953e2676e0Svikram uint_t
9963e2676e0Svikram di_flags(di_node_t node)
9973e2676e0Svikram {
9983e2676e0Svikram 	return (DI_NODE(node)->flags);
9993e2676e0Svikram }
10003e2676e0Svikram 
100125e8c5aaSvikram uint_t
100225e8c5aaSvikram di_retired(di_node_t node)
100325e8c5aaSvikram {
100425e8c5aaSvikram 	return (di_flags(node) & DEVI_RETIRED);
100525e8c5aaSvikram }
100625e8c5aaSvikram 
10077c478bd9Sstevel@tonic-gate ddi_devid_t
10087c478bd9Sstevel@tonic-gate di_devid(di_node_t node)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->devid == 0)
10117c478bd9Sstevel@tonic-gate 		return (NULL);
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	return ((ddi_devid_t)((caddr_t)node +
10147c478bd9Sstevel@tonic-gate 	    DI_NODE(node)->devid - DI_NODE(node)->self));
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate int
10187c478bd9Sstevel@tonic-gate di_driver_major(di_node_t node)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	int major;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	major = DI_NODE(node)->drv_major;
10237c478bd9Sstevel@tonic-gate 	if (major < 0)
10247c478bd9Sstevel@tonic-gate 		return (-1);
10257c478bd9Sstevel@tonic-gate 	return (major);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate char *
10297c478bd9Sstevel@tonic-gate di_driver_name(di_node_t node)
10307c478bd9Sstevel@tonic-gate {
10317c478bd9Sstevel@tonic-gate 	int major;
10327c478bd9Sstevel@tonic-gate 	caddr_t pa;
10337c478bd9Sstevel@tonic-gate 	struct di_devnm *devnm;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	major = DI_NODE(node)->drv_major;
10367c478bd9Sstevel@tonic-gate 	if (major < 0)
10377c478bd9Sstevel@tonic-gate 		return (NULL);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
10407c478bd9Sstevel@tonic-gate 	devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	if (devnm[major].name)
10437c478bd9Sstevel@tonic-gate 		return (pa + devnm[major].name);
10447c478bd9Sstevel@tonic-gate 	else
10457c478bd9Sstevel@tonic-gate 		return (NULL);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate uint_t
10497c478bd9Sstevel@tonic-gate di_driver_ops(di_node_t node)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	int major;
10527c478bd9Sstevel@tonic-gate 	caddr_t pa;
10537c478bd9Sstevel@tonic-gate 	struct di_devnm *devnm;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	major = DI_NODE(node)->drv_major;
10567c478bd9Sstevel@tonic-gate 	if (major < 0)
10577c478bd9Sstevel@tonic-gate 		return (0);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
10607c478bd9Sstevel@tonic-gate 	devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	return (devnm[major].ops);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * returns the length of the path, caller must free memory
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate char *
10697c478bd9Sstevel@tonic-gate di_devfs_path(di_node_t node)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	caddr_t pa;
10727c478bd9Sstevel@tonic-gate 	di_node_t parent;
10737c478bd9Sstevel@tonic-gate 	int depth = 0, len = 0;
10747c478bd9Sstevel@tonic-gate 	char *buf, *name[MAX_TREE_DEPTH], *addr[MAX_TREE_DEPTH];
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
10777c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10787c478bd9Sstevel@tonic-gate 		return (NULL);
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	/*
10827c478bd9Sstevel@tonic-gate 	 * trace back to root, note the node_name & address
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 	while ((parent = di_parent_node(node)) != DI_NODE_NIL) {
10857c478bd9Sstevel@tonic-gate 		name[depth] = di_node_name(node);
10867c478bd9Sstevel@tonic-gate 		len += strlen(name[depth]) + 1;		/* 1 for '/' */
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		if ((addr[depth] = di_bus_addr(node)) != NULL)
10897c478bd9Sstevel@tonic-gate 			len += strlen(addr[depth]) + 1;	/* 1 for '@' */
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		node = parent;
10927c478bd9Sstevel@tonic-gate 		depth++;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	/*
10967c478bd9Sstevel@tonic-gate 	 * get the path to the root of snapshot
10977c478bd9Sstevel@tonic-gate 	 */
10987c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
10997c478bd9Sstevel@tonic-gate 	name[depth] = DI_ALL(pa)->root_path;
11007c478bd9Sstevel@tonic-gate 	len += strlen(name[depth]) + 1;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/*
11037c478bd9Sstevel@tonic-gate 	 * allocate buffer and assemble path
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	if ((buf = malloc(len)) == NULL) {
11067c478bd9Sstevel@tonic-gate 		return (NULL);
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, name[depth]);
11107c478bd9Sstevel@tonic-gate 	len = strlen(buf);
11117c478bd9Sstevel@tonic-gate 	if (buf[len - 1] == '/')
11127c478bd9Sstevel@tonic-gate 		len--;	/* delete trailing '/' */
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	while (depth) {
11157c478bd9Sstevel@tonic-gate 		depth--;
11167c478bd9Sstevel@tonic-gate 		buf[len] = '/';
11177c478bd9Sstevel@tonic-gate 		(void) strcpy(buf + len + 1, name[depth]);
11187c478bd9Sstevel@tonic-gate 		len += strlen(name[depth]) + 1;
11197c478bd9Sstevel@tonic-gate 		if (addr[depth] && addr[depth][0] != '\0') {
11207c478bd9Sstevel@tonic-gate 			buf[len] = '@';
11217c478bd9Sstevel@tonic-gate 			(void) strcpy(buf + len + 1, addr[depth]);
11227c478bd9Sstevel@tonic-gate 			len += strlen(addr[depth]) + 1;
11237c478bd9Sstevel@tonic-gate 		}
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	return (buf);
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate char *
11307c478bd9Sstevel@tonic-gate di_devfs_minor_path(di_minor_t minor)
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 	di_node_t	node;
1133*602ca9eaScth 	char		*full_path, *name, *devfspath;
11347c478bd9Sstevel@tonic-gate 	int		full_path_len;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (minor == DI_MINOR_NIL) {
11377c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11387c478bd9Sstevel@tonic-gate 		return (NULL);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	name = di_minor_name(minor);
11427c478bd9Sstevel@tonic-gate 	node = di_minor_devinfo(minor);
1143*602ca9eaScth 	devfspath = di_devfs_path(node);
1144*602ca9eaScth 	if (devfspath == NULL)
11457c478bd9Sstevel@tonic-gate 		return (NULL);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/* make the full path to the device minor node */
1148*602ca9eaScth 	full_path_len = strlen(devfspath) + strlen(name) + 2;
11497c478bd9Sstevel@tonic-gate 	full_path = (char *)calloc(1, full_path_len);
11507c478bd9Sstevel@tonic-gate 	if (full_path != NULL)
1151*602ca9eaScth 		(void) snprintf(full_path, full_path_len, "%s:%s",
1152*602ca9eaScth 		    devfspath, name);
1153*602ca9eaScth 
1154*602ca9eaScth 	di_devfs_path_free(devfspath);
1155*602ca9eaScth 	return (full_path);
1156*602ca9eaScth }
1157*602ca9eaScth 
1158*602ca9eaScth /*
1159*602ca9eaScth  * Produce a string representation of path to di_path_t (pathinfo node). This
1160*602ca9eaScth  * string is identical to di_devfs_path had the device been enumerated under
1161*602ca9eaScth  * the pHCI: it has a base path to pHCI, then uses node_name of client, and
1162*602ca9eaScth  * device unit-address of pathinfo node.
1163*602ca9eaScth  */
1164*602ca9eaScth char *
1165*602ca9eaScth di_path_devfs_path(di_path_t path)
1166*602ca9eaScth {
1167*602ca9eaScth 	di_node_t	phci_node;
1168*602ca9eaScth 	char		*phci_path, *path_name, *path_addr;
1169*602ca9eaScth 	char		*full_path;
1170*602ca9eaScth 	int		full_path_len;
1171*602ca9eaScth 
1172*602ca9eaScth 	if (path == DI_PATH_NIL) {
1173*602ca9eaScth 		errno = EINVAL;
1174*602ca9eaScth 		return (NULL);
1175*602ca9eaScth 	}
11767c478bd9Sstevel@tonic-gate 
1177*602ca9eaScth 	/* get name@addr for path */
1178*602ca9eaScth 	path_name = di_path_node_name(path);
1179*602ca9eaScth 	path_addr = di_path_bus_addr(path);
1180*602ca9eaScth 	if ((path_name == NULL) || (path_addr == NULL))
1181*602ca9eaScth 		return (NULL);
1182*602ca9eaScth 
1183*602ca9eaScth 	/* base path to pHCI devinfo node */
1184*602ca9eaScth 	phci_node = di_path_phci_node(path);
1185*602ca9eaScth 	if (phci_node == NULL)
1186*602ca9eaScth 		return (NULL);
1187*602ca9eaScth 	phci_path = di_devfs_path(phci_node);
1188*602ca9eaScth 	if (phci_path == NULL)
1189*602ca9eaScth 		return (NULL);
1190*602ca9eaScth 
1191*602ca9eaScth 	/* make the full string representation of path */
1192*602ca9eaScth 	full_path_len = strlen(phci_path) + 1 + strlen(path_name) +
1193*602ca9eaScth 	    1 + strlen(path_addr) + 1;
1194*602ca9eaScth 	full_path = (char *)calloc(1, full_path_len);
1195*602ca9eaScth 
1196*602ca9eaScth 	if (full_path != NULL)
1197*602ca9eaScth 		(void) snprintf(full_path, full_path_len, "%s/%s@%s",
1198*602ca9eaScth 		    phci_path, path_name, path_addr);
1199*602ca9eaScth 	di_devfs_path_free(phci_path);
12007c478bd9Sstevel@tonic-gate 	return (full_path);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
1203*602ca9eaScth char *
1204*602ca9eaScth di_path_client_devfs_path(di_path_t path)
1205*602ca9eaScth {
1206*602ca9eaScth 	return (di_devfs_path(di_path_client_node(path)));
1207*602ca9eaScth }
1208*602ca9eaScth 
12097c478bd9Sstevel@tonic-gate void
12107c478bd9Sstevel@tonic-gate di_devfs_path_free(char *buf)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
12137c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "di_devfs_path_free NULL arg!\n"));
12147c478bd9Sstevel@tonic-gate 		return;
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	free(buf);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
1220*602ca9eaScth /*
1221*602ca9eaScth  * Return 1 if name is a IEEE-1275 generic name. If new generic
1222*602ca9eaScth  * names are defined, they should be added to this table
1223*602ca9eaScth  */
1224*602ca9eaScth static int
1225*602ca9eaScth is_generic(const char *name, int len)
1226*602ca9eaScth {
1227*602ca9eaScth 	const char	**gp;
1228*602ca9eaScth 
1229*602ca9eaScth 	/* from IEEE-1275 recommended practices section 3 */
1230*602ca9eaScth 	static const char *generic_names[] = {
1231*602ca9eaScth 	    "atm",
1232*602ca9eaScth 	    "disk",
1233*602ca9eaScth 	    "display",
1234*602ca9eaScth 	    "dma-controller",
1235*602ca9eaScth 	    "ethernet",
1236*602ca9eaScth 	    "fcs",
1237*602ca9eaScth 	    "fdc",
1238*602ca9eaScth 	    "fddi",
1239*602ca9eaScth 	    "fibre-channel",
1240*602ca9eaScth 	    "ide",
1241*602ca9eaScth 	    "interrupt-controller",
1242*602ca9eaScth 	    "isa",
1243*602ca9eaScth 	    "keyboard",
1244*602ca9eaScth 	    "memory",
1245*602ca9eaScth 	    "mouse",
1246*602ca9eaScth 	    "nvram",
1247*602ca9eaScth 	    "pc-card",
1248*602ca9eaScth 	    "pci",
1249*602ca9eaScth 	    "printer",
1250*602ca9eaScth 	    "rtc",
1251*602ca9eaScth 	    "sbus",
1252*602ca9eaScth 	    "scanner",
1253*602ca9eaScth 	    "scsi",
1254*602ca9eaScth 	    "serial",
1255*602ca9eaScth 	    "sound",
1256*602ca9eaScth 	    "ssa",
1257*602ca9eaScth 	    "tape",
1258*602ca9eaScth 	    "timer",
1259*602ca9eaScth 	    "token-ring",
1260*602ca9eaScth 	    "vme",
1261*602ca9eaScth 	    0
1262*602ca9eaScth 	};
1263*602ca9eaScth 
1264*602ca9eaScth 	for (gp = generic_names; *gp; gp++) {
1265*602ca9eaScth 		if ((strncmp(*gp, name, len) == 0) &&
1266*602ca9eaScth 		    (strlen(*gp) == len))
1267*602ca9eaScth 			return (1);
1268*602ca9eaScth 	}
1269*602ca9eaScth 	return (0);
1270*602ca9eaScth }
1271*602ca9eaScth 
1272*602ca9eaScth /*
1273*602ca9eaScth  * Determine if two paths below /devices refer to the same device, ignoring
1274*602ca9eaScth  * any generic .vs. non-generic 'name' issues in "[[/]name[@addr[:minor]]]*".
1275*602ca9eaScth  * Return 1 if the paths match.
1276*602ca9eaScth  */
1277*602ca9eaScth int
1278*602ca9eaScth di_devfs_path_match(const char *dp1, const char *dp2)
1279*602ca9eaScth {
1280*602ca9eaScth 	const char	*p1, *p2;
1281*602ca9eaScth 	const char	*ec1, *ec2;
1282*602ca9eaScth 	const char	*at1, *at2;
1283*602ca9eaScth 	char		nc;
1284*602ca9eaScth 	int		g1, g2;
1285*602ca9eaScth 
1286*602ca9eaScth 	/* progress through both strings */
1287*602ca9eaScth 	for (p1 = dp1, p2 = dp2; (*p1 == *p2) && *p1; p1++, p2++) {
1288*602ca9eaScth 		/* require match until the start of a component */
1289*602ca9eaScth 		if (*p1 != '/')
1290*602ca9eaScth 			continue;
1291*602ca9eaScth 
1292*602ca9eaScth 		/* advance p1 and p2 to start of 'name' in component */
1293*602ca9eaScth 		nc = *(p1 + 1);
1294*602ca9eaScth 		if ((nc == '\0') || (nc == '/'))
1295*602ca9eaScth 			continue;		/* skip trash */
1296*602ca9eaScth 		p1++;
1297*602ca9eaScth 		p2++;
1298*602ca9eaScth 
1299*602ca9eaScth 		/*
1300*602ca9eaScth 		 * Both p1 and p2 point to beginning of 'name' in component.
1301*602ca9eaScth 		 * Determine where current component ends: next '/' or '\0'.
1302*602ca9eaScth 		 */
1303*602ca9eaScth 		ec1 = strchr(p1, '/');
1304*602ca9eaScth 		if (ec1 == NULL)
1305*602ca9eaScth 			ec1 = p1 + strlen(p1);
1306*602ca9eaScth 		ec2 = strchr(p2, '/');
1307*602ca9eaScth 		if (ec2 == NULL)
1308*602ca9eaScth 			ec2 = p2 + strlen(p2);
1309*602ca9eaScth 
1310*602ca9eaScth 		/* Determine where name ends based on whether '@' exists */
1311*602ca9eaScth 		at1 = strchr(p1, '@');
1312*602ca9eaScth 		at2 = strchr(p2, '@');
1313*602ca9eaScth 		if (at1 && (at1 < ec1))
1314*602ca9eaScth 			ec1 = at1;
1315*602ca9eaScth 		if (at2 && (at2 < ec2))
1316*602ca9eaScth 			ec2 = at2;
1317*602ca9eaScth 
1318*602ca9eaScth 		/*
1319*602ca9eaScth 		 * At this point p[12] point to beginning of name and
1320*602ca9eaScth 		 * ec[12] point to character past the end of name. Determine
1321*602ca9eaScth 		 * if the names are generic.
1322*602ca9eaScth 		 */
1323*602ca9eaScth 		g1 = is_generic(p1, ec1 - p1);
1324*602ca9eaScth 		g2 = is_generic(p2, ec2 - p2);
1325*602ca9eaScth 
1326*602ca9eaScth 		if (g1 != g2) {
1327*602ca9eaScth 			/*
1328*602ca9eaScth 			 * one generic and one non-generic
1329*602ca9eaScth 			 * skip past the names in the match.
1330*602ca9eaScth 			 */
1331*602ca9eaScth 			p1 = ec1;
1332*602ca9eaScth 			p2 = ec2;
1333*602ca9eaScth 		} else {
1334*602ca9eaScth 			if (*p1 != *p2)
1335*602ca9eaScth 				break;
1336*602ca9eaScth 		}
1337*602ca9eaScth 	}
1338*602ca9eaScth 
1339*602ca9eaScth 	return ((*p1 == *p2) ? 1 : 0);
1340*602ca9eaScth }
1341*602ca9eaScth 
13427c478bd9Sstevel@tonic-gate /* minor data access */
13437c478bd9Sstevel@tonic-gate di_minor_t
13447c478bd9Sstevel@tonic-gate di_minor_next(di_node_t node, di_minor_t minor)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	caddr_t pa;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/*
13497c478bd9Sstevel@tonic-gate 	 * paranoid error checking
13507c478bd9Sstevel@tonic-gate 	 */
13517c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
13527c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13537c478bd9Sstevel@tonic-gate 		return (DI_MINOR_NIL);
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	/*
13577c478bd9Sstevel@tonic-gate 	 * minor is not NIL
13587c478bd9Sstevel@tonic-gate 	 */
13597c478bd9Sstevel@tonic-gate 	if (minor != DI_MINOR_NIL) {
13607c478bd9Sstevel@tonic-gate 		if (DI_MINOR(minor)->next != 0)
13617c478bd9Sstevel@tonic-gate 			return ((di_minor_t)((void *)((caddr_t)minor -
13627c478bd9Sstevel@tonic-gate 			    DI_MINOR(minor)->self + DI_MINOR(minor)->next)));
13637c478bd9Sstevel@tonic-gate 		else {
13647c478bd9Sstevel@tonic-gate 			errno = ENXIO;
13657c478bd9Sstevel@tonic-gate 			return (DI_MINOR_NIL);
13667c478bd9Sstevel@tonic-gate 		}
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	/*
13707c478bd9Sstevel@tonic-gate 	 * minor is NIL-->caller asks for first minor node
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->minor_data != 0) {
13737c478bd9Sstevel@tonic-gate 		return (DI_MINOR((caddr_t)node - DI_NODE(node)->self +
13747c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->minor_data));
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/*
13787c478bd9Sstevel@tonic-gate 	 * no minor data-->check if snapshot includes minor data
13797c478bd9Sstevel@tonic-gate 	 *	in order to set the correct errno
13807c478bd9Sstevel@tonic-gate 	 */
13817c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
13827c478bd9Sstevel@tonic-gate 	if (DINFOMINOR & DI_ALL(pa)->command)
13837c478bd9Sstevel@tonic-gate 		errno = ENXIO;
13847c478bd9Sstevel@tonic-gate 	else
13857c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	return (DI_MINOR_NIL);
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate /* private interface for dealing with alias minor link generation */
13917c478bd9Sstevel@tonic-gate di_node_t
13927c478bd9Sstevel@tonic-gate di_minor_devinfo(di_minor_t minor)
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	if (minor == DI_MINOR_NIL) {
13957c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13967c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	return (DI_NODE((caddr_t)minor - DI_MINOR(minor)->self +
14007c478bd9Sstevel@tonic-gate 	    DI_MINOR(minor)->node));
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate ddi_minor_type
14047c478bd9Sstevel@tonic-gate di_minor_type(di_minor_t minor)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate 	return (DI_MINOR(minor)->type);
14077c478bd9Sstevel@tonic-gate }
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate char *
14107c478bd9Sstevel@tonic-gate di_minor_name(di_minor_t minor)
14117c478bd9Sstevel@tonic-gate {
14127c478bd9Sstevel@tonic-gate 	if (DI_MINOR(minor)->name == 0)
14137c478bd9Sstevel@tonic-gate 		return (NULL);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	return ((caddr_t)minor - DI_MINOR(minor)->self + DI_MINOR(minor)->name);
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate dev_t
14197c478bd9Sstevel@tonic-gate di_minor_devt(di_minor_t minor)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	return (makedev(DI_MINOR(minor)->dev_major,
1422*602ca9eaScth 	    DI_MINOR(minor)->dev_minor));
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate int
14267c478bd9Sstevel@tonic-gate di_minor_spectype(di_minor_t minor)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	return (DI_MINOR(minor)->spec_type);
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate char *
14327c478bd9Sstevel@tonic-gate di_minor_nodetype(di_minor_t minor)
14337c478bd9Sstevel@tonic-gate {
14347c478bd9Sstevel@tonic-gate 	if (DI_MINOR(minor)->node_type == 0)
14357c478bd9Sstevel@tonic-gate 		return (NULL);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	return ((caddr_t)minor -
1438*602ca9eaScth 	    DI_MINOR(minor)->self + DI_MINOR(minor)->node_type);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate /*
14427c478bd9Sstevel@tonic-gate  * Single public interface for accessing software properties
14437c478bd9Sstevel@tonic-gate  */
14447c478bd9Sstevel@tonic-gate di_prop_t
14457c478bd9Sstevel@tonic-gate di_prop_next(di_node_t node, di_prop_t prop)
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	int list = DI_PROP_DRV_LIST;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	/*
14507c478bd9Sstevel@tonic-gate 	 * paranoid check
14517c478bd9Sstevel@tonic-gate 	 */
14527c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
14537c478bd9Sstevel@tonic-gate 		errno = EINVAL;
14547c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
14557c478bd9Sstevel@tonic-gate 	}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	/*
14587c478bd9Sstevel@tonic-gate 	 * Find which prop list we are at
14597c478bd9Sstevel@tonic-gate 	 */
14607c478bd9Sstevel@tonic-gate 	if (prop != DI_PROP_NIL)
14617c478bd9Sstevel@tonic-gate 		list = DI_PROP(prop)->prop_list;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	do {
14647c478bd9Sstevel@tonic-gate 		switch (list++) {
14657c478bd9Sstevel@tonic-gate 		case DI_PROP_DRV_LIST:
14667c478bd9Sstevel@tonic-gate 			prop = di_prop_drv_next(node, prop);
14677c478bd9Sstevel@tonic-gate 			break;
14687c478bd9Sstevel@tonic-gate 		case DI_PROP_SYS_LIST:
14697c478bd9Sstevel@tonic-gate 			prop = di_prop_sys_next(node, prop);
14707c478bd9Sstevel@tonic-gate 			break;
14717c478bd9Sstevel@tonic-gate 		case DI_PROP_GLB_LIST:
14727c478bd9Sstevel@tonic-gate 			prop = di_prop_global_next(node, prop);
14737c478bd9Sstevel@tonic-gate 			break;
14747c478bd9Sstevel@tonic-gate 		case DI_PROP_HW_LIST:
14757c478bd9Sstevel@tonic-gate 			prop = di_prop_hw_next(node, prop);
14767c478bd9Sstevel@tonic-gate 			break;
14777c478bd9Sstevel@tonic-gate 		default:	/* shouldn't happen */
14787c478bd9Sstevel@tonic-gate 			errno = EFAULT;
14797c478bd9Sstevel@tonic-gate 			return (DI_PROP_NIL);
14807c478bd9Sstevel@tonic-gate 		}
14817c478bd9Sstevel@tonic-gate 	} while ((prop == DI_PROP_NIL) && (list <= DI_PROP_HW_LIST));
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	return (prop);
14847c478bd9Sstevel@tonic-gate }
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate dev_t
14877c478bd9Sstevel@tonic-gate di_prop_devt(di_prop_t prop)
14887c478bd9Sstevel@tonic-gate {
14897c478bd9Sstevel@tonic-gate 	return (makedev(DI_PROP(prop)->dev_major, DI_PROP(prop)->dev_minor));
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate char *
14937c478bd9Sstevel@tonic-gate di_prop_name(di_prop_t prop)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_name == 0)
14967c478bd9Sstevel@tonic-gate 		return (NULL);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	return ((caddr_t)prop - DI_PROP(prop)->self + DI_PROP(prop)->prop_name);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate int
15027c478bd9Sstevel@tonic-gate di_prop_type(di_prop_t prop)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate 	uint_t flags = DI_PROP(prop)->prop_flags;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_UNDEF_IT)
15077c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_UNDEF_IT);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
15107c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_BOOLEAN);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	if ((flags & DDI_PROP_TYPE_MASK) == DDI_PROP_TYPE_ANY)
15137c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_UNKNOWN);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_INT)
15167c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_INT);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_INT64)
15197c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_INT64);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_STRING)
15227c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_STRING);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_BYTE)
15257c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_BYTE);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	/*
15287c478bd9Sstevel@tonic-gate 	 * Shouldn't get here. In case we do, return unknown type.
15297c478bd9Sstevel@tonic-gate 	 *
15307c478bd9Sstevel@tonic-gate 	 * XXX--When DDI_PROP_TYPE_COMPOSITE is implemented, we need
15317c478bd9Sstevel@tonic-gate 	 *	to add DI_PROP_TYPE_COMPOSITE.
15327c478bd9Sstevel@tonic-gate 	 */
15337c478bd9Sstevel@tonic-gate 	DPRINTF((DI_ERR, "Unimplemented property type: 0x%x\n", flags));
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	return (DI_PROP_TYPE_UNKNOWN);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate /*
15397c478bd9Sstevel@tonic-gate  * Extract type-specific values of an property
15407c478bd9Sstevel@tonic-gate  */
15417c478bd9Sstevel@tonic-gate extern int di_prop_decode_common(void *prop_data, int len,
15427c478bd9Sstevel@tonic-gate 	int ddi_type, int prom);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate int
15457c478bd9Sstevel@tonic-gate di_prop_ints(di_prop_t prop, int **prop_data)
15467c478bd9Sstevel@tonic-gate {
15477c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
15487c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
15517c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
15527c478bd9Sstevel@tonic-gate 		errno = EFAULT;
15537c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
15547c478bd9Sstevel@tonic-gate 		return (-1);
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	*prop_data = (int *)((void *)((caddr_t)prop - DI_PROP(prop)->self
15587c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data));
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
15617c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_INT, 0));
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate int
15657c478bd9Sstevel@tonic-gate di_prop_int64(di_prop_t prop, int64_t **prop_data)
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
15687c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
15717c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
15727c478bd9Sstevel@tonic-gate 		errno = EFAULT;
15737c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
15747c478bd9Sstevel@tonic-gate 		return (-1);
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	*prop_data = (int64_t *)((void *)((caddr_t)prop - DI_PROP(prop)->self
15787c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data));
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
15817c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_INT64, 0));
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate int
15857c478bd9Sstevel@tonic-gate di_prop_strings(di_prop_t prop, char **prop_data)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
15887c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
15917c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
15927c478bd9Sstevel@tonic-gate 		errno = EFAULT;
15937c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
15947c478bd9Sstevel@tonic-gate 		return (-1);
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	*prop_data = (char *)((caddr_t)prop - DI_PROP(prop)->self
15987c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
16017c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_STRING, 0));
16027c478bd9Sstevel@tonic-gate }
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate int
16057c478bd9Sstevel@tonic-gate di_prop_bytes(di_prop_t prop, uchar_t **prop_data)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
16087c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
16117c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
16127c478bd9Sstevel@tonic-gate 		errno = EFAULT;
16137c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
16147c478bd9Sstevel@tonic-gate 		return (-1);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	*prop_data = (uchar_t *)((caddr_t)prop - DI_PROP(prop)->self
16187c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data);
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
16217c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_BYTE, 0));
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate /*
16257c478bd9Sstevel@tonic-gate  * returns 1 for match, 0 for no match
16267c478bd9Sstevel@tonic-gate  */
16277c478bd9Sstevel@tonic-gate static int
16287c478bd9Sstevel@tonic-gate match_prop(di_prop_t prop, dev_t match_dev, const char *name, int type)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	int prop_type;
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate #ifdef DEBUG
16337c478bd9Sstevel@tonic-gate 	if (di_prop_name(prop) == NULL) {
16347c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "libdevinfo: property has no name!\n"));
16357c478bd9Sstevel@tonic-gate 		return (0);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate #endif /* DEBUG */
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	if (strcmp(name, di_prop_name(prop)) != 0)
16407c478bd9Sstevel@tonic-gate 		return (0);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	if ((match_dev != DDI_DEV_T_ANY) && (di_prop_devt(prop) != match_dev))
16437c478bd9Sstevel@tonic-gate 		return (0);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	/*
16467c478bd9Sstevel@tonic-gate 	 * XXX prop_type is different from DDI_*. See PSARC 1997/127.
16477c478bd9Sstevel@tonic-gate 	 */
16487c478bd9Sstevel@tonic-gate 	prop_type = di_prop_type(prop);
16497c478bd9Sstevel@tonic-gate 	if ((prop_type != DI_PROP_TYPE_UNKNOWN) && (prop_type != type) &&
16507c478bd9Sstevel@tonic-gate 	    (prop_type != DI_PROP_TYPE_BOOLEAN))
16517c478bd9Sstevel@tonic-gate 		return (0);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	return (1);
16547c478bd9Sstevel@tonic-gate }
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate static di_prop_t
16577c478bd9Sstevel@tonic-gate di_prop_search(dev_t match_dev, di_node_t node, const char *name,
16587c478bd9Sstevel@tonic-gate     int type)
16597c478bd9Sstevel@tonic-gate {
16607c478bd9Sstevel@tonic-gate 	di_prop_t prop = DI_PROP_NIL;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * The check on match_dev follows ddi_prop_lookup_common().
16647c478bd9Sstevel@tonic-gate 	 * Other checks are libdevinfo specific implementation.
16657c478bd9Sstevel@tonic-gate 	 */
16667c478bd9Sstevel@tonic-gate 	if ((node == DI_NODE_NIL) || (name == NULL) || (strlen(name) == 0) ||
16677c478bd9Sstevel@tonic-gate 	    (match_dev == DDI_DEV_T_NONE) || !DI_PROP_TYPE_VALID(type)) {
16687c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16697c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
16737c478bd9Sstevel@tonic-gate 		DPRINTF((DI_TRACE1, "match prop name %s, devt 0x%lx, type %d\n",
16747c478bd9Sstevel@tonic-gate 		    di_prop_name(prop), di_prop_devt(prop),
16757c478bd9Sstevel@tonic-gate 		    di_prop_type(prop)));
16767c478bd9Sstevel@tonic-gate 		if (match_prop(prop, match_dev, name, type))
16777c478bd9Sstevel@tonic-gate 			return (prop);
16787c478bd9Sstevel@tonic-gate 	}
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16833ebafc43Sjveta di_prop_t
16843ebafc43Sjveta di_prop_find(dev_t match_dev, di_node_t node, const char *name)
16853ebafc43Sjveta {
16863ebafc43Sjveta 	di_prop_t prop = DI_PROP_NIL;
16873ebafc43Sjveta 
16883ebafc43Sjveta 	if ((node == DI_NODE_NIL) || (name == NULL) || (strlen(name) == 0) ||
16893ebafc43Sjveta 	    (match_dev == DDI_DEV_T_NONE)) {
16903ebafc43Sjveta 		errno = EINVAL;
16913ebafc43Sjveta 		return (DI_PROP_NIL);
16923ebafc43Sjveta 	}
16933ebafc43Sjveta 
16943ebafc43Sjveta 	while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
16953ebafc43Sjveta 		DPRINTF((DI_TRACE1, "found prop name %s, devt 0x%lx, type %d\n",
16963ebafc43Sjveta 		    di_prop_name(prop), di_prop_devt(prop),
16973ebafc43Sjveta 		    di_prop_type(prop)));
16983ebafc43Sjveta 
16993ebafc43Sjveta 		if (strcmp(name, di_prop_name(prop)) == 0 &&
17003ebafc43Sjveta 		    (match_dev == DDI_DEV_T_ANY ||
17013ebafc43Sjveta 		    di_prop_devt(prop) == match_dev))
17023ebafc43Sjveta 			return (prop);
17033ebafc43Sjveta 	}
17043ebafc43Sjveta 
17053ebafc43Sjveta 	return (DI_PROP_NIL);
17063ebafc43Sjveta }
17073ebafc43Sjveta 
17087c478bd9Sstevel@tonic-gate int
17097c478bd9Sstevel@tonic-gate di_prop_lookup_ints(dev_t dev, di_node_t node, const char *prop_name,
17107c478bd9Sstevel@tonic-gate 	int **prop_data)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	di_prop_t prop;
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
17157c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT)) == DI_PROP_NIL)
17167c478bd9Sstevel@tonic-gate 		return (-1);
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	return (di_prop_ints(prop, (void *)prop_data));
17197c478bd9Sstevel@tonic-gate }
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate int
17227c478bd9Sstevel@tonic-gate di_prop_lookup_int64(dev_t dev, di_node_t node, const char *prop_name,
17237c478bd9Sstevel@tonic-gate 	int64_t **prop_data)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	di_prop_t prop;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
17287c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT64)) == DI_PROP_NIL)
17297c478bd9Sstevel@tonic-gate 		return (-1);
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	return (di_prop_int64(prop, (void *)prop_data));
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate int
17357c478bd9Sstevel@tonic-gate di_prop_lookup_strings(dev_t dev, di_node_t node, const char *prop_name,
17367c478bd9Sstevel@tonic-gate     char **prop_data)
17377c478bd9Sstevel@tonic-gate {
17387c478bd9Sstevel@tonic-gate 	di_prop_t prop;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
17417c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_STRING)) == DI_PROP_NIL)
17427c478bd9Sstevel@tonic-gate 		return (-1);
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 	return (di_prop_strings(prop, (void *)prop_data));
17457c478bd9Sstevel@tonic-gate }
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate int
17487c478bd9Sstevel@tonic-gate di_prop_lookup_bytes(dev_t dev, di_node_t node, const char *prop_name,
17497c478bd9Sstevel@tonic-gate 	uchar_t **prop_data)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	di_prop_t prop;
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
17547c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_BYTE)) == DI_PROP_NIL)
17557c478bd9Sstevel@tonic-gate 		return (-1);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	return (di_prop_bytes(prop, (void *)prop_data));
17587c478bd9Sstevel@tonic-gate }
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate /*
17617c478bd9Sstevel@tonic-gate  * Consolidation private property access functions
17627c478bd9Sstevel@tonic-gate  */
17637c478bd9Sstevel@tonic-gate enum prop_type {
17647c478bd9Sstevel@tonic-gate 	PROP_TYPE_DRV,
17657c478bd9Sstevel@tonic-gate 	PROP_TYPE_SYS,
17667c478bd9Sstevel@tonic-gate 	PROP_TYPE_GLOB,
17677c478bd9Sstevel@tonic-gate 	PROP_TYPE_HW
17687c478bd9Sstevel@tonic-gate };
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate static di_prop_t
17717c478bd9Sstevel@tonic-gate di_prop_next_common(di_node_t node, di_prop_t prop, int prop_type)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	caddr_t pa;
17747c478bd9Sstevel@tonic-gate 	di_off_t prop_off = 0;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	if (prop != DI_PROP_NIL) {
17777c478bd9Sstevel@tonic-gate 		if (DI_PROP(prop)->next) {
17787c478bd9Sstevel@tonic-gate 			return (DI_PROP((caddr_t)prop -
17797c478bd9Sstevel@tonic-gate 			    DI_PROP(prop)->self + DI_PROP(prop)->next));
17807c478bd9Sstevel@tonic-gate 		} else {
17817c478bd9Sstevel@tonic-gate 			return (DI_PROP_NIL);
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * prop is NIL, caller asks for first property
17887c478bd9Sstevel@tonic-gate 	 */
17897c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
17907c478bd9Sstevel@tonic-gate 	switch (prop_type) {
17917c478bd9Sstevel@tonic-gate 	case PROP_TYPE_DRV:
17927c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->drv_prop;
17937c478bd9Sstevel@tonic-gate 		break;
17947c478bd9Sstevel@tonic-gate 	case PROP_TYPE_SYS:
17957c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->sys_prop;
17967c478bd9Sstevel@tonic-gate 		break;
17977c478bd9Sstevel@tonic-gate 	case PROP_TYPE_HW:
17987c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->hw_prop;
17997c478bd9Sstevel@tonic-gate 		break;
18007c478bd9Sstevel@tonic-gate 	case PROP_TYPE_GLOB:
18017c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->glob_prop;
18027c478bd9Sstevel@tonic-gate 		if (prop_off == -1) {
18037c478bd9Sstevel@tonic-gate 			/* no global property */
18047c478bd9Sstevel@tonic-gate 			prop_off = 0;
18057c478bd9Sstevel@tonic-gate 		} else if ((prop_off == 0) && (DI_NODE(node)->drv_major >= 0)) {
18067c478bd9Sstevel@tonic-gate 			/* refer to devnames array */
18077c478bd9Sstevel@tonic-gate 			struct di_devnm *devnm = DI_DEVNM(pa +
18087c478bd9Sstevel@tonic-gate 			    DI_ALL(pa)->devnames + (DI_NODE(node)->drv_major *
18097c478bd9Sstevel@tonic-gate 			    sizeof (struct di_devnm)));
18107c478bd9Sstevel@tonic-gate 			prop_off = devnm->global_prop;
18117c478bd9Sstevel@tonic-gate 		}
18127c478bd9Sstevel@tonic-gate 		break;
18137c478bd9Sstevel@tonic-gate 	}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if (prop_off) {
18167c478bd9Sstevel@tonic-gate 		return (DI_PROP(pa + prop_off));
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	/*
18207c478bd9Sstevel@tonic-gate 	 * no prop found. Check the reason for not found
18217c478bd9Sstevel@tonic-gate 	 */
18227c478bd9Sstevel@tonic-gate 	if (DINFOPROP & DI_ALL(pa)->command)
18237c478bd9Sstevel@tonic-gate 		errno = ENXIO;
18247c478bd9Sstevel@tonic-gate 	else
18257c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate di_prop_t
18317c478bd9Sstevel@tonic-gate di_prop_drv_next(di_node_t node, di_prop_t prop)
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_DRV));
18347c478bd9Sstevel@tonic-gate }
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate di_prop_t
18377c478bd9Sstevel@tonic-gate di_prop_sys_next(di_node_t node, di_prop_t prop)
18387c478bd9Sstevel@tonic-gate {
18397c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_SYS));
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate di_prop_t
18437c478bd9Sstevel@tonic-gate di_prop_global_next(di_node_t node, di_prop_t prop)
18447c478bd9Sstevel@tonic-gate {
18457c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_GLOB));
18467c478bd9Sstevel@tonic-gate }
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate di_prop_t
18497c478bd9Sstevel@tonic-gate di_prop_hw_next(di_node_t node, di_prop_t prop)
18507c478bd9Sstevel@tonic-gate {
18517c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_HW));
18527c478bd9Sstevel@tonic-gate }
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate int
18557c478bd9Sstevel@tonic-gate di_prop_rawdata(di_prop_t prop, uchar_t **prop_data)
18567c478bd9Sstevel@tonic-gate {
18577c478bd9Sstevel@tonic-gate #ifdef DEBUG
18587c478bd9Sstevel@tonic-gate 	if (prop == DI_PROP_NIL) {
18597c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18607c478bd9Sstevel@tonic-gate 		return (-1);
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate #endif /* DEBUG */
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0) {
18657c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
18667c478bd9Sstevel@tonic-gate 		return (0);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
18707c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
18717c478bd9Sstevel@tonic-gate 		errno = EFAULT;
18727c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
18737c478bd9Sstevel@tonic-gate 		return (-1);
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	/*
18777c478bd9Sstevel@tonic-gate 	 * No memory allocation.
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	*prop_data = (uchar_t *)((caddr_t)prop - DI_PROP(prop)->self +
18807c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_data);
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	return (DI_PROP(prop)->prop_len);
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate /*
18867c478bd9Sstevel@tonic-gate  * Consolidation private interfaces for accessing I/O multipathing data
18877c478bd9Sstevel@tonic-gate  */
18887c478bd9Sstevel@tonic-gate di_path_t
1889*602ca9eaScth di_path_phci_next_path(di_node_t node, di_path_t path)
18907c478bd9Sstevel@tonic-gate {
18917c478bd9Sstevel@tonic-gate 	caddr_t pa;
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	/*
18947c478bd9Sstevel@tonic-gate 	 * path is not NIL
18957c478bd9Sstevel@tonic-gate 	 */
18967c478bd9Sstevel@tonic-gate 	if (path != DI_PATH_NIL) {
18977c478bd9Sstevel@tonic-gate 		if (DI_PATH(path)->path_p_link != 0)
18987c478bd9Sstevel@tonic-gate 			return (DI_PATH((void *)((caddr_t)path -
18997c478bd9Sstevel@tonic-gate 			    DI_PATH(path)->self + DI_PATH(path)->path_p_link)));
19007c478bd9Sstevel@tonic-gate 		else {
19017c478bd9Sstevel@tonic-gate 			errno = ENXIO;
19027c478bd9Sstevel@tonic-gate 			return (DI_PATH_NIL);
19037c478bd9Sstevel@tonic-gate 		}
19047c478bd9Sstevel@tonic-gate 	}
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	/*
19077c478bd9Sstevel@tonic-gate 	 * Path is NIL; the caller is asking for the first path info node
19087c478bd9Sstevel@tonic-gate 	 */
19097c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->multipath_phci != 0) {
1910*602ca9eaScth 		DPRINTF((DI_INFO, "phci_next_path: returning %p\n",
1911*602ca9eaScth 		    ((caddr_t)node -
19127c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->self + DI_NODE(node)->multipath_phci)));
19137c478bd9Sstevel@tonic-gate 		return (DI_PATH((caddr_t)node - DI_NODE(node)->self +
19147c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->multipath_phci));
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	/*
19187c478bd9Sstevel@tonic-gate 	 * No pathing data; check if the snapshot includes path data in order
19197c478bd9Sstevel@tonic-gate 	 * to set errno properly.
19207c478bd9Sstevel@tonic-gate 	 */
19217c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
19227c478bd9Sstevel@tonic-gate 	if (DINFOPATH & (DI_ALL(pa)->command))
19237c478bd9Sstevel@tonic-gate 		errno = ENXIO;
19247c478bd9Sstevel@tonic-gate 	else
19257c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	return (DI_PATH_NIL);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate di_path_t
1931*602ca9eaScth di_path_client_next_path(di_node_t node, di_path_t path)
19327c478bd9Sstevel@tonic-gate {
19337c478bd9Sstevel@tonic-gate 	caddr_t pa;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	/*
19367c478bd9Sstevel@tonic-gate 	 * path is not NIL
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	if (path != DI_PATH_NIL) {
19397c478bd9Sstevel@tonic-gate 		if (DI_PATH(path)->path_c_link != 0)
19407c478bd9Sstevel@tonic-gate 			return (DI_PATH((caddr_t)path - DI_PATH(path)->self
19417c478bd9Sstevel@tonic-gate 			    + DI_PATH(path)->path_c_link));
19427c478bd9Sstevel@tonic-gate 		else {
19437c478bd9Sstevel@tonic-gate 			errno = ENXIO;
19447c478bd9Sstevel@tonic-gate 			return (DI_PATH_NIL);
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	/*
19497c478bd9Sstevel@tonic-gate 	 * Path is NIL; the caller is asking for the first path info node
19507c478bd9Sstevel@tonic-gate 	 */
19517c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->multipath_client != 0) {
1952*602ca9eaScth 		DPRINTF((DI_INFO, "client_next_path: returning %p\n",
1953*602ca9eaScth 		    ((caddr_t)node -
19547c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->self + DI_NODE(node)->multipath_client)));
19557c478bd9Sstevel@tonic-gate 		return (DI_PATH((caddr_t)node - DI_NODE(node)->self +
19567c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->multipath_client));
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	/*
19607c478bd9Sstevel@tonic-gate 	 * No pathing data; check if the snapshot includes path data in order
19617c478bd9Sstevel@tonic-gate 	 * to set errno properly.
19627c478bd9Sstevel@tonic-gate 	 */
19637c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
19647c478bd9Sstevel@tonic-gate 	if (DINFOPATH & (DI_ALL(pa)->command))
19657c478bd9Sstevel@tonic-gate 		errno = ENXIO;
19667c478bd9Sstevel@tonic-gate 	else
19677c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	return (DI_PATH_NIL);
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate /*
1973*602ca9eaScth  * XXX Remove the private di_path_(addr,next,next_phci,next_client) interfaces
1974*602ca9eaScth  * below after NWS consolidation switches to using di_path_bus_addr,
1975*602ca9eaScth  * di_path_phci_next_path, and di_path_client_next_path per CR6638521.
19767c478bd9Sstevel@tonic-gate  */
1977*602ca9eaScth char *
1978*602ca9eaScth di_path_addr(di_path_t path, char *buf)
1979*602ca9eaScth {
1980*602ca9eaScth 	caddr_t pa;		/* starting address of map */
1981*602ca9eaScth 
1982*602ca9eaScth 	pa = (caddr_t)path - DI_PATH(path)->self;
1983*602ca9eaScth 
1984*602ca9eaScth 	(void) strncpy(buf, (char *)(pa + DI_PATH(path)->path_addr),
1985*602ca9eaScth 	    MAXPATHLEN);
1986*602ca9eaScth 	return (buf);
1987*602ca9eaScth }
19887c478bd9Sstevel@tonic-gate di_path_t
19897c478bd9Sstevel@tonic-gate di_path_next(di_node_t node, di_path_t path)
19907c478bd9Sstevel@tonic-gate {
19917c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
19927c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19937c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->multipath_client) {
1997*602ca9eaScth 		return (di_path_client_next_path(node, path));
19987c478bd9Sstevel@tonic-gate 	} else if (DI_NODE(node)->multipath_phci) {
1999*602ca9eaScth 		return (di_path_phci_next_path(node, path));
20007c478bd9Sstevel@tonic-gate 	} else {
20017c478bd9Sstevel@tonic-gate 		/*
20027c478bd9Sstevel@tonic-gate 		 * The node had multipathing data but didn't appear to be a
20037c478bd9Sstevel@tonic-gate 		 * phci *or* a client; probably a programmer error.
20047c478bd9Sstevel@tonic-gate 		 */
20057c478bd9Sstevel@tonic-gate 		errno = EINVAL;
20067c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate }
2009*602ca9eaScth di_path_t
2010*602ca9eaScth di_path_next_phci(di_node_t node, di_path_t path)
2011*602ca9eaScth {
2012*602ca9eaScth 	return (di_path_client_next_path(node, path));
2013*602ca9eaScth }
2014*602ca9eaScth di_path_t
2015*602ca9eaScth di_path_next_client(di_node_t node, di_path_t path)
2016*602ca9eaScth {
2017*602ca9eaScth 	return (di_path_phci_next_path(node, path));
2018*602ca9eaScth }
2019*602ca9eaScth 
2020*602ca9eaScth 
2021*602ca9eaScth 
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate di_path_state_t
20247c478bd9Sstevel@tonic-gate di_path_state(di_path_t path)
20257c478bd9Sstevel@tonic-gate {
20267c478bd9Sstevel@tonic-gate 	return ((di_path_state_t)DI_PATH(path)->path_state);
20277c478bd9Sstevel@tonic-gate }
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate char *
2030*602ca9eaScth di_path_node_name(di_path_t path)
20317c478bd9Sstevel@tonic-gate {
2032*602ca9eaScth 	di_node_t	client_node;
20337c478bd9Sstevel@tonic-gate 
2034*602ca9eaScth 	/* pathinfo gets node_name from client */
2035*602ca9eaScth 	if ((client_node = di_path_client_node(path)) == NULL)
2036*602ca9eaScth 		return (NULL);
2037*602ca9eaScth 	return (di_node_name(client_node));
2038*602ca9eaScth }
20397c478bd9Sstevel@tonic-gate 
2040*602ca9eaScth char *
2041*602ca9eaScth di_path_bus_addr(di_path_t path)
2042*602ca9eaScth {
2043*602ca9eaScth 	caddr_t pa = (caddr_t)path - DI_PATH(path)->self;
2044*602ca9eaScth 
2045*602ca9eaScth 	if (DI_PATH(path)->path_addr == 0)
2046*602ca9eaScth 		return (NULL);
2047*602ca9eaScth 
2048*602ca9eaScth 	return ((char *)(pa + DI_PATH(path)->path_addr));
2049*602ca9eaScth }
2050*602ca9eaScth 
2051*602ca9eaScth int
2052*602ca9eaScth di_path_instance(di_path_t path)
2053*602ca9eaScth {
2054*602ca9eaScth 	return (DI_PATH(path)->path_instance);
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate di_node_t
20587c478bd9Sstevel@tonic-gate di_path_client_node(di_path_t path)
20597c478bd9Sstevel@tonic-gate {
20607c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if (path == DI_PATH_NIL) {
20637c478bd9Sstevel@tonic-gate 		errno = EINVAL;
20647c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get client node for path %p\n", path));
20687c478bd9Sstevel@tonic-gate 
20697c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	if (DI_PATH(path)->path_client) {
20727c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_PATH(path)->path_client));
20737c478bd9Sstevel@tonic-gate 	}
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	/*
20767c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
20777c478bd9Sstevel@tonic-gate 	 *   If parent doesn't exist and node is not the root,
20787c478bd9Sstevel@tonic-gate 	 *   set errno to ENOTSUP. Otherwise, set errno to ENXIO.
20797c478bd9Sstevel@tonic-gate 	 */
20807c478bd9Sstevel@tonic-gate 	if ((DI_PATH(path)->path_snap_state & DI_PATH_SNAP_NOCLIENT) == 0)
20817c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
20827c478bd9Sstevel@tonic-gate 	else
20837c478bd9Sstevel@tonic-gate 		errno = ENXIO;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
20867c478bd9Sstevel@tonic-gate }
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate di_node_t
20897c478bd9Sstevel@tonic-gate di_path_phci_node(di_path_t path)
20907c478bd9Sstevel@tonic-gate {
20917c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 	if (path == DI_PATH_NIL) {
20947c478bd9Sstevel@tonic-gate 		errno = EINVAL;
20957c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get phci node for path %p\n", path));
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	if (DI_PATH(path)->path_phci) {
21037c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_PATH(path)->path_phci));
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	/*
21077c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
21087c478bd9Sstevel@tonic-gate 	 *   If parent doesn't exist and node is not the root,
21097c478bd9Sstevel@tonic-gate 	 *   set errno to ENOTSUP. Otherwise, set errno to ENXIO.
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	if ((DI_PATH(path)->path_snap_state & DI_PATH_SNAP_NOPHCI) == 0)
21127c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
21137c478bd9Sstevel@tonic-gate 	else
21147c478bd9Sstevel@tonic-gate 		errno = ENXIO;
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
21177c478bd9Sstevel@tonic-gate }
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate di_path_prop_t
21207c478bd9Sstevel@tonic-gate di_path_prop_next(di_path_t path, di_path_prop_t prop)
21217c478bd9Sstevel@tonic-gate {
21227c478bd9Sstevel@tonic-gate 	caddr_t pa;
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	if (path == DI_PATH_NIL) {
21257c478bd9Sstevel@tonic-gate 		errno = EINVAL;
21267c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
21277c478bd9Sstevel@tonic-gate 	}
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	/*
21307c478bd9Sstevel@tonic-gate 	 * prop is not NIL
21317c478bd9Sstevel@tonic-gate 	 */
21327c478bd9Sstevel@tonic-gate 	if (prop != DI_PROP_NIL) {
21337c478bd9Sstevel@tonic-gate 		if (DI_PROP(prop)->next != 0)
21347c478bd9Sstevel@tonic-gate 			return (DI_PATHPROP((caddr_t)prop -
21357c478bd9Sstevel@tonic-gate 			    DI_PROP(prop)->self + DI_PROP(prop)->next));
21367c478bd9Sstevel@tonic-gate 		else {
21377c478bd9Sstevel@tonic-gate 			errno = ENXIO;
21387c478bd9Sstevel@tonic-gate 			return (DI_PROP_NIL);
21397c478bd9Sstevel@tonic-gate 		}
21407c478bd9Sstevel@tonic-gate 	}
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	/*
21437c478bd9Sstevel@tonic-gate 	 * prop is NIL-->caller asks for first property
21447c478bd9Sstevel@tonic-gate 	 */
21457c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
21467c478bd9Sstevel@tonic-gate 	if (DI_PATH(path)->path_prop != 0) {
21477c478bd9Sstevel@tonic-gate 		return (DI_PATHPROP(pa + DI_PATH(path)->path_prop));
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	/*
21517c478bd9Sstevel@tonic-gate 	 * no property data-->check if snapshot includes props
21527c478bd9Sstevel@tonic-gate 	 *	in order to set the correct errno
21537c478bd9Sstevel@tonic-gate 	 */
21547c478bd9Sstevel@tonic-gate 	if (DINFOPROP & (DI_ALL(pa)->command))
21557c478bd9Sstevel@tonic-gate 		errno = ENXIO;
21567c478bd9Sstevel@tonic-gate 	else
21577c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate char *
21637c478bd9Sstevel@tonic-gate di_path_prop_name(di_path_prop_t prop)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
21667c478bd9Sstevel@tonic-gate 	pa = (caddr_t)prop - DI_PATHPROP(prop)->self;
21677c478bd9Sstevel@tonic-gate 	return ((char *)(pa + DI_PATHPROP(prop)->prop_name));
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate int
21717c478bd9Sstevel@tonic-gate di_path_prop_len(di_path_prop_t prop)
21727c478bd9Sstevel@tonic-gate {
21737c478bd9Sstevel@tonic-gate 	return (DI_PATHPROP(prop)->prop_len);
21747c478bd9Sstevel@tonic-gate }
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate int
21777c478bd9Sstevel@tonic-gate di_path_prop_type(di_path_prop_t prop)
21787c478bd9Sstevel@tonic-gate {
21797c478bd9Sstevel@tonic-gate 	switch (DI_PATHPROP(prop)->prop_type) {
21807c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_INT:
21817c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_INT);
21827c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_INT64:
21837c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_INT64);
21847c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_BYTE:
21857c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_BYTE);
21867c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_STRING:
21877c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_STRING);
21887c478bd9Sstevel@tonic-gate 	}
21897c478bd9Sstevel@tonic-gate 	return (DI_PROP_TYPE_UNKNOWN);
21907c478bd9Sstevel@tonic-gate }
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate int
21937c478bd9Sstevel@tonic-gate di_path_prop_bytes(di_path_prop_t prop, uchar_t **prop_data)
21947c478bd9Sstevel@tonic-gate {
21957c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
21967c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
21977c478bd9Sstevel@tonic-gate 		errno = EFAULT;
21987c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
21997c478bd9Sstevel@tonic-gate 		return (-1);
22007c478bd9Sstevel@tonic-gate 	}
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	*prop_data = (uchar_t *)((caddr_t)prop - DI_PATHPROP(prop)->self
22037c478bd9Sstevel@tonic-gate 	    + DI_PATHPROP(prop)->prop_data);
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
22067c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_BYTE, 0));
22077c478bd9Sstevel@tonic-gate }
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate int
22107c478bd9Sstevel@tonic-gate di_path_prop_ints(di_path_prop_t prop, int **prop_data)
22117c478bd9Sstevel@tonic-gate {
22127c478bd9Sstevel@tonic-gate 	if (DI_PATHPROP(prop)->prop_len == 0)
22137c478bd9Sstevel@tonic-gate 		return (0);
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
22167c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
22177c478bd9Sstevel@tonic-gate 		errno = EFAULT;
22187c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
22197c478bd9Sstevel@tonic-gate 		return (-1);
22207c478bd9Sstevel@tonic-gate 	}
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	*prop_data = (int *)((void *)((caddr_t)prop - DI_PATHPROP(prop)->self
22237c478bd9Sstevel@tonic-gate 	    + DI_PATHPROP(prop)->prop_data));
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
22267c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_INT, 0));
22277c478bd9Sstevel@tonic-gate }
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate int
22307c478bd9Sstevel@tonic-gate di_path_prop_int64s(di_path_prop_t prop, int64_t **prop_data)
22317c478bd9Sstevel@tonic-gate {
22327c478bd9Sstevel@tonic-gate 	if (DI_PATHPROP(prop)->prop_len == 0)
22337c478bd9Sstevel@tonic-gate 		return (0);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
22367c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
22377c478bd9Sstevel@tonic-gate 		errno = EFAULT;
22387c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
22397c478bd9Sstevel@tonic-gate 		return (-1);
22407c478bd9Sstevel@tonic-gate 	}
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	*prop_data = (int64_t *)((void *)((caddr_t)prop -
22437c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->self + DI_PATHPROP(prop)->prop_data));
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
22467c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_INT64, 0));
22477c478bd9Sstevel@tonic-gate }
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate int
22507c478bd9Sstevel@tonic-gate di_path_prop_strings(di_path_prop_t prop, char **prop_data)
22517c478bd9Sstevel@tonic-gate {
22527c478bd9Sstevel@tonic-gate 	if (DI_PATHPROP(prop)->prop_len == 0)
22537c478bd9Sstevel@tonic-gate 		return (0);
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
22567c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
22577c478bd9Sstevel@tonic-gate 		errno = EFAULT;
22587c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
22597c478bd9Sstevel@tonic-gate 		return (-1);
22607c478bd9Sstevel@tonic-gate 	}
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	*prop_data = (char *)((caddr_t)prop - DI_PATHPROP(prop)->self
22637c478bd9Sstevel@tonic-gate 	    + DI_PATHPROP(prop)->prop_data);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
22667c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_STRING, 0));
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate static di_path_prop_t
22707c478bd9Sstevel@tonic-gate di_path_prop_search(di_path_t path, const char *name, int type)
22717c478bd9Sstevel@tonic-gate {
22727c478bd9Sstevel@tonic-gate 	di_path_prop_t prop = DI_PROP_NIL;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	/*
22757c478bd9Sstevel@tonic-gate 	 * Sanity check arguments
22767c478bd9Sstevel@tonic-gate 	 */
22777c478bd9Sstevel@tonic-gate 	if ((path == DI_PATH_NIL) || (name == NULL) || (strlen(name) == 0) ||
22787c478bd9Sstevel@tonic-gate 	    !DI_PROP_TYPE_VALID(type)) {
22797c478bd9Sstevel@tonic-gate 		errno = EINVAL;
22807c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
22817c478bd9Sstevel@tonic-gate 	}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	while ((prop = di_path_prop_next(path, prop)) != DI_PROP_NIL) {
22847c478bd9Sstevel@tonic-gate 		int prop_type = di_path_prop_type(prop);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 		DPRINTF((DI_TRACE1, "match path prop name %s, type %d\n",
22877c478bd9Sstevel@tonic-gate 		    di_path_prop_name(prop), prop_type));
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		if (strcmp(name, di_path_prop_name(prop)) != 0)
22907c478bd9Sstevel@tonic-gate 			continue;
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 		if ((prop_type != DI_PROP_TYPE_UNKNOWN) && (prop_type != type))
22937c478bd9Sstevel@tonic-gate 			continue;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 		return (prop);
22967c478bd9Sstevel@tonic-gate 	}
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
22997c478bd9Sstevel@tonic-gate }
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate int
23027c478bd9Sstevel@tonic-gate di_path_prop_lookup_bytes(di_path_t path, const char *prop_name,
23037c478bd9Sstevel@tonic-gate     uchar_t **prop_data)
23047c478bd9Sstevel@tonic-gate {
23057c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
23087c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_BYTE)) == DI_PROP_NIL)
23097c478bd9Sstevel@tonic-gate 		return (-1);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	return (di_path_prop_bytes(prop, prop_data));
23127c478bd9Sstevel@tonic-gate }
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate int
23157c478bd9Sstevel@tonic-gate di_path_prop_lookup_ints(di_path_t path, const char *prop_name,
23167c478bd9Sstevel@tonic-gate     int **prop_data)
23177c478bd9Sstevel@tonic-gate {
23187c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
23217c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT)) == DI_PROP_NIL)
23227c478bd9Sstevel@tonic-gate 		return (-1);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	return (di_path_prop_ints(prop, prop_data));
23257c478bd9Sstevel@tonic-gate }
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate int
23287c478bd9Sstevel@tonic-gate di_path_prop_lookup_int64s(di_path_t path, const char *prop_name,
23297c478bd9Sstevel@tonic-gate     int64_t **prop_data)
23307c478bd9Sstevel@tonic-gate {
23317c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
23347c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT64)) == DI_PROP_NIL)
23357c478bd9Sstevel@tonic-gate 		return (-1);
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	return (di_path_prop_int64s(prop, prop_data));
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate int di_path_prop_lookup_strings(di_path_t path, const char *prop_name,
23417c478bd9Sstevel@tonic-gate     char **prop_data)
23427c478bd9Sstevel@tonic-gate {
23437c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
23467c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_STRING)) == DI_PROP_NIL)
23477c478bd9Sstevel@tonic-gate 		return (-1);
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	return (di_path_prop_strings(prop, prop_data));
23507c478bd9Sstevel@tonic-gate }
23517c478bd9Sstevel@tonic-gate 
23528c4f8890Srs /*
23538c4f8890Srs  * Consolidation private interfaces for traversing vhci nodes.
23548c4f8890Srs  */
23558c4f8890Srs di_node_t
23568c4f8890Srs di_vhci_first_node(di_node_t root)
23578c4f8890Srs {
23588c4f8890Srs 	struct di_all *dap;
23598c4f8890Srs 	caddr_t		pa;		/* starting address of map */
23608c4f8890Srs 
23618c4f8890Srs 	DPRINTF((DI_INFO, "Get first vhci node\n"));
23628c4f8890Srs 
23638c4f8890Srs 	if (root == DI_NODE_NIL) {
23648c4f8890Srs 		errno = EINVAL;
23658c4f8890Srs 		return (DI_NODE_NIL);
23668c4f8890Srs 	}
23678c4f8890Srs 
23688c4f8890Srs 	pa = (caddr_t)root - DI_NODE(root)->self;
23698c4f8890Srs 	dap = DI_ALL(pa);
23708c4f8890Srs 
23718c4f8890Srs 	if (dap->top_vhci_devinfo == NULL) {
23728c4f8890Srs 		errno = ENXIO;
23738c4f8890Srs 		return (DI_NODE_NIL);
23748c4f8890Srs 	}
23758c4f8890Srs 
23768c4f8890Srs 	return (DI_NODE(pa + dap->top_vhci_devinfo));
23778c4f8890Srs }
23788c4f8890Srs 
23798c4f8890Srs di_node_t
23808c4f8890Srs di_vhci_next_node(di_node_t node)
23818c4f8890Srs {
23828c4f8890Srs 	caddr_t		pa;		/* starting address of map */
23838c4f8890Srs 
23848c4f8890Srs 	if (node == DI_NODE_NIL) {
23858c4f8890Srs 		errno = EINVAL;
23868c4f8890Srs 		return (DI_NODE_NIL);
23878c4f8890Srs 	}
23888c4f8890Srs 
23898c4f8890Srs 	DPRINTF((DI_TRACE, "next vhci node on the snap shot:"
23908c4f8890Srs 	    " current=%s\n", di_node_name(node)));
23918c4f8890Srs 
23928c4f8890Srs 	if (DI_NODE(node)->next_vhci == NULL) {
23938c4f8890Srs 		errno = ENXIO;
23948c4f8890Srs 		return (DI_NODE_NIL);
23958c4f8890Srs 	}
23968c4f8890Srs 
23978c4f8890Srs 	pa = (caddr_t)node - DI_NODE(node)->self;
23988c4f8890Srs 
23998c4f8890Srs 	return (DI_NODE(pa + DI_NODE(node)->next_vhci));
24008c4f8890Srs }
24018c4f8890Srs 
24028c4f8890Srs /*
24038c4f8890Srs  * Consolidation private interfaces for traversing phci nodes.
24048c4f8890Srs  */
24058c4f8890Srs di_node_t
24068c4f8890Srs di_phci_first_node(di_node_t vhci_node)
24078c4f8890Srs {
24088c4f8890Srs 	caddr_t		pa;		/* starting address of map */
24098c4f8890Srs 
24108c4f8890Srs 	DPRINTF((DI_INFO, "Get first phci node:\n"
24118c4f8890Srs 	    " current=%s", di_node_name(vhci_node)));
24128c4f8890Srs 
24138c4f8890Srs 	if (vhci_node == DI_NODE_NIL) {
24148c4f8890Srs 		errno = EINVAL;
24158c4f8890Srs 		return (DI_NODE_NIL);
24168c4f8890Srs 	}
24178c4f8890Srs 
24188c4f8890Srs 	pa = (caddr_t)vhci_node - DI_NODE(vhci_node)->self;
24198c4f8890Srs 
24208c4f8890Srs 	if (DI_NODE(vhci_node)->top_phci == NULL) {
24218c4f8890Srs 		errno = ENXIO;
24228c4f8890Srs 		return (DI_NODE_NIL);
24238c4f8890Srs 	}
24248c4f8890Srs 
24258c4f8890Srs 	return (DI_NODE(pa + DI_NODE(vhci_node)->top_phci));
24268c4f8890Srs }
24278c4f8890Srs 
24288c4f8890Srs di_node_t
24298c4f8890Srs di_phci_next_node(di_node_t node)
24308c4f8890Srs {
24318c4f8890Srs 	caddr_t		pa;		/* starting address of map */
24328c4f8890Srs 
24338c4f8890Srs 	if (node == DI_NODE_NIL) {
24348c4f8890Srs 		errno = EINVAL;
24358c4f8890Srs 		return (DI_NODE_NIL);
24368c4f8890Srs 	}
24378c4f8890Srs 
24388c4f8890Srs 	DPRINTF((DI_TRACE, "next phci node on the snap shot:"
24398c4f8890Srs 	    " current=%s\n", di_node_name(node)));
24408c4f8890Srs 
24418c4f8890Srs 	if (DI_NODE(node)->next_phci == NULL) {
24428c4f8890Srs 		errno = ENXIO;
24438c4f8890Srs 		return (DI_NODE_NIL);
24448c4f8890Srs 	}
24458c4f8890Srs 
24468c4f8890Srs 	pa = (caddr_t)node - DI_NODE(node)->self;
24478c4f8890Srs 
24488c4f8890Srs 	return (DI_NODE(pa + DI_NODE(node)->next_phci));
24498c4f8890Srs }
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate /*
24527c478bd9Sstevel@tonic-gate  * Consolidation private interfaces for private data
24537c478bd9Sstevel@tonic-gate  */
24547c478bd9Sstevel@tonic-gate void *
24557c478bd9Sstevel@tonic-gate di_parent_private_data(di_node_t node)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate 	caddr_t pa;
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent_data == 0) {
24607c478bd9Sstevel@tonic-gate 		errno = ENXIO;
24617c478bd9Sstevel@tonic-gate 		return (NULL);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent_data == (di_off_t)-1) {
24657c478bd9Sstevel@tonic-gate 		/*
24667c478bd9Sstevel@tonic-gate 		 * Private data requested, but not obtained due to a memory
24677c478bd9Sstevel@tonic-gate 		 * error (e.g. wrong format specified)
24687c478bd9Sstevel@tonic-gate 		 */
24697c478bd9Sstevel@tonic-gate 		errno = EFAULT;
24707c478bd9Sstevel@tonic-gate 		return (NULL);
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
24747c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent_data)
24757c478bd9Sstevel@tonic-gate 		return (pa + DI_NODE(node)->parent_data);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	if (DI_ALL(pa)->command & DINFOPRIVDATA)
24787c478bd9Sstevel@tonic-gate 		errno = ENXIO;
24797c478bd9Sstevel@tonic-gate 	else
24807c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	return (NULL);
24837c478bd9Sstevel@tonic-gate }
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate void *
24867c478bd9Sstevel@tonic-gate di_driver_private_data(di_node_t node)
24877c478bd9Sstevel@tonic-gate {
24887c478bd9Sstevel@tonic-gate 	caddr_t pa;
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->driver_data == 0) {
24917c478bd9Sstevel@tonic-gate 		errno = ENXIO;
24927c478bd9Sstevel@tonic-gate 		return (NULL);
24937c478bd9Sstevel@tonic-gate 	}
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->driver_data == (di_off_t)-1) {
24967c478bd9Sstevel@tonic-gate 		/*
24977c478bd9Sstevel@tonic-gate 		 * Private data requested, but not obtained due to a memory
24987c478bd9Sstevel@tonic-gate 		 * error (e.g. wrong format specified)
24997c478bd9Sstevel@tonic-gate 		 */
25007c478bd9Sstevel@tonic-gate 		errno = EFAULT;
25017c478bd9Sstevel@tonic-gate 		return (NULL);
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
25057c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->driver_data)
25067c478bd9Sstevel@tonic-gate 		return (pa + DI_NODE(node)->driver_data);
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	if (DI_ALL(pa)->command & DINFOPRIVDATA)
25097c478bd9Sstevel@tonic-gate 		errno = ENXIO;
25107c478bd9Sstevel@tonic-gate 	else
25117c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 	return (NULL);
25147c478bd9Sstevel@tonic-gate }
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate /*
25177c478bd9Sstevel@tonic-gate  * PROM property access
25187c478bd9Sstevel@tonic-gate  */
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate /*
25217c478bd9Sstevel@tonic-gate  * openprom driver stuff:
25227c478bd9Sstevel@tonic-gate  *	The maximum property length depends on the buffer size. We use
25237c478bd9Sstevel@tonic-gate  *	OPROMMAXPARAM defined in <sys/openpromio.h>
25247c478bd9Sstevel@tonic-gate  *
25257c478bd9Sstevel@tonic-gate  *	MAXNAMESZ is max property name. obpdefs.h defines it as 32 based on 1275
25267c478bd9Sstevel@tonic-gate  *	MAXVALSZ is maximum value size, which is whatever space left in buf
25277c478bd9Sstevel@tonic-gate  */
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate #define	OBP_MAXBUF	OPROMMAXPARAM - sizeof (int)
25307c478bd9Sstevel@tonic-gate #define	OBP_MAXPROPLEN	OBP_MAXBUF - OBP_MAXPROPNAME;
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate struct di_prom_prop {
25337c478bd9Sstevel@tonic-gate 	char *name;
25347c478bd9Sstevel@tonic-gate 	int len;
25357c478bd9Sstevel@tonic-gate 	uchar_t *data;
25367c478bd9Sstevel@tonic-gate 	struct di_prom_prop *next;	/* form a linked list */
25377c478bd9Sstevel@tonic-gate };
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate struct di_prom_handle { /* handle to prom */
25407c478bd9Sstevel@tonic-gate 	mutex_t lock;	/* synchronize access to openprom fd */
25417c478bd9Sstevel@tonic-gate 	int	fd;	/* /dev/openprom file descriptor */
25427c478bd9Sstevel@tonic-gate 	struct di_prom_prop *list;	/* linked list of prop */
25437c478bd9Sstevel@tonic-gate 	union {
25447c478bd9Sstevel@tonic-gate 		char buf[OPROMMAXPARAM];
25457c478bd9Sstevel@tonic-gate 		struct openpromio opp;
25467c478bd9Sstevel@tonic-gate 	} oppbuf;
25477c478bd9Sstevel@tonic-gate };
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate di_prom_handle_t
25507c478bd9Sstevel@tonic-gate di_prom_init()
25517c478bd9Sstevel@tonic-gate {
25527c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p;
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (struct di_prom_handle))) == NULL)
25557c478bd9Sstevel@tonic-gate 		return (DI_PROM_HANDLE_NIL);
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_prom_init: get prom handle 0x%p\n", p));
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	(void) mutex_init(&p->lock, USYNC_THREAD, NULL);
25607c478bd9Sstevel@tonic-gate 	if ((p->fd = open("/dev/openprom", O_RDONLY)) < 0) {
25617c478bd9Sstevel@tonic-gate 		free(p);
25627c478bd9Sstevel@tonic-gate 		return (DI_PROM_HANDLE_NIL);
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 	p->list = NULL;
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	return ((di_prom_handle_t)p);
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate static void
25707c478bd9Sstevel@tonic-gate di_prom_prop_free(struct di_prom_prop *list)
25717c478bd9Sstevel@tonic-gate {
25727c478bd9Sstevel@tonic-gate 	struct di_prom_prop *tmp = list;
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 	while (tmp != NULL) {
25757c478bd9Sstevel@tonic-gate 		list = tmp->next;
25767c478bd9Sstevel@tonic-gate 		if (tmp->name != NULL) {
25777c478bd9Sstevel@tonic-gate 			free(tmp->name);
25787c478bd9Sstevel@tonic-gate 		}
25797c478bd9Sstevel@tonic-gate 		if (tmp->data != NULL) {
25807c478bd9Sstevel@tonic-gate 			free(tmp->data);
25817c478bd9Sstevel@tonic-gate 		}
25827c478bd9Sstevel@tonic-gate 		free(tmp);
25837c478bd9Sstevel@tonic-gate 		tmp = list;
25847c478bd9Sstevel@tonic-gate 	}
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate void
25887c478bd9Sstevel@tonic-gate di_prom_fini(di_prom_handle_t ph)
25897c478bd9Sstevel@tonic-gate {
25907c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_prom_fini: free prom handle 0x%p\n", p));
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 	(void) close(p->fd);
25957c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&p->lock);
25967c478bd9Sstevel@tonic-gate 	di_prom_prop_free(p->list);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	free(p);
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate /*
26027c478bd9Sstevel@tonic-gate  * Internal library interface for locating the property
26037c478bd9Sstevel@tonic-gate  * XXX: ph->lock must be held for the duration of call.
26047c478bd9Sstevel@tonic-gate  */
26057c478bd9Sstevel@tonic-gate static di_prom_prop_t
26067c478bd9Sstevel@tonic-gate di_prom_prop_found(di_prom_handle_t ph, int nodeid,
26077c478bd9Sstevel@tonic-gate 	di_prom_prop_t prom_prop)
26087c478bd9Sstevel@tonic-gate {
26097c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
26107c478bd9Sstevel@tonic-gate 	struct openpromio *opp = &p->oppbuf.opp;
26117c478bd9Sstevel@tonic-gate 	int *ip = (int *)((void *)opp->oprom_array);
26127c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop = (struct di_prom_prop *)prom_prop;
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE1, "Looking for nodeid 0x%x\n", nodeid));
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate 	/*
26177c478bd9Sstevel@tonic-gate 	 * Set "current" nodeid in the openprom driver
26187c478bd9Sstevel@tonic-gate 	 */
26197c478bd9Sstevel@tonic-gate 	opp->oprom_size = sizeof (int);
26207c478bd9Sstevel@tonic-gate 	*ip = nodeid;
26217c478bd9Sstevel@tonic-gate 	if (ioctl(p->fd, OPROMSETNODEID, opp) < 0) {
26227c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "*** Nodeid not found 0x%x\n", nodeid));
26237c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
26247c478bd9Sstevel@tonic-gate 	}
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Found nodeid 0x%x\n", nodeid));
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 	bzero(opp, OBP_MAXBUF);
26297c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPNAME;
26307c478bd9Sstevel@tonic-gate 	if (prom_prop != DI_PROM_PROP_NIL)
26317c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, prop->name);
26327c478bd9Sstevel@tonic-gate 
26337c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMNXTPROP, opp) < 0) || (opp->oprom_size == 0))
26347c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	/*
26377c478bd9Sstevel@tonic-gate 	 * Prom property found. Allocate struct for storing prop
26387c478bd9Sstevel@tonic-gate 	 *   (reuse variable prop)
26397c478bd9Sstevel@tonic-gate 	 */
26407c478bd9Sstevel@tonic-gate 	if ((prop = malloc(sizeof (struct di_prom_prop))) == NULL)
26417c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	/*
26447c478bd9Sstevel@tonic-gate 	 * Get a copy of property name
26457c478bd9Sstevel@tonic-gate 	 */
26467c478bd9Sstevel@tonic-gate 	if ((prop->name = strdup(opp->oprom_array)) == NULL) {
26477c478bd9Sstevel@tonic-gate 		free(prop);
26487c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
26497c478bd9Sstevel@tonic-gate 	}
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	/*
26527c478bd9Sstevel@tonic-gate 	 * get property value and length
26537c478bd9Sstevel@tonic-gate 	 */
26547c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPLEN;
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMGETPROP, opp) < 0) ||
26577c478bd9Sstevel@tonic-gate 	    (opp->oprom_size == (uint_t)-1)) {
26587c478bd9Sstevel@tonic-gate 		free(prop->name);
26597c478bd9Sstevel@tonic-gate 		free(prop);
26607c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
26617c478bd9Sstevel@tonic-gate 	}
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	/*
26647c478bd9Sstevel@tonic-gate 	 * make a copy of the property value
26657c478bd9Sstevel@tonic-gate 	 */
26667c478bd9Sstevel@tonic-gate 	prop->len = opp->oprom_size;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	if (prop->len == 0)
26697c478bd9Sstevel@tonic-gate 		prop->data = NULL;
26707c478bd9Sstevel@tonic-gate 	else if ((prop->data = malloc(prop->len)) == NULL) {
26717c478bd9Sstevel@tonic-gate 		free(prop->name);
26727c478bd9Sstevel@tonic-gate 		free(prop);
26737c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
26747c478bd9Sstevel@tonic-gate 	}
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	bcopy(opp->oprom_array, prop->data, prop->len);
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	/*
26797c478bd9Sstevel@tonic-gate 	 * Prepend prop to list in prom handle
26807c478bd9Sstevel@tonic-gate 	 */
26817c478bd9Sstevel@tonic-gate 	prop->next = p->list;
26827c478bd9Sstevel@tonic-gate 	p->list = prop;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	return ((di_prom_prop_t)prop);
26857c478bd9Sstevel@tonic-gate }
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate di_prom_prop_t
26887c478bd9Sstevel@tonic-gate di_prom_prop_next(di_prom_handle_t ph, di_node_t node, di_prom_prop_t prom_prop)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE1, "Search next prop for node 0x%p with ph 0x%p\n",
2693*602ca9eaScth 	    node, p));
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 	/*
26967c478bd9Sstevel@tonic-gate 	 * paranoid check
26977c478bd9Sstevel@tonic-gate 	 */
26987c478bd9Sstevel@tonic-gate 	if ((ph == DI_PROM_HANDLE_NIL) || (node == DI_NODE_NIL)) {
26997c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27007c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
27017c478bd9Sstevel@tonic-gate 	}
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 	if (di_nodeid(node) != DI_PROM_NODEID) {
27047c478bd9Sstevel@tonic-gate 		errno = ENXIO;
27057c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate 	/*
27097c478bd9Sstevel@tonic-gate 	 * synchronize access to prom file descriptor
27107c478bd9Sstevel@tonic-gate 	 */
27117c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&p->lock);
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	/*
27147c478bd9Sstevel@tonic-gate 	 * look for next property
27157c478bd9Sstevel@tonic-gate 	 */
27167c478bd9Sstevel@tonic-gate 	prom_prop = di_prom_prop_found(ph, DI_NODE(node)->nodeid, prom_prop);
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&p->lock);
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate 	return (prom_prop);
27217c478bd9Sstevel@tonic-gate }
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate char *
27247c478bd9Sstevel@tonic-gate di_prom_prop_name(di_prom_prop_t prom_prop)
27257c478bd9Sstevel@tonic-gate {
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * paranoid check
27287c478bd9Sstevel@tonic-gate 	 */
27297c478bd9Sstevel@tonic-gate 	if (prom_prop == DI_PROM_PROP_NIL) {
27307c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27317c478bd9Sstevel@tonic-gate 		return (NULL);
27327c478bd9Sstevel@tonic-gate 	}
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	return (((struct di_prom_prop *)prom_prop)->name);
27357c478bd9Sstevel@tonic-gate }
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate int
27387c478bd9Sstevel@tonic-gate di_prom_prop_data(di_prom_prop_t prom_prop, uchar_t **prom_prop_data)
27397c478bd9Sstevel@tonic-gate {
27407c478bd9Sstevel@tonic-gate 	/*
27417c478bd9Sstevel@tonic-gate 	 * paranoid check
27427c478bd9Sstevel@tonic-gate 	 */
27437c478bd9Sstevel@tonic-gate 	if (prom_prop == DI_PROM_PROP_NIL) {
27447c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27457c478bd9Sstevel@tonic-gate 		return (NULL);
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	*prom_prop_data = ((struct di_prom_prop *)prom_prop)->data;
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	return (((struct di_prom_prop *)prom_prop)->len);
27517c478bd9Sstevel@tonic-gate }
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate /*
27547c478bd9Sstevel@tonic-gate  * Internal library interface for locating the property
27557c478bd9Sstevel@tonic-gate  *    Returns length if found, -1 if prop doesn't exist.
27567c478bd9Sstevel@tonic-gate  */
27577c478bd9Sstevel@tonic-gate static struct di_prom_prop *
27587c478bd9Sstevel@tonic-gate di_prom_prop_lookup_common(di_prom_handle_t ph, di_node_t node,
27597c478bd9Sstevel@tonic-gate 	const char *prom_prop_name)
27607c478bd9Sstevel@tonic-gate {
27617c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
27627c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
27637c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 	/*
27667c478bd9Sstevel@tonic-gate 	 * paranoid check
27677c478bd9Sstevel@tonic-gate 	 */
27687c478bd9Sstevel@tonic-gate 	if ((ph == DI_PROM_HANDLE_NIL) || (node == DI_NODE_NIL)) {
27697c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27707c478bd9Sstevel@tonic-gate 		return (NULL);
27717c478bd9Sstevel@tonic-gate 	}
27727c478bd9Sstevel@tonic-gate 
27737c478bd9Sstevel@tonic-gate 	if (di_nodeid(node) != DI_PROM_NODEID) {
27747c478bd9Sstevel@tonic-gate 		errno = ENXIO;
27757c478bd9Sstevel@tonic-gate 		return (NULL);
27767c478bd9Sstevel@tonic-gate 	}
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 	opp = &p->oppbuf.opp;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&p->lock);
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate 	opp->oprom_size = sizeof (int);
27837c478bd9Sstevel@tonic-gate 	opp->oprom_node = DI_NODE(node)->nodeid;
27847c478bd9Sstevel@tonic-gate 	if (ioctl(p->fd, OPROMSETNODEID, opp) < 0) {
27857c478bd9Sstevel@tonic-gate 		errno = ENXIO;
27867c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "*** Nodeid not found 0x%x\n",
27877c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->nodeid));
27887c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
27897c478bd9Sstevel@tonic-gate 		return (NULL);
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	/*
27937c478bd9Sstevel@tonic-gate 	 * get property length
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	bzero(opp, OBP_MAXBUF);
27967c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPLEN;
27977c478bd9Sstevel@tonic-gate 	(void) strcpy(opp->oprom_array, prom_prop_name);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMGETPROPLEN, opp) < 0) ||
28007c478bd9Sstevel@tonic-gate 	    (opp->oprom_len == -1)) {
28017c478bd9Sstevel@tonic-gate 		/* no such property */
28027c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
28037c478bd9Sstevel@tonic-gate 		return (NULL);
28047c478bd9Sstevel@tonic-gate 	}
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 	/*
28077c478bd9Sstevel@tonic-gate 	 * Prom property found. Allocate struct for storing prop
28087c478bd9Sstevel@tonic-gate 	 */
28097c478bd9Sstevel@tonic-gate 	if ((prop = malloc(sizeof (struct di_prom_prop))) == NULL) {
28107c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
28117c478bd9Sstevel@tonic-gate 		return (NULL);
28127c478bd9Sstevel@tonic-gate 	}
28137c478bd9Sstevel@tonic-gate 	prop->name = NULL;	/* we don't need the name */
28147c478bd9Sstevel@tonic-gate 	prop->len = opp->oprom_len;
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
28177c478bd9Sstevel@tonic-gate 		prop->data = NULL;
28187c478bd9Sstevel@tonic-gate 		prop->next = p->list;
28197c478bd9Sstevel@tonic-gate 		p->list = prop;
28207c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
28217c478bd9Sstevel@tonic-gate 		return (prop);
28227c478bd9Sstevel@tonic-gate 	}
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	/*
28257c478bd9Sstevel@tonic-gate 	 * retrieve the property value
28267c478bd9Sstevel@tonic-gate 	 */
28277c478bd9Sstevel@tonic-gate 	bzero(opp, OBP_MAXBUF);
28287c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPLEN;
28297c478bd9Sstevel@tonic-gate 	(void) strcpy(opp->oprom_array, prom_prop_name);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMGETPROP, opp) < 0) ||
28327c478bd9Sstevel@tonic-gate 	    (opp->oprom_size == (uint_t)-1)) {
28337c478bd9Sstevel@tonic-gate 		/* error retrieving property value */
28347c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
28357c478bd9Sstevel@tonic-gate 		free(prop);
28367c478bd9Sstevel@tonic-gate 		return (NULL);
28377c478bd9Sstevel@tonic-gate 	}
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	/*
28407c478bd9Sstevel@tonic-gate 	 * make a copy of the property value, stick in ph->list
28417c478bd9Sstevel@tonic-gate 	 */
28427c478bd9Sstevel@tonic-gate 	if ((prop->data = malloc(prop->len)) == NULL) {
28437c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
28447c478bd9Sstevel@tonic-gate 		free(prop);
28457c478bd9Sstevel@tonic-gate 		return (NULL);
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	bcopy(opp->oprom_array, prop->data, prop->len);
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 	prop->next = p->list;
28517c478bd9Sstevel@tonic-gate 	p->list = prop;
28527c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&p->lock);
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	return (prop);
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate int
28587c478bd9Sstevel@tonic-gate di_prom_prop_lookup_ints(di_prom_handle_t ph, di_node_t node,
28597c478bd9Sstevel@tonic-gate 	const char *prom_prop_name, int **prom_prop_data)
28607c478bd9Sstevel@tonic-gate {
28617c478bd9Sstevel@tonic-gate 	int len;
28627c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 	prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
28677c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
28687c478bd9Sstevel@tonic-gate 		return (-1);
28697c478bd9Sstevel@tonic-gate 	}
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
28727c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
28737c478bd9Sstevel@tonic-gate 		return (0);
28747c478bd9Sstevel@tonic-gate 	}
28757c478bd9Sstevel@tonic-gate 
28767c478bd9Sstevel@tonic-gate 	len = di_prop_decode_common((void *)&prop->data, prop->len,
2877*602ca9eaScth 	    DI_PROP_TYPE_INT, 1);
28787c478bd9Sstevel@tonic-gate 	*prom_prop_data = (int *)((void *)prop->data);
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	return (len);
28817c478bd9Sstevel@tonic-gate }
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate int
28847c478bd9Sstevel@tonic-gate di_prom_prop_lookup_strings(di_prom_handle_t ph, di_node_t node,
28857c478bd9Sstevel@tonic-gate 	const char *prom_prop_name, char **prom_prop_data)
28867c478bd9Sstevel@tonic-gate {
28877c478bd9Sstevel@tonic-gate 	int len;
28887c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
28937c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
28947c478bd9Sstevel@tonic-gate 		return (-1);
28957c478bd9Sstevel@tonic-gate 	}
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
28987c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
28997c478bd9Sstevel@tonic-gate 		return (0);
29007c478bd9Sstevel@tonic-gate 	}
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	/*
29037c478bd9Sstevel@tonic-gate 	 * Fix an openprom bug (OBP string not NULL terminated).
29047c478bd9Sstevel@tonic-gate 	 * XXX This should really be fixed in promif.
29057c478bd9Sstevel@tonic-gate 	 */
29067c478bd9Sstevel@tonic-gate 	if (((char *)prop->data)[prop->len - 1] != '\0') {
29077c478bd9Sstevel@tonic-gate 		uchar_t *tmp;
29087c478bd9Sstevel@tonic-gate 		prop->len++;
29097c478bd9Sstevel@tonic-gate 		if ((tmp = realloc(prop->data, prop->len)) == NULL)
29107c478bd9Sstevel@tonic-gate 			return (-1);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 		prop->data = tmp;
29137c478bd9Sstevel@tonic-gate 		((char *)prop->data)[prop->len - 1] = '\0';
29147c478bd9Sstevel@tonic-gate 		DPRINTF((DI_INFO, "OBP string not NULL terminated: "
29157c478bd9Sstevel@tonic-gate 		    "node=%s, prop=%s, val=%s\n",
29167c478bd9Sstevel@tonic-gate 		    di_node_name(node), prom_prop_name, prop->data));
29177c478bd9Sstevel@tonic-gate 	}
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 	len = di_prop_decode_common((void *)&prop->data, prop->len,
29207c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_STRING, 1);
29217c478bd9Sstevel@tonic-gate 	*prom_prop_data = (char *)prop->data;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	return (len);
29247c478bd9Sstevel@tonic-gate }
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate int
29277c478bd9Sstevel@tonic-gate di_prom_prop_lookup_bytes(di_prom_handle_t ph, di_node_t node,
29287c478bd9Sstevel@tonic-gate 	const char *prom_prop_name, uchar_t **prom_prop_data)
29297c478bd9Sstevel@tonic-gate {
29307c478bd9Sstevel@tonic-gate 	int len;
29317c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 	prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
29367c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
29377c478bd9Sstevel@tonic-gate 		return (-1);
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
29417c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
29427c478bd9Sstevel@tonic-gate 		return (0);
29437c478bd9Sstevel@tonic-gate 	}
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	len = di_prop_decode_common((void *)&prop->data, prop->len,
29467c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_BYTE, 1);
29477c478bd9Sstevel@tonic-gate 	*prom_prop_data = prop->data;
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	return (len);
29507c478bd9Sstevel@tonic-gate }
29517c478bd9Sstevel@tonic-gate 
29523ebafc43Sjveta /*
29533ebafc43Sjveta  * returns an allocated array through <prop_data> only when its count > 0
29543ebafc43Sjveta  * and the number of entries (count) as the function return value;
29553ebafc43Sjveta  * use di_slot_names_free() to free the array
29563ebafc43Sjveta  */
29573ebafc43Sjveta int
29583ebafc43Sjveta di_prop_slot_names(di_prop_t prop, di_slot_name_t **prop_data)
29593ebafc43Sjveta {
29603ebafc43Sjveta 	int rawlen, count;
29613ebafc43Sjveta 	uchar_t *rawdata;
29623ebafc43Sjveta 	char *nm = di_prop_name(prop);
29633ebafc43Sjveta 
29643ebafc43Sjveta 	if (nm == NULL || strcmp(DI_PROP_SLOT_NAMES, nm) != 0)
29653ebafc43Sjveta 		goto ERROUT;
29663ebafc43Sjveta 
29673ebafc43Sjveta 	rawlen = di_prop_rawdata(prop, &rawdata);
29683ebafc43Sjveta 	if (rawlen <= 0 || rawdata == NULL)
29693ebafc43Sjveta 		goto ERROUT;
29703ebafc43Sjveta 
29713ebafc43Sjveta 	count = di_slot_names_decode(rawdata, rawlen, prop_data);
29723ebafc43Sjveta 	if (count < 0 || *prop_data == NULL)
29733ebafc43Sjveta 		goto ERROUT;
29743ebafc43Sjveta 
29753ebafc43Sjveta 	return (count);
29763ebafc43Sjveta 	/*NOTREACHED*/
29773ebafc43Sjveta ERROUT:
29783ebafc43Sjveta 	errno = EFAULT;
29793ebafc43Sjveta 	*prop_data = NULL;
29803ebafc43Sjveta 	return (-1);
29813ebafc43Sjveta }
29823ebafc43Sjveta 
29833ebafc43Sjveta int
29843ebafc43Sjveta di_prop_lookup_slot_names(dev_t dev, di_node_t node,
29853ebafc43Sjveta     di_slot_name_t **prop_data)
29863ebafc43Sjveta {
29873ebafc43Sjveta 	di_prop_t prop;
29883ebafc43Sjveta 
29893ebafc43Sjveta 	/*
29903ebafc43Sjveta 	 * change this if and when DI_PROP_TYPE_COMPOSITE is implemented
29913ebafc43Sjveta 	 * and slot-names is properly flagged as such
29923ebafc43Sjveta 	 */
29933ebafc43Sjveta 	if ((prop = di_prop_find(dev, node, DI_PROP_SLOT_NAMES)) ==
29943ebafc43Sjveta 	    DI_PROP_NIL) {
29953ebafc43Sjveta 		*prop_data = NULL;
29963ebafc43Sjveta 		return (-1);
29973ebafc43Sjveta 	}
29983ebafc43Sjveta 
29993ebafc43Sjveta 	return (di_prop_slot_names(prop, (void *)prop_data));
30003ebafc43Sjveta }
30013ebafc43Sjveta 
30023ebafc43Sjveta /*
30033ebafc43Sjveta  * returns an allocated array through <prop_data> only when its count > 0
30043ebafc43Sjveta  * and the number of entries (count) as the function return value;
30053ebafc43Sjveta  * use di_slot_names_free() to free the array
30063ebafc43Sjveta  */
30073ebafc43Sjveta int
30083ebafc43Sjveta di_prom_prop_slot_names(di_prom_prop_t prom_prop, di_slot_name_t **prop_data)
30093ebafc43Sjveta {
30103ebafc43Sjveta 	int rawlen, count;
30113ebafc43Sjveta 	uchar_t *rawdata;
30123ebafc43Sjveta 
30133ebafc43Sjveta 	rawlen = di_prom_prop_data(prom_prop, &rawdata);
30143ebafc43Sjveta 	if (rawlen <= 0 || rawdata == NULL)
30153ebafc43Sjveta 		goto ERROUT;
30163ebafc43Sjveta 
30173ebafc43Sjveta 	count = di_slot_names_decode(rawdata, rawlen, prop_data);
30183ebafc43Sjveta 	if (count < 0 || *prop_data == NULL)
30193ebafc43Sjveta 		goto ERROUT;
30203ebafc43Sjveta 
30213ebafc43Sjveta 	return (count);
30223ebafc43Sjveta 	/*NOTREACHED*/
30233ebafc43Sjveta ERROUT:
30243ebafc43Sjveta 	errno = EFAULT;
30253ebafc43Sjveta 	*prop_data = NULL;
30263ebafc43Sjveta 	return (-1);
30273ebafc43Sjveta }
30283ebafc43Sjveta 
30293ebafc43Sjveta int
30303ebafc43Sjveta di_prom_prop_lookup_slot_names(di_prom_handle_t ph, di_node_t node,
30313ebafc43Sjveta     di_slot_name_t **prop_data)
30323ebafc43Sjveta {
30333ebafc43Sjveta 	struct di_prom_prop *prom_prop;
30343ebafc43Sjveta 
30353ebafc43Sjveta 	prom_prop = di_prom_prop_lookup_common(ph, node, DI_PROP_SLOT_NAMES);
30363ebafc43Sjveta 	if (prom_prop == NULL) {
30373ebafc43Sjveta 		*prop_data = NULL;
30383ebafc43Sjveta 		return (-1);
30393ebafc43Sjveta 	}
30403ebafc43Sjveta 
30413ebafc43Sjveta 	return (di_prom_prop_slot_names(prom_prop, prop_data));
30423ebafc43Sjveta }
30433ebafc43Sjveta 
30447c478bd9Sstevel@tonic-gate di_lnode_t
30457c478bd9Sstevel@tonic-gate di_link_to_lnode(di_link_t link, uint_t endpoint)
30467c478bd9Sstevel@tonic-gate {
30477c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	if ((link == DI_LINK_NIL) ||
30507c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
30517c478bd9Sstevel@tonic-gate 		errno = EINVAL;
30527c478bd9Sstevel@tonic-gate 		return (DI_LNODE_NIL);
30537c478bd9Sstevel@tonic-gate 	}
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)link - DI_LINK(link)->self);
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC) {
30587c478bd9Sstevel@tonic-gate 		return (DI_LNODE((caddr_t)di_all + DI_LINK(link)->src_lnode));
30597c478bd9Sstevel@tonic-gate 	} else {
30607c478bd9Sstevel@tonic-gate 		return (DI_LNODE((caddr_t)di_all + DI_LINK(link)->tgt_lnode));
30617c478bd9Sstevel@tonic-gate 	}
30627c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
30637c478bd9Sstevel@tonic-gate }
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate char *
30667c478bd9Sstevel@tonic-gate di_lnode_name(di_lnode_t lnode)
30677c478bd9Sstevel@tonic-gate {
30687c478bd9Sstevel@tonic-gate 	return (di_driver_name(di_lnode_devinfo(lnode)));
30697c478bd9Sstevel@tonic-gate }
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate di_node_t
30727c478bd9Sstevel@tonic-gate di_lnode_devinfo(di_lnode_t lnode)
30737c478bd9Sstevel@tonic-gate {
30747c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)lnode - DI_LNODE(lnode)->self);
30777c478bd9Sstevel@tonic-gate 	return (DI_NODE((caddr_t)di_all + DI_LNODE(lnode)->node));
30787c478bd9Sstevel@tonic-gate }
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate int
30817c478bd9Sstevel@tonic-gate di_lnode_devt(di_lnode_t lnode, dev_t *devt)
30827c478bd9Sstevel@tonic-gate {
30837c478bd9Sstevel@tonic-gate 	if ((lnode == DI_LNODE_NIL) || (devt == NULL)) {
30847c478bd9Sstevel@tonic-gate 		errno = EINVAL;
30857c478bd9Sstevel@tonic-gate 		return (-1);
30867c478bd9Sstevel@tonic-gate 	}
30877c478bd9Sstevel@tonic-gate 	if ((DI_LNODE(lnode)->dev_major == (major_t)-1) &&
30887c478bd9Sstevel@tonic-gate 	    (DI_LNODE(lnode)->dev_minor == (minor_t)-1))
30897c478bd9Sstevel@tonic-gate 		return (-1);
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 	*devt = makedev(DI_LNODE(lnode)->dev_major, DI_LNODE(lnode)->dev_minor);
30927c478bd9Sstevel@tonic-gate 	return (0);
30937c478bd9Sstevel@tonic-gate }
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate int
30967c478bd9Sstevel@tonic-gate di_link_spectype(di_link_t link)
30977c478bd9Sstevel@tonic-gate {
30987c478bd9Sstevel@tonic-gate 	return (DI_LINK(link)->spec_type);
30997c478bd9Sstevel@tonic-gate }
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate void
31027c478bd9Sstevel@tonic-gate di_minor_private_set(di_minor_t minor, void *data)
31037c478bd9Sstevel@tonic-gate {
31047c478bd9Sstevel@tonic-gate 	DI_MINOR(minor)->user_private_data = (uintptr_t)data;
31057c478bd9Sstevel@tonic-gate }
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate void *
31087c478bd9Sstevel@tonic-gate di_minor_private_get(di_minor_t minor)
31097c478bd9Sstevel@tonic-gate {
31108309866bSanish 	return ((void *)(uintptr_t)DI_MINOR(minor)->user_private_data);
31117c478bd9Sstevel@tonic-gate }
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate void
31147c478bd9Sstevel@tonic-gate di_node_private_set(di_node_t node, void *data)
31157c478bd9Sstevel@tonic-gate {
31167c478bd9Sstevel@tonic-gate 	DI_NODE(node)->user_private_data = (uintptr_t)data;
31177c478bd9Sstevel@tonic-gate }
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate void *
31207c478bd9Sstevel@tonic-gate di_node_private_get(di_node_t node)
31217c478bd9Sstevel@tonic-gate {
31228309866bSanish 	return ((void *)(uintptr_t)DI_NODE(node)->user_private_data);
31237c478bd9Sstevel@tonic-gate }
31247c478bd9Sstevel@tonic-gate 
3125*602ca9eaScth void
3126*602ca9eaScth di_path_private_set(di_path_t path, void *data)
3127*602ca9eaScth {
3128*602ca9eaScth 	DI_PATH(path)->user_private_data = (uintptr_t)data;
3129*602ca9eaScth }
3130*602ca9eaScth 
3131*602ca9eaScth void *
3132*602ca9eaScth di_path_private_get(di_path_t path)
3133*602ca9eaScth {
3134*602ca9eaScth 	return ((void *)(uintptr_t)DI_PATH(path)->user_private_data);
3135*602ca9eaScth }
3136*602ca9eaScth 
31377c478bd9Sstevel@tonic-gate void
31387c478bd9Sstevel@tonic-gate di_lnode_private_set(di_lnode_t lnode, void *data)
31397c478bd9Sstevel@tonic-gate {
31407c478bd9Sstevel@tonic-gate 	DI_LNODE(lnode)->user_private_data = (uintptr_t)data;
31417c478bd9Sstevel@tonic-gate }
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate void *
31447c478bd9Sstevel@tonic-gate di_lnode_private_get(di_lnode_t lnode)
31457c478bd9Sstevel@tonic-gate {
31468309866bSanish 	return ((void *)(uintptr_t)DI_LNODE(lnode)->user_private_data);
31477c478bd9Sstevel@tonic-gate }
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate void
31507c478bd9Sstevel@tonic-gate di_link_private_set(di_link_t link, void *data)
31517c478bd9Sstevel@tonic-gate {
31527c478bd9Sstevel@tonic-gate 	DI_LINK(link)->user_private_data = (uintptr_t)data;
31537c478bd9Sstevel@tonic-gate }
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate void *
31567c478bd9Sstevel@tonic-gate di_link_private_get(di_link_t link)
31577c478bd9Sstevel@tonic-gate {
31588309866bSanish 	return ((void *)(uintptr_t)DI_LINK(link)->user_private_data);
31597c478bd9Sstevel@tonic-gate }
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate di_lnode_t
31627c478bd9Sstevel@tonic-gate di_lnode_next(di_node_t node, di_lnode_t lnode)
31637c478bd9Sstevel@tonic-gate {
31647c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	/*
31677c478bd9Sstevel@tonic-gate 	 * paranoid error checking
31687c478bd9Sstevel@tonic-gate 	 */
31697c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
31707c478bd9Sstevel@tonic-gate 		errno = EINVAL;
31717c478bd9Sstevel@tonic-gate 		return (DI_LNODE_NIL);
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)node - DI_NODE(node)->self);
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	if (lnode == DI_NODE_NIL) {
31777c478bd9Sstevel@tonic-gate 		if (DI_NODE(node)->lnodes != NULL)
31787c478bd9Sstevel@tonic-gate 			return (DI_LNODE((caddr_t)di_all +
31797c478bd9Sstevel@tonic-gate 			    DI_NODE(node)->lnodes));
31807c478bd9Sstevel@tonic-gate 	} else {
31817c478bd9Sstevel@tonic-gate 		if (DI_LNODE(lnode)->node_next != NULL)
31827c478bd9Sstevel@tonic-gate 			return (DI_LNODE((caddr_t)di_all +
31837c478bd9Sstevel@tonic-gate 			    DI_LNODE(lnode)->node_next));
31847c478bd9Sstevel@tonic-gate 	}
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	if (DINFOLYR & DI_ALL(di_all)->command)
31877c478bd9Sstevel@tonic-gate 		errno = ENXIO;
31887c478bd9Sstevel@tonic-gate 	else
31897c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate 	return (DI_LNODE_NIL);
31927c478bd9Sstevel@tonic-gate }
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate di_link_t
31957c478bd9Sstevel@tonic-gate di_link_next_by_node(di_node_t node, di_link_t link, uint_t endpoint)
31967c478bd9Sstevel@tonic-gate {
31977c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	/*
32007c478bd9Sstevel@tonic-gate 	 * paranoid error checking
32017c478bd9Sstevel@tonic-gate 	 */
32027c478bd9Sstevel@tonic-gate 	if ((node == DI_NODE_NIL) ||
32037c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
32047c478bd9Sstevel@tonic-gate 		errno = EINVAL;
32057c478bd9Sstevel@tonic-gate 		return (DI_LINK_NIL);
32067c478bd9Sstevel@tonic-gate 	}
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)node - DI_NODE(node)->self);
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC) {
32117c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
32127c478bd9Sstevel@tonic-gate 			if (DI_NODE(node)->src_links != NULL)
32137c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
32147c478bd9Sstevel@tonic-gate 				    DI_NODE(node)->src_links));
32157c478bd9Sstevel@tonic-gate 		} else {
32167c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->src_node_next != NULL)
32177c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
32187c478bd9Sstevel@tonic-gate 				    DI_LINK(link)->src_node_next));
32197c478bd9Sstevel@tonic-gate 		}
32207c478bd9Sstevel@tonic-gate 	} else {
32217c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
32227c478bd9Sstevel@tonic-gate 			if (DI_NODE(node)->tgt_links != NULL)
32237c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
32247c478bd9Sstevel@tonic-gate 				    DI_NODE(node)->tgt_links));
32257c478bd9Sstevel@tonic-gate 		} else {
32267c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->tgt_node_next != NULL)
32277c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
32287c478bd9Sstevel@tonic-gate 				    DI_LINK(link)->tgt_node_next));
32297c478bd9Sstevel@tonic-gate 		}
32307c478bd9Sstevel@tonic-gate 	}
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 	if (DINFOLYR & DI_ALL(di_all)->command)
32337c478bd9Sstevel@tonic-gate 		errno = ENXIO;
32347c478bd9Sstevel@tonic-gate 	else
32357c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	return (DI_LINK_NIL);
32387c478bd9Sstevel@tonic-gate }
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate di_link_t
32417c478bd9Sstevel@tonic-gate di_link_next_by_lnode(di_lnode_t lnode, di_link_t link, uint_t endpoint)
32427c478bd9Sstevel@tonic-gate {
32437c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	/*
32467c478bd9Sstevel@tonic-gate 	 * paranoid error checking
32477c478bd9Sstevel@tonic-gate 	 */
32487c478bd9Sstevel@tonic-gate 	if ((lnode == DI_LNODE_NIL) ||
32497c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
32507c478bd9Sstevel@tonic-gate 		errno = EINVAL;
32517c478bd9Sstevel@tonic-gate 		return (DI_LINK_NIL);
32527c478bd9Sstevel@tonic-gate 	}
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)lnode - DI_LNODE(lnode)->self);
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC) {
32577c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
32587c478bd9Sstevel@tonic-gate 			if (DI_LNODE(lnode)->link_out == NULL)
32597c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
32607c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
3261*602ca9eaScth 			    DI_LNODE(lnode)->link_out));
32627c478bd9Sstevel@tonic-gate 		} else {
32637c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->src_link_next == NULL)
32647c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
32657c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
3266*602ca9eaScth 			    DI_LINK(link)->src_link_next));
32677c478bd9Sstevel@tonic-gate 		}
32687c478bd9Sstevel@tonic-gate 	} else {
32697c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
32707c478bd9Sstevel@tonic-gate 			if (DI_LNODE(lnode)->link_in == NULL)
32717c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
32727c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
3273*602ca9eaScth 			    DI_LNODE(lnode)->link_in));
32747c478bd9Sstevel@tonic-gate 		} else {
32757c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->tgt_link_next == NULL)
32767c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
32777c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
3278*602ca9eaScth 			    DI_LINK(link)->tgt_link_next));
32797c478bd9Sstevel@tonic-gate 		}
32807c478bd9Sstevel@tonic-gate 	}
32817c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
32827c478bd9Sstevel@tonic-gate }
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate /*
32857c478bd9Sstevel@tonic-gate  * Internal library function:
32867c478bd9Sstevel@tonic-gate  *   Invoke callback for each link data on the link list of first node
32877c478bd9Sstevel@tonic-gate  *   on node_list headp, and place children of first node on the list.
32887c478bd9Sstevel@tonic-gate  *
32897c478bd9Sstevel@tonic-gate  *   This is similar to walk_one_node, except we only walk in child
32907c478bd9Sstevel@tonic-gate  *   first mode.
32917c478bd9Sstevel@tonic-gate  */
32927c478bd9Sstevel@tonic-gate static void
32937c478bd9Sstevel@tonic-gate walk_one_link(struct node_list **headp, uint_t ep,
32947c478bd9Sstevel@tonic-gate     void *arg, int (*callback)(di_link_t link, void *arg))
32957c478bd9Sstevel@tonic-gate {
32967c478bd9Sstevel@tonic-gate 	int		action = DI_WALK_CONTINUE;
32977c478bd9Sstevel@tonic-gate 	di_link_t	link = DI_LINK_NIL;
32987c478bd9Sstevel@tonic-gate 	di_node_t	node = (*headp)->node;
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	while ((link = di_link_next_by_node(node, link, ep)) != DI_LINK_NIL) {
33017c478bd9Sstevel@tonic-gate 		action = callback(link, arg);
33027c478bd9Sstevel@tonic-gate 		if (action == DI_WALK_TERMINATE) {
33037c478bd9Sstevel@tonic-gate 			break;
33047c478bd9Sstevel@tonic-gate 		}
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	update_node_list(action, DI_WALK_LINKGEN, headp);
33087c478bd9Sstevel@tonic-gate }
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate int
33117c478bd9Sstevel@tonic-gate di_walk_link(di_node_t root, uint_t flag, uint_t endpoint, void *arg,
33127c478bd9Sstevel@tonic-gate     int (*link_callback)(di_link_t link, void *arg))
33137c478bd9Sstevel@tonic-gate {
33147c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate #ifdef DEBUG
3317*602ca9eaScth 	char *devfspath = di_devfs_path(root);
33187c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "walking %s link data under %s\n",
3319*602ca9eaScth 	    (endpoint == DI_LINK_SRC) ? "src" : "tgt", devfspath));
3320*602ca9eaScth 	di_devfs_path_free(devfspath);
33217c478bd9Sstevel@tonic-gate #endif
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	/*
33247c478bd9Sstevel@tonic-gate 	 * paranoid error checking
33257c478bd9Sstevel@tonic-gate 	 */
33267c478bd9Sstevel@tonic-gate 	if ((root == DI_NODE_NIL) || (link_callback == NULL) || (flag != 0) ||
33277c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
33287c478bd9Sstevel@tonic-gate 		errno = EINVAL;
33297c478bd9Sstevel@tonic-gate 		return (-1);
33307c478bd9Sstevel@tonic-gate 	}
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
33337c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
33347c478bd9Sstevel@tonic-gate 		return (-1);
33357c478bd9Sstevel@tonic-gate 	}
33367c478bd9Sstevel@tonic-gate 
33377c478bd9Sstevel@tonic-gate 	head->next = NULL;
33387c478bd9Sstevel@tonic-gate 	head->node = root;
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start link data walking from node %s\n",
3341*602ca9eaScth 	    di_node_name(root)));
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	while (head != NULL)
33447c478bd9Sstevel@tonic-gate 		walk_one_link(&head, endpoint, arg, link_callback);
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 	return (0);
33477c478bd9Sstevel@tonic-gate }
33487c478bd9Sstevel@tonic-gate 
33497c478bd9Sstevel@tonic-gate /*
33507c478bd9Sstevel@tonic-gate  * Internal library function:
33517c478bd9Sstevel@tonic-gate  *   Invoke callback for each link data on the link list of first node
33527c478bd9Sstevel@tonic-gate  *   on node_list headp, and place children of first node on the list.
33537c478bd9Sstevel@tonic-gate  *
33547c478bd9Sstevel@tonic-gate  *   This is similar to walk_one_node, except we only walk in child
33557c478bd9Sstevel@tonic-gate  *   first mode.
33567c478bd9Sstevel@tonic-gate  */
33577c478bd9Sstevel@tonic-gate static void
33587c478bd9Sstevel@tonic-gate walk_one_lnode(struct node_list **headp, void *arg,
33597c478bd9Sstevel@tonic-gate     int (*callback)(di_lnode_t lnode, void *arg))
33607c478bd9Sstevel@tonic-gate {
33617c478bd9Sstevel@tonic-gate 	int		action = DI_WALK_CONTINUE;
33627c478bd9Sstevel@tonic-gate 	di_lnode_t	lnode = DI_LNODE_NIL;
33637c478bd9Sstevel@tonic-gate 	di_node_t	node = (*headp)->node;
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 	while ((lnode = di_lnode_next(node, lnode)) != DI_LNODE_NIL) {
33667c478bd9Sstevel@tonic-gate 		action = callback(lnode, arg);
33677c478bd9Sstevel@tonic-gate 		if (action == DI_WALK_TERMINATE) {
33687c478bd9Sstevel@tonic-gate 			break;
33697c478bd9Sstevel@tonic-gate 		}
33707c478bd9Sstevel@tonic-gate 	}
33717c478bd9Sstevel@tonic-gate 
33727c478bd9Sstevel@tonic-gate 	update_node_list(action, DI_WALK_LINKGEN, headp);
33737c478bd9Sstevel@tonic-gate }
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate int
33767c478bd9Sstevel@tonic-gate di_walk_lnode(di_node_t root, uint_t flag, void *arg,
33777c478bd9Sstevel@tonic-gate     int (*lnode_callback)(di_lnode_t lnode, void *arg))
33787c478bd9Sstevel@tonic-gate {
33797c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate #ifdef DEBUG
3382*602ca9eaScth 	char *devfspath = di_devfs_path(root);
3383*602ca9eaScth 	DPRINTF((DI_INFO, "walking lnode data under %s\n", devfspath));
3384*602ca9eaScth 	di_devfs_path_free(devfspath);
33857c478bd9Sstevel@tonic-gate #endif
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate 	/*
33887c478bd9Sstevel@tonic-gate 	 * paranoid error checking
33897c478bd9Sstevel@tonic-gate 	 */
33907c478bd9Sstevel@tonic-gate 	if ((root == DI_NODE_NIL) || (lnode_callback == NULL) || (flag != 0)) {
33917c478bd9Sstevel@tonic-gate 		errno = EINVAL;
33927c478bd9Sstevel@tonic-gate 		return (-1);
33937c478bd9Sstevel@tonic-gate 	}
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
33967c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
33977c478bd9Sstevel@tonic-gate 		return (-1);
33987c478bd9Sstevel@tonic-gate 	}
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	head->next = NULL;
34017c478bd9Sstevel@tonic-gate 	head->node = root;
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start lnode data walking from node %s\n",
3404*602ca9eaScth 	    di_node_name(root)));
34057c478bd9Sstevel@tonic-gate 
34067c478bd9Sstevel@tonic-gate 	while (head != NULL)
34077c478bd9Sstevel@tonic-gate 		walk_one_lnode(&head, arg, lnode_callback);
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	return (0);
34107c478bd9Sstevel@tonic-gate }
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate di_node_t
3413*602ca9eaScth di_lookup_node(di_node_t root, char *devfspath)
34147c478bd9Sstevel@tonic-gate {
34157c478bd9Sstevel@tonic-gate 	struct di_all *dap;
34167c478bd9Sstevel@tonic-gate 	di_node_t node;
3417*602ca9eaScth 	char *copy, *slash, *pname, *paddr;
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate 	/*
34207c478bd9Sstevel@tonic-gate 	 * Path must be absolute and musn't have duplicate slashes
34217c478bd9Sstevel@tonic-gate 	 */
3422*602ca9eaScth 	if (*devfspath != '/' || strstr(devfspath, "//")) {
3423*602ca9eaScth 		DPRINTF((DI_ERR, "Invalid path: %s\n", devfspath));
34247c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
34257c478bd9Sstevel@tonic-gate 	}
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
34287c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "root node is DI_NODE_NIL\n"));
34297c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
34307c478bd9Sstevel@tonic-gate 	}
34317c478bd9Sstevel@tonic-gate 
34327c478bd9Sstevel@tonic-gate 	dap = DI_ALL((caddr_t)root - DI_NODE(root)->self);
34337c478bd9Sstevel@tonic-gate 	if (strcmp(dap->root_path, "/") != 0) {
34347c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "snapshot root not / : %s\n", dap->root_path));
34357c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 
3438*602ca9eaScth 	if ((copy = strdup(devfspath)) == NULL) {
3439*602ca9eaScth 		DPRINTF((DI_ERR, "strdup failed on: %s\n", devfspath));
34407c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
34417c478bd9Sstevel@tonic-gate 	}
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 	for (slash = copy, node = root; slash; ) {
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 		/*
3446*602ca9eaScth 		 * Handle devfspath = "/" case as well as trailing '/'
34477c478bd9Sstevel@tonic-gate 		 */
34487c478bd9Sstevel@tonic-gate 		if (*(slash + 1) == '\0')
34497c478bd9Sstevel@tonic-gate 			break;
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 		/*
34527c478bd9Sstevel@tonic-gate 		 * More path-components exist. Deal with the next one
34537c478bd9Sstevel@tonic-gate 		 */
34547c478bd9Sstevel@tonic-gate 		pname = slash + 1;
34557c478bd9Sstevel@tonic-gate 		node = di_child_node(node);
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 		if (slash = strchr(pname, '/'))
34587c478bd9Sstevel@tonic-gate 			*slash = '\0';
34597c478bd9Sstevel@tonic-gate 		if (paddr = strchr(pname, '@'))
34607c478bd9Sstevel@tonic-gate 			*paddr++ = '\0';
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 		for (; node != DI_NODE_NIL; node = di_sibling_node(node)) {
34637c478bd9Sstevel@tonic-gate 			char *name, *baddr;
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 			name = di_node_name(node);
34667c478bd9Sstevel@tonic-gate 			baddr = di_bus_addr(node);
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 			if (strcmp(pname, name) != 0)
34697c478bd9Sstevel@tonic-gate 				continue;
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 			/*
34727c478bd9Sstevel@tonic-gate 			 * Mappings between a "path-address" and bus-addr
34737c478bd9Sstevel@tonic-gate 			 *
34747c478bd9Sstevel@tonic-gate 			 *	paddr		baddr
34757c478bd9Sstevel@tonic-gate 			 *	---------------------
34767c478bd9Sstevel@tonic-gate 			 *	NULL		NULL
34777c478bd9Sstevel@tonic-gate 			 *	NULL		""
34787c478bd9Sstevel@tonic-gate 			 *	""		N/A	(invalid paddr)
34797c478bd9Sstevel@tonic-gate 			 */
34807c478bd9Sstevel@tonic-gate 			if (paddr && baddr && strcmp(paddr, baddr) == 0)
34817c478bd9Sstevel@tonic-gate 				break;
34827c478bd9Sstevel@tonic-gate 			if (paddr == NULL && (baddr == NULL || *baddr == '\0'))
34837c478bd9Sstevel@tonic-gate 				break;
34847c478bd9Sstevel@tonic-gate 		}
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate 		/*
34877c478bd9Sstevel@tonic-gate 		 * No nodes in the sibling list or there was no match
34887c478bd9Sstevel@tonic-gate 		 */
34897c478bd9Sstevel@tonic-gate 		if (node == DI_NODE_NIL) {
34907c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "%s@%s: no node\n", pname, paddr));
3491*602ca9eaScth 			free(copy);
34927c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
34937c478bd9Sstevel@tonic-gate 		}
34947c478bd9Sstevel@tonic-gate 	}
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 	assert(node != DI_NODE_NIL);
3497*602ca9eaScth 	free(copy);
34987c478bd9Sstevel@tonic-gate 	return (node);
34997c478bd9Sstevel@tonic-gate }
35007c478bd9Sstevel@tonic-gate 
3501*602ca9eaScth di_path_t
3502*602ca9eaScth di_lookup_path(di_node_t root, char *devfspath)
3503*602ca9eaScth {
3504*602ca9eaScth 	di_node_t	phci_node;
3505*602ca9eaScth 	di_path_t	path = DI_PATH_NIL;
3506*602ca9eaScth 	char		*copy, *lastslash;
3507*602ca9eaScth 	char		*pname, *paddr;
3508*602ca9eaScth 	char		*path_name, *path_addr;
3509*602ca9eaScth 
3510*602ca9eaScth 	if ((copy = strdup(devfspath)) == NULL) {
3511*602ca9eaScth 		DPRINTF((DI_ERR, "strdup failed on: %s\n", devfspath));
3512*602ca9eaScth 		return (DI_NODE_NIL);
3513*602ca9eaScth 	}
3514*602ca9eaScth 
3515*602ca9eaScth 	if ((lastslash = strrchr(copy, '/')) == NULL) {
3516*602ca9eaScth 		DPRINTF((DI_ERR, "failed to find component: %s\n", devfspath));
3517*602ca9eaScth 		goto out;
3518*602ca9eaScth 	}
3519*602ca9eaScth 
3520*602ca9eaScth 	/* stop at pHCI and find the node for the phci */
3521*602ca9eaScth 	*lastslash = '\0';
3522*602ca9eaScth 	phci_node = di_lookup_node(root, copy);
3523*602ca9eaScth 	if (phci_node == NULL) {
3524*602ca9eaScth 		DPRINTF((DI_ERR, "failed to find component: %s\n", devfspath));
3525*602ca9eaScth 		goto out;
3526*602ca9eaScth 	}
3527*602ca9eaScth 
3528*602ca9eaScth 	/* set up pname and paddr for last component */
3529*602ca9eaScth 	pname = lastslash + 1;
3530*602ca9eaScth 	if ((paddr = strchr(pname, '@')) == NULL) {
3531*602ca9eaScth 		DPRINTF((DI_ERR, "failed to find unit-addr: %s\n", devfspath));
3532*602ca9eaScth 		goto out;
3533*602ca9eaScth 	}
3534*602ca9eaScth 	*paddr++ = '\0';
3535*602ca9eaScth 
3536*602ca9eaScth 	/* walk paths below phci looking for match */
3537*602ca9eaScth 	for (path = di_path_phci_next_path(phci_node, DI_PATH_NIL);
3538*602ca9eaScth 	    path != DI_PATH_NIL;
3539*602ca9eaScth 	    path = di_path_phci_next_path(phci_node, path)) {
3540*602ca9eaScth 
3541*602ca9eaScth 		/* get name@addr of path */
3542*602ca9eaScth 		path_name = di_path_node_name(path);
3543*602ca9eaScth 		path_addr = di_path_bus_addr(path);
3544*602ca9eaScth 		if ((path_name == NULL) || (path_addr == NULL))
3545*602ca9eaScth 			continue;
3546*602ca9eaScth 
3547*602ca9eaScth 		/* break on match */
3548*602ca9eaScth 		if ((strcmp(pname, path_name) == 0) &&
3549*602ca9eaScth 		    (strcmp(paddr, path_addr) == 0))
3550*602ca9eaScth 			break;
3551*602ca9eaScth 	}
3552*602ca9eaScth 
3553*602ca9eaScth out:	free(copy);
3554*602ca9eaScth 	return (path);
3555*602ca9eaScth }
3556*602ca9eaScth 
35577c478bd9Sstevel@tonic-gate static char *
35587c478bd9Sstevel@tonic-gate msglevel2str(di_debug_t msglevel)
35597c478bd9Sstevel@tonic-gate {
35607c478bd9Sstevel@tonic-gate 	switch (msglevel) {
35617c478bd9Sstevel@tonic-gate 		case DI_ERR:
35627c478bd9Sstevel@tonic-gate 			return ("ERROR");
35637c478bd9Sstevel@tonic-gate 		case DI_INFO:
35647c478bd9Sstevel@tonic-gate 			return ("Info");
35657c478bd9Sstevel@tonic-gate 		case DI_TRACE:
35667c478bd9Sstevel@tonic-gate 			return ("Trace");
35677c478bd9Sstevel@tonic-gate 		case DI_TRACE1:
35687c478bd9Sstevel@tonic-gate 			return ("Trace1");
35697c478bd9Sstevel@tonic-gate 		case DI_TRACE2:
35707c478bd9Sstevel@tonic-gate 			return ("Trace2");
35717c478bd9Sstevel@tonic-gate 		default:
35727c478bd9Sstevel@tonic-gate 			return ("UNKNOWN");
35737c478bd9Sstevel@tonic-gate 	}
35747c478bd9Sstevel@tonic-gate }
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate void
35777c478bd9Sstevel@tonic-gate dprint(di_debug_t msglevel, const char *fmt, ...)
35787c478bd9Sstevel@tonic-gate {
35797c478bd9Sstevel@tonic-gate 	va_list	ap;
35807c478bd9Sstevel@tonic-gate 	char	*estr;
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	if (di_debug <= DI_QUIET)
35837c478bd9Sstevel@tonic-gate 		return;
35847c478bd9Sstevel@tonic-gate 
35857c478bd9Sstevel@tonic-gate 	if (di_debug < msglevel)
35867c478bd9Sstevel@tonic-gate 		return;
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 	estr = msglevel2str(msglevel);
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	assert(estr);
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "libdevinfo[%lu]: %s: ",
35957c478bd9Sstevel@tonic-gate 	    (ulong_t)getpid(), estr);
35967c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate 	va_end(ap);
35997c478bd9Sstevel@tonic-gate }
36007c478bd9Sstevel@tonic-gate 
36017c478bd9Sstevel@tonic-gate /* end of devinfo.c */
3602