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