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 2007 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <errno.h> 34 #include <ctype.h> 35 #include <alloca.h> 36 #include <limits.h> 37 #include <fm/topo_mod.h> 38 #include <fm/topo_hc.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_subr.h> 48 #include <topo_prop.h> 49 #include <hc.h> 50 51 static int hc_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t, 52 topo_instance_t, void *, void *); 53 static void hc_release(topo_mod_t *, tnode_t *); 54 static int hc_fmri_nvl2str(topo_mod_t *, tnode_t *, topo_version_t, 55 nvlist_t *, nvlist_t **); 56 static int hc_fmri_str2nvl(topo_mod_t *, tnode_t *, topo_version_t, 57 nvlist_t *, nvlist_t **); 58 static int hc_compare(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *, 59 nvlist_t **); 60 static int hc_fmri_present(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *, 61 nvlist_t **); 62 static int hc_fmri_unusable(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *, 63 nvlist_t **); 64 static int hc_fmri_create_meth(topo_mod_t *, tnode_t *, topo_version_t, 65 nvlist_t *, nvlist_t **); 66 static int hc_fmri_prop_get(topo_mod_t *, tnode_t *, topo_version_t, 67 nvlist_t *, nvlist_t **); 68 static int hc_fmri_prop_set(topo_mod_t *, tnode_t *, topo_version_t, 69 nvlist_t *, nvlist_t **); 70 static int hc_fmri_pgrp_get(topo_mod_t *, tnode_t *, topo_version_t, 71 nvlist_t *, nvlist_t **); 72 73 static nvlist_t *hc_fmri_create(topo_mod_t *, nvlist_t *, int, const char *, 74 topo_instance_t inst, const nvlist_t *, const char *, const char *, 75 const char *); 76 77 const topo_method_t hc_methods[] = { 78 { TOPO_METH_NVL2STR, TOPO_METH_NVL2STR_DESC, TOPO_METH_NVL2STR_VERSION, 79 TOPO_STABILITY_INTERNAL, hc_fmri_nvl2str }, 80 { TOPO_METH_STR2NVL, TOPO_METH_STR2NVL_DESC, TOPO_METH_STR2NVL_VERSION, 81 TOPO_STABILITY_INTERNAL, hc_fmri_str2nvl }, 82 { TOPO_METH_COMPARE, TOPO_METH_COMPARE_DESC, TOPO_METH_COMPARE_VERSION, 83 TOPO_STABILITY_INTERNAL, hc_compare }, 84 { TOPO_METH_PRESENT, TOPO_METH_PRESENT_DESC, TOPO_METH_PRESENT_VERSION, 85 TOPO_STABILITY_INTERNAL, hc_fmri_present }, 86 { TOPO_METH_UNUSABLE, TOPO_METH_UNUSABLE_DESC, 87 TOPO_METH_UNUSABLE_VERSION, TOPO_STABILITY_INTERNAL, 88 hc_fmri_unusable }, 89 { TOPO_METH_FMRI, TOPO_METH_FMRI_DESC, TOPO_METH_FMRI_VERSION, 90 TOPO_STABILITY_INTERNAL, hc_fmri_create_meth }, 91 { TOPO_METH_PROP_GET, TOPO_METH_PROP_GET_DESC, 92 TOPO_METH_PROP_GET_VERSION, TOPO_STABILITY_INTERNAL, 93 hc_fmri_prop_get }, 94 { TOPO_METH_PROP_SET, TOPO_METH_PROP_SET_DESC, 95 TOPO_METH_PROP_SET_VERSION, TOPO_STABILITY_INTERNAL, 96 hc_fmri_prop_set }, 97 { TOPO_METH_PGRP_GET, TOPO_METH_PGRP_GET_DESC, 98 TOPO_METH_PGRP_GET_VERSION, TOPO_STABILITY_INTERNAL, 99 hc_fmri_pgrp_get }, 100 { NULL } 101 }; 102 103 static const topo_modops_t hc_ops = 104 { hc_enum, hc_release }; 105 static const topo_modinfo_t hc_info = 106 { HC, FM_FMRI_SCHEME_HC, HC_VERSION, &hc_ops }; 107 108 static const hcc_t hc_canon[] = { 109 { BRANCH, TOPO_STABILITY_PRIVATE }, 110 { CMP, TOPO_STABILITY_PRIVATE }, 111 { CENTERPLANE, TOPO_STABILITY_PRIVATE }, 112 { CHASSIS, TOPO_STABILITY_PRIVATE }, 113 { CHIP, TOPO_STABILITY_PRIVATE }, 114 { CHIP_SELECT, TOPO_STABILITY_PRIVATE }, 115 { CPU, TOPO_STABILITY_PRIVATE }, 116 { DIMM, TOPO_STABILITY_PRIVATE }, 117 { DISK, TOPO_STABILITY_PRIVATE }, 118 { DRAMCHANNEL, TOPO_STABILITY_PRIVATE }, 119 { HOSTBRIDGE, TOPO_STABILITY_PRIVATE }, 120 { INTERCONNECT, TOPO_STABILITY_PRIVATE }, 121 { IOBOARD, TOPO_STABILITY_PRIVATE }, 122 { MEMORYCONTROL, TOPO_STABILITY_PRIVATE }, 123 { MOTHERBOARD, TOPO_STABILITY_PRIVATE }, 124 { NIU, TOPO_STABILITY_PRIVATE }, 125 { NIUFN, TOPO_STABILITY_PRIVATE }, 126 { PCI_BUS, TOPO_STABILITY_PRIVATE }, 127 { PCI_DEVICE, TOPO_STABILITY_PRIVATE }, 128 { PCI_FUNCTION, TOPO_STABILITY_PRIVATE }, 129 { PCIEX_BUS, TOPO_STABILITY_PRIVATE }, 130 { PCIEX_DEVICE, TOPO_STABILITY_PRIVATE }, 131 { PCIEX_FUNCTION, TOPO_STABILITY_PRIVATE }, 132 { PCIEX_ROOT, TOPO_STABILITY_PRIVATE }, 133 { PCIEX_SWUP, TOPO_STABILITY_PRIVATE }, 134 { PCIEX_SWDWN, TOPO_STABILITY_PRIVATE }, 135 { RANK, TOPO_STABILITY_PRIVATE }, 136 { SATA_PORT, TOPO_STABILITY_PRIVATE }, 137 { SYSTEMBOARD, TOPO_STABILITY_PRIVATE }, 138 { XAUI, TOPO_STABILITY_PRIVATE }, 139 { XFP, TOPO_STABILITY_PRIVATE } 140 }; 141 142 static int hc_ncanon = sizeof (hc_canon) / sizeof (hcc_t); 143 144 int 145 hc_init(topo_mod_t *mod, topo_version_t version) 146 { 147 /* 148 * Turn on module debugging output 149 */ 150 if (getenv("TOPOHCDEBUG")) 151 topo_mod_setdebug(mod); 152 153 topo_mod_dprintf(mod, "initializing hc builtin\n"); 154 155 if (version != HC_VERSION) 156 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 157 158 if (topo_mod_register(mod, &hc_info, TOPO_VERSION) != 0) { 159 topo_mod_dprintf(mod, "failed to register hc: " 160 "%s\n", topo_mod_errmsg(mod)); 161 return (-1); /* mod errno already set */ 162 } 163 164 return (0); 165 } 166 167 void 168 hc_fini(topo_mod_t *mod) 169 { 170 topo_mod_unregister(mod); 171 } 172 173 174 static const topo_pgroup_info_t sys_pgroup = { 175 TOPO_PGROUP_SYSTEM, 176 TOPO_STABILITY_PRIVATE, 177 TOPO_STABILITY_PRIVATE, 178 1 179 }; 180 181 static const topo_pgroup_info_t auth_pgroup = { 182 FM_FMRI_AUTHORITY, 183 TOPO_STABILITY_PRIVATE, 184 TOPO_STABILITY_PRIVATE, 185 1 186 }; 187 188 static void 189 hc_prop_set(tnode_t *node, nvlist_t *auth) 190 { 191 int err; 192 char isa[MAXNAMELEN]; 193 struct utsname uts; 194 char *prod, *csn, *server; 195 196 if (auth == NULL) 197 return; 198 199 if (topo_pgroup_create(node, &auth_pgroup, &err) != 0) { 200 if (err != ETOPO_PROP_DEFD) 201 return; 202 } 203 204 /* 205 * Inherit if we can, it saves memory 206 */ 207 if (topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_PRODUCT, 208 &err) != 0) { 209 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT, &prod) 210 == 0) 211 (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY, 212 FM_FMRI_AUTH_PRODUCT, TOPO_PROP_IMMUTABLE, prod, 213 &err); 214 } 215 if (topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_CHASSIS, 216 &err) != 0) { 217 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS, &csn) == 0) 218 (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY, 219 FM_FMRI_AUTH_CHASSIS, TOPO_PROP_IMMUTABLE, csn, 220 &err); 221 } 222 if (topo_prop_inherit(node, FM_FMRI_AUTHORITY, FM_FMRI_AUTH_SERVER, 223 &err) != 0) { 224 if (nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server) 225 == 0) 226 (void) topo_prop_set_string(node, FM_FMRI_AUTHORITY, 227 FM_FMRI_AUTH_SERVER, TOPO_PROP_IMMUTABLE, server, 228 &err); 229 } 230 231 if (topo_pgroup_create(node, &sys_pgroup, &err) != 0) 232 return; 233 234 isa[0] = '\0'; 235 (void) sysinfo(SI_ARCHITECTURE, isa, sizeof (isa)); 236 (void) uname(&uts); 237 (void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM, TOPO_PROP_ISA, 238 TOPO_PROP_IMMUTABLE, isa, &err); 239 (void) topo_prop_set_string(node, TOPO_PGROUP_SYSTEM, TOPO_PROP_MACHINE, 240 TOPO_PROP_IMMUTABLE, uts.machine, &err); 241 } 242 243 /*ARGSUSED*/ 244 int 245 hc_enum(topo_mod_t *mod, tnode_t *pnode, const char *name, topo_instance_t min, 246 topo_instance_t max, void *notused1, void *notused2) 247 { 248 nvlist_t *pfmri = NULL; 249 nvlist_t *nvl; 250 nvlist_t *auth; 251 tnode_t *node; 252 int err; 253 /* 254 * Register root node methods 255 */ 256 if (strcmp(name, HC) == 0) { 257 (void) topo_method_register(mod, pnode, hc_methods); 258 return (0); 259 } 260 if (min != max) { 261 topo_mod_dprintf(mod, 262 "Request to enumerate %s component with an " 263 "ambiguous instance number, min (%d) != max (%d).\n", 264 HC, min, max); 265 return (topo_mod_seterrno(mod, EINVAL)); 266 } 267 268 (void) topo_node_resource(pnode, &pfmri, &err); 269 auth = topo_mod_auth(mod, pnode); 270 nvl = hc_fmri_create(mod, pfmri, FM_HC_SCHEME_VERSION, name, min, 271 auth, NULL, NULL, NULL); 272 nvlist_free(pfmri); /* callee ignores NULLs */ 273 if (nvl == NULL) { 274 nvlist_free(auth); 275 return (-1); 276 } 277 278 if ((node = topo_node_bind(mod, pnode, name, min, nvl)) == NULL) { 279 topo_mod_dprintf(mod, "topo_node_bind failed: %s\n", 280 topo_strerror(topo_mod_errno(mod))); 281 nvlist_free(auth); 282 nvlist_free(nvl); 283 return (-1); 284 } 285 286 hc_prop_set(node, auth); 287 nvlist_free(nvl); 288 nvlist_free(auth); 289 290 return (0); 291 } 292 293 /*ARGSUSED*/ 294 static void 295 hc_release(topo_mod_t *mp, tnode_t *node) 296 { 297 topo_method_unregister_all(mp, node); 298 } 299 300 static int 301 fmri_compare(topo_mod_t *mod, nvlist_t *nv1, nvlist_t *nv2) 302 { 303 uint8_t v1, v2; 304 nvlist_t **hcp1, **hcp2; 305 int err, i; 306 uint_t nhcp1, nhcp2; 307 308 if (nvlist_lookup_uint8(nv1, FM_VERSION, &v1) != 0 || 309 nvlist_lookup_uint8(nv2, FM_VERSION, &v2) != 0 || 310 v1 > FM_HC_SCHEME_VERSION || v2 > FM_HC_SCHEME_VERSION) 311 return (topo_mod_seterrno(mod, EMOD_FMRI_VERSION)); 312 313 err = nvlist_lookup_nvlist_array(nv1, FM_FMRI_HC_LIST, &hcp1, &nhcp1); 314 err |= nvlist_lookup_nvlist_array(nv2, FM_FMRI_HC_LIST, &hcp2, &nhcp2); 315 if (err != 0) 316 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 317 318 if (nhcp1 != nhcp2) 319 return (0); 320 321 for (i = 0; i < nhcp1; i++) { 322 char *nm1 = NULL; 323 char *nm2 = NULL; 324 char *id1 = NULL; 325 char *id2 = NULL; 326 327 (void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_NAME, &nm1); 328 (void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_NAME, &nm2); 329 (void) nvlist_lookup_string(hcp1[i], FM_FMRI_HC_ID, &id1); 330 (void) nvlist_lookup_string(hcp2[i], FM_FMRI_HC_ID, &id2); 331 if (nm1 == NULL || nm2 == NULL || id1 == NULL || id2 == NULL) 332 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 333 334 if (strcmp(nm1, nm2) == 0 && strcmp(id1, id2) == 0) 335 continue; 336 337 return (0); 338 } 339 340 return (1); 341 } 342 343 /*ARGSUSED*/ 344 static int 345 hc_compare(topo_mod_t *mod, tnode_t *node, topo_version_t version, 346 nvlist_t *in, nvlist_t **out) 347 { 348 int ret; 349 uint32_t compare; 350 nvlist_t *nv1, *nv2; 351 352 if (version > TOPO_METH_COMPARE_VERSION) 353 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 354 355 if (nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NV1, &nv1) != 0 || 356 nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NV1, &nv2) != 0) 357 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 358 359 ret = fmri_compare(mod, nv1, nv2); 360 if (ret < 0) 361 return (-1); 362 363 compare = ret; 364 if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) == 0) { 365 if (nvlist_add_uint32(*out, TOPO_METH_COMPARE_RET, 366 compare) == 0) 367 return (0); 368 else 369 nvlist_free(*out); 370 } 371 372 return (-1); 373 } 374 375 static ssize_t 376 fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen) 377 { 378 nvlist_t **hcprs = NULL; 379 nvlist_t *anvl = NULL; 380 uint8_t version; 381 ssize_t size = 0; 382 uint_t hcnprs; 383 char *achas = NULL; 384 char *adom = NULL; 385 char *aprod = NULL; 386 char *asrvr = NULL; 387 char *ahost = NULL; 388 char *serial = NULL; 389 char *part = NULL; 390 char *root = NULL; 391 char *rev = NULL; 392 int more_auth = 0; 393 int err, i; 394 395 if (nvlist_lookup_uint8(nvl, FM_VERSION, &version) != 0 || 396 version > FM_HC_SCHEME_VERSION) 397 return (-1); 398 399 /* Get authority, if present */ 400 err = nvlist_lookup_nvlist(nvl, FM_FMRI_AUTHORITY, &anvl); 401 if (err != 0 && err != ENOENT) 402 return (-1); 403 404 if ((err = nvlist_lookup_string(nvl, FM_FMRI_HC_ROOT, &root)) != 0) 405 return (-1); 406 407 err = nvlist_lookup_nvlist_array(nvl, FM_FMRI_HC_LIST, &hcprs, &hcnprs); 408 if (err != 0 || hcprs == NULL) 409 return (-1); 410 411 if (anvl != NULL) { 412 (void) nvlist_lookup_string(anvl, 413 FM_FMRI_AUTH_PRODUCT, &aprod); 414 (void) nvlist_lookup_string(anvl, 415 FM_FMRI_AUTH_CHASSIS, &achas); 416 (void) nvlist_lookup_string(anvl, 417 FM_FMRI_AUTH_DOMAIN, &adom); 418 (void) nvlist_lookup_string(anvl, 419 FM_FMRI_AUTH_SERVER, &asrvr); 420 (void) nvlist_lookup_string(anvl, 421 FM_FMRI_AUTH_HOST, &ahost); 422 if (aprod != NULL) 423 more_auth++; 424 if (achas != NULL) 425 more_auth++; 426 if (adom != NULL) 427 more_auth++; 428 if (asrvr != NULL) 429 more_auth++; 430 if (ahost != NULL) 431 more_auth++; 432 } 433 434 (void) nvlist_lookup_string(nvl, FM_FMRI_HC_SERIAL_ID, &serial); 435 (void) nvlist_lookup_string(nvl, FM_FMRI_HC_PART, &part); 436 (void) nvlist_lookup_string(nvl, FM_FMRI_HC_REVISION, &rev); 437 438 /* hc:// */ 439 topo_fmristr_build(&size, buf, buflen, FM_FMRI_SCHEME_HC, NULL, "://"); 440 441 /* authority, if any */ 442 if (aprod != NULL) 443 topo_fmristr_build(&size, 444 buf, buflen, aprod, ":" FM_FMRI_AUTH_PRODUCT "=", NULL); 445 if (achas != NULL) 446 topo_fmristr_build(&size, 447 buf, buflen, achas, ":" FM_FMRI_AUTH_CHASSIS "=", NULL); 448 if (adom != NULL) 449 topo_fmristr_build(&size, 450 buf, buflen, adom, ":" FM_FMRI_AUTH_DOMAIN "=", NULL); 451 if (asrvr != NULL) 452 topo_fmristr_build(&size, 453 buf, buflen, asrvr, ":" FM_FMRI_AUTH_SERVER "=", NULL); 454 if (ahost != NULL) 455 topo_fmristr_build(&size, 456 buf, buflen, ahost, ":" FM_FMRI_AUTH_HOST "=", NULL); 457 458 /* hardware-id part */ 459 topo_fmristr_build(&size, 460 buf, buflen, serial, ":" FM_FMRI_HC_SERIAL_ID "=", NULL); 461 topo_fmristr_build(&size, 462 buf, buflen, part, ":" FM_FMRI_HC_PART "=", NULL); 463 topo_fmristr_build(&size, 464 buf, buflen, rev, ":" FM_FMRI_HC_REVISION "=", NULL); 465 466 /* separating slash */ 467 topo_fmristr_build(&size, buf, buflen, "/", NULL, NULL); 468 469 /* hc-root */ 470 topo_fmristr_build(&size, buf, buflen, root, NULL, NULL); 471 472 /* all the pairs */ 473 for (i = 0; i < hcnprs; i++) { 474 char *nm = NULL; 475 char *id = NULL; 476 477 if (i > 0) 478 topo_fmristr_build(&size, 479 buf, buflen, "/", NULL, NULL); 480 (void) nvlist_lookup_string(hcprs[i], FM_FMRI_HC_NAME, &nm); 481 (void) nvlist_lookup_string(hcprs[i], FM_FMRI_HC_ID, &id); 482 if (nm == NULL || id == NULL) 483 return (0); 484 topo_fmristr_build(&size, buf, buflen, nm, NULL, "="); 485 topo_fmristr_build(&size, buf, buflen, id, NULL, NULL); 486 } 487 488 return (size); 489 } 490 491 /*ARGSUSED*/ 492 static int 493 hc_fmri_nvl2str(topo_mod_t *mod, tnode_t *node, topo_version_t version, 494 nvlist_t *nvl, nvlist_t **out) 495 { 496 ssize_t len; 497 char *name = NULL; 498 nvlist_t *fmristr; 499 500 if (version > TOPO_METH_NVL2STR_VERSION) 501 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 502 503 if ((len = fmri_nvl2str(nvl, NULL, 0)) == 0 || 504 (name = topo_mod_alloc(mod, len + 1)) == NULL || 505 fmri_nvl2str(nvl, name, len + 1) == 0) { 506 if (name != NULL) 507 topo_mod_free(mod, name, len + 1); 508 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 509 } 510 511 if (topo_mod_nvalloc(mod, &fmristr, NV_UNIQUE_NAME) != 0) 512 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 513 if (nvlist_add_string(fmristr, "fmri-string", name) != 0) { 514 topo_mod_free(mod, name, len + 1); 515 nvlist_free(fmristr); 516 return (topo_mod_seterrno(mod, EMOD_FMRI_NVL)); 517 } 518 topo_mod_free(mod, name, len + 1); 519 *out = fmristr; 520 521 return (0); 522 } 523 524 static nvlist_t * 525 hc_base_fmri_create(topo_mod_t *mod, const nvlist_t *auth, const char *part, 526 const char *rev, const char *serial) 527 { 528 nvlist_t *fmri; 529 int err = 0; 530 531 /* 532 * Create base HC nvlist 533 */ 534 if (topo_mod_nvalloc(mod, &fmri, NV_UNIQUE_NAME) != 0) 535 return (NULL); 536 537 err = nvlist_add_uint8(fmri, FM_VERSION, FM_HC_SCHEME_VERSION); 538 err |= nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC); 539 err |= nvlist_add_string(fmri, FM_FMRI_HC_ROOT, ""); 540 if (err != 0) { 541 nvlist_free(fmri); 542 return (NULL); 543 } 544 545 /* 546 * Add optional payload members 547 */ 548 if (serial != NULL) 549 (void) nvlist_add_string(fmri, FM_FMRI_HC_SERIAL_ID, serial); 550 if (part != NULL) 551 (void) nvlist_add_string(fmri, FM_FMRI_HC_PART, part); 552 if (rev != NULL) 553 (void) nvlist_add_string(fmri, FM_FMRI_HC_REVISION, rev); 554 if (auth != NULL) 555 (void) nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY, 556 (nvlist_t *)auth); 557 558 return (fmri); 559 } 560 561 static nvlist_t ** 562 make_hc_pairs(topo_mod_t *mod, char *fmri, int *num) 563 { 564 nvlist_t **pa; 565 char *hc, *fromstr; 566 char *starti, *startn, *endi, *endi2; 567 char *ne, *ns; 568 char *cname = NULL; 569 char *find; 570 char *cid = NULL; 571 int nslashes = 0; 572 int npairs = 0; 573 int i; 574 575 if ((hc = topo_mod_strdup(mod, fmri + 5)) == NULL) 576 return (NULL); 577 578 /* 579 * Count equal signs and slashes to determine how many 580 * hc-pairs will be present in the final FMRI. There should 581 * be at least as many slashes as equal signs. There can be 582 * more, though if the string after an = includes them. 583 */ 584 if ((fromstr = strchr(hc, '/')) == NULL) 585 return (NULL); 586 587 find = fromstr; 588 while ((ne = strchr(find, '=')) != NULL) { 589 find = ne + 1; 590 npairs++; 591 } 592 593 find = fromstr; 594 while ((ns = strchr(find, '/')) != NULL) { 595 find = ns + 1; 596 nslashes++; 597 } 598 599 /* 600 * Do we appear to have a well-formed string version of the FMRI? 601 */ 602 if (nslashes < npairs || npairs == 0) { 603 topo_mod_strfree(mod, hc); 604 return (NULL); 605 } 606 607 *num = npairs; 608 609 find = fromstr; 610 611 if ((pa = topo_mod_alloc(mod, npairs * sizeof (nvlist_t *))) == NULL) { 612 topo_mod_strfree(mod, hc); 613 return (NULL); 614 } 615 616 /* 617 * We go through a pretty complicated procedure to find the 618 * name and id for each pair. That's because, unfortunately, 619 * we have some ids that can have slashes within them. So 620 * we can't just search for the next slash after the equal sign 621 * and decide that starts a new pair. Instead we have to find 622 * an equal sign for the next pair and work our way back to the 623 * slash from there. 624 */ 625 for (i = 0; i < npairs; i++) { 626 pa[i] = NULL; 627 startn = strchr(find, '/'); 628 if (startn == NULL) 629 break; 630 startn++; 631 starti = strchr(find, '='); 632 if (starti == NULL) 633 break; 634 *starti = '\0'; 635 if ((cname = topo_mod_strdup(mod, startn)) == NULL) 636 break; 637 *starti++ = '='; 638 endi = strchr(starti, '='); 639 if (endi != NULL) { 640 *endi = '\0'; 641 endi2 = strrchr(starti, '/'); 642 if (endi2 == NULL) 643 break; 644 *endi = '='; 645 *endi2 = '\0'; 646 if ((cid = topo_mod_strdup(mod, starti)) == NULL) 647 break; 648 *endi2 = '/'; 649 find = endi2; 650 } else { 651 if ((cid = topo_mod_strdup(mod, starti)) == NULL) 652 break; 653 find = starti + strlen(starti); 654 } 655 if (topo_mod_nvalloc(mod, &pa[i], NV_UNIQUE_NAME) < 0) 656 break; 657 658 if (nvlist_add_string(pa[i], FM_FMRI_HC_NAME, cname) || 659 nvlist_add_string(pa[i], FM_FMRI_HC_ID, cid)) 660 break; 661 662 topo_mod_strfree(mod, cname); 663 topo_mod_strfree(mod, cid); 664 cname = NULL; 665 cid = NULL; 666 } 667 668 topo_mod_strfree(mod, cname); 669 topo_mod_strfree(mod, cid); 670 671 if (i < npairs) { 672 while (i >= 0) 673 nvlist_free(pa[i + 1]); 674 topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *)); 675 topo_mod_strfree(mod, hc); 676 return (NULL); 677 } 678 679 topo_mod_strfree(mod, hc); 680 681 return (pa); 682 } 683 684 void 685 make_hc_auth(topo_mod_t *mod, char *fmri, char **serial, char **part, 686 char **rev, nvlist_t **auth) 687 { 688 char *starti, *startn, *endi, *copy; 689 char *aname, *aid, *fs; 690 nvlist_t *na = NULL; 691 size_t len; 692 693 if ((copy = topo_mod_strdup(mod, fmri + 5)) == NULL) 694 return; 695 696 len = strlen(copy); 697 698 /* 699 * Make sure there are a valid authority members 700 */ 701 startn = strchr(copy, ':'); 702 fs = strchr(copy, '/'); 703 704 if (startn == NULL || fs == NULL) { 705 topo_mod_strfree(mod, copy); 706 return; 707 } 708 709 /* 710 * The first colon we encounter must occur before the 711 * first slash 712 */ 713 if (startn > fs) 714 return; 715 716 do { 717 if (++startn >= copy + len) 718 break; 719 720 if ((starti = strchr(startn, '=')) == NULL) 721 break; 722 723 *starti = '\0'; 724 if (++starti > copy + len) 725 break; 726 727 if ((aname = topo_mod_strdup(mod, startn)) == NULL) 728 break; 729 730 startn = endi = strchr(starti, ':'); 731 if (endi == NULL) 732 if ((endi = strchr(starti, '/')) == NULL) 733 break; 734 735 *endi = '\0'; 736 if ((aid = topo_mod_strdup(mod, starti)) == NULL) { 737 topo_mod_strfree(mod, aname); 738 break; 739 } 740 741 /* 742 * Return possible serial, part and revision 743 */ 744 if (strcmp(aname, FM_FMRI_HC_SERIAL_ID) == 0) { 745 *serial = topo_mod_strdup(mod, aid); 746 } else if (strcmp(aname, FM_FMRI_HC_PART) == 0) { 747 *part = topo_mod_strdup(mod, aid); 748 } else if (strcmp(aname, FM_FMRI_HC_REVISION) == 0) { 749 *rev = topo_mod_strdup(mod, aid); 750 } else { 751 if (na == NULL) { 752 if (topo_mod_nvalloc(mod, &na, 753 NV_UNIQUE_NAME) == 0) { 754 (void) nvlist_add_string(na, aname, 755 aid); 756 } 757 } else { 758 (void) nvlist_add_string(na, aname, aid); 759 } 760 } 761 topo_mod_strfree(mod, aname); 762 topo_mod_strfree(mod, aid); 763 764 } while (startn != NULL); 765 766 *auth = na; 767 768 topo_mod_free(mod, copy, len + 1); 769 } 770 771 /*ARGSUSED*/ 772 static int 773 hc_fmri_str2nvl(topo_mod_t *mod, tnode_t *node, topo_version_t version, 774 nvlist_t *in, nvlist_t **out) 775 { 776 nvlist_t **pa = NULL; 777 nvlist_t *nf = NULL; 778 nvlist_t *auth = NULL; 779 char *str; 780 char *serial = NULL, *part = NULL, *rev = NULL; 781 int npairs; 782 int i, e; 783 784 if (version > TOPO_METH_STR2NVL_VERSION) 785 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 786 787 if (nvlist_lookup_string(in, "fmri-string", &str) != 0) 788 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 789 790 /* We're expecting a string version of an hc scheme FMRI */ 791 if (strncmp(str, "hc://", 5) != 0) 792 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM)); 793 794 if ((pa = make_hc_pairs(mod, str, &npairs)) == NULL) 795 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM)); 796 797 make_hc_auth(mod, str, &serial, &part, &rev, &auth); 798 if ((nf = hc_base_fmri_create(mod, auth, part, rev, serial)) == NULL) 799 goto hcfmbail; 800 if ((e = nvlist_add_uint32(nf, FM_FMRI_HC_LIST_SZ, npairs)) == 0) 801 e = nvlist_add_nvlist_array(nf, FM_FMRI_HC_LIST, pa, npairs); 802 if (e != 0) { 803 topo_mod_dprintf(mod, "construction of new hc nvl failed"); 804 goto hcfmbail; 805 } 806 for (i = 0; i < npairs; i++) 807 nvlist_free(pa[i]); 808 topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *)); 809 if (serial != NULL) 810 topo_mod_strfree(mod, serial); 811 if (part != NULL) 812 topo_mod_strfree(mod, part); 813 if (rev != NULL) 814 topo_mod_strfree(mod, rev); 815 nvlist_free(auth); 816 817 *out = nf; 818 819 return (0); 820 821 hcfmbail: 822 if (nf != NULL) 823 nvlist_free(nf); 824 for (i = 0; i < npairs; i++) 825 nvlist_free(pa[i]); 826 topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *)); 827 if (serial != NULL) 828 topo_mod_strfree(mod, serial); 829 if (part != NULL) 830 topo_mod_strfree(mod, part); 831 if (rev != NULL) 832 topo_mod_strfree(mod, rev); 833 nvlist_free(auth); 834 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM)); 835 } 836 837 static nvlist_t * 838 hc_list_create(topo_mod_t *mod, const char *name, char *inst) 839 { 840 int err; 841 nvlist_t *hc; 842 843 if (topo_mod_nvalloc(mod, &hc, NV_UNIQUE_NAME) != 0) 844 return (NULL); 845 846 err = nvlist_add_string(hc, FM_FMRI_HC_NAME, name); 847 err |= nvlist_add_string(hc, FM_FMRI_HC_ID, inst); 848 if (err != 0) { 849 nvlist_free(hc); 850 return (NULL); 851 } 852 853 return (hc); 854 } 855 856 static nvlist_t * 857 hc_create_seterror(topo_mod_t *mod, nvlist_t **hcl, int n, nvlist_t *fmri, 858 int err) 859 { 860 int i; 861 862 if (hcl != NULL) { 863 for (i = 0; i < n + 1; ++i) 864 nvlist_free(hcl[i]); 865 866 topo_mod_free(mod, hcl, sizeof (nvlist_t *) * (n + 1)); 867 } 868 869 nvlist_free(fmri); 870 871 (void) topo_mod_seterrno(mod, err); 872 873 topo_mod_dprintf(mod, "unable to create hc FMRI: %s\n", 874 topo_mod_errmsg(mod)); 875 876 return (NULL); 877 } 878 879 static int 880 hc_name_canonical(topo_mod_t *mod, const char *name) 881 { 882 int i; 883 884 if (getenv("NOHCCHECK") != NULL) 885 return (1); 886 887 /* 888 * Only enumerate elements with correct canonical names 889 */ 890 for (i = 0; i < hc_ncanon; i++) { 891 if (strcmp(name, hc_canon[i].hcc_name) == 0) 892 break; 893 } 894 if (i >= hc_ncanon) { 895 topo_mod_dprintf(mod, "non-canonical name %s\n", 896 name); 897 return (0); 898 } else { 899 return (1); 900 } 901 } 902 903 static nvlist_t * 904 hc_fmri_create(topo_mod_t *mod, nvlist_t *pfmri, int version, const char *name, 905 topo_instance_t inst, const nvlist_t *auth, const char *part, 906 const char *rev, const char *serial) 907 { 908 int i; 909 char str[21]; /* sizeof (UINT64_MAX) + '\0' */ 910 uint_t pelems = 0; 911 nvlist_t **phcl = NULL; 912 nvlist_t **hcl = NULL; 913 nvlist_t *fmri = NULL; 914 915 if (version > FM_HC_SCHEME_VERSION) 916 return (hc_create_seterror(mod, 917 hcl, pelems, fmri, EMOD_VER_OLD)); 918 else if (version < FM_HC_SCHEME_VERSION) 919 return (hc_create_seterror(mod, 920 hcl, pelems, fmri, EMOD_VER_NEW)); 921 922 /* 923 * Check that the requested name is in our canonical list 924 */ 925 if (hc_name_canonical(mod, name) == 0) 926 return (hc_create_seterror(mod, 927 hcl, pelems, fmri, EMOD_NONCANON)); 928 /* 929 * Copy the parent's HC_LIST 930 */ 931 if (pfmri != NULL) { 932 if (nvlist_lookup_nvlist_array(pfmri, FM_FMRI_HC_LIST, 933 &phcl, &pelems) != 0) 934 return (hc_create_seterror(mod, 935 hcl, pelems, fmri, EMOD_FMRI_MALFORM)); 936 } 937 938 hcl = topo_mod_zalloc(mod, sizeof (nvlist_t *) * (pelems + 1)); 939 if (hcl == NULL) 940 return (hc_create_seterror(mod, hcl, pelems, fmri, 941 EMOD_NOMEM)); 942 943 for (i = 0; i < pelems; ++i) 944 if (topo_mod_nvdup(mod, phcl[i], &hcl[i]) != 0) 945 return (hc_create_seterror(mod, 946 hcl, pelems, fmri, EMOD_FMRI_NVL)); 947 948 (void) snprintf(str, sizeof (str), "%d", inst); 949 if ((hcl[i] = hc_list_create(mod, name, str)) == NULL) 950 return (hc_create_seterror(mod, 951 hcl, pelems, fmri, EMOD_FMRI_NVL)); 952 953 if ((fmri = hc_base_fmri_create(mod, auth, part, rev, serial)) == NULL) 954 return (hc_create_seterror(mod, 955 hcl, pelems, fmri, EMOD_FMRI_NVL)); 956 957 if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, hcl, pelems + 1) 958 != 0) 959 return (hc_create_seterror(mod, 960 hcl, pelems, fmri, EMOD_FMRI_NVL)); 961 962 if (hcl != NULL) { 963 for (i = 0; i < pelems + 1; ++i) { 964 if (hcl[i] != NULL) 965 nvlist_free(hcl[i]); 966 } 967 topo_mod_free(mod, hcl, sizeof (nvlist_t *) * (pelems + 1)); 968 } 969 970 return (fmri); 971 } 972 973 /*ARGSUSED*/ 974 static int 975 hc_fmri_create_meth(topo_mod_t *mod, tnode_t *node, topo_version_t version, 976 nvlist_t *in, nvlist_t **out) 977 { 978 int ret; 979 nvlist_t *args, *pfmri = NULL; 980 nvlist_t *auth; 981 uint32_t inst; 982 char *name, *serial, *rev, *part; 983 984 if (version > TOPO_METH_FMRI_VERSION) 985 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 986 987 /* First the must-have fields */ 988 if (nvlist_lookup_string(in, TOPO_METH_FMRI_ARG_NAME, &name) != 0) 989 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 990 if (nvlist_lookup_uint32(in, TOPO_METH_FMRI_ARG_INST, &inst) != 0) 991 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 992 993 /* 994 * args is optional 995 */ 996 pfmri = NULL; 997 auth = NULL; 998 serial = rev = part = NULL; 999 if ((ret = nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args)) 1000 != 0) { 1001 if (ret != ENOENT) 1002 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1003 } else { 1004 1005 /* And then optional arguments */ 1006 (void) nvlist_lookup_nvlist(args, TOPO_METH_FMRI_ARG_PARENT, 1007 &pfmri); 1008 (void) nvlist_lookup_nvlist(args, TOPO_METH_FMRI_ARG_AUTH, 1009 &auth); 1010 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_PART, 1011 &part); 1012 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_REV, &rev); 1013 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_SER, 1014 &serial); 1015 } 1016 1017 *out = hc_fmri_create(mod, pfmri, version, name, inst, auth, part, 1018 rev, serial); 1019 if (*out == NULL) 1020 return (-1); 1021 return (0); 1022 } 1023 1024 struct hc_walk { 1025 topo_mod_walk_cb_t hcw_cb; 1026 void *hcw_priv; 1027 topo_walk_t *hcw_wp; 1028 nvlist_t **hcw_list; 1029 uint_t hcw_index; 1030 uint_t hcw_end; 1031 }; 1032 1033 /* 1034 * Generic walker for the hc-scheme topo tree. This function uses the 1035 * hierachical nature of the hc-scheme to step through efficiently through 1036 * the topo hc tree. Node lookups are done by topo_walk_byid() at each 1037 * component level to avoid unnecessary traversal of the tree. 1038 * 1039 */ 1040 static int 1041 hc_walker(topo_mod_t *mod, tnode_t *node, void *pdata) 1042 { 1043 int i, err; 1044 struct hc_walk *hwp = (struct hc_walk *)pdata; 1045 char *name, *id; 1046 topo_instance_t inst; 1047 1048 i = hwp->hcw_index; 1049 if (i > hwp->hcw_end) { 1050 (void) topo_mod_seterrno(mod, ETOPO_PROP_NOENT); 1051 return (TOPO_WALK_TERMINATE); 1052 } 1053 1054 err = nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_NAME, &name); 1055 err |= nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_ID, &id); 1056 1057 if (err != 0) { 1058 (void) topo_mod_seterrno(mod, EMOD_NVL_INVAL); 1059 return (TOPO_WALK_ERR); 1060 } 1061 1062 inst = atoi(id); 1063 topo_mod_dprintf(mod, "hc_walker: walking node:%s=%d for hc:" 1064 "%s=%d at %d, end at %d \n", topo_node_name(node), 1065 topo_node_instance(node), name, inst, i, hwp->hcw_end); 1066 if (i == hwp->hcw_end) { 1067 /* 1068 * We are at the end of the hc-list. Verify that 1069 * the last node contains the name/instance we are looking for. 1070 */ 1071 if (strcmp(topo_node_name(node), name) == 0 && 1072 inst == topo_node_instance(node)) { 1073 if ((err = hwp->hcw_cb(mod, node, hwp->hcw_priv)) 1074 != 0) { 1075 (void) topo_mod_seterrno(mod, err); 1076 topo_mod_dprintf(mod, "hc_walker: callback " 1077 "failed: %s\n ", topo_mod_errmsg(mod)); 1078 return (TOPO_WALK_ERR); 1079 } 1080 topo_mod_dprintf(mod, "hc_walker: callback " 1081 "complete: terminate walk\n"); 1082 return (TOPO_WALK_TERMINATE); 1083 } else { 1084 topo_mod_dprintf(mod, "hc_walker: %s=%d\n " 1085 "not found\n", name, inst); 1086 return (TOPO_WALK_TERMINATE); 1087 } 1088 } 1089 1090 hwp->hcw_index = ++i; 1091 err = nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_NAME, &name); 1092 err |= nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_ID, &id); 1093 if (err != 0) { 1094 (void) topo_mod_seterrno(mod, err); 1095 return (TOPO_WALK_ERR); 1096 } 1097 inst = atoi(id); 1098 1099 topo_mod_dprintf(mod, "hc_walker: walk byid of %s=%d \n", name, 1100 inst); 1101 return (topo_walk_byid(hwp->hcw_wp, name, inst)); 1102 1103 } 1104 1105 static struct hc_walk * 1106 hc_walk_init(topo_mod_t *mod, tnode_t *node, nvlist_t *rsrc, 1107 topo_mod_walk_cb_t cb, void *pdata) 1108 { 1109 int err; 1110 uint_t sz; 1111 struct hc_walk *hwp; 1112 topo_walk_t *wp; 1113 1114 if ((hwp = topo_mod_alloc(mod, sizeof (struct hc_walk))) == NULL) 1115 (void) topo_mod_seterrno(mod, EMOD_NOMEM); 1116 1117 if (nvlist_lookup_nvlist_array(rsrc, FM_FMRI_HC_LIST, &hwp->hcw_list, 1118 &sz) != 0) { 1119 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1120 (void) topo_mod_seterrno(mod, EMOD_METHOD_INVAL); 1121 return (NULL); 1122 } 1123 1124 hwp->hcw_end = sz - 1; 1125 hwp->hcw_index = 0; 1126 hwp->hcw_priv = pdata; 1127 hwp->hcw_cb = cb; 1128 if ((wp = topo_mod_walk_init(mod, node, hc_walker, (void *)hwp, &err)) 1129 == NULL) { 1130 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1131 (void) topo_mod_seterrno(mod, err); 1132 return (NULL); 1133 } 1134 1135 hwp->hcw_wp = wp; 1136 1137 return (hwp); 1138 } 1139 1140 struct prop_lookup { 1141 const char *pl_pgroup; 1142 const char *pl_pname; 1143 int pl_flag; 1144 nvlist_t *pl_args; 1145 nvlist_t *pl_rsrc; 1146 nvlist_t *pl_prop; 1147 }; 1148 1149 /*ARGSUSED*/ 1150 static int 1151 hc_prop_get(topo_mod_t *mod, tnode_t *node, void *pdata) 1152 { 1153 int err = 0; 1154 1155 struct prop_lookup *plp = (struct prop_lookup *)pdata; 1156 1157 (void) topo_prop_getprop(node, plp->pl_pgroup, plp->pl_pname, 1158 plp->pl_args, &plp->pl_prop, &err); 1159 1160 return (err); 1161 } 1162 1163 static int 1164 hc_fmri_prop_get(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1165 nvlist_t *in, nvlist_t **out) 1166 { 1167 int err; 1168 struct hc_walk *hwp; 1169 struct prop_lookup *plp; 1170 1171 if (version > TOPO_METH_PROP_GET_VERSION) 1172 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1173 1174 if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL) 1175 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1176 1177 err = nvlist_lookup_string(in, TOPO_PROP_GROUP, 1178 (char **)&plp->pl_pgroup); 1179 err |= nvlist_lookup_string(in, TOPO_PROP_VAL_NAME, 1180 (char **)&plp->pl_pname); 1181 err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc); 1182 if (err != 0) { 1183 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1184 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1185 } 1186 1187 /* 1188 * Private args to prop method are optional 1189 */ 1190 if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &plp->pl_args)) 1191 != 0) { 1192 if (err != ENOENT) { 1193 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1194 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1195 } else { 1196 plp->pl_args = NULL; 1197 } 1198 } 1199 1200 plp->pl_prop = NULL; 1201 if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_prop_get, 1202 (void *)plp)) != NULL) { 1203 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1204 TOPO_WALK_ERR) 1205 err = -1; 1206 else 1207 err = 0; 1208 topo_walk_fini(hwp->hcw_wp); 1209 } else { 1210 err = -1; 1211 } 1212 1213 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1214 1215 if (plp->pl_prop != NULL) 1216 *out = plp->pl_prop; 1217 1218 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1219 1220 return (err); 1221 } 1222 1223 /*ARGSUSED*/ 1224 static int 1225 hc_pgrp_get(topo_mod_t *mod, tnode_t *node, void *pdata) 1226 { 1227 int err = 0; 1228 1229 struct prop_lookup *plp = (struct prop_lookup *)pdata; 1230 1231 (void) topo_prop_getpgrp(node, plp->pl_pgroup, &plp->pl_prop, &err); 1232 1233 return (err); 1234 } 1235 1236 static int 1237 hc_fmri_pgrp_get(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1238 nvlist_t *in, nvlist_t **out) 1239 { 1240 int err; 1241 struct hc_walk *hwp; 1242 struct prop_lookup *plp; 1243 1244 if (version > TOPO_METH_PGRP_GET_VERSION) 1245 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1246 1247 if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL) 1248 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1249 1250 err = nvlist_lookup_string(in, TOPO_PROP_GROUP, 1251 (char **)&plp->pl_pgroup); 1252 err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc); 1253 if (err != 0) { 1254 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1255 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1256 } 1257 1258 plp->pl_prop = NULL; 1259 if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_pgrp_get, 1260 (void *)plp)) != NULL) { 1261 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1262 TOPO_WALK_ERR) 1263 err = -1; 1264 else 1265 err = 0; 1266 topo_walk_fini(hwp->hcw_wp); 1267 } else { 1268 err = -1; 1269 } 1270 1271 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1272 1273 if (plp->pl_prop != NULL) 1274 *out = plp->pl_prop; 1275 1276 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1277 1278 return (err); 1279 } 1280 1281 /*ARGSUSED*/ 1282 static int 1283 hc_prop_setprop(topo_mod_t *mod, tnode_t *node, void *pdata) 1284 { 1285 int err = 0; 1286 1287 struct prop_lookup *plp = (struct prop_lookup *)pdata; 1288 1289 (void) topo_prop_setprop(node, plp->pl_pgroup, plp->pl_prop, 1290 plp->pl_flag, plp->pl_args, &err); 1291 1292 return (err); 1293 } 1294 1295 /*ARGSUSED*/ 1296 static int 1297 hc_fmri_prop_set(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1298 nvlist_t *in, nvlist_t **out) 1299 { 1300 int err; 1301 struct hc_walk *hwp; 1302 struct prop_lookup *plp; 1303 1304 if (version > TOPO_METH_PROP_SET_VERSION) 1305 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1306 1307 if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL) 1308 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1309 1310 err = nvlist_lookup_string(in, TOPO_PROP_GROUP, 1311 (char **)&plp->pl_pgroup); 1312 err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc); 1313 err |= nvlist_lookup_nvlist(in, TOPO_PROP_VAL, &plp->pl_prop); 1314 err |= nvlist_lookup_int32(in, TOPO_PROP_FLAG, &plp->pl_flag); 1315 if (err != 0) { 1316 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1317 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1318 } 1319 1320 /* 1321 * Private args to prop method are optional 1322 */ 1323 if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &plp->pl_args)) 1324 != 0) { 1325 if (err != ENOENT) 1326 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1327 else 1328 plp->pl_args = NULL; 1329 } 1330 1331 if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_prop_setprop, 1332 (void *)plp)) != NULL) { 1333 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1334 TOPO_WALK_ERR) 1335 err = -1; 1336 else 1337 err = 0; 1338 topo_walk_fini(hwp->hcw_wp); 1339 } else { 1340 err = -1; 1341 } 1342 1343 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1344 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1345 1346 return (err); 1347 } 1348 1349 struct hc_args { 1350 nvlist_t *ha_fmri; 1351 nvlist_t *ha_nvl; 1352 }; 1353 1354 static int 1355 hc_is_present(topo_mod_t *mod, tnode_t *node, void *pdata) 1356 { 1357 int err; 1358 struct hc_args *hap = (struct hc_args *)pdata; 1359 1360 /* 1361 * check with the enumerator that created this FMRI 1362 * (topo node) 1363 */ 1364 if (topo_method_invoke(node, TOPO_METH_PRESENT, 1365 TOPO_METH_PRESENT_VERSION, hap->ha_fmri, &hap->ha_nvl, 1366 &err) < 0) { 1367 1368 /* 1369 * Err on the side of caution and return present 1370 */ 1371 if (topo_mod_nvalloc(mod, &hap->ha_nvl, NV_UNIQUE_NAME) == 0) 1372 if (nvlist_add_uint32(hap->ha_nvl, 1373 TOPO_METH_PRESENT_RET, 1) == 0) 1374 return (0); 1375 1376 return (ETOPO_PROP_NVL); 1377 } 1378 1379 return (0); 1380 } 1381 1382 static int 1383 hc_fmri_present(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1384 nvlist_t *in, nvlist_t **out) 1385 { 1386 int err; 1387 struct hc_walk *hwp; 1388 struct hc_args *hap; 1389 1390 if (version > TOPO_METH_PRESENT_VERSION) 1391 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1392 1393 if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL) 1394 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1395 1396 hap->ha_fmri = in; 1397 hap->ha_nvl = NULL; 1398 if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_is_present, 1399 (void *)hap)) != NULL) { 1400 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1401 TOPO_WALK_ERR) 1402 err = -1; 1403 else 1404 err = 0; 1405 topo_walk_fini(hwp->hcw_wp); 1406 } else { 1407 err = -1; 1408 } 1409 1410 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1411 1412 if (hap->ha_nvl != NULL) 1413 *out = hap->ha_nvl; 1414 1415 topo_mod_free(mod, hap, sizeof (struct hc_args)); 1416 1417 return (err); 1418 } 1419 1420 static int 1421 hc_unusable(topo_mod_t *mod, tnode_t *node, void *pdata) 1422 { 1423 int err; 1424 struct hc_args *hap = (struct hc_args *)pdata; 1425 1426 /* 1427 * check with the enumerator that created this FMRI 1428 * (topo node) 1429 */ 1430 if (topo_method_invoke(node, TOPO_METH_UNUSABLE, 1431 TOPO_METH_UNUSABLE_VERSION, hap->ha_fmri, &hap->ha_nvl, 1432 &err) < 0) { 1433 1434 /* 1435 * Err on the side of caution and return usable 1436 */ 1437 if (topo_mod_nvalloc(mod, &hap->ha_nvl, NV_UNIQUE_NAME) == 0) 1438 if (nvlist_add_uint32(hap->ha_nvl, 1439 TOPO_METH_UNUSABLE_RET, 0) == 0) 1440 return (0); 1441 1442 return (ETOPO_PROP_NVL); 1443 } 1444 1445 return (err); 1446 } 1447 1448 static int 1449 hc_fmri_unusable(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1450 nvlist_t *in, nvlist_t **out) 1451 { 1452 int err; 1453 struct hc_walk *hwp; 1454 struct hc_args *hap; 1455 1456 if (version > TOPO_METH_UNUSABLE_VERSION) 1457 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1458 1459 if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL) 1460 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1461 1462 hap->ha_fmri = in; 1463 hap->ha_nvl = NULL; 1464 if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_unusable, 1465 (void *)hap)) != NULL) { 1466 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1467 TOPO_WALK_ERR) 1468 err = -1; 1469 else 1470 err = 0; 1471 topo_walk_fini(hwp->hcw_wp); 1472 } else { 1473 err = -1; 1474 } 1475 1476 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1477 1478 if (hap->ha_nvl != NULL) 1479 *out = hap->ha_nvl; 1480 1481 topo_mod_free(mod, hap, sizeof (struct hc_args)); 1482 1483 return (err); 1484 } 1485