xref: /illumos-gate/usr/src/uts/common/os/fm.c (revision bbf21555)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22392e836bSGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Fault Management Architecture (FMA) Resource and Protocol Support
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * The routines contained herein provide services to support kernel subsystems
297c478bd9Sstevel@tonic-gate  * in publishing fault management telemetry (see PSARC 2002/412 and 2003/089).
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Name-Value Pair Lists
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * The embodiment of an FMA protocol element (event, fmri or authority) is a
347c478bd9Sstevel@tonic-gate  * name-value pair list (nvlist_t).  FMA-specific nvlist construtor and
357c478bd9Sstevel@tonic-gate  * destructor functions, fm_nvlist_create() and fm_nvlist_destroy(), are used
367c478bd9Sstevel@tonic-gate  * to create an nvpair list using custom allocators.  Callers may choose to
377c478bd9Sstevel@tonic-gate  * allocate either from the kernel memory allocator, or from a preallocated
387c478bd9Sstevel@tonic-gate  * buffer, useful in constrained contexts like high-level interrupt routines.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * Protocol Event and FMRI Construction
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * Convenience routines are provided to construct nvlist events according to
437c478bd9Sstevel@tonic-gate  * the FMA Event Protocol and Naming Schema specification for ereports and
447c478bd9Sstevel@tonic-gate  * FMRIs for the dev, cpu, hc, mem, legacy hc and de schemes.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * ENA Manipulation
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Routines to generate ENA formats 0, 1 and 2 are available as well as
497c478bd9Sstevel@tonic-gate  * routines to increment formats 1 and 2.  Individual fields within the
507c478bd9Sstevel@tonic-gate  * ENA are extractable via fm_ena_time_get(), fm_ena_id_get(),
517c478bd9Sstevel@tonic-gate  * fm_ena_format_get() and fm_ena_gen_get().
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <sys/time.h>
567c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
577c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
587c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
597c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
607c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
617c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
627c478bd9Sstevel@tonic-gate #include <sys/systm.h>
637c478bd9Sstevel@tonic-gate #include <sys/ddifm.h>
647c478bd9Sstevel@tonic-gate #include <sys/ddifm_impl.h>
657c478bd9Sstevel@tonic-gate #include <sys/spl.h>
667c478bd9Sstevel@tonic-gate #include <sys/dumphdr.h>
677c478bd9Sstevel@tonic-gate #include <sys/compress.h>
687c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
697c478bd9Sstevel@tonic-gate #include <sys/console.h>
707c478bd9Sstevel@tonic-gate #include <sys/panic.h>
717aec1d6eScindi #include <sys/kobj.h>
727aec1d6eScindi #include <sys/sunddi.h>
737c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
747c478bd9Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
757c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
767c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * URL and SUNW-MSG-ID value to display for fm_panic(), defined below.  These
807c478bd9Sstevel@tonic-gate  * values must be kept in sync with the FMA source code in usr/src/cmd/fm.
817c478bd9Sstevel@tonic-gate  */
82654b400cSJoshua M. Clulow static const char *fm_url = "http://illumos.org/msg";
837c478bd9Sstevel@tonic-gate static const char *fm_msgid = "SUNOS-8000-0G";
847c478bd9Sstevel@tonic-gate static char *volatile fm_panicstr = NULL;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate errorq_t *ereport_errorq;
877c478bd9Sstevel@tonic-gate void *ereport_dumpbuf;
887c478bd9Sstevel@tonic-gate size_t ereport_dumplen;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static uint_t ereport_chanlen = ERPT_EVCH_MAX;
917c478bd9Sstevel@tonic-gate static evchan_t *ereport_chan = NULL;
927c478bd9Sstevel@tonic-gate static ulong_t ereport_qlen = 0;
937c478bd9Sstevel@tonic-gate static size_t ereport_size = 0;
947c478bd9Sstevel@tonic-gate static int ereport_cols = 80;
957c478bd9Sstevel@tonic-gate 
9614b25e8aSAdrian Frost extern void fastreboot_disable_highpil(void);
9714b25e8aSAdrian Frost 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Common fault management kstats to record ereport generation
1007c478bd9Sstevel@tonic-gate  * failures
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate struct erpt_kstat {
1047c478bd9Sstevel@tonic-gate 	kstat_named_t	erpt_dropped;		/* num erpts dropped on post */
1057c478bd9Sstevel@tonic-gate 	kstat_named_t	erpt_set_failed;	/* num erpt set failures */
1067c478bd9Sstevel@tonic-gate 	kstat_named_t	fmri_set_failed;	/* num fmri set failures */
1077c478bd9Sstevel@tonic-gate 	kstat_named_t	payload_set_failed;	/* num payload set failures */
1087c478bd9Sstevel@tonic-gate };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static struct erpt_kstat erpt_kstat_data = {
1117c478bd9Sstevel@tonic-gate 	{ "erpt-dropped", KSTAT_DATA_UINT64 },
1127c478bd9Sstevel@tonic-gate 	{ "erpt-set-failed", KSTAT_DATA_UINT64 },
1137c478bd9Sstevel@tonic-gate 	{ "fmri-set-failed", KSTAT_DATA_UINT64 },
1147c478bd9Sstevel@tonic-gate 	{ "payload-set-failed", KSTAT_DATA_UINT64 }
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1187c478bd9Sstevel@tonic-gate static void
fm_drain(void * private,void * data,errorq_elem_t * eep)1197c478bd9Sstevel@tonic-gate fm_drain(void *private, void *data, errorq_elem_t *eep)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = errorq_elem_nvl(ereport_errorq, eep);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (!panicstr)
1247c478bd9Sstevel@tonic-gate 		(void) fm_ereport_post(nvl, EVCH_TRYHARD);
1257c478bd9Sstevel@tonic-gate 	else
1267c478bd9Sstevel@tonic-gate 		fm_nvprint(nvl);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate void
fm_init(void)1307c478bd9Sstevel@tonic-gate fm_init(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	(void) sysevent_evc_bind(FM_ERROR_CHAN,
1357c478bd9Sstevel@tonic-gate 	    &ereport_chan, EVCH_CREAT | EVCH_HOLD_PEND);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	(void) sysevent_evc_control(ereport_chan,
1387c478bd9Sstevel@tonic-gate 	    EVCH_SET_CHAN_LEN, &ereport_chanlen);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (ereport_qlen == 0)
1417c478bd9Sstevel@tonic-gate 		ereport_qlen = ERPT_MAX_ERRS * MAX(max_ncpus, 4);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (ereport_size == 0)
1447c478bd9Sstevel@tonic-gate 		ereport_size = ERPT_DATA_SZ;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	ereport_errorq = errorq_nvcreate("fm_ereport_queue",
1477c478bd9Sstevel@tonic-gate 	    (errorq_func_t)fm_drain, NULL, ereport_qlen, ereport_size,
1487c478bd9Sstevel@tonic-gate 	    FM_ERR_PIL, ERRORQ_VITAL);
1497c478bd9Sstevel@tonic-gate 	if (ereport_errorq == NULL)
1507c478bd9Sstevel@tonic-gate 		panic("failed to create required ereport error queue");
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	ereport_dumpbuf = kmem_alloc(ereport_size, KM_SLEEP);
1537c478bd9Sstevel@tonic-gate 	ereport_dumplen = ereport_size;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* Initialize ereport allocation and generation kstats */
1567c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "fm", "misc", KSTAT_TYPE_NAMED,
1577c478bd9Sstevel@tonic-gate 	    sizeof (struct erpt_kstat) / sizeof (kstat_named_t),
1587c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
1617c478bd9Sstevel@tonic-gate 		ksp->ks_data = &erpt_kstat_data;
1627c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
1637c478bd9Sstevel@tonic-gate 	} else {
1647c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "failed to create fm/misc kstat\n");
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Formatting utility function for fm_nvprintr.  We attempt to wrap chunks of
1717c478bd9Sstevel@tonic-gate  * output so they aren't split across console lines, and return the end column.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate /*PRINTFLIKE4*/
1747c478bd9Sstevel@tonic-gate static int
fm_printf(int depth,int c,int cols,const char * format,...)1757c478bd9Sstevel@tonic-gate fm_printf(int depth, int c, int cols, const char *format, ...)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	va_list ap;
1787c478bd9Sstevel@tonic-gate 	int width;
1797c478bd9Sstevel@tonic-gate 	char c1;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1827c478bd9Sstevel@tonic-gate 	width = vsnprintf(&c1, sizeof (c1), format, ap);
1837c478bd9Sstevel@tonic-gate 	va_end(ap);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (c + width >= cols) {
1867c478bd9Sstevel@tonic-gate 		console_printf("\n\r");
1877c478bd9Sstevel@tonic-gate 		c = 0;
1887c478bd9Sstevel@tonic-gate 		if (format[0] != ' ' && depth > 0) {
1897c478bd9Sstevel@tonic-gate 			console_printf(" ");
1907c478bd9Sstevel@tonic-gate 			c++;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	va_start(ap, format);
1957c478bd9Sstevel@tonic-gate 	console_vprintf(format, ap);
1967c478bd9Sstevel@tonic-gate 	va_end(ap);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return ((c + width) % cols);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Recursively print a nvlist in the specified column width and return the
2037c478bd9Sstevel@tonic-gate  * column we end up in.  This function is called recursively by fm_nvprint(),
2047c478bd9Sstevel@tonic-gate  * below.  We generically format the entire nvpair using hexadecimal
2057c478bd9Sstevel@tonic-gate  * integers and strings, and elide any integer arrays.  Arrays are basically
2067c478bd9Sstevel@tonic-gate  * used for cache dumps right now, so we suppress them so as not to overwhelm
2077c478bd9Sstevel@tonic-gate  * the amount of console output we produce at panic time.  This can be further
2087c478bd9Sstevel@tonic-gate  * enhanced as FMA technology grows based upon the needs of consumers.  All
2097c478bd9Sstevel@tonic-gate  * FMA telemetry is logged using the dump device transport, so the console
2107c478bd9Sstevel@tonic-gate  * output serves only as a fallback in case this procedure is unsuccessful.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static int
fm_nvprintr(nvlist_t * nvl,int d,int c,int cols)2137c478bd9Sstevel@tonic-gate fm_nvprintr(nvlist_t *nvl, int d, int c, int cols)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	for (nvp = nvlist_next_nvpair(nvl, NULL);
2187c478bd9Sstevel@tonic-gate 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		data_type_t type = nvpair_type(nvp);
2217c478bd9Sstevel@tonic-gate 		const char *name = nvpair_name(nvp);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		boolean_t b;
2247c478bd9Sstevel@tonic-gate 		uint8_t i8;
2257c478bd9Sstevel@tonic-gate 		uint16_t i16;
2267c478bd9Sstevel@tonic-gate 		uint32_t i32;
2277c478bd9Sstevel@tonic-gate 		uint64_t i64;
2287c478bd9Sstevel@tonic-gate 		char *str;
2297c478bd9Sstevel@tonic-gate 		nvlist_t *cnv;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if (strcmp(name, FM_CLASS) == 0)
2327c478bd9Sstevel@tonic-gate 			continue; /* already printed by caller */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		c = fm_printf(d, c, cols, " %s=", name);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		switch (type) {
2377c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN:
2387c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " 1");
2397c478bd9Sstevel@tonic-gate 			break;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
2427c478bd9Sstevel@tonic-gate 			(void) nvpair_value_boolean_value(nvp, &b);
2437c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, b ? "1" : "0");
2447c478bd9Sstevel@tonic-gate 			break;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
2477c478bd9Sstevel@tonic-gate 			(void) nvpair_value_byte(nvp, &i8);
2487c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8:
2527c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int8(nvp, (void *)&i8);
2537c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2547c478bd9Sstevel@tonic-gate 			break;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
2577c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint8(nvp, &i8);
2587c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16:
2627c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int16(nvp, (void *)&i16);
2637c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
2677c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint16(nvp, &i16);
2687c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32:
2727c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (void *)&i32);
2737c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
2747c478bd9Sstevel@tonic-gate 			break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
2777c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint32(nvp, &i32);
2787c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64:
2827c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int64(nvp, (void *)&i64);
2837c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2847c478bd9Sstevel@tonic-gate 			    (u_longlong_t)i64);
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
2887c478bd9Sstevel@tonic-gate 			(void) nvpair_value_uint64(nvp, &i64);
2897c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2907c478bd9Sstevel@tonic-gate 			    (u_longlong_t)i64);
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		case DATA_TYPE_HRTIME:
2947c478bd9Sstevel@tonic-gate 			(void) nvpair_value_hrtime(nvp, (void *)&i64);
2957c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2967c478bd9Sstevel@tonic-gate 			    (u_longlong_t)i64);
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING:
3007c478bd9Sstevel@tonic-gate 			(void) nvpair_value_string(nvp, &str);
3017c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "\"%s\"",
3027c478bd9Sstevel@tonic-gate 			    str ? str : "<NULL>");
3037c478bd9Sstevel@tonic-gate 			break;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
3067c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[");
3077c478bd9Sstevel@tonic-gate 			(void) nvpair_value_nvlist(nvp, &cnv);
3087c478bd9Sstevel@tonic-gate 			c = fm_nvprintr(cnv, d + 1, c, cols);
3097c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " ]");
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 
312e4b86885SCheng Sean Ye 		case DATA_TYPE_NVLIST_ARRAY: {
313e4b86885SCheng Sean Ye 			nvlist_t **val;
314e4b86885SCheng Sean Ye 			uint_t i, nelem;
315e4b86885SCheng Sean Ye 
316e4b86885SCheng Sean Ye 			c = fm_printf(d + 1, c, cols, "[");
317e4b86885SCheng Sean Ye 			(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
318e4b86885SCheng Sean Ye 			for (i = 0; i < nelem; i++) {
319e4b86885SCheng Sean Ye 				c = fm_nvprintr(val[i], d + 1, c, cols);
320e4b86885SCheng Sean Ye 			}
321e4b86885SCheng Sean Ye 			c = fm_printf(d + 1, c, cols, " ]");
322e4b86885SCheng Sean Ye 			}
323e4b86885SCheng Sean Ye 			break;
324e4b86885SCheng Sean Ye 
3257c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
3267c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
3277c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
3287c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
3297c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
3307c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
3317c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
3327c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
3337c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
3347c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
3357c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
3367c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[...]");
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UNKNOWN:
3393df2e8b2SRobert Mustacchi 		case DATA_TYPE_DONTCARE:
3407c478bd9Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "<unknown>");
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	return (c);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate void
fm_nvprint(nvlist_t * nvl)3497c478bd9Sstevel@tonic-gate fm_nvprint(nvlist_t *nvl)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 	char *class;
3527c478bd9Sstevel@tonic-gate 	int c = 0;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	console_printf("\r");
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) == 0)
3577c478bd9Sstevel@tonic-gate 		c = fm_printf(0, c, ereport_cols, "%s", class);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (fm_nvprintr(nvl, 0, c, ereport_cols) != 0)
3607c478bd9Sstevel@tonic-gate 		console_printf("\n");
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	console_printf("\n");
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * Wrapper for panic() that first produces an FMA-style message for admins.
367*bbf21555SRichard Lowe  * Normally such messages are generated by fmd(8)'s syslog-msgs agent: this
3687c478bd9Sstevel@tonic-gate  * is the one exception to that rule and the only error that gets messaged.
3697c478bd9Sstevel@tonic-gate  * This function is intended for use by subsystems that have detected a fatal
3707c478bd9Sstevel@tonic-gate  * error and enqueued appropriate ereports and wish to then force a panic.
3717c478bd9Sstevel@tonic-gate  */
3727c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
3737c478bd9Sstevel@tonic-gate void
fm_panic(const char * format,...)3747c478bd9Sstevel@tonic-gate fm_panic(const char *format, ...)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	va_list ap;
3777c478bd9Sstevel@tonic-gate 
37875d94465SJosef 'Jeff' Sipek 	(void) atomic_cas_ptr((void *)&fm_panicstr, NULL, (void *)format);
37986ef0a63SRichard Lowe #if defined(__x86)
38014b25e8aSAdrian Frost 	fastreboot_disable_highpil();
38186ef0a63SRichard Lowe #endif /* __x86 */
3827c478bd9Sstevel@tonic-gate 	va_start(ap, format);
3837c478bd9Sstevel@tonic-gate 	vpanic(format, ap);
3847c478bd9Sstevel@tonic-gate 	va_end(ap);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
387f6e214c7SGavin Maltby /*
388f6e214c7SGavin Maltby  * Simply tell the caller if fm_panicstr is set, ie. an fma event has
389f6e214c7SGavin Maltby  * caused the panic. If so, something other than the default panic
390f6e214c7SGavin Maltby  * diagnosis method will diagnose the cause of the panic.
391f6e214c7SGavin Maltby  */
392f6e214c7SGavin Maltby int
is_fm_panic()393f6e214c7SGavin Maltby is_fm_panic()
394f6e214c7SGavin Maltby {
395f6e214c7SGavin Maltby 	if (fm_panicstr)
396f6e214c7SGavin Maltby 		return (1);
397f6e214c7SGavin Maltby 	else
398f6e214c7SGavin Maltby 		return (0);
399f6e214c7SGavin Maltby }
400f6e214c7SGavin Maltby 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * Print any appropriate FMA banner message before the panic message.  This
4037c478bd9Sstevel@tonic-gate  * function is called by panicsys() and prints the message for fm_panic().
4047c478bd9Sstevel@tonic-gate  * We print the message here so that it comes after the system is quiesced.
4057c478bd9Sstevel@tonic-gate  * A one-line summary is recorded in the log only (cmn_err(9F) with "!" prefix).
4067c478bd9Sstevel@tonic-gate  * The rest of the message is for the console only and not needed in the log,
4077c478bd9Sstevel@tonic-gate  * so it is printed using console_printf().  We break it up into multiple
4087c478bd9Sstevel@tonic-gate  * chunks so as to avoid overflowing any small legacy prom_printf() buffers.
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate void
fm_banner(void)4117c478bd9Sstevel@tonic-gate fm_banner(void)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	timespec_t tod;
4147c478bd9Sstevel@tonic-gate 	hrtime_t now;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if (!fm_panicstr)
4177c478bd9Sstevel@tonic-gate 		return; /* panic was not initiated by fm_panic(); do nothing */
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (panicstr) {
4207c478bd9Sstevel@tonic-gate 		tod = panic_hrestime;
4217c478bd9Sstevel@tonic-gate 		now = panic_hrtime;
4227c478bd9Sstevel@tonic-gate 	} else {
4237c478bd9Sstevel@tonic-gate 		gethrestime(&tod);
4247c478bd9Sstevel@tonic-gate 		now = gethrtime_waitfree();
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!SUNW-MSG-ID: %s, "
4287c478bd9Sstevel@tonic-gate 	    "TYPE: Error, VER: 1, SEVERITY: Major\n", fm_msgid);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	console_printf(
4317c478bd9Sstevel@tonic-gate "\n\rSUNW-MSG-ID: %s, TYPE: Error, VER: 1, SEVERITY: Major\n"
4327c478bd9Sstevel@tonic-gate "EVENT-TIME: 0x%lx.0x%lx (0x%llx)\n",
4337c478bd9Sstevel@tonic-gate 	    fm_msgid, tod.tv_sec, tod.tv_nsec, (u_longlong_t)now);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	console_printf(
4361cf6ec7eSsuha "PLATFORM: %s, CSN: -, HOSTNAME: %s\n"
4377c478bd9Sstevel@tonic-gate "SOURCE: %s, REV: %s %s\n",
4387c478bd9Sstevel@tonic-gate 	    platform, utsname.nodename, utsname.sysname,
4397c478bd9Sstevel@tonic-gate 	    utsname.release, utsname.version);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	console_printf(
4427c478bd9Sstevel@tonic-gate "DESC: Errors have been detected that require a reboot to ensure system\n"
4437c478bd9Sstevel@tonic-gate "integrity.  See %s/%s for more information.\n",
4447c478bd9Sstevel@tonic-gate 	    fm_url, fm_msgid);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	console_printf(
4477c478bd9Sstevel@tonic-gate "AUTO-RESPONSE: Solaris will attempt to save and diagnose the error telemetry\n"
4487c478bd9Sstevel@tonic-gate "IMPACT: The system will sync files, save a crash dump if needed, and reboot\n"
4497c478bd9Sstevel@tonic-gate "REC-ACTION: Save the error summary below in case telemetry cannot be saved\n");
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	console_printf("\n");
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Utility function to write all of the pending ereports to the dump device.
4567c478bd9Sstevel@tonic-gate  * This function is called at either normal reboot or panic time, and simply
4577c478bd9Sstevel@tonic-gate  * iterates over the in-transit messages in the ereport sysevent channel.
4587c478bd9Sstevel@tonic-gate  */
4597c478bd9Sstevel@tonic-gate void
fm_ereport_dump(void)4607c478bd9Sstevel@tonic-gate fm_ereport_dump(void)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	evchanq_t *chq;
4637c478bd9Sstevel@tonic-gate 	sysevent_t *sep;
4647c478bd9Sstevel@tonic-gate 	erpt_dump_t ed;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	timespec_t tod;
4677c478bd9Sstevel@tonic-gate 	hrtime_t now;
4687c478bd9Sstevel@tonic-gate 	char *buf;
4697c478bd9Sstevel@tonic-gate 	size_t len;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (panicstr) {
4727c478bd9Sstevel@tonic-gate 		tod = panic_hrestime;
4737c478bd9Sstevel@tonic-gate 		now = panic_hrtime;
4747c478bd9Sstevel@tonic-gate 	} else {
4757c478bd9Sstevel@tonic-gate 		if (ereport_errorq != NULL)
4767c478bd9Sstevel@tonic-gate 			errorq_drain(ereport_errorq);
4777c478bd9Sstevel@tonic-gate 		gethrestime(&tod);
4787c478bd9Sstevel@tonic-gate 		now = gethrtime_waitfree();
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * In the panic case, sysevent_evc_walk_init() will return NULL.
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	if ((chq = sysevent_evc_walk_init(ereport_chan, NULL)) == NULL &&
4857c478bd9Sstevel@tonic-gate 	    !panicstr)
4867c478bd9Sstevel@tonic-gate 		return; /* event channel isn't initialized yet */
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	while ((sep = sysevent_evc_walk_step(chq)) != NULL) {
4897c478bd9Sstevel@tonic-gate 		if ((buf = sysevent_evc_event_attr(sep, &len)) == NULL)
4907c478bd9Sstevel@tonic-gate 			break;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		ed.ed_magic = ERPT_MAGIC;
4937c478bd9Sstevel@tonic-gate 		ed.ed_chksum = checksum32(buf, len);
4947c478bd9Sstevel@tonic-gate 		ed.ed_size = (uint32_t)len;
4957c478bd9Sstevel@tonic-gate 		ed.ed_pad = 0;
4967c478bd9Sstevel@tonic-gate 		ed.ed_hrt_nsec = SE_TIME(sep);
4977c478bd9Sstevel@tonic-gate 		ed.ed_hrt_base = now;
4987c478bd9Sstevel@tonic-gate 		ed.ed_tod_base.sec = tod.tv_sec;
4997c478bd9Sstevel@tonic-gate 		ed.ed_tod_base.nsec = tod.tv_nsec;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		dumpvp_write(&ed, sizeof (ed));
5027c478bd9Sstevel@tonic-gate 		dumpvp_write(buf, len);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	sysevent_evc_walk_fini(chq);
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * Post an error report (ereport) to the sysevent error channel.  The error
5107c478bd9Sstevel@tonic-gate  * channel must be established with a prior call to sysevent_evc_create()
5117c478bd9Sstevel@tonic-gate  * before publication may occur.
5127c478bd9Sstevel@tonic-gate  */
5137c478bd9Sstevel@tonic-gate void
fm_ereport_post(nvlist_t * ereport,int evc_flag)5147c478bd9Sstevel@tonic-gate fm_ereport_post(nvlist_t *ereport, int evc_flag)
5157c478bd9Sstevel@tonic-gate {
5167c478bd9Sstevel@tonic-gate 	size_t nvl_size = 0;
5177c478bd9Sstevel@tonic-gate 	evchan_t *error_chan;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	(void) nvlist_size(ereport, &nvl_size, NV_ENCODE_NATIVE);
5207c478bd9Sstevel@tonic-gate 	if (nvl_size > ERPT_DATA_SZ || nvl_size == 0) {
5211a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_dropped.value.ui64);
5227c478bd9Sstevel@tonic-gate 		return;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (sysevent_evc_bind(FM_ERROR_CHAN, &error_chan,
5267c478bd9Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
5271a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_dropped.value.ui64);
5287c478bd9Sstevel@tonic-gate 		return;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (sysevent_evc_publish(error_chan, EC_FM, ESC_FM_ERROR,
5327c478bd9Sstevel@tonic-gate 	    SUNW_VENDOR, FM_PUB, ereport, evc_flag) != 0) {
5331a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_dropped.value.ui64);
53449b225e1SGavin Maltby 		(void) sysevent_evc_unbind(error_chan);
5357c478bd9Sstevel@tonic-gate 		return;
5367c478bd9Sstevel@tonic-gate 	}
53749b225e1SGavin Maltby 	(void) sysevent_evc_unbind(error_chan);
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate  * Wrapppers for FM nvlist allocators
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate /* ARGSUSED */
5447c478bd9Sstevel@tonic-gate static void *
i_fm_alloc(nv_alloc_t * nva,size_t size)5457c478bd9Sstevel@tonic-gate i_fm_alloc(nv_alloc_t *nva, size_t size)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	return (kmem_zalloc(size, KM_SLEEP));
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /* ARGSUSED */
5517c478bd9Sstevel@tonic-gate static void
i_fm_free(nv_alloc_t * nva,void * buf,size_t size)5527c478bd9Sstevel@tonic-gate i_fm_free(nv_alloc_t *nva, void *buf, size_t size)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	kmem_free(buf, size);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate const nv_alloc_ops_t fm_mem_alloc_ops = {
5587c478bd9Sstevel@tonic-gate 	NULL,
5597c478bd9Sstevel@tonic-gate 	NULL,
5607c478bd9Sstevel@tonic-gate 	i_fm_alloc,
5617c478bd9Sstevel@tonic-gate 	i_fm_free,
5627c478bd9Sstevel@tonic-gate 	NULL
5637c478bd9Sstevel@tonic-gate };
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Create and initialize a new nv_alloc_t for a fixed buffer, buf.  A pointer
5677c478bd9Sstevel@tonic-gate  * to the newly allocated nv_alloc_t structure is returned upon success or NULL
5687c478bd9Sstevel@tonic-gate  * is returned to indicate that the nv_alloc structure could not be created.
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate nv_alloc_t *
fm_nva_xcreate(char * buf,size_t bufsz)5717c478bd9Sstevel@tonic-gate fm_nva_xcreate(char *buf, size_t bufsz)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate 	nv_alloc_t *nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	if (bufsz == 0 || nv_alloc_init(nvhdl, nv_fixed_ops, buf, bufsz) != 0) {
5767c478bd9Sstevel@tonic-gate 		kmem_free(nvhdl, sizeof (nv_alloc_t));
5777c478bd9Sstevel@tonic-gate 		return (NULL);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	return (nvhdl);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Destroy a previously allocated nv_alloc structure.  The fixed buffer
5857c478bd9Sstevel@tonic-gate  * associated with nva must be freed by the caller.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate void
fm_nva_xdestroy(nv_alloc_t * nva)5887c478bd9Sstevel@tonic-gate fm_nva_xdestroy(nv_alloc_t *nva)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	nv_alloc_fini(nva);
5917c478bd9Sstevel@tonic-gate 	kmem_free(nva, sizeof (nv_alloc_t));
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate  * Create a new nv list.  A pointer to a new nv list structure is returned
5967c478bd9Sstevel@tonic-gate  * upon success or NULL is returned to indicate that the structure could
5977c478bd9Sstevel@tonic-gate  * not be created.  The newly created nv list is created and managed by the
5987c478bd9Sstevel@tonic-gate  * operations installed in nva.   If nva is NULL, the default FMA nva
5997c478bd9Sstevel@tonic-gate  * operations are installed and used.
6007c478bd9Sstevel@tonic-gate  *
6017c478bd9Sstevel@tonic-gate  * When called from the kernel and nva == NULL, this function must be called
6027c478bd9Sstevel@tonic-gate  * from passive kernel context with no locks held that can prevent a
6037c478bd9Sstevel@tonic-gate  * sleeping memory allocation from occurring.  Otherwise, this function may
6047c478bd9Sstevel@tonic-gate  * be called from other kernel contexts as long a valid nva created via
6057c478bd9Sstevel@tonic-gate  * fm_nva_create() is supplied.
6067c478bd9Sstevel@tonic-gate  */
6077c478bd9Sstevel@tonic-gate nvlist_t *
fm_nvlist_create(nv_alloc_t * nva)6087c478bd9Sstevel@tonic-gate fm_nvlist_create(nv_alloc_t *nva)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	int hdl_alloced = 0;
6117c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
6127c478bd9Sstevel@tonic-gate 	nv_alloc_t *nvhdl;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	if (nva == NULL) {
6157c478bd9Sstevel@tonic-gate 		nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		if (nv_alloc_init(nvhdl, &fm_mem_alloc_ops, NULL, 0) != 0) {
6187c478bd9Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
6197c478bd9Sstevel@tonic-gate 			return (NULL);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		hdl_alloced = 1;
6227c478bd9Sstevel@tonic-gate 	} else {
6237c478bd9Sstevel@tonic-gate 		nvhdl = nva;
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nvhdl) != 0) {
6277c478bd9Sstevel@tonic-gate 		if (hdl_alloced) {
6287c478bd9Sstevel@tonic-gate 			nv_alloc_fini(nvhdl);
62972b70389SJakub Jermar 			kmem_free(nvhdl, sizeof (nv_alloc_t));
6307c478bd9Sstevel@tonic-gate 		}
6317c478bd9Sstevel@tonic-gate 		return (NULL);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	return (nvl);
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * Destroy a previously allocated nvlist structure.  flag indicates whether
6397c478bd9Sstevel@tonic-gate  * or not the associated nva structure should be freed (FM_NVA_FREE) or
6407c478bd9Sstevel@tonic-gate  * retained (FM_NVA_RETAIN).  Retaining the nv alloc structure allows
6417c478bd9Sstevel@tonic-gate  * it to be re-used for future nvlist creation operations.
6427c478bd9Sstevel@tonic-gate  */
6437c478bd9Sstevel@tonic-gate void
fm_nvlist_destroy(nvlist_t * nvl,int flag)6447c478bd9Sstevel@tonic-gate fm_nvlist_destroy(nvlist_t *nvl, int flag)
6457c478bd9Sstevel@tonic-gate {
6467aec1d6eScindi 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
6497c478bd9Sstevel@tonic-gate 
6507aec1d6eScindi 	if (nva != NULL) {
6517c478bd9Sstevel@tonic-gate 		if (flag == FM_NVA_FREE)
6527aec1d6eScindi 			fm_nva_xdestroy(nva);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate int
i_fm_payload_set(nvlist_t * payload,const char * name,va_list ap)6577c478bd9Sstevel@tonic-gate i_fm_payload_set(nvlist_t *payload, const char *name, va_list ap)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	int nelem, ret = 0;
6607c478bd9Sstevel@tonic-gate 	data_type_t type;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	while (ret == 0 && name != NULL) {
6637c478bd9Sstevel@tonic-gate 		type = va_arg(ap, data_type_t);
6647c478bd9Sstevel@tonic-gate 		switch (type) {
6657c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
6667c478bd9Sstevel@tonic-gate 			ret = nvlist_add_byte(payload, name,
6677c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6687c478bd9Sstevel@tonic-gate 			break;
6697c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
6707c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6717c478bd9Sstevel@tonic-gate 			ret = nvlist_add_byte_array(payload, name,
6727c478bd9Sstevel@tonic-gate 			    va_arg(ap, uchar_t *), nelem);
6737c478bd9Sstevel@tonic-gate 			break;
6747c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
6757c478bd9Sstevel@tonic-gate 			ret = nvlist_add_boolean_value(payload, name,
6767c478bd9Sstevel@tonic-gate 			    va_arg(ap, boolean_t));
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
6797c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6807c478bd9Sstevel@tonic-gate 			ret = nvlist_add_boolean_array(payload, name,
6817c478bd9Sstevel@tonic-gate 			    va_arg(ap, boolean_t *), nelem);
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8:
6847c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int8(payload, name,
6857c478bd9Sstevel@tonic-gate 			    va_arg(ap, int));
6867c478bd9Sstevel@tonic-gate 			break;
6877c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
6887c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6897c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int8_array(payload, name,
6907c478bd9Sstevel@tonic-gate 			    va_arg(ap, int8_t *), nelem);
6917c478bd9Sstevel@tonic-gate 			break;
6927c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
6937c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint8(payload, name,
6947c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6957c478bd9Sstevel@tonic-gate 			break;
6967c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
6977c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6987c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint8_array(payload, name,
6997c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint8_t *), nelem);
7007c478bd9Sstevel@tonic-gate 			break;
7017c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16:
7027c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int16(payload, name,
7037c478bd9Sstevel@tonic-gate 			    va_arg(ap, int));
7047c478bd9Sstevel@tonic-gate 			break;
7057c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
7067c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7077c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int16_array(payload, name,
7087c478bd9Sstevel@tonic-gate 			    va_arg(ap, int16_t *), nelem);
7097c478bd9Sstevel@tonic-gate 			break;
7107c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
7117c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint16(payload, name,
7127c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint_t));
7137c478bd9Sstevel@tonic-gate 			break;
7147c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
7157c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7167c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint16_array(payload, name,
7177c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint16_t *), nelem);
7187c478bd9Sstevel@tonic-gate 			break;
7197c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32:
7207c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int32(payload, name,
7217c478bd9Sstevel@tonic-gate 			    va_arg(ap, int32_t));
7227c478bd9Sstevel@tonic-gate 			break;
7237c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
7247c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7257c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int32_array(payload, name,
7267c478bd9Sstevel@tonic-gate 			    va_arg(ap, int32_t *), nelem);
7277c478bd9Sstevel@tonic-gate 			break;
7287c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
7297c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint32(payload, name,
7307c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint32_t));
7317c478bd9Sstevel@tonic-gate 			break;
7327c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
7337c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7347c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint32_array(payload, name,
7357c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint32_t *), nelem);
7367c478bd9Sstevel@tonic-gate 			break;
7377c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64:
7387c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int64(payload, name,
7397c478bd9Sstevel@tonic-gate 			    va_arg(ap, int64_t));
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
7427c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7437c478bd9Sstevel@tonic-gate 			ret = nvlist_add_int64_array(payload, name,
7447c478bd9Sstevel@tonic-gate 			    va_arg(ap, int64_t *), nelem);
7457c478bd9Sstevel@tonic-gate 			break;
7467c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
7477c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint64(payload, name,
7487c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint64_t));
7497c478bd9Sstevel@tonic-gate 			break;
7507c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
7517c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7527c478bd9Sstevel@tonic-gate 			ret = nvlist_add_uint64_array(payload, name,
7537c478bd9Sstevel@tonic-gate 			    va_arg(ap, uint64_t *), nelem);
7547c478bd9Sstevel@tonic-gate 			break;
7557c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING:
7567c478bd9Sstevel@tonic-gate 			ret = nvlist_add_string(payload, name,
7577c478bd9Sstevel@tonic-gate 			    va_arg(ap, char *));
7587c478bd9Sstevel@tonic-gate 			break;
7597c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
7607c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7617c478bd9Sstevel@tonic-gate 			ret = nvlist_add_string_array(payload, name,
7627c478bd9Sstevel@tonic-gate 			    va_arg(ap, char **), nelem);
7637c478bd9Sstevel@tonic-gate 			break;
7647c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
7657c478bd9Sstevel@tonic-gate 			ret = nvlist_add_nvlist(payload, name,
7667c478bd9Sstevel@tonic-gate 			    va_arg(ap, nvlist_t *));
7677c478bd9Sstevel@tonic-gate 			break;
7687c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
7697c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7707c478bd9Sstevel@tonic-gate 			ret = nvlist_add_nvlist_array(payload, name,
7717c478bd9Sstevel@tonic-gate 			    va_arg(ap, nvlist_t **), nelem);
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		default:
7747c478bd9Sstevel@tonic-gate 			ret = EINVAL;
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		name = va_arg(ap, char *);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	return (ret);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate void
fm_payload_set(nvlist_t * payload,...)7837c478bd9Sstevel@tonic-gate fm_payload_set(nvlist_t *payload, ...)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	int ret;
7867c478bd9Sstevel@tonic-gate 	const char *name;
7877c478bd9Sstevel@tonic-gate 	va_list ap;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	va_start(ap, payload);
7907c478bd9Sstevel@tonic-gate 	name = va_arg(ap, char *);
7917c478bd9Sstevel@tonic-gate 	ret = i_fm_payload_set(payload, name, ap);
7927c478bd9Sstevel@tonic-gate 	va_end(ap);
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (ret)
7951a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.payload_set_failed.value.ui64);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate /*
7997c478bd9Sstevel@tonic-gate  * Set-up and validate the members of an ereport event according to:
8007c478bd9Sstevel@tonic-gate  *
8017c478bd9Sstevel@tonic-gate  *	Member name		Type		Value
8027c478bd9Sstevel@tonic-gate  *	====================================================
8037c478bd9Sstevel@tonic-gate  *	class			string		ereport
8047c478bd9Sstevel@tonic-gate  *	version			uint8_t		0
8057c478bd9Sstevel@tonic-gate  *	ena			uint64_t	<ena>
8067c478bd9Sstevel@tonic-gate  *	detector		nvlist_t	<detector>
8077c478bd9Sstevel@tonic-gate  *	ereport-payload		nvlist_t	<var args>
8087c478bd9Sstevel@tonic-gate  *
809392e836bSGavin Maltby  * We don't actually add a 'version' member to the payload.  Really,
810392e836bSGavin Maltby  * the version quoted to us by our caller is that of the category 1
811392e836bSGavin Maltby  * "ereport" event class (and we require FM_EREPORT_VERS0) but
812392e836bSGavin Maltby  * the payload version of the actual leaf class event under construction
813392e836bSGavin Maltby  * may be something else.  Callers should supply a version in the varargs,
814392e836bSGavin Maltby  * or (better) we could take two version arguments - one for the
815392e836bSGavin Maltby  * ereport category 1 classification (expect FM_EREPORT_VERS0) and one
816392e836bSGavin Maltby  * for the leaf class.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate void
fm_ereport_set(nvlist_t * ereport,int version,const char * erpt_class,uint64_t ena,const nvlist_t * detector,...)8197c478bd9Sstevel@tonic-gate fm_ereport_set(nvlist_t *ereport, int version, const char *erpt_class,
8207c478bd9Sstevel@tonic-gate     uint64_t ena, const nvlist_t *detector, ...)
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	char ereport_class[FM_MAX_CLASS];
8237c478bd9Sstevel@tonic-gate 	const char *name;
8247c478bd9Sstevel@tonic-gate 	va_list ap;
8257c478bd9Sstevel@tonic-gate 	int ret;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (version != FM_EREPORT_VERS0) {
8281a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_set_failed.value.ui64);
8297c478bd9Sstevel@tonic-gate 		return;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	(void) snprintf(ereport_class, FM_MAX_CLASS, "%s.%s",
8337c478bd9Sstevel@tonic-gate 	    FM_EREPORT_CLASS, erpt_class);
8347c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(ereport, FM_CLASS, ereport_class) != 0) {
8351a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_set_failed.value.ui64);
8367c478bd9Sstevel@tonic-gate 		return;
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint64(ereport, FM_EREPORT_ENA, ena)) {
8401a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_set_failed.value.ui64);
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (nvlist_add_nvlist(ereport, FM_EREPORT_DETECTOR,
8447c478bd9Sstevel@tonic-gate 	    (nvlist_t *)detector) != 0) {
8451a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_set_failed.value.ui64);
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	va_start(ap, detector);
8497c478bd9Sstevel@tonic-gate 	name = va_arg(ap, const char *);
8507c478bd9Sstevel@tonic-gate 	ret = i_fm_payload_set(ereport, name, ap);
8517c478bd9Sstevel@tonic-gate 	va_end(ap);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if (ret)
8541a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.erpt_set_failed.value.ui64);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577aec1d6eScindi /*
8587aec1d6eScindi  * Set-up and validate the members of an hc fmri according to;
8597aec1d6eScindi  *
8607aec1d6eScindi  *	Member name		Type		Value
8617aec1d6eScindi  *	===================================================
8627aec1d6eScindi  *	version			uint8_t		0
8637aec1d6eScindi  *	auth			nvlist_t	<auth>
8647aec1d6eScindi  *	hc-name			string		<name>
8657aec1d6eScindi  *	hc-id			string		<id>
8667aec1d6eScindi  *
8677aec1d6eScindi  * Note that auth and hc-id are optional members.
8687aec1d6eScindi  */
8697aec1d6eScindi 
8707aec1d6eScindi #define	HC_MAXPAIRS	20
8717aec1d6eScindi #define	HC_MAXNAMELEN	50
8727aec1d6eScindi 
8737c478bd9Sstevel@tonic-gate static int
fm_fmri_hc_set_common(nvlist_t * fmri,int version,const nvlist_t * auth)8747aec1d6eScindi fm_fmri_hc_set_common(nvlist_t *fmri, int version, const nvlist_t *auth)
8757c478bd9Sstevel@tonic-gate {
8767aec1d6eScindi 	if (version != FM_HC_SCHEME_VERSION) {
8771a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
8787aec1d6eScindi 		return (0);
8797aec1d6eScindi 	}
8807c478bd9Sstevel@tonic-gate 
8817aec1d6eScindi 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0 ||
8827aec1d6eScindi 	    nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC) != 0) {
8831a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
8847c478bd9Sstevel@tonic-gate 		return (0);
8857aec1d6eScindi 	}
8867c478bd9Sstevel@tonic-gate 
8877aec1d6eScindi 	if (auth != NULL && nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
8887aec1d6eScindi 	    (nvlist_t *)auth) != 0) {
8891a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
8907aec1d6eScindi 		return (0);
8917aec1d6eScindi 	}
8927c478bd9Sstevel@tonic-gate 
8937aec1d6eScindi 	return (1);
8947aec1d6eScindi }
8957aec1d6eScindi 
8967aec1d6eScindi void
fm_fmri_hc_set(nvlist_t * fmri,int version,const nvlist_t * auth,nvlist_t * snvl,int npairs,...)8977aec1d6eScindi fm_fmri_hc_set(nvlist_t *fmri, int version, const nvlist_t *auth,
8987aec1d6eScindi     nvlist_t *snvl, int npairs, ...)
8997aec1d6eScindi {
9007aec1d6eScindi 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
9017aec1d6eScindi 	nvlist_t *pairs[HC_MAXPAIRS];
9027aec1d6eScindi 	va_list ap;
9037aec1d6eScindi 	int i;
9047aec1d6eScindi 
9057aec1d6eScindi 	if (!fm_fmri_hc_set_common(fmri, version, auth))
9067aec1d6eScindi 		return;
9077aec1d6eScindi 
9087aec1d6eScindi 	npairs = MIN(npairs, HC_MAXPAIRS);
9097aec1d6eScindi 
9107aec1d6eScindi 	va_start(ap, npairs);
9117aec1d6eScindi 	for (i = 0; i < npairs; i++) {
9127aec1d6eScindi 		const char *name = va_arg(ap, const char *);
9137aec1d6eScindi 		uint32_t id = va_arg(ap, uint32_t);
9147aec1d6eScindi 		char idstr[11];
9157aec1d6eScindi 
9167aec1d6eScindi 		(void) snprintf(idstr, sizeof (idstr), "%u", id);
9177aec1d6eScindi 
9187aec1d6eScindi 		pairs[i] = fm_nvlist_create(nva);
9197aec1d6eScindi 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
9207aec1d6eScindi 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
9211a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
9221a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 	}
9257aec1d6eScindi 	va_end(ap);
9267c478bd9Sstevel@tonic-gate 
9277aec1d6eScindi 	if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs, npairs) != 0)
9281a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
9297c478bd9Sstevel@tonic-gate 
9307aec1d6eScindi 	for (i = 0; i < npairs; i++)
9317aec1d6eScindi 		fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
9327c478bd9Sstevel@tonic-gate 
9337aec1d6eScindi 	if (snvl != NULL) {
9347aec1d6eScindi 		if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
9351a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
9361a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
9377aec1d6eScindi 		}
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate  * Set-up and validate the members of an dev fmri according to:
9437c478bd9Sstevel@tonic-gate  *
9447c478bd9Sstevel@tonic-gate  *	Member name		Type		Value
9457c478bd9Sstevel@tonic-gate  *	====================================================
9467c478bd9Sstevel@tonic-gate  *	version			uint8_t		0
9477c478bd9Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
9487c478bd9Sstevel@tonic-gate  *	devpath			string		<devpath>
949392e836bSGavin Maltby  *	[devid]			string		<devid>
950392e836bSGavin Maltby  *	[target-port-l0id]	string		<target-port-lun0-id>
9517c478bd9Sstevel@tonic-gate  *
9527c478bd9Sstevel@tonic-gate  * Note that auth and devid are optional members.
9537c478bd9Sstevel@tonic-gate  */
9547c478bd9Sstevel@tonic-gate void
fm_fmri_dev_set(nvlist_t * fmri_dev,int version,const nvlist_t * auth,const char * devpath,const char * devid,const char * tpl0)9557c478bd9Sstevel@tonic-gate fm_fmri_dev_set(nvlist_t *fmri_dev, int version, const nvlist_t *auth,
956392e836bSGavin Maltby     const char *devpath, const char *devid, const char *tpl0)
9577c478bd9Sstevel@tonic-gate {
958392e836bSGavin Maltby 	int err = 0;
9597c478bd9Sstevel@tonic-gate 
960392e836bSGavin Maltby 	if (version != DEV_SCHEME_VERSION0) {
9611a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
9627c478bd9Sstevel@tonic-gate 		return;
9637c478bd9Sstevel@tonic-gate 	}
9647c478bd9Sstevel@tonic-gate 
965392e836bSGavin Maltby 	err |= nvlist_add_uint8(fmri_dev, FM_VERSION, version);
966392e836bSGavin Maltby 	err |= nvlist_add_string(fmri_dev, FM_FMRI_SCHEME, FM_FMRI_SCHEME_DEV);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	if (auth != NULL) {
969392e836bSGavin Maltby 		err |= nvlist_add_nvlist(fmri_dev, FM_FMRI_AUTHORITY,
970392e836bSGavin Maltby 		    (nvlist_t *)auth);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 
973392e836bSGavin Maltby 	err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_PATH, devpath);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	if (devid != NULL)
976392e836bSGavin Maltby 		err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_ID, devid);
977392e836bSGavin Maltby 
978392e836bSGavin Maltby 	if (tpl0 != NULL)
979392e836bSGavin Maltby 		err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_TGTPTLUN0, tpl0);
980392e836bSGavin Maltby 
981392e836bSGavin Maltby 	if (err)
9821a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
983392e836bSGavin Maltby 
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate /*
9877c478bd9Sstevel@tonic-gate  * Set-up and validate the members of an cpu fmri according to:
9887c478bd9Sstevel@tonic-gate  *
9897c478bd9Sstevel@tonic-gate  *	Member name		Type		Value
9907c478bd9Sstevel@tonic-gate  *	====================================================
9917c478bd9Sstevel@tonic-gate  *	version			uint8_t		0
9927c478bd9Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
9937c478bd9Sstevel@tonic-gate  *	cpuid			uint32_t	<cpu_id>
9947c478bd9Sstevel@tonic-gate  *	cpumask			uint8_t		<cpu_mask>
9957c478bd9Sstevel@tonic-gate  *	serial			uint64_t	<serial_id>
9967c478bd9Sstevel@tonic-gate  *
9977aec1d6eScindi  * Note that auth, cpumask, serial are optional members.
9987c478bd9Sstevel@tonic-gate  *
9997c478bd9Sstevel@tonic-gate  */
10007c478bd9Sstevel@tonic-gate void
fm_fmri_cpu_set(nvlist_t * fmri_cpu,int version,const nvlist_t * auth,uint32_t cpu_id,uint8_t * cpu_maskp,const char * serial_idp)10017c478bd9Sstevel@tonic-gate fm_fmri_cpu_set(nvlist_t *fmri_cpu, int version, const nvlist_t *auth,
10027aec1d6eScindi     uint32_t cpu_id, uint8_t *cpu_maskp, const char *serial_idp)
10037c478bd9Sstevel@tonic-gate {
10047aec1d6eScindi 	uint64_t *failedp = &erpt_kstat_data.fmri_set_failed.value.ui64;
10057aec1d6eScindi 
10067aec1d6eScindi 	if (version < CPU_SCHEME_VERSION1) {
10071a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(failedp);
10087c478bd9Sstevel@tonic-gate 		return;
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_cpu, FM_VERSION, version) != 0) {
10121a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(failedp);
10137c478bd9Sstevel@tonic-gate 		return;
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(fmri_cpu, FM_FMRI_SCHEME,
10177c478bd9Sstevel@tonic-gate 	    FM_FMRI_SCHEME_CPU) != 0) {
10181a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(failedp);
10197c478bd9Sstevel@tonic-gate 		return;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227aec1d6eScindi 	if (auth != NULL && nvlist_add_nvlist(fmri_cpu, FM_FMRI_AUTHORITY,
10237aec1d6eScindi 	    (nvlist_t *)auth) != 0)
10241a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(failedp);
10257c478bd9Sstevel@tonic-gate 
10267aec1d6eScindi 	if (nvlist_add_uint32(fmri_cpu, FM_FMRI_CPU_ID, cpu_id) != 0)
10271a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(failedp);
10287c478bd9Sstevel@tonic-gate 
10297aec1d6eScindi 	if (cpu_maskp != NULL && nvlist_add_uint8(fmri_cpu, FM_FMRI_CPU_MASK,
10307aec1d6eScindi 	    *cpu_maskp) != 0)
10311a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(failedp);
10327c478bd9Sstevel@tonic-gate 
10337aec1d6eScindi 	if (serial_idp == NULL || nvlist_add_string(fmri_cpu,
10347aec1d6eScindi 	    FM_FMRI_CPU_SERIAL_ID, (char *)serial_idp) != 0)
10351a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(failedp);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate  * Set-up and validate the members of a mem according to:
10407c478bd9Sstevel@tonic-gate  *
10417c478bd9Sstevel@tonic-gate  *	Member name		Type		Value
10427c478bd9Sstevel@tonic-gate  *	====================================================
10437c478bd9Sstevel@tonic-gate  *	version			uint8_t		0
10447c478bd9Sstevel@tonic-gate  *	auth			nvlist_t	<auth>		[optional]
10457c478bd9Sstevel@tonic-gate  *	unum			string		<unum>
1046d00f0155Sayznaga  *	serial			string		<serial>	[optional*]
1047d00f0155Sayznaga  *	offset			uint64_t	<offset>	[optional]
10487c478bd9Sstevel@tonic-gate  *
1049d00f0155Sayznaga  *	* serial is required if offset is present
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate void
fm_fmri_mem_set(nvlist_t * fmri,int version,const nvlist_t * auth,const char * unum,const char * serial,uint64_t offset)10527c478bd9Sstevel@tonic-gate fm_fmri_mem_set(nvlist_t *fmri, int version, const nvlist_t *auth,
1053d00f0155Sayznaga     const char *unum, const char *serial, uint64_t offset)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	if (version != MEM_SCHEME_VERSION0) {
10561a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
10577c478bd9Sstevel@tonic-gate 		return;
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
1060d00f0155Sayznaga 	if (!serial && (offset != (uint64_t)-1)) {
10611a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1062d00f0155Sayznaga 		return;
1063d00f0155Sayznaga 	}
1064d00f0155Sayznaga 
10657c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
10661a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
10677c478bd9Sstevel@tonic-gate 		return;
10687c478bd9Sstevel@tonic-gate 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MEM) != 0) {
10711a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
10727c478bd9Sstevel@tonic-gate 		return;
10737c478bd9Sstevel@tonic-gate 	}
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (auth != NULL) {
10767c478bd9Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
10777c478bd9Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
10781a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
10791a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
10807c478bd9Sstevel@tonic-gate 		}
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_MEM_UNUM, unum) != 0) {
10841a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (serial != NULL) {
10887c478bd9Sstevel@tonic-gate 		if (nvlist_add_string_array(fmri, FM_FMRI_MEM_SERIAL_ID,
10897c478bd9Sstevel@tonic-gate 		    (char **)&serial, 1) != 0) {
10901a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
10911a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
10927c478bd9Sstevel@tonic-gate 		}
10931a5e258fSJosef 'Jeff' Sipek 		if (offset != (uint64_t)-1 && nvlist_add_uint64(fmri,
10941a5e258fSJosef 'Jeff' Sipek 		    FM_FMRI_MEM_OFFSET, offset) != 0) {
10951a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
10961a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1097d00f0155Sayznaga 		}
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
1101ea8dc4b6Seschrock void
fm_fmri_zfs_set(nvlist_t * fmri,int version,uint64_t pool_guid,uint64_t vdev_guid)1102ea8dc4b6Seschrock fm_fmri_zfs_set(nvlist_t *fmri, int version, uint64_t pool_guid,
1103ea8dc4b6Seschrock     uint64_t vdev_guid)
1104ea8dc4b6Seschrock {
1105ea8dc4b6Seschrock 	if (version != ZFS_SCHEME_VERSION0) {
11061a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1107ea8dc4b6Seschrock 		return;
1108ea8dc4b6Seschrock 	}
1109ea8dc4b6Seschrock 
1110ea8dc4b6Seschrock 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
11111a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1112ea8dc4b6Seschrock 		return;
1113ea8dc4b6Seschrock 	}
1114ea8dc4b6Seschrock 
1115ea8dc4b6Seschrock 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS) != 0) {
11161a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1117ea8dc4b6Seschrock 		return;
1118ea8dc4b6Seschrock 	}
1119ea8dc4b6Seschrock 
1120ea8dc4b6Seschrock 	if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_POOL, pool_guid) != 0) {
11211a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1122ea8dc4b6Seschrock 	}
1123ea8dc4b6Seschrock 
1124ea8dc4b6Seschrock 	if (vdev_guid != 0) {
1125ea8dc4b6Seschrock 		if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_VDEV, vdev_guid) != 0) {
11261a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
11271a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1128ea8dc4b6Seschrock 		}
1129ea8dc4b6Seschrock 	}
1130ea8dc4b6Seschrock }
1131ea8dc4b6Seschrock 
11327c478bd9Sstevel@tonic-gate uint64_t
fm_ena_increment(uint64_t ena)11337c478bd9Sstevel@tonic-gate fm_ena_increment(uint64_t ena)
11347c478bd9Sstevel@tonic-gate {
11357c478bd9Sstevel@tonic-gate 	uint64_t new_ena;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
11387c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT1:
11397c478bd9Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT1_GEN_SHFT);
11407c478bd9Sstevel@tonic-gate 		break;
11417c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT2:
11427c478bd9Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT2_GEN_SHFT);
11437c478bd9Sstevel@tonic-gate 		break;
11447c478bd9Sstevel@tonic-gate 	default:
11457c478bd9Sstevel@tonic-gate 		new_ena = 0;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	return (new_ena);
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate uint64_t
fm_ena_generate_cpu(uint64_t timestamp,processorid_t cpuid,uchar_t format)11527c478bd9Sstevel@tonic-gate fm_ena_generate_cpu(uint64_t timestamp, processorid_t cpuid, uchar_t format)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	uint64_t ena = 0;
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	switch (format) {
11577c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT1:
11587c478bd9Sstevel@tonic-gate 		if (timestamp) {
11597c478bd9Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11607c478bd9Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
11617c478bd9Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
11627c478bd9Sstevel@tonic-gate 			    ((timestamp << ENA_FMT1_TIME_SHFT) &
11637c478bd9Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
11647c478bd9Sstevel@tonic-gate 		} else {
11657c478bd9Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11667c478bd9Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
11677c478bd9Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
11687c478bd9Sstevel@tonic-gate 			    ((gethrtime_waitfree() << ENA_FMT1_TIME_SHFT) &
11697c478bd9Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
11707c478bd9Sstevel@tonic-gate 		}
11717c478bd9Sstevel@tonic-gate 		break;
11727c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT2:
11737c478bd9Sstevel@tonic-gate 		ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11747c478bd9Sstevel@tonic-gate 		    ((timestamp << ENA_FMT2_TIME_SHFT) & ENA_FMT2_TIME_MASK));
11757c478bd9Sstevel@tonic-gate 		break;
11767c478bd9Sstevel@tonic-gate 	default:
11777c478bd9Sstevel@tonic-gate 		break;
11787c478bd9Sstevel@tonic-gate 	}
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	return (ena);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate uint64_t
fm_ena_generate(uint64_t timestamp,uchar_t format)11847c478bd9Sstevel@tonic-gate fm_ena_generate(uint64_t timestamp, uchar_t format)
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate 	return (fm_ena_generate_cpu(timestamp, CPU->cpu_id, format));
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate uint64_t
fm_ena_generation_get(uint64_t ena)11907c478bd9Sstevel@tonic-gate fm_ena_generation_get(uint64_t ena)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate 	uint64_t gen;
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
11957c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT1:
11967c478bd9Sstevel@tonic-gate 		gen = (ena & ENA_FMT1_GEN_MASK) >> ENA_FMT1_GEN_SHFT;
11977c478bd9Sstevel@tonic-gate 		break;
11987c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT2:
11997c478bd9Sstevel@tonic-gate 		gen = (ena & ENA_FMT2_GEN_MASK) >> ENA_FMT2_GEN_SHFT;
12007c478bd9Sstevel@tonic-gate 		break;
12017c478bd9Sstevel@tonic-gate 	default:
12027c478bd9Sstevel@tonic-gate 		gen = 0;
12037c478bd9Sstevel@tonic-gate 		break;
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	return (gen);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate uchar_t
fm_ena_format_get(uint64_t ena)12107c478bd9Sstevel@tonic-gate fm_ena_format_get(uint64_t ena)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	return (ENA_FORMAT(ena));
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate uint64_t
fm_ena_id_get(uint64_t ena)12177c478bd9Sstevel@tonic-gate fm_ena_id_get(uint64_t ena)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	uint64_t id;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
12227c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT1:
12237c478bd9Sstevel@tonic-gate 		id = (ena & ENA_FMT1_ID_MASK) >> ENA_FMT1_ID_SHFT;
12247c478bd9Sstevel@tonic-gate 		break;
12257c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT2:
12267c478bd9Sstevel@tonic-gate 		id = (ena & ENA_FMT2_ID_MASK) >> ENA_FMT2_ID_SHFT;
12277c478bd9Sstevel@tonic-gate 		break;
12287c478bd9Sstevel@tonic-gate 	default:
12297c478bd9Sstevel@tonic-gate 		id = 0;
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	return (id);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate uint64_t
fm_ena_time_get(uint64_t ena)12367c478bd9Sstevel@tonic-gate fm_ena_time_get(uint64_t ena)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate 	uint64_t time;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
12417c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT1:
12427c478bd9Sstevel@tonic-gate 		time = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
12437c478bd9Sstevel@tonic-gate 		break;
12447c478bd9Sstevel@tonic-gate 	case FM_ENA_FMT2:
12457c478bd9Sstevel@tonic-gate 		time = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
12467c478bd9Sstevel@tonic-gate 		break;
12477c478bd9Sstevel@tonic-gate 	default:
12487c478bd9Sstevel@tonic-gate 		time = 0;
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	return (time);
12527c478bd9Sstevel@tonic-gate }
12537aec1d6eScindi 
12547aec1d6eScindi /*
12557aec1d6eScindi  * Convert a getpcstack() trace to symbolic name+offset, and add the resulting
12567aec1d6eScindi  * string array to a Fault Management ereport as FM_EREPORT_PAYLOAD_NAME_STACK.
12577aec1d6eScindi  */
12587aec1d6eScindi void
fm_payload_stack_add(nvlist_t * payload,const pc_t * stack,int depth)12597aec1d6eScindi fm_payload_stack_add(nvlist_t *payload, const pc_t *stack, int depth)
12607aec1d6eScindi {
12617aec1d6eScindi 	int i;
12627aec1d6eScindi 	char *sym;
12637aec1d6eScindi 	ulong_t off;
12647aec1d6eScindi 	char *stkpp[FM_STK_DEPTH];
12657aec1d6eScindi 	char buf[FM_STK_DEPTH * FM_SYM_SZ];
12667aec1d6eScindi 	char *stkp = buf;
12677aec1d6eScindi 
12687aec1d6eScindi 	for (i = 0; i < depth && i != FM_STK_DEPTH; i++, stkp += FM_SYM_SZ) {
12697aec1d6eScindi 		if ((sym = kobj_getsymname(stack[i], &off)) != NULL)
12707aec1d6eScindi 			(void) snprintf(stkp, FM_SYM_SZ, "%s+%lx", sym, off);
12717aec1d6eScindi 		else
12727aec1d6eScindi 			(void) snprintf(stkp, FM_SYM_SZ, "%lx", (long)stack[i]);
12737aec1d6eScindi 		stkpp[i] = stkp;
12747aec1d6eScindi 	}
12757aec1d6eScindi 
12767aec1d6eScindi 	fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_STACK,
12773ddd2818Sdilpreet 	    DATA_TYPE_STRING_ARRAY, depth, stkpp, NULL);
12787aec1d6eScindi }
1279d6c90996SAbhinandan Ekande 
1280d6c90996SAbhinandan Ekande void
print_msg_hwerr(ctid_t ct_id,proc_t * p)1281d6c90996SAbhinandan Ekande print_msg_hwerr(ctid_t ct_id, proc_t *p)
1282d6c90996SAbhinandan Ekande {
1283d6c90996SAbhinandan Ekande 	uprintf("Killed process %d (%s) in contract id %d "
1284d6c90996SAbhinandan Ekande 	    "due to hardware error\n", p->p_pid, p->p_user.u_comm, ct_id);
1285d6c90996SAbhinandan Ekande }
1286074bb90dSTom Pothier 
1287074bb90dSTom Pothier void
fm_fmri_hc_create(nvlist_t * fmri,int version,const nvlist_t * auth,nvlist_t * snvl,nvlist_t * bboard,int npairs,...)1288074bb90dSTom Pothier fm_fmri_hc_create(nvlist_t *fmri, int version, const nvlist_t *auth,
1289074bb90dSTom Pothier     nvlist_t *snvl, nvlist_t *bboard, int npairs, ...)
1290074bb90dSTom Pothier {
1291074bb90dSTom Pothier 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
1292074bb90dSTom Pothier 	nvlist_t *pairs[HC_MAXPAIRS];
1293074bb90dSTom Pothier 	nvlist_t **hcl;
1294074bb90dSTom Pothier 	uint_t n;
1295074bb90dSTom Pothier 	int i, j;
1296074bb90dSTom Pothier 	va_list ap;
1297074bb90dSTom Pothier 	char *hcname, *hcid;
1298074bb90dSTom Pothier 
1299074bb90dSTom Pothier 	if (!fm_fmri_hc_set_common(fmri, version, auth))
1300074bb90dSTom Pothier 		return;
1301074bb90dSTom Pothier 
1302074bb90dSTom Pothier 	/*
1303074bb90dSTom Pothier 	 * copy the bboard nvpairs to the pairs array
1304074bb90dSTom Pothier 	 */
1305074bb90dSTom Pothier 	if (nvlist_lookup_nvlist_array(bboard, FM_FMRI_HC_LIST, &hcl, &n)
1306074bb90dSTom Pothier 	    != 0) {
13071a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1308074bb90dSTom Pothier 		return;
1309074bb90dSTom Pothier 	}
1310074bb90dSTom Pothier 
1311074bb90dSTom Pothier 	for (i = 0; i < n; i++) {
1312074bb90dSTom Pothier 		if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME,
1313074bb90dSTom Pothier 		    &hcname) != 0) {
13141a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
13151a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1316074bb90dSTom Pothier 			return;
1317074bb90dSTom Pothier 		}
1318074bb90dSTom Pothier 		if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &hcid) != 0) {
13191a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
13201a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1321074bb90dSTom Pothier 			return;
1322074bb90dSTom Pothier 		}
1323074bb90dSTom Pothier 
1324074bb90dSTom Pothier 		pairs[i] = fm_nvlist_create(nva);
1325074bb90dSTom Pothier 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, hcname) != 0 ||
1326074bb90dSTom Pothier 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, hcid) != 0) {
1327074bb90dSTom Pothier 			for (j = 0; j <= i; j++) {
1328074bb90dSTom Pothier 				if (pairs[j] != NULL)
1329074bb90dSTom Pothier 					fm_nvlist_destroy(pairs[j],
1330074bb90dSTom Pothier 					    FM_NVA_RETAIN);
1331074bb90dSTom Pothier 			}
13321a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
13331a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1334074bb90dSTom Pothier 			return;
1335074bb90dSTom Pothier 		}
1336074bb90dSTom Pothier 	}
1337074bb90dSTom Pothier 
1338074bb90dSTom Pothier 	/*
1339074bb90dSTom Pothier 	 * create the pairs from passed in pairs
1340074bb90dSTom Pothier 	 */
1341074bb90dSTom Pothier 	npairs = MIN(npairs, HC_MAXPAIRS);
1342074bb90dSTom Pothier 
1343074bb90dSTom Pothier 	va_start(ap, npairs);
1344074bb90dSTom Pothier 	for (i = n; i < npairs + n; i++) {
1345074bb90dSTom Pothier 		const char *name = va_arg(ap, const char *);
1346074bb90dSTom Pothier 		uint32_t id = va_arg(ap, uint32_t);
1347074bb90dSTom Pothier 		char idstr[11];
1348074bb90dSTom Pothier 		(void) snprintf(idstr, sizeof (idstr), "%u", id);
1349074bb90dSTom Pothier 		pairs[i] = fm_nvlist_create(nva);
1350074bb90dSTom Pothier 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
1351074bb90dSTom Pothier 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
1352074bb90dSTom Pothier 			for (j = 0; j <= i; j++) {
1353074bb90dSTom Pothier 				if (pairs[j] != NULL)
1354074bb90dSTom Pothier 					fm_nvlist_destroy(pairs[j],
1355074bb90dSTom Pothier 					    FM_NVA_RETAIN);
1356074bb90dSTom Pothier 			}
13571a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
13581a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1359074bb90dSTom Pothier 			return;
1360074bb90dSTom Pothier 		}
1361074bb90dSTom Pothier 	}
1362074bb90dSTom Pothier 	va_end(ap);
1363074bb90dSTom Pothier 
1364074bb90dSTom Pothier 	/*
1365074bb90dSTom Pothier 	 * Create the fmri hc list
1366074bb90dSTom Pothier 	 */
1367074bb90dSTom Pothier 	if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs,
1368074bb90dSTom Pothier 	    npairs + n) != 0) {
13691a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&erpt_kstat_data.fmri_set_failed.value.ui64);
1370074bb90dSTom Pothier 		return;
1371074bb90dSTom Pothier 	}
1372074bb90dSTom Pothier 
1373074bb90dSTom Pothier 	for (i = 0; i < npairs + n; i++) {
1374074bb90dSTom Pothier 			fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
1375074bb90dSTom Pothier 	}
1376074bb90dSTom Pothier 
1377074bb90dSTom Pothier 	if (snvl != NULL) {
1378074bb90dSTom Pothier 		if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
13791a5e258fSJosef 'Jeff' Sipek 			atomic_inc_64(
13801a5e258fSJosef 'Jeff' Sipek 			    &erpt_kstat_data.fmri_set_failed.value.ui64);
1381074bb90dSTom Pothier 			return;
1382074bb90dSTom Pothier 		}
1383074bb90dSTom Pothier 	}
1384074bb90dSTom Pothier }
1385