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 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <strings.h>
32 #include <libnvpair.h>
33 #include <sys/types.h>
34 #include <fm/topo_mod.h>
35 
36 #define	BUFSZ	128
37 
38 static char *
get_fmtstr(topo_mod_t * mod,nvlist_t * in)39 get_fmtstr(topo_mod_t *mod, nvlist_t *in)
40 {
41 	char *fmtstr;
42 	nvlist_t *args;
43 	int ret;
44 
45 	topo_mod_dprintf(mod, "get_fmtstr() called\n");
46 
47 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
48 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
49 		    strerror(ret));
50 		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
51 		return (NULL);
52 	}
53 	if ((ret = nvlist_lookup_string(args, "format", &fmtstr)) != 0) {
54 		topo_mod_dprintf(mod, "Failed to lookup 'format' arg (%s)\n",
55 		    strerror(ret));
56 		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
57 		return (NULL);
58 	}
59 	return (fmtstr);
60 }
61 
62 static int
store_prop_val(topo_mod_t * mod,void * buf,char * propname,topo_type_t type,nvlist_t ** out)63 store_prop_val(topo_mod_t *mod, void *buf, char *propname, topo_type_t type,
64     nvlist_t **out)
65 {
66 	if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) != 0) {
67 		topo_mod_dprintf(mod, "Failed to allocate 'out' nvlist\n");
68 		return (topo_mod_seterrno(mod, EMOD_NOMEM));
69 	}
70 	if (nvlist_add_string(*out, TOPO_PROP_VAL_NAME, propname) != 0) {
71 		topo_mod_dprintf(mod, "Failed to set '%s'\n",
72 		    TOPO_PROP_VAL_NAME);
73 		nvlist_free(*out);
74 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
75 	}
76 	if (nvlist_add_uint32(*out, TOPO_PROP_VAL_TYPE, type)
77 	    != 0) {
78 		topo_mod_dprintf(mod, "Failed to set '%s'\n",
79 		    TOPO_PROP_VAL_TYPE);
80 		nvlist_free(*out);
81 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
82 	}
83 	if (type == TOPO_TYPE_STRING) {
84 		if (nvlist_add_string(*out, TOPO_PROP_VAL_VAL, (char *)buf)
85 		    != 0) {
86 			topo_mod_dprintf(mod, "Failed to set '%s'\n",
87 			    TOPO_PROP_VAL_VAL);
88 			nvlist_free(*out);
89 			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
90 		}
91 	} else if (type == TOPO_TYPE_FMRI) {
92 		if (nvlist_add_nvlist(*out, TOPO_PROP_VAL_VAL, (nvlist_t *)buf)
93 		    != 0) {
94 			topo_mod_dprintf(mod, "Failed to set '%s'\n",
95 			    TOPO_PROP_VAL_VAL);
96 			nvlist_free(*out);
97 			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
98 		}
99 	} else if (type == TOPO_TYPE_UINT32) {
100 		if (nvlist_add_uint32(*out, TOPO_PROP_VAL_VAL, *(uint32_t *)buf)
101 		    != 0) {
102 			topo_mod_dprintf(mod, "Failed to set '%s'\n",
103 			    TOPO_PROP_VAL_VAL);
104 			nvlist_free(*out);
105 			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
106 		}
107 	}
108 	return (0);
109 }
110 
111 /*
112  * This is a somewhat generic property method for labelling the PSU and fan
113  * FRU's that we're enumerating.  It takes the following three arguments:
114  *
115  * format:	a string containing a printf-like format with a two %d tokens
116  *              for the cpu and dimm slot label numbers, which this method
117  *              computes
118  *
119  *              i.e.: Fan %d
120  *
121  * offset:      a numeric offset that we'll number the FRU's from.  This is to
122  *              allow for the fact that some systems may number the FRU's
123  *              from zero while others may start from one
124  */
125 /* ARGSUSED */
126 int
ipmi_fru_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)127 ipmi_fru_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
128     nvlist_t *in, nvlist_t **out)
129 {
130 	char *fmtstr, buf[BUFSZ];
131 	int ret;
132 	uint32_t offset;
133 	nvlist_t *args;
134 
135 	topo_mod_dprintf(mod, "ipmi_fru_label() called\n");
136 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
137 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
138 		    strerror(ret));
139 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
140 	}
141 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
142 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
143 		    strerror(ret));
144 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
145 	}
146 
147 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
148 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
149 		/* topo errno already set */
150 		return (-1);
151 	}
152 
153 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
154 	(void) snprintf(buf, BUFSZ, fmtstr,
155 	    (topo_node_instance(node) + offset));
156 
157 	if (store_prop_val(mod, (void *)buf, "label", TOPO_TYPE_STRING, out)
158 	    != 0) {
159 		topo_mod_dprintf(mod, "Failed to set label\n");
160 		/* topo errno already set */
161 		return (-1);
162 	}
163 
164 	return (0);
165 }
166 /*
167  * This is a somewhat generic property method for attaching a FRU fmri onto
168  * a power supply or fan based on the assumption that the FRU will either be
169  * the fan/psu itself or the parent node.
170  *
171  * entity:	either "self" or "parent"
172  */
173 /* ARGSUSED */
174 int
ipmi_fru_fmri(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)175 ipmi_fru_fmri(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
176     nvlist_t *in, nvlist_t **out)
177 {
178 	char *entity;
179 	int ret, err;
180 	nvlist_t *args, *fru;
181 
182 	topo_mod_dprintf(mod, "ipmi_fru_fmri() called\n");
183 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
184 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
185 		    strerror(ret));
186 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
187 	}
188 	if ((ret = nvlist_lookup_string(args, "entity", &entity)) != 0) {
189 		topo_mod_dprintf(mod, "Failed to lookup 'entity' arg (%s)\n",
190 		    strerror(ret));
191 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
192 	}
193 
194 	if (strcasecmp(entity, "self") == 0) {
195 		if (topo_node_resource(node, &fru, &err) != 0)
196 			return (-1);
197 	} else if (strcasecmp(entity, "parent") == 0) {
198 		if (topo_node_resource(topo_node_parent(node), &fru, &err) != 0)
199 			return (-1);
200 	} else {
201 		topo_mod_dprintf(mod, "Invalid 'entity' value\n");
202 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
203 	}
204 
205 	if (store_prop_val(mod, (void *)fru, "FRU", TOPO_TYPE_FMRI, out) != 0) {
206 		nvlist_free(fru);
207 		topo_mod_dprintf(mod, "Failed to set FRU\n");
208 		/* topo errno already set */
209 		return (-1);
210 	}
211 
212 	nvlist_free(fru);
213 
214 	return (0);
215 }
216