17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5dd33c4e4Stimh * Common Development and Distribution License (the "License").
6dd33c4e4Stimh * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21dd33c4e4Stimh
227c478bd9Sstevel@tonic-gate /*
23*25c6ff4bSstephh * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <fm/fmd_fmri.h>
2824db4641Seschrock #include <fm/libtopo.h>
2924db4641Seschrock #include <fm/topo_mod.h>
307c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
317c478bd9Sstevel@tonic-gate #include <alloca.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate
3424db4641Seschrock int
fmd_fmri_init(void)3524db4641Seschrock fmd_fmri_init(void)
367c478bd9Sstevel@tonic-gate {
3724db4641Seschrock return (0);
387c478bd9Sstevel@tonic-gate }
397c478bd9Sstevel@tonic-gate
4024db4641Seschrock void
fmd_fmri_fini(void)4124db4641Seschrock fmd_fmri_fini(void)
4224db4641Seschrock {
4324db4641Seschrock }
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate ssize_t
fmd_fmri_nvl2str(nvlist_t * nvl,char * buf,size_t buflen)467c478bd9Sstevel@tonic-gate fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate int err;
4924db4641Seschrock ssize_t len;
5024db4641Seschrock topo_hdl_t *thp;
5124db4641Seschrock char *str;
527c478bd9Sstevel@tonic-gate
5324db4641Seschrock if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
547c478bd9Sstevel@tonic-gate return (fmd_fmri_set_errno(EINVAL));
557c478bd9Sstevel@tonic-gate
5624db4641Seschrock if (topo_fmri_nvl2str(thp, nvl, &str, &err) != 0) {
5724db4641Seschrock fmd_fmri_topo_rele(thp);
587c478bd9Sstevel@tonic-gate return (fmd_fmri_set_errno(EINVAL));
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
6124db4641Seschrock if (buf != NULL)
6224db4641Seschrock len = snprintf(buf, buflen, "%s", str);
6324db4641Seschrock else
6424db4641Seschrock len = strlen(str);
657c478bd9Sstevel@tonic-gate
6624db4641Seschrock topo_hdl_strfree(thp, str);
6724db4641Seschrock fmd_fmri_topo_rele(thp);
687c478bd9Sstevel@tonic-gate
6924db4641Seschrock return (len);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate int
fmd_fmri_present(nvlist_t * nvl)737c478bd9Sstevel@tonic-gate fmd_fmri_present(nvlist_t *nvl)
747c478bd9Sstevel@tonic-gate {
7524db4641Seschrock int err, present;
7624db4641Seschrock topo_hdl_t *thp;
777c478bd9Sstevel@tonic-gate
7824db4641Seschrock if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
797c478bd9Sstevel@tonic-gate return (fmd_fmri_set_errno(EINVAL));
8024db4641Seschrock err = 0;
8124db4641Seschrock present = topo_fmri_present(thp, nvl, &err);
8224db4641Seschrock fmd_fmri_topo_rele(thp);
837c478bd9Sstevel@tonic-gate
8424db4641Seschrock return (present);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate int
fmd_fmri_replaced(nvlist_t * nvl)88*25c6ff4bSstephh fmd_fmri_replaced(nvlist_t *nvl)
89*25c6ff4bSstephh {
90*25c6ff4bSstephh int err, rval;
91*25c6ff4bSstephh topo_hdl_t *thp;
92*25c6ff4bSstephh
93*25c6ff4bSstephh if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
94*25c6ff4bSstephh return (fmd_fmri_set_errno(EINVAL));
95*25c6ff4bSstephh err = 0;
96*25c6ff4bSstephh rval = topo_fmri_replaced(thp, nvl, &err);
97*25c6ff4bSstephh fmd_fmri_topo_rele(thp);
98*25c6ff4bSstephh
99*25c6ff4bSstephh return (rval);
100*25c6ff4bSstephh }
101*25c6ff4bSstephh
102*25c6ff4bSstephh int
fmd_fmri_unusable(nvlist_t * nvl)1037c478bd9Sstevel@tonic-gate fmd_fmri_unusable(nvlist_t *nvl)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate uint8_t version;
10624db4641Seschrock int err, unusable;
10724db4641Seschrock topo_hdl_t *thp;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
1107c478bd9Sstevel@tonic-gate version > FM_DEV_SCHEME_VERSION)
1117c478bd9Sstevel@tonic-gate return (fmd_fmri_set_errno(EINVAL));
1127c478bd9Sstevel@tonic-gate
11324db4641Seschrock if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
11424db4641Seschrock return (fmd_fmri_set_errno(EINVAL));
11524db4641Seschrock err = 0;
11624db4641Seschrock unusable = topo_fmri_unusable(thp, nvl, &err);
11724db4641Seschrock fmd_fmri_topo_rele(thp);
11824db4641Seschrock
11924db4641Seschrock if (err != 0)
1207c478bd9Sstevel@tonic-gate return (0);
12124db4641Seschrock else
12224db4641Seschrock return (unusable);
1237c478bd9Sstevel@tonic-gate }
124*25c6ff4bSstephh
125*25c6ff4bSstephh int
fmd_fmri_service_state(nvlist_t * nvl)126*25c6ff4bSstephh fmd_fmri_service_state(nvlist_t *nvl)
127*25c6ff4bSstephh {
128*25c6ff4bSstephh uint8_t version;
129*25c6ff4bSstephh int err, service_state;
130*25c6ff4bSstephh topo_hdl_t *thp;
131*25c6ff4bSstephh
132*25c6ff4bSstephh if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
133*25c6ff4bSstephh version > FM_DEV_SCHEME_VERSION)
134*25c6ff4bSstephh return (fmd_fmri_set_errno(EINVAL));
135*25c6ff4bSstephh
136*25c6ff4bSstephh if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
137*25c6ff4bSstephh return (fmd_fmri_set_errno(EINVAL));
138*25c6ff4bSstephh err = 0;
139*25c6ff4bSstephh service_state = topo_fmri_service_state(thp, nvl, &err);
140*25c6ff4bSstephh fmd_fmri_topo_rele(thp);
141*25c6ff4bSstephh
142*25c6ff4bSstephh if (err != 0)
143*25c6ff4bSstephh return (FMD_SERVICE_STATE_UNKNOWN);
144*25c6ff4bSstephh else
145*25c6ff4bSstephh return (service_state);
146*25c6ff4bSstephh }
147