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
561db5dcaSrd  * Common Development and Distribution License (the "License").
661db5dcaSrd  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22bdee4892SMichael Bergknoff  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * PICL plug-in that creates device tree nodes for all platforms
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <assert.h>
367c478bd9Sstevel@tonic-gate #include <alloca.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <stropts.h>
397c478bd9Sstevel@tonic-gate #include <syslog.h>
407c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
417c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
427c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
437c478bd9Sstevel@tonic-gate #include <sys/time.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <picl.h>
467c478bd9Sstevel@tonic-gate #include <picltree.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/processor.h>
497c478bd9Sstevel@tonic-gate #include <kstat.h>
507c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
517c478bd9Sstevel@tonic-gate #include <dirent.h>
527c478bd9Sstevel@tonic-gate #include <libintl.h>
537c478bd9Sstevel@tonic-gate #include <pthread.h>
547c478bd9Sstevel@tonic-gate #include <libnvpair.h>
557c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
567c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
577c478bd9Sstevel@tonic-gate #include <sys/obpdefs.h>
587c478bd9Sstevel@tonic-gate #include <sys/openpromio.h>
597c478bd9Sstevel@tonic-gate #include "picldevtree.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Plugin registration entry points
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static void	picldevtree_register(void);
657c478bd9Sstevel@tonic-gate static void	picldevtree_init(void);
667c478bd9Sstevel@tonic-gate static void	picldevtree_fini(void);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static void	picldevtree_evhandler(const char *ename, const void *earg,
697c478bd9Sstevel@tonic-gate 		    size_t size, void *cookie);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #pragma	init(picldevtree_register)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Log message texts
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate #define	DEVINFO_PLUGIN_INIT_FAILED	gettext("SUNW_picldevtree failed!\n")
777c478bd9Sstevel@tonic-gate #define	PICL_EVENT_DROPPED	\
787c478bd9Sstevel@tonic-gate 	gettext("SUNW_picldevtree '%s' event dropped.\n")
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Macro to get PCI device id (from IEEE 1275 spec)
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate #define	PCI_DEVICE_ID(x)			(((x) >> 11) & 0x1f)
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Local variables
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static picld_plugin_reg_t  my_reg_info = {
887c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_VERSION_1,
897c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_CRITICAL,
907c478bd9Sstevel@tonic-gate 	"SUNW_picldevtree",
917c478bd9Sstevel@tonic-gate 	picldevtree_init,
927c478bd9Sstevel@tonic-gate 	picldevtree_fini
937c478bd9Sstevel@tonic-gate };
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Debug enabling environment variable
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #define	SUNW_PICLDEVTREE_PLUGIN_DEBUG	"SUNW_PICLDEVTREE_PLUGIN_DEBUG"
997c478bd9Sstevel@tonic-gate static	int		picldevtree_debug = 0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static	conf_entries_t 	*conf_name_class_map = NULL;
1027c478bd9Sstevel@tonic-gate static	builtin_map_t	sun4u_map[] = {
1037c478bd9Sstevel@tonic-gate 	/* MAX_NAMEVAL_SIZE */
1047c478bd9Sstevel@tonic-gate 	{ "SUNW,bpp", PICL_CLASS_PARALLEL},
1057c478bd9Sstevel@tonic-gate 	{ "parallel", PICL_CLASS_PARALLEL},
1067c478bd9Sstevel@tonic-gate 	{ "floppy", PICL_CLASS_FLOPPY},
1077c478bd9Sstevel@tonic-gate 	{ "memory", PICL_CLASS_MEMORY},
1087c478bd9Sstevel@tonic-gate 	{ "ebus", PICL_CLASS_EBUS},
1097c478bd9Sstevel@tonic-gate 	{ "i2c", PICL_CLASS_I2C},
1107c478bd9Sstevel@tonic-gate 	{ "usb", PICL_CLASS_USB},
1117c478bd9Sstevel@tonic-gate 	{ "isa", PICL_CLASS_ISA},
1127c478bd9Sstevel@tonic-gate 	{ "dma", PICL_CLASS_DMA},
1137c478bd9Sstevel@tonic-gate 	{ "keyboard", PICL_CLASS_KEYBOARD},
1147c478bd9Sstevel@tonic-gate 	{ "mouse", PICL_CLASS_MOUSE},
1157c478bd9Sstevel@tonic-gate 	{ "fan-control", PICL_CLASS_FAN_CONTROL},
1167c478bd9Sstevel@tonic-gate 	{ "sc", PICL_CLASS_SYSTEM_CONTROLLER},
1177c478bd9Sstevel@tonic-gate 	{ "dimm", PICL_CLASS_SEEPROM},
1187c478bd9Sstevel@tonic-gate 	{ "dimm-fru", PICL_CLASS_SEEPROM},
1197c478bd9Sstevel@tonic-gate 	{ "cpu", PICL_CLASS_SEEPROM},
1207c478bd9Sstevel@tonic-gate 	{ "cpu-fru", PICL_CLASS_SEEPROM},
1217c478bd9Sstevel@tonic-gate 	{ "flashprom", PICL_CLASS_FLASHPROM},
1227c478bd9Sstevel@tonic-gate 	{ "temperature", PICL_CLASS_TEMPERATURE_DEVICE},
1237c478bd9Sstevel@tonic-gate 	{ "motherboard", PICL_CLASS_SEEPROM},
1247c478bd9Sstevel@tonic-gate 	{ "motherboard-fru", PICL_CLASS_SEEPROM},
1257c478bd9Sstevel@tonic-gate 	{ "motherboard-fru-prom", PICL_CLASS_SEEPROM},
1267c478bd9Sstevel@tonic-gate 	{ "pmu", PICL_CLASS_PMU},
1277c478bd9Sstevel@tonic-gate 	{ "sound", PICL_CLASS_SOUND},
1287c478bd9Sstevel@tonic-gate 	{ "firewire", PICL_CLASS_FIREWIRE},
1297c478bd9Sstevel@tonic-gate 	{ "i2c-at34c02", PICL_CLASS_SEEPROM},
1307c478bd9Sstevel@tonic-gate 	{ "hardware-monitor", PICL_CLASS_HARDWARE_MONITOR},
1317c478bd9Sstevel@tonic-gate 	{ "", ""}
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate static	builtin_map_t	i86pc_map[] = {
1347c478bd9Sstevel@tonic-gate 	/* MAX_NAMEVAL_SIZE */
1357c478bd9Sstevel@tonic-gate 	{ "cpus", PICL_CLASS_I86CPUS},
1367c478bd9Sstevel@tonic-gate 	{ "cpu", PICL_CLASS_CPU},
1377c478bd9Sstevel@tonic-gate 	{ "memory", PICL_CLASS_MEMORY},
1387c478bd9Sstevel@tonic-gate 	{ "asy", PICL_CLASS_SERIAL},
1397c478bd9Sstevel@tonic-gate 	{ "", ""}
1407c478bd9Sstevel@tonic-gate };
1417c478bd9Sstevel@tonic-gate static	pname_type_map_t	pname_type_map[] = {
1427c478bd9Sstevel@tonic-gate 	{ "reg", PICL_PTYPE_BYTEARRAY},
1437c478bd9Sstevel@tonic-gate 	{ "device_type", PICL_PTYPE_CHARSTRING},
1447c478bd9Sstevel@tonic-gate 	{ "ranges", PICL_PTYPE_BYTEARRAY},
1457c478bd9Sstevel@tonic-gate 	{ "status", PICL_PTYPE_CHARSTRING},
1467c478bd9Sstevel@tonic-gate 	{ "compatible", PICL_PTYPE_CHARSTRING},
1477c478bd9Sstevel@tonic-gate 	{ "interrupts", PICL_PTYPE_BYTEARRAY},
1487c478bd9Sstevel@tonic-gate 	{ "model", PICL_PTYPE_CHARSTRING},
1497c478bd9Sstevel@tonic-gate 	{ "address", PICL_PTYPE_BYTEARRAY},
1507c478bd9Sstevel@tonic-gate 	{ "vendor-id", PICL_PTYPE_UNSIGNED_INT},
1517c478bd9Sstevel@tonic-gate 	{ "device-id", PICL_PTYPE_UNSIGNED_INT},
1527c478bd9Sstevel@tonic-gate 	{ "revision-id", PICL_PTYPE_UNSIGNED_INT},
1537c478bd9Sstevel@tonic-gate 	{ "class-code", PICL_PTYPE_UNSIGNED_INT},
1547c478bd9Sstevel@tonic-gate 	{ "min-grant", PICL_PTYPE_UNSIGNED_INT},
1557c478bd9Sstevel@tonic-gate 	{ "max-latency", PICL_PTYPE_UNSIGNED_INT},
1567c478bd9Sstevel@tonic-gate 	{ "devsel-speed", PICL_PTYPE_UNSIGNED_INT},
1577c478bd9Sstevel@tonic-gate 	{ "subsystem-id", PICL_PTYPE_UNSIGNED_INT},
1587c478bd9Sstevel@tonic-gate 	{ "subsystem-vendor-id", PICL_PTYPE_UNSIGNED_INT},
1597c478bd9Sstevel@tonic-gate 	{ "assigned-addresses", PICL_PTYPE_BYTEARRAY},
1607c478bd9Sstevel@tonic-gate 	{ "configuration#", PICL_PTYPE_UNSIGNED_INT},
1617c478bd9Sstevel@tonic-gate 	{ "assigned-address", PICL_PTYPE_UNSIGNED_INT},
1627c478bd9Sstevel@tonic-gate 	{ "#address-cells", PICL_PTYPE_UNSIGNED_INT},
1637c478bd9Sstevel@tonic-gate 	{ "#size-cells", PICL_PTYPE_UNSIGNED_INT},
1647c478bd9Sstevel@tonic-gate 	{ "clock-frequency", PICL_PTYPE_UNSIGNED_INT},
1657c478bd9Sstevel@tonic-gate 	{ "scsi-initiator-id", PICL_PTYPE_UNSIGNED_INT},
1667c478bd9Sstevel@tonic-gate 	{ "differential", PICL_PTYPE_UNSIGNED_INT},
1677c478bd9Sstevel@tonic-gate 	{ "idprom", PICL_PTYPE_BYTEARRAY},
1687c478bd9Sstevel@tonic-gate 	{ "bus-range", PICL_PTYPE_BYTEARRAY},
1697c478bd9Sstevel@tonic-gate 	{ "alternate-reg", PICL_PTYPE_BYTEARRAY},
1707c478bd9Sstevel@tonic-gate 	{ "power-consumption", PICL_PTYPE_BYTEARRAY},
1717c478bd9Sstevel@tonic-gate 	{ "slot-names", PICL_PTYPE_BYTEARRAY},
1727c478bd9Sstevel@tonic-gate 	{ "burst-sizes", PICL_PTYPE_UNSIGNED_INT},
1737c478bd9Sstevel@tonic-gate 	{ "up-burst-sizes", PICL_PTYPE_UNSIGNED_INT},
1747c478bd9Sstevel@tonic-gate 	{ "slot-address-bits", PICL_PTYPE_UNSIGNED_INT},
1757c478bd9Sstevel@tonic-gate 	{ "eisa-slots", PICL_PTYPE_BYTEARRAY},
1767c478bd9Sstevel@tonic-gate 	{ "dma", PICL_PTYPE_BYTEARRAY},
1777c478bd9Sstevel@tonic-gate 	{ "slot-names-index", PICL_PTYPE_UNSIGNED_INT},
1787c478bd9Sstevel@tonic-gate 	{ "pnp-csn", PICL_PTYPE_UNSIGNED_INT},
1797c478bd9Sstevel@tonic-gate 	{ "pnp-data", PICL_PTYPE_BYTEARRAY},
1807c478bd9Sstevel@tonic-gate 	{ "description", PICL_PTYPE_CHARSTRING},
1817c478bd9Sstevel@tonic-gate 	{ "pnp-id", PICL_PTYPE_CHARSTRING},
1827c478bd9Sstevel@tonic-gate 	{ "max-frame-size", PICL_PTYPE_UNSIGNED_INT},
1837c478bd9Sstevel@tonic-gate 	{ "address-bits", PICL_PTYPE_UNSIGNED_INT},
1847c478bd9Sstevel@tonic-gate 	{ "local-mac-address", PICL_PTYPE_BYTEARRAY},
1857c478bd9Sstevel@tonic-gate 	{ "mac-address", PICL_PTYPE_BYTEARRAY},
1867c478bd9Sstevel@tonic-gate 	{ "character-set", PICL_PTYPE_CHARSTRING},
1877c478bd9Sstevel@tonic-gate 	{ "available", PICL_PTYPE_BYTEARRAY},
1887c478bd9Sstevel@tonic-gate 	{ "port-wwn", PICL_PTYPE_BYTEARRAY},
1897c478bd9Sstevel@tonic-gate 	{ "node-wwn", PICL_PTYPE_BYTEARRAY},
1907c478bd9Sstevel@tonic-gate 	{ "width", PICL_PTYPE_UNSIGNED_INT},
1917c478bd9Sstevel@tonic-gate 	{ "linebytes", PICL_PTYPE_UNSIGNED_INT},
1927c478bd9Sstevel@tonic-gate 	{ "height", PICL_PTYPE_UNSIGNED_INT},
1937c478bd9Sstevel@tonic-gate 	{ "banner-name", PICL_PTYPE_CHARSTRING},
1947c478bd9Sstevel@tonic-gate 	{ "reset-reason", PICL_PTYPE_CHARSTRING},
1957c478bd9Sstevel@tonic-gate 	{ "implementation#", PICL_PTYPE_UNSIGNED_INT},
1967c478bd9Sstevel@tonic-gate 	{ "version#", PICL_PTYPE_UNSIGNED_INT},
1977c478bd9Sstevel@tonic-gate 	{ "icache-size", PICL_PTYPE_UNSIGNED_INT},
1987c478bd9Sstevel@tonic-gate 	{ "icache-line-size", PICL_PTYPE_UNSIGNED_INT},
1997c478bd9Sstevel@tonic-gate 	{ "icache-associativity", PICL_PTYPE_UNSIGNED_INT},
2007c478bd9Sstevel@tonic-gate 	{ "l1-icache-size", PICL_PTYPE_UNSIGNED_INT},
2017c478bd9Sstevel@tonic-gate 	{ "l1-icache-line-size", PICL_PTYPE_UNSIGNED_INT},
2027c478bd9Sstevel@tonic-gate 	{ "l1-icache-associativity", PICL_PTYPE_UNSIGNED_INT},
2037c478bd9Sstevel@tonic-gate 	{ "#itlb-entries", PICL_PTYPE_UNSIGNED_INT},
2047c478bd9Sstevel@tonic-gate 	{ "dcache-size", PICL_PTYPE_UNSIGNED_INT},
2057c478bd9Sstevel@tonic-gate 	{ "dcache-line-size", PICL_PTYPE_UNSIGNED_INT},
2067c478bd9Sstevel@tonic-gate 	{ "dcache-associativity", PICL_PTYPE_UNSIGNED_INT},
2077c478bd9Sstevel@tonic-gate 	{ "l1-dcache-size", PICL_PTYPE_UNSIGNED_INT},
2087c478bd9Sstevel@tonic-gate 	{ "l1-dcache-line-size", PICL_PTYPE_UNSIGNED_INT},
2097c478bd9Sstevel@tonic-gate 	{ "l1-dcache-associativity", PICL_PTYPE_UNSIGNED_INT},
2107c478bd9Sstevel@tonic-gate 	{ "#dtlb-entries", PICL_PTYPE_UNSIGNED_INT},
2117c478bd9Sstevel@tonic-gate 	{ "ecache-size", PICL_PTYPE_UNSIGNED_INT},
2127c478bd9Sstevel@tonic-gate 	{ "ecache-line-size", PICL_PTYPE_UNSIGNED_INT},
2137c478bd9Sstevel@tonic-gate 	{ "ecache-associativity", PICL_PTYPE_UNSIGNED_INT},
2147c478bd9Sstevel@tonic-gate 	{ "l2-cache-size", PICL_PTYPE_UNSIGNED_INT},
2157c478bd9Sstevel@tonic-gate 	{ "l2-cache-line-size", PICL_PTYPE_UNSIGNED_INT},
2167c478bd9Sstevel@tonic-gate 	{ "l2-cache-associativity", PICL_PTYPE_UNSIGNED_INT},
2177c478bd9Sstevel@tonic-gate 	{ "l2-cache-sharing", PICL_PTYPE_BYTEARRAY},
2187c478bd9Sstevel@tonic-gate 	{ "mask#", PICL_PTYPE_UNSIGNED_INT},
2197c478bd9Sstevel@tonic-gate 	{ "manufacturer#", PICL_PTYPE_UNSIGNED_INT},
2207c478bd9Sstevel@tonic-gate 	{ "sparc-version", PICL_PTYPE_UNSIGNED_INT},
2217c478bd9Sstevel@tonic-gate 	{ "version", PICL_PTYPE_CHARSTRING},
2227c478bd9Sstevel@tonic-gate 	{ "cpu-model", PICL_PTYPE_UNSIGNED_INT},
2237c478bd9Sstevel@tonic-gate 	{ "memory-layout", PICL_PTYPE_BYTEARRAY},
2247c478bd9Sstevel@tonic-gate 	{ "#interrupt-cells", PICL_PTYPE_UNSIGNED_INT},
2257c478bd9Sstevel@tonic-gate 	{ "interrupt-map", PICL_PTYPE_BYTEARRAY},
2267c478bd9Sstevel@tonic-gate 	{ "interrupt-map-mask", PICL_PTYPE_BYTEARRAY}
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #define	PNAME_MAP_SIZE	sizeof (pname_type_map) / sizeof (pname_type_map_t)
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static	builtin_map_t	*builtin_map_ptr = NULL;
2327c478bd9Sstevel@tonic-gate static	int		builtin_map_size = 0;
2337c478bd9Sstevel@tonic-gate static	char		mach_name[SYS_NMLN];
2347c478bd9Sstevel@tonic-gate static	di_prom_handle_t	ph = DI_PROM_HANDLE_NIL;
235bdee4892SMichael Bergknoff static	int		snapshot_stale;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /*
2387c478bd9Sstevel@tonic-gate  * UnitAddress mapping table
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate static	unitaddr_func_t	encode_default_unitaddr;
2417c478bd9Sstevel@tonic-gate static	unitaddr_func_t	encode_optional_unitaddr;
2427c478bd9Sstevel@tonic-gate static	unitaddr_func_t	encode_scsi_unitaddr;
2437c478bd9Sstevel@tonic-gate static	unitaddr_func_t	encode_upa_unitaddr;
2447c478bd9Sstevel@tonic-gate static	unitaddr_func_t	encode_gptwo_jbus_unitaddr;
2457c478bd9Sstevel@tonic-gate static	unitaddr_func_t	encode_pci_unitaddr;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static	unitaddr_map_t unitaddr_map_table[] = {
2487c478bd9Sstevel@tonic-gate 	{PICL_CLASS_JBUS, encode_gptwo_jbus_unitaddr, 0},
2497c478bd9Sstevel@tonic-gate 	{PICL_CLASS_GPTWO, encode_gptwo_jbus_unitaddr, 0},
2507c478bd9Sstevel@tonic-gate 	{PICL_CLASS_PCI, encode_pci_unitaddr, 0},
2517ef4fcfbSvenki 	{PICL_CLASS_PCIEX, encode_pci_unitaddr, 0},
2527c478bd9Sstevel@tonic-gate 	{PICL_CLASS_UPA, encode_upa_unitaddr, 0},
2537c478bd9Sstevel@tonic-gate 	{PICL_CLASS_SCSI, encode_scsi_unitaddr, 0},
2547c478bd9Sstevel@tonic-gate 	{PICL_CLASS_SCSI2, encode_scsi_unitaddr, 0},
2557c478bd9Sstevel@tonic-gate 	{PICL_CLASS_EBUS, encode_default_unitaddr, 2},
2567c478bd9Sstevel@tonic-gate 	{PICL_CLASS_SBUS, encode_default_unitaddr, 2},
2577c478bd9Sstevel@tonic-gate 	{PICL_CLASS_I2C, encode_default_unitaddr, 2},
2587c478bd9Sstevel@tonic-gate 	{PICL_CLASS_USB, encode_default_unitaddr, 1},
2597c478bd9Sstevel@tonic-gate 	{PICL_CLASS_PMU, encode_optional_unitaddr, 2},
2607c478bd9Sstevel@tonic-gate 	{NULL, encode_default_unitaddr, 0}
2617c478bd9Sstevel@tonic-gate };
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate static int add_unitaddr_prop_to_subtree(picl_nodehdl_t nodeh);
2647c478bd9Sstevel@tonic-gate static int get_unitaddr(picl_nodehdl_t parh, picl_nodehdl_t nodeh,
2657c478bd9Sstevel@tonic-gate 	char *unitaddr, size_t ualen);
2667ef4fcfbSvenki static void set_pci_pciex_deviceid(picl_nodehdl_t plafh);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * The mc event completion handler.
2707c478bd9Sstevel@tonic-gate  * The arguments are event name buffer and a packed nvlist buffer
2717c478bd9Sstevel@tonic-gate  * with the size specifying the size of unpacked nvlist. These
2727c478bd9Sstevel@tonic-gate  * buffers are deallcoated here.
2737c478bd9Sstevel@tonic-gate  *
2747c478bd9Sstevel@tonic-gate  * Also, if a memory controller node is being removed then destroy the
2757c478bd9Sstevel@tonic-gate  * PICL subtree associated with that memory controller.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static void
2787c478bd9Sstevel@tonic-gate mc_completion_handler(char *ename, void *earg, size_t size)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	mch;
2817c478bd9Sstevel@tonic-gate 	nvlist_t	*unpack_nvl;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (strcmp(ename, PICLEVENT_MC_REMOVED) == 0 &&
2847c478bd9Sstevel@tonic-gate 	    nvlist_unpack(earg, size, &unpack_nvl, NULL) == 0) {
2857c478bd9Sstevel@tonic-gate 		mch = NULL;
2867c478bd9Sstevel@tonic-gate 		(void) nvlist_lookup_uint64(unpack_nvl,
2877c478bd9Sstevel@tonic-gate 		    PICLEVENTARG_NODEHANDLE, &mch);
2887c478bd9Sstevel@tonic-gate 		if (mch != NULL) {
2897c478bd9Sstevel@tonic-gate 			if (picldevtree_debug)
2907c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO,
2917c478bd9Sstevel@tonic-gate 				    "picldevtree: destroying_node:%llx\n",
2927c478bd9Sstevel@tonic-gate 				    mch);
2937c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(mch);
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 		nvlist_free(unpack_nvl);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	free(ename);
2997c478bd9Sstevel@tonic-gate 	free(earg);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Functions to post memory controller change event
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate static int
3067c478bd9Sstevel@tonic-gate post_mc_event(char *ename, picl_nodehdl_t mch)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	nvlist_t	*nvl;
3097c478bd9Sstevel@tonic-gate 	size_t		nvl_size;
3107c478bd9Sstevel@tonic-gate 	char		*pack_buf;
3117c478bd9Sstevel@tonic-gate 	char		*ev_name;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	ev_name = strdup(ename);
3147c478bd9Sstevel@tonic-gate 	if (ev_name == NULL)
3157c478bd9Sstevel@tonic-gate 		return (-1);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, NULL)) {
3187c478bd9Sstevel@tonic-gate 		free(ev_name);
3197c478bd9Sstevel@tonic-gate 		return (-1);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	pack_buf = NULL;
3237c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint64(nvl, PICLEVENTARG_NODEHANDLE, mch) ||
3247c478bd9Sstevel@tonic-gate 	    nvlist_pack(nvl, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
3257c478bd9Sstevel@tonic-gate 		free(ev_name);
3267c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
3277c478bd9Sstevel@tonic-gate 		return (-1);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (picldevtree_debug)
3317c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO,
3327c478bd9Sstevel@tonic-gate 		    "picldevtree: posting MC event ename:%s nodeh:%llx\n",
3337c478bd9Sstevel@tonic-gate 		    ev_name, mch);
3347c478bd9Sstevel@tonic-gate 	if (ptree_post_event(ev_name, pack_buf, nvl_size,
3357c478bd9Sstevel@tonic-gate 	    mc_completion_handler) != PICL_SUCCESS) {
3367c478bd9Sstevel@tonic-gate 		free(ev_name);
3377c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
3387c478bd9Sstevel@tonic-gate 		return (-1);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
3417c478bd9Sstevel@tonic-gate 	return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Lookup a name in the name to class map tables
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate static int
3487c478bd9Sstevel@tonic-gate lookup_name_class_map(char *classbuf, const char *nm)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	conf_entries_t	*ptr;
3517c478bd9Sstevel@tonic-gate 	int		i;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	/*
3547c478bd9Sstevel@tonic-gate 	 * check name to class mapping in conf file
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	ptr = conf_name_class_map;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	while (ptr != NULL) {
3597c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->name, nm) == 0) {
3607c478bd9Sstevel@tonic-gate 			(void) strlcpy(classbuf, ptr->piclclass,
3617c478bd9Sstevel@tonic-gate 			    PICL_CLASSNAMELEN_MAX);
3627c478bd9Sstevel@tonic-gate 			return (0);
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		ptr = ptr->next;
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * check name to class mapping in builtin table
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	if (builtin_map_ptr == NULL)
3717c478bd9Sstevel@tonic-gate 		return (-1);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	for (i = 0; i < builtin_map_size; ++i)
3747c478bd9Sstevel@tonic-gate 		if (strcmp(builtin_map_ptr[i].name, nm) == 0) {
3757c478bd9Sstevel@tonic-gate 			(void) strlcpy(classbuf, builtin_map_ptr[i].piclclass,
3767c478bd9Sstevel@tonic-gate 			    PICL_CLASSNAMELEN_MAX);
3777c478bd9Sstevel@tonic-gate 			return (0);
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 	return (-1);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Lookup a prop name in the pname to class map table
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate static int
3867c478bd9Sstevel@tonic-gate lookup_pname_type_map(const char *pname, picl_prop_type_t *type)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	int		i;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	for (i = 0; i < PNAME_MAP_SIZE; ++i)
3917c478bd9Sstevel@tonic-gate 		if (strcmp(pname_type_map[i].pname, pname) == 0) {
3927c478bd9Sstevel@tonic-gate 			*type = pname_type_map[i].type;
3937c478bd9Sstevel@tonic-gate 			return (0);
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	return (-1);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * Return the number of strings in the buffer
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate static int
4037c478bd9Sstevel@tonic-gate get_string_count(char *strdat, int length)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	int	count;
4067c478bd9Sstevel@tonic-gate 	char	*lastnull;
4077c478bd9Sstevel@tonic-gate 	char	*nullptr;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	count = 1;
4107c478bd9Sstevel@tonic-gate 	for (lastnull = &strdat[length - 1], nullptr = strchr(strdat, '\0');
4117c478bd9Sstevel@tonic-gate 	    nullptr != lastnull; nullptr = strchr(nullptr+1, '\0'))
4127c478bd9Sstevel@tonic-gate 		count++;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	return (count);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * Return 1 if the node has a "reg" property
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate static int
4217c478bd9Sstevel@tonic-gate has_reg_prop(di_node_t dn)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	int			*pdata;
4247c478bd9Sstevel@tonic-gate 	int			dret;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	dret = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, OBP_REG, &pdata);
4277c478bd9Sstevel@tonic-gate 	if (dret > 0)
4287c478bd9Sstevel@tonic-gate 		return (1);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (!ph)
4317c478bd9Sstevel@tonic-gate 		return (0);
4327c478bd9Sstevel@tonic-gate 	dret = di_prom_prop_lookup_ints(ph, dn, OBP_REG, &pdata);
4337c478bd9Sstevel@tonic-gate 	return (dret < 0 ? 0 : 1);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * This function copies a PROM node's device_type property value into the
4387c478bd9Sstevel@tonic-gate  * buffer given by outbuf. The buffer size is PICL_CLASSNAMELEN_MAX.
4397c478bd9Sstevel@tonic-gate  *
4407c478bd9Sstevel@tonic-gate  * We reclassify device_type 'fru-prom' to PICL class 'seeprom'
4417c478bd9Sstevel@tonic-gate  * for FRUID support.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate static int
4447c478bd9Sstevel@tonic-gate get_device_type(char *outbuf, di_node_t dn)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	char			*pdata;
4477c478bd9Sstevel@tonic-gate 	char			*pdatap;
4487c478bd9Sstevel@tonic-gate 	int			dret;
4497c478bd9Sstevel@tonic-gate 	int			i;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	dret = di_prop_lookup_strings(DDI_DEV_T_ANY, dn, OBP_DEVICETYPE,
4527c478bd9Sstevel@tonic-gate 	    &pdata);
4537c478bd9Sstevel@tonic-gate 	if (dret <= 0) {
4547c478bd9Sstevel@tonic-gate 		if (!ph)
4557c478bd9Sstevel@tonic-gate 			return (-1);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 		dret = di_prom_prop_lookup_strings(ph, dn, OBP_DEVICETYPE,
4587c478bd9Sstevel@tonic-gate 		    &pdata);
4597c478bd9Sstevel@tonic-gate 		if (dret <= 0) {
4607c478bd9Sstevel@tonic-gate 			return (-1);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (dret != 1) {
4657c478bd9Sstevel@tonic-gate 		/*
4667c478bd9Sstevel@tonic-gate 		 * multiple strings
4677c478bd9Sstevel@tonic-gate 		 */
4687c478bd9Sstevel@tonic-gate 		pdatap = pdata;
4697c478bd9Sstevel@tonic-gate 		for (i = 0; i < (dret - 1); ++i) {
4707c478bd9Sstevel@tonic-gate 			pdatap += strlen(pdatap);
4717c478bd9Sstevel@tonic-gate 			*pdatap = '-';	/* replace '\0' with '-' */
4727c478bd9Sstevel@tonic-gate 			pdatap++;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	if (strcasecmp(pdata, "fru-prom") == 0) {
4767c478bd9Sstevel@tonic-gate 		/*
4777c478bd9Sstevel@tonic-gate 		 * Use PICL 'seeprom' class for fru-prom device types
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		(void) strlcpy(outbuf, PICL_CLASS_SEEPROM,
4807c478bd9Sstevel@tonic-gate 		    PICL_CLASSNAMELEN_MAX);
4817c478bd9Sstevel@tonic-gate 	} else {
4827c478bd9Sstevel@tonic-gate 		(void) strlcpy(outbuf, pdata, PICL_CLASSNAMELEN_MAX);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	return (0);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * Get the minor node name in the class buffer passed
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate static int
4917c478bd9Sstevel@tonic-gate get_minor_class(char *classbuf, di_node_t dn)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	di_minor_t	mi_node;
4947c478bd9Sstevel@tonic-gate 	char		*mi_nodetype;
4957c478bd9Sstevel@tonic-gate 	char		*mi_name;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/* get minor node type */
4987c478bd9Sstevel@tonic-gate 	mi_node = di_minor_next(dn, DI_MINOR_NIL);
4997c478bd9Sstevel@tonic-gate 	if (mi_node == DI_MINOR_NIL)
5007c478bd9Sstevel@tonic-gate 		return (-1);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	mi_nodetype = di_minor_nodetype(mi_node);
5037c478bd9Sstevel@tonic-gate 	if (mi_nodetype == NULL) { /* no type info, return name */
5047c478bd9Sstevel@tonic-gate 		mi_name = di_minor_name(mi_node);
5057c478bd9Sstevel@tonic-gate 		if (mi_name == NULL)
5067c478bd9Sstevel@tonic-gate 			return (-1);
5077c478bd9Sstevel@tonic-gate 		(void) strlcpy(classbuf, mi_name, PICL_CLASSNAMELEN_MAX);
5087c478bd9Sstevel@tonic-gate 		return (0);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate #define	DDI_NODETYPE(x, y) (strncmp(x, y, (sizeof (y) - 1)) == 0)
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * convert the string to the picl class for non-peudo nodes
5157c478bd9Sstevel@tonic-gate 	 */
5167c478bd9Sstevel@tonic-gate 	if (DDI_NODETYPE(mi_nodetype, DDI_PSEUDO))
5177c478bd9Sstevel@tonic-gate 		return (-1);
5187c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK_WWN))
5197c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_BLOCK);
5207c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK_CHAN))
5217c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_BLOCK);
5227c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_CD))
5237c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_CDROM);
5247c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_CD_CHAN))
5257c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_CDROM);
5267c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_FD))
5277c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_FLOPPY);
5287c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK_FABRIC))
5297c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_FABRIC);
5307c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK))
5317c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_BLOCK);
5327c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_MOUSE))
5337c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_MOUSE);
5347c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_KEYBOARD))
5357c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_KEYBOARD);
5367c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_ATTACHMENT_POINT))
5377c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_ATTACHMENT_POINT);
5387c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_TAPE))
5397c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_TAPE);
5407c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_SCSI_ENCLOSURE))
5417c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_SCSI);
5427c478bd9Sstevel@tonic-gate 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_ENCLOSURE)) {
5437c478bd9Sstevel@tonic-gate 		char	*colon;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		if ((colon = strchr(mi_nodetype, ':')) == NULL)
5467c478bd9Sstevel@tonic-gate 			return (-1);
5477c478bd9Sstevel@tonic-gate 		++colon;
5487c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, colon);
5497c478bd9Sstevel@tonic-gate 	} else {	/* unrecognized type, return name */
5507c478bd9Sstevel@tonic-gate 		mi_name = di_minor_name(mi_node);
5517c478bd9Sstevel@tonic-gate 		if (mi_name == NULL)
5527c478bd9Sstevel@tonic-gate 			return (-1);
5537c478bd9Sstevel@tonic-gate 		(void) strlcpy(classbuf, mi_name, PICL_CLASSNAMELEN_MAX);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 	return (0);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*
5597c478bd9Sstevel@tonic-gate  * Derive PICL class using the compatible property of the node
5607c478bd9Sstevel@tonic-gate  * We use the map table to map compatible property value to
5617c478bd9Sstevel@tonic-gate  * class.
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate static int
5647c478bd9Sstevel@tonic-gate get_compatible_class(char *outbuf, di_node_t dn)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	char			*pdata;
5677c478bd9Sstevel@tonic-gate 	char			*pdatap;
5687c478bd9Sstevel@tonic-gate 	int			dret;
5697c478bd9Sstevel@tonic-gate 	int			i;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	dret = di_prop_lookup_strings(DDI_DEV_T_ANY, dn, OBP_COMPATIBLE,
5727c478bd9Sstevel@tonic-gate 	    &pdata);
5737c478bd9Sstevel@tonic-gate 	if (dret <= 0) {
5747c478bd9Sstevel@tonic-gate 		if (!ph)
5757c478bd9Sstevel@tonic-gate 			return (-1);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		dret = di_prom_prop_lookup_strings(ph, dn, OBP_COMPATIBLE,
5787c478bd9Sstevel@tonic-gate 		    &pdata);
5797c478bd9Sstevel@tonic-gate 		if (dret <= 0) {
5807c478bd9Sstevel@tonic-gate 			return (-1);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	pdatap = pdata;
5857c478bd9Sstevel@tonic-gate 	for (i = 0; i < dret; ++i) {
5867c478bd9Sstevel@tonic-gate 		if (lookup_name_class_map(outbuf, pdatap) == 0)
5877c478bd9Sstevel@tonic-gate 			return (0);
5887c478bd9Sstevel@tonic-gate 		pdatap += strlen(pdatap);
5897c478bd9Sstevel@tonic-gate 		pdatap++;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	return (-1);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate  * For a given device node find the PICL class to use. Returns NULL
5967c478bd9Sstevel@tonic-gate  * for non device node
5977c478bd9Sstevel@tonic-gate  */
5987c478bd9Sstevel@tonic-gate static int
5997c478bd9Sstevel@tonic-gate get_node_class(char *classbuf, di_node_t dn, const char *nodename)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate 	if (get_device_type(classbuf, dn) == 0) {
6027c478bd9Sstevel@tonic-gate 		if (di_nodeid(dn) == DI_PROM_NODEID) {
6037c478bd9Sstevel@tonic-gate 			/*
6047c478bd9Sstevel@tonic-gate 			 * discard place holder nodes
6057c478bd9Sstevel@tonic-gate 			 */
6067c478bd9Sstevel@tonic-gate 			if ((strcmp(classbuf, DEVICE_TYPE_BLOCK) == 0) ||
6077c478bd9Sstevel@tonic-gate 			    (strcmp(classbuf, DEVICE_TYPE_BYTE) == 0) ||
6087c478bd9Sstevel@tonic-gate 			    (strcmp(classbuf, DEVICE_TYPE_SES) == 0) ||
6097c478bd9Sstevel@tonic-gate 			    (strcmp(classbuf, DEVICE_TYPE_FP) == 0) ||
6107c478bd9Sstevel@tonic-gate 			    (strcmp(classbuf, DEVICE_TYPE_DISK) == 0))
6117c478bd9Sstevel@tonic-gate 				return (-1);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 			return (0);
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 		return (0);	/* return device_type value */
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if (get_compatible_class(classbuf, dn) == 0) {
6197c478bd9Sstevel@tonic-gate 		return (0);	/* derive class using compatible prop */
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if (lookup_name_class_map(classbuf, nodename) == 0)
6237c478bd9Sstevel@tonic-gate 		return (0);	/* derive class using name prop */
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if (has_reg_prop(dn)) { /* use default obp-device */
6267c478bd9Sstevel@tonic-gate 		(void) strcpy(classbuf, PICL_CLASS_OBP_DEVICE);
6277c478bd9Sstevel@tonic-gate 		return (0);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	return (get_minor_class(classbuf, dn));
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * Add a table property containing nrows with one column
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static int
6377c478bd9Sstevel@tonic-gate add_string_list_prop(picl_nodehdl_t nodeh, char *name, char *strlist,
6387c478bd9Sstevel@tonic-gate     unsigned int nrows)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
6417c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
6427c478bd9Sstevel@tonic-gate 	picl_prophdl_t		tblh;
6437c478bd9Sstevel@tonic-gate 	int			err;
6447c478bd9Sstevel@tonic-gate 	unsigned int		i;
6457c478bd9Sstevel@tonic-gate 	unsigned int		j;
6467c478bd9Sstevel@tonic-gate 	picl_prophdl_t		*proprow;
6477c478bd9Sstevel@tonic-gate 	int			len;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate #define	NCOLS_IN_STRING_TABLE	1
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
6527c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_TABLE, PICL_READ, sizeof (picl_prophdl_t), name,
6537c478bd9Sstevel@tonic-gate 	    NULL, NULL);
6547c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6557c478bd9Sstevel@tonic-gate 		return (err);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	err = ptree_create_table(&tblh);
6587c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6597c478bd9Sstevel@tonic-gate 		return (err);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, &tblh, &proph);
6627c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
6637c478bd9Sstevel@tonic-gate 		return (err);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	proprow = alloca(sizeof (picl_prophdl_t) * nrows);
6667c478bd9Sstevel@tonic-gate 	if (proprow == NULL) {
6677c478bd9Sstevel@tonic-gate 		(void) ptree_destroy_prop(proph);
6687c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	for (j = 0; j < nrows; ++j) {
6727c478bd9Sstevel@tonic-gate 		len = strlen(strlist) + 1;
6737c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
6747c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ, len, name,
6757c478bd9Sstevel@tonic-gate 		    NULL, NULL);
6767c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 		err = ptree_create_prop(&propinfo, strlist, &proprow[j]);
6797c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6807c478bd9Sstevel@tonic-gate 			break;
6817c478bd9Sstevel@tonic-gate 		strlist += len;
6827c478bd9Sstevel@tonic-gate 		err = ptree_add_row_to_table(tblh, NCOLS_IN_STRING_TABLE,
6837c478bd9Sstevel@tonic-gate 		    &proprow[j]);
6847c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6857c478bd9Sstevel@tonic-gate 			break;
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
6897c478bd9Sstevel@tonic-gate 		for (i = 0; i < j; ++i)
6907c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(proprow[i]);
6917c478bd9Sstevel@tonic-gate 		(void) ptree_delete_prop(proph);
6927c478bd9Sstevel@tonic-gate 		(void) ptree_destroy_prop(proph);
6937c478bd9Sstevel@tonic-gate 		return (err);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * return 1 if this node has this property with the given value
7017c478bd9Sstevel@tonic-gate  */
7027c478bd9Sstevel@tonic-gate static int
7037c478bd9Sstevel@tonic-gate compare_string_propval(picl_nodehdl_t nodeh, const char *pname,
7047c478bd9Sstevel@tonic-gate     const char *pval)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	char			*pvalbuf;
7077c478bd9Sstevel@tonic-gate 	int			err;
7087c478bd9Sstevel@tonic-gate 	int			len;
7097c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
7107c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, pname, &proph);
7137c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)	/* prop doesn't exist */
7147c478bd9Sstevel@tonic-gate 		return (0);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	err = ptree_get_propinfo(proph, &pinfo);
7177c478bd9Sstevel@tonic-gate 	if (pinfo.piclinfo.type != PICL_PTYPE_CHARSTRING)
7187c478bd9Sstevel@tonic-gate 		return (0);	/* not string prop */
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	len = strlen(pval) + 1;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	pvalbuf = alloca(len);
7237c478bd9Sstevel@tonic-gate 	if (pvalbuf == NULL)
7247c478bd9Sstevel@tonic-gate 		return (0);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(proph, pvalbuf, len);
7277c478bd9Sstevel@tonic-gate 	if ((err == PICL_SUCCESS) && (strcmp(pvalbuf, pval) == 0))
7287c478bd9Sstevel@tonic-gate 		return (1);	/* prop match */
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	return (0);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * This function recursively searches the tree for a node that has
7357c478bd9Sstevel@tonic-gate  * the specified string property name and value
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate static int
7387c478bd9Sstevel@tonic-gate find_node_by_string_prop(picl_nodehdl_t rooth, const char *pname,
7397c478bd9Sstevel@tonic-gate     const char *pval, picl_nodehdl_t *nodeh)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		childh;
7427c478bd9Sstevel@tonic-gate 	int			err;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	for (err = ptree_get_propval_by_name(rooth, PICL_PROP_CHILD, &childh,
7457c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
7465e3e415aSfw 	    err = ptree_get_propval_by_name(childh, PICL_PROP_PEER, &childh,
7475e3e415aSfw 	    sizeof (picl_nodehdl_t))) {
7487c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
7497c478bd9Sstevel@tonic-gate 			return (err);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		if (compare_string_propval(childh, pname, pval)) {
7527c478bd9Sstevel@tonic-gate 			*nodeh = childh;
7537c478bd9Sstevel@tonic-gate 			return (PICL_SUCCESS);
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		if (find_node_by_string_prop(childh, pname, pval, nodeh) ==
7577c478bd9Sstevel@tonic-gate 		    PICL_SUCCESS)
7587c478bd9Sstevel@tonic-gate 			return (PICL_SUCCESS);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate  * check if this is a string prop
7667c478bd9Sstevel@tonic-gate  * If the length is less than or equal to 4, assume it's not a string list.
7677c478bd9Sstevel@tonic-gate  * If there is any non-ascii or non-print char, it's not a string prop
7687c478bd9Sstevel@tonic-gate  * If \0 is in the first char or any two consecutive \0's exist,
7697c478bd9Sstevel@tonic-gate  * it's a bytearray prop.
7707c478bd9Sstevel@tonic-gate  * Return value: 0 means it's not a string prop, 1 means it's a string prop
7717c478bd9Sstevel@tonic-gate  */
7727c478bd9Sstevel@tonic-gate static int
7737c478bd9Sstevel@tonic-gate is_string_propval(unsigned char *pdata, int len)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	int	i;
7767c478bd9Sstevel@tonic-gate 	int	lastindex;
7777c478bd9Sstevel@tonic-gate 	int	prevnull = -1;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	switch (len) {
7807c478bd9Sstevel@tonic-gate 	case 1:
7817c478bd9Sstevel@tonic-gate 		if (!isascii(pdata[0]) || !isprint(pdata[0]))
7827c478bd9Sstevel@tonic-gate 			return (0);
7837c478bd9Sstevel@tonic-gate 		return (1);
7847c478bd9Sstevel@tonic-gate 	case 2:
7857c478bd9Sstevel@tonic-gate 	case 3:
7867c478bd9Sstevel@tonic-gate 	case 4:
7877c478bd9Sstevel@tonic-gate 		lastindex = len;
7887c478bd9Sstevel@tonic-gate 		if (pdata[len-1] == '\0')
7897c478bd9Sstevel@tonic-gate 			lastindex = len - 1;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		for (i = 0; i < lastindex; i++)
7927c478bd9Sstevel@tonic-gate 			if (!isascii(pdata[i]) || !isprint(pdata[i]))
7937c478bd9Sstevel@tonic-gate 				return (0);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		return (1);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	default:
7987c478bd9Sstevel@tonic-gate 		if (len <= 0)
7997c478bd9Sstevel@tonic-gate 			return (0);
8007c478bd9Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
8017c478bd9Sstevel@tonic-gate 			if (!isascii(pdata[i]) || !isprint(pdata[i])) {
8027c478bd9Sstevel@tonic-gate 				if (pdata[i] != '\0')
8037c478bd9Sstevel@tonic-gate 					return (0);
8047c478bd9Sstevel@tonic-gate 				/*
8057c478bd9Sstevel@tonic-gate 				 * if the null char is in the first char
8067c478bd9Sstevel@tonic-gate 				 * or two consecutive nulls' exist,
8077c478bd9Sstevel@tonic-gate 				 * it's a bytearray prop
8087c478bd9Sstevel@tonic-gate 				 */
8097c478bd9Sstevel@tonic-gate 				if ((i == 0) || ((i - prevnull) == 1))
8107c478bd9Sstevel@tonic-gate 					return (0);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 				prevnull = i;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 		break;
8167c478bd9Sstevel@tonic-gate 	}
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	return (1);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate  * This function counts the number of strings in the value buffer pdata
8237c478bd9Sstevel@tonic-gate  * and creates a property.
8247c478bd9Sstevel@tonic-gate  * If there is only one string in the buffer, pdata, a charstring property
8257c478bd9Sstevel@tonic-gate  * type is created and added.
8267c478bd9Sstevel@tonic-gate  * If there are more than one string in the buffer, pdata, then a table
8277c478bd9Sstevel@tonic-gate  * of charstrings is added.
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate static int
8307c478bd9Sstevel@tonic-gate process_charstring_data(picl_nodehdl_t nodeh, char *pname, unsigned char *pdata,
8317c478bd9Sstevel@tonic-gate     int retval)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	int			err;
8347c478bd9Sstevel@tonic-gate 	int			strcount;
8357c478bd9Sstevel@tonic-gate 	char			*strdat;
8367c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * append the null char at the end of string when there is
8407c478bd9Sstevel@tonic-gate 	 * no null terminator
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	if (pdata[retval - 1] != '\0') {
8437c478bd9Sstevel@tonic-gate 		strdat = alloca(retval + 1);
8447c478bd9Sstevel@tonic-gate 		(void) memcpy(strdat, pdata, retval);
8457c478bd9Sstevel@tonic-gate 		strdat[retval] = '\0';
8467c478bd9Sstevel@tonic-gate 		retval++;
8477c478bd9Sstevel@tonic-gate 	} else {
8487c478bd9Sstevel@tonic-gate 		strdat = alloca(retval);
8497c478bd9Sstevel@tonic-gate 		(void) memcpy(strdat, pdata, retval);
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	/*
8537c478bd9Sstevel@tonic-gate 	 * If it's a string list, create a table prop
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 	strcount = get_string_count(strdat, retval);
8567c478bd9Sstevel@tonic-gate 	if (strcount > 1) {
8577c478bd9Sstevel@tonic-gate 		err = add_string_list_prop(nodeh, pname,
8587c478bd9Sstevel@tonic-gate 		    strdat, strcount);
8597c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
8607c478bd9Sstevel@tonic-gate 			return (err);
8617c478bd9Sstevel@tonic-gate 	} else {
8627c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
8637c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
8647c478bd9Sstevel@tonic-gate 		    strlen(strdat) + 1, pname, NULL,
8657c478bd9Sstevel@tonic-gate 		    NULL);
8667c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
8677c478bd9Sstevel@tonic-gate 			return (err);
8687c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &propinfo,
8697c478bd9Sstevel@tonic-gate 		    strdat, NULL);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * Add the OBP properties as properties of the PICL node
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate static int
8787c478bd9Sstevel@tonic-gate add_openprom_props(picl_nodehdl_t nodeh, di_node_t di_node)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	di_prom_prop_t		promp;
8817c478bd9Sstevel@tonic-gate 	char			*pname;
8827c478bd9Sstevel@tonic-gate 	unsigned char		*pdata;
8837c478bd9Sstevel@tonic-gate 	int			retval;
8847c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
8857c478bd9Sstevel@tonic-gate 	int			err;
8867c478bd9Sstevel@tonic-gate 	picl_prop_type_t	type;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (!ph)
8897c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	for (promp = di_prom_prop_next(ph, di_node, DI_PROM_PROP_NIL);
8927c478bd9Sstevel@tonic-gate 	    promp != DI_PROM_PROP_NIL;
8935e3e415aSfw 	    promp = di_prom_prop_next(ph, di_node, promp)) {
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		pname = di_prom_prop_name(promp);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		retval = di_prom_prop_data(promp, &pdata);
8987c478bd9Sstevel@tonic-gate 		if (retval < 0) {
8997c478bd9Sstevel@tonic-gate 			return (PICL_SUCCESS);
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		if (retval == 0) {
9027c478bd9Sstevel@tonic-gate 			err = ptree_init_propinfo(&propinfo,
9037c478bd9Sstevel@tonic-gate 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_VOID,
9047c478bd9Sstevel@tonic-gate 			    PICL_READ, (size_t)0, pname, NULL, NULL);
9057c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
9067c478bd9Sstevel@tonic-gate 				return (err);
9077c478bd9Sstevel@tonic-gate 			}
9087c478bd9Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(nodeh, &propinfo, NULL,
9097c478bd9Sstevel@tonic-gate 			    NULL);
9107c478bd9Sstevel@tonic-gate 			continue;
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		/*
9147c478bd9Sstevel@tonic-gate 		 * Get the prop type from pname map table
9157c478bd9Sstevel@tonic-gate 		 */
9167c478bd9Sstevel@tonic-gate 		if (lookup_pname_type_map(pname, &type) == 0) {
9177c478bd9Sstevel@tonic-gate 			if (type == PICL_PTYPE_CHARSTRING) {
9187c478bd9Sstevel@tonic-gate 				err = process_charstring_data(nodeh, pname,
9197c478bd9Sstevel@tonic-gate 				    pdata, retval);
9207c478bd9Sstevel@tonic-gate 				if (err != PICL_SUCCESS) {
9217c478bd9Sstevel@tonic-gate 					return (err);
9227c478bd9Sstevel@tonic-gate 				}
9237c478bd9Sstevel@tonic-gate 				continue;
9247c478bd9Sstevel@tonic-gate 			}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 			err = ptree_init_propinfo(&propinfo,
9277c478bd9Sstevel@tonic-gate 			    PTREE_PROPINFO_VERSION, type, PICL_READ,
9287c478bd9Sstevel@tonic-gate 			    retval, pname, NULL, NULL);
9297c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
9307c478bd9Sstevel@tonic-gate 				return (err);
9317c478bd9Sstevel@tonic-gate 			}
9327c478bd9Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(nodeh, &propinfo,
9337c478bd9Sstevel@tonic-gate 			    pdata, NULL);
9347c478bd9Sstevel@tonic-gate 		} else if (!is_string_propval(pdata, retval)) {
9357c478bd9Sstevel@tonic-gate 			switch (retval) {
9367c478bd9Sstevel@tonic-gate 			case sizeof (uint8_t):
9377c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
9387c478bd9Sstevel@tonic-gate 			case sizeof (uint16_t):
9397c478bd9Sstevel@tonic-gate 				/*FALLTHROUGH*/
9407c478bd9Sstevel@tonic-gate 			case sizeof (uint32_t):
9417c478bd9Sstevel@tonic-gate 				type = PICL_PTYPE_UNSIGNED_INT;
9427c478bd9Sstevel@tonic-gate 				break;
9437c478bd9Sstevel@tonic-gate 			default:
9447c478bd9Sstevel@tonic-gate 				type = PICL_PTYPE_BYTEARRAY;
9457c478bd9Sstevel@tonic-gate 				break;
9467c478bd9Sstevel@tonic-gate 			}
9477c478bd9Sstevel@tonic-gate 			err = ptree_init_propinfo(&propinfo,
9487c478bd9Sstevel@tonic-gate 			    PTREE_PROPINFO_VERSION, type, PICL_READ,
9497c478bd9Sstevel@tonic-gate 			    retval, pname, NULL, NULL);
9507c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
9517c478bd9Sstevel@tonic-gate 				return (err);
9527c478bd9Sstevel@tonic-gate 			}
9537c478bd9Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(nodeh, &propinfo,
9547c478bd9Sstevel@tonic-gate 			    pdata, NULL);
9557c478bd9Sstevel@tonic-gate 		} else {
9567c478bd9Sstevel@tonic-gate 			err = process_charstring_data(nodeh, pname, pdata,
9577c478bd9Sstevel@tonic-gate 			    retval);
9587c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
9597c478bd9Sstevel@tonic-gate 				return (err);
9607c478bd9Sstevel@tonic-gate 			}
9617c478bd9Sstevel@tonic-gate 		}
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
96761db5dcaSrd static void
96861db5dcaSrd add_boolean_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val)
96961db5dcaSrd {
97061db5dcaSrd 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
97161db5dcaSrd 	    PICL_PTYPE_VOID, PICL_READ, (size_t)0, di_val, NULL, NULL);
97261db5dcaSrd 	(void) ptree_create_and_add_prop(nodeh, &propinfo, NULL, NULL);
97361db5dcaSrd }
97461db5dcaSrd 
97561db5dcaSrd static void
97661db5dcaSrd add_uints_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val,
97761db5dcaSrd     int *idata, int len)
97861db5dcaSrd {
97961db5dcaSrd 	if (len == 1)
98061db5dcaSrd 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
98161db5dcaSrd 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (int), di_val,
98261db5dcaSrd 		    NULL, NULL);
98361db5dcaSrd 	else
98461db5dcaSrd 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
98561db5dcaSrd 		    PICL_PTYPE_BYTEARRAY, PICL_READ, len * sizeof (int), di_val,
98661db5dcaSrd 		    NULL, NULL);
98761db5dcaSrd 
98861db5dcaSrd 	(void) ptree_create_and_add_prop(nodeh, &propinfo, idata, NULL);
98961db5dcaSrd }
99061db5dcaSrd 
99161db5dcaSrd static void
99261db5dcaSrd add_strings_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val,
99361db5dcaSrd     char *sdata, int len)
99461db5dcaSrd {
99561db5dcaSrd 	if (len == 1) {
99661db5dcaSrd 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
99761db5dcaSrd 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(sdata) + 1, di_val,
99861db5dcaSrd 		    NULL, NULL);
99961db5dcaSrd 		(void) ptree_create_and_add_prop(nodeh, &propinfo, sdata, NULL);
100061db5dcaSrd 	} else {
100161db5dcaSrd 		(void) add_string_list_prop(nodeh, di_val, sdata, len);
100261db5dcaSrd 	}
100361db5dcaSrd }
100461db5dcaSrd 
100561db5dcaSrd static void
100661db5dcaSrd add_bytes_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val,
100761db5dcaSrd     unsigned char *bdata, int len)
100861db5dcaSrd {
100961db5dcaSrd 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
101061db5dcaSrd 	    PICL_PTYPE_BYTEARRAY, PICL_READ, len, di_val, NULL, NULL);
101161db5dcaSrd 	(void) ptree_create_and_add_prop(nodeh, &propinfo, bdata, NULL);
101261db5dcaSrd }
101361db5dcaSrd 
1014*43e66171SMichael Bergknoff static const char *
1015*43e66171SMichael Bergknoff path_state_name(di_path_state_t st)
1016*43e66171SMichael Bergknoff {
1017*43e66171SMichael Bergknoff 	switch (st) {
1018*43e66171SMichael Bergknoff 		case DI_PATH_STATE_ONLINE:
1019*43e66171SMichael Bergknoff 			return ("online");
1020*43e66171SMichael Bergknoff 		case DI_PATH_STATE_STANDBY:
1021*43e66171SMichael Bergknoff 			return ("standby");
1022*43e66171SMichael Bergknoff 		case DI_PATH_STATE_OFFLINE:
1023*43e66171SMichael Bergknoff 			return ("offline");
1024*43e66171SMichael Bergknoff 		case DI_PATH_STATE_FAULT:
1025*43e66171SMichael Bergknoff 			return ("faulted");
1026*43e66171SMichael Bergknoff 	}
1027*43e66171SMichael Bergknoff 	return ("unknown");
1028*43e66171SMichael Bergknoff }
1029*43e66171SMichael Bergknoff 
1030*43e66171SMichael Bergknoff /*
1031*43e66171SMichael Bergknoff  * This function is the volatile property handler for the multipath node
1032*43e66171SMichael Bergknoff  * "State" property. It must locate the associated devinfo node in order to
1033*43e66171SMichael Bergknoff  * determine the current state. Since the devinfo node can have multiple
1034*43e66171SMichael Bergknoff  * paths the devfs_path is used to locate the correct path.
1035*43e66171SMichael Bergknoff  */
1036*43e66171SMichael Bergknoff static int
1037*43e66171SMichael Bergknoff get_path_state_name(ptree_rarg_t *rarg, void *vbuf)
1038*43e66171SMichael Bergknoff {
1039*43e66171SMichael Bergknoff 	int		err;
1040*43e66171SMichael Bergknoff 	picl_nodehdl_t	parh;
1041*43e66171SMichael Bergknoff 	char		devfs_path[PATH_MAX];
1042*43e66171SMichael Bergknoff 	di_node_t	di_node;
1043*43e66171SMichael Bergknoff 	di_node_t	di_root;
1044*43e66171SMichael Bergknoff 	di_path_t	pi = DI_PATH_NIL;
1045*43e66171SMichael Bergknoff 	picl_nodehdl_t	mpnode;
1046*43e66171SMichael Bergknoff 
1047*43e66171SMichael Bergknoff 	(void) strlcpy(vbuf, "unknown", MAX_STATE_SIZE);
1048*43e66171SMichael Bergknoff 
1049*43e66171SMichael Bergknoff 	mpnode = rarg->nodeh;
1050*43e66171SMichael Bergknoff 
1051*43e66171SMichael Bergknoff 	/*
1052*43e66171SMichael Bergknoff 	 * The parent node represents the vHCI.
1053*43e66171SMichael Bergknoff 	 */
1054*43e66171SMichael Bergknoff 	err = ptree_get_propval_by_name(mpnode, PICL_PROP_PARENT, &parh,
1055*43e66171SMichael Bergknoff 	    sizeof (picl_nodehdl_t));
1056*43e66171SMichael Bergknoff 	if (err != PICL_SUCCESS) {
1057*43e66171SMichael Bergknoff 		return (PICL_SUCCESS);
1058*43e66171SMichael Bergknoff 	}
1059*43e66171SMichael Bergknoff 
1060*43e66171SMichael Bergknoff 	/*
1061*43e66171SMichael Bergknoff 	 * The PICL_PROP_DEVFS_PATH property will be used to locate the
1062*43e66171SMichael Bergknoff 	 * devinfo node for the vHCI driver.
1063*43e66171SMichael Bergknoff 	 */
1064*43e66171SMichael Bergknoff 	err = ptree_get_propval_by_name(parh, PICL_PROP_DEVFS_PATH, devfs_path,
1065*43e66171SMichael Bergknoff 	    sizeof (devfs_path));
1066*43e66171SMichael Bergknoff 	if (err != PICL_SUCCESS) {
1067*43e66171SMichael Bergknoff 		return (PICL_SUCCESS);
1068*43e66171SMichael Bergknoff 	}
1069*43e66171SMichael Bergknoff 	/*
1070*43e66171SMichael Bergknoff 	 * Find the di_node for the vHCI driver. It will be used to scan
1071*43e66171SMichael Bergknoff 	 * the path information nodes.
1072*43e66171SMichael Bergknoff 	 */
1073*43e66171SMichael Bergknoff 	di_root = di_init("/", DINFOCACHE);
1074*43e66171SMichael Bergknoff 	if (di_root == DI_NODE_NIL) {
1075*43e66171SMichael Bergknoff 		return (PICL_SUCCESS);
1076*43e66171SMichael Bergknoff 	}
1077*43e66171SMichael Bergknoff 	di_node = di_lookup_node(di_root, devfs_path);
1078*43e66171SMichael Bergknoff 	if (di_node == DI_NODE_NIL) {
1079*43e66171SMichael Bergknoff 		di_fini(di_root);
1080*43e66171SMichael Bergknoff 		return (PICL_SUCCESS);
1081*43e66171SMichael Bergknoff 	}
1082*43e66171SMichael Bergknoff 
1083*43e66171SMichael Bergknoff 	/*
1084*43e66171SMichael Bergknoff 	 * The devfs_path will be used below to match the
1085*43e66171SMichael Bergknoff 	 * proper path information node.
1086*43e66171SMichael Bergknoff 	 */
1087*43e66171SMichael Bergknoff 	err = ptree_get_propval_by_name(mpnode, PICL_PROP_DEVFS_PATH,
1088*43e66171SMichael Bergknoff 	    devfs_path, sizeof (devfs_path));
1089*43e66171SMichael Bergknoff 	if (err != PICL_SUCCESS) {
1090*43e66171SMichael Bergknoff 		di_fini(di_root);
1091*43e66171SMichael Bergknoff 		return (PICL_SUCCESS);
1092*43e66171SMichael Bergknoff 	}
1093*43e66171SMichael Bergknoff 
1094*43e66171SMichael Bergknoff 	/*
1095*43e66171SMichael Bergknoff 	 * Scan the path information nodes looking for the matching devfs
1096*43e66171SMichael Bergknoff 	 * path. When found obtain the state information.
1097*43e66171SMichael Bergknoff 	 */
1098*43e66171SMichael Bergknoff 	while ((pi = di_path_next_phci(di_node, pi)) != DI_PATH_NIL) {
1099*43e66171SMichael Bergknoff 		char		*di_path;
1100*43e66171SMichael Bergknoff 		di_node_t	phci_node = di_path_phci_node(pi);
1101*43e66171SMichael Bergknoff 
1102*43e66171SMichael Bergknoff 		if (phci_node == DI_PATH_NIL)
1103*43e66171SMichael Bergknoff 			continue;
1104*43e66171SMichael Bergknoff 
1105*43e66171SMichael Bergknoff 		di_path = di_devfs_path(phci_node);
1106*43e66171SMichael Bergknoff 		if (di_path) {
1107*43e66171SMichael Bergknoff 			if (strcmp(di_path, devfs_path) != 0) {
1108*43e66171SMichael Bergknoff 				di_devfs_path_free(di_path);
1109*43e66171SMichael Bergknoff 				continue;
1110*43e66171SMichael Bergknoff 			}
1111*43e66171SMichael Bergknoff 			(void) strlcpy(vbuf, path_state_name(di_path_state(pi)),
1112*43e66171SMichael Bergknoff 			    MAX_STATE_SIZE);
1113*43e66171SMichael Bergknoff 			di_devfs_path_free(di_path);
1114*43e66171SMichael Bergknoff 			break;
1115*43e66171SMichael Bergknoff 		}
1116*43e66171SMichael Bergknoff 	}
1117*43e66171SMichael Bergknoff 
1118*43e66171SMichael Bergknoff 	di_fini(di_root);
1119*43e66171SMichael Bergknoff 	return (PICL_SUCCESS);
1120*43e66171SMichael Bergknoff }
1121*43e66171SMichael Bergknoff 
1122*43e66171SMichael Bergknoff static void
1123*43e66171SMichael Bergknoff add_di_path_prop(picl_nodehdl_t nodeh, di_path_prop_t di_path_prop)
1124*43e66171SMichael Bergknoff {
1125*43e66171SMichael Bergknoff 	int			di_ptype;
1126*43e66171SMichael Bergknoff 	char			*di_val;
1127*43e66171SMichael Bergknoff 	ptree_propinfo_t	propinfo;
1128*43e66171SMichael Bergknoff 	int			*idata;
1129*43e66171SMichael Bergknoff 	char			*sdata;
1130*43e66171SMichael Bergknoff 	unsigned char		*bdata;
1131*43e66171SMichael Bergknoff 	int			len;
1132*43e66171SMichael Bergknoff 
1133*43e66171SMichael Bergknoff 	di_ptype = di_path_prop_type(di_path_prop);
1134*43e66171SMichael Bergknoff 	di_val = di_path_prop_name(di_path_prop);
1135*43e66171SMichael Bergknoff 
1136*43e66171SMichael Bergknoff 	switch (di_ptype) {
1137*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_BOOLEAN:
1138*43e66171SMichael Bergknoff 		add_boolean_prop(nodeh, propinfo, di_val);
1139*43e66171SMichael Bergknoff 		break;
1140*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_INT:
1141*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_INT64:
1142*43e66171SMichael Bergknoff 		len = di_path_prop_ints(di_path_prop, &idata);
1143*43e66171SMichael Bergknoff 		if (len < 0)
1144*43e66171SMichael Bergknoff 			/* Received error, so ignore prop */
1145*43e66171SMichael Bergknoff 			break;
1146*43e66171SMichael Bergknoff 		add_uints_prop(nodeh, propinfo, di_val, idata, len);
1147*43e66171SMichael Bergknoff 		break;
1148*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_STRING:
1149*43e66171SMichael Bergknoff 		len = di_path_prop_strings(di_path_prop, &sdata);
1150*43e66171SMichael Bergknoff 		if (len <= 0)
1151*43e66171SMichael Bergknoff 			break;
1152*43e66171SMichael Bergknoff 		add_strings_prop(nodeh, propinfo, di_val, sdata, len);
1153*43e66171SMichael Bergknoff 		break;
1154*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_BYTE:
1155*43e66171SMichael Bergknoff 		len = di_path_prop_bytes(di_path_prop, &bdata);
1156*43e66171SMichael Bergknoff 		if (len < 0)
1157*43e66171SMichael Bergknoff 			break;
1158*43e66171SMichael Bergknoff 		add_bytes_prop(nodeh, propinfo, di_val, bdata, len);
1159*43e66171SMichael Bergknoff 		break;
1160*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_UNKNOWN:
1161*43e66171SMichael Bergknoff 		/*
1162*43e66171SMichael Bergknoff 		 * Unknown type, we'll try and guess what it should be.
1163*43e66171SMichael Bergknoff 		 */
1164*43e66171SMichael Bergknoff 		len = di_path_prop_strings(di_path_prop, &sdata);
1165*43e66171SMichael Bergknoff 		if ((len > 0) && (sdata[0] != 0)) {
1166*43e66171SMichael Bergknoff 			add_strings_prop(nodeh, propinfo, di_val, sdata,
1167*43e66171SMichael Bergknoff 			    len);
1168*43e66171SMichael Bergknoff 			break;
1169*43e66171SMichael Bergknoff 		}
1170*43e66171SMichael Bergknoff 		len = di_path_prop_ints(di_path_prop, &idata);
1171*43e66171SMichael Bergknoff 		if (len > 0) {
1172*43e66171SMichael Bergknoff 			add_uints_prop(nodeh, propinfo, di_val,
1173*43e66171SMichael Bergknoff 			    idata, len);
1174*43e66171SMichael Bergknoff 			break;
1175*43e66171SMichael Bergknoff 		}
1176*43e66171SMichael Bergknoff 		len = di_path_prop_bytes(di_path_prop, &bdata);
1177*43e66171SMichael Bergknoff 		if (len > 0)
1178*43e66171SMichael Bergknoff 			add_bytes_prop(nodeh, propinfo,
1179*43e66171SMichael Bergknoff 			    di_val, bdata, len);
1180*43e66171SMichael Bergknoff 		else if (len == 0)
1181*43e66171SMichael Bergknoff 			add_boolean_prop(nodeh, propinfo,
1182*43e66171SMichael Bergknoff 			    di_val);
1183*43e66171SMichael Bergknoff 		break;
1184*43e66171SMichael Bergknoff 	case DI_PROP_TYPE_UNDEF_IT:
1185*43e66171SMichael Bergknoff 		break;
1186*43e66171SMichael Bergknoff 	default:
1187*43e66171SMichael Bergknoff 		break;
1188*43e66171SMichael Bergknoff 	}
1189*43e66171SMichael Bergknoff }
1190*43e66171SMichael Bergknoff 
1191*43e66171SMichael Bergknoff /*
1192*43e66171SMichael Bergknoff  * Add nodes for path information (PSARC/1999/647, PSARC/2008/437)
1193*43e66171SMichael Bergknoff  */
1194*43e66171SMichael Bergknoff static void
1195*43e66171SMichael Bergknoff construct_mpath_node(picl_nodehdl_t parh, di_node_t di_node)
1196*43e66171SMichael Bergknoff {
1197*43e66171SMichael Bergknoff 	di_path_t 		pi = DI_PATH_NIL;
1198*43e66171SMichael Bergknoff 
1199*43e66171SMichael Bergknoff 	while ((pi = di_path_next_phci(di_node, pi)) != DI_PATH_NIL) {
1200*43e66171SMichael Bergknoff 		di_node_t 		phci_node = di_path_phci_node(pi);
1201*43e66171SMichael Bergknoff 		di_path_prop_t 		di_path_prop;
1202*43e66171SMichael Bergknoff 		picl_nodehdl_t		nodeh;
1203*43e66171SMichael Bergknoff 		ptree_propinfo_t	propinfo;
1204*43e66171SMichael Bergknoff 		int			err;
1205*43e66171SMichael Bergknoff 		int			instance;
1206*43e66171SMichael Bergknoff 		char			*di_val;
1207*43e66171SMichael Bergknoff 
1208*43e66171SMichael Bergknoff 		if (phci_node == DI_PATH_NIL)
1209*43e66171SMichael Bergknoff 			continue;
1210*43e66171SMichael Bergknoff 
1211*43e66171SMichael Bergknoff 		err = ptree_create_and_add_node(parh, PICL_CLASS_MULTIPATH,
1212*43e66171SMichael Bergknoff 		    PICL_CLASS_MULTIPATH, &nodeh);
1213*43e66171SMichael Bergknoff 		if (err != PICL_SUCCESS)
1214*43e66171SMichael Bergknoff 			continue;
1215*43e66171SMichael Bergknoff 
1216*43e66171SMichael Bergknoff 		instance = di_instance(phci_node);
1217*43e66171SMichael Bergknoff 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1218*43e66171SMichael Bergknoff 		    PICL_PTYPE_INT, PICL_READ, sizeof (instance),
1219*43e66171SMichael Bergknoff 		    PICL_PROP_INSTANCE, NULL, NULL);
1220*43e66171SMichael Bergknoff 		(void) ptree_create_and_add_prop(nodeh, &propinfo, &instance,
1221*43e66171SMichael Bergknoff 		    NULL);
1222*43e66171SMichael Bergknoff 
1223*43e66171SMichael Bergknoff 		di_val = di_devfs_path(phci_node);
1224*43e66171SMichael Bergknoff 		if (di_val) {
1225*43e66171SMichael Bergknoff 			(void) ptree_init_propinfo(&propinfo,
1226*43e66171SMichael Bergknoff 			    PTREE_PROPINFO_VERSION,
1227*43e66171SMichael Bergknoff 			    PICL_PTYPE_CHARSTRING, PICL_READ,
1228*43e66171SMichael Bergknoff 			    strlen(di_val) + 1, PICL_PROP_DEVFS_PATH,
1229*43e66171SMichael Bergknoff 			    NULL, NULL);
1230*43e66171SMichael Bergknoff 			(void) ptree_create_and_add_prop(nodeh,
1231*43e66171SMichael Bergknoff 			    &propinfo, di_val, NULL);
1232*43e66171SMichael Bergknoff 			di_devfs_path_free(di_val);
1233*43e66171SMichael Bergknoff 		}
1234*43e66171SMichael Bergknoff 
1235*43e66171SMichael Bergknoff 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1236*43e66171SMichael Bergknoff 		    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE),
1237*43e66171SMichael Bergknoff 		    MAX_STATE_SIZE, PICL_PROP_STATE, get_path_state_name, NULL);
1238*43e66171SMichael Bergknoff 		(void) ptree_create_and_add_prop(nodeh, &propinfo, NULL, NULL);
1239*43e66171SMichael Bergknoff 
1240*43e66171SMichael Bergknoff 		for (di_path_prop = di_path_prop_next(pi, DI_PROP_NIL);
1241*43e66171SMichael Bergknoff 		    di_path_prop != DI_PROP_NIL;
1242*43e66171SMichael Bergknoff 		    di_path_prop = di_path_prop_next(pi, di_path_prop)) {
1243*43e66171SMichael Bergknoff 			add_di_path_prop(nodeh, di_path_prop);
1244*43e66171SMichael Bergknoff 		}
1245*43e66171SMichael Bergknoff 	}
1246*43e66171SMichael Bergknoff }
1247*43e66171SMichael Bergknoff 
12487c478bd9Sstevel@tonic-gate /*
12497c478bd9Sstevel@tonic-gate  * Add properties provided by libdevinfo
12507c478bd9Sstevel@tonic-gate  */
12517c478bd9Sstevel@tonic-gate static void
12527c478bd9Sstevel@tonic-gate add_devinfo_props(picl_nodehdl_t nodeh, di_node_t di_node)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate 	int			instance;
12557c478bd9Sstevel@tonic-gate 	char			*di_val;
12567c478bd9Sstevel@tonic-gate 	di_prop_t		di_prop;
12577c478bd9Sstevel@tonic-gate 	int			di_ptype;
12587c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
125961db5dcaSrd 	char			*sdata;
126061db5dcaSrd 	unsigned char		*bdata;
126161db5dcaSrd 	int			*idata;
126261db5dcaSrd 	int			len;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	instance = di_instance(di_node);
12657c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
12667c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ, sizeof (instance), PICL_PROP_INSTANCE,
12677c478bd9Sstevel@tonic-gate 	    NULL, NULL);
12687c478bd9Sstevel@tonic-gate 	(void) ptree_create_and_add_prop(nodeh, &propinfo, &instance, NULL);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	di_val = di_bus_addr(di_node);
12717c478bd9Sstevel@tonic-gate 	if (di_val) {
12727c478bd9Sstevel@tonic-gate 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
12737c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
12747c478bd9Sstevel@tonic-gate 		    PICL_PROP_BUS_ADDR, NULL, NULL);
12757c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
12767c478bd9Sstevel@tonic-gate 		    NULL);
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	di_val = di_binding_name(di_node);
12807c478bd9Sstevel@tonic-gate 	if (di_val) {
12817c478bd9Sstevel@tonic-gate 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
12827c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
12837c478bd9Sstevel@tonic-gate 		    PICL_PROP_BINDING_NAME, NULL, NULL);
12847c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
12857c478bd9Sstevel@tonic-gate 		    NULL);
12867c478bd9Sstevel@tonic-gate 	}
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate 	di_val = di_driver_name(di_node);
12897c478bd9Sstevel@tonic-gate 	if (di_val) {
12907c478bd9Sstevel@tonic-gate 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
12917c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
12927c478bd9Sstevel@tonic-gate 		    PICL_PROP_DRIVER_NAME, NULL, NULL);
12937c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
12947c478bd9Sstevel@tonic-gate 		    NULL);
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	di_val = di_devfs_path(di_node);
12987c478bd9Sstevel@tonic-gate 	if (di_val) {
12997c478bd9Sstevel@tonic-gate 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
13007c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
13017c478bd9Sstevel@tonic-gate 		    PICL_PROP_DEVFS_PATH, NULL, NULL);
13027c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
13037c478bd9Sstevel@tonic-gate 		    NULL);
13047c478bd9Sstevel@tonic-gate 		di_devfs_path_free(di_val);
13057c478bd9Sstevel@tonic-gate 	}
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	for (di_prop = di_prop_next(di_node, DI_PROP_NIL);
13087c478bd9Sstevel@tonic-gate 	    di_prop != DI_PROP_NIL;
13095e3e415aSfw 	    di_prop = di_prop_next(di_node, di_prop)) {
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 		di_val = di_prop_name(di_prop);
13127c478bd9Sstevel@tonic-gate 		di_ptype = di_prop_type(di_prop);
131361db5dcaSrd 
13147c478bd9Sstevel@tonic-gate 		switch (di_ptype) {
13157c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_BOOLEAN:
131661db5dcaSrd 			add_boolean_prop(nodeh, propinfo, di_val);
13177c478bd9Sstevel@tonic-gate 			break;
131861db5dcaSrd 		case DI_PROP_TYPE_INT:
13197c478bd9Sstevel@tonic-gate 			len = di_prop_ints(di_prop, &idata);
13207c478bd9Sstevel@tonic-gate 			if (len < 0)
132161db5dcaSrd 				/* Received error, so ignore prop */
13227c478bd9Sstevel@tonic-gate 				break;
132361db5dcaSrd 			add_uints_prop(nodeh, propinfo, di_val, idata, len);
132461db5dcaSrd 			break;
132561db5dcaSrd 		case DI_PROP_TYPE_STRING:
13267c478bd9Sstevel@tonic-gate 			len = di_prop_strings(di_prop, &sdata);
13277c478bd9Sstevel@tonic-gate 			if (len < 0)
13287c478bd9Sstevel@tonic-gate 				break;
132961db5dcaSrd 			add_strings_prop(nodeh, propinfo, di_val, sdata, len);
133061db5dcaSrd 			break;
133161db5dcaSrd 		case DI_PROP_TYPE_BYTE:
13327c478bd9Sstevel@tonic-gate 			len = di_prop_bytes(di_prop, &bdata);
13337c478bd9Sstevel@tonic-gate 			if (len < 0)
13347c478bd9Sstevel@tonic-gate 				break;
133561db5dcaSrd 			add_bytes_prop(nodeh, propinfo, di_val, bdata, len);
133661db5dcaSrd 			break;
13377c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_UNKNOWN:
133861db5dcaSrd 			/*
133961db5dcaSrd 			 * Unknown type, we'll try and guess what it should be.
134061db5dcaSrd 			 */
134161db5dcaSrd 			len = di_prop_strings(di_prop, &sdata);
134261db5dcaSrd 			if ((len > 0) && (sdata[0] != 0)) {
134361db5dcaSrd 				add_strings_prop(nodeh, propinfo, di_val, sdata,
134461db5dcaSrd 				    len);
134561db5dcaSrd 				break;
134661db5dcaSrd 			}
134761db5dcaSrd 			len = di_prop_ints(di_prop, &idata);
134861db5dcaSrd 			if (len > 0) {
134961db5dcaSrd 				add_uints_prop(nodeh, propinfo, di_val,
135061db5dcaSrd 				    idata, len);
135161db5dcaSrd 				break;
135261db5dcaSrd 			}
135361db5dcaSrd 			len = di_prop_rawdata(di_prop, &bdata);
135461db5dcaSrd 			if (len > 0)
135561db5dcaSrd 				add_bytes_prop(nodeh, propinfo,
135661db5dcaSrd 				    di_val, bdata, len);
135761db5dcaSrd 			else if (len == 0)
135861db5dcaSrd 				add_boolean_prop(nodeh, propinfo,
135961db5dcaSrd 				    di_val);
13607c478bd9Sstevel@tonic-gate 			break;
13617c478bd9Sstevel@tonic-gate 		case DI_PROP_TYPE_UNDEF_IT:
13627c478bd9Sstevel@tonic-gate 			break;
13637c478bd9Sstevel@tonic-gate 		default:
13647c478bd9Sstevel@tonic-gate 			break;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate }
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate /*
13707c478bd9Sstevel@tonic-gate  * This function creates the /obp node in the PICL tree for OBP nodes
13717c478bd9Sstevel@tonic-gate  * without a device type class.
13727c478bd9Sstevel@tonic-gate  */
13737c478bd9Sstevel@tonic-gate static int
13747c478bd9Sstevel@tonic-gate construct_picl_openprom(picl_nodehdl_t rooth, picl_nodehdl_t *obph)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	tmph;
13777c478bd9Sstevel@tonic-gate 	int		err;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_node(rooth, PICL_NODE_OBP,
13807c478bd9Sstevel@tonic-gate 	    PICL_CLASS_PICL, &tmph);
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
13837c478bd9Sstevel@tonic-gate 		return (err);
13847c478bd9Sstevel@tonic-gate 	*obph = tmph;
13857c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate /*
13897c478bd9Sstevel@tonic-gate  * This function creates the /platform node in the PICL tree and
13907c478bd9Sstevel@tonic-gate  * its properties. It sets the "platform-name" property to the
13917c478bd9Sstevel@tonic-gate  * platform name
13927c478bd9Sstevel@tonic-gate  */
13937c478bd9Sstevel@tonic-gate static int
13947c478bd9Sstevel@tonic-gate construct_picl_platform(picl_nodehdl_t rooth, di_node_t di_root,
13957c478bd9Sstevel@tonic-gate     picl_nodehdl_t *piclh)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	int			err;
13987c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		plafh;
13997c478bd9Sstevel@tonic-gate 	char			*nodename;
14007c478bd9Sstevel@tonic-gate 	char			nodeclass[PICL_CLASSNAMELEN_MAX];
14017c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
14027c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	nodename = di_node_name(di_root);
14057c478bd9Sstevel@tonic-gate 	if (nodename == NULL)
14067c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	err = 0;
14097c478bd9Sstevel@tonic-gate 	if (di_nodeid(di_root) == DI_PROM_NODEID ||
14107c478bd9Sstevel@tonic-gate 	    di_nodeid(di_root) == DI_SID_NODEID)
14117c478bd9Sstevel@tonic-gate 		err = get_device_type(nodeclass, di_root);
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	if (err < 0)
14147c478bd9Sstevel@tonic-gate 		(void) strcpy(nodeclass, PICL_CLASS_UPA);	/* default */
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_node(rooth, PICL_NODE_PLATFORM,
14177c478bd9Sstevel@tonic-gate 	    nodeclass, &plafh);
14187c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
14197c478bd9Sstevel@tonic-gate 		return (err);
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
14227c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(nodename) + 1,
14237c478bd9Sstevel@tonic-gate 	    PICL_PROP_PLATFORM_NAME, NULL, NULL);
14247c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(plafh, &propinfo, nodename, &proph);
14257c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
14267c478bd9Sstevel@tonic-gate 		return (err);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	(void) add_devinfo_props(plafh, di_root);
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	(void) add_openprom_props(plafh, di_root);
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	*piclh = plafh;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate /*
14387c478bd9Sstevel@tonic-gate  * This function creates a node in /obp tree for the libdevinfo handle.
14397c478bd9Sstevel@tonic-gate  */
14407c478bd9Sstevel@tonic-gate static int
14417c478bd9Sstevel@tonic-gate construct_obp_node(picl_nodehdl_t parh, di_node_t dn, picl_nodehdl_t *chdh)
14427c478bd9Sstevel@tonic-gate {
14437c478bd9Sstevel@tonic-gate 	int		err;
14447c478bd9Sstevel@tonic-gate 	char		*nodename;
14457c478bd9Sstevel@tonic-gate 	char		nodeclass[PICL_CLASSNAMELEN_MAX];
14467c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	anodeh;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	nodename = di_node_name(dn);	/* PICL_PROP_NAME */
14497c478bd9Sstevel@tonic-gate 	if (nodename == NULL)
14507c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	if (strcmp(nodename, "pseudo") == 0)
14537c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if ((di_nodeid(dn) == DI_PROM_NODEID) &&
14567c478bd9Sstevel@tonic-gate 	    (get_device_type(nodeclass, dn) == 0))
14577c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_node(parh, nodename, nodename, &anodeh);
14607c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
14617c478bd9Sstevel@tonic-gate 		return (err);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	add_devinfo_props(anodeh, dn);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	(void) add_openprom_props(anodeh, dn);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	*chdh = anodeh;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
14707c478bd9Sstevel@tonic-gate }
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate /*
14737c478bd9Sstevel@tonic-gate  * This function creates a PICL node in /platform tree for a device
14747c478bd9Sstevel@tonic-gate  */
14757c478bd9Sstevel@tonic-gate static int
14767c478bd9Sstevel@tonic-gate construct_devtype_node(picl_nodehdl_t parh, char *nodename,
14777c478bd9Sstevel@tonic-gate     char *nodeclass, di_node_t dn, picl_nodehdl_t *chdh)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate 	int			err;
14807c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		anodeh;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_node(parh, nodename, nodeclass, &anodeh);
14837c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
14847c478bd9Sstevel@tonic-gate 		return (err);
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	(void) add_devinfo_props(anodeh, dn);
14877c478bd9Sstevel@tonic-gate 	(void) add_openprom_props(anodeh, dn);
1488*43e66171SMichael Bergknoff 	construct_mpath_node(anodeh, dn);
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	*chdh = anodeh;
14917c478bd9Sstevel@tonic-gate 	return (err);
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate /*
14957c478bd9Sstevel@tonic-gate  * Create a subtree of "picl" class nodes in /obp for these nodes
14967c478bd9Sstevel@tonic-gate  */
14977c478bd9Sstevel@tonic-gate static int
14987c478bd9Sstevel@tonic-gate construct_openprom_tree(picl_nodehdl_t nodeh, di_node_t  dinode)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate 	di_node_t	cnode;
15017c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
15027c478bd9Sstevel@tonic-gate 	int		err;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	err = construct_obp_node(nodeh, dinode, &chdh);
15057c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
15067c478bd9Sstevel@tonic-gate 		return (err);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	for (cnode = di_child_node(dinode); cnode != DI_NODE_NIL;
15097c478bd9Sstevel@tonic-gate 	    cnode = di_sibling_node(cnode))
15107c478bd9Sstevel@tonic-gate 		(void) construct_openprom_tree(chdh, cnode);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate }
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate /*
15177c478bd9Sstevel@tonic-gate  * Process the libdevinfo device tree and create nodes in /platform or /obp
15187c478bd9Sstevel@tonic-gate  * PICL tree.
15197c478bd9Sstevel@tonic-gate  *
15207c478bd9Sstevel@tonic-gate  * This routine traverses the immediate children of "dinode" device and
15217c478bd9Sstevel@tonic-gate  * determines the node class for that child. If it finds a valid class
15227c478bd9Sstevel@tonic-gate  * name, then it builds a PICL node under /platform subtree and calls itself
15237c478bd9Sstevel@tonic-gate  * recursively to construct the subtree for that child node. Otherwise, if
15247c478bd9Sstevel@tonic-gate  * the parent_class is NULL, then it constructs a node and subtree under /obp
15257c478bd9Sstevel@tonic-gate  * subtree.
15267c478bd9Sstevel@tonic-gate  *
15277c478bd9Sstevel@tonic-gate  * Note that we skip the children nodes that don't have a valid class name
15287c478bd9Sstevel@tonic-gate  * and the parent_class is non NULL to prevent creation of any placeholder
15297c478bd9Sstevel@tonic-gate  * nodes (such as sd,...).
15307c478bd9Sstevel@tonic-gate  */
15317c478bd9Sstevel@tonic-gate static int
15327c478bd9Sstevel@tonic-gate construct_devinfo_tree(picl_nodehdl_t plafh, picl_nodehdl_t obph,
15337c478bd9Sstevel@tonic-gate     di_node_t dinode, char *parent_class)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate 	di_node_t	cnode;
15367c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
15377c478bd9Sstevel@tonic-gate 	char		nodeclass[PICL_CLASSNAMELEN_MAX];
15387c478bd9Sstevel@tonic-gate 	char		*nodename;
15397c478bd9Sstevel@tonic-gate 	int		err;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	err = PICL_SUCCESS;
15427c478bd9Sstevel@tonic-gate 	for (cnode = di_child_node(dinode); cnode != DI_NODE_NIL;
15437c478bd9Sstevel@tonic-gate 	    cnode = di_sibling_node(cnode)) {
15447c478bd9Sstevel@tonic-gate 		nodename = di_node_name(cnode);	/* PICL_PROP_NAME */
15457c478bd9Sstevel@tonic-gate 		if (nodename == NULL)
15467c478bd9Sstevel@tonic-gate 			continue;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 		err = get_node_class(nodeclass, cnode, nodename);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		if (err == 0) {
15517c478bd9Sstevel@tonic-gate 			err = construct_devtype_node(plafh, nodename,
15527c478bd9Sstevel@tonic-gate 			    nodeclass, cnode, &chdh);
15537c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
15547c478bd9Sstevel@tonic-gate 				return (err);
15557c478bd9Sstevel@tonic-gate 			err = construct_devinfo_tree(chdh, obph, cnode,
15567c478bd9Sstevel@tonic-gate 			    nodeclass);
15577c478bd9Sstevel@tonic-gate 		} else if (parent_class == NULL)
15587c478bd9Sstevel@tonic-gate 			err = construct_openprom_tree(obph, cnode);
15597c478bd9Sstevel@tonic-gate 		else
15607c478bd9Sstevel@tonic-gate 			continue;
15617c478bd9Sstevel@tonic-gate 		/*
15627c478bd9Sstevel@tonic-gate 		 * if parent_class is non NULL, skip the children nodes
15637c478bd9Sstevel@tonic-gate 		 * that don't have a valid device class - eliminates
15647c478bd9Sstevel@tonic-gate 		 * placeholder nodes (sd,...) from being created.
15657c478bd9Sstevel@tonic-gate 		 */
15667c478bd9Sstevel@tonic-gate 	}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	return (err);
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate  * This function is called from the event handler called from the daemon
15747c478bd9Sstevel@tonic-gate  * on PICL events.
15757c478bd9Sstevel@tonic-gate  *
15767c478bd9Sstevel@tonic-gate  * This routine traverses the children of the "dinode" device and
15777c478bd9Sstevel@tonic-gate  * creates a PICL node for each child not found in the PICL tree and
15787c478bd9Sstevel@tonic-gate  * invokes itself recursively to create a subtree for the newly created
15797c478bd9Sstevel@tonic-gate  * child node. It also checks if the node being created is a meory
15807c478bd9Sstevel@tonic-gate  * controller. If so, it posts PICLEVENT_MC_ADDED PICL event to the PICL
15817c478bd9Sstevel@tonic-gate  * framework.
15827c478bd9Sstevel@tonic-gate  */
15837c478bd9Sstevel@tonic-gate static int
15847c478bd9Sstevel@tonic-gate update_subtree(picl_nodehdl_t nodeh, di_node_t dinode)
15857c478bd9Sstevel@tonic-gate {
15867c478bd9Sstevel@tonic-gate 	di_node_t	cnode;
15877c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
15887c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nh;
15897c478bd9Sstevel@tonic-gate 	char		*nodename;
15907c478bd9Sstevel@tonic-gate 	char		nodeclass[PICL_CLASSNAMELEN_MAX];
15917c478bd9Sstevel@tonic-gate 	char		*path_buf;
15927c478bd9Sstevel@tonic-gate 	char		buf[MAX_UNIT_ADDRESS_LEN];
15937c478bd9Sstevel@tonic-gate 	char		unitaddr[MAX_UNIT_ADDRESS_LEN];
15947c478bd9Sstevel@tonic-gate 	char		path_w_ua[MAXPATHLEN];
15957c478bd9Sstevel@tonic-gate 	char		path_wo_ua[MAXPATHLEN];
15967c478bd9Sstevel@tonic-gate 	char		*strp;
15977c478bd9Sstevel@tonic-gate 	int		gotit;
15987c478bd9Sstevel@tonic-gate 	int		err;
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	for (cnode = di_child_node(dinode); cnode != DI_NODE_NIL;
16017c478bd9Sstevel@tonic-gate 	    cnode = di_sibling_node(cnode)) {
16027c478bd9Sstevel@tonic-gate 		path_buf = di_devfs_path(cnode);
16037c478bd9Sstevel@tonic-gate 		if (path_buf == NULL)
16047c478bd9Sstevel@tonic-gate 			continue;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 		nodename = di_node_name(cnode);
16077c478bd9Sstevel@tonic-gate 		if (nodename == NULL) {
16087c478bd9Sstevel@tonic-gate 			di_devfs_path_free(path_buf);
16097c478bd9Sstevel@tonic-gate 			continue;
16107c478bd9Sstevel@tonic-gate 		}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 		err = get_node_class(nodeclass, cnode, nodename);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 		if (err < 0) {
16157c478bd9Sstevel@tonic-gate 			di_devfs_path_free(path_buf);
16167c478bd9Sstevel@tonic-gate 			continue;
16177c478bd9Sstevel@tonic-gate 		}
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 		/*
16207c478bd9Sstevel@tonic-gate 		 * this is quite complicated - both path_buf and any nodes
16217c478bd9Sstevel@tonic-gate 		 * already in the picl tree may, or may not, have the
16227c478bd9Sstevel@tonic-gate 		 * @<unit_addr> at the end of their names. So we must
16237c478bd9Sstevel@tonic-gate 		 * take path_buf and work out what the device path would
16247c478bd9Sstevel@tonic-gate 		 * be both with and without the unit_address, then search
16257c478bd9Sstevel@tonic-gate 		 * the picl tree for both forms.
16267c478bd9Sstevel@tonic-gate 		 */
16277c478bd9Sstevel@tonic-gate 		if (((strp = strrchr(path_buf, '/')) != NULL) &&
16287c478bd9Sstevel@tonic-gate 		    strchr(strp, '@') == NULL) {
16297c478bd9Sstevel@tonic-gate 			/*
16307c478bd9Sstevel@tonic-gate 			 * this is an unattached node - so the path is not
16317c478bd9Sstevel@tonic-gate 			 * unique. Need to find out which node it is.
16327c478bd9Sstevel@tonic-gate 			 * Find the unit_address from the obp properties.
16337c478bd9Sstevel@tonic-gate 			 */
16347c478bd9Sstevel@tonic-gate 			err = ptree_create_node(nodename, nodeclass, &chdh);
16357c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
16367c478bd9Sstevel@tonic-gate 				return (err);
16377c478bd9Sstevel@tonic-gate 			(void) add_openprom_props(chdh, cnode);
16387c478bd9Sstevel@tonic-gate 			err = get_unitaddr(nodeh, chdh, unitaddr,
16397c478bd9Sstevel@tonic-gate 			    sizeof (unitaddr));
16407c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
16417c478bd9Sstevel@tonic-gate 				return (err);
16427c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(chdh);
16437c478bd9Sstevel@tonic-gate 			(void) snprintf(path_w_ua, sizeof (path_w_ua), "%s@%s",
16447c478bd9Sstevel@tonic-gate 			    path_buf, unitaddr);
16457c478bd9Sstevel@tonic-gate 			(void) snprintf(path_wo_ua, sizeof (path_wo_ua), "%s",
16467c478bd9Sstevel@tonic-gate 			    path_buf);
16477c478bd9Sstevel@tonic-gate 		} else {
16487c478bd9Sstevel@tonic-gate 			/*
16497c478bd9Sstevel@tonic-gate 			 * this is an attached node - so the path is unique
16507c478bd9Sstevel@tonic-gate 			 */
16517c478bd9Sstevel@tonic-gate 			(void) snprintf(path_w_ua, sizeof (path_w_ua), "%s",
16527c478bd9Sstevel@tonic-gate 			    path_buf);
16537c478bd9Sstevel@tonic-gate 			(void) snprintf(path_wo_ua, sizeof (path_wo_ua), "%s",
16547c478bd9Sstevel@tonic-gate 			    path_buf);
16557c478bd9Sstevel@tonic-gate 			strp = strrchr(path_wo_ua, '@');
16567c478bd9Sstevel@tonic-gate 			*strp++ = '\0';
16577c478bd9Sstevel@tonic-gate 			(void) snprintf(unitaddr, sizeof (unitaddr), "%s",
16587c478bd9Sstevel@tonic-gate 			    strp);
16597c478bd9Sstevel@tonic-gate 		}
16607c478bd9Sstevel@tonic-gate 		/*
16617c478bd9Sstevel@tonic-gate 		 * first look for node with unit address in devfs_path
16627c478bd9Sstevel@tonic-gate 		 */
16637c478bd9Sstevel@tonic-gate 		if (ptree_find_node(nodeh, PICL_PROP_DEVFS_PATH,
16647c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, path_w_ua, strlen(path_w_ua) + 1,
16657c478bd9Sstevel@tonic-gate 		    &nh) == PICL_SUCCESS) {
16667c478bd9Sstevel@tonic-gate 			/*
16677c478bd9Sstevel@tonic-gate 			 * node already there - there's nothing we need to do
16687c478bd9Sstevel@tonic-gate 			 */
16697c478bd9Sstevel@tonic-gate 			if (picldevtree_debug > 1)
16707c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO,
16717c478bd9Sstevel@tonic-gate 				    "update_subtree: path:%s node exists\n",
16727c478bd9Sstevel@tonic-gate 				    path_buf);
16737c478bd9Sstevel@tonic-gate 			di_devfs_path_free(path_buf);
16747c478bd9Sstevel@tonic-gate 			continue;
16757c478bd9Sstevel@tonic-gate 		}
16767c478bd9Sstevel@tonic-gate 		/*
16777c478bd9Sstevel@tonic-gate 		 * now look for node without unit address in devfs_path.
16787c478bd9Sstevel@tonic-gate 		 * This might be just one out of several
16797c478bd9Sstevel@tonic-gate 		 * nodes - need to check all siblings
16807c478bd9Sstevel@tonic-gate 		 */
16817c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD,
16827c478bd9Sstevel@tonic-gate 		    &chdh, sizeof (chdh));
16837c478bd9Sstevel@tonic-gate 		if ((err != PICL_SUCCESS) && (err != PICL_PROPNOTFOUND))
16847c478bd9Sstevel@tonic-gate 			return (err);
16857c478bd9Sstevel@tonic-gate 		gotit = 0;
16867c478bd9Sstevel@tonic-gate 		while (err == PICL_SUCCESS) {
16877c478bd9Sstevel@tonic-gate 			err = ptree_get_propval_by_name(chdh,
16887c478bd9Sstevel@tonic-gate 			    PICL_PROP_DEVFS_PATH, buf, sizeof (buf));
16897c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
16907c478bd9Sstevel@tonic-gate 				return (err);
16917c478bd9Sstevel@tonic-gate 			if (strcmp(buf, path_wo_ua) == 0) {
16927c478bd9Sstevel@tonic-gate 				err = ptree_get_propval_by_name(chdh,
16937c478bd9Sstevel@tonic-gate 				    PICL_PROP_UNIT_ADDRESS, buf, sizeof (buf));
16947c478bd9Sstevel@tonic-gate 				if (err != PICL_SUCCESS)
16957c478bd9Sstevel@tonic-gate 					return (err);
16967c478bd9Sstevel@tonic-gate 				if (strcmp(buf, unitaddr) == 0) {
16977c478bd9Sstevel@tonic-gate 					gotit = 1;
16987c478bd9Sstevel@tonic-gate 					break;
16997c478bd9Sstevel@tonic-gate 				}
17007c478bd9Sstevel@tonic-gate 			}
17017c478bd9Sstevel@tonic-gate 			err = ptree_get_propval_by_name(chdh,
17027c478bd9Sstevel@tonic-gate 			    PICL_PROP_PEER, &chdh, sizeof (chdh));
17037c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
17047c478bd9Sstevel@tonic-gate 				break;
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 		if (gotit) {
17077c478bd9Sstevel@tonic-gate 			/*
17087c478bd9Sstevel@tonic-gate 			 * node already there - there's nothing we need to do
17097c478bd9Sstevel@tonic-gate 			 */
17107c478bd9Sstevel@tonic-gate 			if (picldevtree_debug > 1)
17117c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO,
17127c478bd9Sstevel@tonic-gate 				    "update_subtree: path:%s node exists\n",
17137c478bd9Sstevel@tonic-gate 				    path_buf);
17147c478bd9Sstevel@tonic-gate 			di_devfs_path_free(path_buf);
17157c478bd9Sstevel@tonic-gate 			continue;
17167c478bd9Sstevel@tonic-gate 		}
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate #define	IS_MC(x)	(strcmp(x, PICL_CLASS_MEMORY_CONTROLLER) == 0 ? 1 : 0)
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 		if (construct_devtype_node(nodeh, nodename, nodeclass, cnode,
17217c478bd9Sstevel@tonic-gate 		    &chdh) == PICL_SUCCESS) {
17227c478bd9Sstevel@tonic-gate 			if (picldevtree_debug)
17237c478bd9Sstevel@tonic-gate 				syslog(LOG_INFO,
17247c478bd9Sstevel@tonic-gate 				    "picldevtree: added node:%s path:%s\n",
17257c478bd9Sstevel@tonic-gate 				    nodename, path_buf);
17267c478bd9Sstevel@tonic-gate 			if (IS_MC(nodeclass)) {
17277c478bd9Sstevel@tonic-gate 				if (post_mc_event(PICLEVENT_MC_ADDED, chdh) !=
17287c478bd9Sstevel@tonic-gate 				    PICL_SUCCESS)
17297c478bd9Sstevel@tonic-gate 					syslog(LOG_WARNING, PICL_EVENT_DROPPED,
17307c478bd9Sstevel@tonic-gate 					    PICLEVENT_MC_ADDED);
17317c478bd9Sstevel@tonic-gate 			}
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 			di_devfs_path_free(path_buf);
17347c478bd9Sstevel@tonic-gate 			(void) update_subtree(chdh, cnode);
17357c478bd9Sstevel@tonic-gate 		}
17367c478bd9Sstevel@tonic-gate 	}
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
1742bdee4892SMichael Bergknoff /*
1743bdee4892SMichael Bergknoff  * Check for a stale OBP node. EINVAL is returned from the openprom(7D) driver
1744bdee4892SMichael Bergknoff  * if the nodeid stored in the snapshot is not valid.
1745bdee4892SMichael Bergknoff  */
1746bdee4892SMichael Bergknoff static int
1747bdee4892SMichael Bergknoff check_stale_node(di_node_t node, void *arg)
1748bdee4892SMichael Bergknoff {
1749bdee4892SMichael Bergknoff 	di_prom_prop_t	promp;
1750bdee4892SMichael Bergknoff 
1751bdee4892SMichael Bergknoff 	errno = 0;
1752bdee4892SMichael Bergknoff 	promp = di_prom_prop_next(ph, node, DI_PROM_PROP_NIL);
1753bdee4892SMichael Bergknoff 	if (promp == DI_PROM_PROP_NIL && errno == EINVAL) {
1754bdee4892SMichael Bergknoff 		snapshot_stale = 1;
1755bdee4892SMichael Bergknoff 		return (DI_WALK_TERMINATE);
1756bdee4892SMichael Bergknoff 	}
1757bdee4892SMichael Bergknoff 	return (DI_WALK_CONTINUE);
1758bdee4892SMichael Bergknoff }
1759bdee4892SMichael Bergknoff 
1760bdee4892SMichael Bergknoff /*
1761bdee4892SMichael Bergknoff  * Walk the snapshot and check the OBP properties of each node.
1762bdee4892SMichael Bergknoff  */
1763bdee4892SMichael Bergknoff static int
1764bdee4892SMichael Bergknoff is_snapshot_stale(di_node_t root)
1765bdee4892SMichael Bergknoff {
1766bdee4892SMichael Bergknoff 	snapshot_stale = 0;
1767bdee4892SMichael Bergknoff 	di_walk_node(root, DI_WALK_CLDFIRST, NULL, check_stale_node);
1768bdee4892SMichael Bergknoff 	return (snapshot_stale);
1769bdee4892SMichael Bergknoff }
1770bdee4892SMichael Bergknoff 
17717c478bd9Sstevel@tonic-gate /*
17727c478bd9Sstevel@tonic-gate  * This function processes the data from libdevinfo and creates nodes
17737c478bd9Sstevel@tonic-gate  * in the PICL tree.
17747c478bd9Sstevel@tonic-gate  */
17757c478bd9Sstevel@tonic-gate static int
17767c478bd9Sstevel@tonic-gate libdevinfo_init(picl_nodehdl_t rooth)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	di_node_t	di_root;
17797c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	plafh;
17807c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	obph;
17817c478bd9Sstevel@tonic-gate 	int		err;
17827c478bd9Sstevel@tonic-gate 
1783ad159281Sarutz 	/*
1784ad159281Sarutz 	 * Use DINFOCACHE so that we obtain all attributes for all
1785ad159281Sarutz 	 * device instances (without necessarily doing a load/attach
1786ad159281Sarutz 	 * of all drivers).  Once the (on-disk) cache file is built, it
1787ad159281Sarutz 	 * exists over a reboot and can be read into memory at a very
1788ad159281Sarutz 	 * low cost.
1789ad159281Sarutz 	 */
1790ad159281Sarutz 	if ((di_root = di_init("/", DINFOCACHE)) == DI_NODE_NIL)
17917c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if ((ph = di_prom_init()) == NULL)
17947c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
1795bdee4892SMichael Bergknoff 
1796bdee4892SMichael Bergknoff 	/*
1797bdee4892SMichael Bergknoff 	 * Check if the snapshot cache contains stale OBP nodeid references.
1798bdee4892SMichael Bergknoff 	 * If it does release the snapshot and obtain a live snapshot from the
1799bdee4892SMichael Bergknoff 	 * kernel.
1800bdee4892SMichael Bergknoff 	 */
1801bdee4892SMichael Bergknoff 	if (is_snapshot_stale(di_root)) {
1802bdee4892SMichael Bergknoff 		syslog(LOG_INFO, "picld detected stale snapshot cache");
1803bdee4892SMichael Bergknoff 		di_fini(di_root);
1804bdee4892SMichael Bergknoff 		if ((di_root = di_init("/", DINFOCPYALL | DINFOFORCE)) ==
1805bdee4892SMichael Bergknoff 		    DI_NODE_NIL) {
1806bdee4892SMichael Bergknoff 			return (PICL_FAILURE);
1807bdee4892SMichael Bergknoff 		}
1808bdee4892SMichael Bergknoff 	}
1809bdee4892SMichael Bergknoff 
18107c478bd9Sstevel@tonic-gate 	/*
18117c478bd9Sstevel@tonic-gate 	 * create platform PICL node using di_root node
18127c478bd9Sstevel@tonic-gate 	 */
18137c478bd9Sstevel@tonic-gate 	err = construct_picl_platform(rooth, di_root, &plafh);
18147c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
18157c478bd9Sstevel@tonic-gate 		di_fini(di_root);
18167c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	err = construct_picl_openprom(rooth, &obph);
18207c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
18217c478bd9Sstevel@tonic-gate 		di_fini(di_root);
18227c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	(void) construct_devinfo_tree(plafh, obph, di_root, NULL);
18267c478bd9Sstevel@tonic-gate 	if (ph) {
18277c478bd9Sstevel@tonic-gate 		di_prom_fini(ph);
18287c478bd9Sstevel@tonic-gate 		ph = NULL;
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 	di_fini(di_root);
18317c478bd9Sstevel@tonic-gate 	return (err);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate /*
18357c478bd9Sstevel@tonic-gate  * This function returns the integer property value
18367c478bd9Sstevel@tonic-gate  */
18377c478bd9Sstevel@tonic-gate static int
18387c478bd9Sstevel@tonic-gate get_int_propval_by_name(picl_nodehdl_t	nodeh, char *pname, int *ival)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	int	err;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(nodeh, pname, ival,
18437c478bd9Sstevel@tonic-gate 	    sizeof (int));
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	return (err);
18467c478bd9Sstevel@tonic-gate }
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate /*
18497c478bd9Sstevel@tonic-gate  * This function returns the port ID (or CPU ID in the case of CMP cores)
18507c478bd9Sstevel@tonic-gate  * of the specific CPU node handle.  If upa_portid exists, return its value.
18517c478bd9Sstevel@tonic-gate  * Otherwise, return portid/cpuid.
18527c478bd9Sstevel@tonic-gate  */
18537c478bd9Sstevel@tonic-gate static int
18547c478bd9Sstevel@tonic-gate get_cpu_portid(picl_nodehdl_t modh, int *id)
18557c478bd9Sstevel@tonic-gate {
18567c478bd9Sstevel@tonic-gate 	int	err;
18577c478bd9Sstevel@tonic-gate 
18585e3e415aSfw 	if (strcmp(mach_name, "sun4u") == 0 ||
18595e3e415aSfw 	    strcmp(mach_name, "sun4v") == 0) {
18607c478bd9Sstevel@tonic-gate 		err = get_int_propval_by_name(modh, OBP_PROP_UPA_PORTID, id);
18617c478bd9Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
18627c478bd9Sstevel@tonic-gate 			return (err);
18637c478bd9Sstevel@tonic-gate 		err = get_int_propval_by_name(modh, OBP_PROP_PORTID, id);
18647c478bd9Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
18657c478bd9Sstevel@tonic-gate 			return (err);
18667c478bd9Sstevel@tonic-gate 		return (get_int_propval_by_name(modh, OBP_PROP_CPUID, id));
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 	if (strcmp(mach_name, "i86pc") == 0)
18697c478bd9Sstevel@tonic-gate 		return (get_int_propval_by_name(modh, PICL_PROP_INSTANCE, id));
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate /*
18757c478bd9Sstevel@tonic-gate  * This function is the volatile read access function of CPU state
18767c478bd9Sstevel@tonic-gate  * property
18777c478bd9Sstevel@tonic-gate  */
18787c478bd9Sstevel@tonic-gate static int
18797c478bd9Sstevel@tonic-gate get_pi_state(ptree_rarg_t *rarg, void *vbuf)
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	int	id;
18827c478bd9Sstevel@tonic-gate 	int	err;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
18857c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
18867c478bd9Sstevel@tonic-gate 		return (err);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	switch (p_online(id, P_STATUS)) {
18897c478bd9Sstevel@tonic-gate 	case P_ONLINE:
18907c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, PS_ONLINE, MAX_STATE_SIZE);
18917c478bd9Sstevel@tonic-gate 		break;
18927c478bd9Sstevel@tonic-gate 	case P_OFFLINE:
18937c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, PS_OFFLINE, MAX_STATE_SIZE);
18947c478bd9Sstevel@tonic-gate 		break;
18957c478bd9Sstevel@tonic-gate 	case P_NOINTR:
18967c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, PS_NOINTR, MAX_STATE_SIZE);
18977c478bd9Sstevel@tonic-gate 		break;
18987c478bd9Sstevel@tonic-gate 	case P_SPARE:
18997c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, PS_SPARE, MAX_STATE_SIZE);
19007c478bd9Sstevel@tonic-gate 		break;
19017c478bd9Sstevel@tonic-gate 	case P_FAULTED:
19027c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, PS_FAULTED, MAX_STATE_SIZE);
19037c478bd9Sstevel@tonic-gate 		break;
19047c478bd9Sstevel@tonic-gate 	case P_POWEROFF:
19057c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, PS_POWEROFF, MAX_STATE_SIZE);
19067c478bd9Sstevel@tonic-gate 		break;
19077c478bd9Sstevel@tonic-gate 	default:
19087c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, "unknown", MAX_STATE_SIZE);
19097c478bd9Sstevel@tonic-gate 		break;
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /*
19157c478bd9Sstevel@tonic-gate  * This function is the volatile read access function of CPU processor_type
19167c478bd9Sstevel@tonic-gate  * property
19177c478bd9Sstevel@tonic-gate  */
19187c478bd9Sstevel@tonic-gate static int
19197c478bd9Sstevel@tonic-gate get_processor_type(ptree_rarg_t *rarg, void *vbuf)
19207c478bd9Sstevel@tonic-gate {
19217c478bd9Sstevel@tonic-gate 	processor_info_t	cpu_info;
19227c478bd9Sstevel@tonic-gate 	int	id;
19237c478bd9Sstevel@tonic-gate 	int	err;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
19267c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
19277c478bd9Sstevel@tonic-gate 		return (err);
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	if (processor_info(id, &cpu_info) >= 0) {
19307c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, cpu_info.pi_processor_type, PI_TYPELEN);
19317c478bd9Sstevel@tonic-gate 	}
19327c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate /*
19367c478bd9Sstevel@tonic-gate  * This function is the volatile read access function of CPU fputypes
19377c478bd9Sstevel@tonic-gate  * property
19387c478bd9Sstevel@tonic-gate  */
19397c478bd9Sstevel@tonic-gate static int
19407c478bd9Sstevel@tonic-gate get_fputypes(ptree_rarg_t *rarg, void *vbuf)
19417c478bd9Sstevel@tonic-gate {
19427c478bd9Sstevel@tonic-gate 	processor_info_t	cpu_info;
19437c478bd9Sstevel@tonic-gate 	int	id;
19447c478bd9Sstevel@tonic-gate 	int	err;
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
19477c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
19487c478bd9Sstevel@tonic-gate 		return (err);
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	if (processor_info(id, &cpu_info) >= 0) {
19517c478bd9Sstevel@tonic-gate 		(void) strlcpy(vbuf, cpu_info.pi_fputypes, PI_FPUTYPE);
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
19547c478bd9Sstevel@tonic-gate }
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate /*
19577c478bd9Sstevel@tonic-gate  * This function is the volatile read access function of CPU StateBegin
19587c478bd9Sstevel@tonic-gate  * property. To minimize overhead, use kstat_chain_update() to refresh
19597c478bd9Sstevel@tonic-gate  * the kstat header info as opposed to invoking kstat_open() every time.
19607c478bd9Sstevel@tonic-gate  */
19617c478bd9Sstevel@tonic-gate static int
19627c478bd9Sstevel@tonic-gate get_pi_state_begin(ptree_rarg_t *rarg, void *vbuf)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate 	int 			err;
19657c478bd9Sstevel@tonic-gate 	int			cpu_id;
19667c478bd9Sstevel@tonic-gate 	static kstat_ctl_t	*kc = NULL;
19677c478bd9Sstevel@tonic-gate 	static pthread_mutex_t	kc_mutex = PTHREAD_MUTEX_INITIALIZER;
19687c478bd9Sstevel@tonic-gate 	kstat_t			*kp;
19697c478bd9Sstevel@tonic-gate 	kstat_named_t		*kn;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &cpu_id);
19727c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
19737c478bd9Sstevel@tonic-gate 		return (err);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&kc_mutex);
19767c478bd9Sstevel@tonic-gate 	if (kc == NULL)
19777c478bd9Sstevel@tonic-gate 		kc = kstat_open();
19787c478bd9Sstevel@tonic-gate 	else if (kstat_chain_update(kc) == -1) {
19797c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
19807c478bd9Sstevel@tonic-gate 		kc = kstat_open();
19817c478bd9Sstevel@tonic-gate 	}
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	if (kc == NULL) {
19847c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&kc_mutex);
19857c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
19867c478bd9Sstevel@tonic-gate 	}
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	/* Get the state_begin from kstat */
19897c478bd9Sstevel@tonic-gate 	if ((kp = kstat_lookup(kc, KSTAT_CPU_INFO, cpu_id, NULL)) == NULL ||
19907c478bd9Sstevel@tonic-gate 	    kp->ks_type != KSTAT_TYPE_NAMED || kstat_read(kc, kp, 0) < 0) {
19917c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&kc_mutex);
19927c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
19937c478bd9Sstevel@tonic-gate 	}
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	kn = kstat_data_lookup(kp, KSTAT_STATE_BEGIN);
19967c478bd9Sstevel@tonic-gate 	if (kn) {
19977c478bd9Sstevel@tonic-gate 		*(uint64_t *)vbuf = (uint64_t)kn->value.l;
19987c478bd9Sstevel@tonic-gate 		err = PICL_SUCCESS;
19997c478bd9Sstevel@tonic-gate 	} else
20007c478bd9Sstevel@tonic-gate 		err = PICL_FAILURE;
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&kc_mutex);
20037c478bd9Sstevel@tonic-gate 	return (err);
20047c478bd9Sstevel@tonic-gate }
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate /*
20077c478bd9Sstevel@tonic-gate  * This function adds CPU information to the CPU nodes
20087c478bd9Sstevel@tonic-gate  */
20097c478bd9Sstevel@tonic-gate /* ARGSUSED */
20107c478bd9Sstevel@tonic-gate static int
20117c478bd9Sstevel@tonic-gate add_processor_info(picl_nodehdl_t cpuh, void *args)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	int 			err;
20147c478bd9Sstevel@tonic-gate 	int			cpu_id;
20157c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
20167c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	err = get_cpu_portid(cpuh, &cpu_id);
20197c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
20207c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
20217c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
20227c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ, sizeof (int), PICL_PROP_ID, NULL, NULL);
20237c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(cpuh, &propinfo, &cpu_id, NULL);
20247c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
20257c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
20287c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), MAX_STATE_SIZE,
20297c478bd9Sstevel@tonic-gate 	    PICL_PROP_STATE, get_pi_state, NULL);
20307c478bd9Sstevel@tonic-gate 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
20337c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), PI_TYPELEN,
20347c478bd9Sstevel@tonic-gate 	    PICL_PROP_PROCESSOR_TYPE, get_processor_type, NULL);
20357c478bd9Sstevel@tonic-gate 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
20387c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), PI_FPUTYPE,
20397c478bd9Sstevel@tonic-gate 	    PICL_PROP_FPUTYPE, get_fputypes, NULL);
20407c478bd9Sstevel@tonic-gate 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
20437c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_TIMESTAMP, PICL_READ|PICL_VOLATILE, sizeof (uint64_t),
20447c478bd9Sstevel@tonic-gate 	    PICL_PROP_STATE_BEGIN, get_pi_state_begin, NULL);
20457c478bd9Sstevel@tonic-gate 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	return (PICL_WALK_CONTINUE);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate /*
20517c478bd9Sstevel@tonic-gate  * This function sets up the "ID" property in every CPU nodes
20527c478bd9Sstevel@tonic-gate  * and adds processor info
20537c478bd9Sstevel@tonic-gate  */
20547c478bd9Sstevel@tonic-gate static int
20557c478bd9Sstevel@tonic-gate setup_cpus(picl_nodehdl_t plafh)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate 	int 			err;
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	err = ptree_walk_tree_by_class(plafh, PICL_CLASS_CPU, NULL,
20607c478bd9Sstevel@tonic-gate 	    add_processor_info);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	return (err);
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate /*
20667c478bd9Sstevel@tonic-gate  * This function format's the manufacture's information for FFB display
20677c478bd9Sstevel@tonic-gate  * devices
20687c478bd9Sstevel@tonic-gate  */
20697c478bd9Sstevel@tonic-gate static void
20707c478bd9Sstevel@tonic-gate fmt_manf_id(manuf_t manufid, int bufsz, char *outbuf)
20717c478bd9Sstevel@tonic-gate {
20727c478bd9Sstevel@tonic-gate 	/*
20737c478bd9Sstevel@tonic-gate 	 * Format the manufacturer's info.  Note a small inconsistency we
20747c478bd9Sstevel@tonic-gate 	 * have to work around - Brooktree has it's part number in decimal,
20757c478bd9Sstevel@tonic-gate 	 * while Mitsubishi has it's part number in hex.
20767c478bd9Sstevel@tonic-gate 	 */
20777c478bd9Sstevel@tonic-gate 	switch (manufid.fld.manf) {
20787c478bd9Sstevel@tonic-gate 	case MANF_BROOKTREE:
20797c478bd9Sstevel@tonic-gate 		(void) snprintf(outbuf, bufsz, "%s %d, version %d",
20807c478bd9Sstevel@tonic-gate 		    "Brooktree", manufid.fld.partno, manufid.fld.version);
20817c478bd9Sstevel@tonic-gate 		break;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	case MANF_MITSUBISHI:
20847c478bd9Sstevel@tonic-gate 		(void) snprintf(outbuf, bufsz, "%s %x, version %d",
20857c478bd9Sstevel@tonic-gate 		    "Mitsubishi", manufid.fld.partno, manufid.fld.version);
20867c478bd9Sstevel@tonic-gate 		break;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	default:
20897c478bd9Sstevel@tonic-gate 		(void) snprintf(outbuf, bufsz,
20907c478bd9Sstevel@tonic-gate 		    "JED code %d, Part num 0x%x, version %d",
20917c478bd9Sstevel@tonic-gate 		    manufid.fld.manf, manufid.fld.partno, manufid.fld.version);
20927c478bd9Sstevel@tonic-gate 	}
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate /*
20967c478bd9Sstevel@tonic-gate  * If it's an ffb device, open ffb devices and return PICL_SUCCESS
20977c478bd9Sstevel@tonic-gate  */
20987c478bd9Sstevel@tonic-gate static int
20997c478bd9Sstevel@tonic-gate open_ffb_device(picl_nodehdl_t ffbh, int *fd)
21007c478bd9Sstevel@tonic-gate {
21017c478bd9Sstevel@tonic-gate 	DIR 			*dirp;
21027c478bd9Sstevel@tonic-gate 	char 			devfs_path[PATH_MAX];
21037c478bd9Sstevel@tonic-gate 	char 			dev_path[PATH_MAX];
21047c478bd9Sstevel@tonic-gate 	char 			*devp;
21057c478bd9Sstevel@tonic-gate 	struct dirent 		*direntp;
21067c478bd9Sstevel@tonic-gate 	int			err;
21077c478bd9Sstevel@tonic-gate 	int			tmpfd;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	/* Get the devfs_path of the ffb devices */
21107c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(ffbh, PICL_PROP_DEVFS_PATH, devfs_path,
21117c478bd9Sstevel@tonic-gate 	    sizeof (devfs_path));
21127c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
21137c478bd9Sstevel@tonic-gate 		return (err);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	/* Get the device node name */
21167c478bd9Sstevel@tonic-gate 	devp = strrchr(devfs_path, '/');
21177c478bd9Sstevel@tonic-gate 	if (devp == NULL)
21187c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
21197c478bd9Sstevel@tonic-gate 	*devp = '\0';
21207c478bd9Sstevel@tonic-gate 	++devp;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	/*
21237c478bd9Sstevel@tonic-gate 	 * Check if device node name has the ffb string
21247c478bd9Sstevel@tonic-gate 	 * If not, assume it's not a ffb device.
21257c478bd9Sstevel@tonic-gate 	 */
21267c478bd9Sstevel@tonic-gate 	if (strstr(devp, FFB_NAME) == NULL)
21277c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	/*
21307c478bd9Sstevel@tonic-gate 	 * Get the parent path of the ffb device node.
21317c478bd9Sstevel@tonic-gate 	 */
21327c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_path, sizeof (dev_path), "%s/%s", "/devices",
21337c478bd9Sstevel@tonic-gate 	    devfs_path);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 	/*
21367c478bd9Sstevel@tonic-gate 	 * Since we don't know ffb's minor nodename,
21377c478bd9Sstevel@tonic-gate 	 * we need to search all the devices under its
21387c478bd9Sstevel@tonic-gate 	 * parent dir by comparing the node name
21397c478bd9Sstevel@tonic-gate 	 */
21407c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(dev_path)) == NULL)
21417c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	while ((direntp = readdir(dirp)) != NULL) {
21447c478bd9Sstevel@tonic-gate 		if (strstr(direntp->d_name, devp) != NULL) {
21457c478bd9Sstevel@tonic-gate 			(void) strcat(dev_path, "/");
21467c478bd9Sstevel@tonic-gate 			(void) strcat(dev_path, direntp->d_name);
21477c478bd9Sstevel@tonic-gate 			tmpfd = open(dev_path, O_RDWR);
21487c478bd9Sstevel@tonic-gate 			if (tmpfd < 0)
21497c478bd9Sstevel@tonic-gate 				continue;
21507c478bd9Sstevel@tonic-gate 			*fd = tmpfd;
21517c478bd9Sstevel@tonic-gate 			(void) closedir(dirp);
21527c478bd9Sstevel@tonic-gate 			return (PICL_SUCCESS);
21537c478bd9Sstevel@tonic-gate 		}
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
21577c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate /*
21617c478bd9Sstevel@tonic-gate  * This function recursively searches the tree for ffb display devices
21627c478bd9Sstevel@tonic-gate  * and add ffb config information
21637c478bd9Sstevel@tonic-gate  */
21647c478bd9Sstevel@tonic-gate static int
21657c478bd9Sstevel@tonic-gate add_ffb_config_info(picl_nodehdl_t rooth)
21667c478bd9Sstevel@tonic-gate {
21677c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
21687c478bd9Sstevel@tonic-gate 	int			err;
21697c478bd9Sstevel@tonic-gate 	char 			piclclass[PICL_CLASSNAMELEN_MAX];
21707c478bd9Sstevel@tonic-gate 	char 			manfidbuf[FFB_MANUF_BUFSIZE];
21717c478bd9Sstevel@tonic-gate 	int 			fd;
21727c478bd9Sstevel@tonic-gate 	int			board_rev;
21737c478bd9Sstevel@tonic-gate 	ffb_sys_info_t		fsi;
21747c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 	for (err = ptree_get_propval_by_name(rooth, PICL_PROP_CHILD, &nodeh,
21777c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
21785e3e415aSfw 	    err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER,
21795e3e415aSfw 	    &nodeh, sizeof (picl_nodehdl_t))) {
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
21827c478bd9Sstevel@tonic-gate 			return (err);
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME,
21857c478bd9Sstevel@tonic-gate 		    piclclass, PICL_CLASSNAMELEN_MAX);
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 		if ((err == PICL_SUCCESS) &&
21887c478bd9Sstevel@tonic-gate 		    (strcmp(piclclass, PICL_CLASS_DISPLAY) == 0)) {
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 			err = open_ffb_device(nodeh, &fd);
21917c478bd9Sstevel@tonic-gate 			if ((err == PICL_SUCCESS) &&
21927c478bd9Sstevel@tonic-gate 			    (ioctl(fd, FFB_SYS_INFO, &fsi) >= 0)) {
21937c478bd9Sstevel@tonic-gate 				(void) ptree_init_propinfo(&pinfo,
21947c478bd9Sstevel@tonic-gate 				    PTREE_PROPINFO_VERSION,
21957c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_UNSIGNED_INT, PICL_READ,
21967c478bd9Sstevel@tonic-gate 				    sizeof (int), PICL_PROP_FFB_BOARD_REV,
21977c478bd9Sstevel@tonic-gate 				    NULL, NULL);
21987c478bd9Sstevel@tonic-gate 				board_rev = fsi.ffb_strap_bits.fld.board_rev;
21997c478bd9Sstevel@tonic-gate 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
22007c478bd9Sstevel@tonic-gate 				    &board_rev, NULL);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 				fmt_manf_id(fsi.dac_version,
22037c478bd9Sstevel@tonic-gate 				    sizeof (manfidbuf), manfidbuf);
22047c478bd9Sstevel@tonic-gate 				(void) ptree_init_propinfo(&pinfo,
22057c478bd9Sstevel@tonic-gate 				    PTREE_PROPINFO_VERSION,
22067c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_CHARSTRING, PICL_READ,
22077c478bd9Sstevel@tonic-gate 				    strlen(manfidbuf) + 1,
22087c478bd9Sstevel@tonic-gate 				    PICL_PROP_FFB_DAC_VER, NULL, NULL);
22097c478bd9Sstevel@tonic-gate 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
22107c478bd9Sstevel@tonic-gate 				    manfidbuf, NULL);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 				fmt_manf_id(fsi.fbram_version,
22137c478bd9Sstevel@tonic-gate 				    sizeof (manfidbuf), manfidbuf);
22147c478bd9Sstevel@tonic-gate 				(void) ptree_init_propinfo(&pinfo,
22157c478bd9Sstevel@tonic-gate 				    PTREE_PROPINFO_VERSION,
22167c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_CHARSTRING, PICL_READ,
22177c478bd9Sstevel@tonic-gate 				    strlen(manfidbuf) + 1,
22187c478bd9Sstevel@tonic-gate 				    PICL_PROP_FFB_FBRAM_VER, NULL,
22197c478bd9Sstevel@tonic-gate 				    NULL);
22207c478bd9Sstevel@tonic-gate 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
22217c478bd9Sstevel@tonic-gate 				    manfidbuf, NULL);
22227c478bd9Sstevel@tonic-gate 				(void) close(fd);
22237c478bd9Sstevel@tonic-gate 			}
22247c478bd9Sstevel@tonic-gate 		} else if (add_ffb_config_info(nodeh) != PICL_SUCCESS)
22257c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
22287c478bd9Sstevel@tonic-gate }
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate static conf_entries_t *
22317c478bd9Sstevel@tonic-gate free_conf_entries(conf_entries_t *list)
22327c478bd9Sstevel@tonic-gate {
22337c478bd9Sstevel@tonic-gate 	conf_entries_t	*el;
22347c478bd9Sstevel@tonic-gate 	conf_entries_t	*del;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	if (list == NULL)
22377c478bd9Sstevel@tonic-gate 		return (NULL);
22387c478bd9Sstevel@tonic-gate 	el = list;
22397c478bd9Sstevel@tonic-gate 	while (el != NULL) {
22407c478bd9Sstevel@tonic-gate 		del = el;
22417c478bd9Sstevel@tonic-gate 		el = el->next;
22427c478bd9Sstevel@tonic-gate 		free(del->name);
22437c478bd9Sstevel@tonic-gate 		free(del->piclclass);
22447c478bd9Sstevel@tonic-gate 		free(del);
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 	return (el);
22477c478bd9Sstevel@tonic-gate }
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate /*
22507c478bd9Sstevel@tonic-gate  * Reading config order: platform, common
22517c478bd9Sstevel@tonic-gate  */
22527c478bd9Sstevel@tonic-gate static conf_entries_t *
22537c478bd9Sstevel@tonic-gate read_conf_file(char *fname, conf_entries_t *list)
22547c478bd9Sstevel@tonic-gate {
22557c478bd9Sstevel@tonic-gate 	FILE		*fp;
22567c478bd9Sstevel@tonic-gate 	char		lbuf[CONFFILE_LINELEN_MAX];
22577c478bd9Sstevel@tonic-gate 	char		*nametok;
22587c478bd9Sstevel@tonic-gate 	char		*classtok;
22597c478bd9Sstevel@tonic-gate 	conf_entries_t	*el;
22607c478bd9Sstevel@tonic-gate 	conf_entries_t	*ptr;
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	if (fname == NULL)
22637c478bd9Sstevel@tonic-gate 		return (list);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	fp = fopen(fname, "r");
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	if (fp == NULL)
22687c478bd9Sstevel@tonic-gate 		return (list);
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	while (fgets(lbuf, CONFFILE_LINELEN_MAX, fp) != NULL) {
22717c478bd9Sstevel@tonic-gate 		if ((lbuf[0] == CONFFILE_COMMENT_CHAR) || (lbuf[0] == '\n'))
22727c478bd9Sstevel@tonic-gate 			continue;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 		nametok = strtok(lbuf, " \t\n");
22757c478bd9Sstevel@tonic-gate 		if (nametok == NULL)
22767c478bd9Sstevel@tonic-gate 			continue;
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 		classtok = strtok(NULL, " \t\n");
22797c478bd9Sstevel@tonic-gate 		if (classtok == NULL)
22807c478bd9Sstevel@tonic-gate 			continue;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 		el = malloc(sizeof (conf_entries_t));
22837c478bd9Sstevel@tonic-gate 		if (el == NULL)
22847c478bd9Sstevel@tonic-gate 			break;
22857c478bd9Sstevel@tonic-gate 		el->name = strdup(nametok);
22867c478bd9Sstevel@tonic-gate 		el->piclclass = strdup(classtok);
22877c478bd9Sstevel@tonic-gate 		if ((el->name == NULL) || (el->piclclass == NULL)) {
22887c478bd9Sstevel@tonic-gate 			free(el);
22897c478bd9Sstevel@tonic-gate 			return (list);
22907c478bd9Sstevel@tonic-gate 		}
22917c478bd9Sstevel@tonic-gate 		el->next = NULL;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		/*
22947c478bd9Sstevel@tonic-gate 		 * Add it to the end of list
22957c478bd9Sstevel@tonic-gate 		 */
22967c478bd9Sstevel@tonic-gate 		if (list == NULL)
22977c478bd9Sstevel@tonic-gate 			list = el;
22987c478bd9Sstevel@tonic-gate 		else {
22997c478bd9Sstevel@tonic-gate 			ptr = list;
23007c478bd9Sstevel@tonic-gate 			while (ptr->next != NULL)
23017c478bd9Sstevel@tonic-gate 				ptr = ptr->next;
23027c478bd9Sstevel@tonic-gate 			ptr->next = el;
23037c478bd9Sstevel@tonic-gate 		}
23047c478bd9Sstevel@tonic-gate 
23057c478bd9Sstevel@tonic-gate 	}
23067c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
23077c478bd9Sstevel@tonic-gate 	return (list);
23087c478bd9Sstevel@tonic-gate }
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate /*
23117c478bd9Sstevel@tonic-gate  * Process the devtree conf file and set up the conf_name_class_map list
23127c478bd9Sstevel@tonic-gate  */
23137c478bd9Sstevel@tonic-gate static void
23147c478bd9Sstevel@tonic-gate process_devtree_conf_file(void)
23157c478bd9Sstevel@tonic-gate {
23167c478bd9Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
23177c478bd9Sstevel@tonic-gate 	char	pname[PATH_MAX];
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	conf_name_class_map = NULL;
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
23227c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
23237c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, DEVTREE_CONFFILE_NAME, PATH_MAX);
23247c478bd9Sstevel@tonic-gate 		conf_name_class_map = read_conf_file(pname,
23257c478bd9Sstevel@tonic-gate 		    conf_name_class_map);
23267c478bd9Sstevel@tonic-gate 	}
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
23297c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
23307c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, DEVTREE_CONFFILE_NAME, PATH_MAX);
23317c478bd9Sstevel@tonic-gate 		conf_name_class_map = read_conf_file(pname,
23327c478bd9Sstevel@tonic-gate 		    conf_name_class_map);
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
23367c478bd9Sstevel@tonic-gate 	    DEVTREE_CONFFILE_NAME);
23377c478bd9Sstevel@tonic-gate 	conf_name_class_map = read_conf_file(pname, conf_name_class_map);
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate static	asr_conf_entries_t	*conf_name_asr_map = NULL;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate static void
23437c478bd9Sstevel@tonic-gate free_asr_conf_entries(asr_conf_entries_t *list) {
23447c478bd9Sstevel@tonic-gate 	asr_conf_entries_t  *el;
23457c478bd9Sstevel@tonic-gate 	asr_conf_entries_t  *del;
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	el = list;
23487c478bd9Sstevel@tonic-gate 	while (el != NULL) {
23497c478bd9Sstevel@tonic-gate 		del = el;
23507c478bd9Sstevel@tonic-gate 		el = el->next;
23517c478bd9Sstevel@tonic-gate 		if (del->name)
23527c478bd9Sstevel@tonic-gate 			free(del->name);
23537c478bd9Sstevel@tonic-gate 		if (del->address)
23547c478bd9Sstevel@tonic-gate 			free(del->address);
23557c478bd9Sstevel@tonic-gate 		if (del->status)
23567c478bd9Sstevel@tonic-gate 			free(del->status);
23577c478bd9Sstevel@tonic-gate 		if (del->piclclass)
23587c478bd9Sstevel@tonic-gate 			free(del->piclclass);
23597c478bd9Sstevel@tonic-gate 		if (del->props)
23607c478bd9Sstevel@tonic-gate 			free(del->props);
23617c478bd9Sstevel@tonic-gate 		free(del);
23627c478bd9Sstevel@tonic-gate 	}
23637c478bd9Sstevel@tonic-gate }
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate /*
23667c478bd9Sstevel@tonic-gate  * Reading config order: platform, common
23677c478bd9Sstevel@tonic-gate  */
23687c478bd9Sstevel@tonic-gate static asr_conf_entries_t *
23697c478bd9Sstevel@tonic-gate read_asr_conf_file(char *fname, asr_conf_entries_t *list)
23707c478bd9Sstevel@tonic-gate {
23717c478bd9Sstevel@tonic-gate 	FILE		*fp;
23727c478bd9Sstevel@tonic-gate 	char		lbuf[CONFFILE_LINELEN_MAX];
23737c478bd9Sstevel@tonic-gate 	char		*nametok;
23747c478bd9Sstevel@tonic-gate 	char		*classtok;
23757c478bd9Sstevel@tonic-gate 	char		*statustok;
23767c478bd9Sstevel@tonic-gate 	char		*addresstok;
23777c478bd9Sstevel@tonic-gate 	char		*propstok;
23787c478bd9Sstevel@tonic-gate 	asr_conf_entries_t	*el;
23797c478bd9Sstevel@tonic-gate 	asr_conf_entries_t	*ptr;
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	if (fname == NULL)
23827c478bd9Sstevel@tonic-gate 		return (list);
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 	fp = fopen(fname, "r");
23857c478bd9Sstevel@tonic-gate 	if (fp == NULL)
23867c478bd9Sstevel@tonic-gate 		return (list);
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	while (fgets(lbuf, CONFFILE_LINELEN_MAX, fp) != NULL) {
23897c478bd9Sstevel@tonic-gate 		if ((lbuf[0] == CONFFILE_COMMENT_CHAR) || (lbuf[0] == '\n'))
23907c478bd9Sstevel@tonic-gate 			continue;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 		nametok = strtok(lbuf, " \t\n");
23937c478bd9Sstevel@tonic-gate 		if (nametok == NULL)
23947c478bd9Sstevel@tonic-gate 			continue;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		classtok = strtok(NULL, " \t\n");
23977c478bd9Sstevel@tonic-gate 		if (classtok == NULL)
23987c478bd9Sstevel@tonic-gate 			continue;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		statustok = strtok(NULL, " \t\n");
24017c478bd9Sstevel@tonic-gate 		if (statustok == NULL)
24027c478bd9Sstevel@tonic-gate 			continue;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 		addresstok = strtok(NULL, " \t\n");
24057c478bd9Sstevel@tonic-gate 		if (addresstok == NULL)
24067c478bd9Sstevel@tonic-gate 			continue;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 		/*
24097c478bd9Sstevel@tonic-gate 		 * props are optional
24107c478bd9Sstevel@tonic-gate 		 */
24117c478bd9Sstevel@tonic-gate 		propstok = strtok(NULL, " \t\n");
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 		el = malloc(sizeof (asr_conf_entries_t));
24147c478bd9Sstevel@tonic-gate 		if (el == NULL)
24157c478bd9Sstevel@tonic-gate 			break;
24167c478bd9Sstevel@tonic-gate 		el->name = strdup(nametok);
24177c478bd9Sstevel@tonic-gate 		el->piclclass = strdup(classtok);
24187c478bd9Sstevel@tonic-gate 		el->status = strdup(statustok);
24197c478bd9Sstevel@tonic-gate 		el->address = strdup(addresstok);
24207c478bd9Sstevel@tonic-gate 		if (propstok != NULL)
24217c478bd9Sstevel@tonic-gate 			el->props = strdup(propstok);
24227c478bd9Sstevel@tonic-gate 		else
24237c478bd9Sstevel@tonic-gate 			el->props = NULL;
24247c478bd9Sstevel@tonic-gate 		if ((el->name == NULL) || (el->piclclass == NULL) ||
24257c478bd9Sstevel@tonic-gate 		    (el->address == NULL) || (el->status == NULL)) {
24267c478bd9Sstevel@tonic-gate 			if (el->name)
24277c478bd9Sstevel@tonic-gate 				free(el->name);
24287c478bd9Sstevel@tonic-gate 			if (el->address)
24297c478bd9Sstevel@tonic-gate 				free(el->address);
24307c478bd9Sstevel@tonic-gate 			if (el->status)
24317c478bd9Sstevel@tonic-gate 				free(el->status);
24327c478bd9Sstevel@tonic-gate 			if (el->piclclass)
24337c478bd9Sstevel@tonic-gate 				free(el->piclclass);
24347c478bd9Sstevel@tonic-gate 			if (el->props)
24357c478bd9Sstevel@tonic-gate 				free(el->props);
24367c478bd9Sstevel@tonic-gate 			free(el);
24377c478bd9Sstevel@tonic-gate 			break;
24387c478bd9Sstevel@tonic-gate 		}
24397c478bd9Sstevel@tonic-gate 		el->next = NULL;
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 		/*
24427c478bd9Sstevel@tonic-gate 		 * Add it to the end of list
24437c478bd9Sstevel@tonic-gate 		 */
24447c478bd9Sstevel@tonic-gate 		if (list == NULL)
24457c478bd9Sstevel@tonic-gate 			list = el;
24467c478bd9Sstevel@tonic-gate 		else {
24477c478bd9Sstevel@tonic-gate 			ptr = list;
24487c478bd9Sstevel@tonic-gate 			while (ptr->next != NULL)
24497c478bd9Sstevel@tonic-gate 				ptr = ptr->next;
24507c478bd9Sstevel@tonic-gate 			ptr->next = el;
24517c478bd9Sstevel@tonic-gate 		}
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 	}
24547c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
24557c478bd9Sstevel@tonic-gate 	return (list);
24567c478bd9Sstevel@tonic-gate }
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate /*
24597c478bd9Sstevel@tonic-gate  * Process the asr conf file
24607c478bd9Sstevel@tonic-gate  */
24617c478bd9Sstevel@tonic-gate static void
24627c478bd9Sstevel@tonic-gate process_asrtree_conf_file(void)
24637c478bd9Sstevel@tonic-gate {
24647c478bd9Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
24657c478bd9Sstevel@tonic-gate 	char	pname[PATH_MAX];
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
24687c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
24697c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, ASRTREE_CONFFILE_NAME, PATH_MAX);
24707c478bd9Sstevel@tonic-gate 		conf_name_asr_map = read_asr_conf_file(pname,
24715e3e415aSfw 		    conf_name_asr_map);
24727c478bd9Sstevel@tonic-gate 	}
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
24757c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
24767c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, ASRTREE_CONFFILE_NAME, PATH_MAX);
24777c478bd9Sstevel@tonic-gate 		conf_name_asr_map = read_asr_conf_file(pname,
24785e3e415aSfw 		    conf_name_asr_map);
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
24827c478bd9Sstevel@tonic-gate 	    ASRTREE_CONFFILE_NAME);
24837c478bd9Sstevel@tonic-gate 	conf_name_asr_map = read_asr_conf_file(pname, conf_name_asr_map);
24847c478bd9Sstevel@tonic-gate }
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate /*
24877c478bd9Sstevel@tonic-gate  * This function reads the export file list from ASR
24887c478bd9Sstevel@tonic-gate  */
24897c478bd9Sstevel@tonic-gate static int
24907c478bd9Sstevel@tonic-gate get_asr_export_list(char **exportlist, int *exportlistlen)
24917c478bd9Sstevel@tonic-gate {
24927c478bd9Sstevel@tonic-gate 	struct openpromio oppbuf;
24937c478bd9Sstevel@tonic-gate 	struct openpromio *opp = &oppbuf;
24947c478bd9Sstevel@tonic-gate 	int d;
24957c478bd9Sstevel@tonic-gate 	int listsize;
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 	d = open("/dev/openprom", O_RDWR);
24987c478bd9Sstevel@tonic-gate 	if (d < 0)
24997c478bd9Sstevel@tonic-gate 		return (0);
25007c478bd9Sstevel@tonic-gate 
25017c478bd9Sstevel@tonic-gate 	if (ioctl(d, OPROMEXPORTLEN, opp) == -1) {
25027c478bd9Sstevel@tonic-gate 		(void) close(d);
25037c478bd9Sstevel@tonic-gate 		return (0);
25047c478bd9Sstevel@tonic-gate 	}
25057c478bd9Sstevel@tonic-gate 	listsize = opp->oprom_size;
25067c478bd9Sstevel@tonic-gate 	opp = (struct openpromio *)malloc(sizeof (struct openpromio) +
25077c478bd9Sstevel@tonic-gate 	    listsize);
25087c478bd9Sstevel@tonic-gate 	if (opp == NULL) {
25097c478bd9Sstevel@tonic-gate 		(void) close(d);
25107c478bd9Sstevel@tonic-gate 		return (0);
25117c478bd9Sstevel@tonic-gate 	}
25127c478bd9Sstevel@tonic-gate 	(void) memset(opp, '\0', sizeof (struct openpromio) + listsize);
25137c478bd9Sstevel@tonic-gate 	opp->oprom_size = listsize;
25147c478bd9Sstevel@tonic-gate 	if (ioctl(d, OPROMEXPORT, opp) == -1) {
25157c478bd9Sstevel@tonic-gate 		free(opp);
25167c478bd9Sstevel@tonic-gate 		(void) close(d);
25177c478bd9Sstevel@tonic-gate 		return (0);
25187c478bd9Sstevel@tonic-gate 	}
25197c478bd9Sstevel@tonic-gate 	*exportlist = malloc(listsize);
25207c478bd9Sstevel@tonic-gate 	if (*exportlist == NULL) {
25217c478bd9Sstevel@tonic-gate 		free(opp);
25227c478bd9Sstevel@tonic-gate 		(void) close(d);
25237c478bd9Sstevel@tonic-gate 		return (0);
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 	(void) memcpy(*exportlist, opp->oprom_array, opp->oprom_size);
25267c478bd9Sstevel@tonic-gate 	free(opp);
25277c478bd9Sstevel@tonic-gate 	*exportlistlen = opp->oprom_size;
25287c478bd9Sstevel@tonic-gate 	(void) close(d);
25297c478bd9Sstevel@tonic-gate 	return (1);
25307c478bd9Sstevel@tonic-gate }
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate /*
25337c478bd9Sstevel@tonic-gate  * Parses properties string, fills in triplet structure with first
25347c478bd9Sstevel@tonic-gate  * type, name, val triplet and returns pointer to next property.
25357c478bd9Sstevel@tonic-gate  * Returns NULL if no valid triplet found
25367c478bd9Sstevel@tonic-gate  * CAUTION: drops \0 characters over separator characters: if you
25377c478bd9Sstevel@tonic-gate  * want to parse the string twice, you'll have to take a copy.
25387c478bd9Sstevel@tonic-gate  */
25397c478bd9Sstevel@tonic-gate static char *
25407c478bd9Sstevel@tonic-gate parse_props_string(char *props, asr_prop_triplet_t *triplet)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	char	*prop_name;
25437c478bd9Sstevel@tonic-gate 	char	*prop_val;
25447c478bd9Sstevel@tonic-gate 	char	*prop_next;
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	prop_name = strchr(props, '?');
25477c478bd9Sstevel@tonic-gate 	if (prop_name == NULL)
25487c478bd9Sstevel@tonic-gate 		return (NULL);
25497c478bd9Sstevel@tonic-gate 	*prop_name++ = '\0';
25507c478bd9Sstevel@tonic-gate 	prop_val = strchr(prop_name, '=');
25517c478bd9Sstevel@tonic-gate 	if (prop_val == NULL)
25527c478bd9Sstevel@tonic-gate 		return (NULL);
25537c478bd9Sstevel@tonic-gate 	*prop_val++ = '\0';
25547c478bd9Sstevel@tonic-gate 	triplet->proptype = props;
25557c478bd9Sstevel@tonic-gate 	triplet->propname = prop_name;
25567c478bd9Sstevel@tonic-gate 	triplet->propval = prop_val;
25577c478bd9Sstevel@tonic-gate 	prop_next = strchr(prop_val, ':');
25587c478bd9Sstevel@tonic-gate 	if (prop_next == NULL)
25597c478bd9Sstevel@tonic-gate 		return (prop_val - 1);
25607c478bd9Sstevel@tonic-gate 	*prop_next++ = '\0';
25617c478bd9Sstevel@tonic-gate 	return (prop_next);
25627c478bd9Sstevel@tonic-gate }
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate static int
25657c478bd9Sstevel@tonic-gate add_status_prop(picl_nodehdl_t chdh, char *status)
25667c478bd9Sstevel@tonic-gate {
25677c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
25687c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
25697c478bd9Sstevel@tonic-gate 	int			err;
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
25727c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(status) + 1,
25737c478bd9Sstevel@tonic-gate 	    PICL_PROP_STATUS, NULL, NULL);
25747c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
25757c478bd9Sstevel@tonic-gate 		return (err);
25767c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(chdh, &propinfo, status, &proph);
25777c478bd9Sstevel@tonic-gate 	return (err);
25787c478bd9Sstevel@tonic-gate }
25797c478bd9Sstevel@tonic-gate 
25807c478bd9Sstevel@tonic-gate static void
25817c478bd9Sstevel@tonic-gate create_asr_node(char *parent, char *child, char *unitaddr, char *class,
25827c478bd9Sstevel@tonic-gate 	char *status, char *props)
25837c478bd9Sstevel@tonic-gate {
25847c478bd9Sstevel@tonic-gate 	char			ptreepath[PATH_MAX];
25857c478bd9Sstevel@tonic-gate 	char			nodename[PICL_PROPNAMELEN_MAX];
25867c478bd9Sstevel@tonic-gate 	char			ua[MAX_UNIT_ADDRESS_LEN];
25877c478bd9Sstevel@tonic-gate 	char			*props_copy = NULL;
25887c478bd9Sstevel@tonic-gate 	char			*next;
25897c478bd9Sstevel@tonic-gate 	char			*prop_string;
25907c478bd9Sstevel@tonic-gate 	boolean_t		found = B_FALSE;
25917c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
25927c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		chdh;
25937c478bd9Sstevel@tonic-gate 	asr_prop_triplet_t	triple;
25947c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
25957c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
25967c478bd9Sstevel@tonic-gate 	int			val;
25977c478bd9Sstevel@tonic-gate 	int			err;
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 	(void) strlcpy(ptreepath, PLATFORM_PATH, PATH_MAX);
26007c478bd9Sstevel@tonic-gate 	(void) strlcat(ptreepath, parent, PATH_MAX);
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	if (ptree_get_node_by_path(ptreepath, &nodeh) != PICL_SUCCESS)
26037c478bd9Sstevel@tonic-gate 		return;
26047c478bd9Sstevel@tonic-gate 	/*
26057c478bd9Sstevel@tonic-gate 	 * see if the required child node already exists
26067c478bd9Sstevel@tonic-gate 	 */
26077c478bd9Sstevel@tonic-gate 	for (err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
26087c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
26097c478bd9Sstevel@tonic-gate 	    err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
26105e3e415aSfw 	    sizeof (picl_nodehdl_t))) {
26117c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
26127c478bd9Sstevel@tonic-gate 			break;
26137c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_NAME,
26147c478bd9Sstevel@tonic-gate 		    (void *)nodename, PICL_PROPNAMELEN_MAX);
26157c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
26167c478bd9Sstevel@tonic-gate 			break;
26177c478bd9Sstevel@tonic-gate 		if (strcmp(nodename, child) != 0)
26187c478bd9Sstevel@tonic-gate 			continue;
26197c478bd9Sstevel@tonic-gate 		/*
26207c478bd9Sstevel@tonic-gate 		 * found a candidate child node
26217c478bd9Sstevel@tonic-gate 		 */
26227c478bd9Sstevel@tonic-gate 		if (unitaddr) {
26237c478bd9Sstevel@tonic-gate 			/*
26247c478bd9Sstevel@tonic-gate 			 * does it match the required unit address?
26257c478bd9Sstevel@tonic-gate 			 */
26267c478bd9Sstevel@tonic-gate 			err = ptree_get_propval_by_name(chdh,
26277c478bd9Sstevel@tonic-gate 			    PICL_PROP_UNIT_ADDRESS, ua, sizeof (ua));
26287c478bd9Sstevel@tonic-gate 			if (err == PICL_PROPNOTFOUND)
26297c478bd9Sstevel@tonic-gate 				continue;
26307c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
26317c478bd9Sstevel@tonic-gate 				break;
26327c478bd9Sstevel@tonic-gate 			if (strcmp(unitaddr, ua) != 0)
26337c478bd9Sstevel@tonic-gate 				continue;
26347c478bd9Sstevel@tonic-gate 		}
26357c478bd9Sstevel@tonic-gate 		if (props == NULL) {
26367c478bd9Sstevel@tonic-gate 			next = "";
26377c478bd9Sstevel@tonic-gate 		} else if (props_copy == NULL) {
26387c478bd9Sstevel@tonic-gate 			props_copy = strdup(props);
26397c478bd9Sstevel@tonic-gate 			if (props_copy == NULL)
26407c478bd9Sstevel@tonic-gate 				return;
26417c478bd9Sstevel@tonic-gate 			next = props_copy;
26427c478bd9Sstevel@tonic-gate 		}
26437c478bd9Sstevel@tonic-gate 		while ((next = parse_props_string(next, &triple)) != NULL) {
26447c478bd9Sstevel@tonic-gate 			err = ptree_get_prop_by_name(chdh, triple.propname,
26457c478bd9Sstevel@tonic-gate 			    &proph);
26467c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
26477c478bd9Sstevel@tonic-gate 				break;
26487c478bd9Sstevel@tonic-gate 			err = ptree_get_propinfo(proph, &propinfo);
26497c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
26507c478bd9Sstevel@tonic-gate 				break;
26517c478bd9Sstevel@tonic-gate 			err = PICL_FAILURE;
26527c478bd9Sstevel@tonic-gate 			switch (propinfo.piclinfo.type) {
26537c478bd9Sstevel@tonic-gate 			case PICL_PTYPE_INT:
26547c478bd9Sstevel@tonic-gate 			case PICL_PTYPE_UNSIGNED_INT:
26557c478bd9Sstevel@tonic-gate 				if (strcmp(triple.proptype, "I") != 0)
26567c478bd9Sstevel@tonic-gate 					break;
26577c478bd9Sstevel@tonic-gate 				err = ptree_get_propval(proph, (void  *)&val,
26587c478bd9Sstevel@tonic-gate 				    sizeof (val));
26597c478bd9Sstevel@tonic-gate 				if (err != PICL_SUCCESS)
26607c478bd9Sstevel@tonic-gate 					break;
26617c478bd9Sstevel@tonic-gate 				if (val != atoi(triple.propval))
26627c478bd9Sstevel@tonic-gate 					err = PICL_FAILURE;
26637c478bd9Sstevel@tonic-gate 				break;
26647c478bd9Sstevel@tonic-gate 			case PICL_PTYPE_CHARSTRING:
26657c478bd9Sstevel@tonic-gate 				if (strcmp(triple.proptype, "S") != 0)
26667c478bd9Sstevel@tonic-gate 					break;
26677c478bd9Sstevel@tonic-gate 				prop_string = malloc(propinfo.piclinfo.size);
26687c478bd9Sstevel@tonic-gate 				if (prop_string == NULL)
26697c478bd9Sstevel@tonic-gate 					break;
26707c478bd9Sstevel@tonic-gate 				err = ptree_get_propval(proph,
26717c478bd9Sstevel@tonic-gate 				    (void *)prop_string,
26727c478bd9Sstevel@tonic-gate 				    propinfo.piclinfo.size);
26737c478bd9Sstevel@tonic-gate 				if (err != PICL_SUCCESS) {
26747c478bd9Sstevel@tonic-gate 					free(prop_string);
26757c478bd9Sstevel@tonic-gate 					break;
26767c478bd9Sstevel@tonic-gate 				}
26777c478bd9Sstevel@tonic-gate 				if (strcmp(prop_string, triple.propval) != 0)
26787c478bd9Sstevel@tonic-gate 					err = PICL_FAILURE;
26797c478bd9Sstevel@tonic-gate 				free(prop_string);
26807c478bd9Sstevel@tonic-gate 				break;
26817c478bd9Sstevel@tonic-gate 			default:
26827c478bd9Sstevel@tonic-gate 				break;
26837c478bd9Sstevel@tonic-gate 			}
26847c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
26857c478bd9Sstevel@tonic-gate 				break;
26867c478bd9Sstevel@tonic-gate 			}
26877c478bd9Sstevel@tonic-gate 		}
26887c478bd9Sstevel@tonic-gate 		if (next == NULL) {
26897c478bd9Sstevel@tonic-gate 			found = B_TRUE;
26907c478bd9Sstevel@tonic-gate 			break;
26917c478bd9Sstevel@tonic-gate 		}
26927c478bd9Sstevel@tonic-gate 	}
26937c478bd9Sstevel@tonic-gate 	if (props_copy)
26947c478bd9Sstevel@tonic-gate 		free(props_copy);
26957c478bd9Sstevel@tonic-gate 	if (found) {
26967c478bd9Sstevel@tonic-gate 		/*
26977c478bd9Sstevel@tonic-gate 		 * does the pre-existing node have a status property?
26987c478bd9Sstevel@tonic-gate 		 */
26997c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_STATUS,
27007c478bd9Sstevel@tonic-gate 		    ua, sizeof (ua));
27017c478bd9Sstevel@tonic-gate 		if (err == PICL_PROPNOTFOUND)
27027c478bd9Sstevel@tonic-gate 			(void) add_status_prop(chdh, status);
27037c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
27047c478bd9Sstevel@tonic-gate 			return;
27057c478bd9Sstevel@tonic-gate 		if ((strcmp(ua, ASR_DISABLED) == 0) ||
27067c478bd9Sstevel@tonic-gate 		    (strcmp(ua, ASR_FAILED) == 0) ||
27077c478bd9Sstevel@tonic-gate 		    ((strcmp(status, ASR_DISABLED) != 0) &&
27087c478bd9Sstevel@tonic-gate 		    (strcmp(status, ASR_FAILED) != 0))) {
27097c478bd9Sstevel@tonic-gate 			return;
27107c478bd9Sstevel@tonic-gate 		}
27117c478bd9Sstevel@tonic-gate 		/*
27127c478bd9Sstevel@tonic-gate 		 * more urgent status now, so replace existing value
27137c478bd9Sstevel@tonic-gate 		 */
27147c478bd9Sstevel@tonic-gate 		err = ptree_get_prop_by_name(chdh, PICL_PROP_STATUS, &proph);
27157c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
27167c478bd9Sstevel@tonic-gate 			return;
27177c478bd9Sstevel@tonic-gate 		(void) ptree_delete_prop(proph);
27187c478bd9Sstevel@tonic-gate 		(void) ptree_destroy_prop(proph);
27197c478bd9Sstevel@tonic-gate 		err = add_status_prop(chdh, status);
27207c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
27217c478bd9Sstevel@tonic-gate 			return;
27227c478bd9Sstevel@tonic-gate 		return;
27237c478bd9Sstevel@tonic-gate 	}
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	/*
27267c478bd9Sstevel@tonic-gate 	 * typical case, node needs adding together with a set of properties
27277c478bd9Sstevel@tonic-gate 	 */
27287c478bd9Sstevel@tonic-gate 	if (ptree_create_and_add_node(nodeh, child, class, &chdh) ==
27297c478bd9Sstevel@tonic-gate 	    PICL_SUCCESS) {
27307c478bd9Sstevel@tonic-gate 		(void) add_status_prop(chdh, status);
27317c478bd9Sstevel@tonic-gate 		if (unitaddr) {
27327c478bd9Sstevel@tonic-gate 			(void) ptree_init_propinfo(&propinfo,
27337c478bd9Sstevel@tonic-gate 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_CHARSTRING,
27347c478bd9Sstevel@tonic-gate 			    PICL_READ, strlen(unitaddr) + 1,
27357c478bd9Sstevel@tonic-gate 			    PICL_PROP_UNIT_ADDRESS, NULL, NULL);
27367c478bd9Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(chdh, &propinfo,
27377c478bd9Sstevel@tonic-gate 			    unitaddr, &proph);
27387c478bd9Sstevel@tonic-gate 			(void) strlcpy(ptreepath, parent, PATH_MAX);
27397c478bd9Sstevel@tonic-gate 			(void) strlcat(ptreepath, "/", PATH_MAX);
27407c478bd9Sstevel@tonic-gate 			(void) strlcat(ptreepath, child, PATH_MAX);
27417c478bd9Sstevel@tonic-gate 			(void) strlcat(ptreepath, "@", PATH_MAX);
27427c478bd9Sstevel@tonic-gate 			(void) strlcat(ptreepath, unitaddr, PATH_MAX);
27437c478bd9Sstevel@tonic-gate 			(void) ptree_init_propinfo(&propinfo,
27447c478bd9Sstevel@tonic-gate 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_CHARSTRING,
27457c478bd9Sstevel@tonic-gate 			    PICL_READ, strlen(ptreepath) + 1,
27467c478bd9Sstevel@tonic-gate 			    PICL_PROP_DEVFS_PATH, NULL, NULL);
27477c478bd9Sstevel@tonic-gate 			(void) ptree_create_and_add_prop(chdh, &propinfo,
27487c478bd9Sstevel@tonic-gate 			    ptreepath, &proph);
27497c478bd9Sstevel@tonic-gate 		}
27507c478bd9Sstevel@tonic-gate 		next = props;
27517c478bd9Sstevel@tonic-gate 		while ((next = parse_props_string(next, &triple)) != NULL) {
27527c478bd9Sstevel@tonic-gate 			/*
27537c478bd9Sstevel@tonic-gate 			 * only handle int and string properties for
27547c478bd9Sstevel@tonic-gate 			 * simplicity
27557c478bd9Sstevel@tonic-gate 			 */
27567c478bd9Sstevel@tonic-gate 			if (strcmp(triple.proptype, "I") == 0) {
27577c478bd9Sstevel@tonic-gate 				(void) ptree_init_propinfo(&propinfo,
27587c478bd9Sstevel@tonic-gate 				    PTREE_PROPINFO_VERSION,
27597c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_INT, PICL_READ,
27607c478bd9Sstevel@tonic-gate 				    sizeof (int), triple.propname, NULL, NULL);
27617c478bd9Sstevel@tonic-gate 				val = atoi(triple.propval);
27627c478bd9Sstevel@tonic-gate 				(void) ptree_create_and_add_prop(chdh,
27637c478bd9Sstevel@tonic-gate 				    &propinfo, &val, &proph);
27647c478bd9Sstevel@tonic-gate 			} else {
27657c478bd9Sstevel@tonic-gate 				(void) ptree_init_propinfo(&propinfo,
27667c478bd9Sstevel@tonic-gate 				    PTREE_PROPINFO_VERSION,
27677c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_CHARSTRING, PICL_READ,
27687c478bd9Sstevel@tonic-gate 				    strlen(triple.propval) + 1,
27695e3e415aSfw 				    triple.propname, NULL, NULL);
27707c478bd9Sstevel@tonic-gate 				(void) ptree_create_and_add_prop(chdh,
27717c478bd9Sstevel@tonic-gate 				    &propinfo, triple.propval, &proph);
27727c478bd9Sstevel@tonic-gate 			}
27737c478bd9Sstevel@tonic-gate 		}
27747c478bd9Sstevel@tonic-gate 	}
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate static void
27787c478bd9Sstevel@tonic-gate add_asr_nodes()
27797c478bd9Sstevel@tonic-gate {
27807c478bd9Sstevel@tonic-gate 	char			*asrexport;
27817c478bd9Sstevel@tonic-gate 	int			asrexportlen;
27827c478bd9Sstevel@tonic-gate 	asr_conf_entries_t	*c = NULL;
27837c478bd9Sstevel@tonic-gate 	int			i;
27847c478bd9Sstevel@tonic-gate 	char			*key;
27857c478bd9Sstevel@tonic-gate 	char			*child;
27867c478bd9Sstevel@tonic-gate 	char			*unitaddr;
27877c478bd9Sstevel@tonic-gate 	uint16_t		count;
27887c478bd9Sstevel@tonic-gate 	int			disabled;
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	if (get_asr_export_list(&asrexport, &asrexportlen) == 0)
27917c478bd9Sstevel@tonic-gate 		return;
27927c478bd9Sstevel@tonic-gate 	process_asrtree_conf_file();
27937c478bd9Sstevel@tonic-gate 	if (conf_name_asr_map == NULL)
27947c478bd9Sstevel@tonic-gate 		return;
27957c478bd9Sstevel@tonic-gate 	i = 0;
27967c478bd9Sstevel@tonic-gate 	while (i < asrexportlen) {
27977c478bd9Sstevel@tonic-gate 		key = &asrexport[i];
27987c478bd9Sstevel@tonic-gate 		i += strlen(key) + 1;
27997c478bd9Sstevel@tonic-gate 		if (i >= asrexportlen)
28007c478bd9Sstevel@tonic-gate 			break;
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 		/*
28037c478bd9Sstevel@tonic-gate 		 * next byte tells us whether failed by diags or manually
28047c478bd9Sstevel@tonic-gate 		 * disabled
28057c478bd9Sstevel@tonic-gate 		 */
28067c478bd9Sstevel@tonic-gate 		disabled = asrexport[i];
28077c478bd9Sstevel@tonic-gate 		i++;
28087c478bd9Sstevel@tonic-gate 		if (i >= asrexportlen)
28097c478bd9Sstevel@tonic-gate 			break;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 		/*
28127c478bd9Sstevel@tonic-gate 		 * only type 1 supported
28137c478bd9Sstevel@tonic-gate 		 */
28147c478bd9Sstevel@tonic-gate 		if (asrexport[i] != 1)
28157c478bd9Sstevel@tonic-gate 			break;
28167c478bd9Sstevel@tonic-gate 		i++;
28177c478bd9Sstevel@tonic-gate 		if (i >= asrexportlen)
28187c478bd9Sstevel@tonic-gate 			break;
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 		/*
28217c478bd9Sstevel@tonic-gate 		 * next two bytes give size of reason string
28227c478bd9Sstevel@tonic-gate 		 */
28237c478bd9Sstevel@tonic-gate 		count = (asrexport[i] << 8) | asrexport[i + 1];
28247c478bd9Sstevel@tonic-gate 		i += count + 2;
28257c478bd9Sstevel@tonic-gate 		if (i > asrexportlen)
28267c478bd9Sstevel@tonic-gate 			break;
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 		/*
28297c478bd9Sstevel@tonic-gate 		 * now look for key in conf file info
28307c478bd9Sstevel@tonic-gate 		 */
28317c478bd9Sstevel@tonic-gate 		c = conf_name_asr_map;
28327c478bd9Sstevel@tonic-gate 		while (c != NULL) {
28337c478bd9Sstevel@tonic-gate 			if (strcmp(key, c->name) == 0) {
28347c478bd9Sstevel@tonic-gate 				child = strrchr(c->address, '/');
28357c478bd9Sstevel@tonic-gate 				*child++ = '\0';
28367c478bd9Sstevel@tonic-gate 				unitaddr = strchr(child, '@');
28377c478bd9Sstevel@tonic-gate 				if (unitaddr)
28387c478bd9Sstevel@tonic-gate 					*unitaddr++ = '\0';
28397c478bd9Sstevel@tonic-gate 				if (strcmp(c->status, ASR_DISABLED) == 0) {
28407c478bd9Sstevel@tonic-gate 					create_asr_node(c->address, child,
28417c478bd9Sstevel@tonic-gate 					    unitaddr, c->piclclass, disabled ?
28427c478bd9Sstevel@tonic-gate 					    ASR_DISABLED : ASR_FAILED,
28437c478bd9Sstevel@tonic-gate 					    c->props);
28447c478bd9Sstevel@tonic-gate 				} else {
28457c478bd9Sstevel@tonic-gate 					create_asr_node(c->address, child,
28467c478bd9Sstevel@tonic-gate 					    unitaddr, c->piclclass, c->status,
28477c478bd9Sstevel@tonic-gate 					    c->props);
28487c478bd9Sstevel@tonic-gate 				}
28497c478bd9Sstevel@tonic-gate 			}
28507c478bd9Sstevel@tonic-gate 			c = c->next;
28517c478bd9Sstevel@tonic-gate 		}
28527c478bd9Sstevel@tonic-gate 	}
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	free_asr_conf_entries(conf_name_asr_map);
28557c478bd9Sstevel@tonic-gate 	free(asrexport);
28567c478bd9Sstevel@tonic-gate }
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate /*
28597c478bd9Sstevel@tonic-gate  * This function adds information to the /platform node
28607c478bd9Sstevel@tonic-gate  */
28617c478bd9Sstevel@tonic-gate static int
28627c478bd9Sstevel@tonic-gate add_platform_info(picl_nodehdl_t plafh)
28637c478bd9Sstevel@tonic-gate {
28647c478bd9Sstevel@tonic-gate 	struct utsname		uts_info;
28657c478bd9Sstevel@tonic-gate 	int			err;
28667c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
28677c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	if (uname(&uts_info) < 0)
28707c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
28737c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.sysname) + 1,
28747c478bd9Sstevel@tonic-gate 	    PICL_PROP_SYSNAME, NULL, NULL);
28757c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.sysname,
28767c478bd9Sstevel@tonic-gate 	    &proph);
28777c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
28787c478bd9Sstevel@tonic-gate 		return (err);
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
28817c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.nodename) + 1,
28827c478bd9Sstevel@tonic-gate 	    PICL_PROP_NODENAME, NULL, NULL);
28837c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.nodename,
28847c478bd9Sstevel@tonic-gate 	    &proph);
28857c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
28867c478bd9Sstevel@tonic-gate 		return (err);
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
28897c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.release) + 1,
28907c478bd9Sstevel@tonic-gate 	    PICL_PROP_RELEASE, NULL, NULL);
28917c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.release,
28927c478bd9Sstevel@tonic-gate 	    &proph);
28937c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
28947c478bd9Sstevel@tonic-gate 		return (err);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
28977c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.version) + 1,
28987c478bd9Sstevel@tonic-gate 	    PICL_PROP_VERSION, NULL, NULL);
28997c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.version,
29007c478bd9Sstevel@tonic-gate 	    &proph);
29017c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
29027c478bd9Sstevel@tonic-gate 		return (err);
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
29057c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.machine) + 1,
29067c478bd9Sstevel@tonic-gate 	    PICL_PROP_MACHINE, NULL, NULL);
29077c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.machine,
29087c478bd9Sstevel@tonic-gate 	    &proph);
29097c478bd9Sstevel@tonic-gate 	return (err);
29107c478bd9Sstevel@tonic-gate }
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate /*
29137c478bd9Sstevel@tonic-gate  * Get first 32-bit value from the reg property
29147c478bd9Sstevel@tonic-gate  */
29157c478bd9Sstevel@tonic-gate static int
29167c478bd9Sstevel@tonic-gate get_first_reg_word(picl_nodehdl_t nodeh, uint32_t *regval)
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate 	int			err;
29197c478bd9Sstevel@tonic-gate 	uint32_t		*regbuf;
29207c478bd9Sstevel@tonic-gate 	picl_prophdl_t  	regh;
29217c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
29247c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) 	/* no reg property */
29257c478bd9Sstevel@tonic-gate 		return (err);
29267c478bd9Sstevel@tonic-gate 	err = ptree_get_propinfo(regh, &pinfo);
29277c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
29287c478bd9Sstevel@tonic-gate 		return (err);
29297c478bd9Sstevel@tonic-gate 	if (pinfo.piclinfo.size < sizeof (uint32_t)) /* too small */
29307c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
29317c478bd9Sstevel@tonic-gate 	regbuf = alloca(pinfo.piclinfo.size);
29327c478bd9Sstevel@tonic-gate 	if (regbuf == NULL)
29337c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
29347c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(regh, regbuf, pinfo.piclinfo.size);
29357c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
29367c478bd9Sstevel@tonic-gate 		return (err);
29377c478bd9Sstevel@tonic-gate 	*regval = *regbuf;	/* get first 32-bit value */
29387c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
29397c478bd9Sstevel@tonic-gate }
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate /*
29427c478bd9Sstevel@tonic-gate  * Get device ID from the reg property
29437c478bd9Sstevel@tonic-gate  */
29447c478bd9Sstevel@tonic-gate static int
29457c478bd9Sstevel@tonic-gate get_device_id(picl_nodehdl_t nodeh, uint32_t *dev_id)
29467c478bd9Sstevel@tonic-gate {
29477c478bd9Sstevel@tonic-gate 	int			err;
29487c478bd9Sstevel@tonic-gate 	uint32_t		regval;
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	err = get_first_reg_word(nodeh, &regval);
29517c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
29527c478bd9Sstevel@tonic-gate 		return (err);
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 	*dev_id = PCI_DEVICE_ID(regval);
29557c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
29567c478bd9Sstevel@tonic-gate }
29577c478bd9Sstevel@tonic-gate 
29587c478bd9Sstevel@tonic-gate /*
29597c478bd9Sstevel@tonic-gate  * add Slot property for children of SBUS node
29607c478bd9Sstevel@tonic-gate  */
29617c478bd9Sstevel@tonic-gate /* ARGSUSED */
29627c478bd9Sstevel@tonic-gate static int
29637c478bd9Sstevel@tonic-gate add_sbus_slots(picl_nodehdl_t pcih, void *args)
29647c478bd9Sstevel@tonic-gate {
29657c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
29667c478bd9Sstevel@tonic-gate 	uint32_t		slot;
29677c478bd9Sstevel@tonic-gate 	int			err;
29687c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 	for (err = ptree_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
29717c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
29725e3e415aSfw 	    err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
29735e3e415aSfw 	    sizeof (picl_nodehdl_t))) {
29747c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
29757c478bd9Sstevel@tonic-gate 			return (err);
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 		if (get_first_reg_word(nodeh, &slot) != 0)
29787c478bd9Sstevel@tonic-gate 			continue;
29797c478bd9Sstevel@tonic-gate 		(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
29807c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (uint32_t),
29817c478bd9Sstevel@tonic-gate 		    PICL_PROP_SLOT, NULL, NULL);
29827c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &pinfo, &slot, NULL);
29837c478bd9Sstevel@tonic-gate 	}
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	return (PICL_WALK_CONTINUE);
29867c478bd9Sstevel@tonic-gate }
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate /*
29897c478bd9Sstevel@tonic-gate  * This function creates a Slot property for SBUS child nodes
29907c478bd9Sstevel@tonic-gate  * which can be correlated with the slot they are plugged into
29917c478bd9Sstevel@tonic-gate  * on the motherboard.
29927c478bd9Sstevel@tonic-gate  */
29937c478bd9Sstevel@tonic-gate static int
29947c478bd9Sstevel@tonic-gate set_sbus_slot(picl_nodehdl_t plafh)
29957c478bd9Sstevel@tonic-gate {
29967c478bd9Sstevel@tonic-gate 	int		err;
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	err = ptree_walk_tree_by_class(plafh, PICL_CLASS_SBUS, NULL,
29997c478bd9Sstevel@tonic-gate 	    add_sbus_slots);
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	return (err);
30027c478bd9Sstevel@tonic-gate }
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate /*
30057ef4fcfbSvenki  * add DeviceID property for children of PCI/PCIEX node
30067c478bd9Sstevel@tonic-gate  */
30077c478bd9Sstevel@tonic-gate /* ARGSUSED */
30087c478bd9Sstevel@tonic-gate static int
30097c478bd9Sstevel@tonic-gate add_pci_deviceids(picl_nodehdl_t pcih, void *args)
30107c478bd9Sstevel@tonic-gate {
30117c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
30127c478bd9Sstevel@tonic-gate 	uint32_t		dev_id;
30137c478bd9Sstevel@tonic-gate 	int			err;
30147c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	for (err = ptree_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
30177c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
30185e3e415aSfw 	    err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
30195e3e415aSfw 	    sizeof (picl_nodehdl_t))) {
30207c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
30217c478bd9Sstevel@tonic-gate 			return (err);
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 		if (get_device_id(nodeh, &dev_id) != 0)
30247c478bd9Sstevel@tonic-gate 			continue;
30257c478bd9Sstevel@tonic-gate 		(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
30267c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (uint32_t),
30277c478bd9Sstevel@tonic-gate 		    PICL_PROP_DEVICE_ID, NULL, NULL);
30287c478bd9Sstevel@tonic-gate 		(void) ptree_create_and_add_prop(nodeh, &pinfo, &dev_id, NULL);
30297c478bd9Sstevel@tonic-gate 	}
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 	return (PICL_WALK_CONTINUE);
30327c478bd9Sstevel@tonic-gate }
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate /*
30357ef4fcfbSvenki  * This function creates a DeviceID property for PCI/PCIEX child nodes
30367c478bd9Sstevel@tonic-gate  * which can be correlated with the slot they are plugged into
30377c478bd9Sstevel@tonic-gate  * on the motherboard.
30387c478bd9Sstevel@tonic-gate  */
30397ef4fcfbSvenki static void
30407ef4fcfbSvenki set_pci_pciex_deviceid(picl_nodehdl_t plafh)
30417c478bd9Sstevel@tonic-gate {
30427ef4fcfbSvenki 	(void) ptree_walk_tree_by_class(plafh, PICL_CLASS_PCI, NULL,
30437c478bd9Sstevel@tonic-gate 	    add_pci_deviceids);
30447c478bd9Sstevel@tonic-gate 
30457ef4fcfbSvenki 	(void) ptree_walk_tree_by_class(plafh, PICL_CLASS_PCIEX, NULL,
30467ef4fcfbSvenki 	    add_pci_deviceids);
30477c478bd9Sstevel@tonic-gate }
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate /*
30507c478bd9Sstevel@tonic-gate  * Default UnitAddress encode function
30517c478bd9Sstevel@tonic-gate  */
30527c478bd9Sstevel@tonic-gate static int
30537c478bd9Sstevel@tonic-gate encode_default_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
30547c478bd9Sstevel@tonic-gate {
30557c478bd9Sstevel@tonic-gate 	int	i, len;
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	/*
30587c478bd9Sstevel@tonic-gate 	 * Encode UnitAddress as %a,%b,%c,...,%n
30597c478bd9Sstevel@tonic-gate 	 */
30607c478bd9Sstevel@tonic-gate 	if (addrcells < 1)
30617c478bd9Sstevel@tonic-gate 		return (-1);
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate 	len = snprintf(buf, sz, "%x", *regprop);
30647c478bd9Sstevel@tonic-gate 	for (i = 1; i < addrcells && len < sz; i++)
30657c478bd9Sstevel@tonic-gate 		len += snprintf(&buf[len], sz-len, ",%x", regprop[i]);
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 	return ((len >= sz) ? -1 : 0);
30687c478bd9Sstevel@tonic-gate }
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate /*
30717c478bd9Sstevel@tonic-gate  * UnitAddress encode function where the last component is not printed
30727c478bd9Sstevel@tonic-gate  * unless non-zero.
30737c478bd9Sstevel@tonic-gate  */
30747c478bd9Sstevel@tonic-gate static int
30757c478bd9Sstevel@tonic-gate encode_optional_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
30767c478bd9Sstevel@tonic-gate {
30777c478bd9Sstevel@tonic-gate 	int	retval;
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 	/*
30807c478bd9Sstevel@tonic-gate 	 * Encode UnitAddress as %a,%b,%c,...,%n where the last component
30817c478bd9Sstevel@tonic-gate 	 * is printed only if non-zero.
30827c478bd9Sstevel@tonic-gate 	 */
30837c478bd9Sstevel@tonic-gate 	if (addrcells > 1 && regprop[addrcells-1] == 0)
30847c478bd9Sstevel@tonic-gate 		retval = encode_default_unitaddr(buf, sz, regprop, addrcells-1);
30857c478bd9Sstevel@tonic-gate 	else
30867c478bd9Sstevel@tonic-gate 		retval = encode_default_unitaddr(buf, sz, regprop, addrcells);
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 	return (retval);
30897c478bd9Sstevel@tonic-gate }
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate /*
30937c478bd9Sstevel@tonic-gate  * UnitAddress encode function for SCSI class of devices
30947c478bd9Sstevel@tonic-gate  */
30957c478bd9Sstevel@tonic-gate static int
30967c478bd9Sstevel@tonic-gate encode_scsi_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
30977c478bd9Sstevel@tonic-gate {
30987c478bd9Sstevel@tonic-gate 	int	len, retval;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	/*
31017c478bd9Sstevel@tonic-gate 	 * #address-cells	Format
31027c478bd9Sstevel@tonic-gate 	 *	2		second component printed only if non-zero
31037c478bd9Sstevel@tonic-gate 	 *
31047c478bd9Sstevel@tonic-gate 	 *	4		regprop:   phys_hi phys_lo lun_hi lun_lo
31057c478bd9Sstevel@tonic-gate 	 *			UnitAddr:  w<phys_hi><phys_lo>,<lun_lo>
31067c478bd9Sstevel@tonic-gate 	 */
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	if (addrcells == 2) {
31097c478bd9Sstevel@tonic-gate 		retval = encode_optional_unitaddr(buf, sz, regprop, addrcells);
31107c478bd9Sstevel@tonic-gate 	} else if (addrcells == 4) {
31117c478bd9Sstevel@tonic-gate 		len = snprintf(buf, sz, "w%08x%08x,%x", regprop[0], regprop[1],
31127c478bd9Sstevel@tonic-gate 		    regprop[3]);
31137c478bd9Sstevel@tonic-gate 		retval = (len >= sz) ? -1 : 0;
31147c478bd9Sstevel@tonic-gate 	} else
31157c478bd9Sstevel@tonic-gate 		retval = -1;
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	return (retval);
31187c478bd9Sstevel@tonic-gate }
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate /*
31217c478bd9Sstevel@tonic-gate  * UnitAddress encode function for UPA devices
31227c478bd9Sstevel@tonic-gate  */
31237c478bd9Sstevel@tonic-gate static int
31247c478bd9Sstevel@tonic-gate encode_upa_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
31257c478bd9Sstevel@tonic-gate {
31267c478bd9Sstevel@tonic-gate 	int	len;
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 	if (addrcells != 2)
31297c478bd9Sstevel@tonic-gate 		return (-1);
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	len = snprintf(buf, sz, "%x,%x", (regprop[0]/2)&0x1f, regprop[1]);
31327c478bd9Sstevel@tonic-gate 	return ((len >= sz) ? -1 : 0);
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate /*
31367c478bd9Sstevel@tonic-gate  * UnitAddress encode function for GPTWO, JBUS devices
31377c478bd9Sstevel@tonic-gate  */
31387c478bd9Sstevel@tonic-gate static int
31397c478bd9Sstevel@tonic-gate encode_gptwo_jbus_unitaddr(char *buf, int sz, uint32_t *regprop,
31407c478bd9Sstevel@tonic-gate     uint_t addrcells)
31417c478bd9Sstevel@tonic-gate {
31427c478bd9Sstevel@tonic-gate 	uint32_t	hi, lo;
31437c478bd9Sstevel@tonic-gate 	int		len, id, off;
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	if (addrcells != 2)
31467c478bd9Sstevel@tonic-gate 		return (-1);
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 	hi = regprop[0];
31497c478bd9Sstevel@tonic-gate 	lo = regprop[1];
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	if (hi & 0x400) {
31527c478bd9Sstevel@tonic-gate 		id = ((hi & 0x1) << 9) | (lo >> 23);	/* agent id */
31537c478bd9Sstevel@tonic-gate 		off = lo & 0x7fffff;			/* config offset */
31547c478bd9Sstevel@tonic-gate 		len = snprintf(buf, sz, "%x,%x", id, off);
31557c478bd9Sstevel@tonic-gate 	} else {
31567c478bd9Sstevel@tonic-gate 		len = snprintf(buf, sz, "m%x,%x", hi, lo);
31577c478bd9Sstevel@tonic-gate 	}
31587c478bd9Sstevel@tonic-gate 	return ((len >= sz) ? -1 : 0);
31597c478bd9Sstevel@tonic-gate }
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate /*
31627c478bd9Sstevel@tonic-gate  * UnitAddress encode function for PCI devices
31637c478bd9Sstevel@tonic-gate  */
31647c478bd9Sstevel@tonic-gate static int
31657c478bd9Sstevel@tonic-gate encode_pci_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
31667c478bd9Sstevel@tonic-gate {
31677c478bd9Sstevel@tonic-gate 	typedef struct {
31687c478bd9Sstevel@tonic-gate 		uint32_t	n:1,		/* relocatable */
31697c478bd9Sstevel@tonic-gate 				p:1,		/* prefetchable */
31707c478bd9Sstevel@tonic-gate 				t:1,		/* address region aliases */
31717c478bd9Sstevel@tonic-gate 				zero:3,		/* must be zero */
31727c478bd9Sstevel@tonic-gate 				ss:2,		/* address space type */
31737c478bd9Sstevel@tonic-gate 				bus:8,		/* bus number */
31747c478bd9Sstevel@tonic-gate 				dev:5,		/* device number */
31757c478bd9Sstevel@tonic-gate 				fn:3,		/* function number */
31767c478bd9Sstevel@tonic-gate 				reg:8;		/* register number */
31777c478bd9Sstevel@tonic-gate 		uint32_t	phys_hi;	/* high physical address */
31787c478bd9Sstevel@tonic-gate 		uint32_t	phys_lo;	/* low physical address */
31797c478bd9Sstevel@tonic-gate 	} pci_addrcell_t;
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	pci_addrcell_t	*p;
31827c478bd9Sstevel@tonic-gate 	int		len;
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	if (addrcells != 3)
31857c478bd9Sstevel@tonic-gate 		return (-1);
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	p = (pci_addrcell_t *)regprop;
31887c478bd9Sstevel@tonic-gate 	switch (p->ss) {
31897c478bd9Sstevel@tonic-gate 	case 0:		/* Config */
31907c478bd9Sstevel@tonic-gate 		if (p->fn)
31917c478bd9Sstevel@tonic-gate 			len = snprintf(buf, sz, "%x,%x", p->dev, p->fn);
31927c478bd9Sstevel@tonic-gate 		else
31937c478bd9Sstevel@tonic-gate 			len = snprintf(buf, sz, "%x", p->dev);
31947c478bd9Sstevel@tonic-gate 		break;
31957c478bd9Sstevel@tonic-gate 	case 1:		/* IO */
31967c478bd9Sstevel@tonic-gate 		len = snprintf(buf, sz, "i%x,%x,%x,%x", p->dev, p->fn, p->reg,
31977c478bd9Sstevel@tonic-gate 		    p->phys_lo);
31987c478bd9Sstevel@tonic-gate 		break;
31997c478bd9Sstevel@tonic-gate 	case 2:		/* Mem32 */
32007c478bd9Sstevel@tonic-gate 		len = snprintf(buf, sz, "m%x,%x,%x,%x", p->dev, p->fn, p->reg,
32017c478bd9Sstevel@tonic-gate 		    p->phys_lo);
32027c478bd9Sstevel@tonic-gate 		break;
32037c478bd9Sstevel@tonic-gate 	case 3:		/* Mem64 */
32047c478bd9Sstevel@tonic-gate 		len = snprintf(buf, sz, "x%x,%x,%x,%x%08x", p->dev, p->fn,
32057c478bd9Sstevel@tonic-gate 		    p->reg, p->phys_hi, p->phys_lo);
32067c478bd9Sstevel@tonic-gate 		break;
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 	return ((len >= sz) ? -1 : 0);
32097c478bd9Sstevel@tonic-gate }
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate /*
32127c478bd9Sstevel@tonic-gate  * Get #address-cells property value
32137c478bd9Sstevel@tonic-gate  */
32147c478bd9Sstevel@tonic-gate static uint_t
32157c478bd9Sstevel@tonic-gate get_addrcells_prop(picl_nodehdl_t nodeh)
32167c478bd9Sstevel@tonic-gate {
32177c478bd9Sstevel@tonic-gate 	int			len, err;
32187c478bd9Sstevel@tonic-gate 	uint32_t		addrcells;
32197c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
32207c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	/*
32237c478bd9Sstevel@tonic-gate 	 * Get #address-cells property.  If not present, use default value.
32247c478bd9Sstevel@tonic-gate 	 */
32257c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, OBP_PROP_ADDRESS_CELLS, &proph);
32267c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
32277c478bd9Sstevel@tonic-gate 		err = ptree_get_propinfo(proph, &pinfo);
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	len = pinfo.piclinfo.size;
32307c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS && len >= sizeof (uint8_t) &&
32317c478bd9Sstevel@tonic-gate 	    len <= sizeof (addrcells)) {
32327c478bd9Sstevel@tonic-gate 		err = ptree_get_propval(proph, &addrcells, len);
32337c478bd9Sstevel@tonic-gate 		if (err == PICL_SUCCESS) {
32347c478bd9Sstevel@tonic-gate 			if (len == sizeof (uint8_t))
32357c478bd9Sstevel@tonic-gate 				addrcells = *(uint8_t *)&addrcells;
32367c478bd9Sstevel@tonic-gate 			else if (len == sizeof (uint16_t))
32377c478bd9Sstevel@tonic-gate 				addrcells = *(uint16_t *)&addrcells;
32387c478bd9Sstevel@tonic-gate 		} else
32397c478bd9Sstevel@tonic-gate 			addrcells = DEFAULT_ADDRESS_CELLS;
32407c478bd9Sstevel@tonic-gate 	} else
32417c478bd9Sstevel@tonic-gate 		addrcells = DEFAULT_ADDRESS_CELLS;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	return (addrcells);
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate /*
32477c478bd9Sstevel@tonic-gate  * Get UnitAddress mapping entry for a node
32487c478bd9Sstevel@tonic-gate  */
32497c478bd9Sstevel@tonic-gate static unitaddr_map_t *
32507c478bd9Sstevel@tonic-gate get_unitaddr_mapping(picl_nodehdl_t nodeh)
32517c478bd9Sstevel@tonic-gate {
32527c478bd9Sstevel@tonic-gate 	int		err;
32537c478bd9Sstevel@tonic-gate 	unitaddr_map_t	*uamap;
32547c478bd9Sstevel@tonic-gate 	char		clname[PICL_CLASSNAMELEN_MAX];
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	/*
32577c478bd9Sstevel@tonic-gate 	 * Get my classname and locate a function to translate "reg" prop
32587c478bd9Sstevel@tonic-gate 	 * into "UnitAddress" prop for my children.
32597c478bd9Sstevel@tonic-gate 	 */
32607c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME, clname,
32617c478bd9Sstevel@tonic-gate 	    sizeof (clname));
32627c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
32637c478bd9Sstevel@tonic-gate 		(void) strcpy(clname, "");	/* NULL class name */
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	for (uamap = &unitaddr_map_table[0]; uamap->class != NULL; uamap++)
32667c478bd9Sstevel@tonic-gate 		if (strcmp(clname, uamap->class) == 0)
32677c478bd9Sstevel@tonic-gate 			break;
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	return (uamap);
32707c478bd9Sstevel@tonic-gate }
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate /*
32737c478bd9Sstevel@tonic-gate  * Add UnitAddress property to the specified node
32747c478bd9Sstevel@tonic-gate  */
32757c478bd9Sstevel@tonic-gate static int
32767c478bd9Sstevel@tonic-gate add_unitaddr_prop(picl_nodehdl_t nodeh, unitaddr_map_t *uamap, uint_t addrcells)
32777c478bd9Sstevel@tonic-gate {
32787c478bd9Sstevel@tonic-gate 	int			regproplen, err;
32797c478bd9Sstevel@tonic-gate 	uint32_t		*regbuf;
32807c478bd9Sstevel@tonic-gate 	picl_prophdl_t		regh;
32817c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
32827c478bd9Sstevel@tonic-gate 	char			unitaddr[MAX_UNIT_ADDRESS_LEN];
32837c478bd9Sstevel@tonic-gate 
32847c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
32857c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
32867c478bd9Sstevel@tonic-gate 		return (err);
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	err = ptree_get_propinfo(regh, &pinfo);
32897c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
32907c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	if (pinfo.piclinfo.size < (addrcells * sizeof (uint32_t)))
32937c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	regproplen = pinfo.piclinfo.size;
32967c478bd9Sstevel@tonic-gate 	regbuf = alloca(regproplen);
32977c478bd9Sstevel@tonic-gate 	if (regbuf == NULL)
32987c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(regh, regbuf, regproplen);
33017c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS || uamap->func == NULL ||
33027c478bd9Sstevel@tonic-gate 	    (uamap->addrcellcnt && uamap->addrcellcnt != addrcells) ||
33037c478bd9Sstevel@tonic-gate 	    (uamap->func)(unitaddr, sizeof (unitaddr), regbuf,
33047c478bd9Sstevel@tonic-gate 	    addrcells) != 0) {
33057c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
33067c478bd9Sstevel@tonic-gate 	}
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
33097c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(unitaddr)+1,
33107c478bd9Sstevel@tonic-gate 	    PICL_PROP_UNIT_ADDRESS, NULL, NULL);
33117c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
33127c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(nodeh, &pinfo, unitaddr, NULL);
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 	return (err);
33157c478bd9Sstevel@tonic-gate }
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate /*
33187c478bd9Sstevel@tonic-gate  * work out UnitAddress property of the specified node
33197c478bd9Sstevel@tonic-gate  */
33207c478bd9Sstevel@tonic-gate static int
33217c478bd9Sstevel@tonic-gate get_unitaddr(picl_nodehdl_t parh, picl_nodehdl_t nodeh, char *unitaddr,
33227c478bd9Sstevel@tonic-gate     size_t ualen)
33237c478bd9Sstevel@tonic-gate {
33247c478bd9Sstevel@tonic-gate 	int			regproplen, err;
33257c478bd9Sstevel@tonic-gate 	uint32_t		*regbuf;
33267c478bd9Sstevel@tonic-gate 	picl_prophdl_t		regh;
33277c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
33287c478bd9Sstevel@tonic-gate 	unitaddr_map_t		*uamap;
33297c478bd9Sstevel@tonic-gate 	uint32_t		addrcells;
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	addrcells = get_addrcells_prop(parh);
33327c478bd9Sstevel@tonic-gate 	uamap = get_unitaddr_mapping(parh);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
33357c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
33367c478bd9Sstevel@tonic-gate 		return (err);
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 	err = ptree_get_propinfo(regh, &pinfo);
33397c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
33407c478bd9Sstevel@tonic-gate 		return (err);
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	if (pinfo.piclinfo.size < (addrcells * sizeof (uint32_t)))
33437c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	regproplen = pinfo.piclinfo.size;
33467c478bd9Sstevel@tonic-gate 	regbuf = alloca(regproplen);
33477c478bd9Sstevel@tonic-gate 	if (regbuf == NULL)
33487c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(regh, regbuf, regproplen);
33517c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS || uamap->func == NULL ||
33527c478bd9Sstevel@tonic-gate 	    (uamap->addrcellcnt && uamap->addrcellcnt != addrcells) ||
33537c478bd9Sstevel@tonic-gate 	    (uamap->func)(unitaddr, ualen, regbuf, addrcells) != 0) {
33547c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
33577c478bd9Sstevel@tonic-gate }
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate /*
33607c478bd9Sstevel@tonic-gate  * Add UnitAddress property to all children of the specified node
33617c478bd9Sstevel@tonic-gate  */
33627c478bd9Sstevel@tonic-gate static int
33637c478bd9Sstevel@tonic-gate add_unitaddr_prop_to_subtree(picl_nodehdl_t nodeh)
33647c478bd9Sstevel@tonic-gate {
33657c478bd9Sstevel@tonic-gate 	int			err;
33667c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		chdh;
33677c478bd9Sstevel@tonic-gate 	unitaddr_map_t		*uamap;
33687c478bd9Sstevel@tonic-gate 	uint32_t		addrcells;
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 	/*
33717c478bd9Sstevel@tonic-gate 	 * Get #address-cells and unit address mapping entry for my
33727c478bd9Sstevel@tonic-gate 	 * node's class
33737c478bd9Sstevel@tonic-gate 	 */
33747c478bd9Sstevel@tonic-gate 	addrcells = get_addrcells_prop(nodeh);
33757c478bd9Sstevel@tonic-gate 	uamap = get_unitaddr_mapping(nodeh);
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate 	/*
33787c478bd9Sstevel@tonic-gate 	 * Add UnitAddress property to my children and their subtree
33797c478bd9Sstevel@tonic-gate 	 */
33807c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
33817c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t));
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 	while (err == PICL_SUCCESS) {
33847c478bd9Sstevel@tonic-gate 		(void) add_unitaddr_prop(chdh, uamap, addrcells);
33857c478bd9Sstevel@tonic-gate 		(void) add_unitaddr_prop_to_subtree(chdh);
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
33887c478bd9Sstevel@tonic-gate 		    sizeof (picl_nodehdl_t));
33897c478bd9Sstevel@tonic-gate 	}
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
33927c478bd9Sstevel@tonic-gate }
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate static int
33957c478bd9Sstevel@tonic-gate update_memory_size_prop(picl_nodehdl_t plafh)
33967c478bd9Sstevel@tonic-gate {
33977c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		memh;
33987c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
33997c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
34007c478bd9Sstevel@tonic-gate 	int			err, nspecs, snum, pval;
34017c478bd9Sstevel@tonic-gate 	char			*regbuf;
34027c478bd9Sstevel@tonic-gate 	memspecs_t		*mspecs;
34037c478bd9Sstevel@tonic-gate 	uint64_t		memsize;
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	/*
34067c478bd9Sstevel@tonic-gate 	 * check if the #size-cells of the platform node is 2
34077c478bd9Sstevel@tonic-gate 	 */
34087c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(plafh, OBP_PROP_SIZE_CELLS, &pval,
34095e3e415aSfw 	    sizeof (pval));
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate 	if (err == PICL_PROPNOTFOUND)
34127c478bd9Sstevel@tonic-gate 		pval = SUPPORTED_NUM_CELL_SIZE;
34137c478bd9Sstevel@tonic-gate 	else if (err != PICL_SUCCESS)
34147c478bd9Sstevel@tonic-gate 		return (err);
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 	/*
34177c478bd9Sstevel@tonic-gate 	 * don't know how to handle other vals
34187c478bd9Sstevel@tonic-gate 	 */
34197c478bd9Sstevel@tonic-gate 	if (pval != SUPPORTED_NUM_CELL_SIZE)
34207c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path(MEMORY_PATH, &memh);
34237c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
34247c478bd9Sstevel@tonic-gate 		return (err);
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	/*
34277c478bd9Sstevel@tonic-gate 	 * Get the REG property to calculate the size of memory
34287c478bd9Sstevel@tonic-gate 	 */
34297c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(memh, OBP_REG, &proph);
34307c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
34317c478bd9Sstevel@tonic-gate 		return (err);
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	err = ptree_get_propinfo(proph, &pinfo);
34347c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
34357c478bd9Sstevel@tonic-gate 		return (err);
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	regbuf = alloca(pinfo.piclinfo.size);
34387c478bd9Sstevel@tonic-gate 	if (regbuf == NULL)
34397c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(proph, regbuf, pinfo.piclinfo.size);
34427c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
34437c478bd9Sstevel@tonic-gate 		return (err);
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 	mspecs = (memspecs_t *)regbuf;
34467c478bd9Sstevel@tonic-gate 	nspecs = pinfo.piclinfo.size / sizeof (memspecs_t);
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	memsize = 0;
34497c478bd9Sstevel@tonic-gate 	for (snum = 0; snum < nspecs; ++snum)
34507c478bd9Sstevel@tonic-gate 		memsize += mspecs[snum].size;
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(memh, PICL_PROP_SIZE, &proph);
34537c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {
34547c478bd9Sstevel@tonic-gate 		err = ptree_update_propval(proph, &memsize, sizeof (memsize));
34557c478bd9Sstevel@tonic-gate 		return (err);
34567c478bd9Sstevel@tonic-gate 	}
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	/*
34597c478bd9Sstevel@tonic-gate 	 * Add the size property
34607c478bd9Sstevel@tonic-gate 	 */
34617c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
34625e3e415aSfw 	    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (memsize),
34635e3e415aSfw 	    PICL_PROP_SIZE, NULL, NULL);
34647c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(memh, &pinfo, &memsize, NULL);
34657c478bd9Sstevel@tonic-gate 	return (err);
34667c478bd9Sstevel@tonic-gate }
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate /*
34697c478bd9Sstevel@tonic-gate  * This function is executed as part of .init when the plugin is
34707c478bd9Sstevel@tonic-gate  * dlopen()ed
34717c478bd9Sstevel@tonic-gate  */
34727c478bd9Sstevel@tonic-gate static void
34737c478bd9Sstevel@tonic-gate picldevtree_register(void)
34747c478bd9Sstevel@tonic-gate {
34757c478bd9Sstevel@tonic-gate 	if (getenv(SUNW_PICLDEVTREE_PLUGIN_DEBUG))
34767c478bd9Sstevel@tonic-gate 		picldevtree_debug = 1;
34777c478bd9Sstevel@tonic-gate 	(void) picld_plugin_register(&my_reg_info);
34787c478bd9Sstevel@tonic-gate }
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate /*
34817c478bd9Sstevel@tonic-gate  * This function is the init entry point of the plugin.
34827c478bd9Sstevel@tonic-gate  * It initializes the /platform tree based on libdevinfo
34837c478bd9Sstevel@tonic-gate  */
34847c478bd9Sstevel@tonic-gate static void
34857c478bd9Sstevel@tonic-gate picldevtree_init(void)
34867c478bd9Sstevel@tonic-gate {
34877c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	rhdl;
34887c478bd9Sstevel@tonic-gate 	int		err;
34897c478bd9Sstevel@tonic-gate 	struct utsname	utsname;
34907c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	plafh;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	if (uname(&utsname) < 0)
34937c478bd9Sstevel@tonic-gate 		return;
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 	(void) strcpy(mach_name, utsname.machine);
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	if (strcmp(mach_name, "sun4u") == 0) {
34987c478bd9Sstevel@tonic-gate 		builtin_map_ptr = sun4u_map;
34990d63ce2bSvenki 		builtin_map_size = sizeof (sun4u_map) / sizeof (builtin_map_t);
35000d63ce2bSvenki 	} else if (strcmp(mach_name, "sun4v") == 0) {
35010d63ce2bSvenki 		builtin_map_ptr = sun4u_map;
35027c478bd9Sstevel@tonic-gate 		builtin_map_size = sizeof (sun4u_map) / sizeof (builtin_map_t);
35037c478bd9Sstevel@tonic-gate 	} else if (strcmp(mach_name, "i86pc") == 0) {
35047c478bd9Sstevel@tonic-gate 		builtin_map_ptr = i86pc_map;
35057c478bd9Sstevel@tonic-gate 		builtin_map_size = sizeof (i86pc_map) / sizeof (builtin_map_t);
35067c478bd9Sstevel@tonic-gate 	} else {
35077c478bd9Sstevel@tonic-gate 		builtin_map_ptr = NULL;
35087c478bd9Sstevel@tonic-gate 		builtin_map_size = 0;
35097c478bd9Sstevel@tonic-gate 	}
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 	err = ptree_get_root(&rhdl);
35127c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
35137c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, DEVINFO_PLUGIN_INIT_FAILED);
35147c478bd9Sstevel@tonic-gate 		return;
35157c478bd9Sstevel@tonic-gate 	}
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	process_devtree_conf_file();
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	if (libdevinfo_init(rhdl) != PICL_SUCCESS) {
35207c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, DEVINFO_PLUGIN_INIT_FAILED);
35217c478bd9Sstevel@tonic-gate 		return;
35227c478bd9Sstevel@tonic-gate 	}
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path(PLATFORM_PATH, &plafh);
35257c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
35267c478bd9Sstevel@tonic-gate 		return;
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	(void) add_unitaddr_prop_to_subtree(plafh);
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 	add_asr_nodes();
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	(void) update_memory_size_prop(plafh);
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 	(void) setup_cpus(plafh);
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate 	(void) add_ffb_config_info(plafh);
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 	(void) add_platform_info(plafh);
35397c478bd9Sstevel@tonic-gate 
35407ef4fcfbSvenki 	set_pci_pciex_deviceid(plafh);
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	(void) set_sbus_slot(plafh);
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
35457c478bd9Sstevel@tonic-gate 	    picldevtree_evhandler, NULL);
35467c478bd9Sstevel@tonic-gate 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
35477c478bd9Sstevel@tonic-gate 	    picldevtree_evhandler, NULL);
35485e3e415aSfw 	(void) ptree_register_handler(PICLEVENT_CPU_STATE_CHANGE,
35495e3e415aSfw 	    picldevtree_evhandler, NULL);
35507c478bd9Sstevel@tonic-gate }
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate /*
35537c478bd9Sstevel@tonic-gate  * This function is the fini entry point of the plugin
35547c478bd9Sstevel@tonic-gate  */
35557c478bd9Sstevel@tonic-gate static void
35567c478bd9Sstevel@tonic-gate picldevtree_fini(void)
35577c478bd9Sstevel@tonic-gate {
35587c478bd9Sstevel@tonic-gate 	/* First unregister the event handlers */
35597c478bd9Sstevel@tonic-gate 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
35607c478bd9Sstevel@tonic-gate 	    picldevtree_evhandler, NULL);
35617c478bd9Sstevel@tonic-gate 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
35627c478bd9Sstevel@tonic-gate 	    picldevtree_evhandler, NULL);
35635e3e415aSfw 	(void) ptree_unregister_handler(PICLEVENT_CPU_STATE_CHANGE,
35645e3e415aSfw 	    picldevtree_evhandler, NULL);
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 	conf_name_class_map = free_conf_entries(conf_name_class_map);
35677c478bd9Sstevel@tonic-gate }
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate /*
35707c478bd9Sstevel@tonic-gate  * This function is the event handler of this plug-in.
35717c478bd9Sstevel@tonic-gate  *
35727c478bd9Sstevel@tonic-gate  * It processes the following events:
35737c478bd9Sstevel@tonic-gate  *
35747c478bd9Sstevel@tonic-gate  *	PICLEVENT_SYSEVENT_DEVICE_ADDED
35757c478bd9Sstevel@tonic-gate  *	PICLEVENT_SYSEVENT_DEVICE_REMOVED
35765e3e415aSfw  *	PICLEVENT_CPU_STATE_CHANGE
35777c478bd9Sstevel@tonic-gate  */
35787c478bd9Sstevel@tonic-gate /* ARGSUSED */
35797c478bd9Sstevel@tonic-gate static void
35807c478bd9Sstevel@tonic-gate picldevtree_evhandler(const char *ename, const void *earg, size_t size,
35817c478bd9Sstevel@tonic-gate     void *cookie)
35827c478bd9Sstevel@tonic-gate {
35837c478bd9Sstevel@tonic-gate 	char			*devfs_path;
35847c478bd9Sstevel@tonic-gate 	char			ptreepath[PATH_MAX];
35857c478bd9Sstevel@tonic-gate 	char			dipath[PATH_MAX];
35867c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		plafh;
35877c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
35887c478bd9Sstevel@tonic-gate 	nvlist_t		*nvlp;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	if (earg == NULL)
35917c478bd9Sstevel@tonic-gate 		return;
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	nvlp = NULL;
35947c478bd9Sstevel@tonic-gate 	if (ptree_get_node_by_path(PLATFORM_PATH, &plafh) != PICL_SUCCESS ||
35957c478bd9Sstevel@tonic-gate 	    nvlist_unpack((char *)earg, size, &nvlp, NULL) ||
35967c478bd9Sstevel@tonic-gate 	    nvlist_lookup_string(nvlp, PICLEVENTARG_DEVFS_PATH, &devfs_path) ||
35977c478bd9Sstevel@tonic-gate 	    strlen(devfs_path) > (PATH_MAX - sizeof (PLATFORM_PATH))) {
35987c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, PICL_EVENT_DROPPED, ename);
35997c478bd9Sstevel@tonic-gate 		if (nvlp)
36007c478bd9Sstevel@tonic-gate 			nvlist_free(nvlp);
36017c478bd9Sstevel@tonic-gate 		return;
36027c478bd9Sstevel@tonic-gate 	}
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	(void) strlcpy(ptreepath, PLATFORM_PATH, PATH_MAX);
36057c478bd9Sstevel@tonic-gate 	(void) strlcat(ptreepath, devfs_path, PATH_MAX);
36067c478bd9Sstevel@tonic-gate 	(void) strlcpy(dipath, devfs_path, PATH_MAX);
36077c478bd9Sstevel@tonic-gate 	nvlist_free(nvlp);
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	if (picldevtree_debug)
36107c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, "picldevtree: event handler invoked ename:%s "
36117c478bd9Sstevel@tonic-gate 		    "ptreepath:%s\n", ename, ptreepath);
36127c478bd9Sstevel@tonic-gate 
36135e3e415aSfw 	if (strcmp(ename, PICLEVENT_CPU_STATE_CHANGE) == 0) {
36145e3e415aSfw 		goto done;
36155e3e415aSfw 	}
36167c478bd9Sstevel@tonic-gate 	if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0) {
36177c478bd9Sstevel@tonic-gate 		di_node_t		devnode;
36187c478bd9Sstevel@tonic-gate 		char		*strp;
36197c478bd9Sstevel@tonic-gate 		picl_nodehdl_t	parh;
36207c478bd9Sstevel@tonic-gate 		char		nodeclass[PICL_CLASSNAMELEN_MAX];
36217c478bd9Sstevel@tonic-gate 		char		*nodename;
36227c478bd9Sstevel@tonic-gate 		int		err;
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 		/* If the node already exist, then nothing else to do here */
36257c478bd9Sstevel@tonic-gate 		if (ptree_get_node_by_path(ptreepath, &nodeh) == PICL_SUCCESS)
36267c478bd9Sstevel@tonic-gate 			return;
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 		/* Skip if unable to find parent PICL node handle */
36297c478bd9Sstevel@tonic-gate 		parh = plafh;
36307c478bd9Sstevel@tonic-gate 		if (((strp = strrchr(ptreepath, '/')) != NULL) &&
36317c478bd9Sstevel@tonic-gate 		    (strp != strchr(ptreepath, '/'))) {
36327c478bd9Sstevel@tonic-gate 			*strp = '\0';
36337c478bd9Sstevel@tonic-gate 			if (ptree_get_node_by_path(ptreepath, &parh) !=
36347c478bd9Sstevel@tonic-gate 			    PICL_SUCCESS)
36357c478bd9Sstevel@tonic-gate 				return;
36367c478bd9Sstevel@tonic-gate 		}
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 		/*
36397c478bd9Sstevel@tonic-gate 		 * If parent is the root node
36407c478bd9Sstevel@tonic-gate 		 */
36417c478bd9Sstevel@tonic-gate 		if (parh == plafh) {
36427c478bd9Sstevel@tonic-gate 			ph = di_prom_init();
36437c478bd9Sstevel@tonic-gate 			devnode = di_init(dipath, DINFOCPYALL);
36447c478bd9Sstevel@tonic-gate 			if (devnode == DI_NODE_NIL) {
36457c478bd9Sstevel@tonic-gate 				if (ph != NULL) {
36467c478bd9Sstevel@tonic-gate 					di_prom_fini(ph);
36477c478bd9Sstevel@tonic-gate 					ph = NULL;
36487c478bd9Sstevel@tonic-gate 				}
36497c478bd9Sstevel@tonic-gate 				return;
36507c478bd9Sstevel@tonic-gate 			}
36517c478bd9Sstevel@tonic-gate 			nodename = di_node_name(devnode);
36527c478bd9Sstevel@tonic-gate 			if (nodename == NULL) {
36537c478bd9Sstevel@tonic-gate 				di_fini(devnode);
36547c478bd9Sstevel@tonic-gate 				if (ph != NULL) {
36557c478bd9Sstevel@tonic-gate 					di_prom_fini(ph);
36567c478bd9Sstevel@tonic-gate 					ph = NULL;
36577c478bd9Sstevel@tonic-gate 				}
36587c478bd9Sstevel@tonic-gate 				return;
36597c478bd9Sstevel@tonic-gate 			}
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 			err = get_node_class(nodeclass, devnode, nodename);
36627c478bd9Sstevel@tonic-gate 			if (err < 0) {
36637c478bd9Sstevel@tonic-gate 				di_fini(devnode);
36647c478bd9Sstevel@tonic-gate 				if (ph != NULL) {
36657c478bd9Sstevel@tonic-gate 					di_prom_fini(ph);
36667c478bd9Sstevel@tonic-gate 					ph = NULL;
36677c478bd9Sstevel@tonic-gate 				}
36687c478bd9Sstevel@tonic-gate 				return;
36697c478bd9Sstevel@tonic-gate 			}
36707c478bd9Sstevel@tonic-gate 			err = construct_devtype_node(plafh, nodename,
36717c478bd9Sstevel@tonic-gate 			    nodeclass, devnode, &nodeh);
36727c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS) {
36737c478bd9Sstevel@tonic-gate 				di_fini(devnode);
36747c478bd9Sstevel@tonic-gate 				if (ph != NULL) {
36757c478bd9Sstevel@tonic-gate 					di_prom_fini(ph);
36767c478bd9Sstevel@tonic-gate 					ph = NULL;
36777c478bd9Sstevel@tonic-gate 				}
36787c478bd9Sstevel@tonic-gate 				return;
36797c478bd9Sstevel@tonic-gate 			}
36807c478bd9Sstevel@tonic-gate 			(void) update_subtree(nodeh, devnode);
36817c478bd9Sstevel@tonic-gate 			(void) add_unitaddr_prop_to_subtree(nodeh);
36827c478bd9Sstevel@tonic-gate 			if (ph != NULL) {
36837c478bd9Sstevel@tonic-gate 				di_prom_fini(ph);
36847c478bd9Sstevel@tonic-gate 				ph = NULL;
36857c478bd9Sstevel@tonic-gate 			}
36867c478bd9Sstevel@tonic-gate 			di_fini(devnode);
36877c478bd9Sstevel@tonic-gate 			goto done;
36887c478bd9Sstevel@tonic-gate 		}
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 		/* kludge ... try without bus-addr first */
36917c478bd9Sstevel@tonic-gate 		if ((strp = strrchr(dipath, '@')) != NULL) {
36927c478bd9Sstevel@tonic-gate 			char *p;
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate 			p = strrchr(dipath, '/');
36957c478bd9Sstevel@tonic-gate 			if (p != NULL && strp > p) {
36967c478bd9Sstevel@tonic-gate 				*strp = '\0';
36977c478bd9Sstevel@tonic-gate 				devnode = di_init(dipath, DINFOCPYALL);
36987c478bd9Sstevel@tonic-gate 				if (devnode != DI_NODE_NIL)
36997c478bd9Sstevel@tonic-gate 					di_fini(devnode);
37007c478bd9Sstevel@tonic-gate 				*strp = '@';
37017c478bd9Sstevel@tonic-gate 			}
37027c478bd9Sstevel@tonic-gate 		}
37037c478bd9Sstevel@tonic-gate 		/* Get parent devnode */
37047c478bd9Sstevel@tonic-gate 		if ((strp = strrchr(dipath, '/')) != NULL)
37057c478bd9Sstevel@tonic-gate 			*++strp = '\0';
37067c478bd9Sstevel@tonic-gate 		devnode = di_init(dipath, DINFOCPYALL);
37077c478bd9Sstevel@tonic-gate 		if (devnode == DI_NODE_NIL)
37087c478bd9Sstevel@tonic-gate 			return;
37097c478bd9Sstevel@tonic-gate 		ph = di_prom_init();
37107c478bd9Sstevel@tonic-gate 		(void) update_subtree(parh, devnode);
37117c478bd9Sstevel@tonic-gate 		(void) add_unitaddr_prop_to_subtree(parh);
37127c478bd9Sstevel@tonic-gate 		if (ph) {
37137c478bd9Sstevel@tonic-gate 			di_prom_fini(ph);
37147c478bd9Sstevel@tonic-gate 			ph = NULL;
37157c478bd9Sstevel@tonic-gate 		}
37167c478bd9Sstevel@tonic-gate 		di_fini(devnode);
37177c478bd9Sstevel@tonic-gate 	} else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0) {
37187c478bd9Sstevel@tonic-gate 		char			delclass[PICL_CLASSNAMELEN_MAX];
37197c478bd9Sstevel@tonic-gate 		char		*strp;
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 		/*
37227c478bd9Sstevel@tonic-gate 		 * if final element of path doesn't have a unit address
37237c478bd9Sstevel@tonic-gate 		 * then it is not uniquely identifiable - cannot remove
37247c478bd9Sstevel@tonic-gate 		 */
37257c478bd9Sstevel@tonic-gate 		if (((strp = strrchr(ptreepath, '/')) != NULL) &&
37267c478bd9Sstevel@tonic-gate 		    strchr(strp, '@') == NULL)
37277c478bd9Sstevel@tonic-gate 			return;
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 		/* skip if can't find the node */
37307c478bd9Sstevel@tonic-gate 		if (ptree_get_node_by_path(ptreepath, &nodeh) != PICL_SUCCESS)
37317c478bd9Sstevel@tonic-gate 			return;
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 		if (ptree_delete_node(nodeh) != PICL_SUCCESS)
37347c478bd9Sstevel@tonic-gate 			return;
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate 		if (picldevtree_debug)
37377c478bd9Sstevel@tonic-gate 			syslog(LOG_INFO,
37387c478bd9Sstevel@tonic-gate 			    "picldevtree: deleted node nodeh:%llx\n", nodeh);
37397c478bd9Sstevel@tonic-gate 		if ((ptree_get_propval_by_name(nodeh,
37407c478bd9Sstevel@tonic-gate 		    PICL_PROP_CLASSNAME, delclass, PICL_CLASSNAMELEN_MAX) ==
37417c478bd9Sstevel@tonic-gate 		    PICL_SUCCESS) && IS_MC(delclass)) {
37427c478bd9Sstevel@tonic-gate 			if (post_mc_event(PICLEVENT_MC_REMOVED, nodeh) !=
37437c478bd9Sstevel@tonic-gate 			    PICL_SUCCESS)
37447c478bd9Sstevel@tonic-gate 				syslog(LOG_WARNING, PICL_EVENT_DROPPED,
37457c478bd9Sstevel@tonic-gate 				    PICLEVENT_MC_REMOVED);
37467c478bd9Sstevel@tonic-gate 		} else
37477c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(nodeh);
37487c478bd9Sstevel@tonic-gate 	}
37497c478bd9Sstevel@tonic-gate done:
37507c478bd9Sstevel@tonic-gate 	(void) setup_cpus(plafh);
37517c478bd9Sstevel@tonic-gate 	(void) add_ffb_config_info(plafh);
37527ef4fcfbSvenki 	set_pci_pciex_deviceid(plafh);
37537c478bd9Sstevel@tonic-gate 	(void) set_sbus_slot(plafh);
37547c478bd9Sstevel@tonic-gate 	if (picldevtree_debug > 1)
37557c478bd9Sstevel@tonic-gate 		syslog(LOG_INFO, "picldevtree: event handler done\n");
37567c478bd9Sstevel@tonic-gate }
3757