1b48b4ad7Szx /*
2b48b4ad7Szx  * CDDL HEADER START
3b48b4ad7Szx  *
4b48b4ad7Szx  * The contents of this file are subject to the terms of the
5b48b4ad7Szx  * Common Development and Distribution License (the "License").
6b48b4ad7Szx  * You may not use this file except in compliance with the License.
7b48b4ad7Szx  *
8b48b4ad7Szx  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9b48b4ad7Szx  * or http://www.opensolaris.org/os/licensing.
10b48b4ad7Szx  * See the License for the specific language governing permissions
11b48b4ad7Szx  * and limitations under the License.
12b48b4ad7Szx  *
13b48b4ad7Szx  * When distributing Covered Code, include this CDDL HEADER in each
14b48b4ad7Szx  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15b48b4ad7Szx  * If applicable, add the following below this CDDL HEADER, with the
16b48b4ad7Szx  * fields enclosed by brackets "[]" replaced with your own identifying
17b48b4ad7Szx  * information: Portions Copyright [yyyy] [name of copyright owner]
18b48b4ad7Szx  *
19b48b4ad7Szx  * CDDL HEADER END
20b48b4ad7Szx  */
21b48b4ad7Szx 
22b48b4ad7Szx /*
239c94f155SCheng Sean Ye  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24b48b4ad7Szx  * Use is subject to license terms.
25b48b4ad7Szx  */
26b48b4ad7Szx 
27b48b4ad7Szx #include <string.h>
28b48b4ad7Szx #include <fm/topo_mod.h>
29b48b4ad7Szx #include <fm/topo_hc.h>
30b48b4ad7Szx #include <sys/fm/protocol.h>
31b48b4ad7Szx /*
32b48b4ad7Szx  * xfp.c
33b48b4ad7Szx  *	sun4v specific xfp enumerators
34b48b4ad7Szx  */
35b48b4ad7Szx 
36b48b4ad7Szx #ifdef __cplusplus
37b48b4ad7Szx extern "C" {
38b48b4ad7Szx #endif
39b48b4ad7Szx 
40b48b4ad7Szx #define	XFP_VERSION	TOPO_VERSION
41b48b4ad7Szx 
42b48b4ad7Szx static int xfp_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
43b48b4ad7Szx 		    topo_instance_t, void *, void *);
44b48b4ad7Szx 
45b48b4ad7Szx static const topo_modops_t xfp_ops =
46b48b4ad7Szx 	{ xfp_enum, NULL };
47b48b4ad7Szx 
48b48b4ad7Szx const topo_modinfo_t xfp_info =
49b48b4ad7Szx 	{XFP, FM_FMRI_SCHEME_HC, XFP_VERSION, &xfp_ops};
50b48b4ad7Szx 
51b48b4ad7Szx static const topo_pgroup_info_t xfp_auth_pgroup = {
52b48b4ad7Szx 	FM_FMRI_AUTHORITY,
53b48b4ad7Szx 	TOPO_STABILITY_PRIVATE,
54b48b4ad7Szx 	TOPO_STABILITY_PRIVATE,
55b48b4ad7Szx 	1
56b48b4ad7Szx };
57b48b4ad7Szx 
58b48b4ad7Szx /*ARGSUSED*/
59ee5416c9Syc int
_topo_init(topo_mod_t * mod,topo_version_t version)60b48b4ad7Szx _topo_init(topo_mod_t *mod, topo_version_t version)
61b48b4ad7Szx {
62b48b4ad7Szx 	/*
63b48b4ad7Szx 	 * Turn on module debugging output
64b48b4ad7Szx 	 */
65b48b4ad7Szx 	if (getenv("TOPOXFPDBG") != NULL)
66b48b4ad7Szx 		topo_mod_setdebug(mod);
67b48b4ad7Szx 	topo_mod_dprintf(mod, "initializing xfp enumerator\n");
68b48b4ad7Szx 
69b48b4ad7Szx 	if (topo_mod_register(mod, &xfp_info, TOPO_VERSION) < 0) {
70b48b4ad7Szx 		topo_mod_dprintf(mod, "xfp registration failed: %s\n",
71b48b4ad7Szx 		    topo_mod_errmsg(mod));
72ee5416c9Syc 		return (-1); /* mod errno already set */
73b48b4ad7Szx 	}
74b48b4ad7Szx 	topo_mod_dprintf(mod, "xfp enum initd\n");
75ee5416c9Syc 	return (0);
76b48b4ad7Szx }
77b48b4ad7Szx 
78b48b4ad7Szx void
_topo_fini(topo_mod_t * mod)79b48b4ad7Szx _topo_fini(topo_mod_t *mod)
80b48b4ad7Szx {
81b48b4ad7Szx 	topo_mod_unregister(mod);
82b48b4ad7Szx }
83b48b4ad7Szx 
84b48b4ad7Szx static tnode_t *
xfp_tnode_create(topo_mod_t * mod,tnode_t * parent,const char * name,topo_instance_t i,void * priv)85b48b4ad7Szx xfp_tnode_create(topo_mod_t *mod, tnode_t *parent,
86b48b4ad7Szx     const char *name, topo_instance_t i, void *priv)
87b48b4ad7Szx {
88b48b4ad7Szx 	int err;
89b48b4ad7Szx 	nvlist_t *fmri;
90b48b4ad7Szx 	tnode_t *ntn;
91b48b4ad7Szx 	nvlist_t *auth = topo_mod_auth(mod, parent);
92b48b4ad7Szx 
93b48b4ad7Szx 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
94b48b4ad7Szx 	    NULL, auth, NULL, NULL, NULL);
95b48b4ad7Szx 	nvlist_free(auth);
96b48b4ad7Szx 
97b48b4ad7Szx 	if (fmri == NULL) {
98b48b4ad7Szx 		topo_mod_dprintf(mod,
99b48b4ad7Szx 		    "Unable to make nvlist for %s bind: %s.\n",
100b48b4ad7Szx 		    name, topo_mod_errmsg(mod));
101b48b4ad7Szx 		return (NULL);
102b48b4ad7Szx 	}
103b48b4ad7Szx 
104b48b4ad7Szx 	ntn = topo_node_bind(mod, parent, name, i, fmri);
105b48b4ad7Szx 	nvlist_free(fmri);
106b48b4ad7Szx 	if (ntn == NULL) {
107b48b4ad7Szx 		topo_mod_dprintf(mod,
108*6597d6fcSRobert Mustacchi 		    "topo_node_bind (%s%" PRIu64 "/%s%" PRIu64 ") failed: %s\n",
109b48b4ad7Szx 		    topo_node_name(parent), topo_node_instance(parent),
110*6597d6fcSRobert Mustacchi 		    name, i, topo_strerror(topo_mod_errno(mod)));
111b48b4ad7Szx 		return (NULL);
112b48b4ad7Szx 	}
113b48b4ad7Szx 
114b48b4ad7Szx 	topo_node_setspecific(ntn, priv);
115b48b4ad7Szx 	if (topo_pgroup_create(ntn, &xfp_auth_pgroup, &err) == 0) {
116b48b4ad7Szx 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
117b48b4ad7Szx 		    FM_FMRI_AUTH_PRODUCT, &err);
1189c94f155SCheng Sean Ye 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
1199c94f155SCheng Sean Ye 		    FM_FMRI_AUTH_PRODUCT_SN, &err);
120b48b4ad7Szx 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
121b48b4ad7Szx 		    FM_FMRI_AUTH_CHASSIS, &err);
122b48b4ad7Szx 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
123b48b4ad7Szx 		    FM_FMRI_AUTH_SERVER, &err);
124b48b4ad7Szx 	}
125b48b4ad7Szx 	return (ntn);
126b48b4ad7Szx }
127b48b4ad7Szx static int
xfp_fru_set(topo_mod_t * mp,tnode_t * tn)128b48b4ad7Szx xfp_fru_set(topo_mod_t *mp, tnode_t *tn)
129b48b4ad7Szx {
130b48b4ad7Szx 	nvlist_t *fmri;
131b48b4ad7Szx 	int err, e;
132b48b4ad7Szx 
133b48b4ad7Szx 	if (topo_node_resource(tn, &fmri, &err) < 0 ||
134b48b4ad7Szx 	    fmri == NULL) {
135b48b4ad7Szx 		topo_mod_dprintf(mp, "FRU_fmri_set error: %s\n",
136b48b4ad7Szx 		    topo_strerror(topo_mod_errno(mp)));
137b48b4ad7Szx 		return (topo_mod_seterrno(mp, err));
138b48b4ad7Szx 	}
139b48b4ad7Szx 	e = topo_node_fru_set(tn, fmri, 0, &err);
140b48b4ad7Szx 	nvlist_free(fmri);
141b48b4ad7Szx 	if (e < 0)
142b48b4ad7Szx 		return (topo_mod_seterrno(mp, err));
143b48b4ad7Szx 	return (0);
144b48b4ad7Szx }
145b48b4ad7Szx static int
xfp_label_set(topo_mod_t * mod,tnode_t * parent,tnode_t * node,topo_instance_t n)146b48b4ad7Szx xfp_label_set(topo_mod_t *mod, tnode_t *parent, tnode_t *node,
147*6597d6fcSRobert Mustacchi     topo_instance_t n)
148b48b4ad7Szx {
149b48b4ad7Szx 	char *label = NULL;
150b48b4ad7Szx 	char *plabel = NULL;
151b48b4ad7Szx 	const char *xfplabel = "/XFP";
152b48b4ad7Szx 	int err, len;
153b48b4ad7Szx 
154b48b4ad7Szx 	if (topo_node_label(parent, &plabel, &err) != 0 ||
155b48b4ad7Szx 	    plabel == NULL) {
156b48b4ad7Szx 		return (-1);
157b48b4ad7Szx 	}
158b48b4ad7Szx 
159b48b4ad7Szx 	len = strlen(plabel) + strlen(xfplabel) + 2;
160b48b4ad7Szx 	label = topo_mod_alloc(mod, len);
161b48b4ad7Szx 	(void) snprintf(label, len, "%s%s%d", plabel, xfplabel, n);
162b48b4ad7Szx 	topo_mod_strfree(mod, plabel);
163b48b4ad7Szx 
164b48b4ad7Szx 	if (label != NULL) {
165b48b4ad7Szx 		if (topo_prop_set_string(node, TOPO_PGROUP_PROTOCOL,
166b48b4ad7Szx 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label,
167b48b4ad7Szx 		    &err) != 0) {
168b48b4ad7Szx 			topo_mod_strfree(mod, label);
169b48b4ad7Szx 			return (topo_mod_seterrno(mod, err));
170b48b4ad7Szx 		}
171b48b4ad7Szx 	}
172b48b4ad7Szx 	topo_mod_free(mod, label, len);
173b48b4ad7Szx 	return (0);
174b48b4ad7Szx }
175b48b4ad7Szx /*ARGSUSED*/
176b48b4ad7Szx static tnode_t *
xfp_declare(tnode_t * parent,const char * name,topo_instance_t i,void * priv,topo_mod_t * mod)177b48b4ad7Szx xfp_declare(tnode_t *parent, const char *name, topo_instance_t i,
178*6597d6fcSRobert Mustacchi     void *priv, topo_mod_t *mod)
179b48b4ad7Szx {
180b48b4ad7Szx 	tnode_t *ntn;
181b48b4ad7Szx 	nvlist_t *fmri = NULL;
182b48b4ad7Szx 	int e;
183b48b4ad7Szx 
184b48b4ad7Szx 	if ((ntn = xfp_tnode_create(mod, parent, name, i, NULL)) == NULL) {
185b48b4ad7Szx 		topo_mod_dprintf(mod, "%s ntn = NULL\n", name);
186b48b4ad7Szx 		return (NULL);
187b48b4ad7Szx 	}
188b48b4ad7Szx 
189b48b4ad7Szx 	(void) xfp_fru_set(mod, ntn);
190b48b4ad7Szx 
191b48b4ad7Szx 	(void) xfp_label_set(mod, parent, ntn, i);
192b48b4ad7Szx 	/* set ASRU to resource fmri */
193b48b4ad7Szx 	if (topo_prop_get_fmri(ntn, TOPO_PGROUP_PROTOCOL,
194b48b4ad7Szx 	    TOPO_PROP_RESOURCE, &fmri, &e) == 0)
195b48b4ad7Szx 		(void) topo_node_asru_set(ntn, fmri, 0, &e);
196b48b4ad7Szx 	nvlist_free(fmri);
197b48b4ad7Szx 
198b48b4ad7Szx 	return (ntn);
199b48b4ad7Szx }
200b48b4ad7Szx 
201b48b4ad7Szx /*ARGSUSED*/
202b48b4ad7Szx static int
xfp_enum(topo_mod_t * mod,tnode_t * rnode,const char * name,topo_instance_t min,topo_instance_t max,void * notused,void * data)203b48b4ad7Szx xfp_enum(topo_mod_t *mod, tnode_t *rnode, const char *name,
204*6597d6fcSRobert Mustacchi     topo_instance_t min, topo_instance_t max, void *notused, void *data)
205b48b4ad7Szx {
206b48b4ad7Szx 	if (strcmp(name, XFP) != 0) {
207b48b4ad7Szx 		topo_mod_dprintf(mod,
208b48b4ad7Szx 		    "Currently only know how to enumerate %s components.\n",
209b48b4ad7Szx 		    XFP);
210b48b4ad7Szx 		return (0);
211b48b4ad7Szx 	}
212b48b4ad7Szx 	if (xfp_declare(rnode, name, min, data, mod) == NULL)
213b48b4ad7Szx 		return (-1);
214b48b4ad7Szx 
215b48b4ad7Szx 	return (0);
216b48b4ad7Szx }
217