#include "priplugin.h"
static int
find_node_by_string_prop(picl_nodehdl_t rooth, const char *pname,
const char *pval, picl_nodehdl_t *nodeh);
static int
compare_string_propval(picl_nodehdl_t nodeh, const char *pname,
const char *pval);
void
io_dev_addlabel(md_t *mdp)
{
int status, substatus, i, node_count, component_count, busaddr_match;
int type_size, nac_size;
picl_nodehdl_t platnode, tpn;
char busaddr[PICL_PROPNAMELEN_MAX], *p, *q;
char path[PICL_PROPNAMELEN_MAX];
mde_cookie_t *components, md_rootnode;
char *type, *nac, *pri_path, *saved_path;
if (mdp == NULL)
return;
md_rootnode = md_root_node(mdp);
if ((status = ptree_get_node_by_path(PLATFORM_PATH, &platnode)) !=
PICL_SUCCESS) {
pri_debug(LOG_NOTICE,
"io_dev_label: can't find platform node: %s\n",
picl_strerror(status));
return;
}
node_count = md_node_count(mdp);
if (node_count == 0) {
pri_debug(LOG_NOTICE, "io_dev_addlabel: no nodes to "
"process\n");
return;
}
components = (mde_cookie_t *)malloc(node_count *
sizeof (mde_cookie_t));
if (components == NULL) {
pri_debug(LOG_NOTICE,
"io_dev_addlabel: can't get memory for IO nodes\n");
return;
}
component_count = md_scan_dag(mdp, md_rootnode,
md_find_name(mdp, "component"),
md_find_name(mdp, "fwd"), components);
for (i = 0; i < component_count; ++i) {
tpn = platnode;
if (md_get_prop_str(mdp, components[i], "type", &type) ==
-1) {
if (md_get_prop_data(mdp, components[i], "type",
(uint8_t **)&type, &type_size)) {
pri_debug(LOG_NOTICE, "io_add_devlabel: "
"can't get type for component %d\n", i);
continue;
}
}
if (strcmp((const char *)type, "io")) {
pri_debug(LOG_NOTICE,
"io_add_devlabel: skipping component %d with "
"type %s\n", i, type);
continue;
}
if (md_get_prop_str(mdp, components[i], "nac", &nac) == -1) {
pri_debug(LOG_NOTICE,
"io_add_devlabel: can't get nac value for device "
"<%s>\n", type);
continue;
} else
nac_size = strlen(nac) + 1;
if (md_get_prop_str(mdp, components[i], "path", &pri_path) ==
-1) {
pri_debug(LOG_NOTICE,
"io_add_devlabel: can't get path value for "
"device <%s>\n", type);
continue;
}
(void) strlcpy(path, pri_path, sizeof (path));
pri_debug(LOG_NOTICE, "io_add_devlabel: processing component "
"%d, type <%s>, nac <%s>, path <%s>\n", i, type, nac,
path);
for (busaddr_match = 1, p = q = (char *)path; q; p = q + 1) {
if (*p == '/')
++p;
if (*p == '@')
++p;
if ((q = strchr((const char *)p, '/')) != NULL)
*q = '\0';
if ((status = find_node_by_string_prop(tpn,
PICL_PROP_BUS_ADDR, (const char *)p, &tpn)) !=
PICL_SUCCESS) {
pri_debug(LOG_NOTICE,
"can't find %s property of <%s> "
"for nac %s: %s\n",
PICL_PROP_BUS_ADDR, p, nac,
picl_strerror(status));
busaddr_match = 0;
break;
}
saved_path = p;
}
if (busaddr_match == 0) {
pri_debug(LOG_NOTICE, "io_add_devlabel: no matching "
"bus-addr path for this nac - skipping\n");
continue;
}
nac_size = strlen((const char *)nac) + 1;
for (status = PICL_SUCCESS; status == PICL_SUCCESS;
status = ptree_get_propval_by_name(tpn,
PICL_PROP_PEER, &tpn, sizeof (picl_nodehdl_t))) {
if ((substatus = ptree_get_propval_by_name(tpn,
PICL_PROP_BUS_ADDR,
busaddr, sizeof (busaddr))) != PICL_SUCCESS) {
pri_debug(LOG_NOTICE,
"io_add_device: can't get %s "
"property from picl devtree: %s\n",
PICL_PROP_BUS_ADDR,
picl_strerror(substatus));
} else {
if (strncmp(busaddr, saved_path,
PICL_PROPNAMELEN_MAX) == 0) {
add_md_prop(tpn, nac_size,
PICL_PROP_LABEL, nac,
PICL_PTYPE_CHARSTRING);
}
}
}
}
free(components);
}
static int
compare_string_propval(picl_nodehdl_t nodeh, const char *pname,
const char *pval)
{
char *pvalbuf;
int err;
int len;
ptree_propinfo_t pinfo;
picl_prophdl_t proph;
err = ptree_get_prop_by_name(nodeh, pname, &proph);
if (err != PICL_SUCCESS)
return (0);
err = ptree_get_propinfo(proph, &pinfo);
if (pinfo.piclinfo.type != PICL_PTYPE_CHARSTRING)
return (0);
len = strlen(pval) + 1;
pvalbuf = alloca(len);
if (pvalbuf == NULL)
return (0);
err = ptree_get_propval(proph, pvalbuf, len);
if ((err == PICL_SUCCESS) && (strcmp(pvalbuf, pval) == 0))
return (1);
return (0);
}
static int
find_node_by_string_prop(picl_nodehdl_t rooth, const char *pname,
const char *pval, picl_nodehdl_t *nodeh)
{
picl_nodehdl_t childh;
int err;
for (err = ptree_get_propval_by_name(rooth, PICL_PROP_CHILD, &childh,
sizeof (picl_nodehdl_t)); err != PICL_PROPNOTFOUND;
err = ptree_get_propval_by_name(childh, PICL_PROP_PEER,
&childh, sizeof (picl_nodehdl_t))) {
if (err != PICL_SUCCESS)
return (err);
if (compare_string_propval(childh, pname, pval)) {
*nodeh = childh;
return (PICL_SUCCESS);
}
}
return (PICL_ENDOFLIST);
}