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