1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #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_protocol.h>
36 #include <topo_subr.h>
37 
38 #include <libtopo.h>
39 
40 static int
41 topo_compute(tnode_t *node, nvlist_t *stub, const char *method,
42     topo_version_t version, nvlist_t *rsrc, nvlist_t **asru, int *err)
43 {
44 	int rc;
45 	char *scheme;
46 	topo_hdl_t *thp = node->tn_hdl;
47 	tnode_t *rnode;
48 
49 	/*
50 	 * First try the originating enumerator for
51 	 * a compute method.  If none is supported, try the
52 	 * node's scheme-specific enumerator.
53 	 */
54 	if (topo_method_invoke(node, method, version, rsrc, asru, err) == 0)
55 		return (0);
56 
57 	if (*err != ETOPO_METHOD_NOTSUP)
58 		return (-1);
59 
60 	if ((rc = nvlist_lookup_string(stub, FM_FMRI_SCHEME, &scheme)) != 0) {
61 		if (rc == ENOENT) {
62 			*err = ETOPO_FMRI_MALFORM;
63 		} else {
64 			*err = ETOPO_FMRI_NVL;
65 		}
66 		return (-1);
67 	}
68 
69 	if ((rnode = topo_hdl_root(thp, scheme)) == NULL) {
70 		*err = ETOPO_METHOD_NOTSUP;
71 		return (-1);
72 	}
73 
74 	if (topo_method_invoke(rnode, method, version, rsrc, asru, err) != 0)
75 		return (-1);
76 
77 	return (0);
78 }
79 
80 int
81 topo_node_asru(tnode_t *node, nvlist_t **asru, nvlist_t *priv, int *err)
82 {
83 	int rc;
84 	nvlist_t *ap;
85 
86 	if (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_ASRU, &ap,
87 	    err) != 0)
88 		return (-1);
89 
90 	if (node->tn_fflags & TOPO_ASRU_COMPUTE) {
91 		rc = topo_compute(node, ap, TOPO_METH_ASRU_COMPUTE,
92 		    TOPO_METH_ASRU_COMPUTE_VERSION, priv, asru, err);
93 		nvlist_free(ap);
94 		return (rc);
95 	} else {
96 		*asru = ap;
97 		return (0);
98 	}
99 }
100 
101 int
102 topo_node_fru(tnode_t *node, nvlist_t **fru, nvlist_t *priv, int *err)
103 {
104 	int rc;
105 	nvlist_t *fp;
106 
107 	if (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_FRU, &fp,
108 	    err) != 0)
109 		return (-1);
110 
111 	if (node->tn_fflags & TOPO_FRU_COMPUTE) {
112 		rc = topo_compute(node, fp, TOPO_METH_FRU_COMPUTE,
113 		    TOPO_METH_FRU_COMPUTE_VERSION, priv, fru, err);
114 		nvlist_free(fp);
115 		return (rc);
116 	} else {
117 		*fru = fp;
118 		return (0);
119 	}
120 }
121 
122 int
123 topo_node_resource(tnode_t *node, nvlist_t **resource, int *err)
124 {
125 
126 	return (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL,
127 	    TOPO_PROP_RESOURCE, resource, err));
128 }
129 
130 int
131 topo_node_label(tnode_t *node, char **label, int *err)
132 {
133 
134 	return (topo_prop_get_string(node, TOPO_PGROUP_PROTOCOL,
135 	    TOPO_PROP_LABEL, label, err));
136 }
137 
138 int
139 topo_node_asru_set(tnode_t *node, nvlist_t *asru, int flag, int *err)
140 {
141 
142 	/*
143 	 * Inherit ASRU property from our parent if not specified
144 	 */
145 	if (asru == NULL) {
146 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL,
147 		    TOPO_PROP_ASRU, err) < 0) {
148 			return (-1);
149 		}
150 	} else {
151 		/*
152 		 * ASRU must be computed on the fly.  asru will
153 		 * contain the scheme module to call for the
154 		 * computation
155 		 */
156 		if (flag & TOPO_ASRU_COMPUTE)
157 			node->tn_fflags |= TOPO_ASRU_COMPUTE;
158 
159 		if (topo_prop_set_fmri(node, TOPO_PGROUP_PROTOCOL,
160 		    TOPO_PROP_ASRU, TOPO_PROP_SET_ONCE, asru, err) < 0)
161 			return (-1);
162 	}
163 
164 	return (0);
165 }
166 
167 int
168 topo_node_fru_set(tnode_t *node, nvlist_t *fru, int flag, int *err)
169 {
170 
171 	/*
172 	 * Inherit FRU property from our parent if * not specified
173 	 */
174 	if (fru == NULL) {
175 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_FRU,
176 		    err) < 0) {
177 			return (-1);
178 		}
179 	} else {
180 		/*
181 		 * FRU must be computed on the fly
182 		 */
183 		if (flag & TOPO_FRU_COMPUTE)
184 			node->tn_fflags |= TOPO_FRU_COMPUTE;
185 
186 		if (topo_prop_set_fmri(node, TOPO_PGROUP_PROTOCOL,
187 		    TOPO_PROP_FRU, TOPO_PROP_SET_ONCE, fru, err) < 0)
188 			return (-1);
189 	}
190 
191 	return (0);
192 }
193 
194 int
195 topo_node_label_set(tnode_t *node, char *label, int *err)
196 {
197 	/*
198 	 * Inherit FRU property from our parent if * not specified
199 	 */
200 	if (label == NULL) {
201 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL,
202 		    TOPO_PROP_LABEL, err) < 0) {
203 			return (-1);
204 		}
205 	} else {
206 		if (topo_prop_set_string(node, TOPO_PGROUP_PROTOCOL,
207 		    TOPO_PROP_LABEL, TOPO_PROP_SET_ONCE, label, err) < 0)
208 			return (-1);
209 	}
210 
211 	return (0);
212 }
213