2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Walk the LDOM PRI component nodes and create appropriate topology nodes
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/time.h>
2N/A#include <stddef.h>
2N/A#include <inttypes.h>
2N/A#include <strings.h>
2N/A#include <string.h>
2N/A#include <libuutil.h>
2N/A#include <libnvpair.h>
2N/A#include <sys/mdesc.h>
2N/A#include <fm/topo_mod.h>
2N/A#include <fm/topo_hc.h>
2N/A#include "pi_impl.h"
2N/A
2N/A#define PI_STR_MIN "instance_min"
2N/A#define PI_STR_MAX "instance_max"
2N/A
2N/A/*
2N/A * Allow for custom topo node creation routines based on topo-hc-name.
2N/A */
2N/Astruct pi_enum_functions_s {
2N/A pi_enum_fn_t *func;
2N/A char *hc_name; /* topo-hc-name */
2N/A};
2N/Atypedef struct pi_enum_functions_s pi_enum_functions_t;
2N/A
2N/Astruct pi_methods_s {
2N/A topo_method_t *meths;
2N/A char *hc_name;
2N/A};
2N/Atypedef struct pi_methods_s pi_methods_t;
2N/A
2N/Aextern topo_method_t
2N/A pi_chip_methods[],
2N/A pi_core_methods[],
2N/A pi_strand_methods[],
2N/A pi_mem_methods[];
2N/A
2N/A/*
2N/A * List of custom enumerators for PRI nodes that require them. The most
2N/A * common nodes are listed first.
2N/A */
2N/Astatic pi_enum_functions_t pi_enum_fns_builtin[] = {
2N/A {pi_enum_cpu, STRAND},
2N/A {pi_enum_cpu, CPU},
2N/A {pi_enum_mem, DIMM},
2N/A {pi_enum_cpu, CORE},
2N/A {pi_enum_cpu, CHIP},
2N/A {pi_enum_hostbridge, HOSTBRIDGE},
2N/A {pi_enum_pciexrc, PCIEX_ROOT},
2N/A {pi_enum_niu, NIU},
2N/A {pi_enum_bay, BAY},
2N/A {NULL, NULL}
2N/A};
2N/Astatic nvlist_t *pi_enum_fns;
2N/A
2N/A/* List of methods that will be registered in the nodes. */
2N/Astatic pi_methods_t pi_meths_builtin[] = {
2N/A {pi_chip_methods, CHIP},
2N/A {pi_core_methods, CORE},
2N/A {pi_strand_methods, STRAND},
2N/A {pi_strand_methods, CPU},
2N/A {pi_mem_methods, DIMM},
2N/A {NULL, NULL}
2N/A};
2N/Anvlist_t *pi_meths;
2N/A
2N/A/*
2N/A * In order to create a topology node from a PRI MDE node we need to know the
2N/A * topology parent node that should be used. So, after creating a topology
2N/A * node from an MDE node, we associate children of the MDE node with the new
2N/A * topology node. Thus, when the children are visited we can know the
2N/A * appropriate parent topology node to use.
2N/A *
2N/A * We take advantage of the libtopo threading model here, which guarantees a
2N/A * single thread and a single invocation at a time for an enumerator. This
2N/A * makes using a file-global safe.
2N/A */
2N/Astatic uu_list_pool_t *walker_pool = NULL;
2N/Astatic uu_list_t *walker_list = NULL;
2N/A
2N/Astruct pi_walkernode_s {
2N/A uu_list_node_t walker_node;
2N/A tnode_t *t_parent; /* Parent topology node */
2N/A mde_cookie_t mde_node; /* Child MDE node index */
2N/A};
2N/Atypedef struct pi_walkernode_s pi_walkernode_t;
2N/A
2N/A
2N/A/* The routine called for each node in the PRI while walking the graph */
2N/Astatic int pi_walker_node(md_t *, mde_cookie_t, mde_cookie_t, void *);
2N/A
2N/A/*
2N/A * Create a sub-range for a given PRI node and associate the given topology
2N/A * node with the children.
2N/A */
2N/Astatic int pi_walker_node_range(topo_mod_t *, md_t *, tnode_t *, mde_cookie_t);
2N/Astatic int pi_walker_node_create(topo_mod_t *, md_t *, mde_cookie_t, tnode_t *,
2N/A topo_instance_t, tnode_t **);
2N/A
2N/A/* Routines to handle the list of topology parents and mde_nodes */
2N/Astatic int pi_walkerlist_compare(const void *, const void *, void *);
2N/Astatic int pi_walkerlist_create(topo_mod_t *);
2N/Astatic void pi_walkerlist_destroy(topo_mod_t *);
2N/Astatic int pi_walkerlist_add(topo_mod_t *, tnode_t *, mde_cookie_t);
2N/Astatic int pi_walkerlist_addtype(topo_mod_t *, nvlist_t *, char *, uint32_t,
2N/A uint32_t);
2N/Astatic int pi_walkerlist_find(topo_mod_t *, mde_cookie_t, tnode_t **);
2N/A
2N/A
2N/Aint
2N/Api_walker_init(topo_mod_t *mod)
2N/A{
2N/A int result;
2N/A pi_enum_functions_t *fp;
2N/A pi_methods_t *mp;
2N/A
2N/A result = topo_mod_nvalloc(mod, &pi_enum_fns, NV_UNIQUE_NAME);
2N/A result |= topo_mod_nvalloc(mod, &pi_meths, NV_UNIQUE_NAME);
2N/A if (result != 0) {
2N/A topo_mod_dprintf(mod, "pi_walker_init failed\n");
2N/A nvlist_free(pi_enum_fns);
2N/A nvlist_free(pi_meths);
2N/A return (-1);
2N/A }
2N/A
2N/A /* Add the builtin functions to the list */
2N/A fp = pi_enum_fns_builtin;
2N/A while (fp != NULL && fp->hc_name != NULL) {
2N/A uint64_t faddr;
2N/A
2N/A faddr = (uint64_t)(uintptr_t)*(fp->func);
2N/A result |= nvlist_add_uint64(pi_enum_fns, fp->hc_name, faddr);
2N/A fp++;
2N/A }
2N/A
2N/A /* Add the builtin methods to the list */
2N/A mp = pi_meths_builtin;
2N/A while (mp != NULL && mp->hc_name != NULL) {
2N/A uint64_t maddr;
2N/A
2N/A maddr = (uint64_t)(uintptr_t)mp->meths;
2N/A result |= nvlist_add_uint64(pi_meths, mp->hc_name, maddr);
2N/A mp++;
2N/A }
2N/A
2N/A if (result != 0) {
2N/A topo_mod_dprintf(mod, "pi_walker_init failed\n");
2N/A nvlist_free(pi_enum_fns);
2N/A nvlist_free(pi_meths);
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/Avoid
2N/Api_walker_fini(topo_mod_t *mod)
2N/A{
2N/A topo_mod_dprintf(mod, "pi_walker_fini: enter\n");
2N/A nvlist_free(pi_enum_fns);
2N/A nvlist_free(pi_meths);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Begin to walk the machine description array starting at the given PRI node.
2N/A */
2N/Aint
2N/Api_walker(pi_enum_t *pip, tnode_t *t_parent, const char *hc_name,
2N/A mde_cookie_t mde_node, mde_str_cookie_t component_cookie,
2N/A mde_str_cookie_t arc_cookie)
2N/A{
2N/A int result;
2N/A hrtime_t starttime;
2N/A hrtime_t endtime;
2N/A topo_mod_t *mod;
2N/A
2N/A if (pip == NULL) {
2N/A return (-1);
2N/A }
2N/A mod = pip->mod;
2N/A
2N/A starttime = gethrtime();
2N/A topo_mod_dprintf(mod, "walker starting at node_0x%llx\n",
2N/A mde_node);
2N/A
2N/A /*
2N/A * Create a list to store topology nodes and their associated machine
2N/A * description index. This allows the code to know the parent of a
2N/A * node when creating topology entries.
2N/A */
2N/A result = pi_walkerlist_create(mod);
2N/A if (result != 0) {
2N/A topo_mod_dprintf(mod, "walker could not create list\n");
2N/A return (result);
2N/A }
2N/A
2N/A /* Create a walker node for the parent of the start node */
2N/A result = pi_walkerlist_add(mod, t_parent, mde_node);
2N/A if (result != 0) {
2N/A pi_walkerlist_destroy(mod);
2N/A topo_mod_dprintf(mod, "walker could not add to list\n");
2N/A (void) topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
2N/A return (result);
2N/A }
2N/A
2N/A /*
2N/A * This is a top-level node. Make sure we call the top level
2N/A * enumerator if there is not already a custom enumerator registered.
2N/A */
2N/A if (! nvlist_exists(pi_enum_fns, hc_name)) {
2N/A uint64_t faddr;
2N/A
2N/A /*
2N/A * There is no enumerator function registered for this
2N/A * hc name. Automatically register the top level node
2N/A * enumerator function.
2N/A */
2N/A faddr = (uint64_t)(uintptr_t)pi_enum_top;
2N/A result = nvlist_add_uint64(pi_enum_fns, hc_name, faddr);
2N/A if (result != 0) {
2N/A pi_walkerlist_destroy(mod);
2N/A topo_mod_dprintf(mod,
2N/A "walker could not register enumerator for type "
2N/A "%s\n", hc_name);
2N/A (void) topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
2N/A return (-1);
2N/A }
2N/A topo_mod_dprintf(mod,
2N/A "walker registered pi_enum_top enumerator for type %s\n",
2N/A hc_name);
2N/A }
2N/A
2N/A /* Walk the machine description list starting at the given node */
2N/A result = md_walk_dag(pip->mdp, mde_node, component_cookie, arc_cookie,
2N/A pi_walker_node, (void *)pip);
2N/A switch (result) {
2N/A case 0:
2N/A /* Successful completion */
2N/A /* DO NOTHING */
2N/A break;
2N/A
2N/A case MDE_WALK_ERROR:
2N/A /*
2N/A * Store that we have a partial enumeration and return
2N/A * that we have encountered an error.
2N/A */
2N/A (void) topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM);
2N/A result = -1;
2N/A break;
2N/A
2N/A default:
2N/A /*
2N/A * This should not happen. We want to always produce
2N/A * as complete a topology as possible, even in the face
2N/A * of errors, however, so set an error and continue.
2N/A */
2N/A topo_mod_dprintf(mod,
2N/A "walker encountered invalid result: %d. "
2N/A "Continuing\n", result);
2N/A (void) topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
2N/A result = 0;
2N/A break;
2N/A }
2N/A
2N/A /* Destroy the walker list, which is no longer necessary */
2N/A pi_walkerlist_destroy(mod);
2N/A
2N/A topo_mod_dprintf(mod, "walker done with node_0x%llx\n", mde_node);
2N/A
2N/A endtime = gethrtime();
2N/A topo_mod_dprintf(mod, "walker scan time %lld ms\n",
2N/A (endtime-starttime)/MICROSEC);
2N/A
2N/A return (result);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Visited once for each node in the machine description. Creates a topo
2N/A * node for the machine description node and associates it with it's parent,
2N/A * by calling an appropriate creation routine for the node type.
2N/A *
2N/A * Output:
2N/A * This routine returns MDE_WALK_NEXT, MDE_WALK_DONE or MDE_WALK_ERROR
2N/A * only.
2N/A */
2N/Astatic int
2N/Api_walker_node(md_t *mdp, mde_cookie_t parent_mde_node, mde_cookie_t mde_node,
2N/A void *private)
2N/A{
2N/A int result;
2N/A pi_enum_t *pip = (pi_enum_t *)private;
2N/A uint64_t skip; /* flag in md to skip this node */
2N/A tnode_t *t_parent; /* topo parent to this md node */
2N/A tnode_t *t_node; /* topo parent to this md node */
2N/A topo_instance_t inst;
2N/A
2N/A topo_mod_t *mod;
2N/A
2N/A /* Make sure we have our private data */
2N/A if (pip == NULL) {
2N/A return (MDE_WALK_ERROR);
2N/A }
2N/A mod = pip->mod;
2N/A
2N/A topo_mod_dprintf(pip->mod,
2N/A "walker processing node_0x%llx parent node 0x%llx\n",
2N/A (uint64_t)mde_node, (uint64_t)parent_mde_node);
2N/A
2N/A /* Should we skip this node ? */
2N/A skip = pi_skip_node(mod, pip->mdp, mde_node);
2N/A if (skip) {
2N/A /* Skip this node and continue to the next node */
2N/A topo_mod_dprintf(mod, "walker skipping node_0x%llx\n",
2N/A (uint64_t)mde_node);
2N/A return (MDE_WALK_NEXT);
2N/A }
2N/A
2N/A result = pi_get_instance(mod, mdp, mde_node, &inst);
2N/A if (result != 0) {
2N/A /*
2N/A * No ID available to place this mde node in the topology so
2N/A * we cannot create a topology node.
2N/A */
2N/A topo_mod_dprintf(mod, "walker skipping node_0x%llx: "
2N/A "no instance\n", (uint64_t)mde_node);
2N/A (void) topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM);
2N/A return (MDE_WALK_NEXT);
2N/A }
2N/A
2N/A /*
2N/A * Find the parent topo node for this machine description node.
2N/A *
2N/A * If found, the element will also be removed from the list and the
2N/A * memory used to keep track of it released. We will only visit an
2N/A * MDE node once and so the memory is no longer needed.
2N/A */
2N/A t_parent = NULL;
2N/A result = pi_walkerlist_find(mod, mde_node, &t_parent);
2N/A if (result != 0 || t_parent == NULL) {
2N/A /*
2N/A * No parent was found or a NULL parent encountered. We
2N/A * cannot create a new topology node without a parent (
2N/A * even for top level nodes). We associate children of
2N/A * this MDE node with a NULL parent to silently skip the
2N/A * remainder of this MDE branch.
2N/A */
2N/A topo_mod_dprintf(mod, "no topo parent found for node_0x%llx\n",
2N/A mde_node);
2N/A result = pi_walker_node_range(mod, mdp, NULL, mde_node);
2N/A (void) topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM);
2N/A
2N/A return (result);
2N/A }
2N/A
2N/A /*
2N/A * We have the mde node instance and parent information.
2N/A * Attempt to create a topology node for this mde node.
2N/A */
2N/A t_node = NULL;
2N/A result = pi_walker_node_create(mod, mdp, mde_node, t_parent, inst,
2N/A &t_node);
2N/A if (result != MDE_WALK_NEXT || t_node == NULL) {
2N/A /*
2N/A * We have failed to create a new topology node based on
2N/A * the current MDE node. We set partial enumeration and
2N/A * return without associating the children of this MDE
2N/A * node with a topology parent. This will propgate the
2N/A * creation error down this MDE branch.
2N/A */
2N/A (void) topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM);
2N/A return (result);
2N/A }
2N/A
2N/A /*
2N/A * Associate the new topology node with any children of this mde node.
2N/A */
2N/A result = pi_walker_node_range(mod, mdp, t_node, mde_node);
2N/A
2N/A topo_mod_dprintf(mod, "walker completed node_0x%llx result = %d\n",
2N/A (uint64_t)mde_node, result);
2N/A
2N/A return (result);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Api_walker_node_create(topo_mod_t *mod, md_t *mdp, mde_cookie_t mde_node,
2N/A tnode_t *t_parent, topo_instance_t inst, tnode_t **t_node)
2N/A{
2N/A int result;
2N/A char *hc_name;
2N/A uint64_t faddr;
2N/A pi_enum_fn_t *func;
2N/A
2N/A if (t_parent == NULL) {
2N/A /*
2N/A * A parent topology node is required even for top-level
2N/A * nodes.
2N/A */
2N/A return (MDE_WALK_NEXT);
2N/A }
2N/A
2N/A /*
2N/A * Find the topo-hc-name for this node which is used to find
2N/A * the specific creation function
2N/A */
2N/A hc_name = pi_get_topo_hc_name(mod, mdp, mde_node);
2N/A if (hc_name == NULL) {
2N/A /* Cannot get the hc-name */
2N/A topo_mod_dprintf(mod,
2N/A "failed to find hc-name for node_0x%llx\n", mde_node);
2N/A return (MDE_WALK_NEXT);
2N/A }
2N/A
2N/A /* Determine the topology node creation routine to use */
2N/A func = pi_enum_generic;
2N/A faddr = 0;
2N/A result = nvlist_lookup_uint64(pi_enum_fns, hc_name, &faddr);
2N/A if (result == 0) {
2N/A /*
2N/A * A function is registered for this node. Convert the
2N/A * address to a pointer to function
2N/A */
2N/A func = (pi_enum_fn_t *)(uintptr_t)faddr;
2N/A }
2N/A
2N/A /*
2N/A * Create a topology node for this mde node by calling the identified
2N/A * enumeration function
2N/A */
2N/A *t_node = NULL;
2N/A result = (func)(mod, mdp, mde_node, inst, t_parent, hc_name, t_node);
2N/A if (result != 0) {
2N/A topo_mod_dprintf(mod,
2N/A "failed to create topo entry for node_0x%llx type %s\n",
2N/A (uint64_t)mde_node, hc_name);
2N/A } else {
2N/A /*
2N/A * Need to check if we need enumerate internal expander
2N/A * attached bays. Since there is no MDE(PRI record) for
2N/A * an internal expander we are invoking the ses enumerator
2N/A * here. It will check if there is any internal enclosure
2N/A * and enumerate expander attached disks.
2N/A * Note that those nodes will be enumerated as a child of
2N/A * of chassis.
2N/A */
2N/A if (strcmp(hc_name, CHASSIS) == 0) {
2N/A if (topo_mod_load(mod, SES, TOPO_VERSION) == NULL) {
2N/A topo_mod_dprintf(mod, "Failed to load %s "
2N/A " enumerator: %s",
2N/A SES, topo_strerror(topo_mod_errno(mod)));
2N/A } else {
2N/A /*
2N/A * min and max instance is handled by
2N/A * the ses enumerator.
2N/A */
2N/A result = topo_mod_enumerate(mod, *t_node, SES,
2N/A SES_ENCLOSURE, 0, 0, NULL);
2N/A if (result != 0)
2N/A topo_mod_dprintf(mod, "Failed to "
2N/A "enumerate expander attached "
2N/A "internal bays. Continue on.");
2N/A }
2N/A }
2N/A }
2N/A
2N/A topo_mod_strfree(mod, hc_name);
2N/A
2N/A return (MDE_WALK_NEXT);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Scan the children of a given MDE node and find all the sets of topo-hc-name
2N/A * types and their instance ranges. From this information we create topology
2N/A * node ranges on the given parent so that when the children are visited and a
2N/A * topology node is created, the range exists and the creation will succeed.
2N/A */
2N/Astatic int
2N/Api_walker_node_range(topo_mod_t *mod, md_t *mdp, tnode_t *t_parent,
2N/A mde_cookie_t mde_node)
2N/A{
2N/A int result;
2N/A int rc;
2N/A int num_arcs;
2N/A nvlist_t *typelist;
2N/A nvpair_t *nvp;
2N/A mde_cookie_t *arcp;
2N/A size_t arcsize;
2N/A int arcidx;
2N/A char *hc_name;
2N/A nvlist_t *hc_range;
2N/A topo_instance_t inst;
2N/A uint32_t min;
2N/A uint32_t max;
2N/A
2N/A if (t_parent == NULL) {
2N/A topo_mod_dprintf(mod,
2N/A "walker failed to create node range with a NULL parent\n");
2N/A return (MDE_WALK_NEXT);
2N/A }
2N/A
2N/A /* Determine how many children the given node has */
2N/A num_arcs = md_get_prop_arcs(mdp, mde_node, MD_STR_FWD, NULL, 0);
2N/A if (num_arcs == 0) {
2N/A /* This node has no children */
2N/A return (MDE_WALK_NEXT);
2N/A }
2N/A topo_mod_dprintf(mod, "node_0x%llx has %d children\n",
2N/A (uint64_t)mde_node, num_arcs);
2N/A
2N/A /* Get the indexes for all the child nodes and put them in an array */
2N/A arcsize = sizeof (mde_cookie_t) * num_arcs;
2N/A arcp = topo_mod_zalloc(mod, arcsize);
2N/A if (arcp == NULL) {
2N/A topo_mod_dprintf(mod, "out of memory\n");
2N/A (void) topo_mod_seterrno(mod, EMOD_NOMEM);
2N/A return (MDE_WALK_ERROR);
2N/A }
2N/A num_arcs = md_get_prop_arcs(mdp, mde_node, MD_STR_FWD, arcp, arcsize);
2N/A
2N/A /*
2N/A * The children of the given node may have multiple types.
2N/A * Potentially, each child may have a different type and we need to
2N/A * create a topo node range for each one.
2N/A *
2N/A * We loop through the children and collect the type information for
2N/A * each one and associate the child with the given parent topo node.
2N/A */
2N/A result = topo_mod_nvalloc(mod, &typelist, NV_UNIQUE_NAME);
2N/A if (result != 0) {
2N/A topo_mod_free(mod, arcp, arcsize);
2N/A (void) topo_mod_seterrno(mod, EMOD_NOMEM);
2N/A return (MDE_WALK_ERROR);
2N/A }
2N/A
2N/A arcidx = 0;
2N/A for (arcidx = 0; arcidx < num_arcs; arcidx++) {
2N/A /* Should this node be skipped? */
2N/A if (pi_skip_node(mod, mdp, arcp[arcidx])) {
2N/A /* Skip this node */
2N/A topo_mod_dprintf(mod, "skipping node_0x%llx\n",
2N/A (uint64_t)arcp[arcidx]);
2N/A continue;
2N/A }
2N/A
2N/A /* Get the type of this node */
2N/A hc_name = pi_get_topo_hc_name(mod, mdp, arcp[arcidx]);
2N/A rc = pi_get_instance(mod, mdp, arcp[arcidx], &inst);
2N/A if (rc == 0 && hc_name != NULL) {
2N/A /* Increment the count of nodes with this type */
2N/A hc_range = NULL;
2N/A rc = nvlist_lookup_nvlist(typelist, hc_name, &hc_range);
2N/A if (rc != 0) {
2N/A /*
2N/A * We have not visited this type yet. Create
2N/A * a new range based on this nodes instance
2N/A * information.
2N/A */
2N/A result = pi_walkerlist_addtype(mod, typelist,
2N/A hc_name, (uint32_t)inst, (uint32_t)inst);
2N/A if (result != 0) {
2N/A /*
2N/A * This error can only if there was a
2N/A * memory failure of some kind. Stop
2N/A * the walk or it will just get worse.
2N/A */
2N/A nvlist_free(typelist);
2N/A topo_mod_strfree(mod, hc_name);
2N/A topo_mod_free(mod, arcp, arcsize);
2N/A (void) topo_mod_seterrno(mod,
2N/A EMOD_PARTIAL_ENUM);
2N/A return (MDE_WALK_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * We know the list exists now or the above
2N/A * would have failed. Just look it up.
2N/A */
2N/A (void) nvlist_lookup_nvlist(typelist, hc_name,
2N/A &hc_range);
2N/A }
2N/A
2N/A /* Re-calculate the range minimums and maximums */
2N/A (void) nvlist_lookup_uint32(hc_range, PI_STR_MIN, &min);
2N/A (void) nvlist_lookup_uint32(hc_range, PI_STR_MAX, &max);
2N/A min = MIN(min, (uint32_t)inst);
2N/A max = MAX(max, (uint32_t)inst);
2N/A (void) nvlist_add_uint32(hc_range, PI_STR_MIN, min);
2N/A (void) nvlist_add_uint32(hc_range, PI_STR_MAX, max);
2N/A
2N/A } else {
2N/A if (hc_name == NULL) {
2N/A topo_mod_dprintf(mod, "node_0x%llx has no "
2N/A "topo_hc_name.", (uint64_t)arcp[arcidx]);
2N/A (void) topo_mod_seterrno(mod,
2N/A EMOD_PARTIAL_ENUM);
2N/A return (MDE_WALK_ERROR);
2N/A }
2N/A
2N/A topo_mod_dprintf(mod, "node_0x%llx type %s has no id. "
2N/A "Excluding from range", (uint64_t)arcp[arcidx],
2N/A hc_name);
2N/A }
2N/A topo_mod_strfree(mod, hc_name);
2N/A
2N/A /*
2N/A * Associate this node with the given topo parent even if it
2N/A * has no instance. We do this so that later an error with
2N/A * the PRI node will be reported instead of an internal
2N/A * error about not being able to find the parent of a node
2N/A */
2N/A rc = pi_walkerlist_add(mod, t_parent, arcp[arcidx]);
2N/A if (rc != 0) {
2N/A topo_mod_dprintf(mod,
2N/A "could not add node_0x%llx to walker list\n",
2N/A (uint64_t)arcp[arcidx]);
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * We have associated all the child nodes with the given topo parent
2N/A * in the walker list. Now we need to create topo ranges for each
2N/A * set of child types under the parent.
2N/A */
2N/A nvp = nvlist_next_nvpair(typelist, NULL);
2N/A while (nvp != NULL) {
2N/A /* Get the type name and count from the list element */
2N/A hc_name = nvpair_name(nvp);
2N/A (void) nvpair_value_nvlist(nvp, &hc_range);
2N/A (void) nvlist_lookup_uint32(hc_range, PI_STR_MIN, &min);
2N/A (void) nvlist_lookup_uint32(hc_range, PI_STR_MAX, &max);
2N/A
2N/A /*
2N/A * We have the number of children with this type.
2N/A * Create an appropriate range.
2N/A */
2N/A topo_mod_dprintf(mod,
2N/A "creating instance range %d to %d of type %s\n",
2N/A min, max, hc_name);
2N/A rc = topo_node_range_create(mod, t_parent, hc_name,
2N/A (topo_instance_t)min, (topo_instance_t)max);
2N/A if (rc != 0) {
2N/A topo_mod_dprintf(mod,
2N/A "failed to created node range %d to %d for "
2N/A "nodes of type %s\n", min, max, hc_name);
2N/A }
2N/A
2N/A /* Check the next node */
2N/A nvp = nvlist_next_nvpair(typelist, nvp);
2N/A }
2N/A topo_mod_free(mod, arcp, arcsize);
2N/A nvlist_free(typelist);
2N/A
2N/A return (MDE_WALK_NEXT);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Api_walkerlist_addtype(topo_mod_t *mod, nvlist_t *typelist, char *hc_name,
2N/A uint32_t min, uint32_t max)
2N/A{
2N/A int result;
2N/A nvlist_t *nvl;
2N/A
2N/A result = topo_mod_nvalloc(mod, &nvl, NV_UNIQUE_NAME);
2N/A if (result != 0) {
2N/A return (result);
2N/A }
2N/A
2N/A /* Create min and max elements in this list */
2N/A if (nvlist_add_uint32(nvl, PI_STR_MIN, min) != 0 ||
2N/A nvlist_add_uint32(nvl, PI_STR_MAX, max) != 0 ||
2N/A nvlist_add_nvlist(typelist, hc_name, nvl) != 0) {
2N/A nvlist_free(nvl);
2N/A return (-1);
2N/A }
2N/A nvlist_free(nvl);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/* ARGSUSED */
2N/Astatic int
2N/Api_walkerlist_compare(const void *left, const void *right, void *private)
2N/A{
2N/A pi_walkernode_t *lp = (pi_walkernode_t *)left;
2N/A pi_walkernode_t *rp = (pi_walkernode_t *)right;
2N/A
2N/A if (lp->mde_node > rp->mde_node) {
2N/A return (1);
2N/A }
2N/A if (lp->mde_node < rp->mde_node) {
2N/A return (-1);
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Api_walkerlist_create(topo_mod_t *mod)
2N/A{
2N/A /* Initialize the uutil list structure */
2N/A walker_pool = uu_list_pool_create("pi_walker_pool",
2N/A sizeof (pi_walkernode_t), offsetof(pi_walkernode_t, walker_node),
2N/A pi_walkerlist_compare, NULL);
2N/A if (walker_pool == NULL) {
2N/A (void) topo_mod_seterrno(mod, EMOD_NOMEM);
2N/A return (-1);
2N/A }
2N/A walker_list = uu_list_create(walker_pool, NULL, 0);
2N/A if (walker_list == NULL) {
2N/A uu_list_pool_destroy(walker_pool);
2N/A walker_pool = NULL;
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Api_walkerlist_destroy(topo_mod_t *mod)
2N/A{
2N/A void *wvp;
2N/A pi_walkernode_t *wp;
2N/A
2N/A /* Destroy our list of items */
2N/A while ((wvp = uu_list_first(walker_list)) != NULL) {
2N/A /*
2N/A * First, we empty the list of elements and free each one.
2N/A * We do not free the data elements as they are libtopo nodes
2N/A * and will be freed by libtopo
2N/A */
2N/A wp = (pi_walkernode_t *)wvp;
2N/A uu_list_remove(walker_list, wvp);
2N/A uu_list_node_fini(wp, &(wp->walker_node), walker_pool);
2N/A
2N/A topo_mod_free(mod, wvp, sizeof (pi_walkernode_t));
2N/A }
2N/A uu_list_destroy(walker_list);
2N/A uu_list_pool_destroy(walker_pool);
2N/A walker_list = NULL;
2N/A walker_pool = NULL;
2N/A}
2N/A
2N/A
2N/Astatic int
2N/Api_walkerlist_add(topo_mod_t *mod, tnode_t *t_parent, mde_cookie_t mde_node)
2N/A{
2N/A uu_list_index_t idx;
2N/A pi_walkernode_t *wnp;
2N/A
2N/A wnp = topo_mod_zalloc(mod, sizeof (pi_walkernode_t));
2N/A if (wnp == NULL) {
2N/A topo_mod_dprintf(mod, "failed to add node_0x%llx parent %p\n",
2N/A (uint64_t)mde_node, t_parent);
2N/A return (-1);
2N/A }
2N/A uu_list_node_init(wnp, &(wnp->walker_node), walker_pool);
2N/A
2N/A wnp->t_parent = t_parent;
2N/A wnp->mde_node = mde_node;
2N/A
2N/A (void) uu_list_find(walker_list, wnp, NULL, &idx);
2N/A uu_list_insert(walker_list, wnp, idx);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * Find the parent topo node for this machine description node.
2N/A *
2N/A * Nodes are removed from the list as they are found. They are only
2N/A * visited once and this eliminates the need for a separate routine
2N/A * that walks the list to free elements later.
2N/A */
2N/Astatic int
2N/Api_walkerlist_find(topo_mod_t *mod, mde_cookie_t mde_node, tnode_t **tpp)
2N/A{
2N/A pi_walkernode_t *result;
2N/A
2N/A uu_list_index_t idx;
2N/A pi_walkernode_t search_criteria;
2N/A
2N/A search_criteria.mde_node = mde_node;
2N/A search_criteria.t_parent = NULL;
2N/A
2N/A *tpp = NULL;
2N/A result = uu_list_find(walker_list, &search_criteria, NULL, &idx);
2N/A if (result == NULL) {
2N/A return (-1);
2N/A }
2N/A *tpp = result->t_parent;
2N/A
2N/A /* Remove this element from the list */
2N/A uu_list_remove(walker_list, result);
2N/A uu_list_node_fini(result, &(result->walker_node), walker_pool);
2N/A topo_mod_free(mod, result, sizeof (pi_walkernode_t));
2N/A
2N/A return (0);
2N/A}