xref: /onnv-gate/usr/src/uts/common/os/fm.c (revision 10664:e29b5b8a591c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229613SAbhinandan.Ekande@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Fault Management Architecture (FMA) Resource and Protocol Support
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * The routines contained herein provide services to support kernel subsystems
300Sstevel@tonic-gate  * in publishing fault management telemetry (see PSARC 2002/412 and 2003/089).
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * Name-Value Pair Lists
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * The embodiment of an FMA protocol element (event, fmri or authority) is a
350Sstevel@tonic-gate  * name-value pair list (nvlist_t).  FMA-specific nvlist construtor and
360Sstevel@tonic-gate  * destructor functions, fm_nvlist_create() and fm_nvlist_destroy(), are used
370Sstevel@tonic-gate  * to create an nvpair list using custom allocators.  Callers may choose to
380Sstevel@tonic-gate  * allocate either from the kernel memory allocator, or from a preallocated
390Sstevel@tonic-gate  * buffer, useful in constrained contexts like high-level interrupt routines.
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * Protocol Event and FMRI Construction
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * Convenience routines are provided to construct nvlist events according to
440Sstevel@tonic-gate  * the FMA Event Protocol and Naming Schema specification for ereports and
450Sstevel@tonic-gate  * FMRIs for the dev, cpu, hc, mem, legacy hc and de schemes.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * ENA Manipulation
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  * Routines to generate ENA formats 0, 1 and 2 are available as well as
500Sstevel@tonic-gate  * routines to increment formats 1 and 2.  Individual fields within the
510Sstevel@tonic-gate  * ENA are extractable via fm_ena_time_get(), fm_ena_id_get(),
520Sstevel@tonic-gate  * fm_ena_format_get() and fm_ena_gen_get().
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/types.h>
560Sstevel@tonic-gate #include <sys/time.h>
570Sstevel@tonic-gate #include <sys/sysevent.h>
580Sstevel@tonic-gate #include <sys/sysevent_impl.h>
590Sstevel@tonic-gate #include <sys/nvpair.h>
600Sstevel@tonic-gate #include <sys/cmn_err.h>
610Sstevel@tonic-gate #include <sys/cpuvar.h>
620Sstevel@tonic-gate #include <sys/sysmacros.h>
630Sstevel@tonic-gate #include <sys/systm.h>
640Sstevel@tonic-gate #include <sys/ddifm.h>
650Sstevel@tonic-gate #include <sys/ddifm_impl.h>
660Sstevel@tonic-gate #include <sys/spl.h>
670Sstevel@tonic-gate #include <sys/dumphdr.h>
680Sstevel@tonic-gate #include <sys/compress.h>
690Sstevel@tonic-gate #include <sys/cpuvar.h>
700Sstevel@tonic-gate #include <sys/console.h>
710Sstevel@tonic-gate #include <sys/panic.h>
721414Scindi #include <sys/kobj.h>
731414Scindi #include <sys/sunddi.h>
740Sstevel@tonic-gate #include <sys/systeminfo.h>
750Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
760Sstevel@tonic-gate #include <sys/fm/util.h>
770Sstevel@tonic-gate #include <sys/fm/protocol.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * URL and SUNW-MSG-ID value to display for fm_panic(), defined below.  These
810Sstevel@tonic-gate  * values must be kept in sync with the FMA source code in usr/src/cmd/fm.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static const char *fm_url = "http://www.sun.com/msg";
840Sstevel@tonic-gate static const char *fm_msgid = "SUNOS-8000-0G";
850Sstevel@tonic-gate static char *volatile fm_panicstr = NULL;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate errorq_t *ereport_errorq;
880Sstevel@tonic-gate void *ereport_dumpbuf;
890Sstevel@tonic-gate size_t ereport_dumplen;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate static uint_t ereport_chanlen = ERPT_EVCH_MAX;
920Sstevel@tonic-gate static evchan_t *ereport_chan = NULL;
930Sstevel@tonic-gate static ulong_t ereport_qlen = 0;
940Sstevel@tonic-gate static size_t ereport_size = 0;
950Sstevel@tonic-gate static int ereport_cols = 80;
960Sstevel@tonic-gate 
97*10664SAdrian.Frost@Sun.COM extern void fastreboot_disable_highpil(void);
98*10664SAdrian.Frost@Sun.COM 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * Common fault management kstats to record ereport generation
1010Sstevel@tonic-gate  * failures
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate struct erpt_kstat {
1050Sstevel@tonic-gate 	kstat_named_t	erpt_dropped;		/* num erpts dropped on post */
1060Sstevel@tonic-gate 	kstat_named_t	erpt_set_failed;	/* num erpt set failures */
1070Sstevel@tonic-gate 	kstat_named_t	fmri_set_failed;	/* num fmri set failures */
1080Sstevel@tonic-gate 	kstat_named_t	payload_set_failed;	/* num payload set failures */
1090Sstevel@tonic-gate };
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static struct erpt_kstat erpt_kstat_data = {
1120Sstevel@tonic-gate 	{ "erpt-dropped", KSTAT_DATA_UINT64 },
1130Sstevel@tonic-gate 	{ "erpt-set-failed", KSTAT_DATA_UINT64 },
1140Sstevel@tonic-gate 	{ "fmri-set-failed", KSTAT_DATA_UINT64 },
1150Sstevel@tonic-gate 	{ "payload-set-failed", KSTAT_DATA_UINT64 }
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*ARGSUSED*/
1190Sstevel@tonic-gate static void
1200Sstevel@tonic-gate fm_drain(void *private, void *data, errorq_elem_t *eep)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	nvlist_t *nvl = errorq_elem_nvl(ereport_errorq, eep);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (!panicstr)
1250Sstevel@tonic-gate 		(void) fm_ereport_post(nvl, EVCH_TRYHARD);
1260Sstevel@tonic-gate 	else
1270Sstevel@tonic-gate 		fm_nvprint(nvl);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate void
1310Sstevel@tonic-gate fm_init(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	kstat_t *ksp;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	(void) sysevent_evc_bind(FM_ERROR_CHAN,
1360Sstevel@tonic-gate 	    &ereport_chan, EVCH_CREAT | EVCH_HOLD_PEND);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	(void) sysevent_evc_control(ereport_chan,
1390Sstevel@tonic-gate 	    EVCH_SET_CHAN_LEN, &ereport_chanlen);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	if (ereport_qlen == 0)
1420Sstevel@tonic-gate 		ereport_qlen = ERPT_MAX_ERRS * MAX(max_ncpus, 4);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	if (ereport_size == 0)
1450Sstevel@tonic-gate 		ereport_size = ERPT_DATA_SZ;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	ereport_errorq = errorq_nvcreate("fm_ereport_queue",
1480Sstevel@tonic-gate 	    (errorq_func_t)fm_drain, NULL, ereport_qlen, ereport_size,
1490Sstevel@tonic-gate 	    FM_ERR_PIL, ERRORQ_VITAL);
1500Sstevel@tonic-gate 	if (ereport_errorq == NULL)
1510Sstevel@tonic-gate 		panic("failed to create required ereport error queue");
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	ereport_dumpbuf = kmem_alloc(ereport_size, KM_SLEEP);
1540Sstevel@tonic-gate 	ereport_dumplen = ereport_size;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/* Initialize ereport allocation and generation kstats */
1570Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "fm", "misc", KSTAT_TYPE_NAMED,
1580Sstevel@tonic-gate 	    sizeof (struct erpt_kstat) / sizeof (kstat_named_t),
1590Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (ksp != NULL) {
1620Sstevel@tonic-gate 		ksp->ks_data = &erpt_kstat_data;
1630Sstevel@tonic-gate 		kstat_install(ksp);
1640Sstevel@tonic-gate 	} else {
1650Sstevel@tonic-gate 		cmn_err(CE_NOTE, "failed to create fm/misc kstat\n");
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Formatting utility function for fm_nvprintr.  We attempt to wrap chunks of
1720Sstevel@tonic-gate  * output so they aren't split across console lines, and return the end column.
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate /*PRINTFLIKE4*/
1750Sstevel@tonic-gate static int
1760Sstevel@tonic-gate fm_printf(int depth, int c, int cols, const char *format, ...)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	va_list ap;
1790Sstevel@tonic-gate 	int width;
1800Sstevel@tonic-gate 	char c1;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	va_start(ap, format);
1830Sstevel@tonic-gate 	width = vsnprintf(&c1, sizeof (c1), format, ap);
1840Sstevel@tonic-gate 	va_end(ap);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (c + width >= cols) {
1870Sstevel@tonic-gate 		console_printf("\n\r");
1880Sstevel@tonic-gate 		c = 0;
1890Sstevel@tonic-gate 		if (format[0] != ' ' && depth > 0) {
1900Sstevel@tonic-gate 			console_printf(" ");
1910Sstevel@tonic-gate 			c++;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	va_start(ap, format);
1960Sstevel@tonic-gate 	console_vprintf(format, ap);
1970Sstevel@tonic-gate 	va_end(ap);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	return ((c + width) % cols);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * Recursively print a nvlist in the specified column width and return the
2040Sstevel@tonic-gate  * column we end up in.  This function is called recursively by fm_nvprint(),
2050Sstevel@tonic-gate  * below.  We generically format the entire nvpair using hexadecimal
2060Sstevel@tonic-gate  * integers and strings, and elide any integer arrays.  Arrays are basically
2070Sstevel@tonic-gate  * used for cache dumps right now, so we suppress them so as not to overwhelm
2080Sstevel@tonic-gate  * the amount of console output we produce at panic time.  This can be further
2090Sstevel@tonic-gate  * enhanced as FMA technology grows based upon the needs of consumers.  All
2100Sstevel@tonic-gate  * FMA telemetry is logged using the dump device transport, so the console
2110Sstevel@tonic-gate  * output serves only as a fallback in case this procedure is unsuccessful.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate static int
2140Sstevel@tonic-gate fm_nvprintr(nvlist_t *nvl, int d, int c, int cols)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	nvpair_t *nvp;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	for (nvp = nvlist_next_nvpair(nvl, NULL);
2190Sstevel@tonic-gate 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		data_type_t type = nvpair_type(nvp);
2220Sstevel@tonic-gate 		const char *name = nvpair_name(nvp);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		boolean_t b;
2250Sstevel@tonic-gate 		uint8_t i8;
2260Sstevel@tonic-gate 		uint16_t i16;
2270Sstevel@tonic-gate 		uint32_t i32;
2280Sstevel@tonic-gate 		uint64_t i64;
2290Sstevel@tonic-gate 		char *str;
2300Sstevel@tonic-gate 		nvlist_t *cnv;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		if (strcmp(name, FM_CLASS) == 0)
2330Sstevel@tonic-gate 			continue; /* already printed by caller */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		c = fm_printf(d, c, cols, " %s=", name);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		switch (type) {
2380Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN:
2390Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " 1");
2400Sstevel@tonic-gate 			break;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
2430Sstevel@tonic-gate 			(void) nvpair_value_boolean_value(nvp, &b);
2440Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, b ? "1" : "0");
2450Sstevel@tonic-gate 			break;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
2480Sstevel@tonic-gate 			(void) nvpair_value_byte(nvp, &i8);
2490Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2500Sstevel@tonic-gate 			break;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		case DATA_TYPE_INT8:
2530Sstevel@tonic-gate 			(void) nvpair_value_int8(nvp, (void *)&i8);
2540Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2550Sstevel@tonic-gate 			break;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
2580Sstevel@tonic-gate 			(void) nvpair_value_uint8(nvp, &i8);
2590Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2600Sstevel@tonic-gate 			break;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		case DATA_TYPE_INT16:
2630Sstevel@tonic-gate 			(void) nvpair_value_int16(nvp, (void *)&i16);
2640Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
2650Sstevel@tonic-gate 			break;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
2680Sstevel@tonic-gate 			(void) nvpair_value_uint16(nvp, &i16);
2690Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		case DATA_TYPE_INT32:
2730Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (void *)&i32);
2740Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
2750Sstevel@tonic-gate 			break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
2780Sstevel@tonic-gate 			(void) nvpair_value_uint32(nvp, &i32);
2790Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
2800Sstevel@tonic-gate 			break;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		case DATA_TYPE_INT64:
2830Sstevel@tonic-gate 			(void) nvpair_value_int64(nvp, (void *)&i64);
2840Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2850Sstevel@tonic-gate 			    (u_longlong_t)i64);
2860Sstevel@tonic-gate 			break;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
2890Sstevel@tonic-gate 			(void) nvpair_value_uint64(nvp, &i64);
2900Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2910Sstevel@tonic-gate 			    (u_longlong_t)i64);
2920Sstevel@tonic-gate 			break;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		case DATA_TYPE_HRTIME:
2950Sstevel@tonic-gate 			(void) nvpair_value_hrtime(nvp, (void *)&i64);
2960Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2970Sstevel@tonic-gate 			    (u_longlong_t)i64);
2980Sstevel@tonic-gate 			break;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		case DATA_TYPE_STRING:
3010Sstevel@tonic-gate 			(void) nvpair_value_string(nvp, &str);
3020Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "\"%s\"",
3030Sstevel@tonic-gate 			    str ? str : "<NULL>");
3040Sstevel@tonic-gate 			break;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
3070Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[");
3080Sstevel@tonic-gate 			(void) nvpair_value_nvlist(nvp, &cnv);
3090Sstevel@tonic-gate 			c = fm_nvprintr(cnv, d + 1, c, cols);
3100Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " ]");
3110Sstevel@tonic-gate 			break;
3120Sstevel@tonic-gate 
3137532SSean.Ye@Sun.COM 		case DATA_TYPE_NVLIST_ARRAY: {
3147532SSean.Ye@Sun.COM 			nvlist_t **val;
3157532SSean.Ye@Sun.COM 			uint_t i, nelem;
3167532SSean.Ye@Sun.COM 
3177532SSean.Ye@Sun.COM 			c = fm_printf(d + 1, c, cols, "[");
3187532SSean.Ye@Sun.COM 			(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
3197532SSean.Ye@Sun.COM 			for (i = 0; i < nelem; i++) {
3207532SSean.Ye@Sun.COM 				c = fm_nvprintr(val[i], d + 1, c, cols);
3217532SSean.Ye@Sun.COM 			}
3227532SSean.Ye@Sun.COM 			c = fm_printf(d + 1, c, cols, " ]");
3237532SSean.Ye@Sun.COM 			}
3247532SSean.Ye@Sun.COM 			break;
3257532SSean.Ye@Sun.COM 
3260Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
3270Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
3280Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
3290Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
3300Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
3310Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
3320Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
3330Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
3340Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
3350Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
3360Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
3370Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[...]");
3380Sstevel@tonic-gate 			break;
3390Sstevel@tonic-gate 		case DATA_TYPE_UNKNOWN:
3400Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "<unknown>");
3410Sstevel@tonic-gate 			break;
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	return (c);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate void
3490Sstevel@tonic-gate fm_nvprint(nvlist_t *nvl)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate 	char *class;
3520Sstevel@tonic-gate 	int c = 0;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	console_printf("\r");
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) == 0)
3570Sstevel@tonic-gate 		c = fm_printf(0, c, ereport_cols, "%s", class);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (fm_nvprintr(nvl, 0, c, ereport_cols) != 0)
3600Sstevel@tonic-gate 		console_printf("\n");
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	console_printf("\n");
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Wrapper for panic() that first produces an FMA-style message for admins.
3670Sstevel@tonic-gate  * Normally such messages are generated by fmd(1M)'s syslog-msgs agent: this
3680Sstevel@tonic-gate  * is the one exception to that rule and the only error that gets messaged.
3690Sstevel@tonic-gate  * This function is intended for use by subsystems that have detected a fatal
3700Sstevel@tonic-gate  * error and enqueued appropriate ereports and wish to then force a panic.
3710Sstevel@tonic-gate  */
3720Sstevel@tonic-gate /*PRINTFLIKE1*/
3730Sstevel@tonic-gate void
3740Sstevel@tonic-gate fm_panic(const char *format, ...)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	va_list ap;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	(void) casptr((void *)&fm_panicstr, NULL, (void *)format);
379*10664SAdrian.Frost@Sun.COM #if defined(__i386) || defined(__amd64)
380*10664SAdrian.Frost@Sun.COM 	fastreboot_disable_highpil();
381*10664SAdrian.Frost@Sun.COM #endif /* __i386 || __amd64 */
3820Sstevel@tonic-gate 	va_start(ap, format);
3830Sstevel@tonic-gate 	vpanic(format, ap);
3840Sstevel@tonic-gate 	va_end(ap);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * Print any appropriate FMA banner message before the panic message.  This
3890Sstevel@tonic-gate  * function is called by panicsys() and prints the message for fm_panic().
3900Sstevel@tonic-gate  * We print the message here so that it comes after the system is quiesced.
3910Sstevel@tonic-gate  * A one-line summary is recorded in the log only (cmn_err(9F) with "!" prefix).
3920Sstevel@tonic-gate  * The rest of the message is for the console only and not needed in the log,
3930Sstevel@tonic-gate  * so it is printed using console_printf().  We break it up into multiple
3940Sstevel@tonic-gate  * chunks so as to avoid overflowing any small legacy prom_printf() buffers.
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate void
3970Sstevel@tonic-gate fm_banner(void)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 	timespec_t tod;
4000Sstevel@tonic-gate 	hrtime_t now;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (!fm_panicstr)
4030Sstevel@tonic-gate 		return; /* panic was not initiated by fm_panic(); do nothing */
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (panicstr) {
4060Sstevel@tonic-gate 		tod = panic_hrestime;
4070Sstevel@tonic-gate 		now = panic_hrtime;
4080Sstevel@tonic-gate 	} else {
4090Sstevel@tonic-gate 		gethrestime(&tod);
4100Sstevel@tonic-gate 		now = gethrtime_waitfree();
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!SUNW-MSG-ID: %s, "
4140Sstevel@tonic-gate 	    "TYPE: Error, VER: 1, SEVERITY: Major\n", fm_msgid);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	console_printf(
4170Sstevel@tonic-gate "\n\rSUNW-MSG-ID: %s, TYPE: Error, VER: 1, SEVERITY: Major\n"
4180Sstevel@tonic-gate "EVENT-TIME: 0x%lx.0x%lx (0x%llx)\n",
4190Sstevel@tonic-gate 	    fm_msgid, tod.tv_sec, tod.tv_nsec, (u_longlong_t)now);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	console_printf(
4226014Ssuha "PLATFORM: %s, CSN: -, HOSTNAME: %s\n"
4230Sstevel@tonic-gate "SOURCE: %s, REV: %s %s\n",
4240Sstevel@tonic-gate 	    platform, utsname.nodename, utsname.sysname,
4250Sstevel@tonic-gate 	    utsname.release, utsname.version);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	console_printf(
4280Sstevel@tonic-gate "DESC: Errors have been detected that require a reboot to ensure system\n"
4290Sstevel@tonic-gate "integrity.  See %s/%s for more information.\n",
4300Sstevel@tonic-gate 	    fm_url, fm_msgid);
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	console_printf(
4330Sstevel@tonic-gate "AUTO-RESPONSE: Solaris will attempt to save and diagnose the error telemetry\n"
4340Sstevel@tonic-gate "IMPACT: The system will sync files, save a crash dump if needed, and reboot\n"
4350Sstevel@tonic-gate "REC-ACTION: Save the error summary below in case telemetry cannot be saved\n");
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	console_printf("\n");
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate  * Utility function to write all of the pending ereports to the dump device.
4420Sstevel@tonic-gate  * This function is called at either normal reboot or panic time, and simply
4430Sstevel@tonic-gate  * iterates over the in-transit messages in the ereport sysevent channel.
4440Sstevel@tonic-gate  */
4450Sstevel@tonic-gate void
4460Sstevel@tonic-gate fm_ereport_dump(void)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	evchanq_t *chq;
4490Sstevel@tonic-gate 	sysevent_t *sep;
4500Sstevel@tonic-gate 	erpt_dump_t ed;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	timespec_t tod;
4530Sstevel@tonic-gate 	hrtime_t now;
4540Sstevel@tonic-gate 	char *buf;
4550Sstevel@tonic-gate 	size_t len;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	if (panicstr) {
4580Sstevel@tonic-gate 		tod = panic_hrestime;
4590Sstevel@tonic-gate 		now = panic_hrtime;
4600Sstevel@tonic-gate 	} else {
4610Sstevel@tonic-gate 		if (ereport_errorq != NULL)
4620Sstevel@tonic-gate 			errorq_drain(ereport_errorq);
4630Sstevel@tonic-gate 		gethrestime(&tod);
4640Sstevel@tonic-gate 		now = gethrtime_waitfree();
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	/*
4680Sstevel@tonic-gate 	 * In the panic case, sysevent_evc_walk_init() will return NULL.
4690Sstevel@tonic-gate 	 */
4700Sstevel@tonic-gate 	if ((chq = sysevent_evc_walk_init(ereport_chan, NULL)) == NULL &&
4710Sstevel@tonic-gate 	    !panicstr)
4720Sstevel@tonic-gate 		return; /* event channel isn't initialized yet */
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	while ((sep = sysevent_evc_walk_step(chq)) != NULL) {
4750Sstevel@tonic-gate 		if ((buf = sysevent_evc_event_attr(sep, &len)) == NULL)
4760Sstevel@tonic-gate 			break;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		ed.ed_magic = ERPT_MAGIC;
4790Sstevel@tonic-gate 		ed.ed_chksum = checksum32(buf, len);
4800Sstevel@tonic-gate 		ed.ed_size = (uint32_t)len;
4810Sstevel@tonic-gate 		ed.ed_pad = 0;
4820Sstevel@tonic-gate 		ed.ed_hrt_nsec = SE_TIME(sep);
4830Sstevel@tonic-gate 		ed.ed_hrt_base = now;
4840Sstevel@tonic-gate 		ed.ed_tod_base.sec = tod.tv_sec;
4850Sstevel@tonic-gate 		ed.ed_tod_base.nsec = tod.tv_nsec;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		dumpvp_write(&ed, sizeof (ed));
4880Sstevel@tonic-gate 		dumpvp_write(buf, len);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	sysevent_evc_walk_fini(chq);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * Post an error report (ereport) to the sysevent error channel.  The error
4960Sstevel@tonic-gate  * channel must be established with a prior call to sysevent_evc_create()
4970Sstevel@tonic-gate  * before publication may occur.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate void
5000Sstevel@tonic-gate fm_ereport_post(nvlist_t *ereport, int evc_flag)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate 	size_t nvl_size = 0;
5030Sstevel@tonic-gate 	evchan_t *error_chan;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	(void) nvlist_size(ereport, &nvl_size, NV_ENCODE_NATIVE);
5060Sstevel@tonic-gate 	if (nvl_size > ERPT_DATA_SZ || nvl_size == 0) {
5070Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
5080Sstevel@tonic-gate 		return;
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (sysevent_evc_bind(FM_ERROR_CHAN, &error_chan,
5120Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
5130Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
5140Sstevel@tonic-gate 		return;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (sysevent_evc_publish(error_chan, EC_FM, ESC_FM_ERROR,
5180Sstevel@tonic-gate 	    SUNW_VENDOR, FM_PUB, ereport, evc_flag) != 0) {
5190Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
5200Sstevel@tonic-gate 		sysevent_evc_unbind(error_chan);
5210Sstevel@tonic-gate 		return;
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 	sysevent_evc_unbind(error_chan);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * Wrapppers for FM nvlist allocators
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate /* ARGSUSED */
5300Sstevel@tonic-gate static void *
5310Sstevel@tonic-gate i_fm_alloc(nv_alloc_t *nva, size_t size)
5320Sstevel@tonic-gate {
5330Sstevel@tonic-gate 	return (kmem_zalloc(size, KM_SLEEP));
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate /* ARGSUSED */
5370Sstevel@tonic-gate static void
5380Sstevel@tonic-gate i_fm_free(nv_alloc_t *nva, void *buf, size_t size)
5390Sstevel@tonic-gate {
5400Sstevel@tonic-gate 	kmem_free(buf, size);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate const nv_alloc_ops_t fm_mem_alloc_ops = {
5440Sstevel@tonic-gate 	NULL,
5450Sstevel@tonic-gate 	NULL,
5460Sstevel@tonic-gate 	i_fm_alloc,
5470Sstevel@tonic-gate 	i_fm_free,
5480Sstevel@tonic-gate 	NULL
5490Sstevel@tonic-gate };
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  * Create and initialize a new nv_alloc_t for a fixed buffer, buf.  A pointer
5530Sstevel@tonic-gate  * to the newly allocated nv_alloc_t structure is returned upon success or NULL
5540Sstevel@tonic-gate  * is returned to indicate that the nv_alloc structure could not be created.
5550Sstevel@tonic-gate  */
5560Sstevel@tonic-gate nv_alloc_t *
5570Sstevel@tonic-gate fm_nva_xcreate(char *buf, size_t bufsz)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate 	nv_alloc_t *nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (bufsz == 0 || nv_alloc_init(nvhdl, nv_fixed_ops, buf, bufsz) != 0) {
5620Sstevel@tonic-gate 		kmem_free(nvhdl, sizeof (nv_alloc_t));
5630Sstevel@tonic-gate 		return (NULL);
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	return (nvhdl);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate /*
5700Sstevel@tonic-gate  * Destroy a previously allocated nv_alloc structure.  The fixed buffer
5710Sstevel@tonic-gate  * associated with nva must be freed by the caller.
5720Sstevel@tonic-gate  */
5730Sstevel@tonic-gate void
5740Sstevel@tonic-gate fm_nva_xdestroy(nv_alloc_t *nva)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	nv_alloc_fini(nva);
5770Sstevel@tonic-gate 	kmem_free(nva, sizeof (nv_alloc_t));
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * Create a new nv list.  A pointer to a new nv list structure is returned
5820Sstevel@tonic-gate  * upon success or NULL is returned to indicate that the structure could
5830Sstevel@tonic-gate  * not be created.  The newly created nv list is created and managed by the
5840Sstevel@tonic-gate  * operations installed in nva.   If nva is NULL, the default FMA nva
5850Sstevel@tonic-gate  * operations are installed and used.
5860Sstevel@tonic-gate  *
5870Sstevel@tonic-gate  * When called from the kernel and nva == NULL, this function must be called
5880Sstevel@tonic-gate  * from passive kernel context with no locks held that can prevent a
5890Sstevel@tonic-gate  * sleeping memory allocation from occurring.  Otherwise, this function may
5900Sstevel@tonic-gate  * be called from other kernel contexts as long a valid nva created via
5910Sstevel@tonic-gate  * fm_nva_create() is supplied.
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate nvlist_t *
5940Sstevel@tonic-gate fm_nvlist_create(nv_alloc_t *nva)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	int hdl_alloced = 0;
5970Sstevel@tonic-gate 	nvlist_t *nvl;
5980Sstevel@tonic-gate 	nv_alloc_t *nvhdl;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (nva == NULL) {
6010Sstevel@tonic-gate 		nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		if (nv_alloc_init(nvhdl, &fm_mem_alloc_ops, NULL, 0) != 0) {
6040Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
6050Sstevel@tonic-gate 			return (NULL);
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 		hdl_alloced = 1;
6080Sstevel@tonic-gate 	} else {
6090Sstevel@tonic-gate 		nvhdl = nva;
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nvhdl) != 0) {
6130Sstevel@tonic-gate 		if (hdl_alloced) {
6140Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
6150Sstevel@tonic-gate 			nv_alloc_fini(nvhdl);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 		return (NULL);
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	return (nvl);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Destroy a previously allocated nvlist structure.  flag indicates whether
6250Sstevel@tonic-gate  * or not the associated nva structure should be freed (FM_NVA_FREE) or
6260Sstevel@tonic-gate  * retained (FM_NVA_RETAIN).  Retaining the nv alloc structure allows
6270Sstevel@tonic-gate  * it to be re-used for future nvlist creation operations.
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate void
6300Sstevel@tonic-gate fm_nvlist_destroy(nvlist_t *nvl, int flag)
6310Sstevel@tonic-gate {
6321414Scindi 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	nvlist_free(nvl);
6350Sstevel@tonic-gate 
6361414Scindi 	if (nva != NULL) {
6370Sstevel@tonic-gate 		if (flag == FM_NVA_FREE)
6381414Scindi 			fm_nva_xdestroy(nva);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate int
6430Sstevel@tonic-gate i_fm_payload_set(nvlist_t *payload, const char *name, va_list ap)
6440Sstevel@tonic-gate {
6450Sstevel@tonic-gate 	int nelem, ret = 0;
6460Sstevel@tonic-gate 	data_type_t type;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	while (ret == 0 && name != NULL) {
6490Sstevel@tonic-gate 		type = va_arg(ap, data_type_t);
6500Sstevel@tonic-gate 		switch (type) {
6510Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
6520Sstevel@tonic-gate 			ret = nvlist_add_byte(payload, name,
6530Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6540Sstevel@tonic-gate 			break;
6550Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
6560Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6570Sstevel@tonic-gate 			ret = nvlist_add_byte_array(payload, name,
6580Sstevel@tonic-gate 			    va_arg(ap, uchar_t *), nelem);
6590Sstevel@tonic-gate 			break;
6600Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
6610Sstevel@tonic-gate 			ret = nvlist_add_boolean_value(payload, name,
6620Sstevel@tonic-gate 			    va_arg(ap, boolean_t));
6630Sstevel@tonic-gate 			break;
6640Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
6650Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6660Sstevel@tonic-gate 			ret = nvlist_add_boolean_array(payload, name,
6670Sstevel@tonic-gate 			    va_arg(ap, boolean_t *), nelem);
6680Sstevel@tonic-gate 			break;
6690Sstevel@tonic-gate 		case DATA_TYPE_INT8:
6700Sstevel@tonic-gate 			ret = nvlist_add_int8(payload, name,
6710Sstevel@tonic-gate 			    va_arg(ap, int));
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
6740Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6750Sstevel@tonic-gate 			ret = nvlist_add_int8_array(payload, name,
6760Sstevel@tonic-gate 			    va_arg(ap, int8_t *), nelem);
6770Sstevel@tonic-gate 			break;
6780Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
6790Sstevel@tonic-gate 			ret = nvlist_add_uint8(payload, name,
6800Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
6830Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6840Sstevel@tonic-gate 			ret = nvlist_add_uint8_array(payload, name,
6850Sstevel@tonic-gate 			    va_arg(ap, uint8_t *), nelem);
6860Sstevel@tonic-gate 			break;
6870Sstevel@tonic-gate 		case DATA_TYPE_INT16:
6880Sstevel@tonic-gate 			ret = nvlist_add_int16(payload, name,
6890Sstevel@tonic-gate 			    va_arg(ap, int));
6900Sstevel@tonic-gate 			break;
6910Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
6920Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6930Sstevel@tonic-gate 			ret = nvlist_add_int16_array(payload, name,
6940Sstevel@tonic-gate 			    va_arg(ap, int16_t *), nelem);
6950Sstevel@tonic-gate 			break;
6960Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
6970Sstevel@tonic-gate 			ret = nvlist_add_uint16(payload, name,
6980Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6990Sstevel@tonic-gate 			break;
7000Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
7010Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7020Sstevel@tonic-gate 			ret = nvlist_add_uint16_array(payload, name,
7030Sstevel@tonic-gate 			    va_arg(ap, uint16_t *), nelem);
7040Sstevel@tonic-gate 			break;
7050Sstevel@tonic-gate 		case DATA_TYPE_INT32:
7060Sstevel@tonic-gate 			ret = nvlist_add_int32(payload, name,
7070Sstevel@tonic-gate 			    va_arg(ap, int32_t));
7080Sstevel@tonic-gate 			break;
7090Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
7100Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7110Sstevel@tonic-gate 			ret = nvlist_add_int32_array(payload, name,
7120Sstevel@tonic-gate 			    va_arg(ap, int32_t *), nelem);
7130Sstevel@tonic-gate 			break;
7140Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
7150Sstevel@tonic-gate 			ret = nvlist_add_uint32(payload, name,
7160Sstevel@tonic-gate 			    va_arg(ap, uint32_t));
7170Sstevel@tonic-gate 			break;
7180Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
7190Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7200Sstevel@tonic-gate 			ret = nvlist_add_uint32_array(payload, name,
7210Sstevel@tonic-gate 			    va_arg(ap, uint32_t *), nelem);
7220Sstevel@tonic-gate 			break;
7230Sstevel@tonic-gate 		case DATA_TYPE_INT64:
7240Sstevel@tonic-gate 			ret = nvlist_add_int64(payload, name,
7250Sstevel@tonic-gate 			    va_arg(ap, int64_t));
7260Sstevel@tonic-gate 			break;
7270Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
7280Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7290Sstevel@tonic-gate 			ret = nvlist_add_int64_array(payload, name,
7300Sstevel@tonic-gate 			    va_arg(ap, int64_t *), nelem);
7310Sstevel@tonic-gate 			break;
7320Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
7330Sstevel@tonic-gate 			ret = nvlist_add_uint64(payload, name,
7340Sstevel@tonic-gate 			    va_arg(ap, uint64_t));
7350Sstevel@tonic-gate 			break;
7360Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
7370Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7380Sstevel@tonic-gate 			ret = nvlist_add_uint64_array(payload, name,
7390Sstevel@tonic-gate 			    va_arg(ap, uint64_t *), nelem);
7400Sstevel@tonic-gate 			break;
7410Sstevel@tonic-gate 		case DATA_TYPE_STRING:
7420Sstevel@tonic-gate 			ret = nvlist_add_string(payload, name,
7430Sstevel@tonic-gate 			    va_arg(ap, char *));
7440Sstevel@tonic-gate 			break;
7450Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
7460Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7470Sstevel@tonic-gate 			ret = nvlist_add_string_array(payload, name,
7480Sstevel@tonic-gate 			    va_arg(ap, char **), nelem);
7490Sstevel@tonic-gate 			break;
7500Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
7510Sstevel@tonic-gate 			ret = nvlist_add_nvlist(payload, name,
7520Sstevel@tonic-gate 			    va_arg(ap, nvlist_t *));
7530Sstevel@tonic-gate 			break;
7540Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
7550Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7560Sstevel@tonic-gate 			ret = nvlist_add_nvlist_array(payload, name,
7570Sstevel@tonic-gate 			    va_arg(ap, nvlist_t **), nelem);
7580Sstevel@tonic-gate 			break;
7590Sstevel@tonic-gate 		default:
7600Sstevel@tonic-gate 			ret = EINVAL;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		name = va_arg(ap, char *);
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 	return (ret);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate void
7690Sstevel@tonic-gate fm_payload_set(nvlist_t *payload, ...)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	int ret;
7720Sstevel@tonic-gate 	const char *name;
7730Sstevel@tonic-gate 	va_list ap;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	va_start(ap, payload);
7760Sstevel@tonic-gate 	name = va_arg(ap, char *);
7770Sstevel@tonic-gate 	ret = i_fm_payload_set(payload, name, ap);
7780Sstevel@tonic-gate 	va_end(ap);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (ret)
7810Sstevel@tonic-gate 		atomic_add_64(
7820Sstevel@tonic-gate 		    &erpt_kstat_data.payload_set_failed.value.ui64, 1);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate /*
7860Sstevel@tonic-gate  * Set-up and validate the members of an ereport event according to:
7870Sstevel@tonic-gate  *
7880Sstevel@tonic-gate  *	Member name		Type		Value
7890Sstevel@tonic-gate  *	====================================================
7900Sstevel@tonic-gate  *	class			string		ereport
7910Sstevel@tonic-gate  *	version			uint8_t		0
7920Sstevel@tonic-gate  *	ena			uint64_t	<ena>
7930Sstevel@tonic-gate  *	detector		nvlist_t	<detector>
7940Sstevel@tonic-gate  *	ereport-payload		nvlist_t	<var args>
7950Sstevel@tonic-gate  *
7960Sstevel@tonic-gate  */
7970Sstevel@tonic-gate void
7980Sstevel@tonic-gate fm_ereport_set(nvlist_t *ereport, int version, const char *erpt_class,
7990Sstevel@tonic-gate     uint64_t ena, const nvlist_t *detector, ...)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	char ereport_class[FM_MAX_CLASS];
8020Sstevel@tonic-gate 	const char *name;
8030Sstevel@tonic-gate 	va_list ap;
8040Sstevel@tonic-gate 	int ret;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	if (version != FM_EREPORT_VERS0) {
8070Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8080Sstevel@tonic-gate 		return;
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	(void) snprintf(ereport_class, FM_MAX_CLASS, "%s.%s",
8120Sstevel@tonic-gate 	    FM_EREPORT_CLASS, erpt_class);
8130Sstevel@tonic-gate 	if (nvlist_add_string(ereport, FM_CLASS, ereport_class) != 0) {
8140Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8150Sstevel@tonic-gate 		return;
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	if (nvlist_add_uint64(ereport, FM_EREPORT_ENA, ena)) {
8190Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (nvlist_add_nvlist(ereport, FM_EREPORT_DETECTOR,
8230Sstevel@tonic-gate 	    (nvlist_t *)detector) != 0) {
8240Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	va_start(ap, detector);
8280Sstevel@tonic-gate 	name = va_arg(ap, const char *);
8290Sstevel@tonic-gate 	ret = i_fm_payload_set(ereport, name, ap);
8300Sstevel@tonic-gate 	va_end(ap);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (ret)
8330Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate 
8361414Scindi /*
8371414Scindi  * Set-up and validate the members of an hc fmri according to;
8381414Scindi  *
8391414Scindi  *	Member name		Type		Value
8401414Scindi  *	===================================================
8411414Scindi  *	version			uint8_t		0
8421414Scindi  *	auth			nvlist_t	<auth>
8431414Scindi  *	hc-name			string		<name>
8441414Scindi  *	hc-id			string		<id>
8451414Scindi  *
8461414Scindi  * Note that auth and hc-id are optional members.
8471414Scindi  */
8480Sstevel@tonic-gate 
8491414Scindi #define	HC_MAXPAIRS	20
8501414Scindi #define	HC_MAXNAMELEN	50
8510Sstevel@tonic-gate 
8521414Scindi static int
8531414Scindi fm_fmri_hc_set_common(nvlist_t *fmri, int version, const nvlist_t *auth)
8541414Scindi {
8551414Scindi 	if (version != FM_HC_SCHEME_VERSION) {
8561414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
8571414Scindi 		return (0);
8581414Scindi 	}
8591414Scindi 
8601414Scindi 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0 ||
8611414Scindi 	    nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC) != 0) {
8621414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
8631414Scindi 		return (0);
8641414Scindi 	}
8651414Scindi 
8661414Scindi 	if (auth != NULL && nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
8671414Scindi 	    (nvlist_t *)auth) != 0) {
8681414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
8691414Scindi 		return (0);
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 
8721414Scindi 	return (1);
8731414Scindi }
8740Sstevel@tonic-gate 
8751414Scindi void
8761414Scindi fm_fmri_hc_set(nvlist_t *fmri, int version, const nvlist_t *auth,
8771414Scindi     nvlist_t *snvl, int npairs, ...)
8781414Scindi {
8791414Scindi 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
8801414Scindi 	nvlist_t *pairs[HC_MAXPAIRS];
8811414Scindi 	va_list ap;
8821414Scindi 	int i;
8831414Scindi 
8841414Scindi 	if (!fm_fmri_hc_set_common(fmri, version, auth))
8851414Scindi 		return;
8861414Scindi 
8871414Scindi 	npairs = MIN(npairs, HC_MAXPAIRS);
8881414Scindi 
8891414Scindi 	va_start(ap, npairs);
8901414Scindi 	for (i = 0; i < npairs; i++) {
8911414Scindi 		const char *name = va_arg(ap, const char *);
8921414Scindi 		uint32_t id = va_arg(ap, uint32_t);
8931414Scindi 		char idstr[11];
8940Sstevel@tonic-gate 
8951414Scindi 		(void) snprintf(idstr, sizeof (idstr), "%u", id);
8961414Scindi 
8971414Scindi 		pairs[i] = fm_nvlist_create(nva);
8981414Scindi 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
8991414Scindi 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
9001414Scindi 			atomic_add_64(
9011414Scindi 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9021414Scindi 		}
9030Sstevel@tonic-gate 	}
9041414Scindi 	va_end(ap);
9050Sstevel@tonic-gate 
9061414Scindi 	if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs, npairs) != 0)
9071414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9081414Scindi 
9091414Scindi 	for (i = 0; i < npairs; i++)
9101414Scindi 		fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
9111414Scindi 
9121414Scindi 	if (snvl != NULL) {
9131414Scindi 		if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
9141414Scindi 			atomic_add_64(
9151414Scindi 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9161414Scindi 		}
9171414Scindi 	}
9180Sstevel@tonic-gate }
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate  * Set-up and validate the members of an dev fmri according to:
9220Sstevel@tonic-gate  *
9230Sstevel@tonic-gate  *	Member name		Type		Value
9240Sstevel@tonic-gate  *	====================================================
9250Sstevel@tonic-gate  *	version			uint8_t		0
9260Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
9270Sstevel@tonic-gate  *	devpath			string		<devpath>
9280Sstevel@tonic-gate  *	devid			string		<devid>
9290Sstevel@tonic-gate  *
9300Sstevel@tonic-gate  * Note that auth and devid are optional members.
9310Sstevel@tonic-gate  */
9320Sstevel@tonic-gate void
9330Sstevel@tonic-gate fm_fmri_dev_set(nvlist_t *fmri_dev, int version, const nvlist_t *auth,
9340Sstevel@tonic-gate     const char *devpath, const char *devid)
9350Sstevel@tonic-gate {
9360Sstevel@tonic-gate 	if (version != DEV_SCHEME_VERSION0) {
9370Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9380Sstevel@tonic-gate 		return;
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_dev, FM_VERSION, version) != 0) {
9420Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9430Sstevel@tonic-gate 		return;
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if (nvlist_add_string(fmri_dev, FM_FMRI_SCHEME,
9470Sstevel@tonic-gate 	    FM_FMRI_SCHEME_DEV) != 0) {
9480Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9490Sstevel@tonic-gate 		return;
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	if (auth != NULL) {
9530Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri_dev, FM_FMRI_AUTHORITY,
9540Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
9550Sstevel@tonic-gate 			atomic_add_64(
9560Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9570Sstevel@tonic-gate 		}
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if (nvlist_add_string(fmri_dev, FM_FMRI_DEV_PATH, devpath) != 0) {
9610Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	if (devid != NULL)
9650Sstevel@tonic-gate 		if (nvlist_add_string(fmri_dev, FM_FMRI_DEV_ID, devid) != 0)
9660Sstevel@tonic-gate 			atomic_add_64(
9670Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * Set-up and validate the members of an cpu fmri according to:
9720Sstevel@tonic-gate  *
9730Sstevel@tonic-gate  *	Member name		Type		Value
9740Sstevel@tonic-gate  *	====================================================
9750Sstevel@tonic-gate  *	version			uint8_t		0
9760Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
9770Sstevel@tonic-gate  *	cpuid			uint32_t	<cpu_id>
9780Sstevel@tonic-gate  *	cpumask			uint8_t		<cpu_mask>
9790Sstevel@tonic-gate  *	serial			uint64_t	<serial_id>
9800Sstevel@tonic-gate  *
9811414Scindi  * Note that auth, cpumask, serial are optional members.
9820Sstevel@tonic-gate  *
9830Sstevel@tonic-gate  */
9840Sstevel@tonic-gate void
9850Sstevel@tonic-gate fm_fmri_cpu_set(nvlist_t *fmri_cpu, int version, const nvlist_t *auth,
9861414Scindi     uint32_t cpu_id, uint8_t *cpu_maskp, const char *serial_idp)
9870Sstevel@tonic-gate {
9881414Scindi 	uint64_t *failedp = &erpt_kstat_data.fmri_set_failed.value.ui64;
9891414Scindi 
9901414Scindi 	if (version < CPU_SCHEME_VERSION1) {
9911414Scindi 		atomic_add_64(failedp, 1);
9920Sstevel@tonic-gate 		return;
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_cpu, FM_VERSION, version) != 0) {
9961414Scindi 		atomic_add_64(failedp, 1);
9970Sstevel@tonic-gate 		return;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if (nvlist_add_string(fmri_cpu, FM_FMRI_SCHEME,
10010Sstevel@tonic-gate 	    FM_FMRI_SCHEME_CPU) != 0) {
10021414Scindi 		atomic_add_64(failedp, 1);
10030Sstevel@tonic-gate 		return;
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 
10061414Scindi 	if (auth != NULL && nvlist_add_nvlist(fmri_cpu, FM_FMRI_AUTHORITY,
10071414Scindi 	    (nvlist_t *)auth) != 0)
10081414Scindi 		atomic_add_64(failedp, 1);
10091414Scindi 
10101414Scindi 	if (nvlist_add_uint32(fmri_cpu, FM_FMRI_CPU_ID, cpu_id) != 0)
10111414Scindi 		atomic_add_64(failedp, 1);
10120Sstevel@tonic-gate 
10131414Scindi 	if (cpu_maskp != NULL && nvlist_add_uint8(fmri_cpu, FM_FMRI_CPU_MASK,
10141414Scindi 	    *cpu_maskp) != 0)
10151414Scindi 		atomic_add_64(failedp, 1);
10160Sstevel@tonic-gate 
10171414Scindi 	if (serial_idp == NULL || nvlist_add_string(fmri_cpu,
10181414Scindi 	    FM_FMRI_CPU_SERIAL_ID, (char *)serial_idp) != 0)
10191414Scindi 			atomic_add_64(failedp, 1);
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate /*
10230Sstevel@tonic-gate  * Set-up and validate the members of a mem according to:
10240Sstevel@tonic-gate  *
10250Sstevel@tonic-gate  *	Member name		Type		Value
10260Sstevel@tonic-gate  *	====================================================
10270Sstevel@tonic-gate  *	version			uint8_t		0
10280Sstevel@tonic-gate  *	auth			nvlist_t	<auth>		[optional]
10290Sstevel@tonic-gate  *	unum			string		<unum>
10301186Sayznaga  *	serial			string		<serial>	[optional*]
10311186Sayznaga  *	offset			uint64_t	<offset>	[optional]
10320Sstevel@tonic-gate  *
10331186Sayznaga  *	* serial is required if offset is present
10340Sstevel@tonic-gate  */
10350Sstevel@tonic-gate void
10360Sstevel@tonic-gate fm_fmri_mem_set(nvlist_t *fmri, int version, const nvlist_t *auth,
10371186Sayznaga     const char *unum, const char *serial, uint64_t offset)
10380Sstevel@tonic-gate {
10390Sstevel@tonic-gate 	if (version != MEM_SCHEME_VERSION0) {
10400Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10410Sstevel@tonic-gate 		return;
10420Sstevel@tonic-gate 	}
10430Sstevel@tonic-gate 
10441186Sayznaga 	if (!serial && (offset != (uint64_t)-1)) {
10451186Sayznaga 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10461186Sayznaga 		return;
10471186Sayznaga 	}
10481186Sayznaga 
10490Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
10500Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10510Sstevel@tonic-gate 		return;
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MEM) != 0) {
10550Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10560Sstevel@tonic-gate 		return;
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	if (auth != NULL) {
10600Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
10610Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
10620Sstevel@tonic-gate 			atomic_add_64(
10630Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10640Sstevel@tonic-gate 		}
10650Sstevel@tonic-gate 	}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_MEM_UNUM, unum) != 0) {
10680Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	if (serial != NULL) {
10720Sstevel@tonic-gate 		if (nvlist_add_string_array(fmri, FM_FMRI_MEM_SERIAL_ID,
10730Sstevel@tonic-gate 		    (char **)&serial, 1) != 0) {
10740Sstevel@tonic-gate 			atomic_add_64(
10750Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10760Sstevel@tonic-gate 		}
10771186Sayznaga 		if (offset != (uint64_t)-1) {
10781186Sayznaga 			if (nvlist_add_uint64(fmri, FM_FMRI_MEM_OFFSET,
10791186Sayznaga 			    offset) != 0) {
10801186Sayznaga 				atomic_add_64(&erpt_kstat_data.
10811186Sayznaga 				    fmri_set_failed.value.ui64, 1);
10821186Sayznaga 			}
10831186Sayznaga 		}
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate 
10871544Seschrock void
10881544Seschrock fm_fmri_zfs_set(nvlist_t *fmri, int version, uint64_t pool_guid,
10891544Seschrock     uint64_t vdev_guid)
10901544Seschrock {
10911544Seschrock 	if (version != ZFS_SCHEME_VERSION0) {
10921544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10931544Seschrock 		return;
10941544Seschrock 	}
10951544Seschrock 
10961544Seschrock 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
10971544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10981544Seschrock 		return;
10991544Seschrock 	}
11001544Seschrock 
11011544Seschrock 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS) != 0) {
11021544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11031544Seschrock 		return;
11041544Seschrock 	}
11051544Seschrock 
11061544Seschrock 	if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_POOL, pool_guid) != 0) {
11071544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11081544Seschrock 	}
11091544Seschrock 
11101544Seschrock 	if (vdev_guid != 0) {
11111544Seschrock 		if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_VDEV, vdev_guid) != 0) {
11121544Seschrock 			atomic_add_64(
11131544Seschrock 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11141544Seschrock 		}
11151544Seschrock 	}
11161544Seschrock }
11171544Seschrock 
11180Sstevel@tonic-gate uint64_t
11190Sstevel@tonic-gate fm_ena_increment(uint64_t ena)
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 	uint64_t new_ena;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
11240Sstevel@tonic-gate 	case FM_ENA_FMT1:
11250Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT1_GEN_SHFT);
11260Sstevel@tonic-gate 		break;
11270Sstevel@tonic-gate 	case FM_ENA_FMT2:
11280Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT2_GEN_SHFT);
11290Sstevel@tonic-gate 		break;
11300Sstevel@tonic-gate 	default:
11310Sstevel@tonic-gate 		new_ena = 0;
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	return (new_ena);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate uint64_t
11380Sstevel@tonic-gate fm_ena_generate_cpu(uint64_t timestamp, processorid_t cpuid, uchar_t format)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	uint64_t ena = 0;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	switch (format) {
11430Sstevel@tonic-gate 	case FM_ENA_FMT1:
11440Sstevel@tonic-gate 		if (timestamp) {
11450Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11460Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
11470Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
11480Sstevel@tonic-gate 			    ((timestamp << ENA_FMT1_TIME_SHFT) &
11490Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
11500Sstevel@tonic-gate 		} else {
11510Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11520Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
11530Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
11540Sstevel@tonic-gate 			    ((gethrtime_waitfree() << ENA_FMT1_TIME_SHFT) &
11550Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		break;
11580Sstevel@tonic-gate 	case FM_ENA_FMT2:
11590Sstevel@tonic-gate 		ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11600Sstevel@tonic-gate 		    ((timestamp << ENA_FMT2_TIME_SHFT) & ENA_FMT2_TIME_MASK));
11610Sstevel@tonic-gate 		break;
11620Sstevel@tonic-gate 	default:
11630Sstevel@tonic-gate 		break;
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	return (ena);
11670Sstevel@tonic-gate }
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate uint64_t
11700Sstevel@tonic-gate fm_ena_generate(uint64_t timestamp, uchar_t format)
11710Sstevel@tonic-gate {
11720Sstevel@tonic-gate 	return (fm_ena_generate_cpu(timestamp, CPU->cpu_id, format));
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate uint64_t
11760Sstevel@tonic-gate fm_ena_generation_get(uint64_t ena)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	uint64_t gen;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
11810Sstevel@tonic-gate 	case FM_ENA_FMT1:
11820Sstevel@tonic-gate 		gen = (ena & ENA_FMT1_GEN_MASK) >> ENA_FMT1_GEN_SHFT;
11830Sstevel@tonic-gate 		break;
11840Sstevel@tonic-gate 	case FM_ENA_FMT2:
11850Sstevel@tonic-gate 		gen = (ena & ENA_FMT2_GEN_MASK) >> ENA_FMT2_GEN_SHFT;
11860Sstevel@tonic-gate 		break;
11870Sstevel@tonic-gate 	default:
11880Sstevel@tonic-gate 		gen = 0;
11890Sstevel@tonic-gate 		break;
11900Sstevel@tonic-gate 	}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	return (gen);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate uchar_t
11960Sstevel@tonic-gate fm_ena_format_get(uint64_t ena)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	return (ENA_FORMAT(ena));
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate uint64_t
12030Sstevel@tonic-gate fm_ena_id_get(uint64_t ena)
12040Sstevel@tonic-gate {
12050Sstevel@tonic-gate 	uint64_t id;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
12080Sstevel@tonic-gate 	case FM_ENA_FMT1:
12090Sstevel@tonic-gate 		id = (ena & ENA_FMT1_ID_MASK) >> ENA_FMT1_ID_SHFT;
12100Sstevel@tonic-gate 		break;
12110Sstevel@tonic-gate 	case FM_ENA_FMT2:
12120Sstevel@tonic-gate 		id = (ena & ENA_FMT2_ID_MASK) >> ENA_FMT2_ID_SHFT;
12130Sstevel@tonic-gate 		break;
12140Sstevel@tonic-gate 	default:
12150Sstevel@tonic-gate 		id = 0;
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	return (id);
12190Sstevel@tonic-gate }
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate uint64_t
12220Sstevel@tonic-gate fm_ena_time_get(uint64_t ena)
12230Sstevel@tonic-gate {
12240Sstevel@tonic-gate 	uint64_t time;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
12270Sstevel@tonic-gate 	case FM_ENA_FMT1:
12280Sstevel@tonic-gate 		time = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
12290Sstevel@tonic-gate 		break;
12300Sstevel@tonic-gate 	case FM_ENA_FMT2:
12310Sstevel@tonic-gate 		time = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
12320Sstevel@tonic-gate 		break;
12330Sstevel@tonic-gate 	default:
12340Sstevel@tonic-gate 		time = 0;
12350Sstevel@tonic-gate 	}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	return (time);
12380Sstevel@tonic-gate }
12391414Scindi 
12401414Scindi /*
12411414Scindi  * Convert a getpcstack() trace to symbolic name+offset, and add the resulting
12421414Scindi  * string array to a Fault Management ereport as FM_EREPORT_PAYLOAD_NAME_STACK.
12431414Scindi  */
12441414Scindi void
12451414Scindi fm_payload_stack_add(nvlist_t *payload, const pc_t *stack, int depth)
12461414Scindi {
12471414Scindi 	int i;
12481414Scindi 	char *sym;
12491414Scindi 	ulong_t off;
12501414Scindi 	char *stkpp[FM_STK_DEPTH];
12511414Scindi 	char buf[FM_STK_DEPTH * FM_SYM_SZ];
12521414Scindi 	char *stkp = buf;
12531414Scindi 
12541414Scindi 	for (i = 0; i < depth && i != FM_STK_DEPTH; i++, stkp += FM_SYM_SZ) {
12551414Scindi 		if ((sym = kobj_getsymname(stack[i], &off)) != NULL)
12561414Scindi 			(void) snprintf(stkp, FM_SYM_SZ, "%s+%lx", sym, off);
12571414Scindi 		else
12581414Scindi 			(void) snprintf(stkp, FM_SYM_SZ, "%lx", (long)stack[i]);
12591414Scindi 		stkpp[i] = stkp;
12601414Scindi 	}
12611414Scindi 
12621414Scindi 	fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_STACK,
12631437Sdilpreet 	    DATA_TYPE_STRING_ARRAY, depth, stkpp, NULL);
12641414Scindi }
12659613SAbhinandan.Ekande@Sun.COM 
12669613SAbhinandan.Ekande@Sun.COM void
12679613SAbhinandan.Ekande@Sun.COM print_msg_hwerr(ctid_t ct_id, proc_t *p)
12689613SAbhinandan.Ekande@Sun.COM {
12699613SAbhinandan.Ekande@Sun.COM 	uprintf("Killed process %d (%s) in contract id %d "
12709613SAbhinandan.Ekande@Sun.COM 	    "due to hardware error\n", p->p_pid, p->p_user.u_comm, ct_id);
12719613SAbhinandan.Ekande@Sun.COM }
1272