1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <cma.h>
30 
31 #include <strings.h>
32 #include <errno.h>
33 #include <time.h>
34 #include <fm/fmd_api.h>
35 #include <sys/fm/protocol.h>
36 #include <sys/systeminfo.h>
37 #include <sys/utsname.h>
38 
39 cma_t cma;
40 
41 cma_stats_t cma_stats = {
42 	{ "cpu_flts", FMD_TYPE_UINT64, "cpu faults resolved" },
43 	{ "cpu_fails", FMD_TYPE_UINT64, "cpu faults unresolveable" },
44 	{ "cpu_blfails", FMD_TYPE_UINT64, "failed cpu blacklists" },
45 	{ "cpu_supp", FMD_TYPE_UINT64, "cpu offlines suppressed" },
46 	{ "cpu_blsupp", FMD_TYPE_UINT64, "cpu blacklists suppressed" },
47 	{ "page_flts", FMD_TYPE_UINT64, "page faults resolved" },
48 	{ "page_fails", FMD_TYPE_UINT64, "page faults unresolveable" },
49 	{ "page_supp", FMD_TYPE_UINT64, "page retires suppressed" },
50 	{ "page_nonent", FMD_TYPE_UINT64, "retires for non-existent fmris" },
51 	{ "page_retmax", FMD_TYPE_UINT64, "hit max retries for page retire" },
52 	{ "bad_flts", FMD_TYPE_UINT64, "invalid fault events received" },
53 	{ "nop_flts", FMD_TYPE_UINT64, "inapplicable fault events received" },
54 	{ "auto_flts", FMD_TYPE_UINT64, "auto-close faults received" }
55 };
56 
57 typedef struct cma_subscriber {
58 	const char *subr_class;
59 	const char *subr_sname;
60 	uint_t subr_svers;
61 	int (*subr_func)(fmd_hdl_t *, nvlist_t *, nvlist_t *, const char *);
62 } cma_subscriber_t;
63 
64 static const cma_subscriber_t cma_subrs[] = {
65 	{ "fault.memory.page", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
66 	    cma_page_retire },
67 	{ "fault.memory.page_sb", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
68 	    cma_page_retire },
69 	{ "fault.memory.page_ck", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
70 	    cma_page_retire },
71 	{ "fault.memory.page_ue", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
72 	    cma_page_retire },
73 	{ "fault.memory.dimm", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
74 	    NULL },
75 	{ "fault.memory.dimm_sb", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
76 	    NULL },
77 	{ "fault.memory.dimm_ck", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
78 	    NULL },
79 	{ "fault.memory.dimm_ue", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
80 	    NULL },
81 	{ "fault.memory.dimm_testfail", FM_FMRI_SCHEME_MEM,
82 	    FM_MEM_SCHEME_VERSION, NULL },
83 	{ "fault.memory.bank", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
84 	    NULL },
85 	{ "fault.memory.datapath", FM_FMRI_SCHEME_MEM, FM_MEM_SCHEME_VERSION,
86 	    NULL },
87 
88 	/*
89 	 * The following faults do NOT retire a cpu thread,
90 	 * and therefore must be intercepted before
91 	 * the default "fault.cpu.*" dispatch to cma_cpu_retire.
92 	 */
93 	{ "fault.cpu.ultraSPARC-T1.freg", FM_FMRI_SCHEME_CPU,
94 	    FM_CPU_SCHEME_VERSION, NULL },
95 	{ "fault.cpu.ultraSPARC-T1.l2cachedata", FM_FMRI_SCHEME_CPU,
96 	    FM_CPU_SCHEME_VERSION, NULL },
97 	{ "fault.cpu.ultraSPARC-T1.l2cachetag", FM_FMRI_SCHEME_CPU,
98 	    FM_CPU_SCHEME_VERSION, NULL },
99 	{ "fault.cpu.ultraSPARC-T1.l2cachectl", FM_FMRI_SCHEME_CPU,
100 	    FM_CPU_SCHEME_VERSION, NULL },
101 	{ "fault.cpu.ultraSPARC-T1.mau", FM_FMRI_SCHEME_CPU,
102 	    FM_CPU_SCHEME_VERSION, NULL },
103 	{ "fault.cpu.amd.dramchannel", FM_FMRI_SCHEME_HC, FM_HC_SCHEME_VERSION,
104 	    NULL },
105 	{ "fault.cpu.*", FM_FMRI_SCHEME_CPU, FM_CPU_SCHEME_VERSION,
106 	    cma_cpu_retire },
107 	{ NULL, NULL, 0, NULL }
108 };
109 
110 static const cma_subscriber_t *
111 nvl2subr(fmd_hdl_t *hdl, nvlist_t *nvl, nvlist_t **asrup)
112 {
113 	const cma_subscriber_t *sp;
114 	nvlist_t *asru;
115 	char *scheme;
116 	uint8_t version;
117 	char *fltclass = "(unknown)";
118 
119 	if (nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU, &asru) != 0 ||
120 	    nvlist_lookup_string(asru, FM_FMRI_SCHEME, &scheme) != 0 ||
121 	    nvlist_lookup_uint8(asru, FM_VERSION, &version) != 0) {
122 		cma_stats.bad_flts.fmds_value.ui64++;
123 		return (NULL);
124 	}
125 
126 	for (sp = cma_subrs; sp->subr_class != NULL; sp++) {
127 		if (fmd_nvl_class_match(hdl, nvl, sp->subr_class) &&
128 		    strcmp(scheme, sp->subr_sname) == 0 &&
129 		    version <= sp->subr_svers) {
130 			*asrup = asru;
131 			return (sp);
132 		}
133 	}
134 
135 	(void) nvlist_lookup_string(nvl, FM_CLASS, &fltclass);
136 	fmd_hdl_error(hdl, "No handling disposition for %s with asru in "
137 	    "scheme \"%s\"\n", fltclass, scheme);
138 	cma_stats.nop_flts.fmds_value.ui64++;
139 	return (NULL);
140 }
141 
142 static void
143 cma_recv_list(fmd_hdl_t *hdl, nvlist_t *nvl)
144 {
145 	char *uuid = NULL;
146 	nvlist_t **nva;
147 	uint_t nvc = 0;
148 	uint_t keepopen;
149 	int err = 0;
150 
151 	err |= nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid);
152 	err |= nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
153 	    &nva, &nvc);
154 	if (err != 0) {
155 		cma_stats.bad_flts.fmds_value.ui64++;
156 		return;
157 	}
158 
159 	keepopen = nvc;
160 	while (nvc-- != 0 && !fmd_case_uuclosed(hdl, uuid)) {
161 		nvlist_t *nvl = *nva++;
162 		const cma_subscriber_t *subr;
163 		nvlist_t *asru;
164 
165 		if ((subr = nvl2subr(hdl, nvl, &asru)) == NULL)
166 			continue;
167 
168 		/*
169 		 * A handler returns CMA_RA_SUCCESS to indicate that
170 		 * from this suspects  point-of-view the case may be
171 		 * closed, CMA_RA_FAILURE otherwise.
172 		 * A handler must not close the case itself.
173 		 */
174 		if (subr->subr_func != NULL) {
175 			err = subr->subr_func(hdl, nvl, asru, uuid);
176 
177 			if (err == CMA_RA_SUCCESS)
178 				keepopen--;
179 		}
180 	}
181 
182 	if (!keepopen)
183 		fmd_case_uuclose(hdl, uuid);
184 }
185 
186 static void
187 cma_recv_one(fmd_hdl_t *hdl, nvlist_t *nvl)
188 {
189 	const cma_subscriber_t *subr;
190 	nvlist_t *asru;
191 
192 	if ((subr = nvl2subr(hdl, nvl, &asru)) == NULL)
193 		return;
194 
195 	if (subr->subr_func != NULL)
196 		(void) subr->subr_func(hdl, nvl, asru, NULL);
197 
198 }
199 
200 /*ARGSUSED*/
201 static void
202 cma_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
203 {
204 	fmd_hdl_debug(hdl, "received %s\n", class);
205 
206 	if (strcmp(class, FM_LIST_SUSPECT_CLASS) == 0)
207 		cma_recv_list(hdl, nvl);
208 	else
209 		cma_recv_one(hdl, nvl);
210 }
211 
212 /*ARGSUSED*/
213 static void
214 cma_timeout(fmd_hdl_t *hdl, id_t id, void *arg)
215 {
216 	if (id == cma.cma_page_timerid)
217 		cma_page_retry(hdl);
218 }
219 
220 static const fmd_hdl_ops_t fmd_ops = {
221 	cma_recv,	/* fmdo_recv */
222 	cma_timeout,	/* fmdo_timeout */
223 	NULL,		/* fmdo_close */
224 	NULL,		/* fmdo_stats */
225 	NULL,		/* fmdo_gc */
226 };
227 
228 static const fmd_prop_t fmd_props[] = {
229 	{ "cpu_tries", FMD_TYPE_UINT32, "10" },
230 	{ "cpu_delay", FMD_TYPE_TIME, "1sec" },
231 	{ "cpu_offline_enable", FMD_TYPE_BOOL, "true" },
232 	{ "cpu_forced_offline", FMD_TYPE_BOOL, "true" },
233 	{ "cpu_blacklist_enable", FMD_TYPE_BOOL, "true" },
234 	{ "page_ret_mindelay", FMD_TYPE_TIME, "1sec" },
235 	{ "page_ret_maxdelay", FMD_TYPE_TIME, "5min" },
236 	{ "page_retire_enable", FMD_TYPE_BOOL, "true" },
237 #ifdef	i386
238 	/*
239 	 * On i386, leaving cases open while we retry the
240 	 * retire can cause the eft module to use large amounts
241 	 * of memory.  Until eft is fixed, we set a maximum number
242 	 * of retries on page retires, after which the case will
243 	 * be closed.
244 	 */
245 	{ "page_retire_maxretries", FMD_TYPE_UINT32, "5" },
246 #else
247 	{ "page_retire_maxretries", FMD_TYPE_UINT32, "0" },
248 #endif	/* i386 */
249 	{ NULL, 0, NULL }
250 };
251 
252 static const fmd_hdl_info_t fmd_info = {
253 	"CPU/Memory Retire Agent", CMA_VERSION, &fmd_ops, fmd_props
254 };
255 
256 void
257 _fmd_init(fmd_hdl_t *hdl)
258 {
259 	hrtime_t nsec;
260 	char buf[SYS_NMLN];
261 	int ret;
262 
263 	/*
264 	 * Abort the cpumem-retire module if Solaris is running under the Xen
265 	 * hypervisor.
266 	 */
267 	ret = sysinfo(SI_PLATFORM, buf, sizeof (buf));
268 	if (ret == -1 || (strncmp(buf, "i86xpv", sizeof (buf)) == 0))
269 		return;
270 
271 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
272 		return; /* invalid data in configuration file */
273 
274 	fmd_hdl_subscribe(hdl, "fault.cpu.*");
275 	fmd_hdl_subscribe(hdl, "fault.memory.*");
276 
277 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (cma_stats) /
278 	    sizeof (fmd_stat_t), (fmd_stat_t *)&cma_stats);
279 
280 	cma.cma_cpu_tries = fmd_prop_get_int32(hdl, "cpu_tries");
281 
282 	nsec = fmd_prop_get_int64(hdl, "cpu_delay");
283 	cma.cma_cpu_delay.tv_sec = nsec / NANOSEC;
284 	cma.cma_cpu_delay.tv_nsec = nsec % NANOSEC;
285 
286 	cma.cma_page_mindelay = fmd_prop_get_int64(hdl, "page_ret_mindelay");
287 	cma.cma_page_maxdelay = fmd_prop_get_int64(hdl, "page_ret_maxdelay");
288 
289 	cma.cma_cpu_dooffline = fmd_prop_get_int32(hdl, "cpu_offline_enable");
290 	cma.cma_cpu_forcedoffline = fmd_prop_get_int32(hdl,
291 	    "cpu_forced_offline");
292 	cma.cma_cpu_doblacklist = fmd_prop_get_int32(hdl,
293 	    "cpu_blacklist_enable");
294 	cma.cma_page_doretire = fmd_prop_get_int32(hdl, "page_retire_enable");
295 	cma.cma_page_maxretries =
296 	    fmd_prop_get_int32(hdl, "page_retire_maxretries");
297 
298 	if (cma.cma_page_maxdelay < cma.cma_page_mindelay)
299 		fmd_hdl_abort(hdl, "page retirement delays conflict\n");
300 }
301 
302 void
303 _fmd_fini(fmd_hdl_t *hdl)
304 {
305 	cma_page_fini(hdl);
306 }
307