xref: /titanic_51/usr/src/cmd/fm/schemes/dev/scheme.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
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 2008 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 <fm/fmd_fmri.h>
30 #include <fm/libtopo.h>
31 #include <fm/topo_mod.h>
32 #include <libdevinfo.h>
33 #include <alloca.h>
34 #include <string.h>
35 
36 int
37 fmd_fmri_init(void)
38 {
39 	return (0);
40 }
41 
42 void
43 fmd_fmri_fini(void)
44 {
45 }
46 
47 ssize_t
48 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
49 {
50 	int err;
51 	ssize_t len;
52 	topo_hdl_t *thp;
53 	char *str;
54 
55 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
56 		return (fmd_fmri_set_errno(EINVAL));
57 
58 	if (topo_fmri_nvl2str(thp, nvl, &str, &err) != 0) {
59 		fmd_fmri_topo_rele(thp);
60 		return (fmd_fmri_set_errno(EINVAL));
61 	}
62 
63 	if (buf != NULL)
64 		len = snprintf(buf, buflen, "%s", str);
65 	else
66 		len = strlen(str);
67 
68 	topo_hdl_strfree(thp, str);
69 	fmd_fmri_topo_rele(thp);
70 
71 	return (len);
72 }
73 
74 int
75 fmd_fmri_present(nvlist_t *nvl)
76 {
77 	int err, present;
78 	topo_hdl_t *thp;
79 
80 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
81 		return (fmd_fmri_set_errno(EINVAL));
82 	err = 0;
83 	present = topo_fmri_present(thp, nvl, &err);
84 	fmd_fmri_topo_rele(thp);
85 
86 	return (present);
87 }
88 
89 int
90 fmd_fmri_replaced(nvlist_t *nvl)
91 {
92 	int err, rval;
93 	topo_hdl_t *thp;
94 
95 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
96 		return (fmd_fmri_set_errno(EINVAL));
97 	err = 0;
98 	rval = topo_fmri_replaced(thp, nvl, &err);
99 	fmd_fmri_topo_rele(thp);
100 
101 	return (rval);
102 }
103 
104 int
105 fmd_fmri_unusable(nvlist_t *nvl)
106 {
107 	uint8_t version;
108 	int err, unusable;
109 	topo_hdl_t *thp;
110 
111 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
112 	    version > FM_DEV_SCHEME_VERSION)
113 		return (fmd_fmri_set_errno(EINVAL));
114 
115 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
116 		return (fmd_fmri_set_errno(EINVAL));
117 	err = 0;
118 	unusable = topo_fmri_unusable(thp, nvl, &err);
119 	fmd_fmri_topo_rele(thp);
120 
121 	if (err != 0)
122 		return (0);
123 	else
124 		return (unusable);
125 }
126 
127 int
128 fmd_fmri_service_state(nvlist_t *nvl)
129 {
130 	uint8_t version;
131 	int err, service_state;
132 	topo_hdl_t *thp;
133 
134 	if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
135 	    version > FM_DEV_SCHEME_VERSION)
136 		return (fmd_fmri_set_errno(EINVAL));
137 
138 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
139 		return (fmd_fmri_set_errno(EINVAL));
140 	err = 0;
141 	service_state = topo_fmri_service_state(thp, nvl, &err);
142 	fmd_fmri_topo_rele(thp);
143 
144 	if (err != 0)
145 		return (FMD_SERVICE_STATE_UNKNOWN);
146 	else
147 		return (service_state);
148 }
149