dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / picl / plugins / sun4u / enchilada / envd / piclenvd.c
bloba780451ec14c6963d442f3a876295f16b73ec06a
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This file contains the environmental PICL plug-in module.
34 * This plugin sets up the PICLTREE for Enchilada WS.
35 * It provides functionality to get/set temperatures and
36 * fan speeds.
38 * The environmental policy defaults to the auto mode
39 * as programmed by OBP at boot time.
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <sys/sysmacros.h>
45 #include <limits.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <stdarg.h>
49 #include <alloca.h>
50 #include <unistd.h>
51 #include <sys/processor.h>
52 #include <syslog.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <picl.h>
56 #include <picltree.h>
57 #include <picldefs.h>
58 #include <pthread.h>
59 #include <signal.h>
60 #include <libdevinfo.h>
61 #include <sys/pm.h>
62 #include <sys/open.h>
63 #include <sys/time.h>
64 #include <sys/utsname.h>
65 #include <sys/systeminfo.h>
66 #include <note.h>
67 #include <sys/i2c/clients/i2c_client.h>
68 #include <sys/i2c/clients/adm1031.h>
69 #include <sys/i2c/clients/pic16f819_reg.h>
70 #include "envd.h"
71 #include <sys/scsi/scsi.h>
72 #include <sys/scsi/generic/commands.h>
76 * PICL plugin entry points
78 static void piclenvd_register(void);
79 static void piclenvd_init(void);
80 static void piclenvd_fini(void);
83 * Env setup routines
85 extern void env_picl_setup(void);
86 extern void env_picl_destroy(void);
87 extern int env_picl_setup_tuneables(void);
90 * Sleep routine used for polling
92 static int get_dimm_fan_speed(int, fanspeed_t *);
93 static int is_dimm_fan_failed(void);
95 #pragma init(piclenvd_register)
98 * Plugin registration information
100 static picld_plugin_reg_t my_reg_info = {
101 PICLD_PLUGIN_VERSION,
102 PICLD_PLUGIN_CRITICAL,
103 "SUNW_piclenvd",
104 piclenvd_init,
105 piclenvd_fini,
108 #define REGISTER_INFORMATION_STRING_LENGTH 16
109 static char dimm_fan_rpm_string[REGISTER_INFORMATION_STRING_LENGTH] = {0};
110 static char dimm_fan_status_string[REGISTER_INFORMATION_STRING_LENGTH] = {0};
111 static char dimm_fan_command_string[REGISTER_INFORMATION_STRING_LENGTH] = {0};
112 static char dimm_fan_debug_string[REGISTER_INFORMATION_STRING_LENGTH] = {0};
114 static int scsi_log_sense(int fd, uchar_t page_code, uchar_t *pagebuf,
115 uint16_t pagelen);
116 static int get_disk_temp(env_disk_t *);
118 * ES Segment data structures
120 static sensor_ctrl_blk_t sensor_ctrl[MAX_SENSORS];
121 static fan_ctrl_blk_t fan_ctrl[MAX_FANS];
122 static fruenvseg_t *envfru = NULL;
125 * Env thread variables
127 static boolean_t system_shutdown_started = B_FALSE;
128 static boolean_t ovtemp_thr1_created = B_FALSE;
129 static pthread_t ovtemp_thr1_id;
130 static pthread_attr_t thr_attr;
131 static boolean_t ovtemp_thr2_created = B_FALSE;
132 static pthread_t ovtemp_thr2_id;
133 static boolean_t dimm_fan_thr_created = B_FALSE;
134 static pthread_t dimm_fan_thr_id;
135 static boolean_t disk_temp_thr_created = B_FALSE;
136 static pthread_t disk_temp_thr_id;
139 * PM thread related variables
141 static pthread_t pmthr_tid; /* pmthr thread ID */
142 static int pm_fd = -1; /* PM device file descriptor */
143 static boolean_t pmthr_created = B_FALSE;
144 static int cur_lpstate; /* cur low power state */
147 * Envd plug-in verbose flag set by SUNW_PICLENVD_DEBUG environment var
148 * Setting the verbose tuneable also enables debugging for better
149 * control
151 int env_debug = 0;
154 * Fan devices
156 static env_fan_t envd_sys_out_fan = {
157 ENV_SYSTEM_OUT_FAN, ENV_SYSTEM_OUT_FAN_DEVFS, NULL,
158 SYSTEM_OUT_FAN_ID, SYSTEM_OUT_FAN_SPEED_MIN, SYSTEM_OUT_FAN_SPEED_MAX,
159 -1, -1,
162 static env_fan_t envd_sys_in_fan = {
163 ENV_SYSTEM_INTAKE_FAN, ENV_SYSTEM_INTAKE_FAN_DEVFS, NULL,
164 SYSTEM_INTAKE_FAN_ID, SYSTEM_INTAKE_FAN_SPEED_MIN,
165 SYSTEM_INTAKE_FAN_SPEED_MAX, -1, -1,
168 static env_fan_t envd_cpu0_fan = {
169 ENV_CPU0_FAN, ENV_CPU0_FAN_DEVFS, NULL,
170 CPU0_FAN_ID, CPU_FAN_SPEED_MIN, CPU_FAN_SPEED_MAX, -1, -1,
173 static env_fan_t envd_cpu1_fan = {
174 ENV_CPU1_FAN, ENV_CPU1_FAN_DEVFS, NULL,
175 CPU1_FAN_ID, CPU_FAN_SPEED_MIN, CPU_FAN_SPEED_MAX, -1, -1,
178 static env_fan_t envd_dimm_fan = {
179 ENV_DIMM_FAN, ENV_DIMM_FAN_DEVFS, NULL,
180 DIMM_FAN_ID, 100, 100, -1, -1,
183 static env_disk_t envd_disk0 = {
184 ENV_DISK0, ENV_DISK0_DEVFS, DISK0_PHYSPATH, DISK0_NODE_PATH,
185 DISK0_ID, -1, -1,
188 static env_disk_t envd_disk1 = {
189 ENV_DISK1, ENV_DISK1_DEVFS, DISK1_PHYSPATH, DISK1_NODE_PATH,
190 DISK1_ID, -1, -1,
194 * The vendor-id and device-id are the properties associated with
195 * the SCSI controller. This is used to identify a particular controller
196 * like LSI1030.
198 #define VENDOR_ID "vendor-id"
199 #define DEVICE_ID "device-id"
202 * The implementation for SCSI disk drives to supply info. about
203 * temperature is not mandatory. Hence we first determine if the
204 * temperature page is supported. To do this we need to scan the list
205 * of pages supported.
207 #define SUPPORTED_LPAGES 0
208 #define TEMPERATURE_PAGE 0x0D
209 #define LOGPAGEHDRSIZE 4
212 * NULL terminated array of fans
214 static env_fan_t *envd_fans[] = {
215 &envd_cpu0_fan,
216 &envd_cpu1_fan,
217 &envd_sys_out_fan,
218 &envd_sys_in_fan,
219 &envd_dimm_fan,
220 NULL
223 static env_disk_t *envd_disks[] = {
224 &envd_disk0,
225 &envd_disk1,
226 NULL
230 * ADM1031 speedrange map is indexed by a 2-bit value
232 static int adm_speedrange_map[] = {1, 2, 4, 8};
235 * ADM1031 devices
237 static char *hwm_devs[] = {
238 CPU_HWM_DEVFS, /* CPU_HWM_ID */
239 SYS_HWM_DEVFS /* SYS_HWM_ID */
243 * Fan names associated with each ADM1031 hwms - used to
244 * print fault messages.
246 static char *hwm_fans[MAX_HWMS][2] = {
247 {ENV_CPU0_FAN, ENV_CPU1_FAN},
248 {ENV_SYSTEM_INTAKE_FAN, ENV_SYSTEM_OUT_FAN}
252 * Temperature sensors
254 static env_sensor_t envd_sensors[] = {
255 { SENSOR_CPU0_DIE, SENSOR_CPU0_DIE_DEVFS, NULL,
256 CPU0_SENSOR_ID, CPU_HWM_ID, (void *)&envd_cpu0_fan, -1},
257 { SENSOR_CPU1_DIE, SENSOR_CPU1_DIE_DEVFS, NULL,
258 CPU1_SENSOR_ID, CPU_HWM_ID, (void *)&envd_cpu1_fan, -1},
259 { SENSOR_INT_AMB_0, SENSOR_INT_AMB_0_DEVFS, NULL,
260 INT_AMB0_SENSOR_ID, CPU_HWM_ID, NULL, -1},
261 { SENSOR_SYS_OUT, SENSOR_SYS_OUT_DEVFS, NULL,
262 SYS_OUT_SENSOR_ID, SYS_HWM_ID, (void *)&envd_sys_out_fan, -1},
263 { SENSOR_INT_AMB_1, SENSOR_INT_AMB_1_DEVFS, NULL,
264 INT_AMB1_SENSOR_ID, SYS_HWM_ID, NULL, -1},
265 { SENSOR_SYS_IN, SENSOR_SYS_IN_DEVFS, NULL,
266 SYS_IN_SENSOR_ID, SYS_HWM_ID, (void *)&envd_sys_in_fan, -1},
268 #define N_ENVD_SENSORS (sizeof (envd_sensors)/sizeof (envd_sensors[0]))
270 #define NOT_AVAILABLE "NA"
273 * ADM1031 macros
275 #define TACH_UNKNOWN 255
276 #define FAN_OUT_OF_RANGE (TACH_UNKNOWN)
277 #define ADM_HYSTERISIS 5
278 #define N_SEQ_TACH 15
280 #define TMIN_MASK (0xF8)
281 #define TMIN_SHIFT (3)
282 #define TMIN_UNITS (4) /* increments of 4 degrees celsius */
283 #define TRANGE_MASK (0x7)
285 #define TMIN(regval) (((regval & TMIN_MASK) >> TMIN_SHIFT) * TMIN_UNITS)
286 #define TRANGE(regval) (regval & TRANGE_MASK)
288 #define GET_TMIN_RANGE(tmin, trange) \
289 ((((tmin / TMIN_UNITS) & TMIN_MASK) << TMIN_SHIFT) | \
290 (trange & TRANGE_MASK))
292 #define TACH_ENABLE_MASK (0x0C)
293 #define ADM_SETFANSPEED_CONV(speed) (15 * speed / 100)
296 * Tuneables
298 #define ENABLE 1
299 #define DISABLE 0
301 int monitor_disk_temp = 1; /* enabled */
302 static int disk_high_warn_temperature = DISK_HIGH_WARN_TEMPERATURE;
303 static int disk_low_warn_temperature = DISK_LOW_WARN_TEMPERATURE;
304 static int disk_high_shutdown_temperature =
305 DISK_HIGH_SHUTDOWN_TEMPERATURE;
306 static int disk_low_shutdown_temperature = DISK_LOW_SHUTDOWN_TEMPERATURE;
307 static int disk_scan_interval = DISK_SCAN_INTERVAL;
309 static int get_monitor_cpu_mode(ptree_rarg_t *parg, void *buf);
310 static int set_monitor_cpu_mode(ptree_warg_t *parg, const void *buf);
311 static int get_monitor_sys_mode(ptree_rarg_t *parg, void *buf);
312 static int set_monitor_sys_mode(ptree_warg_t *parg, const void *buf);
313 static int get_int_val(ptree_rarg_t *parg, void *buf);
314 static int set_int_val(ptree_warg_t *parg, const void *buf);
315 static int get_string_val(ptree_rarg_t *parg, void *buf);
316 static int set_string_val(ptree_warg_t *parg, const void *buf);
317 static int get_cpu_tach(ptree_rarg_t *parg, void *buf);
318 static int set_cpu_tach(ptree_warg_t *parg, const void *buf);
319 static int get_sys_tach(ptree_rarg_t *parg, void *buf);
320 static int set_sys_tach(ptree_warg_t *parg, const void *buf);
322 static int shutdown_override = 0;
323 static int sensor_poll_interval = SENSORPOLL_INTERVAL;
324 static int warning_interval = WARNING_INTERVAL;
325 static int disk_warning_interval = DISK_WARNING_INTERVAL;
326 static int disk_warning_duration = DISK_WARNING_DURATION;
327 static int shutdown_interval = SHUTDOWN_INTERVAL;
328 static int disk_shutdown_interval = DISK_SHUTDOWN_INTERVAL;
329 static int ovtemp_monitor = 1; /* enabled */
330 static int pm_monitor = 1; /* enabled */
331 static int mon_fanstat = 1; /* enabled */
333 static int cpu_mode;
334 static int sys_mode;
335 static int cpu_tach;
336 static int sys_tach;
337 static char shutdown_cmd[] = SHUTDOWN_CMD;
339 env_tuneable_t tuneables[] = {
340 {"ovtemp-monitor", PICL_PTYPE_INT, &ovtemp_monitor,
341 &get_int_val, &set_int_val, sizeof (int)},
343 {"pm-monitor", PICL_PTYPE_INT, &pm_monitor,
344 &get_int_val, &set_int_val, sizeof (int)},
346 {"shutdown-override", PICL_PTYPE_INT, &shutdown_override,
347 &get_int_val, &set_int_val, sizeof (int)},
349 {"cpu-hm-automode-enable", PICL_PTYPE_INT, &cpu_mode,
350 &get_monitor_cpu_mode, &set_monitor_cpu_mode,
351 sizeof (int)},
353 {"sys-hm-automode-enable", PICL_PTYPE_INT, &sys_mode,
354 &get_monitor_sys_mode, &set_monitor_sys_mode,
355 sizeof (int)},
357 {"sensor-poll-interval", PICL_PTYPE_INT,
358 &sensor_poll_interval,
359 &get_int_val, &set_int_val,
360 sizeof (int)},
362 {"disk-scan-interval", PICL_PTYPE_INT,
363 &disk_scan_interval,
364 &get_int_val, &set_int_val,
365 sizeof (int)},
367 {"warning-interval", PICL_PTYPE_INT, &warning_interval,
368 &get_int_val, &set_int_val,
369 sizeof (int)},
371 {"shutdown-interval", PICL_PTYPE_INT, &shutdown_interval,
372 &get_int_val, &set_int_val,
373 sizeof (int)},
375 {"disk_warning-interval", PICL_PTYPE_INT, &disk_warning_interval,
376 &get_int_val, &set_int_val,
377 sizeof (int)},
379 {"disk_warning-duration", PICL_PTYPE_INT, &disk_warning_duration,
380 &get_int_val, &set_int_val,
381 sizeof (int)},
383 {"disk_shutdown-interval", PICL_PTYPE_INT, &disk_shutdown_interval,
384 &get_int_val, &set_int_val,
385 sizeof (int)},
387 {"shutdown-command", PICL_PTYPE_CHARSTRING, shutdown_cmd,
388 &get_string_val, &set_string_val,
389 sizeof (shutdown_cmd)},
391 {"cpu-tach-enable", PICL_PTYPE_INT, &cpu_tach,
392 &get_cpu_tach, &set_cpu_tach,
393 sizeof (int)},
395 {"sys-tach-enable", PICL_PTYPE_INT, &sys_tach,
396 &get_sys_tach, &set_sys_tach,
397 sizeof (int)},
399 {"monitor-fanstat", PICL_PTYPE_INT, &mon_fanstat,
400 &get_int_val, &set_int_val, sizeof (int)},
402 {"monitor-disk-temp", PICL_PTYPE_INT, &monitor_disk_temp,
403 &get_int_val, &set_int_val, sizeof (int)},
405 {"disk-high-warn-temperature", PICL_PTYPE_INT,
406 &disk_high_warn_temperature, &get_int_val,
407 &set_int_val, sizeof (int)},
409 {"disk-low-warn-temperature", PICL_PTYPE_INT,
410 &disk_low_warn_temperature, &get_int_val,
411 &set_int_val, sizeof (int)},
413 {"disk-high-shutdown-temperature", PICL_PTYPE_INT,
414 &disk_high_shutdown_temperature, &get_int_val,
415 &set_int_val, sizeof (int)},
417 {"disk-low-shutdown-temperature", PICL_PTYPE_INT,
418 &disk_low_shutdown_temperature, &get_int_val,
419 &set_int_val, sizeof (int)},
421 {"verbose", PICL_PTYPE_INT, &env_debug,
422 &get_int_val, &set_int_val, sizeof (int)},
428 * We use this to figure out how many tuneables there are
429 * This is variable because the publishing routine needs this info
430 * in piclenvsetup.c
432 int ntuneables = (sizeof (tuneables)/sizeof (tuneables[0]));
435 * Table Handling Code
437 static void
438 fini_table(table_t *tblp)
440 if (tblp == NULL)
441 return;
442 free(tblp->xymap);
443 free(tblp);
446 static table_t *
447 init_table(int npoints)
449 table_t *tblp;
450 point_t *xy;
452 if (npoints == 0)
453 return (NULL);
455 if ((tblp = malloc(sizeof (*tblp))) == NULL)
456 return (NULL);
458 if ((xy = malloc(sizeof (*xy) * npoints)) == NULL) {
459 free(tblp);
460 return (NULL);
463 tblp->nentries = npoints;
464 tblp->xymap = xy;
466 return (tblp);
470 * function: calculates y for a given x based on a table of points
471 * for monotonically increasing x values.
472 * 'tbl' specifies the table to use, 'val' specifies the 'x', returns 'y'
474 static int
475 y_of_x(table_t *tbl, int xval)
477 int i;
478 int entries;
479 point_t *xymap;
480 float newval;
481 float dy, dx, slope;
483 entries = tbl->nentries;
484 xymap = tbl->xymap;
486 * If the temperature is outside the correction table
487 * then simply return the original value.
489 if ((xval < xymap[0].x) || (xval > xymap[entries - 1].x))
490 return (xval);
491 if (xval == xymap[0].x)
492 return (xymap[0].y);
493 if (xval == xymap[entries - 1].x)
494 return (xymap[entries - 1].y);
496 for (i = 1; i < entries - 1; i++) {
497 if (xval == xymap[i].x)
498 return (xymap[i].y);
499 if (xval < xymap[i].x)
500 break;
504 * Use linear interpolation
506 dy = (float)(xymap[i].y - xymap[i-1].y);
507 dx = (float)(xymap[i].x - xymap[i-1].x);
508 slope = dy/dx;
509 newval = xymap[i - 1].y + slope * (xval - xymap[i - 1].x);
510 return ((int)(newval + (newval >= 0 ? 0.5 : -0.5)));
514 * Get environmental segment from the specified FRU SEEPROM
516 static int
517 get_envseg(int fd, void **envsegp, int *envseglenp)
519 int i, segcnt, envseglen;
520 section_layout_t section;
521 segment_layout_t segment;
522 uint8_t *envseg;
524 if (lseek(fd, (long)SECTION_HDR_OFFSET, 0) == -1L ||
525 read(fd, &section, sizeof (section)) != sizeof (section)) {
526 return (EINVAL);
530 * Verify we have the correct section and contents are valid
531 * For now, we don't verify the CRC.
533 if (section.header_tag != SECTION_HDR_TAG ||
534 GET_UNALIGN16(&section.header_version[0]) != SECTION_HDR_VER) {
535 if (env_debug)
536 envd_log(LOG_INFO,
537 "Invalid section header tag:%x version:%x\n",
538 section.header_tag,
539 GET_UNALIGN16(&section.header_version));
540 return (EINVAL);
544 * Locate our environmental segment
546 segcnt = section.segment_count;
547 for (i = 0; i < segcnt; i++) {
548 if (read(fd, &segment, sizeof (segment)) != sizeof (segment)) {
549 return (EINVAL);
551 if (env_debug)
552 envd_log(LOG_INFO,
553 "Seg name: %x desc:%x off:%x len:%x\n",
554 GET_UNALIGN16(&segment.name),
555 GET_UNALIGN32(&segment.descriptor[0]),
556 GET_UNALIGN16(&segment.offset),
557 GET_UNALIGN16(&segment.length));
558 if (GET_UNALIGN16(&segment.name) == ENVSEG_NAME)
559 break;
562 if (i >= segcnt) {
563 return (ENOENT);
567 * Allocate memory to hold the environmental segment data.
569 envseglen = GET_UNALIGN16(&segment.length);
570 if ((envseg = malloc(envseglen)) == NULL) {
571 return (ENOMEM);
574 if (lseek(fd, (long)GET_UNALIGN16(&segment.offset), 0) == -1L ||
575 read(fd, envseg, envseglen) != envseglen) {
576 (void) free(envseg);
577 return (EIO);
579 *envsegp = envseg;
580 *envseglenp = envseglen;
581 return (0);
585 * Get all environmental segments
586 * Return NULL on error
588 static fruenvseg_t *
589 get_fru_envsegs(void)
591 fruenvseg_t *fruenvsegs;
592 envseg_layout_t *envsegp;
593 void *envsegbufp;
594 int fd, envseglen, hdrlen;
595 char path[PATH_MAX];
597 fruenvsegs = NULL;
598 fruenvsegs = malloc(sizeof (*fruenvsegs));
599 if (fruenvsegs == NULL) {
600 return (NULL);
604 * Now get the environmental segment from this FRU
606 (void) snprintf(path, sizeof (path), "%s%s", I2C_DEVFS, MBFRU_DEV);
607 fd = open(path, O_RDONLY);
608 if (fd == -1) {
609 envd_log(LOG_ERR, ENV_FRU_OPEN_FAIL, errno, path);
610 free(fruenvsegs);
611 return (NULL);
615 * Read environmental segment from this FRU SEEPROM
617 if (get_envseg(fd, &envsegbufp, &envseglen) != 0) {
618 envd_log(LOG_ERR, ENV_FRU_BAD_ENVSEG, path);
619 free(fruenvsegs);
620 (void) close(fd);
621 return (NULL);
625 * Validate envseg version number and header length
627 envsegp = (envseg_layout_t *)envsegbufp;
628 hdrlen = sizeof (envseg_layout_t) -
629 sizeof (envseg_sensor_t) +
630 (envsegp->sensor_count) * sizeof (envseg_sensor_t);
632 if (envsegp->version != ENVSEG_VERSION ||
633 envseglen < hdrlen) {
635 * version mismatch or header not big enough
637 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG, FRU_SEEPROM_NAME);
638 if (envsegbufp != NULL)
639 (void) free(envsegbufp);
640 free(fruenvsegs);
641 (void) close(fd);
642 return (NULL);
645 fruenvsegs->envseglen = envseglen;
646 fruenvsegs->envsegbufp = envsegbufp;
647 (void) close(fd);
648 return (fruenvsegs);
651 static int
652 process_fru_seeprom(unsigned char *buff)
654 id_off_t id;
655 int i;
656 int id_offset = 0;
657 int nsensors;
658 int nfans;
659 env_fan_t *fnodep;
660 env_sensor_t *snodep;
662 #define NSENSOR_OFFSET 1
663 #define ID_OFF_SIZE 6
664 #define NFANS_OFFSET(x) ((x * ID_OFF_SIZE) + 2)
666 nsensors = (int)buff[NSENSOR_OFFSET];
667 if (nsensors != MAX_SENSORS) {
668 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG, FRU_SEEPROM_NAME);
669 return (-1);
672 nfans = (int)buff[NFANS_OFFSET(nsensors)];
673 if (nfans != MAX_FANS) {
674 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG, FRU_SEEPROM_NAME);
675 return (-1);
678 while (nsensors > 0) {
679 (void) memcpy((char *)&id,
680 (char *)&buff[id_offset + 2],
681 ID_OFF_SIZE);
683 if (env_debug)
684 envd_log(LOG_ERR, "\n Sensor Id %x offset %x",
685 id.id, id.offset);
687 if (id.id > MAX_SENSOR_ID) {
688 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG,
689 FRU_SEEPROM_NAME);
690 return (-1);
694 * Copy into the sensor control block array according to the
695 * sensor ID
697 (void) memcpy((char *)&sensor_ctrl[id.id],
698 (char *)&buff[id.offset],
699 sizeof (sensor_ctrl_blk_t));
700 nsensors--;
701 id_offset += ID_OFF_SIZE;
705 * Skip past no of Fan entry(single byte)
707 id_offset++;
708 while (nfans > 0) {
709 (void) memcpy((char *)&id, (char *)&buff[id_offset + 2],
710 ID_OFF_SIZE);
712 if (env_debug)
713 envd_log(LOG_ERR, "\n Fan Id %x offset %x", id.id,
714 id.offset);
716 if (id.id > 3) {
717 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG,
718 FRU_SEEPROM_NAME);
719 return (-1);
722 (void) memcpy((char *)&fan_ctrl[id.id],
723 (char *)&buff[id.offset], sizeof (fan_ctrl_blk_t));
725 nfans--;
726 id_offset += ID_OFF_SIZE;
730 * Match Sensor/ES ID and point correct data
731 * based on IDs
733 for (i = 0; i < N_ENVD_SENSORS; i++) {
734 snodep = &envd_sensors[i];
735 snodep->es_ptr = &sensor_ctrl[snodep->id];
739 * Match Fan/ES ID and point to correct ES Data
740 * based on IDs
742 for (i = 0; (fnodep = envd_fans[i]) != NULL; i++)
743 fnodep->es_ptr = &fan_ctrl[fnodep->id];
745 return (0);
748 static int
749 envd_es_setup(void)
751 envfru = get_fru_envsegs();
752 if (envfru == NULL) {
753 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG, FRU_SEEPROM_NAME);
754 return (-1);
756 return (process_fru_seeprom((uchar_t *)envfru->envsegbufp));
759 static void
760 envd_es_destroy(void)
762 if (envfru != NULL)
763 free(envfru->envsegbufp);
767 * Lookup fan and return a pointer to env_fan_t data structure.
769 env_fan_t *
770 fan_lookup(char *name)
772 int i;
773 env_fan_t *fanp;
775 for (i = 0; (fanp = envd_fans[i]) != NULL; i++) {
776 if (strcmp(fanp->name, name) == 0)
777 return (fanp);
779 return (NULL);
783 * Lookup sensor and return a pointer to env_sensor_t data structure.
785 env_sensor_t *
786 sensor_lookup(char *name)
788 env_sensor_t *sensorp;
789 int i;
791 for (i = 0; i < N_ENVD_SENSORS; ++i) {
792 sensorp = &envd_sensors[i];
793 if (strcmp(sensorp->name, name) == 0)
794 return (sensorp);
796 return (NULL);
800 * Lookup disk and return a pointer to env_disk_t data structure.
802 env_disk_t *
803 disk_lookup(char *name)
805 int i;
806 env_disk_t *diskp;
808 for (i = 0; (diskp = envd_disks[i]) != NULL; i++) {
809 if (strncmp(diskp->name, name, strlen(name)) == 0)
810 return (diskp);
812 return (NULL);
816 * Get current temperature
817 * Returns -1 on error, 0 if successful
820 get_temperature(env_sensor_t *sensorp, tempr_t *temp)
822 int fd = sensorp->fd;
823 int retval = 0;
825 if (fd == -1)
826 retval = -1;
827 else if (ioctl(fd, I2C_GET_TEMPERATURE, temp) == -1) {
829 retval = -1;
831 if (sensorp->error == 0) {
832 sensorp->error = 1;
833 envd_log(LOG_WARNING, ENV_SENSOR_ACCESS_FAIL,
834 sensorp->name, errno, strerror(errno));
836 } else if (sensorp->error != 0) {
837 sensorp->error = 0;
838 envd_log(LOG_WARNING, ENV_SENSOR_ACCESS_OK, sensorp->name);
840 if (sensorp->crtbl != NULL) {
841 *temp = (tempr_t)y_of_x(sensorp->crtbl, *temp);
844 return (retval);
848 * Get current disk temperature
849 * Returns -1 on error, 0 if successful
852 disk_temperature(env_disk_t *diskp, tempr_t *temp)
854 int retval = 0;
856 if (diskp == NULL)
857 retval = -1;
858 else {
859 *temp = diskp->current_temp;
861 return (retval);
865 * Get uncorrected current temperature
866 * Returns -1 on error, 0 if successful
868 static int
869 get_raw_temperature(env_sensor_t *sensorp, tempr_t *temp)
871 int fd = sensorp->fd;
872 int retval = 0;
874 if (fd == -1)
875 retval = -1;
876 else if (ioctl(fd, I2C_GET_TEMPERATURE, temp) == -1) {
877 retval = -1;
880 return (retval);
884 * Return Fan RPM given N & tach
885 * count and N are retrived from the
886 * ADM1031 chip.
888 static int
889 tach_to_rpm(int n, uint8_t tach)
891 if (n * tach == 0)
892 return (0);
893 return ((ADCSAMPLE * 60) / (n * tach));
896 static int
897 get_raw_fan_speed(env_fan_t *fanp, uint8_t *fanspeedp)
899 int fan_fd;
900 int retval = 0;
902 fan_fd = fanp->fd;
904 if (fan_fd == -1)
905 retval = -1;
906 else if (ioctl(fan_fd, I2C_GET_FAN_SPEED, fanspeedp) == -1) {
907 retval = -1;
911 return (retval);
915 * Get current fan speed
916 * This function returns a RPM value for fanspeed
917 * in fanspeedp.
918 * Returns -1 on error, 0 if successful
921 get_fan_speed(env_fan_t *fanp, fanspeed_t *fanspeedp)
923 int fan_fd;
924 uint8_t tach;
926 fan_fd = fanp->fd;
928 if (fan_fd == -1)
929 return (-1);
930 if (fanp->id == DIMM_FAN_ID) {
931 return (get_dimm_fan_speed(fan_fd, fanspeedp));
933 if (ioctl(fan_fd, I2C_GET_FAN_SPEED, &tach) == -1) {
934 return (-1);
938 * Fanspeeds are reported as 0
939 * if the tach is out of range or fan status is off
940 * and if monitoring fan status is enabled.
942 if (mon_fanstat && (!fanp->fanstat || tach == FAN_OUT_OF_RANGE)) {
943 *fanspeedp = 0;
944 } else {
945 *fanspeedp =
946 tach_to_rpm(fanp->speedrange, tach);
949 return (0);
953 * Set fan speed
954 * This function accepts a percentage of fan speed
955 * from 0-100 and programs the HW monitor fans to the corresponding
956 * fanspeed value.
957 * Returns -1 on error, -2 on invalid args passed, 0 if successful
960 set_fan_speed(env_fan_t *fanp, fanspeed_t fanspeed)
962 int fan_fd;
963 int retval = 0;
964 uint8_t speed;
966 fan_fd = fanp->fd;
967 if (fan_fd == -1)
968 return (-1);
970 if (fanspeed < 0 || fanspeed > 100)
971 return (-2);
973 speed = (uint8_t)ADM_SETFANSPEED_CONV(fanspeed);
975 if (ioctl(fan_fd, I2C_SET_FAN_SPEED, &speed) == -1) {
976 retval = -1;
978 return (retval);
982 * close all fan devices
984 static void
985 envd_close_fans(void)
987 int i;
988 env_fan_t *fanp;
990 for (i = 0; (fanp = envd_fans[i]) != NULL; i++) {
991 if (fanp->fd != -1) {
992 (void) close(fanp->fd);
993 fanp->fd = -1;
999 * Close sensor devices and freeup resources
1001 static void
1002 envd_close_sensors(void)
1004 env_sensor_t *sensorp;
1005 int i;
1007 for (i = 0; i < N_ENVD_SENSORS; ++i) {
1008 sensorp = &envd_sensors[i];
1009 if (sensorp->fd != -1) {
1010 (void) close(sensorp->fd);
1011 sensorp->fd = -1;
1013 if (sensorp->crtbl != NULL)
1014 fini_table(sensorp->crtbl);
1019 * Open fan devices and initialize per fan data structure.
1020 * Returns #fans found.
1022 static int
1023 envd_setup_fans(void)
1025 int i, fd;
1026 env_fan_t *fanp;
1027 char path[PATH_MAX];
1028 int fancnt = 0;
1029 uint8_t n = 0;
1030 picl_nodehdl_t tnodeh;
1031 i2c_reg_t i2c_reg;
1033 for (i = 0; (fanp = envd_fans[i]) != NULL; i++) {
1034 /* make sure cpu0/1 present for validating cpu fans */
1035 if (fanp->id == CPU0_FAN_ID) {
1036 if (ptree_get_node_by_path(CPU0_PATH, &tnodeh) !=
1037 PICL_SUCCESS) {
1038 fanp->present = B_FALSE;
1039 continue;
1042 if (fanp->id == CPU1_FAN_ID) {
1043 if (ptree_get_node_by_path(CPU1_PATH, &tnodeh) !=
1044 PICL_SUCCESS) {
1045 fanp->present = B_FALSE;
1046 continue;
1049 if (fanp->id == DIMM_FAN_ID) {
1050 if (ptree_get_node_by_path(DIMM_FAN_CONTROLLER_PATH,
1051 &tnodeh) != PICL_SUCCESS) {
1052 if (env_debug)
1053 envd_log(LOG_ERR,
1054 "dimm Fan not found in the system.\n");
1055 fanp->present = B_FALSE;
1056 continue;
1059 (void) strcpy(path, "/devices");
1060 (void) strlcat(path, fanp->devfs_path, sizeof (path));
1061 fd = open(path, O_RDWR);
1062 if (fd == -1) {
1063 envd_log(LOG_CRIT,
1064 ENV_FAN_OPEN_FAIL, fanp->name,
1065 fanp->devfs_path, errno, strerror(errno));
1066 fanp->present = B_FALSE;
1067 continue;
1069 fanp->fd = fd;
1070 if (fanp->id == DIMM_FAN_ID) {
1072 * set the SW aware bit in command register.
1073 * Clear the Fan fault latch bit.
1075 i2c_reg.reg_num = PIC16F819_COMMAND_REGISTER;
1076 i2c_reg.reg_value = (PIC16F819_SW_AWARE_MODE |
1077 PIC16F819_FAN_FAULT_CLEAR);
1078 if (ioctl(fd, I2C_SET_REG, &i2c_reg) == -1) {
1079 if (env_debug)
1080 envd_log(LOG_ERR,
1081 "Error in writing to COMMAND reg. of DIMM FAN controller\n");
1083 } else {
1084 /* Get speed range value */
1085 if (ioctl(fd, ADM1031_GET_FAN_FEATURE, &n) != -1) {
1086 fanp->speedrange =
1087 adm_speedrange_map[(n >> 6) & 0x03];
1088 } else {
1089 fanp->speedrange = FAN_RANGE_DEFAULT;
1092 fanp->present = B_TRUE;
1093 fanp->fanstat = 0;
1094 fanp->cspeed = TACH_UNKNOWN;
1095 fanp->lspeed = TACH_UNKNOWN;
1096 fanp->conccnt = 0;
1097 fancnt++;
1099 return (fancnt);
1102 static int
1103 envd_setup_disks(void)
1105 int ret, i, page_index, page_len;
1106 picl_nodehdl_t tnodeh;
1107 env_disk_t *diskp;
1108 uint_t vendor_id;
1109 uint_t device_id;
1110 uchar_t log_page[256];
1113 * Check if the SCSi controller on the system is 1010 or 1030
1116 if (ptree_get_node_by_path(SCSI_CONTROLLER_NODE_PATH,
1117 &tnodeh) != PICL_SUCCESS) {
1118 if (env_debug)
1119 envd_log(LOG_ERR,
1120 "On-Board SCSI controller not found in the system.\n");
1121 monitor_disk_temp = 0;
1122 return (-1);
1125 if ((ret = ptree_get_propval_by_name(tnodeh, VENDOR_ID,
1126 &vendor_id,
1127 sizeof (vendor_id))) != 0) {
1128 if (env_debug)
1129 envd_log(LOG_ERR,
1130 "Error in getting vendor-id for SCSI controller. ret = %d errno = 0x%d\n",
1131 ret, errno);
1132 monitor_disk_temp = 0;
1133 return (-1);
1135 if ((ret = ptree_get_propval_by_name(tnodeh, DEVICE_ID,
1136 &device_id,
1137 sizeof (device_id))) != 0) {
1138 if (env_debug)
1139 envd_log(LOG_ERR,
1140 "Error in getting device-id for SCSI controller. ret = %d errno = 0x%d\n",
1141 ret, errno);
1142 monitor_disk_temp = 0;
1143 return (-1);
1145 if (env_debug)
1146 envd_log(LOG_ERR, "vendor-id=0x%x device-id=0x%x\n",
1147 vendor_id, device_id);
1148 if ((vendor_id != LSI1030_VENDOR_ID) ||
1149 (device_id != LSI1030_DEVICE_ID)) {
1150 monitor_disk_temp = 0;
1151 return (-1);
1154 * We have found LSI1030 SCSi controller onboard.
1157 for (i = 0; (diskp = envd_disks[i]) != NULL; i++) {
1159 if (ptree_get_node_by_path(diskp->nodepath,
1160 &tnodeh) != PICL_SUCCESS) {
1161 diskp->present = B_FALSE;
1162 if (env_debug)
1163 envd_log(LOG_ERR,
1164 "DISK %d not found in the system.\n",
1165 diskp->id);
1166 continue;
1168 diskp->fd = open(diskp->devfs_path, O_RDONLY);
1169 if (diskp->fd == -1) {
1170 diskp->present = B_FALSE;
1171 envd_log(LOG_ERR,
1172 "Error in opening %s errno = 0x%x\n",
1173 diskp->devfs_path, errno);
1174 continue;
1176 diskp->present = B_TRUE;
1177 diskp->tpage_supported = B_FALSE;
1179 * Find out if the Temperature page is supported by the disk.
1181 ret = scsi_log_sense(diskp->fd, SUPPORTED_LPAGES,
1182 log_page, sizeof (log_page));
1183 if (ret != 0) {
1184 continue;
1186 page_len = ((log_page[2] << 8) & 0xFF00) | log_page[3];
1188 for (page_index = LOGPAGEHDRSIZE;
1189 page_index < page_len + LOGPAGEHDRSIZE; page_index++) {
1190 switch (log_page[page_index]) {
1191 case TEMPERATURE_PAGE:
1192 diskp->tpage_supported = B_TRUE;
1193 if (env_debug)
1194 envd_log(LOG_ERR,
1195 "tpage supported for %s\n",
1196 diskp->nodepath);
1197 default:
1198 break;
1201 diskp->warning_tstamp = 0;
1202 diskp->shutdown_tstamp = 0;
1203 diskp->high_warning = disk_high_warn_temperature;
1204 diskp->low_warning = disk_low_warn_temperature;
1205 diskp->high_shutdown = disk_high_shutdown_temperature;
1206 diskp->low_shutdown = disk_low_shutdown_temperature;
1207 ret = get_disk_temp(diskp);
1209 return (0);
1213 * Open temperature sensor devices and initialize per sensor data structure.
1214 * Returns #sensors found.
1216 static int
1217 envd_setup_sensors(void)
1219 env_sensor_t *sensorp;
1220 sensor_ctrl_blk_t *es_ptr;
1221 table_t *tblp;
1222 char path[PATH_MAX];
1223 int sensorcnt = 0;
1224 int i, j, nentries;
1225 int16_t tmin = 0;
1226 picl_nodehdl_t tnodeh;
1228 for (i = 0; i < N_ENVD_SENSORS; ++i) {
1229 sensorp = &envd_sensors[i];
1230 /* Initialize sensor's initial state */
1231 sensorp->shutdown_initiated = B_FALSE;
1232 sensorp->warning_tstamp = 0;
1233 sensorp->shutdown_tstamp = 0;
1234 sensorp->error = 0;
1235 sensorp->crtbl = NULL;
1236 /* make sure cpu0/1 sensors are present */
1237 if (sensorp->id == CPU0_SENSOR_ID) {
1238 if (ptree_get_node_by_path(CPU0_PATH, &tnodeh) !=
1239 PICL_SUCCESS) {
1240 sensorp->present = B_FALSE;
1241 continue;
1244 if (sensorp->id == CPU1_SENSOR_ID) {
1245 if (ptree_get_node_by_path(CPU1_PATH, &tnodeh) !=
1246 PICL_SUCCESS) {
1247 sensorp->present = B_FALSE;
1248 continue;
1251 (void) strcpy(path, "/devices");
1252 (void) strlcat(path, sensorp->devfs_path,
1253 sizeof (path));
1254 sensorp->fd = open(path, O_RDWR);
1255 if (sensorp->fd == -1) {
1256 envd_log(LOG_ERR, ENV_SENSOR_OPEN_FAIL,
1257 sensorp->name, sensorp->devfs_path,
1258 errno, strerror(errno));
1259 sensorp->present = B_FALSE;
1260 continue;
1262 sensorp->present = B_TRUE;
1263 sensorcnt++;
1266 * Get Tmin
1269 if (ioctl(sensorp->fd, ADM1031_GET_TEMP_MIN_RANGE,
1270 &tmin) != -1) {
1271 sensorp->tmin = TMIN(tmin);
1272 } else {
1273 sensorp->tmin = -1;
1275 if (env_debug)
1276 envd_log(LOG_ERR, "Sensor %s tmin %d",
1277 sensorp->name, sensorp->tmin);
1280 * Create a correction table
1281 * if correction pairs are present in es
1282 * segment.
1284 es_ptr = sensorp->es_ptr;
1286 if (es_ptr == NULL) {
1287 continue;
1289 nentries = es_ptr->correctionEntries;
1291 if (nentries <= 2) {
1292 if (env_debug)
1293 envd_log(LOG_CRIT, "sensor correction <2");
1294 continue;
1297 sensorp->crtbl = init_table(nentries);
1298 if (sensorp->crtbl == NULL)
1299 continue;
1300 tblp = sensorp->crtbl;
1301 tblp->xymap[0].x =
1302 (char)es_ptr->correctionPair[0].measured;
1303 tblp->xymap[0].y =
1304 (char)es_ptr->correctionPair[0].corrected;
1306 for (j = 1; j < nentries; ++j) {
1307 tblp->xymap[j].x =
1308 (char)es_ptr->correctionPair[j].measured;
1309 tblp->xymap[j].y =
1310 (char)es_ptr->correctionPair[j].corrected;
1312 if (tblp->xymap[j].x <= tblp->xymap[j - 1].x) {
1313 fini_table(tblp);
1314 sensorp->crtbl = NULL;
1315 envd_log(LOG_CRIT, ENV_FRU_BAD_ENVSEG,
1316 FRU_SEEPROM_NAME);
1317 break;
1321 if (env_debug) {
1322 envd_log(LOG_CRIT, "Sensor correction %s",
1323 sensorp->name);
1324 for (j = 0; j < nentries; j++)
1325 envd_log(LOG_CRIT, " %d %d",
1326 tblp->xymap[j].x, tblp->xymap[j].y);
1329 return (sensorcnt);
1333 * Modify ADM Tmin/ranges depending what power level
1334 * we are from.
1336 static void
1337 updateadm_ranges(char *name, uchar_t cur_lpstate)
1339 env_sensor_t *sensorp;
1340 fan_ctrl_blk_t *fanctl;
1341 uchar_t tmin;
1342 uchar_t trange;
1343 uint16_t tdata;
1344 int sysfd;
1345 uchar_t sys_id = SYS_HWM_ID;
1346 uint8_t mode;
1347 static uint16_t tsave[2] = {0, 0};
1348 /* Index of saved Tmin/Trange for two sensors */
1349 uint16_t tindex = 0;
1351 sensorp = sensor_lookup(name);
1352 if (sensorp == NULL)
1353 return;
1356 * If there is only one Control pairs then return
1358 fanctl = ((env_fan_t *)sensorp->fanp)->es_ptr;
1360 if (fanctl != NULL && fanctl->no_ctl_pairs <= 1)
1361 return;
1364 * if fan control specifies that ranges are same then
1365 * we skip re-programming adm chip.
1368 tmin = fanctl->fan_ctl_pairs[0].tMin;
1369 trange = fanctl->fan_ctl_pairs[0].tRange;
1370 if ((tmin == fanctl->fan_ctl_pairs[1].tMin) &&
1371 (trange == fanctl->fan_ctl_pairs[1].tRange))
1372 return;
1374 sysfd = open(hwm_devs[sys_id], O_RDWR);
1375 if (sysfd == -1) {
1376 if (env_debug)
1377 envd_log(LOG_ERR, ENV_ADM_OPEN_FAIL, hwm_devs[sys_id],
1378 errno, strerror(errno));
1379 return;
1381 tindex = ((strcmp(name, SENSOR_SYS_IN) == 0) ? 0 : 1);
1383 /* Read ADM default value only for the first time */
1384 if (tsave[tindex] == 0) {
1385 if (ioctl(sensorp->fd, ADM1031_GET_TEMP_MIN_RANGE,
1386 &tsave[tindex]) == -1) {
1387 if (env_debug)
1388 envd_log(LOG_ERR,
1389 "read tminrange ioctl failed");
1390 (void) close(sysfd);
1391 return;
1395 * Need to reinit ADM to manual mode for Tmin range to be
1396 * effective.
1398 mode = ADM1031_MANUAL_MODE;
1399 if (ioctl(sysfd, ADM1031_SET_MONITOR_MODE, &mode) == -1) {
1400 if (env_debug)
1401 envd_log(LOG_ERR, ENV_ADM_MANUAL_MODE);
1402 (void) close(sysfd);
1403 return;
1406 if (cur_lpstate == 1) {
1408 * ADM 1031 Tmin/Trange register need to be reprogrammed.
1410 tdata = ((fanctl->fan_ctl_pairs[cur_lpstate].tMin / TMIN_UNITS)
1411 << TMIN_SHIFT);
1412 /* Need to pack tRange in ADM bits 2:0 */
1413 switch (fanctl->fan_ctl_pairs[cur_lpstate].tRange) {
1414 case 5:
1415 break;
1417 case 10:
1418 tdata |= 1;
1419 break;
1421 case 20:
1422 tdata |= 2;
1423 break;
1425 case 40:
1426 tdata |= 3;
1427 break;
1429 case 80:
1430 tdata |= 4;
1431 break;
1433 } else
1434 tdata = tsave[tindex];
1436 if (ioctl(sensorp->fd, ADM1031_SET_TEMP_MIN_RANGE,
1437 &tdata) != -1)
1438 sensorp->tmin = TMIN(tdata);
1440 mode = ADM1031_AUTO_MODE;
1441 if (ioctl(sysfd, ADM1031_SET_MONITOR_MODE, &mode) == -1) {
1442 if (env_debug)
1443 envd_log(LOG_ERR, ENV_ADM_AUTO_MODE);
1445 (void) close(sysfd);
1448 /* ARGSUSED */
1449 static void *
1450 pmthr(void *args)
1452 pm_state_change_t pmstate;
1453 char physpath[PATH_MAX];
1454 int pre_lpstate;
1456 pmstate.physpath = physpath;
1457 pmstate.size = sizeof (physpath);
1458 cur_lpstate = 0;
1459 pre_lpstate = 1;
1461 pm_fd = open(PM_DEVICE, O_RDWR);
1462 if (pm_fd == -1) {
1463 envd_log(LOG_ERR, PM_THREAD_EXITING, errno, strerror(errno));
1464 return (NULL);
1466 for (;;) {
1468 * Get PM state change events to check if the system
1469 * is in lowest power state and adjust ADM hardware
1470 * monitor's fan speed settings.
1472 * To minimize polling, we use the blocking interface
1473 * to get the power state change event here.
1475 if (ioctl(pm_fd, PM_GET_STATE_CHANGE_WAIT, &pmstate) != 0) {
1476 if (errno != EINTR)
1477 break;
1478 continue;
1480 do {
1481 if (env_debug) {
1482 envd_log(LOG_INFO,
1483 "pmstate event:0x%x flags:%x"
1484 "comp:%d oldval:%d newval:%d path:%s\n",
1485 pmstate.event, pmstate.flags,
1486 pmstate.component,
1487 pmstate.old_level,
1488 pmstate.new_level,
1489 pmstate.physpath);
1491 cur_lpstate =
1492 (pmstate.flags & PSC_ALL_LOWEST) ? 1 : 0;
1493 } while (ioctl(pm_fd, PM_GET_STATE_CHANGE, &pmstate) == 0);
1495 * Change ADM ranges as per E* Requirements. Update
1496 * happens only for valid state changes.
1498 if (pre_lpstate != cur_lpstate) {
1499 pre_lpstate = cur_lpstate;
1500 updateadm_ranges(SENSOR_SYS_OUT, cur_lpstate);
1501 updateadm_ranges(SENSOR_SYS_IN, cur_lpstate);
1504 /* Not reached */
1505 return (NULL);
1509 * This function is used to reasonably predict the
1510 * state of the fan (ON/OFF) using tmin and current temperature.
1512 * We know the fan is on if temp >= tmin and fan is off if
1513 * temp < (Tmin - Hysterisis).
1515 * When the temperature is in between we don't know if the fan is on/off
1516 * because the temperature could be decreasing and not have crossed
1517 * Tmin - hysterisis and vice a versa.
1519 * FAN ON
1520 * Tmin
1521 * -------------------------------------------
1523 * FAN ON/OFF
1525 * --------------------------------------------
1526 * Tmin - Hysterisis
1527 * FAN OFF
1529 * To solve the problem of finding out if the fan is on/off in our gray region
1530 * we keep track of the last read tach and the current read tach. From
1531 * experimentation and from discussions with analog devices it is unlikely that
1532 * if the fans are on we will get a constant tach reading more than 5 times in
1533 * a row. This is not but the most fool proof approach but the best we can do.
1535 * This routine implements the above logic for a sensor with an
1536 * associated fan. The caller garauntees sensorp and fanp are not null.
1539 static void
1540 check_fanstat(env_sensor_t *sensorp)
1542 env_fan_t *fanp = sensorp->fanp;
1543 tempr_t temp;
1544 uint8_t fanspeed;
1546 if (get_raw_temperature(sensorp, &temp) == -1)
1547 return;
1549 if (temp < (sensorp->tmin - ADM_HYSTERISIS)) {
1551 fanp->fanstat = 0; /* Fan off */
1552 fanp->lspeed = TACH_UNKNOWN; /* Reset Last read tach */
1553 fanp->conccnt = 0;
1555 } else if (temp >= sensorp->tmin) {
1557 fanp->fanstat = 1; /* Fan on */
1558 fanp->lspeed = TACH_UNKNOWN;
1559 fanp->conccnt = 0;
1561 } else {
1562 if (get_raw_fan_speed(fanp, &fanspeed) == -1)
1563 return;
1565 fanp->cspeed = fanspeed;
1567 * First time in the gray area
1568 * set last read speed to current speed
1570 if (fanp->lspeed == TACH_UNKNOWN) {
1571 fanp->lspeed = fanspeed;
1572 } else {
1573 if (fanp->lspeed != fanp->cspeed) {
1574 fanp->conccnt = 0;
1575 fanp->fanstat = 1;
1576 } else {
1577 fanp->conccnt++;
1579 if (fanp->conccnt >= N_SEQ_TACH)
1580 fanp->fanstat = 0;
1582 fanp->lspeed = fanp->cspeed;
1587 * There is an issue with the ADM1031 chip that causes the chip
1588 * to not update the tach register in case the fan stops. The
1589 * fans stop when the temperature measured (temp) drops below
1590 * Tmin - Hysterisis and turn on when the temp >= Tmin.
1592 * Since the tach registers don't update and remain stuck at the
1593 * last read tach value our get_fan_speed function always returns
1594 * a non-zero RPM reading.
1596 * To fix this we need to figure out when the fans will be on/off
1597 * depending on the current temperature. Currently we poll for
1598 * interrupts, we can use that loop to determine what the current
1599 * temperature is and if the fans should be on/off.
1601 * We get current temperature and check the fans.
1603 static void
1604 monitor_fanstat(void)
1606 env_sensor_t *sensorp;
1607 env_fan_t *fanp;
1608 int i;
1610 for (i = 0; i < N_ENVD_SENSORS; i++) {
1611 sensorp = &envd_sensors[i];
1613 if (!sensorp)
1614 continue;
1616 fanp = sensorp->fanp;
1618 if (!(fanp && fanp->present))
1619 continue;
1621 if (sensorp->tmin != -1) {
1622 check_fanstat(sensorp);
1623 } else {
1624 fanp->fanstat = 1;
1630 static int
1631 handle_overtemp_interrupt(int hwm_id)
1633 env_sensor_t *sensorp;
1634 tempr_t temp;
1635 uchar_t smap[MAX_SENSORS];
1636 time_t ct;
1637 uchar_t i;
1638 char msgbuf[BUFSIZ];
1639 char syscmd[BUFSIZ];
1640 boolean_t return_flag;
1641 int ret;
1642 timespec_t to;
1643 pthread_mutex_t env_monitor_mutex = PTHREAD_MUTEX_INITIALIZER;
1644 pthread_cond_t env_monitor_cv = PTHREAD_COND_INITIALIZER;
1646 /* Clear Map of Sensor Entries */
1647 (void) memset(smap, SENSOR_OK, sizeof (smap));
1649 for (;;) {
1650 for (i = 0; i < N_ENVD_SENSORS; i++) {
1651 sensorp = &envd_sensors[i];
1654 * Check whether the sensor belongs to the
1655 * interrupting ADM hardware monitor
1657 if (sensorp->hwm_id != hwm_id)
1658 continue;
1660 if (sensorp->present == B_FALSE)
1661 continue;
1663 * if shutdown is initiated then we simply loop
1664 * through the sensors until shutdown
1666 if (sensorp->shutdown_initiated == B_TRUE)
1667 continue;
1669 /* get current temp for this sensor */
1670 if (get_temperature(sensorp, &temp) == -1)
1671 continue;
1673 sensorp->cur_temp = temp;
1675 if (env_debug)
1676 envd_log(LOG_ERR,
1677 "sensor name %s, cur temp %d, "
1678 "HW %d LW %d SD %d LS %d\n",
1679 sensorp->name, temp,
1680 sensorp->es_ptr->high_warning,
1681 (int)sensorp->es_ptr->low_warning,
1682 sensorp->es_ptr->high_shutdown,
1683 (int)sensorp->es_ptr->low_shutdown);
1685 if (TEMP_IN_WARNING_RANGE(sensorp->cur_temp, sensorp)) {
1687 * Log on warning atmost one second
1689 ct = (time_t)(gethrtime() / NANOSEC);
1690 if ((ct - sensorp->warning_tstamp) >=
1691 warning_interval) {
1692 envd_log(LOG_CRIT,
1693 ENV_WARNING_MSG, sensorp->name,
1694 temp,
1695 sensorp->es_ptr->low_warning,
1696 sensorp->es_ptr->high_warning);
1697 sensorp->warning_tstamp = ct;
1699 smap[i] = SENSOR_WARN;
1700 } else {
1702 * We will fall in this caterory only if
1703 * Temperature drops/increases from warning
1704 * threshold. If so we set sensor map to
1705 * OK so that we can exit the loop if
1706 * shutdown not initiated.
1708 smap[i] = SENSOR_OK;
1711 if (TEMP_IN_SHUTDOWN_RANGE(temp, sensorp) &&
1712 !shutdown_override) {
1713 ct = (time_t)(gethrtime() / NANOSEC);
1714 if (sensorp->shutdown_tstamp == 0)
1715 sensorp->shutdown_tstamp = ct;
1716 if ((ct - sensorp->shutdown_tstamp) >=
1717 shutdown_interval) {
1718 sensorp->shutdown_initiated = B_TRUE;
1719 (void) snprintf(msgbuf, sizeof (msgbuf),
1720 ENV_SHUTDOWN_MSG, sensorp->name,
1721 temp,
1722 sensorp->es_ptr->low_shutdown,
1723 sensorp->es_ptr->high_shutdown);
1724 envd_log(LOG_ALERT, msgbuf);
1726 if (system_shutdown_started == B_FALSE) {
1727 (void) snprintf(syscmd, sizeof (syscmd),
1728 "%s \"%s\"", SHUTDOWN_CMD, msgbuf);
1729 envd_log(LOG_ALERT, syscmd);
1730 system_shutdown_started = B_TRUE;
1731 (void) system(syscmd);
1733 } else if (sensorp->shutdown_tstamp != 0)
1734 sensorp->shutdown_tstamp = 0;
1738 * Sweep thorugh Sensor Map and if warnings OR shutdown
1739 * are not logged then return to caller.
1741 return_flag = B_TRUE;
1742 for (i = 0; i < N_ENVD_SENSORS; i++)
1743 if (smap[i] == SENSOR_WARN)
1744 return_flag = B_FALSE;
1746 if ((return_flag == B_TRUE) &&
1747 (system_shutdown_started == B_FALSE)) {
1748 return (1);
1751 wait_till_timeout:
1753 * We use pthread_cond_reltimedwait_np to sleep for
1754 * fixed interval of time.
1755 * earlier implementation used alarm() call which
1756 * fails in Multi threaded environment. If multiple
1757 * threads call alarm() only one of the threads is
1758 * sent the SIGALRM signal.
1760 (void) pthread_mutex_lock(&env_monitor_mutex);
1761 ret = pthread_cond_reltimedwait_np(&env_monitor_cv,
1762 &env_monitor_mutex, &to);
1763 to.tv_sec = SENSORPOLL_INTERVAL;
1764 to.tv_nsec = 0;
1765 if (ret != ETIMEDOUT) {
1766 (void) pthread_mutex_unlock(&env_monitor_mutex);
1767 goto wait_till_timeout;
1769 (void) pthread_mutex_unlock(&env_monitor_mutex);
1774 * This is env thread which monitors the current temperature when
1775 * warning threshold is exceeded. The job is to make sure it does
1776 * not execced/decrease shutdown threshold. If it does it will start
1777 * forced shutdown to avoid reaching hardware poweroff via THERM interrupt.
1778 * For Enchilada there will be two threads, one for each ADM chip.
1780 static void *
1781 ovtemp_thr(void *args)
1783 int fd;
1784 uint8_t stat[2];
1785 int hwm_id = (int)args;
1786 int err;
1787 env_fan_t *fanp;
1788 timespec_t to;
1789 int ret;
1790 pthread_mutex_t env_monitor_mutex = PTHREAD_MUTEX_INITIALIZER;
1791 pthread_cond_t env_monitor_cv = PTHREAD_COND_INITIALIZER;
1793 fd = open(hwm_devs[hwm_id], O_RDWR);
1794 if (fd == -1) {
1795 envd_log(LOG_ERR, ENV_ADM_OPEN_FAIL, hwm_devs[hwm_id],
1796 errno, strerror(errno));
1797 return (NULL);
1799 if (env_debug)
1800 envd_log(LOG_ERR, "ovtemp thread for %s running...\n",
1801 hwm_devs[hwm_id]);
1803 for (;;) {
1805 * Sleep for specified seconds before issuing IOCTL
1806 * again.
1810 * We use pthread_cond_reltimedwait_np to sleep for
1811 * fixed interval of time.
1812 * earlier implementation used alarm() call which
1813 * fails in Multi threaded environment. If multiple
1814 * threads call alarm() only one of the threads is
1815 * sent the SIGALRM signal.
1817 (void) pthread_mutex_lock(&env_monitor_mutex);
1818 ret = pthread_cond_reltimedwait_np(&env_monitor_cv,
1819 &env_monitor_mutex, &to);
1820 to.tv_sec = INTERRUPTPOLL_INTERVAL;
1821 to.tv_nsec = 0;
1822 if (ret != ETIMEDOUT) {
1823 (void) pthread_mutex_unlock(&env_monitor_mutex);
1824 continue;
1826 (void) pthread_mutex_unlock(&env_monitor_mutex);
1828 * Monitor the sensors to update fan status
1830 if (mon_fanstat)
1831 monitor_fanstat();
1834 * Read ADM1031 two Status Registers to determine source
1835 * of Interrupts.
1838 if ((err = ioctl(fd, ADM1031_GET_STATUS_1, &stat[0])) != -1)
1839 err = ioctl(fd, ADM1031_GET_STATUS_2, &stat[1]);
1841 if (err == -1) {
1842 if (env_debug)
1843 envd_log(LOG_ERR,
1844 "OverTemp: Status Error");
1845 continue;
1848 if (env_debug)
1849 envd_log(LOG_ERR, "INTR %s, Stat1 %x, Stat2 %x",
1850 hwm_devs[hwm_id], stat[0], stat[1]);
1852 if (stat[0] & FANFAULT) {
1853 fanp = fan_lookup(hwm_fans[hwm_id][HWM_FAN1]);
1854 if (fanp && fanp->present)
1855 envd_log(LOG_ERR, ENV_FAN_FAULT,
1856 hwm_devs[hwm_id],
1857 hwm_fans[hwm_id][HWM_FAN1]);
1859 if (stat[1] & FANFAULT) {
1860 fanp = fan_lookup(hwm_fans[hwm_id][HWM_FAN2]);
1861 if (fanp && fanp->present)
1862 envd_log(LOG_ERR, ENV_FAN_FAULT,
1863 hwm_devs[hwm_id],
1864 hwm_fans[hwm_id][HWM_FAN2]);
1867 * Check respective Remote/Local High, Low before start
1868 * manual monitoring
1870 if ((stat[0] & STAT1MASK) || (stat[1] & STAT2MASK))
1871 (void) handle_overtemp_interrupt(hwm_id);
1873 } /* end of for ever loop */
1874 /*NOTREACHED*/
1875 return (NULL);
1878 static void *
1879 dimm_fan_thr(void *args)
1881 char syscmd[BUFSIZ];
1882 char msgbuf[BUFSIZ];
1883 i2c_reg_t i2c_reg;
1884 timespec_t to;
1885 int ret;
1886 pthread_mutex_t env_monitor_mutex = PTHREAD_MUTEX_INITIALIZER;
1887 pthread_cond_t env_monitor_cv = PTHREAD_COND_INITIALIZER;
1890 for (;;) {
1892 * Sleep for specified seconds before issuing IOCTL
1893 * again.
1895 (void) pthread_mutex_lock(&env_monitor_mutex);
1896 ret = pthread_cond_reltimedwait_np(&env_monitor_cv,
1897 &env_monitor_mutex, &to);
1898 to.tv_sec = INTERRUPTPOLL_INTERVAL;
1899 to.tv_nsec = 0;
1900 if (ret != ETIMEDOUT) {
1901 (void) pthread_mutex_unlock(&env_monitor_mutex);
1902 continue;
1904 (void) pthread_mutex_unlock(&env_monitor_mutex);
1906 * We write to the comand register periodically
1907 * to inform the PIC firmware that Solaris is
1908 * Monitoring the dimm fan periodically.
1910 i2c_reg.reg_num = PIC16F819_COMMAND_REGISTER;
1911 i2c_reg.reg_value = PIC16F819_SW_AWARE_MODE;
1912 if (ioctl(envd_dimm_fan.fd,
1913 I2C_SET_REG, &i2c_reg) == -1) {
1914 if (env_debug)
1915 envd_log(LOG_ERR,
1916 "Error in writing to COMMAND reg. of DIMM FAN controller\n");
1919 * We initiate shutdown if fan status indicates
1920 * failure.
1922 if (is_dimm_fan_failed() != 0) {
1924 * Mark Dimm fan present as False so that we
1925 * do not WARN the user of the Fan failure
1926 * repeatedly.
1928 envd_dimm_fan.present = B_FALSE;
1929 (void) snprintf(msgbuf, sizeof (msgbuf),
1930 ENV_DIMM_FAN_FAILURE_SHUTDOWN_MSG,
1931 ENV_DIMM_FAN,
1932 dimm_fan_rpm_string, dimm_fan_status_string,
1933 dimm_fan_command_string,
1934 dimm_fan_debug_string);
1935 envd_log(LOG_ALERT, msgbuf);
1937 if (system_shutdown_started == B_FALSE) {
1938 system_shutdown_started = B_TRUE;
1939 (void) snprintf(syscmd, sizeof (syscmd),
1940 "%s \"%s\"",
1941 SHUTDOWN_CMD,
1942 msgbuf);
1943 envd_log(LOG_ALERT, syscmd);
1944 (void) system(syscmd);
1948 /*NOTREACHED*/
1949 return (NULL);
1951 static int
1952 scsi_log_sense(int fd, uchar_t page_code, uchar_t *pagebuf, uint16_t pagelen)
1954 struct uscsi_cmd ucmd_buf;
1955 uchar_t cdb_buf[CDB_GROUP1];
1956 struct scsi_extended_sense sense_buf;
1957 int ret_val;
1959 bzero((void *)&cdb_buf, sizeof (cdb_buf));
1960 bzero((void *)&ucmd_buf, sizeof (ucmd_buf));
1961 bzero((void *)&sense_buf, sizeof (sense_buf));
1963 cdb_buf[0] = SCMD_LOG_SENSE_G1;
1964 cdb_buf[2] = (0x01 << 6) | page_code;
1965 cdb_buf[7] = (uchar_t)((pagelen & 0xFF00) >> 8);
1966 cdb_buf[8] = (uchar_t)(pagelen & 0x00FF);
1968 ucmd_buf.uscsi_cdb = (char *)cdb_buf;
1969 ucmd_buf.uscsi_cdblen = sizeof (cdb_buf);
1970 ucmd_buf.uscsi_bufaddr = (caddr_t)pagebuf;
1971 ucmd_buf.uscsi_buflen = pagelen;
1972 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
1973 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
1974 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
1975 ucmd_buf.uscsi_timeout = 60;
1977 ret_val = ioctl(fd, USCSICMD, ucmd_buf);
1978 if (ret_val == 0 && ucmd_buf.uscsi_status == 0) {
1979 if (env_debug)
1980 envd_log(LOG_ERR,
1981 "log sense command for page_code 0x%x succeeded\n", page_code);
1982 return (ret_val);
1984 if (env_debug)
1985 envd_log(LOG_ERR,
1986 "log sense command failed.ret_val = 0x%x status = 0x%x errno = 0x%x\n",
1987 ret_val, ucmd_buf.uscsi_status, errno);
1988 return (1);
1991 static int
1992 get_disk_temp(env_disk_t *diskp)
1994 int ret;
1995 uchar_t tpage[256];
1997 ret = scsi_log_sense(diskp->fd,
1998 TEMPERATURE_PAGE,
1999 tpage, sizeof (tpage));
2000 if (ret != 0) {
2001 diskp->current_temp = DISK_INVALID_TEMP;
2002 diskp->ref_temp = DISK_INVALID_TEMP;
2003 return (-1);
2006 * For the current temperature verify that the parameter
2007 * length is 0x02 and the parameter code is 0x00
2008 * Temperature value of 255(0xFF) is considered INVALID.
2010 if ((tpage[7] == 0x02) && (tpage[4] == 0x00) &&
2011 (tpage[5] == 0x00)) {
2012 if (tpage[9] == 0xFF) {
2013 diskp->current_temp = DISK_INVALID_TEMP;
2014 return (-1);
2015 } else {
2016 diskp->current_temp = tpage[9];
2021 * For the reference temperature verify that the parameter
2022 * length is 0x02 and the parameter code is 0x01
2023 * Temperature value of 255(0xFF) is considered INVALID.
2025 if ((tpage[13] == 0x02) && (tpage[10] == 0x00) &&
2026 (tpage[11] == 0x01)) {
2027 if (tpage[15] == 0xFF) {
2028 diskp->ref_temp = DISK_INVALID_TEMP;
2029 } else {
2030 diskp->ref_temp = tpage[15];
2033 return (0);
2036 /* ARGSUSED */
2037 static void *
2038 disk_temp_thr(void *args)
2040 char syscmd[BUFSIZ];
2041 char msgbuf[BUFSIZ];
2042 timespec_t to;
2043 int ret, i;
2044 env_disk_t *diskp;
2045 pthread_mutex_t env_monitor_mutex = PTHREAD_MUTEX_INITIALIZER;
2046 pthread_cond_t env_monitor_cv = PTHREAD_COND_INITIALIZER;
2047 pm_state_change_t pmstate;
2048 int idle_time;
2049 int disk_pm_fd;
2050 time_t ct;
2052 disk_pm_fd = open(PM_DEVICE, O_RDWR);
2053 if (disk_pm_fd == -1) {
2054 envd_log(LOG_ERR,
2055 DISK_TEMP_THREAD_EXITING,
2056 errno, strerror(errno));
2057 return (NULL);
2059 for (;;) {
2061 * Sleep for specified seconds before issuing IOCTL
2062 * again.
2064 (void) pthread_mutex_lock(&env_monitor_mutex);
2065 ret = pthread_cond_reltimedwait_np(&env_monitor_cv,
2066 &env_monitor_mutex, &to);
2067 to.tv_sec = disk_scan_interval;
2068 to.tv_nsec = 0;
2069 if (ret != ETIMEDOUT) {
2070 (void) pthread_mutex_unlock(&env_monitor_mutex);
2071 continue;
2073 (void) pthread_mutex_unlock(&env_monitor_mutex);
2074 for (i = 0; (diskp = envd_disks[i]) != NULL; i++) {
2075 if (diskp->present == B_FALSE)
2076 continue;
2077 if (diskp->tpage_supported == B_FALSE)
2078 continue;
2080 * If the disk temperature is above the warning threshold
2081 * continue monitoring until the temperature drops below
2082 * warning threshold.
2083 * if the temperature is in the NORMAL range monitor only
2084 * when the disk is BUSY.
2085 * We do not want to read the disk temperature if the disk is
2086 * is idling. The reason for this is disk will never get into
2087 * lowest power mode if we scan the disk temperature
2088 * peridoically. To avoid this situation we first determine
2089 * the idle_time of the disk. If the disk has been IDLE since
2090 * we scanned the temperature last time we will not read the
2091 * temperature.
2093 if (!DISK_TEMP_IN_WARNING_RANGE(diskp->current_temp, diskp)) {
2094 pmstate.physpath = diskp->physpath;
2095 pmstate.size = strlen(diskp->physpath);
2096 pmstate.component = 0;
2097 if ((idle_time =
2098 ioctl(disk_pm_fd,
2099 PM_GET_TIME_IDLE, &pmstate)) == -1) {
2100 if (errno != EINTR) {
2101 if (env_debug)
2102 envd_log(LOG_ERR,
2103 "ioctl PM_GET_TIME_IDLE failed for DISK0. errno=0x%x\n",
2104 errno);
2105 continue;
2107 continue;
2109 if (idle_time >= (disk_scan_interval/2)) {
2110 if (env_debug) {
2111 envd_log(LOG_ERR,
2112 "%s idle time = %d\n",
2113 diskp->name, idle_time);
2115 continue;
2118 ret = get_disk_temp(diskp);
2119 if (ret != 0)
2120 continue;
2121 if (env_debug) {
2122 envd_log(LOG_ERR,
2123 "%s temp = %d ref. temp = %d\n",
2124 diskp->name, diskp->current_temp, diskp->ref_temp);
2127 * If this disk already triggered system shutdown, don't
2128 * log any more shutdown/warning messages for it.
2130 if (diskp->shutdown_initiated)
2131 continue;
2134 * Check for the temperature in warning and shutdown range
2135 * and take appropriate action.
2137 if (DISK_TEMP_IN_WARNING_RANGE(diskp->current_temp, diskp)) {
2139 * Check if the temperature has been in warning
2140 * range during last disk_warning_duration interval.
2141 * If so, the temperature is truly in warning
2142 * range and we need to log a warning message,
2143 * but no more than once every disk_warning_interval
2144 * seconds.
2146 time_t wtstamp = diskp->warning_tstamp;
2148 ct = (time_t)(gethrtime() / NANOSEC);
2149 if (diskp->warning_start == 0)
2150 diskp->warning_start = ct;
2151 if (((ct - diskp->warning_start) >=
2152 disk_warning_duration) && (wtstamp == 0 ||
2153 (ct - wtstamp) >= disk_warning_interval)) {
2154 envd_log(LOG_CRIT, ENV_WARNING_MSG,
2155 diskp->name, diskp->current_temp,
2156 diskp->low_warning,
2157 diskp->high_warning);
2158 diskp->warning_tstamp = ct;
2160 } else if (diskp->warning_start != 0)
2161 diskp->warning_start = 0;
2163 if (!shutdown_override &&
2164 DISK_TEMP_IN_SHUTDOWN_RANGE(diskp->current_temp, diskp)) {
2165 ct = (time_t)(gethrtime() / NANOSEC);
2166 if (diskp->shutdown_tstamp == 0)
2167 diskp->shutdown_tstamp = ct;
2170 * Shutdown the system if the temperature remains
2171 * in the shutdown range for over disk_shutdown_interval
2172 * seconds.
2174 if ((ct - diskp->shutdown_tstamp) >=
2175 disk_shutdown_interval) {
2176 /* log error */
2177 diskp->shutdown_initiated = B_TRUE;
2178 (void) snprintf(msgbuf, sizeof (msgbuf),
2179 ENV_SHUTDOWN_MSG, diskp->name,
2180 diskp->current_temp, diskp->low_shutdown,
2181 diskp->high_shutdown);
2182 envd_log(LOG_ALERT, msgbuf);
2184 /* shutdown the system (only once) */
2185 if (system_shutdown_started == B_FALSE) {
2186 (void) snprintf(syscmd, sizeof (syscmd),
2187 "%s \"%s\"", shutdown_cmd, msgbuf);
2188 envd_log(LOG_ALERT, syscmd);
2189 system_shutdown_started = B_TRUE;
2190 (void) system(syscmd);
2193 } else if (diskp->shutdown_tstamp != 0)
2194 diskp->shutdown_tstamp = 0;
2197 } /* end of forever loop */
2201 * Setup envrionmental monitor state and start threads to monitor
2202 * temperature and power management state.
2203 * Returns -1 on error, 0 if successful.
2205 static int
2206 envd_setup(void)
2208 int ret;
2210 if (getenv("SUNW_piclenvd_debug") != NULL)
2211 env_debug = 1;
2213 if (pthread_attr_init(&thr_attr) != 0 ||
2214 pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM) != 0) {
2215 return (-1);
2218 ret = envd_es_setup();
2219 if (ret < 0) {
2220 ovtemp_monitor = 0;
2221 pm_monitor = 0;
2225 * Setup temperature sensors and fail if we can't open
2226 * at least one sensor.
2228 if (envd_setup_sensors() <= 0) {
2229 return (NULL);
2233 * Setup fan device (don't fail even if we can't access
2234 * the fan as we can still monitor temeperature.
2236 (void) envd_setup_fans();
2238 (void) envd_setup_disks();
2240 /* If ES Segment setup failed,don't create thread */
2242 if (ovtemp_monitor && ovtemp_thr1_created == B_FALSE) {
2243 if (pthread_create(&ovtemp_thr1_id, &thr_attr, ovtemp_thr,
2244 (void *)CPU_HWM_ID) != 0)
2245 envd_log(LOG_ERR, ENVTHR_THREAD_CREATE_FAILED);
2246 else
2247 ovtemp_thr1_created = B_TRUE;
2250 if (ovtemp_monitor && ovtemp_thr2_created == B_FALSE) {
2251 if (pthread_create(&ovtemp_thr2_id, &thr_attr, ovtemp_thr,
2252 (void *)SYS_HWM_ID) != 0)
2253 envd_log(LOG_ERR, ENVTHR_THREAD_CREATE_FAILED);
2254 else
2255 ovtemp_thr2_created = B_TRUE;
2258 if (envd_dimm_fan.present) {
2259 if (dimm_fan_thr_created == B_FALSE) {
2260 if (pthread_create(&dimm_fan_thr_id, &thr_attr, dimm_fan_thr,
2261 NULL) != 0)
2262 envd_log(LOG_ERR, ENVTHR_THREAD_CREATE_FAILED);
2263 else
2264 dimm_fan_thr_created = B_TRUE;
2269 * Create a thread to monitor PM state
2271 if (pm_monitor && pmthr_created == B_FALSE) {
2272 if (pthread_create(&pmthr_tid, &thr_attr, pmthr,
2273 NULL) != 0)
2274 envd_log(LOG_CRIT, PM_THREAD_CREATE_FAILED);
2275 else
2276 pmthr_created = B_TRUE;
2278 if (monitor_disk_temp) {
2279 if (disk_temp_thr_created == B_FALSE) {
2280 if (pthread_create(&disk_temp_thr_id, &thr_attr, disk_temp_thr,
2281 NULL) != 0)
2282 envd_log(LOG_ERR, ENVTHR_THREAD_CREATE_FAILED);
2283 else
2284 disk_temp_thr_created = B_TRUE;
2287 return (0);
2290 static void
2291 piclenvd_register(void)
2293 picld_plugin_register(&my_reg_info);
2296 static void
2297 piclenvd_init(void)
2300 (void) env_picl_setup_tuneables();
2303 * Setup the environmental data structures
2305 if (envd_setup() != 0) {
2306 envd_log(LOG_CRIT, ENVD_PLUGIN_INIT_FAILED);
2307 return;
2311 * Now setup/populate PICL tree
2313 env_picl_setup();
2316 static void
2317 piclenvd_fini(void)
2321 * Invoke env_picl_destroy() to remove any PICL nodes/properties
2322 * (including volatile properties) we created. Once this call
2323 * returns, there can't be any more calls from the PICL framework
2324 * to get current temperature or fan speed.
2326 env_picl_destroy();
2327 envd_close_sensors();
2328 envd_close_fans();
2329 envd_es_destroy();
2332 /*VARARGS2*/
2333 void
2334 envd_log(int pri, const char *fmt, ...)
2336 va_list ap;
2338 va_start(ap, fmt);
2339 vsyslog(pri, fmt, ap);
2340 va_end(ap);
2344 * Tunables support functions
2346 static env_tuneable_t *
2347 tuneable_lookup(picl_prophdl_t proph)
2349 int i;
2350 env_tuneable_t *tuneablep = NULL;
2352 for (i = 0; i < ntuneables; i++) {
2353 tuneablep = &tuneables[i];
2354 if (tuneablep->proph == proph)
2355 return (tuneablep);
2358 return (NULL);
2361 static int
2362 get_cpu_tach(ptree_rarg_t *parg, void *buf)
2364 picl_prophdl_t proph;
2365 env_tuneable_t *tuneablep;
2366 int fd;
2367 int8_t cfg;
2369 proph = parg->proph;
2371 tuneablep = tuneable_lookup(proph);
2373 if (tuneablep == NULL)
2374 return (PICL_FAILURE);
2376 fd = open(CPU_HWM_DEVFS, O_RDWR);
2378 if (fd == -1) {
2379 return (PICL_FAILURE);
2382 if (ioctl(fd, ADM1031_GET_CONFIG_2, &cfg) == -1) {
2383 return (PICL_FAILURE);
2386 if ((cfg & TACH_ENABLE_MASK) == TACH_ENABLE_MASK) {
2387 *((int *)tuneablep->value) = ENABLE;
2388 } else {
2389 *((int *)tuneablep->value) = DISABLE;
2392 (void) memcpy(buf, tuneablep->value,
2393 tuneablep->nbytes);
2395 (void) close(fd);
2396 return (PICL_SUCCESS);
2399 static int
2400 set_cpu_tach(ptree_warg_t *parg, const void *buf)
2402 picl_prophdl_t proph;
2403 env_tuneable_t *tuneablep;
2404 int fd, val;
2405 int8_t cfg;
2407 if (parg->cred.dc_euid != 0)
2408 return (PICL_PERMDENIED);
2410 proph = parg->proph;
2412 tuneablep = tuneable_lookup(proph);
2414 if (tuneablep == NULL)
2415 return (PICL_FAILURE);
2418 fd = open(CPU_HWM_DEVFS, O_RDWR);
2420 if (fd == -1) {
2421 return (PICL_FAILURE);
2424 if (ioctl(fd, ADM1031_GET_CONFIG_2, &cfg) == -1) {
2425 return (PICL_FAILURE);
2428 (void) memcpy(&val, (caddr_t)buf, sizeof (val));
2430 if (val == ENABLE) {
2431 cfg |= TACH_ENABLE_MASK;
2432 } else if (val == DISABLE) {
2433 cfg &= ~TACH_ENABLE_MASK;
2437 if (ioctl(fd, ADM1031_SET_CONFIG_2, &cfg) == -1) {
2438 return (PICL_FAILURE);
2441 (void) close(fd);
2442 return (PICL_SUCCESS);
2445 static int
2446 get_sys_tach(ptree_rarg_t *parg, void *buf)
2448 picl_prophdl_t proph;
2449 env_tuneable_t *tuneablep;
2450 int fd;
2451 int8_t cfg;
2453 proph = parg->proph;
2455 tuneablep = tuneable_lookup(proph);
2457 if (tuneablep == NULL)
2458 return (PICL_FAILURE);
2460 fd = open(SYS_HWM_DEVFS, O_RDWR);
2462 if (fd == -1) {
2463 return (PICL_FAILURE);
2466 if (ioctl(fd, ADM1031_GET_CONFIG_2, &cfg) == -1) {
2467 return (PICL_FAILURE);
2470 if ((cfg & TACH_ENABLE_MASK) == TACH_ENABLE_MASK) {
2471 *((int *)tuneablep->value) = ENABLE;
2472 } else {
2473 *((int *)tuneablep->value) = DISABLE;
2476 (void) memcpy(buf, tuneablep->value,
2477 tuneablep->nbytes);
2479 (void) close(fd);
2480 return (PICL_SUCCESS);
2483 static int
2484 set_sys_tach(ptree_warg_t *parg, const void *buf)
2486 picl_prophdl_t proph;
2487 env_tuneable_t *tuneablep;
2488 int fd, val;
2489 int8_t cfg;
2491 if (parg->cred.dc_euid != 0)
2492 return (PICL_PERMDENIED);
2494 proph = parg->proph;
2496 tuneablep = tuneable_lookup(proph);
2498 if (tuneablep == NULL)
2499 return (PICL_FAILURE);
2502 fd = open(SYS_HWM_DEVFS, O_RDWR);
2504 if (fd == -1) {
2505 return (PICL_FAILURE);
2508 if (ioctl(fd, ADM1031_GET_CONFIG_2, &cfg) == -1) {
2509 return (PICL_FAILURE);
2512 (void) memcpy(&val, buf, sizeof (val));
2514 if (val == ENABLE) {
2515 cfg |= TACH_ENABLE_MASK;
2516 } else if (val == DISABLE) {
2517 cfg &= ~TACH_ENABLE_MASK;
2521 if (ioctl(fd, ADM1031_SET_CONFIG_2, &cfg) == -1) {
2522 return (PICL_FAILURE);
2525 (void) close(fd);
2526 return (PICL_SUCCESS);
2529 static int
2530 get_monitor_cpu_mode(ptree_rarg_t *parg, void *buf)
2532 picl_prophdl_t proph;
2533 env_tuneable_t *tuneablep;
2534 int fd;
2535 int8_t mmode;
2537 proph = parg->proph;
2539 tuneablep = tuneable_lookup(proph);
2541 if (tuneablep == NULL)
2542 return (PICL_FAILURE);
2544 fd = open(CPU_HWM_DEVFS, O_RDWR);
2546 if (fd == -1) {
2547 return (PICL_FAILURE);
2550 if (ioctl(fd, ADM1031_GET_MONITOR_MODE, &mmode) == -1) {
2551 return (PICL_FAILURE);
2554 if (mmode == ADM1031_AUTO_MODE) {
2555 *((int *)tuneablep->value) = ENABLE;
2556 } else {
2557 *((int *)tuneablep->value) = DISABLE;
2560 (void) memcpy(buf, tuneablep->value,
2561 tuneablep->nbytes);
2563 (void) close(fd);
2564 return (PICL_SUCCESS);
2567 static int
2568 set_monitor_cpu_mode(ptree_warg_t *parg, const void *buf)
2570 picl_prophdl_t proph;
2571 env_tuneable_t *tuneablep;
2572 int fd, val;
2573 int8_t mmode;
2575 if (parg->cred.dc_euid != 0)
2576 return (PICL_PERMDENIED);
2578 proph = parg->proph;
2580 tuneablep = tuneable_lookup(proph);
2582 if (tuneablep == NULL)
2583 return (PICL_FAILURE);
2585 fd = open(CPU_HWM_DEVFS, O_RDWR);
2587 if (fd == -1) {
2588 return (PICL_FAILURE);
2591 (void) memcpy(&val, buf, sizeof (val));
2593 if (val == ENABLE) {
2594 mmode = ADM1031_AUTO_MODE;
2595 } else if (val == DISABLE) {
2596 mmode = ADM1031_MANUAL_MODE;
2599 if (ioctl(fd, ADM1031_SET_MONITOR_MODE, &mmode) == -1) {
2600 return (PICL_FAILURE);
2603 (void) close(fd);
2604 return (PICL_SUCCESS);
2607 static int
2608 get_monitor_sys_mode(ptree_rarg_t *parg, void *buf)
2610 picl_prophdl_t proph;
2611 env_tuneable_t *tuneablep;
2612 int fd;
2613 int8_t mmode;
2615 proph = parg->proph;
2617 tuneablep = tuneable_lookup(proph);
2619 if (tuneablep == NULL)
2620 return (PICL_FAILURE);
2622 fd = open(SYS_HWM_DEVFS, O_RDWR);
2624 if (fd == -1) {
2625 return (PICL_FAILURE);
2628 if (ioctl(fd, ADM1031_GET_MONITOR_MODE, &mmode) == -1) {
2629 return (PICL_FAILURE);
2632 if (mmode == ADM1031_AUTO_MODE) {
2633 *((int *)tuneablep->value) = ENABLE;
2634 } else {
2635 *((int *)tuneablep->value) = DISABLE;
2638 (void) memcpy(buf, tuneablep->value,
2639 tuneablep->nbytes);
2641 (void) close(fd);
2642 return (PICL_SUCCESS);
2645 static int
2646 set_monitor_sys_mode(ptree_warg_t *parg, const void *buf)
2648 picl_prophdl_t proph;
2649 env_tuneable_t *tuneablep;
2650 int fd, val;
2651 int8_t mmode;
2653 if (parg->cred.dc_euid != 0)
2654 return (PICL_PERMDENIED);
2656 proph = parg->proph;
2658 tuneablep = tuneable_lookup(proph);
2660 if (tuneablep == NULL)
2661 return (PICL_FAILURE);
2663 fd = open(SYS_HWM_DEVFS, O_RDWR);
2665 if (fd == -1) {
2666 return (PICL_FAILURE);
2669 (void) memcpy(&val, buf, sizeof (val));
2671 if (val == ENABLE) {
2672 mmode = ADM1031_AUTO_MODE;
2673 } else if (val == DISABLE) {
2674 mmode = ADM1031_MANUAL_MODE;
2677 if (ioctl(fd, ADM1031_SET_MONITOR_MODE, &mmode) == -1) {
2678 return (PICL_FAILURE);
2681 (void) close(fd);
2682 return (PICL_SUCCESS);
2685 static int
2686 get_string_val(ptree_rarg_t *parg, void *buf)
2688 picl_prophdl_t proph;
2689 env_tuneable_t *tuneablep;
2691 proph = parg->proph;
2693 tuneablep = tuneable_lookup(proph);
2695 if (tuneablep == NULL)
2696 return (PICL_FAILURE);
2698 (void) memcpy(buf, (caddr_t)tuneablep->value,
2699 tuneablep->nbytes);
2701 return (PICL_SUCCESS);
2704 static int
2705 set_string_val(ptree_warg_t *parg, const void *buf)
2707 picl_prophdl_t proph;
2708 env_tuneable_t *tuneablep;
2710 if (parg->cred.dc_euid != 0)
2711 return (PICL_PERMDENIED);
2713 proph = parg->proph;
2715 tuneablep = tuneable_lookup(proph);
2717 if (tuneablep == NULL)
2718 return (PICL_FAILURE);
2720 (void) memcpy((caddr_t)tuneables->value, (caddr_t)buf,
2721 tuneables->nbytes);
2724 return (PICL_SUCCESS);
2727 static int
2728 get_int_val(ptree_rarg_t *parg, void *buf)
2730 picl_prophdl_t proph;
2731 env_tuneable_t *tuneablep;
2733 proph = parg->proph;
2735 tuneablep = tuneable_lookup(proph);
2737 if (tuneablep == NULL)
2738 return (PICL_FAILURE);
2740 (void) memcpy((int *)buf, (int *)tuneablep->value,
2741 tuneablep->nbytes);
2743 return (PICL_SUCCESS);
2746 static int
2747 set_int_val(ptree_warg_t *parg, const void *buf)
2749 picl_prophdl_t proph;
2750 env_tuneable_t *tuneablep;
2752 if (parg->cred.dc_euid != 0)
2753 return (PICL_PERMDENIED);
2755 proph = parg->proph;
2757 tuneablep = tuneable_lookup(proph);
2759 if (tuneablep == NULL)
2760 return (PICL_FAILURE);
2762 (void) memcpy((int *)tuneablep->value, (int *)buf,
2763 tuneablep->nbytes);
2765 return (PICL_SUCCESS);
2769 get_dimm_fan_speed(int fan_fd, fanspeed_t *fanspeedp)
2771 int16_t dimm_fan_period;
2772 i2c_reg_t i2c_reg;
2775 * The dimm fan period is 16 bit value and we need to read
2776 * registers 2 and 3 to get the LSB and MSB values.
2778 i2c_reg.reg_num = PIC16F819_FAN_PERIOD_MSB_REGISTER;
2779 if (ioctl(fan_fd, I2C_GET_REG, &i2c_reg) == -1) {
2780 if (env_debug)
2781 envd_log(LOG_ERR,
2782 "Error in reading FAN_PERIOD MSB REGISTER\n");
2783 return (-1);
2785 dimm_fan_period = (i2c_reg.reg_value << 8);
2786 i2c_reg.reg_num = PIC16F819_FAN_PERIOD_LSB_REGISTER;
2787 if (ioctl(fan_fd, I2C_GET_REG, &i2c_reg) == -1) {
2788 if (env_debug)
2789 envd_log(LOG_ERR,
2790 "Error in reading FAN_PERIOD LSB REGISTER\n");
2791 return (-1);
2793 dimm_fan_period |= i2c_reg.reg_value;
2794 if (env_debug)
2795 envd_log(LOG_ERR,
2796 " dimm fan tach period is 0x%x\n", dimm_fan_period);
2797 if (dimm_fan_period == 0) {
2798 if (env_debug)
2799 envd_log(LOG_ERR,
2800 "dimm fan tach period read as zero. Illegal value.\n");
2801 return (-1);
2803 *fanspeedp = PIC16F819_FAN_TACH_TO_RPM(dimm_fan_period);
2804 return (0);
2808 is_dimm_fan_failed(void)
2810 i2c_reg_t i2c_reg;
2811 fanspeed_t fan_speed;
2812 int retry_count;
2814 if (envd_dimm_fan.fd == -1)
2815 return (-1);
2817 * read register 1 to look at Fan fault bit.
2819 i2c_reg.reg_num = PIC16F819_STATUS_REGISTER;
2820 retry_count = MAX_RETRIES_FOR_PIC16F819_REG_READ;
2821 while (retry_count > 0) {
2822 if (ioctl(envd_dimm_fan.fd, I2C_GET_REG, &i2c_reg) == -1) {
2823 retry_count--;
2824 continue;
2825 } else break;
2827 if (retry_count != MAX_RETRIES_FOR_PIC16F819_REG_READ) {
2828 if (env_debug)
2829 envd_log(LOG_ERR,
2830 "%d retries attempted in reading STATUS register.\n",
2831 (MAX_RETRIES_FOR_PIC16F819_REG_READ - retry_count));
2833 if (retry_count == 0) {
2834 (void) strncpy(dimm_fan_status_string, NOT_AVAILABLE,
2835 sizeof (dimm_fan_status_string));
2836 (void) strncpy(dimm_fan_command_string, NOT_AVAILABLE,
2837 sizeof (dimm_fan_command_string));
2838 (void) strncpy(dimm_fan_debug_string, NOT_AVAILABLE,
2839 sizeof (dimm_fan_debug_string));
2840 (void) strncpy(dimm_fan_rpm_string, NOT_AVAILABLE,
2841 sizeof (dimm_fan_rpm_string));
2842 return (-1);
2844 if (env_debug)
2845 envd_log(LOG_ERR,
2846 "DIMM FAN STATUS reg = 0x%x\n", i2c_reg.reg_value);
2847 if (i2c_reg.reg_value & PIC16F819_FAN_FAILED) {
2848 (void) snprintf(dimm_fan_status_string,
2849 sizeof (dimm_fan_status_string), "0x%x",
2850 i2c_reg.reg_value);
2851 i2c_reg.reg_num = PIC16F819_DEBUG_REGISTER;
2852 if (ioctl(envd_dimm_fan.fd, I2C_GET_REG, &i2c_reg) == -1) {
2853 (void) strncpy(dimm_fan_debug_string, NOT_AVAILABLE,
2854 sizeof (dimm_fan_debug_string));
2855 } else {
2856 (void) snprintf(dimm_fan_debug_string,
2857 sizeof (dimm_fan_debug_string),
2858 "0x%x", i2c_reg.reg_value);
2860 i2c_reg.reg_num = PIC16F819_COMMAND_REGISTER;
2861 if (ioctl(envd_dimm_fan.fd, I2C_GET_REG, &i2c_reg) == -1) {
2862 (void) strncpy(dimm_fan_command_string, NOT_AVAILABLE,
2863 sizeof (dimm_fan_command_string));
2864 } else {
2865 (void) snprintf(dimm_fan_command_string,
2866 sizeof (dimm_fan_command_string),
2867 "0x%x", i2c_reg.reg_value);
2869 if (get_dimm_fan_speed(envd_dimm_fan.fd, &fan_speed) == -1) {
2870 (void) strncpy(dimm_fan_rpm_string, NOT_AVAILABLE,
2871 sizeof (dimm_fan_rpm_string));
2872 } else {
2873 (void) snprintf(dimm_fan_rpm_string,
2874 sizeof (dimm_fan_rpm_string),
2875 "%d", fan_speed);
2877 return (1);
2878 } else return (0);