xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/xfp/xfp.c (revision 34bbc83afbf22a6f8e504cb99d76c97c017cb5f4)
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 <string.h>
28 #include <fm/topo_mod.h>
29 #include <fm/topo_hc.h>
30 #include <sys/fm/protocol.h>
31 /*
32  * xfp.c
33  *	sun4v specific xfp enumerators
34  */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #define	XFP_VERSION	TOPO_VERSION
41 
42 static int xfp_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
43 		    topo_instance_t, void *, void *);
44 
45 static const topo_modops_t xfp_ops =
46 	{ xfp_enum, NULL };
47 
48 const topo_modinfo_t xfp_info =
49 	{XFP, FM_FMRI_SCHEME_HC, XFP_VERSION, &xfp_ops};
50 
51 static const topo_pgroup_info_t xfp_auth_pgroup = {
52 	FM_FMRI_AUTHORITY,
53 	TOPO_STABILITY_PRIVATE,
54 	TOPO_STABILITY_PRIVATE,
55 	1
56 };
57 
58 /*ARGSUSED*/
59 int
60 _topo_init(topo_mod_t *mod, topo_version_t version)
61 {
62 	/*
63 	 * Turn on module debugging output
64 	 */
65 	if (getenv("TOPOXFPDBG") != NULL)
66 		topo_mod_setdebug(mod);
67 	topo_mod_dprintf(mod, "initializing xfp enumerator\n");
68 
69 	if (topo_mod_register(mod, &xfp_info, TOPO_VERSION) < 0) {
70 		topo_mod_dprintf(mod, "xfp registration failed: %s\n",
71 		    topo_mod_errmsg(mod));
72 		return (-1); /* mod errno already set */
73 	}
74 	topo_mod_dprintf(mod, "xfp enum initd\n");
75 	return (0);
76 }
77 
78 void
79 _topo_fini(topo_mod_t *mod)
80 {
81 	topo_mod_unregister(mod);
82 }
83 
84 static tnode_t *
85 xfp_tnode_create(topo_mod_t *mod, tnode_t *parent,
86     const char *name, topo_instance_t i, void *priv)
87 {
88 	int err;
89 	nvlist_t *fmri;
90 	tnode_t *ntn;
91 	nvlist_t *auth = topo_mod_auth(mod, parent);
92 
93 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
94 	    NULL, auth, NULL, NULL, NULL);
95 	nvlist_free(auth);
96 
97 	if (fmri == NULL) {
98 		topo_mod_dprintf(mod,
99 		    "Unable to make nvlist for %s bind: %s.\n",
100 		    name, topo_mod_errmsg(mod));
101 		return (NULL);
102 	}
103 
104 	ntn = topo_node_bind(mod, parent, name, i, fmri);
105 	nvlist_free(fmri);
106 	if (ntn == NULL) {
107 		topo_mod_dprintf(mod,
108 		    "topo_node_bind (%s%" PRIu64 "/%s%" PRIu64 ") failed: %s\n",
109 		    topo_node_name(parent), topo_node_instance(parent),
110 		    name, i, topo_strerror(topo_mod_errno(mod)));
111 		return (NULL);
112 	}
113 
114 	topo_node_setspecific(ntn, priv);
115 	if (topo_pgroup_create(ntn, &xfp_auth_pgroup, &err) == 0) {
116 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
117 		    FM_FMRI_AUTH_PRODUCT, &err);
118 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
119 		    FM_FMRI_AUTH_PRODUCT_SN, &err);
120 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
121 		    FM_FMRI_AUTH_CHASSIS, &err);
122 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
123 		    FM_FMRI_AUTH_SERVER, &err);
124 	}
125 	return (ntn);
126 }
127 static int
128 xfp_fru_set(topo_mod_t *mp, tnode_t *tn)
129 {
130 	nvlist_t *fmri;
131 	int err, e;
132 
133 	if (topo_node_resource(tn, &fmri, &err) < 0 ||
134 	    fmri == NULL) {
135 		topo_mod_dprintf(mp, "FRU_fmri_set error: %s\n",
136 		    topo_strerror(topo_mod_errno(mp)));
137 		return (topo_mod_seterrno(mp, err));
138 	}
139 	e = topo_node_fru_set(tn, fmri, 0, &err);
140 	nvlist_free(fmri);
141 	if (e < 0)
142 		return (topo_mod_seterrno(mp, err));
143 	return (0);
144 }
145 static int
146 xfp_label_set(topo_mod_t *mod, tnode_t *parent, tnode_t *node,
147     topo_instance_t n)
148 {
149 	char *label = NULL;
150 	char *plabel = NULL;
151 	const char *xfplabel = "/XFP";
152 	int err, len;
153 
154 	if (topo_node_label(parent, &plabel, &err) != 0 ||
155 	    plabel == NULL) {
156 		return (-1);
157 	}
158 
159 	len = strlen(plabel) + strlen(xfplabel) + 2;
160 	label = topo_mod_alloc(mod, len);
161 	(void) snprintf(label, len, "%s%s%d", plabel, xfplabel, n);
162 	topo_mod_strfree(mod, plabel);
163 
164 	if (label != NULL) {
165 		if (topo_prop_set_string(node, TOPO_PGROUP_PROTOCOL,
166 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label,
167 		    &err) != 0) {
168 			topo_mod_strfree(mod, label);
169 			return (topo_mod_seterrno(mod, err));
170 		}
171 	}
172 	topo_mod_free(mod, label, len);
173 	return (0);
174 }
175 /*ARGSUSED*/
176 static tnode_t *
177 xfp_declare(tnode_t *parent, const char *name, topo_instance_t i,
178     void *priv, topo_mod_t *mod)
179 {
180 	tnode_t *ntn;
181 	nvlist_t *fmri = NULL;
182 	int e;
183 
184 	if ((ntn = xfp_tnode_create(mod, parent, name, i, NULL)) == NULL) {
185 		topo_mod_dprintf(mod, "%s ntn = NULL\n", name);
186 		return (NULL);
187 	}
188 
189 	(void) xfp_fru_set(mod, ntn);
190 
191 	(void) xfp_label_set(mod, parent, ntn, i);
192 	/* set ASRU to resource fmri */
193 	if (topo_prop_get_fmri(ntn, TOPO_PGROUP_PROTOCOL,
194 	    TOPO_PROP_RESOURCE, &fmri, &e) == 0)
195 		(void) topo_node_asru_set(ntn, fmri, 0, &e);
196 	nvlist_free(fmri);
197 
198 	return (ntn);
199 }
200 
201 /*ARGSUSED*/
202 static int
203 xfp_enum(topo_mod_t *mod, tnode_t *rnode, const char *name,
204     topo_instance_t min, topo_instance_t max, void *notused, void *data)
205 {
206 	if (strcmp(name, XFP) != 0) {
207 		topo_mod_dprintf(mod,
208 		    "Currently only know how to enumerate %s components.\n",
209 		    XFP);
210 		return (0);
211 	}
212 	if (xfp_declare(rnode, name, min, data, mod) == NULL)
213 		return (-1);
214 
215 	return (0);
216 }
217