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