xref: /onnv-gate/usr/src/uts/common/os/fm.c (revision 12967:ab9ae749152f)
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 /*
2212213SGavin.Maltby@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * Fault Management Architecture (FMA) Resource and Protocol Support
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * The routines contained herein provide services to support kernel subsystems
290Sstevel@tonic-gate  * in publishing fault management telemetry (see PSARC 2002/412 and 2003/089).
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * Name-Value Pair Lists
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * The embodiment of an FMA protocol element (event, fmri or authority) is a
340Sstevel@tonic-gate  * name-value pair list (nvlist_t).  FMA-specific nvlist construtor and
350Sstevel@tonic-gate  * destructor functions, fm_nvlist_create() and fm_nvlist_destroy(), are used
360Sstevel@tonic-gate  * to create an nvpair list using custom allocators.  Callers may choose to
370Sstevel@tonic-gate  * allocate either from the kernel memory allocator, or from a preallocated
380Sstevel@tonic-gate  * buffer, useful in constrained contexts like high-level interrupt routines.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  * Protocol Event and FMRI Construction
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * Convenience routines are provided to construct nvlist events according to
430Sstevel@tonic-gate  * the FMA Event Protocol and Naming Schema specification for ereports and
440Sstevel@tonic-gate  * FMRIs for the dev, cpu, hc, mem, legacy hc and de schemes.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * ENA Manipulation
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * Routines to generate ENA formats 0, 1 and 2 are available as well as
490Sstevel@tonic-gate  * routines to increment formats 1 and 2.  Individual fields within the
500Sstevel@tonic-gate  * ENA are extractable via fm_ena_time_get(), fm_ena_id_get(),
510Sstevel@tonic-gate  * fm_ena_format_get() and fm_ena_gen_get().
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <sys/time.h>
560Sstevel@tonic-gate #include <sys/sysevent.h>
570Sstevel@tonic-gate #include <sys/sysevent_impl.h>
580Sstevel@tonic-gate #include <sys/nvpair.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/cpuvar.h>
610Sstevel@tonic-gate #include <sys/sysmacros.h>
620Sstevel@tonic-gate #include <sys/systm.h>
630Sstevel@tonic-gate #include <sys/ddifm.h>
640Sstevel@tonic-gate #include <sys/ddifm_impl.h>
650Sstevel@tonic-gate #include <sys/spl.h>
660Sstevel@tonic-gate #include <sys/dumphdr.h>
670Sstevel@tonic-gate #include <sys/compress.h>
680Sstevel@tonic-gate #include <sys/cpuvar.h>
690Sstevel@tonic-gate #include <sys/console.h>
700Sstevel@tonic-gate #include <sys/panic.h>
711414Scindi #include <sys/kobj.h>
721414Scindi #include <sys/sunddi.h>
730Sstevel@tonic-gate #include <sys/systeminfo.h>
740Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
750Sstevel@tonic-gate #include <sys/fm/util.h>
760Sstevel@tonic-gate #include <sys/fm/protocol.h>
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * URL and SUNW-MSG-ID value to display for fm_panic(), defined below.  These
800Sstevel@tonic-gate  * values must be kept in sync with the FMA source code in usr/src/cmd/fm.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static const char *fm_url = "http://www.sun.com/msg";
830Sstevel@tonic-gate static const char *fm_msgid = "SUNOS-8000-0G";
840Sstevel@tonic-gate static char *volatile fm_panicstr = NULL;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate errorq_t *ereport_errorq;
870Sstevel@tonic-gate void *ereport_dumpbuf;
880Sstevel@tonic-gate size_t ereport_dumplen;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static uint_t ereport_chanlen = ERPT_EVCH_MAX;
910Sstevel@tonic-gate static evchan_t *ereport_chan = NULL;
920Sstevel@tonic-gate static ulong_t ereport_qlen = 0;
930Sstevel@tonic-gate static size_t ereport_size = 0;
940Sstevel@tonic-gate static int ereport_cols = 80;
950Sstevel@tonic-gate 
9610664SAdrian.Frost@Sun.COM extern void fastreboot_disable_highpil(void);
9710664SAdrian.Frost@Sun.COM 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Common fault management kstats to record ereport generation
1000Sstevel@tonic-gate  * failures
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate struct erpt_kstat {
1040Sstevel@tonic-gate 	kstat_named_t	erpt_dropped;		/* num erpts dropped on post */
1050Sstevel@tonic-gate 	kstat_named_t	erpt_set_failed;	/* num erpt set failures */
1060Sstevel@tonic-gate 	kstat_named_t	fmri_set_failed;	/* num fmri set failures */
1070Sstevel@tonic-gate 	kstat_named_t	payload_set_failed;	/* num payload set failures */
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static struct erpt_kstat erpt_kstat_data = {
1110Sstevel@tonic-gate 	{ "erpt-dropped", KSTAT_DATA_UINT64 },
1120Sstevel@tonic-gate 	{ "erpt-set-failed", KSTAT_DATA_UINT64 },
1130Sstevel@tonic-gate 	{ "fmri-set-failed", KSTAT_DATA_UINT64 },
1140Sstevel@tonic-gate 	{ "payload-set-failed", KSTAT_DATA_UINT64 }
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*ARGSUSED*/
1180Sstevel@tonic-gate static void
fm_drain(void * private,void * data,errorq_elem_t * eep)1190Sstevel@tonic-gate fm_drain(void *private, void *data, errorq_elem_t *eep)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	nvlist_t *nvl = errorq_elem_nvl(ereport_errorq, eep);
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (!panicstr)
1240Sstevel@tonic-gate 		(void) fm_ereport_post(nvl, EVCH_TRYHARD);
1250Sstevel@tonic-gate 	else
1260Sstevel@tonic-gate 		fm_nvprint(nvl);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate void
fm_init(void)1300Sstevel@tonic-gate fm_init(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	kstat_t *ksp;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	(void) sysevent_evc_bind(FM_ERROR_CHAN,
1350Sstevel@tonic-gate 	    &ereport_chan, EVCH_CREAT | EVCH_HOLD_PEND);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	(void) sysevent_evc_control(ereport_chan,
1380Sstevel@tonic-gate 	    EVCH_SET_CHAN_LEN, &ereport_chanlen);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (ereport_qlen == 0)
1410Sstevel@tonic-gate 		ereport_qlen = ERPT_MAX_ERRS * MAX(max_ncpus, 4);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	if (ereport_size == 0)
1440Sstevel@tonic-gate 		ereport_size = ERPT_DATA_SZ;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	ereport_errorq = errorq_nvcreate("fm_ereport_queue",
1470Sstevel@tonic-gate 	    (errorq_func_t)fm_drain, NULL, ereport_qlen, ereport_size,
1480Sstevel@tonic-gate 	    FM_ERR_PIL, ERRORQ_VITAL);
1490Sstevel@tonic-gate 	if (ereport_errorq == NULL)
1500Sstevel@tonic-gate 		panic("failed to create required ereport error queue");
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	ereport_dumpbuf = kmem_alloc(ereport_size, KM_SLEEP);
1530Sstevel@tonic-gate 	ereport_dumplen = ereport_size;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/* Initialize ereport allocation and generation kstats */
1560Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "fm", "misc", KSTAT_TYPE_NAMED,
1570Sstevel@tonic-gate 	    sizeof (struct erpt_kstat) / sizeof (kstat_named_t),
1580Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (ksp != NULL) {
1610Sstevel@tonic-gate 		ksp->ks_data = &erpt_kstat_data;
1620Sstevel@tonic-gate 		kstat_install(ksp);
1630Sstevel@tonic-gate 	} else {
1640Sstevel@tonic-gate 		cmn_err(CE_NOTE, "failed to create fm/misc kstat\n");
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Formatting utility function for fm_nvprintr.  We attempt to wrap chunks of
1710Sstevel@tonic-gate  * output so they aren't split across console lines, and return the end column.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate /*PRINTFLIKE4*/
1740Sstevel@tonic-gate static int
fm_printf(int depth,int c,int cols,const char * format,...)1750Sstevel@tonic-gate fm_printf(int depth, int c, int cols, const char *format, ...)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	va_list ap;
1780Sstevel@tonic-gate 	int width;
1790Sstevel@tonic-gate 	char c1;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	va_start(ap, format);
1820Sstevel@tonic-gate 	width = vsnprintf(&c1, sizeof (c1), format, ap);
1830Sstevel@tonic-gate 	va_end(ap);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (c + width >= cols) {
1860Sstevel@tonic-gate 		console_printf("\n\r");
1870Sstevel@tonic-gate 		c = 0;
1880Sstevel@tonic-gate 		if (format[0] != ' ' && depth > 0) {
1890Sstevel@tonic-gate 			console_printf(" ");
1900Sstevel@tonic-gate 			c++;
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	va_start(ap, format);
1950Sstevel@tonic-gate 	console_vprintf(format, ap);
1960Sstevel@tonic-gate 	va_end(ap);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	return ((c + width) % cols);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Recursively print a nvlist in the specified column width and return the
2030Sstevel@tonic-gate  * column we end up in.  This function is called recursively by fm_nvprint(),
2040Sstevel@tonic-gate  * below.  We generically format the entire nvpair using hexadecimal
2050Sstevel@tonic-gate  * integers and strings, and elide any integer arrays.  Arrays are basically
2060Sstevel@tonic-gate  * used for cache dumps right now, so we suppress them so as not to overwhelm
2070Sstevel@tonic-gate  * the amount of console output we produce at panic time.  This can be further
2080Sstevel@tonic-gate  * enhanced as FMA technology grows based upon the needs of consumers.  All
2090Sstevel@tonic-gate  * FMA telemetry is logged using the dump device transport, so the console
2100Sstevel@tonic-gate  * output serves only as a fallback in case this procedure is unsuccessful.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate static int
fm_nvprintr(nvlist_t * nvl,int d,int c,int cols)2130Sstevel@tonic-gate fm_nvprintr(nvlist_t *nvl, int d, int c, int cols)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	nvpair_t *nvp;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	for (nvp = nvlist_next_nvpair(nvl, NULL);
2180Sstevel@tonic-gate 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 		data_type_t type = nvpair_type(nvp);
2210Sstevel@tonic-gate 		const char *name = nvpair_name(nvp);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		boolean_t b;
2240Sstevel@tonic-gate 		uint8_t i8;
2250Sstevel@tonic-gate 		uint16_t i16;
2260Sstevel@tonic-gate 		uint32_t i32;
2270Sstevel@tonic-gate 		uint64_t i64;
2280Sstevel@tonic-gate 		char *str;
2290Sstevel@tonic-gate 		nvlist_t *cnv;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		if (strcmp(name, FM_CLASS) == 0)
2320Sstevel@tonic-gate 			continue; /* already printed by caller */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		c = fm_printf(d, c, cols, " %s=", name);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		switch (type) {
2370Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN:
2380Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " 1");
2390Sstevel@tonic-gate 			break;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
2420Sstevel@tonic-gate 			(void) nvpair_value_boolean_value(nvp, &b);
2430Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, b ? "1" : "0");
2440Sstevel@tonic-gate 			break;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
2470Sstevel@tonic-gate 			(void) nvpair_value_byte(nvp, &i8);
2480Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2490Sstevel@tonic-gate 			break;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 		case DATA_TYPE_INT8:
2520Sstevel@tonic-gate 			(void) nvpair_value_int8(nvp, (void *)&i8);
2530Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
2570Sstevel@tonic-gate 			(void) nvpair_value_uint8(nvp, &i8);
2580Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
2590Sstevel@tonic-gate 			break;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		case DATA_TYPE_INT16:
2620Sstevel@tonic-gate 			(void) nvpair_value_int16(nvp, (void *)&i16);
2630Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
2640Sstevel@tonic-gate 			break;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
2670Sstevel@tonic-gate 			(void) nvpair_value_uint16(nvp, &i16);
2680Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
2690Sstevel@tonic-gate 			break;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		case DATA_TYPE_INT32:
2720Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (void *)&i32);
2730Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
2740Sstevel@tonic-gate 			break;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
2770Sstevel@tonic-gate 			(void) nvpair_value_uint32(nvp, &i32);
2780Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
2790Sstevel@tonic-gate 			break;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		case DATA_TYPE_INT64:
2820Sstevel@tonic-gate 			(void) nvpair_value_int64(nvp, (void *)&i64);
2830Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2840Sstevel@tonic-gate 			    (u_longlong_t)i64);
2850Sstevel@tonic-gate 			break;
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
2880Sstevel@tonic-gate 			(void) nvpair_value_uint64(nvp, &i64);
2890Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2900Sstevel@tonic-gate 			    (u_longlong_t)i64);
2910Sstevel@tonic-gate 			break;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		case DATA_TYPE_HRTIME:
2940Sstevel@tonic-gate 			(void) nvpair_value_hrtime(nvp, (void *)&i64);
2950Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
2960Sstevel@tonic-gate 			    (u_longlong_t)i64);
2970Sstevel@tonic-gate 			break;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		case DATA_TYPE_STRING:
3000Sstevel@tonic-gate 			(void) nvpair_value_string(nvp, &str);
3010Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "\"%s\"",
3020Sstevel@tonic-gate 			    str ? str : "<NULL>");
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
3060Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[");
3070Sstevel@tonic-gate 			(void) nvpair_value_nvlist(nvp, &cnv);
3080Sstevel@tonic-gate 			c = fm_nvprintr(cnv, d + 1, c, cols);
3090Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " ]");
3100Sstevel@tonic-gate 			break;
3110Sstevel@tonic-gate 
3127532SSean.Ye@Sun.COM 		case DATA_TYPE_NVLIST_ARRAY: {
3137532SSean.Ye@Sun.COM 			nvlist_t **val;
3147532SSean.Ye@Sun.COM 			uint_t i, nelem;
3157532SSean.Ye@Sun.COM 
3167532SSean.Ye@Sun.COM 			c = fm_printf(d + 1, c, cols, "[");
3177532SSean.Ye@Sun.COM 			(void) nvpair_value_nvlist_array(nvp, &val, &nelem);
3187532SSean.Ye@Sun.COM 			for (i = 0; i < nelem; i++) {
3197532SSean.Ye@Sun.COM 				c = fm_nvprintr(val[i], d + 1, c, cols);
3207532SSean.Ye@Sun.COM 			}
3217532SSean.Ye@Sun.COM 			c = fm_printf(d + 1, c, cols, " ]");
3227532SSean.Ye@Sun.COM 			}
3237532SSean.Ye@Sun.COM 			break;
3247532SSean.Ye@Sun.COM 
3250Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
3260Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
3270Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
3280Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
3290Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
3300Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
3310Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
3320Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
3330Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
3340Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
3350Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
3360Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[...]");
3370Sstevel@tonic-gate 			break;
3380Sstevel@tonic-gate 		case DATA_TYPE_UNKNOWN:
3390Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "<unknown>");
3400Sstevel@tonic-gate 			break;
3410Sstevel@tonic-gate 		}
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (c);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate void
fm_nvprint(nvlist_t * nvl)3480Sstevel@tonic-gate fm_nvprint(nvlist_t *nvl)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	char *class;
3510Sstevel@tonic-gate 	int c = 0;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	console_printf("\r");
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) == 0)
3560Sstevel@tonic-gate 		c = fm_printf(0, c, ereport_cols, "%s", class);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	if (fm_nvprintr(nvl, 0, c, ereport_cols) != 0)
3590Sstevel@tonic-gate 		console_printf("\n");
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	console_printf("\n");
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * Wrapper for panic() that first produces an FMA-style message for admins.
3660Sstevel@tonic-gate  * Normally such messages are generated by fmd(1M)'s syslog-msgs agent: this
3670Sstevel@tonic-gate  * is the one exception to that rule and the only error that gets messaged.
3680Sstevel@tonic-gate  * This function is intended for use by subsystems that have detected a fatal
3690Sstevel@tonic-gate  * error and enqueued appropriate ereports and wish to then force a panic.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate /*PRINTFLIKE1*/
3720Sstevel@tonic-gate void
fm_panic(const char * format,...)3730Sstevel@tonic-gate fm_panic(const char *format, ...)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	va_list ap;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	(void) casptr((void *)&fm_panicstr, NULL, (void *)format);
37810664SAdrian.Frost@Sun.COM #if defined(__i386) || defined(__amd64)
37910664SAdrian.Frost@Sun.COM 	fastreboot_disable_highpil();
38010664SAdrian.Frost@Sun.COM #endif /* __i386 || __amd64 */
3810Sstevel@tonic-gate 	va_start(ap, format);
3820Sstevel@tonic-gate 	vpanic(format, ap);
3830Sstevel@tonic-gate 	va_end(ap);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*
387*12967Sgavin.maltby@oracle.com  * Simply tell the caller if fm_panicstr is set, ie. an fma event has
388*12967Sgavin.maltby@oracle.com  * caused the panic. If so, something other than the default panic
389*12967Sgavin.maltby@oracle.com  * diagnosis method will diagnose the cause of the panic.
390*12967Sgavin.maltby@oracle.com  */
391*12967Sgavin.maltby@oracle.com int
is_fm_panic()392*12967Sgavin.maltby@oracle.com is_fm_panic()
393*12967Sgavin.maltby@oracle.com {
394*12967Sgavin.maltby@oracle.com 	if (fm_panicstr)
395*12967Sgavin.maltby@oracle.com 		return (1);
396*12967Sgavin.maltby@oracle.com 	else
397*12967Sgavin.maltby@oracle.com 		return (0);
398*12967Sgavin.maltby@oracle.com }
399*12967Sgavin.maltby@oracle.com 
400*12967Sgavin.maltby@oracle.com /*
4010Sstevel@tonic-gate  * Print any appropriate FMA banner message before the panic message.  This
4020Sstevel@tonic-gate  * function is called by panicsys() and prints the message for fm_panic().
4030Sstevel@tonic-gate  * We print the message here so that it comes after the system is quiesced.
4040Sstevel@tonic-gate  * A one-line summary is recorded in the log only (cmn_err(9F) with "!" prefix).
4050Sstevel@tonic-gate  * The rest of the message is for the console only and not needed in the log,
4060Sstevel@tonic-gate  * so it is printed using console_printf().  We break it up into multiple
4070Sstevel@tonic-gate  * chunks so as to avoid overflowing any small legacy prom_printf() buffers.
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate void
fm_banner(void)4100Sstevel@tonic-gate fm_banner(void)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	timespec_t tod;
4130Sstevel@tonic-gate 	hrtime_t now;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (!fm_panicstr)
4160Sstevel@tonic-gate 		return; /* panic was not initiated by fm_panic(); do nothing */
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	if (panicstr) {
4190Sstevel@tonic-gate 		tod = panic_hrestime;
4200Sstevel@tonic-gate 		now = panic_hrtime;
4210Sstevel@tonic-gate 	} else {
4220Sstevel@tonic-gate 		gethrestime(&tod);
4230Sstevel@tonic-gate 		now = gethrtime_waitfree();
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!SUNW-MSG-ID: %s, "
4270Sstevel@tonic-gate 	    "TYPE: Error, VER: 1, SEVERITY: Major\n", fm_msgid);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	console_printf(
4300Sstevel@tonic-gate "\n\rSUNW-MSG-ID: %s, TYPE: Error, VER: 1, SEVERITY: Major\n"
4310Sstevel@tonic-gate "EVENT-TIME: 0x%lx.0x%lx (0x%llx)\n",
4320Sstevel@tonic-gate 	    fm_msgid, tod.tv_sec, tod.tv_nsec, (u_longlong_t)now);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	console_printf(
4356014Ssuha "PLATFORM: %s, CSN: -, HOSTNAME: %s\n"
4360Sstevel@tonic-gate "SOURCE: %s, REV: %s %s\n",
4370Sstevel@tonic-gate 	    platform, utsname.nodename, utsname.sysname,
4380Sstevel@tonic-gate 	    utsname.release, utsname.version);
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	console_printf(
4410Sstevel@tonic-gate "DESC: Errors have been detected that require a reboot to ensure system\n"
4420Sstevel@tonic-gate "integrity.  See %s/%s for more information.\n",
4430Sstevel@tonic-gate 	    fm_url, fm_msgid);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	console_printf(
4460Sstevel@tonic-gate "AUTO-RESPONSE: Solaris will attempt to save and diagnose the error telemetry\n"
4470Sstevel@tonic-gate "IMPACT: The system will sync files, save a crash dump if needed, and reboot\n"
4480Sstevel@tonic-gate "REC-ACTION: Save the error summary below in case telemetry cannot be saved\n");
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	console_printf("\n");
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * Utility function to write all of the pending ereports to the dump device.
4550Sstevel@tonic-gate  * This function is called at either normal reboot or panic time, and simply
4560Sstevel@tonic-gate  * iterates over the in-transit messages in the ereport sysevent channel.
4570Sstevel@tonic-gate  */
4580Sstevel@tonic-gate void
fm_ereport_dump(void)4590Sstevel@tonic-gate fm_ereport_dump(void)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	evchanq_t *chq;
4620Sstevel@tonic-gate 	sysevent_t *sep;
4630Sstevel@tonic-gate 	erpt_dump_t ed;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	timespec_t tod;
4660Sstevel@tonic-gate 	hrtime_t now;
4670Sstevel@tonic-gate 	char *buf;
4680Sstevel@tonic-gate 	size_t len;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (panicstr) {
4710Sstevel@tonic-gate 		tod = panic_hrestime;
4720Sstevel@tonic-gate 		now = panic_hrtime;
4730Sstevel@tonic-gate 	} else {
4740Sstevel@tonic-gate 		if (ereport_errorq != NULL)
4750Sstevel@tonic-gate 			errorq_drain(ereport_errorq);
4760Sstevel@tonic-gate 		gethrestime(&tod);
4770Sstevel@tonic-gate 		now = gethrtime_waitfree();
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	/*
4810Sstevel@tonic-gate 	 * In the panic case, sysevent_evc_walk_init() will return NULL.
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 	if ((chq = sysevent_evc_walk_init(ereport_chan, NULL)) == NULL &&
4840Sstevel@tonic-gate 	    !panicstr)
4850Sstevel@tonic-gate 		return; /* event channel isn't initialized yet */
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	while ((sep = sysevent_evc_walk_step(chq)) != NULL) {
4880Sstevel@tonic-gate 		if ((buf = sysevent_evc_event_attr(sep, &len)) == NULL)
4890Sstevel@tonic-gate 			break;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 		ed.ed_magic = ERPT_MAGIC;
4920Sstevel@tonic-gate 		ed.ed_chksum = checksum32(buf, len);
4930Sstevel@tonic-gate 		ed.ed_size = (uint32_t)len;
4940Sstevel@tonic-gate 		ed.ed_pad = 0;
4950Sstevel@tonic-gate 		ed.ed_hrt_nsec = SE_TIME(sep);
4960Sstevel@tonic-gate 		ed.ed_hrt_base = now;
4970Sstevel@tonic-gate 		ed.ed_tod_base.sec = tod.tv_sec;
4980Sstevel@tonic-gate 		ed.ed_tod_base.nsec = tod.tv_nsec;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		dumpvp_write(&ed, sizeof (ed));
5010Sstevel@tonic-gate 		dumpvp_write(buf, len);
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	sysevent_evc_walk_fini(chq);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate  * Post an error report (ereport) to the sysevent error channel.  The error
5090Sstevel@tonic-gate  * channel must be established with a prior call to sysevent_evc_create()
5100Sstevel@tonic-gate  * before publication may occur.
5110Sstevel@tonic-gate  */
5120Sstevel@tonic-gate void
fm_ereport_post(nvlist_t * ereport,int evc_flag)5130Sstevel@tonic-gate fm_ereport_post(nvlist_t *ereport, int evc_flag)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	size_t nvl_size = 0;
5160Sstevel@tonic-gate 	evchan_t *error_chan;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	(void) nvlist_size(ereport, &nvl_size, NV_ENCODE_NATIVE);
5190Sstevel@tonic-gate 	if (nvl_size > ERPT_DATA_SZ || nvl_size == 0) {
5200Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
5210Sstevel@tonic-gate 		return;
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (sysevent_evc_bind(FM_ERROR_CHAN, &error_chan,
5250Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
5260Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
5270Sstevel@tonic-gate 		return;
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (sysevent_evc_publish(error_chan, EC_FM, ESC_FM_ERROR,
5310Sstevel@tonic-gate 	    SUNW_VENDOR, FM_PUB, ereport, evc_flag) != 0) {
5320Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
53311102SGavin.Maltby@Sun.COM 		(void) sysevent_evc_unbind(error_chan);
5340Sstevel@tonic-gate 		return;
5350Sstevel@tonic-gate 	}
53611102SGavin.Maltby@Sun.COM 	(void) sysevent_evc_unbind(error_chan);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * Wrapppers for FM nvlist allocators
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate /* ARGSUSED */
5430Sstevel@tonic-gate static void *
i_fm_alloc(nv_alloc_t * nva,size_t size)5440Sstevel@tonic-gate i_fm_alloc(nv_alloc_t *nva, size_t size)
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 	return (kmem_zalloc(size, KM_SLEEP));
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /* ARGSUSED */
5500Sstevel@tonic-gate static void
i_fm_free(nv_alloc_t * nva,void * buf,size_t size)5510Sstevel@tonic-gate i_fm_free(nv_alloc_t *nva, void *buf, size_t size)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate 	kmem_free(buf, size);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate const nv_alloc_ops_t fm_mem_alloc_ops = {
5570Sstevel@tonic-gate 	NULL,
5580Sstevel@tonic-gate 	NULL,
5590Sstevel@tonic-gate 	i_fm_alloc,
5600Sstevel@tonic-gate 	i_fm_free,
5610Sstevel@tonic-gate 	NULL
5620Sstevel@tonic-gate };
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Create and initialize a new nv_alloc_t for a fixed buffer, buf.  A pointer
5660Sstevel@tonic-gate  * to the newly allocated nv_alloc_t structure is returned upon success or NULL
5670Sstevel@tonic-gate  * is returned to indicate that the nv_alloc structure could not be created.
5680Sstevel@tonic-gate  */
5690Sstevel@tonic-gate nv_alloc_t *
fm_nva_xcreate(char * buf,size_t bufsz)5700Sstevel@tonic-gate fm_nva_xcreate(char *buf, size_t bufsz)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	nv_alloc_t *nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if (bufsz == 0 || nv_alloc_init(nvhdl, nv_fixed_ops, buf, bufsz) != 0) {
5750Sstevel@tonic-gate 		kmem_free(nvhdl, sizeof (nv_alloc_t));
5760Sstevel@tonic-gate 		return (NULL);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	return (nvhdl);
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate /*
5830Sstevel@tonic-gate  * Destroy a previously allocated nv_alloc structure.  The fixed buffer
5840Sstevel@tonic-gate  * associated with nva must be freed by the caller.
5850Sstevel@tonic-gate  */
5860Sstevel@tonic-gate void
fm_nva_xdestroy(nv_alloc_t * nva)5870Sstevel@tonic-gate fm_nva_xdestroy(nv_alloc_t *nva)
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	nv_alloc_fini(nva);
5900Sstevel@tonic-gate 	kmem_free(nva, sizeof (nv_alloc_t));
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  * Create a new nv list.  A pointer to a new nv list structure is returned
5950Sstevel@tonic-gate  * upon success or NULL is returned to indicate that the structure could
5960Sstevel@tonic-gate  * not be created.  The newly created nv list is created and managed by the
5970Sstevel@tonic-gate  * operations installed in nva.   If nva is NULL, the default FMA nva
5980Sstevel@tonic-gate  * operations are installed and used.
5990Sstevel@tonic-gate  *
6000Sstevel@tonic-gate  * When called from the kernel and nva == NULL, this function must be called
6010Sstevel@tonic-gate  * from passive kernel context with no locks held that can prevent a
6020Sstevel@tonic-gate  * sleeping memory allocation from occurring.  Otherwise, this function may
6030Sstevel@tonic-gate  * be called from other kernel contexts as long a valid nva created via
6040Sstevel@tonic-gate  * fm_nva_create() is supplied.
6050Sstevel@tonic-gate  */
6060Sstevel@tonic-gate nvlist_t *
fm_nvlist_create(nv_alloc_t * nva)6070Sstevel@tonic-gate fm_nvlist_create(nv_alloc_t *nva)
6080Sstevel@tonic-gate {
6090Sstevel@tonic-gate 	int hdl_alloced = 0;
6100Sstevel@tonic-gate 	nvlist_t *nvl;
6110Sstevel@tonic-gate 	nv_alloc_t *nvhdl;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (nva == NULL) {
6140Sstevel@tonic-gate 		nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 		if (nv_alloc_init(nvhdl, &fm_mem_alloc_ops, NULL, 0) != 0) {
6170Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
6180Sstevel@tonic-gate 			return (NULL);
6190Sstevel@tonic-gate 		}
6200Sstevel@tonic-gate 		hdl_alloced = 1;
6210Sstevel@tonic-gate 	} else {
6220Sstevel@tonic-gate 		nvhdl = nva;
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nvhdl) != 0) {
6260Sstevel@tonic-gate 		if (hdl_alloced) {
62712726SJakub.Jermar@Sun.COM 			nv_alloc_fini(nvhdl);
6280Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 		return (NULL);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	return (nvl);
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate  * Destroy a previously allocated nvlist structure.  flag indicates whether
6380Sstevel@tonic-gate  * or not the associated nva structure should be freed (FM_NVA_FREE) or
6390Sstevel@tonic-gate  * retained (FM_NVA_RETAIN).  Retaining the nv alloc structure allows
6400Sstevel@tonic-gate  * it to be re-used for future nvlist creation operations.
6410Sstevel@tonic-gate  */
6420Sstevel@tonic-gate void
fm_nvlist_destroy(nvlist_t * nvl,int flag)6430Sstevel@tonic-gate fm_nvlist_destroy(nvlist_t *nvl, int flag)
6440Sstevel@tonic-gate {
6451414Scindi 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(nvl);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	nvlist_free(nvl);
6480Sstevel@tonic-gate 
6491414Scindi 	if (nva != NULL) {
6500Sstevel@tonic-gate 		if (flag == FM_NVA_FREE)
6511414Scindi 			fm_nva_xdestroy(nva);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate int
i_fm_payload_set(nvlist_t * payload,const char * name,va_list ap)6560Sstevel@tonic-gate i_fm_payload_set(nvlist_t *payload, const char *name, va_list ap)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	int nelem, ret = 0;
6590Sstevel@tonic-gate 	data_type_t type;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	while (ret == 0 && name != NULL) {
6620Sstevel@tonic-gate 		type = va_arg(ap, data_type_t);
6630Sstevel@tonic-gate 		switch (type) {
6640Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
6650Sstevel@tonic-gate 			ret = nvlist_add_byte(payload, name,
6660Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6670Sstevel@tonic-gate 			break;
6680Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
6690Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6700Sstevel@tonic-gate 			ret = nvlist_add_byte_array(payload, name,
6710Sstevel@tonic-gate 			    va_arg(ap, uchar_t *), nelem);
6720Sstevel@tonic-gate 			break;
6730Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
6740Sstevel@tonic-gate 			ret = nvlist_add_boolean_value(payload, name,
6750Sstevel@tonic-gate 			    va_arg(ap, boolean_t));
6760Sstevel@tonic-gate 			break;
6770Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
6780Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6790Sstevel@tonic-gate 			ret = nvlist_add_boolean_array(payload, name,
6800Sstevel@tonic-gate 			    va_arg(ap, boolean_t *), nelem);
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		case DATA_TYPE_INT8:
6830Sstevel@tonic-gate 			ret = nvlist_add_int8(payload, name,
6840Sstevel@tonic-gate 			    va_arg(ap, int));
6850Sstevel@tonic-gate 			break;
6860Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
6870Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6880Sstevel@tonic-gate 			ret = nvlist_add_int8_array(payload, name,
6890Sstevel@tonic-gate 			    va_arg(ap, int8_t *), nelem);
6900Sstevel@tonic-gate 			break;
6910Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
6920Sstevel@tonic-gate 			ret = nvlist_add_uint8(payload, name,
6930Sstevel@tonic-gate 			    va_arg(ap, uint_t));
6940Sstevel@tonic-gate 			break;
6950Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
6960Sstevel@tonic-gate 			nelem = va_arg(ap, int);
6970Sstevel@tonic-gate 			ret = nvlist_add_uint8_array(payload, name,
6980Sstevel@tonic-gate 			    va_arg(ap, uint8_t *), nelem);
6990Sstevel@tonic-gate 			break;
7000Sstevel@tonic-gate 		case DATA_TYPE_INT16:
7010Sstevel@tonic-gate 			ret = nvlist_add_int16(payload, name,
7020Sstevel@tonic-gate 			    va_arg(ap, int));
7030Sstevel@tonic-gate 			break;
7040Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
7050Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7060Sstevel@tonic-gate 			ret = nvlist_add_int16_array(payload, name,
7070Sstevel@tonic-gate 			    va_arg(ap, int16_t *), nelem);
7080Sstevel@tonic-gate 			break;
7090Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
7100Sstevel@tonic-gate 			ret = nvlist_add_uint16(payload, name,
7110Sstevel@tonic-gate 			    va_arg(ap, uint_t));
7120Sstevel@tonic-gate 			break;
7130Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
7140Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7150Sstevel@tonic-gate 			ret = nvlist_add_uint16_array(payload, name,
7160Sstevel@tonic-gate 			    va_arg(ap, uint16_t *), nelem);
7170Sstevel@tonic-gate 			break;
7180Sstevel@tonic-gate 		case DATA_TYPE_INT32:
7190Sstevel@tonic-gate 			ret = nvlist_add_int32(payload, name,
7200Sstevel@tonic-gate 			    va_arg(ap, int32_t));
7210Sstevel@tonic-gate 			break;
7220Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
7230Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7240Sstevel@tonic-gate 			ret = nvlist_add_int32_array(payload, name,
7250Sstevel@tonic-gate 			    va_arg(ap, int32_t *), nelem);
7260Sstevel@tonic-gate 			break;
7270Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
7280Sstevel@tonic-gate 			ret = nvlist_add_uint32(payload, name,
7290Sstevel@tonic-gate 			    va_arg(ap, uint32_t));
7300Sstevel@tonic-gate 			break;
7310Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
7320Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7330Sstevel@tonic-gate 			ret = nvlist_add_uint32_array(payload, name,
7340Sstevel@tonic-gate 			    va_arg(ap, uint32_t *), nelem);
7350Sstevel@tonic-gate 			break;
7360Sstevel@tonic-gate 		case DATA_TYPE_INT64:
7370Sstevel@tonic-gate 			ret = nvlist_add_int64(payload, name,
7380Sstevel@tonic-gate 			    va_arg(ap, int64_t));
7390Sstevel@tonic-gate 			break;
7400Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
7410Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7420Sstevel@tonic-gate 			ret = nvlist_add_int64_array(payload, name,
7430Sstevel@tonic-gate 			    va_arg(ap, int64_t *), nelem);
7440Sstevel@tonic-gate 			break;
7450Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
7460Sstevel@tonic-gate 			ret = nvlist_add_uint64(payload, name,
7470Sstevel@tonic-gate 			    va_arg(ap, uint64_t));
7480Sstevel@tonic-gate 			break;
7490Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
7500Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7510Sstevel@tonic-gate 			ret = nvlist_add_uint64_array(payload, name,
7520Sstevel@tonic-gate 			    va_arg(ap, uint64_t *), nelem);
7530Sstevel@tonic-gate 			break;
7540Sstevel@tonic-gate 		case DATA_TYPE_STRING:
7550Sstevel@tonic-gate 			ret = nvlist_add_string(payload, name,
7560Sstevel@tonic-gate 			    va_arg(ap, char *));
7570Sstevel@tonic-gate 			break;
7580Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
7590Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7600Sstevel@tonic-gate 			ret = nvlist_add_string_array(payload, name,
7610Sstevel@tonic-gate 			    va_arg(ap, char **), nelem);
7620Sstevel@tonic-gate 			break;
7630Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
7640Sstevel@tonic-gate 			ret = nvlist_add_nvlist(payload, name,
7650Sstevel@tonic-gate 			    va_arg(ap, nvlist_t *));
7660Sstevel@tonic-gate 			break;
7670Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
7680Sstevel@tonic-gate 			nelem = va_arg(ap, int);
7690Sstevel@tonic-gate 			ret = nvlist_add_nvlist_array(payload, name,
7700Sstevel@tonic-gate 			    va_arg(ap, nvlist_t **), nelem);
7710Sstevel@tonic-gate 			break;
7720Sstevel@tonic-gate 		default:
7730Sstevel@tonic-gate 			ret = EINVAL;
7740Sstevel@tonic-gate 		}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 		name = va_arg(ap, char *);
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	return (ret);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate void
fm_payload_set(nvlist_t * payload,...)7820Sstevel@tonic-gate fm_payload_set(nvlist_t *payload, ...)
7830Sstevel@tonic-gate {
7840Sstevel@tonic-gate 	int ret;
7850Sstevel@tonic-gate 	const char *name;
7860Sstevel@tonic-gate 	va_list ap;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	va_start(ap, payload);
7890Sstevel@tonic-gate 	name = va_arg(ap, char *);
7900Sstevel@tonic-gate 	ret = i_fm_payload_set(payload, name, ap);
7910Sstevel@tonic-gate 	va_end(ap);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (ret)
7940Sstevel@tonic-gate 		atomic_add_64(
7950Sstevel@tonic-gate 		    &erpt_kstat_data.payload_set_failed.value.ui64, 1);
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate /*
7990Sstevel@tonic-gate  * Set-up and validate the members of an ereport event according to:
8000Sstevel@tonic-gate  *
8010Sstevel@tonic-gate  *	Member name		Type		Value
8020Sstevel@tonic-gate  *	====================================================
8030Sstevel@tonic-gate  *	class			string		ereport
8040Sstevel@tonic-gate  *	version			uint8_t		0
8050Sstevel@tonic-gate  *	ena			uint64_t	<ena>
8060Sstevel@tonic-gate  *	detector		nvlist_t	<detector>
8070Sstevel@tonic-gate  *	ereport-payload		nvlist_t	<var args>
8080Sstevel@tonic-gate  *
80912213SGavin.Maltby@Sun.COM  * We don't actually add a 'version' member to the payload.  Really,
81012213SGavin.Maltby@Sun.COM  * the version quoted to us by our caller is that of the category 1
81112213SGavin.Maltby@Sun.COM  * "ereport" event class (and we require FM_EREPORT_VERS0) but
81212213SGavin.Maltby@Sun.COM  * the payload version of the actual leaf class event under construction
81312213SGavin.Maltby@Sun.COM  * may be something else.  Callers should supply a version in the varargs,
81412213SGavin.Maltby@Sun.COM  * or (better) we could take two version arguments - one for the
81512213SGavin.Maltby@Sun.COM  * ereport category 1 classification (expect FM_EREPORT_VERS0) and one
81612213SGavin.Maltby@Sun.COM  * for the leaf class.
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate void
fm_ereport_set(nvlist_t * ereport,int version,const char * erpt_class,uint64_t ena,const nvlist_t * detector,...)8190Sstevel@tonic-gate fm_ereport_set(nvlist_t *ereport, int version, const char *erpt_class,
8200Sstevel@tonic-gate     uint64_t ena, const nvlist_t *detector, ...)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	char ereport_class[FM_MAX_CLASS];
8230Sstevel@tonic-gate 	const char *name;
8240Sstevel@tonic-gate 	va_list ap;
8250Sstevel@tonic-gate 	int ret;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (version != FM_EREPORT_VERS0) {
8280Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8290Sstevel@tonic-gate 		return;
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	(void) snprintf(ereport_class, FM_MAX_CLASS, "%s.%s",
8330Sstevel@tonic-gate 	    FM_EREPORT_CLASS, erpt_class);
8340Sstevel@tonic-gate 	if (nvlist_add_string(ereport, FM_CLASS, ereport_class) != 0) {
8350Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8360Sstevel@tonic-gate 		return;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	if (nvlist_add_uint64(ereport, FM_EREPORT_ENA, ena)) {
8400Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if (nvlist_add_nvlist(ereport, FM_EREPORT_DETECTOR,
8440Sstevel@tonic-gate 	    (nvlist_t *)detector) != 0) {
8450Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	va_start(ap, detector);
8490Sstevel@tonic-gate 	name = va_arg(ap, const char *);
8500Sstevel@tonic-gate 	ret = i_fm_payload_set(ereport, name, ap);
8510Sstevel@tonic-gate 	va_end(ap);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (ret)
8540Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8571414Scindi /*
8581414Scindi  * Set-up and validate the members of an hc fmri according to;
8591414Scindi  *
8601414Scindi  *	Member name		Type		Value
8611414Scindi  *	===================================================
8621414Scindi  *	version			uint8_t		0
8631414Scindi  *	auth			nvlist_t	<auth>
8641414Scindi  *	hc-name			string		<name>
8651414Scindi  *	hc-id			string		<id>
8661414Scindi  *
8671414Scindi  * Note that auth and hc-id are optional members.
8681414Scindi  */
8690Sstevel@tonic-gate 
8701414Scindi #define	HC_MAXPAIRS	20
8711414Scindi #define	HC_MAXNAMELEN	50
8720Sstevel@tonic-gate 
8731414Scindi static int
fm_fmri_hc_set_common(nvlist_t * fmri,int version,const nvlist_t * auth)8741414Scindi fm_fmri_hc_set_common(nvlist_t *fmri, int version, const nvlist_t *auth)
8751414Scindi {
8761414Scindi 	if (version != FM_HC_SCHEME_VERSION) {
8771414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
8781414Scindi 		return (0);
8791414Scindi 	}
8801414Scindi 
8811414Scindi 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0 ||
8821414Scindi 	    nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC) != 0) {
8831414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
8841414Scindi 		return (0);
8851414Scindi 	}
8861414Scindi 
8871414Scindi 	if (auth != NULL && nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
8881414Scindi 	    (nvlist_t *)auth) != 0) {
8891414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
8901414Scindi 		return (0);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 
8931414Scindi 	return (1);
8941414Scindi }
8950Sstevel@tonic-gate 
8961414Scindi void
fm_fmri_hc_set(nvlist_t * fmri,int version,const nvlist_t * auth,nvlist_t * snvl,int npairs,...)8971414Scindi fm_fmri_hc_set(nvlist_t *fmri, int version, const nvlist_t *auth,
8981414Scindi     nvlist_t *snvl, int npairs, ...)
8991414Scindi {
9001414Scindi 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
9011414Scindi 	nvlist_t *pairs[HC_MAXPAIRS];
9021414Scindi 	va_list ap;
9031414Scindi 	int i;
9041414Scindi 
9051414Scindi 	if (!fm_fmri_hc_set_common(fmri, version, auth))
9061414Scindi 		return;
9071414Scindi 
9081414Scindi 	npairs = MIN(npairs, HC_MAXPAIRS);
9091414Scindi 
9101414Scindi 	va_start(ap, npairs);
9111414Scindi 	for (i = 0; i < npairs; i++) {
9121414Scindi 		const char *name = va_arg(ap, const char *);
9131414Scindi 		uint32_t id = va_arg(ap, uint32_t);
9141414Scindi 		char idstr[11];
9150Sstevel@tonic-gate 
9161414Scindi 		(void) snprintf(idstr, sizeof (idstr), "%u", id);
9171414Scindi 
9181414Scindi 		pairs[i] = fm_nvlist_create(nva);
9191414Scindi 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
9201414Scindi 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
9211414Scindi 			atomic_add_64(
9221414Scindi 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9231414Scindi 		}
9240Sstevel@tonic-gate 	}
9251414Scindi 	va_end(ap);
9260Sstevel@tonic-gate 
9271414Scindi 	if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs, npairs) != 0)
9281414Scindi 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9291414Scindi 
9301414Scindi 	for (i = 0; i < npairs; i++)
9311414Scindi 		fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
9321414Scindi 
9331414Scindi 	if (snvl != NULL) {
9341414Scindi 		if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
9351414Scindi 			atomic_add_64(
9361414Scindi 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9371414Scindi 		}
9381414Scindi 	}
9390Sstevel@tonic-gate }
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /*
9420Sstevel@tonic-gate  * Set-up and validate the members of an dev fmri according to:
9430Sstevel@tonic-gate  *
9440Sstevel@tonic-gate  *	Member name		Type		Value
9450Sstevel@tonic-gate  *	====================================================
9460Sstevel@tonic-gate  *	version			uint8_t		0
9470Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
9480Sstevel@tonic-gate  *	devpath			string		<devpath>
94912213SGavin.Maltby@Sun.COM  *	[devid]			string		<devid>
95012213SGavin.Maltby@Sun.COM  *	[target-port-l0id]	string		<target-port-lun0-id>
9510Sstevel@tonic-gate  *
9520Sstevel@tonic-gate  * Note that auth and devid are optional members.
9530Sstevel@tonic-gate  */
9540Sstevel@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)9550Sstevel@tonic-gate fm_fmri_dev_set(nvlist_t *fmri_dev, int version, const nvlist_t *auth,
95612213SGavin.Maltby@Sun.COM     const char *devpath, const char *devid, const char *tpl0)
9570Sstevel@tonic-gate {
95812213SGavin.Maltby@Sun.COM 	int err = 0;
95912213SGavin.Maltby@Sun.COM 
9600Sstevel@tonic-gate 	if (version != DEV_SCHEME_VERSION0) {
9610Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
9620Sstevel@tonic-gate 		return;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
96512213SGavin.Maltby@Sun.COM 	err |= nvlist_add_uint8(fmri_dev, FM_VERSION, version);
96612213SGavin.Maltby@Sun.COM 	err |= nvlist_add_string(fmri_dev, FM_FMRI_SCHEME, FM_FMRI_SCHEME_DEV);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	if (auth != NULL) {
96912213SGavin.Maltby@Sun.COM 		err |= nvlist_add_nvlist(fmri_dev, FM_FMRI_AUTHORITY,
97012213SGavin.Maltby@Sun.COM 		    (nvlist_t *)auth);
9710Sstevel@tonic-gate 	}
9720Sstevel@tonic-gate 
97312213SGavin.Maltby@Sun.COM 	err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_PATH, devpath);
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	if (devid != NULL)
97612213SGavin.Maltby@Sun.COM 		err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_ID, devid);
97712213SGavin.Maltby@Sun.COM 
97812213SGavin.Maltby@Sun.COM 	if (tpl0 != NULL)
97912213SGavin.Maltby@Sun.COM 		err |= nvlist_add_string(fmri_dev, FM_FMRI_DEV_TGTPTLUN0, tpl0);
98012213SGavin.Maltby@Sun.COM 
98112213SGavin.Maltby@Sun.COM 	if (err)
98212213SGavin.Maltby@Sun.COM 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
98312213SGavin.Maltby@Sun.COM 
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate /*
9870Sstevel@tonic-gate  * Set-up and validate the members of an cpu fmri according to:
9880Sstevel@tonic-gate  *
9890Sstevel@tonic-gate  *	Member name		Type		Value
9900Sstevel@tonic-gate  *	====================================================
9910Sstevel@tonic-gate  *	version			uint8_t		0
9920Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
9930Sstevel@tonic-gate  *	cpuid			uint32_t	<cpu_id>
9940Sstevel@tonic-gate  *	cpumask			uint8_t		<cpu_mask>
9950Sstevel@tonic-gate  *	serial			uint64_t	<serial_id>
9960Sstevel@tonic-gate  *
9971414Scindi  * Note that auth, cpumask, serial are optional members.
9980Sstevel@tonic-gate  *
9990Sstevel@tonic-gate  */
10000Sstevel@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)10010Sstevel@tonic-gate fm_fmri_cpu_set(nvlist_t *fmri_cpu, int version, const nvlist_t *auth,
10021414Scindi     uint32_t cpu_id, uint8_t *cpu_maskp, const char *serial_idp)
10030Sstevel@tonic-gate {
10041414Scindi 	uint64_t *failedp = &erpt_kstat_data.fmri_set_failed.value.ui64;
10051414Scindi 
10061414Scindi 	if (version < CPU_SCHEME_VERSION1) {
10071414Scindi 		atomic_add_64(failedp, 1);
10080Sstevel@tonic-gate 		return;
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_cpu, FM_VERSION, version) != 0) {
10121414Scindi 		atomic_add_64(failedp, 1);
10130Sstevel@tonic-gate 		return;
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (nvlist_add_string(fmri_cpu, FM_FMRI_SCHEME,
10170Sstevel@tonic-gate 	    FM_FMRI_SCHEME_CPU) != 0) {
10181414Scindi 		atomic_add_64(failedp, 1);
10190Sstevel@tonic-gate 		return;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 
10221414Scindi 	if (auth != NULL && nvlist_add_nvlist(fmri_cpu, FM_FMRI_AUTHORITY,
10231414Scindi 	    (nvlist_t *)auth) != 0)
10241414Scindi 		atomic_add_64(failedp, 1);
10251414Scindi 
10261414Scindi 	if (nvlist_add_uint32(fmri_cpu, FM_FMRI_CPU_ID, cpu_id) != 0)
10271414Scindi 		atomic_add_64(failedp, 1);
10280Sstevel@tonic-gate 
10291414Scindi 	if (cpu_maskp != NULL && nvlist_add_uint8(fmri_cpu, FM_FMRI_CPU_MASK,
10301414Scindi 	    *cpu_maskp) != 0)
10311414Scindi 		atomic_add_64(failedp, 1);
10320Sstevel@tonic-gate 
10331414Scindi 	if (serial_idp == NULL || nvlist_add_string(fmri_cpu,
10341414Scindi 	    FM_FMRI_CPU_SERIAL_ID, (char *)serial_idp) != 0)
10351414Scindi 			atomic_add_64(failedp, 1);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate  * Set-up and validate the members of a mem according to:
10400Sstevel@tonic-gate  *
10410Sstevel@tonic-gate  *	Member name		Type		Value
10420Sstevel@tonic-gate  *	====================================================
10430Sstevel@tonic-gate  *	version			uint8_t		0
10440Sstevel@tonic-gate  *	auth			nvlist_t	<auth>		[optional]
10450Sstevel@tonic-gate  *	unum			string		<unum>
10461186Sayznaga  *	serial			string		<serial>	[optional*]
10471186Sayznaga  *	offset			uint64_t	<offset>	[optional]
10480Sstevel@tonic-gate  *
10491186Sayznaga  *	* serial is required if offset is present
10500Sstevel@tonic-gate  */
10510Sstevel@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)10520Sstevel@tonic-gate fm_fmri_mem_set(nvlist_t *fmri, int version, const nvlist_t *auth,
10531186Sayznaga     const char *unum, const char *serial, uint64_t offset)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate 	if (version != MEM_SCHEME_VERSION0) {
10560Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10570Sstevel@tonic-gate 		return;
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10601186Sayznaga 	if (!serial && (offset != (uint64_t)-1)) {
10611186Sayznaga 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10621186Sayznaga 		return;
10631186Sayznaga 	}
10641186Sayznaga 
10650Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
10660Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10670Sstevel@tonic-gate 		return;
10680Sstevel@tonic-gate 	}
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MEM) != 0) {
10710Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10720Sstevel@tonic-gate 		return;
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if (auth != NULL) {
10760Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
10770Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
10780Sstevel@tonic-gate 			atomic_add_64(
10790Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10800Sstevel@tonic-gate 		}
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_MEM_UNUM, unum) != 0) {
10840Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	if (serial != NULL) {
10880Sstevel@tonic-gate 		if (nvlist_add_string_array(fmri, FM_FMRI_MEM_SERIAL_ID,
10890Sstevel@tonic-gate 		    (char **)&serial, 1) != 0) {
10900Sstevel@tonic-gate 			atomic_add_64(
10910Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
10920Sstevel@tonic-gate 		}
10931186Sayznaga 		if (offset != (uint64_t)-1) {
10941186Sayznaga 			if (nvlist_add_uint64(fmri, FM_FMRI_MEM_OFFSET,
10951186Sayznaga 			    offset) != 0) {
10961186Sayznaga 				atomic_add_64(&erpt_kstat_data.
10971186Sayznaga 				    fmri_set_failed.value.ui64, 1);
10981186Sayznaga 			}
10991186Sayznaga 		}
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11031544Seschrock void
fm_fmri_zfs_set(nvlist_t * fmri,int version,uint64_t pool_guid,uint64_t vdev_guid)11041544Seschrock fm_fmri_zfs_set(nvlist_t *fmri, int version, uint64_t pool_guid,
11051544Seschrock     uint64_t vdev_guid)
11061544Seschrock {
11071544Seschrock 	if (version != ZFS_SCHEME_VERSION0) {
11081544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11091544Seschrock 		return;
11101544Seschrock 	}
11111544Seschrock 
11121544Seschrock 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
11131544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11141544Seschrock 		return;
11151544Seschrock 	}
11161544Seschrock 
11171544Seschrock 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS) != 0) {
11181544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11191544Seschrock 		return;
11201544Seschrock 	}
11211544Seschrock 
11221544Seschrock 	if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_POOL, pool_guid) != 0) {
11231544Seschrock 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11241544Seschrock 	}
11251544Seschrock 
11261544Seschrock 	if (vdev_guid != 0) {
11271544Seschrock 		if (nvlist_add_uint64(fmri, FM_FMRI_ZFS_VDEV, vdev_guid) != 0) {
11281544Seschrock 			atomic_add_64(
11291544Seschrock 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
11301544Seschrock 		}
11311544Seschrock 	}
11321544Seschrock }
11331544Seschrock 
11340Sstevel@tonic-gate uint64_t
fm_ena_increment(uint64_t ena)11350Sstevel@tonic-gate fm_ena_increment(uint64_t ena)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	uint64_t new_ena;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
11400Sstevel@tonic-gate 	case FM_ENA_FMT1:
11410Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT1_GEN_SHFT);
11420Sstevel@tonic-gate 		break;
11430Sstevel@tonic-gate 	case FM_ENA_FMT2:
11440Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT2_GEN_SHFT);
11450Sstevel@tonic-gate 		break;
11460Sstevel@tonic-gate 	default:
11470Sstevel@tonic-gate 		new_ena = 0;
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	return (new_ena);
11510Sstevel@tonic-gate }
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate uint64_t
fm_ena_generate_cpu(uint64_t timestamp,processorid_t cpuid,uchar_t format)11540Sstevel@tonic-gate fm_ena_generate_cpu(uint64_t timestamp, processorid_t cpuid, uchar_t format)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	uint64_t ena = 0;
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	switch (format) {
11590Sstevel@tonic-gate 	case FM_ENA_FMT1:
11600Sstevel@tonic-gate 		if (timestamp) {
11610Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11620Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
11630Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
11640Sstevel@tonic-gate 			    ((timestamp << ENA_FMT1_TIME_SHFT) &
11650Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
11660Sstevel@tonic-gate 		} else {
11670Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11680Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
11690Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
11700Sstevel@tonic-gate 			    ((gethrtime_waitfree() << ENA_FMT1_TIME_SHFT) &
11710Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
11720Sstevel@tonic-gate 		}
11730Sstevel@tonic-gate 		break;
11740Sstevel@tonic-gate 	case FM_ENA_FMT2:
11750Sstevel@tonic-gate 		ena = (uint64_t)((format & ENA_FORMAT_MASK) |
11760Sstevel@tonic-gate 		    ((timestamp << ENA_FMT2_TIME_SHFT) & ENA_FMT2_TIME_MASK));
11770Sstevel@tonic-gate 		break;
11780Sstevel@tonic-gate 	default:
11790Sstevel@tonic-gate 		break;
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	return (ena);
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate uint64_t
fm_ena_generate(uint64_t timestamp,uchar_t format)11860Sstevel@tonic-gate fm_ena_generate(uint64_t timestamp, uchar_t format)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	return (fm_ena_generate_cpu(timestamp, CPU->cpu_id, format));
11890Sstevel@tonic-gate }
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate uint64_t
fm_ena_generation_get(uint64_t ena)11920Sstevel@tonic-gate fm_ena_generation_get(uint64_t ena)
11930Sstevel@tonic-gate {
11940Sstevel@tonic-gate 	uint64_t gen;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
11970Sstevel@tonic-gate 	case FM_ENA_FMT1:
11980Sstevel@tonic-gate 		gen = (ena & ENA_FMT1_GEN_MASK) >> ENA_FMT1_GEN_SHFT;
11990Sstevel@tonic-gate 		break;
12000Sstevel@tonic-gate 	case FM_ENA_FMT2:
12010Sstevel@tonic-gate 		gen = (ena & ENA_FMT2_GEN_MASK) >> ENA_FMT2_GEN_SHFT;
12020Sstevel@tonic-gate 		break;
12030Sstevel@tonic-gate 	default:
12040Sstevel@tonic-gate 		gen = 0;
12050Sstevel@tonic-gate 		break;
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	return (gen);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate uchar_t
fm_ena_format_get(uint64_t ena)12120Sstevel@tonic-gate fm_ena_format_get(uint64_t ena)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	return (ENA_FORMAT(ena));
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate uint64_t
fm_ena_id_get(uint64_t ena)12190Sstevel@tonic-gate fm_ena_id_get(uint64_t ena)
12200Sstevel@tonic-gate {
12210Sstevel@tonic-gate 	uint64_t id;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
12240Sstevel@tonic-gate 	case FM_ENA_FMT1:
12250Sstevel@tonic-gate 		id = (ena & ENA_FMT1_ID_MASK) >> ENA_FMT1_ID_SHFT;
12260Sstevel@tonic-gate 		break;
12270Sstevel@tonic-gate 	case FM_ENA_FMT2:
12280Sstevel@tonic-gate 		id = (ena & ENA_FMT2_ID_MASK) >> ENA_FMT2_ID_SHFT;
12290Sstevel@tonic-gate 		break;
12300Sstevel@tonic-gate 	default:
12310Sstevel@tonic-gate 		id = 0;
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	return (id);
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate uint64_t
fm_ena_time_get(uint64_t ena)12380Sstevel@tonic-gate fm_ena_time_get(uint64_t ena)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	uint64_t time;
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
12430Sstevel@tonic-gate 	case FM_ENA_FMT1:
12440Sstevel@tonic-gate 		time = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
12450Sstevel@tonic-gate 		break;
12460Sstevel@tonic-gate 	case FM_ENA_FMT2:
12470Sstevel@tonic-gate 		time = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
12480Sstevel@tonic-gate 		break;
12490Sstevel@tonic-gate 	default:
12500Sstevel@tonic-gate 		time = 0;
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	return (time);
12540Sstevel@tonic-gate }
12551414Scindi 
12561414Scindi /*
12571414Scindi  * Convert a getpcstack() trace to symbolic name+offset, and add the resulting
12581414Scindi  * string array to a Fault Management ereport as FM_EREPORT_PAYLOAD_NAME_STACK.
12591414Scindi  */
12601414Scindi void
fm_payload_stack_add(nvlist_t * payload,const pc_t * stack,int depth)12611414Scindi fm_payload_stack_add(nvlist_t *payload, const pc_t *stack, int depth)
12621414Scindi {
12631414Scindi 	int i;
12641414Scindi 	char *sym;
12651414Scindi 	ulong_t off;
12661414Scindi 	char *stkpp[FM_STK_DEPTH];
12671414Scindi 	char buf[FM_STK_DEPTH * FM_SYM_SZ];
12681414Scindi 	char *stkp = buf;
12691414Scindi 
12701414Scindi 	for (i = 0; i < depth && i != FM_STK_DEPTH; i++, stkp += FM_SYM_SZ) {
12711414Scindi 		if ((sym = kobj_getsymname(stack[i], &off)) != NULL)
12721414Scindi 			(void) snprintf(stkp, FM_SYM_SZ, "%s+%lx", sym, off);
12731414Scindi 		else
12741414Scindi 			(void) snprintf(stkp, FM_SYM_SZ, "%lx", (long)stack[i]);
12751414Scindi 		stkpp[i] = stkp;
12761414Scindi 	}
12771414Scindi 
12781414Scindi 	fm_payload_set(payload, FM_EREPORT_PAYLOAD_NAME_STACK,
12791437Sdilpreet 	    DATA_TYPE_STRING_ARRAY, depth, stkpp, NULL);
12801414Scindi }
12819613SAbhinandan.Ekande@Sun.COM 
12829613SAbhinandan.Ekande@Sun.COM void
print_msg_hwerr(ctid_t ct_id,proc_t * p)12839613SAbhinandan.Ekande@Sun.COM print_msg_hwerr(ctid_t ct_id, proc_t *p)
12849613SAbhinandan.Ekande@Sun.COM {
12859613SAbhinandan.Ekande@Sun.COM 	uprintf("Killed process %d (%s) in contract id %d "
12869613SAbhinandan.Ekande@Sun.COM 	    "due to hardware error\n", p->p_pid, p->p_user.u_comm, ct_id);
12879613SAbhinandan.Ekande@Sun.COM }
128810942STom.Pothier@Sun.COM 
128910942STom.Pothier@Sun.COM void
fm_fmri_hc_create(nvlist_t * fmri,int version,const nvlist_t * auth,nvlist_t * snvl,nvlist_t * bboard,int npairs,...)129010942STom.Pothier@Sun.COM fm_fmri_hc_create(nvlist_t *fmri, int version, const nvlist_t *auth,
129110942STom.Pothier@Sun.COM     nvlist_t *snvl, nvlist_t *bboard, int npairs, ...)
129210942STom.Pothier@Sun.COM {
129310942STom.Pothier@Sun.COM 	nv_alloc_t *nva = nvlist_lookup_nv_alloc(fmri);
129410942STom.Pothier@Sun.COM 	nvlist_t *pairs[HC_MAXPAIRS];
129510942STom.Pothier@Sun.COM 	nvlist_t **hcl;
129610942STom.Pothier@Sun.COM 	uint_t n;
129710942STom.Pothier@Sun.COM 	int i, j;
129810942STom.Pothier@Sun.COM 	va_list ap;
129910942STom.Pothier@Sun.COM 	char *hcname, *hcid;
130010942STom.Pothier@Sun.COM 
130110942STom.Pothier@Sun.COM 	if (!fm_fmri_hc_set_common(fmri, version, auth))
130210942STom.Pothier@Sun.COM 		return;
130310942STom.Pothier@Sun.COM 
130410942STom.Pothier@Sun.COM 	/*
130510942STom.Pothier@Sun.COM 	 * copy the bboard nvpairs to the pairs array
130610942STom.Pothier@Sun.COM 	 */
130710942STom.Pothier@Sun.COM 	if (nvlist_lookup_nvlist_array(bboard, FM_FMRI_HC_LIST, &hcl, &n)
130810942STom.Pothier@Sun.COM 	    != 0) {
130910942STom.Pothier@Sun.COM 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
131010942STom.Pothier@Sun.COM 		return;
131110942STom.Pothier@Sun.COM 	}
131210942STom.Pothier@Sun.COM 
131310942STom.Pothier@Sun.COM 	for (i = 0; i < n; i++) {
131410942STom.Pothier@Sun.COM 		if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_NAME,
131510942STom.Pothier@Sun.COM 		    &hcname) != 0) {
131610942STom.Pothier@Sun.COM 			atomic_add_64(
131710942STom.Pothier@Sun.COM 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
131810942STom.Pothier@Sun.COM 			return;
131910942STom.Pothier@Sun.COM 		}
132010942STom.Pothier@Sun.COM 		if (nvlist_lookup_string(hcl[i], FM_FMRI_HC_ID, &hcid) != 0) {
132110942STom.Pothier@Sun.COM 			atomic_add_64(
132210942STom.Pothier@Sun.COM 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
132310942STom.Pothier@Sun.COM 			return;
132410942STom.Pothier@Sun.COM 		}
132510942STom.Pothier@Sun.COM 
132610942STom.Pothier@Sun.COM 		pairs[i] = fm_nvlist_create(nva);
132710942STom.Pothier@Sun.COM 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, hcname) != 0 ||
132810942STom.Pothier@Sun.COM 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, hcid) != 0) {
132910942STom.Pothier@Sun.COM 			for (j = 0; j <= i; j++) {
133010942STom.Pothier@Sun.COM 				if (pairs[j] != NULL)
133110942STom.Pothier@Sun.COM 					fm_nvlist_destroy(pairs[j],
133210942STom.Pothier@Sun.COM 					    FM_NVA_RETAIN);
133310942STom.Pothier@Sun.COM 			}
133410942STom.Pothier@Sun.COM 			atomic_add_64(
133510942STom.Pothier@Sun.COM 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
133610942STom.Pothier@Sun.COM 			return;
133710942STom.Pothier@Sun.COM 		}
133810942STom.Pothier@Sun.COM 	}
133910942STom.Pothier@Sun.COM 
134010942STom.Pothier@Sun.COM 	/*
134110942STom.Pothier@Sun.COM 	 * create the pairs from passed in pairs
134210942STom.Pothier@Sun.COM 	 */
134310942STom.Pothier@Sun.COM 	npairs = MIN(npairs, HC_MAXPAIRS);
134410942STom.Pothier@Sun.COM 
134510942STom.Pothier@Sun.COM 	va_start(ap, npairs);
134610942STom.Pothier@Sun.COM 	for (i = n; i < npairs + n; i++) {
134710942STom.Pothier@Sun.COM 		const char *name = va_arg(ap, const char *);
134810942STom.Pothier@Sun.COM 		uint32_t id = va_arg(ap, uint32_t);
134910942STom.Pothier@Sun.COM 		char idstr[11];
135010942STom.Pothier@Sun.COM 		(void) snprintf(idstr, sizeof (idstr), "%u", id);
135110942STom.Pothier@Sun.COM 		pairs[i] = fm_nvlist_create(nva);
135210942STom.Pothier@Sun.COM 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
135310942STom.Pothier@Sun.COM 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0) {
135410942STom.Pothier@Sun.COM 			for (j = 0; j <= i; j++) {
135510942STom.Pothier@Sun.COM 				if (pairs[j] != NULL)
135610942STom.Pothier@Sun.COM 					fm_nvlist_destroy(pairs[j],
135710942STom.Pothier@Sun.COM 					    FM_NVA_RETAIN);
135810942STom.Pothier@Sun.COM 			}
135910942STom.Pothier@Sun.COM 			atomic_add_64(
136010942STom.Pothier@Sun.COM 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
136110942STom.Pothier@Sun.COM 			return;
136210942STom.Pothier@Sun.COM 		}
136310942STom.Pothier@Sun.COM 	}
136410942STom.Pothier@Sun.COM 	va_end(ap);
136510942STom.Pothier@Sun.COM 
136610942STom.Pothier@Sun.COM 	/*
136710942STom.Pothier@Sun.COM 	 * Create the fmri hc list
136810942STom.Pothier@Sun.COM 	 */
136910942STom.Pothier@Sun.COM 	if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST, pairs,
137010942STom.Pothier@Sun.COM 	    npairs + n) != 0) {
137110942STom.Pothier@Sun.COM 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
137210942STom.Pothier@Sun.COM 		return;
137310942STom.Pothier@Sun.COM 	}
137410942STom.Pothier@Sun.COM 
137510942STom.Pothier@Sun.COM 	for (i = 0; i < npairs + n; i++) {
137610942STom.Pothier@Sun.COM 			fm_nvlist_destroy(pairs[i], FM_NVA_RETAIN);
137710942STom.Pothier@Sun.COM 	}
137810942STom.Pothier@Sun.COM 
137910942STom.Pothier@Sun.COM 	if (snvl != NULL) {
138010942STom.Pothier@Sun.COM 		if (nvlist_add_nvlist(fmri, FM_FMRI_HC_SPECIFIC, snvl) != 0) {
138110942STom.Pothier@Sun.COM 			atomic_add_64(
138210942STom.Pothier@Sun.COM 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
138310942STom.Pothier@Sun.COM 			return;
138410942STom.Pothier@Sun.COM 		}
138510942STom.Pothier@Sun.COM 	}
138610942STom.Pothier@Sun.COM }
1387