1 /*
   2  *
   3  * CDDL HEADER START
   4  *
   5  * The contents of this file are subject to the terms of the
   6  * Common Development and Distribution License (the "License").
   7  * You may not use this file except in compliance with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 
  23 /*
  24  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
  25  */
  26 
  27 #include <stdio.h>
  28 #include <stdlib.h>
  29 #include <string.h>
  30 #include <errno.h>
  31 #include <ctype.h>
  32 #include <alloca.h>
  33 #include <assert.h>
  34 #include <limits.h>
  35 #include <zone.h>
  36 #include <fm/topo_mod.h>
  37 #include <fm/topo_hc.h>
  38 #include <fm/fmd_fmri.h>
  39 #include <sys/param.h>
  40 #include <sys/systeminfo.h>
  41 #include <sys/fm/protocol.h>
  42 #include <sys/stat.h>
  43 #include <sys/systeminfo.h>
  44 #include <sys/utsname.h>
  45 
  46 #include <topo_method.h>
  47 #include <topo_module.h>
  48 #include <topo_subr.h>
  49 #include <topo_prop.h>
  50 #include <topo_tree.h>
  51 #include <hc.h>
  52 
  53 static int hc_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
  54     topo_instance_t, void *, void *);
  55 static void hc_release(topo_mod_t *, tnode_t *);
  56 static int hc_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t,
  57     nvlist_t *, nvlist_t **);
  58 static int hc_fmri_str2nvl(topo_mod_t *, tnode_t *, topo_version_t,
  59     nvlist_t *, nvlist_t **);
  60 static int hc_compare(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
  61     nvlist_t **);
  62 static int hc_fmri_present(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
  63     nvlist_t **);
  64 static int hc_fmri_replaced(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
  65     nvlist_t **);
  66 static int hc_fmri_unusable(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
  67     nvlist_t **);
  68 static int hc_fmri_expand(topo_mod_t *, tnode_t *, topo_version_t,
  69     nvlist_t *, nvlist_t **);
  70 static int hc_fmri_retire(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
  71     nvlist_t **);
  72 static int hc_fmri_unretire(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
  73     nvlist_t **);
  74 static int hc_fmri_service_state(topo_mod_t *, tnode_t *, topo_version_t,
  75     nvlist_t *, nvlist_t **);
  76 static int hc_fmri_create_meth(topo_mod_t *, tnode_t *, topo_version_t,
  77     nvlist_t *, nvlist_t **);
  78 static int hc_fmri_prop_get(topo_mod_t *, tnode_t *, topo_version_t,
  79     nvlist_t *, nvlist_t **);
  80 static int hc_fmri_prop_set(topo_mod_t *, tnode_t *, topo_version_t,
  81     nvlist_t *, nvlist_t **);
  82 static int hc_fmri_pgrp_get(topo_mod_t *, tnode_t *, topo_version_t,
  83     nvlist_t *, nvlist_t **);
  84 static int hc_fmri_facility(topo_mod_t *, tnode_t *, topo_version_t,
  85     nvlist_t *, nvlist_t **);
  86 
  87 static nvlist_t *hc_fmri_create(topo_mod_t *, nvlist_t *, int, const char *,
  88     topo_instance_t inst, const nvlist_t *, const char *, const char *,
  89     const char *);
  90 
  91 const topo_method_t hc_methods[] = {
  92         { TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION,
  93             TOPO_STABILITY_INTERNAL, hc_fmri_nvl2str },
  94         { TOPO_METH_STR2NVL, TOPO_METH_STR2NVL_DESC, TOPO_METH_STR2NVL_VERSION,
  95             TOPO_STABILITY_INTERNAL, hc_fmri_str2nvl },
  96         { TOPO_METH_COMPARE, TOPO_METH_COMPARE_DESC, TOPO_METH_COMPARE_VERSION,
  97             TOPO_STABILITY_INTERNAL, hc_compare },
  98         { TOPO_METH_PRESENT, TOPO_METH_PRESENT_DESC, TOPO_METH_PRESENT_VERSION,
  99             TOPO_STABILITY_INTERNAL, hc_fmri_present },
 100         { TOPO_METH_REPLACED, TOPO_METH_REPLACED_DESC,
 101             TOPO_METH_REPLACED_VERSION, TOPO_STABILITY_INTERNAL,
 102             hc_fmri_replaced },
 103         { TOPO_METH_UNUSABLE, TOPO_METH_UNUSABLE_DESC,
 104             TOPO_METH_UNUSABLE_VERSION, TOPO_STABILITY_INTERNAL,
 105             hc_fmri_unusable },
 106         { TOPO_METH_EXPAND, TOPO_METH_EXPAND_DESC,
 107             TOPO_METH_EXPAND_VERSION, TOPO_STABILITY_INTERNAL,
 108             hc_fmri_expand },
 109         { TOPO_METH_RETIRE, TOPO_METH_RETIRE_DESC,
 110             TOPO_METH_RETIRE_VERSION, TOPO_STABILITY_INTERNAL,
 111             hc_fmri_retire },
 112         { TOPO_METH_UNRETIRE, TOPO_METH_UNRETIRE_DESC,
 113             TOPO_METH_UNRETIRE_VERSION, TOPO_STABILITY_INTERNAL,
 114             hc_fmri_unretire },
 115         { TOPO_METH_SERVICE_STATE, TOPO_METH_SERVICE_STATE_DESC,
 116             TOPO_METH_SERVICE_STATE_VERSION, TOPO_STABILITY_INTERNAL,
 117             hc_fmri_service_state },
 118         { TOPO_METH_FMRI, TOPO_METH_FMRI_DESC, TOPO_METH_FMRI_VERSION,
 119             TOPO_STABILITY_INTERNAL, hc_fmri_create_meth },
 120         { TOPO_METH_PROP_GET, TOPO_METH_PROP_GET_DESC,
 121             TOPO_METH_PROP_GET_VERSION, TOPO_STABILITY_INTERNAL,
 122             hc_fmri_prop_get },
 123         { TOPO_METH_PROP_SET, TOPO_METH_PROP_SET_DESC,
 124             TOPO_METH_PROP_SET_VERSION, TOPO_STABILITY_INTERNAL,
 125             hc_fmri_prop_set },
 126         { TOPO_METH_PGRP_GET, TOPO_METH_PGRP_GET_DESC,
 127             TOPO_METH_PGRP_GET_VERSION, TOPO_STABILITY_INTERNAL,
 128             hc_fmri_pgrp_get },
 129         { TOPO_METH_FACILITY, TOPO_METH_FACILITY_DESC,
 130             TOPO_METH_FACILITY_VERSION, TOPO_STABILITY_INTERNAL,
 131             hc_fmri_facility },
 132         { NULL }
 133 };
 134 
 135 static const topo_modops_t hc_ops =
 136         { hc_enum, hc_release };
 137 static const topo_modinfo_t hc_info =
 138         { HC, FM_FMRI_SCHEME_HC, HC_VERSION, &hc_ops };
 139 
 140 static const hcc_t hc_canon[] = {
 141         { BANK, TOPO_STABILITY_PRIVATE },
 142         { BAY, TOPO_STABILITY_PRIVATE },
 143         { BLADE, TOPO_STABILITY_PRIVATE },
 144         { BRANCH, TOPO_STABILITY_PRIVATE },
 145         { CMP, TOPO_STABILITY_PRIVATE },
 146         { CENTERPLANE, TOPO_STABILITY_PRIVATE },
 147         { CHASSIS, TOPO_STABILITY_PRIVATE },
 148         { CHIP, TOPO_STABILITY_PRIVATE },
 149         { CHIP_SELECT, TOPO_STABILITY_PRIVATE },
 150         { CORE, TOPO_STABILITY_PRIVATE },
 151         { CONTROLLER, TOPO_STABILITY_PRIVATE },
 152         { CPU, TOPO_STABILITY_PRIVATE },
 153         { CPUBOARD, TOPO_STABILITY_PRIVATE },
 154         { DIMM, TOPO_STABILITY_PRIVATE },
 155         { DISK, TOPO_STABILITY_PRIVATE },
 156         { DRAM, TOPO_STABILITY_PRIVATE },
 157         { DRAMCHANNEL, TOPO_STABILITY_PRIVATE },
 158         { FAN, TOPO_STABILITY_PRIVATE },
 159         { FANBOARD, TOPO_STABILITY_PRIVATE },
 160         { FANMODULE, TOPO_STABILITY_PRIVATE },
 161         { HBA, TOPO_STABILITY_PRIVATE },
 162         { HOSTBRIDGE, TOPO_STABILITY_PRIVATE },
 163         { INTERCONNECT, TOPO_STABILITY_PRIVATE },
 164         { IOBOARD, TOPO_STABILITY_PRIVATE },
 165         { IPORT, TOPO_STABILITY_PRIVATE },
 166         { MEMBOARD, TOPO_STABILITY_PRIVATE },
 167         { MEMORYBUFFER, TOPO_STABILITY_PRIVATE },
 168         { MEMORYCONTROL, TOPO_STABILITY_PRIVATE },
 169         { MICROCORE, TOPO_STABILITY_PRIVATE },
 170         { MOTHERBOARD, TOPO_STABILITY_PRIVATE },
 171         { NIU, TOPO_STABILITY_PRIVATE },
 172         { NIUFN, TOPO_STABILITY_PRIVATE },
 173         { PCI_BUS, TOPO_STABILITY_PRIVATE },
 174         { PCI_DEVICE, TOPO_STABILITY_PRIVATE },
 175         { PCI_FUNCTION, TOPO_STABILITY_PRIVATE },
 176         { PCIEX_BUS, TOPO_STABILITY_PRIVATE },
 177         { PCIEX_DEVICE, TOPO_STABILITY_PRIVATE },
 178         { PCIEX_FUNCTION, TOPO_STABILITY_PRIVATE },
 179         { PCIEX_ROOT, TOPO_STABILITY_PRIVATE },
 180         { PCIEX_SWUP, TOPO_STABILITY_PRIVATE },
 181         { PCIEX_SWDWN, TOPO_STABILITY_PRIVATE },
 182         { POWERBOARD, TOPO_STABILITY_PRIVATE },
 183         { POWERMODULE, TOPO_STABILITY_PRIVATE },
 184         { PSU, TOPO_STABILITY_PRIVATE },
 185         { RANK, TOPO_STABILITY_PRIVATE },
 186         { RECEPTACLE, TOPO_STABILITY_PRIVATE },
 187         { RISER, TOPO_STABILITY_PRIVATE },
 188         { SASEXPANDER, TOPO_STABILITY_PRIVATE },
 189         { SCSI_DEVICE, TOPO_STABILITY_PRIVATE },
 190         { SHELF, TOPO_STABILITY_PRIVATE },
 191         { SES_ENCLOSURE, TOPO_STABILITY_PRIVATE },
 192         { SMP_DEVICE, TOPO_STABILITY_PRIVATE },
 193         { SP, TOPO_STABILITY_PRIVATE },
 194         { STRAND, TOPO_STABILITY_PRIVATE },
 195         { SUBCHASSIS, TOPO_STABILITY_PRIVATE },
 196         { SYSTEMBOARD, TOPO_STABILITY_PRIVATE },
 197         { XAUI, TOPO_STABILITY_PRIVATE },
 198         { XFP, TOPO_STABILITY_PRIVATE }
 199 };
 200 
 201 static int hc_ncanon = sizeof (hc_canon) / sizeof (hcc_t);
 202 
 203 int
 204 hc_init(topo_mod_t *mod, topo_version_t version)
 205 {
 206         /*
 207          * Turn on module debugging output
 208          */
 209         if (getenv("TOPOHCDEBUG"))
 210                 topo_mod_setdebug(mod);
 211 
 212         topo_mod_dprintf(mod, "initializing hc builtin\n");
 213 
 214         if (version != HC_VERSION)
 215                 return (topo_mod_seterrno(mod, EMOD_VER_NEW));
 216 
 217         if (topo_mod_register(mod, &hc_info, TOPO_VERSION) != 0) {
 218                 topo_mod_dprintf(mod, "failed to register hc: "
 219                     "%s\n", topo_mod_errmsg(mod));
 220                 return (-1); /* mod errno already set */
 221         }
 222 
 223         return (0);
 224 }
 225 
 226 void
 227 hc_fini(topo_mod_t *mod)
 228 {
 229         topo_mod_unregister(mod);
 230 }
 231 
 232 
 233 static const topo_pgroup_info_t sys_pgroup = {
 234         TOPO_PGROUP_SYSTEM,
 235         TOPO_STABILITY_PRIVATE,
 236         TOPO_STABILITY_PRIVATE,
 237         1
 238 };
 239 
 240 static const topo_pgroup_info_t auth_pgroup = {
 241         FM_FMRI_AUTHORITY,
 242         TOPO_STABILITY_PRIVATE,
 243         TOPO_STABILITY_PRIVATE,
 244         1
 245 };
 246 
 247 static void
 248 hc_prop_set(tnode_t *node, nvlist_t *auth)
 249 {
 250         int err;
 251         char isa[MAXNAMELEN];
 252         struct utsname uts;
 253         char *prod, *psn, *csn, *server;
 254 
 255         if (auth == NULL)
 256                 return;
 257 
 258         if (topo_pgroup_create(node, &auth_pgroup, &err) != 0) {
 259                 if (err != ETOPO_PROP_DEFD)
 260                         return;
 261         }
 262 
 263         /*
 264          * Inherit if we can, it saves memory
 265          */
 266         if ((topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_PRODUCT,
 267             &err) != 0) && (err != ETOPO_PROP_DEFD)) {
 268                 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &prod)
 269                     == 0)
 270                         (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY,
 271                             FM_FMRI_AUTH_PRODUCT, TOPO_PROP_IMMUTABLE, prod,
 272                             &err);
 273         }
 274         if ((topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_PRODUCT_SN,
 275             &err) != 0) && (err != ETOPO_PROP_DEFD)) {
 276                 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT_SN, &psn)
 277                     == 0)
 278                         (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY,
 279                             FM_FMRI_AUTH_PRODUCT_SN, TOPO_PROP_IMMUTABLE, psn,
 280                             &err);
 281         }
 282         if ((topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_CHASSIS,
 283             &err) != 0) && (err != ETOPO_PROP_DEFD)) {
 284                 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &csn) == 0)
 285                         (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY,
 286                             FM_FMRI_AUTH_CHASSIS, TOPO_PROP_IMMUTABLE, csn,
 287                             &err);
 288         }
 289         if ((topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_SERVER,
 290             &err) != 0) && (err != ETOPO_PROP_DEFD)) {
 291                 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server)
 292                     == 0)
 293                         (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY,
 294                             FM_FMRI_AUTH_SERVER, TOPO_PROP_IMMUTABLE, server,
 295                             &err);
 296         }
 297 
 298         if (topo_pgroup_create(node, &sys_pgroup, &err) != 0)
 299                 return;
 300 
 301         isa[0] = '\0';
 302         (void) sysinfo(SI_ARCHITECTURE, isa, sizeof (isa));
 303         (void) uname(&uts);
 304         (void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM, TOPO_PROP_ISA,
 305             TOPO_PROP_IMMUTABLE, isa, &err);
 306         (void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM, TOPO_PROP_MACHINE,
 307             TOPO_PROP_IMMUTABLE, uts.machine, &err);
 308 }
 309 
 310 /*ARGSUSED*/
 311 int
 312 hc_enum(topo_mod_t *mod, tnode_t *pnode, const char *name, topo_instance_t min,
 313     topo_instance_t max, void *notused1, void *notused2)
 314 {
 315         int isglobal = (getzoneid() == GLOBAL_ZONEID);
 316         nvlist_t *pfmri = NULL;
 317         nvlist_t *nvl;
 318         nvlist_t *auth;
 319         tnode_t *node;
 320         int err;
 321         /*
 322          * Register root node methods
 323          */
 324         if (strcmp(name, HC) == 0) {
 325                 (void) topo_method_register(mod, pnode, hc_methods);
 326                 return (0);
 327         }
 328         if (min != max) {
 329                 topo_mod_dprintf(mod,
 330                     "Request to enumerate %s component with an "
 331                     "ambiguous instance number, min (%d) != max (%d).\n",
 332                     HC, min, max);
 333                 return (topo_mod_seterrno(mod, EINVAL));
 334         }
 335 
 336         if (!isglobal)
 337                 return (0);
 338 
 339         (void) topo_node_resource(pnode, &pfmri, &err);
 340         auth = topo_mod_auth(mod, pnode);
 341         nvl = hc_fmri_create(mod, pfmri, FM_HC_SCHEME_VERSION, name, min,
 342             auth, NULL, NULL, NULL);
 343         nvlist_free(pfmri);     /* callee ignores NULLs */
 344         if (nvl == NULL) {
 345                 nvlist_free(auth);
 346                 return (-1);
 347         }
 348 
 349         if ((node = topo_node_bind(mod, pnode, name, min, nvl)) == NULL) {
 350                 topo_mod_dprintf(mod, "topo_node_bind failed: %s\n",
 351                     topo_strerror(topo_mod_errno(mod)));
 352                 nvlist_free(auth);
 353                 nvlist_free(nvl);
 354                 return (-1);
 355         }
 356 
 357         /*
 358          * Set FRU for the motherboard node
 359          */
 360         if (strcmp(name, MOTHERBOARD) == 0)
 361                 (void) topo_node_fru_set(node, nvl, 0, &err);
 362 
 363         hc_prop_set(node, auth);
 364         nvlist_free(nvl);
 365         nvlist_free(auth);
 366 
 367         return (0);
 368 }
 369 
 370 /*ARGSUSED*/
 371 static void
 372 hc_release(topo_mod_t *mp, tnode_t *node)
 373 {
 374         topo_method_unregister_all(mp, node);
 375 }
 376 
 377 static int
 378 fmri_compare(topo_mod_t *mod, nvlist_t *nv1, nvlist_t *nv2)
 379 {
 380         uint8_t v1, v2;
 381         nvlist_t **hcp1, **hcp2;
 382         nvlist_t *f1 = NULL, *f2 = NULL;
 383         int err, i;
 384         uint_t nhcp1, nhcp2;
 385         char *f1str, *f2str;
 386 
 387         if (nvlist_lookup_uint8(nv1, FM_VERSION, &v1) != 0 ||
 388             nvlist_lookup_uint8(nv2, FM_VERSION, &v2) != 0 ||
 389             v1 > FM_HC_SCHEME_VERSION || v2 > FM_HC_SCHEME_VERSION)
 390                 return (topo_mod_seterrno(mod, EMOD_FMRI_VERSION));
 391 
 392         err = nvlist_lookup_nvlist_array(nv1, FM_FMRI_HC_LIST, &hcp1, &nhcp1);
 393         err |= nvlist_lookup_nvlist_array(nv2, FM_FMRI_HC_LIST, &hcp2, &nhcp2);
 394         if (err != 0)
 395                 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
 396 
 397         if (nhcp1 != nhcp2)
 398                 return (0);
 399 
 400         for (i = 0; i < nhcp1; i++) {
 401                 char *nm1 = NULL;
 402                 char *nm2 = NULL;
 403                 char *id1 = NULL;
 404                 char *id2 = NULL;
 405 
 406                 (void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_NAME, &nm1);
 407                 (void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_NAME, &nm2);
 408                 (void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_ID, &id1);
 409                 (void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_ID, &id2);
 410                 if (nm1 == NULL || nm2 == NULL || id1 == NULL || id2 == NULL)
 411                         return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
 412 
 413                 if (strcmp(nm1, nm2) == 0 && strcmp(id1, id2) == 0)
 414                         continue;
 415 
 416                 return (0);
 417         }
 418 
 419         /*
 420          * Finally, check if the FMRI's represent a facility node.  If so, then
 421          * verify that the facilty type ("sensor"|"indicator") and facility
 422          * name match.
 423          */
 424         (void) nvlist_lookup_nvlist(nv1, FM_FMRI_FACILITY, &f1);
 425         (void) nvlist_lookup_nvlist(nv2, FM_FMRI_FACILITY, &f2);
 426 
 427         if (f1 == NULL && f2 == NULL)
 428                 return (1);
 429         else if (f1 == NULL || f2 == NULL)
 430                 return (0);
 431 
 432         if (nvlist_lookup_string(f1, FM_FMRI_FACILITY_NAME, &f1str) == 0 &&
 433             nvlist_lookup_string(f2, FM_FMRI_FACILITY_NAME, &f2str) == 0 &&
 434             strcmp(f1str, f2str) == 0 &&
 435             nvlist_lookup_string(f1, FM_FMRI_FACILITY_TYPE, &f1str) == 0 &&
 436             nvlist_lookup_string(f2, FM_FMRI_FACILITY_TYPE, &f2str) == 0 &&
 437             strcmp(f1str, f2str) == 0) {
 438                 return (1);
 439         }
 440         return (0);
 441 }
 442 
 443 /*ARGSUSED*/
 444 static int
 445 hc_compare(topo_mod_t *mod, tnode_t *node, topo_version_t version,
 446     nvlist_t *in, nvlist_t **out)
 447 {
 448         int ret;
 449         uint32_t compare;
 450         nvlist_t *nv1, *nv2;
 451 
 452         if (version > TOPO_METH_COMPARE_VERSION)
 453                 return (topo_mod_seterrno(mod, EMOD_VER_NEW));
 454 
 455         if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NV1, &nv1) != 0 ||
 456             nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NV2, &nv2) != 0)
 457                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
 458 
 459         ret = fmri_compare(mod, nv1, nv2);
 460         if (ret < 0)
 461                 return (-1);
 462 
 463         compare = ret;
 464         if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) == 0) {
 465                 if (nvlist_add_uint32(*out, TOPO_METH_COMPARE_RET,
 466                     compare) == 0)
 467                         return (0);
 468                 else
 469                         nvlist_free(*out);
 470         }
 471 
 472         return (-1);
 473 }
 474 
 475 static ssize_t
 476 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
 477 {
 478         nvlist_t **hcprs = NULL;
 479         nvlist_t *hcsp = NULL;
 480         nvlist_t *anvl = NULL;
 481         nvpair_t *apair;
 482         nvlist_t *fnvl;
 483         uint8_t version;
 484         ssize_t size = 0;
 485         uint_t hcnprs;
 486         char *serial = NULL;
 487         char *part = NULL;
 488         char *root = NULL;
 489         char *rev = NULL;
 490         char *aname, *aval;
 491         char *fname = NULL, *ftype = NULL;
 492         int err, i;
 493 
 494         if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 ||
 495             version > FM_HC_SCHEME_VERSION)
 496                 return (0);
 497 
 498         /* Get authority, if present */
 499         err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl);
 500         if (err != 0 && err != ENOENT)
 501                 return (0);
 502 
 503         (void) nvlist_lookup_string(nvl, FM_FMRI_HC_ROOT, &root);
 504 
 505         err = nvlist_lookup_nvlist_array(nvl, FM_FMRI_HC_LIST, &hcprs, &hcnprs);
 506         if (err != 0 || hcprs == NULL)
 507                 return (0);
 508 
 509         (void) nvlist_lookup_string(nvl, FM_FMRI_HC_SERIAL_ID, &serial);
 510         (void) nvlist_lookup_string(nvl, FM_FMRI_HC_PART, &part);
 511         (void) nvlist_lookup_string(nvl, FM_FMRI_HC_REVISION, &rev);
 512 
 513         /* hc:// */
 514         topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_HC, NULL, "://");
 515 
 516         /* authority, if any */
 517         if (anvl != NULL) {
 518                 for (apair = nvlist_next_nvpair(anvl, NULL);
 519                     apair != NULL; apair = nvlist_next_nvpair(anvl, apair)) {
 520                         if (nvpair_type(apair) != DATA_TYPE_STRING ||
 521                             nvpair_value_string(apair, &aval) != 0)
 522                                 continue;
 523                         aname = nvpair_name(apair);
 524                         topo_fmristr_build(&size, buf, buflen, ":", NULL, NULL);
 525                         topo_fmristr_build(&size, buf, buflen, "=",
 526                             aname, aval);
 527                 }
 528         }
 529 
 530         /* hardware-id part */
 531         topo_fmristr_build(&size,
 532             buf, buflen, serial, ":" FM_FMRI_HC_SERIAL_ID "=", NULL);
 533         topo_fmristr_build(&size,
 534             buf, buflen, part, ":" FM_FMRI_HC_PART "=", NULL);
 535         topo_fmristr_build(&size,
 536             buf, buflen, rev, ":" FM_FMRI_HC_REVISION "=", NULL);
 537 
 538         /* separating slash */
 539         topo_fmristr_build(&size, buf, buflen, "/", NULL, NULL);
 540 
 541         /* hc-root */
 542         if (root)
 543                 topo_fmristr_build(&size, buf, buflen, root, NULL, NULL);
 544 
 545         /* all the pairs */
 546         for (i = 0; i < hcnprs; i++) {
 547                 char *nm = NULL;
 548                 char *id = NULL;
 549 
 550                 if (i > 0)
 551                         topo_fmristr_build(&size,
 552                             buf, buflen, "/", NULL, NULL);
 553                 (void) nvlist_lookup_string(hcprs[i], FM_FMRI_HC_NAME, &nm);
 554                 (void) nvlist_lookup_string(hcprs[i], FM_FMRI_HC_ID, &id);
 555                 if (nm == NULL || id == NULL)
 556                         return (0);
 557                 topo_fmristr_build(&size, buf, buflen, nm, NULL, "=");
 558                 topo_fmristr_build(&size, buf, buflen, id, NULL, NULL);
 559         }
 560 
 561         /* append offset/physaddr if it exists in hc-specific */
 562         if (nvlist_lookup_nvlist(nvl, FM_FMRI_HC_SPECIFIC, &hcsp) == 0) {
 563                 char *hcsn = NULL;
 564                 char hexstr[17];
 565                 uint64_t val;
 566 
 567                 if (nvlist_lookup_uint64(hcsp, FM_FMRI_HC_SPECIFIC_OFFSET,
 568                     &val) == 0 || nvlist_lookup_uint64(hcsp,
 569                     "asru-" FM_FMRI_HC_SPECIFIC_OFFSET, &val) == 0)
 570                         hcsn = FM_FMRI_HC_SPECIFIC_OFFSET;
 571                 else if (nvlist_lookup_uint64(hcsp,
 572                     FM_FMRI_HC_SPECIFIC_PHYSADDR, &val) == 0 ||
 573                     nvlist_lookup_uint64(hcsp,
 574                     "asru-" FM_FMRI_HC_SPECIFIC_PHYSADDR, &val) == 0)
 575                         hcsn = FM_FMRI_HC_SPECIFIC_PHYSADDR;
 576 
 577                 if (hcsn != NULL) {
 578                         (void) snprintf(hexstr, sizeof (hexstr), "%llx", val);
 579                         topo_fmristr_build(&size, buf, buflen, "/", NULL, NULL);
 580                         topo_fmristr_build(&size, buf, buflen, "=", hcsn,
 581                             hexstr);
 582                 }
 583         }
 584 
 585         /*
 586          * If the nvlist represents a facility node, then we append the
 587          * facility type and name to the end of the string representation using
 588          * the format below:
 589          *
 590          * ?<ftype>=<fname>
 591          */
 592         if (nvlist_lookup_nvlist(nvl, FM_FMRI_FACILITY, &fnvl) == 0) {
 593                 if (nvlist_lookup_string(fnvl, FM_FMRI_FACILITY_NAME,
 594                     &fname) != 0 || nvlist_lookup_string(fnvl,
 595                     FM_FMRI_FACILITY_TYPE, &ftype) != 0)
 596                         return (0);
 597                 topo_fmristr_build(&size, buf, buflen, "?", NULL, NULL);
 598                 topo_fmristr_build(&size, buf, buflen, "=", ftype, fname);
 599         }
 600 
 601         return (size);
 602 }
 603 
 604 /*ARGSUSED*/
 605 static int
 606 hc_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version,
 607     nvlist_t *nvl, nvlist_t **out)
 608 {
 609         ssize_t len;
 610         char *name = NULL;
 611         nvlist_t *fmristr;
 612 
 613         if (version > TOPO_METH_NVL2STR_VERSION)
 614                 return (topo_mod_seterrno(mod, EMOD_VER_NEW));
 615 
 616         if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 ||
 617             (name = topo_mod_alloc(mod, len + 1)) == NULL ||
 618             fmri_nvl2str(nvl, name, len + 1) == 0) {
 619                 if (name != NULL)
 620                         topo_mod_free(mod, name, len + 1);
 621                 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
 622         }
 623 
 624         if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) {
 625                 topo_mod_free(mod, name, len + 1);
 626                 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
 627         }
 628         if (nvlist_add_string(fmristr, "fmri-string", name) != 0) {
 629                 topo_mod_free(mod, name, len + 1);
 630                 nvlist_free(fmristr);
 631                 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
 632         }
 633         topo_mod_free(mod, name, len + 1);
 634         *out = fmristr;
 635 
 636         return (0);
 637 }
 638 
 639 static nvlist_t *
 640 hc_base_fmri_create(topo_mod_t *mod, const nvlist_t *auth, const char *part,
 641     const char *rev, const char *serial)
 642 {
 643         nvlist_t *fmri;
 644         int err = 0;
 645 
 646         /*
 647          * Create base HC nvlist
 648          */
 649         if (topo_mod_nvalloc(mod, &fmri, NV_UNIQUE_NAME) != 0)
 650                 return (NULL);
 651 
 652         err = nvlist_add_uint8(fmri, FM_VERSION, FM_HC_SCHEME_VERSION);
 653         err |= nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
 654         err |= nvlist_add_string(fmri, FM_FMRI_HC_ROOT, "");
 655         if (err != 0) {
 656                 nvlist_free(fmri);
 657                 return (NULL);
 658         }
 659 
 660         /*
 661          * Add optional payload members
 662          */
 663         if (serial != NULL)
 664                 (void) nvlist_add_string(fmri, FM_FMRI_HC_SERIAL_ID, serial);
 665         if (part != NULL)
 666                 (void) nvlist_add_string(fmri, FM_FMRI_HC_PART, part);
 667         if (rev != NULL)
 668                 (void) nvlist_add_string(fmri, FM_FMRI_HC_REVISION, rev);
 669         if (auth != NULL)
 670                 (void) nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
 671                     (nvlist_t *)auth);
 672 
 673         return (fmri);
 674 }
 675 
 676 static nvlist_t **
 677 make_hc_pairs(topo_mod_t *mod, char *fmri, int *num)
 678 {
 679         nvlist_t **pa;
 680         char *hc, *fromstr;
 681         char *starti, *startn, *endi, *endi2;
 682         char *ne, *ns;
 683         char *cname = NULL;
 684         char *find;
 685         char *cid = NULL;
 686         int nslashes = 0;
 687         int npairs = 0;
 688         int i, hclen;
 689 
 690         if ((hc = topo_mod_strdup(mod, fmri + 5)) == NULL)
 691                 return (NULL);
 692 
 693         hclen = strlen(hc) + 1;
 694 
 695         /*
 696          * Count equal signs and slashes to determine how many
 697          * hc-pairs will be present in the final FMRI.  There should
 698          * be at least as many slashes as equal signs.  There can be
 699          * more, though if the string after an = includes them.
 700          */
 701         if ((fromstr = strchr(hc, '/')) == NULL)
 702                 return (NULL);
 703 
 704         find = fromstr;
 705         while ((ne = strchr(find, '=')) != NULL) {
 706                 find = ne + 1;
 707                 npairs++;
 708         }
 709 
 710         find = fromstr;
 711         while ((ns = strchr(find, '/')) != NULL) {
 712                 find = ns + 1;
 713                 nslashes++;
 714         }
 715 
 716         /*
 717          * Do we appear to have a well-formed string version of the FMRI?
 718          */
 719         if (nslashes < npairs || npairs == 0) {
 720                 topo_mod_free(mod, hc, hclen);
 721                 return (NULL);
 722         }
 723 
 724         *num = npairs;
 725 
 726         find = fromstr;
 727 
 728         if ((pa = topo_mod_zalloc(mod, npairs * sizeof (nvlist_t *))) == NULL) {
 729                 topo_mod_free(mod, hc, hclen);
 730                 return (NULL);
 731         }
 732 
 733         /*
 734          * We go through a pretty complicated procedure to find the
 735          * name and id for each pair.  That's because, unfortunately,
 736          * we have some ids that can have slashes within them.  So
 737          * we can't just search for the next slash after the equal sign
 738          * and decide that starts a new pair.  Instead we have to find
 739          * an equal sign for the next pair and work our way back to the
 740          * slash from there.
 741          */
 742         for (i = 0; i < npairs; i++) {
 743                 startn = strchr(find, '/');
 744                 if (startn == NULL)
 745                         break;
 746                 startn++;
 747                 starti = strchr(find, '=');
 748                 if (starti == NULL)
 749                         break;
 750                 *starti = '\0';
 751                 if ((cname = topo_mod_strdup(mod, startn)) == NULL)
 752                         break;
 753                 *starti++ = '=';
 754                 endi = strchr(starti, '=');
 755                 if (endi != NULL) {
 756                         *endi = '\0';
 757                         endi2 = strrchr(starti, '/');
 758                         if (endi2 == NULL)
 759                                 break;
 760                         *endi = '=';
 761                         *endi2 = '\0';
 762                         if ((cid = topo_mod_strdup(mod, starti)) == NULL)
 763                                 break;
 764                         *endi2 = '/';
 765                         find = endi2;
 766                 } else {
 767                         if ((cid = topo_mod_strdup(mod, starti)) == NULL)
 768                                 break;
 769                         find = starti + strlen(starti);
 770                 }
 771                 if (topo_mod_nvalloc(mod, &pa[i], NV_UNIQUE_NAME) < 0)
 772                         break;
 773 
 774                 if (nvlist_add_string(pa[i], FM_FMRI_HC_NAME, cname) ||
 775                     nvlist_add_string(pa[i], FM_FMRI_HC_ID, cid))
 776                         break;
 777 
 778                 topo_mod_strfree(mod, cname);
 779                 topo_mod_strfree(mod, cid);
 780                 cname = NULL;
 781                 cid = NULL;
 782         }
 783 
 784         topo_mod_strfree(mod, cname);
 785         topo_mod_strfree(mod, cid);
 786 
 787         if (i < npairs) {
 788                 for (i = 0; i < npairs; i++)
 789                         nvlist_free(pa[i]);
 790                 topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *));
 791                 topo_mod_free(mod, hc, hclen);
 792                 return (NULL);
 793         }
 794 
 795         topo_mod_free(mod, hc, hclen);
 796 
 797         return (pa);
 798 }
 799 
 800 int
 801 make_hc_auth(topo_mod_t *mod, char *fmri, char **serial, char **part,
 802 char **rev, nvlist_t **auth)
 803 {
 804         char *starti, *startn, *endi, *copy;
 805         char *aname = NULL, *aid = NULL, *fs;
 806         nvlist_t *na = NULL;
 807         size_t len;
 808 
 809         if ((copy = topo_mod_strdup(mod, fmri + 5)) == NULL)
 810                 return (-1);
 811 
 812         len = strlen(copy);
 813 
 814         /*
 815          * Make sure there are a valid authority members
 816          */
 817         startn = strchr(copy, ':');
 818         fs = strchr(copy, '/');
 819 
 820         if (startn == NULL || fs == NULL) {
 821                 topo_mod_strfree(mod, copy);
 822                 return (0);
 823         }
 824 
 825         /*
 826          * The first colon we encounter must occur before the
 827          * first slash
 828          */
 829         if (startn > fs)
 830                 goto hcabail;
 831 
 832         do {
 833                 if (++startn >= copy + len)
 834                         break;
 835 
 836                 if ((starti = strchr(startn, '=')) == NULL)
 837                         goto hcabail;
 838 
 839                 *starti = '\0';
 840                 if (++starti > copy + len)
 841                         goto hcabail;
 842 
 843                 if ((aname = topo_mod_strdup(mod, startn)) == NULL)
 844                         goto hcabail;
 845 
 846                 startn = endi = strchr(starti, ':');
 847                 if (endi == NULL)
 848                         if ((endi = strchr(starti, '/')) == NULL)
 849                                 break;
 850 
 851                 *endi = '\0';
 852                 if ((aid = topo_mod_strdup(mod, starti)) == NULL)
 853                         goto hcabail;
 854 
 855                 /*
 856                  * Return possible serial, part and revision
 857                  */
 858                 if (strcmp(aname, FM_FMRI_HC_SERIAL_ID) == 0) {
 859                         *serial = topo_mod_strdup(mod, aid);
 860                 } else if (strcmp(aname, FM_FMRI_HC_PART) == 0) {
 861                         *part = topo_mod_strdup(mod, aid);
 862                 } else if (strcmp(aname, FM_FMRI_HC_REVISION) == 0) {
 863                         *rev = topo_mod_strdup(mod, aid);
 864                 } else {
 865                         if (na == NULL) {
 866                                 if (topo_mod_nvalloc(mod, &na,
 867                                     NV_UNIQUE_NAME) == 0) {
 868                                         (void) nvlist_add_string(na, aname,
 869                                             aid);
 870                                 }
 871                         } else {
 872                                 (void) nvlist_add_string(na, aname, aid);
 873                         }
 874                 }
 875                 topo_mod_strfree(mod, aname);
 876                 topo_mod_strfree(mod, aid);
 877                 aname = aid = NULL;
 878 
 879         } while (startn != NULL);
 880 
 881         *auth = na;
 882 
 883         topo_mod_free(mod, copy, len + 1);
 884         return (0);
 885 
 886 hcabail:
 887         topo_mod_free(mod, copy, len + 1);
 888         topo_mod_strfree(mod, aname);
 889         topo_mod_strfree(mod, aid);
 890         nvlist_free(na);
 891         return (-1);
 892 }
 893 
 894 
 895 /*
 896  * This function creates an nvlist to represent the facility portion of an
 897  * hc-scheme node, given a string representation of the fmri.  This is called by
 898  * hc_fmri_str2nvl.  If the string does not contain a facility component
 899  * (e.g. ?<ftype>=<fname>) then it bails early and returns 0.
 900  *
 901  * On failure it returns -1 and sets the topo mod errno
 902  */
 903 int
 904 make_facility(topo_mod_t *mod, char *str, nvlist_t **nvl)
 905 {
 906         char *fac, *copy, *fname, *ftype;
 907         nvlist_t *nf = NULL;
 908         size_t len;
 909 
 910         if ((fac = strchr(str, '?')) == NULL)
 911                 return (0);
 912 
 913         ++fac;
 914         if ((copy = topo_mod_strdup(mod, fac)) == NULL)
 915                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
 916 
 917         fac = copy;
 918         len = strlen(fac);
 919 
 920         if ((fname = strchr(fac, '=')) == NULL) {
 921                 topo_mod_free(mod, copy, len + 1);
 922                 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
 923         }
 924 
 925         fname[0] = '\0';
 926         ++fname;
 927         ftype = fac;
 928 
 929         if (topo_mod_nvalloc(mod, &nf, NV_UNIQUE_NAME) != 0) {
 930                 topo_mod_free(mod, copy, len + 1);
 931                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
 932         }
 933 
 934         if (nvlist_add_string(nf, FM_FMRI_FACILITY_NAME, fname) != 0 ||
 935             nvlist_add_string(nf, FM_FMRI_FACILITY_TYPE, ftype) != 0) {
 936                 topo_mod_free(mod, copy, len + 1);
 937                 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL));
 938         }
 939 
 940         topo_mod_free(mod, copy, len + 1);
 941 
 942         *nvl = nf;
 943 
 944         return (0);
 945 }
 946 
 947 /*ARGSUSED*/
 948 static int
 949 hc_fmri_str2nvl(topo_mod_t *mod, tnode_t *node, topo_version_t version,
 950     nvlist_t *in, nvlist_t **out)
 951 {
 952         nvlist_t **pa = NULL;
 953         nvlist_t *nf = NULL;
 954         nvlist_t *auth = NULL;
 955         nvlist_t *fac = NULL;
 956         char *str;
 957         char *serial = NULL, *part = NULL, *rev = NULL, *hcsn = NULL;
 958         int npairs, n;
 959         int i, e;
 960 
 961         if (version > TOPO_METH_STR2NVL_VERSION)
 962                 return (topo_mod_seterrno(mod, EMOD_VER_NEW));
 963 
 964         if (nvlist_lookup_string(in, "fmri-string", &str) != 0)
 965                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
 966 
 967         /* We're expecting a string version of an hc scheme FMRI */
 968         if (strncmp(str, "hc://", 5) != 0)
 969                 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
 970 
 971         if ((pa = make_hc_pairs(mod, str, &npairs)) == NULL)
 972                 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
 973 
 974         if (make_hc_auth(mod, str, &serial, &part, &rev, &auth) < 0)
 975                 goto hcfmbail;
 976 
 977         if ((nf = hc_base_fmri_create(mod, auth, part, rev, serial)) == NULL)
 978                 goto hcfmbail;
 979 
 980         n = npairs;
 981 
 982         /*
 983          * If the last pair in hc-list is offset or physaddr, we move
 984          * it to hc-specific.
 985          */
 986         (void) nvlist_lookup_string(pa[npairs - 1], FM_FMRI_HC_NAME, &hcsn);
 987         if (strcmp(hcsn, FM_FMRI_HC_SPECIFIC_OFFSET) == 0 ||
 988             strcmp(hcsn, FM_FMRI_HC_SPECIFIC_PHYSADDR) == 0) {
 989                 char *hcid;
 990                 nvlist_t *hcsp;
 991                 uint64_t val;
 992 
 993                 (void) nvlist_lookup_string(pa[npairs - 1], FM_FMRI_HC_ID,
 994                     &hcid);
 995                 val = strtoull(hcid, NULL, 16);
 996                 if (topo_mod_nvalloc(mod, &hcsp, NV_UNIQUE_NAME) != 0)
 997                         goto hcfmbail;
 998                 if (nvlist_add_uint64(hcsp, hcsn, val) != 0 ||
 999                     nvlist_add_nvlist(nf, FM_FMRI_HC_SPECIFIC, hcsp) != 0) {
1000                         nvlist_free(hcsp);
1001                         goto hcfmbail;
1002                 }
1003 
1004                 nvlist_free(hcsp);
1005                 n--;
1006         }
1007 
1008         if ((e = nvlist_add_uint32(nf, FM_FMRI_HC_LIST_SZ, n)) == 0)
1009                 e = nvlist_add_nvlist_array(nf, FM_FMRI_HC_LIST, pa, n);
1010         if (e != 0) {
1011                 topo_mod_dprintf(mod, "construction of new hc nvl failed");
1012                 goto hcfmbail;
1013         }
1014 
1015         /*
1016          * Clean-up
1017          */
1018         for (i = 0; i < npairs; i++)
1019                 nvlist_free(pa[i]);
1020         topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *));
1021         topo_mod_strfree(mod, serial);
1022         topo_mod_strfree(mod, part);
1023         topo_mod_strfree(mod, rev);
1024         nvlist_free(auth);
1025 
1026         if (make_facility(mod, str, &fac) == -1)
1027                 goto hcfmbail;
1028 
1029         if (fac != NULL) {
1030                 if (nvlist_add_nvlist(nf, FM_FMRI_FACILITY, fac) != 0)
1031                         goto hcfmbail;
1032         }
1033 
1034         *out = nf;
1035 
1036         return (0);
1037 
1038 hcfmbail:
1039         nvlist_free(nf);
1040         for (i = 0; i < npairs; i++)
1041                 nvlist_free(pa[i]);
1042         topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *));
1043 
1044         topo_mod_strfree(mod, serial);
1045         topo_mod_strfree(mod, part);
1046         topo_mod_strfree(mod, rev);
1047         nvlist_free(auth);
1048         nvlist_free(nf);
1049         return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM));
1050 }
1051 
1052 static nvlist_t *
1053 hc_list_create(topo_mod_t *mod, const char *name, char *inst)
1054 {
1055         int err;
1056         nvlist_t *hc;
1057 
1058         if (topo_mod_nvalloc(mod, &hc, NV_UNIQUE_NAME) != 0)
1059                 return (NULL);
1060 
1061         err = nvlist_add_string(hc, FM_FMRI_HC_NAME, name);
1062         err |= nvlist_add_string(hc, FM_FMRI_HC_ID, inst);
1063         if (err != 0) {
1064                 nvlist_free(hc);
1065                 return (NULL);
1066         }
1067 
1068         return (hc);
1069 }
1070 
1071 static nvlist_t *
1072 hc_create_seterror(topo_mod_t *mod, nvlist_t **hcl, int n, nvlist_t *fmri,
1073     int err)
1074 {
1075         int i;
1076 
1077         if (hcl != NULL) {
1078                 for (i = 0; i < n + 1; ++i)
1079                         nvlist_free(hcl[i]);
1080 
1081                 topo_mod_free(mod, hcl, sizeof (nvlist_t *) * (n + 1));
1082         }
1083 
1084         nvlist_free(fmri);
1085 
1086         (void) topo_mod_seterrno(mod, err);
1087 
1088         topo_mod_dprintf(mod, "unable to create hc FMRI: %s\n",
1089             topo_mod_errmsg(mod));
1090 
1091         return (NULL);
1092 }
1093 
1094 static int
1095 hc_name_canonical(topo_mod_t *mod, const char *name)
1096 {
1097         int i;
1098 
1099         if (getenv("NOHCCHECK") != NULL)
1100                 return (1);
1101 
1102         /*
1103          * Only enumerate elements with correct canonical names
1104          */
1105         for (i = 0; i < hc_ncanon; i++) {
1106                 if (strcmp(name, hc_canon[i].hcc_name) == 0)
1107                         break;
1108         }
1109         if (i >= hc_ncanon) {
1110                 topo_mod_dprintf(mod, "non-canonical name %s\n",
1111                     name);
1112                 return (0);
1113         } else {
1114                 return (1);
1115         }
1116 }
1117 
1118 static nvlist_t *
1119 hc_fmri_create(topo_mod_t *mod, nvlist_t *pfmri, int version, const char *name,
1120     topo_instance_t inst, const nvlist_t *auth, const char *part,
1121     const char *rev, const char *serial)
1122 {
1123         int i;
1124         char str[21]; /* sizeof (UINT64_MAX) + '\0' */
1125         uint_t pelems = 0;
1126         nvlist_t **phcl = NULL;
1127         nvlist_t **hcl = NULL;
1128         nvlist_t *fmri = NULL;
1129 
1130         if (version > FM_HC_SCHEME_VERSION)
1131                 return (hc_create_seterror(mod,
1132                     hcl, pelems, fmri, EMOD_VER_OLD));
1133         else if (version < FM_HC_SCHEME_VERSION)
1134                 return (hc_create_seterror(mod,
1135                     hcl, pelems, fmri, EMOD_VER_NEW));
1136 
1137         /*
1138          * Check that the requested name is in our canonical list
1139          */
1140         if (hc_name_canonical(mod, name) == 0)
1141                 return (hc_create_seterror(mod,
1142                     hcl, pelems, fmri, EMOD_NONCANON));
1143         /*
1144          * Copy the parent's HC_LIST
1145          */
1146         if (pfmri != NULL) {
1147                 if (nvlist_lookup_nvlist_array(pfmri, FM_FMRI_HC_LIST,
1148                     &phcl, &pelems) != 0)
1149                         return (hc_create_seterror(mod,
1150                             hcl, pelems, fmri, EMOD_FMRI_MALFORM));
1151         }
1152 
1153         hcl = topo_mod_zalloc(mod, sizeof (nvlist_t *) * (pelems + 1));
1154         if (hcl == NULL)
1155                 return (hc_create_seterror(mod,  hcl, pelems, fmri,
1156                     EMOD_NOMEM));
1157 
1158         for (i = 0; i < pelems; ++i)
1159                 if (topo_mod_nvdup(mod, phcl[i], &hcl[i]) != 0)
1160                         return (hc_create_seterror(mod,
1161                             hcl, pelems, fmri, EMOD_FMRI_NVL));
1162 
1163         (void) snprintf(str, sizeof (str), "%d", inst);
1164         if ((hcl[i] = hc_list_create(mod, name, str)) == NULL)
1165                 return (hc_create_seterror(mod,
1166                     hcl, pelems, fmri, EMOD_FMRI_NVL));
1167 
1168         if ((fmri = hc_base_fmri_create(mod, auth, part, rev, serial)) == NULL)
1169                 return (hc_create_seterror(mod,
1170                     hcl, pelems, fmri, EMOD_FMRI_NVL));
1171 
1172         if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, hcl, pelems + 1)
1173             != 0)
1174                 return (hc_create_seterror(mod,
1175                     hcl, pelems, fmri, EMOD_FMRI_NVL));
1176 
1177         if (hcl != NULL) {
1178                 for (i = 0; i < pelems + 1; ++i) {
1179                         nvlist_free(hcl[i]);
1180                 }
1181                 topo_mod_free(mod, hcl, sizeof (nvlist_t *) * (pelems + 1));
1182         }
1183 
1184         return (fmri);
1185 }
1186 
1187 /*ARGSUSED*/
1188 static int
1189 hc_fmri_create_meth(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1190     nvlist_t *in, nvlist_t **out)
1191 {
1192         int ret;
1193         nvlist_t *args, *pfmri = NULL;
1194         nvlist_t *auth;
1195         uint32_t inst;
1196         char *name, *serial, *rev, *part;
1197 
1198         if (version > TOPO_METH_FMRI_VERSION)
1199                 return (topo_mod_seterrno(mod, EMOD_VER_NEW));
1200 
1201         /* First the must-have fields */
1202         if (nvlist_lookup_string(in, TOPO_METH_FMRI_ARG_NAME, &name) != 0)
1203                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1204         if (nvlist_lookup_uint32(in, TOPO_METH_FMRI_ARG_INST, &inst) != 0)
1205                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1206 
1207         /*
1208          * args is optional
1209          */
1210         pfmri = NULL;
1211         auth = NULL;
1212         serial = rev = part = NULL;
1213         if ((ret = nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args))
1214             != 0) {
1215                 if (ret != ENOENT)
1216                         return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1217         } else {
1218 
1219                 /* And then optional arguments */
1220                 (void) nvlist_lookup_nvlist(args, TOPO_METH_FMRI_ARG_PARENT,
1221                     &pfmri);
1222                 (void) nvlist_lookup_nvlist(args, TOPO_METH_FMRI_ARG_AUTH,
1223                     &auth);
1224                 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_PART,
1225                     &part);
1226                 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_REV, &rev);
1227                 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_SER,
1228                     &serial);
1229         }
1230 
1231         *out = hc_fmri_create(mod, pfmri, version, name, inst, auth, part,
1232             rev, serial);
1233         if (*out == NULL)
1234                 return (-1);
1235         return (0);
1236 }
1237 
1238 struct hc_walk {
1239         topo_mod_walk_cb_t hcw_cb;
1240         void *hcw_priv;
1241         topo_walk_t *hcw_wp;
1242         nvlist_t **hcw_list;
1243         nvlist_t *hcw_fmri;
1244         nvlist_t *hcw_fac;
1245         uint_t hcw_index;
1246         uint_t hcw_end;
1247 };
1248 
1249 /*
1250  * Returns true if the given node is beneath the specified FMRI.  This uses
1251  * the TOPO_METH_CONTAINS method, because some enumerators (such as external
1252  * enclosures) may want to do a comparison based on chassis WWN instead of the
1253  * instance ID.  If this comparison function fails or is not supported, then we
1254  * fall back to a direct name/instance comparison.
1255  */
1256 static int
1257 hc_match(topo_mod_t *mod, tnode_t *node, nvlist_t *fmri, const char *name,
1258     topo_instance_t inst, boolean_t *result)
1259 {
1260         nvlist_t *rsrc;
1261         nvlist_t *arg, *nvl;
1262         uint32_t match = 0;
1263         int err;
1264 
1265         if (topo_node_resource(node, &rsrc, &err) != 0)
1266                 return (-1);
1267 
1268         if (topo_mod_nvalloc(mod, &arg, NV_UNIQUE_NAME) != 0 ||
1269             nvlist_add_nvlist(arg, TOPO_METH_FMRI_ARG_FMRI,
1270             rsrc) != 0 ||
1271             nvlist_add_nvlist(arg, TOPO_METH_FMRI_ARG_SUBFMRI,
1272             fmri) != 0) {
1273                 nvlist_free(rsrc);
1274                 (void) topo_mod_seterrno(mod, EMOD_NOMEM);
1275                 return (-1);
1276         }
1277 
1278         nvlist_free(rsrc);
1279 
1280         if (topo_method_invoke(node, TOPO_METH_CONTAINS,
1281             TOPO_METH_CONTAINS_VERSION, arg, &nvl, &err) != 0) {
1282                 nvlist_free(arg);
1283                 if (err == ETOPO_METHOD_NOTSUP) {
1284                         match = (strcmp(name,
1285                             topo_node_name(node)) == 0 &&
1286                             inst == topo_node_instance(node));
1287                 } else {
1288                         return (-1);
1289                 }
1290         } else {
1291                 nvlist_free(arg);
1292                 if (nvlist_lookup_uint32(nvl, TOPO_METH_CONTAINS_RET,
1293                     &match) != 0) {
1294                         nvlist_free(nvl);
1295                         (void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
1296                         return (-1);
1297                 }
1298                 nvlist_free(nvl);
1299         }
1300 
1301         *result = (match != 0);
1302         return (0);
1303 }
1304 
1305 /*
1306  * Ideally, we should just be able to call topo_walk_bysibling().  But that
1307  * code assumes that the name/instance pair will match, so we need to
1308  * explicitly iterate over children of the parent looking for a matching value.
1309  */
1310 static int
1311 hc_walk_sibling(topo_mod_t *mod, tnode_t *node, struct hc_walk *hwp,
1312     const char *name, topo_instance_t inst)
1313 {
1314         tnode_t *pnp = topo_node_parent(node);
1315         topo_walk_t *wp = hwp->hcw_wp;
1316         tnode_t *np;
1317         boolean_t matched;
1318         int status;
1319 
1320         for (np = topo_child_first(pnp); np != NULL;
1321             np = topo_child_next(pnp, np)) {
1322                 topo_node_hold(np);
1323                 if (hc_match(mod, np, hwp->hcw_fmri, name, inst,
1324                     &matched) == 0 && matched) {
1325                         wp->tw_node = np;
1326                         if (wp->tw_mod != NULL)
1327                                 status = wp->tw_cb(mod, np, hwp);
1328                         else
1329                                 status = wp->tw_cb(wp->tw_thp, np, hwp);
1330                         topo_node_rele(np);
1331                         wp->tw_node = node;
1332                         return (status);
1333                 }
1334 
1335                 topo_node_rele(np);
1336         }
1337 
1338         return (TOPO_WALK_TERMINATE);
1339 }
1340 
1341 /*
1342  * Generic walker for the hc-scheme topo tree.  This function uses the
1343  * hierachical nature of the hc-scheme to efficiently step through
1344  * the topo hc tree.  Node lookups are done by topo_walk_byid() and
1345  * topo_walk_bysibling()  at each component level to avoid unnecessary
1346  * traversal of the tree.  hc_walker() never returns TOPO_WALK_NEXT, so
1347  * whether TOPO_WALK_CHILD or TOPO_WALK_SIBLING is specified by
1348  * topo_walk_step() doesn't affect the traversal.
1349  */
1350 static int
1351 hc_walker(topo_mod_t *mod, tnode_t *node, void *pdata)
1352 {
1353         int i, err;
1354         struct hc_walk *hwp = (struct hc_walk *)pdata;
1355         char *name, *id;
1356         char *fname, *ftype;
1357         topo_instance_t inst;
1358         boolean_t match;
1359 
1360         i = hwp->hcw_index;
1361         if (i > hwp->hcw_end) {
1362                 if (hwp->hcw_fac != NULL) {
1363                         if ((err = hwp->hcw_cb(mod, node, hwp->hcw_priv))
1364                             != 0) {
1365                                 (void) topo_mod_seterrno(mod, err);
1366                                 topo_mod_dprintf(mod, "hc_walker: callback "
1367                                     "failed: %s\n ", topo_mod_errmsg(mod));
1368                                 return (TOPO_WALK_ERR);
1369                         }
1370                         topo_mod_dprintf(mod, "hc_walker: callback "
1371                             "complete: terminate walk\n");
1372                         return (TOPO_WALK_TERMINATE);
1373                 } else {
1374                         topo_mod_dprintf(mod, "hc_walker: node not found\n");
1375                         return (TOPO_WALK_TERMINATE);
1376                 }
1377         }
1378 
1379         err = nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_NAME, &name);
1380         err |= nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_ID, &id);
1381 
1382         if (err != 0) {
1383                 (void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
1384                 return (TOPO_WALK_ERR);
1385         }
1386 
1387         inst = atoi(id);
1388 
1389         /*
1390          * Check to see if our node matches the requested FMRI.  If it doesn't
1391          * (because the enumerator determines matching based on something other
1392          * than name/instance, or because we're at the first level below the
1393          * root), then iterate over siblings to find the matching node.
1394          */
1395         if (hc_match(mod, node, hwp->hcw_fmri, name, inst, &match) != 0)
1396                 return (TOPO_WALK_ERR);
1397 
1398         if (!match)
1399                 return (hc_walk_sibling(mod, node, hwp, name, inst));
1400 
1401         topo_mod_dprintf(mod, "hc_walker: walking node:%s=%d for hc:"
1402             "%s=%d at %d, end at %d \n", topo_node_name(node),
1403             topo_node_instance(node), name, inst, i, hwp->hcw_end);
1404 
1405         if (i == hwp->hcw_end) {
1406 
1407                 /*
1408                  * We are at the end of the hc-list.  Now, check for
1409                  * a facility leaf and walk one more time.
1410                  */
1411                 if (hwp->hcw_fac != NULL) {
1412                         err = nvlist_lookup_string(hwp->hcw_fac,
1413                             FM_FMRI_FACILITY_NAME, &fname);
1414                         err |= nvlist_lookup_string(hwp->hcw_fac,
1415                             FM_FMRI_FACILITY_TYPE, &ftype);
1416                         if (err != 0) {
1417                                 (void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
1418                                 return (TOPO_WALK_ERR);
1419                         }
1420                         hwp->hcw_index++;
1421                         topo_mod_dprintf(mod, "hc_walker: walk to facility "
1422                             "node:%s=%s\n", fname, ftype);
1423                         return (topo_walk_byid(hwp->hcw_wp, fname, 0));
1424                 }
1425 
1426                 /*
1427                  * Otherwise, this is the node we're looking for.
1428                  */
1429                 if ((err = hwp->hcw_cb(mod, node, hwp->hcw_priv)) != 0) {
1430                         (void) topo_mod_seterrno(mod, err);
1431                         topo_mod_dprintf(mod, "hc_walker: callback "
1432                             "failed: %s\n ", topo_mod_errmsg(mod));
1433                         return (TOPO_WALK_ERR);
1434                 } else {
1435                         topo_mod_dprintf(mod, "hc_walker: callback "
1436                             "complete: terminate walk\n");
1437                         return (TOPO_WALK_TERMINATE);
1438                 }
1439         }
1440 
1441         /*
1442          * Move on to the next component in the hc-list
1443          */
1444         hwp->hcw_index = ++i;
1445         err = nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_NAME, &name);
1446         err |= nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_ID, &id);
1447         if (err != 0) {
1448                 (void) topo_mod_seterrno(mod, err);
1449                 return (TOPO_WALK_ERR);
1450         }
1451         inst = atoi(id);
1452 
1453         return (topo_walk_byid(hwp->hcw_wp, name, inst));
1454 
1455 }
1456 
1457 static struct hc_walk *
1458 hc_walk_init(topo_mod_t *mod, tnode_t *node, nvlist_t *rsrc,
1459     topo_mod_walk_cb_t cb, void *pdata)
1460 {
1461         int err, ret;
1462         uint_t sz;
1463         struct hc_walk *hwp;
1464         topo_walk_t *wp;
1465 
1466         if ((hwp = topo_mod_alloc(mod, sizeof (struct hc_walk))) == NULL) {
1467                 (void) topo_mod_seterrno(mod, EMOD_NOMEM);
1468                 return (NULL);
1469         }
1470 
1471         if (nvlist_lookup_nvlist_array(rsrc, FM_FMRI_HC_LIST, &hwp->hcw_list,
1472             &sz) != 0) {
1473                 topo_mod_dprintf(mod, "hc_walk_init: failed to lookup %s "
1474                     "nvlist\n", FM_FMRI_HC_LIST);
1475                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1476                 (void) topo_mod_seterrno(mod, EMOD_METHOD_INVAL);
1477                 return (NULL);
1478         }
1479         if ((ret = nvlist_lookup_nvlist(rsrc, FM_FMRI_FACILITY, &hwp->hcw_fac))
1480             != 0) {
1481                 if (ret != ENOENT) {
1482                         topo_mod_dprintf(mod, "hc_walk_init: unexpected error "
1483                             "looking up %s nvlist", FM_FMRI_FACILITY);
1484                         topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1485                         (void) topo_mod_seterrno(mod, EMOD_METHOD_INVAL);
1486                         return (NULL);
1487                 } else {
1488                         hwp->hcw_fac = NULL;
1489                 }
1490         }
1491 
1492         hwp->hcw_fmri = rsrc;
1493         hwp->hcw_end = sz - 1;
1494         hwp->hcw_index = 0;
1495         hwp->hcw_priv = pdata;
1496         hwp->hcw_cb = cb;
1497         if ((wp = topo_mod_walk_init(mod, node, hc_walker, (void *)hwp, &err))
1498             == NULL) {
1499                 topo_mod_dprintf(mod, "hc_walk_init: topo_mod_walk_init failed "
1500                     "(%s)\n", topo_strerror(err));
1501                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1502                 (void) topo_mod_seterrno(mod, err);
1503                 return (NULL);
1504         }
1505 
1506         hwp->hcw_wp = wp;
1507 
1508         return (hwp);
1509 }
1510 
1511 struct prop_lookup {
1512         const char *pl_pgroup;
1513         const char *pl_pname;
1514         int pl_flag;
1515         nvlist_t *pl_args;
1516         nvlist_t *pl_rsrc;
1517         nvlist_t *pl_prop;
1518 };
1519 
1520 /*ARGSUSED*/
1521 static int
1522 hc_prop_get(topo_mod_t *mod, tnode_t *node, void *pdata)
1523 {
1524         int err = 0;
1525 
1526         struct prop_lookup *plp = (struct prop_lookup *)pdata;
1527 
1528         (void) topo_prop_getprop(node, plp->pl_pgroup, plp->pl_pname,
1529             plp->pl_args, &plp->pl_prop, &err);
1530 
1531         return (err);
1532 }
1533 
1534 static int
1535 hc_fmri_prop_get(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1536     nvlist_t *in, nvlist_t **out)
1537 {
1538         int err;
1539         struct hc_walk *hwp;
1540         struct prop_lookup *plp;
1541 
1542         if (version > TOPO_METH_PROP_GET_VERSION)
1543                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
1544 
1545         if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL)
1546                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
1547 
1548         err = nvlist_lookup_string(in, TOPO_PROP_GROUP,
1549             (char **)&plp->pl_pgroup);
1550         err |= nvlist_lookup_string(in, TOPO_PROP_VAL_NAME,
1551             (char **)&plp->pl_pname);
1552         err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc);
1553         if (err != 0) {
1554                 topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1555                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1556         }
1557 
1558         /*
1559          * Private args to prop method are optional
1560          */
1561         if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &plp->pl_args))
1562             != 0) {
1563                 if (err != ENOENT) {
1564                         topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1565                         return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1566                 } else {
1567                         plp->pl_args = NULL;
1568                 }
1569         }
1570 
1571         plp->pl_prop = NULL;
1572         if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_prop_get,
1573             (void *)plp)) != NULL) {
1574                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
1575                     TOPO_WALK_ERR)
1576                         err = -1;
1577                 else
1578                         err = 0;
1579                 topo_walk_fini(hwp->hcw_wp);
1580                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1581         } else {
1582                 err = -1;
1583         }
1584 
1585         if (plp->pl_prop != NULL)
1586                 *out = plp->pl_prop;
1587 
1588         topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1589 
1590         return (err);
1591 }
1592 
1593 /*ARGSUSED*/
1594 static int
1595 hc_pgrp_get(topo_mod_t *mod, tnode_t *node, void *pdata)
1596 {
1597         int err = 0;
1598 
1599         struct prop_lookup *plp = (struct prop_lookup *)pdata;
1600 
1601         (void) topo_prop_getpgrp(node, plp->pl_pgroup, &plp->pl_prop, &err);
1602 
1603         return (err);
1604 }
1605 
1606 static int
1607 hc_fmri_pgrp_get(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1608     nvlist_t *in, nvlist_t **out)
1609 {
1610         int err;
1611         struct hc_walk *hwp;
1612         struct prop_lookup *plp;
1613 
1614         if (version > TOPO_METH_PGRP_GET_VERSION)
1615                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
1616 
1617         if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL)
1618                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
1619 
1620         err = nvlist_lookup_string(in, TOPO_PROP_GROUP,
1621             (char **)&plp->pl_pgroup);
1622         err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc);
1623         if (err != 0) {
1624                 topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1625                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1626         }
1627 
1628         plp->pl_prop = NULL;
1629         if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_pgrp_get,
1630             (void *)plp)) != NULL) {
1631                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
1632                     TOPO_WALK_ERR)
1633                         err = -1;
1634                 else
1635                         err = 0;
1636                 topo_walk_fini(hwp->hcw_wp);
1637                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1638         } else {
1639                 err = -1;
1640         }
1641 
1642         if (plp->pl_prop != NULL)
1643                 *out = plp->pl_prop;
1644 
1645         topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1646 
1647         return (err);
1648 }
1649 
1650 /*ARGSUSED*/
1651 static int
1652 hc_prop_setprop(topo_mod_t *mod, tnode_t *node, void *pdata)
1653 {
1654         int err = 0;
1655 
1656         struct prop_lookup *plp = (struct prop_lookup *)pdata;
1657 
1658         (void) topo_prop_setprop(node, plp->pl_pgroup, plp->pl_prop,
1659             plp->pl_flag, plp->pl_args, &err);
1660 
1661         return (err);
1662 }
1663 
1664 /*ARGSUSED*/
1665 static int
1666 hc_fmri_prop_set(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1667     nvlist_t *in, nvlist_t **out)
1668 {
1669         int err;
1670         struct hc_walk *hwp;
1671         struct prop_lookup *plp;
1672 
1673         if (version > TOPO_METH_PROP_SET_VERSION)
1674                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
1675 
1676         if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL)
1677                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
1678 
1679         err = nvlist_lookup_string(in, TOPO_PROP_GROUP,
1680             (char **)&plp->pl_pgroup);
1681         err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc);
1682         err |= nvlist_lookup_nvlist(in, TOPO_PROP_VAL, &plp->pl_prop);
1683         err |= nvlist_lookup_int32(in, TOPO_PROP_FLAG, &plp->pl_flag);
1684         if (err != 0) {
1685                 topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1686                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1687         }
1688 
1689         /*
1690          * Private args to prop method are optional
1691          */
1692         if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &plp->pl_args))
1693             != 0) {
1694                 if (err != ENOENT)
1695                         return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
1696                 else
1697                         plp->pl_args = NULL;
1698         }
1699 
1700         if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_prop_setprop,
1701             (void *)plp)) != NULL) {
1702                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
1703                     TOPO_WALK_ERR)
1704                         err = -1;
1705                 else
1706                         err = 0;
1707                 topo_walk_fini(hwp->hcw_wp);
1708                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1709         } else {
1710                 err = -1;
1711         }
1712 
1713         topo_mod_free(mod, plp, sizeof (struct prop_lookup));
1714 
1715         return (err);
1716 }
1717 
1718 struct hc_args {
1719         nvlist_t *ha_fmri;
1720         nvlist_t *ha_nvl;
1721         char *ha_method_name;
1722         topo_version_t ha_method_ver;
1723 };
1724 
1725 static int
1726 hc_auth_changed(nvlist_t *nva, nvlist_t *nvb, const char *propname)
1727 {
1728         char *stra, *strb;
1729 
1730         if (nvlist_lookup_string(nva, propname, &stra) != 0 ||
1731             nvlist_lookup_string(nvb, propname, &strb) != 0)
1732                 return (FMD_OBJ_STATE_UNKNOWN);
1733 
1734         if (strcmp(stra, strb) != 0)
1735                 return (FMD_OBJ_STATE_REPLACED);
1736         else
1737                 return (FMD_OBJ_STATE_STILL_PRESENT);
1738 }
1739 
1740 static int
1741 hc_is_present(topo_mod_t *mod, tnode_t *node, void *pdata)
1742 {
1743         int err;
1744         struct hc_args *hap = (struct hc_args *)pdata;
1745         nvlist_t *rsrc;
1746         boolean_t present;
1747 
1748         /*
1749          * check with the enumerator that created this FMRI
1750          * (topo node)
1751          */
1752         if (topo_method_invoke(node, TOPO_METH_PRESENT,
1753             TOPO_METH_PRESENT_VERSION, hap->ha_fmri, &hap->ha_nvl,
1754             &err) < 0) {
1755 
1756                 /*
1757                  * If the method exists but failed for some other reason,
1758                  * propagate the error as making any decision over presence is
1759                  * impossible.
1760                  */
1761                 if (err != ETOPO_METHOD_NOTSUP)
1762                         return (err);
1763 
1764                 /*
1765                  * Check the authority information.  If the part id or serial
1766                  * number doesn't match, then it isn't the same FMRI.
1767                  * Otherwise, assume presence.
1768                  */
1769                 if (topo_node_resource(node, &rsrc, &err) != 0)
1770                         return (err);
1771 
1772                 present = B_TRUE;
1773                 if (hc_auth_changed(hap->ha_fmri, rsrc,
1774                     FM_FMRI_HC_SERIAL_ID) == FMD_OBJ_STATE_REPLACED ||
1775                     hc_auth_changed(hap->ha_fmri, rsrc,
1776                     FM_FMRI_HC_PART) == FMD_OBJ_STATE_REPLACED) {
1777                         present = B_FALSE;
1778                 }
1779                 nvlist_free(rsrc);
1780 
1781                 if (topo_mod_nvalloc(mod, &hap->ha_nvl, NV_UNIQUE_NAME) != 0)
1782                         return (EMOD_NOMEM);
1783 
1784                 if (nvlist_add_uint32(hap->ha_nvl,
1785                     TOPO_METH_PRESENT_RET, present) != 0) {
1786                         nvlist_free(hap->ha_nvl);
1787                         hap->ha_nvl = NULL;
1788                         return (EMOD_NOMEM);
1789                 }
1790         }
1791 
1792         return (0);
1793 }
1794 
1795 static int
1796 hc_fmri_present(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1797     nvlist_t *in, nvlist_t **out)
1798 {
1799         int err;
1800         struct hc_walk *hwp;
1801         struct hc_args *hap;
1802 
1803         if (version > TOPO_METH_PRESENT_VERSION)
1804                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
1805 
1806         if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL)
1807                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
1808 
1809         hap->ha_fmri = in;
1810         hap->ha_nvl = NULL;
1811         if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_is_present,
1812             (void *)hap)) != NULL) {
1813                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
1814                     TOPO_WALK_ERR)
1815                         err = -1;
1816                 else
1817                         err = 0;
1818                 topo_walk_fini(hwp->hcw_wp);
1819                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1820         } else {
1821                 err = -1;
1822         }
1823 
1824         if (hap->ha_nvl != NULL)
1825                 *out = hap->ha_nvl;
1826 
1827         topo_mod_free(mod, hap, sizeof (struct hc_args));
1828 
1829         return (err);
1830 }
1831 
1832 static int
1833 hc_is_replaced(topo_mod_t *mod, tnode_t *node, void *pdata)
1834 {
1835         int err;
1836         struct hc_args *hap = (struct hc_args *)pdata;
1837         uint32_t present = 0;
1838         nvlist_t *rsrc;
1839         uint32_t rval = FMD_OBJ_STATE_UNKNOWN;
1840 
1841         /*
1842          * check with the enumerator that created this FMRI
1843          * (topo node)
1844          */
1845         if (topo_method_invoke(node, TOPO_METH_REPLACED,
1846             TOPO_METH_REPLACED_VERSION, hap->ha_fmri, &hap->ha_nvl,
1847             &err) < 0) {
1848                 /*
1849                  * If the method exists but failed for some other
1850                  * reason, propagate the error as making any decision
1851                  * over presence is impossible.
1852                  */
1853                 if (err != ETOPO_METHOD_NOTSUP)
1854                         return (err);
1855 
1856                 /*
1857                  * Enumerator didn't provide "replaced" method -
1858                  * try "present" method
1859                  */
1860                 if (topo_method_invoke(node, TOPO_METH_PRESENT,
1861                     TOPO_METH_PRESENT_VERSION, hap->ha_fmri, &hap->ha_nvl,
1862                     &err) < 0) {
1863                         /*
1864                          * If the method exists but failed for some other
1865                          * reason, propagate the error as making any decision
1866                          * over presence is impossible.
1867                          */
1868                         if (err != ETOPO_METHOD_NOTSUP)
1869                                 return (err);
1870 
1871                         /*
1872                          * Enumerator didn't provide "present" method either -
1873                          * so check the authority information.  If the part id
1874                          * or serial number doesn't match, then it isn't the
1875                          * same FMRI. Otherwise, if we have a serial number and
1876                          * it hasn't changed, then assume it is the same FMRI.
1877                          */
1878                         if (topo_node_resource(node, &rsrc, &err) != 0)
1879                                 return (err);
1880                         rval = hc_auth_changed(hap->ha_fmri, rsrc,
1881                             FM_FMRI_HC_PART);
1882                         if (rval != FMD_OBJ_STATE_REPLACED)
1883                                 rval = hc_auth_changed(hap->ha_fmri, rsrc,
1884                                     FM_FMRI_HC_SERIAL_ID);
1885                         nvlist_free(rsrc);
1886                         if (topo_mod_nvalloc(mod, &hap->ha_nvl,
1887                             NV_UNIQUE_NAME) != 0)
1888                                 return (EMOD_NOMEM);
1889                         if (nvlist_add_uint32(hap->ha_nvl,
1890                             TOPO_METH_REPLACED_RET, rval) != 0) {
1891                                 nvlist_free(hap->ha_nvl);
1892                                 hap->ha_nvl = NULL;
1893                                 return (ETOPO_PROP_NVL);
1894                         }
1895                 } else {
1896                         (void) nvlist_lookup_uint32(hap->ha_nvl,
1897                             TOPO_METH_PRESENT_RET, &present);
1898                         (void) nvlist_remove(hap->ha_nvl,
1899                             TOPO_METH_PRESENT_RET, DATA_TYPE_UINT32);
1900                         if (nvlist_add_uint32(hap->ha_nvl,
1901                             TOPO_METH_REPLACED_RET,
1902                             present ? FMD_OBJ_STATE_UNKNOWN :
1903                             FMD_OBJ_STATE_NOT_PRESENT) != 0) {
1904                                 nvlist_free(hap->ha_nvl);
1905                                 hap->ha_nvl = NULL;
1906                                 return (ETOPO_PROP_NVL);
1907                         }
1908                 }
1909         }
1910         return (0);
1911 }
1912 
1913 static int
1914 hc_fmri_replaced(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1915     nvlist_t *in, nvlist_t **out)
1916 {
1917         int err;
1918         struct hc_walk *hwp;
1919         struct hc_args *hap;
1920 
1921         if (version > TOPO_METH_REPLACED_VERSION)
1922                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
1923 
1924         if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL)
1925                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
1926 
1927         hap->ha_fmri = in;
1928         hap->ha_nvl = NULL;
1929         if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_is_replaced,
1930             (void *)hap)) != NULL) {
1931                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
1932                     TOPO_WALK_ERR)
1933                         err = -1;
1934                 else
1935                         err = 0;
1936                 topo_walk_fini(hwp->hcw_wp);
1937                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
1938         } else {
1939                 err = -1;
1940         }
1941 
1942         if (hap->ha_nvl != NULL)
1943                 *out = hap->ha_nvl;
1944 
1945         topo_mod_free(mod, hap, sizeof (struct hc_args));
1946 
1947         return (err);
1948 }
1949 
1950 static int
1951 hc_unusable(topo_mod_t *mod, tnode_t *node, void *pdata)
1952 {
1953         int err;
1954         struct hc_args *hap = (struct hc_args *)pdata;
1955 
1956         /*
1957          * check with the enumerator that created this FMRI
1958          * (topo node)
1959          */
1960         if (topo_method_invoke(node, TOPO_METH_UNUSABLE,
1961             TOPO_METH_UNUSABLE_VERSION, hap->ha_fmri, &hap->ha_nvl,
1962             &err) < 0) {
1963 
1964                 /*
1965                  * Err on the side of caution and return usable
1966                  */
1967                 if (topo_mod_nvalloc(mod, &hap->ha_nvl, NV_UNIQUE_NAME) == 0)
1968                         if (nvlist_add_uint32(hap->ha_nvl,
1969                             TOPO_METH_UNUSABLE_RET, 0) == 0)
1970                                 return (0);
1971 
1972                 return (ETOPO_PROP_NVL);
1973         }
1974 
1975         return (0);
1976 }
1977 
1978 static int
1979 hc_fmri_unusable(topo_mod_t *mod, tnode_t *node, topo_version_t version,
1980     nvlist_t *in, nvlist_t **out)
1981 {
1982         int err;
1983         struct hc_walk *hwp;
1984         struct hc_args *hap;
1985 
1986         if (version > TOPO_METH_UNUSABLE_VERSION)
1987                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
1988 
1989         if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL)
1990                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
1991 
1992         hap->ha_fmri = in;
1993         hap->ha_nvl = NULL;
1994         if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_unusable,
1995             (void *)hap)) != NULL) {
1996                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
1997                     TOPO_WALK_ERR)
1998                         err = -1;
1999                 else
2000                         err = 0;
2001                 topo_walk_fini(hwp->hcw_wp);
2002                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
2003         } else {
2004                 err = -1;
2005         }
2006 
2007         if (hap->ha_nvl != NULL)
2008                 *out = hap->ha_nvl;
2009 
2010         topo_mod_free(mod, hap, sizeof (struct hc_args));
2011 
2012         return (err);
2013 }
2014 
2015 struct fac_lookup {
2016         const char *fl_fac_type;
2017         uint32_t fl_fac_subtype;
2018 #ifdef _LP64
2019         uint64_t fl_callback;
2020         uint64_t fl_callback_args;
2021 #else
2022         uint32_t fl_callback;
2023         uint32_t fl_callback_args;
2024 #endif
2025         nvlist_t *fl_rsrc;
2026         nvlist_t *fl_fac_rsrc;
2027 };
2028 
2029 static int
2030 hc_fac_get(topo_mod_t *mod, tnode_t *node, void *pdata)
2031 {
2032         struct fac_lookup *flp = (struct fac_lookup *)pdata;
2033         topo_walk_cb_t cb = (topo_walk_cb_t)flp->fl_callback;
2034         topo_faclist_t faclist, *tmp;
2035         int err, ret = 0;
2036 
2037         /*
2038          * Lookup the specified facility node.  Return with an error if we can't
2039          * find it.
2040          */
2041         if (topo_node_facility(mod->tm_hdl, node, flp->fl_fac_type,
2042             flp->fl_fac_subtype, &faclist, &err) != 0) {
2043                 topo_mod_dprintf(mod, "hc_fac_get: topo_node_facility "
2044                     "failed\n");
2045                 return (TOPO_WALK_ERR);
2046         }
2047 
2048         /*
2049          * Invoke user's callback for each facility node in the topo list,
2050          * passing in a pointer to the facility node
2051          */
2052         for (tmp = topo_list_next(&faclist.tf_list); tmp != NULL;
2053             tmp = topo_list_next(tmp)) {
2054 
2055                 if ((err = cb(mod->tm_hdl, tmp->tf_node,
2056                     (void *)flp->fl_callback_args)) != 0) {
2057                         (void) topo_mod_seterrno(mod, err);
2058                         topo_mod_dprintf(mod, "hc_fac_get: callback failed: "
2059                             "%s\n ", topo_mod_errmsg(mod));
2060                         ret = TOPO_WALK_ERR;
2061                         break;
2062                 }
2063         }
2064 
2065         while ((tmp = topo_list_next(&faclist.tf_list)) != NULL) {
2066                 topo_list_delete(&faclist.tf_list, tmp);
2067                 topo_mod_free(mod, tmp, sizeof (topo_faclist_t));
2068         }
2069         return (ret);
2070 }
2071 
2072 static int
2073 hc_fmri_facility(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2074     nvlist_t *in, nvlist_t **out)
2075 {
2076         int err = 0;
2077         struct hc_walk *hwp;
2078         struct fac_lookup *flp;
2079 
2080         if (version > TOPO_METH_FACILITY_VERSION)
2081                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
2082 
2083         if ((flp = topo_mod_alloc(mod, sizeof (struct fac_lookup))) == NULL)
2084                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
2085 
2086         /*
2087          * lookup arguments: hw resource, facility type, facility subtype,
2088          *  callback and callback args
2089          */
2090         err = nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &flp->fl_rsrc);
2091         err |= nvlist_lookup_string(in, FM_FMRI_FACILITY_TYPE,
2092             (char **)&flp->fl_fac_type);
2093         err |= nvlist_lookup_uint32(in, "type", &flp->fl_fac_subtype);
2094 #ifdef _LP64
2095         err |= nvlist_lookup_uint64(in, "callback", &flp->fl_callback);
2096         err |= nvlist_lookup_uint64(in, "callback-args",
2097             &flp->fl_callback_args);
2098 #else
2099         err |= nvlist_lookup_uint32(in, "callback", &flp->fl_callback);
2100         err |= nvlist_lookup_uint32(in, "callback-args",
2101             &flp->fl_callback_args);
2102 #endif
2103         if (err != 0) {
2104                 topo_mod_dprintf(mod, "hc_fmri_facility: failed to construct "
2105                     "walker arg nvlist\n");
2106                 topo_mod_free(mod, flp, sizeof (struct fac_lookup));
2107                 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL));
2108         }
2109 
2110         flp->fl_fac_rsrc = NULL;
2111         if ((hwp = hc_walk_init(mod, node, flp->fl_rsrc, hc_fac_get,
2112             (void *)flp)) != NULL) {
2113                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
2114                     TOPO_WALK_ERR)
2115                         err = -1;
2116                 else
2117                         err = 0;
2118                 topo_walk_fini(hwp->hcw_wp);
2119                 topo_mod_free(mod, hwp, sizeof (struct hc_walk));
2120         } else {
2121                 topo_mod_dprintf(mod, "hc_fmri_facility: failed to initialize "
2122                     "hc walker\n");
2123                 err = -1;
2124         }
2125 
2126         if (flp->fl_fac_rsrc != NULL)
2127                 *out = flp->fl_fac_rsrc;
2128 
2129         topo_mod_free(mod, flp, sizeof (struct fac_lookup));
2130 
2131         return (err);
2132 }
2133 
2134 /* ARGSUSED */
2135 static int
2136 hc_expand(topo_mod_t *mod, tnode_t *node, void *pdata)
2137 {
2138         int err;
2139         nvlist_t *nvl;
2140         const char **namep;
2141         struct hc_args *hap = (struct hc_args *)pdata;
2142         const char *names[] = {
2143                 FM_FMRI_HC_SERIAL_ID,
2144                 FM_FMRI_HC_PART,
2145                 FM_FMRI_HC_REVISION,
2146                 NULL
2147         };
2148 
2149         if (topo_node_resource(node, &nvl, &err) != 0)
2150                 return (ETOPO_METHOD_FAIL);
2151 
2152         for (namep = names; *namep != NULL; namep++) {
2153                 char *in_val, *node_val;
2154 
2155                 if (nvlist_lookup_string(nvl, *namep, &node_val) != 0)
2156                         continue;
2157 
2158                 if (nvlist_lookup_string(hap->ha_fmri, *namep, &in_val) == 0) {
2159                         if (strcmp(in_val, node_val) == 0)
2160                                 continue;
2161                         (void) nvlist_remove(hap->ha_fmri, *namep,
2162                             DATA_TYPE_STRING);
2163                 }
2164 
2165                 if (nvlist_add_string(hap->ha_fmri, *namep, node_val) != 0) {
2166                         nvlist_free(nvl);
2167                         return (ETOPO_PROP_NVL);
2168                 }
2169         }
2170         nvlist_free(nvl);
2171 
2172         return (0);
2173 }
2174 
2175 /* ARGSUSED */
2176 static int
2177 hc_fmri_expand(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2178     nvlist_t *in, nvlist_t **out)
2179 {
2180         int err;
2181         struct hc_walk *hwp;
2182         struct hc_args *hap;
2183 
2184         if (version > TOPO_METH_EXPAND_VERSION)
2185                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
2186 
2187         if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL)
2188                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
2189 
2190         hap->ha_fmri = in;
2191         hap->ha_nvl = NULL;
2192         if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_expand,
2193             (void *)hap)) != NULL) {
2194                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
2195                     TOPO_WALK_ERR)
2196                         err = -1;
2197                 else
2198                         err = 0;
2199                 topo_walk_fini(hwp->hcw_wp);
2200         } else {
2201                 err = -1;
2202         }
2203 
2204         topo_mod_free(mod, hwp, sizeof (struct hc_walk));
2205 
2206         /* expand method should not return out nvlist */
2207         assert(hap->ha_nvl == NULL);
2208 
2209         topo_mod_free(mod, hap, sizeof (struct hc_args));
2210 
2211         return (err);
2212 }
2213 
2214 static int
2215 hc_retire_subr(topo_mod_t *mod, tnode_t *node, void *pdata)
2216 {
2217         int err, rc;
2218         struct hc_args *hap = (struct hc_args *)pdata;
2219 
2220         topo_mod_dprintf(mod, "hc_retire_subr: invoking method %s\n",
2221             hap->ha_method_name);
2222         /*
2223          * check with the enumerator that created this FMRI
2224          * (topo node)
2225          */
2226         rc = topo_method_invoke(node, hap->ha_method_name,
2227             hap->ha_method_ver, hap->ha_fmri, &hap->ha_nvl, &err);
2228 
2229         topo_mod_dprintf(mod, "hc_retire_subr: invoking method %s "
2230             "returned %d\n", hap->ha_method_name, rc);
2231 
2232         return (rc < 0 ? err : 0);
2233 }
2234 
2235 static int
2236 hc_fmri_retire_subr(topo_mod_t *mod, tnode_t *node, char *method_name,
2237     topo_version_t builtin_version, topo_version_t version, nvlist_t *in,
2238     nvlist_t **out)
2239 {
2240         int err;
2241         struct hc_walk *hwp;
2242         struct hc_args *hap;
2243 
2244         if (version > builtin_version)
2245                 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW));
2246 
2247         if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL)
2248                 return (topo_mod_seterrno(mod, EMOD_NOMEM));
2249 
2250         hap->ha_fmri = in;
2251         hap->ha_nvl = NULL;
2252         hap->ha_method_name = method_name;
2253         hap->ha_method_ver = version;
2254         if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_retire_subr,
2255             (void *)hap)) != NULL) {
2256                 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) ==
2257                     TOPO_WALK_ERR)
2258                         err = -1;
2259                 else
2260                         err = 0;
2261                 topo_walk_fini(hwp->hcw_wp);
2262         } else {
2263                 err = -1;
2264         }
2265 
2266         topo_mod_free(mod, hwp, sizeof (struct hc_walk));
2267 
2268         if (hap->ha_nvl != NULL)
2269                 *out = hap->ha_nvl;
2270 
2271         topo_mod_free(mod, hap, sizeof (struct hc_args));
2272 
2273         return (err);
2274 }
2275 
2276 static int
2277 hc_fmri_retire(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2278     nvlist_t *in, nvlist_t **out)
2279 {
2280         return (hc_fmri_retire_subr(mod, node, TOPO_METH_RETIRE,
2281             TOPO_METH_RETIRE_VERSION, version, in, out));
2282 }
2283 
2284 static int
2285 hc_fmri_unretire(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2286     nvlist_t *in, nvlist_t **out)
2287 {
2288         return (hc_fmri_retire_subr(mod, node, TOPO_METH_UNRETIRE,
2289             TOPO_METH_UNRETIRE_VERSION, version, in, out));
2290 }
2291 
2292 static int
2293 hc_fmri_service_state(topo_mod_t *mod, tnode_t *node, topo_version_t version,
2294     nvlist_t *in, nvlist_t **out)
2295 {
2296         return (hc_fmri_retire_subr(mod, node, TOPO_METH_SERVICE_STATE,
2297             TOPO_METH_SERVICE_STATE_VERSION, version, in, out));
2298 }