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
37extern "C" {
38#endif
39
40#define	XFP_VERSION	TOPO_VERSION
41
42static int xfp_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
43		    topo_instance_t, void *, void *);
44
45static const topo_modops_t xfp_ops =
46	{ xfp_enum, NULL };
47
48const topo_modinfo_t xfp_info =
49	{XFP, FM_FMRI_SCHEME_HC, XFP_VERSION, &xfp_ops};
50
51static 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*/
59int
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
78void
79_topo_fini(topo_mod_t *mod)
80{
81	topo_mod_unregister(mod);
82}
83
84static tnode_t *
85xfp_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%d/%s%d) failed: %s\n",
109		    topo_node_name(parent), topo_node_instance(parent),
110		    name, i,
111		    topo_strerror(topo_mod_errno(mod)));
112		return (NULL);
113	}
114
115	topo_node_setspecific(ntn, priv);
116	if (topo_pgroup_create(ntn, &xfp_auth_pgroup, &err) == 0) {
117		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
118		    FM_FMRI_AUTH_PRODUCT, &err);
119		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
120		    FM_FMRI_AUTH_PRODUCT_SN, &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}
128static int
129xfp_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}
146static int
147xfp_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*/
177static tnode_t *
178xfp_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*/
203static int
204xfp_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