xref: /illumos-gate/usr/src/lib/libdevinfo/devinfo.c (revision 8c4f8890c870d3bd16cbcaeed2dc4679d5e076b5)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
228309866bSanish 
237c478bd9Sstevel@tonic-gate /*
248309866bSanish  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Interfaces for getting device configuration data from kernel
327c478bd9Sstevel@tonic-gate  * through the devinfo driver.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <strings.h>
397c478bd9Sstevel@tonic-gate #include <stropts.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <poll.h>
427c478bd9Sstevel@tonic-gate #include <synch.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
457c478bd9Sstevel@tonic-gate #include <sys/obpdefs.h>
467c478bd9Sstevel@tonic-gate #include <sys/stat.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/time.h>
497c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
507c478bd9Sstevel@tonic-gate #include <stdarg.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	NDEBUG 1
537c478bd9Sstevel@tonic-gate #include <assert.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include "libdevinfo.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Debug message levels
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate typedef enum {
617c478bd9Sstevel@tonic-gate 	DI_QUIET = 0,	/* No debug messages - the default */
627c478bd9Sstevel@tonic-gate 	DI_ERR = 1,
637c478bd9Sstevel@tonic-gate 	DI_INFO,
647c478bd9Sstevel@tonic-gate 	DI_TRACE,
657c478bd9Sstevel@tonic-gate 	DI_TRACE1,
667c478bd9Sstevel@tonic-gate 	DI_TRACE2
677c478bd9Sstevel@tonic-gate } di_debug_t;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int di_debug = DI_QUIET;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	DPRINTF(args)	{ if (di_debug != DI_QUIET) dprint args; }
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate void dprint(di_debug_t msglevel, const char *fmt, ...);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #pragma init(_libdevinfo_init)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate void
797c478bd9Sstevel@tonic-gate _libdevinfo_init()
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	char	*debug_str = getenv("_LIBDEVINFO_DEBUG");
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (debug_str) {
847c478bd9Sstevel@tonic-gate 		errno = 0;
857c478bd9Sstevel@tonic-gate 		di_debug = atoi(debug_str);
867c478bd9Sstevel@tonic-gate 		if (errno || di_debug < DI_QUIET)
877c478bd9Sstevel@tonic-gate 			di_debug = DI_QUIET;
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate di_node_t
927c478bd9Sstevel@tonic-gate di_init(const char *phys_path, uint_t flag)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	return (di_init_impl(phys_path, flag, NULL));
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * We use blocking_open() to guarantee access to the devinfo device, if open()
997c478bd9Sstevel@tonic-gate  * is failing with EAGAIN.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static int
1027c478bd9Sstevel@tonic-gate blocking_open(const char *path, int oflag)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	int fd;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	while ((fd = open(path, oflag)) == -1 && errno == EAGAIN)
1077c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 1 * MILLISEC);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	return (fd);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /* private interface */
1137c478bd9Sstevel@tonic-gate di_node_t
1147c478bd9Sstevel@tonic-gate di_init_driver(const char *drv_name, uint_t flag)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	int fd;
1177c478bd9Sstevel@tonic-gate 	char driver[MAXPATHLEN];
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/*
1207c478bd9Sstevel@tonic-gate 	 * Don't allow drv_name to exceed MAXPATHLEN - 1, or 1023,
1217c478bd9Sstevel@tonic-gate 	 * which should be sufficient for any sensible programmer.
1227c478bd9Sstevel@tonic-gate 	 */
1237c478bd9Sstevel@tonic-gate 	if ((drv_name == NULL) || (strlen(drv_name) >= MAXPATHLEN)) {
1247c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1257c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	(void) strcpy(driver, drv_name);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * open the devinfo driver
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo",
1337c478bd9Sstevel@tonic-gate 	    O_RDONLY)) == -1) {
1347c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n", errno));
1357c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DINFOLODRV, driver) != 0) {
1397c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "failed to load driver %s\n", driver));
1407c478bd9Sstevel@tonic-gate 		(void) close(fd);
1417c478bd9Sstevel@tonic-gate 		errno = ENXIO;
1427c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	(void) close(fd);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Driver load succeeded, return a snapshot
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	return (di_init("/", flag));
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate di_node_t
1537c478bd9Sstevel@tonic-gate di_init_impl(const char *phys_path, uint_t flag,
1547c478bd9Sstevel@tonic-gate 	struct di_priv_data *priv)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	caddr_t pa;
1577c478bd9Sstevel@tonic-gate 	int fd, map_size;
1587c478bd9Sstevel@tonic-gate 	struct di_all *dap;
1597c478bd9Sstevel@tonic-gate 	struct dinfo_io dinfo_io;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	uint_t pageoffset = sysconf(_SC_PAGESIZE) - 1;
1627c478bd9Sstevel@tonic-gate 	uint_t pagemask = ~pageoffset;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_init: taking a snapshot\n"));
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * Make sure there is no minor name in the path
1687c478bd9Sstevel@tonic-gate 	 * and the path do not start with /devices....
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	if (strchr(phys_path, ':') ||
1717c478bd9Sstevel@tonic-gate 	    (strncmp(phys_path, "/devices", 8) == 0) ||
1727c478bd9Sstevel@tonic-gate 	    (strlen(phys_path) > MAXPATHLEN)) {
1737c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1747c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (strlen(phys_path) == 0)
1787c478bd9Sstevel@tonic-gate 		(void) sprintf(dinfo_io.root_path, "/");
1797c478bd9Sstevel@tonic-gate 	else if (*phys_path != '/')
1807c478bd9Sstevel@tonic-gate 		(void) snprintf(dinfo_io.root_path, sizeof (dinfo_io.root_path),
1817c478bd9Sstevel@tonic-gate 		    "/%s", phys_path);
1827c478bd9Sstevel@tonic-gate 	else
1837c478bd9Sstevel@tonic-gate 		(void) snprintf(dinfo_io.root_path, sizeof (dinfo_io.root_path),
1847c478bd9Sstevel@tonic-gate 		    "%s", phys_path);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * If private data is requested, copy the format specification
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	if (flag & DINFOPRIVDATA & 0xff) {
1907c478bd9Sstevel@tonic-gate 		if (priv)
1917c478bd9Sstevel@tonic-gate 			bcopy(priv, &dinfo_io.priv,
1927c478bd9Sstevel@tonic-gate 			    sizeof (struct di_priv_data));
1937c478bd9Sstevel@tonic-gate 		else {
1947c478bd9Sstevel@tonic-gate 			errno = EINVAL;
1957c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * Attempt to open the devinfo driver.  Make a second attempt at the
2017c478bd9Sstevel@tonic-gate 	 * read-only minor node if we don't have privileges to open the full
2027c478bd9Sstevel@tonic-gate 	 * version _and_ if we're not requesting operations that the read-only
2037c478bd9Sstevel@tonic-gate 	 * node can't perform.  (Setgid processes would fail an access() test,
2047c478bd9Sstevel@tonic-gate 	 * of course.)
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo",
2077c478bd9Sstevel@tonic-gate 	    O_RDONLY)) == -1) {
2087c478bd9Sstevel@tonic-gate 		if ((flag & DINFOFORCE) == DINFOFORCE ||
2097c478bd9Sstevel@tonic-gate 		    (flag & DINFOPRIVDATA) == DINFOPRIVDATA) {
2107c478bd9Sstevel@tonic-gate 			/*
2117c478bd9Sstevel@tonic-gate 			 * We wanted to perform a privileged operation, but the
2127c478bd9Sstevel@tonic-gate 			 * privileged node isn't available.  Don't modify errno
2137c478bd9Sstevel@tonic-gate 			 * on our way out (but display it if we're running with
2147c478bd9Sstevel@tonic-gate 			 * di_debug set).
2157c478bd9Sstevel@tonic-gate 			 */
2167c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n",
2177c478bd9Sstevel@tonic-gate 			    errno));
2187c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo,ro",
2227c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == -1) {
2237c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n",
2247c478bd9Sstevel@tonic-gate 			    errno));
2257c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Verify that there is no major conflict, i.e., we are indeed opening
2317c478bd9Sstevel@tonic-gate 	 * the devinfo driver.
2327c478bd9Sstevel@tonic-gate 	 */
2337c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DINFOIDENT, NULL) != DI_MAGIC) {
2347c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR,
2357c478bd9Sstevel@tonic-gate 		    "driver ID failed; check for major conflict\n"));
2367c478bd9Sstevel@tonic-gate 		(void) close(fd);
2377c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * create snapshot
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	if ((map_size = ioctl(fd, flag, &dinfo_io)) < 0) {
2447c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "devinfo ioctl failed with "
2457c478bd9Sstevel@tonic-gate 		    "error: %d\n", errno));
2467c478bd9Sstevel@tonic-gate 		(void) close(fd);
2477c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2487c478bd9Sstevel@tonic-gate 	} else if (map_size == 0) {
2497c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "%s not found\n", phys_path));
2507c478bd9Sstevel@tonic-gate 		errno = ENXIO;
2517c478bd9Sstevel@tonic-gate 		(void) close(fd);
2527c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * copy snapshot to userland
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	map_size = (map_size + pageoffset) & pagemask;
2597c478bd9Sstevel@tonic-gate 	if ((pa = valloc(map_size)) == NULL) {
2607c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "valloc failed for snapshot\n"));
2617c478bd9Sstevel@tonic-gate 		(void) close(fd);
2627c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DINFOUSRLD, pa) != map_size) {
2667c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "failed to copy snapshot to usrld\n"));
2677c478bd9Sstevel@tonic-gate 		(void) close(fd);
2687c478bd9Sstevel@tonic-gate 		free(pa);
2697c478bd9Sstevel@tonic-gate 		errno = EFAULT;
2707c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	(void) close(fd);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	dap = DI_ALL(pa);
2767c478bd9Sstevel@tonic-gate 	if (dap->top_devinfo == 0) {	/* phys_path not found */
2777c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "%s not found\n", phys_path));
2787c478bd9Sstevel@tonic-gate 		free(pa);
2797c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2807c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	return (DI_NODE(pa + dap->top_devinfo));
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate void
2877c478bd9Sstevel@tonic-gate di_fini(di_node_t root)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_fini: freeing a snapshot\n"));
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * paranoid checking
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
2977c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "di_fini called with NIL arg\n"));
2987c478bd9Sstevel@tonic-gate 		return;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	/*
3027c478bd9Sstevel@tonic-gate 	 * The root contains its own offset--self.
3037c478bd9Sstevel@tonic-gate 	 * Subtracting it from root address, we get the starting addr.
3047c478bd9Sstevel@tonic-gate 	 * The map_size is stored at the beginning of snapshot.
3057c478bd9Sstevel@tonic-gate 	 * Once we have starting address and size, we can free().
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	pa = (caddr_t)root - DI_NODE(root)->self;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	free(pa);
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate di_node_t
3137c478bd9Sstevel@tonic-gate di_parent_node(di_node_t node)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3187c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3197c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get parent of node %s\n", di_node_name(node)));
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent) {
3277c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_NODE(node)->parent));
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
3327c478bd9Sstevel@tonic-gate 	 *   If parent doesn't exist and node is not the root,
3337c478bd9Sstevel@tonic-gate 	 *   set errno to ENOTSUP. Otherwise, set errno to ENXIO.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	if (strcmp(DI_ALL(pa)->root_path, "/") != 0)
3367c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
3377c478bd9Sstevel@tonic-gate 	else
3387c478bd9Sstevel@tonic-gate 		errno = ENXIO;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate di_node_t
3447c478bd9Sstevel@tonic-gate di_sibling_node(di_node_t node)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3497c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3507c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get sibling of node %s\n", di_node_name(node)));
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->sibling) {
3587c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_NODE(node)->sibling));
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
3637c478bd9Sstevel@tonic-gate 	 *   Sibling doesn't exist, figure out if ioctl command
3647c478bd9Sstevel@tonic-gate 	 *   has DINFOSUBTREE set. If it doesn't, set errno to
3657c478bd9Sstevel@tonic-gate 	 *   ENOTSUP.
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (!(DI_ALL(pa)->command & DINFOSUBTREE))
3687c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
3697c478bd9Sstevel@tonic-gate 	else
3707c478bd9Sstevel@tonic-gate 		errno = ENXIO;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate di_node_t
3767c478bd9Sstevel@tonic-gate di_child_node(di_node_t node)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get child of node %s\n", di_node_name(node)));
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
3837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3847c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->child) {
3907c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_NODE(node)->child));
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
3957c478bd9Sstevel@tonic-gate 	 *   Child doesn't exist, figure out if DINFOSUBTREE is set.
3967c478bd9Sstevel@tonic-gate 	 *   If it isn't, set errno to ENOTSUP.
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 	if (!(DI_ALL(pa)->command & DINFOSUBTREE))
3997c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
4007c478bd9Sstevel@tonic-gate 	else
4017c478bd9Sstevel@tonic-gate 		errno = ENXIO;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate di_node_t
4077c478bd9Sstevel@tonic-gate di_drv_first_node(const char *drv_name, di_node_t root)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	caddr_t		pa;		/* starting address of map */
4107c478bd9Sstevel@tonic-gate 	int		major, devcnt;
4117c478bd9Sstevel@tonic-gate 	struct di_devnm	*devnm;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Get first node of driver %s\n", drv_name));
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
4167c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4177c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * get major number of driver
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	pa = (caddr_t)root - DI_NODE(root)->self;
4247c478bd9Sstevel@tonic-gate 	devcnt = DI_ALL(pa)->devcnt;
4257c478bd9Sstevel@tonic-gate 	devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	for (major = 0; major < devcnt; major++)
4287c478bd9Sstevel@tonic-gate 		if (devnm[major].name && (strcmp(drv_name,
4297c478bd9Sstevel@tonic-gate 		    (char *)(pa + devnm[major].name)) == 0))
4307c478bd9Sstevel@tonic-gate 			break;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (major >= devcnt) {
4337c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4347c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (!(devnm[major].head)) {
4387c478bd9Sstevel@tonic-gate 		errno = ENXIO;
4397c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return (DI_NODE(pa + devnm[major].head));
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate di_node_t
4467c478bd9Sstevel@tonic-gate di_drv_next_node(di_node_t node)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	caddr_t		pa;		/* starting address of map */
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
4517c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4527c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "next node on per driver list:"
4567c478bd9Sstevel@tonic-gate 	    " current=%s, driver=%s\n",
4577c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_driver_name(node)));
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->next == (di_off_t)-1) {
4607c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
4617c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->next == NULL) {
4677c478bd9Sstevel@tonic-gate 		errno = ENXIO;
4687c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	return (DI_NODE(pa + DI_NODE(node)->next));
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * Internal library interfaces:
4767c478bd9Sstevel@tonic-gate  *   node_list etc. for node walking
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate struct node_list {
4797c478bd9Sstevel@tonic-gate 	struct node_list *next;
4807c478bd9Sstevel@tonic-gate 	di_node_t node;
4817c478bd9Sstevel@tonic-gate };
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate static void
4847c478bd9Sstevel@tonic-gate free_node_list(struct node_list **headp)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	struct node_list *tmp;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	while (*headp) {
4897c478bd9Sstevel@tonic-gate 		tmp = *headp;
4907c478bd9Sstevel@tonic-gate 		*headp = (*headp)->next;
4917c478bd9Sstevel@tonic-gate 		free(tmp);
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate static void
4967c478bd9Sstevel@tonic-gate append_node_list(struct node_list **headp, struct node_list *list)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	struct node_list *tmp;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (*headp == NULL) {
5017c478bd9Sstevel@tonic-gate 		*headp = list;
5027c478bd9Sstevel@tonic-gate 		return;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (list == NULL)	/* a minor optimization */
5067c478bd9Sstevel@tonic-gate 		return;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	tmp = *headp;
5097c478bd9Sstevel@tonic-gate 	while (tmp->next)
5107c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	tmp->next = list;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate static void
5167c478bd9Sstevel@tonic-gate prepend_node_list(struct node_list **headp, struct node_list *list)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	struct node_list *tmp;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	if (list == NULL)
5217c478bd9Sstevel@tonic-gate 		return;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	tmp = *headp;
5247c478bd9Sstevel@tonic-gate 	*headp = list;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	if (tmp == NULL)	/* a minor optimization */
5277c478bd9Sstevel@tonic-gate 		return;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	while (list->next)
5307c478bd9Sstevel@tonic-gate 		list = list->next;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	list->next = tmp;
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate  * returns 1 if node is a descendant of parent, 0 otherwise
5377c478bd9Sstevel@tonic-gate  */
5387c478bd9Sstevel@tonic-gate static int
5397c478bd9Sstevel@tonic-gate is_descendant(di_node_t node, di_node_t parent)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	/*
5427c478bd9Sstevel@tonic-gate 	 * DI_NODE_NIL is parent of root, so it is
5437c478bd9Sstevel@tonic-gate 	 * the parent of all nodes.
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 	if (parent == DI_NODE_NIL) {
5467c478bd9Sstevel@tonic-gate 		return (1);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	do {
5507c478bd9Sstevel@tonic-gate 		node = di_parent_node(node);
5517c478bd9Sstevel@tonic-gate 	} while ((node != DI_NODE_NIL) && (node != parent));
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	return (node != DI_NODE_NIL);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate /*
5577c478bd9Sstevel@tonic-gate  * Insert list before the first node which is NOT a descendent of parent.
5587c478bd9Sstevel@tonic-gate  * This is needed to reproduce the exact walking order of link generators.
5597c478bd9Sstevel@tonic-gate  */
5607c478bd9Sstevel@tonic-gate static void
5617c478bd9Sstevel@tonic-gate insert_node_list(struct node_list **headp, struct node_list *list,
5627c478bd9Sstevel@tonic-gate     di_node_t parent)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate 	struct node_list *tmp, *tmp1;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	if (list == NULL)
5677c478bd9Sstevel@tonic-gate 		return;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	tmp = *headp;
5707c478bd9Sstevel@tonic-gate 	if (tmp == NULL) {	/* a minor optimization */
5717c478bd9Sstevel@tonic-gate 		*headp = list;
5727c478bd9Sstevel@tonic-gate 		return;
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (!is_descendant(tmp->node, parent)) {
5767c478bd9Sstevel@tonic-gate 		prepend_node_list(headp, list);
5777c478bd9Sstevel@tonic-gate 		return;
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * Find first node which is not a descendant
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	while (tmp->next && is_descendant(tmp->next->node, parent)) {
5847c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	tmp1 = tmp->next;
5887c478bd9Sstevel@tonic-gate 	tmp->next = list;
5897c478bd9Sstevel@tonic-gate 	append_node_list(headp, tmp1);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate  *   Get a linked list of handles of all children
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate static struct node_list *
5967c478bd9Sstevel@tonic-gate get_children(di_node_t node)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	di_node_t child;
5997c478bd9Sstevel@tonic-gate 	struct node_list *result, *tmp;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE1, "Get children of node %s\n", di_node_name(node)));
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if ((child = di_child_node(node)) == DI_NODE_NIL) {
6047c478bd9Sstevel@tonic-gate 		return (NULL);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if ((result = malloc(sizeof (struct node_list))) == NULL) {
6087c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
6097c478bd9Sstevel@tonic-gate 		return (NULL);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	result->node = child;
6137c478bd9Sstevel@tonic-gate 	tmp = result;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	while ((child = di_sibling_node(tmp->node)) != DI_NODE_NIL) {
6167c478bd9Sstevel@tonic-gate 		if ((tmp->next = malloc(sizeof (struct node_list))) == NULL) {
6177c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "malloc of node_list failed\n"));
6187c478bd9Sstevel@tonic-gate 			free_node_list(&result);
6197c478bd9Sstevel@tonic-gate 			return (NULL);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
6227c478bd9Sstevel@tonic-gate 		tmp->node = child;
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	tmp->next = NULL;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	return (result);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * Internal library interface:
6327c478bd9Sstevel@tonic-gate  *   Delete all siblings of the first node from the node_list, along with
6337c478bd9Sstevel@tonic-gate  *   the first node itself.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate static void
6367c478bd9Sstevel@tonic-gate prune_sib(struct node_list **headp)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	di_node_t parent, curr_par, curr_gpar;
6397c478bd9Sstevel@tonic-gate 	struct node_list *curr, *prev;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * get handle to parent of first node
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	if ((parent = di_parent_node((*headp)->node)) == DI_NODE_NIL) {
6457c478bd9Sstevel@tonic-gate 		/*
6467c478bd9Sstevel@tonic-gate 		 * This must be the root of the snapshot, so can't
6477c478bd9Sstevel@tonic-gate 		 * have any siblings.
6487c478bd9Sstevel@tonic-gate 		 *
6497c478bd9Sstevel@tonic-gate 		 * XXX Put a check here just in case.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		if ((*headp)->next)
6527c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "Unexpected err in di_walk_node.\n"));
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		free(*headp);
6557c478bd9Sstevel@tonic-gate 		*headp = NULL;
6567c478bd9Sstevel@tonic-gate 		return;
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * To be complete, we should also delete the children
6617c478bd9Sstevel@tonic-gate 	 * of siblings that have already been visited.
6627c478bd9Sstevel@tonic-gate 	 * This happens for DI_WALK_SIBFIRST when the first node
6637c478bd9Sstevel@tonic-gate 	 * is NOT the first in the linked list of siblings.
6647c478bd9Sstevel@tonic-gate 	 *
6657c478bd9Sstevel@tonic-gate 	 * Hence, we compare parent with BOTH the parent and grandparent
6667c478bd9Sstevel@tonic-gate 	 * of nodes, and delete node is a match is found.
6677c478bd9Sstevel@tonic-gate 	 */
6687c478bd9Sstevel@tonic-gate 	prev = *headp;
6697c478bd9Sstevel@tonic-gate 	curr = prev->next;
6707c478bd9Sstevel@tonic-gate 	while (curr) {
6717c478bd9Sstevel@tonic-gate 		if (((curr_par = di_parent_node(curr->node)) != DI_NODE_NIL) &&
6727c478bd9Sstevel@tonic-gate 		    ((curr_par == parent) || ((curr_gpar =
6737c478bd9Sstevel@tonic-gate 		    di_parent_node(curr_par)) != DI_NODE_NIL) &&
6747c478bd9Sstevel@tonic-gate 		    (curr_gpar == parent))) {
6757c478bd9Sstevel@tonic-gate 			/*
6767c478bd9Sstevel@tonic-gate 			 * match parent/grandparent: delete curr
6777c478bd9Sstevel@tonic-gate 			 */
6787c478bd9Sstevel@tonic-gate 			prev->next = curr->next;
6797c478bd9Sstevel@tonic-gate 			free(curr);
6807c478bd9Sstevel@tonic-gate 			curr = prev->next;
6817c478bd9Sstevel@tonic-gate 		} else
6827c478bd9Sstevel@tonic-gate 			curr = curr->next;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * delete the first node
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	curr = *headp;
6897c478bd9Sstevel@tonic-gate 	*headp = curr->next;
6907c478bd9Sstevel@tonic-gate 	free(curr);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Internal library function:
6957c478bd9Sstevel@tonic-gate  *	Update node list based on action (return code from callback)
6967c478bd9Sstevel@tonic-gate  *	and flag specifying walking behavior.
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate static void
6997c478bd9Sstevel@tonic-gate update_node_list(int action, uint_t flag, struct node_list **headp)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	struct node_list *children, *tmp;
7027c478bd9Sstevel@tonic-gate 	di_node_t parent = di_parent_node((*headp)->node);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	switch (action) {
7057c478bd9Sstevel@tonic-gate 	case DI_WALK_TERMINATE:
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * free the node list and be done
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		children = NULL;
7107c478bd9Sstevel@tonic-gate 		free_node_list(headp);
7117c478bd9Sstevel@tonic-gate 		break;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	case DI_WALK_PRUNESIB:
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * Get list of children and prune siblings
7167c478bd9Sstevel@tonic-gate 		 */
7177c478bd9Sstevel@tonic-gate 		children = get_children((*headp)->node);
7187c478bd9Sstevel@tonic-gate 		prune_sib(headp);
7197c478bd9Sstevel@tonic-gate 		break;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	case DI_WALK_PRUNECHILD:
7227c478bd9Sstevel@tonic-gate 		/*
7237c478bd9Sstevel@tonic-gate 		 * Set children to NULL and pop first node
7247c478bd9Sstevel@tonic-gate 		 */
7257c478bd9Sstevel@tonic-gate 		children = NULL;
7267c478bd9Sstevel@tonic-gate 		tmp = *headp;
7277c478bd9Sstevel@tonic-gate 		*headp = tmp->next;
7287c478bd9Sstevel@tonic-gate 		free(tmp);
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	case DI_WALK_CONTINUE:
7327c478bd9Sstevel@tonic-gate 	default:
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * Get list of children and pop first node
7357c478bd9Sstevel@tonic-gate 		 */
7367c478bd9Sstevel@tonic-gate 		children = get_children((*headp)->node);
7377c478bd9Sstevel@tonic-gate 		tmp = *headp;
7387c478bd9Sstevel@tonic-gate 		*headp = tmp->next;
7397c478bd9Sstevel@tonic-gate 		free(tmp);
7407c478bd9Sstevel@tonic-gate 		break;
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/*
7447c478bd9Sstevel@tonic-gate 	 * insert the list of children
7457c478bd9Sstevel@tonic-gate 	 */
7467c478bd9Sstevel@tonic-gate 	switch (flag) {
7477c478bd9Sstevel@tonic-gate 	case DI_WALK_CLDFIRST:
7487c478bd9Sstevel@tonic-gate 		prepend_node_list(headp, children);
7497c478bd9Sstevel@tonic-gate 		break;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	case DI_WALK_SIBFIRST:
7527c478bd9Sstevel@tonic-gate 		append_node_list(headp, children);
7537c478bd9Sstevel@tonic-gate 		break;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	case DI_WALK_LINKGEN:
7567c478bd9Sstevel@tonic-gate 	default:
7577c478bd9Sstevel@tonic-gate 		insert_node_list(headp, children, parent);
7587c478bd9Sstevel@tonic-gate 		break;
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate  * Internal library function:
7647c478bd9Sstevel@tonic-gate  *   Invoke callback on one node and update the list of nodes to be walked
7657c478bd9Sstevel@tonic-gate  *   based on the flag and return code.
7667c478bd9Sstevel@tonic-gate  */
7677c478bd9Sstevel@tonic-gate static void
7687c478bd9Sstevel@tonic-gate walk_one_node(struct node_list **headp, uint_t flag, void *arg,
7697c478bd9Sstevel@tonic-gate 	int (*callback)(di_node_t, void *))
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Walking node %s\n", di_node_name((*headp)->node)));
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	update_node_list(callback((*headp)->node, arg),
7747c478bd9Sstevel@tonic-gate 	    flag & DI_WALK_MASK, headp);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate int
7787c478bd9Sstevel@tonic-gate di_walk_node(di_node_t root, uint_t flag, void *arg,
7797c478bd9Sstevel@tonic-gate 	int (*node_callback)(di_node_t, void *))
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	if (root == NULL) {
7847c478bd9Sstevel@tonic-gate 		errno = EINVAL;
7857c478bd9Sstevel@tonic-gate 		return (-1);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
7897c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
7907c478bd9Sstevel@tonic-gate 		return (-1);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	head->next = NULL;
7947c478bd9Sstevel@tonic-gate 	head->node = root;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start node walking from node %s\n",
7977c478bd9Sstevel@tonic-gate 	    di_node_name(root)));
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	while (head != NULL)
8007c478bd9Sstevel@tonic-gate 		walk_one_node(&head, flag, arg, node_callback);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	return (0);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * Internal library function:
8077c478bd9Sstevel@tonic-gate  *   Invoke callback for each minor on the minor list of first node
8087c478bd9Sstevel@tonic-gate  *   on node_list headp, and place children of first node on the list.
8097c478bd9Sstevel@tonic-gate  *
8107c478bd9Sstevel@tonic-gate  *   This is similar to walk_one_node, except we only walk in child
8117c478bd9Sstevel@tonic-gate  *   first mode.
8127c478bd9Sstevel@tonic-gate  */
8137c478bd9Sstevel@tonic-gate static void
8147c478bd9Sstevel@tonic-gate walk_one_minor_list(struct node_list **headp, const char *desired_type,
8157c478bd9Sstevel@tonic-gate 	uint_t flag, void *arg, int (*callback)(di_node_t, di_minor_t, void *))
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	int ddm_type;
8187c478bd9Sstevel@tonic-gate 	int action = DI_WALK_CONTINUE;
8197c478bd9Sstevel@tonic-gate 	char *node_type;
8207c478bd9Sstevel@tonic-gate 	di_minor_t minor = DI_MINOR_NIL;
8217c478bd9Sstevel@tonic-gate 	di_node_t node = (*headp)->node;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
8247c478bd9Sstevel@tonic-gate 		ddm_type = di_minor_type(minor);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		if ((ddm_type == DDM_ALIAS) && !(flag & DI_CHECK_ALIAS))
8277c478bd9Sstevel@tonic-gate 			continue;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		if ((ddm_type == DDM_INTERNAL_PATH) &&
8307c478bd9Sstevel@tonic-gate 		    !(flag & DI_CHECK_INTERNAL_PATH))
8317c478bd9Sstevel@tonic-gate 			continue;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		node_type = di_minor_nodetype(minor);
8347c478bd9Sstevel@tonic-gate 		if ((desired_type != NULL) && ((node_type == NULL) ||
8357c478bd9Sstevel@tonic-gate 		    strncmp(desired_type, node_type, strlen(desired_type))
8367c478bd9Sstevel@tonic-gate 		    != 0))
8377c478bd9Sstevel@tonic-gate 			continue;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		if ((action = callback(node, minor, arg)) ==
8407c478bd9Sstevel@tonic-gate 		    DI_WALK_TERMINATE) {
8417c478bd9Sstevel@tonic-gate 			break;
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	update_node_list(action, DI_WALK_LINKGEN, headp);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate int
8497c478bd9Sstevel@tonic-gate di_walk_minor(di_node_t root, const char *minor_type, uint_t flag, void *arg,
8507c478bd9Sstevel@tonic-gate 	int (*minor_callback)(di_node_t, di_minor_t, void *))
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate #ifdef DEBUG
8557c478bd9Sstevel@tonic-gate 	char *path = di_devfs_path(root);
8567c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "walking minor nodes under %s\n", path));
8577c478bd9Sstevel@tonic-gate 	di_devfs_path_free(path);
8587c478bd9Sstevel@tonic-gate #endif
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	if (root == NULL) {
8617c478bd9Sstevel@tonic-gate 		errno = EINVAL;
8627c478bd9Sstevel@tonic-gate 		return (-1);
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
8667c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
8677c478bd9Sstevel@tonic-gate 		return (-1);
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	head->next = NULL;
8717c478bd9Sstevel@tonic-gate 	head->node = root;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start minor walking from node %s\n",
8747c478bd9Sstevel@tonic-gate 		di_node_name(root)));
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	while (head != NULL)
8777c478bd9Sstevel@tonic-gate 		walk_one_minor_list(&head, minor_type, flag, arg,
8787c478bd9Sstevel@tonic-gate 		    minor_callback);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	return (0);
8817c478bd9Sstevel@tonic-gate }
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate /*
8847c478bd9Sstevel@tonic-gate  * generic node parameters
8857c478bd9Sstevel@tonic-gate  *   Calling these routines always succeeds.
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate char *
8887c478bd9Sstevel@tonic-gate di_node_name(di_node_t node)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	return ((caddr_t)node + DI_NODE(node)->node_name - DI_NODE(node)->self);
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /* returns NULL ptr or a valid ptr to non-NULL string */
8947c478bd9Sstevel@tonic-gate char *
8957c478bd9Sstevel@tonic-gate di_bus_addr(di_node_t node)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 	caddr_t pa = (caddr_t)node - DI_NODE(node)->self;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->address == 0)
9007c478bd9Sstevel@tonic-gate 		return (NULL);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	return ((char *)(pa + DI_NODE(node)->address));
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate char *
9067c478bd9Sstevel@tonic-gate di_binding_name(di_node_t node)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	caddr_t pa = (caddr_t)node - DI_NODE(node)->self;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->bind_name == 0)
9117c478bd9Sstevel@tonic-gate 		return (NULL);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	return ((char *)(pa + DI_NODE(node)->bind_name));
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate int
9177c478bd9Sstevel@tonic-gate di_compatible_names(di_node_t node, char **names)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	char *c;
9207c478bd9Sstevel@tonic-gate 	int len, size, entries = 0;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->compat_names == 0) {
9237c478bd9Sstevel@tonic-gate 		*names = NULL;
9247c478bd9Sstevel@tonic-gate 		return (0);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	*names = (caddr_t)node +
9287c478bd9Sstevel@tonic-gate 		DI_NODE(node)->compat_names - DI_NODE(node)->self;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	c = *names;
9317c478bd9Sstevel@tonic-gate 	len = DI_NODE(node)->compat_length;
9327c478bd9Sstevel@tonic-gate 	while (len > 0) {
9337c478bd9Sstevel@tonic-gate 		entries++;
9347c478bd9Sstevel@tonic-gate 		size = strlen(c) + 1;
9357c478bd9Sstevel@tonic-gate 		len -= size;
9367c478bd9Sstevel@tonic-gate 		c += size;
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	return (entries);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate int
9437c478bd9Sstevel@tonic-gate di_instance(di_node_t node)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	return (DI_NODE(node)->instance);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * XXX: emulate the return value of the old implementation
9507c478bd9Sstevel@tonic-gate  * using info from devi_node_class and devi_node_attributes.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate int
9537c478bd9Sstevel@tonic-gate di_nodeid(di_node_t node)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->node_class == DDI_NC_PROM)
9567c478bd9Sstevel@tonic-gate 		return (DI_PROM_NODEID);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->attributes & DDI_PERSISTENT)
9597c478bd9Sstevel@tonic-gate 		return (DI_SID_NODEID);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	return (DI_PSEUDO_NODEID);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate uint_t
9657c478bd9Sstevel@tonic-gate di_state(di_node_t node)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	uint_t result = 0;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (di_node_state(node) < DS_ATTACHED)
9707c478bd9Sstevel@tonic-gate 		result |= DI_DRIVER_DETACHED;
9717c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_DEVICE_OFFLINE)
9727c478bd9Sstevel@tonic-gate 		result |= DI_DEVICE_OFFLINE;
9737c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_DEVICE_DOWN)
9747c478bd9Sstevel@tonic-gate 		result |= DI_DEVICE_OFFLINE;
9757c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_BUS_QUIESCED)
9767c478bd9Sstevel@tonic-gate 		result |= DI_BUS_QUIESCED;
9777c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->state & DEVI_BUS_DOWN)
9787c478bd9Sstevel@tonic-gate 		result |= DI_BUS_DOWN;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	return (result);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate ddi_node_state_t
9847c478bd9Sstevel@tonic-gate di_node_state(di_node_t node)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	return (DI_NODE(node)->node_state);
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate ddi_devid_t
9907c478bd9Sstevel@tonic-gate di_devid(di_node_t node)
9917c478bd9Sstevel@tonic-gate {
9927c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->devid == 0)
9937c478bd9Sstevel@tonic-gate 		return (NULL);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	return ((ddi_devid_t)((caddr_t)node +
9967c478bd9Sstevel@tonic-gate 	    DI_NODE(node)->devid - DI_NODE(node)->self));
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate int
10007c478bd9Sstevel@tonic-gate di_driver_major(di_node_t node)
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	int major;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	major = DI_NODE(node)->drv_major;
10057c478bd9Sstevel@tonic-gate 	if (major < 0)
10067c478bd9Sstevel@tonic-gate 		return (-1);
10077c478bd9Sstevel@tonic-gate 	return (major);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate char *
10117c478bd9Sstevel@tonic-gate di_driver_name(di_node_t node)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	int major;
10147c478bd9Sstevel@tonic-gate 	caddr_t pa;
10157c478bd9Sstevel@tonic-gate 	struct di_devnm *devnm;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	major = DI_NODE(node)->drv_major;
10187c478bd9Sstevel@tonic-gate 	if (major < 0)
10197c478bd9Sstevel@tonic-gate 		return (NULL);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
10227c478bd9Sstevel@tonic-gate 	devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	if (devnm[major].name)
10257c478bd9Sstevel@tonic-gate 		return (pa + devnm[major].name);
10267c478bd9Sstevel@tonic-gate 	else
10277c478bd9Sstevel@tonic-gate 		return (NULL);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate uint_t
10317c478bd9Sstevel@tonic-gate di_driver_ops(di_node_t node)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	int major;
10347c478bd9Sstevel@tonic-gate 	caddr_t pa;
10357c478bd9Sstevel@tonic-gate 	struct di_devnm *devnm;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	major = DI_NODE(node)->drv_major;
10387c478bd9Sstevel@tonic-gate 	if (major < 0)
10397c478bd9Sstevel@tonic-gate 		return (0);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
10427c478bd9Sstevel@tonic-gate 	devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (devnm[major].ops);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * returns the length of the path, caller must free memory
10497c478bd9Sstevel@tonic-gate  */
10507c478bd9Sstevel@tonic-gate char *
10517c478bd9Sstevel@tonic-gate di_devfs_path(di_node_t node)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	caddr_t pa;
10547c478bd9Sstevel@tonic-gate 	di_node_t parent;
10557c478bd9Sstevel@tonic-gate 	int depth = 0, len = 0;
10567c478bd9Sstevel@tonic-gate 	char *buf, *name[MAX_TREE_DEPTH], *addr[MAX_TREE_DEPTH];
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
10597c478bd9Sstevel@tonic-gate 		errno = EINVAL;
10607c478bd9Sstevel@tonic-gate 		return (NULL);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/*
10647c478bd9Sstevel@tonic-gate 	 * trace back to root, note the node_name & address
10657c478bd9Sstevel@tonic-gate 	 */
10667c478bd9Sstevel@tonic-gate 	while ((parent = di_parent_node(node)) != DI_NODE_NIL) {
10677c478bd9Sstevel@tonic-gate 		name[depth] = di_node_name(node);
10687c478bd9Sstevel@tonic-gate 		len += strlen(name[depth]) + 1;		/* 1 for '/' */
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 		if ((addr[depth] = di_bus_addr(node)) != NULL)
10717c478bd9Sstevel@tonic-gate 			len += strlen(addr[depth]) + 1;	/* 1 for '@' */
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		node = parent;
10747c478bd9Sstevel@tonic-gate 		depth++;
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	/*
10787c478bd9Sstevel@tonic-gate 	 * get the path to the root of snapshot
10797c478bd9Sstevel@tonic-gate 	 */
10807c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
10817c478bd9Sstevel@tonic-gate 	name[depth] = DI_ALL(pa)->root_path;
10827c478bd9Sstevel@tonic-gate 	len += strlen(name[depth]) + 1;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * allocate buffer and assemble path
10867c478bd9Sstevel@tonic-gate 	 */
10877c478bd9Sstevel@tonic-gate 	if ((buf = malloc(len)) == NULL) {
10887c478bd9Sstevel@tonic-gate 		return (NULL);
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, name[depth]);
10927c478bd9Sstevel@tonic-gate 	len = strlen(buf);
10937c478bd9Sstevel@tonic-gate 	if (buf[len - 1] == '/')
10947c478bd9Sstevel@tonic-gate 		len--;	/* delete trailing '/' */
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	while (depth) {
10977c478bd9Sstevel@tonic-gate 		depth--;
10987c478bd9Sstevel@tonic-gate 		buf[len] = '/';
10997c478bd9Sstevel@tonic-gate 		(void) strcpy(buf + len + 1, name[depth]);
11007c478bd9Sstevel@tonic-gate 		len += strlen(name[depth]) + 1;
11017c478bd9Sstevel@tonic-gate 		if (addr[depth] && addr[depth][0] != '\0') {
11027c478bd9Sstevel@tonic-gate 			buf[len] = '@';
11037c478bd9Sstevel@tonic-gate 			(void) strcpy(buf + len + 1, addr[depth]);
11047c478bd9Sstevel@tonic-gate 			len += strlen(addr[depth]) + 1;
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	return (buf);
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate char *
11127c478bd9Sstevel@tonic-gate di_devfs_minor_path(di_minor_t minor)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	di_node_t	node;
11157c478bd9Sstevel@tonic-gate 	char		*full_path, *name, *path;
11167c478bd9Sstevel@tonic-gate 	int		full_path_len;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	if (minor == DI_MINOR_NIL) {
11197c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11207c478bd9Sstevel@tonic-gate 		return (NULL);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	name = di_minor_name(minor);
11247c478bd9Sstevel@tonic-gate 	node = di_minor_devinfo(minor);
11257c478bd9Sstevel@tonic-gate 	path = di_devfs_path(node);
11267c478bd9Sstevel@tonic-gate 	if (path == NULL)
11277c478bd9Sstevel@tonic-gate 		return (NULL);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/* make the full path to the device minor node */
11307c478bd9Sstevel@tonic-gate 	full_path_len = strlen(path) + strlen(name) + 2;
11317c478bd9Sstevel@tonic-gate 	full_path = (char *)calloc(1, full_path_len);
11327c478bd9Sstevel@tonic-gate 	if (full_path != NULL)
11337c478bd9Sstevel@tonic-gate 		(void) snprintf(full_path, full_path_len, "%s:%s", path, name);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	di_devfs_path_free(path);
11367c478bd9Sstevel@tonic-gate 	return (full_path);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate void
11407c478bd9Sstevel@tonic-gate di_devfs_path_free(char *buf)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
11437c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "di_devfs_path_free NULL arg!\n"));
11447c478bd9Sstevel@tonic-gate 		return;
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	free(buf);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate /* minor data access */
11517c478bd9Sstevel@tonic-gate di_minor_t
11527c478bd9Sstevel@tonic-gate di_minor_next(di_node_t node, di_minor_t minor)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	caddr_t pa;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/*
11577c478bd9Sstevel@tonic-gate 	 * paranoid error checking
11587c478bd9Sstevel@tonic-gate 	 */
11597c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
11607c478bd9Sstevel@tonic-gate 		errno = EINVAL;
11617c478bd9Sstevel@tonic-gate 		return (DI_MINOR_NIL);
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	/*
11657c478bd9Sstevel@tonic-gate 	 * minor is not NIL
11667c478bd9Sstevel@tonic-gate 	 */
11677c478bd9Sstevel@tonic-gate 	if (minor != DI_MINOR_NIL) {
11687c478bd9Sstevel@tonic-gate 		if (DI_MINOR(minor)->next != 0)
11697c478bd9Sstevel@tonic-gate 			return ((di_minor_t)((void *)((caddr_t)minor -
11707c478bd9Sstevel@tonic-gate 			    DI_MINOR(minor)->self + DI_MINOR(minor)->next)));
11717c478bd9Sstevel@tonic-gate 		else {
11727c478bd9Sstevel@tonic-gate 			errno = ENXIO;
11737c478bd9Sstevel@tonic-gate 			return (DI_MINOR_NIL);
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 	}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	/*
11787c478bd9Sstevel@tonic-gate 	 * minor is NIL-->caller asks for first minor node
11797c478bd9Sstevel@tonic-gate 	 */
11807c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->minor_data != 0) {
11817c478bd9Sstevel@tonic-gate 		return (DI_MINOR((caddr_t)node - DI_NODE(node)->self +
11827c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->minor_data));
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	/*
11867c478bd9Sstevel@tonic-gate 	 * no minor data-->check if snapshot includes minor data
11877c478bd9Sstevel@tonic-gate 	 *	in order to set the correct errno
11887c478bd9Sstevel@tonic-gate 	 */
11897c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
11907c478bd9Sstevel@tonic-gate 	if (DINFOMINOR & DI_ALL(pa)->command)
11917c478bd9Sstevel@tonic-gate 		errno = ENXIO;
11927c478bd9Sstevel@tonic-gate 	else
11937c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	return (DI_MINOR_NIL);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate /* private interface for dealing with alias minor link generation */
11997c478bd9Sstevel@tonic-gate di_node_t
12007c478bd9Sstevel@tonic-gate di_minor_devinfo(di_minor_t minor)
12017c478bd9Sstevel@tonic-gate {
12027c478bd9Sstevel@tonic-gate 	if (minor == DI_MINOR_NIL) {
12037c478bd9Sstevel@tonic-gate 		errno = EINVAL;
12047c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	return (DI_NODE((caddr_t)minor - DI_MINOR(minor)->self +
12087c478bd9Sstevel@tonic-gate 	    DI_MINOR(minor)->node));
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate ddi_minor_type
12127c478bd9Sstevel@tonic-gate di_minor_type(di_minor_t minor)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	return (DI_MINOR(minor)->type);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate char *
12187c478bd9Sstevel@tonic-gate di_minor_name(di_minor_t minor)
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 	if (DI_MINOR(minor)->name == 0)
12217c478bd9Sstevel@tonic-gate 		return (NULL);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	return ((caddr_t)minor - DI_MINOR(minor)->self + DI_MINOR(minor)->name);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate dev_t
12277c478bd9Sstevel@tonic-gate di_minor_devt(di_minor_t minor)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	return (makedev(DI_MINOR(minor)->dev_major,
12307c478bd9Sstevel@tonic-gate 		DI_MINOR(minor)->dev_minor));
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate int
12347c478bd9Sstevel@tonic-gate di_minor_spectype(di_minor_t minor)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	return (DI_MINOR(minor)->spec_type);
12377c478bd9Sstevel@tonic-gate }
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate char *
12407c478bd9Sstevel@tonic-gate di_minor_nodetype(di_minor_t minor)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	if (DI_MINOR(minor)->node_type == 0)
12437c478bd9Sstevel@tonic-gate 		return (NULL);
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	return ((caddr_t)minor -
12467c478bd9Sstevel@tonic-gate 		DI_MINOR(minor)->self + DI_MINOR(minor)->node_type);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate /*
12507c478bd9Sstevel@tonic-gate  * Single public interface for accessing software properties
12517c478bd9Sstevel@tonic-gate  */
12527c478bd9Sstevel@tonic-gate di_prop_t
12537c478bd9Sstevel@tonic-gate di_prop_next(di_node_t node, di_prop_t prop)
12547c478bd9Sstevel@tonic-gate {
12557c478bd9Sstevel@tonic-gate 	int list = DI_PROP_DRV_LIST;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	/*
12587c478bd9Sstevel@tonic-gate 	 * paranoid check
12597c478bd9Sstevel@tonic-gate 	 */
12607c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
12617c478bd9Sstevel@tonic-gate 		errno = EINVAL;
12627c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
12637c478bd9Sstevel@tonic-gate 	}
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/*
12667c478bd9Sstevel@tonic-gate 	 * Find which prop list we are at
12677c478bd9Sstevel@tonic-gate 	 */
12687c478bd9Sstevel@tonic-gate 	if (prop != DI_PROP_NIL)
12697c478bd9Sstevel@tonic-gate 		list = DI_PROP(prop)->prop_list;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	do {
12727c478bd9Sstevel@tonic-gate 		switch (list++) {
12737c478bd9Sstevel@tonic-gate 		case DI_PROP_DRV_LIST:
12747c478bd9Sstevel@tonic-gate 			prop = di_prop_drv_next(node, prop);
12757c478bd9Sstevel@tonic-gate 			break;
12767c478bd9Sstevel@tonic-gate 		case DI_PROP_SYS_LIST:
12777c478bd9Sstevel@tonic-gate 			prop = di_prop_sys_next(node, prop);
12787c478bd9Sstevel@tonic-gate 			break;
12797c478bd9Sstevel@tonic-gate 		case DI_PROP_GLB_LIST:
12807c478bd9Sstevel@tonic-gate 			prop = di_prop_global_next(node, prop);
12817c478bd9Sstevel@tonic-gate 			break;
12827c478bd9Sstevel@tonic-gate 		case DI_PROP_HW_LIST:
12837c478bd9Sstevel@tonic-gate 			prop = di_prop_hw_next(node, prop);
12847c478bd9Sstevel@tonic-gate 			break;
12857c478bd9Sstevel@tonic-gate 		default:	/* shouldn't happen */
12867c478bd9Sstevel@tonic-gate 			errno = EFAULT;
12877c478bd9Sstevel@tonic-gate 			return (DI_PROP_NIL);
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 	} while ((prop == DI_PROP_NIL) && (list <= DI_PROP_HW_LIST));
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	return (prop);
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate dev_t
12957c478bd9Sstevel@tonic-gate di_prop_devt(di_prop_t prop)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	return (makedev(DI_PROP(prop)->dev_major, DI_PROP(prop)->dev_minor));
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate char *
13017c478bd9Sstevel@tonic-gate di_prop_name(di_prop_t prop)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_name == 0)
13047c478bd9Sstevel@tonic-gate 		return (NULL);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	return ((caddr_t)prop - DI_PROP(prop)->self + DI_PROP(prop)->prop_name);
13077c478bd9Sstevel@tonic-gate }
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate int
13107c478bd9Sstevel@tonic-gate di_prop_type(di_prop_t prop)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	uint_t flags = DI_PROP(prop)->prop_flags;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_UNDEF_IT)
13157c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_UNDEF_IT);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
13187c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_BOOLEAN);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if ((flags & DDI_PROP_TYPE_MASK) == DDI_PROP_TYPE_ANY)
13217c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_UNKNOWN);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_INT)
13247c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_INT);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_INT64)
13277c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_INT64);
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_STRING)
13307c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_STRING);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	if (flags & DDI_PROP_TYPE_BYTE)
13337c478bd9Sstevel@tonic-gate 		return (DI_PROP_TYPE_BYTE);
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * Shouldn't get here. In case we do, return unknown type.
13377c478bd9Sstevel@tonic-gate 	 *
13387c478bd9Sstevel@tonic-gate 	 * XXX--When DDI_PROP_TYPE_COMPOSITE is implemented, we need
13397c478bd9Sstevel@tonic-gate 	 *	to add DI_PROP_TYPE_COMPOSITE.
13407c478bd9Sstevel@tonic-gate 	 */
13417c478bd9Sstevel@tonic-gate 	DPRINTF((DI_ERR, "Unimplemented property type: 0x%x\n", flags));
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	return (DI_PROP_TYPE_UNKNOWN);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /*
13477c478bd9Sstevel@tonic-gate  * Extract type-specific values of an property
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate extern int di_prop_decode_common(void *prop_data, int len,
13507c478bd9Sstevel@tonic-gate 	int ddi_type, int prom);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate int
13537c478bd9Sstevel@tonic-gate di_prop_ints(di_prop_t prop, int **prop_data)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
13567c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
13597c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
13607c478bd9Sstevel@tonic-gate 		errno = EFAULT;
13617c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
13627c478bd9Sstevel@tonic-gate 		return (-1);
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	*prop_data = (int *)((void *)((caddr_t)prop - DI_PROP(prop)->self
13667c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data));
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
13697c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_INT, 0));
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate int
13737c478bd9Sstevel@tonic-gate di_prop_int64(di_prop_t prop, int64_t **prop_data)
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
13767c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
13797c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
13807c478bd9Sstevel@tonic-gate 		errno = EFAULT;
13817c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
13827c478bd9Sstevel@tonic-gate 		return (-1);
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	*prop_data = (int64_t *)((void *)((caddr_t)prop - DI_PROP(prop)->self
13867c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data));
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
13897c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_INT64, 0));
13907c478bd9Sstevel@tonic-gate }
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate int
13937c478bd9Sstevel@tonic-gate di_prop_strings(di_prop_t prop, char **prop_data)
13947c478bd9Sstevel@tonic-gate {
13957c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
13967c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
13997c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
14007c478bd9Sstevel@tonic-gate 		errno = EFAULT;
14017c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
14027c478bd9Sstevel@tonic-gate 		return (-1);
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	*prop_data = (char *)((caddr_t)prop - DI_PROP(prop)->self
14067c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
14097c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_STRING, 0));
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate int
14137c478bd9Sstevel@tonic-gate di_prop_bytes(di_prop_t prop, uchar_t **prop_data)
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0)
14167c478bd9Sstevel@tonic-gate 		return (0);	/* boolean property */
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
14197c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
14207c478bd9Sstevel@tonic-gate 		errno = EFAULT;
14217c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
14227c478bd9Sstevel@tonic-gate 		return (-1);
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	*prop_data = (uchar_t *)((caddr_t)prop - DI_PROP(prop)->self
14267c478bd9Sstevel@tonic-gate 	    + DI_PROP(prop)->prop_data);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
14297c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_len, DI_PROP_TYPE_BYTE, 0));
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate /*
14337c478bd9Sstevel@tonic-gate  * returns 1 for match, 0 for no match
14347c478bd9Sstevel@tonic-gate  */
14357c478bd9Sstevel@tonic-gate static int
14367c478bd9Sstevel@tonic-gate match_prop(di_prop_t prop, dev_t match_dev, const char *name, int type)
14377c478bd9Sstevel@tonic-gate {
14387c478bd9Sstevel@tonic-gate 	int prop_type;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate #ifdef DEBUG
14417c478bd9Sstevel@tonic-gate 	if (di_prop_name(prop) == NULL) {
14427c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "libdevinfo: property has no name!\n"));
14437c478bd9Sstevel@tonic-gate 		return (0);
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate #endif /* DEBUG */
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	if (strcmp(name, di_prop_name(prop)) != 0)
14487c478bd9Sstevel@tonic-gate 		return (0);
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	if ((match_dev != DDI_DEV_T_ANY) && (di_prop_devt(prop) != match_dev))
14517c478bd9Sstevel@tonic-gate 		return (0);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/*
14547c478bd9Sstevel@tonic-gate 	 * XXX prop_type is different from DDI_*. See PSARC 1997/127.
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	prop_type = di_prop_type(prop);
14577c478bd9Sstevel@tonic-gate 	if ((prop_type != DI_PROP_TYPE_UNKNOWN) && (prop_type != type) &&
14587c478bd9Sstevel@tonic-gate 	    (prop_type != DI_PROP_TYPE_BOOLEAN))
14597c478bd9Sstevel@tonic-gate 		return (0);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	return (1);
14627c478bd9Sstevel@tonic-gate }
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate static di_prop_t
14657c478bd9Sstevel@tonic-gate di_prop_search(dev_t match_dev, di_node_t node, const char *name,
14667c478bd9Sstevel@tonic-gate     int type)
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	di_prop_t prop = DI_PROP_NIL;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	/*
14717c478bd9Sstevel@tonic-gate 	 * The check on match_dev follows ddi_prop_lookup_common().
14727c478bd9Sstevel@tonic-gate 	 * Other checks are libdevinfo specific implementation.
14737c478bd9Sstevel@tonic-gate 	 */
14747c478bd9Sstevel@tonic-gate 	if ((node == DI_NODE_NIL) || (name == NULL) || (strlen(name) == 0) ||
14757c478bd9Sstevel@tonic-gate 	    (match_dev == DDI_DEV_T_NONE) || !DI_PROP_TYPE_VALID(type)) {
14767c478bd9Sstevel@tonic-gate 		errno = EINVAL;
14777c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
14817c478bd9Sstevel@tonic-gate 		DPRINTF((DI_TRACE1, "match prop name %s, devt 0x%lx, type %d\n",
14827c478bd9Sstevel@tonic-gate 		    di_prop_name(prop), di_prop_devt(prop),
14837c478bd9Sstevel@tonic-gate 		    di_prop_type(prop)));
14847c478bd9Sstevel@tonic-gate 		if (match_prop(prop, match_dev, name, type))
14857c478bd9Sstevel@tonic-gate 			return (prop);
14867c478bd9Sstevel@tonic-gate 	}
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate int
14927c478bd9Sstevel@tonic-gate di_prop_lookup_ints(dev_t dev, di_node_t node, const char *prop_name,
14937c478bd9Sstevel@tonic-gate 	int **prop_data)
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	di_prop_t prop;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
14987c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT)) == DI_PROP_NIL)
14997c478bd9Sstevel@tonic-gate 		return (-1);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	return (di_prop_ints(prop, (void *)prop_data));
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate int
15057c478bd9Sstevel@tonic-gate di_prop_lookup_int64(dev_t dev, di_node_t node, const char *prop_name,
15067c478bd9Sstevel@tonic-gate 	int64_t **prop_data)
15077c478bd9Sstevel@tonic-gate {
15087c478bd9Sstevel@tonic-gate 	di_prop_t prop;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
15117c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT64)) == DI_PROP_NIL)
15127c478bd9Sstevel@tonic-gate 		return (-1);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	return (di_prop_int64(prop, (void *)prop_data));
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate int
15187c478bd9Sstevel@tonic-gate di_prop_lookup_strings(dev_t dev, di_node_t node, const char *prop_name,
15197c478bd9Sstevel@tonic-gate     char **prop_data)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	di_prop_t prop;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
15247c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_STRING)) == DI_PROP_NIL)
15257c478bd9Sstevel@tonic-gate 		return (-1);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	return (di_prop_strings(prop, (void *)prop_data));
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate int
15317c478bd9Sstevel@tonic-gate di_prop_lookup_bytes(dev_t dev, di_node_t node, const char *prop_name,
15327c478bd9Sstevel@tonic-gate 	uchar_t **prop_data)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	di_prop_t prop;
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 	if ((prop = di_prop_search(dev, node, prop_name,
15377c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_BYTE)) == DI_PROP_NIL)
15387c478bd9Sstevel@tonic-gate 		return (-1);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	return (di_prop_bytes(prop, (void *)prop_data));
15417c478bd9Sstevel@tonic-gate }
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate /*
15447c478bd9Sstevel@tonic-gate  * Consolidation private property access functions
15457c478bd9Sstevel@tonic-gate  */
15467c478bd9Sstevel@tonic-gate enum prop_type {
15477c478bd9Sstevel@tonic-gate 	PROP_TYPE_DRV,
15487c478bd9Sstevel@tonic-gate 	PROP_TYPE_SYS,
15497c478bd9Sstevel@tonic-gate 	PROP_TYPE_GLOB,
15507c478bd9Sstevel@tonic-gate 	PROP_TYPE_HW
15517c478bd9Sstevel@tonic-gate };
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate static di_prop_t
15547c478bd9Sstevel@tonic-gate di_prop_next_common(di_node_t node, di_prop_t prop, int prop_type)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	caddr_t pa;
15577c478bd9Sstevel@tonic-gate 	di_off_t prop_off = 0;
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	if (prop != DI_PROP_NIL) {
15607c478bd9Sstevel@tonic-gate 		if (DI_PROP(prop)->next) {
15617c478bd9Sstevel@tonic-gate 			return (DI_PROP((caddr_t)prop -
15627c478bd9Sstevel@tonic-gate 			    DI_PROP(prop)->self + DI_PROP(prop)->next));
15637c478bd9Sstevel@tonic-gate 		} else {
15647c478bd9Sstevel@tonic-gate 			return (DI_PROP_NIL);
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate 	}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	/*
15707c478bd9Sstevel@tonic-gate 	 * prop is NIL, caller asks for first property
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
15737c478bd9Sstevel@tonic-gate 	switch (prop_type) {
15747c478bd9Sstevel@tonic-gate 	case PROP_TYPE_DRV:
15757c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->drv_prop;
15767c478bd9Sstevel@tonic-gate 		break;
15777c478bd9Sstevel@tonic-gate 	case PROP_TYPE_SYS:
15787c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->sys_prop;
15797c478bd9Sstevel@tonic-gate 		break;
15807c478bd9Sstevel@tonic-gate 	case PROP_TYPE_HW:
15817c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->hw_prop;
15827c478bd9Sstevel@tonic-gate 		break;
15837c478bd9Sstevel@tonic-gate 	case PROP_TYPE_GLOB:
15847c478bd9Sstevel@tonic-gate 		prop_off = DI_NODE(node)->glob_prop;
15857c478bd9Sstevel@tonic-gate 		if (prop_off == -1) {
15867c478bd9Sstevel@tonic-gate 			/* no global property */
15877c478bd9Sstevel@tonic-gate 			prop_off = 0;
15887c478bd9Sstevel@tonic-gate 		} else if ((prop_off == 0) && (DI_NODE(node)->drv_major >= 0)) {
15897c478bd9Sstevel@tonic-gate 			/* refer to devnames array */
15907c478bd9Sstevel@tonic-gate 			struct di_devnm *devnm = DI_DEVNM(pa +
15917c478bd9Sstevel@tonic-gate 			    DI_ALL(pa)->devnames + (DI_NODE(node)->drv_major *
15927c478bd9Sstevel@tonic-gate 			    sizeof (struct di_devnm)));
15937c478bd9Sstevel@tonic-gate 			prop_off = devnm->global_prop;
15947c478bd9Sstevel@tonic-gate 		}
15957c478bd9Sstevel@tonic-gate 		break;
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	if (prop_off) {
15997c478bd9Sstevel@tonic-gate 		return (DI_PROP(pa + prop_off));
16007c478bd9Sstevel@tonic-gate 	}
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	/*
16037c478bd9Sstevel@tonic-gate 	 * no prop found. Check the reason for not found
16047c478bd9Sstevel@tonic-gate 	 */
16057c478bd9Sstevel@tonic-gate 	if (DINFOPROP & DI_ALL(pa)->command)
16067c478bd9Sstevel@tonic-gate 		errno = ENXIO;
16077c478bd9Sstevel@tonic-gate 	else
16087c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
16117c478bd9Sstevel@tonic-gate }
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate di_prop_t
16147c478bd9Sstevel@tonic-gate di_prop_drv_next(di_node_t node, di_prop_t prop)
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_DRV));
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate di_prop_t
16207c478bd9Sstevel@tonic-gate di_prop_sys_next(di_node_t node, di_prop_t prop)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_SYS));
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate di_prop_t
16267c478bd9Sstevel@tonic-gate di_prop_global_next(di_node_t node, di_prop_t prop)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_GLOB));
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate di_prop_t
16327c478bd9Sstevel@tonic-gate di_prop_hw_next(di_node_t node, di_prop_t prop)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	return (di_prop_next_common(node, prop, PROP_TYPE_HW));
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate int
16387c478bd9Sstevel@tonic-gate di_prop_rawdata(di_prop_t prop, uchar_t **prop_data)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate #ifdef DEBUG
16417c478bd9Sstevel@tonic-gate 	if (prop == DI_PROP_NIL) {
16427c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16437c478bd9Sstevel@tonic-gate 		return (-1);
16447c478bd9Sstevel@tonic-gate 	}
16457c478bd9Sstevel@tonic-gate #endif /* DEBUG */
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	if (DI_PROP(prop)->prop_len == 0) {
16487c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
16497c478bd9Sstevel@tonic-gate 		return (0);
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	if ((DI_PROP(prop)->prop_data == 0) ||
16537c478bd9Sstevel@tonic-gate 	    (DI_PROP(prop)->prop_data == (di_off_t)-1)) {
16547c478bd9Sstevel@tonic-gate 		errno = EFAULT;
16557c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
16567c478bd9Sstevel@tonic-gate 		return (-1);
16577c478bd9Sstevel@tonic-gate 	}
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	/*
16607c478bd9Sstevel@tonic-gate 	 * No memory allocation.
16617c478bd9Sstevel@tonic-gate 	 */
16627c478bd9Sstevel@tonic-gate 	*prop_data = (uchar_t *)((caddr_t)prop - DI_PROP(prop)->self +
16637c478bd9Sstevel@tonic-gate 	    DI_PROP(prop)->prop_data);
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	return (DI_PROP(prop)->prop_len);
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate /*
16697c478bd9Sstevel@tonic-gate  * Consolidation private interfaces for accessing I/O multipathing data
16707c478bd9Sstevel@tonic-gate  */
16717c478bd9Sstevel@tonic-gate di_path_t
16727c478bd9Sstevel@tonic-gate di_path_next_client(di_node_t node, di_path_t path)
16737c478bd9Sstevel@tonic-gate {
16747c478bd9Sstevel@tonic-gate 	caddr_t pa;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	/*
16777c478bd9Sstevel@tonic-gate 	 * path is not NIL
16787c478bd9Sstevel@tonic-gate 	 */
16797c478bd9Sstevel@tonic-gate 	if (path != DI_PATH_NIL) {
16807c478bd9Sstevel@tonic-gate 		if (DI_PATH(path)->path_p_link != 0)
16817c478bd9Sstevel@tonic-gate 			return (DI_PATH((void *)((caddr_t)path -
16827c478bd9Sstevel@tonic-gate 			    DI_PATH(path)->self + DI_PATH(path)->path_p_link)));
16837c478bd9Sstevel@tonic-gate 		else {
16847c478bd9Sstevel@tonic-gate 			errno = ENXIO;
16857c478bd9Sstevel@tonic-gate 			return (DI_PATH_NIL);
16867c478bd9Sstevel@tonic-gate 		}
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	/*
16907c478bd9Sstevel@tonic-gate 	 * Path is NIL; the caller is asking for the first path info node
16917c478bd9Sstevel@tonic-gate 	 */
16927c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->multipath_phci != 0) {
16937c478bd9Sstevel@tonic-gate 		DPRINTF((DI_INFO, "phci: returning %p\n", ((caddr_t)node -
16947c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->self + DI_NODE(node)->multipath_phci)));
16957c478bd9Sstevel@tonic-gate 		return (DI_PATH((caddr_t)node - DI_NODE(node)->self +
16967c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->multipath_phci));
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * No pathing data; check if the snapshot includes path data in order
17017c478bd9Sstevel@tonic-gate 	 * to set errno properly.
17027c478bd9Sstevel@tonic-gate 	 */
17037c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
17047c478bd9Sstevel@tonic-gate 	if (DINFOPATH & (DI_ALL(pa)->command))
17057c478bd9Sstevel@tonic-gate 		errno = ENXIO;
17067c478bd9Sstevel@tonic-gate 	else
17077c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	return (DI_PATH_NIL);
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate di_path_t
17137c478bd9Sstevel@tonic-gate di_path_next_phci(di_node_t node, di_path_t path)
17147c478bd9Sstevel@tonic-gate {
17157c478bd9Sstevel@tonic-gate 	caddr_t pa;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	/*
17187c478bd9Sstevel@tonic-gate 	 * path is not NIL
17197c478bd9Sstevel@tonic-gate 	 */
17207c478bd9Sstevel@tonic-gate 	if (path != DI_PATH_NIL) {
17217c478bd9Sstevel@tonic-gate 		if (DI_PATH(path)->path_c_link != 0)
17227c478bd9Sstevel@tonic-gate 			return (DI_PATH((caddr_t)path - DI_PATH(path)->self
17237c478bd9Sstevel@tonic-gate 			    + DI_PATH(path)->path_c_link));
17247c478bd9Sstevel@tonic-gate 		else {
17257c478bd9Sstevel@tonic-gate 			errno = ENXIO;
17267c478bd9Sstevel@tonic-gate 			return (DI_PATH_NIL);
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * Path is NIL; the caller is asking for the first path info node
17327c478bd9Sstevel@tonic-gate 	 */
17337c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->multipath_client != 0) {
17347c478bd9Sstevel@tonic-gate 		DPRINTF((DI_INFO, "client: returning %p\n", ((caddr_t)node -
17357c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->self + DI_NODE(node)->multipath_client)));
17367c478bd9Sstevel@tonic-gate 		return (DI_PATH((caddr_t)node - DI_NODE(node)->self +
17377c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->multipath_client));
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/*
17417c478bd9Sstevel@tonic-gate 	 * No pathing data; check if the snapshot includes path data in order
17427c478bd9Sstevel@tonic-gate 	 * to set errno properly.
17437c478bd9Sstevel@tonic-gate 	 */
17447c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
17457c478bd9Sstevel@tonic-gate 	if (DINFOPATH & (DI_ALL(pa)->command))
17467c478bd9Sstevel@tonic-gate 		errno = ENXIO;
17477c478bd9Sstevel@tonic-gate 	else
17487c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	return (DI_PATH_NIL);
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate /*
17547c478bd9Sstevel@tonic-gate  * XXX Obsolete wrapper to be removed. Won't work under multilevel.
17557c478bd9Sstevel@tonic-gate  */
17567c478bd9Sstevel@tonic-gate di_path_t
17577c478bd9Sstevel@tonic-gate di_path_next(di_node_t node, di_path_t path)
17587c478bd9Sstevel@tonic-gate {
17597c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
17607c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17617c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->multipath_client) {
17657c478bd9Sstevel@tonic-gate 		return (di_path_next_phci(node, path));
17667c478bd9Sstevel@tonic-gate 	} else if (DI_NODE(node)->multipath_phci) {
17677c478bd9Sstevel@tonic-gate 		return (di_path_next_client(node, path));
17687c478bd9Sstevel@tonic-gate 	} else {
17697c478bd9Sstevel@tonic-gate 		/*
17707c478bd9Sstevel@tonic-gate 		 * The node had multipathing data but didn't appear to be a
17717c478bd9Sstevel@tonic-gate 		 * phci *or* a client; probably a programmer error.
17727c478bd9Sstevel@tonic-gate 		 */
17737c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17747c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate di_path_state_t
17797c478bd9Sstevel@tonic-gate di_path_state(di_path_t path)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate 	return ((di_path_state_t)DI_PATH(path)->path_state);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate char *
17857c478bd9Sstevel@tonic-gate di_path_addr(di_path_t path, char *buf)
17867c478bd9Sstevel@tonic-gate {
17877c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	(void) strncpy(buf, (char *)(pa + DI_PATH(path)->path_addr),
17927c478bd9Sstevel@tonic-gate 	    MAXPATHLEN);
17937c478bd9Sstevel@tonic-gate 	return (buf);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate di_node_t
17977c478bd9Sstevel@tonic-gate di_path_client_node(di_path_t path)
17987c478bd9Sstevel@tonic-gate {
17997c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	if (path == DI_PATH_NIL) {
18027c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18037c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get client node for path %p\n", path));
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	if (DI_PATH(path)->path_client) {
18117c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_PATH(path)->path_client));
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	/*
18157c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
18167c478bd9Sstevel@tonic-gate 	 *   If parent doesn't exist and node is not the root,
18177c478bd9Sstevel@tonic-gate 	 *   set errno to ENOTSUP. Otherwise, set errno to ENXIO.
18187c478bd9Sstevel@tonic-gate 	 */
18197c478bd9Sstevel@tonic-gate 	if ((DI_PATH(path)->path_snap_state & DI_PATH_SNAP_NOCLIENT) == 0)
18207c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
18217c478bd9Sstevel@tonic-gate 	else
18227c478bd9Sstevel@tonic-gate 		errno = ENXIO;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate di_node_t
18287c478bd9Sstevel@tonic-gate di_path_phci_node(di_path_t path)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	if (path == DI_PATH_NIL) {
18337c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18347c478bd9Sstevel@tonic-gate 		return (DI_PATH_NIL);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Get phci node for path %p\n", path));
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if (DI_PATH(path)->path_phci) {
18427c478bd9Sstevel@tonic-gate 		return (DI_NODE(pa + DI_PATH(path)->path_phci));
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	/*
18467c478bd9Sstevel@tonic-gate 	 * Deal with error condition:
18477c478bd9Sstevel@tonic-gate 	 *   If parent doesn't exist and node is not the root,
18487c478bd9Sstevel@tonic-gate 	 *   set errno to ENOTSUP. Otherwise, set errno to ENXIO.
18497c478bd9Sstevel@tonic-gate 	 */
18507c478bd9Sstevel@tonic-gate 	if ((DI_PATH(path)->path_snap_state & DI_PATH_SNAP_NOPHCI) == 0)
18517c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
18527c478bd9Sstevel@tonic-gate 	else
18537c478bd9Sstevel@tonic-gate 		errno = ENXIO;
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	return (DI_NODE_NIL);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate di_path_prop_t
18597c478bd9Sstevel@tonic-gate di_path_prop_next(di_path_t path, di_path_prop_t prop)
18607c478bd9Sstevel@tonic-gate {
18617c478bd9Sstevel@tonic-gate 	caddr_t pa;
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	if (path == DI_PATH_NIL) {
18647c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18657c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * prop is not NIL
18707c478bd9Sstevel@tonic-gate 	 */
18717c478bd9Sstevel@tonic-gate 	if (prop != DI_PROP_NIL) {
18727c478bd9Sstevel@tonic-gate 		if (DI_PROP(prop)->next != 0)
18737c478bd9Sstevel@tonic-gate 			return (DI_PATHPROP((caddr_t)prop -
18747c478bd9Sstevel@tonic-gate 			    DI_PROP(prop)->self + DI_PROP(prop)->next));
18757c478bd9Sstevel@tonic-gate 		else {
18767c478bd9Sstevel@tonic-gate 			errno = ENXIO;
18777c478bd9Sstevel@tonic-gate 			return (DI_PROP_NIL);
18787c478bd9Sstevel@tonic-gate 		}
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	/*
18827c478bd9Sstevel@tonic-gate 	 * prop is NIL-->caller asks for first property
18837c478bd9Sstevel@tonic-gate 	 */
18847c478bd9Sstevel@tonic-gate 	pa = (caddr_t)path - DI_PATH(path)->self;
18857c478bd9Sstevel@tonic-gate 	if (DI_PATH(path)->path_prop != 0) {
18867c478bd9Sstevel@tonic-gate 		return (DI_PATHPROP(pa + DI_PATH(path)->path_prop));
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	/*
18907c478bd9Sstevel@tonic-gate 	 * no property data-->check if snapshot includes props
18917c478bd9Sstevel@tonic-gate 	 *	in order to set the correct errno
18927c478bd9Sstevel@tonic-gate 	 */
18937c478bd9Sstevel@tonic-gate 	if (DINFOPROP & (DI_ALL(pa)->command))
18947c478bd9Sstevel@tonic-gate 		errno = ENXIO;
18957c478bd9Sstevel@tonic-gate 	else
18967c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
18997c478bd9Sstevel@tonic-gate }
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate char *
19027c478bd9Sstevel@tonic-gate di_path_prop_name(di_path_prop_t prop)
19037c478bd9Sstevel@tonic-gate {
19047c478bd9Sstevel@tonic-gate 	caddr_t pa;		/* starting address of map */
19057c478bd9Sstevel@tonic-gate 	pa = (caddr_t)prop - DI_PATHPROP(prop)->self;
19067c478bd9Sstevel@tonic-gate 	return ((char *)(pa + DI_PATHPROP(prop)->prop_name));
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate int
19107c478bd9Sstevel@tonic-gate di_path_prop_len(di_path_prop_t prop)
19117c478bd9Sstevel@tonic-gate {
19127c478bd9Sstevel@tonic-gate 	return (DI_PATHPROP(prop)->prop_len);
19137c478bd9Sstevel@tonic-gate }
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate int
19167c478bd9Sstevel@tonic-gate di_path_prop_type(di_path_prop_t prop)
19177c478bd9Sstevel@tonic-gate {
19187c478bd9Sstevel@tonic-gate 	switch (DI_PATHPROP(prop)->prop_type) {
19197c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_INT:
19207c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_INT);
19217c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_INT64:
19227c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_INT64);
19237c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_BYTE:
19247c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_BYTE);
19257c478bd9Sstevel@tonic-gate 		case DDI_PROP_TYPE_STRING:
19267c478bd9Sstevel@tonic-gate 			return (DI_PROP_TYPE_STRING);
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 	return (DI_PROP_TYPE_UNKNOWN);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate int
19327c478bd9Sstevel@tonic-gate di_path_prop_bytes(di_path_prop_t prop, uchar_t **prop_data)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
19357c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
19367c478bd9Sstevel@tonic-gate 		errno = EFAULT;
19377c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
19387c478bd9Sstevel@tonic-gate 		return (-1);
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	*prop_data = (uchar_t *)((caddr_t)prop - DI_PATHPROP(prop)->self
19427c478bd9Sstevel@tonic-gate 	    + DI_PATHPROP(prop)->prop_data);
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
19457c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_BYTE, 0));
19467c478bd9Sstevel@tonic-gate }
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate int
19497c478bd9Sstevel@tonic-gate di_path_prop_ints(di_path_prop_t prop, int **prop_data)
19507c478bd9Sstevel@tonic-gate {
19517c478bd9Sstevel@tonic-gate 	if (DI_PATHPROP(prop)->prop_len == 0)
19527c478bd9Sstevel@tonic-gate 		return (0);
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
19557c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
19567c478bd9Sstevel@tonic-gate 		errno = EFAULT;
19577c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
19587c478bd9Sstevel@tonic-gate 		return (-1);
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	*prop_data = (int *)((void *)((caddr_t)prop - DI_PATHPROP(prop)->self
19627c478bd9Sstevel@tonic-gate 	    + DI_PATHPROP(prop)->prop_data));
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
19657c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_INT, 0));
19667c478bd9Sstevel@tonic-gate }
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate int
19697c478bd9Sstevel@tonic-gate di_path_prop_int64s(di_path_prop_t prop, int64_t **prop_data)
19707c478bd9Sstevel@tonic-gate {
19717c478bd9Sstevel@tonic-gate 	if (DI_PATHPROP(prop)->prop_len == 0)
19727c478bd9Sstevel@tonic-gate 		return (0);
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
19757c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
19767c478bd9Sstevel@tonic-gate 		errno = EFAULT;
19777c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
19787c478bd9Sstevel@tonic-gate 		return (-1);
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	*prop_data = (int64_t *)((void *)((caddr_t)prop -
19827c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->self + DI_PATHPROP(prop)->prop_data));
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
19857c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_INT64, 0));
19867c478bd9Sstevel@tonic-gate }
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate int
19897c478bd9Sstevel@tonic-gate di_path_prop_strings(di_path_prop_t prop, char **prop_data)
19907c478bd9Sstevel@tonic-gate {
19917c478bd9Sstevel@tonic-gate 	if (DI_PATHPROP(prop)->prop_len == 0)
19927c478bd9Sstevel@tonic-gate 		return (0);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	if ((DI_PATHPROP(prop)->prop_data == 0) ||
19957c478bd9Sstevel@tonic-gate 	    (DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
19967c478bd9Sstevel@tonic-gate 		errno = EFAULT;
19977c478bd9Sstevel@tonic-gate 		*prop_data = NULL;
19987c478bd9Sstevel@tonic-gate 		return (-1);
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	*prop_data = (char *)((caddr_t)prop - DI_PATHPROP(prop)->self
20027c478bd9Sstevel@tonic-gate 	    + DI_PATHPROP(prop)->prop_data);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	return (di_prop_decode_common((void *)prop_data,
20057c478bd9Sstevel@tonic-gate 	    DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_STRING, 0));
20067c478bd9Sstevel@tonic-gate }
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate static di_path_prop_t
20097c478bd9Sstevel@tonic-gate di_path_prop_search(di_path_t path, const char *name, int type)
20107c478bd9Sstevel@tonic-gate {
20117c478bd9Sstevel@tonic-gate 	di_path_prop_t prop = DI_PROP_NIL;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 	/*
20147c478bd9Sstevel@tonic-gate 	 * Sanity check arguments
20157c478bd9Sstevel@tonic-gate 	 */
20167c478bd9Sstevel@tonic-gate 	if ((path == DI_PATH_NIL) || (name == NULL) || (strlen(name) == 0) ||
20177c478bd9Sstevel@tonic-gate 	    !DI_PROP_TYPE_VALID(type)) {
20187c478bd9Sstevel@tonic-gate 		errno = EINVAL;
20197c478bd9Sstevel@tonic-gate 		return (DI_PROP_NIL);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	while ((prop = di_path_prop_next(path, prop)) != DI_PROP_NIL) {
20237c478bd9Sstevel@tonic-gate 		int prop_type = di_path_prop_type(prop);
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 		DPRINTF((DI_TRACE1, "match path prop name %s, type %d\n",
20267c478bd9Sstevel@tonic-gate 		    di_path_prop_name(prop), prop_type));
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 		if (strcmp(name, di_path_prop_name(prop)) != 0)
20297c478bd9Sstevel@tonic-gate 			continue;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 		if ((prop_type != DI_PROP_TYPE_UNKNOWN) && (prop_type != type))
20327c478bd9Sstevel@tonic-gate 			continue;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		return (prop);
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	return (DI_PROP_NIL);
20387c478bd9Sstevel@tonic-gate }
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate int
20417c478bd9Sstevel@tonic-gate di_path_prop_lookup_bytes(di_path_t path, const char *prop_name,
20427c478bd9Sstevel@tonic-gate     uchar_t **prop_data)
20437c478bd9Sstevel@tonic-gate {
20447c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
20477c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_BYTE)) == DI_PROP_NIL)
20487c478bd9Sstevel@tonic-gate 		return (-1);
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	return (di_path_prop_bytes(prop, prop_data));
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate int
20547c478bd9Sstevel@tonic-gate di_path_prop_lookup_ints(di_path_t path, const char *prop_name,
20557c478bd9Sstevel@tonic-gate     int **prop_data)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
20607c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT)) == DI_PROP_NIL)
20617c478bd9Sstevel@tonic-gate 		return (-1);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	return (di_path_prop_ints(prop, prop_data));
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate int
20677c478bd9Sstevel@tonic-gate di_path_prop_lookup_int64s(di_path_t path, const char *prop_name,
20687c478bd9Sstevel@tonic-gate     int64_t **prop_data)
20697c478bd9Sstevel@tonic-gate {
20707c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
20737c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_INT64)) == DI_PROP_NIL)
20747c478bd9Sstevel@tonic-gate 		return (-1);
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	return (di_path_prop_int64s(prop, prop_data));
20777c478bd9Sstevel@tonic-gate }
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate int di_path_prop_lookup_strings(di_path_t path, const char *prop_name,
20807c478bd9Sstevel@tonic-gate     char **prop_data)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	di_path_prop_t prop;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if ((prop = di_path_prop_search(path, prop_name,
20857c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_STRING)) == DI_PROP_NIL)
20867c478bd9Sstevel@tonic-gate 		return (-1);
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	return (di_path_prop_strings(prop, prop_data));
20897c478bd9Sstevel@tonic-gate }
20907c478bd9Sstevel@tonic-gate 
2091*8c4f8890Srs135747 /*
2092*8c4f8890Srs135747  * Consolidation private interfaces for traversing vhci nodes.
2093*8c4f8890Srs135747  */
2094*8c4f8890Srs135747 di_node_t
2095*8c4f8890Srs135747 di_vhci_first_node(di_node_t root)
2096*8c4f8890Srs135747 {
2097*8c4f8890Srs135747 	struct di_all *dap;
2098*8c4f8890Srs135747 	caddr_t		pa;		/* starting address of map */
2099*8c4f8890Srs135747 
2100*8c4f8890Srs135747 	DPRINTF((DI_INFO, "Get first vhci node\n"));
2101*8c4f8890Srs135747 
2102*8c4f8890Srs135747 	if (root == DI_NODE_NIL) {
2103*8c4f8890Srs135747 		errno = EINVAL;
2104*8c4f8890Srs135747 		return (DI_NODE_NIL);
2105*8c4f8890Srs135747 	}
2106*8c4f8890Srs135747 
2107*8c4f8890Srs135747 	pa = (caddr_t)root - DI_NODE(root)->self;
2108*8c4f8890Srs135747 	dap = DI_ALL(pa);
2109*8c4f8890Srs135747 
2110*8c4f8890Srs135747 	if (dap->top_vhci_devinfo == NULL) {
2111*8c4f8890Srs135747 		errno = ENXIO;
2112*8c4f8890Srs135747 		return (DI_NODE_NIL);
2113*8c4f8890Srs135747 	}
2114*8c4f8890Srs135747 
2115*8c4f8890Srs135747 	return (DI_NODE(pa + dap->top_vhci_devinfo));
2116*8c4f8890Srs135747 }
2117*8c4f8890Srs135747 
2118*8c4f8890Srs135747 di_node_t
2119*8c4f8890Srs135747 di_vhci_next_node(di_node_t node)
2120*8c4f8890Srs135747 {
2121*8c4f8890Srs135747 	caddr_t		pa;		/* starting address of map */
2122*8c4f8890Srs135747 
2123*8c4f8890Srs135747 	if (node == DI_NODE_NIL) {
2124*8c4f8890Srs135747 		errno = EINVAL;
2125*8c4f8890Srs135747 		return (DI_NODE_NIL);
2126*8c4f8890Srs135747 	}
2127*8c4f8890Srs135747 
2128*8c4f8890Srs135747 	DPRINTF((DI_TRACE, "next vhci node on the snap shot:"
2129*8c4f8890Srs135747 	    " current=%s\n", di_node_name(node)));
2130*8c4f8890Srs135747 
2131*8c4f8890Srs135747 	if (DI_NODE(node)->next_vhci == NULL) {
2132*8c4f8890Srs135747 		errno = ENXIO;
2133*8c4f8890Srs135747 		return (DI_NODE_NIL);
2134*8c4f8890Srs135747 	}
2135*8c4f8890Srs135747 
2136*8c4f8890Srs135747 	pa = (caddr_t)node - DI_NODE(node)->self;
2137*8c4f8890Srs135747 
2138*8c4f8890Srs135747 	return (DI_NODE(pa + DI_NODE(node)->next_vhci));
2139*8c4f8890Srs135747 }
2140*8c4f8890Srs135747 
2141*8c4f8890Srs135747 /*
2142*8c4f8890Srs135747  * Consolidation private interfaces for traversing phci nodes.
2143*8c4f8890Srs135747  */
2144*8c4f8890Srs135747 di_node_t
2145*8c4f8890Srs135747 di_phci_first_node(di_node_t vhci_node)
2146*8c4f8890Srs135747 {
2147*8c4f8890Srs135747 	caddr_t		pa;		/* starting address of map */
2148*8c4f8890Srs135747 
2149*8c4f8890Srs135747 	DPRINTF((DI_INFO, "Get first phci node:\n"
2150*8c4f8890Srs135747 	    " current=%s", di_node_name(vhci_node)));
2151*8c4f8890Srs135747 
2152*8c4f8890Srs135747 	if (vhci_node == DI_NODE_NIL) {
2153*8c4f8890Srs135747 		errno = EINVAL;
2154*8c4f8890Srs135747 		return (DI_NODE_NIL);
2155*8c4f8890Srs135747 	}
2156*8c4f8890Srs135747 
2157*8c4f8890Srs135747 	pa = (caddr_t)vhci_node - DI_NODE(vhci_node)->self;
2158*8c4f8890Srs135747 
2159*8c4f8890Srs135747 	if (DI_NODE(vhci_node)->top_phci == NULL) {
2160*8c4f8890Srs135747 		errno = ENXIO;
2161*8c4f8890Srs135747 		return (DI_NODE_NIL);
2162*8c4f8890Srs135747 	}
2163*8c4f8890Srs135747 
2164*8c4f8890Srs135747 	return (DI_NODE(pa + DI_NODE(vhci_node)->top_phci));
2165*8c4f8890Srs135747 }
2166*8c4f8890Srs135747 
2167*8c4f8890Srs135747 di_node_t
2168*8c4f8890Srs135747 di_phci_next_node(di_node_t node)
2169*8c4f8890Srs135747 {
2170*8c4f8890Srs135747 	caddr_t		pa;		/* starting address of map */
2171*8c4f8890Srs135747 
2172*8c4f8890Srs135747 	if (node == DI_NODE_NIL) {
2173*8c4f8890Srs135747 		errno = EINVAL;
2174*8c4f8890Srs135747 		return (DI_NODE_NIL);
2175*8c4f8890Srs135747 	}
2176*8c4f8890Srs135747 
2177*8c4f8890Srs135747 	DPRINTF((DI_TRACE, "next phci node on the snap shot:"
2178*8c4f8890Srs135747 	    " current=%s\n", di_node_name(node)));
2179*8c4f8890Srs135747 
2180*8c4f8890Srs135747 	if (DI_NODE(node)->next_phci == NULL) {
2181*8c4f8890Srs135747 		errno = ENXIO;
2182*8c4f8890Srs135747 		return (DI_NODE_NIL);
2183*8c4f8890Srs135747 	}
2184*8c4f8890Srs135747 
2185*8c4f8890Srs135747 	pa = (caddr_t)node - DI_NODE(node)->self;
2186*8c4f8890Srs135747 
2187*8c4f8890Srs135747 	return (DI_NODE(pa + DI_NODE(node)->next_phci));
2188*8c4f8890Srs135747 }
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate /*
21917c478bd9Sstevel@tonic-gate  * Consolidation private interfaces for private data
21927c478bd9Sstevel@tonic-gate  */
21937c478bd9Sstevel@tonic-gate void *
21947c478bd9Sstevel@tonic-gate di_parent_private_data(di_node_t node)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	caddr_t pa;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent_data == 0) {
21997c478bd9Sstevel@tonic-gate 		errno = ENXIO;
22007c478bd9Sstevel@tonic-gate 		return (NULL);
22017c478bd9Sstevel@tonic-gate 	}
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent_data == (di_off_t)-1) {
22047c478bd9Sstevel@tonic-gate 		/*
22057c478bd9Sstevel@tonic-gate 		 * Private data requested, but not obtained due to a memory
22067c478bd9Sstevel@tonic-gate 		 * error (e.g. wrong format specified)
22077c478bd9Sstevel@tonic-gate 		 */
22087c478bd9Sstevel@tonic-gate 		errno = EFAULT;
22097c478bd9Sstevel@tonic-gate 		return (NULL);
22107c478bd9Sstevel@tonic-gate 	}
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
22137c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->parent_data)
22147c478bd9Sstevel@tonic-gate 		return (pa + DI_NODE(node)->parent_data);
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	if (DI_ALL(pa)->command & DINFOPRIVDATA)
22177c478bd9Sstevel@tonic-gate 		errno = ENXIO;
22187c478bd9Sstevel@tonic-gate 	else
22197c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate 	return (NULL);
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate void *
22257c478bd9Sstevel@tonic-gate di_driver_private_data(di_node_t node)
22267c478bd9Sstevel@tonic-gate {
22277c478bd9Sstevel@tonic-gate 	caddr_t pa;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->driver_data == 0) {
22307c478bd9Sstevel@tonic-gate 		errno = ENXIO;
22317c478bd9Sstevel@tonic-gate 		return (NULL);
22327c478bd9Sstevel@tonic-gate 	}
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->driver_data == (di_off_t)-1) {
22357c478bd9Sstevel@tonic-gate 		/*
22367c478bd9Sstevel@tonic-gate 		 * Private data requested, but not obtained due to a memory
22377c478bd9Sstevel@tonic-gate 		 * error (e.g. wrong format specified)
22387c478bd9Sstevel@tonic-gate 		 */
22397c478bd9Sstevel@tonic-gate 		errno = EFAULT;
22407c478bd9Sstevel@tonic-gate 		return (NULL);
22417c478bd9Sstevel@tonic-gate 	}
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	pa = (caddr_t)node - DI_NODE(node)->self;
22447c478bd9Sstevel@tonic-gate 	if (DI_NODE(node)->driver_data)
22457c478bd9Sstevel@tonic-gate 		return (pa + DI_NODE(node)->driver_data);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	if (DI_ALL(pa)->command & DINFOPRIVDATA)
22487c478bd9Sstevel@tonic-gate 		errno = ENXIO;
22497c478bd9Sstevel@tonic-gate 	else
22507c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	return (NULL);
22537c478bd9Sstevel@tonic-gate }
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate /*
22567c478bd9Sstevel@tonic-gate  * PROM property access
22577c478bd9Sstevel@tonic-gate  */
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate /*
22607c478bd9Sstevel@tonic-gate  * openprom driver stuff:
22617c478bd9Sstevel@tonic-gate  *	The maximum property length depends on the buffer size. We use
22627c478bd9Sstevel@tonic-gate  *	OPROMMAXPARAM defined in <sys/openpromio.h>
22637c478bd9Sstevel@tonic-gate  *
22647c478bd9Sstevel@tonic-gate  *	MAXNAMESZ is max property name. obpdefs.h defines it as 32 based on 1275
22657c478bd9Sstevel@tonic-gate  *	MAXVALSZ is maximum value size, which is whatever space left in buf
22667c478bd9Sstevel@tonic-gate  */
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate #define	OBP_MAXBUF	OPROMMAXPARAM - sizeof (int)
22697c478bd9Sstevel@tonic-gate #define	OBP_MAXPROPLEN	OBP_MAXBUF - OBP_MAXPROPNAME;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate struct di_prom_prop {
22727c478bd9Sstevel@tonic-gate 	char *name;
22737c478bd9Sstevel@tonic-gate 	int len;
22747c478bd9Sstevel@tonic-gate 	uchar_t *data;
22757c478bd9Sstevel@tonic-gate 	struct di_prom_prop *next;	/* form a linked list */
22767c478bd9Sstevel@tonic-gate };
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate struct di_prom_handle { /* handle to prom */
22797c478bd9Sstevel@tonic-gate 	mutex_t lock;	/* synchronize access to openprom fd */
22807c478bd9Sstevel@tonic-gate 	int	fd;	/* /dev/openprom file descriptor */
22817c478bd9Sstevel@tonic-gate 	struct di_prom_prop *list;	/* linked list of prop */
22827c478bd9Sstevel@tonic-gate 	union {
22837c478bd9Sstevel@tonic-gate 		char buf[OPROMMAXPARAM];
22847c478bd9Sstevel@tonic-gate 		struct openpromio opp;
22857c478bd9Sstevel@tonic-gate 	} oppbuf;
22867c478bd9Sstevel@tonic-gate };
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate di_prom_handle_t
22897c478bd9Sstevel@tonic-gate di_prom_init()
22907c478bd9Sstevel@tonic-gate {
22917c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	if ((p = malloc(sizeof (struct di_prom_handle))) == NULL)
22947c478bd9Sstevel@tonic-gate 		return (DI_PROM_HANDLE_NIL);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_prom_init: get prom handle 0x%p\n", p));
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	(void) mutex_init(&p->lock, USYNC_THREAD, NULL);
22997c478bd9Sstevel@tonic-gate 	if ((p->fd = open("/dev/openprom", O_RDONLY)) < 0) {
23007c478bd9Sstevel@tonic-gate 		free(p);
23017c478bd9Sstevel@tonic-gate 		return (DI_PROM_HANDLE_NIL);
23027c478bd9Sstevel@tonic-gate 	}
23037c478bd9Sstevel@tonic-gate 	p->list = NULL;
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	return ((di_prom_handle_t)p);
23067c478bd9Sstevel@tonic-gate }
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate static void
23097c478bd9Sstevel@tonic-gate di_prom_prop_free(struct di_prom_prop *list)
23107c478bd9Sstevel@tonic-gate {
23117c478bd9Sstevel@tonic-gate 	struct di_prom_prop *tmp = list;
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	while (tmp != NULL) {
23147c478bd9Sstevel@tonic-gate 		list = tmp->next;
23157c478bd9Sstevel@tonic-gate 		if (tmp->name != NULL) {
23167c478bd9Sstevel@tonic-gate 			free(tmp->name);
23177c478bd9Sstevel@tonic-gate 		}
23187c478bd9Sstevel@tonic-gate 		if (tmp->data != NULL) {
23197c478bd9Sstevel@tonic-gate 			free(tmp->data);
23207c478bd9Sstevel@tonic-gate 		}
23217c478bd9Sstevel@tonic-gate 		free(tmp);
23227c478bd9Sstevel@tonic-gate 		tmp = list;
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate }
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate void
23277c478bd9Sstevel@tonic-gate di_prom_fini(di_prom_handle_t ph)
23287c478bd9Sstevel@tonic-gate {
23297c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "di_prom_fini: free prom handle 0x%p\n", p));
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	(void) close(p->fd);
23347c478bd9Sstevel@tonic-gate 	(void) mutex_destroy(&p->lock);
23357c478bd9Sstevel@tonic-gate 	di_prom_prop_free(p->list);
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	free(p);
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate /*
23417c478bd9Sstevel@tonic-gate  * Internal library interface for locating the property
23427c478bd9Sstevel@tonic-gate  * XXX: ph->lock must be held for the duration of call.
23437c478bd9Sstevel@tonic-gate  */
23447c478bd9Sstevel@tonic-gate static di_prom_prop_t
23457c478bd9Sstevel@tonic-gate di_prom_prop_found(di_prom_handle_t ph, int nodeid,
23467c478bd9Sstevel@tonic-gate 	di_prom_prop_t prom_prop)
23477c478bd9Sstevel@tonic-gate {
23487c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
23497c478bd9Sstevel@tonic-gate 	struct openpromio *opp = &p->oppbuf.opp;
23507c478bd9Sstevel@tonic-gate 	int *ip = (int *)((void *)opp->oprom_array);
23517c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop = (struct di_prom_prop *)prom_prop;
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE1, "Looking for nodeid 0x%x\n", nodeid));
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	/*
23567c478bd9Sstevel@tonic-gate 	 * Set "current" nodeid in the openprom driver
23577c478bd9Sstevel@tonic-gate 	 */
23587c478bd9Sstevel@tonic-gate 	opp->oprom_size = sizeof (int);
23597c478bd9Sstevel@tonic-gate 	*ip = nodeid;
23607c478bd9Sstevel@tonic-gate 	if (ioctl(p->fd, OPROMSETNODEID, opp) < 0) {
23617c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "*** Nodeid not found 0x%x\n", nodeid));
23627c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
23637c478bd9Sstevel@tonic-gate 	}
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE, "Found nodeid 0x%x\n", nodeid));
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	bzero(opp, OBP_MAXBUF);
23687c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPNAME;
23697c478bd9Sstevel@tonic-gate 	if (prom_prop != DI_PROM_PROP_NIL)
23707c478bd9Sstevel@tonic-gate 		(void) strcpy(opp->oprom_array, prop->name);
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMNXTPROP, opp) < 0) || (opp->oprom_size == 0))
23737c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	/*
23767c478bd9Sstevel@tonic-gate 	 * Prom property found. Allocate struct for storing prop
23777c478bd9Sstevel@tonic-gate 	 *   (reuse variable prop)
23787c478bd9Sstevel@tonic-gate 	 */
23797c478bd9Sstevel@tonic-gate 	if ((prop = malloc(sizeof (struct di_prom_prop))) == NULL)
23807c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 	/*
23837c478bd9Sstevel@tonic-gate 	 * Get a copy of property name
23847c478bd9Sstevel@tonic-gate 	 */
23857c478bd9Sstevel@tonic-gate 	if ((prop->name = strdup(opp->oprom_array)) == NULL) {
23867c478bd9Sstevel@tonic-gate 		free(prop);
23877c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
23887c478bd9Sstevel@tonic-gate 	}
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	/*
23917c478bd9Sstevel@tonic-gate 	 * get property value and length
23927c478bd9Sstevel@tonic-gate 	 */
23937c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPLEN;
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMGETPROP, opp) < 0) ||
23967c478bd9Sstevel@tonic-gate 	    (opp->oprom_size == (uint_t)-1)) {
23977c478bd9Sstevel@tonic-gate 		free(prop->name);
23987c478bd9Sstevel@tonic-gate 		free(prop);
23997c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
24007c478bd9Sstevel@tonic-gate 	}
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	/*
24037c478bd9Sstevel@tonic-gate 	 * make a copy of the property value
24047c478bd9Sstevel@tonic-gate 	 */
24057c478bd9Sstevel@tonic-gate 	prop->len = opp->oprom_size;
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	if (prop->len == 0)
24087c478bd9Sstevel@tonic-gate 		prop->data = NULL;
24097c478bd9Sstevel@tonic-gate 	else if ((prop->data = malloc(prop->len)) == NULL) {
24107c478bd9Sstevel@tonic-gate 		free(prop->name);
24117c478bd9Sstevel@tonic-gate 		free(prop);
24127c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
24137c478bd9Sstevel@tonic-gate 	}
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 	bcopy(opp->oprom_array, prop->data, prop->len);
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	/*
24187c478bd9Sstevel@tonic-gate 	 * Prepend prop to list in prom handle
24197c478bd9Sstevel@tonic-gate 	 */
24207c478bd9Sstevel@tonic-gate 	prop->next = p->list;
24217c478bd9Sstevel@tonic-gate 	p->list = prop;
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	return ((di_prom_prop_t)prop);
24247c478bd9Sstevel@tonic-gate }
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate di_prom_prop_t
24277c478bd9Sstevel@tonic-gate di_prom_prop_next(di_prom_handle_t ph, di_node_t node, di_prom_prop_t prom_prop)
24287c478bd9Sstevel@tonic-gate {
24297c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	DPRINTF((DI_TRACE1, "Search next prop for node 0x%p with ph 0x%p\n",
24327c478bd9Sstevel@tonic-gate 		node, p));
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	/*
24357c478bd9Sstevel@tonic-gate 	 * paranoid check
24367c478bd9Sstevel@tonic-gate 	 */
24377c478bd9Sstevel@tonic-gate 	if ((ph == DI_PROM_HANDLE_NIL) || (node == DI_NODE_NIL)) {
24387c478bd9Sstevel@tonic-gate 		errno = EINVAL;
24397c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
24407c478bd9Sstevel@tonic-gate 	}
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	if (di_nodeid(node) != DI_PROM_NODEID) {
24437c478bd9Sstevel@tonic-gate 		errno = ENXIO;
24447c478bd9Sstevel@tonic-gate 		return (DI_PROM_PROP_NIL);
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	/*
24487c478bd9Sstevel@tonic-gate 	 * synchronize access to prom file descriptor
24497c478bd9Sstevel@tonic-gate 	 */
24507c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&p->lock);
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	/*
24537c478bd9Sstevel@tonic-gate 	 * look for next property
24547c478bd9Sstevel@tonic-gate 	 */
24557c478bd9Sstevel@tonic-gate 	prom_prop = di_prom_prop_found(ph, DI_NODE(node)->nodeid, prom_prop);
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&p->lock);
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	return (prom_prop);
24607c478bd9Sstevel@tonic-gate }
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate char *
24637c478bd9Sstevel@tonic-gate di_prom_prop_name(di_prom_prop_t prom_prop)
24647c478bd9Sstevel@tonic-gate {
24657c478bd9Sstevel@tonic-gate 	/*
24667c478bd9Sstevel@tonic-gate 	 * paranoid check
24677c478bd9Sstevel@tonic-gate 	 */
24687c478bd9Sstevel@tonic-gate 	if (prom_prop == DI_PROM_PROP_NIL) {
24697c478bd9Sstevel@tonic-gate 		errno = EINVAL;
24707c478bd9Sstevel@tonic-gate 		return (NULL);
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	return (((struct di_prom_prop *)prom_prop)->name);
24747c478bd9Sstevel@tonic-gate }
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate int
24777c478bd9Sstevel@tonic-gate di_prom_prop_data(di_prom_prop_t prom_prop, uchar_t **prom_prop_data)
24787c478bd9Sstevel@tonic-gate {
24797c478bd9Sstevel@tonic-gate 	/*
24807c478bd9Sstevel@tonic-gate 	 * paranoid check
24817c478bd9Sstevel@tonic-gate 	 */
24827c478bd9Sstevel@tonic-gate 	if (prom_prop == DI_PROM_PROP_NIL) {
24837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
24847c478bd9Sstevel@tonic-gate 		return (NULL);
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	*prom_prop_data = ((struct di_prom_prop *)prom_prop)->data;
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 	return (((struct di_prom_prop *)prom_prop)->len);
24907c478bd9Sstevel@tonic-gate }
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate /*
24937c478bd9Sstevel@tonic-gate  * Internal library interface for locating the property
24947c478bd9Sstevel@tonic-gate  *    Returns length if found, -1 if prop doesn't exist.
24957c478bd9Sstevel@tonic-gate  */
24967c478bd9Sstevel@tonic-gate static struct di_prom_prop *
24977c478bd9Sstevel@tonic-gate di_prom_prop_lookup_common(di_prom_handle_t ph, di_node_t node,
24987c478bd9Sstevel@tonic-gate 	const char *prom_prop_name)
24997c478bd9Sstevel@tonic-gate {
25007c478bd9Sstevel@tonic-gate 	struct openpromio *opp;
25017c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
25027c478bd9Sstevel@tonic-gate 	struct di_prom_handle *p = (struct di_prom_handle *)ph;
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	/*
25057c478bd9Sstevel@tonic-gate 	 * paranoid check
25067c478bd9Sstevel@tonic-gate 	 */
25077c478bd9Sstevel@tonic-gate 	if ((ph == DI_PROM_HANDLE_NIL) || (node == DI_NODE_NIL)) {
25087c478bd9Sstevel@tonic-gate 		errno = EINVAL;
25097c478bd9Sstevel@tonic-gate 		return (NULL);
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	if (di_nodeid(node) != DI_PROM_NODEID) {
25137c478bd9Sstevel@tonic-gate 		errno = ENXIO;
25147c478bd9Sstevel@tonic-gate 		return (NULL);
25157c478bd9Sstevel@tonic-gate 	}
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	opp = &p->oppbuf.opp;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&p->lock);
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	opp->oprom_size = sizeof (int);
25227c478bd9Sstevel@tonic-gate 	opp->oprom_node = DI_NODE(node)->nodeid;
25237c478bd9Sstevel@tonic-gate 	if (ioctl(p->fd, OPROMSETNODEID, opp) < 0) {
25247c478bd9Sstevel@tonic-gate 		errno = ENXIO;
25257c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "*** Nodeid not found 0x%x\n",
25267c478bd9Sstevel@tonic-gate 		    DI_NODE(node)->nodeid));
25277c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
25287c478bd9Sstevel@tonic-gate 		return (NULL);
25297c478bd9Sstevel@tonic-gate 	}
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	/*
25327c478bd9Sstevel@tonic-gate 	 * get property length
25337c478bd9Sstevel@tonic-gate 	 */
25347c478bd9Sstevel@tonic-gate 	bzero(opp, OBP_MAXBUF);
25357c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPLEN;
25367c478bd9Sstevel@tonic-gate 	(void) strcpy(opp->oprom_array, prom_prop_name);
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMGETPROPLEN, opp) < 0) ||
25397c478bd9Sstevel@tonic-gate 	    (opp->oprom_len == -1)) {
25407c478bd9Sstevel@tonic-gate 		/* no such property */
25417c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
25427c478bd9Sstevel@tonic-gate 		return (NULL);
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	/*
25467c478bd9Sstevel@tonic-gate 	 * Prom property found. Allocate struct for storing prop
25477c478bd9Sstevel@tonic-gate 	 */
25487c478bd9Sstevel@tonic-gate 	if ((prop = malloc(sizeof (struct di_prom_prop))) == NULL) {
25497c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
25507c478bd9Sstevel@tonic-gate 		return (NULL);
25517c478bd9Sstevel@tonic-gate 	}
25527c478bd9Sstevel@tonic-gate 	prop->name = NULL;	/* we don't need the name */
25537c478bd9Sstevel@tonic-gate 	prop->len = opp->oprom_len;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
25567c478bd9Sstevel@tonic-gate 		prop->data = NULL;
25577c478bd9Sstevel@tonic-gate 		prop->next = p->list;
25587c478bd9Sstevel@tonic-gate 		p->list = prop;
25597c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
25607c478bd9Sstevel@tonic-gate 		return (prop);
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	/*
25647c478bd9Sstevel@tonic-gate 	 * retrieve the property value
25657c478bd9Sstevel@tonic-gate 	 */
25667c478bd9Sstevel@tonic-gate 	bzero(opp, OBP_MAXBUF);
25677c478bd9Sstevel@tonic-gate 	opp->oprom_size = OBP_MAXPROPLEN;
25687c478bd9Sstevel@tonic-gate 	(void) strcpy(opp->oprom_array, prom_prop_name);
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 	if ((ioctl(p->fd, OPROMGETPROP, opp) < 0) ||
25717c478bd9Sstevel@tonic-gate 	    (opp->oprom_size == (uint_t)-1)) {
25727c478bd9Sstevel@tonic-gate 		/* error retrieving property value */
25737c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
25747c478bd9Sstevel@tonic-gate 		free(prop);
25757c478bd9Sstevel@tonic-gate 		return (NULL);
25767c478bd9Sstevel@tonic-gate 	}
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	/*
25797c478bd9Sstevel@tonic-gate 	 * make a copy of the property value, stick in ph->list
25807c478bd9Sstevel@tonic-gate 	 */
25817c478bd9Sstevel@tonic-gate 	if ((prop->data = malloc(prop->len)) == NULL) {
25827c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&p->lock);
25837c478bd9Sstevel@tonic-gate 		free(prop);
25847c478bd9Sstevel@tonic-gate 		return (NULL);
25857c478bd9Sstevel@tonic-gate 	}
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 	bcopy(opp->oprom_array, prop->data, prop->len);
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	prop->next = p->list;
25907c478bd9Sstevel@tonic-gate 	p->list = prop;
25917c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&p->lock);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	return (prop);
25947c478bd9Sstevel@tonic-gate }
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate int
25977c478bd9Sstevel@tonic-gate di_prom_prop_lookup_ints(di_prom_handle_t ph, di_node_t node,
25987c478bd9Sstevel@tonic-gate 	const char *prom_prop_name, int **prom_prop_data)
25997c478bd9Sstevel@tonic-gate {
26007c478bd9Sstevel@tonic-gate 	int len;
26017c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
26067c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
26077c478bd9Sstevel@tonic-gate 		return (-1);
26087c478bd9Sstevel@tonic-gate 	}
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
26117c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
26127c478bd9Sstevel@tonic-gate 		return (0);
26137c478bd9Sstevel@tonic-gate 	}
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	len = di_prop_decode_common((void *)&prop->data, prop->len,
26167c478bd9Sstevel@tonic-gate 		DI_PROP_TYPE_INT, 1);
26177c478bd9Sstevel@tonic-gate 	*prom_prop_data = (int *)((void *)prop->data);
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	return (len);
26207c478bd9Sstevel@tonic-gate }
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate int
26237c478bd9Sstevel@tonic-gate di_prom_prop_lookup_strings(di_prom_handle_t ph, di_node_t node,
26247c478bd9Sstevel@tonic-gate 	const char *prom_prop_name, char **prom_prop_data)
26257c478bd9Sstevel@tonic-gate {
26267c478bd9Sstevel@tonic-gate 	int len;
26277c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
26327c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
26337c478bd9Sstevel@tonic-gate 		return (-1);
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
26377c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
26387c478bd9Sstevel@tonic-gate 		return (0);
26397c478bd9Sstevel@tonic-gate 	}
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	/*
26427c478bd9Sstevel@tonic-gate 	 * Fix an openprom bug (OBP string not NULL terminated).
26437c478bd9Sstevel@tonic-gate 	 * XXX This should really be fixed in promif.
26447c478bd9Sstevel@tonic-gate 	 */
26457c478bd9Sstevel@tonic-gate 	if (((char *)prop->data)[prop->len - 1] != '\0') {
26467c478bd9Sstevel@tonic-gate 		uchar_t *tmp;
26477c478bd9Sstevel@tonic-gate 		prop->len++;
26487c478bd9Sstevel@tonic-gate 		if ((tmp = realloc(prop->data, prop->len)) == NULL)
26497c478bd9Sstevel@tonic-gate 			return (-1);
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 		prop->data = tmp;
26527c478bd9Sstevel@tonic-gate 		((char *)prop->data)[prop->len - 1] = '\0';
26537c478bd9Sstevel@tonic-gate 		DPRINTF((DI_INFO, "OBP string not NULL terminated: "
26547c478bd9Sstevel@tonic-gate 		    "node=%s, prop=%s, val=%s\n",
26557c478bd9Sstevel@tonic-gate 		    di_node_name(node), prom_prop_name, prop->data));
26567c478bd9Sstevel@tonic-gate 	}
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	len = di_prop_decode_common((void *)&prop->data, prop->len,
26597c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_STRING, 1);
26607c478bd9Sstevel@tonic-gate 	*prom_prop_data = (char *)prop->data;
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	return (len);
26637c478bd9Sstevel@tonic-gate }
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate int
26667c478bd9Sstevel@tonic-gate di_prom_prop_lookup_bytes(di_prom_handle_t ph, di_node_t node,
26677c478bd9Sstevel@tonic-gate 	const char *prom_prop_name, uchar_t **prom_prop_data)
26687c478bd9Sstevel@tonic-gate {
26697c478bd9Sstevel@tonic-gate 	int len;
26707c478bd9Sstevel@tonic-gate 	struct di_prom_prop *prop;
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	if (prop == NULL) {
26757c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
26767c478bd9Sstevel@tonic-gate 		return (-1);
26777c478bd9Sstevel@tonic-gate 	}
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 	if (prop->len == 0) {	/* boolean property */
26807c478bd9Sstevel@tonic-gate 		*prom_prop_data = NULL;
26817c478bd9Sstevel@tonic-gate 		return (0);
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	len = di_prop_decode_common((void *)&prop->data, prop->len,
26857c478bd9Sstevel@tonic-gate 	    DI_PROP_TYPE_BYTE, 1);
26867c478bd9Sstevel@tonic-gate 	*prom_prop_data = prop->data;
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	return (len);
26897c478bd9Sstevel@tonic-gate }
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate di_lnode_t
26927c478bd9Sstevel@tonic-gate di_link_to_lnode(di_link_t link, uint_t endpoint)
26937c478bd9Sstevel@tonic-gate {
26947c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	if ((link == DI_LINK_NIL) ||
26977c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
26987c478bd9Sstevel@tonic-gate 		errno = EINVAL;
26997c478bd9Sstevel@tonic-gate 		return (DI_LNODE_NIL);
27007c478bd9Sstevel@tonic-gate 	}
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)link - DI_LINK(link)->self);
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC) {
27057c478bd9Sstevel@tonic-gate 		return (DI_LNODE((caddr_t)di_all + DI_LINK(link)->src_lnode));
27067c478bd9Sstevel@tonic-gate 	} else {
27077c478bd9Sstevel@tonic-gate 		return (DI_LNODE((caddr_t)di_all + DI_LINK(link)->tgt_lnode));
27087c478bd9Sstevel@tonic-gate 	}
27097c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate char *
27137c478bd9Sstevel@tonic-gate di_lnode_name(di_lnode_t lnode)
27147c478bd9Sstevel@tonic-gate {
27157c478bd9Sstevel@tonic-gate 	return (di_driver_name(di_lnode_devinfo(lnode)));
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate di_node_t
27197c478bd9Sstevel@tonic-gate di_lnode_devinfo(di_lnode_t lnode)
27207c478bd9Sstevel@tonic-gate {
27217c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)lnode - DI_LNODE(lnode)->self);
27247c478bd9Sstevel@tonic-gate 	return (DI_NODE((caddr_t)di_all + DI_LNODE(lnode)->node));
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate int
27287c478bd9Sstevel@tonic-gate di_lnode_devt(di_lnode_t lnode, dev_t *devt)
27297c478bd9Sstevel@tonic-gate {
27307c478bd9Sstevel@tonic-gate 	if ((lnode == DI_LNODE_NIL) || (devt == NULL)) {
27317c478bd9Sstevel@tonic-gate 		errno = EINVAL;
27327c478bd9Sstevel@tonic-gate 		return (-1);
27337c478bd9Sstevel@tonic-gate 	}
27347c478bd9Sstevel@tonic-gate 	if ((DI_LNODE(lnode)->dev_major == (major_t)-1) &&
27357c478bd9Sstevel@tonic-gate 	    (DI_LNODE(lnode)->dev_minor == (minor_t)-1))
27367c478bd9Sstevel@tonic-gate 		return (-1);
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	*devt = makedev(DI_LNODE(lnode)->dev_major, DI_LNODE(lnode)->dev_minor);
27397c478bd9Sstevel@tonic-gate 	return (0);
27407c478bd9Sstevel@tonic-gate }
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate int
27437c478bd9Sstevel@tonic-gate di_link_spectype(di_link_t link)
27447c478bd9Sstevel@tonic-gate {
27457c478bd9Sstevel@tonic-gate 	return (DI_LINK(link)->spec_type);
27467c478bd9Sstevel@tonic-gate }
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate void
27497c478bd9Sstevel@tonic-gate di_minor_private_set(di_minor_t minor, void *data)
27507c478bd9Sstevel@tonic-gate {
27517c478bd9Sstevel@tonic-gate 	DI_MINOR(minor)->user_private_data = (uintptr_t)data;
27527c478bd9Sstevel@tonic-gate }
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate void *
27557c478bd9Sstevel@tonic-gate di_minor_private_get(di_minor_t minor)
27567c478bd9Sstevel@tonic-gate {
27578309866bSanish 	return ((void *)(uintptr_t)DI_MINOR(minor)->user_private_data);
27587c478bd9Sstevel@tonic-gate }
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate void
27617c478bd9Sstevel@tonic-gate di_node_private_set(di_node_t node, void *data)
27627c478bd9Sstevel@tonic-gate {
27637c478bd9Sstevel@tonic-gate 	DI_NODE(node)->user_private_data = (uintptr_t)data;
27647c478bd9Sstevel@tonic-gate }
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate void *
27677c478bd9Sstevel@tonic-gate di_node_private_get(di_node_t node)
27687c478bd9Sstevel@tonic-gate {
27698309866bSanish 	return ((void *)(uintptr_t)DI_NODE(node)->user_private_data);
27707c478bd9Sstevel@tonic-gate }
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate void
27737c478bd9Sstevel@tonic-gate di_lnode_private_set(di_lnode_t lnode, void *data)
27747c478bd9Sstevel@tonic-gate {
27757c478bd9Sstevel@tonic-gate 	DI_LNODE(lnode)->user_private_data = (uintptr_t)data;
27767c478bd9Sstevel@tonic-gate }
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate void *
27797c478bd9Sstevel@tonic-gate di_lnode_private_get(di_lnode_t lnode)
27807c478bd9Sstevel@tonic-gate {
27818309866bSanish 	return ((void *)(uintptr_t)DI_LNODE(lnode)->user_private_data);
27827c478bd9Sstevel@tonic-gate }
27837c478bd9Sstevel@tonic-gate 
27847c478bd9Sstevel@tonic-gate void
27857c478bd9Sstevel@tonic-gate di_link_private_set(di_link_t link, void *data)
27867c478bd9Sstevel@tonic-gate {
27877c478bd9Sstevel@tonic-gate 	DI_LINK(link)->user_private_data = (uintptr_t)data;
27887c478bd9Sstevel@tonic-gate }
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate void *
27917c478bd9Sstevel@tonic-gate di_link_private_get(di_link_t link)
27927c478bd9Sstevel@tonic-gate {
27938309866bSanish 	return ((void *)(uintptr_t)DI_LINK(link)->user_private_data);
27947c478bd9Sstevel@tonic-gate }
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate di_lnode_t
27977c478bd9Sstevel@tonic-gate di_lnode_next(di_node_t node, di_lnode_t lnode)
27987c478bd9Sstevel@tonic-gate {
27997c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	/*
28027c478bd9Sstevel@tonic-gate 	 * paranoid error checking
28037c478bd9Sstevel@tonic-gate 	 */
28047c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
28057c478bd9Sstevel@tonic-gate 		errno = EINVAL;
28067c478bd9Sstevel@tonic-gate 		return (DI_LNODE_NIL);
28077c478bd9Sstevel@tonic-gate 	}
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)node - DI_NODE(node)->self);
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 	if (lnode == DI_NODE_NIL) {
28127c478bd9Sstevel@tonic-gate 		if (DI_NODE(node)->lnodes != NULL)
28137c478bd9Sstevel@tonic-gate 			return (DI_LNODE((caddr_t)di_all +
28147c478bd9Sstevel@tonic-gate 			    DI_NODE(node)->lnodes));
28157c478bd9Sstevel@tonic-gate 	} else {
28167c478bd9Sstevel@tonic-gate 		if (DI_LNODE(lnode)->node_next != NULL)
28177c478bd9Sstevel@tonic-gate 			return (DI_LNODE((caddr_t)di_all +
28187c478bd9Sstevel@tonic-gate 			    DI_LNODE(lnode)->node_next));
28197c478bd9Sstevel@tonic-gate 	}
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	if (DINFOLYR & DI_ALL(di_all)->command)
28227c478bd9Sstevel@tonic-gate 		errno = ENXIO;
28237c478bd9Sstevel@tonic-gate 	else
28247c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	return (DI_LNODE_NIL);
28277c478bd9Sstevel@tonic-gate }
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate di_link_t
28307c478bd9Sstevel@tonic-gate di_link_next_by_node(di_node_t node, di_link_t link, uint_t endpoint)
28317c478bd9Sstevel@tonic-gate {
28327c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate 	/*
28357c478bd9Sstevel@tonic-gate 	 * paranoid error checking
28367c478bd9Sstevel@tonic-gate 	 */
28377c478bd9Sstevel@tonic-gate 	if ((node == DI_NODE_NIL) ||
28387c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
28397c478bd9Sstevel@tonic-gate 		errno = EINVAL;
28407c478bd9Sstevel@tonic-gate 		return (DI_LINK_NIL);
28417c478bd9Sstevel@tonic-gate 	}
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)node - DI_NODE(node)->self);
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC) {
28467c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
28477c478bd9Sstevel@tonic-gate 			if (DI_NODE(node)->src_links != NULL)
28487c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
28497c478bd9Sstevel@tonic-gate 				    DI_NODE(node)->src_links));
28507c478bd9Sstevel@tonic-gate 		} else {
28517c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->src_node_next != NULL)
28527c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
28537c478bd9Sstevel@tonic-gate 				    DI_LINK(link)->src_node_next));
28547c478bd9Sstevel@tonic-gate 		}
28557c478bd9Sstevel@tonic-gate 	} else {
28567c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
28577c478bd9Sstevel@tonic-gate 			if (DI_NODE(node)->tgt_links != NULL)
28587c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
28597c478bd9Sstevel@tonic-gate 				    DI_NODE(node)->tgt_links));
28607c478bd9Sstevel@tonic-gate 		} else {
28617c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->tgt_node_next != NULL)
28627c478bd9Sstevel@tonic-gate 				return (DI_LINK((caddr_t)di_all +
28637c478bd9Sstevel@tonic-gate 				    DI_LINK(link)->tgt_node_next));
28647c478bd9Sstevel@tonic-gate 		}
28657c478bd9Sstevel@tonic-gate 	}
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	if (DINFOLYR & DI_ALL(di_all)->command)
28687c478bd9Sstevel@tonic-gate 		errno = ENXIO;
28697c478bd9Sstevel@tonic-gate 	else
28707c478bd9Sstevel@tonic-gate 		errno = ENOTSUP;
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	return (DI_LINK_NIL);
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate di_link_t
28767c478bd9Sstevel@tonic-gate di_link_next_by_lnode(di_lnode_t lnode, di_link_t link, uint_t endpoint)
28777c478bd9Sstevel@tonic-gate {
28787c478bd9Sstevel@tonic-gate 	struct di_all *di_all;
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	/*
28817c478bd9Sstevel@tonic-gate 	 * paranoid error checking
28827c478bd9Sstevel@tonic-gate 	 */
28837c478bd9Sstevel@tonic-gate 	if ((lnode == DI_LNODE_NIL) ||
28847c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
28857c478bd9Sstevel@tonic-gate 		errno = EINVAL;
28867c478bd9Sstevel@tonic-gate 		return (DI_LINK_NIL);
28877c478bd9Sstevel@tonic-gate 	}
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	di_all = DI_ALL((caddr_t)lnode - DI_LNODE(lnode)->self);
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 	if (endpoint == DI_LINK_SRC) {
28927c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
28937c478bd9Sstevel@tonic-gate 			if (DI_LNODE(lnode)->link_out == NULL)
28947c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
28957c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
28967c478bd9Sstevel@tonic-gate 				    DI_LNODE(lnode)->link_out));
28977c478bd9Sstevel@tonic-gate 		} else {
28987c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->src_link_next == NULL)
28997c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
29007c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
29017c478bd9Sstevel@tonic-gate 				    DI_LINK(link)->src_link_next));
29027c478bd9Sstevel@tonic-gate 		}
29037c478bd9Sstevel@tonic-gate 	} else {
29047c478bd9Sstevel@tonic-gate 		if (link == DI_LINK_NIL) {
29057c478bd9Sstevel@tonic-gate 			if (DI_LNODE(lnode)->link_in == NULL)
29067c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
29077c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
29087c478bd9Sstevel@tonic-gate 				    DI_LNODE(lnode)->link_in));
29097c478bd9Sstevel@tonic-gate 		} else {
29107c478bd9Sstevel@tonic-gate 			if (DI_LINK(link)->tgt_link_next == NULL)
29117c478bd9Sstevel@tonic-gate 				return (DI_LINK_NIL);
29127c478bd9Sstevel@tonic-gate 			return (DI_LINK((caddr_t)di_all +
29137c478bd9Sstevel@tonic-gate 				    DI_LINK(link)->tgt_link_next));
29147c478bd9Sstevel@tonic-gate 		}
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
29177c478bd9Sstevel@tonic-gate }
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate /*
29207c478bd9Sstevel@tonic-gate  * Internal library function:
29217c478bd9Sstevel@tonic-gate  *   Invoke callback for each link data on the link list of first node
29227c478bd9Sstevel@tonic-gate  *   on node_list headp, and place children of first node on the list.
29237c478bd9Sstevel@tonic-gate  *
29247c478bd9Sstevel@tonic-gate  *   This is similar to walk_one_node, except we only walk in child
29257c478bd9Sstevel@tonic-gate  *   first mode.
29267c478bd9Sstevel@tonic-gate  */
29277c478bd9Sstevel@tonic-gate static void
29287c478bd9Sstevel@tonic-gate walk_one_link(struct node_list **headp, uint_t ep,
29297c478bd9Sstevel@tonic-gate     void *arg, int (*callback)(di_link_t link, void *arg))
29307c478bd9Sstevel@tonic-gate {
29317c478bd9Sstevel@tonic-gate 	int		action = DI_WALK_CONTINUE;
29327c478bd9Sstevel@tonic-gate 	di_link_t	link = DI_LINK_NIL;
29337c478bd9Sstevel@tonic-gate 	di_node_t	node = (*headp)->node;
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	while ((link = di_link_next_by_node(node, link, ep)) != DI_LINK_NIL) {
29367c478bd9Sstevel@tonic-gate 		action = callback(link, arg);
29377c478bd9Sstevel@tonic-gate 		if (action == DI_WALK_TERMINATE) {
29387c478bd9Sstevel@tonic-gate 			break;
29397c478bd9Sstevel@tonic-gate 		}
29407c478bd9Sstevel@tonic-gate 	}
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	update_node_list(action, DI_WALK_LINKGEN, headp);
29437c478bd9Sstevel@tonic-gate }
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate int
29467c478bd9Sstevel@tonic-gate di_walk_link(di_node_t root, uint_t flag, uint_t endpoint, void *arg,
29477c478bd9Sstevel@tonic-gate     int (*link_callback)(di_link_t link, void *arg))
29487c478bd9Sstevel@tonic-gate {
29497c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate #ifdef DEBUG
29527c478bd9Sstevel@tonic-gate 	char *path = di_devfs_path(root);
29537c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "walking %s link data under %s\n",
29547c478bd9Sstevel@tonic-gate 		    (endpoint == DI_LINK_SRC) ? "src" : "tgt", path));
29557c478bd9Sstevel@tonic-gate 	di_devfs_path_free(path);
29567c478bd9Sstevel@tonic-gate #endif
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate 	/*
29597c478bd9Sstevel@tonic-gate 	 * paranoid error checking
29607c478bd9Sstevel@tonic-gate 	 */
29617c478bd9Sstevel@tonic-gate 	if ((root == DI_NODE_NIL) || (link_callback == NULL) || (flag != 0) ||
29627c478bd9Sstevel@tonic-gate 	    ((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
29637c478bd9Sstevel@tonic-gate 		errno = EINVAL;
29647c478bd9Sstevel@tonic-gate 		return (-1);
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
29687c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
29697c478bd9Sstevel@tonic-gate 		return (-1);
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	head->next = NULL;
29737c478bd9Sstevel@tonic-gate 	head->node = root;
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start link data walking from node %s\n",
29767c478bd9Sstevel@tonic-gate 		di_node_name(root)));
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	while (head != NULL)
29797c478bd9Sstevel@tonic-gate 		walk_one_link(&head, endpoint, arg, link_callback);
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	return (0);
29827c478bd9Sstevel@tonic-gate }
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate /*
29857c478bd9Sstevel@tonic-gate  * Internal library function:
29867c478bd9Sstevel@tonic-gate  *   Invoke callback for each link data on the link list of first node
29877c478bd9Sstevel@tonic-gate  *   on node_list headp, and place children of first node on the list.
29887c478bd9Sstevel@tonic-gate  *
29897c478bd9Sstevel@tonic-gate  *   This is similar to walk_one_node, except we only walk in child
29907c478bd9Sstevel@tonic-gate  *   first mode.
29917c478bd9Sstevel@tonic-gate  */
29927c478bd9Sstevel@tonic-gate static void
29937c478bd9Sstevel@tonic-gate walk_one_lnode(struct node_list **headp, void *arg,
29947c478bd9Sstevel@tonic-gate     int (*callback)(di_lnode_t lnode, void *arg))
29957c478bd9Sstevel@tonic-gate {
29967c478bd9Sstevel@tonic-gate 	int		action = DI_WALK_CONTINUE;
29977c478bd9Sstevel@tonic-gate 	di_lnode_t	lnode = DI_LNODE_NIL;
29987c478bd9Sstevel@tonic-gate 	di_node_t	node = (*headp)->node;
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 	while ((lnode = di_lnode_next(node, lnode)) != DI_LNODE_NIL) {
30017c478bd9Sstevel@tonic-gate 		action = callback(lnode, arg);
30027c478bd9Sstevel@tonic-gate 		if (action == DI_WALK_TERMINATE) {
30037c478bd9Sstevel@tonic-gate 			break;
30047c478bd9Sstevel@tonic-gate 		}
30057c478bd9Sstevel@tonic-gate 	}
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	update_node_list(action, DI_WALK_LINKGEN, headp);
30087c478bd9Sstevel@tonic-gate }
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate int
30117c478bd9Sstevel@tonic-gate di_walk_lnode(di_node_t root, uint_t flag, void *arg,
30127c478bd9Sstevel@tonic-gate     int (*lnode_callback)(di_lnode_t lnode, void *arg))
30137c478bd9Sstevel@tonic-gate {
30147c478bd9Sstevel@tonic-gate 	struct node_list  *head;	/* node_list for tree walk */
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate #ifdef DEBUG
30177c478bd9Sstevel@tonic-gate 	char *path = di_devfs_path(root);
30187c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "walking lnode data under %s\n", path));
30197c478bd9Sstevel@tonic-gate 	di_devfs_path_free(path);
30207c478bd9Sstevel@tonic-gate #endif
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	/*
30237c478bd9Sstevel@tonic-gate 	 * paranoid error checking
30247c478bd9Sstevel@tonic-gate 	 */
30257c478bd9Sstevel@tonic-gate 	if ((root == DI_NODE_NIL) || (lnode_callback == NULL) || (flag != 0)) {
30267c478bd9Sstevel@tonic-gate 		errno = EINVAL;
30277c478bd9Sstevel@tonic-gate 		return (-1);
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	if ((head = malloc(sizeof (struct node_list))) == NULL) {
30317c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "malloc of node_list failed\n"));
30327c478bd9Sstevel@tonic-gate 		return (-1);
30337c478bd9Sstevel@tonic-gate 	}
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	head->next = NULL;
30367c478bd9Sstevel@tonic-gate 	head->node = root;
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	DPRINTF((DI_INFO, "Start lnode data walking from node %s\n",
30397c478bd9Sstevel@tonic-gate 		di_node_name(root)));
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	while (head != NULL)
30427c478bd9Sstevel@tonic-gate 		walk_one_lnode(&head, arg, lnode_callback);
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	return (0);
30457c478bd9Sstevel@tonic-gate }
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate di_node_t
30487c478bd9Sstevel@tonic-gate di_lookup_node(di_node_t root, char *path)
30497c478bd9Sstevel@tonic-gate {
30507c478bd9Sstevel@tonic-gate 	struct di_all *dap;
30517c478bd9Sstevel@tonic-gate 	di_node_t node;
30527c478bd9Sstevel@tonic-gate 	char copy[MAXPATHLEN];
30537c478bd9Sstevel@tonic-gate 	char *slash, *pname, *paddr;
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	/*
30567c478bd9Sstevel@tonic-gate 	 * Path must be absolute and musn't have duplicate slashes
30577c478bd9Sstevel@tonic-gate 	 */
30587c478bd9Sstevel@tonic-gate 	if (*path != '/' || strstr(path, "//")) {
30597c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "Invalid path: %s\n", path));
30607c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
30617c478bd9Sstevel@tonic-gate 	}
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	if (root == DI_NODE_NIL) {
30647c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "root node is DI_NODE_NIL\n"));
30657c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
30667c478bd9Sstevel@tonic-gate 	}
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	dap = DI_ALL((caddr_t)root - DI_NODE(root)->self);
30697c478bd9Sstevel@tonic-gate 	if (strcmp(dap->root_path, "/") != 0) {
30707c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "snapshot root not / : %s\n", dap->root_path));
30717c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
30727c478bd9Sstevel@tonic-gate 	}
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	if (strlcpy(copy, path, sizeof (copy)) >= sizeof (copy)) {
30757c478bd9Sstevel@tonic-gate 		DPRINTF((DI_ERR, "path too long: %s\n", path));
30767c478bd9Sstevel@tonic-gate 		return (DI_NODE_NIL);
30777c478bd9Sstevel@tonic-gate 	}
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	for (slash = copy, node = root; slash; ) {
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 		/*
30827c478bd9Sstevel@tonic-gate 		 * Handle path = "/" case as well as trailing '/'
30837c478bd9Sstevel@tonic-gate 		 */
30847c478bd9Sstevel@tonic-gate 		if (*(slash + 1) == '\0')
30857c478bd9Sstevel@tonic-gate 			break;
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 		/*
30887c478bd9Sstevel@tonic-gate 		 * More path-components exist. Deal with the next one
30897c478bd9Sstevel@tonic-gate 		 */
30907c478bd9Sstevel@tonic-gate 		pname = slash + 1;
30917c478bd9Sstevel@tonic-gate 		node = di_child_node(node);
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 		if (slash = strchr(pname, '/'))
30947c478bd9Sstevel@tonic-gate 			*slash = '\0';
30957c478bd9Sstevel@tonic-gate 		if (paddr = strchr(pname, '@'))
30967c478bd9Sstevel@tonic-gate 			*paddr++ = '\0';
30977c478bd9Sstevel@tonic-gate 
30987c478bd9Sstevel@tonic-gate 		for (; node != DI_NODE_NIL; node = di_sibling_node(node)) {
30997c478bd9Sstevel@tonic-gate 			char *name, *baddr;
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 			name = di_node_name(node);
31027c478bd9Sstevel@tonic-gate 			baddr = di_bus_addr(node);
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 			if (strcmp(pname, name) != 0)
31057c478bd9Sstevel@tonic-gate 				continue;
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 			/*
31087c478bd9Sstevel@tonic-gate 			 * Mappings between a "path-address" and bus-addr
31097c478bd9Sstevel@tonic-gate 			 *
31107c478bd9Sstevel@tonic-gate 			 *	paddr		baddr
31117c478bd9Sstevel@tonic-gate 			 *	---------------------
31127c478bd9Sstevel@tonic-gate 			 *	NULL		NULL
31137c478bd9Sstevel@tonic-gate 			 *	NULL		""
31147c478bd9Sstevel@tonic-gate 			 *	""		N/A	(invalid paddr)
31157c478bd9Sstevel@tonic-gate 			 */
31167c478bd9Sstevel@tonic-gate 			if (paddr && baddr && strcmp(paddr, baddr) == 0)
31177c478bd9Sstevel@tonic-gate 				break;
31187c478bd9Sstevel@tonic-gate 			if (paddr == NULL && (baddr == NULL || *baddr == '\0'))
31197c478bd9Sstevel@tonic-gate 				break;
31207c478bd9Sstevel@tonic-gate 		}
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 		/*
31237c478bd9Sstevel@tonic-gate 		 * No nodes in the sibling list or there was no match
31247c478bd9Sstevel@tonic-gate 		 */
31257c478bd9Sstevel@tonic-gate 		if (node == DI_NODE_NIL) {
31267c478bd9Sstevel@tonic-gate 			DPRINTF((DI_ERR, "%s@%s: no node\n", pname, paddr));
31277c478bd9Sstevel@tonic-gate 			return (DI_NODE_NIL);
31287c478bd9Sstevel@tonic-gate 		}
31297c478bd9Sstevel@tonic-gate 	}
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	assert(node != DI_NODE_NIL);
31327c478bd9Sstevel@tonic-gate 	return (node);
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate static char *
31367c478bd9Sstevel@tonic-gate msglevel2str(di_debug_t msglevel)
31377c478bd9Sstevel@tonic-gate {
31387c478bd9Sstevel@tonic-gate 	switch (msglevel) {
31397c478bd9Sstevel@tonic-gate 		case DI_ERR:
31407c478bd9Sstevel@tonic-gate 			return ("ERROR");
31417c478bd9Sstevel@tonic-gate 		case DI_INFO:
31427c478bd9Sstevel@tonic-gate 			return ("Info");
31437c478bd9Sstevel@tonic-gate 		case DI_TRACE:
31447c478bd9Sstevel@tonic-gate 			return ("Trace");
31457c478bd9Sstevel@tonic-gate 		case DI_TRACE1:
31467c478bd9Sstevel@tonic-gate 			return ("Trace1");
31477c478bd9Sstevel@tonic-gate 		case DI_TRACE2:
31487c478bd9Sstevel@tonic-gate 			return ("Trace2");
31497c478bd9Sstevel@tonic-gate 		default:
31507c478bd9Sstevel@tonic-gate 			return ("UNKNOWN");
31517c478bd9Sstevel@tonic-gate 	}
31527c478bd9Sstevel@tonic-gate }
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate void
31557c478bd9Sstevel@tonic-gate dprint(di_debug_t msglevel, const char *fmt, ...)
31567c478bd9Sstevel@tonic-gate {
31577c478bd9Sstevel@tonic-gate 	va_list	ap;
31587c478bd9Sstevel@tonic-gate 	char	*estr;
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	if (di_debug <= DI_QUIET)
31617c478bd9Sstevel@tonic-gate 		return;
31627c478bd9Sstevel@tonic-gate 
31637c478bd9Sstevel@tonic-gate 	if (di_debug < msglevel)
31647c478bd9Sstevel@tonic-gate 		return;
31657c478bd9Sstevel@tonic-gate 
31667c478bd9Sstevel@tonic-gate 	estr = msglevel2str(msglevel);
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	assert(estr);
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
31717c478bd9Sstevel@tonic-gate 
31727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "libdevinfo[%lu]: %s: ",
31737c478bd9Sstevel@tonic-gate 	    (ulong_t)getpid(), estr);
31747c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	va_end(ap);
31777c478bd9Sstevel@tonic-gate }
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate /* end of devinfo.c */
3180