xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_protocol.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <assert.h>
29 #include <pthread.h>
30 #include <strings.h>
31 #include <sys/fm/protocol.h>
32 
33 #include <topo_alloc.h>
34 #include <topo_error.h>
35 #include <topo_method.h>
36 #include <topo_prop.h>
37 #include <topo_protocol.h>
38 #include <topo_subr.h>
39 
40 #include <libtopo.h>
41 
42 int
43 topo_node_asru(tnode_t *node, nvlist_t **asru, nvlist_t *priv, int *err)
44 {
45 	nvlist_t *prop, *ap;
46 
47 	if (topo_prop_getprop(node, TOPO_PGROUP_PROTOCOL,
48 	    TOPO_PROP_ASRU, priv, &prop, err) < 0)
49 		return (-1);
50 
51 	if (nvlist_lookup_nvlist(prop, TOPO_PROP_VAL_VAL, &ap) != 0 ||
52 	    topo_hdl_nvdup(node->tn_hdl, ap, asru) < 0) {
53 		*err = ETOPO_PROP_NVL;
54 		nvlist_free(prop);
55 		return (-1);
56 	}
57 
58 	nvlist_free(prop);
59 
60 	return (0);
61 }
62 
63 int
64 topo_node_fru(tnode_t *node, nvlist_t **fru, nvlist_t *priv, int *err)
65 {
66 	nvlist_t *prop, *fp;
67 
68 	if (topo_prop_getprop(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_FRU,
69 	    priv, &prop, err) < 0)
70 		return (-1);
71 
72 	if (nvlist_lookup_nvlist(prop, TOPO_PROP_VAL_VAL, &fp) != 0 ||
73 	    topo_hdl_nvdup(node->tn_hdl, fp, fru) < 0) {
74 		*err = ETOPO_PROP_NVL;
75 		nvlist_free(prop);
76 		return (-1);
77 	}
78 
79 	nvlist_free(prop);
80 
81 	return (0);
82 }
83 
84 int
85 topo_node_resource(tnode_t *node, nvlist_t **resource, int *err)
86 {
87 
88 	return (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL,
89 	    TOPO_PROP_RESOURCE, resource, err));
90 }
91 
92 int
93 topo_node_label(tnode_t *node, char **label, int *err)
94 {
95 
96 	return (topo_prop_get_string(node, TOPO_PGROUP_PROTOCOL,
97 	    TOPO_PROP_LABEL, label, err));
98 }
99 
100 int
101 topo_node_asru_set(tnode_t *node, nvlist_t *asru, int flag, int *err)
102 {
103 	/*
104 	 * Inherit ASRU property from our parent if asru not specified
105 	 */
106 	if (asru == NULL) {
107 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL,
108 		    TOPO_PROP_ASRU, err) < 0) {
109 			return (-1);
110 		}
111 
112 		return (0);
113 	}
114 
115 	if (flag & TOPO_ASRU_COMPUTE) {
116 		if (topo_prop_method_register(node, TOPO_PGROUP_PROTOCOL,
117 		    TOPO_PROP_ASRU, TOPO_TYPE_FMRI, TOPO_METH_ASRU_COMPUTE,
118 		    asru, err) < 0)
119 			return (-1);
120 	} else {
121 		if (topo_prop_set_fmri(node, TOPO_PGROUP_PROTOCOL,
122 		    TOPO_PROP_ASRU, TOPO_PROP_IMMUTABLE, asru, err) < 0)
123 			return (-1);
124 	}
125 
126 	return (0);
127 }
128 
129 int
130 topo_node_fru_set(tnode_t *node, nvlist_t *fru, int flag, int *err)
131 {
132 
133 	/*
134 	 * Inherit FRU property from our parent if * not specified
135 	 */
136 	if (fru == NULL) {
137 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_FRU,
138 		    err) < 0) {
139 			return (-1);
140 		}
141 	}
142 
143 	if (flag & TOPO_FRU_COMPUTE) {
144 		if (topo_prop_method_register(node, TOPO_PGROUP_PROTOCOL,
145 		    TOPO_PROP_FRU, TOPO_TYPE_FMRI, TOPO_METH_FRU_COMPUTE,
146 		    fru, err) < 0)
147 			return (-1);
148 	} else {
149 		if (topo_prop_set_fmri(node, TOPO_PGROUP_PROTOCOL,
150 		    TOPO_PROP_FRU, TOPO_PROP_IMMUTABLE, fru, err) < 0)
151 			return (-1);
152 	}
153 
154 
155 	return (0);
156 }
157 
158 int
159 topo_node_label_set(tnode_t *node, char *label, int *err)
160 {
161 
162 	/*
163 	 * Inherit FRU property from our parent if * not specified
164 	 */
165 	if (label == NULL) {
166 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL,
167 		    TOPO_PROP_LABEL, err) < 0) {
168 			return (-1);
169 		}
170 	} else {
171 		if (topo_prop_set_string(node, TOPO_PGROUP_PROTOCOL,
172 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label, err) < 0)
173 			return (-1);
174 	}
175 
176 	return (0);
177 }
178