piclenvd.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains the environmental PICL plug-in module.
*/
/*
* This plugin sets up the PICLTREE for Chicago WS.
* fan speeds.
*
* The environmental policy defaults to the auto mode
* as programmed by OBP at boot time.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysmacros.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <alloca.h>
#include <unistd.h>
#include <sys/processor.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <picl.h>
#include <picltree.h>
#include <picldefs.h>
#include <pthread.h>
#include <signal.h>
#include <libdevinfo.h>
#include <sys/systeminfo.h>
#include <note.h>
#include <sys/pic16f747.h>
#include "envd.h"
int debug_fd;
/*
* PICL plugin entry points
*/
static void piclenvd_register(void);
static void piclenvd_init(void);
static void piclenvd_fini(void);
/*
* Env setup routines
*/
extern void env_picl_setup(void);
extern void env_picl_destroy(void);
extern int env_picl_setup_tuneables(void);
#pragma init(piclenvd_register)
/*
* Plugin registration information
*/
static picld_plugin_reg_t my_reg_info = {
"SUNW_piclenvd",
};
#define REGISTER_INFORMATION_STRING_LENGTH 16
static char fan_rpm_string[REGISTER_INFORMATION_STRING_LENGTH] = {0};
static char fan_status_string[REGISTER_INFORMATION_STRING_LENGTH] = {0};
static int get_disk_temp(env_disk_t *);
/*
* Env thread variables
*/
static pthread_t system_temp_thr1_id;
static pthread_attr_t thr_attr;
static pthread_t disk_temp_thr_id;
static pthread_t fan_thr_id;
/*
* PM thread related variables
*/
static int cur_lpstate; /* cur low power state */
/*
* Envd plug-in verbose flag set by SUNW_PICLENVD_DEBUG environment var
* Setting the verbose tuneable also enables debugging for better
* control
*/
int env_debug = 0;
/*
* These are debug variables for keeping track of the total number
* of Fan and Temp sensor retries over the lifetime of the plugin.
*/
static int total_fan_retries = 0;
static int total_temp_retries = 0;
/*
* Fan devices
*/
static env_fan_t envd_system_fan0 = {
};
static env_fan_t envd_system_fan1 = {
};
static env_fan_t envd_system_fan2 = {
};
static env_fan_t envd_system_fan3 = {
};
static env_fan_t envd_system_fan4 = {
};
/*
* Disk devices
*/
static env_disk_t envd_disk0 = {
DISK0_ID, -1,
};
static env_disk_t envd_disk1 = {
DISK1_ID, -1,
};
static env_disk_t envd_disk2 = {
DISK2_ID, -1,
};
static env_disk_t envd_disk3 = {
DISK3_ID, -1,
};
/*
* Sensors
*/
static env_sensor_t envd_sensor_cpu0 = {
};
static env_sensor_t envd_sensor_cpu1 = {
};
static env_sensor_t envd_sensor_adt7462 = {
};
static env_sensor_t envd_sensor_mb = {
};
static env_sensor_t envd_sensor_lm95221 = {
};
static env_sensor_t envd_sensor_fire = {
};
static env_sensor_t envd_sensor_lsi1064 = {
};
static env_sensor_t envd_sensor_front_panel = {
};
/*
* The vendor-id and device-id are the properties associated with
* the SCSI controller. This is used to identify a particular controller
* like LSI1064.
*/
#define VENDOR_ID "vendor-id"
#define DEVICE_ID "device-id"
/*
* The implementation for SCSI disk drives to supply info. about
* temperature is not mandatory. Hence we first determine if the
* temperature page is supported. To do this we need to scan the list
* of pages supported.
*/
#define SUPPORTED_LPAGES 0
#define TEMPERATURE_PAGE 0x0D
#define LOGPAGEHDRSIZE 4
/*
* NULL terminated array of fans
*/
};
/*
* NULL terminated array of disks
*/
static env_disk_t *envd_disks[] = {
};
/*
* NULL terminated array of temperature sensors
*/
#define N_ENVD_SENSORS 8
static env_sensor_t *envd_sensors[] = {
};
#define NOT_AVAILABLE "NA"
/*
* Tuneables
*/
#define ENABLE 1
#define DISABLE 0
int monitor_disk_temp = 1;
static int disk_high_warn_temperature = DISK_HIGH_WARN_TEMPERATURE;
static int disk_low_warn_temperature = DISK_LOW_WARN_TEMPERATURE;
static int disk_high_shutdown_temperature =
static int disk_low_shutdown_temperature = DISK_LOW_SHUTDOWN_TEMPERATURE;
static int disk_scan_interval = DISK_SCAN_INTERVAL;
static int sensor_scan_interval = SENSOR_SCAN_INTERVAL;
static int fan_scan_interval = FAN_SCAN_INTERVAL;
static int shutdown_override = 0;
static int sensor_warning_interval = SENSOR_WARNING_INTERVAL;
static int sensor_warning_duration = SENSOR_WARNING_DURATION;
static int sensor_shutdown_interval = SENSOR_SHUTDOWN_INTERVAL;
static int disk_warning_interval = DISK_WARNING_INTERVAL;
static int disk_warning_duration = DISK_WARNING_DURATION;
static int disk_shutdown_interval = DISK_SHUTDOWN_INTERVAL;
static char shutdown_cmd[] = SHUTDOWN_CMD;
env_tuneable_t tuneables[] = {
&get_int_val, &set_int_val, sizeof (int)},
&get_int_val, &set_int_val, sizeof (int)},
&get_int_val, &set_int_val, sizeof (int)},
&get_int_val, &set_int_val, sizeof (int)},
{"sensor-warning-duration", PICL_PTYPE_INT,
sizeof (int)},
{"disk-scan-interval", PICL_PTYPE_INT,
sizeof (int)},
{"fan-scan-interval", PICL_PTYPE_INT,
sizeof (int)},
{"sensor-scan-interval", PICL_PTYPE_INT,
sizeof (int)},
sizeof (int)},
sizeof (int)},
sizeof (int)},
sizeof (int)},
sizeof (int)},
sizeof (shutdown_cmd)},
&get_int_val, &set_int_val, sizeof (int)},
{"disk-high-warn-temperature", PICL_PTYPE_INT,
&set_int_val, sizeof (int)},
{"disk-low-warn-temperature", PICL_PTYPE_INT,
&set_int_val, sizeof (int)},
{"disk-high-shutdown-temperature", PICL_PTYPE_INT,
&set_int_val, sizeof (int)},
{"disk-low-shutdown-temperature", PICL_PTYPE_INT,
&set_int_val, sizeof (int)},
/*
* Check if the front panel exists.
*/
{"front-panel-present", PICL_PTYPE_INT,
&set_int_val, sizeof (int)},
&get_int_val, &set_int_val, sizeof (int)}
};
/*
* We use this to figure out how many tuneables there are
* This is variable because the publishing routine needs this info
* in piclenvsetup.c
*/
/*
* Lookup fan and return a pointer to env_fan_t data structure.
*/
fan_lookup(char *name)
{
int i;
return (fanp);
}
return (NULL);
}
/*
* Lookup sensor and return a pointer to env_sensor_t data structure.
*/
sensor_lookup(char *name)
{
int i;
for (i = 0; i < N_ENVD_SENSORS; ++i) {
sensorp = envd_sensors[i];
return (sensorp);
}
return (NULL);
}
/*
* Lookup disk and return a pointer to env_disk_t data structure.
*/
disk_lookup(char *name)
{
int i;
return (diskp);
}
return (NULL);
}
/*
* Get current temperature
* Returns -1 on error, 0 if successful
*/
int
{
int retval = 0;
if (fd == -1)
retval = -1;
retval = -1;
}
(void) sleep(1);
}
if (total_temp_retries && env_debug) {
"Total retries for sensors = %d",
}
}
return (retval);
}
/*
* Get current disk temperature
* Returns -1 on error, 0 if successful
*/
int
{
int retval = 0;
retval = -1;
else
return (retval);
}
/*
* Get current fan speed
* This function returns a RPM value for fanspeed
* in fanspeedp.
* Returns -1 on error, 0 if successful
*/
int
{
int fan_fd;
int real_tach;
int retries;
if (fan_fd == -1)
return (-1);
if (has_fan_failed(fanp)) {
*fanspeedp = 0;
return (0);
}
/* try to read the fan information */
continue;
(void) sleep(1);
}
if (retries == MAX_FAN_RETRIES) {
return (-1);
}
if (total_fan_retries && env_debug) {
}
return (0);
}
/*
* Set fan speed
* This function accepts a percentage of fan speed
* from 0-100 and programs the HW monitor fans to the corresponding
* fanspeed value.
* Returns -1 on error, -2 on invalid args passed, 0 if successful
*/
int
{
int fan_fd;
int retval = 0;
if (fan_fd == -1)
return (-1);
return (-2);
retval = -1;
return (retval);
}
/*
* close all fan devices
*/
static void
envd_close_fans(void)
{
int i;
}
}
}
/*
* Close sensor devices and freeup resources
*/
static void
envd_close_sensors(void)
{
int i;
for (i = 0; i < N_ENVD_SENSORS; ++i) {
sensorp = envd_sensors[i];
}
}
}
/*
* Open fan devices and initialize per fan data structure.
* Returns #fans found.
*/
static int
envd_setup_fans(void)
{
int i, fd;
int fancnt = 0;
/* Make sure cpu0/1 present for validating cpu fans */
PICL_SUCCESS) {
if (env_debug) {
"get node by path failed for %s\n",
}
continue;
}
}
PICL_SUCCESS) {
if (env_debug) {
"get node by path failed for %s\n", CPU0_PATH);
}
continue;
}
}
if (fd == -1) {
continue;
}
}
return (fancnt);
}
static int
envd_setup_disks(void)
{
&tnodeh) != PICL_SUCCESS) {
if (env_debug)
"On-Board SCSI controller %s not found in the system.\n",
monitor_disk_temp = 0;
return (-1);
}
sizeof (vendor_id))) != 0) {
if (env_debug)
"Error in getting vendor-id for SCSI controller. ret = %d errno = 0x%d\n",
monitor_disk_temp = 0;
return (-1);
}
if (env_debug)
"Error in getting device-id for SCSI controller. ret = %d errno = 0x%d\n",
monitor_disk_temp = 0;
return (-1);
}
/*
* We have found LSI1064 SCSi controller onboard.
*/
&tnodeh) != PICL_SUCCESS) {
if (env_debug)
"DISK %d: %s not found in the system.\n",
continue;
}
if (env_debug)
"Error in opening %s errno = 0x%x\n",
continue;
}
diskp->warning_tstamp = 0;
diskp->shutdown_tstamp = 0;
/*
* Find out if the Temperature page is supported by the disk.
*/
if (ret != 0)
continue;
for (page_index = LOGPAGEHDRSIZE;
switch (log_page[page_index]) {
case TEMPERATURE_PAGE:
if (env_debug)
"tpage supported for %s\n",
default:
break;
}
}
if (ret != 0) {
if (env_debug) {
" error reading temperature of:%s\n",
}
} else {
if (env_debug) {
}
}
}
return (0);
}
/*
* Open temperature sensor devices and initialize per sensor data structure.
* Returns #sensors found.
*/
static int
envd_setup_sensors(void)
{
int sensorcnt = 0;
int i;
for (i = 0; i < N_ENVD_SENSORS; ++i) {
if (env_debug)
sensorp = envd_sensors[i];
/* Initialize sensor's initial state */
sensorp->warning_tstamp = 0;
sensorp->shutdown_tstamp = 0;
/* Make sure cpu0/1 sensors are present */
PICL_SUCCESS) {
if (env_debug) {
"get node by path failed for %s\n",
}
continue;
}
}
PICL_SUCCESS) {
if (env_debug) {
"get node by path failed for %s\n",
}
continue;
}
}
if (env_debug) {
}
continue;
}
/*
* Determine if the front panel is attached, we want the
* information if it exists, but should not shut down
* the system if it is removed.
*/
int retries;
retries++) {
PIC_GET_TEMPERATURE, temp) != 0)) {
continue;
}
}
if (retries == MAX_SENSOR_RETRIES) {
front_panel_present = 0;
}
}
sensorcnt++;
}
return (sensorcnt);
}
/* ARGSUSED */
static void *
{
int pre_lpstate;
int env_monitor_fd;
cur_lpstate = 0;
pre_lpstate = 1;
if (pm_fd == -1) {
return (NULL);
}
for (;;) {
/*
* Get PM state change events to check if the system
* is in lowest power state and inform PIC which controls
* fan speeds.
*
* To minimize polling, we use the blocking interface
* to get the power state change event here.
*/
break;
continue;
}
do {
if (env_debug) {
"pmstate event:0x%x flags:%x"
"comp:%d oldval:%d newval:%d path:%s\n",
}
if (pre_lpstate != cur_lpstate) {
if (env_debug)
"setting PIC ESTAR SATE to %x\n",
if (env_monitor_fd != -1) {
&estar_state) < 0) {
if (env_debug)
"unable to set ESTAR_MODE in PIC\n");
}
(void) close(env_monitor_fd);
} else {
if (env_debug)
"Failed to open %s\n",
}
}
}
/*NOTREACHED*/
return (NULL);
}
/*
* This is env thread which monitors the current temperature when
* warning threshold is exceeded. The job is to make sure it does
* forced shutdown to avoid reaching hardware poweroff via THERM interrupt.
*/
/*ARGSUSED*/
static void *
system_temp_thr(void *args)
{
int ret, i;
/*
* If front panel is not present, ignore readings from the
* corresponding sensor. We do this by setting the thresholds
* to the maximum (and minimum) values possible.
*/
if (!front_panel_present) {
}
for (;;) {
/*
* Sleep for specified seconds before issuing IOCTL
* again.
*/
(void) pthread_mutex_lock(&env_monitor_mutex);
&env_monitor_mutex, &to);
(void) pthread_mutex_unlock(&env_monitor_mutex);
continue;
}
(void) pthread_mutex_unlock(&env_monitor_mutex);
for (i = 0; i < N_ENVD_SENSORS; i++) {
sensorp = envd_sensors[i];
continue;
continue;
if (env_debug) {
"%s temp = %d",
}
/*
* If this sensor already triggered system shutdown, don't
*/
if (sensorp->shutdown_initiated)
continue;
/*
* Check for the temperature in warning and shutdown range
* and take appropriate action.
*/
sensorp)) {
/*
* Check if the temperature has been in warning
* range during last sensor_warning_duration interval.
* If so, the temperature is truly in warning
* range and we need to log a warning message,
* but no more than once every sensor_warning_interval
* seconds.
*/
if (sensorp->warning_start == 0)
}
} else if (sensorp->warning_start != 0)
sensorp->warning_start = 0;
if (!shutdown_override &&
sensorp)) {
if (sensorp->shutdown_tstamp == 0)
/*
* Shutdown the system if the temperature remains
* in the shutdown range for over
* sensor_shutdown_interval seconds.
*/
/* log error */
/* shutdown the system (only once) */
if (system_shutdown_started ==
B_FALSE) {
sizeof (syscmd),
"%s \"%s\"", shutdown_cmd,
msgbuf);
}
}
} else if (sensorp->shutdown_tstamp != 0)
sensorp->shutdown_tstamp = 0;
}
} /* end of forever loop */
/*NOTREACHED*/
return (NULL);
}
static int
{
struct scsi_extended_sense sense_buf;
int ret_val;
cdb_buf[0] = SCMD_LOG_SENSE_G1;
if (env_debug)
"log sense command for page_code 0x%x succeeded\n", page_code);
return (ret_val);
}
if (env_debug)
"log sense command for %s failed. page_code 0x%x ret_val = 0x%x status = 0x%x" \
return (1);
}
static int
{
int ret;
if (ret != 0) {
return (-1);
}
/*
* For the current temperature verify that the parameter
* length is 0x02 and the parameter code is 0x00
* Temperature value of 255(0xFF) is considered INVALID.
*/
return (-1);
} else {
}
}
/*
* For the reference temperature verify that the parameter
* length is 0x02 and the parameter code is 0x01
* Temperature value of 255(0xFF) is considered INVALID.
*/
} else {
}
}
return (0);
}
/* ARGSUSED */
static void *
disk_temp_thr(void *args)
{
int ret, i;
int idle_time;
int disk_pm_fd;
if (disk_pm_fd == -1) {
return (NULL);
}
for (;;) {
/*
* Sleep for specified seconds before issuing IOCTL
* again.
*/
(void) pthread_mutex_lock(&env_monitor_mutex);
&env_monitor_mutex, &to);
(void) pthread_mutex_unlock(
continue;
}
(void) pthread_mutex_unlock(&env_monitor_mutex);
continue;
continue;
/*
* If the disk temperature is above the warning threshold
* continue monitoring until the temperature drops below
* warning threshold.
* if the temperature is in the NORMAL range monitor only
* when the disk is BUSY.
* We do not want to read the disk temperature if the disk is
* is idling. The reason for this is disk will never get into
* lowest power mode if we scan the disk temperature
* peridoically. To avoid this situation we first determine
* the idle_time of the disk. If the disk has been IDLE since
* we scanned the temperature last time we will not read the
* temperature.
*/
if ((idle_time =
&pmstate)) == -1) {
if (env_debug)
"ioctl PM_GET_TIME_IDLE failed for DISK0. errno=0x%x\n",
errno);
continue;
}
continue;
}
if (env_debug) {
}
continue;
}
}
if (ret != 0)
continue;
if (env_debug) {
}
/*
* If this disk already triggered system shutdown, don't
*/
if (diskp->shutdown_initiated)
continue;
/*
* Check for the temperature in warning and shutdown range
* and take appropriate action.
*/
/*
* Check if the temperature has been in warning
* range during last disk_warning_duration interval.
* If so, the temperature is truly in warning
* range and we need to log a warning message,
* but no more than once every disk_warning_interval
* seconds.
*/
if (diskp->warning_start == 0)
disk_warning_duration) && (wtstamp == 0 ||
}
} else if (diskp->warning_start != 0)
diskp->warning_start = 0;
if (!shutdown_override &&
if (diskp->shutdown_tstamp == 0)
/*
* Shutdown the system if the temperature remains
* in the shutdown range for over disk_shutdown_interval
* seconds.
*/
/* log error */
/* shutdown the system (only once) */
if (system_shutdown_started == B_FALSE) {
}
}
} else if (diskp->shutdown_tstamp != 0)
diskp->shutdown_tstamp = 0;
}
} /* end of forever loop */
}
static void *
{
int ret, i;
#ifdef __lint
#endif
for (;;) {
/*
* Sleep for specified seconds before issuing IOCTL
* again.
*/
(void) pthread_mutex_lock(&env_monitor_mutex);
&env_monitor_mutex, &to);
(void) pthread_mutex_unlock(&env_monitor_mutex);
continue;
}
(void) pthread_mutex_unlock(&env_monitor_mutex);
continue;
/*
* We initiate shutdown if fan status indicates
* failure. Also, don't warn repeatedly.
*/
continue;
} else {
continue;
}
}
}
/*NOTREACHED*/
return (NULL);
}
/*
* Setup envrionmental monitor state and start threads to monitor
* temperature, fan, disk and power management state.
* Returns -1 on error, 0 if successful.
*/
static int
envd_setup(void)
{
env_debug = 1;
if (pthread_attr_init(&thr_attr) != 0 ||
return (-1);
}
/*
* Setup temperature sensors and fail if we can't open
* at least one sensor.
*/
if (envd_setup_sensors() <= 0) {
if (env_debug)
/*
* return (NULL);
*/
system_temp_monitor = 0;
}
/*
* Setup fan device (don't fail even if we can't access
* the fan as we can still monitor temeperature.
*/
(void) envd_setup_fans();
/*
* Environmental monitoring of SATA disks isn't ready yet.
*/
(void) envd_setup_disks();
system_temp_thr, NULL) != 0)
else {
if (env_debug)
"Created thread to monitor system temperatures\n");
}
}
NULL) != 0)
else {
if (env_debug)
"Created thread to monitor system fans\n");
}
}
/*
* Create a thread to monitor PM state
*/
NULL) != 0)
else {
if (env_debug)
"Created thread to monitor system power state\n");
}
}
if (monitor_disk_temp) {
if (disk_temp_thr_created == B_FALSE) {
disk_temp_thr, NULL) != 0)
else {
if (env_debug)
"Created thread for disk temperatures\n");
}
}
}
return (0);
}
static void
piclenvd_register(void)
{
}
static void
piclenvd_init(void)
{
(void) env_picl_setup_tuneables();
/*
* Setup the environmental data structures
*/
if (envd_setup() != 0) {
return;
}
/*
*/
}
static void
piclenvd_fini(void)
{
/*
* Invoke env_picl_destroy() to remove any PICL nodes/properties
* (including volatile properties) we created. Once this call
* returns, there can't be any more calls from the PICL framework
* to get current temperature or fan speed.
*/
}
/*VARARGS2*/
void
{
}
/*
* Tunables support functions
*/
static env_tuneable_t *
{
int i;
for (i = 0; i < ntuneables; i++) {
return (tuneablep);
}
return (NULL);
}
static int
{
return (PICL_FAILURE);
return (PICL_SUCCESS);
}
static int
{
return (PICL_PERMDENIED);
return (PICL_FAILURE);
return (PICL_SUCCESS);
}
static int
{
return (PICL_FAILURE);
return (PICL_SUCCESS);
}
static int
{
return (PICL_PERMDENIED);
return (PICL_FAILURE);
return (PICL_SUCCESS);
}
{
int retry_count;
int real_tach;
return (B_TRUE);
/*
* Read RF_FAN_STATUS bit of the fan fault register, retry if
* the PIC is busy, with a 1 second delay to allow it to update.
*/
while (retry_count > 0) {
retry_count--;
(void) sleep(1);
continue;
} else {
if (status & 0x1) {
retry_count--;
(void) sleep(1);
continue;
}
break;
}
}
if (retry_count != MAX_RETRIES_FOR_FAN_FAULT) {
if (env_debug) {
"%d retries attempted in reading fan status.\n",
}
}
if (retry_count == 0) {
sizeof (fan_status_string));
sizeof (fan_rpm_string));
return (B_TRUE);
}
if (env_debug)
/*
* ST_FFAULT bit isn't implemented yet and we're reading only
* individual fan status
*/
if (status & 0x1) {
"0x%x", status);
sizeof (fan_rpm_string));
} else {
"%d", fan_speed);
}
return (B_TRUE);
}
return (B_FALSE);
}