#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include <libnvpair.h>
#include <sys/types.h>
#include <libipmi.h>
#include <fm/topo_mod.h>
#include <ctype.h>
#include "chip.h"
#define BUFSZ 128
#define JEDEC_TBL_SZ 5
static const char *jedec_tbl[JEDEC_TBL_SZ][2] =
{
{ "HYUNDAI ELECTRONICS", "00AD" },
{ "INFINEON", "00C1" },
{ "MICRON TECHNOLOGY", "002C" },
{ "QIMONDA", "7F51" },
{ "SAMSUNG", "00CE" },
};
static int
ipmi_serial_lookup(topo_mod_t *mod, char *ipmi_tag, char *buf)
{
char *fru_data;
int i, found_id = 0, serial_len;
ipmi_handle_t *hdl;
ipmi_sdr_fru_locator_t *fru_loc;
ipmi_fru_prod_info_t prod_info;
topo_mod_dprintf(mod, "ipmi_serial_lookup() called\n");
if ((hdl = topo_mod_ipmi_hold(mod)) == NULL) {
topo_mod_dprintf(mod, "Failed to get IPMI handle\n");
return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
}
topo_mod_dprintf(mod, "Looking up FRU data for %s ...\n", ipmi_tag);
if ((fru_loc = ipmi_sdr_lookup_fru(hdl, (const char *)ipmi_tag))
== NULL) {
topo_mod_dprintf(mod, "Failed to lookup %s (%s)\n", ipmi_tag,
ipmi_errmsg(hdl));
topo_mod_ipmi_rele(mod);
return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
}
topo_mod_dprintf(mod, "Reading FRU data ...\n");
if (ipmi_fru_read(hdl, fru_loc, &fru_data) < 0) {
topo_mod_dprintf(mod, "Failed to read FRU data (%s)\n",
ipmi_errmsg(hdl));
topo_mod_ipmi_rele(mod);
return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
}
topo_mod_dprintf(mod, "Parsing product info area ...\n");
if (ipmi_fru_parse_product(hdl, fru_data, &prod_info) < 0) {
topo_mod_dprintf(mod, "Failed to read FRU product info (%s)\n",
ipmi_errmsg(hdl));
free(fru_data);
topo_mod_ipmi_rele(mod);
return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
}
free(fru_data);
topo_mod_ipmi_rele(mod);
topo_mod_dprintf(mod, "FRU Product Serial: %s\n",
prod_info.ifpi_product_serial);
topo_mod_dprintf(mod, "Manufacturer Name: \"%s\"\n",
prod_info.ifpi_manuf_name);
serial_len = strnlen(prod_info.ifpi_product_serial, FRU_INFO_MAXLEN);
if (serial_len == 18) {
(void) memcpy(buf, prod_info.ifpi_product_serial, 18);
*(buf+18) = '\0';
return (0);
}
if (serial_len != 8) {
*buf = '\0';
return (0);
}
for (i = 0; prod_info.ifpi_manuf_name[i]; i++) {
prod_info.ifpi_manuf_name[i] =
toupper(prod_info.ifpi_manuf_name[i]);
if (!isalpha(prod_info.ifpi_manuf_name[i]) &&
!isdigit(prod_info.ifpi_manuf_name[i]))
prod_info.ifpi_manuf_name[i] = (char)0x20;
}
topo_mod_dprintf(mod, "Normalized Manufacturer Name \"%s\"\n",
prod_info.ifpi_manuf_name);
for (i = 0; i < JEDEC_TBL_SZ; i++)
if (strcmp(prod_info.ifpi_manuf_name, jedec_tbl[i][0]) == 0) {
found_id = 1;
break;
}
if (found_id)
(void) memcpy(buf, jedec_tbl[i][1], 4);
else
(void) memcpy(buf, (char *)("0000"), 4);
(void) memcpy((buf+4), (char *)("000000"), 6);
(void) memcpy((buf+10), prod_info.ifpi_product_serial, 8);
*(buf+18) = '\0';
return (0);
}
int
get_dimm_serial(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
nvlist_t *in, nvlist_t **out)
{
char **entity_refs, fru_serial[FRU_INFO_MAXLEN];
int err, rv = 0, i;
uint_t nelems;
boolean_t found_serial = B_FALSE;
if (topo_prop_get_string_array(node, TOPO_PGROUP_IPMI, "entity_ref",
&entity_refs, &nelems, &err) != 0) {
topo_mod_dprintf(mod, "%s: Failed to lookup entity_ref property"
" (%s)", __func__, topo_strerror(err));
return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
}
for (i = 0; i < nelems; i++) {
if (ipmi_serial_lookup(mod, entity_refs[i], fru_serial) == 0) {
found_serial = B_TRUE;
break;
} else
topo_mod_dprintf(mod, "Failed to lookup serial for "
"%s\n", entity_refs[i]);
}
if (! found_serial)
(void) strcpy(fru_serial, "");
if (store_prop_val(mod, fru_serial, "serial", out) != 0) {
topo_mod_dprintf(mod, "Failed to set serial\n");
rv = -1;
}
topo_mod_strfreev(mod, entity_refs, nelems);
return (rv);
}