10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5555Stimh * Common Development and Distribution License (the "License").
6555Stimh * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21555Stimh
220Sstevel@tonic-gate /*
23*7275Sstephh * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <fm/fmd_fmri.h>
304198Seschrock #include <fm/libtopo.h>
314198Seschrock #include <fm/topo_mod.h>
320Sstevel@tonic-gate #include <libdevinfo.h>
330Sstevel@tonic-gate #include <alloca.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate
364198Seschrock int
fmd_fmri_init(void)374198Seschrock fmd_fmri_init(void)
380Sstevel@tonic-gate {
394198Seschrock return (0);
400Sstevel@tonic-gate }
410Sstevel@tonic-gate
424198Seschrock void
fmd_fmri_fini(void)434198Seschrock fmd_fmri_fini(void)
444198Seschrock {
454198Seschrock }
460Sstevel@tonic-gate
470Sstevel@tonic-gate ssize_t
fmd_fmri_nvl2str(nvlist_t * nvl,char * buf,size_t buflen)480Sstevel@tonic-gate fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
490Sstevel@tonic-gate {
504198Seschrock int err;
514198Seschrock ssize_t len;
524198Seschrock topo_hdl_t *thp;
534198Seschrock char *str;
544198Seschrock
554198Seschrock if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
564198Seschrock return (fmd_fmri_set_errno(EINVAL));
574198Seschrock
584198Seschrock if (topo_fmri_nvl2str(thp, nvl, &str, &err) != 0) {
594198Seschrock fmd_fmri_topo_rele(thp);
604198Seschrock return (fmd_fmri_set_errno(EINVAL));
614198Seschrock }
624198Seschrock
634198Seschrock if (buf != NULL)
644198Seschrock len = snprintf(buf, buflen, "%s", str);
654198Seschrock else
664198Seschrock len = strlen(str);
674198Seschrock
684198Seschrock topo_hdl_strfree(thp, str);
694198Seschrock fmd_fmri_topo_rele(thp);
704198Seschrock
714198Seschrock return (len);
724198Seschrock }
734198Seschrock
744198Seschrock int
fmd_fmri_present(nvlist_t * nvl)754198Seschrock fmd_fmri_present(nvlist_t *nvl)
764198Seschrock {
774198Seschrock int err, present;
784198Seschrock topo_hdl_t *thp;
794198Seschrock
804198Seschrock if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
814198Seschrock return (fmd_fmri_set_errno(EINVAL));
824198Seschrock err = 0;
834198Seschrock present = topo_fmri_present(thp, nvl, &err);
844198Seschrock fmd_fmri_topo_rele(thp);
854198Seschrock
86*7275Sstephh return (present);
87*7275Sstephh }
88*7275Sstephh
89*7275Sstephh int
fmd_fmri_replaced(nvlist_t * nvl)90*7275Sstephh fmd_fmri_replaced(nvlist_t *nvl)
91*7275Sstephh {
92*7275Sstephh int err, rval;
93*7275Sstephh topo_hdl_t *thp;
94*7275Sstephh
95*7275Sstephh if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
96*7275Sstephh return (fmd_fmri_set_errno(EINVAL));
97*7275Sstephh err = 0;
98*7275Sstephh rval = topo_fmri_replaced(thp, nvl, &err);
99*7275Sstephh fmd_fmri_topo_rele(thp);
100*7275Sstephh
101*7275Sstephh return (rval);
1024198Seschrock }
1034198Seschrock
1044198Seschrock int
fmd_fmri_unusable(nvlist_t * nvl)1054198Seschrock fmd_fmri_unusable(nvlist_t *nvl)
1064198Seschrock {
1070Sstevel@tonic-gate uint8_t version;
1084198Seschrock int err, unusable;
1094198Seschrock topo_hdl_t *thp;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
1120Sstevel@tonic-gate version > FM_DEV_SCHEME_VERSION)
1130Sstevel@tonic-gate return (fmd_fmri_set_errno(EINVAL));
1140Sstevel@tonic-gate
1154198Seschrock if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
1160Sstevel@tonic-gate return (fmd_fmri_set_errno(EINVAL));
1174198Seschrock err = 0;
1184198Seschrock unusable = topo_fmri_unusable(thp, nvl, &err);
1194198Seschrock fmd_fmri_topo_rele(thp);
1200Sstevel@tonic-gate
1214198Seschrock if (err != 0)
1224198Seschrock return (0);
1234198Seschrock else
1244198Seschrock return (unusable);
1250Sstevel@tonic-gate }
126*7275Sstephh
127*7275Sstephh int
fmd_fmri_service_state(nvlist_t * nvl)128*7275Sstephh fmd_fmri_service_state(nvlist_t *nvl)
129*7275Sstephh {
130*7275Sstephh uint8_t version;
131*7275Sstephh int err, service_state;
132*7275Sstephh topo_hdl_t *thp;
133*7275Sstephh
134*7275Sstephh if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
135*7275Sstephh version > FM_DEV_SCHEME_VERSION)
136*7275Sstephh return (fmd_fmri_set_errno(EINVAL));
137*7275Sstephh
138*7275Sstephh if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
139*7275Sstephh return (fmd_fmri_set_errno(EINVAL));
140*7275Sstephh err = 0;
141*7275Sstephh service_state = topo_fmri_service_state(thp, nvl, &err);
142*7275Sstephh fmd_fmri_topo_rele(thp);
143*7275Sstephh
144*7275Sstephh if (err != 0)
145*7275Sstephh return (FMD_SERVICE_STATE_UNKNOWN);
146*7275Sstephh else
147*7275Sstephh return (service_state);
148*7275Sstephh }
149