xref: /onnv-gate/usr/src/uts/common/os/fm.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Fault Management Architecture (FMA) Resource and Protocol Support
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * The routines contained herein provide services to support kernel subsystems
33*0Sstevel@tonic-gate  * in publishing fault management telemetry (see PSARC 2002/412 and 2003/089).
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * Name-Value Pair Lists
36*0Sstevel@tonic-gate  *
37*0Sstevel@tonic-gate  * The embodiment of an FMA protocol element (event, fmri or authority) is a
38*0Sstevel@tonic-gate  * name-value pair list (nvlist_t).  FMA-specific nvlist construtor and
39*0Sstevel@tonic-gate  * destructor functions, fm_nvlist_create() and fm_nvlist_destroy(), are used
40*0Sstevel@tonic-gate  * to create an nvpair list using custom allocators.  Callers may choose to
41*0Sstevel@tonic-gate  * allocate either from the kernel memory allocator, or from a preallocated
42*0Sstevel@tonic-gate  * buffer, useful in constrained contexts like high-level interrupt routines.
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * Protocol Event and FMRI Construction
45*0Sstevel@tonic-gate  *
46*0Sstevel@tonic-gate  * Convenience routines are provided to construct nvlist events according to
47*0Sstevel@tonic-gate  * the FMA Event Protocol and Naming Schema specification for ereports and
48*0Sstevel@tonic-gate  * FMRIs for the dev, cpu, hc, mem, legacy hc and de schemes.
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * ENA Manipulation
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  * Routines to generate ENA formats 0, 1 and 2 are available as well as
53*0Sstevel@tonic-gate  * routines to increment formats 1 and 2.  Individual fields within the
54*0Sstevel@tonic-gate  * ENA are extractable via fm_ena_time_get(), fm_ena_id_get(),
55*0Sstevel@tonic-gate  * fm_ena_format_get() and fm_ena_gen_get().
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #include <sys/types.h>
59*0Sstevel@tonic-gate #include <sys/time.h>
60*0Sstevel@tonic-gate #include <sys/sysevent.h>
61*0Sstevel@tonic-gate #include <sys/sysevent_impl.h>
62*0Sstevel@tonic-gate #include <sys/nvpair.h>
63*0Sstevel@tonic-gate #include <sys/nvpair_impl.h>
64*0Sstevel@tonic-gate #include <sys/cmn_err.h>
65*0Sstevel@tonic-gate #include <sys/cpuvar.h>
66*0Sstevel@tonic-gate #include <sys/sysmacros.h>
67*0Sstevel@tonic-gate #include <sys/systm.h>
68*0Sstevel@tonic-gate #include <sys/ddifm.h>
69*0Sstevel@tonic-gate #include <sys/ddifm_impl.h>
70*0Sstevel@tonic-gate #include <sys/spl.h>
71*0Sstevel@tonic-gate #include <sys/dumphdr.h>
72*0Sstevel@tonic-gate #include <sys/compress.h>
73*0Sstevel@tonic-gate #include <sys/cpuvar.h>
74*0Sstevel@tonic-gate #include <sys/console.h>
75*0Sstevel@tonic-gate #include <sys/panic.h>
76*0Sstevel@tonic-gate #include <sys/systeminfo.h>
77*0Sstevel@tonic-gate #include <sys/sysevent/eventdefs.h>
78*0Sstevel@tonic-gate #include <sys/fm/util.h>
79*0Sstevel@tonic-gate #include <sys/fm/protocol.h>
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * URL and SUNW-MSG-ID value to display for fm_panic(), defined below.  These
83*0Sstevel@tonic-gate  * values must be kept in sync with the FMA source code in usr/src/cmd/fm.
84*0Sstevel@tonic-gate  */
85*0Sstevel@tonic-gate static const char *fm_url = "http://www.sun.com/msg";
86*0Sstevel@tonic-gate static const char *fm_msgid = "SUNOS-8000-0G";
87*0Sstevel@tonic-gate static char *volatile fm_panicstr = NULL;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate errorq_t *ereport_errorq;
90*0Sstevel@tonic-gate void *ereport_dumpbuf;
91*0Sstevel@tonic-gate size_t ereport_dumplen;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate static uint_t ereport_chanlen = ERPT_EVCH_MAX;
94*0Sstevel@tonic-gate static evchan_t *ereport_chan = NULL;
95*0Sstevel@tonic-gate static ulong_t ereport_qlen = 0;
96*0Sstevel@tonic-gate static size_t ereport_size = 0;
97*0Sstevel@tonic-gate static int ereport_cols = 80;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate  * Common fault management kstats to record ereport generation
101*0Sstevel@tonic-gate  * failures
102*0Sstevel@tonic-gate  */
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate struct erpt_kstat {
105*0Sstevel@tonic-gate 	kstat_named_t	erpt_dropped;		/* num erpts dropped on post */
106*0Sstevel@tonic-gate 	kstat_named_t	erpt_set_failed;	/* num erpt set failures */
107*0Sstevel@tonic-gate 	kstat_named_t	fmri_set_failed;	/* num fmri set failures */
108*0Sstevel@tonic-gate 	kstat_named_t	payload_set_failed;	/* num payload set failures */
109*0Sstevel@tonic-gate };
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static struct erpt_kstat erpt_kstat_data = {
112*0Sstevel@tonic-gate 	{ "erpt-dropped", KSTAT_DATA_UINT64 },
113*0Sstevel@tonic-gate 	{ "erpt-set-failed", KSTAT_DATA_UINT64 },
114*0Sstevel@tonic-gate 	{ "fmri-set-failed", KSTAT_DATA_UINT64 },
115*0Sstevel@tonic-gate 	{ "payload-set-failed", KSTAT_DATA_UINT64 }
116*0Sstevel@tonic-gate };
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*ARGSUSED*/
119*0Sstevel@tonic-gate static void
120*0Sstevel@tonic-gate fm_drain(void *private, void *data, errorq_elem_t *eep)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	nvlist_t *nvl = errorq_elem_nvl(ereport_errorq, eep);
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if (!panicstr)
125*0Sstevel@tonic-gate 		(void) fm_ereport_post(nvl, EVCH_TRYHARD);
126*0Sstevel@tonic-gate 	else
127*0Sstevel@tonic-gate 		fm_nvprint(nvl);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate void
131*0Sstevel@tonic-gate fm_init(void)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	kstat_t *ksp;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	(void) sysevent_evc_bind(FM_ERROR_CHAN,
136*0Sstevel@tonic-gate 	    &ereport_chan, EVCH_CREAT | EVCH_HOLD_PEND);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	(void) sysevent_evc_control(ereport_chan,
139*0Sstevel@tonic-gate 	    EVCH_SET_CHAN_LEN, &ereport_chanlen);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (ereport_qlen == 0)
142*0Sstevel@tonic-gate 		ereport_qlen = ERPT_MAX_ERRS * MAX(max_ncpus, 4);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if (ereport_size == 0)
145*0Sstevel@tonic-gate 		ereport_size = ERPT_DATA_SZ;
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	ereport_errorq = errorq_nvcreate("fm_ereport_queue",
148*0Sstevel@tonic-gate 	    (errorq_func_t)fm_drain, NULL, ereport_qlen, ereport_size,
149*0Sstevel@tonic-gate 	    FM_ERR_PIL, ERRORQ_VITAL);
150*0Sstevel@tonic-gate 	if (ereport_errorq == NULL)
151*0Sstevel@tonic-gate 		panic("failed to create required ereport error queue");
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	ereport_dumpbuf = kmem_alloc(ereport_size, KM_SLEEP);
154*0Sstevel@tonic-gate 	ereport_dumplen = ereport_size;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	/* Initialize ereport allocation and generation kstats */
157*0Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "fm", "misc", KSTAT_TYPE_NAMED,
158*0Sstevel@tonic-gate 	    sizeof (struct erpt_kstat) / sizeof (kstat_named_t),
159*0Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	if (ksp != NULL) {
162*0Sstevel@tonic-gate 		ksp->ks_data = &erpt_kstat_data;
163*0Sstevel@tonic-gate 		kstat_install(ksp);
164*0Sstevel@tonic-gate 	} else {
165*0Sstevel@tonic-gate 		cmn_err(CE_NOTE, "failed to create fm/misc kstat\n");
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	}
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate  * Formatting utility function for fm_nvprintr.  We attempt to wrap chunks of
172*0Sstevel@tonic-gate  * output so they aren't split across console lines, and return the end column.
173*0Sstevel@tonic-gate  */
174*0Sstevel@tonic-gate /*PRINTFLIKE4*/
175*0Sstevel@tonic-gate static int
176*0Sstevel@tonic-gate fm_printf(int depth, int c, int cols, const char *format, ...)
177*0Sstevel@tonic-gate {
178*0Sstevel@tonic-gate 	va_list ap;
179*0Sstevel@tonic-gate 	int width;
180*0Sstevel@tonic-gate 	char c1;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 	va_start(ap, format);
183*0Sstevel@tonic-gate 	width = vsnprintf(&c1, sizeof (c1), format, ap);
184*0Sstevel@tonic-gate 	va_end(ap);
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (c + width >= cols) {
187*0Sstevel@tonic-gate 		console_printf("\n\r");
188*0Sstevel@tonic-gate 		c = 0;
189*0Sstevel@tonic-gate 		if (format[0] != ' ' && depth > 0) {
190*0Sstevel@tonic-gate 			console_printf(" ");
191*0Sstevel@tonic-gate 			c++;
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	va_start(ap, format);
196*0Sstevel@tonic-gate 	console_vprintf(format, ap);
197*0Sstevel@tonic-gate 	va_end(ap);
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	return ((c + width) % cols);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate /*
203*0Sstevel@tonic-gate  * Recursively print a nvlist in the specified column width and return the
204*0Sstevel@tonic-gate  * column we end up in.  This function is called recursively by fm_nvprint(),
205*0Sstevel@tonic-gate  * below.  We generically format the entire nvpair using hexadecimal
206*0Sstevel@tonic-gate  * integers and strings, and elide any integer arrays.  Arrays are basically
207*0Sstevel@tonic-gate  * used for cache dumps right now, so we suppress them so as not to overwhelm
208*0Sstevel@tonic-gate  * the amount of console output we produce at panic time.  This can be further
209*0Sstevel@tonic-gate  * enhanced as FMA technology grows based upon the needs of consumers.  All
210*0Sstevel@tonic-gate  * FMA telemetry is logged using the dump device transport, so the console
211*0Sstevel@tonic-gate  * output serves only as a fallback in case this procedure is unsuccessful.
212*0Sstevel@tonic-gate  */
213*0Sstevel@tonic-gate static int
214*0Sstevel@tonic-gate fm_nvprintr(nvlist_t *nvl, int d, int c, int cols)
215*0Sstevel@tonic-gate {
216*0Sstevel@tonic-gate 	nvpair_t *nvp;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	for (nvp = nvlist_next_nvpair(nvl, NULL);
219*0Sstevel@tonic-gate 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 		data_type_t type = nvpair_type(nvp);
222*0Sstevel@tonic-gate 		const char *name = nvpair_name(nvp);
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		boolean_t b;
225*0Sstevel@tonic-gate 		uint8_t i8;
226*0Sstevel@tonic-gate 		uint16_t i16;
227*0Sstevel@tonic-gate 		uint32_t i32;
228*0Sstevel@tonic-gate 		uint64_t i64;
229*0Sstevel@tonic-gate 		char *str;
230*0Sstevel@tonic-gate 		nvlist_t *cnv;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 		if (strcmp(name, FM_CLASS) == 0)
233*0Sstevel@tonic-gate 			continue; /* already printed by caller */
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		c = fm_printf(d, c, cols, " %s=", name);
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		switch (type) {
238*0Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN:
239*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " 1");
240*0Sstevel@tonic-gate 			break;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
243*0Sstevel@tonic-gate 			(void) nvpair_value_boolean_value(nvp, &b);
244*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, b ? "1" : "0");
245*0Sstevel@tonic-gate 			break;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
248*0Sstevel@tonic-gate 			(void) nvpair_value_byte(nvp, &i8);
249*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
250*0Sstevel@tonic-gate 			break;
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 		case DATA_TYPE_INT8:
253*0Sstevel@tonic-gate 			(void) nvpair_value_int8(nvp, (void *)&i8);
254*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
255*0Sstevel@tonic-gate 			break;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
258*0Sstevel@tonic-gate 			(void) nvpair_value_uint8(nvp, &i8);
259*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i8);
260*0Sstevel@tonic-gate 			break;
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 		case DATA_TYPE_INT16:
263*0Sstevel@tonic-gate 			(void) nvpair_value_int16(nvp, (void *)&i16);
264*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
265*0Sstevel@tonic-gate 			break;
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
268*0Sstevel@tonic-gate 			(void) nvpair_value_uint16(nvp, &i16);
269*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i16);
270*0Sstevel@tonic-gate 			break;
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 		case DATA_TYPE_INT32:
273*0Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (void *)&i32);
274*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
275*0Sstevel@tonic-gate 			break;
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
278*0Sstevel@tonic-gate 			(void) nvpair_value_uint32(nvp, &i32);
279*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%x", i32);
280*0Sstevel@tonic-gate 			break;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 		case DATA_TYPE_INT64:
283*0Sstevel@tonic-gate 			(void) nvpair_value_int64(nvp, (void *)&i64);
284*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
285*0Sstevel@tonic-gate 			    (u_longlong_t)i64);
286*0Sstevel@tonic-gate 			break;
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
289*0Sstevel@tonic-gate 			(void) nvpair_value_uint64(nvp, &i64);
290*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
291*0Sstevel@tonic-gate 			    (u_longlong_t)i64);
292*0Sstevel@tonic-gate 			break;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 		case DATA_TYPE_HRTIME:
295*0Sstevel@tonic-gate 			(void) nvpair_value_hrtime(nvp, (void *)&i64);
296*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "%llx",
297*0Sstevel@tonic-gate 			    (u_longlong_t)i64);
298*0Sstevel@tonic-gate 			break;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		case DATA_TYPE_STRING:
301*0Sstevel@tonic-gate 			(void) nvpair_value_string(nvp, &str);
302*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "\"%s\"",
303*0Sstevel@tonic-gate 			    str ? str : "<NULL>");
304*0Sstevel@tonic-gate 			break;
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
307*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[");
308*0Sstevel@tonic-gate 			(void) nvpair_value_nvlist(nvp, &cnv);
309*0Sstevel@tonic-gate 			c = fm_nvprintr(cnv, d + 1, c, cols);
310*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, " ]");
311*0Sstevel@tonic-gate 			break;
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
314*0Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
315*0Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
316*0Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
317*0Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
318*0Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
319*0Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
320*0Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
321*0Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
322*0Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
323*0Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
324*0Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
325*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "[...]");
326*0Sstevel@tonic-gate 			break;
327*0Sstevel@tonic-gate 		case DATA_TYPE_UNKNOWN:
328*0Sstevel@tonic-gate 			c = fm_printf(d + 1, c, cols, "<unknown>");
329*0Sstevel@tonic-gate 			break;
330*0Sstevel@tonic-gate 		}
331*0Sstevel@tonic-gate 	}
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate 	return (c);
334*0Sstevel@tonic-gate }
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate void
337*0Sstevel@tonic-gate fm_nvprint(nvlist_t *nvl)
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	char *class;
340*0Sstevel@tonic-gate 	int c = 0;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	console_printf("\r");
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvl, FM_CLASS, &class) == 0)
345*0Sstevel@tonic-gate 		c = fm_printf(0, c, ereport_cols, "%s", class);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	if (fm_nvprintr(nvl, 0, c, ereport_cols) != 0)
348*0Sstevel@tonic-gate 		console_printf("\n");
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	console_printf("\n");
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate /*
354*0Sstevel@tonic-gate  * Wrapper for panic() that first produces an FMA-style message for admins.
355*0Sstevel@tonic-gate  * Normally such messages are generated by fmd(1M)'s syslog-msgs agent: this
356*0Sstevel@tonic-gate  * is the one exception to that rule and the only error that gets messaged.
357*0Sstevel@tonic-gate  * This function is intended for use by subsystems that have detected a fatal
358*0Sstevel@tonic-gate  * error and enqueued appropriate ereports and wish to then force a panic.
359*0Sstevel@tonic-gate  */
360*0Sstevel@tonic-gate /*PRINTFLIKE1*/
361*0Sstevel@tonic-gate void
362*0Sstevel@tonic-gate fm_panic(const char *format, ...)
363*0Sstevel@tonic-gate {
364*0Sstevel@tonic-gate 	va_list ap;
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	(void) casptr((void *)&fm_panicstr, NULL, (void *)format);
367*0Sstevel@tonic-gate 	va_start(ap, format);
368*0Sstevel@tonic-gate 	vpanic(format, ap);
369*0Sstevel@tonic-gate 	va_end(ap);
370*0Sstevel@tonic-gate }
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate /*
373*0Sstevel@tonic-gate  * Print any appropriate FMA banner message before the panic message.  This
374*0Sstevel@tonic-gate  * function is called by panicsys() and prints the message for fm_panic().
375*0Sstevel@tonic-gate  * We print the message here so that it comes after the system is quiesced.
376*0Sstevel@tonic-gate  * A one-line summary is recorded in the log only (cmn_err(9F) with "!" prefix).
377*0Sstevel@tonic-gate  * The rest of the message is for the console only and not needed in the log,
378*0Sstevel@tonic-gate  * so it is printed using console_printf().  We break it up into multiple
379*0Sstevel@tonic-gate  * chunks so as to avoid overflowing any small legacy prom_printf() buffers.
380*0Sstevel@tonic-gate  */
381*0Sstevel@tonic-gate void
382*0Sstevel@tonic-gate fm_banner(void)
383*0Sstevel@tonic-gate {
384*0Sstevel@tonic-gate 	timespec_t tod;
385*0Sstevel@tonic-gate 	hrtime_t now;
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	if (!fm_panicstr)
388*0Sstevel@tonic-gate 		return; /* panic was not initiated by fm_panic(); do nothing */
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	if (panicstr) {
391*0Sstevel@tonic-gate 		tod = panic_hrestime;
392*0Sstevel@tonic-gate 		now = panic_hrtime;
393*0Sstevel@tonic-gate 	} else {
394*0Sstevel@tonic-gate 		gethrestime(&tod);
395*0Sstevel@tonic-gate 		now = gethrtime_waitfree();
396*0Sstevel@tonic-gate 	}
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	cmn_err(CE_NOTE, "!SUNW-MSG-ID: %s, "
399*0Sstevel@tonic-gate 	    "TYPE: Error, VER: 1, SEVERITY: Major\n", fm_msgid);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	console_printf(
402*0Sstevel@tonic-gate "\n\rSUNW-MSG-ID: %s, TYPE: Error, VER: 1, SEVERITY: Major\n"
403*0Sstevel@tonic-gate "EVENT-TIME: 0x%lx.0x%lx (0x%llx)\n",
404*0Sstevel@tonic-gate 	    fm_msgid, tod.tv_sec, tod.tv_nsec, (u_longlong_t)now);
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	console_printf(
407*0Sstevel@tonic-gate "PLATFORM: %s, CSN: -, HOSTNAME: %s\n"
408*0Sstevel@tonic-gate "SOURCE: %s, REV: %s %s\n",
409*0Sstevel@tonic-gate 	    platform, utsname.nodename, utsname.sysname,
410*0Sstevel@tonic-gate 	    utsname.release, utsname.version);
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	console_printf(
413*0Sstevel@tonic-gate "DESC: Errors have been detected that require a reboot to ensure system\n"
414*0Sstevel@tonic-gate "integrity.  See %s/%s for more information.\n",
415*0Sstevel@tonic-gate 	    fm_url, fm_msgid);
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	console_printf(
418*0Sstevel@tonic-gate "AUTO-RESPONSE: Solaris will attempt to save and diagnose the error telemetry\n"
419*0Sstevel@tonic-gate "IMPACT: The system will sync files, save a crash dump if needed, and reboot\n"
420*0Sstevel@tonic-gate "REC-ACTION: Save the error summary below in case telemetry cannot be saved\n");
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	console_printf("\n");
423*0Sstevel@tonic-gate }
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate /*
426*0Sstevel@tonic-gate  * Utility function to write all of the pending ereports to the dump device.
427*0Sstevel@tonic-gate  * This function is called at either normal reboot or panic time, and simply
428*0Sstevel@tonic-gate  * iterates over the in-transit messages in the ereport sysevent channel.
429*0Sstevel@tonic-gate  */
430*0Sstevel@tonic-gate void
431*0Sstevel@tonic-gate fm_ereport_dump(void)
432*0Sstevel@tonic-gate {
433*0Sstevel@tonic-gate 	evchanq_t *chq;
434*0Sstevel@tonic-gate 	sysevent_t *sep;
435*0Sstevel@tonic-gate 	erpt_dump_t ed;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	timespec_t tod;
438*0Sstevel@tonic-gate 	hrtime_t now;
439*0Sstevel@tonic-gate 	char *buf;
440*0Sstevel@tonic-gate 	size_t len;
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	if (panicstr) {
443*0Sstevel@tonic-gate 		tod = panic_hrestime;
444*0Sstevel@tonic-gate 		now = panic_hrtime;
445*0Sstevel@tonic-gate 	} else {
446*0Sstevel@tonic-gate 		if (ereport_errorq != NULL)
447*0Sstevel@tonic-gate 			errorq_drain(ereport_errorq);
448*0Sstevel@tonic-gate 		gethrestime(&tod);
449*0Sstevel@tonic-gate 		now = gethrtime_waitfree();
450*0Sstevel@tonic-gate 	}
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	/*
453*0Sstevel@tonic-gate 	 * In the panic case, sysevent_evc_walk_init() will return NULL.
454*0Sstevel@tonic-gate 	 */
455*0Sstevel@tonic-gate 	if ((chq = sysevent_evc_walk_init(ereport_chan, NULL)) == NULL &&
456*0Sstevel@tonic-gate 	    !panicstr)
457*0Sstevel@tonic-gate 		return; /* event channel isn't initialized yet */
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	while ((sep = sysevent_evc_walk_step(chq)) != NULL) {
460*0Sstevel@tonic-gate 		if ((buf = sysevent_evc_event_attr(sep, &len)) == NULL)
461*0Sstevel@tonic-gate 			break;
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 		ed.ed_magic = ERPT_MAGIC;
464*0Sstevel@tonic-gate 		ed.ed_chksum = checksum32(buf, len);
465*0Sstevel@tonic-gate 		ed.ed_size = (uint32_t)len;
466*0Sstevel@tonic-gate 		ed.ed_pad = 0;
467*0Sstevel@tonic-gate 		ed.ed_hrt_nsec = SE_TIME(sep);
468*0Sstevel@tonic-gate 		ed.ed_hrt_base = now;
469*0Sstevel@tonic-gate 		ed.ed_tod_base.sec = tod.tv_sec;
470*0Sstevel@tonic-gate 		ed.ed_tod_base.nsec = tod.tv_nsec;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 		dumpvp_write(&ed, sizeof (ed));
473*0Sstevel@tonic-gate 		dumpvp_write(buf, len);
474*0Sstevel@tonic-gate 	}
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	sysevent_evc_walk_fini(chq);
477*0Sstevel@tonic-gate }
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate /*
480*0Sstevel@tonic-gate  * Post an error report (ereport) to the sysevent error channel.  The error
481*0Sstevel@tonic-gate  * channel must be established with a prior call to sysevent_evc_create()
482*0Sstevel@tonic-gate  * before publication may occur.
483*0Sstevel@tonic-gate  */
484*0Sstevel@tonic-gate void
485*0Sstevel@tonic-gate fm_ereport_post(nvlist_t *ereport, int evc_flag)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate 	size_t nvl_size = 0;
488*0Sstevel@tonic-gate 	evchan_t *error_chan;
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 	(void) nvlist_size(ereport, &nvl_size, NV_ENCODE_NATIVE);
491*0Sstevel@tonic-gate 	if (nvl_size > ERPT_DATA_SZ || nvl_size == 0) {
492*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
493*0Sstevel@tonic-gate 		return;
494*0Sstevel@tonic-gate 	}
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	if (sysevent_evc_bind(FM_ERROR_CHAN, &error_chan,
497*0Sstevel@tonic-gate 	    EVCH_CREAT|EVCH_HOLD_PEND) != 0) {
498*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
499*0Sstevel@tonic-gate 		return;
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	if (sysevent_evc_publish(error_chan, EC_FM, ESC_FM_ERROR,
503*0Sstevel@tonic-gate 	    SUNW_VENDOR, FM_PUB, ereport, evc_flag) != 0) {
504*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_dropped.value.ui64, 1);
505*0Sstevel@tonic-gate 		sysevent_evc_unbind(error_chan);
506*0Sstevel@tonic-gate 		return;
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate 	sysevent_evc_unbind(error_chan);
509*0Sstevel@tonic-gate }
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate /*
512*0Sstevel@tonic-gate  * Wrapppers for FM nvlist allocators
513*0Sstevel@tonic-gate  */
514*0Sstevel@tonic-gate /* ARGSUSED */
515*0Sstevel@tonic-gate static void *
516*0Sstevel@tonic-gate i_fm_alloc(nv_alloc_t *nva, size_t size)
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate 	return (kmem_zalloc(size, KM_SLEEP));
519*0Sstevel@tonic-gate }
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate /* ARGSUSED */
522*0Sstevel@tonic-gate static void
523*0Sstevel@tonic-gate i_fm_free(nv_alloc_t *nva, void *buf, size_t size)
524*0Sstevel@tonic-gate {
525*0Sstevel@tonic-gate 	kmem_free(buf, size);
526*0Sstevel@tonic-gate }
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate const nv_alloc_ops_t fm_mem_alloc_ops = {
529*0Sstevel@tonic-gate 	NULL,
530*0Sstevel@tonic-gate 	NULL,
531*0Sstevel@tonic-gate 	i_fm_alloc,
532*0Sstevel@tonic-gate 	i_fm_free,
533*0Sstevel@tonic-gate 	NULL
534*0Sstevel@tonic-gate };
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate /*
537*0Sstevel@tonic-gate  * Create and initialize a new nv_alloc_t for a fixed buffer, buf.  A pointer
538*0Sstevel@tonic-gate  * to the newly allocated nv_alloc_t structure is returned upon success or NULL
539*0Sstevel@tonic-gate  * is returned to indicate that the nv_alloc structure could not be created.
540*0Sstevel@tonic-gate  */
541*0Sstevel@tonic-gate nv_alloc_t *
542*0Sstevel@tonic-gate fm_nva_xcreate(char *buf, size_t bufsz)
543*0Sstevel@tonic-gate {
544*0Sstevel@tonic-gate 	nv_alloc_t *nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate 	if (bufsz == 0 || nv_alloc_init(nvhdl, nv_fixed_ops, buf, bufsz) != 0) {
547*0Sstevel@tonic-gate 		kmem_free(nvhdl, sizeof (nv_alloc_t));
548*0Sstevel@tonic-gate 		return (NULL);
549*0Sstevel@tonic-gate 	}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 	return (nvhdl);
552*0Sstevel@tonic-gate }
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate /*
555*0Sstevel@tonic-gate  * Destroy a previously allocated nv_alloc structure.  The fixed buffer
556*0Sstevel@tonic-gate  * associated with nva must be freed by the caller.
557*0Sstevel@tonic-gate  */
558*0Sstevel@tonic-gate void
559*0Sstevel@tonic-gate fm_nva_xdestroy(nv_alloc_t *nva)
560*0Sstevel@tonic-gate {
561*0Sstevel@tonic-gate 	nv_alloc_fini(nva);
562*0Sstevel@tonic-gate 	kmem_free(nva, sizeof (nv_alloc_t));
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate /*
566*0Sstevel@tonic-gate  * Create a new nv list.  A pointer to a new nv list structure is returned
567*0Sstevel@tonic-gate  * upon success or NULL is returned to indicate that the structure could
568*0Sstevel@tonic-gate  * not be created.  The newly created nv list is created and managed by the
569*0Sstevel@tonic-gate  * operations installed in nva.   If nva is NULL, the default FMA nva
570*0Sstevel@tonic-gate  * operations are installed and used.
571*0Sstevel@tonic-gate  *
572*0Sstevel@tonic-gate  * When called from the kernel and nva == NULL, this function must be called
573*0Sstevel@tonic-gate  * from passive kernel context with no locks held that can prevent a
574*0Sstevel@tonic-gate  * sleeping memory allocation from occurring.  Otherwise, this function may
575*0Sstevel@tonic-gate  * be called from other kernel contexts as long a valid nva created via
576*0Sstevel@tonic-gate  * fm_nva_create() is supplied.
577*0Sstevel@tonic-gate  */
578*0Sstevel@tonic-gate nvlist_t *
579*0Sstevel@tonic-gate fm_nvlist_create(nv_alloc_t *nva)
580*0Sstevel@tonic-gate {
581*0Sstevel@tonic-gate 	int hdl_alloced = 0;
582*0Sstevel@tonic-gate 	nvlist_t *nvl;
583*0Sstevel@tonic-gate 	nv_alloc_t *nvhdl;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	if (nva == NULL) {
586*0Sstevel@tonic-gate 		nvhdl = kmem_zalloc(sizeof (nv_alloc_t), KM_SLEEP);
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 		if (nv_alloc_init(nvhdl, &fm_mem_alloc_ops, NULL, 0) != 0) {
589*0Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
590*0Sstevel@tonic-gate 			return (NULL);
591*0Sstevel@tonic-gate 		}
592*0Sstevel@tonic-gate 		hdl_alloced = 1;
593*0Sstevel@tonic-gate 	} else {
594*0Sstevel@tonic-gate 		nvhdl = nva;
595*0Sstevel@tonic-gate 	}
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 	if (nvlist_xalloc(&nvl, NV_UNIQUE_NAME, nvhdl) != 0) {
598*0Sstevel@tonic-gate 		if (hdl_alloced) {
599*0Sstevel@tonic-gate 			kmem_free(nvhdl, sizeof (nv_alloc_t));
600*0Sstevel@tonic-gate 			nv_alloc_fini(nvhdl);
601*0Sstevel@tonic-gate 		}
602*0Sstevel@tonic-gate 		return (NULL);
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate 	return (nvl);
606*0Sstevel@tonic-gate }
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate /*
609*0Sstevel@tonic-gate  * Destroy a previously allocated nvlist structure.  flag indicates whether
610*0Sstevel@tonic-gate  * or not the associated nva structure should be freed (FM_NVA_FREE) or
611*0Sstevel@tonic-gate  * retained (FM_NVA_RETAIN).  Retaining the nv alloc structure allows
612*0Sstevel@tonic-gate  * it to be re-used for future nvlist creation operations.
613*0Sstevel@tonic-gate  */
614*0Sstevel@tonic-gate void
615*0Sstevel@tonic-gate fm_nvlist_destroy(nvlist_t *nvl, int flag)
616*0Sstevel@tonic-gate {
617*0Sstevel@tonic-gate 	nv_alloc_t *nvhdl;
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	nvhdl = ((nvpriv_t *)(uintptr_t)nvl->nvl_priv)->nvp_nva;
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	nvlist_free(nvl);
622*0Sstevel@tonic-gate 
623*0Sstevel@tonic-gate 	if (nvhdl != NULL) {
624*0Sstevel@tonic-gate 		if (flag == FM_NVA_FREE)
625*0Sstevel@tonic-gate 			fm_nva_xdestroy(nvhdl);
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate }
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate int
630*0Sstevel@tonic-gate i_fm_payload_set(nvlist_t *payload, const char *name, va_list ap)
631*0Sstevel@tonic-gate {
632*0Sstevel@tonic-gate 	int nelem, ret = 0;
633*0Sstevel@tonic-gate 	data_type_t type;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	while (ret == 0 && name != NULL) {
636*0Sstevel@tonic-gate 		type = va_arg(ap, data_type_t);
637*0Sstevel@tonic-gate 		switch (type) {
638*0Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
639*0Sstevel@tonic-gate 			ret = nvlist_add_byte(payload, name,
640*0Sstevel@tonic-gate 			    va_arg(ap, uint_t));
641*0Sstevel@tonic-gate 			break;
642*0Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
643*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
644*0Sstevel@tonic-gate 			ret = nvlist_add_byte_array(payload, name,
645*0Sstevel@tonic-gate 			    va_arg(ap, uchar_t *), nelem);
646*0Sstevel@tonic-gate 			break;
647*0Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
648*0Sstevel@tonic-gate 			ret = nvlist_add_boolean_value(payload, name,
649*0Sstevel@tonic-gate 			    va_arg(ap, boolean_t));
650*0Sstevel@tonic-gate 			break;
651*0Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
652*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
653*0Sstevel@tonic-gate 			ret = nvlist_add_boolean_array(payload, name,
654*0Sstevel@tonic-gate 			    va_arg(ap, boolean_t *), nelem);
655*0Sstevel@tonic-gate 			break;
656*0Sstevel@tonic-gate 		case DATA_TYPE_INT8:
657*0Sstevel@tonic-gate 			ret = nvlist_add_int8(payload, name,
658*0Sstevel@tonic-gate 			    va_arg(ap, int));
659*0Sstevel@tonic-gate 			break;
660*0Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
661*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
662*0Sstevel@tonic-gate 			ret = nvlist_add_int8_array(payload, name,
663*0Sstevel@tonic-gate 			    va_arg(ap, int8_t *), nelem);
664*0Sstevel@tonic-gate 			break;
665*0Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
666*0Sstevel@tonic-gate 			ret = nvlist_add_uint8(payload, name,
667*0Sstevel@tonic-gate 			    va_arg(ap, uint_t));
668*0Sstevel@tonic-gate 			break;
669*0Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
670*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
671*0Sstevel@tonic-gate 			ret = nvlist_add_uint8_array(payload, name,
672*0Sstevel@tonic-gate 			    va_arg(ap, uint8_t *), nelem);
673*0Sstevel@tonic-gate 			break;
674*0Sstevel@tonic-gate 		case DATA_TYPE_INT16:
675*0Sstevel@tonic-gate 			ret = nvlist_add_int16(payload, name,
676*0Sstevel@tonic-gate 			    va_arg(ap, int));
677*0Sstevel@tonic-gate 			break;
678*0Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
679*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
680*0Sstevel@tonic-gate 			ret = nvlist_add_int16_array(payload, name,
681*0Sstevel@tonic-gate 			    va_arg(ap, int16_t *), nelem);
682*0Sstevel@tonic-gate 			break;
683*0Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
684*0Sstevel@tonic-gate 			ret = nvlist_add_uint16(payload, name,
685*0Sstevel@tonic-gate 			    va_arg(ap, uint_t));
686*0Sstevel@tonic-gate 			break;
687*0Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
688*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
689*0Sstevel@tonic-gate 			ret = nvlist_add_uint16_array(payload, name,
690*0Sstevel@tonic-gate 			    va_arg(ap, uint16_t *), nelem);
691*0Sstevel@tonic-gate 			break;
692*0Sstevel@tonic-gate 		case DATA_TYPE_INT32:
693*0Sstevel@tonic-gate 			ret = nvlist_add_int32(payload, name,
694*0Sstevel@tonic-gate 			    va_arg(ap, int32_t));
695*0Sstevel@tonic-gate 			break;
696*0Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
697*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
698*0Sstevel@tonic-gate 			ret = nvlist_add_int32_array(payload, name,
699*0Sstevel@tonic-gate 			    va_arg(ap, int32_t *), nelem);
700*0Sstevel@tonic-gate 			break;
701*0Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
702*0Sstevel@tonic-gate 			ret = nvlist_add_uint32(payload, name,
703*0Sstevel@tonic-gate 			    va_arg(ap, uint32_t));
704*0Sstevel@tonic-gate 			break;
705*0Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
706*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
707*0Sstevel@tonic-gate 			ret = nvlist_add_uint32_array(payload, name,
708*0Sstevel@tonic-gate 			    va_arg(ap, uint32_t *), nelem);
709*0Sstevel@tonic-gate 			break;
710*0Sstevel@tonic-gate 		case DATA_TYPE_INT64:
711*0Sstevel@tonic-gate 			ret = nvlist_add_int64(payload, name,
712*0Sstevel@tonic-gate 			    va_arg(ap, int64_t));
713*0Sstevel@tonic-gate 			break;
714*0Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
715*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
716*0Sstevel@tonic-gate 			ret = nvlist_add_int64_array(payload, name,
717*0Sstevel@tonic-gate 			    va_arg(ap, int64_t *), nelem);
718*0Sstevel@tonic-gate 			break;
719*0Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
720*0Sstevel@tonic-gate 			ret = nvlist_add_uint64(payload, name,
721*0Sstevel@tonic-gate 			    va_arg(ap, uint64_t));
722*0Sstevel@tonic-gate 			break;
723*0Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
724*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
725*0Sstevel@tonic-gate 			ret = nvlist_add_uint64_array(payload, name,
726*0Sstevel@tonic-gate 			    va_arg(ap, uint64_t *), nelem);
727*0Sstevel@tonic-gate 			break;
728*0Sstevel@tonic-gate 		case DATA_TYPE_STRING:
729*0Sstevel@tonic-gate 			ret = nvlist_add_string(payload, name,
730*0Sstevel@tonic-gate 			    va_arg(ap, char *));
731*0Sstevel@tonic-gate 			break;
732*0Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
733*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
734*0Sstevel@tonic-gate 			ret = nvlist_add_string_array(payload, name,
735*0Sstevel@tonic-gate 			    va_arg(ap, char **), nelem);
736*0Sstevel@tonic-gate 			break;
737*0Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
738*0Sstevel@tonic-gate 			ret = nvlist_add_nvlist(payload, name,
739*0Sstevel@tonic-gate 			    va_arg(ap, nvlist_t *));
740*0Sstevel@tonic-gate 			break;
741*0Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
742*0Sstevel@tonic-gate 			nelem = va_arg(ap, int);
743*0Sstevel@tonic-gate 			ret = nvlist_add_nvlist_array(payload, name,
744*0Sstevel@tonic-gate 			    va_arg(ap, nvlist_t **), nelem);
745*0Sstevel@tonic-gate 			break;
746*0Sstevel@tonic-gate 		default:
747*0Sstevel@tonic-gate 			ret = EINVAL;
748*0Sstevel@tonic-gate 		}
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate 		name = va_arg(ap, char *);
751*0Sstevel@tonic-gate 	}
752*0Sstevel@tonic-gate 	return (ret);
753*0Sstevel@tonic-gate }
754*0Sstevel@tonic-gate 
755*0Sstevel@tonic-gate void
756*0Sstevel@tonic-gate fm_payload_set(nvlist_t *payload, ...)
757*0Sstevel@tonic-gate {
758*0Sstevel@tonic-gate 	int ret;
759*0Sstevel@tonic-gate 	const char *name;
760*0Sstevel@tonic-gate 	va_list ap;
761*0Sstevel@tonic-gate 
762*0Sstevel@tonic-gate 	va_start(ap, payload);
763*0Sstevel@tonic-gate 	name = va_arg(ap, char *);
764*0Sstevel@tonic-gate 	ret = i_fm_payload_set(payload, name, ap);
765*0Sstevel@tonic-gate 	va_end(ap);
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	if (ret)
768*0Sstevel@tonic-gate 		atomic_add_64(
769*0Sstevel@tonic-gate 		    &erpt_kstat_data.payload_set_failed.value.ui64, 1);
770*0Sstevel@tonic-gate }
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate /*
773*0Sstevel@tonic-gate  * Set-up and validate the members of an ereport event according to:
774*0Sstevel@tonic-gate  *
775*0Sstevel@tonic-gate  *	Member name		Type		Value
776*0Sstevel@tonic-gate  *	====================================================
777*0Sstevel@tonic-gate  *	class			string		ereport
778*0Sstevel@tonic-gate  *	version			uint8_t		0
779*0Sstevel@tonic-gate  *	ena			uint64_t	<ena>
780*0Sstevel@tonic-gate  *	detector		nvlist_t	<detector>
781*0Sstevel@tonic-gate  *	ereport-payload		nvlist_t	<var args>
782*0Sstevel@tonic-gate  *
783*0Sstevel@tonic-gate  */
784*0Sstevel@tonic-gate void
785*0Sstevel@tonic-gate fm_ereport_set(nvlist_t *ereport, int version, const char *erpt_class,
786*0Sstevel@tonic-gate     uint64_t ena, const nvlist_t *detector, ...)
787*0Sstevel@tonic-gate {
788*0Sstevel@tonic-gate 	char ereport_class[FM_MAX_CLASS];
789*0Sstevel@tonic-gate 	const char *name;
790*0Sstevel@tonic-gate 	va_list ap;
791*0Sstevel@tonic-gate 	int ret;
792*0Sstevel@tonic-gate 
793*0Sstevel@tonic-gate 	if (version != FM_EREPORT_VERS0) {
794*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
795*0Sstevel@tonic-gate 		return;
796*0Sstevel@tonic-gate 	}
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	(void) snprintf(ereport_class, FM_MAX_CLASS, "%s.%s",
799*0Sstevel@tonic-gate 	    FM_EREPORT_CLASS, erpt_class);
800*0Sstevel@tonic-gate 	if (nvlist_add_string(ereport, FM_CLASS, ereport_class) != 0) {
801*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
802*0Sstevel@tonic-gate 		return;
803*0Sstevel@tonic-gate 	}
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 	if (nvlist_add_uint64(ereport, FM_EREPORT_ENA, ena)) {
806*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
807*0Sstevel@tonic-gate 	}
808*0Sstevel@tonic-gate 
809*0Sstevel@tonic-gate 	if (nvlist_add_nvlist(ereport, FM_EREPORT_DETECTOR,
810*0Sstevel@tonic-gate 	    (nvlist_t *)detector) != 0) {
811*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
812*0Sstevel@tonic-gate 	}
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	va_start(ap, detector);
815*0Sstevel@tonic-gate 	name = va_arg(ap, const char *);
816*0Sstevel@tonic-gate 	ret = i_fm_payload_set(ereport, name, ap);
817*0Sstevel@tonic-gate 	va_end(ap);
818*0Sstevel@tonic-gate 
819*0Sstevel@tonic-gate 	if (ret)
820*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.erpt_set_failed.value.ui64, 1);
821*0Sstevel@tonic-gate }
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate static int
824*0Sstevel@tonic-gate i_fm_fmri_hc_set_v0(nvlist_t *hc, uint32_t size, va_list ap)
825*0Sstevel@tonic-gate {
826*0Sstevel@tonic-gate 	int i, ret;
827*0Sstevel@tonic-gate 	const char *name, *id;
828*0Sstevel@tonic-gate 	nvlist_t **hc_nvl;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	if (size <= 0)
831*0Sstevel@tonic-gate 		return (0);
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 	hc_nvl = kmem_zalloc(size * sizeof (nvlist_t *), KM_SLEEP);
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	for (i = 0; i < size; ++i) {
836*0Sstevel@tonic-gate 		name = va_arg(ap, const char *);
837*0Sstevel@tonic-gate 		if (name == NULL) {
838*0Sstevel@tonic-gate 			ret = EINVAL;
839*0Sstevel@tonic-gate 			goto fail;
840*0Sstevel@tonic-gate 		}
841*0Sstevel@tonic-gate 		id = va_arg(ap, const char *);
842*0Sstevel@tonic-gate 		if ((hc_nvl[i] = fm_nvlist_create(
843*0Sstevel@tonic-gate 		    ((nvpriv_t *)(uintptr_t)hc->nvl_priv)->nvp_nva)) == NULL) {
844*0Sstevel@tonic-gate 			ret = ENOMEM;
845*0Sstevel@tonic-gate 			goto fail;
846*0Sstevel@tonic-gate 		}
847*0Sstevel@tonic-gate 		if ((ret = nvlist_add_string(hc_nvl[i], FM_FMRI_HC_NAME,
848*0Sstevel@tonic-gate 		    name)) != 0)
849*0Sstevel@tonic-gate 			goto fail;
850*0Sstevel@tonic-gate 		if ((ret = nvlist_add_string(hc_nvl[i], FM_FMRI_HC_ID,
851*0Sstevel@tonic-gate 		    id)) != 0)
852*0Sstevel@tonic-gate 			goto fail;
853*0Sstevel@tonic-gate 	}
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	if ((ret = nvlist_add_nvlist_array(hc, FM_FMRI_HC_LIST, hc_nvl,
856*0Sstevel@tonic-gate 	    size)) != 0)
857*0Sstevel@tonic-gate 		goto fail;
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	kmem_free(hc_nvl, size * sizeof (nvlist_t *));
860*0Sstevel@tonic-gate 	return (0);
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate fail:
863*0Sstevel@tonic-gate 	for (i = 0; i < size; ++i) {
864*0Sstevel@tonic-gate 		if (hc_nvl[i] != NULL)
865*0Sstevel@tonic-gate 			fm_nvlist_destroy(hc_nvl[i], FM_NVA_RETAIN);
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	kmem_free(hc_nvl, size * sizeof (nvlist_t *));
869*0Sstevel@tonic-gate 	return (ret);
870*0Sstevel@tonic-gate }
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate /*
873*0Sstevel@tonic-gate  * Set-up and validate the members of an dev fmri according to:
874*0Sstevel@tonic-gate  *
875*0Sstevel@tonic-gate  *	Member name		Type		Value
876*0Sstevel@tonic-gate  *	====================================================
877*0Sstevel@tonic-gate  *	version			uint8_t		0
878*0Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
879*0Sstevel@tonic-gate  *	devpath			string		<devpath>
880*0Sstevel@tonic-gate  *	devid			string		<devid>
881*0Sstevel@tonic-gate  *
882*0Sstevel@tonic-gate  * Note that auth and devid are optional members.
883*0Sstevel@tonic-gate  */
884*0Sstevel@tonic-gate void
885*0Sstevel@tonic-gate fm_fmri_dev_set(nvlist_t *fmri_dev, int version, const nvlist_t *auth,
886*0Sstevel@tonic-gate     const char *devpath, const char *devid)
887*0Sstevel@tonic-gate {
888*0Sstevel@tonic-gate 	if (version != DEV_SCHEME_VERSION0) {
889*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
890*0Sstevel@tonic-gate 		return;
891*0Sstevel@tonic-gate 	}
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_dev, FM_VERSION, version) != 0) {
894*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
895*0Sstevel@tonic-gate 		return;
896*0Sstevel@tonic-gate 	}
897*0Sstevel@tonic-gate 
898*0Sstevel@tonic-gate 	if (nvlist_add_string(fmri_dev, FM_FMRI_SCHEME,
899*0Sstevel@tonic-gate 	    FM_FMRI_SCHEME_DEV) != 0) {
900*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
901*0Sstevel@tonic-gate 		return;
902*0Sstevel@tonic-gate 	}
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 	if (auth != NULL) {
905*0Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri_dev, FM_FMRI_AUTHORITY,
906*0Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
907*0Sstevel@tonic-gate 			atomic_add_64(
908*0Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
909*0Sstevel@tonic-gate 		}
910*0Sstevel@tonic-gate 	}
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate 	if (nvlist_add_string(fmri_dev, FM_FMRI_DEV_PATH, devpath) != 0) {
913*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
914*0Sstevel@tonic-gate 	}
915*0Sstevel@tonic-gate 
916*0Sstevel@tonic-gate 	if (devid != NULL)
917*0Sstevel@tonic-gate 		if (nvlist_add_string(fmri_dev, FM_FMRI_DEV_ID, devid) != 0)
918*0Sstevel@tonic-gate 			atomic_add_64(
919*0Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
920*0Sstevel@tonic-gate }
921*0Sstevel@tonic-gate 
922*0Sstevel@tonic-gate /*
923*0Sstevel@tonic-gate  * Set-up and validate the members of an cpu fmri according to:
924*0Sstevel@tonic-gate  *
925*0Sstevel@tonic-gate  *	Member name		Type		Value
926*0Sstevel@tonic-gate  *	====================================================
927*0Sstevel@tonic-gate  *	version			uint8_t		0
928*0Sstevel@tonic-gate  *	auth			nvlist_t	<auth>
929*0Sstevel@tonic-gate  *	cpuid			uint32_t	<cpu_id>
930*0Sstevel@tonic-gate  *	cpumask			uint8_t		<cpu_mask>
931*0Sstevel@tonic-gate  *	serial			uint64_t	<serial_id>
932*0Sstevel@tonic-gate  *
933*0Sstevel@tonic-gate  * Note that auth is an optional member.
934*0Sstevel@tonic-gate  *
935*0Sstevel@tonic-gate  */
936*0Sstevel@tonic-gate void
937*0Sstevel@tonic-gate fm_fmri_cpu_set(nvlist_t *fmri_cpu, int version, const nvlist_t *auth,
938*0Sstevel@tonic-gate     uint32_t cpu_id, uint8_t cpu_mask, uint64_t serial_id)
939*0Sstevel@tonic-gate {
940*0Sstevel@tonic-gate 	if (version != CPU_SCHEME_VERSION0) {
941*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
942*0Sstevel@tonic-gate 		return;
943*0Sstevel@tonic-gate 	}
944*0Sstevel@tonic-gate 
945*0Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_cpu, FM_VERSION, version) != 0) {
946*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
947*0Sstevel@tonic-gate 		return;
948*0Sstevel@tonic-gate 	}
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate 	if (nvlist_add_string(fmri_cpu, FM_FMRI_SCHEME,
951*0Sstevel@tonic-gate 	    FM_FMRI_SCHEME_CPU) != 0) {
952*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
953*0Sstevel@tonic-gate 		return;
954*0Sstevel@tonic-gate 	}
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 	if (auth != NULL)
957*0Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri_cpu, FM_FMRI_AUTHORITY,
958*0Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
959*0Sstevel@tonic-gate 			atomic_add_64(
960*0Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
961*0Sstevel@tonic-gate 		}
962*0Sstevel@tonic-gate 
963*0Sstevel@tonic-gate 	if (nvlist_add_uint32(fmri_cpu, FM_FMRI_CPU_ID, cpu_id) != 0) {
964*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
965*0Sstevel@tonic-gate 	}
966*0Sstevel@tonic-gate 
967*0Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri_cpu, FM_FMRI_CPU_MASK, cpu_mask) != 0) {
968*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
969*0Sstevel@tonic-gate 	}
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	if (nvlist_add_uint64(fmri_cpu, FM_FMRI_CPU_SERIAL_ID, serial_id)
972*0Sstevel@tonic-gate 	    != 0)
973*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
974*0Sstevel@tonic-gate }
975*0Sstevel@tonic-gate 
976*0Sstevel@tonic-gate /*
977*0Sstevel@tonic-gate  * Set-up and validate the members of a mem according to:
978*0Sstevel@tonic-gate  *
979*0Sstevel@tonic-gate  *	Member name		Type		Value
980*0Sstevel@tonic-gate  *	====================================================
981*0Sstevel@tonic-gate  *	version			uint8_t		0
982*0Sstevel@tonic-gate  *	auth			nvlist_t	<auth>		[optional]
983*0Sstevel@tonic-gate  *	unum			string		<unum>
984*0Sstevel@tonic-gate  *	serial			string		<serial>	[optional]
985*0Sstevel@tonic-gate  *
986*0Sstevel@tonic-gate  */
987*0Sstevel@tonic-gate void
988*0Sstevel@tonic-gate fm_fmri_mem_set(nvlist_t *fmri, int version, const nvlist_t *auth,
989*0Sstevel@tonic-gate     const char *unum, const char *serial)
990*0Sstevel@tonic-gate {
991*0Sstevel@tonic-gate 	if (version != MEM_SCHEME_VERSION0) {
992*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
993*0Sstevel@tonic-gate 		return;
994*0Sstevel@tonic-gate 	}
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 	if (nvlist_add_uint8(fmri, FM_VERSION, version) != 0) {
997*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
998*0Sstevel@tonic-gate 		return;
999*0Sstevel@tonic-gate 	}
1000*0Sstevel@tonic-gate 
1001*0Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_MEM) != 0) {
1002*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1003*0Sstevel@tonic-gate 		return;
1004*0Sstevel@tonic-gate 	}
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	if (auth != NULL) {
1007*0Sstevel@tonic-gate 		if (nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY,
1008*0Sstevel@tonic-gate 		    (nvlist_t *)auth) != 0) {
1009*0Sstevel@tonic-gate 			atomic_add_64(
1010*0Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1011*0Sstevel@tonic-gate 		}
1012*0Sstevel@tonic-gate 	}
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 	if (nvlist_add_string(fmri, FM_FMRI_MEM_UNUM, unum) != 0) {
1015*0Sstevel@tonic-gate 		atomic_add_64(&erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1016*0Sstevel@tonic-gate 	}
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 	if (serial != NULL) {
1019*0Sstevel@tonic-gate 		if (nvlist_add_string_array(fmri, FM_FMRI_MEM_SERIAL_ID,
1020*0Sstevel@tonic-gate 		    (char **)&serial, 1) != 0) {
1021*0Sstevel@tonic-gate 			atomic_add_64(
1022*0Sstevel@tonic-gate 			    &erpt_kstat_data.fmri_set_failed.value.ui64, 1);
1023*0Sstevel@tonic-gate 		}
1024*0Sstevel@tonic-gate 	}
1025*0Sstevel@tonic-gate }
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate uint64_t
1028*0Sstevel@tonic-gate fm_ena_increment(uint64_t ena)
1029*0Sstevel@tonic-gate {
1030*0Sstevel@tonic-gate 	uint64_t new_ena;
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
1033*0Sstevel@tonic-gate 	case FM_ENA_FMT1:
1034*0Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT1_GEN_SHFT);
1035*0Sstevel@tonic-gate 		break;
1036*0Sstevel@tonic-gate 	case FM_ENA_FMT2:
1037*0Sstevel@tonic-gate 		new_ena = ena + (1 << ENA_FMT2_GEN_SHFT);
1038*0Sstevel@tonic-gate 		break;
1039*0Sstevel@tonic-gate 	default:
1040*0Sstevel@tonic-gate 		new_ena = 0;
1041*0Sstevel@tonic-gate 	}
1042*0Sstevel@tonic-gate 
1043*0Sstevel@tonic-gate 	return (new_ena);
1044*0Sstevel@tonic-gate }
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate uint64_t
1047*0Sstevel@tonic-gate fm_ena_generate_cpu(uint64_t timestamp, processorid_t cpuid, uchar_t format)
1048*0Sstevel@tonic-gate {
1049*0Sstevel@tonic-gate 	uint64_t ena = 0;
1050*0Sstevel@tonic-gate 
1051*0Sstevel@tonic-gate 	switch (format) {
1052*0Sstevel@tonic-gate 	case FM_ENA_FMT1:
1053*0Sstevel@tonic-gate 		if (timestamp) {
1054*0Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1055*0Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
1056*0Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
1057*0Sstevel@tonic-gate 			    ((timestamp << ENA_FMT1_TIME_SHFT) &
1058*0Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
1059*0Sstevel@tonic-gate 		} else {
1060*0Sstevel@tonic-gate 			ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1061*0Sstevel@tonic-gate 			    ((cpuid << ENA_FMT1_CPUID_SHFT) &
1062*0Sstevel@tonic-gate 			    ENA_FMT1_CPUID_MASK) |
1063*0Sstevel@tonic-gate 			    ((gethrtime_waitfree() << ENA_FMT1_TIME_SHFT) &
1064*0Sstevel@tonic-gate 			    ENA_FMT1_TIME_MASK));
1065*0Sstevel@tonic-gate 		}
1066*0Sstevel@tonic-gate 		break;
1067*0Sstevel@tonic-gate 	case FM_ENA_FMT2:
1068*0Sstevel@tonic-gate 		ena = (uint64_t)((format & ENA_FORMAT_MASK) |
1069*0Sstevel@tonic-gate 		    ((timestamp << ENA_FMT2_TIME_SHFT) & ENA_FMT2_TIME_MASK));
1070*0Sstevel@tonic-gate 		break;
1071*0Sstevel@tonic-gate 	default:
1072*0Sstevel@tonic-gate 		break;
1073*0Sstevel@tonic-gate 	}
1074*0Sstevel@tonic-gate 
1075*0Sstevel@tonic-gate 	return (ena);
1076*0Sstevel@tonic-gate }
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate uint64_t
1079*0Sstevel@tonic-gate fm_ena_generate(uint64_t timestamp, uchar_t format)
1080*0Sstevel@tonic-gate {
1081*0Sstevel@tonic-gate 	return (fm_ena_generate_cpu(timestamp, CPU->cpu_id, format));
1082*0Sstevel@tonic-gate }
1083*0Sstevel@tonic-gate 
1084*0Sstevel@tonic-gate uint64_t
1085*0Sstevel@tonic-gate fm_ena_generation_get(uint64_t ena)
1086*0Sstevel@tonic-gate {
1087*0Sstevel@tonic-gate 	uint64_t gen;
1088*0Sstevel@tonic-gate 
1089*0Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
1090*0Sstevel@tonic-gate 	case FM_ENA_FMT1:
1091*0Sstevel@tonic-gate 		gen = (ena & ENA_FMT1_GEN_MASK) >> ENA_FMT1_GEN_SHFT;
1092*0Sstevel@tonic-gate 		break;
1093*0Sstevel@tonic-gate 	case FM_ENA_FMT2:
1094*0Sstevel@tonic-gate 		gen = (ena & ENA_FMT2_GEN_MASK) >> ENA_FMT2_GEN_SHFT;
1095*0Sstevel@tonic-gate 		break;
1096*0Sstevel@tonic-gate 	default:
1097*0Sstevel@tonic-gate 		gen = 0;
1098*0Sstevel@tonic-gate 		break;
1099*0Sstevel@tonic-gate 	}
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate 	return (gen);
1102*0Sstevel@tonic-gate }
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate uchar_t
1105*0Sstevel@tonic-gate fm_ena_format_get(uint64_t ena)
1106*0Sstevel@tonic-gate {
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	return (ENA_FORMAT(ena));
1109*0Sstevel@tonic-gate }
1110*0Sstevel@tonic-gate 
1111*0Sstevel@tonic-gate uint64_t
1112*0Sstevel@tonic-gate fm_ena_id_get(uint64_t ena)
1113*0Sstevel@tonic-gate {
1114*0Sstevel@tonic-gate 	uint64_t id;
1115*0Sstevel@tonic-gate 
1116*0Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
1117*0Sstevel@tonic-gate 	case FM_ENA_FMT1:
1118*0Sstevel@tonic-gate 		id = (ena & ENA_FMT1_ID_MASK) >> ENA_FMT1_ID_SHFT;
1119*0Sstevel@tonic-gate 		break;
1120*0Sstevel@tonic-gate 	case FM_ENA_FMT2:
1121*0Sstevel@tonic-gate 		id = (ena & ENA_FMT2_ID_MASK) >> ENA_FMT2_ID_SHFT;
1122*0Sstevel@tonic-gate 		break;
1123*0Sstevel@tonic-gate 	default:
1124*0Sstevel@tonic-gate 		id = 0;
1125*0Sstevel@tonic-gate 	}
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 	return (id);
1128*0Sstevel@tonic-gate }
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate uint64_t
1131*0Sstevel@tonic-gate fm_ena_time_get(uint64_t ena)
1132*0Sstevel@tonic-gate {
1133*0Sstevel@tonic-gate 	uint64_t time;
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate 	switch (ENA_FORMAT(ena)) {
1136*0Sstevel@tonic-gate 	case FM_ENA_FMT1:
1137*0Sstevel@tonic-gate 		time = (ena & ENA_FMT1_TIME_MASK) >> ENA_FMT1_TIME_SHFT;
1138*0Sstevel@tonic-gate 		break;
1139*0Sstevel@tonic-gate 	case FM_ENA_FMT2:
1140*0Sstevel@tonic-gate 		time = (ena & ENA_FMT2_TIME_MASK) >> ENA_FMT2_TIME_SHFT;
1141*0Sstevel@tonic-gate 		break;
1142*0Sstevel@tonic-gate 	default:
1143*0Sstevel@tonic-gate 		time = 0;
1144*0Sstevel@tonic-gate 	}
1145*0Sstevel@tonic-gate 
1146*0Sstevel@tonic-gate 	return (time);
1147*0Sstevel@tonic-gate }
1148