17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*35551380Skmohan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <alloca.h>
287c478bd9Sstevel@tonic-gate #include <picl.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include "picldefs.h"
347c478bd9Sstevel@tonic-gate #include "fru_data.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include "libfruds.h"
377c478bd9Sstevel@tonic-gate #include "libfrup.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* ========================================================================= */
407c478bd9Sstevel@tonic-gate #define	TREEHDL_TO_PICLHDL(treehdl) ((picl_nodehdl_t)treehdl)
417c478bd9Sstevel@tonic-gate #define	PICLHDL_TO_TREEHDL(piclhdl) ((fru_treehdl_t)piclhdl)
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #define	TREESEGHDL_TO_PICLHDL(treeseghdl) ((picl_nodehdl_t)treeseghdl)
447c478bd9Sstevel@tonic-gate #define	PICLHDL_TO_TREESEGHDL(piclhdl) ((fru_treeseghdl_t)piclhdl)
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* Cache of the root node for quick checks */
477c478bd9Sstevel@tonic-gate static picl_nodehdl_t picl_root_node;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* ========================================================================= */
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Map the PICL errors the plugin would give me to FRU errors
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate static fru_errno_t
map_plugin_err(int picl_err)557c478bd9Sstevel@tonic-gate map_plugin_err(int picl_err)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	switch (picl_err) {
587c478bd9Sstevel@tonic-gate 		case PICL_SUCCESS:
597c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
607c478bd9Sstevel@tonic-gate 		case PICL_PERMDENIED:
617c478bd9Sstevel@tonic-gate 			return (FRU_INVALPERM);
627c478bd9Sstevel@tonic-gate 		case PICL_PROPEXISTS:
637c478bd9Sstevel@tonic-gate 			return (FRU_DUPSEG);
647c478bd9Sstevel@tonic-gate 		case PICL_NOSPACE:
657c478bd9Sstevel@tonic-gate 			return (FRU_NOSPACE);
66*35551380Skmohan 		case PICL_NORESPONSE:
67*35551380Skmohan 			return (FRU_NORESPONSE);
68*35551380Skmohan 		case PICL_PROPNOTFOUND:
69*35551380Skmohan 			return (FRU_NODENOTFOUND);
70*35551380Skmohan 		case PICL_ENDOFLIST:
71*35551380Skmohan 			return (FRU_DATANOTFOUND);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	return (FRU_IOERROR);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* ========================================================================= */
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * cause a refresh of the sub-nodes by writing anything to the container
797c478bd9Sstevel@tonic-gate  * property of the node.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static fru_errno_t
update_data_nodes(picl_nodehdl_t handle)827c478bd9Sstevel@tonic-gate update_data_nodes(picl_nodehdl_t handle)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	uint32_t container = FRUDATA_DELETE_TAG_KEY;
857c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_set_propval_by_name(handle,
887c478bd9Sstevel@tonic-gate 		PICL_PROP_CONTAINER, (void *)&container,
897c478bd9Sstevel@tonic-gate 		sizeof (container))) != PICL_SUCCESS) {
907c478bd9Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* ========================================================================= */
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * picl like function which gets a string property with the proper length
997c478bd9Sstevel@tonic-gate  * NOTE: returns picl errno values NOT fru_errno_t
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static int
get_strprop_by_name(picl_nodehdl_t handle,char * prop_name,char ** string)1027c478bd9Sstevel@tonic-gate get_strprop_by_name(picl_nodehdl_t handle, char *prop_name, char **string)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
1057c478bd9Sstevel@tonic-gate 	picl_prophdl_t proph;
1067c478bd9Sstevel@tonic-gate 	picl_propinfo_t prop_info;
1077c478bd9Sstevel@tonic-gate 	char *tmp_buf = NULL;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propinfo_by_name(handle, prop_name,
1107c478bd9Sstevel@tonic-gate 		&prop_info, &proph)) != PICL_SUCCESS) {
1117c478bd9Sstevel@tonic-gate 		return (picl_err);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	tmp_buf = malloc((sizeof (*tmp_buf) * prop_info.size));
1157c478bd9Sstevel@tonic-gate 	if (tmp_buf == NULL) {
1167c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval(proph, tmp_buf, prop_info.size))
1207c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1217c478bd9Sstevel@tonic-gate 		free(tmp_buf);
1227c478bd9Sstevel@tonic-gate 		return (picl_err);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	*string = tmp_buf;
1267c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* ========================================================================= */
1307c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_name_from_hdl(fru_treehdl_t node,char ** name)1317c478bd9Sstevel@tonic-gate fpt_get_name_from_hdl(fru_treehdl_t node, char **name)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
1347c478bd9Sstevel@tonic-gate 	char *tmp_name = NULL;
1357c478bd9Sstevel@tonic-gate 	char *label = NULL;
1367c478bd9Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	/* get the name */
1397c478bd9Sstevel@tonic-gate 	if ((picl_err = get_strprop_by_name(handle, PICL_PROP_NAME,
1407c478bd9Sstevel@tonic-gate 		&tmp_name)) != PICL_SUCCESS) {
141*35551380Skmohan 		return (map_plugin_err(picl_err));
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* get the label, if any */
1457c478bd9Sstevel@tonic-gate 	if ((picl_err = get_strprop_by_name(handle, PICL_PROP_LABEL,
1467c478bd9Sstevel@tonic-gate 		&label)) != PICL_SUCCESS) {
1477c478bd9Sstevel@tonic-gate 		if (picl_err != PICL_PROPNOTFOUND) {
1487c478bd9Sstevel@tonic-gate 			free(tmp_name);
149*35551380Skmohan 			return (map_plugin_err(picl_err));
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		/* else PICL_PROPNOTFOUND is OK because not all nodes */
1527c478bd9Sstevel@tonic-gate 		/* will have a label. */
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* construct the name as nessecary */
1567c478bd9Sstevel@tonic-gate 	if (label == NULL) {
1577c478bd9Sstevel@tonic-gate 		*name = strdup(tmp_name);
1587c478bd9Sstevel@tonic-gate 	} else {
1597c478bd9Sstevel@tonic-gate #define	FRU_LABEL_PADDING 10
1607c478bd9Sstevel@tonic-gate 		size_t buf_size = strlen(tmp_name) + strlen(label) +
1617c478bd9Sstevel@tonic-gate 			FRU_LABEL_PADDING;
1627c478bd9Sstevel@tonic-gate 		char *tmp = malloc(buf_size);
1637c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
1647c478bd9Sstevel@tonic-gate 			free(tmp_name);
1657c478bd9Sstevel@tonic-gate 			free(label);
1667c478bd9Sstevel@tonic-gate 			return (FRU_FAILURE);
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 		snprintf(tmp, buf_size, "%s?%s=%s", tmp_name,
1697c478bd9Sstevel@tonic-gate 			PICL_PROP_LABEL, label);
1707c478bd9Sstevel@tonic-gate 		*name = tmp;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	free(tmp_name);
1747c478bd9Sstevel@tonic-gate 	free(label);
1757c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /* ========================================================================= */
1797c478bd9Sstevel@tonic-gate /* compare the node name to the name passed */
1807c478bd9Sstevel@tonic-gate static fru_errno_t
cmp_node_name(picl_nodehdl_t node,const char * name)1817c478bd9Sstevel@tonic-gate cmp_node_name(picl_nodehdl_t node, const char *name)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	char *node_name = NULL;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (get_strprop_by_name(node, PICL_PROP_NAME, &node_name)
1867c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
1877c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (strcmp(node_name, name) == 0) {
1917c478bd9Sstevel@tonic-gate 		free(node_name);
1927c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	free(node_name);
1967c478bd9Sstevel@tonic-gate 	return (FRU_FAILURE);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* ========================================================================= */
2007c478bd9Sstevel@tonic-gate /* compare the node class name to the name passed */
2017c478bd9Sstevel@tonic-gate static fru_errno_t
cmp_class_name(picl_nodehdl_t node,const char * name)2027c478bd9Sstevel@tonic-gate cmp_class_name(picl_nodehdl_t node, const char *name)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	char *class_name = NULL;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (get_strprop_by_name(node, PICL_PROP_CLASSNAME, &class_name)
2077c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
2087c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if (strcmp(class_name, name) == 0) {
2127c478bd9Sstevel@tonic-gate 		free(class_name);
2137c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	free(class_name);
2177c478bd9Sstevel@tonic-gate 	return (FRU_FAILURE);
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /* ========================================================================= */
2227c478bd9Sstevel@tonic-gate /* get the "frutree" root node */
2237c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_root(fru_treehdl_t * node)2247c478bd9Sstevel@tonic-gate fpt_get_root(fru_treehdl_t *node)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	picl_nodehdl_t picl_node;
2277c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	picl_err = picl_get_root(&picl_node);
2307c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(picl_node, PICL_PROP_CHILD,
2317c478bd9Sstevel@tonic-gate 		(void *)&picl_node, sizeof (picl_node)))
2327c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
233*35551380Skmohan 		return (map_plugin_err(picl_err));
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	while (cmp_node_name(picl_node, PICL_NODE_FRUTREE)
2377c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS) {
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		if ((picl_err = picl_get_propval_by_name(picl_node,
2407c478bd9Sstevel@tonic-gate 			PICL_PROP_PEER, (void *)&picl_node,
2417c478bd9Sstevel@tonic-gate 			sizeof (picl_node))) == PICL_PROPNOTFOUND) {
2427c478bd9Sstevel@tonic-gate 			return (FRU_NODENOTFOUND);
2437c478bd9Sstevel@tonic-gate 		} else if (picl_err != PICL_SUCCESS) {
244*35551380Skmohan 			return (map_plugin_err(picl_err));
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	picl_root_node = picl_node;
2497c478bd9Sstevel@tonic-gate 	*node = PICLHDL_TO_TREEHDL(picl_node);
2507c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /* ========================================================================= */
2547c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_peer(fru_treehdl_t sibling,fru_treehdl_t * peer)2557c478bd9Sstevel@tonic-gate fpt_get_peer(fru_treehdl_t sibling, fru_treehdl_t *peer)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
2587c478bd9Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(sibling);
2597c478bd9Sstevel@tonic-gate 	picl_nodehdl_t picl_peer;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	rc = picl_get_propval_by_name(handle, PICL_PROP_PEER,
2627c478bd9Sstevel@tonic-gate 		(void *)&picl_peer, sizeof (picl_peer));
2637c478bd9Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
264*35551380Skmohan 		return (map_plugin_err(rc));
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	*peer = PICLHDL_TO_TREEHDL(picl_peer);
2687c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* ========================================================================= */
2727c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_child(fru_treehdl_t handle,fru_treehdl_t * child)2737c478bd9Sstevel@tonic-gate fpt_get_child(fru_treehdl_t handle, fru_treehdl_t *child)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	picl_nodehdl_t p_child;
2767c478bd9Sstevel@tonic-gate 	int rc = picl_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
2777c478bd9Sstevel@tonic-gate 		PICL_PROP_CHILD, (void *)&p_child, sizeof (p_child));
2787c478bd9Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
279*35551380Skmohan 		return (map_plugin_err(rc));
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	*child = PICLHDL_TO_TREEHDL(p_child);
2837c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /* ========================================================================= */
2877c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_parent(fru_treehdl_t handle,fru_treehdl_t * parent)2887c478bd9Sstevel@tonic-gate fpt_get_parent(fru_treehdl_t handle, fru_treehdl_t *parent)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
2917c478bd9Sstevel@tonic-gate 	picl_nodehdl_t p_parent;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/* do not allow the libfru users to see the parent of the root */
2947c478bd9Sstevel@tonic-gate 	if (TREEHDL_TO_PICLHDL(handle) == picl_root_node) {
2957c478bd9Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	rc = picl_get_propval_by_name(TREEHDL_TO_PICLHDL(handle),
2997c478bd9Sstevel@tonic-gate 		PICL_PROP_PARENT, (void *)&p_parent, sizeof (p_parent));
3007c478bd9Sstevel@tonic-gate 	if (rc != PICL_SUCCESS) {
301*35551380Skmohan 		return (map_plugin_err(rc));
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	*parent = PICLHDL_TO_TREEHDL(p_parent);
3057c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /* ========================================================================= */
3097c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_node_type(fru_treehdl_t node,fru_node_t * type)3107c478bd9Sstevel@tonic-gate fpt_get_node_type(fru_treehdl_t node, fru_node_t *type)
3117c478bd9Sstevel@tonic-gate {
312*35551380Skmohan 	int	rc = PICL_SUCCESS;
3137c478bd9Sstevel@tonic-gate 	char picl_class[PICL_PROPNAMELEN_MAX];
3147c478bd9Sstevel@tonic-gate 	picl_nodehdl_t handle = TREEHDL_TO_PICLHDL(node);
3157c478bd9Sstevel@tonic-gate 
316*35551380Skmohan 	if ((rc = picl_get_propval_by_name(handle, PICL_PROP_CLASSNAME,
317*35551380Skmohan 		picl_class, sizeof (picl_class))) != PICL_SUCCESS) {
318*35551380Skmohan 		return (map_plugin_err(rc));
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	if (strcmp(picl_class, PICL_CLASS_LOCATION) == 0)  {
3227c478bd9Sstevel@tonic-gate 		*type = FRU_NODE_LOCATION;
3237c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3247c478bd9Sstevel@tonic-gate 	} else if (strcmp(picl_class, PICL_CLASS_FRU) == 0) {
3257c478bd9Sstevel@tonic-gate 		picl_prophdl_t proph;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		/* check for the CONTAINER_PROP property which indicates */
3287c478bd9Sstevel@tonic-gate 		/* there is data for this node.  (ie fru is a container) */
3297c478bd9Sstevel@tonic-gate 		if (picl_get_prop_by_name(handle,
3307c478bd9Sstevel@tonic-gate 			PICL_PROP_CONTAINER, &proph) == PICL_SUCCESS) {
3317c478bd9Sstevel@tonic-gate 			*type = FRU_NODE_CONTAINER;
3327c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		*type = FRU_NODE_FRU;
3357c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	*type = FRU_NODE_UNKNOWN;
3397c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /* ========================================================================= */
3437c478bd9Sstevel@tonic-gate /* find the next section or return NODENOTFOUND */
3447c478bd9Sstevel@tonic-gate static fru_errno_t
find_next_section(picl_nodehdl_t current,picl_nodehdl_t * next)3457c478bd9Sstevel@tonic-gate find_next_section(picl_nodehdl_t current, picl_nodehdl_t *next)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	picl_nodehdl_t rc_next;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	if (picl_get_propval_by_name(current, PICL_PROP_PEER,
3507c478bd9Sstevel@tonic-gate 		(void *)&rc_next, sizeof (rc_next)) != PICL_SUCCESS) {
3517c478bd9Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/* Make sure this is a "Section" node */
3557c478bd9Sstevel@tonic-gate 	if (cmp_class_name(rc_next, PICL_CLASS_SECTION)
3567c478bd9Sstevel@tonic-gate 			== FRU_SUCCESS) {
3577c478bd9Sstevel@tonic-gate 		*next = rc_next;
3587c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/* and if this is not good keep trying to find a peer which */
3627c478bd9Sstevel@tonic-gate 	/* is a section */
3637c478bd9Sstevel@tonic-gate 	return (find_next_section(rc_next, next));
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate /* ========================================================================= */
3677c478bd9Sstevel@tonic-gate /* find the first section or return NODENOTFOUND */
3687c478bd9Sstevel@tonic-gate static fru_errno_t
find_first_section(picl_nodehdl_t parent,picl_nodehdl_t * section)3697c478bd9Sstevel@tonic-gate find_first_section(picl_nodehdl_t parent, picl_nodehdl_t *section)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	picl_nodehdl_t rc_section;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (picl_get_propval_by_name(parent, PICL_PROP_CHILD,
3747c478bd9Sstevel@tonic-gate 		(void *)&rc_section, sizeof (rc_section)) != PICL_SUCCESS) {
3757c478bd9Sstevel@tonic-gate 		return (FRU_NODENOTFOUND);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/* Make sure this is a "Section" node */
3797c478bd9Sstevel@tonic-gate 	if (cmp_class_name(rc_section, PICL_CLASS_SECTION)
3807c478bd9Sstevel@tonic-gate 			== FRU_SUCCESS) {
3817c478bd9Sstevel@tonic-gate 		*section = rc_section;
3827c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/* and if this is not good keep trying to find a peer which */
3867c478bd9Sstevel@tonic-gate 	/* is a section */
3877c478bd9Sstevel@tonic-gate 	return (find_next_section(rc_section, section));
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /* ========================================================================= */
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Find the handle of the segment node "segment".
3937c478bd9Sstevel@tonic-gate  * also returns the hardware description of this segment.  (read from the
3947c478bd9Sstevel@tonic-gate  * section this was found in.)
3957c478bd9Sstevel@tonic-gate  * If the ign_cor_flg is set this will still succeed even if the segment is
3967c478bd9Sstevel@tonic-gate  * corrupt, otherwise it will return FRU_SEGCORRUPT for corrupt segments
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate #define	IGN_CORRUPT_YES 1
3997c478bd9Sstevel@tonic-gate #define	IGN_CORRUPT_NO 0
4007c478bd9Sstevel@tonic-gate static fru_errno_t
get_segment_node(picl_nodehdl_t handle,const char * segment,picl_nodehdl_t * seg_hdl,fru_seg_hwdesc_t * hw_desc,int ign_cor_flg)4017c478bd9Sstevel@tonic-gate get_segment_node(picl_nodehdl_t handle, const char *segment,
4027c478bd9Sstevel@tonic-gate 	picl_nodehdl_t *seg_hdl, fru_seg_hwdesc_t *hw_desc, int ign_cor_flg)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
4057c478bd9Sstevel@tonic-gate 	picl_nodehdl_t sect_node;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if ((err = update_data_nodes(handle)) != FRU_SUCCESS) {
4087c478bd9Sstevel@tonic-gate 		return (err);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if ((err = find_first_section(handle, &sect_node)) != FRU_SUCCESS) {
4127c478bd9Sstevel@tonic-gate 		return (err);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* while there are sections. */
4167c478bd9Sstevel@tonic-gate 	while (err == FRU_SUCCESS) {
4177c478bd9Sstevel@tonic-gate 		uint32_t num_segs = 0;
4187c478bd9Sstevel@tonic-gate 		int rc = PICL_SUCCESS;
4197c478bd9Sstevel@tonic-gate 		picl_nodehdl_t seg_node;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		/* do this just in case the Segments have not been built. */
4227c478bd9Sstevel@tonic-gate 		if ((rc = picl_get_propval_by_name(sect_node,
4237c478bd9Sstevel@tonic-gate 			PICL_PROP_NUM_SEGMENTS,
4247c478bd9Sstevel@tonic-gate 			(void *)&num_segs,
4257c478bd9Sstevel@tonic-gate 			sizeof (num_segs))) != PICL_SUCCESS) {
4267c478bd9Sstevel@tonic-gate 			return (map_plugin_err(rc));
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		/* while there are segments. */
4307c478bd9Sstevel@tonic-gate 		rc = picl_get_propval_by_name(sect_node, PICL_PROP_CHILD,
4317c478bd9Sstevel@tonic-gate 			(void *)&seg_node, sizeof (seg_node));
4327c478bd9Sstevel@tonic-gate 		while (rc == PICL_SUCCESS) {
4337c478bd9Sstevel@tonic-gate 			char name[PICL_PROPNAMELEN_MAX];
4347c478bd9Sstevel@tonic-gate 			picl_get_propval_by_name(seg_node, PICL_PROP_NAME,
4357c478bd9Sstevel@tonic-gate 				name, sizeof (name));
4367c478bd9Sstevel@tonic-gate 			if (strcmp(segment, name) == 0) {
4377c478bd9Sstevel@tonic-gate 				int dummy = 0;
4387c478bd9Sstevel@tonic-gate 				int protection = 0;
4397c478bd9Sstevel@tonic-gate 				/* NUM_TAGS prop exists iff segment is OK */
4407c478bd9Sstevel@tonic-gate 				if ((ign_cor_flg == IGN_CORRUPT_NO) &&
4417c478bd9Sstevel@tonic-gate 					(picl_get_propval_by_name(seg_node,
4427c478bd9Sstevel@tonic-gate 					PICL_PROP_NUM_TAGS,
4437c478bd9Sstevel@tonic-gate 					(void *)&dummy,
4447c478bd9Sstevel@tonic-gate 					sizeof (dummy)) != PICL_SUCCESS)) {
4457c478bd9Sstevel@tonic-gate 					return (FRU_SEGCORRUPT);
4467c478bd9Sstevel@tonic-gate 				}
4477c478bd9Sstevel@tonic-gate 				/* get the HW protections of this section. */
448*35551380Skmohan 				if ((rc = picl_get_propval_by_name(sect_node,
4497c478bd9Sstevel@tonic-gate 					PICL_PROP_PROTECTED,
4507c478bd9Sstevel@tonic-gate 					(void *)&protection,
451*35551380Skmohan 					sizeof (protection)))
452*35551380Skmohan 							!= PICL_SUCCESS) {
453*35551380Skmohan 					return (map_plugin_err(rc));
4547c478bd9Sstevel@tonic-gate 				}
4557c478bd9Sstevel@tonic-gate 				hw_desc->all_bits = 0;
4567c478bd9Sstevel@tonic-gate 				hw_desc->field.read_only = protection;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 				*seg_hdl = seg_node;
4597c478bd9Sstevel@tonic-gate 				return (FRU_SUCCESS);
4607c478bd9Sstevel@tonic-gate 			}
4617c478bd9Sstevel@tonic-gate 			rc = picl_get_propval_by_name(seg_node, PICL_PROP_PEER,
4627c478bd9Sstevel@tonic-gate 				(void *)&seg_node, sizeof (seg_node));
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		/* Peer property not found is ok */
4667c478bd9Sstevel@tonic-gate 		if (rc != PICL_PROPNOTFOUND) {
467*35551380Skmohan 			return (map_plugin_err(rc));
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		err = find_next_section(sect_node, &sect_node);
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	return (FRU_INVALSEG);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /* ========================================================================= */
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * For the section handle passed add to list all the segment names found.
4797c478bd9Sstevel@tonic-gate  * Also incriments total by the number found.
4807c478bd9Sstevel@tonic-gate  */
4817c478bd9Sstevel@tonic-gate static fru_errno_t
add_segs_for_section(picl_nodehdl_t section,fru_strlist_t * list)4827c478bd9Sstevel@tonic-gate add_segs_for_section(picl_nodehdl_t section, fru_strlist_t *list)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	uint32_t num_segments = 0;
4857c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if ((rc = picl_get_propval_by_name(section,
4887c478bd9Sstevel@tonic-gate 		PICL_PROP_NUM_SEGMENTS,
4897c478bd9Sstevel@tonic-gate 		(void *)&num_segments,
4907c478bd9Sstevel@tonic-gate 		sizeof (num_segments))) != PICL_SUCCESS) {
4917c478bd9Sstevel@tonic-gate 		fru_destroy_strlist(list);
4927c478bd9Sstevel@tonic-gate 		return (map_plugin_err(rc));
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (num_segments != 0) {
4967c478bd9Sstevel@tonic-gate 		picl_nodehdl_t seg_node;
4977c478bd9Sstevel@tonic-gate 		int total_space = list->num + num_segments;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		list->strs = realloc(list->strs,
5007c478bd9Sstevel@tonic-gate 			(sizeof (*(list->strs)) * (total_space)));
5017c478bd9Sstevel@tonic-gate 		if (list->strs == NULL) {
5027c478bd9Sstevel@tonic-gate 			return (FRU_FAILURE);
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		/* get the first segment */
5067c478bd9Sstevel@tonic-gate 		rc = picl_get_propval_by_name(section,
5077c478bd9Sstevel@tonic-gate 			PICL_PROP_CHILD, (void *)&seg_node,
5087c478bd9Sstevel@tonic-gate 			sizeof (seg_node));
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		/* while there are more segments. */
5117c478bd9Sstevel@tonic-gate 		while (rc == PICL_SUCCESS) {
5127c478bd9Sstevel@tonic-gate 			char name[FRU_SEGNAMELEN +1];
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 			if ((rc = picl_get_propval_by_name(seg_node,
5157c478bd9Sstevel@tonic-gate 				PICL_PROP_NAME, name,
5167c478bd9Sstevel@tonic-gate 				sizeof (name))) != PICL_SUCCESS) {
5177c478bd9Sstevel@tonic-gate 				break;
5187c478bd9Sstevel@tonic-gate 			}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 			/* check array bounds */
5217c478bd9Sstevel@tonic-gate 			if (list->num >= total_space) {
5227c478bd9Sstevel@tonic-gate 				/* PICL reported incorrect number of segs */
5237c478bd9Sstevel@tonic-gate 				return (FRU_IOERROR);
5247c478bd9Sstevel@tonic-gate 			}
5257c478bd9Sstevel@tonic-gate 			list->strs[(list->num)++] = strdup(name);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			rc = picl_get_propval_by_name(seg_node,
5287c478bd9Sstevel@tonic-gate 				PICL_PROP_PEER, (void *)&seg_node,
5297c478bd9Sstevel@tonic-gate 				sizeof (seg_node));
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		/* Peer property not found is ok */
5337c478bd9Sstevel@tonic-gate 		if (rc != PICL_PROPNOTFOUND) {
534*35551380Skmohan 			return (map_plugin_err(rc));
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /* ========================================================================= */
5427c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_seg_list(fru_treehdl_t handle,fru_strlist_t * list)5437c478bd9Sstevel@tonic-gate fpt_get_seg_list(fru_treehdl_t handle, fru_strlist_t *list)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	fru_errno_t err;
5467c478bd9Sstevel@tonic-gate 	picl_nodehdl_t sect_node;
5477c478bd9Sstevel@tonic-gate 	fru_strlist_t rc_list;
5487c478bd9Sstevel@tonic-gate 	rc_list.num = 0;
5497c478bd9Sstevel@tonic-gate 	rc_list.strs = NULL;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if ((err = update_data_nodes(TREEHDL_TO_PICLHDL(handle)))
5527c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS) {
5537c478bd9Sstevel@tonic-gate 		return (err);
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &sect_node))
5577c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS) {
5587c478bd9Sstevel@tonic-gate 		return (err);
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	/* while there are sections. */
5627c478bd9Sstevel@tonic-gate 	while (err == FRU_SUCCESS) {
5637c478bd9Sstevel@tonic-gate 		if ((err = add_segs_for_section(sect_node, &rc_list))
5647c478bd9Sstevel@tonic-gate 				!= FRU_SUCCESS) {
5657c478bd9Sstevel@tonic-gate 			fru_destroy_strlist(&rc_list);
5667c478bd9Sstevel@tonic-gate 			return (err);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		err = find_next_section(sect_node, &sect_node);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	list->num = rc_list.num;
5727c478bd9Sstevel@tonic-gate 	list->strs = rc_list.strs;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /* ========================================================================= */
5787c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_seg_def(fru_treehdl_t handle,const char * seg_name,fru_segdef_t * def)5797c478bd9Sstevel@tonic-gate fpt_get_seg_def(fru_treehdl_t handle, const char *seg_name, fru_segdef_t *def)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
5827c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg_node;
5837c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	fru_segdesc_t desc;
5867c478bd9Sstevel@tonic-gate 	uint32_t size;
5877c478bd9Sstevel@tonic-gate 	uint32_t address;
5887c478bd9Sstevel@tonic-gate 	/* LINTED */
5897c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
5927c478bd9Sstevel@tonic-gate 		&seg_node, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS)
5937c478bd9Sstevel@tonic-gate 		return (err);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
5967c478bd9Sstevel@tonic-gate 		PICL_PROP_DESCRIPTOR,
5977c478bd9Sstevel@tonic-gate 		&desc, sizeof (desc))) != PICL_SUCCESS) {
598*35551380Skmohan 		return (map_plugin_err(picl_err));
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
6027c478bd9Sstevel@tonic-gate 		PICL_PROP_LENGTH,
6037c478bd9Sstevel@tonic-gate 		&size, sizeof (size))) != PICL_SUCCESS) {
604*35551380Skmohan 		return (map_plugin_err(picl_err));
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
6087c478bd9Sstevel@tonic-gate 		PICL_PROP_OFFSET,
6097c478bd9Sstevel@tonic-gate 		&address, sizeof (address))) != PICL_SUCCESS) {
610*35551380Skmohan 		return (map_plugin_err(picl_err));
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	def->version = LIBFRU_VERSION;
6147c478bd9Sstevel@tonic-gate 	strlcpy(def->name, seg_name, FRU_SEGNAMELEN+1);
6157c478bd9Sstevel@tonic-gate 	def->desc = desc;
6167c478bd9Sstevel@tonic-gate 	def->size = size;
6177c478bd9Sstevel@tonic-gate 	def->address = address;
6187c478bd9Sstevel@tonic-gate 	def->hw_desc = hw_desc;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /* ========================================================================= */
6247c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_add_seg(fru_treehdl_t handle,fru_segdef_t * def)6257c478bd9Sstevel@tonic-gate fpt_add_seg(fru_treehdl_t handle, fru_segdef_t *def)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
6287c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6297c478bd9Sstevel@tonic-gate 	picl_nodehdl_t section;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate  * for every section which has a ADD_SEGMENT_PROP try and add the segment
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate 	if ((err = find_first_section(TREEHDL_TO_PICLHDL(handle), &section))
6357c478bd9Sstevel@tonic-gate 		!= FRU_SUCCESS) {
6367c478bd9Sstevel@tonic-gate 		return (err);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	do {
6397c478bd9Sstevel@tonic-gate 		fru_segdef_t dummy;
6407c478bd9Sstevel@tonic-gate 		if ((picl_err = picl_get_propval_by_name(section,
6417c478bd9Sstevel@tonic-gate 			PICL_PROP_ADD_SEGMENT, &dummy, sizeof (dummy)))
6427c478bd9Sstevel@tonic-gate 				== PICL_SUCCESS) {
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 			picl_err = picl_set_propval_by_name(section,
6457c478bd9Sstevel@tonic-gate 				PICL_PROP_ADD_SEGMENT, def, sizeof (*def));
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 			return (map_plugin_err(picl_err));
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 	} while (find_next_section(section, &section) == FRU_SUCCESS);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /* ========================================================================= */
6557c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_delete_seg(fru_treehdl_t handle,const char * seg_name)6567c478bd9Sstevel@tonic-gate fpt_delete_seg(fru_treehdl_t handle, const char *seg_name)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg_hdl;
6597c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
6607c478bd9Sstevel@tonic-gate 	fru_errno_t err;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	int dead_flag = FRUDATA_DELETE_TAG_KEY;
6637c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
6667c478bd9Sstevel@tonic-gate 		&seg_hdl, &hw_desc, IGN_CORRUPT_YES)) != FRU_SUCCESS) {
6677c478bd9Sstevel@tonic-gate 		return (err);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	rc = picl_set_propval_by_name(seg_hdl, PICL_PROP_DELETE_SEGMENT,
6717c478bd9Sstevel@tonic-gate 		&dead_flag, sizeof (dead_flag));
6727c478bd9Sstevel@tonic-gate 	return (map_plugin_err(rc));
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate /* ========================================================================= */
6767c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_add_tag_to_seg(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,uint8_t * data,size_t data_len)6777c478bd9Sstevel@tonic-gate fpt_add_tag_to_seg(fru_treehdl_t handle, const char *seg_name,
6787c478bd9Sstevel@tonic-gate 		fru_tag_t tag, uint8_t *data, size_t data_len)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
6817c478bd9Sstevel@tonic-gate 	picl_nodehdl_t segHdl;
6827c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
6837c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
6847c478bd9Sstevel@tonic-gate 	size_t buf_size = 0;
6857c478bd9Sstevel@tonic-gate 	uint8_t *buffer = NULL;
6867c478bd9Sstevel@tonic-gate 	picl_prophdl_t add_prop;
6877c478bd9Sstevel@tonic-gate 	picl_propinfo_t add_prop_info;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
6907c478bd9Sstevel@tonic-gate 		&segHdl, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
6917c478bd9Sstevel@tonic-gate 		return (err);
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/* get the length of the buffer required. */
6957c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_prop_by_name(segHdl,
6967c478bd9Sstevel@tonic-gate 		PICL_PROP_ADD_PACKET,
6977c478bd9Sstevel@tonic-gate 		&add_prop)) != PICL_SUCCESS) {
698*35551380Skmohan 		return (map_plugin_err(picl_err));
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propinfo(add_prop, &add_prop_info))
7017c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) {
702*35551380Skmohan 		return (map_plugin_err(picl_err));
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	buf_size = add_prop_info.size;
7067c478bd9Sstevel@tonic-gate 	if (data_len >= (buf_size - get_tag_size(get_tag_type(&tag)))) {
7077c478bd9Sstevel@tonic-gate 		return (FRU_NOSPACE);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	buffer = malloc(buf_size);
7117c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
7127c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	/* write the tag and data into the buffer */
7157c478bd9Sstevel@tonic-gate 	memcpy(buffer, &tag, get_tag_size(get_tag_type(&tag)));
7167c478bd9Sstevel@tonic-gate 	memcpy((void *)(buffer+get_tag_size(get_tag_type(&tag))),
7177c478bd9Sstevel@tonic-gate 		data, data_len);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	picl_err = picl_set_propval(add_prop, buffer, buf_size);
7207c478bd9Sstevel@tonic-gate 	free(buffer);
7217c478bd9Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /* ========================================================================= */
7257c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_tag_list(fru_treehdl_t handle,const char * seg_name,fru_tag_t ** tags,int * number)7267c478bd9Sstevel@tonic-gate fpt_get_tag_list(fru_treehdl_t handle, const char *seg_name,
7277c478bd9Sstevel@tonic-gate 		fru_tag_t **tags, int *number)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg_node;
7307c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
7317c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
7327c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagTable;
7337c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
7347c478bd9Sstevel@tonic-gate 	unsigned int total_tags = 0;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	/* return variables */
7377c478bd9Sstevel@tonic-gate 	fru_tag_t *rc_tags = NULL;
7387c478bd9Sstevel@tonic-gate 	unsigned int rc_num = 0;
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), seg_name,
7417c478bd9Sstevel@tonic-gate 		&seg_node, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
7427c478bd9Sstevel@tonic-gate 		return (err);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	/* get the number of tags and allocate array for them */
7467c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
7477c478bd9Sstevel@tonic-gate 		PICL_PROP_NUM_TAGS,
7487c478bd9Sstevel@tonic-gate 		(void *)&total_tags,
7497c478bd9Sstevel@tonic-gate 		sizeof (total_tags))) != PICL_SUCCESS) {
750*35551380Skmohan 		return (map_plugin_err(picl_err));
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (total_tags == 0) {
7547c478bd9Sstevel@tonic-gate 		*tags = rc_tags;
7557c478bd9Sstevel@tonic-gate 		*number = rc_num;
7567c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	rc_tags = malloc((sizeof (*rc_tags) * total_tags));
7607c478bd9Sstevel@tonic-gate 	if (rc_tags == NULL) {
7617c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/* go through the tagTable and fill in the array */
7657c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(seg_node,
7667c478bd9Sstevel@tonic-gate 		PICL_PROP_PACKET_TABLE,
7677c478bd9Sstevel@tonic-gate 		&tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
7687c478bd9Sstevel@tonic-gate 		free(rc_tags);
769*35551380Skmohan 		return (map_plugin_err(picl_err));
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 	picl_err = picl_get_next_by_col(tagTable, &tagTable);
7727c478bd9Sstevel@tonic-gate 	while (picl_err == PICL_SUCCESS) {
7737c478bd9Sstevel@tonic-gate 		/* check array bounds */
7747c478bd9Sstevel@tonic-gate 		if (rc_num >= total_tags) {
7757c478bd9Sstevel@tonic-gate 			free(rc_tags);
7767c478bd9Sstevel@tonic-gate 			return (FRU_FAILURE);
7777c478bd9Sstevel@tonic-gate 		}
7787c478bd9Sstevel@tonic-gate 		/* fill in the array */
7797c478bd9Sstevel@tonic-gate 		if ((picl_err = picl_get_propval(tagTable,
7807c478bd9Sstevel@tonic-gate 			(void *)&(rc_tags[rc_num++]),
7817c478bd9Sstevel@tonic-gate 			sizeof (fru_tag_t))) != PICL_SUCCESS) {
7827c478bd9Sstevel@tonic-gate 			free(rc_tags);
783*35551380Skmohan 			return (map_plugin_err(picl_err));
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		/* get the next tag */
7867c478bd9Sstevel@tonic-gate 		picl_err = picl_get_next_by_col(tagTable, &tagTable);
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	if (picl_err == PICL_ENDOFLIST) {
7907c478bd9Sstevel@tonic-gate 		*tags = rc_tags;
7917c478bd9Sstevel@tonic-gate 		*number = rc_num;
7927c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
7937c478bd9Sstevel@tonic-gate 	}
794*35551380Skmohan 	return (map_plugin_err(picl_err));
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate /* ========================================================================= */
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate  * From the handle, segment name, tag, and instance of the tag get me:
8007c478bd9Sstevel@tonic-gate  * segHdl: The segment handle for this segment.
8017c478bd9Sstevel@tonic-gate  * tagHdl: tag property handle in the tag table for this instance "tag"
8027c478bd9Sstevel@tonic-gate  */
8037c478bd9Sstevel@tonic-gate static fru_errno_t
get_tag_handle(picl_nodehdl_t handle,const char * segment,fru_tag_t tag,int instance,picl_nodehdl_t * segHdl,picl_prophdl_t * tagHdl)8047c478bd9Sstevel@tonic-gate get_tag_handle(picl_nodehdl_t handle, const char *segment,
8057c478bd9Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8067c478bd9Sstevel@tonic-gate 		picl_nodehdl_t *segHdl,
8077c478bd9Sstevel@tonic-gate 		picl_prophdl_t *tagHdl)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	fru_seg_hwdesc_t hw_desc;
8107c478bd9Sstevel@tonic-gate 	fru_errno_t err;
8117c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagTable = 0;
8127c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8137c478bd9Sstevel@tonic-gate 	picl_nodehdl_t tmp_seg;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	fru_tag_t foundTag;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if ((err = get_segment_node(TREEHDL_TO_PICLHDL(handle), segment,
8187c478bd9Sstevel@tonic-gate 		&tmp_seg, &hw_desc, IGN_CORRUPT_NO)) != FRU_SUCCESS) {
8197c478bd9Sstevel@tonic-gate 		return (err);
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	foundTag.raw_data = 0;
8237c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval_by_name(tmp_seg,
8247c478bd9Sstevel@tonic-gate 		PICL_PROP_PACKET_TABLE,
8257c478bd9Sstevel@tonic-gate 		&tagTable, sizeof (tagTable))) != PICL_SUCCESS) {
826*35551380Skmohan 		return (map_plugin_err(picl_err));
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	picl_err = picl_get_next_by_col(tagTable, &tagTable);
8307c478bd9Sstevel@tonic-gate 	while ((picl_err != PICL_ENDOFLIST) &&
8317c478bd9Sstevel@tonic-gate 		(picl_err == PICL_SUCCESS)) {
8327c478bd9Sstevel@tonic-gate 		if ((picl_err = picl_get_propval(tagTable, (void *)&foundTag,
8337c478bd9Sstevel@tonic-gate 			sizeof (foundTag))) != PICL_SUCCESS) {
834*35551380Skmohan 			return (map_plugin_err(picl_err));
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		if ((tags_equal(tag, foundTag) == 1) && (instance-- == 0)) {
8377c478bd9Sstevel@tonic-gate 			*segHdl = tmp_seg;
8387c478bd9Sstevel@tonic-gate 			*tagHdl = tagTable;
8397c478bd9Sstevel@tonic-gate 			return (FRU_SUCCESS);
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 		picl_err = picl_get_next_by_col(tagTable, &tagTable);
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
844*35551380Skmohan 	return (map_plugin_err(picl_err));
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /* ========================================================================= */
8487c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_tag_data(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance,uint8_t ** data,size_t * data_len)8497c478bd9Sstevel@tonic-gate fpt_get_tag_data(fru_treehdl_t handle, const char *seg_name,
8507c478bd9Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8517c478bd9Sstevel@tonic-gate 		uint8_t **data, size_t *data_len)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	fru_errno_t err = FRU_SUCCESS;
8547c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8557c478bd9Sstevel@tonic-gate 	uint8_t *buffer;
8567c478bd9Sstevel@tonic-gate 	int buf_len = 0;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg;
8597c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if ((err = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
8627c478bd9Sstevel@tonic-gate 		tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
8637c478bd9Sstevel@tonic-gate 		return (err);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_next_by_row(tagHdl, &tagHdl))
8677c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
868*35551380Skmohan 		return (map_plugin_err(picl_err));
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	buf_len = get_payload_length(&tag);
8727c478bd9Sstevel@tonic-gate 	buffer = malloc(buf_len);
8737c478bd9Sstevel@tonic-gate 	if (buffer == NULL) {
8747c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_propval(tagHdl, buffer, buf_len))
8787c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
8797c478bd9Sstevel@tonic-gate 		free(buffer);
8807c478bd9Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	*data = buffer;
8847c478bd9Sstevel@tonic-gate 	*data_len = buf_len;
8857c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate /* ========================================================================= */
8897c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_set_tag_data(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance,uint8_t * data,size_t data_len)8907c478bd9Sstevel@tonic-gate fpt_set_tag_data(fru_treehdl_t handle, const char *seg_name,
8917c478bd9Sstevel@tonic-gate 		fru_tag_t tag, int instance,
8927c478bd9Sstevel@tonic-gate 		uint8_t *data, size_t data_len)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	fru_errno_t rc = FRU_SUCCESS;
8957c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	picl_nodehdl_t seg;
8987c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
9017c478bd9Sstevel@tonic-gate 		tag, instance, &seg, &tagHdl)) != FRU_SUCCESS) {
9027c478bd9Sstevel@tonic-gate 		return (rc);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_get_next_by_row(tagHdl, &tagHdl))
9067c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
907*35551380Skmohan 		return (map_plugin_err(picl_err));
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	if ((picl_err = picl_set_propval(tagHdl, data, data_len))
9117c478bd9Sstevel@tonic-gate 		!= PICL_SUCCESS) {
9127c478bd9Sstevel@tonic-gate 		return (map_plugin_err(picl_err));
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate /* ========================================================================= */
9197c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_delete_tag(fru_treehdl_t handle,const char * seg_name,fru_tag_t tag,int instance)9207c478bd9Sstevel@tonic-gate fpt_delete_tag(fru_treehdl_t handle, const char *seg_name, fru_tag_t tag,
9217c478bd9Sstevel@tonic-gate 		int instance)
9227c478bd9Sstevel@tonic-gate {
9237c478bd9Sstevel@tonic-gate 	fru_errno_t rc = FRU_SUCCESS;
9247c478bd9Sstevel@tonic-gate 	int picl_err = PICL_SUCCESS;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	picl_nodehdl_t segHdl;
9277c478bd9Sstevel@tonic-gate 	picl_prophdl_t tagHdl;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/* get tag handle */
9307c478bd9Sstevel@tonic-gate 	if ((rc = get_tag_handle(TREEHDL_TO_PICLHDL(handle), seg_name,
9317c478bd9Sstevel@tonic-gate 		tag, instance, &segHdl, &tagHdl)) != FRU_SUCCESS) {
9327c478bd9Sstevel@tonic-gate 		return (rc);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/* set up key */
9367c478bd9Sstevel@tonic-gate 	tag.raw_data &= FRUDATA_DELETE_TAG_MASK;
9377c478bd9Sstevel@tonic-gate 	tag.raw_data |= FRUDATA_DELETE_TAG_KEY;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	/* Write back */
9407c478bd9Sstevel@tonic-gate 	picl_err = picl_set_propval(tagHdl, (void *)&(tag.raw_data),
9417c478bd9Sstevel@tonic-gate 		sizeof (tag.raw_data));
9427c478bd9Sstevel@tonic-gate 	return (map_plugin_err(picl_err));
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate /* ========================================================================= */
9467c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_for_each_segment(fru_treehdl_t treenode,int (* function)(fru_treeseghdl_t segment,void * args),void * args)9477c478bd9Sstevel@tonic-gate fpt_for_each_segment(fru_treehdl_t treenode,
9487c478bd9Sstevel@tonic-gate 			int (*function)(fru_treeseghdl_t segment, void *args),
9497c478bd9Sstevel@tonic-gate 			void *args)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	int		num_segments = 0, status;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	fru_errno_t	saved_status = FRU_SUCCESS;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	container = TREEHDL_TO_PICLHDL(treenode),
9567c478bd9Sstevel@tonic-gate 			section, segment;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	if ((status = update_data_nodes(container)) != FRU_SUCCESS)
9607c478bd9Sstevel@tonic-gate 		return (status);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	/* process each section */
9637c478bd9Sstevel@tonic-gate 	for (status = picl_get_propval_by_name(container, PICL_PROP_CHILD,
9647c478bd9Sstevel@tonic-gate 						&section, sizeof (section));
9657c478bd9Sstevel@tonic-gate 		status == PICL_SUCCESS;
9667c478bd9Sstevel@tonic-gate 		status = picl_get_propval_by_name(section, PICL_PROP_PEER,
9677c478bd9Sstevel@tonic-gate 							&section,
9687c478bd9Sstevel@tonic-gate 							sizeof (section))) {
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 		if (cmp_class_name(section, PICL_CLASS_SECTION) != FRU_SUCCESS)
9717c478bd9Sstevel@tonic-gate 			continue;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 		if ((status = picl_get_propval_by_name(section,
9747c478bd9Sstevel@tonic-gate 							PICL_PROP_NUM_SEGMENTS,
9757c478bd9Sstevel@tonic-gate 							&num_segments,
9767c478bd9Sstevel@tonic-gate 							sizeof (num_segments)))
9777c478bd9Sstevel@tonic-gate 		    == PICL_PROPNOTFOUND) {
9787c478bd9Sstevel@tonic-gate 			continue;
9797c478bd9Sstevel@tonic-gate 		} else if (status != PICL_SUCCESS) {
9807c478bd9Sstevel@tonic-gate 			saved_status = map_plugin_err(status);
9817c478bd9Sstevel@tonic-gate 			continue;
9827c478bd9Sstevel@tonic-gate 		} else if (num_segments == 0) {
9837c478bd9Sstevel@tonic-gate 			continue;
9847c478bd9Sstevel@tonic-gate 		}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		/* process each segment */
9877c478bd9Sstevel@tonic-gate 		for (status = picl_get_propval_by_name(section,
9887c478bd9Sstevel@tonic-gate 							PICL_PROP_CHILD,
9897c478bd9Sstevel@tonic-gate 							&segment,
9907c478bd9Sstevel@tonic-gate 							sizeof (segment));
9917c478bd9Sstevel@tonic-gate 			status == PICL_SUCCESS;
9927c478bd9Sstevel@tonic-gate 			status = picl_get_propval_by_name(segment,
9937c478bd9Sstevel@tonic-gate 							PICL_PROP_PEER,
9947c478bd9Sstevel@tonic-gate 							&segment,
9957c478bd9Sstevel@tonic-gate 							sizeof (segment))) {
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 			if (cmp_class_name(segment, PICL_CLASS_SEGMENT)
9987c478bd9Sstevel@tonic-gate 			    != FRU_SUCCESS) continue;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 			if ((status = function(PICLHDL_TO_TREESEGHDL(segment),
10017c478bd9Sstevel@tonic-gate 						args))
10027c478bd9Sstevel@tonic-gate 			    != FRU_SUCCESS) return (status);
10037c478bd9Sstevel@tonic-gate 		}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		if (status != PICL_PROPNOTFOUND)
10067c478bd9Sstevel@tonic-gate 			saved_status = map_plugin_err(status);
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (status != PICL_PROPNOTFOUND)
10107c478bd9Sstevel@tonic-gate 		saved_status = map_plugin_err(status);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	return (saved_status);
10137c478bd9Sstevel@tonic-gate }
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate /* ========================================================================= */
10167c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_get_segment_name(fru_treeseghdl_t segment,char ** name)10177c478bd9Sstevel@tonic-gate fpt_get_segment_name(fru_treeseghdl_t segment, char **name)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	char		*propval;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	int		status;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph = 0;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	picl_propinfo_t	propinfo;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 
1028*35551380Skmohan 	if ((status = picl_get_propinfo_by_name(TREESEGHDL_TO_PICLHDL(segment),
1029*35551380Skmohan 		PICL_PROP_NAME, &propinfo, &proph))
10307c478bd9Sstevel@tonic-gate 	    != PICL_SUCCESS)
1031*35551380Skmohan 		return (map_plugin_err(status));
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (propinfo.size == 0)
10347c478bd9Sstevel@tonic-gate 		return (FRU_INVALDATASIZE);
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if ((propval = malloc(propinfo.size)) == NULL)
10377c478bd9Sstevel@tonic-gate 		return (FRU_NOSPACE);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if ((status = picl_get_propval(proph, propval, propinfo.size))
10407c478bd9Sstevel@tonic-gate 	    != PICL_SUCCESS) {
10417c478bd9Sstevel@tonic-gate 		free(propval);
10427c478bd9Sstevel@tonic-gate 		return (map_plugin_err(status));
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	*name = propval;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate /* ========================================================================= */
10517c478bd9Sstevel@tonic-gate static fru_errno_t
fpt_for_each_packet(fru_treeseghdl_t treesegment,int (* function)(fru_tag_t * tag,uint8_t * payload,size_t length,void * args),void * args)10527c478bd9Sstevel@tonic-gate fpt_for_each_packet(fru_treeseghdl_t treesegment,
10537c478bd9Sstevel@tonic-gate 			int (*function)(fru_tag_t *tag, uint8_t *payload,
10547c478bd9Sstevel@tonic-gate 					size_t length,
10557c478bd9Sstevel@tonic-gate 			void *args),
10567c478bd9Sstevel@tonic-gate     void *args)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	int		status;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	uint8_t		*payload;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	segment = TREESEGHDL_TO_PICLHDL(treesegment);
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	picl_prophdl_t	packet, payloadh = 0;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	picl_propinfo_t	propinfo;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	fru_segdesc_t	descriptor;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	fru_tag_t	tag;
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	if ((status = picl_get_propval_by_name(segment, PICL_PROP_DESCRIPTOR,
10747c478bd9Sstevel@tonic-gate 						&descriptor,
10757c478bd9Sstevel@tonic-gate 						sizeof (descriptor)))
10767c478bd9Sstevel@tonic-gate 	    != PICL_SUCCESS) return (map_plugin_err(status));
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if (descriptor.field.opaque)
10797c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (descriptor.field.encrypted && (encrypt_func == NULL))
10827c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if ((status = picl_get_propval_by_name(segment, PICL_PROP_PACKET_TABLE,
10857c478bd9Sstevel@tonic-gate 						&packet, sizeof (packet)))
10867c478bd9Sstevel@tonic-gate 	    == PICL_PROPNOTFOUND)
10877c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
10887c478bd9Sstevel@tonic-gate 	else if (status != PICL_SUCCESS)
10897c478bd9Sstevel@tonic-gate 		return (map_plugin_err(status));
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	while ((status = picl_get_next_by_col(packet, &packet))
10927c478bd9Sstevel@tonic-gate 		== PICL_SUCCESS) {
10937c478bd9Sstevel@tonic-gate 		if (((status = picl_get_propval(packet, &tag, sizeof (tag)))
10947c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) ||
10957c478bd9Sstevel@tonic-gate 		    ((status = picl_get_next_by_row(packet, &payloadh))
10967c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS) ||
10977c478bd9Sstevel@tonic-gate 		    ((status = picl_get_propinfo(payloadh, &propinfo))
10987c478bd9Sstevel@tonic-gate 			!= PICL_SUCCESS))
10997c478bd9Sstevel@tonic-gate 			return (map_plugin_err(status));
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 		if (propinfo.size > 0) {
11027c478bd9Sstevel@tonic-gate 			payload = alloca(propinfo.size);
11037c478bd9Sstevel@tonic-gate 			if ((status = picl_get_propval(payloadh, payload,
11047c478bd9Sstevel@tonic-gate 							propinfo.size))
11057c478bd9Sstevel@tonic-gate 			    != PICL_SUCCESS) return (map_plugin_err(status));
11067c478bd9Sstevel@tonic-gate 		} else {
11077c478bd9Sstevel@tonic-gate 			payload = NULL;
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 		if ((descriptor.field.encrypted) &&
11117c478bd9Sstevel@tonic-gate 		    ((status = encrypt_func(FRU_DECRYPT, payload,
11127c478bd9Sstevel@tonic-gate 						propinfo.size))
11137c478bd9Sstevel@tonic-gate 			!= FRU_SUCCESS)) return status;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		if ((status = function(&tag, payload, propinfo.size, args))
11167c478bd9Sstevel@tonic-gate 		    != FRU_SUCCESS) return (status);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (status == PICL_ENDOFLIST)
11207c478bd9Sstevel@tonic-gate 		return (FRU_SUCCESS);
11217c478bd9Sstevel@tonic-gate 	else
11227c478bd9Sstevel@tonic-gate 		return (map_plugin_err(status));
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate /* ========================================================================= */
11267c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
11277c478bd9Sstevel@tonic-gate static fru_errno_t
initialize(int argc,char ** argv)11287c478bd9Sstevel@tonic-gate initialize(int argc, char **argv)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	/* LINTED */
11317c478bd9Sstevel@tonic-gate 	int rc = PICL_SUCCESS;
11327c478bd9Sstevel@tonic-gate 	if ((rc = picl_initialize()) != PICL_SUCCESS) {
11337c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate /* ========================================================================= */
11407c478bd9Sstevel@tonic-gate static fru_errno_t
shutdown(void)11417c478bd9Sstevel@tonic-gate shutdown(void)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	if (picl_shutdown() != PICL_SUCCESS) {
11447c478bd9Sstevel@tonic-gate 		return (FRU_FAILURE);
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate 	return (FRU_SUCCESS);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /* ========================================================================= */
11507c478bd9Sstevel@tonic-gate /* object for libfru to link to */
11517c478bd9Sstevel@tonic-gate fru_datasource_t data_source =
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	LIBFRU_DS_VER,
11547c478bd9Sstevel@tonic-gate 	initialize,
11557c478bd9Sstevel@tonic-gate 	shutdown,
11567c478bd9Sstevel@tonic-gate 	fpt_get_root,
11577c478bd9Sstevel@tonic-gate 	fpt_get_child,
11587c478bd9Sstevel@tonic-gate 	fpt_get_peer,
11597c478bd9Sstevel@tonic-gate 	fpt_get_parent,
11607c478bd9Sstevel@tonic-gate 	fpt_get_name_from_hdl,
11617c478bd9Sstevel@tonic-gate 	fpt_get_node_type,
11627c478bd9Sstevel@tonic-gate 	fpt_get_seg_list,
11637c478bd9Sstevel@tonic-gate 	fpt_get_seg_def,
11647c478bd9Sstevel@tonic-gate 	fpt_add_seg,
11657c478bd9Sstevel@tonic-gate 	fpt_delete_seg,
11667c478bd9Sstevel@tonic-gate 	fpt_for_each_segment,
11677c478bd9Sstevel@tonic-gate 	fpt_get_segment_name,
11687c478bd9Sstevel@tonic-gate 	fpt_add_tag_to_seg,
11697c478bd9Sstevel@tonic-gate 	fpt_get_tag_list,
11707c478bd9Sstevel@tonic-gate 	fpt_get_tag_data,
11717c478bd9Sstevel@tonic-gate 	fpt_set_tag_data,
11727c478bd9Sstevel@tonic-gate 	fpt_delete_tag,
11737c478bd9Sstevel@tonic-gate 	fpt_for_each_packet
11747c478bd9Sstevel@tonic-gate };
1175