xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_protocol.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
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  * Copyright (c) 2018, Joyent, Inc.
25  */
26 
27 #include <assert.h>
28 #include <pthread.h>
29 #include <strings.h>
30 #include <sys/fm/protocol.h>
31 
32 #include <topo_alloc.h>
33 #include <topo_error.h>
34 #include <topo_method.h>
35 #include <topo_prop.h>
36 #include <topo_protocol.h>
37 #include <topo_subr.h>
38 
39 #include <libtopo.h>
40 
41 int
42 topo_node_asru(tnode_t *node, nvlist_t **asru, nvlist_t *priv, int *err)
43 {
44 	nvlist_t *prop, *ap;
45 
46 	if (topo_prop_getprop(node, TOPO_PGROUP_PROTOCOL,
47 	    TOPO_PROP_ASRU, priv, &prop, err) < 0)
48 		return (-1);
49 
50 	if (nvlist_lookup_nvlist(prop, TOPO_PROP_VAL_VAL, &ap) != 0 ||
51 	    topo_hdl_nvdup(node->tn_hdl, ap, asru) < 0) {
52 		*err = ETOPO_PROP_NVL;
53 		nvlist_free(prop);
54 		return (-1);
55 	}
56 
57 	nvlist_free(prop);
58 
59 	return (0);
60 }
61 
62 int
63 topo_node_fru(tnode_t *node, nvlist_t **fru, nvlist_t *priv, int *err)
64 {
65 	nvlist_t *prop, *fp;
66 
67 	if (topo_prop_getprop(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_FRU,
68 	    priv, &prop, err) < 0)
69 		return (-1);
70 
71 	if (nvlist_lookup_nvlist(prop, TOPO_PROP_VAL_VAL, &fp) != 0 ||
72 	    topo_hdl_nvdup(node->tn_hdl, fp, fru) < 0) {
73 		*err = ETOPO_PROP_NVL;
74 		nvlist_free(prop);
75 		return (-1);
76 	}
77 
78 	nvlist_free(prop);
79 
80 	return (0);
81 }
82 
83 int
84 topo_node_resource(tnode_t *node, nvlist_t **resource, int *err)
85 {
86 
87 	return (topo_prop_get_fmri(node, TOPO_PGROUP_PROTOCOL,
88 	    TOPO_PROP_RESOURCE, resource, err));
89 }
90 
91 int
92 topo_node_label(tnode_t *node, char **label, int *err)
93 {
94 
95 	return (topo_prop_get_string(node, TOPO_PGROUP_PROTOCOL,
96 	    TOPO_PROP_LABEL, label, err));
97 }
98 
99 int
100 topo_node_asru_set(tnode_t *node, nvlist_t *asru, int flag, int *err)
101 {
102 	/*
103 	 * Inherit ASRU property from our parent if asru not specified
104 	 */
105 	if (asru == NULL) {
106 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL,
107 		    TOPO_PROP_ASRU, err) < 0) {
108 			return (-1);
109 		}
110 
111 		return (0);
112 	}
113 
114 	if (flag & TOPO_ASRU_COMPUTE) {
115 		if (topo_prop_method_register(node, TOPO_PGROUP_PROTOCOL,
116 		    TOPO_PROP_ASRU, TOPO_TYPE_FMRI, TOPO_METH_ASRU_COMPUTE,
117 		    asru, err) < 0)
118 			return (-1);
119 	} else {
120 		if (topo_prop_set_fmri(node, TOPO_PGROUP_PROTOCOL,
121 		    TOPO_PROP_ASRU, TOPO_PROP_IMMUTABLE, asru, err) < 0)
122 			return (-1);
123 	}
124 
125 	return (0);
126 }
127 
128 int
129 topo_node_fru_set(tnode_t *node, nvlist_t *fru, int flag, int *err)
130 {
131 
132 	/*
133 	 * Inherit FRU property from our parent if not specified
134 	 */
135 	if (fru == NULL) {
136 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL, TOPO_PROP_FRU,
137 		    err) < 0) {
138 			return (-1);
139 		}
140 	} else if (flag & TOPO_FRU_COMPUTE) {
141 		if (topo_prop_method_register(node, TOPO_PGROUP_PROTOCOL,
142 		    TOPO_PROP_FRU, TOPO_TYPE_FMRI, TOPO_METH_FRU_COMPUTE,
143 		    fru, err) < 0)
144 			return (-1);
145 	} else {
146 		if (topo_prop_set_fmri(node, TOPO_PGROUP_PROTOCOL,
147 		    TOPO_PROP_FRU, TOPO_PROP_IMMUTABLE, fru, err) < 0)
148 			return (-1);
149 	}
150 
151 
152 	return (0);
153 }
154 
155 int
156 topo_node_label_set(tnode_t *node, const char *label, int *err)
157 {
158 
159 	/*
160 	 * Inherit label property from our parent if * not specified
161 	 */
162 	if (label == NULL) {
163 		if (topo_prop_inherit(node, TOPO_PGROUP_PROTOCOL,
164 		    TOPO_PROP_LABEL, err) < 0) {
165 			return (-1);
166 		}
167 	} else {
168 		if (topo_prop_set_string(node, TOPO_PGROUP_PROTOCOL,
169 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label, err) < 0)
170 			return (-1);
171 	}
172 
173 	return (0);
174 }
175