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 2006 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 <stdlib.h>
30 #include <sys/fm/protocol.h>
31 #include <fm/topo_mod.h>
32 #include <fm/topo_hc.h>
33 #include <fm/libtopo.h>
34 
35 int
36 child_range_add(topo_mod_t *mp, tnode_t *tn, const char *cnm,
37     topo_instance_t imin, topo_instance_t imax)
38 {
39 	int e;
40 
41 	e = topo_node_range_create(mp, tn, cnm, imin, imax);
42 	if (e != 0) {
43 		topo_mod_dprintf(mp, "add child range (%s) failed: %s\n",
44 		    cnm, topo_strerror(topo_mod_errno(mp)));
45 		return (-1);
46 	}
47 	return (0);
48 }
49 
50 ulong_t
51 strtonum(topo_mod_t *mp, char *str, int *err)
52 {
53 	ulong_t r;
54 	char *e;
55 
56 	r = strtoul(str, &e, 16);
57 	if (e == str) {
58 		topo_mod_dprintf(mp,
59 		    "Trouble converting %s to a number!\n", str);
60 		*err = -1;
61 		return (0);
62 	}
63 	*err = 0;
64 	return (r);
65 }
66 
67 tnode_t *
68 tnode_create(topo_mod_t *mp, tnode_t *parent,
69     const char *name, topo_instance_t i, void *priv)
70 {
71 	nvlist_t *fmri;
72 	tnode_t *ntn;
73 	nvlist_t *auth;
74 
75 	auth = topo_mod_auth(mp, parent);
76 	fmri = topo_mod_hcfmri(mp, parent, FM_HC_SCHEME_VERSION, name, i, NULL,
77 	    auth, NULL, NULL, NULL);
78 	nvlist_free(auth);
79 	if (fmri == NULL) {
80 		topo_mod_dprintf(mp,
81 		    "Unable to make nvlist for %s bind.\n", name);
82 		return (NULL);
83 	}
84 
85 	ntn = topo_node_bind(mp, parent, name, i, fmri);
86 	if (ntn == NULL) {
87 		topo_mod_dprintf(mp,
88 		    "topo_node_bind (%s%d/%s%d) failed: %s\n",
89 		    topo_node_name(parent), topo_node_instance(parent),
90 		    name, i,
91 		    topo_strerror(topo_mod_errno(mp)));
92 		nvlist_free(fmri);
93 		return (NULL);
94 	}
95 	nvlist_free(fmri);
96 	topo_node_setspecific(ntn, priv);
97 
98 	return (ntn);
99 }
100 
101 /*ARGSUSED*/
102 int
103 labelmethod_inherit(topo_mod_t *mp, tnode_t *tn, nvlist_t *in, nvlist_t **out)
104 {
105 	int err;
106 
107 	/*
108 	 * Ignore the input and output nvlists and directly set the
109 	 * label as inheritance from the parent
110 	 */
111 	*out = NULL;
112 	if (topo_node_label_set(tn, NULL, &err) < 0) {
113 		if (err != ETOPO_PROP_NOENT)
114 			return (topo_mod_seterrno(mp, err));
115 	}
116 	return (0);
117 }
118