22eeaed1robj * CDDL HEADER START
32eeaed1robj *
42eeaed1robj * The contents of this file are subject to the terms of the
52eeaed1robj * Common Development and Distribution License (the "License").
62eeaed1robj * You may not use this file except in compliance with the License.
72eeaed1robj *
82eeaed1robj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92eeaed1robj * or http://www.opensolaris.org/os/licensing.
102eeaed1robj * See the License for the specific language governing permissions
112eeaed1robj * and limitations under the License.
122eeaed1robj *
132eeaed1robj * When distributing Covered Code, include this CDDL HEADER in each
142eeaed1robj * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152eeaed1robj * If applicable, add the following below this CDDL HEADER, with the
162eeaed1robj * fields enclosed by brackets "[]" replaced with your own identifying
172eeaed1robj * information: Portions Copyright [yyyy] [name of copyright owner]
182eeaed1robj *
192eeaed1robj * CDDL HEADER END
202eeaed1robj */
23e5dcf7bRobert Johnston * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242eeaed1robj * Use is subject to license terms.
252eeaed1robj */
272eeaed1robj#include <stdio.h>
282eeaed1robj#include <stdlib.h>
292eeaed1robj#include <stdarg.h>
302eeaed1robj#include <string.h>
312eeaed1robj#include <strings.h>
322eeaed1robj#include <libnvpair.h>
332eeaed1robj#include <sys/types.h>
342eeaed1robj#include <fm/topo_mod.h>
362eeaed1robj#define	BUFSZ	128
382eeaed1robjstatic char *
392eeaed1robjget_fmtstr(topo_mod_t *mod, nvlist_t *in)
412eeaed1robj	char *fmtstr;
422eeaed1robj	nvlist_t *args;
432eeaed1robj	int ret;
452eeaed1robj	topo_mod_dprintf(mod, "get_fmtstr() called\n");
472eeaed1robj	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
482eeaed1robj		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
492eeaed1robj		    strerror(ret));
502eeaed1robj		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
512eeaed1robj		return (NULL);
522eeaed1robj	}
532eeaed1robj	if ((ret = nvlist_lookup_string(args, "format", &fmtstr)) != 0) {
542eeaed1robj		topo_mod_dprintf(mod, "Failed to lookup 'format' arg (%s)\n",
552eeaed1robj		    strerror(ret));
562eeaed1robj		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
572eeaed1robj		return (NULL);
582eeaed1robj	}
592eeaed1robj	return (fmtstr);
622eeaed1robjstatic int
632eeaed1robjstore_prop_val(topo_mod_t *mod, void *buf, char *propname, topo_type_t type,
642eeaed1robj    nvlist_t **out)
662eeaed1robj	if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) != 0) {
672eeaed1robj		topo_mod_dprintf(mod, "Failed to allocate 'out' nvlist\n");
682eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NOMEM));
692eeaed1robj	}
702eeaed1robj	if (nvlist_add_string(*out, TOPO_PROP_VAL_NAME, propname) != 0) {
712eeaed1robj		topo_mod_dprintf(mod, "Failed to set '%s'\n",
722eeaed1robj		    TOPO_PROP_VAL_NAME);
732eeaed1robj		nvlist_free(*out);
742eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
752eeaed1robj	}
762eeaed1robj	if (nvlist_add_uint32(*out, TOPO_PROP_VAL_TYPE, type)
772eeaed1robj	    != 0) {
782eeaed1robj		topo_mod_dprintf(mod, "Failed to set '%s'\n",
792eeaed1robj		    TOPO_PROP_VAL_TYPE);
802eeaed1robj		nvlist_free(*out);
812eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
822eeaed1robj	}
832eeaed1robj	if (type == TOPO_TYPE_STRING) {
842eeaed1robj		if (nvlist_add_string(*out, TOPO_PROP_VAL_VAL, (char *)buf)
852eeaed1robj		    != 0) {
862eeaed1robj			topo_mod_dprintf(mod, "Failed to set '%s'\n",
872eeaed1robj			    TOPO_PROP_VAL_VAL);
882eeaed1robj			nvlist_free(*out);
892eeaed1robj			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
902eeaed1robj		}
91e5dcf7bRobert Johnston	} else if (type == TOPO_TYPE_FMRI) {
922eeaed1robj		if (nvlist_add_nvlist(*out, TOPO_PROP_VAL_VAL, (nvlist_t *)buf)
932eeaed1robj		    != 0) {
942eeaed1robj			topo_mod_dprintf(mod, "Failed to set '%s'\n",
952eeaed1robj			    TOPO_PROP_VAL_VAL);
962eeaed1robj			nvlist_free(*out);
972eeaed1robj			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
982eeaed1robj		}
99e5dcf7bRobert Johnston	} else if (type == TOPO_TYPE_UINT32) {
100e5dcf7bRobert Johnston		if (nvlist_add_uint32(*out, TOPO_PROP_VAL_VAL, *(uint32_t *)buf)
101e5dcf7bRobert Johnston		    != 0) {
102e5dcf7bRobert Johnston			topo_mod_dprintf(mod, "Failed to set '%s'\n",
103e5dcf7bRobert Johnston			    TOPO_PROP_VAL_VAL);
104e5dcf7bRobert Johnston			nvlist_free(*out);
105e5dcf7bRobert Johnston			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
106e5dcf7bRobert Johnston		}
107e5dcf7bRobert Johnston	}
1082eeaed1robj	return (0);
1122eeaed1robj * This is a somewhat generic property method for labelling the PSU and fan
1132eeaed1robj * FRU's that we're enumerating.  It takes the following three arguments:
1142eeaed1robj *
1152eeaed1robj * format:	a string containing a printf-like format with a two %d tokens
1162eeaed1robj *              for the cpu and dimm slot label numbers, which this method
1172eeaed1robj *              computes
1182eeaed1robj *
1192eeaed1robj *              i.e.: Fan %d
1202eeaed1robj *
1212eeaed1robj * offset:      a numeric offset that we'll number the FRU's from.  This is to
1222eeaed1robj *              allow for the fact that some systems may number the FRU's
1232eeaed1robj *              from zero while others may start from one
1242eeaed1robj */
1252eeaed1robj/* ARGSUSED */
1272eeaed1robjipmi_fru_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
1282eeaed1robj    nvlist_t *in, nvlist_t **out)
1302eeaed1robj	char *fmtstr, buf[BUFSZ];
1312eeaed1robj	int ret;
1322eeaed1robj	uint32_t offset;
1332eeaed1robj	nvlist_t *args;
1352eeaed1robj	topo_mod_dprintf(mod, "ipmi_fru_label() called\n");
1362eeaed1robj	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
1372eeaed1robj		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
1382eeaed1robj		    strerror(ret));
1392eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
1402eeaed1robj	}
1412eeaed1robj	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
1422eeaed1robj		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
1432eeaed1robj		    strerror(ret));
1442eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
1452eeaed1robj	}
1472eeaed1robj	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
1482eeaed1robj		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
1492eeaed1robj		/* topo errno already set */
1502eeaed1robj		return (-1);
1512eeaed1robj	}
1532eeaed1robj	/* LINTED: E_SEC_PRINTF_VAR_FMT */
1542eeaed1robj	(void) snprintf(buf, BUFSZ, fmtstr,
1552eeaed1robj	    (topo_node_instance(node) + offset));
1572eeaed1robj	if (store_prop_val(mod, (void *)buf, "label", TOPO_TYPE_STRING, out)
1582eeaed1robj	    != 0) {
1592eeaed1robj		topo_mod_dprintf(mod, "Failed to set label\n");
1602eeaed1robj		/* topo errno already set */
1612eeaed1robj		return (-1);
1622eeaed1robj	}
1642eeaed1robj	return (0);
1672eeaed1robj * This is a somewhat generic property method for attaching a FRU fmri onto
1682eeaed1robj * a power supply or fan based on the assumption that the FRU will either be
1692eeaed1robj * the fan/psu itself or the parent node.
1702eeaed1robj *
1712eeaed1robj * entity:	either "self" or "parent"
1722eeaed1robj */
1732eeaed1robj/* ARGSUSED */
1752eeaed1robjipmi_fru_fmri(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
1762eeaed1robj    nvlist_t *in, nvlist_t **out)
1782eeaed1robj	char *entity;
1792eeaed1robj	int ret, err;
1802eeaed1robj	nvlist_t *args, *fru;
1822eeaed1robj	topo_mod_dprintf(mod, "ipmi_fru_fmri() called\n");
1832eeaed1robj	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
1842eeaed1robj		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
1852eeaed1robj		    strerror(ret));
1862eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
1872eeaed1robj	}
1882eeaed1robj	if ((ret = nvlist_lookup_string(args, "entity", &entity)) != 0) {
1892eeaed1robj		topo_mod_dprintf(mod, "Failed to lookup 'entity' arg (%s)\n",
1902eeaed1robj		    strerror(ret));
1912eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
1922eeaed1robj	}
1942eeaed1robj	if (strcasecmp(entity, "self") == 0) {
1952eeaed1robj		if (topo_node_resource(node, &fru, &err) != 0)
1962eeaed1robj			return (-1);
1972eeaed1robj	} else if (strcasecmp(entity, "parent") == 0) {
1982eeaed1robj		if (topo_node_resource(topo_node_parent(node), &fru, &err) != 0)
1992eeaed1robj			return (-1);
2002eeaed1robj	} else {
2012eeaed1robj		topo_mod_dprintf(mod, "Invalid 'entity' value\n");
2022eeaed1robj		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
2032eeaed1robj	}
2052eeaed1robj	if (store_prop_val(mod, (void *)fru, "FRU", TOPO_TYPE_FMRI, out) != 0) {
2062eeaed1robj		nvlist_free(fru);
2072eeaed1robj		topo_mod_dprintf(mod, "Failed to set FRU\n");
2082eeaed1robj		/* topo errno already set */
2092eeaed1robj		return (-1);
2102eeaed1robj	}
2122eeaed1robj	nvlist_free(fru);
2142eeaed1robj	return (0);