xref: /illumos-gate/usr/src/cmd/picl/plugins/common/devtree/picldevtree.c (revision 6a074c93c5dee390d8ca2377f42e55418f0a9eb3)
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 2007 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 	    strcmp(mach_name, "sun4v") == 0) {
1576 		err = get_int_propval_by_name(modh, OBP_PROP_UPA_PORTID, id);
1577 		if (err == PICL_SUCCESS)
1578 			return (err);
1579 		err = get_int_propval_by_name(modh, OBP_PROP_PORTID, id);
1580 		if (err == PICL_SUCCESS)
1581 			return (err);
1582 		return (get_int_propval_by_name(modh, OBP_PROP_CPUID, id));
1583 	}
1584 	if (strcmp(mach_name, "i86pc") == 0)
1585 		return (get_int_propval_by_name(modh, PICL_PROP_INSTANCE, id));
1586 
1587 	return (PICL_FAILURE);
1588 }
1589 
1590 /*
1591  * This function is the volatile read access function of CPU state
1592  * property
1593  */
1594 static int
1595 get_pi_state(ptree_rarg_t *rarg, void *vbuf)
1596 {
1597 	int	id;
1598 	int	err;
1599 
1600 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
1601 	if (err != PICL_SUCCESS)
1602 		return (err);
1603 
1604 	switch (p_online(id, P_STATUS)) {
1605 	case P_ONLINE:
1606 		(void) strlcpy(vbuf, PS_ONLINE, MAX_STATE_SIZE);
1607 		break;
1608 	case P_OFFLINE:
1609 		(void) strlcpy(vbuf, PS_OFFLINE, MAX_STATE_SIZE);
1610 		break;
1611 	case P_NOINTR:
1612 		(void) strlcpy(vbuf, PS_NOINTR, MAX_STATE_SIZE);
1613 		break;
1614 	case P_SPARE:
1615 		(void) strlcpy(vbuf, PS_SPARE, MAX_STATE_SIZE);
1616 		break;
1617 	case P_FAULTED:
1618 		(void) strlcpy(vbuf, PS_FAULTED, MAX_STATE_SIZE);
1619 		break;
1620 	case P_POWEROFF:
1621 		(void) strlcpy(vbuf, PS_POWEROFF, MAX_STATE_SIZE);
1622 		break;
1623 	default:
1624 		(void) strlcpy(vbuf, "unknown", MAX_STATE_SIZE);
1625 		break;
1626 	}
1627 	return (PICL_SUCCESS);
1628 }
1629 
1630 /*
1631  * This function is the volatile read access function of CPU processor_type
1632  * property
1633  */
1634 static int
1635 get_processor_type(ptree_rarg_t *rarg, void *vbuf)
1636 {
1637 	processor_info_t	cpu_info;
1638 	int	id;
1639 	int	err;
1640 
1641 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
1642 	if (err != PICL_SUCCESS)
1643 		return (err);
1644 
1645 	if (processor_info(id, &cpu_info) >= 0) {
1646 		(void) strlcpy(vbuf, cpu_info.pi_processor_type, PI_TYPELEN);
1647 	}
1648 	return (PICL_SUCCESS);
1649 }
1650 
1651 /*
1652  * This function is the volatile read access function of CPU fputypes
1653  * property
1654  */
1655 static int
1656 get_fputypes(ptree_rarg_t *rarg, void *vbuf)
1657 {
1658 	processor_info_t	cpu_info;
1659 	int	id;
1660 	int	err;
1661 
1662 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &id);
1663 	if (err != PICL_SUCCESS)
1664 		return (err);
1665 
1666 	if (processor_info(id, &cpu_info) >= 0) {
1667 		(void) strlcpy(vbuf, cpu_info.pi_fputypes, PI_FPUTYPE);
1668 	}
1669 	return (PICL_SUCCESS);
1670 }
1671 
1672 /*
1673  * This function is the volatile read access function of CPU StateBegin
1674  * property. To minimize overhead, use kstat_chain_update() to refresh
1675  * the kstat header info as opposed to invoking kstat_open() every time.
1676  */
1677 static int
1678 get_pi_state_begin(ptree_rarg_t *rarg, void *vbuf)
1679 {
1680 	int 			err;
1681 	int			cpu_id;
1682 	static kstat_ctl_t	*kc = NULL;
1683 	static pthread_mutex_t	kc_mutex = PTHREAD_MUTEX_INITIALIZER;
1684 	kstat_t			*kp;
1685 	kstat_named_t		*kn;
1686 
1687 	err = get_int_propval_by_name(rarg->nodeh, PICL_PROP_ID, &cpu_id);
1688 	if (err != PICL_SUCCESS)
1689 		return (err);
1690 
1691 	(void) pthread_mutex_lock(&kc_mutex);
1692 	if (kc == NULL)
1693 		kc = kstat_open();
1694 	else if (kstat_chain_update(kc) == -1) {
1695 		(void) kstat_close(kc);
1696 		kc = kstat_open();
1697 	}
1698 
1699 	if (kc == NULL) {
1700 		(void) pthread_mutex_unlock(&kc_mutex);
1701 		return (PICL_FAILURE);
1702 	}
1703 
1704 	/* Get the state_begin from kstat */
1705 	if ((kp = kstat_lookup(kc, KSTAT_CPU_INFO, cpu_id, NULL)) == NULL ||
1706 	    kp->ks_type != KSTAT_TYPE_NAMED || kstat_read(kc, kp, 0) < 0) {
1707 		(void) pthread_mutex_unlock(&kc_mutex);
1708 		return (PICL_FAILURE);
1709 	}
1710 
1711 	kn = kstat_data_lookup(kp, KSTAT_STATE_BEGIN);
1712 	if (kn) {
1713 		*(uint64_t *)vbuf = (uint64_t)kn->value.l;
1714 		err = PICL_SUCCESS;
1715 	} else
1716 		err = PICL_FAILURE;
1717 
1718 	(void) pthread_mutex_unlock(&kc_mutex);
1719 	return (err);
1720 }
1721 
1722 /*
1723  * This function adds CPU information to the CPU nodes
1724  */
1725 /* ARGSUSED */
1726 static int
1727 add_processor_info(picl_nodehdl_t cpuh, void *args)
1728 {
1729 	int 			err;
1730 	int			cpu_id;
1731 	ptree_propinfo_t	propinfo;
1732 	ptree_propinfo_t	pinfo;
1733 
1734 	err = get_cpu_portid(cpuh, &cpu_id);
1735 	if (err != PICL_SUCCESS)
1736 		return (PICL_WALK_CONTINUE);
1737 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
1738 	    PICL_PTYPE_INT, PICL_READ, sizeof (int), PICL_PROP_ID, NULL, NULL);
1739 	err = ptree_create_and_add_prop(cpuh, &propinfo, &cpu_id, NULL);
1740 	if (err != PICL_SUCCESS)
1741 		return (PICL_WALK_CONTINUE);
1742 
1743 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1744 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), MAX_STATE_SIZE,
1745 	    PICL_PROP_STATE, get_pi_state, NULL);
1746 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1747 
1748 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1749 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), PI_TYPELEN,
1750 	    PICL_PROP_PROCESSOR_TYPE, get_processor_type, NULL);
1751 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1752 
1753 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1754 	    PICL_PTYPE_CHARSTRING, (PICL_READ|PICL_VOLATILE), PI_FPUTYPE,
1755 	    PICL_PROP_FPUTYPE, get_fputypes, NULL);
1756 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1757 
1758 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
1759 	    PICL_PTYPE_TIMESTAMP, PICL_READ|PICL_VOLATILE, sizeof (uint64_t),
1760 	    PICL_PROP_STATE_BEGIN, get_pi_state_begin, NULL);
1761 	(void) ptree_create_and_add_prop(cpuh, &pinfo, NULL, NULL);
1762 
1763 	return (PICL_WALK_CONTINUE);
1764 }
1765 
1766 /*
1767  * This function sets up the "ID" property in every CPU nodes
1768  * and adds processor info
1769  */
1770 static int
1771 setup_cpus(picl_nodehdl_t plafh)
1772 {
1773 	int 			err;
1774 
1775 	err = ptree_walk_tree_by_class(plafh, PICL_CLASS_CPU, NULL,
1776 	    add_processor_info);
1777 
1778 	return (err);
1779 }
1780 
1781 /*
1782  * This function format's the manufacture's information for FFB display
1783  * devices
1784  */
1785 static void
1786 fmt_manf_id(manuf_t manufid, int bufsz, char *outbuf)
1787 {
1788 	/*
1789 	 * Format the manufacturer's info.  Note a small inconsistency we
1790 	 * have to work around - Brooktree has it's part number in decimal,
1791 	 * while Mitsubishi has it's part number in hex.
1792 	 */
1793 	switch (manufid.fld.manf) {
1794 	case MANF_BROOKTREE:
1795 		(void) snprintf(outbuf, bufsz, "%s %d, version %d",
1796 		    "Brooktree", manufid.fld.partno, manufid.fld.version);
1797 		break;
1798 
1799 	case MANF_MITSUBISHI:
1800 		(void) snprintf(outbuf, bufsz, "%s %x, version %d",
1801 		    "Mitsubishi", manufid.fld.partno, manufid.fld.version);
1802 		break;
1803 
1804 	default:
1805 		(void) snprintf(outbuf, bufsz,
1806 		    "JED code %d, Part num 0x%x, version %d",
1807 		    manufid.fld.manf, manufid.fld.partno, manufid.fld.version);
1808 	}
1809 }
1810 
1811 /*
1812  * If it's an ffb device, open ffb devices and return PICL_SUCCESS
1813  */
1814 static int
1815 open_ffb_device(picl_nodehdl_t ffbh, int *fd)
1816 {
1817 	DIR 			*dirp;
1818 	char 			devfs_path[PATH_MAX];
1819 	char 			dev_path[PATH_MAX];
1820 	char 			*devp;
1821 	struct dirent 		*direntp;
1822 	int			err;
1823 	int			tmpfd;
1824 
1825 	/* Get the devfs_path of the ffb devices */
1826 	err = ptree_get_propval_by_name(ffbh, PICL_PROP_DEVFS_PATH, devfs_path,
1827 	    sizeof (devfs_path));
1828 	if (err != PICL_SUCCESS)
1829 		return (err);
1830 
1831 	/* Get the device node name */
1832 	devp = strrchr(devfs_path, '/');
1833 	if (devp == NULL)
1834 		return (PICL_FAILURE);
1835 	*devp = '\0';
1836 	++devp;
1837 
1838 	/*
1839 	 * Check if device node name has the ffb string
1840 	 * If not, assume it's not a ffb device.
1841 	 */
1842 	if (strstr(devp, FFB_NAME) == NULL)
1843 		return (PICL_FAILURE);
1844 
1845 	/*
1846 	 * Get the parent path of the ffb device node.
1847 	 */
1848 	(void) snprintf(dev_path, sizeof (dev_path), "%s/%s", "/devices",
1849 	    devfs_path);
1850 
1851 	/*
1852 	 * Since we don't know ffb's minor nodename,
1853 	 * we need to search all the devices under its
1854 	 * parent dir by comparing the node name
1855 	 */
1856 	if ((dirp = opendir(dev_path)) == NULL)
1857 		return (PICL_FAILURE);
1858 
1859 	while ((direntp = readdir(dirp)) != NULL) {
1860 		if (strstr(direntp->d_name, devp) != NULL) {
1861 			(void) strcat(dev_path, "/");
1862 			(void) strcat(dev_path, direntp->d_name);
1863 			tmpfd = open(dev_path, O_RDWR);
1864 			if (tmpfd < 0)
1865 				continue;
1866 			*fd = tmpfd;
1867 			(void) closedir(dirp);
1868 			return (PICL_SUCCESS);
1869 		}
1870 	}
1871 
1872 	(void) closedir(dirp);
1873 	return (PICL_FAILURE);
1874 }
1875 
1876 /*
1877  * This function recursively searches the tree for ffb display devices
1878  * and add ffb config information
1879  */
1880 static int
1881 add_ffb_config_info(picl_nodehdl_t rooth)
1882 {
1883 	picl_nodehdl_t		nodeh;
1884 	int			err;
1885 	char 			piclclass[PICL_CLASSNAMELEN_MAX];
1886 	char 			manfidbuf[FFB_MANUF_BUFSIZE];
1887 	int 			fd;
1888 	int			board_rev;
1889 	ffb_sys_info_t		fsi;
1890 	ptree_propinfo_t	pinfo;
1891 
1892 	for (err = ptree_get_propval_by_name(rooth, PICL_PROP_CHILD, &nodeh,
1893 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
1894 	    err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER,
1895 	    &nodeh, sizeof (picl_nodehdl_t))) {
1896 
1897 		if (err != PICL_SUCCESS)
1898 			return (err);
1899 
1900 		err = ptree_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME,
1901 		    piclclass, PICL_CLASSNAMELEN_MAX);
1902 
1903 		if ((err == PICL_SUCCESS) &&
1904 		    (strcmp(piclclass, PICL_CLASS_DISPLAY) == 0)) {
1905 
1906 			err = open_ffb_device(nodeh, &fd);
1907 			if ((err == PICL_SUCCESS) &&
1908 			    (ioctl(fd, FFB_SYS_INFO, &fsi) >= 0)) {
1909 				(void) ptree_init_propinfo(&pinfo,
1910 				    PTREE_PROPINFO_VERSION,
1911 				    PICL_PTYPE_UNSIGNED_INT, PICL_READ,
1912 				    sizeof (int), PICL_PROP_FFB_BOARD_REV,
1913 				    NULL, NULL);
1914 				board_rev = fsi.ffb_strap_bits.fld.board_rev;
1915 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
1916 				    &board_rev, NULL);
1917 
1918 				fmt_manf_id(fsi.dac_version,
1919 				    sizeof (manfidbuf), manfidbuf);
1920 				(void) ptree_init_propinfo(&pinfo,
1921 				    PTREE_PROPINFO_VERSION,
1922 				    PICL_PTYPE_CHARSTRING, PICL_READ,
1923 				    strlen(manfidbuf) + 1,
1924 				    PICL_PROP_FFB_DAC_VER, NULL, NULL);
1925 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
1926 				    manfidbuf, NULL);
1927 
1928 				fmt_manf_id(fsi.fbram_version,
1929 				    sizeof (manfidbuf), manfidbuf);
1930 				(void) ptree_init_propinfo(&pinfo,
1931 				    PTREE_PROPINFO_VERSION,
1932 				    PICL_PTYPE_CHARSTRING, PICL_READ,
1933 				    strlen(manfidbuf) + 1,
1934 				    PICL_PROP_FFB_FBRAM_VER, NULL,
1935 				    NULL);
1936 				(void) ptree_create_and_add_prop(nodeh, &pinfo,
1937 				    manfidbuf, NULL);
1938 				(void) close(fd);
1939 			}
1940 		} else if (add_ffb_config_info(nodeh) != PICL_SUCCESS)
1941 			return (PICL_FAILURE);
1942 	}
1943 	return (PICL_SUCCESS);
1944 }
1945 
1946 static conf_entries_t *
1947 free_conf_entries(conf_entries_t *list)
1948 {
1949 	conf_entries_t	*el;
1950 	conf_entries_t	*del;
1951 
1952 	if (list == NULL)
1953 		return (NULL);
1954 	el = list;
1955 	while (el != NULL) {
1956 		del = el;
1957 		el = el->next;
1958 		free(del->name);
1959 		free(del->piclclass);
1960 		free(del);
1961 	}
1962 	return (el);
1963 }
1964 
1965 /*
1966  * Reading config order: platform, common
1967  */
1968 static conf_entries_t *
1969 read_conf_file(char *fname, conf_entries_t *list)
1970 {
1971 	FILE		*fp;
1972 	char		lbuf[CONFFILE_LINELEN_MAX];
1973 	char		*nametok;
1974 	char		*classtok;
1975 	conf_entries_t	*el;
1976 	conf_entries_t	*ptr;
1977 
1978 	if (fname == NULL)
1979 		return (list);
1980 
1981 	fp = fopen(fname, "r");
1982 
1983 	if (fp == NULL)
1984 		return (list);
1985 
1986 	while (fgets(lbuf, CONFFILE_LINELEN_MAX, fp) != NULL) {
1987 		if ((lbuf[0] == CONFFILE_COMMENT_CHAR) || (lbuf[0] == '\n'))
1988 			continue;
1989 
1990 		nametok = strtok(lbuf, " \t\n");
1991 		if (nametok == NULL)
1992 			continue;
1993 
1994 		classtok = strtok(NULL, " \t\n");
1995 		if (classtok == NULL)
1996 			continue;
1997 
1998 		el = malloc(sizeof (conf_entries_t));
1999 		if (el == NULL)
2000 			break;
2001 		el->name = strdup(nametok);
2002 		el->piclclass = strdup(classtok);
2003 		if ((el->name == NULL) || (el->piclclass == NULL)) {
2004 			free(el);
2005 			return (list);
2006 		}
2007 		el->next = NULL;
2008 
2009 		/*
2010 		 * Add it to the end of list
2011 		 */
2012 		if (list == NULL)
2013 			list = el;
2014 		else {
2015 			ptr = list;
2016 			while (ptr->next != NULL)
2017 				ptr = ptr->next;
2018 			ptr->next = el;
2019 		}
2020 
2021 	}
2022 	(void) fclose(fp);
2023 	return (list);
2024 }
2025 
2026 /*
2027  * Process the devtree conf file and set up the conf_name_class_map list
2028  */
2029 static void
2030 process_devtree_conf_file(void)
2031 {
2032 	char	nmbuf[SYS_NMLN];
2033 	char	pname[PATH_MAX];
2034 
2035 	conf_name_class_map = NULL;
2036 
2037 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
2038 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2039 		(void) strlcat(pname, DEVTREE_CONFFILE_NAME, PATH_MAX);
2040 		conf_name_class_map = read_conf_file(pname,
2041 		    conf_name_class_map);
2042 	}
2043 
2044 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
2045 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2046 		(void) strlcat(pname, DEVTREE_CONFFILE_NAME, PATH_MAX);
2047 		conf_name_class_map = read_conf_file(pname,
2048 		    conf_name_class_map);
2049 	}
2050 
2051 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
2052 	    DEVTREE_CONFFILE_NAME);
2053 	conf_name_class_map = read_conf_file(pname, conf_name_class_map);
2054 }
2055 
2056 static	asr_conf_entries_t	*conf_name_asr_map = NULL;
2057 
2058 static void
2059 free_asr_conf_entries(asr_conf_entries_t *list) {
2060 	asr_conf_entries_t  *el;
2061 	asr_conf_entries_t  *del;
2062 
2063 	el = list;
2064 	while (el != NULL) {
2065 		del = el;
2066 		el = el->next;
2067 		if (del->name)
2068 			free(del->name);
2069 		if (del->address)
2070 			free(del->address);
2071 		if (del->status)
2072 			free(del->status);
2073 		if (del->piclclass)
2074 			free(del->piclclass);
2075 		if (del->props)
2076 			free(del->props);
2077 		free(del);
2078 	}
2079 }
2080 
2081 /*
2082  * Reading config order: platform, common
2083  */
2084 static asr_conf_entries_t *
2085 read_asr_conf_file(char *fname, asr_conf_entries_t *list)
2086 {
2087 	FILE		*fp;
2088 	char		lbuf[CONFFILE_LINELEN_MAX];
2089 	char		*nametok;
2090 	char		*classtok;
2091 	char		*statustok;
2092 	char		*addresstok;
2093 	char		*propstok;
2094 	asr_conf_entries_t	*el;
2095 	asr_conf_entries_t	*ptr;
2096 
2097 	if (fname == NULL)
2098 		return (list);
2099 
2100 	fp = fopen(fname, "r");
2101 	if (fp == NULL)
2102 		return (list);
2103 
2104 	while (fgets(lbuf, CONFFILE_LINELEN_MAX, fp) != NULL) {
2105 		if ((lbuf[0] == CONFFILE_COMMENT_CHAR) || (lbuf[0] == '\n'))
2106 			continue;
2107 
2108 		nametok = strtok(lbuf, " \t\n");
2109 		if (nametok == NULL)
2110 			continue;
2111 
2112 		classtok = strtok(NULL, " \t\n");
2113 		if (classtok == NULL)
2114 			continue;
2115 
2116 		statustok = strtok(NULL, " \t\n");
2117 		if (statustok == NULL)
2118 			continue;
2119 
2120 		addresstok = strtok(NULL, " \t\n");
2121 		if (addresstok == NULL)
2122 			continue;
2123 
2124 		/*
2125 		 * props are optional
2126 		 */
2127 		propstok = strtok(NULL, " \t\n");
2128 
2129 		el = malloc(sizeof (asr_conf_entries_t));
2130 		if (el == NULL)
2131 			break;
2132 		el->name = strdup(nametok);
2133 		el->piclclass = strdup(classtok);
2134 		el->status = strdup(statustok);
2135 		el->address = strdup(addresstok);
2136 		if (propstok != NULL)
2137 			el->props = strdup(propstok);
2138 		else
2139 			el->props = NULL;
2140 		if ((el->name == NULL) || (el->piclclass == NULL) ||
2141 		    (el->address == NULL) || (el->status == NULL)) {
2142 			if (el->name)
2143 				free(el->name);
2144 			if (el->address)
2145 				free(el->address);
2146 			if (el->status)
2147 				free(el->status);
2148 			if (el->piclclass)
2149 				free(el->piclclass);
2150 			if (el->props)
2151 				free(el->props);
2152 			free(el);
2153 			break;
2154 		}
2155 		el->next = NULL;
2156 
2157 		/*
2158 		 * Add it to the end of list
2159 		 */
2160 		if (list == NULL)
2161 			list = el;
2162 		else {
2163 			ptr = list;
2164 			while (ptr->next != NULL)
2165 				ptr = ptr->next;
2166 			ptr->next = el;
2167 		}
2168 
2169 	}
2170 	(void) fclose(fp);
2171 	return (list);
2172 }
2173 
2174 /*
2175  * Process the asr conf file
2176  */
2177 static void
2178 process_asrtree_conf_file(void)
2179 {
2180 	char	nmbuf[SYS_NMLN];
2181 	char	pname[PATH_MAX];
2182 
2183 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
2184 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2185 		(void) strlcat(pname, ASRTREE_CONFFILE_NAME, PATH_MAX);
2186 		conf_name_asr_map = read_asr_conf_file(pname,
2187 		    conf_name_asr_map);
2188 	}
2189 
2190 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
2191 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
2192 		(void) strlcat(pname, ASRTREE_CONFFILE_NAME, PATH_MAX);
2193 		conf_name_asr_map = read_asr_conf_file(pname,
2194 		    conf_name_asr_map);
2195 	}
2196 
2197 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
2198 	    ASRTREE_CONFFILE_NAME);
2199 	conf_name_asr_map = read_asr_conf_file(pname, conf_name_asr_map);
2200 }
2201 
2202 /*
2203  * This function reads the export file list from ASR
2204  */
2205 static int
2206 get_asr_export_list(char **exportlist, int *exportlistlen)
2207 {
2208 	struct openpromio oppbuf;
2209 	struct openpromio *opp = &oppbuf;
2210 	int d;
2211 	int listsize;
2212 
2213 	d = open("/dev/openprom", O_RDWR);
2214 	if (d < 0)
2215 		return (0);
2216 
2217 	if (ioctl(d, OPROMEXPORTLEN, opp) == -1) {
2218 		(void) close(d);
2219 		return (0);
2220 	}
2221 	listsize = opp->oprom_size;
2222 	opp = (struct openpromio *)malloc(sizeof (struct openpromio) +
2223 	    listsize);
2224 	if (opp == NULL) {
2225 		(void) close(d);
2226 		return (0);
2227 	}
2228 	(void) memset(opp, '\0', sizeof (struct openpromio) + listsize);
2229 	opp->oprom_size = listsize;
2230 	if (ioctl(d, OPROMEXPORT, opp) == -1) {
2231 		free(opp);
2232 		(void) close(d);
2233 		return (0);
2234 	}
2235 	*exportlist = malloc(listsize);
2236 	if (*exportlist == NULL) {
2237 		free(opp);
2238 		(void) close(d);
2239 		return (0);
2240 	}
2241 	(void) memcpy(*exportlist, opp->oprom_array, opp->oprom_size);
2242 	free(opp);
2243 	*exportlistlen = opp->oprom_size;
2244 	(void) close(d);
2245 	return (1);
2246 }
2247 
2248 /*
2249  * Parses properties string, fills in triplet structure with first
2250  * type, name, val triplet and returns pointer to next property.
2251  * Returns NULL if no valid triplet found
2252  * CAUTION: drops \0 characters over separator characters: if you
2253  * want to parse the string twice, you'll have to take a copy.
2254  */
2255 static char *
2256 parse_props_string(char *props, asr_prop_triplet_t *triplet)
2257 {
2258 	char	*prop_name;
2259 	char	*prop_val;
2260 	char	*prop_next;
2261 
2262 	prop_name = strchr(props, '?');
2263 	if (prop_name == NULL)
2264 		return (NULL);
2265 	*prop_name++ = '\0';
2266 	prop_val = strchr(prop_name, '=');
2267 	if (prop_val == NULL)
2268 		return (NULL);
2269 	*prop_val++ = '\0';
2270 	triplet->proptype = props;
2271 	triplet->propname = prop_name;
2272 	triplet->propval = prop_val;
2273 	prop_next = strchr(prop_val, ':');
2274 	if (prop_next == NULL)
2275 		return (prop_val - 1);
2276 	*prop_next++ = '\0';
2277 	return (prop_next);
2278 }
2279 
2280 static int
2281 add_status_prop(picl_nodehdl_t chdh, char *status)
2282 {
2283 	ptree_propinfo_t	propinfo;
2284 	picl_prophdl_t		proph;
2285 	int			err;
2286 
2287 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2288 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(status) + 1,
2289 	    PICL_PROP_STATUS, NULL, NULL);
2290 	if (err != PICL_SUCCESS)
2291 		return (err);
2292 	err = ptree_create_and_add_prop(chdh, &propinfo, status, &proph);
2293 	return (err);
2294 }
2295 
2296 static void
2297 create_asr_node(char *parent, char *child, char *unitaddr, char *class,
2298 	char *status, char *props)
2299 {
2300 	char			ptreepath[PATH_MAX];
2301 	char			nodename[PICL_PROPNAMELEN_MAX];
2302 	char			ua[MAX_UNIT_ADDRESS_LEN];
2303 	char			*props_copy = NULL;
2304 	char			*next;
2305 	char			*prop_string;
2306 	boolean_t		found = B_FALSE;
2307 	picl_nodehdl_t		nodeh;
2308 	picl_nodehdl_t		chdh;
2309 	asr_prop_triplet_t	triple;
2310 	ptree_propinfo_t	propinfo;
2311 	picl_prophdl_t		proph;
2312 	int			val;
2313 	int			err;
2314 
2315 	(void) strlcpy(ptreepath, PLATFORM_PATH, PATH_MAX);
2316 	(void) strlcat(ptreepath, parent, PATH_MAX);
2317 
2318 	if (ptree_get_node_by_path(ptreepath, &nodeh) != PICL_SUCCESS)
2319 		return;
2320 	/*
2321 	 * see if the required child node already exists
2322 	 */
2323 	for (err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
2324 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
2325 	    err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
2326 	    sizeof (picl_nodehdl_t))) {
2327 		if (err != PICL_SUCCESS)
2328 			break;
2329 		err = ptree_get_propval_by_name(chdh, PICL_PROP_NAME,
2330 		    (void *)nodename, PICL_PROPNAMELEN_MAX);
2331 		if (err != PICL_SUCCESS)
2332 			break;
2333 		if (strcmp(nodename, child) != 0)
2334 			continue;
2335 		/*
2336 		 * found a candidate child node
2337 		 */
2338 		if (unitaddr) {
2339 			/*
2340 			 * does it match the required unit address?
2341 			 */
2342 			err = ptree_get_propval_by_name(chdh,
2343 			    PICL_PROP_UNIT_ADDRESS, ua, sizeof (ua));
2344 			if (err == PICL_PROPNOTFOUND)
2345 				continue;
2346 			if (err != PICL_SUCCESS)
2347 				break;
2348 			if (strcmp(unitaddr, ua) != 0)
2349 				continue;
2350 		}
2351 		if (props == NULL) {
2352 			next = "";
2353 		} else if (props_copy == NULL) {
2354 			props_copy = strdup(props);
2355 			if (props_copy == NULL)
2356 				return;
2357 			next = props_copy;
2358 		}
2359 		while ((next = parse_props_string(next, &triple)) != NULL) {
2360 			err = ptree_get_prop_by_name(chdh, triple.propname,
2361 			    &proph);
2362 			if (err != PICL_SUCCESS)
2363 				break;
2364 			err = ptree_get_propinfo(proph, &propinfo);
2365 			if (err != PICL_SUCCESS)
2366 				break;
2367 			err = PICL_FAILURE;
2368 			switch (propinfo.piclinfo.type) {
2369 			case PICL_PTYPE_INT:
2370 			case PICL_PTYPE_UNSIGNED_INT:
2371 				if (strcmp(triple.proptype, "I") != 0)
2372 					break;
2373 				err = ptree_get_propval(proph, (void  *)&val,
2374 				    sizeof (val));
2375 				if (err != PICL_SUCCESS)
2376 					break;
2377 				if (val != atoi(triple.propval))
2378 					err = PICL_FAILURE;
2379 				break;
2380 			case PICL_PTYPE_CHARSTRING:
2381 				if (strcmp(triple.proptype, "S") != 0)
2382 					break;
2383 				prop_string = malloc(propinfo.piclinfo.size);
2384 				if (prop_string == NULL)
2385 					break;
2386 				err = ptree_get_propval(proph,
2387 				    (void *)prop_string,
2388 				    propinfo.piclinfo.size);
2389 				if (err != PICL_SUCCESS) {
2390 					free(prop_string);
2391 					break;
2392 				}
2393 				if (strcmp(prop_string, triple.propval) != 0)
2394 					err = PICL_FAILURE;
2395 				free(prop_string);
2396 				break;
2397 			default:
2398 				break;
2399 			}
2400 			if (err != PICL_SUCCESS) {
2401 				break;
2402 			}
2403 		}
2404 		if (next == NULL) {
2405 			found = B_TRUE;
2406 			break;
2407 		}
2408 	}
2409 	if (props_copy)
2410 		free(props_copy);
2411 	if (found) {
2412 		/*
2413 		 * does the pre-existing node have a status property?
2414 		 */
2415 		err = ptree_get_propval_by_name(chdh, PICL_PROP_STATUS,
2416 		    ua, sizeof (ua));
2417 		if (err == PICL_PROPNOTFOUND)
2418 			(void) add_status_prop(chdh, status);
2419 		if (err != PICL_SUCCESS)
2420 			return;
2421 		if ((strcmp(ua, ASR_DISABLED) == 0) ||
2422 		    (strcmp(ua, ASR_FAILED) == 0) ||
2423 		    ((strcmp(status, ASR_DISABLED) != 0) &&
2424 		    (strcmp(status, ASR_FAILED) != 0))) {
2425 			return;
2426 		}
2427 		/*
2428 		 * more urgent status now, so replace existing value
2429 		 */
2430 		err = ptree_get_prop_by_name(chdh, PICL_PROP_STATUS, &proph);
2431 		if (err != PICL_SUCCESS)
2432 			return;
2433 		(void) ptree_delete_prop(proph);
2434 		(void) ptree_destroy_prop(proph);
2435 		err = add_status_prop(chdh, status);
2436 		if (err != PICL_SUCCESS)
2437 			return;
2438 		return;
2439 	}
2440 
2441 	/*
2442 	 * typical case, node needs adding together with a set of properties
2443 	 */
2444 	if (ptree_create_and_add_node(nodeh, child, class, &chdh) ==
2445 	    PICL_SUCCESS) {
2446 		(void) add_status_prop(chdh, status);
2447 		if (unitaddr) {
2448 			(void) ptree_init_propinfo(&propinfo,
2449 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_CHARSTRING,
2450 			    PICL_READ, strlen(unitaddr) + 1,
2451 			    PICL_PROP_UNIT_ADDRESS, NULL, NULL);
2452 			(void) ptree_create_and_add_prop(chdh, &propinfo,
2453 			    unitaddr, &proph);
2454 			(void) strlcpy(ptreepath, parent, PATH_MAX);
2455 			(void) strlcat(ptreepath, "/", PATH_MAX);
2456 			(void) strlcat(ptreepath, child, PATH_MAX);
2457 			(void) strlcat(ptreepath, "@", PATH_MAX);
2458 			(void) strlcat(ptreepath, unitaddr, PATH_MAX);
2459 			(void) ptree_init_propinfo(&propinfo,
2460 			    PTREE_PROPINFO_VERSION, PICL_PTYPE_CHARSTRING,
2461 			    PICL_READ, strlen(ptreepath) + 1,
2462 			    PICL_PROP_DEVFS_PATH, NULL, NULL);
2463 			(void) ptree_create_and_add_prop(chdh, &propinfo,
2464 			    ptreepath, &proph);
2465 		}
2466 		next = props;
2467 		while ((next = parse_props_string(next, &triple)) != NULL) {
2468 			/*
2469 			 * only handle int and string properties for
2470 			 * simplicity
2471 			 */
2472 			if (strcmp(triple.proptype, "I") == 0) {
2473 				(void) ptree_init_propinfo(&propinfo,
2474 				    PTREE_PROPINFO_VERSION,
2475 				    PICL_PTYPE_INT, PICL_READ,
2476 				    sizeof (int), triple.propname, NULL, NULL);
2477 				val = atoi(triple.propval);
2478 				(void) ptree_create_and_add_prop(chdh,
2479 				    &propinfo, &val, &proph);
2480 			} else {
2481 				(void) ptree_init_propinfo(&propinfo,
2482 				    PTREE_PROPINFO_VERSION,
2483 				    PICL_PTYPE_CHARSTRING, PICL_READ,
2484 				    strlen(triple.propval) + 1,
2485 				    triple.propname, NULL, NULL);
2486 				(void) ptree_create_and_add_prop(chdh,
2487 				    &propinfo, triple.propval, &proph);
2488 			}
2489 		}
2490 	}
2491 }
2492 
2493 static void
2494 add_asr_nodes()
2495 {
2496 	char			*asrexport;
2497 	int			asrexportlen;
2498 	asr_conf_entries_t	*c = NULL;
2499 	int			i;
2500 	char			*key;
2501 	char			*child;
2502 	char			*unitaddr;
2503 	uint16_t		count;
2504 	int			disabled;
2505 
2506 	if (get_asr_export_list(&asrexport, &asrexportlen) == 0)
2507 		return;
2508 	process_asrtree_conf_file();
2509 	if (conf_name_asr_map == NULL)
2510 		return;
2511 	i = 0;
2512 	while (i < asrexportlen) {
2513 		key = &asrexport[i];
2514 		i += strlen(key) + 1;
2515 		if (i >= asrexportlen)
2516 			break;
2517 
2518 		/*
2519 		 * next byte tells us whether failed by diags or manually
2520 		 * disabled
2521 		 */
2522 		disabled = asrexport[i];
2523 		i++;
2524 		if (i >= asrexportlen)
2525 			break;
2526 
2527 		/*
2528 		 * only type 1 supported
2529 		 */
2530 		if (asrexport[i] != 1)
2531 			break;
2532 		i++;
2533 		if (i >= asrexportlen)
2534 			break;
2535 
2536 		/*
2537 		 * next two bytes give size of reason string
2538 		 */
2539 		count = (asrexport[i] << 8) | asrexport[i + 1];
2540 		i += count + 2;
2541 		if (i > asrexportlen)
2542 			break;
2543 
2544 		/*
2545 		 * now look for key in conf file info
2546 		 */
2547 		c = conf_name_asr_map;
2548 		while (c != NULL) {
2549 			if (strcmp(key, c->name) == 0) {
2550 				child = strrchr(c->address, '/');
2551 				*child++ = '\0';
2552 				unitaddr = strchr(child, '@');
2553 				if (unitaddr)
2554 					*unitaddr++ = '\0';
2555 				if (strcmp(c->status, ASR_DISABLED) == 0) {
2556 					create_asr_node(c->address, child,
2557 					    unitaddr, c->piclclass, disabled ?
2558 					    ASR_DISABLED : ASR_FAILED,
2559 					    c->props);
2560 				} else {
2561 					create_asr_node(c->address, child,
2562 					    unitaddr, c->piclclass, c->status,
2563 					    c->props);
2564 				}
2565 			}
2566 			c = c->next;
2567 		}
2568 	}
2569 
2570 	free_asr_conf_entries(conf_name_asr_map);
2571 	free(asrexport);
2572 }
2573 
2574 /*
2575  * This function adds information to the /platform node
2576  */
2577 static int
2578 add_platform_info(picl_nodehdl_t plafh)
2579 {
2580 	struct utsname		uts_info;
2581 	int			err;
2582 	ptree_propinfo_t	propinfo;
2583 	picl_prophdl_t		proph;
2584 
2585 	if (uname(&uts_info) < 0)
2586 		return (PICL_FAILURE);
2587 
2588 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2589 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.sysname) + 1,
2590 	    PICL_PROP_SYSNAME, NULL, NULL);
2591 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.sysname,
2592 	    &proph);
2593 	if (err != PICL_SUCCESS)
2594 		return (err);
2595 
2596 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2597 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.nodename) + 1,
2598 	    PICL_PROP_NODENAME, NULL, NULL);
2599 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.nodename,
2600 	    &proph);
2601 	if (err != PICL_SUCCESS)
2602 		return (err);
2603 
2604 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2605 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.release) + 1,
2606 	    PICL_PROP_RELEASE, NULL, NULL);
2607 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.release,
2608 	    &proph);
2609 	if (err != PICL_SUCCESS)
2610 		return (err);
2611 
2612 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2613 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.version) + 1,
2614 	    PICL_PROP_VERSION, NULL, NULL);
2615 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.version,
2616 	    &proph);
2617 	if (err != PICL_SUCCESS)
2618 		return (err);
2619 
2620 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2621 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(uts_info.machine) + 1,
2622 	    PICL_PROP_MACHINE, NULL, NULL);
2623 	err = ptree_create_and_add_prop(plafh, &propinfo, uts_info.machine,
2624 	    &proph);
2625 	return (err);
2626 }
2627 
2628 /*
2629  * Get first 32-bit value from the reg property
2630  */
2631 static int
2632 get_first_reg_word(picl_nodehdl_t nodeh, uint32_t *regval)
2633 {
2634 	int			err;
2635 	uint32_t		*regbuf;
2636 	picl_prophdl_t  	regh;
2637 	ptree_propinfo_t	pinfo;
2638 
2639 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
2640 	if (err != PICL_SUCCESS) 	/* no reg property */
2641 		return (err);
2642 	err = ptree_get_propinfo(regh, &pinfo);
2643 	if (err != PICL_SUCCESS)
2644 		return (err);
2645 	if (pinfo.piclinfo.size < sizeof (uint32_t)) /* too small */
2646 		return (PICL_FAILURE);
2647 	regbuf = alloca(pinfo.piclinfo.size);
2648 	if (regbuf == NULL)
2649 		return (PICL_FAILURE);
2650 	err = ptree_get_propval(regh, regbuf, pinfo.piclinfo.size);
2651 	if (err != PICL_SUCCESS)
2652 		return (err);
2653 	*regval = *regbuf;	/* get first 32-bit value */
2654 	return (PICL_SUCCESS);
2655 }
2656 
2657 /*
2658  * Get device ID from the reg property
2659  */
2660 static int
2661 get_device_id(picl_nodehdl_t nodeh, uint32_t *dev_id)
2662 {
2663 	int			err;
2664 	uint32_t		regval;
2665 
2666 	err = get_first_reg_word(nodeh, &regval);
2667 	if (err != PICL_SUCCESS)
2668 		return (err);
2669 
2670 	*dev_id = PCI_DEVICE_ID(regval);
2671 	return (PICL_SUCCESS);
2672 }
2673 
2674 /*
2675  * add Slot property for children of SBUS node
2676  */
2677 /* ARGSUSED */
2678 static int
2679 add_sbus_slots(picl_nodehdl_t pcih, void *args)
2680 {
2681 	picl_nodehdl_t		nodeh;
2682 	uint32_t		slot;
2683 	int			err;
2684 	ptree_propinfo_t	pinfo;
2685 
2686 	for (err = ptree_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
2687 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
2688 	    err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
2689 	    sizeof (picl_nodehdl_t))) {
2690 		if (err != PICL_SUCCESS)
2691 			return (err);
2692 
2693 		if (get_first_reg_word(nodeh, &slot) != 0)
2694 			continue;
2695 		(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
2696 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (uint32_t),
2697 		    PICL_PROP_SLOT, NULL, NULL);
2698 		(void) ptree_create_and_add_prop(nodeh, &pinfo, &slot, NULL);
2699 	}
2700 
2701 	return (PICL_WALK_CONTINUE);
2702 }
2703 
2704 /*
2705  * This function creates a Slot property for SBUS child nodes
2706  * which can be correlated with the slot they are plugged into
2707  * on the motherboard.
2708  */
2709 static int
2710 set_sbus_slot(picl_nodehdl_t plafh)
2711 {
2712 	int		err;
2713 
2714 	err = ptree_walk_tree_by_class(plafh, PICL_CLASS_SBUS, NULL,
2715 	    add_sbus_slots);
2716 
2717 	return (err);
2718 }
2719 
2720 /*
2721  * add DeviceID property for children of PCI/PCIEX node
2722  */
2723 /* ARGSUSED */
2724 static int
2725 add_pci_deviceids(picl_nodehdl_t pcih, void *args)
2726 {
2727 	picl_nodehdl_t		nodeh;
2728 	uint32_t		dev_id;
2729 	int			err;
2730 	ptree_propinfo_t	pinfo;
2731 
2732 	for (err = ptree_get_propval_by_name(pcih, PICL_PROP_CHILD, &nodeh,
2733 	    sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
2734 	    err = ptree_get_propval_by_name(nodeh, PICL_PROP_PEER, &nodeh,
2735 	    sizeof (picl_nodehdl_t))) {
2736 		if (err != PICL_SUCCESS)
2737 			return (err);
2738 
2739 		if (get_device_id(nodeh, &dev_id) != 0)
2740 			continue;
2741 		(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
2742 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (uint32_t),
2743 		    PICL_PROP_DEVICE_ID, NULL, NULL);
2744 		(void) ptree_create_and_add_prop(nodeh, &pinfo, &dev_id, NULL);
2745 	}
2746 
2747 	return (PICL_WALK_CONTINUE);
2748 }
2749 
2750 /*
2751  * This function creates a DeviceID property for PCI/PCIEX child nodes
2752  * which can be correlated with the slot they are plugged into
2753  * on the motherboard.
2754  */
2755 static void
2756 set_pci_pciex_deviceid(picl_nodehdl_t plafh)
2757 {
2758 	(void) ptree_walk_tree_by_class(plafh, PICL_CLASS_PCI, NULL,
2759 	    add_pci_deviceids);
2760 
2761 	(void) ptree_walk_tree_by_class(plafh, PICL_CLASS_PCIEX, NULL,
2762 	    add_pci_deviceids);
2763 }
2764 
2765 /*
2766  * Default UnitAddress encode function
2767  */
2768 static int
2769 encode_default_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2770 {
2771 	int	i, len;
2772 
2773 	/*
2774 	 * Encode UnitAddress as %a,%b,%c,...,%n
2775 	 */
2776 	if (addrcells < 1)
2777 		return (-1);
2778 
2779 	len = snprintf(buf, sz, "%x", *regprop);
2780 	for (i = 1; i < addrcells && len < sz; i++)
2781 		len += snprintf(&buf[len], sz-len, ",%x", regprop[i]);
2782 
2783 	return ((len >= sz) ? -1 : 0);
2784 }
2785 
2786 /*
2787  * UnitAddress encode function where the last component is not printed
2788  * unless non-zero.
2789  */
2790 static int
2791 encode_optional_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2792 {
2793 	int	retval;
2794 
2795 	/*
2796 	 * Encode UnitAddress as %a,%b,%c,...,%n where the last component
2797 	 * is printed only if non-zero.
2798 	 */
2799 	if (addrcells > 1 && regprop[addrcells-1] == 0)
2800 		retval = encode_default_unitaddr(buf, sz, regprop, addrcells-1);
2801 	else
2802 		retval = encode_default_unitaddr(buf, sz, regprop, addrcells);
2803 
2804 	return (retval);
2805 }
2806 
2807 
2808 /*
2809  * UnitAddress encode function for SCSI class of devices
2810  */
2811 static int
2812 encode_scsi_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2813 {
2814 	int	len, retval;
2815 
2816 	/*
2817 	 * #address-cells	Format
2818 	 *	2		second component printed only if non-zero
2819 	 *
2820 	 *	4		regprop:   phys_hi phys_lo lun_hi lun_lo
2821 	 *			UnitAddr:  w<phys_hi><phys_lo>,<lun_lo>
2822 	 */
2823 
2824 	if (addrcells == 2) {
2825 		retval = encode_optional_unitaddr(buf, sz, regprop, addrcells);
2826 	} else if (addrcells == 4) {
2827 		len = snprintf(buf, sz, "w%08x%08x,%x", regprop[0], regprop[1],
2828 		    regprop[3]);
2829 		retval = (len >= sz) ? -1 : 0;
2830 	} else
2831 		retval = -1;
2832 
2833 	return (retval);
2834 }
2835 
2836 /*
2837  * UnitAddress encode function for UPA devices
2838  */
2839 static int
2840 encode_upa_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2841 {
2842 	int	len;
2843 
2844 	if (addrcells != 2)
2845 		return (-1);
2846 
2847 	len = snprintf(buf, sz, "%x,%x", (regprop[0]/2)&0x1f, regprop[1]);
2848 	return ((len >= sz) ? -1 : 0);
2849 }
2850 
2851 /*
2852  * UnitAddress encode function for GPTWO, JBUS devices
2853  */
2854 static int
2855 encode_gptwo_jbus_unitaddr(char *buf, int sz, uint32_t *regprop,
2856     uint_t addrcells)
2857 {
2858 	uint32_t	hi, lo;
2859 	int		len, id, off;
2860 
2861 	if (addrcells != 2)
2862 		return (-1);
2863 
2864 	hi = regprop[0];
2865 	lo = regprop[1];
2866 
2867 	if (hi & 0x400) {
2868 		id = ((hi & 0x1) << 9) | (lo >> 23);	/* agent id */
2869 		off = lo & 0x7fffff;			/* config offset */
2870 		len = snprintf(buf, sz, "%x,%x", id, off);
2871 	} else {
2872 		len = snprintf(buf, sz, "m%x,%x", hi, lo);
2873 	}
2874 	return ((len >= sz) ? -1 : 0);
2875 }
2876 
2877 /*
2878  * UnitAddress encode function for PCI devices
2879  */
2880 static int
2881 encode_pci_unitaddr(char *buf, int sz, uint32_t *regprop, uint_t addrcells)
2882 {
2883 	typedef struct {
2884 		uint32_t	n:1,		/* relocatable */
2885 				p:1,		/* prefetchable */
2886 				t:1,		/* address region aliases */
2887 				zero:3,		/* must be zero */
2888 				ss:2,		/* address space type */
2889 				bus:8,		/* bus number */
2890 				dev:5,		/* device number */
2891 				fn:3,		/* function number */
2892 				reg:8;		/* register number */
2893 		uint32_t	phys_hi;	/* high physical address */
2894 		uint32_t	phys_lo;	/* low physical address */
2895 	} pci_addrcell_t;
2896 
2897 	pci_addrcell_t	*p;
2898 	int		len;
2899 
2900 	if (addrcells != 3)
2901 		return (-1);
2902 
2903 	p = (pci_addrcell_t *)regprop;
2904 	switch (p->ss) {
2905 	case 0:		/* Config */
2906 		if (p->fn)
2907 			len = snprintf(buf, sz, "%x,%x", p->dev, p->fn);
2908 		else
2909 			len = snprintf(buf, sz, "%x", p->dev);
2910 		break;
2911 	case 1:		/* IO */
2912 		len = snprintf(buf, sz, "i%x,%x,%x,%x", p->dev, p->fn, p->reg,
2913 		    p->phys_lo);
2914 		break;
2915 	case 2:		/* Mem32 */
2916 		len = snprintf(buf, sz, "m%x,%x,%x,%x", p->dev, p->fn, p->reg,
2917 		    p->phys_lo);
2918 		break;
2919 	case 3:		/* Mem64 */
2920 		len = snprintf(buf, sz, "x%x,%x,%x,%x%08x", p->dev, p->fn,
2921 		    p->reg, p->phys_hi, p->phys_lo);
2922 		break;
2923 	}
2924 	return ((len >= sz) ? -1 : 0);
2925 }
2926 
2927 /*
2928  * Get #address-cells property value
2929  */
2930 static uint_t
2931 get_addrcells_prop(picl_nodehdl_t nodeh)
2932 {
2933 	int			len, err;
2934 	uint32_t		addrcells;
2935 	ptree_propinfo_t	pinfo;
2936 	picl_prophdl_t		proph;
2937 
2938 	/*
2939 	 * Get #address-cells property.  If not present, use default value.
2940 	 */
2941 	err = ptree_get_prop_by_name(nodeh, OBP_PROP_ADDRESS_CELLS, &proph);
2942 	if (err == PICL_SUCCESS)
2943 		err = ptree_get_propinfo(proph, &pinfo);
2944 
2945 	len = pinfo.piclinfo.size;
2946 	if (err == PICL_SUCCESS && len >= sizeof (uint8_t) &&
2947 	    len <= sizeof (addrcells)) {
2948 		err = ptree_get_propval(proph, &addrcells, len);
2949 		if (err == PICL_SUCCESS) {
2950 			if (len == sizeof (uint8_t))
2951 				addrcells = *(uint8_t *)&addrcells;
2952 			else if (len == sizeof (uint16_t))
2953 				addrcells = *(uint16_t *)&addrcells;
2954 		} else
2955 			addrcells = DEFAULT_ADDRESS_CELLS;
2956 	} else
2957 		addrcells = DEFAULT_ADDRESS_CELLS;
2958 
2959 	return (addrcells);
2960 }
2961 
2962 /*
2963  * Get UnitAddress mapping entry for a node
2964  */
2965 static unitaddr_map_t *
2966 get_unitaddr_mapping(picl_nodehdl_t nodeh)
2967 {
2968 	int		err;
2969 	unitaddr_map_t	*uamap;
2970 	char		clname[PICL_CLASSNAMELEN_MAX];
2971 
2972 	/*
2973 	 * Get my classname and locate a function to translate "reg" prop
2974 	 * into "UnitAddress" prop for my children.
2975 	 */
2976 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CLASSNAME, clname,
2977 	    sizeof (clname));
2978 	if (err != PICL_SUCCESS)
2979 		(void) strcpy(clname, "");	/* NULL class name */
2980 
2981 	for (uamap = &unitaddr_map_table[0]; uamap->class != NULL; uamap++)
2982 		if (strcmp(clname, uamap->class) == 0)
2983 			break;
2984 
2985 	return (uamap);
2986 }
2987 
2988 /*
2989  * Add UnitAddress property to the specified node
2990  */
2991 static int
2992 add_unitaddr_prop(picl_nodehdl_t nodeh, unitaddr_map_t *uamap, uint_t addrcells)
2993 {
2994 	int			regproplen, err;
2995 	uint32_t		*regbuf;
2996 	picl_prophdl_t		regh;
2997 	ptree_propinfo_t	pinfo;
2998 	char			unitaddr[MAX_UNIT_ADDRESS_LEN];
2999 
3000 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
3001 	if (err != PICL_SUCCESS)
3002 		return (err);
3003 
3004 	err = ptree_get_propinfo(regh, &pinfo);
3005 	if (err != PICL_SUCCESS)
3006 		return (PICL_FAILURE);
3007 
3008 	if (pinfo.piclinfo.size < (addrcells * sizeof (uint32_t)))
3009 		return (PICL_FAILURE);
3010 
3011 	regproplen = pinfo.piclinfo.size;
3012 	regbuf = alloca(regproplen);
3013 	if (regbuf == NULL)
3014 		return (PICL_FAILURE);
3015 
3016 	err = ptree_get_propval(regh, regbuf, regproplen);
3017 	if (err != PICL_SUCCESS || uamap->func == NULL ||
3018 	    (uamap->addrcellcnt && uamap->addrcellcnt != addrcells) ||
3019 	    (uamap->func)(unitaddr, sizeof (unitaddr), regbuf,
3020 	    addrcells) != 0) {
3021 		return (PICL_FAILURE);
3022 	}
3023 
3024 	err = ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
3025 	    PICL_PTYPE_CHARSTRING, PICL_READ, strlen(unitaddr)+1,
3026 	    PICL_PROP_UNIT_ADDRESS, NULL, NULL);
3027 	if (err == PICL_SUCCESS)
3028 		err = ptree_create_and_add_prop(nodeh, &pinfo, unitaddr, NULL);
3029 
3030 	return (err);
3031 }
3032 
3033 /*
3034  * work out UnitAddress property of the specified node
3035  */
3036 static int
3037 get_unitaddr(picl_nodehdl_t parh, picl_nodehdl_t nodeh, char *unitaddr,
3038     size_t ualen)
3039 {
3040 	int			regproplen, err;
3041 	uint32_t		*regbuf;
3042 	picl_prophdl_t		regh;
3043 	ptree_propinfo_t	pinfo;
3044 	unitaddr_map_t		*uamap;
3045 	uint32_t		addrcells;
3046 
3047 	addrcells = get_addrcells_prop(parh);
3048 	uamap = get_unitaddr_mapping(parh);
3049 
3050 	err = ptree_get_prop_by_name(nodeh, OBP_REG, &regh);
3051 	if (err != PICL_SUCCESS)
3052 		return (err);
3053 
3054 	err = ptree_get_propinfo(regh, &pinfo);
3055 	if (err != PICL_SUCCESS)
3056 		return (err);
3057 
3058 	if (pinfo.piclinfo.size < (addrcells * sizeof (uint32_t)))
3059 		return (PICL_FAILURE);
3060 
3061 	regproplen = pinfo.piclinfo.size;
3062 	regbuf = alloca(regproplen);
3063 	if (regbuf == NULL)
3064 		return (PICL_FAILURE);
3065 
3066 	err = ptree_get_propval(regh, regbuf, regproplen);
3067 	if (err != PICL_SUCCESS || uamap->func == NULL ||
3068 	    (uamap->addrcellcnt && uamap->addrcellcnt != addrcells) ||
3069 	    (uamap->func)(unitaddr, ualen, regbuf, addrcells) != 0) {
3070 		return (PICL_FAILURE);
3071 	}
3072 	return (PICL_SUCCESS);
3073 }
3074 
3075 /*
3076  * Add UnitAddress property to all children of the specified node
3077  */
3078 static int
3079 add_unitaddr_prop_to_subtree(picl_nodehdl_t nodeh)
3080 {
3081 	int			err;
3082 	picl_nodehdl_t		chdh;
3083 	unitaddr_map_t		*uamap;
3084 	uint32_t		addrcells;
3085 
3086 	/*
3087 	 * Get #address-cells and unit address mapping entry for my
3088 	 * node's class
3089 	 */
3090 	addrcells = get_addrcells_prop(nodeh);
3091 	uamap = get_unitaddr_mapping(nodeh);
3092 
3093 	/*
3094 	 * Add UnitAddress property to my children and their subtree
3095 	 */
3096 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
3097 	    sizeof (picl_nodehdl_t));
3098 
3099 	while (err == PICL_SUCCESS) {
3100 		(void) add_unitaddr_prop(chdh, uamap, addrcells);
3101 		(void) add_unitaddr_prop_to_subtree(chdh);
3102 
3103 		err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
3104 		    sizeof (picl_nodehdl_t));
3105 	}
3106 
3107 	return (PICL_SUCCESS);
3108 }
3109 
3110 static int
3111 update_memory_size_prop(picl_nodehdl_t plafh)
3112 {
3113 	picl_nodehdl_t		memh;
3114 	picl_prophdl_t		proph;
3115 	ptree_propinfo_t	pinfo;
3116 	int			err, nspecs, snum, pval;
3117 	char			*regbuf;
3118 	memspecs_t		*mspecs;
3119 	uint64_t		memsize;
3120 
3121 	/*
3122 	 * check if the #size-cells of the platform node is 2
3123 	 */
3124 	err = ptree_get_propval_by_name(plafh, OBP_PROP_SIZE_CELLS, &pval,
3125 	    sizeof (pval));
3126 
3127 	if (err == PICL_PROPNOTFOUND)
3128 		pval = SUPPORTED_NUM_CELL_SIZE;
3129 	else if (err != PICL_SUCCESS)
3130 		return (err);
3131 
3132 	/*
3133 	 * don't know how to handle other vals
3134 	 */
3135 	if (pval != SUPPORTED_NUM_CELL_SIZE)
3136 		return (PICL_FAILURE);
3137 
3138 	err = ptree_get_node_by_path(MEMORY_PATH, &memh);
3139 	if (err != PICL_SUCCESS)
3140 		return (err);
3141 
3142 	/*
3143 	 * Get the REG property to calculate the size of memory
3144 	 */
3145 	err = ptree_get_prop_by_name(memh, OBP_REG, &proph);
3146 	if (err != PICL_SUCCESS)
3147 		return (err);
3148 
3149 	err = ptree_get_propinfo(proph, &pinfo);
3150 	if (err != PICL_SUCCESS)
3151 		return (err);
3152 
3153 	regbuf = alloca(pinfo.piclinfo.size);
3154 	if (regbuf == NULL)
3155 		return (PICL_FAILURE);
3156 
3157 	err = ptree_get_propval(proph, regbuf, pinfo.piclinfo.size);
3158 	if (err != PICL_SUCCESS)
3159 		return (err);
3160 
3161 	mspecs = (memspecs_t *)regbuf;
3162 	nspecs = pinfo.piclinfo.size / sizeof (memspecs_t);
3163 
3164 	memsize = 0;
3165 	for (snum = 0; snum < nspecs; ++snum)
3166 		memsize += mspecs[snum].size;
3167 
3168 	err = ptree_get_prop_by_name(memh, PICL_PROP_SIZE, &proph);
3169 	if (err == PICL_SUCCESS) {
3170 		err = ptree_update_propval(proph, &memsize, sizeof (memsize));
3171 		return (err);
3172 	}
3173 
3174 	/*
3175 	 * Add the size property
3176 	 */
3177 	(void) ptree_init_propinfo(&pinfo, PTREE_PROPINFO_VERSION,
3178 	    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (memsize),
3179 	    PICL_PROP_SIZE, NULL, NULL);
3180 	err = ptree_create_and_add_prop(memh, &pinfo, &memsize, NULL);
3181 	return (err);
3182 }
3183 
3184 /*
3185  * This function is executed as part of .init when the plugin is
3186  * dlopen()ed
3187  */
3188 static void
3189 picldevtree_register(void)
3190 {
3191 	if (getenv(SUNW_PICLDEVTREE_PLUGIN_DEBUG))
3192 		picldevtree_debug = 1;
3193 	(void) picld_plugin_register(&my_reg_info);
3194 }
3195 
3196 /*
3197  * This function is the init entry point of the plugin.
3198  * It initializes the /platform tree based on libdevinfo
3199  */
3200 static void
3201 picldevtree_init(void)
3202 {
3203 	picl_nodehdl_t	rhdl;
3204 	int		err;
3205 	struct utsname	utsname;
3206 	picl_nodehdl_t	plafh;
3207 
3208 	if (uname(&utsname) < 0)
3209 		return;
3210 
3211 	(void) strcpy(mach_name, utsname.machine);
3212 
3213 	if (strcmp(mach_name, "sun4u") == 0) {
3214 		builtin_map_ptr = sun4u_map;
3215 		builtin_map_size = sizeof (sun4u_map) / sizeof (builtin_map_t);
3216 	} else if (strcmp(mach_name, "sun4v") == 0) {
3217 		builtin_map_ptr = sun4u_map;
3218 		builtin_map_size = sizeof (sun4u_map) / sizeof (builtin_map_t);
3219 	} else if (strcmp(mach_name, "i86pc") == 0) {
3220 		builtin_map_ptr = i86pc_map;
3221 		builtin_map_size = sizeof (i86pc_map) / sizeof (builtin_map_t);
3222 	} else {
3223 		builtin_map_ptr = NULL;
3224 		builtin_map_size = 0;
3225 	}
3226 
3227 	err = ptree_get_root(&rhdl);
3228 	if (err != PICL_SUCCESS) {
3229 		syslog(LOG_ERR, DEVINFO_PLUGIN_INIT_FAILED);
3230 		return;
3231 	}
3232 
3233 	process_devtree_conf_file();
3234 
3235 	if (libdevinfo_init(rhdl) != PICL_SUCCESS) {
3236 		syslog(LOG_ERR, DEVINFO_PLUGIN_INIT_FAILED);
3237 		return;
3238 	}
3239 
3240 	err = ptree_get_node_by_path(PLATFORM_PATH, &plafh);
3241 	if (err != PICL_SUCCESS)
3242 		return;
3243 
3244 	(void) add_unitaddr_prop_to_subtree(plafh);
3245 
3246 	add_asr_nodes();
3247 
3248 	(void) update_memory_size_prop(plafh);
3249 
3250 	(void) setup_cpus(plafh);
3251 
3252 	(void) add_ffb_config_info(plafh);
3253 
3254 	(void) add_platform_info(plafh);
3255 
3256 	set_pci_pciex_deviceid(plafh);
3257 
3258 	(void) set_sbus_slot(plafh);
3259 
3260 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
3261 	    picldevtree_evhandler, NULL);
3262 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
3263 	    picldevtree_evhandler, NULL);
3264 	(void) ptree_register_handler(PICLEVENT_CPU_STATE_CHANGE,
3265 	    picldevtree_evhandler, NULL);
3266 }
3267 
3268 /*
3269  * This function is the fini entry point of the plugin
3270  */
3271 static void
3272 picldevtree_fini(void)
3273 {
3274 	/* First unregister the event handlers */
3275 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
3276 	    picldevtree_evhandler, NULL);
3277 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
3278 	    picldevtree_evhandler, NULL);
3279 	(void) ptree_unregister_handler(PICLEVENT_CPU_STATE_CHANGE,
3280 	    picldevtree_evhandler, NULL);
3281 
3282 	conf_name_class_map = free_conf_entries(conf_name_class_map);
3283 }
3284 
3285 /*
3286  * This function is the event handler of this plug-in.
3287  *
3288  * It processes the following events:
3289  *
3290  *	PICLEVENT_SYSEVENT_DEVICE_ADDED
3291  *	PICLEVENT_SYSEVENT_DEVICE_REMOVED
3292  *	PICLEVENT_CPU_STATE_CHANGE
3293  */
3294 /* ARGSUSED */
3295 static void
3296 picldevtree_evhandler(const char *ename, const void *earg, size_t size,
3297     void *cookie)
3298 {
3299 	char			*devfs_path;
3300 	char			ptreepath[PATH_MAX];
3301 	char			dipath[PATH_MAX];
3302 	picl_nodehdl_t		plafh;
3303 	picl_nodehdl_t		nodeh;
3304 	nvlist_t		*nvlp;
3305 
3306 	if (earg == NULL)
3307 		return;
3308 
3309 	nvlp = NULL;
3310 	if (ptree_get_node_by_path(PLATFORM_PATH, &plafh) != PICL_SUCCESS ||
3311 	    nvlist_unpack((char *)earg, size, &nvlp, NULL) ||
3312 	    nvlist_lookup_string(nvlp, PICLEVENTARG_DEVFS_PATH, &devfs_path) ||
3313 	    strlen(devfs_path) > (PATH_MAX - sizeof (PLATFORM_PATH))) {
3314 		syslog(LOG_INFO, PICL_EVENT_DROPPED, ename);
3315 		if (nvlp)
3316 			nvlist_free(nvlp);
3317 		return;
3318 	}
3319 
3320 	(void) strlcpy(ptreepath, PLATFORM_PATH, PATH_MAX);
3321 	(void) strlcat(ptreepath, devfs_path, PATH_MAX);
3322 	(void) strlcpy(dipath, devfs_path, PATH_MAX);
3323 	nvlist_free(nvlp);
3324 
3325 	if (picldevtree_debug)
3326 		syslog(LOG_INFO, "picldevtree: event handler invoked ename:%s "
3327 		    "ptreepath:%s\n", ename, ptreepath);
3328 
3329 	if (strcmp(ename, PICLEVENT_CPU_STATE_CHANGE) == 0) {
3330 		goto done;
3331 	}
3332 	if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0) {
3333 		di_node_t		devnode;
3334 		char		*strp;
3335 		picl_nodehdl_t	parh;
3336 		char		nodeclass[PICL_CLASSNAMELEN_MAX];
3337 		char		*nodename;
3338 		int		err;
3339 
3340 		/* If the node already exist, then nothing else to do here */
3341 		if (ptree_get_node_by_path(ptreepath, &nodeh) == PICL_SUCCESS)
3342 			return;
3343 
3344 		/* Skip if unable to find parent PICL node handle */
3345 		parh = plafh;
3346 		if (((strp = strrchr(ptreepath, '/')) != NULL) &&
3347 		    (strp != strchr(ptreepath, '/'))) {
3348 			*strp = '\0';
3349 			if (ptree_get_node_by_path(ptreepath, &parh) !=
3350 			    PICL_SUCCESS)
3351 				return;
3352 		}
3353 
3354 		/*
3355 		 * If parent is the root node
3356 		 */
3357 		if (parh == plafh) {
3358 			ph = di_prom_init();
3359 			devnode = di_init(dipath, DINFOCPYALL);
3360 			if (devnode == DI_NODE_NIL) {
3361 				if (ph != NULL) {
3362 					di_prom_fini(ph);
3363 					ph = NULL;
3364 				}
3365 				return;
3366 			}
3367 			nodename = di_node_name(devnode);
3368 			if (nodename == NULL) {
3369 				di_fini(devnode);
3370 				if (ph != NULL) {
3371 					di_prom_fini(ph);
3372 					ph = NULL;
3373 				}
3374 				return;
3375 			}
3376 
3377 			err = get_node_class(nodeclass, devnode, nodename);
3378 			if (err < 0) {
3379 				di_fini(devnode);
3380 				if (ph != NULL) {
3381 					di_prom_fini(ph);
3382 					ph = NULL;
3383 				}
3384 				return;
3385 			}
3386 			err = construct_devtype_node(plafh, nodename,
3387 			    nodeclass, devnode, &nodeh);
3388 			if (err != PICL_SUCCESS) {
3389 				di_fini(devnode);
3390 				if (ph != NULL) {
3391 					di_prom_fini(ph);
3392 					ph = NULL;
3393 				}
3394 				return;
3395 			}
3396 			(void) update_subtree(nodeh, devnode);
3397 			(void) add_unitaddr_prop_to_subtree(nodeh);
3398 			if (ph != NULL) {
3399 				di_prom_fini(ph);
3400 				ph = NULL;
3401 			}
3402 			di_fini(devnode);
3403 			goto done;
3404 		}
3405 
3406 		/* kludge ... try without bus-addr first */
3407 		if ((strp = strrchr(dipath, '@')) != NULL) {
3408 			char *p;
3409 
3410 			p = strrchr(dipath, '/');
3411 			if (p != NULL && strp > p) {
3412 				*strp = '\0';
3413 				devnode = di_init(dipath, DINFOCPYALL);
3414 				if (devnode != DI_NODE_NIL)
3415 					di_fini(devnode);
3416 				*strp = '@';
3417 			}
3418 		}
3419 		/* Get parent devnode */
3420 		if ((strp = strrchr(dipath, '/')) != NULL)
3421 			*++strp = '\0';
3422 		devnode = di_init(dipath, DINFOCPYALL);
3423 		if (devnode == DI_NODE_NIL)
3424 			return;
3425 		ph = di_prom_init();
3426 		(void) update_subtree(parh, devnode);
3427 		(void) add_unitaddr_prop_to_subtree(parh);
3428 		if (ph) {
3429 			di_prom_fini(ph);
3430 			ph = NULL;
3431 		}
3432 		di_fini(devnode);
3433 	} else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0) {
3434 		char			delclass[PICL_CLASSNAMELEN_MAX];
3435 		char		*strp;
3436 
3437 		/*
3438 		 * if final element of path doesn't have a unit address
3439 		 * then it is not uniquely identifiable - cannot remove
3440 		 */
3441 		if (((strp = strrchr(ptreepath, '/')) != NULL) &&
3442 		    strchr(strp, '@') == NULL)
3443 			return;
3444 
3445 		/* skip if can't find the node */
3446 		if (ptree_get_node_by_path(ptreepath, &nodeh) != PICL_SUCCESS)
3447 			return;
3448 
3449 		if (ptree_delete_node(nodeh) != PICL_SUCCESS)
3450 			return;
3451 
3452 		if (picldevtree_debug)
3453 			syslog(LOG_INFO,
3454 			    "picldevtree: deleted node nodeh:%llx\n", nodeh);
3455 		if ((ptree_get_propval_by_name(nodeh,
3456 		    PICL_PROP_CLASSNAME, delclass, PICL_CLASSNAMELEN_MAX) ==
3457 		    PICL_SUCCESS) && IS_MC(delclass)) {
3458 			if (post_mc_event(PICLEVENT_MC_REMOVED, nodeh) !=
3459 			    PICL_SUCCESS)
3460 				syslog(LOG_WARNING, PICL_EVENT_DROPPED,
3461 				    PICLEVENT_MC_REMOVED);
3462 		} else
3463 			(void) ptree_destroy_node(nodeh);
3464 	}
3465 done:
3466 	(void) setup_cpus(plafh);
3467 	(void) add_ffb_config_info(plafh);
3468 	set_pci_pciex_deviceid(plafh);
3469 	(void) set_sbus_slot(plafh);
3470 	if (picldevtree_debug > 1)
3471 		syslog(LOG_INFO, "picldevtree: event handler done\n");
3472 }
3473