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