/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Subroutines used by the i86pc Generic Topology Enumerator
*/
#include <sys/types.h>
#include <strings.h>
#include <deflt.h>
#include <fcntl.h>
#include <unistd.h>
#include <fm/topo_mod.h>
#include <fm/topo_hc.h>
#include <sys/devfm.h>
#include <sys/pci.h>
#include <sys/systeminfo.h>
#include <sys/fm/protocol.h>
#include <sys/utsname.h>
#include <sys/smbios.h>
#include <sys/smbios_impl.h>
#include <x86pi_impl.h>
static const topo_pgroup_info_t sys_pgroup = {
TOPO_PGROUP_SYSTEM,
TOPO_STABILITY_PRIVATE,
TOPO_STABILITY_PRIVATE,
1
};
static const topo_pgroup_info_t auth_pgroup = {
FM_FMRI_AUTHORITY,
TOPO_STABILITY_PRIVATE,
TOPO_STABILITY_PRIVATE,
1
};
/*
* Free hcfmri strings.
*/
void
x86pi_hcfmri_info_fini(topo_mod_t *mod, x86pi_hcfmri_t *hc)
{
if (hc->hc_name != NULL)
topo_mod_strfree(mod, (char *)hc->hc_name);
if (hc->manufacturer != NULL)
topo_mod_strfree(mod, (char *)hc->manufacturer);
if (hc->product != NULL)
topo_mod_strfree(mod, (char *)hc->product);
if (hc->version != NULL)
topo_mod_strfree(mod, (char *)hc->version);
if (hc->serial_number != NULL)
topo_mod_strfree(mod, (char *)hc->serial_number);
if (hc->asset_tag != NULL)
topo_mod_strfree(mod, (char *)hc->asset_tag);
if (hc->location != NULL)
topo_mod_strfree(mod, (char *)hc->location);
if (hc->part_number != NULL)
topo_mod_strfree(mod, (char *)hc->part_number);
}
/*
* Get the server hostname (the ID as far as the topo authority is
* concerned) from sysinfo and return a copy to the caller.
*
* The string must be freed with topo_mod_strfree()
*/
char *
x86pi_get_serverid(topo_mod_t *mod)
{
int result;
char hostname[MAXNAMELEN];
topo_mod_dprintf(mod, "x86pi_get_serverid\n");
result = sysinfo(SI_HOSTNAME, hostname, sizeof (hostname));
/* Everything is freed up and it's time to return the platform-id */
if (result == -1) {
return (NULL);
}
topo_mod_dprintf(mod, "x86pi_get_serverid: hostname = %s\n", hostname);
return (topo_mod_strdup(mod, hostname));
}
/*
* Go through the smbios structures looking for a type. Fill in
* the structure count as well as the id(s) of the struct types.
*/
void
x86pi_smb_strcnt(topo_mod_t *mod, smbs_cnt_t *stype)
{
const smb_struct_t *sp;
int nstructs;
int i, cnt;
smbios_hdl_t *shp;
shp = topo_mod_smbios(mod);
if (shp == NULL) {
stype->count = 0;
return;
}
nstructs = shp->sh_nstructs;
sp = shp->sh_structs;
for (i = 0, cnt = 0; i < nstructs; i++, sp++) {
if (sp->smbst_hdr->smbh_type == stype->type) {
stype->ids[cnt].node = NULL;
stype->ids[cnt].id = sp->smbst_hdr->smbh_hdl;
cnt++;
}
}
stype->count = cnt;
}
/*
* Calculate the authority information for a node. Always create the
* appropriate property group even if complete authority is not found.
*/
int
x86pi_set_auth(topo_mod_t *mod, x86pi_hcfmri_t *hcfmri, tnode_t *t_parent,
tnode_t *t_node)
{
int result;
int err;
int is_chassis = 0;
int chassis_instance = 0;
nvlist_t *auth;
char *f = "x86pi_set_auth";
if (mod == NULL || t_parent == NULL || t_node == NULL) {
return (-1);
}
if ((topo_pgroup_create(t_node, &auth_pgroup, &err) != 0) &&
(err != ETOPO_PROP_DEFD)) {
/*
* We failed to create the property group and it was not
* already defined. Set the err code and return failure.
*/
(void) topo_mod_seterrno(mod, err);
return (-1);
}
/* Determnine if this is a chassis node and set it's instance */
if ((strlen(hcfmri->hc_name) == strlen(CHASSIS)) &&
strncmp(hcfmri->hc_name, CHASSIS, strlen(CHASSIS)) == 0) {
is_chassis = 1;
chassis_instance = hcfmri->instance;
}
/*
* Set the authority data.
*
* Continue even if some authority information is not available
* to enumerate as much as possible.
*/
/* either not a chassis node, or chassis #0 */
if (is_chassis == 0 || (is_chassis == 1 && chassis_instance == 0)) {
/*
* Get the chassis authority information.
*/
auth = topo_mod_auth(mod, t_parent);
result = topo_node_set_auth_chassis(mod, auth, t_node);
if (result != 0) {
topo_mod_dprintf(mod,
"%s: failed to set %s for %s node\n",
f, FM_FMRI_AUTHORITY, hcfmri->hc_name);
}
}
nvlist_free(auth);
return (0);
}
/*
* Calculate a generic FRU for the given node. If the node is not a FRU,
* then inherit the FRU data from the nodes parent.
*/
int
x86pi_set_frufmri(topo_mod_t *mod, x86pi_hcfmri_t *hcfmri, tnode_t *t_parent,
tnode_t *t_node, int flag)
{
int result;
int err;
nvlist_t *auth = NULL;
nvlist_t *frufmri = NULL;
if (t_node == NULL || mod == NULL) {
return (-1);
}
/*
* Determine if this node is a FRU
*/
if (!(flag & X86PI_ENUM_FRU)) {
/* This node is not a FRU. Inherit from parent and return */
(void) topo_node_fru_set(t_node, NULL, 0, &result);
return (0);
}
/*
* This node is a FRU. Create an FMRI.
*/
auth = topo_mod_auth(mod, t_parent);
frufmri = topo_mod_hcfmri(mod, t_parent, FM_HC_SCHEME_VERSION,
hcfmri->hc_name, hcfmri->instance, NULL, auth,
hcfmri->part_number, hcfmri->version, hcfmri->serial_number);
if (frufmri == NULL) {
topo_mod_dprintf(mod, "failed to create FRU: %s\n",
topo_strerror(topo_mod_errno(mod)));
}
nvlist_free(auth);
/* Set the FRU, whether NULL or not */
result = topo_node_fru_set(t_node, frufmri, 0, &err);
if (result != 0) {
(void) topo_mod_seterrno(mod, err);
}
nvlist_free(frufmri);
return (result);
}
/*
* Set the label for a topo node.
*/
int
x86pi_set_label(topo_mod_t *mod, const char *label, const char *name,
tnode_t *t_node)
{
int result;
int err;
if (mod == NULL) {
return (-1);
}
/*
* Set the label for this topology node.
* Note that a NULL label will inherit the label from topology
* node's parent.
*/
result = topo_node_label_set(t_node, (char *)label, &err);
if (result != 0) {
(void) topo_mod_seterrno(mod, err);
topo_mod_dprintf(mod, "x86pi_set_label: failed with label %s "
"on %s node: %s\n", (label == NULL ? "NULL" : label),
name, topo_strerror(err));
}
return (result);
}
/*
* Calculate the system information for a node. Inherit the data if
* possible, but always create an appropriate property group.
*/
int
x86pi_set_system(topo_mod_t *mod, tnode_t *t_node)
{
int result;
int err;
struct utsname uts;
char isa[MAXNAMELEN];
if (mod == NULL || t_node == NULL) {
return (-1);
}
result = topo_pgroup_create(t_node, &sys_pgroup, &err);
if (result != 0 && err != ETOPO_PROP_DEFD) {
/*
* We failed to create the property group and it was not
* already defined. Set the err code and return failure.
*/
(void) topo_mod_seterrno(mod, err);
return (-1);
}
result = topo_prop_inherit(t_node, TOPO_PGROUP_SYSTEM, TOPO_PROP_ISA,
&err);
if (result != 0 && err != ETOPO_PROP_DEFD) {
isa[0] = '\0';
result = sysinfo(SI_ARCHITECTURE, isa, sizeof (isa));
if (result == -1) {
/* Preserve the error and continue */
topo_mod_dprintf(mod, "x86pi_set_system: failed to "
"read SI_ARCHITECTURE: %d\n", errno);
}
if (strnlen(isa, MAXNAMELEN) > 0) {
result = topo_prop_set_string(t_node,
TOPO_PGROUP_SYSTEM, TOPO_PROP_ISA,
TOPO_PROP_IMMUTABLE, isa, &err);
if (result != 0) {
/* Preserve the error and continue */
(void) topo_mod_seterrno(mod, err);
topo_mod_dprintf(mod,
"x86pi_set_auth: failed to "
"set property %s (%d) : %s\n",
TOPO_PROP_ISA, err, topo_strerror(err));
}
}
}
result = topo_prop_inherit(t_node, TOPO_PGROUP_SYSTEM,
TOPO_PROP_MACHINE, &err);
if (result != 0 && err != ETOPO_PROP_DEFD) {
result = uname(&uts);
if (result == -1) {
/* Preserve the error and continue */
(void) topo_mod_seterrno(mod, errno);
topo_mod_dprintf(mod, "x86pi_set_system: failed to "
"read uname: %d\n", errno);
}
if (strnlen(uts.machine, sizeof (uts.machine)) > 0) {
result = topo_prop_set_string(t_node,
TOPO_PGROUP_SYSTEM, TOPO_PROP_MACHINE,
TOPO_PROP_IMMUTABLE, uts.machine, &err);
if (result != 0) {
/* Preserve the error and continue */
(void) topo_mod_seterrno(mod, err);
topo_mod_dprintf(mod,
"x86pi_set_auth: failed to "
"set property %s (%d) : %s\n",
TOPO_PROP_MACHINE, err, topo_strerror(err));
}
}
}
return (0);
}
/*
* Topo module routine to update authority identity.
*/
typedef void topo_auth_fn_t(topo_mod_t *, tnode_t *, char *, char *, char *);
/*
* Set the system/component system/chassis identity information to the topo
* handle auth structure. This data is held in SMBIOS structures.
*
* System - Type 129 System Extended Information SUN_OEM_EXT_SYSTEM 129
* System Component - Type 1 System Information SMB_TYPE_SYSTEM 1
* Chassis - Type 3 System Enclosure or Chassis SMB_TYPE_CHASSIS 3
*/
int
x86pi_set_topo_auth(topo_mod_t *mod)
{
int rv;
int i = 0;
const char *mfg = NULL;
const char *name = NULL;
const char *part = NULL;
const char *serial = NULL;
const char *product_id = NULL; /* old school */
nvlist_t *nvl;
smbios_struct_t s;
smbios_info_t info;
smbios_hdl_t *shp;
id_t id;
int smb_type, auth_type;
boolean_t noex_sys = B_FALSE; /* no type 129 */
char *f = "x86pi_set_topo_auth";
/* the order of the next two must be maintained */
int smb_types[] = {
SUN_OEM_EXT_SYSTEM, /* type 129 */
SMB_TYPE_SYSTEM, /* type 1 */
SMB_TYPE_CHASSIS, /* type 3 */
-1
};
int auth_types[] = {
FM_FMRI_AUTH_TYPE_SYSTEM,
FM_FMRI_AUTH_TYPE_SYS_COMP,
FM_FMRI_AUTH_TYPE_CHASSIS,
-1
};
/* make sure we have an SMBIOS handle */
shp = topo_mod_smbios(mod);
if (shp == NULL) {
topo_mod_dprintf(mod, "%s: failed to get smbios handle.", f);
return (-1);
}
/* allocate the topo authority nvlist */
rv = topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME);
if (rv != 0) {
topo_mod_dprintf(mod,
"%s: failed to xalloc topo auth nvlist\n", f);
return (-1);
}
rv = nvlist_add_uint8(nvl, FM_VERSION, FM_FMRI_AUTH_VERSION);
if (rv != 0) {
topo_mod_dprintf(mod, "%s: failed to set FM_VERSION\n");
nvlist_free(nvl);
return (-1);
}
/*
* There should only be one SMBIOS type 1 and type 129 structure, and
* we only want the first SMBIOS type 3 (chassis=0) structure. So use
* the first one found here.
*/
for (i = 0, smb_type = smb_types[i], auth_type = auth_types[i];
smb_type != -1;
++i, smb_type = smb_types[i], auth_type = auth_types[i]) {
/* find SMBIOS structure */
topo_mod_dprintf(mod,
"%s: get type %d SMBIOS structure\n", f, smb_type);
rv = smbios_lookup_type(shp, smb_type, &s);
if (rv == 0) {
id = s.smbstr_id;
topo_mod_dprintf(mod, "%s: type: %d id: %d\n",
f, smb_type, id);
/* grab strings */
rv = smbios_info_common(shp, id, &info);
if (rv == 0) {
if (auth_type == -1) {
continue;
}
topo_mod_dprintf(mod, "%s: SMBIOS type %d: "
"mfg(%s) product(%s) part(%s) serial(%s)\n",
f, smb_type, info.smbi_manufacturer,
info.smbi_product, info.smbi_part,
info.smbi_serial);
/* cleanup strings */
mfg = x86pi_cleanup_smbios_str(mod,
info.smbi_manufacturer, 0);
if (smb_type == SMB_TYPE_CHASSIS) {
/* ch name is in version field */
name = x86pi_cleanup_smbios_str(mod,
info.smbi_version, 0);
} else {
name = x86pi_cleanup_smbios_str(mod,
info.smbi_product, 0);
}
if (smb_type == SMB_TYPE_SYSTEM) {
/* sys p/n is in SKU field */
smbios_system_t s;
(void) smbios_info_system(shp, &s);
part = x86pi_cleanup_smbios_str(mod,
s.smbs_sku, 0);
} else if (smb_type == SMB_TYPE_CHASSIS) {
/* ch p/n is in asset tag field */
part = x86pi_cleanup_smbios_str(mod,
info.smbi_asset, 0);
} else {
part = x86pi_cleanup_smbios_str(mod,
info.smbi_part, 0);
}
serial = x86pi_cleanup_smbios_str(mod,
info.smbi_serial, 0);
/* save off product-id */
if (auth_type == FM_FMRI_AUTH_TYPE_SYS_COMP)
product_id = topo_mod_strdup(mod,
(char *)name);
} else {
topo_mod_dprintf(mod,
"%s: failed to get type %d strings\n",
f, smb_type);
}
} else {
if (smb_type == SUN_OEM_EXT_SYSTEM)
noex_sys = B_TRUE;
}
topo_mod_dprintf(mod, "%s: %s auth: manufacturer(%s) "
"name(%s) part(%s) serial(%s)\n", f,
auth_type == FM_FMRI_AUTH_TYPE_SYSTEM ? "System" :
auth_type == FM_FMRI_AUTH_TYPE_SYS_COMP ?
"Component System" : auth_type ==
FM_FMRI_AUTH_TYPE_CHASSIS ? "Chassis" : "Unknown",
mfg ? mfg : "NULL", name ? name : "NULL",
part ? part : "NULL", serial ? serial : "NULL");
/* use product_id if old BIOS or no chassis name */
if (auth_type == FM_FMRI_AUTH_TYPE_CHASSIS &&
(noex_sys || name == NULL)) {
if (name != NULL)
topo_mod_strfree(mod, (char *)name);
name = topo_mod_strdup(mod, (char *)product_id);
}
/* no values set to "unknown" */
if (mfg == NULL || *mfg == '\0')
mfg = topo_mod_strdup(mod, TOPO_AUTH_UNKNOWN);
if (name == NULL || *name == '\0')
name = topo_mod_strdup(mod, TOPO_AUTH_UNKNOWN);
if (part == NULL || *part == '\0')
part = topo_mod_strdup(mod, TOPO_AUTH_UNKNOWN);
if (serial == NULL || *serial == '\0')
serial = topo_mod_strdup(mod, TOPO_AUTH_UNKNOWN);
/* set identity strings into topo handle */
topo_mod_auth_set_nvl(mod, &nvl, auth_type, (char *)mfg,
(char *)name, (char *)part, (char *)serial);
/* free strings */
if (mfg != NULL)
topo_mod_strfree(mod, (char *)mfg);
if (name != NULL)
topo_mod_strfree(mod, (char *)name);
if (part != NULL)
topo_mod_strfree(mod, (char *)part);
if (serial != NULL)
topo_mod_strfree(mod, (char *)serial);
mfg = name = part = serial = NULL;
}
/* copy the nvlist to the topo handle */
topo_mod_auth_set_th(mod, nvl);
/* clean up */
if (product_id != NULL)
topo_mod_strfree(mod, (char *)product_id);
nvlist_free(nvl);
return (0);
}
/*
* All the checks for compatibility are done within the kernel where the
* ereport generators are. They'll determine first if there's a problem
* and the topo enum will follow suit. The /dev/fm ioclt returns the value
* of the x86gentopo_legacy kernel variable which determines if this platform
* will provide an x86 generic topo or legacy topo enumeration.
*/
/* ARGSUSED */
int
x86pi_check_comp(topo_mod_t *mod)
{
int rv;
int fd;
int32_t legacy;
nvlist_t *nvl = NULL;
fm_ioc_data_t fid;
char *ibuf = NULL, *obuf = NULL;
size_t insz = 0, outsz = 0;
char *f = "x86pi_check_comp";
smbios_hdl_t *shp;
shp = topo_mod_smbios(mod);
if (shp == NULL)
return (X86PI_NONE);
/* open /dev/fm */
fd = open("/dev/fm", O_RDONLY);
if (fd < 0) {
topo_mod_dprintf(mod, "%s: failed to open /dev/fm.\n", f);
return (X86PI_NONE);
}
/* set up buffers and ioctl data structure */
outsz = FM_IOC_MAXBUFSZ;
obuf = topo_mod_alloc(mod, outsz);
if (obuf == NULL) {
perror("umem_alloc");
return (X86PI_NONE);
}
fid.fid_version = 1;
fid.fid_insz = insz;
fid.fid_inbuf = ibuf;
fid.fid_outsz = outsz;
fid.fid_outbuf = obuf;
/* send the ioctl to /dev/fm to retrieve legacy variable */
rv = ioctl(fd, FM_IOC_GENTOPO_LEGACY, &fid);
if (rv < 0) {
topo_mod_dprintf(mod, "%s: ioctl to /dev/fm failed", f);
perror("fm_ioctl");
(void) close(fd);
return (X86PI_NONE);
}
(void) close(fd);
(void) nvlist_unpack(fid.fid_outbuf, fid.fid_outsz, &nvl, 0);
(void) nvlist_lookup_int32(nvl, FM_GENTOPO_LEGACY, &legacy);
nvlist_free(nvl);
topo_mod_free(mod, obuf, outsz);
if (legacy == 1) {
/* legacy kernel variable set; will do the same */
return (X86PI_NONE);
}
/* legacy kernel variable not set; generic topo enum */
return (X86PI_FULL);
}
const char *
x86pi_cleanup_smbios_str(topo_mod_t *mod, const char *begin, int str_type)
{
char buf[MAXNAMELEN];
const char *end, *cp;
char *pp;
char c;
int i;
end = begin + strlen(begin);
while (begin < end && isspace(*begin))
begin++;
while (begin < end && isspace(*(end - 1)))
end--;
if (begin >= end)
return (NULL);
cp = begin;
for (i = 0; i < MAXNAMELEN - 1; i++) {
if (cp >= end)
break;
c = *cp;
if (str_type == LABEL) {
if (!isprint(c))
buf[i] = '-';
else
buf[i] = c;
} else {
if (c == ':' || c == '=' || c == '/' ||
isspace(c) || !isprint(c))
buf[i] = '-';
else
buf[i] = c;
}
cp++;
}
buf[i] = 0;
pp = topo_mod_strdup(mod, buf);
return (pp);
}
/*
* Return Bus/Dev/Func from "reg" devinfo property.
*/
uint16_t
x86pi_bdf(topo_mod_t *mod, di_node_t node)
{
int *val;
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &val) < 0) {
topo_mod_dprintf(mod, "couldn't get \"reg\" prop: %s.\n",
strerror(errno));
return ((uint16_t)-1);
}
return (uint16_t)((*val & PCI_REG_BDFR_M) >> PCI_REG_FUNC_SHIFT);
}
/*
* Return PHY from "sata-phy" devinfo proporty.
*/
int
x86pi_phy(topo_mod_t *mod, di_node_t node)
{
int *phy;
if (di_prop_lookup_ints(DDI_DEV_T_ANY, node, "sata-phy", &phy) < 0) {
topo_mod_dprintf(mod, "couldn't get \"sata-phy\" prop: %s.\n",
strerror(errno));
return (-1);
}
return (*phy);
}