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