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