xref: /titanic_44/usr/src/lib/libprtdiag/common/kstat.c (revision 8d4e547db823a866b8f73efc0acdc423e2963caf)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <kvm.h>
35 #include <varargs.h>
36 #include <errno.h>
37 #include <time.h>
38 #include <dirent.h>
39 #include <fcntl.h>
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <sys/utsname.h>
44 #include <sys/openpromio.h>
45 #include <kstat.h>
46 #include <libintl.h>
47 #include <syslog.h>
48 #include <sys/dkio.h>
49 #include "pdevinfo.h"
50 #include "display.h"
51 #include "pdevinfo_sun4u.h"
52 #include "display_sun4u.h"
53 #include "libprtdiag.h"
54 
55 /*
56  * This module does the reading and interpreting of sun4u system
57  * kstats. These kstats are created by the following drivers:
58  * fhc, environ, sysctrl. Each board in the tree should have
59  * kstats created for it.  There are also system wide kstats that
60  * are created.
61  */
62 void
63 read_platform_kstats(Sys_tree *tree, struct system_kstat_data *sys_kstat,
64 	struct bd_kstat_data *bdp, struct envctrl_kstat_data *ep)
65 {
66 	Board_node	*bnode;
67 	kstat_ctl_t	*kc;
68 	kstat_t		*ksp;
69 	kstat_named_t	*knp;
70 	int		i;
71 	struct hp_info	*hp;
72 
73 #ifdef lint
74 	ep = ep;
75 #endif
76 	if ((kc = kstat_open()) == NULL) {
77 		return;
78 	}
79 
80 	/* For each board in the system, read the kstats for it. */
81 	for (bnode = tree->bd_list; bnode != NULL; bnode = bnode->next) {
82 		int board;
83 
84 		/*
85 		 * Kstat instances numbers are set by fhc, ac, simmstat,
86 		 * and environ drivers based on their board# property.
87 		 */
88 		board = bnode->board_num;
89 		bdp = &sys_kstat->bd_ksp_list[board];
90 
91 		/* Try to find an FHC instance for this board number */
92 		ksp = kstat_lookup(kc, UNIX, board, FHC_KSTAT_NAME);
93 
94 		/* Atempt to read the FHC kstat */
95 		if ((ksp != NULL) && (kstat_read(kc, ksp, NULL) == -1)) {
96 			ksp = NULL;
97 		}
98 
99 		/* Now read out the data if the kstat read OK */
100 		if (ksp != NULL) {
101 			/*
102 			 * We set the kstats_ok flag to good here. If we
103 			 * fail one of the data reads, we set it to bad.
104 			 */
105 			bdp->fhc_kstats_ok = 1;
106 
107 			/*
108 			 * For each data value, If the Kstat named struct
109 			 * is found, then get the data out.
110 			 */
111 			knp = kstat_data_lookup(ksp, CSR_KSTAT_NAMED);
112 			if (knp != NULL) {
113 				bdp->fhc_csr = knp->value.ul;
114 			} else {
115 				bdp->fhc_kstats_ok = 0;
116 			}
117 			knp = kstat_data_lookup(ksp, BSR_KSTAT_NAMED);
118 			if (knp != NULL) {
119 				bdp->fhc_bsr = knp->value.ul;
120 			} else {
121 				bdp->fhc_kstats_ok = 0;
122 			}
123 		}
124 
125 		/* Try to find an AC instance for this board number */
126 		ksp = kstat_lookup(kc, UNIX, board, AC_KSTAT_NAME);
127 
128 		/* Attempt to read the AC kstat. */
129 		if ((ksp != NULL) && (kstat_read(kc, ksp, NULL) == -1)) {
130 			ksp = NULL;
131 		}
132 
133 		/* If the AC kstat exists, try to read the data from it. */
134 		if (ksp != NULL) {
135 			/*
136 			 * We set the kstats_ok flag to good here. If we
137 			 * fail one of the data reads, we set it to bad.
138 			 */
139 			bdp->ac_kstats_ok = 1;
140 			bdp->ac_memstat_ok = 1;
141 
142 			/*
143 			 * For each data value, If the Kstat named struct
144 			 * is found, then get the data out.
145 			 */
146 
147 			knp = kstat_data_lookup(ksp, MEMCTL_KSTAT_NAMED);
148 			if (knp != NULL) {
149 				bdp->ac_memctl = knp->value.ull;
150 			} else {
151 				bdp->ac_kstats_ok = 0;
152 			}
153 
154 			knp = kstat_data_lookup(ksp, MEMDECODE0_KSTAT_NAMED);
155 			if (knp != NULL) {
156 				bdp->ac_memdecode[0] = knp->value.ull;
157 			} else {
158 				bdp->ac_kstats_ok = 0;
159 			}
160 
161 			knp = kstat_data_lookup(ksp, MEMDECODE1_KSTAT_NAMED);
162 			if (knp != NULL) {
163 				bdp->ac_memdecode[1] = knp->value.ull;
164 			} else {
165 				bdp->ac_kstats_ok = 0;
166 			}
167 
168 			knp = kstat_data_lookup(ksp, BANK_0_KSTAT_NAMED);
169 			if (knp != NULL) {
170 				bdp->mem_stat[0].status = knp->value.c[0];
171 				bdp->mem_stat[0].condition = knp->value.c[1];
172 			} else {
173 				bdp->ac_memstat_ok = 0;
174 			}
175 
176 			knp = kstat_data_lookup(ksp, BANK_1_KSTAT_NAMED);
177 			if (knp != NULL) {
178 				bdp->mem_stat[1].status = knp->value.c[0];
179 				bdp->mem_stat[1].condition = knp->value.c[1];
180 			} else {
181 				bdp->ac_memstat_ok = 0;
182 			}
183 
184 		}
185 
186 		/* Try to find an simmstat instance for this board number */
187 		ksp = kstat_lookup(kc, UNIX, board, SIMMSTAT_KSTAT_NAME);
188 
189 		if (ksp != NULL) {
190 			if (kstat_read(kc, ksp, NULL) == -1) {
191 				bdp->simmstat_kstats_ok = 0;
192 			} else {
193 				bdp->simmstat_kstats_ok = 1;
194 				(void) memcpy(&bdp->simm_status, ksp->ks_data,
195 					sizeof (bdp->simm_status));
196 			}
197 		}
198 
199 		/* Try to find an overtemp kstat instance for this board */
200 		ksp = kstat_lookup(kc, UNIX, board, OVERTEMP_KSTAT_NAME);
201 
202 		if (ksp != NULL) {
203 			if (kstat_read(kc, ksp, NULL) == -1) {
204 				bdp->temp_kstat_ok = 0;
205 			} else {
206 				bdp->temp_kstat_ok = 1;
207 				(void) memcpy(&bdp->tempstat, ksp->ks_data,
208 					sizeof (bdp->tempstat));
209 				/* XXX - this is for 2.5.1 testing.  remove */
210 				if (sizeof (bdp->tempstat) > ksp->ks_data_size)
211 					bdp->tempstat.trend = TREND_UNKNOWN;
212 			}
213 		}
214 	}
215 
216 	/* Read the kstats for the system control board */
217 	ksp = kstat_lookup(kc, UNIX, 0, SYSCTRL_KSTAT_NAME);
218 
219 	if ((ksp != NULL) && (kstat_read(kc, ksp, NULL) == -1)) {
220 		sys_kstat->sys_kstats_ok = 0;
221 		ksp = NULL;
222 	}
223 
224 	if (ksp != NULL) {
225 		sys_kstat->sys_kstats_ok = 1;
226 
227 		knp = kstat_data_lookup(ksp, CSR_KSTAT_NAMED);
228 		if (knp != NULL) {
229 			sys_kstat->sysctrl = knp->value.c[0];
230 		} else {
231 			sys_kstat->sys_kstats_ok = 0;
232 		}
233 
234 		knp = kstat_data_lookup(ksp, STAT1_KSTAT_NAMED);
235 		if (knp != NULL) {
236 			sys_kstat->sysstat1 = knp->value.c[0];
237 		} else {
238 			sys_kstat->sys_kstats_ok = 0;
239 		}
240 
241 		knp = kstat_data_lookup(ksp, STAT2_KSTAT_NAMED);
242 		if (knp != NULL) {
243 			sys_kstat->sysstat2 = knp->value.c[0];
244 		} else {
245 			sys_kstat->sys_kstats_ok = 0;
246 		}
247 
248 		knp = kstat_data_lookup(ksp, CLK_FREQ2_KSTAT_NAMED);
249 		if (knp != NULL) {
250 			sys_kstat->clk_freq2 = knp->value.c[0];
251 		} else {
252 			sys_kstat->sys_kstats_ok = 0;
253 		}
254 
255 		knp = kstat_data_lookup(ksp, FAN_KSTAT_NAMED);
256 		if (knp != NULL) {
257 			sys_kstat->fan_status = knp->value.c[0];
258 		} else {
259 			sys_kstat->sys_kstats_ok = 0;
260 		}
261 
262 		knp = kstat_data_lookup(ksp, KEY_KSTAT_NAMED);
263 		if (knp != NULL) {
264 			sys_kstat->keysw_status = knp->value.c[0];
265 		} else {
266 			sys_kstat->sys_kstats_ok = 0;
267 		}
268 
269 		knp = kstat_data_lookup(ksp, POWER_KSTAT_NAMED);
270 		if (knp != NULL) {
271 			sys_kstat->power_state =
272 				(enum power_state)knp->value.l;
273 		} else {
274 			sys_kstat->sys_kstats_ok = 0;
275 		}
276 
277 		knp = kstat_data_lookup(ksp, CLK_VER_KSTAT_NAME);
278 		if (knp != NULL) {
279 			sys_kstat->clk_ver = knp->value.c[0];
280 		} else {
281 			/*
282 			 * the clock version register only appears
283 			 * on new clock boards
284 			 */
285 			sys_kstat->clk_ver = 0;
286 		}
287 
288 	}
289 
290 	/* Read the kstats for the power supply stats */
291 	ksp = kstat_lookup(kc, UNIX, 0, PSSHAD_KSTAT_NAME);
292 
293 	if ((ksp != NULL) && (kstat_read(kc, ksp, NULL) != -1)) {
294 		sys_kstat->psstat_kstat_ok = 1;
295 		(void) memcpy(&sys_kstat->ps_shadow[0], ksp->ks_data,
296 			sizeof (sys_kstat->ps_shadow));
297 	} else {
298 		sys_kstat->psstat_kstat_ok = 0;
299 	}
300 
301 	/* read the overtemp kstat for the system control board */
302 	/* Try to find an overtemp kstat instance for this board */
303 	ksp = kstat_lookup(kc, UNIX, CLOCK_BOARD_INDEX, OVERTEMP_KSTAT_NAME);
304 
305 	if (ksp != NULL) {
306 		if (kstat_read(kc, ksp, NULL) == -1) {
307 			sys_kstat->temp_kstat_ok = 0;
308 		} else {
309 			sys_kstat->temp_kstat_ok = 1;
310 			(void) memcpy(&sys_kstat->tempstat, ksp->ks_data,
311 				sizeof (sys_kstat->tempstat));
312 			/* XXX - this is for 2.5.1 testing.  remove */
313 			if (sizeof (sys_kstat->tempstat) > ksp->ks_data_size)
314 				sys_kstat->tempstat.trend = TREND_UNKNOWN;
315 		}
316 	}
317 
318 	/* Read the reset-info kstat from one of the boards. */
319 	ksp = kstat_lookup(kc, UNIX, 0, RESETINFO_KSTAT_NAME);
320 
321 	if (ksp == NULL) {
322 		sys_kstat->reset_kstats_ok = 0;
323 	} else if (kstat_read(kc, ksp, NULL) == -1) {
324 		sys_kstat->reset_kstats_ok = 0;
325 	} else {
326 		sys_kstat->reset_kstats_ok = 1;
327 		(void) memcpy(&sys_kstat->reset_info, ksp->ks_data,
328 			sizeof (sys_kstat->reset_info));
329 	}
330 
331 	/* read kstats for hotplugged boards */
332 	for (i = 0, hp = &sys_kstat->hp_info[0]; i < MAX_BOARDS; i++, hp++) {
333 		ksp = kstat_lookup(kc, UNIX, i, BDLIST_KSTAT_NAME);
334 
335 		if (ksp == NULL) {
336 			continue;
337 		}
338 
339 		if (kstat_read(kc, ksp, NULL) == -1) {
340 			hp->kstat_ok = 0;
341 		} else {
342 			hp->kstat_ok = 1;
343 			(void) memcpy(&hp->bd_info, ksp->ks_data,
344 				sizeof (hp->bd_info));
345 		}
346 	}
347 
348 	/* read in the kstat for the fault list. */
349 	ksp = kstat_lookup(kc, UNIX, 0, FT_LIST_KSTAT_NAME);
350 
351 	if (ksp == NULL) {
352 		sys_kstat->ft_kstat_ok = 0;
353 	} else {
354 		if (kstat_read(kc, ksp, NULL) == -1) {
355 			perror("kstat read");
356 			sys_kstat->ft_kstat_ok = 0;
357 			return;
358 		}
359 
360 		sys_kstat->nfaults = ksp->ks_data_size /
361 			sizeof (struct ft_list);
362 
363 		sys_kstat->ft_array =
364 			(struct ft_list *)malloc(ksp->ks_data_size);
365 
366 		if (sys_kstat->ft_array == NULL) {
367 			perror("Malloc");
368 			exit(2);
369 		}
370 		sys_kstat->ft_kstat_ok = 1;
371 		(void) memcpy(sys_kstat->ft_array, ksp->ks_data,
372 			ksp->ks_data_size);
373 	}
374 }
375 
376 /*
377  * This function does the reading and interpreting of sun4u system
378  * kstats. These kstats are created by the following drivers:
379  * fhc, environ, sysctrl. Each board in the tree should have
380  * kstats created for it.  There are also system wide kstats that
381  * are created.
382  */
383 void
384 read_sun4u_kstats(Sys_tree *tree, struct system_kstat_data *sys_kstat)
385 {
386 #if 0
387 	Board_node 	*bnode;
388 	kstat_t		*ksp;
389 	kstat_named_t	*knp;
390 	struct hp_info *hp;
391 	struct envctrltwo_kstat_data *ecp;
392 #endif
393 	kstat_ctl_t 	*kc;
394 	int		i;
395 	struct bd_kstat_data *bdp;
396 	struct envctrl_kstat_data *ep;
397 
398 	if ((kc = kstat_open()) == NULL) {
399 		return;
400 	}
401 #ifdef lint
402 	kc = kc;
403 #endif
404 
405 	/* Initialize the kstats structure */
406 	sys_kstat->sys_kstats_ok = 0;
407 	sys_kstat->temp_kstat_ok = 0;
408 	sys_kstat->reset_kstats_ok = 0;
409 	sys_kstat->ft_kstat_ok = 0;
410 	sys_kstat->envctrl_kstat_ok = 0;
411 	for (i = 0; i < MAX_BOARDS; i++) {
412 		bdp = &sys_kstat->bd_ksp_list[i];
413 		bdp->ac_kstats_ok = 0;
414 		bdp->fhc_kstats_ok = 0;
415 		bdp->simmstat_kstats_ok = 0;
416 		bdp->temp_kstat_ok = 0;
417 
418 		sys_kstat->hp_info[i].kstat_ok = 0;
419 	}
420 	for (i = 0; i < MAX_DEVS; i++) {
421 		ep = &sys_kstat->env_data;
422 		ep->ps_kstats[i].instance = I2C_NODEV;
423 		ep->fan_kstats[i].instance = I2C_NODEV;
424 		ep->encl_kstats[i].instance = I2C_NODEV;
425 	}
426 
427 	read_platform_kstats(tree, sys_kstat, bdp, ep);
428 }
429