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