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, aid); 755 } 756 } else { 757 (void) nvlist_add_string(na, aname, aid); 758 } 759 } 760 topo_mod_strfree(mod, aname); 761 topo_mod_strfree(mod, aid); 762 763 } while (startn != NULL); 764 765 *auth = na; 766 767 topo_mod_free(mod, copy, len + 1); 768 } 769 770 /*ARGSUSED*/ 771 static int 772 hc_fmri_str2nvl(topo_mod_t *mod, tnode_t *node, topo_version_t version, 773 nvlist_t *in, nvlist_t **out) 774 { 775 nvlist_t **pa = NULL; 776 nvlist_t *nf = NULL; 777 nvlist_t *auth = NULL; 778 char *str; 779 char *serial = NULL, *part = NULL, *rev = NULL; 780 int npairs; 781 int i, e; 782 783 if (version > TOPO_METH_STR2NVL_VERSION) 784 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 785 786 if (nvlist_lookup_string(in, "fmri-string", &str) != 0) 787 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 788 789 /* We're expecting a string version of an hc scheme FMRI */ 790 if (strncmp(str, "hc://", 5) != 0) 791 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM)); 792 793 if ((pa = make_hc_pairs(mod, str, &npairs)) == NULL) 794 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM)); 795 796 make_hc_auth(mod, str, &serial, &part, &rev, &auth); 797 if ((nf = hc_base_fmri_create(mod, auth, part, rev, serial)) == NULL) 798 goto hcfmbail; 799 if ((e = nvlist_add_uint32(nf, FM_FMRI_HC_LIST_SZ, npairs)) == 0) 800 e = nvlist_add_nvlist_array(nf, FM_FMRI_HC_LIST, pa, npairs); 801 if (e != 0) { 802 topo_mod_dprintf(mod, "construction of new hc nvl failed"); 803 goto hcfmbail; 804 } 805 for (i = 0; i < npairs; i++) 806 nvlist_free(pa[i]); 807 topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *)); 808 if (serial != NULL) 809 topo_mod_strfree(mod, serial); 810 if (part != NULL) 811 topo_mod_strfree(mod, part); 812 if (rev != NULL) 813 topo_mod_strfree(mod, rev); 814 nvlist_free(auth); 815 816 *out = nf; 817 818 return (0); 819 820 hcfmbail: 821 if (nf != NULL) 822 nvlist_free(nf); 823 for (i = 0; i < npairs; i++) 824 nvlist_free(pa[i]); 825 topo_mod_free(mod, pa, npairs * sizeof (nvlist_t *)); 826 if (serial != NULL) 827 topo_mod_strfree(mod, serial); 828 if (part != NULL) 829 topo_mod_strfree(mod, part); 830 if (rev != NULL) 831 topo_mod_strfree(mod, rev); 832 nvlist_free(auth); 833 return (topo_mod_seterrno(mod, EMOD_FMRI_MALFORM)); 834 } 835 836 static nvlist_t * 837 hc_list_create(topo_mod_t *mod, const char *name, char *inst) 838 { 839 int err; 840 nvlist_t *hc; 841 842 if (topo_mod_nvalloc(mod, &hc, NV_UNIQUE_NAME) != 0) 843 return (NULL); 844 845 err = nvlist_add_string(hc, FM_FMRI_HC_NAME, name); 846 err |= nvlist_add_string(hc, FM_FMRI_HC_ID, inst); 847 if (err != 0) { 848 nvlist_free(hc); 849 return (NULL); 850 } 851 852 return (hc); 853 } 854 855 static nvlist_t * 856 hc_create_seterror(topo_mod_t *mod, nvlist_t **hcl, int n, nvlist_t *fmri, 857 int err) 858 { 859 int i; 860 861 if (hcl != NULL) { 862 for (i = 0; i < n + 1; ++i) 863 nvlist_free(hcl[i]); 864 865 topo_mod_free(mod, hcl, sizeof (nvlist_t *) * (n + 1)); 866 } 867 868 nvlist_free(fmri); 869 870 (void) topo_mod_seterrno(mod, err); 871 872 topo_mod_dprintf(mod, "unable to create hc FMRI: %s\n", 873 topo_mod_errmsg(mod)); 874 875 return (NULL); 876 } 877 878 static int 879 hc_name_canonical(topo_mod_t *mod, const char *name) 880 { 881 int i; 882 883 if (getenv("NOHCCHECK") != NULL) 884 return (1); 885 886 /* 887 * Only enumerate elements with correct canonical names 888 */ 889 for (i = 0; i < hc_ncanon; i++) { 890 if (strcmp(name, hc_canon[i].hcc_name) == 0) 891 break; 892 } 893 if (i >= hc_ncanon) { 894 topo_mod_dprintf(mod, "non-canonical name %s\n", 895 name); 896 return (0); 897 } else { 898 return (1); 899 } 900 } 901 902 static nvlist_t * 903 hc_fmri_create(topo_mod_t *mod, nvlist_t *pfmri, int version, const char *name, 904 topo_instance_t inst, const nvlist_t *auth, const char *part, 905 const char *rev, const char *serial) 906 { 907 int i; 908 char str[21]; /* sizeof (UINT64_MAX) + '\0' */ 909 uint_t pelems = 0; 910 nvlist_t **phcl = NULL; 911 nvlist_t **hcl = NULL; 912 nvlist_t *fmri = NULL; 913 914 if (version > FM_HC_SCHEME_VERSION) 915 return (hc_create_seterror(mod, 916 hcl, pelems, fmri, EMOD_VER_OLD)); 917 else if (version < FM_HC_SCHEME_VERSION) 918 return (hc_create_seterror(mod, 919 hcl, pelems, fmri, EMOD_VER_NEW)); 920 921 /* 922 * Check that the requested name is in our canonical list 923 */ 924 if (hc_name_canonical(mod, name) == 0) 925 return (hc_create_seterror(mod, 926 hcl, pelems, fmri, EMOD_NONCANON)); 927 /* 928 * Copy the parent's HC_LIST 929 */ 930 if (pfmri != NULL) { 931 if (nvlist_lookup_nvlist_array(pfmri, FM_FMRI_HC_LIST, 932 &phcl, &pelems) != 0) 933 return (hc_create_seterror(mod, 934 hcl, pelems, fmri, EMOD_FMRI_MALFORM)); 935 } 936 937 hcl = topo_mod_zalloc(mod, sizeof (nvlist_t *) * (pelems + 1)); 938 if (hcl == NULL) 939 return (hc_create_seterror(mod, hcl, pelems, fmri, 940 EMOD_NOMEM)); 941 942 for (i = 0; i < pelems; ++i) 943 if (topo_mod_nvdup(mod, phcl[i], &hcl[i]) != 0) 944 return (hc_create_seterror(mod, 945 hcl, pelems, fmri, EMOD_FMRI_NVL)); 946 947 (void) snprintf(str, sizeof (str), "%d", inst); 948 if ((hcl[i] = hc_list_create(mod, name, str)) == NULL) 949 return (hc_create_seterror(mod, 950 hcl, pelems, fmri, EMOD_FMRI_NVL)); 951 952 if ((fmri = hc_base_fmri_create(mod, auth, part, rev, serial)) == NULL) 953 return (hc_create_seterror(mod, 954 hcl, pelems, fmri, EMOD_FMRI_NVL)); 955 956 if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, hcl, pelems + 1) 957 != 0) 958 return (hc_create_seterror(mod, 959 hcl, pelems, fmri, EMOD_FMRI_NVL)); 960 961 if (hcl != NULL) { 962 for (i = 0; i < pelems + 1; ++i) { 963 if (hcl[i] != NULL) 964 nvlist_free(hcl[i]); 965 } 966 topo_mod_free(mod, hcl, sizeof (nvlist_t *) * (pelems + 1)); 967 } 968 969 return (fmri); 970 } 971 972 /*ARGSUSED*/ 973 static int 974 hc_fmri_create_meth(topo_mod_t *mod, tnode_t *node, topo_version_t version, 975 nvlist_t *in, nvlist_t **out) 976 { 977 int ret; 978 nvlist_t *args, *pfmri = NULL; 979 nvlist_t *auth; 980 uint32_t inst; 981 char *name, *serial, *rev, *part; 982 983 if (version > TOPO_METH_FMRI_VERSION) 984 return (topo_mod_seterrno(mod, EMOD_VER_NEW)); 985 986 /* First the must-have fields */ 987 if (nvlist_lookup_string(in, TOPO_METH_FMRI_ARG_NAME, &name) != 0) 988 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 989 if (nvlist_lookup_uint32(in, TOPO_METH_FMRI_ARG_INST, &inst) != 0) 990 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 991 992 /* 993 * args is optional 994 */ 995 pfmri = NULL; 996 auth = NULL; 997 serial = rev = part = NULL; 998 if ((ret = nvlist_lookup_nvlist(in, TOPO_METH_FMRI_ARG_NVL, &args)) 999 != 0) { 1000 if (ret != ENOENT) 1001 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1002 } else { 1003 1004 /* And then optional arguments */ 1005 (void) nvlist_lookup_nvlist(args, TOPO_METH_FMRI_ARG_PARENT, 1006 &pfmri); 1007 (void) nvlist_lookup_nvlist(args, TOPO_METH_FMRI_ARG_AUTH, 1008 &auth); 1009 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_PART, 1010 &part); 1011 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_REV, &rev); 1012 (void) nvlist_lookup_string(args, TOPO_METH_FMRI_ARG_SER, 1013 &serial); 1014 } 1015 1016 *out = hc_fmri_create(mod, pfmri, version, name, inst, auth, part, 1017 rev, serial); 1018 if (*out == NULL) 1019 return (-1); 1020 return (0); 1021 } 1022 1023 struct hc_walk { 1024 topo_mod_walk_cb_t hcw_cb; 1025 void *hcw_priv; 1026 topo_walk_t *hcw_wp; 1027 nvlist_t **hcw_list; 1028 uint_t hcw_index; 1029 uint_t hcw_end; 1030 }; 1031 1032 /* 1033 * Generic walker for the hc-scheme topo tree. This function uses the 1034 * hierachical nature of the hc-scheme to step through efficiently through 1035 * the topo hc tree. Node lookups are done by topo_walk_byid() at each 1036 * component level to avoid unnecessary traversal of the tree. 1037 * 1038 */ 1039 static int 1040 hc_walker(topo_mod_t *mod, tnode_t *node, void *pdata) 1041 { 1042 int i, err; 1043 struct hc_walk *hwp = (struct hc_walk *)pdata; 1044 char *name, *id; 1045 topo_instance_t inst; 1046 1047 i = hwp->hcw_index; 1048 if (i > hwp->hcw_end) { 1049 (void) topo_mod_seterrno(mod, ETOPO_PROP_NOENT); 1050 return (TOPO_WALK_TERMINATE); 1051 } 1052 1053 err = nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_NAME, &name); 1054 err |= nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_ID, &id); 1055 1056 if (err != 0) { 1057 (void) topo_mod_seterrno(mod, EMOD_NVL_INVAL); 1058 return (TOPO_WALK_ERR); 1059 } 1060 1061 inst = atoi(id); 1062 topo_mod_dprintf(mod, "hc_walker: walking node:%s=%d for hc:" 1063 "%s=%d at %d, end at %d \n", topo_node_name(node), 1064 topo_node_instance(node), name, inst, i, hwp->hcw_end); 1065 if (i == hwp->hcw_end) { 1066 /* 1067 * We are at the end of the hc-list. Verify that 1068 * the last node contains the name/instance we are looking for. 1069 */ 1070 if (strcmp(topo_node_name(node), name) == 0 && 1071 inst == topo_node_instance(node)) { 1072 if ((err = hwp->hcw_cb(mod, node, hwp->hcw_priv)) 1073 != 0) { 1074 (void) topo_mod_seterrno(mod, err); 1075 topo_mod_dprintf(mod, "hc_walker: callback " 1076 "failed: %s\n ", topo_mod_errmsg(mod)); 1077 return (TOPO_WALK_ERR); 1078 } 1079 topo_mod_dprintf(mod, "hc_walker: callback " 1080 "complete: terminate walk\n"); 1081 return (TOPO_WALK_TERMINATE); 1082 } else { 1083 topo_mod_dprintf(mod, "hc_walker: %s=%d\n " 1084 "not found\n", name, inst); 1085 return (TOPO_WALK_TERMINATE); 1086 } 1087 } 1088 1089 hwp->hcw_index = ++i; 1090 err = nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_NAME, &name); 1091 err |= nvlist_lookup_string(hwp->hcw_list[i], FM_FMRI_HC_ID, &id); 1092 if (err != 0) { 1093 (void) topo_mod_seterrno(mod, err); 1094 return (TOPO_WALK_ERR); 1095 } 1096 inst = atoi(id); 1097 1098 topo_mod_dprintf(mod, "hc_walker: walk byid of %s=%d \n", name, 1099 inst); 1100 return (topo_walk_byid(hwp->hcw_wp, name, inst)); 1101 1102 } 1103 1104 static struct hc_walk * 1105 hc_walk_init(topo_mod_t *mod, tnode_t *node, nvlist_t *rsrc, 1106 topo_mod_walk_cb_t cb, void *pdata) 1107 { 1108 int err; 1109 uint_t sz; 1110 struct hc_walk *hwp; 1111 topo_walk_t *wp; 1112 1113 if ((hwp = topo_mod_alloc(mod, sizeof (struct hc_walk))) == NULL) 1114 (void) topo_mod_seterrno(mod, EMOD_NOMEM); 1115 1116 if (nvlist_lookup_nvlist_array(rsrc, FM_FMRI_HC_LIST, &hwp->hcw_list, 1117 &sz) != 0) { 1118 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1119 (void) topo_mod_seterrno(mod, EMOD_METHOD_INVAL); 1120 return (NULL); 1121 } 1122 1123 hwp->hcw_end = sz - 1; 1124 hwp->hcw_index = 0; 1125 hwp->hcw_priv = pdata; 1126 hwp->hcw_cb = cb; 1127 if ((wp = topo_mod_walk_init(mod, node, hc_walker, (void *)hwp, &err)) 1128 == NULL) { 1129 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1130 (void) topo_mod_seterrno(mod, err); 1131 return (NULL); 1132 } 1133 1134 hwp->hcw_wp = wp; 1135 1136 return (hwp); 1137 } 1138 1139 struct prop_lookup { 1140 const char *pl_pgroup; 1141 const char *pl_pname; 1142 int pl_flag; 1143 nvlist_t *pl_args; 1144 nvlist_t *pl_rsrc; 1145 nvlist_t *pl_prop; 1146 }; 1147 1148 /*ARGSUSED*/ 1149 static int 1150 hc_prop_get(topo_mod_t *mod, tnode_t *node, void *pdata) 1151 { 1152 int err = 0; 1153 1154 struct prop_lookup *plp = (struct prop_lookup *)pdata; 1155 1156 (void) topo_prop_getprop(node, plp->pl_pgroup, plp->pl_pname, 1157 plp->pl_args, &plp->pl_prop, &err); 1158 1159 return (err); 1160 } 1161 1162 static int 1163 hc_fmri_prop_get(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1164 nvlist_t *in, nvlist_t **out) 1165 { 1166 int err; 1167 struct hc_walk *hwp; 1168 struct prop_lookup *plp; 1169 1170 if (version > TOPO_METH_PROP_GET_VERSION) 1171 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1172 1173 if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL) 1174 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1175 1176 err = nvlist_lookup_string(in, TOPO_PROP_GROUP, 1177 (char **)&plp->pl_pgroup); 1178 err |= nvlist_lookup_string(in, TOPO_PROP_VAL_NAME, 1179 (char **)&plp->pl_pname); 1180 err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc); 1181 if (err != 0) { 1182 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1183 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1184 } 1185 1186 /* 1187 * Private args to prop method are optional 1188 */ 1189 if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &plp->pl_args)) 1190 != 0) { 1191 if (err != ENOENT) { 1192 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1193 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1194 } else { 1195 plp->pl_args = NULL; 1196 } 1197 } 1198 1199 plp->pl_prop = NULL; 1200 if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_prop_get, 1201 (void *)plp)) != NULL) { 1202 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1203 TOPO_WALK_ERR) 1204 err = -1; 1205 else 1206 err = 0; 1207 topo_walk_fini(hwp->hcw_wp); 1208 } else { 1209 err = -1; 1210 } 1211 1212 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1213 1214 if (plp->pl_prop != NULL) 1215 *out = plp->pl_prop; 1216 1217 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1218 1219 return (err); 1220 } 1221 1222 /*ARGSUSED*/ 1223 static int 1224 hc_pgrp_get(topo_mod_t *mod, tnode_t *node, void *pdata) 1225 { 1226 int err = 0; 1227 1228 struct prop_lookup *plp = (struct prop_lookup *)pdata; 1229 1230 (void) topo_prop_getpgrp(node, plp->pl_pgroup, &plp->pl_prop, &err); 1231 1232 return (err); 1233 } 1234 1235 static int 1236 hc_fmri_pgrp_get(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1237 nvlist_t *in, nvlist_t **out) 1238 { 1239 int err; 1240 struct hc_walk *hwp; 1241 struct prop_lookup *plp; 1242 1243 if (version > TOPO_METH_PGRP_GET_VERSION) 1244 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1245 1246 if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL) 1247 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1248 1249 err = nvlist_lookup_string(in, TOPO_PROP_GROUP, 1250 (char **)&plp->pl_pgroup); 1251 err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc); 1252 if (err != 0) { 1253 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1254 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1255 } 1256 1257 plp->pl_prop = NULL; 1258 if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_pgrp_get, 1259 (void *)plp)) != NULL) { 1260 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1261 TOPO_WALK_ERR) 1262 err = -1; 1263 else 1264 err = 0; 1265 topo_walk_fini(hwp->hcw_wp); 1266 } else { 1267 err = -1; 1268 } 1269 1270 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1271 1272 if (plp->pl_prop != NULL) 1273 *out = plp->pl_prop; 1274 1275 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1276 1277 return (err); 1278 } 1279 1280 /*ARGSUSED*/ 1281 static int 1282 hc_prop_setprop(topo_mod_t *mod, tnode_t *node, void *pdata) 1283 { 1284 int err = 0; 1285 1286 struct prop_lookup *plp = (struct prop_lookup *)pdata; 1287 1288 (void) topo_prop_setprop(node, plp->pl_pgroup, plp->pl_prop, 1289 plp->pl_flag, plp->pl_args, &err); 1290 1291 return (err); 1292 } 1293 1294 /*ARGSUSED*/ 1295 static int 1296 hc_fmri_prop_set(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1297 nvlist_t *in, nvlist_t **out) 1298 { 1299 int err; 1300 struct hc_walk *hwp; 1301 struct prop_lookup *plp; 1302 1303 if (version > TOPO_METH_PROP_SET_VERSION) 1304 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1305 1306 if ((plp = topo_mod_alloc(mod, sizeof (struct prop_lookup))) == NULL) 1307 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1308 1309 err = nvlist_lookup_string(in, TOPO_PROP_GROUP, 1310 (char **)&plp->pl_pgroup); 1311 err |= nvlist_lookup_nvlist(in, TOPO_PROP_RESOURCE, &plp->pl_rsrc); 1312 err |= nvlist_lookup_nvlist(in, TOPO_PROP_VAL, &plp->pl_prop); 1313 err |= nvlist_lookup_int32(in, TOPO_PROP_FLAG, &plp->pl_flag); 1314 if (err != 0) { 1315 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1316 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1317 } 1318 1319 /* 1320 * Private args to prop method are optional 1321 */ 1322 if ((err = nvlist_lookup_nvlist(in, TOPO_PROP_PARGS, &plp->pl_args)) 1323 != 0) { 1324 if (err != ENOENT) 1325 return (topo_mod_seterrno(mod, EMOD_METHOD_INVAL)); 1326 else 1327 plp->pl_args = NULL; 1328 } 1329 1330 if ((hwp = hc_walk_init(mod, node, plp->pl_rsrc, hc_prop_setprop, 1331 (void *)plp)) != NULL) { 1332 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1333 TOPO_WALK_ERR) 1334 err = -1; 1335 else 1336 err = 0; 1337 topo_walk_fini(hwp->hcw_wp); 1338 } else { 1339 err = -1; 1340 } 1341 1342 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1343 topo_mod_free(mod, plp, sizeof (struct prop_lookup)); 1344 1345 return (err); 1346 } 1347 1348 struct hc_args { 1349 nvlist_t *ha_fmri; 1350 nvlist_t *ha_nvl; 1351 }; 1352 1353 static int 1354 hc_is_present(topo_mod_t *mod, tnode_t *node, void *pdata) 1355 { 1356 int err; 1357 struct hc_args *hap = (struct hc_args *)pdata; 1358 1359 /* 1360 * check with the enumerator that created this FMRI 1361 * (topo node) 1362 */ 1363 if (topo_method_invoke(node, TOPO_METH_PRESENT, 1364 TOPO_METH_PRESENT_VERSION, hap->ha_fmri, &hap->ha_nvl, 1365 &err) < 0) { 1366 1367 /* 1368 * Err on the side of caution and return present 1369 */ 1370 if (topo_mod_nvalloc(mod, &hap->ha_nvl, NV_UNIQUE_NAME) == 0) 1371 if (nvlist_add_uint32(hap->ha_nvl, 1372 TOPO_METH_PRESENT_RET, 1) == 0) 1373 return (0); 1374 1375 return (ETOPO_PROP_NVL); 1376 } 1377 1378 return (err); 1379 } 1380 1381 static int 1382 hc_fmri_present(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1383 nvlist_t *in, nvlist_t **out) 1384 { 1385 int err; 1386 struct hc_walk *hwp; 1387 struct hc_args *hap; 1388 1389 if (version > TOPO_METH_PRESENT_VERSION) 1390 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1391 1392 if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL) 1393 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1394 1395 hap->ha_fmri = in; 1396 hap->ha_nvl = NULL; 1397 if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_is_present, 1398 (void *)hap)) != NULL) { 1399 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1400 TOPO_WALK_ERR) 1401 err = -1; 1402 else 1403 err = 0; 1404 topo_walk_fini(hwp->hcw_wp); 1405 } else { 1406 err = -1; 1407 } 1408 1409 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1410 1411 if (hap->ha_nvl != NULL) 1412 *out = hap->ha_nvl; 1413 1414 topo_mod_free(mod, hap, sizeof (struct hc_args)); 1415 1416 return (err); 1417 } 1418 1419 static int 1420 hc_unusable(topo_mod_t *mod, tnode_t *node, void *pdata) 1421 { 1422 int err; 1423 struct hc_args *hap = (struct hc_args *)pdata; 1424 1425 /* 1426 * check with the enumerator that created this FMRI 1427 * (topo node) 1428 */ 1429 if (topo_method_invoke(node, TOPO_METH_UNUSABLE, 1430 TOPO_METH_UNUSABLE_VERSION, hap->ha_fmri, &hap->ha_nvl, 1431 &err) < 0) { 1432 1433 /* 1434 * Err on the side of caution and return usable 1435 */ 1436 if (topo_mod_nvalloc(mod, &hap->ha_nvl, NV_UNIQUE_NAME) == 0) 1437 if (nvlist_add_uint32(hap->ha_nvl, 1438 TOPO_METH_UNUSABLE_RET, 0) == 0) 1439 return (0); 1440 1441 return (ETOPO_PROP_NVL); 1442 } 1443 1444 return (err); 1445 } 1446 1447 static int 1448 hc_fmri_unusable(topo_mod_t *mod, tnode_t *node, topo_version_t version, 1449 nvlist_t *in, nvlist_t **out) 1450 { 1451 int err; 1452 struct hc_walk *hwp; 1453 struct hc_args *hap; 1454 1455 if (version > TOPO_METH_UNUSABLE_VERSION) 1456 return (topo_mod_seterrno(mod, ETOPO_METHOD_VERNEW)); 1457 1458 if ((hap = topo_mod_alloc(mod, sizeof (struct hc_args))) == NULL) 1459 return (topo_mod_seterrno(mod, EMOD_NOMEM)); 1460 1461 hap->ha_fmri = in; 1462 hap->ha_nvl = NULL; 1463 if ((hwp = hc_walk_init(mod, node, hap->ha_fmri, hc_unusable, 1464 (void *)hap)) != NULL) { 1465 if (topo_walk_step(hwp->hcw_wp, TOPO_WALK_CHILD) == 1466 TOPO_WALK_ERR) 1467 err = -1; 1468 else 1469 err = 0; 1470 topo_walk_fini(hwp->hcw_wp); 1471 } else { 1472 err = -1; 1473 } 1474 1475 topo_mod_free(mod, hwp, sizeof (struct hc_walk)); 1476 1477 if (hap->ha_nvl != NULL) 1478 *out = hap->ha_nvl; 1479 1480 topo_mod_free(mod, hap, sizeof (struct hc_args)); 1481 1482 return (err); 1483 } 1484