xref: /titanic_44/usr/src/cmd/picl/plugins/common/devtree/picldevtree.c (revision 39c23413b8df94a95f67b34cfd4a4dfc3fd0b48d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PICL plug-in that creates device tree nodes for all platforms
30  */
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <assert.h>
38 #include <alloca.h>
39 #include <unistd.h>
40 #include <stropts.h>
41 #include <syslog.h>
42 #include <libdevinfo.h>
43 #include <sys/dkio.h>
44 #include <sys/vtoc.h>
45 #include <sys/time.h>
46 #include <fcntl.h>
47 #include <picl.h>
48 #include <picltree.h>
49 #include <sys/types.h>
50 #include <sys/processor.h>
51 #include <kstat.h>
52 #include <sys/sysinfo.h>
53 #include <dirent.h>
54 #include <libintl.h>
55 #include <pthread.h>
56 #include <libnvpair.h>
57 #include <sys/utsname.h>
58 #include <sys/systeminfo.h>
59 #include <sys/obpdefs.h>
60 #include <sys/openpromio.h>
61 #include "picldevtree.h"
62 
63 /*
64  * Plugin registration entry points
65  */
66 static void	picldevtree_register(void);
67 static void	picldevtree_init(void);
68 static void	picldevtree_fini(void);
69 
70 static void	picldevtree_evhandler(const char *ename, const void *earg,
71 		    size_t size, void *cookie);
72 
73 #pragma	init(picldevtree_register)
74 
75 /*
76  * Log message texts
77  */
78 #define	DEVINFO_PLUGIN_INIT_FAILED	gettext("SUNW_picldevtree failed!\n")
79 #define	PICL_EVENT_DROPPED	\
80 	gettext("SUNW_picldevtree '%s' event dropped.\n")
81 
82 /*
83  * Macro to get PCI device id (from IEEE 1275 spec)
84  */
85 #define	PCI_DEVICE_ID(x)			(((x) >> 11) & 0x1f)
86 /*
87  * Local variables
88  */
89 static picld_plugin_reg_t  my_reg_info = {
90 	PICLD_PLUGIN_VERSION_1,
91 	PICLD_PLUGIN_CRITICAL,
92 	"SUNW_picldevtree",
93 	picldevtree_init,
94 	picldevtree_fini
95 };
96 
97 /*
98  * Debug enabling environment variable
99  */
100 #define	SUNW_PICLDEVTREE_PLUGIN_DEBUG	"SUNW_PICLDEVTREE_PLUGIN_DEBUG"
101 static	int		picldevtree_debug = 0;
102 
103 static	conf_entries_t 	*conf_name_class_map = NULL;
104 static	builtin_map_t	sun4u_map[] = {
105 	/* MAX_NAMEVAL_SIZE */
106 	{ "SUNW,bpp", PICL_CLASS_PARALLEL},
107 	{ "parallel", PICL_CLASS_PARALLEL},
108 	{ "floppy", PICL_CLASS_FLOPPY},
109 	{ "memory", PICL_CLASS_MEMORY},
110 	{ "ebus", PICL_CLASS_EBUS},
111 	{ "i2c", PICL_CLASS_I2C},
112 	{ "usb", PICL_CLASS_USB},
113 	{ "isa", PICL_CLASS_ISA},
114 	{ "dma", PICL_CLASS_DMA},
115 	{ "keyboard", PICL_CLASS_KEYBOARD},
116 	{ "mouse", PICL_CLASS_MOUSE},
117 	{ "fan-control", PICL_CLASS_FAN_CONTROL},
118 	{ "sc", PICL_CLASS_SYSTEM_CONTROLLER},
119 	{ "dimm", PICL_CLASS_SEEPROM},
120 	{ "dimm-fru", PICL_CLASS_SEEPROM},
121 	{ "cpu", PICL_CLASS_SEEPROM},
122 	{ "cpu-fru", PICL_CLASS_SEEPROM},
123 	{ "flashprom", PICL_CLASS_FLASHPROM},
124 	{ "temperature", PICL_CLASS_TEMPERATURE_DEVICE},
125 	{ "motherboard", PICL_CLASS_SEEPROM},
126 	{ "motherboard-fru", PICL_CLASS_SEEPROM},
127 	{ "motherboard-fru-prom", PICL_CLASS_SEEPROM},
128 	{ "pmu", PICL_CLASS_PMU},
129 	{ "sound", PICL_CLASS_SOUND},
130 	{ "firewire", PICL_CLASS_FIREWIRE},
131 	{ "i2c-at34c02", PICL_CLASS_SEEPROM},
132 	{ "hardware-monitor", PICL_CLASS_HARDWARE_MONITOR},
133 	{ "", ""}
134 };
135 static	builtin_map_t	i86pc_map[] = {
136 	/* MAX_NAMEVAL_SIZE */
137 	{ "cpus", PICL_CLASS_I86CPUS},
138 	{ "cpu", PICL_CLASS_CPU},
139 	{ "memory", PICL_CLASS_MEMORY},
140 	{ "asy", PICL_CLASS_SERIAL},
141 	{ "", ""}
142 };
143 static	pname_type_map_t	pname_type_map[] = {
144 	{ "reg", PICL_PTYPE_BYTEARRAY},
145 	{ "device_type", PICL_PTYPE_CHARSTRING},
146 	{ "ranges", PICL_PTYPE_BYTEARRAY},
147 	{ "status", PICL_PTYPE_CHARSTRING},
148 	{ "compatible", PICL_PTYPE_CHARSTRING},
149 	{ "interrupts", PICL_PTYPE_BYTEARRAY},
150 	{ "model", PICL_PTYPE_CHARSTRING},
151 	{ "address", PICL_PTYPE_BYTEARRAY},
152 	{ "vendor-id", PICL_PTYPE_UNSIGNED_INT},
153 	{ "device-id", PICL_PTYPE_UNSIGNED_INT},
154 	{ "revision-id", PICL_PTYPE_UNSIGNED_INT},
155 	{ "class-code", PICL_PTYPE_UNSIGNED_INT},
156 	{ "min-grant", PICL_PTYPE_UNSIGNED_INT},
157 	{ "max-latency", PICL_PTYPE_UNSIGNED_INT},
158 	{ "devsel-speed", PICL_PTYPE_UNSIGNED_INT},
159 	{ "subsystem-id", PICL_PTYPE_UNSIGNED_INT},
160 	{ "subsystem-vendor-id", PICL_PTYPE_UNSIGNED_INT},
161 	{ "assigned-addresses", PICL_PTYPE_BYTEARRAY},
162 	{ "configuration#", PICL_PTYPE_UNSIGNED_INT},
163 	{ "assigned-address", PICL_PTYPE_UNSIGNED_INT},
164 	{ "#address-cells", PICL_PTYPE_UNSIGNED_INT},
165 	{ "#size-cells", PICL_PTYPE_UNSIGNED_INT},
166 	{ "clock-frequency", PICL_PTYPE_UNSIGNED_INT},
167 	{ "scsi-initiator-id", PICL_PTYPE_UNSIGNED_INT},
168 	{ "differential", PICL_PTYPE_UNSIGNED_INT},
169 	{ "idprom", PICL_PTYPE_BYTEARRAY},
170 	{ "bus-range", PICL_PTYPE_BYTEARRAY},
171 	{ "alternate-reg", PICL_PTYPE_BYTEARRAY},
172 	{ "power-consumption", PICL_PTYPE_BYTEARRAY},
173 	{ "slot-names", PICL_PTYPE_BYTEARRAY},
174 	{ "burst-sizes", PICL_PTYPE_UNSIGNED_INT},
175 	{ "up-burst-sizes", PICL_PTYPE_UNSIGNED_INT},
176 	{ "slot-address-bits", PICL_PTYPE_UNSIGNED_INT},
177 	{ "eisa-slots", PICL_PTYPE_BYTEARRAY},
178 	{ "dma", PICL_PTYPE_BYTEARRAY},
179 	{ "slot-names-index", PICL_PTYPE_UNSIGNED_INT},
180 	{ "pnp-csn", PICL_PTYPE_UNSIGNED_INT},
181 	{ "pnp-data", PICL_PTYPE_BYTEARRAY},
182 	{ "description", PICL_PTYPE_CHARSTRING},
183 	{ "pnp-id", PICL_PTYPE_CHARSTRING},
184 	{ "max-frame-size", PICL_PTYPE_UNSIGNED_INT},
185 	{ "address-bits", PICL_PTYPE_UNSIGNED_INT},
186 	{ "local-mac-address", PICL_PTYPE_BYTEARRAY},
187 	{ "mac-address", PICL_PTYPE_BYTEARRAY},
188 	{ "character-set", PICL_PTYPE_CHARSTRING},
189 	{ "available", PICL_PTYPE_BYTEARRAY},
190 	{ "port-wwn", PICL_PTYPE_BYTEARRAY},
191 	{ "node-wwn", PICL_PTYPE_BYTEARRAY},
192 	{ "width", PICL_PTYPE_UNSIGNED_INT},
193 	{ "linebytes", PICL_PTYPE_UNSIGNED_INT},
194 	{ "height", PICL_PTYPE_UNSIGNED_INT},
195 	{ "banner-name", PICL_PTYPE_CHARSTRING},
196 	{ "reset-reason", PICL_PTYPE_CHARSTRING},
197 	{ "implementation#", PICL_PTYPE_UNSIGNED_INT},
198 	{ "version#", PICL_PTYPE_UNSIGNED_INT},
199 	{ "icache-size", PICL_PTYPE_UNSIGNED_INT},
200 	{ "icache-line-size", PICL_PTYPE_UNSIGNED_INT},
201 	{ "icache-associativity", PICL_PTYPE_UNSIGNED_INT},
202 	{ "l1-icache-size", PICL_PTYPE_UNSIGNED_INT},
203 	{ "l1-icache-line-size", PICL_PTYPE_UNSIGNED_INT},
204 	{ "l1-icache-associativity", PICL_PTYPE_UNSIGNED_INT},
205 	{ "#itlb-entries", PICL_PTYPE_UNSIGNED_INT},
206 	{ "dcache-size", PICL_PTYPE_UNSIGNED_INT},
207 	{ "dcache-line-size", PICL_PTYPE_UNSIGNED_INT},
208 	{ "dcache-associativity", PICL_PTYPE_UNSIGNED_INT},
209 	{ "l1-dcache-size", PICL_PTYPE_UNSIGNED_INT},
210 	{ "l1-dcache-line-size", PICL_PTYPE_UNSIGNED_INT},
211 	{ "l1-dcache-associativity", PICL_PTYPE_UNSIGNED_INT},
212 	{ "#dtlb-entries", PICL_PTYPE_UNSIGNED_INT},
213 	{ "ecache-size", PICL_PTYPE_UNSIGNED_INT},
214 	{ "ecache-line-size", PICL_PTYPE_UNSIGNED_INT},
215 	{ "ecache-associativity", PICL_PTYPE_UNSIGNED_INT},
216 	{ "l2-cache-size", PICL_PTYPE_UNSIGNED_INT},
217 	{ "l2-cache-line-size", PICL_PTYPE_UNSIGNED_INT},
218 	{ "l2-cache-associativity", PICL_PTYPE_UNSIGNED_INT},
219 	{ "l2-cache-sharing", PICL_PTYPE_BYTEARRAY},
220 	{ "mask#", PICL_PTYPE_UNSIGNED_INT},
221 	{ "manufacturer#", PICL_PTYPE_UNSIGNED_INT},
222 	{ "sparc-version", PICL_PTYPE_UNSIGNED_INT},
223 	{ "version", PICL_PTYPE_CHARSTRING},
224 	{ "cpu-model", PICL_PTYPE_UNSIGNED_INT},
225 	{ "memory-layout", PICL_PTYPE_BYTEARRAY},
226 	{ "#interrupt-cells", PICL_PTYPE_UNSIGNED_INT},
227 	{ "interrupt-map", PICL_PTYPE_BYTEARRAY},
228 	{ "interrupt-map-mask", PICL_PTYPE_BYTEARRAY}
229 };
230 
231 #define	PNAME_MAP_SIZE	sizeof (pname_type_map) / sizeof (pname_type_map_t)
232 
233 static	builtin_map_t	*builtin_map_ptr = NULL;
234 static	int		builtin_map_size = 0;
235 static	char		mach_name[SYS_NMLN];
236 static	di_prom_handle_t	ph = DI_PROM_HANDLE_NIL;
237 
238 /*
239  * UnitAddress mapping table
240  */
241 static	unitaddr_func_t	encode_default_unitaddr;
242 static	unitaddr_func_t	encode_optional_unitaddr;
243 static	unitaddr_func_t	encode_scsi_unitaddr;
244 static	unitaddr_func_t	encode_upa_unitaddr;
245 static	unitaddr_func_t	encode_gptwo_jbus_unitaddr;
246 static	unitaddr_func_t	encode_pci_unitaddr;
247 
248 static	unitaddr_map_t unitaddr_map_table[] = {
249 	{PICL_CLASS_JBUS, encode_gptwo_jbus_unitaddr, 0},
250 	{PICL_CLASS_GPTWO, encode_gptwo_jbus_unitaddr, 0},
251 	{PICL_CLASS_PCI, encode_pci_unitaddr, 0},
252 	{PICL_CLASS_PCIEX, encode_pci_unitaddr, 0},
253 	{PICL_CLASS_UPA, encode_upa_unitaddr, 0},
254 	{PICL_CLASS_SCSI, encode_scsi_unitaddr, 0},
255 	{PICL_CLASS_SCSI2, encode_scsi_unitaddr, 0},
256 	{PICL_CLASS_EBUS, encode_default_unitaddr, 2},
257 	{PICL_CLASS_SBUS, encode_default_unitaddr, 2},
258 	{PICL_CLASS_I2C, encode_default_unitaddr, 2},
259 	{PICL_CLASS_USB, encode_default_unitaddr, 1},
260 	{PICL_CLASS_PMU, encode_optional_unitaddr, 2},
261 	{NULL, encode_default_unitaddr, 0}
262 };
263 
264 static int add_unitaddr_prop_to_subtree(picl_nodehdl_t nodeh);
265 static int get_unitaddr(picl_nodehdl_t parh, picl_nodehdl_t nodeh,
266 	char *unitaddr, size_t ualen);
267 static void set_pci_pciex_deviceid(picl_nodehdl_t plafh);
268 
269 /*
270  * The mc event completion handler.
271  * The arguments are event name buffer and a packed nvlist buffer
272  * with the size specifying the size of unpacked nvlist. These
273  * buffers are deallcoated here.
274  *
275  * Also, if a memory controller node is being removed then destroy the
276  * PICL subtree associated with that memory controller.
277  */
278 static void
279 mc_completion_handler(char *ename, void *earg, size_t size)
280 {
281 	picl_nodehdl_t	mch;
282 	nvlist_t	*unpack_nvl;
283 
284 	if (strcmp(ename, PICLEVENT_MC_REMOVED) == 0 &&
285 	    nvlist_unpack(earg, size, &unpack_nvl, NULL) == 0) {
286 		mch = NULL;
287 		(void) nvlist_lookup_uint64(unpack_nvl,
288 		    PICLEVENTARG_NODEHANDLE, &mch);
289 		if (mch != NULL) {
290 			if (picldevtree_debug)
291 				syslog(LOG_INFO,
292 				    "picldevtree: destroying_node:%llx\n",
293 				    mch);
294 			(void) ptree_destroy_node(mch);
295 		}
296 		nvlist_free(unpack_nvl);
297 	}
298 
299 	free(ename);
300 	free(earg);
301 }
302 
303 /*
304  * Functions to post memory controller change event
305  */
306 static int
307 post_mc_event(char *ename, picl_nodehdl_t mch)
308 {
309 	nvlist_t	*nvl;
310 	size_t		nvl_size;
311 	char		*pack_buf;
312 	char		*ev_name;
313 
314 	ev_name = strdup(ename);
315 	if (ev_name == NULL)
316 		return (-1);
317 
318 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, NULL)) {
319 		free(ev_name);
320 		return (-1);
321 	}
322 
323 	pack_buf = NULL;
324 	if (nvlist_add_uint64(nvl, PICLEVENTARG_NODEHANDLE, mch) ||
325 	    nvlist_pack(nvl, &pack_buf, &nvl_size, NV_ENCODE_NATIVE, NULL)) {
326 		free(ev_name);
327 		nvlist_free(nvl);
328 		return (-1);
329 	}
330 
331 	if (picldevtree_debug)
332 		syslog(LOG_INFO,
333 		    "picldevtree: posting MC event ename:%s nodeh:%llx\n",
334 		    ev_name, mch);
335 	if (ptree_post_event(ev_name, pack_buf, nvl_size,
336 	    mc_completion_handler) != PICL_SUCCESS) {
337 		free(ev_name);
338 		nvlist_free(nvl);
339 		return (-1);
340 	}
341 	nvlist_free(nvl);
342 	return (0);
343 }
344 
345 /*
346  * Lookup a name in the name to class map tables
347  */
348 static int
349 lookup_name_class_map(char *classbuf, const char *nm)
350 {
351 	conf_entries_t	*ptr;
352 	int		i;
353 
354 	/*
355 	 * check name to class mapping in conf file
356 	 */
357 	ptr = conf_name_class_map;
358 
359 	while (ptr != NULL) {
360 		if (strcmp(ptr->name, nm) == 0) {
361 			(void) strlcpy(classbuf, ptr->piclclass,
362 			    PICL_CLASSNAMELEN_MAX);
363 			return (0);
364 		}
365 		ptr = ptr->next;
366 	}
367 
368 	/*
369 	 * check name to class mapping in builtin table
370 	 */
371 	if (builtin_map_ptr == NULL)
372 		return (-1);
373 
374 	for (i = 0; i < builtin_map_size; ++i)
375 		if (strcmp(builtin_map_ptr[i].name, nm) == 0) {
376 			(void) strlcpy(classbuf, builtin_map_ptr[i].piclclass,
377 			    PICL_CLASSNAMELEN_MAX);
378 			return (0);
379 		}
380 	return (-1);
381 }
382 
383 /*
384  * Lookup a prop name in the pname to class map table
385  */
386 static int
387 lookup_pname_type_map(const char *pname, picl_prop_type_t *type)
388 {
389 	int		i;
390 
391 	for (i = 0; i < PNAME_MAP_SIZE; ++i)
392 		if (strcmp(pname_type_map[i].pname, pname) == 0) {
393 			*type = pname_type_map[i].type;
394 			return (0);
395 		}
396 
397 	return (-1);
398 }
399 
400 /*
401  * Return the number of strings in the buffer
402  */
403 static int
404 get_string_count(char *strdat, int length)
405 {
406 	int	count;
407 	char	*lastnull;
408 	char	*nullptr;
409 
410 	count = 1;
411 	for (lastnull = &strdat[length - 1], nullptr = strchr(strdat, '\0');
412 	    nullptr != lastnull; nullptr = strchr(nullptr+1, '\0'))
413 		count++;
414 
415 	return (count);
416 }
417 
418 /*
419  * Return 1 if the node has a "reg" property
420  */
421 static int
422 has_reg_prop(di_node_t dn)
423 {
424 	int			*pdata;
425 	int			dret;
426 
427 	dret = di_prop_lookup_ints(DDI_DEV_T_ANY, dn, OBP_REG, &pdata);
428 	if (dret > 0)
429 		return (1);
430 
431 	if (!ph)
432 		return (0);
433 	dret = di_prom_prop_lookup_ints(ph, dn, OBP_REG, &pdata);
434 	return (dret < 0 ? 0 : 1);
435 }
436 
437 /*
438  * This function copies a PROM node's device_type property value into the
439  * buffer given by outbuf. The buffer size is PICL_CLASSNAMELEN_MAX.
440  *
441  * We reclassify device_type 'fru-prom' to PICL class 'seeprom'
442  * for FRUID support.
443  */
444 static int
445 get_device_type(char *outbuf, di_node_t dn)
446 {
447 	char			*pdata;
448 	char			*pdatap;
449 	int			dret;
450 	int			i;
451 
452 	dret = di_prop_lookup_strings(DDI_DEV_T_ANY, dn, OBP_DEVICETYPE,
453 	    &pdata);
454 	if (dret <= 0) {
455 		if (!ph)
456 			return (-1);
457 
458 		dret = di_prom_prop_lookup_strings(ph, dn, OBP_DEVICETYPE,
459 		    &pdata);
460 		if (dret <= 0) {
461 			return (-1);
462 		}
463 	}
464 
465 	if (dret != 1) {
466 		/*
467 		 * multiple strings
468 		 */
469 		pdatap = pdata;
470 		for (i = 0; i < (dret - 1); ++i) {
471 			pdatap += strlen(pdatap);
472 			*pdatap = '-';	/* replace '\0' with '-' */
473 			pdatap++;
474 		}
475 	}
476 	if (strcasecmp(pdata, "fru-prom") == 0) {
477 		/*
478 		 * Use PICL 'seeprom' class for fru-prom device types
479 		 */
480 		(void) strlcpy(outbuf, PICL_CLASS_SEEPROM,
481 		    PICL_CLASSNAMELEN_MAX);
482 	} else {
483 		(void) strlcpy(outbuf, pdata, PICL_CLASSNAMELEN_MAX);
484 	}
485 	return (0);
486 }
487 
488 /*
489  * Get the minor node name in the class buffer passed
490  */
491 static int
492 get_minor_class(char *classbuf, di_node_t dn)
493 {
494 	di_minor_t	mi_node;
495 	char		*mi_nodetype;
496 	char		*mi_name;
497 
498 	/* get minor node type */
499 	mi_node = di_minor_next(dn, DI_MINOR_NIL);
500 	if (mi_node == DI_MINOR_NIL)
501 		return (-1);
502 
503 	mi_nodetype = di_minor_nodetype(mi_node);
504 	if (mi_nodetype == NULL) { /* no type info, return name */
505 		mi_name = di_minor_name(mi_node);
506 		if (mi_name == NULL)
507 			return (-1);
508 		(void) strlcpy(classbuf, mi_name, PICL_CLASSNAMELEN_MAX);
509 		return (0);
510 	}
511 
512 #define	DDI_NODETYPE(x, y) (strncmp(x, y, (sizeof (y) - 1)) == 0)
513 
514 	/*
515 	 * convert the string to the picl class for non-peudo nodes
516 	 */
517 	if (DDI_NODETYPE(mi_nodetype, DDI_PSEUDO))
518 		return (-1);
519 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK_WWN))
520 		(void) strcpy(classbuf, PICL_CLASS_BLOCK);
521 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK_CHAN))
522 		(void) strcpy(classbuf, PICL_CLASS_BLOCK);
523 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_CD))
524 		(void) strcpy(classbuf, PICL_CLASS_CDROM);
525 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_CD_CHAN))
526 		(void) strcpy(classbuf, PICL_CLASS_CDROM);
527 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_FD))
528 		(void) strcpy(classbuf, PICL_CLASS_FLOPPY);
529 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK_FABRIC))
530 		(void) strcpy(classbuf, PICL_CLASS_FABRIC);
531 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_BLOCK))
532 		(void) strcpy(classbuf, PICL_CLASS_BLOCK);
533 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_MOUSE))
534 		(void) strcpy(classbuf, PICL_CLASS_MOUSE);
535 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_KEYBOARD))
536 		(void) strcpy(classbuf, PICL_CLASS_KEYBOARD);
537 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_ATTACHMENT_POINT))
538 		(void) strcpy(classbuf, PICL_CLASS_ATTACHMENT_POINT);
539 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_TAPE))
540 		(void) strcpy(classbuf, PICL_CLASS_TAPE);
541 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_SCSI_ENCLOSURE))
542 		(void) strcpy(classbuf, PICL_CLASS_SCSI);
543 	else if (DDI_NODETYPE(mi_nodetype, DDI_NT_ENCLOSURE)) {
544 		char	*colon;
545 
546 		if ((colon = strchr(mi_nodetype, ':')) == NULL)
547 			return (-1);
548 		++colon;
549 		(void) strcpy(classbuf, colon);
550 	} else {	/* unrecognized type, return name */
551 		mi_name = di_minor_name(mi_node);
552 		if (mi_name == NULL)
553 			return (-1);
554 		(void) strlcpy(classbuf, mi_name, PICL_CLASSNAMELEN_MAX);
555 	}
556 	return (0);
557 }
558 
559 /*
560  * Derive PICL class using the compatible property of the node
561  * We use the map table to map compatible property value to
562  * class.
563  */
564 static int
565 get_compatible_class(char *outbuf, di_node_t dn)
566 {
567 	char			*pdata;
568 	char			*pdatap;
569 	int			dret;
570 	int			i;
571 
572 	dret = di_prop_lookup_strings(DDI_DEV_T_ANY, dn, OBP_COMPATIBLE,
573 	    &pdata);
574 	if (dret <= 0) {
575 		if (!ph)
576 			return (-1);
577 
578 		dret = di_prom_prop_lookup_strings(ph, dn, OBP_COMPATIBLE,
579 		    &pdata);
580 		if (dret <= 0) {
581 			return (-1);
582 		}
583 	}
584 
585 	pdatap = pdata;
586 	for (i = 0; i < dret; ++i) {
587 		if (lookup_name_class_map(outbuf, pdatap) == 0)
588 			return (0);
589 		pdatap += strlen(pdatap);
590 		pdatap++;
591 	}
592 	return (-1);
593 }
594 
595 /*
596  * For a given device node find the PICL class to use. Returns NULL
597  * for non device node
598  */
599 static int
600 get_node_class(char *classbuf, di_node_t dn, const char *nodename)
601 {
602 	if (get_device_type(classbuf, dn) == 0) {
603 		if (di_nodeid(dn) == DI_PROM_NODEID) {
604 			/*
605 			 * discard place holder nodes
606 			 */
607 			if ((strcmp(classbuf, DEVICE_TYPE_BLOCK) == 0) ||
608 			    (strcmp(classbuf, DEVICE_TYPE_BYTE) == 0) ||
609 			    (strcmp(classbuf, DEVICE_TYPE_SES) == 0) ||
610 			    (strcmp(classbuf, DEVICE_TYPE_FP) == 0) ||
611 			    (strcmp(classbuf, DEVICE_TYPE_DISK) == 0))
612 				return (-1);
613 
614 			return (0);
615 		}
616 		return (0);	/* return device_type value */
617 	}
618 
619 	if (get_compatible_class(classbuf, dn) == 0) {
620 		return (0);	/* derive class using compatible prop */
621 	}
622 
623 	if (lookup_name_class_map(classbuf, nodename) == 0)
624 		return (0);	/* derive class using name prop */
625 
626 	if (has_reg_prop(dn)) { /* use default obp-device */
627 		(void) strcpy(classbuf, PICL_CLASS_OBP_DEVICE);
628 		return (0);
629 	}
630 
631 	return (get_minor_class(classbuf, dn));
632 }
633 
634 /*
635  * Add a table property containing nrows with one column
636  */
637 static int
638 add_string_list_prop(picl_nodehdl_t nodeh, char *name, char *strlist,
639     unsigned int nrows)
640 {
641 	ptree_propinfo_t	propinfo;
642 	picl_prophdl_t		proph;
643 	picl_prophdl_t		tblh;
644 	int			err;
645 	unsigned int		i;
646 	unsigned int		j;
647 	picl_prophdl_t		*proprow;
648 	int			len;
649 
650 #define	NCOLS_IN_STRING_TABLE	1
651 
652 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
653 	    PICL_PTYPE_TABLE, PICL_READ, sizeof (picl_prophdl_t), name,
654 	    NULL, NULL);
655 	if (err != PICL_SUCCESS)
656 		return (err);
657 
658 	err = ptree_create_table(&tblh);
659 	if (err != PICL_SUCCESS)
660 		return (err);
661 
662 	err = ptree_create_and_add_prop(nodeh, &propinfo, &tblh, &proph);
663 	if (err != PICL_SUCCESS)
664 		return (err);
665 
666 	proprow = alloca(sizeof (picl_prophdl_t) * nrows);
667 	if (proprow == NULL) {
668 		(void) ptree_destroy_prop(proph);
669 		return (PICL_FAILURE);
670 	}
671 
672 	for (j = 0; j < nrows; ++j) {
673 		len = strlen(strlist) + 1;
674 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
675 		    PICL_PTYPE_CHARSTRING, PICL_READ, len, name,
676 		    NULL, NULL);
677 		if (err != PICL_SUCCESS)
678 			break;
679 		err = ptree_create_prop(&propinfo, strlist, &proprow[j]);
680 		if (err != PICL_SUCCESS)
681 			break;
682 		strlist += len;
683 		err = ptree_add_row_to_table(tblh, NCOLS_IN_STRING_TABLE,
684 		    &proprow[j]);
685 		if (err != PICL_SUCCESS)
686 			break;
687 	}
688 
689 	if (err != PICL_SUCCESS) {
690 		for (i = 0; i < j; ++i)
691 			(void) ptree_destroy_prop(proprow[i]);
692 		(void) ptree_delete_prop(proph);
693 		(void) ptree_destroy_prop(proph);
694 		return (err);
695 	}
696 
697 	return (PICL_SUCCESS);
698 }
699 
700 /*
701  * return 1 if this node has this property with the given value
702  */
703 static int
704 compare_string_propval(picl_nodehdl_t nodeh, const char *pname,
705     const char *pval)
706 {
707 	char			*pvalbuf;
708 	int			err;
709 	int			len;
710 	ptree_propinfo_t	pinfo;
711 	picl_prophdl_t		proph;
712 
713 	err = ptree_get_prop_by_name(nodeh, pname, &proph);
714 	if (err != PICL_SUCCESS)	/* prop doesn't exist */
715 		return (0);
716 
717 	err = ptree_get_propinfo(proph, &pinfo);
718 	if (pinfo.piclinfo.type != PICL_PTYPE_CHARSTRING)
719 		return (0);	/* not string prop */
720 
721 	len = strlen(pval) + 1;
722 
723 	pvalbuf = alloca(len);
724 	if (pvalbuf == NULL)
725 		return (0);
726 
727 	err = ptree_get_propval(proph, pvalbuf, len);
728 	if ((err == PICL_SUCCESS) && (strcmp(pvalbuf, pval) == 0))
729 		return (1);	/* prop match */
730 
731 	return (0);
732 }
733 
734 /*
735  * This function recursively searches the tree for a node that has
736  * the specified string property name and value
737  */
738 static int
739 find_node_by_string_prop(picl_nodehdl_t rooth, const char *pname,
740     const char *pval, picl_nodehdl_t *nodeh)
741 {
742 	picl_nodehdl_t		childh;
743 	int			err;
744 
745 	for (err = ptree_get_propval_by_name(rooth, PICL_PROP_CHILD, &childh,
746 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
747 		err = ptree_get_propval_by_name(childh, PICL_PROP_PEER, &childh,
748 		    sizeof (picl_nodehdl_t))) {
749 		if (err != PICL_SUCCESS)
750 			return (err);
751 
752 		if (compare_string_propval(childh, pname, pval)) {
753 			*nodeh = childh;
754 			return (PICL_SUCCESS);
755 		}
756 
757 		if (find_node_by_string_prop(childh, pname, pval, nodeh) ==
758 		    PICL_SUCCESS)
759 			return (PICL_SUCCESS);
760 	}
761 
762 	return (PICL_FAILURE);
763 }
764 
765 /*
766  * check if this is a string prop
767  * If the length is less than or equal to 4, assume it's not a string list.
768  * If there is any non-ascii or non-print char, it's not a string prop
769  * If \0 is in the first char or any two consecutive \0's exist,
770  * it's a bytearray prop.
771  * Return value: 0 means it's not a string prop, 1 means it's a string prop
772  */
773 static int
774 is_string_propval(unsigned char *pdata, int len)
775 {
776 	int	i;
777 	int	lastindex;
778 	int	prevnull = -1;
779 
780 	switch (len) {
781 	case 1:
782 		if (!isascii(pdata[0]) || !isprint(pdata[0]))
783 			return (0);
784 		return (1);
785 	case 2:
786 	case 3:
787 	case 4:
788 		lastindex = len;
789 		if (pdata[len-1] == '\0')
790 			lastindex = len - 1;
791 
792 		for (i = 0; i < lastindex; i++)
793 			if (!isascii(pdata[i]) || !isprint(pdata[i]))
794 				return (0);
795 
796 		return (1);
797 
798 	default:
799 		if (len <= 0)
800 			return (0);
801 		for (i = 0; i < len; i++) {
802 			if (!isascii(pdata[i]) || !isprint(pdata[i])) {
803 				if (pdata[i] != '\0')
804 					return (0);
805 				/*
806 				 * if the null char is in the first char
807 				 * or two consecutive nulls' exist,
808 				 * it's a bytearray prop
809 				 */
810 				if ((i == 0) || ((i - prevnull) == 1))
811 					return (0);
812 
813 				prevnull = i;
814 			}
815 		}
816 		break;
817 	}
818 
819 	return (1);
820 }
821 
822 /*
823  * This function counts the number of strings in the value buffer pdata
824  * and creates a property.
825  * If there is only one string in the buffer, pdata, a charstring property
826  * type is created and added.
827  * If there are more than one string in the buffer, pdata, then a table
828  * of charstrings is added.
829  */
830 static int
831 process_charstring_data(picl_nodehdl_t nodeh, char *pname, unsigned char *pdata,
832     int retval)
833 {
834 	int			err;
835 	int			strcount;
836 	char			*strdat;
837 	ptree_propinfo_t	propinfo;
838 
839 	/*
840 	 * append the null char at the end of string when there is
841 	 * no null terminator
842 	 */
843 	if (pdata[retval - 1] != '\0') {
844 		strdat = alloca(retval + 1);
845 		(void) memcpy(strdat, pdata, retval);
846 		strdat[retval] = '\0';
847 		retval++;
848 	} else {
849 		strdat = alloca(retval);
850 		(void) memcpy(strdat, pdata, retval);
851 	}
852 
853 	/*
854 	 * If it's a string list, create a table prop
855 	 */
856 	strcount = get_string_count(strdat, retval);
857 	if (strcount > 1) {
858 		err = add_string_list_prop(nodeh, pname,
859 		    strdat, strcount);
860 		if (err != PICL_SUCCESS)
861 			return (err);
862 	} else {
863 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
864 		    PICL_PTYPE_CHARSTRING, PICL_READ,
865 		    strlen(strdat) + 1, pname, NULL,
866 		    NULL);
867 		if (err != PICL_SUCCESS)
868 			return (err);
869 		(void) ptree_create_and_add_prop(nodeh, &propinfo,
870 		    strdat, NULL);
871 	}
872 	return (PICL_SUCCESS);
873 }
874 
875 /*
876  * Add the OBP properties as properties of the PICL node
877  */
878 static int
879 add_openprom_props(picl_nodehdl_t nodeh, di_node_t di_node)
880 {
881 	di_prom_prop_t		promp;
882 	char			*pname;
883 	unsigned char		*pdata;
884 	int			retval;
885 	ptree_propinfo_t	propinfo;
886 	int			err;
887 	picl_prop_type_t	type;
888 
889 	if (!ph)
890 		return (PICL_FAILURE);
891 
892 	for (promp = di_prom_prop_next(ph, di_node, DI_PROM_PROP_NIL);
893 	    promp != DI_PROM_PROP_NIL;
894 		promp = di_prom_prop_next(ph, di_node, promp)) {
895 
896 		pname = di_prom_prop_name(promp);
897 
898 		retval = di_prom_prop_data(promp, &pdata);
899 		if (retval < 0) {
900 			return (PICL_SUCCESS);
901 		}
902 		if (retval == 0) {
903 			err = ptree_init_propinfo(&propinfo,
904 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_VOID,
905 			    PICL_READ, (size_t)0, pname, NULL, NULL);
906 			if (err != PICL_SUCCESS) {
907 				return (err);
908 			}
909 			(void) ptree_create_and_add_prop(nodeh, &propinfo, NULL,
910 			    NULL);
911 			continue;
912 		}
913 
914 		/*
915 		 * Get the prop type from pname map table
916 		 */
917 		if (lookup_pname_type_map(pname, &type) == 0) {
918 			if (type == PICL_PTYPE_CHARSTRING) {
919 				err = process_charstring_data(nodeh, pname,
920 				    pdata, retval);
921 				if (err != PICL_SUCCESS) {
922 					return (err);
923 				}
924 				continue;
925 			}
926 
927 			err = ptree_init_propinfo(&propinfo,
928 			    PTREE_PROPINFO_VERSION, type, PICL_READ,
929 			    retval, pname, NULL, NULL);
930 			if (err != PICL_SUCCESS) {
931 				return (err);
932 			}
933 			(void) ptree_create_and_add_prop(nodeh, &propinfo,
934 			    pdata, NULL);
935 		} else if (!is_string_propval(pdata, retval)) {
936 			switch (retval) {
937 			case sizeof (uint8_t):
938 				/*FALLTHROUGH*/
939 			case sizeof (uint16_t):
940 				/*FALLTHROUGH*/
941 			case sizeof (uint32_t):
942 				type = PICL_PTYPE_UNSIGNED_INT;
943 				break;
944 			default:
945 				type = PICL_PTYPE_BYTEARRAY;
946 				break;
947 			}
948 			err = ptree_init_propinfo(&propinfo,
949 			    PTREE_PROPINFO_VERSION, type, PICL_READ,
950 			    retval, pname, NULL, NULL);
951 			if (err != PICL_SUCCESS) {
952 				return (err);
953 			}
954 			(void) ptree_create_and_add_prop(nodeh, &propinfo,
955 			    pdata, NULL);
956 		} else {
957 			err = process_charstring_data(nodeh, pname, pdata,
958 			    retval);
959 			if (err != PICL_SUCCESS) {
960 				return (err);
961 			}
962 		}
963 	}
964 
965 	return (PICL_SUCCESS);
966 }
967 
968 static void
969 add_boolean_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val)
970 {
971 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
972 	    PICL_PTYPE_VOID, PICL_READ, (size_t)0, di_val, NULL, NULL);
973 	(void) ptree_create_and_add_prop(nodeh, &propinfo, NULL, NULL);
974 }
975 
976 static void
977 add_uints_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val,
978     int *idata, int len)
979 {
980 	if (len == 1)
981 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
982 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (int), di_val,
983 		    NULL, NULL);
984 	else
985 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
986 		    PICL_PTYPE_BYTEARRAY, PICL_READ, len * sizeof (int), di_val,
987 		    NULL, NULL);
988 
989 	(void) ptree_create_and_add_prop(nodeh, &propinfo, idata, NULL);
990 }
991 
992 static void
993 add_strings_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val,
994     char *sdata, int len)
995 {
996 	if (len == 1) {
997 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
998 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(sdata) + 1, di_val,
999 		    NULL, NULL);
1000 		(void) ptree_create_and_add_prop(nodeh, &propinfo, sdata, NULL);
1001 	} else {
1002 		(void) add_string_list_prop(nodeh, di_val, sdata, len);
1003 	}
1004 }
1005 
1006 static void
1007 add_bytes_prop(picl_nodehdl_t nodeh, ptree_propinfo_t propinfo, char *di_val,
1008     unsigned char *bdata, int len)
1009 {
1010 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1011 	    PICL_PTYPE_BYTEARRAY, PICL_READ, len, di_val, NULL, NULL);
1012 	(void) ptree_create_and_add_prop(nodeh, &propinfo, bdata, NULL);
1013 }
1014 
1015 /*
1016  * Add properties provided by libdevinfo
1017  */
1018 static void
1019 add_devinfo_props(picl_nodehdl_t nodeh, di_node_t di_node)
1020 {
1021 	int			instance;
1022 	char			*di_val;
1023 	di_prop_t		di_prop;
1024 	int			di_ptype;
1025 	ptree_propinfo_t	propinfo;
1026 	char			*sdata;
1027 	unsigned char		*bdata;
1028 	int			*idata;
1029 	int			len;
1030 
1031 	instance = di_instance(di_node);
1032 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1033 	    PICL_PTYPE_INT, PICL_READ, sizeof (instance), PICL_PROP_INSTANCE,
1034 	    NULL, NULL);
1035 	(void) ptree_create_and_add_prop(nodeh, &propinfo, &instance, NULL);
1036 
1037 	di_val = di_bus_addr(di_node);
1038 	if (di_val) {
1039 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1040 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
1041 		    PICL_PROP_BUS_ADDR, NULL, NULL);
1042 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
1043 		    NULL);
1044 	}
1045 
1046 	di_val = di_binding_name(di_node);
1047 	if (di_val) {
1048 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1049 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
1050 		    PICL_PROP_BINDING_NAME, NULL, NULL);
1051 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
1052 		    NULL);
1053 	}
1054 
1055 	di_val = di_driver_name(di_node);
1056 	if (di_val) {
1057 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1058 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
1059 		    PICL_PROP_DRIVER_NAME, NULL, NULL);
1060 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
1061 		    NULL);
1062 	}
1063 
1064 	di_val = di_devfs_path(di_node);
1065 	if (di_val) {
1066 		(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1067 		    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(di_val) + 1,
1068 		    PICL_PROP_DEVFS_PATH, NULL, NULL);
1069 		(void) ptree_create_and_add_prop(nodeh, &propinfo, di_val,
1070 		    NULL);
1071 		di_devfs_path_free(di_val);
1072 	}
1073 
1074 	for (di_prop = di_prop_next(di_node, DI_PROP_NIL);
1075 	    di_prop != DI_PROP_NIL;
1076 		di_prop = di_prop_next(di_node, di_prop)) {
1077 
1078 		di_val = di_prop_name(di_prop);
1079 		di_ptype = di_prop_type(di_prop);
1080 
1081 		switch (di_ptype) {
1082 		case DI_PROP_TYPE_BOOLEAN:
1083 			add_boolean_prop(nodeh, propinfo, di_val);
1084 			break;
1085 		case DI_PROP_TYPE_INT:
1086 			len = di_prop_ints(di_prop, &idata);
1087 			if (len < 0)
1088 				/* Received error, so ignore prop */
1089 				break;
1090 			add_uints_prop(nodeh, propinfo, di_val, idata, len);
1091 			break;
1092 		case DI_PROP_TYPE_STRING:
1093 			len = di_prop_strings(di_prop, &sdata);
1094 			if (len < 0)
1095 				break;
1096 			add_strings_prop(nodeh, propinfo, di_val, sdata, len);
1097 			break;
1098 		case DI_PROP_TYPE_BYTE:
1099 			len = di_prop_bytes(di_prop, &bdata);
1100 			if (len < 0)
1101 				break;
1102 			add_bytes_prop(nodeh, propinfo, di_val, bdata, len);
1103 			break;
1104 		case DI_PROP_TYPE_UNKNOWN:
1105 			/*
1106 			 * Unknown type, we'll try and guess what it should be.
1107 			 */
1108 			len = di_prop_strings(di_prop, &sdata);
1109 			if ((len > 0) && (sdata[0] != 0)) {
1110 				add_strings_prop(nodeh, propinfo, di_val, sdata,
1111 				    len);
1112 				break;
1113 			}
1114 			len = di_prop_ints(di_prop, &idata);
1115 			if (len > 0) {
1116 				add_uints_prop(nodeh, propinfo, di_val,
1117 				    idata, len);
1118 				break;
1119 			}
1120 			len = di_prop_rawdata(di_prop, &bdata);
1121 			if (len > 0)
1122 				add_bytes_prop(nodeh, propinfo,
1123 				    di_val, bdata, len);
1124 			else if (len == 0)
1125 				add_boolean_prop(nodeh, propinfo,
1126 				    di_val);
1127 			break;
1128 		case DI_PROP_TYPE_UNDEF_IT:
1129 			break;
1130 		default:
1131 			break;
1132 		}
1133 	}
1134 }
1135 
1136 /*
1137  * This function creates the /obp node in the PICL tree for OBP nodes
1138  * without a device type class.
1139  */
1140 static int
1141 construct_picl_openprom(picl_nodehdl_t rooth, picl_nodehdl_t *obph)
1142 {
1143 	picl_nodehdl_t	tmph;
1144 	int		err;
1145 
1146 	err = ptree_create_and_add_node(rooth, PICL_NODE_OBP,
1147 	    PICL_CLASS_PICL, &tmph);
1148 
1149 	if (err != PICL_SUCCESS)
1150 		return (err);
1151 	*obph = tmph;
1152 	return (PICL_SUCCESS);
1153 }
1154 
1155 /*
1156  * This function creates the /platform node in the PICL tree and
1157  * its properties. It sets the "platform-name" property to the
1158  * platform name
1159  */
1160 static int
1161 construct_picl_platform(picl_nodehdl_t rooth, di_node_t di_root,
1162     picl_nodehdl_t *piclh)
1163 {
1164 	int			err;
1165 	picl_nodehdl_t		plafh;
1166 	char			*nodename;
1167 	char			nodeclass[PICL_CLASSNAMELEN_MAX];
1168 	ptree_propinfo_t	propinfo;
1169 	picl_prophdl_t		proph;
1170 
1171 	nodename = di_node_name(di_root);
1172 	if (nodename == NULL)
1173 		return (PICL_FAILURE);
1174 
1175 	err = 0;
1176 	if (di_nodeid(di_root) == DI_PROM_NODEID ||
1177 	    di_nodeid(di_root) == DI_SID_NODEID)
1178 		err = get_device_type(nodeclass, di_root);
1179 
1180 	if (err < 0)
1181 		(void) strcpy(nodeclass, PICL_CLASS_UPA);	/* default */
1182 
1183 	err = ptree_create_and_add_node(rooth, PICL_NODE_PLATFORM,
1184 	    nodeclass, &plafh);
1185 	if (err != PICL_SUCCESS)
1186 		return (err);
1187 
1188 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1189 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(nodename) + 1,
1190 	    PICL_PROP_PLATFORM_NAME, NULL, NULL);
1191 	err = ptree_create_and_add_prop(plafh, &propinfo, nodename, &proph);
1192 	if (err != PICL_SUCCESS)
1193 		return (err);
1194 
1195 	(void) add_devinfo_props(plafh, di_root);
1196 
1197 	(void) add_openprom_props(plafh, di_root);
1198 
1199 	*piclh = plafh;
1200 
1201 	return (PICL_SUCCESS);
1202 }
1203 
1204 /*
1205  * This function creates a node in /obp tree for the libdevinfo handle.
1206  */
1207 static int
1208 construct_obp_node(picl_nodehdl_t parh, di_node_t dn, picl_nodehdl_t *chdh)
1209 {
1210 	int		err;
1211 	char		*nodename;
1212 	char		nodeclass[PICL_CLASSNAMELEN_MAX];
1213 	picl_nodehdl_t	anodeh;
1214 
1215 	nodename = di_node_name(dn);	/* PICL_PROP_NAME */
1216 	if (nodename == NULL)
1217 		return (PICL_FAILURE);
1218 
1219 	if (strcmp(nodename, "pseudo") == 0)
1220 		return (PICL_FAILURE);
1221 
1222 	if ((di_nodeid(dn) == DI_PROM_NODEID) &&
1223 	    (get_device_type(nodeclass, dn) == 0))
1224 		return (PICL_FAILURE);
1225 
1226 	err = ptree_create_and_add_node(parh, nodename, nodename, &anodeh);
1227 	if (err != PICL_SUCCESS)
1228 		return (err);
1229 
1230 	add_devinfo_props(anodeh, dn);
1231 
1232 	(void) add_openprom_props(anodeh, dn);
1233 
1234 	*chdh = anodeh;
1235 
1236 	return (PICL_SUCCESS);
1237 }
1238 
1239 /*
1240  * This function creates a PICL node in /platform tree for a device
1241  */
1242 static int
1243 construct_devtype_node(picl_nodehdl_t parh, char *nodename,
1244     char *nodeclass, di_node_t dn, picl_nodehdl_t *chdh)
1245 {
1246 	int			err;
1247 	picl_nodehdl_t		anodeh;
1248 
1249 	err = ptree_create_and_add_node(parh, nodename, nodeclass, &anodeh);
1250 	if (err != PICL_SUCCESS)
1251 		return (err);
1252 
1253 	(void) add_devinfo_props(anodeh, dn);
1254 	(void) add_openprom_props(anodeh, dn);
1255 
1256 	*chdh = anodeh;
1257 	return (err);
1258 }
1259 
1260 /*
1261  * Create a subtree of "picl" class nodes in /obp for these nodes
1262  */
1263 static int
1264 construct_openprom_tree(picl_nodehdl_t nodeh, di_node_t  dinode)
1265 {
1266 	di_node_t	cnode;
1267 	picl_nodehdl_t	chdh;
1268 	int		err;
1269 
1270 	err = construct_obp_node(nodeh, dinode, &chdh);
1271 	if (err != PICL_SUCCESS)
1272 		return (err);
1273 
1274 	for (cnode = di_child_node(dinode); cnode != DI_NODE_NIL;
1275 	    cnode = di_sibling_node(cnode))
1276 		(void) construct_openprom_tree(chdh, cnode);
1277 
1278 	return (PICL_SUCCESS);
1279 
1280 }
1281 
1282 /*
1283  * Process the libdevinfo device tree and create nodes in /platform or /obp
1284  * PICL tree.
1285  *
1286  * This routine traverses the immediate children of "dinode" device and
1287  * determines the node class for that child. If it finds a valid class
1288  * name, then it builds a PICL node under /platform subtree and calls itself
1289  * recursively to construct the subtree for that child node. Otherwise, if
1290  * the parent_class is NULL, then it constructs a node and subtree under /obp
1291  * subtree.
1292  *
1293  * Note that we skip the children nodes that don't have a valid class name
1294  * and the parent_class is non NULL to prevent creation of any placeholder
1295  * nodes (such as sd,...).
1296  */
1297 static int
1298 construct_devinfo_tree(picl_nodehdl_t plafh, picl_nodehdl_t obph,
1299     di_node_t dinode, char *parent_class)
1300 {
1301 	di_node_t	cnode;
1302 	picl_nodehdl_t	chdh;
1303 	char		nodeclass[PICL_CLASSNAMELEN_MAX];
1304 	char		*nodename;
1305 	int		err;
1306 
1307 	err = PICL_SUCCESS;
1308 	for (cnode = di_child_node(dinode); cnode != DI_NODE_NIL;
1309 	    cnode = di_sibling_node(cnode)) {
1310 		nodename = di_node_name(cnode);	/* PICL_PROP_NAME */
1311 		if (nodename == NULL)
1312 			continue;
1313 
1314 		err = get_node_class(nodeclass, cnode, nodename);
1315 
1316 		if (err == 0) {
1317 			err = construct_devtype_node(plafh, nodename,
1318 			    nodeclass, cnode, &chdh);
1319 			if (err != PICL_SUCCESS)
1320 				return (err);
1321 			err = construct_devinfo_tree(chdh, obph, cnode,
1322 			    nodeclass);
1323 		} else if (parent_class == NULL)
1324 			err = construct_openprom_tree(obph, cnode);
1325 		else
1326 			continue;
1327 		/*
1328 		 * if parent_class is non NULL, skip the children nodes
1329 		 * that don't have a valid device class - eliminates
1330 		 * placeholder nodes (sd,...) from being created.
1331 		 */
1332 	}
1333 
1334 	return (err);
1335 
1336 }
1337 
1338 /*
1339  * This function is called from the event handler called from the daemon
1340  * on PICL events.
1341  *
1342  * This routine traverses the children of the "dinode" device and
1343  * creates a PICL node for each child not found in the PICL tree and
1344  * invokes itself recursively to create a subtree for the newly created
1345  * child node. It also checks if the node being created is a meory
1346  * controller. If so, it posts PICLEVENT_MC_ADDED PICL event to the PICL
1347  * framework.
1348  */
1349 static int
1350 update_subtree(picl_nodehdl_t nodeh, di_node_t dinode)
1351 {
1352 	di_node_t	cnode;
1353 	picl_nodehdl_t	chdh;
1354 	picl_nodehdl_t	nh;
1355 	char		*nodename;
1356 	char		nodeclass[PICL_CLASSNAMELEN_MAX];
1357 	char		*path_buf;
1358 	char		buf[MAX_UNIT_ADDRESS_LEN];
1359 	char		unitaddr[MAX_UNIT_ADDRESS_LEN];
1360 	char		path_w_ua[MAXPATHLEN];
1361 	char		path_wo_ua[MAXPATHLEN];
1362 	char		*strp;
1363 	int		gotit;
1364 	int		err;
1365 
1366 	for (cnode = di_child_node(dinode); cnode != DI_NODE_NIL;
1367 	    cnode = di_sibling_node(cnode)) {
1368 		path_buf = di_devfs_path(cnode);
1369 		if (path_buf == NULL)
1370 			continue;
1371 
1372 		nodename = di_node_name(cnode);
1373 		if (nodename == NULL) {
1374 			di_devfs_path_free(path_buf);
1375 			continue;
1376 		}
1377 
1378 		err = get_node_class(nodeclass, cnode, nodename);
1379 
1380 		if (err < 0) {
1381 			di_devfs_path_free(path_buf);
1382 			continue;
1383 		}
1384 
1385 		/*
1386 		 * this is quite complicated - both path_buf and any nodes
1387 		 * already in the picl tree may, or may not, have the
1388 		 * @<unit_addr> at the end of their names. So we must
1389 		 * take path_buf and work out what the device path would
1390 		 * be both with and without the unit_address, then search
1391 		 * the picl tree for both forms.
1392 		 */
1393 		if (((strp = strrchr(path_buf, '/')) != NULL) &&
1394 		    strchr(strp, '@') == NULL) {
1395 			/*
1396 			 * this is an unattached node - so the path is not
1397 			 * unique. Need to find out which node it is.
1398 			 * Find the unit_address from the obp properties.
1399 			 */
1400 			err = ptree_create_node(nodename, nodeclass, &chdh);
1401 			if (err != PICL_SUCCESS)
1402 				return (err);
1403 			(void) add_openprom_props(chdh, cnode);
1404 			err = get_unitaddr(nodeh, chdh, unitaddr,
1405 			    sizeof (unitaddr));
1406 			if (err != PICL_SUCCESS)
1407 				return (err);
1408 			(void) ptree_destroy_node(chdh);
1409 			(void) snprintf(path_w_ua, sizeof (path_w_ua), "%s@%s",
1410 			    path_buf, unitaddr);
1411 			(void) snprintf(path_wo_ua, sizeof (path_wo_ua), "%s",
1412 			    path_buf);
1413 		} else {
1414 			/*
1415 			 * this is an attached node - so the path is unique
1416 			 */
1417 			(void) snprintf(path_w_ua, sizeof (path_w_ua), "%s",
1418 			    path_buf);
1419 			(void) snprintf(path_wo_ua, sizeof (path_wo_ua), "%s",
1420 			    path_buf);
1421 			strp = strrchr(path_wo_ua, '@');
1422 			*strp++ = '\0';
1423 			(void) snprintf(unitaddr, sizeof (unitaddr), "%s",
1424 			    strp);
1425 		}
1426 		/*
1427 		 * first look for node with unit address in devfs_path
1428 		 */
1429 		if (ptree_find_node(nodeh, PICL_PROP_DEVFS_PATH,
1430 		    PICL_PTYPE_CHARSTRING, path_w_ua, strlen(path_w_ua) + 1,
1431 		    &nh) == PICL_SUCCESS) {
1432 			/*
1433 			 * node already there - there's nothing we need to do
1434 			 */
1435 			if (picldevtree_debug > 1)
1436 				syslog(LOG_INFO,
1437 				    "update_subtree: path:%s node exists\n",
1438 				    path_buf);
1439 			di_devfs_path_free(path_buf);
1440 			continue;
1441 		}
1442 		/*
1443 		 * now look for node without unit address in devfs_path.
1444 		 * This might be just one out of several
1445 		 * nodes - need to check all siblings
1446 		 */
1447 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD,
1448 		    &chdh, sizeof (chdh));
1449 		if ((err != PICL_SUCCESS) && (err != PICL_PROPNOTFOUND))
1450 			return (err);
1451 		gotit = 0;
1452 		while (err == PICL_SUCCESS) {
1453 			err = ptree_get_propval_by_name(chdh,
1454 			    PICL_PROP_DEVFS_PATH, buf, sizeof (buf));
1455 			if (err != PICL_SUCCESS)
1456 				return (err);
1457 			if (strcmp(buf, path_wo_ua) == 0) {
1458 				err = ptree_get_propval_by_name(chdh,
1459 				    PICL_PROP_UNIT_ADDRESS, buf, sizeof (buf));
1460 				if (err != PICL_SUCCESS)
1461 					return (err);
1462 				if (strcmp(buf, unitaddr) == 0) {
1463 					gotit = 1;
1464 					break;
1465 				}
1466 			}
1467 			err = ptree_get_propval_by_name(chdh,
1468 			    PICL_PROP_PEER, &chdh, sizeof (chdh));
1469 			if (err != PICL_SUCCESS)
1470 				break;
1471 		}
1472 		if (gotit) {
1473 			/*
1474 			 * node already there - there's nothing we need to do
1475 			 */
1476 			if (picldevtree_debug > 1)
1477 				syslog(LOG_INFO,
1478 				    "update_subtree: path:%s node exists\n",
1479 				    path_buf);
1480 			di_devfs_path_free(path_buf);
1481 			continue;
1482 		}
1483 
1484 #define	IS_MC(x)	(strcmp(x, PICL_CLASS_MEMORY_CONTROLLER) == 0 ? 1 : 0)
1485 
1486 		if (construct_devtype_node(nodeh, nodename, nodeclass, cnode,
1487 		    &chdh) == PICL_SUCCESS) {
1488 			if (picldevtree_debug)
1489 				syslog(LOG_INFO,
1490 				    "picldevtree: added node:%s path:%s\n",
1491 				    nodename, path_buf);
1492 			if (IS_MC(nodeclass)) {
1493 				if (post_mc_event(PICLEVENT_MC_ADDED, chdh) !=
1494 				    PICL_SUCCESS)
1495 					syslog(LOG_WARNING, PICL_EVENT_DROPPED,
1496 					    PICLEVENT_MC_ADDED);
1497 			}
1498 
1499 			di_devfs_path_free(path_buf);
1500 			(void) update_subtree(chdh, cnode);
1501 		}
1502 	}
1503 
1504 	return (PICL_SUCCESS);
1505 
1506 }
1507 
1508 /*
1509  * This function processes the data from libdevinfo and creates nodes
1510  * in the PICL tree.
1511  */
1512 static int
1513 libdevinfo_init(picl_nodehdl_t rooth)
1514 {
1515 	di_node_t	di_root;
1516 	picl_nodehdl_t	plafh;
1517 	picl_nodehdl_t	obph;
1518 	int		err;
1519 
1520 
1521 	if ((di_root = di_init("/", DINFOCPYALL)) == DI_NODE_NIL)
1522 		return (PICL_FAILURE);
1523 
1524 	if ((ph = di_prom_init()) == NULL)
1525 		return (PICL_FAILURE);
1526 	/*
1527 	 * create platform PICL node using di_root node
1528 	 */
1529 	err = construct_picl_platform(rooth, di_root, &plafh);
1530 	if (err != PICL_SUCCESS) {
1531 		di_fini(di_root);
1532 		return (PICL_FAILURE);
1533 	}
1534 
1535 	err = construct_picl_openprom(rooth, &obph);
1536 	if (err != PICL_SUCCESS) {
1537 		di_fini(di_root);
1538 		return (PICL_FAILURE);
1539 	}
1540 
1541 	(void) construct_devinfo_tree(plafh, obph, di_root, NULL);
1542 	if (ph) {
1543 		di_prom_fini(ph);
1544 		ph = NULL;
1545 	}
1546 	di_fini(di_root);
1547 	return (err);
1548 }
1549 
1550 /*
1551  * This function returns the integer property value
1552  */
1553 static int
1554 get_int_propval_by_name(picl_nodehdl_t	nodeh, char *pname, int *ival)
1555 {
1556 	int	err;
1557 
1558 	err = ptree_get_propval_by_name(nodeh, pname, ival,
1559 	    sizeof (int));
1560 
1561 	return (err);
1562 }
1563 
1564 /*
1565  * This function returns the port ID (or CPU ID in the case of CMP cores)
1566  * of the specific CPU node handle.  If upa_portid exists, return its value.
1567  * Otherwise, return portid/cpuid.
1568  */
1569 static int
1570 get_cpu_portid(picl_nodehdl_t modh, int *id)
1571 {
1572 	int	err;
1573 
1574 	if (strcmp(mach_name, "sun4u") == 0) {
1575 		err = get_int_propval_by_name(modh, OBP_PROP_UPA_PORTID, id);
1576 		if (err == PICL_SUCCESS)
1577 			return (err);
1578 		err = get_int_propval_by_name(modh, OBP_PROP_PORTID, id);
1579 		if (err == PICL_SUCCESS)
1580 			return (err);
1581 		return (get_int_propval_by_name(modh, OBP_PROP_CPUID, id));
1582 	}
1583 	if (strcmp(mach_name, "i86pc") == 0)
1584 		return (get_int_propval_by_name(modh, PICL_PROP_INSTANCE, id));
1585 
1586 	return (PICL_FAILURE);
1587 }
1588 
1589 /*
1590  * This function is the volatile read access function of CPU state
1591  * property
1592  */
1593 static int
1594 get_pi_state(ptree_rarg_t *rarg, void *vbuf)
1595 {
1596 	int	id;
1597 	int	err;
1598 
1599 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
1600 	if (err != PICL_SUCCESS)
1601 		return (err);
1602 
1603 	switch (p_online(id, P_STATUS)) {
1604 	case P_ONLINE:
1605 		(void) strlcpy(vbuf, PS_ONLINE, MAX_STATE_SIZE);
1606 		break;
1607 	case P_OFFLINE:
1608 		(void) strlcpy(vbuf, PS_OFFLINE, MAX_STATE_SIZE);
1609 		break;
1610 	case P_NOINTR:
1611 		(void) strlcpy(vbuf, PS_NOINTR, MAX_STATE_SIZE);
1612 		break;
1613 	case P_SPARE:
1614 		(void) strlcpy(vbuf, PS_SPARE, MAX_STATE_SIZE);
1615 		break;
1616 	case P_FAULTED:
1617 		(void) strlcpy(vbuf, PS_FAULTED, MAX_STATE_SIZE);
1618 		break;
1619 	case P_POWEROFF:
1620 		(void) strlcpy(vbuf, PS_POWEROFF, MAX_STATE_SIZE);
1621 		break;
1622 	default:
1623 		(void) strlcpy(vbuf, "unknown", MAX_STATE_SIZE);
1624 		break;
1625 	}
1626 	return (PICL_SUCCESS);
1627 }
1628 
1629 /*
1630  * This function is the volatile read access function of CPU processor_type
1631  * property
1632  */
1633 static int
1634 get_processor_type(ptree_rarg_t *rarg, void *vbuf)
1635 {
1636 	processor_info_t	cpu_info;
1637 	int	id;
1638 	int	err;
1639 
1640 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
1641 	if (err != PICL_SUCCESS)
1642 		return (err);
1643 
1644 	if (processor_info(id, &cpu_info) >= 0) {
1645 		(void) strlcpy(vbuf, cpu_info.pi_processor_type, PI_TYPELEN);
1646 	}
1647 	return (PICL_SUCCESS);
1648 }
1649 
1650 /*
1651  * This function is the volatile read access function of CPU fputypes
1652  * property
1653  */
1654 static int
1655 get_fputypes(ptree_rarg_t *rarg, void *vbuf)
1656 {
1657 	processor_info_t	cpu_info;
1658 	int	id;
1659 	int	err;
1660 
1661 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
1662 	if (err != PICL_SUCCESS)
1663 		return (err);
1664 
1665 	if (processor_info(id, &cpu_info) >= 0) {
1666 		(void) strlcpy(vbuf, cpu_info.pi_fputypes, PI_FPUTYPE);
1667 	}
1668 	return (PICL_SUCCESS);
1669 }
1670 
1671 /*
1672  * This function is the volatile read access function of CPU StateBegin
1673  * property. To minimize overhead, use kstat_chain_update() to refresh
1674  * the kstat header info as opposed to invoking kstat_open() every time.
1675  */
1676 static int
1677 get_pi_state_begin(ptree_rarg_t *rarg, void *vbuf)
1678 {
1679 	int 			err;
1680 	int			cpu_id;
1681 	static kstat_ctl_t	*kc = NULL;
1682 	static pthread_mutex_t	kc_mutex = PTHREAD_MUTEX_INITIALIZER;
1683 	kstat_t			*kp;
1684 	kstat_named_t		*kn;
1685 
1686 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &cpu_id);
1687 	if (err != PICL_SUCCESS)
1688 		return (err);
1689 
1690 	(void) pthread_mutex_lock(&kc_mutex);
1691 	if (kc == NULL)
1692 		kc = kstat_open();
1693 	else if (kstat_chain_update(kc) == -1) {
1694 		(void) kstat_close(kc);
1695 		kc = kstat_open();
1696 	}
1697 
1698 	if (kc == NULL) {
1699 		(void) pthread_mutex_unlock(&kc_mutex);
1700 		return (PICL_FAILURE);
1701 	}
1702 
1703 	/* Get the state_begin from kstat */
1704 	if ((kp = kstat_lookup(kc, KSTAT_CPU_INFO, cpu_id, NULL)) == NULL ||
1705 	    kp->ks_type != KSTAT_TYPE_NAMED || kstat_read(kc, kp, 0) < 0) {
1706 		(void) pthread_mutex_unlock(&kc_mutex);
1707 		return (PICL_FAILURE);
1708 	}
1709 
1710 	kn = kstat_data_lookup(kp, KSTAT_STATE_BEGIN);
1711 	if (kn) {
1712 		*(uint64_t *)vbuf = (uint64_t)kn->value.l;
1713 		err = PICL_SUCCESS;
1714 	} else
1715 		err = PICL_FAILURE;
1716 
1717 	(void) pthread_mutex_unlock(&kc_mutex);
1718 	return (err);
1719 }
1720 
1721 /*
1722  * This function adds CPU information to the CPU nodes
1723  */
1724 /* ARGSUSED */
1725 static int
1726 add_processor_info(picl_nodehdl_t cpuh, void *args)
1727 {
1728 	int 			err;
1729 	int			cpu_id;
1730 	ptree_propinfo_t	propinfo;
1731 	ptree_propinfo_t	pinfo;
1732 
1733 	err = get_cpu_portid(cpuh, &cpu_id);
1734 	if (err != PICL_SUCCESS)
1735 		return (PICL_WALK_CONTINUE);
1736 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1737 	    PICL_PTYPE_INT, PICL_READ, sizeof (int), PICL_PROP_ID, NULL, NULL);
1738 	err = ptree_create_and_add_prop(cpuh, &propinfo, &cpu_id, NULL);
1739 	if (err != PICL_SUCCESS)
1740 		return (PICL_WALK_CONTINUE);
1741 
1742 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1743 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), MAX_STATE_SIZE,
1744 	    PICL_PROP_STATE, get_pi_state, NULL);
1745 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1746 
1747 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1748 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), PI_TYPELEN,
1749 	    PICL_PROP_PROCESSOR_TYPE, get_processor_type, NULL);
1750 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1751 
1752 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1753 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), PI_FPUTYPE,
1754 	    PICL_PROP_FPUTYPE, get_fputypes, NULL);
1755 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1756 
1757 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1758 	    PICL_PTYPE_TIMESTAMP, PICL_READ|PICL_VOLATILE, sizeof (uint64_t),
1759 	    PICL_PROP_STATE_BEGIN, get_pi_state_begin, NULL);
1760 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1761 
1762 	return (PICL_WALK_CONTINUE);
1763 }
1764 
1765 /*
1766  * This function sets up the "ID" property in every CPU nodes
1767  * and adds processor info
1768  */
1769 static int
1770 setup_cpus(picl_nodehdl_t plafh)
1771 {
1772 	int 			err;
1773 
1774 	err = ptree_walk_tree_by_class(plafh, PICL_CLASS_CPU, NULL,
1775 	    add_processor_info);
1776 
1777 	return (err);
1778 }
1779 
1780 /*
1781  * This function format's the manufacture's information for FFB display
1782  * devices
1783  */
1784 static void
1785 fmt_manf_id(manuf_t manufid, int bufsz, char *outbuf)
1786 {
1787 	/*
1788 	 * Format the manufacturer's info.  Note a small inconsistency we
1789 	 * have to work around - Brooktree has it's part number in decimal,
1790 	 * while Mitsubishi has it's part number in hex.
1791 	 */
1792 	switch (manufid.fld.manf) {
1793 	case MANF_BROOKTREE:
1794 		(void) snprintf(outbuf, bufsz, "%s %d, version %d",
1795 		    "Brooktree", manufid.fld.partno, manufid.fld.version);
1796 		break;
1797 
1798 	case MANF_MITSUBISHI:
1799 		(void) snprintf(outbuf, bufsz, "%s %x, version %d",
1800 		    "Mitsubishi", manufid.fld.partno, manufid.fld.version);
1801 		break;
1802 
1803 	default:
1804 		(void) snprintf(outbuf, bufsz,
1805 		    "JED code %d, Part num 0x%x, version %d",
1806 		    manufid.fld.manf, manufid.fld.partno, manufid.fld.version);
1807 	}
1808 }
1809 
1810 /*
1811  * If it's an ffb device, open ffb devices and return PICL_SUCCESS
1812  */
1813 static int
1814 open_ffb_device(picl_nodehdl_t ffbh, int *fd)
1815 {
1816 	DIR 			*dirp;
1817 	char 			devfs_path[PATH_MAX];
1818 	char 			dev_path[PATH_MAX];
1819 	char 			*devp;
1820 	struct dirent 		*direntp;
1821 	int			err;
1822 	int			tmpfd;
1823 
1824 	/* Get the devfs_path of the ffb devices */
1825 	err = ptree_get_propval_by_name(ffbh, PICL_PROP_DEVFS_PATH, devfs_path,
1826 	    sizeof (devfs_path));
1827 	if (err != PICL_SUCCESS)
1828 		return (err);
1829 
1830 	/* Get the device node name */
1831 	devp = strrchr(devfs_path, '/');
1832 	if (devp == NULL)
1833 		return (PICL_FAILURE);
1834 	*devp = '\0';
1835 	++devp;
1836 
1837 	/*
1838 	 * Check if device node name has the ffb string
1839 	 * If not, assume it's not a ffb device.
1840 	 */
1841 	if (strstr(devp, FFB_NAME) == NULL)
1842 		return (PICL_FAILURE);
1843 
1844 	/*
1845 	 * Get the parent path of the ffb device node.
1846 	 */
1847 	(void) snprintf(dev_path, sizeof (dev_path), "%s/%s", "/devices",
1848 	    devfs_path);
1849 
1850 	/*
1851 	 * Since we don't know ffb's minor nodename,
1852 	 * we need to search all the devices under its
1853 	 * parent dir by comparing the node name
1854 	 */
1855 	if ((dirp = opendir(dev_path)) == NULL)
1856 		return (PICL_FAILURE);
1857 
1858 	while ((direntp = readdir(dirp)) != NULL) {
1859 		if (strstr(direntp->d_name, devp) != NULL) {
1860 			(void) strcat(dev_path, "/");
1861 			(void) strcat(dev_path, direntp->d_name);
1862 			tmpfd = open(dev_path, O_RDWR);
1863 			if (tmpfd < 0)
1864 				continue;
1865 			*fd = tmpfd;
1866 			(void) closedir(dirp);
1867 			return (PICL_SUCCESS);
1868 		}
1869 	}
1870 
1871 	(void) closedir(dirp);
1872 	return (PICL_FAILURE);
1873 }
1874 
1875 /*
1876  * This function recursively searches the tree for ffb display devices
1877  * and add ffb config information
1878  */
1879 static int
1880 add_ffb_config_info(picl_nodehdl_t rooth)
1881 {
1882 	picl_nodehdl_t		nodeh;
1883 	int			err;
1884 	char 			piclclass[PICL_CLASSNAMELEN_MAX];
1885 	char 			manfidbuf[FFB_MANUF_BUFSIZE];
1886 	int 			fd;
1887 	int			board_rev;
1888 	ffb_sys_info_t		fsi;
1889 	ptree_propinfo_t	pinfo;
1890 
1891 	for (err = ptree_get_propval_by_name(rooth, PICL_PROP_CHILD, &nodeh,
1892 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
1893 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER,
1894 		    &nodeh, sizeof (picl_nodehdl_t))) {
1895 
1896 		if (err != PICL_SUCCESS)
1897 			return (err);
1898 
1899 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME,
1900 		    piclclass, PICL_CLASSNAMELEN_MAX);
1901 
1902 		if ((err == PICL_SUCCESS) &&
1903 		    (strcmp(piclclass, PICL_CLASS_DISPLAY) == 0)) {
1904 
1905 			err = open_ffb_device(nodeh, &fd);
1906 			if ((err == PICL_SUCCESS) &&
1907 			    (ioctl(fd, FFB_SYS_INFO, &fsi) >= 0)) {
1908 				(void) ptree_init_propinfo(&pinfo,
1909 				    PTREE_PROPINFO_VERSION,
1910 				    PICL_PTYPE_UNSIGNED_INT, PICL_READ,
1911 				    sizeof (int), PICL_PROP_FFB_BOARD_REV,
1912 				    NULL, NULL);
1913 				board_rev = fsi.ffb_strap_bits.fld.board_rev;
1914 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
1915 				    &board_rev, NULL);
1916 
1917 				fmt_manf_id(fsi.dac_version,
1918 				    sizeof (manfidbuf), manfidbuf);
1919 				(void) ptree_init_propinfo(&pinfo,
1920 				    PTREE_PROPINFO_VERSION,
1921 				    PICL_PTYPE_CHARSTRING, PICL_READ,
1922 				    strlen(manfidbuf) + 1,
1923 				    PICL_PROP_FFB_DAC_VER, NULL, NULL);
1924 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
1925 				    manfidbuf, NULL);
1926 
1927 				fmt_manf_id(fsi.fbram_version,
1928 				    sizeof (manfidbuf), manfidbuf);
1929 				(void) ptree_init_propinfo(&pinfo,
1930 				    PTREE_PROPINFO_VERSION,
1931 				    PICL_PTYPE_CHARSTRING, PICL_READ,
1932 				    strlen(manfidbuf) + 1,
1933 				    PICL_PROP_FFB_FBRAM_VER, NULL,
1934 				    NULL);
1935 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
1936 				    manfidbuf, NULL);
1937 				(void) close(fd);
1938 			}
1939 		} else if (add_ffb_config_info(nodeh) != PICL_SUCCESS)
1940 			return (PICL_FAILURE);
1941 	}
1942 	return (PICL_SUCCESS);
1943 }
1944 
1945 static conf_entries_t *
1946 free_conf_entries(conf_entries_t *list)
1947 {
1948 	conf_entries_t	*el;
1949 	conf_entries_t	*del;
1950 
1951 	if (list == NULL)
1952 		return (NULL);
1953 	el = list;
1954 	while (el != NULL) {
1955 		del = el;
1956 		el = el->next;
1957 		free(del->name);
1958 		free(del->piclclass);
1959 		free(del);
1960 	}
1961 	return (el);
1962 }
1963 
1964 /*
1965  * Reading config order: platform, common
1966  */
1967 static conf_entries_t *
1968 read_conf_file(char *fname, conf_entries_t *list)
1969 {
1970 	FILE		*fp;
1971 	char		lbuf[CONFFILE_LINELEN_MAX];
1972 	char		*nametok;
1973 	char		*classtok;
1974 	conf_entries_t	*el;
1975 	conf_entries_t	*ptr;
1976 
1977 	if (fname == NULL)
1978 		return (list);
1979 
1980 	fp = fopen(fname, "r");
1981 
1982 	if (fp == NULL)
1983 		return (list);
1984 
1985 	while (fgets(lbuf, CONFFILE_LINELEN_MAX, fp) != NULL) {
1986 		if ((lbuf[0] == CONFFILE_COMMENT_CHAR) || (lbuf[0] == '\n'))
1987 			continue;
1988 
1989 		nametok = strtok(lbuf, " \t\n");
1990 		if (nametok == NULL)
1991 			continue;
1992 
1993 		classtok = strtok(NULL, " \t\n");
1994 		if (classtok == NULL)
1995 			continue;
1996 
1997 		el = malloc(sizeof (conf_entries_t));
1998 		if (el == NULL)
1999 			break;
2000 		el->name = strdup(nametok);
2001 		el->piclclass = strdup(classtok);
2002 		if ((el->name == NULL) || (el->piclclass == NULL)) {
2003 			free(el);
2004 			return (list);
2005 		}
2006 		el->next = NULL;
2007 
2008 		/*
2009 		 * Add it to the end of list
2010 		 */
2011 		if (list == NULL)
2012 			list = el;
2013 		else {
2014 			ptr = list;
2015 			while (ptr->next != NULL)
2016 				ptr = ptr->next;
2017 			ptr->next = el;
2018 		}
2019 
2020 	}
2021 	(void) fclose(fp);
2022 	return (list);
2023 }
2024 
2025 /*
2026  * Process the devtree conf file and set up the conf_name_class_map list
2027  */
2028 static void
2029 process_devtree_conf_file(void)
2030 {
2031 	char	nmbuf[SYS_NMLN];
2032 	char	pname[PATH_MAX];
2033 
2034 	conf_name_class_map = NULL;
2035 
2036 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
2037 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2038 		(void) strlcat(pname, DEVTREE_CONFFILE_NAME, PATH_MAX);
2039 		conf_name_class_map = read_conf_file(pname,
2040 		    conf_name_class_map);
2041 	}
2042 
2043 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
2044 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2045 		(void) strlcat(pname, DEVTREE_CONFFILE_NAME, PATH_MAX);
2046 		conf_name_class_map = read_conf_file(pname,
2047 		    conf_name_class_map);
2048 	}
2049 
2050 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
2051 	    DEVTREE_CONFFILE_NAME);
2052 	conf_name_class_map = read_conf_file(pname, conf_name_class_map);
2053 }
2054 
2055 static	asr_conf_entries_t	*conf_name_asr_map = NULL;
2056 
2057 static void
2058 free_asr_conf_entries(asr_conf_entries_t *list) {
2059 	asr_conf_entries_t  *el;
2060 	asr_conf_entries_t  *del;
2061 
2062 	el = list;
2063 	while (el != NULL) {
2064 		del = el;
2065 		el = el->next;
2066 		if (del->name)
2067 			free(del->name);
2068 		if (del->address)
2069 			free(del->address);
2070 		if (del->status)
2071 			free(del->status);
2072 		if (del->piclclass)
2073 			free(del->piclclass);
2074 		if (del->props)
2075 			free(del->props);
2076 		free(del);
2077 	}
2078 }
2079 
2080 /*
2081  * Reading config order: platform, common
2082  */
2083 static asr_conf_entries_t *
2084 read_asr_conf_file(char *fname, asr_conf_entries_t *list)
2085 {
2086 	FILE		*fp;
2087 	char		lbuf[CONFFILE_LINELEN_MAX];
2088 	char		*nametok;
2089 	char		*classtok;
2090 	char		*statustok;
2091 	char		*addresstok;
2092 	char		*propstok;
2093 	asr_conf_entries_t	*el;
2094 	asr_conf_entries_t	*ptr;
2095 
2096 	if (fname == NULL)
2097 		return (list);
2098 
2099 	fp = fopen(fname, "r");
2100 	if (fp == NULL)
2101 		return (list);
2102 
2103 	while (fgets(lbuf, CONFFILE_LINELEN_MAX, fp) != NULL) {
2104 		if ((lbuf[0] == CONFFILE_COMMENT_CHAR) || (lbuf[0] == '\n'))
2105 			continue;
2106 
2107 		nametok = strtok(lbuf, " \t\n");
2108 		if (nametok == NULL)
2109 			continue;
2110 
2111 		classtok = strtok(NULL, " \t\n");
2112 		if (classtok == NULL)
2113 			continue;
2114 
2115 		statustok = strtok(NULL, " \t\n");
2116 		if (statustok == NULL)
2117 			continue;
2118 
2119 		addresstok = strtok(NULL, " \t\n");
2120 		if (addresstok == NULL)
2121 			continue;
2122 
2123 		/*
2124 		 * props are optional
2125 		 */
2126 		propstok = strtok(NULL, " \t\n");
2127 
2128 		el = malloc(sizeof (asr_conf_entries_t));
2129 		if (el == NULL)
2130 			break;
2131 		el->name = strdup(nametok);
2132 		el->piclclass = strdup(classtok);
2133 		el->status = strdup(statustok);
2134 		el->address = strdup(addresstok);
2135 		if (propstok != NULL)
2136 			el->props = strdup(propstok);
2137 		else
2138 			el->props = NULL;
2139 		if ((el->name == NULL) || (el->piclclass == NULL) ||
2140 		    (el->address == NULL) || (el->status == NULL)) {
2141 			if (el->name)
2142 				free(el->name);
2143 			if (el->address)
2144 				free(el->address);
2145 			if (el->status)
2146 				free(el->status);
2147 			if (el->piclclass)
2148 				free(el->piclclass);
2149 			if (el->props)
2150 				free(el->props);
2151 			free(el);
2152 			break;
2153 		}
2154 		el->next = NULL;
2155 
2156 		/*
2157 		 * Add it to the end of list
2158 		 */
2159 		if (list == NULL)
2160 			list = el;
2161 		else {
2162 			ptr = list;
2163 			while (ptr->next != NULL)
2164 				ptr = ptr->next;
2165 			ptr->next = el;
2166 		}
2167 
2168 	}
2169 	(void) fclose(fp);
2170 	return (list);
2171 }
2172 
2173 /*
2174  * Process the asr conf file
2175  */
2176 static void
2177 process_asrtree_conf_file(void)
2178 {
2179 	char	nmbuf[SYS_NMLN];
2180 	char	pname[PATH_MAX];
2181 
2182 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
2183 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2184 		(void) strlcat(pname, ASRTREE_CONFFILE_NAME, PATH_MAX);
2185 		conf_name_asr_map = read_asr_conf_file(pname,
2186 			conf_name_asr_map);
2187 	}
2188 
2189 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
2190 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2191 		(void) strlcat(pname, ASRTREE_CONFFILE_NAME, PATH_MAX);
2192 		conf_name_asr_map = read_asr_conf_file(pname,
2193 			conf_name_asr_map);
2194 	}
2195 
2196 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
2197 	    ASRTREE_CONFFILE_NAME);
2198 	conf_name_asr_map = read_asr_conf_file(pname, conf_name_asr_map);
2199 }
2200 
2201 /*
2202  * This function reads the export file list from ASR
2203  */
2204 static int
2205 get_asr_export_list(char **exportlist, int *exportlistlen)
2206 {
2207 	struct openpromio oppbuf;
2208 	struct openpromio *opp = &oppbuf;
2209 	int d;
2210 	int listsize;
2211 
2212 	d = open("/dev/openprom", O_RDWR);
2213 	if (d < 0)
2214 		return (0);
2215 
2216 	if (ioctl(d, OPROMEXPORTLEN, opp) == -1) {
2217 		(void) close(d);
2218 		return (0);
2219 	}
2220 	listsize = opp->oprom_size;
2221 	opp = (struct openpromio *)malloc(sizeof (struct openpromio) +
2222 	    listsize);
2223 	if (opp == NULL) {
2224 		(void) close(d);
2225 		return (0);
2226 	}
2227 	(void) memset(opp, '\0', sizeof (struct openpromio) + listsize);
2228 	opp->oprom_size = listsize;
2229 	if (ioctl(d, OPROMEXPORT, opp) == -1) {
2230 		free(opp);
2231 		(void) close(d);
2232 		return (0);
2233 	}
2234 	*exportlist = malloc(listsize);
2235 	if (*exportlist == NULL) {
2236 		free(opp);
2237 		(void) close(d);
2238 		return (0);
2239 	}
2240 	(void) memcpy(*exportlist, opp->oprom_array, opp->oprom_size);
2241 	free(opp);
2242 	*exportlistlen = opp->oprom_size;
2243 	(void) close(d);
2244 	return (1);
2245 }
2246 
2247 /*
2248  * Parses properties string, fills in triplet structure with first
2249  * type, name, val triplet and returns pointer to next property.
2250  * Returns NULL if no valid triplet found
2251  * CAUTION: drops \0 characters over separator characters: if you
2252  * want to parse the string twice, you'll have to take a copy.
2253  */
2254 static char *
2255 parse_props_string(char *props, asr_prop_triplet_t *triplet)
2256 {
2257 	char	*prop_name;
2258 	char	*prop_val;
2259 	char	*prop_next;
2260 
2261 	prop_name = strchr(props, '?');
2262 	if (prop_name == NULL)
2263 		return (NULL);
2264 	*prop_name++ = '\0';
2265 	prop_val = strchr(prop_name, '=');
2266 	if (prop_val == NULL)
2267 		return (NULL);
2268 	*prop_val++ = '\0';
2269 	triplet->proptype = props;
2270 	triplet->propname = prop_name;
2271 	triplet->propval = prop_val;
2272 	prop_next = strchr(prop_val, ':');
2273 	if (prop_next == NULL)
2274 		return (prop_val - 1);
2275 	*prop_next++ = '\0';
2276 	return (prop_next);
2277 }
2278 
2279 static int
2280 add_status_prop(picl_nodehdl_t chdh, char *status)
2281 {
2282 	ptree_propinfo_t	propinfo;
2283 	picl_prophdl_t		proph;
2284 	int			err;
2285 
2286 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2287 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(status) + 1,
2288 	    PICL_PROP_STATUS, NULL, NULL);
2289 	if (err != PICL_SUCCESS)
2290 		return (err);
2291 	err = ptree_create_and_add_prop(chdh, &propinfo, status, &proph);
2292 	return (err);
2293 }
2294 
2295 static void
2296 create_asr_node(char *parent, char *child, char *unitaddr, char *class,
2297 	char *status, char *props)
2298 {
2299 	char			ptreepath[PATH_MAX];
2300 	char			nodename[PICL_PROPNAMELEN_MAX];
2301 	char			ua[MAX_UNIT_ADDRESS_LEN];
2302 	char			*props_copy = NULL;
2303 	char			*next;
2304 	char			*prop_string;
2305 	boolean_t		found = B_FALSE;
2306 	picl_nodehdl_t		nodeh;
2307 	picl_nodehdl_t		chdh;
2308 	asr_prop_triplet_t	triple;
2309 	ptree_propinfo_t	propinfo;
2310 	picl_prophdl_t		proph;
2311 	int			val;
2312 	int			err;
2313 
2314 	(void) strlcpy(ptreepath, PLATFORM_PATH, PATH_MAX);
2315 	(void) strlcat(ptreepath, parent, PATH_MAX);
2316 
2317 	if (ptree_get_node_by_path(ptreepath, &nodeh) != PICL_SUCCESS)
2318 		return;
2319 	/*
2320 	 * see if the required child node already exists
2321 	 */
2322 	for (err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
2323 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
2324 	    err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
2325 		    sizeof (picl_nodehdl_t))) {
2326 		if (err != PICL_SUCCESS)
2327 			break;
2328 		err = ptree_get_propval_by_name(chdh, PICL_PROP_NAME,
2329 		    (void *)nodename, PICL_PROPNAMELEN_MAX);
2330 		if (err != PICL_SUCCESS)
2331 			break;
2332 		if (strcmp(nodename, child) != 0)
2333 			continue;
2334 		/*
2335 		 * found a candidate child node
2336 		 */
2337 		if (unitaddr) {
2338 			/*
2339 			 * does it match the required unit address?
2340 			 */
2341 			err = ptree_get_propval_by_name(chdh,
2342 			    PICL_PROP_UNIT_ADDRESS, ua, sizeof (ua));
2343 			if (err == PICL_PROPNOTFOUND)
2344 				continue;
2345 			if (err != PICL_SUCCESS)
2346 				break;
2347 			if (strcmp(unitaddr, ua) != 0)
2348 				continue;
2349 		}
2350 		if (props == NULL) {
2351 			next = "";
2352 		} else if (props_copy == NULL) {
2353 			props_copy = strdup(props);
2354 			if (props_copy == NULL)
2355 				return;
2356 			next = props_copy;
2357 		}
2358 		while ((next = parse_props_string(next, &triple)) != NULL) {
2359 			err = ptree_get_prop_by_name(chdh, triple.propname,
2360 			    &proph);
2361 			if (err != PICL_SUCCESS)
2362 				break;
2363 			err = ptree_get_propinfo(proph, &propinfo);
2364 			if (err != PICL_SUCCESS)
2365 				break;
2366 			err = PICL_FAILURE;
2367 			switch (propinfo.piclinfo.type) {
2368 			case PICL_PTYPE_INT:
2369 			case PICL_PTYPE_UNSIGNED_INT:
2370 				if (strcmp(triple.proptype, "I") != 0)
2371 					break;
2372 				err = ptree_get_propval(proph, (void  *)&val,
2373 				    sizeof (val));
2374 				if (err != PICL_SUCCESS)
2375 					break;
2376 				if (val != atoi(triple.propval))
2377 					err = PICL_FAILURE;
2378 				break;
2379 			case PICL_PTYPE_CHARSTRING:
2380 				if (strcmp(triple.proptype, "S") != 0)
2381 					break;
2382 				prop_string = malloc(propinfo.piclinfo.size);
2383 				if (prop_string == NULL)
2384 					break;
2385 				err = ptree_get_propval(proph,
2386 				    (void *)prop_string,
2387 				    propinfo.piclinfo.size);
2388 				if (err != PICL_SUCCESS) {
2389 					free(prop_string);
2390 					break;
2391 				}
2392 				if (strcmp(prop_string, triple.propval) != 0)
2393 					err = PICL_FAILURE;
2394 				free(prop_string);
2395 				break;
2396 			default:
2397 				break;
2398 			}
2399 			if (err != PICL_SUCCESS) {
2400 				break;
2401 			}
2402 		}
2403 		if (next == NULL) {
2404 			found = B_TRUE;
2405 			break;
2406 		}
2407 	}
2408 	if (props_copy)
2409 		free(props_copy);
2410 	if (found) {
2411 		/*
2412 		 * does the pre-existing node have a status property?
2413 		 */
2414 		err = ptree_get_propval_by_name(chdh, PICL_PROP_STATUS,
2415 		    ua, sizeof (ua));
2416 		if (err == PICL_PROPNOTFOUND)
2417 			(void) add_status_prop(chdh, status);
2418 		if (err != PICL_SUCCESS)
2419 			return;
2420 		if ((strcmp(ua, ASR_DISABLED) == 0) ||
2421 		    (strcmp(ua, ASR_FAILED) == 0) ||
2422 		    ((strcmp(status, ASR_DISABLED) != 0) &&
2423 		    (strcmp(status, ASR_FAILED) != 0))) {
2424 			return;
2425 		}
2426 		/*
2427 		 * more urgent status now, so replace existing value
2428 		 */
2429 		err = ptree_get_prop_by_name(chdh, PICL_PROP_STATUS, &proph);
2430 		if (err != PICL_SUCCESS)
2431 			return;
2432 		(void) ptree_delete_prop(proph);
2433 		(void) ptree_destroy_prop(proph);
2434 		err = add_status_prop(chdh, status);
2435 		if (err != PICL_SUCCESS)
2436 			return;
2437 		return;
2438 	}
2439 
2440 	/*
2441 	 * typical case, node needs adding together with a set of properties
2442 	 */
2443 	if (ptree_create_and_add_node(nodeh, child, class, &chdh) ==
2444 	    PICL_SUCCESS) {
2445 		(void) add_status_prop(chdh, status);
2446 		if (unitaddr) {
2447 			(void) ptree_init_propinfo(&propinfo,
2448 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_CHARSTRING,
2449 			    PICL_READ, strlen(unitaddr) + 1,
2450 			    PICL_PROP_UNIT_ADDRESS, NULL, NULL);
2451 			(void) ptree_create_and_add_prop(chdh, &propinfo,
2452 			    unitaddr, &proph);
2453 			(void) strlcpy(ptreepath, parent, PATH_MAX);
2454 			(void) strlcat(ptreepath, "/", PATH_MAX);
2455 			(void) strlcat(ptreepath, child, PATH_MAX);
2456 			(void) strlcat(ptreepath, "@", PATH_MAX);
2457 			(void) strlcat(ptreepath, unitaddr, PATH_MAX);
2458 			(void) ptree_init_propinfo(&propinfo,
2459 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_CHARSTRING,
2460 			    PICL_READ, strlen(ptreepath) + 1,
2461 			    PICL_PROP_DEVFS_PATH, NULL, NULL);
2462 			(void) ptree_create_and_add_prop(chdh, &propinfo,
2463 			    ptreepath, &proph);
2464 		}
2465 		next = props;
2466 		while ((next = parse_props_string(next, &triple)) != NULL) {
2467 			/*
2468 			 * only handle int and string properties for
2469 			 * simplicity
2470 			 */
2471 			if (strcmp(triple.proptype, "I") == 0) {
2472 				(void) ptree_init_propinfo(&propinfo,
2473 				    PTREE_PROPINFO_VERSION,
2474 				    PICL_PTYPE_INT, PICL_READ,
2475 				    sizeof (int), triple.propname, NULL, NULL);
2476 				val = atoi(triple.propval);
2477 				(void) ptree_create_and_add_prop(chdh,
2478 				    &propinfo, &val, &proph);
2479 			} else {
2480 				(void) ptree_init_propinfo(&propinfo,
2481 				    PTREE_PROPINFO_VERSION,
2482 				    PICL_PTYPE_CHARSTRING, PICL_READ,
2483 				    strlen(triple.propval) + 1,
2484 					triple.propname, NULL, NULL);
2485 				(void) ptree_create_and_add_prop(chdh,
2486 				    &propinfo, triple.propval, &proph);
2487 			}
2488 		}
2489 	}
2490 }
2491 
2492 static void
2493 add_asr_nodes()
2494 {
2495 	char			*asrexport;
2496 	int			asrexportlen;
2497 	asr_conf_entries_t	*c = NULL;
2498 	int			i;
2499 	char			*key;
2500 	char			*child;
2501 	char			*unitaddr;
2502 	uint16_t		count;
2503 	int			disabled;
2504 
2505 	if (get_asr_export_list(&asrexport, &asrexportlen) == 0)
2506 		return;
2507 	process_asrtree_conf_file();
2508 	if (conf_name_asr_map == NULL)
2509 		return;
2510 	i = 0;
2511 	while (i < asrexportlen) {
2512 		key = &asrexport[i];
2513 		i += strlen(key) + 1;
2514 		if (i >= asrexportlen)
2515 			break;
2516 
2517 		/*
2518 		 * next byte tells us whether failed by diags or manually
2519 		 * disabled
2520 		 */
2521 		disabled = asrexport[i];
2522 		i++;
2523 		if (i >= asrexportlen)
2524 			break;
2525 
2526 		/*
2527 		 * only type 1 supported
2528 		 */
2529 		if (asrexport[i] != 1)
2530 			break;
2531 		i++;
2532 		if (i >= asrexportlen)
2533 			break;
2534 
2535 		/*
2536 		 * next two bytes give size of reason string
2537 		 */
2538 		count = (asrexport[i] << 8) | asrexport[i + 1];
2539 		i += count + 2;
2540 		if (i > asrexportlen)
2541 			break;
2542 
2543 		/*
2544 		 * now look for key in conf file info
2545 		 */
2546 		c = conf_name_asr_map;
2547 		while (c != NULL) {
2548 			if (strcmp(key, c->name) == 0) {
2549 				child = strrchr(c->address, '/');
2550 				*child++ = '\0';
2551 				unitaddr = strchr(child, '@');
2552 				if (unitaddr)
2553 					*unitaddr++ = '\0';
2554 				if (strcmp(c->status, ASR_DISABLED) == 0) {
2555 					create_asr_node(c->address, child,
2556 					    unitaddr, c->piclclass, disabled ?
2557 					    ASR_DISABLED : ASR_FAILED,
2558 					    c->props);
2559 				} else {
2560 					create_asr_node(c->address, child,
2561 					    unitaddr, c->piclclass, c->status,
2562 					    c->props);
2563 				}
2564 			}
2565 			c = c->next;
2566 		}
2567 	}
2568 
2569 	free_asr_conf_entries(conf_name_asr_map);
2570 	free(asrexport);
2571 }
2572 
2573 /*
2574  * This function adds information to the /platform node
2575  */
2576 static int
2577 add_platform_info(picl_nodehdl_t plafh)
2578 {
2579 	struct utsname		uts_info;
2580 	int			err;
2581 	ptree_propinfo_t	propinfo;
2582 	picl_prophdl_t		proph;
2583 
2584 	if (uname(&uts_info) < 0)
2585 		return (PICL_FAILURE);
2586 
2587 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2588 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.sysname) + 1,
2589 	    PICL_PROP_SYSNAME, NULL, NULL);
2590 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.sysname,
2591 	    &proph);
2592 	if (err != PICL_SUCCESS)
2593 		return (err);
2594 
2595 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2596 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.nodename) + 1,
2597 	    PICL_PROP_NODENAME, NULL, NULL);
2598 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.nodename,
2599 	    &proph);
2600 	if (err != PICL_SUCCESS)
2601 		return (err);
2602 
2603 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2604 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.release) + 1,
2605 	    PICL_PROP_RELEASE, NULL, NULL);
2606 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.release,
2607 	    &proph);
2608 	if (err != PICL_SUCCESS)
2609 		return (err);
2610 
2611 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2612 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.version) + 1,
2613 	    PICL_PROP_VERSION, NULL, NULL);
2614 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.version,
2615 	    &proph);
2616 	if (err != PICL_SUCCESS)
2617 		return (err);
2618 
2619 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2620 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.machine) + 1,
2621 	    PICL_PROP_MACHINE, NULL, NULL);
2622 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.machine,
2623 	    &proph);
2624 	return (err);
2625 }
2626 
2627 /*
2628  * Get first 32-bit value from the reg property
2629  */
2630 static int
2631 get_first_reg_word(picl_nodehdl_t nodeh, uint32_t *regval)
2632 {
2633 	int			err;
2634 	uint32_t		*regbuf;
2635 	picl_prophdl_t  	regh;
2636 	ptree_propinfo_t	pinfo;
2637 
2638 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
2639 	if (err != PICL_SUCCESS) 	/* no reg property */
2640 		return (err);
2641 	err = ptree_get_propinfo(regh, &pinfo);
2642 	if (err != PICL_SUCCESS)
2643 		return (err);
2644 	if (pinfo.piclinfo.size < sizeof (uint32_t)) /* too small */
2645 		return (PICL_FAILURE);
2646 	regbuf = alloca(pinfo.piclinfo.size);
2647 	if (regbuf == NULL)
2648 		return (PICL_FAILURE);
2649 	err = ptree_get_propval(regh, regbuf, pinfo.piclinfo.size);
2650 	if (err != PICL_SUCCESS)
2651 		return (err);
2652 	*regval = *regbuf;	/* get first 32-bit value */
2653 	return (PICL_SUCCESS);
2654 }
2655 
2656 /*
2657  * Get device ID from the reg property
2658  */
2659 static int
2660 get_device_id(picl_nodehdl_t nodeh, uint32_t *dev_id)
2661 {
2662 	int			err;
2663 	uint32_t		regval;
2664 
2665 	err = get_first_reg_word(nodeh, &regval);
2666 	if (err != PICL_SUCCESS)
2667 		return (err);
2668 
2669 	*dev_id = PCI_DEVICE_ID(regval);
2670 	return (PICL_SUCCESS);
2671 }
2672 
2673 /*
2674  * add Slot property for children of SBUS node
2675  */
2676 /* ARGSUSED */
2677 static int
2678 add_sbus_slots(picl_nodehdl_t pcih, void *args)
2679 {
2680 	picl_nodehdl_t		nodeh;
2681 	uint32_t		slot;
2682 	int			err;
2683 	ptree_propinfo_t	pinfo;
2684 
2685 	for (err = ptree_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
2686 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
2687 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
2688 		    sizeof (picl_nodehdl_t))) {
2689 		if (err != PICL_SUCCESS)
2690 			return (err);
2691 
2692 		if (get_first_reg_word(nodeh, &slot) != 0)
2693 			continue;
2694 		(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
2695 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (uint32_t),
2696 		    PICL_PROP_SLOT, NULL, NULL);
2697 		(void) ptree_create_and_add_prop(nodeh, &pinfo, &slot, NULL);
2698 	}
2699 
2700 	return (PICL_WALK_CONTINUE);
2701 }
2702 
2703 /*
2704  * This function creates a Slot property for SBUS child nodes
2705  * which can be correlated with the slot they are plugged into
2706  * on the motherboard.
2707  */
2708 static int
2709 set_sbus_slot(picl_nodehdl_t plafh)
2710 {
2711 	int		err;
2712 
2713 	err = ptree_walk_tree_by_class(plafh, PICL_CLASS_SBUS, NULL,
2714 	    add_sbus_slots);
2715 
2716 	return (err);
2717 }
2718 
2719 /*
2720  * add DeviceID property for children of PCI/PCIEX node
2721  */
2722 /* ARGSUSED */
2723 static int
2724 add_pci_deviceids(picl_nodehdl_t pcih, void *args)
2725 {
2726 	picl_nodehdl_t		nodeh;
2727 	uint32_t		dev_id;
2728 	int			err;
2729 	ptree_propinfo_t	pinfo;
2730 
2731 	for (err = ptree_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
2732 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
2733 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
2734 		    sizeof (picl_nodehdl_t))) {
2735 		if (err != PICL_SUCCESS)
2736 			return (err);
2737 
2738 		if (get_device_id(nodeh, &dev_id) != 0)
2739 			continue;
2740 		(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
2741 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (uint32_t),
2742 		    PICL_PROP_DEVICE_ID, NULL, NULL);
2743 		(void) ptree_create_and_add_prop(nodeh, &pinfo, &dev_id, NULL);
2744 	}
2745 
2746 	return (PICL_WALK_CONTINUE);
2747 }
2748 
2749 /*
2750  * This function creates a DeviceID property for PCI/PCIEX child nodes
2751  * which can be correlated with the slot they are plugged into
2752  * on the motherboard.
2753  */
2754 static void
2755 set_pci_pciex_deviceid(picl_nodehdl_t plafh)
2756 {
2757 	(void) ptree_walk_tree_by_class(plafh, PICL_CLASS_PCI, NULL,
2758 	    add_pci_deviceids);
2759 
2760 	(void) ptree_walk_tree_by_class(plafh, PICL_CLASS_PCIEX, NULL,
2761 	    add_pci_deviceids);
2762 }
2763 
2764 /*
2765  * Default UnitAddress encode function
2766  */
2767 static int
2768 encode_default_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2769 {
2770 	int	i, len;
2771 
2772 	/*
2773 	 * Encode UnitAddress as %a,%b,%c,...,%n
2774 	 */
2775 	if (addrcells < 1)
2776 		return (-1);
2777 
2778 	len = snprintf(buf, sz, "%x", *regprop);
2779 	for (i = 1; i < addrcells && len < sz; i++)
2780 		len += snprintf(&buf[len], sz-len, ",%x", regprop[i]);
2781 
2782 	return ((len >= sz) ? -1 : 0);
2783 }
2784 
2785 /*
2786  * UnitAddress encode function where the last component is not printed
2787  * unless non-zero.
2788  */
2789 static int
2790 encode_optional_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2791 {
2792 	int	retval;
2793 
2794 	/*
2795 	 * Encode UnitAddress as %a,%b,%c,...,%n where the last component
2796 	 * is printed only if non-zero.
2797 	 */
2798 	if (addrcells > 1 && regprop[addrcells-1] == 0)
2799 		retval = encode_default_unitaddr(buf, sz, regprop, addrcells-1);
2800 	else
2801 		retval = encode_default_unitaddr(buf, sz, regprop, addrcells);
2802 
2803 	return (retval);
2804 }
2805 
2806 
2807 /*
2808  * UnitAddress encode function for SCSI class of devices
2809  */
2810 static int
2811 encode_scsi_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2812 {
2813 	int	len, retval;
2814 
2815 	/*
2816 	 * #address-cells	Format
2817 	 *	2		second component printed only if non-zero
2818 	 *
2819 	 *	4		regprop:   phys_hi phys_lo lun_hi lun_lo
2820 	 *			UnitAddr:  w<phys_hi><phys_lo>,<lun_lo>
2821 	 */
2822 
2823 	if (addrcells == 2) {
2824 		retval = encode_optional_unitaddr(buf, sz, regprop, addrcells);
2825 	} else if (addrcells == 4) {
2826 		len = snprintf(buf, sz, "w%08x%08x,%x", regprop[0], regprop[1],
2827 		    regprop[3]);
2828 		retval = (len >= sz) ? -1 : 0;
2829 	} else
2830 		retval = -1;
2831 
2832 	return (retval);
2833 }
2834 
2835 /*
2836  * UnitAddress encode function for UPA devices
2837  */
2838 static int
2839 encode_upa_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2840 {
2841 	int	len;
2842 
2843 	if (addrcells != 2)
2844 		return (-1);
2845 
2846 	len = snprintf(buf, sz, "%x,%x", (regprop[0]/2)&0x1f, regprop[1]);
2847 	return ((len >= sz) ? -1 : 0);
2848 }
2849 
2850 /*
2851  * UnitAddress encode function for GPTWO, JBUS devices
2852  */
2853 static int
2854 encode_gptwo_jbus_unitaddr(char *buf, int sz, uint32_t *regprop,
2855     uint_t addrcells)
2856 {
2857 	uint32_t	hi, lo;
2858 	int		len, id, off;
2859 
2860 	if (addrcells != 2)
2861 		return (-1);
2862 
2863 	hi = regprop[0];
2864 	lo = regprop[1];
2865 
2866 	if (hi & 0x400) {
2867 		id = ((hi & 0x1) << 9) | (lo >> 23);	/* agent id */
2868 		off = lo & 0x7fffff;			/* config offset */
2869 		len = snprintf(buf, sz, "%x,%x", id, off);
2870 	} else {
2871 		len = snprintf(buf, sz, "m%x,%x", hi, lo);
2872 	}
2873 	return ((len >= sz) ? -1 : 0);
2874 }
2875 
2876 /*
2877  * UnitAddress encode function for PCI devices
2878  */
2879 static int
2880 encode_pci_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2881 {
2882 	typedef struct {
2883 		uint32_t	n:1,		/* relocatable */
2884 				p:1,		/* prefetchable */
2885 				t:1,		/* address region aliases */
2886 				zero:3,		/* must be zero */
2887 				ss:2,		/* address space type */
2888 				bus:8,		/* bus number */
2889 				dev:5,		/* device number */
2890 				fn:3,		/* function number */
2891 				reg:8;		/* register number */
2892 		uint32_t	phys_hi;	/* high physical address */
2893 		uint32_t	phys_lo;	/* low physical address */
2894 	} pci_addrcell_t;
2895 
2896 	pci_addrcell_t	*p;
2897 	int		len;
2898 
2899 	if (addrcells != 3)
2900 		return (-1);
2901 
2902 	p = (pci_addrcell_t *)regprop;
2903 	switch (p->ss) {
2904 	case 0:		/* Config */
2905 		if (p->fn)
2906 			len = snprintf(buf, sz, "%x,%x", p->dev, p->fn);
2907 		else
2908 			len = snprintf(buf, sz, "%x", p->dev);
2909 		break;
2910 	case 1:		/* IO */
2911 		len = snprintf(buf, sz, "i%x,%x,%x,%x", p->dev, p->fn, p->reg,
2912 		    p->phys_lo);
2913 		break;
2914 	case 2:		/* Mem32 */
2915 		len = snprintf(buf, sz, "m%x,%x,%x,%x", p->dev, p->fn, p->reg,
2916 		    p->phys_lo);
2917 		break;
2918 	case 3:		/* Mem64 */
2919 		len = snprintf(buf, sz, "x%x,%x,%x,%x%08x", p->dev, p->fn,
2920 		    p->reg, p->phys_hi, p->phys_lo);
2921 		break;
2922 	}
2923 	return ((len >= sz) ? -1 : 0);
2924 }
2925 
2926 /*
2927  * Get #address-cells property value
2928  */
2929 static uint_t
2930 get_addrcells_prop(picl_nodehdl_t nodeh)
2931 {
2932 	int			len, err;
2933 	uint32_t		addrcells;
2934 	ptree_propinfo_t	pinfo;
2935 	picl_prophdl_t		proph;
2936 
2937 	/*
2938 	 * Get #address-cells property.  If not present, use default value.
2939 	 */
2940 	err = ptree_get_prop_by_name(nodeh, OBP_PROP_ADDRESS_CELLS, &proph);
2941 	if (err == PICL_SUCCESS)
2942 		err = ptree_get_propinfo(proph, &pinfo);
2943 
2944 	len = pinfo.piclinfo.size;
2945 	if (err == PICL_SUCCESS && len >= sizeof (uint8_t) &&
2946 	    len <= sizeof (addrcells)) {
2947 		err = ptree_get_propval(proph, &addrcells, len);
2948 		if (err == PICL_SUCCESS) {
2949 			if (len == sizeof (uint8_t))
2950 				addrcells = *(uint8_t *)&addrcells;
2951 			else if (len == sizeof (uint16_t))
2952 				addrcells = *(uint16_t *)&addrcells;
2953 		} else
2954 			addrcells = DEFAULT_ADDRESS_CELLS;
2955 	} else
2956 		addrcells = DEFAULT_ADDRESS_CELLS;
2957 
2958 	return (addrcells);
2959 }
2960 
2961 /*
2962  * Get UnitAddress mapping entry for a node
2963  */
2964 static unitaddr_map_t *
2965 get_unitaddr_mapping(picl_nodehdl_t nodeh)
2966 {
2967 	int		err;
2968 	unitaddr_map_t	*uamap;
2969 	char		clname[PICL_CLASSNAMELEN_MAX];
2970 
2971 	/*
2972 	 * Get my classname and locate a function to translate "reg" prop
2973 	 * into "UnitAddress" prop for my children.
2974 	 */
2975 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME, clname,
2976 	    sizeof (clname));
2977 	if (err != PICL_SUCCESS)
2978 		(void) strcpy(clname, "");	/* NULL class name */
2979 
2980 	for (uamap = &unitaddr_map_table[0]; uamap->class != NULL; uamap++)
2981 		if (strcmp(clname, uamap->class) == 0)
2982 			break;
2983 
2984 	return (uamap);
2985 }
2986 
2987 /*
2988  * Add UnitAddress property to the specified node
2989  */
2990 static int
2991 add_unitaddr_prop(picl_nodehdl_t nodeh, unitaddr_map_t *uamap, uint_t addrcells)
2992 {
2993 	int			regproplen, err;
2994 	uint32_t		*regbuf;
2995 	picl_prophdl_t		regh;
2996 	ptree_propinfo_t	pinfo;
2997 	char			unitaddr[MAX_UNIT_ADDRESS_LEN];
2998 
2999 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
3000 	if (err != PICL_SUCCESS)
3001 		return (err);
3002 
3003 	err = ptree_get_propinfo(regh, &pinfo);
3004 	if (err != PICL_SUCCESS)
3005 		return (PICL_FAILURE);
3006 
3007 	if (pinfo.piclinfo.size < (addrcells * sizeof (uint32_t)))
3008 		return (PICL_FAILURE);
3009 
3010 	regproplen = pinfo.piclinfo.size;
3011 	regbuf = alloca(regproplen);
3012 	if (regbuf == NULL)
3013 		return (PICL_FAILURE);
3014 
3015 	err = ptree_get_propval(regh, regbuf, regproplen);
3016 	if (err != PICL_SUCCESS || uamap->func == NULL ||
3017 	    (uamap->addrcellcnt && uamap->addrcellcnt != addrcells) ||
3018 	    (uamap->func)(unitaddr, sizeof (unitaddr), regbuf,
3019 	    addrcells) != 0) {
3020 		return (PICL_FAILURE);
3021 	}
3022 
3023 	err = ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
3024 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(unitaddr)+1,
3025 	    PICL_PROP_UNIT_ADDRESS, NULL, NULL);
3026 	if (err == PICL_SUCCESS)
3027 		err = ptree_create_and_add_prop(nodeh, &pinfo, unitaddr, NULL);
3028 
3029 	return (err);
3030 }
3031 
3032 /*
3033  * work out UnitAddress property of the specified node
3034  */
3035 static int
3036 get_unitaddr(picl_nodehdl_t parh, picl_nodehdl_t nodeh, char *unitaddr,
3037     size_t ualen)
3038 {
3039 	int			regproplen, err;
3040 	uint32_t		*regbuf;
3041 	picl_prophdl_t		regh;
3042 	ptree_propinfo_t	pinfo;
3043 	unitaddr_map_t		*uamap;
3044 	uint32_t		addrcells;
3045 
3046 	addrcells = get_addrcells_prop(parh);
3047 	uamap = get_unitaddr_mapping(parh);
3048 
3049 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
3050 	if (err != PICL_SUCCESS)
3051 		return (err);
3052 
3053 	err = ptree_get_propinfo(regh, &pinfo);
3054 	if (err != PICL_SUCCESS)
3055 		return (err);
3056 
3057 	if (pinfo.piclinfo.size < (addrcells * sizeof (uint32_t)))
3058 		return (PICL_FAILURE);
3059 
3060 	regproplen = pinfo.piclinfo.size;
3061 	regbuf = alloca(regproplen);
3062 	if (regbuf == NULL)
3063 		return (PICL_FAILURE);
3064 
3065 	err = ptree_get_propval(regh, regbuf, regproplen);
3066 	if (err != PICL_SUCCESS || uamap->func == NULL ||
3067 	    (uamap->addrcellcnt && uamap->addrcellcnt != addrcells) ||
3068 	    (uamap->func)(unitaddr, ualen, regbuf, addrcells) != 0) {
3069 		return (PICL_FAILURE);
3070 	}
3071 	return (PICL_SUCCESS);
3072 }
3073 
3074 /*
3075  * Add UnitAddress property to all children of the specified node
3076  */
3077 static int
3078 add_unitaddr_prop_to_subtree(picl_nodehdl_t nodeh)
3079 {
3080 	int			err;
3081 	picl_nodehdl_t		chdh;
3082 	unitaddr_map_t		*uamap;
3083 	uint32_t		addrcells;
3084 
3085 	/*
3086 	 * Get #address-cells and unit address mapping entry for my
3087 	 * node's class
3088 	 */
3089 	addrcells = get_addrcells_prop(nodeh);
3090 	uamap = get_unitaddr_mapping(nodeh);
3091 
3092 	/*
3093 	 * Add UnitAddress property to my children and their subtree
3094 	 */
3095 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
3096 	    sizeof (picl_nodehdl_t));
3097 
3098 	while (err == PICL_SUCCESS) {
3099 		(void) add_unitaddr_prop(chdh, uamap, addrcells);
3100 		(void) add_unitaddr_prop_to_subtree(chdh);
3101 
3102 		err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
3103 		    sizeof (picl_nodehdl_t));
3104 	}
3105 
3106 	return (PICL_SUCCESS);
3107 }
3108 
3109 static int
3110 update_memory_size_prop(picl_nodehdl_t plafh)
3111 {
3112 	picl_nodehdl_t		memh;
3113 	picl_prophdl_t		proph;
3114 	ptree_propinfo_t	pinfo;
3115 	int			err, nspecs, snum, pval;
3116 	char			*regbuf;
3117 	memspecs_t		*mspecs;
3118 	uint64_t		memsize;
3119 
3120 	/*
3121 	 * check if the #size-cells of the platform node is 2
3122 	 */
3123 	err = ptree_get_propval_by_name(plafh, OBP_PROP_SIZE_CELLS, &pval,
3124 		sizeof (pval));
3125 
3126 	if (err == PICL_PROPNOTFOUND)
3127 		pval = SUPPORTED_NUM_CELL_SIZE;
3128 	else if (err != PICL_SUCCESS)
3129 		return (err);
3130 
3131 	/*
3132 	 * don't know how to handle other vals
3133 	 */
3134 	if (pval != SUPPORTED_NUM_CELL_SIZE)
3135 		return (PICL_FAILURE);
3136 
3137 	err = ptree_get_node_by_path(MEMORY_PATH, &memh);
3138 	if (err != PICL_SUCCESS)
3139 		return (err);
3140 
3141 	/*
3142 	 * Get the REG property to calculate the size of memory
3143 	 */
3144 	err = ptree_get_prop_by_name(memh, OBP_REG, &proph);
3145 	if (err != PICL_SUCCESS)
3146 		return (err);
3147 
3148 	err = ptree_get_propinfo(proph, &pinfo);
3149 	if (err != PICL_SUCCESS)
3150 		return (err);
3151 
3152 	regbuf = alloca(pinfo.piclinfo.size);
3153 	if (regbuf == NULL)
3154 		return (PICL_FAILURE);
3155 
3156 	err = ptree_get_propval(proph, regbuf, pinfo.piclinfo.size);
3157 	if (err != PICL_SUCCESS)
3158 		return (err);
3159 
3160 	mspecs = (memspecs_t *)regbuf;
3161 	nspecs = pinfo.piclinfo.size / sizeof (memspecs_t);
3162 
3163 	memsize = 0;
3164 	for (snum = 0; snum < nspecs; ++snum)
3165 		memsize += mspecs[snum].size;
3166 
3167 	err = ptree_get_prop_by_name(memh, PICL_PROP_SIZE, &proph);
3168 	if (err == PICL_SUCCESS) {
3169 		err = ptree_update_propval(proph, &memsize, sizeof (memsize));
3170 		return (err);
3171 	}
3172 
3173 	/*
3174 	 * Add the size property
3175 	 */
3176 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
3177 		PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (memsize),
3178 		PICL_PROP_SIZE, NULL, NULL);
3179 	err = ptree_create_and_add_prop(memh, &pinfo, &memsize, NULL);
3180 	return (err);
3181 }
3182 
3183 /*
3184  * This function is executed as part of .init when the plugin is
3185  * dlopen()ed
3186  */
3187 static void
3188 picldevtree_register(void)
3189 {
3190 	if (getenv(SUNW_PICLDEVTREE_PLUGIN_DEBUG))
3191 		picldevtree_debug = 1;
3192 	(void) picld_plugin_register(&my_reg_info);
3193 }
3194 
3195 /*
3196  * This function is the init entry point of the plugin.
3197  * It initializes the /platform tree based on libdevinfo
3198  */
3199 static void
3200 picldevtree_init(void)
3201 {
3202 	picl_nodehdl_t	rhdl;
3203 	int		err;
3204 	struct utsname	utsname;
3205 	picl_nodehdl_t	plafh;
3206 
3207 	if (uname(&utsname) < 0)
3208 		return;
3209 
3210 	(void) strcpy(mach_name, utsname.machine);
3211 
3212 	if (strcmp(mach_name, "sun4u") == 0) {
3213 		builtin_map_ptr = sun4u_map;
3214 		builtin_map_size = sizeof (sun4u_map) / sizeof (builtin_map_t);
3215 	} else if (strcmp(mach_name, "i86pc") == 0) {
3216 		builtin_map_ptr = i86pc_map;
3217 		builtin_map_size = sizeof (i86pc_map) / sizeof (builtin_map_t);
3218 	} else {
3219 		builtin_map_ptr = NULL;
3220 		builtin_map_size = 0;
3221 	}
3222 
3223 	err = ptree_get_root(&rhdl);
3224 	if (err != PICL_SUCCESS) {
3225 		syslog(LOG_ERR, DEVINFO_PLUGIN_INIT_FAILED);
3226 		return;
3227 	}
3228 
3229 	process_devtree_conf_file();
3230 
3231 	if (libdevinfo_init(rhdl) != PICL_SUCCESS) {
3232 		syslog(LOG_ERR, DEVINFO_PLUGIN_INIT_FAILED);
3233 		return;
3234 	}
3235 
3236 	err = ptree_get_node_by_path(PLATFORM_PATH, &plafh);
3237 	if (err != PICL_SUCCESS)
3238 		return;
3239 
3240 	(void) add_unitaddr_prop_to_subtree(plafh);
3241 
3242 	add_asr_nodes();
3243 
3244 	(void) update_memory_size_prop(plafh);
3245 
3246 	(void) setup_cpus(plafh);
3247 
3248 	(void) add_ffb_config_info(plafh);
3249 
3250 	(void) add_platform_info(plafh);
3251 
3252 	set_pci_pciex_deviceid(plafh);
3253 
3254 	(void) set_sbus_slot(plafh);
3255 
3256 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
3257 	    picldevtree_evhandler, NULL);
3258 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
3259 	    picldevtree_evhandler, NULL);
3260 }
3261 
3262 /*
3263  * This function is the fini entry point of the plugin
3264  */
3265 static void
3266 picldevtree_fini(void)
3267 {
3268 	/* First unregister the event handlers */
3269 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
3270 	    picldevtree_evhandler, NULL);
3271 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
3272 	    picldevtree_evhandler, NULL);
3273 
3274 	conf_name_class_map = free_conf_entries(conf_name_class_map);
3275 }
3276 
3277 /*
3278  * This function is the event handler of this plug-in.
3279  *
3280  * It processes the following events:
3281  *
3282  *	PICLEVENT_SYSEVENT_DEVICE_ADDED
3283  *	PICLEVENT_SYSEVENT_DEVICE_REMOVED
3284  */
3285 /* ARGSUSED */
3286 static void
3287 picldevtree_evhandler(const char *ename, const void *earg, size_t size,
3288     void *cookie)
3289 {
3290 	char			*devfs_path;
3291 	char			ptreepath[PATH_MAX];
3292 	char			dipath[PATH_MAX];
3293 	picl_nodehdl_t		plafh;
3294 	picl_nodehdl_t		nodeh;
3295 	nvlist_t		*nvlp;
3296 
3297 	if (earg == NULL)
3298 		return;
3299 
3300 	nvlp = NULL;
3301 	if (ptree_get_node_by_path(PLATFORM_PATH, &plafh) != PICL_SUCCESS ||
3302 	    nvlist_unpack((char *)earg, size, &nvlp, NULL) ||
3303 	    nvlist_lookup_string(nvlp, PICLEVENTARG_DEVFS_PATH, &devfs_path) ||
3304 	    strlen(devfs_path) > (PATH_MAX - sizeof (PLATFORM_PATH))) {
3305 		syslog(LOG_INFO, PICL_EVENT_DROPPED, ename);
3306 		if (nvlp)
3307 			nvlist_free(nvlp);
3308 		return;
3309 	}
3310 
3311 	(void) strlcpy(ptreepath, PLATFORM_PATH, PATH_MAX);
3312 	(void) strlcat(ptreepath, devfs_path, PATH_MAX);
3313 	(void) strlcpy(dipath, devfs_path, PATH_MAX);
3314 	nvlist_free(nvlp);
3315 
3316 	if (picldevtree_debug)
3317 		syslog(LOG_INFO, "picldevtree: event handler invoked ename:%s "
3318 		    "ptreepath:%s\n", ename, ptreepath);
3319 
3320 	if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0) {
3321 		di_node_t		devnode;
3322 		char		*strp;
3323 		picl_nodehdl_t	parh;
3324 		char		nodeclass[PICL_CLASSNAMELEN_MAX];
3325 		char		*nodename;
3326 		int		err;
3327 
3328 		/* If the node already exist, then nothing else to do here */
3329 		if (ptree_get_node_by_path(ptreepath, &nodeh) == PICL_SUCCESS)
3330 			return;
3331 
3332 		/* Skip if unable to find parent PICL node handle */
3333 		parh = plafh;
3334 		if (((strp = strrchr(ptreepath, '/')) != NULL) &&
3335 		    (strp != strchr(ptreepath, '/'))) {
3336 			*strp = '\0';
3337 			if (ptree_get_node_by_path(ptreepath, &parh) !=
3338 			    PICL_SUCCESS)
3339 				return;
3340 		}
3341 
3342 		/*
3343 		 * If parent is the root node
3344 		 */
3345 		if (parh == plafh) {
3346 			ph = di_prom_init();
3347 			devnode = di_init(dipath, DINFOCPYALL);
3348 			if (devnode == DI_NODE_NIL) {
3349 				if (ph != NULL) {
3350 					di_prom_fini(ph);
3351 					ph = NULL;
3352 				}
3353 				return;
3354 			}
3355 			nodename = di_node_name(devnode);
3356 			if (nodename == NULL) {
3357 				di_fini(devnode);
3358 				if (ph != NULL) {
3359 					di_prom_fini(ph);
3360 					ph = NULL;
3361 				}
3362 				return;
3363 			}
3364 
3365 			err = get_node_class(nodeclass, devnode, nodename);
3366 			if (err < 0) {
3367 				di_fini(devnode);
3368 				if (ph != NULL) {
3369 					di_prom_fini(ph);
3370 					ph = NULL;
3371 				}
3372 				return;
3373 			}
3374 			err = construct_devtype_node(plafh, nodename,
3375 			    nodeclass, devnode, &nodeh);
3376 			if (err != PICL_SUCCESS) {
3377 				di_fini(devnode);
3378 				if (ph != NULL) {
3379 					di_prom_fini(ph);
3380 					ph = NULL;
3381 				}
3382 				return;
3383 			}
3384 			(void) update_subtree(nodeh, devnode);
3385 			(void) add_unitaddr_prop_to_subtree(nodeh);
3386 			if (ph != NULL) {
3387 				di_prom_fini(ph);
3388 				ph = NULL;
3389 			}
3390 			di_fini(devnode);
3391 			goto done;
3392 		}
3393 
3394 		/* kludge ... try without bus-addr first */
3395 		if ((strp = strrchr(dipath, '@')) != NULL) {
3396 			char *p;
3397 
3398 			p = strrchr(dipath, '/');
3399 			if (p != NULL && strp > p) {
3400 				*strp = '\0';
3401 				devnode = di_init(dipath, DINFOCPYALL);
3402 				if (devnode != DI_NODE_NIL)
3403 					di_fini(devnode);
3404 				*strp = '@';
3405 			}
3406 		}
3407 		/* Get parent devnode */
3408 		if ((strp = strrchr(dipath, '/')) != NULL)
3409 			*++strp = '\0';
3410 		devnode = di_init(dipath, DINFOCPYALL);
3411 		if (devnode == DI_NODE_NIL)
3412 			return;
3413 		ph = di_prom_init();
3414 		(void) update_subtree(parh, devnode);
3415 		(void) add_unitaddr_prop_to_subtree(parh);
3416 		if (ph) {
3417 			di_prom_fini(ph);
3418 			ph = NULL;
3419 		}
3420 		di_fini(devnode);
3421 	} else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0) {
3422 		char			delclass[PICL_CLASSNAMELEN_MAX];
3423 		char		*strp;
3424 
3425 		/*
3426 		 * if final element of path doesn't have a unit address
3427 		 * then it is not uniquely identifiable - cannot remove
3428 		 */
3429 		if (((strp = strrchr(ptreepath, '/')) != NULL) &&
3430 		    strchr(strp, '@') == NULL)
3431 			return;
3432 
3433 		/* skip if can't find the node */
3434 		if (ptree_get_node_by_path(ptreepath, &nodeh) != PICL_SUCCESS)
3435 			return;
3436 
3437 		if (ptree_delete_node(nodeh) != PICL_SUCCESS)
3438 			return;
3439 
3440 		if (picldevtree_debug)
3441 			syslog(LOG_INFO,
3442 			    "picldevtree: deleted node nodeh:%llx\n", nodeh);
3443 		if ((ptree_get_propval_by_name(nodeh,
3444 		    PICL_PROP_CLASSNAME, delclass, PICL_CLASSNAMELEN_MAX) ==
3445 		    PICL_SUCCESS) && IS_MC(delclass)) {
3446 			if (post_mc_event(PICLEVENT_MC_REMOVED, nodeh) !=
3447 			    PICL_SUCCESS)
3448 				syslog(LOG_WARNING, PICL_EVENT_DROPPED,
3449 				    PICLEVENT_MC_REMOVED);
3450 		} else
3451 			(void) ptree_destroy_node(nodeh);
3452 	}
3453 done:
3454 	(void) setup_cpus(plafh);
3455 	(void) add_ffb_config_info(plafh);
3456 	set_pci_pciex_deviceid(plafh);
3457 	(void) set_sbus_slot(plafh);
3458 	if (picldevtree_debug > 1)
3459 		syslog(LOG_INFO, "picldevtree: event handler done\n");
3460 }
3461