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  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <gmem_state.h>
26 #include <gmem_mem.h>
27 #include <gmem_page.h>
28 #include <gmem_dimm.h>
29 #include <gmem.h>
30 
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <strings.h>
35 #include <fm/fmd_api.h>
36 #include <fm/libtopo.h>
37 #include <sys/fm/protocol.h>
38 #include <sys/async.h>
39 
40 gmem_t gmem;
41 
42 typedef struct gmem_subscriber {
43 	const char *subr_class;
44 	gmem_evdisp_t (*subr_func)(fmd_hdl_t *, fmd_event_t *, nvlist_t *,
45 	    const char *);
46 	gmem_evdisp_stat_t subr_stat;
47 } gmem_subscriber_t;
48 
49 static gmem_subscriber_t gmem_subscribers[] = {
50 	{ "ereport.cpu.generic-sparc.mem-is",	gmem_ce },
51 	{ "ereport.cpu.generic-sparc.mem-unk",	gmem_ce },
52 	{ "ereport.cpu.generic-sparc.mem-cs",	gmem_ce },
53 	{ "ereport.cpu.generic-sparc.mem-ss",	gmem_ce },
54 	{ NULL, NULL }
55 };
56 
57 static void
gmem_recv(fmd_hdl_t * hdl,fmd_event_t * ep,nvlist_t * nvl,const char * class)58 gmem_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
59 {
60 	gmem_subscriber_t *sp;
61 	int disp;
62 
63 	fmd_hdl_debug(hdl, "gmem_recv: begin: %s\n", strrchr(class, '.') + 1);
64 
65 	for (sp = gmem_subscribers; sp->subr_class != NULL; sp++) {
66 		if (fmd_nvl_class_match(hdl, nvl, sp->subr_class)) {
67 			disp = sp->subr_func(hdl, ep, nvl, class);
68 			((fmd_stat_t *)&sp->subr_stat)[disp].fmds_value.ui64++;
69 			fmd_hdl_debug(hdl, "gmem_recv: done: %s (disp %d)\n",
70 			    strrchr(class, '.') + 1, disp);
71 			return;
72 		}
73 	}
74 
75 	fmd_hdl_debug(hdl, "gmem_recv: dropping %s - unable to handle\n",
76 	    class);
77 }
78 
79 static void
gmem_close(fmd_hdl_t * hdl,fmd_case_t * cp)80 gmem_close(fmd_hdl_t *hdl, fmd_case_t *cp)
81 {
82 	gmem_case_closer_t *cl = fmd_case_getspecific(hdl, cp);
83 	const char *uuid = fmd_case_uuid(hdl, cp);
84 
85 	/*
86 	 * Our active cases all have closers registered in case-specific data.
87 	 * Cases in the process of closing (for which we've freed all associated
88 	 * data, but which haven't had an fmd-initiated fmdo_close callback)
89 	 * have had their case-specific data nulled out.
90 	 */
91 	fmd_hdl_debug(hdl, "close case %s%s\n", uuid,
92 	    (cl == NULL ? " (no cl)" : ""));
93 
94 	if (cl != NULL)
95 		cl->cl_func(hdl, cl->cl_arg);
96 }
97 
98 static void
gmem_gc(fmd_hdl_t * hdl)99 gmem_gc(fmd_hdl_t *hdl)
100 {
101 	gmem_mem_gc(hdl);
102 }
103 
104 static gmem_stat_t gm_stats = {
105 	{ "bad_mem_resource", FMD_TYPE_UINT64,
106 	    "memory resource missing or malformed" },
107 	{ "bad_close", FMD_TYPE_UINT64, "case close for nonexistent case" },
108 	{ "old_erpt", FMD_TYPE_UINT64, "ereport out of date wrt hardware" },
109 	{ "dimm_creat", FMD_TYPE_UINT64, "created new mem module structure" },
110 	{ "page_creat", FMD_TYPE_UINT64, "created new page structure" },
111 	{ "ce_unknown", FMD_TYPE_UINT64, "unknown CEs" },
112 	{ "ce_interm", FMD_TYPE_UINT64, "intermittent CEs" },
113 	{ "ce_clearable_persis", FMD_TYPE_UINT64, "clearable persistent CEs" },
114 	{ "ce_sticky", FMD_TYPE_UINT64, "sticky CEs" },
115 	{ "dimm_migrat", FMD_TYPE_UINT64, "DIMMs migrated to new version" }
116 };
117 
118 static const fmd_prop_t fmd_props[] = {
119 	{ "ce_n", FMD_TYPE_UINT32, "3" },
120 	{ "ce_t", FMD_TYPE_TIME, "72h" },
121 	{ "filter_ratio", FMD_TYPE_UINT32, "0" },
122 	{ "max_retired_pages", FMD_TYPE_UINT32, "512" },
123 	{ "low_ce_thresh", FMD_TYPE_UINT32, "128"},
124 	{ "nupos", FMD_TYPE_UINT32, "4"},
125 	{ "dupce", FMD_TYPE_UINT32, "120"},
126 	{ NULL, 0, NULL }
127 };
128 
129 static const fmd_hdl_ops_t fmd_ops = {
130 	gmem_recv,	/* fmdo_recv */
131 	NULL,
132 	gmem_close,	/* fmdo_close */
133 	NULL,		/* fmdo_stats */
134 	gmem_gc		/* fmdo_gc */
135 };
136 
137 static const fmd_hdl_info_t fmd_info = {
138 	"SPARC-Generic-Memory Diagnosis", GMEM_VERSION, &fmd_ops, fmd_props
139 };
140 
141 static const struct gmem_evdisp_name {
142 	const char *evn_name;
143 	const char *evn_desc;
144 } gmem_evdisp_names[] = {
145 	{ "%s", "ok %s ereports" },			/* GMEM_EVD_OK */
146 	{ "bad_%s", "bad %s ereports" },		/* GMEM_EVD_BAD */
147 	{ "unused_%s", "unused %s ereports" },		/* GMEM_EVD_UNUSED */
148 	{ "redun_%s", "redundant %s ereports" },	/* GMEM_EVD_REDUN */
149 };
150 
151 void
_fmd_fini(fmd_hdl_t * hdl)152 _fmd_fini(fmd_hdl_t *hdl)
153 {
154 	gmem_mem_fini(hdl);
155 	gmem_page_fini(hdl);
156 }
157 
158 void
_fmd_init(fmd_hdl_t * hdl)159 _fmd_init(fmd_hdl_t *hdl)
160 {
161 	gmem_subscriber_t *sp;
162 
163 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0)
164 		return; /* error in configuration file or fmd_info */
165 
166 	for (sp = gmem_subscribers; sp->subr_class != NULL; sp++)
167 		fmd_hdl_subscribe(hdl, sp->subr_class);
168 
169 	bzero(&gmem, sizeof (gmem_t));
170 
171 	gmem.gm_stats = (gmem_stat_t *)fmd_stat_create(hdl, FMD_STAT_NOALLOC,
172 	    sizeof (gm_stats) / sizeof (fmd_stat_t),
173 	    (fmd_stat_t *)&gm_stats);
174 
175 	for (sp = gmem_subscribers; sp->subr_class != NULL; sp++) {
176 		const char *type = strrchr(sp->subr_class, '.') + 1;
177 		int i;
178 
179 		for (i = 0; i < sizeof (gmem_evdisp_names) /
180 		    sizeof (struct gmem_evdisp_name); i++) {
181 			fmd_stat_t *stat = ((fmd_stat_t *)&sp->subr_stat) + i;
182 
183 			(void) snprintf(stat->fmds_name,
184 			    sizeof (stat->fmds_name),
185 			    gmem_evdisp_names[i].evn_name, type);
186 
187 			stat->fmds_type = FMD_TYPE_UINT64;
188 			(void) snprintf(stat->fmds_desc,
189 			    sizeof (stat->fmds_desc),
190 			    gmem_evdisp_names[i].evn_desc, type);
191 
192 			(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, 1, stat);
193 		}
194 	}
195 
196 	gmem.gm_pagesize = sysconf(_SC_PAGESIZE);
197 	gmem.gm_pagemask = ~((uint64_t)gmem.gm_pagesize - 1);
198 
199 	gmem.gm_max_retired_pages = fmd_prop_get_int32(hdl,
200 	    "max_retired_pages");
201 
202 	gmem.gm_ce_n = fmd_prop_get_int32(hdl, "ce_n");
203 	gmem.gm_ce_t = fmd_prop_get_int64(hdl, "ce_t");
204 	gmem.gm_filter_ratio = fmd_prop_get_int32(hdl, "filter_ratio");
205 	gmem.gm_low_ce_thresh = fmd_prop_get_int32(hdl, "low_ce_thresh");
206 	gmem.gm_nupos = fmd_prop_get_int32(hdl, "nupos");
207 	gmem.gm_dupce = fmd_prop_get_int32(hdl, "dupce");
208 
209 
210 	if (gmem_state_restore(hdl) < 0) {
211 		_fmd_fini(hdl);
212 		fmd_hdl_abort(hdl, "failed to restore saved state\n");
213 	}
214 }
215