/*
* A hwmon driver for the IBM System Director Active Energy Manager (AEM)
* temperature/power/energy sensors and capping functionality.
* Copyright (C) 2008 IBM
*
* Author: Darrick J. Wong <djwong@us.ibm.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/ipmi.h>
#include <linux/module.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/jiffies.h>
#include <linux/mutex.h>
#include <linux/kdev_t.h>
#include <linux/spinlock.h>
#include <linux/idr.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/platform_device.h>
#include <linux/math64.h>
#include <linux/time.h>
#define REFRESH_INTERVAL (HZ)
#define IPMI_TIMEOUT (30 * HZ)
#define DRVNAME "aem"
#define AEM_NETFN 0x2E
#define AEM_FIND_FW_CMD 0x80
#define AEM_ELEMENT_CMD 0x81
#define AEM_FW_INSTANCE_CMD 0x82
#define AEM_READ_ELEMENT_CFG 0x80
#define AEM_READ_BUFFER 0x81
#define AEM_READ_REGISTER 0x82
#define AEM_WRITE_REGISTER 0x83
#define AEM_SET_REG_MASK 0x84
#define AEM_CLEAR_REG_MASK 0x85
#define AEM_READ_ELEMENT_CFG2 0x86
#define AEM_CONTROL_ELEMENT 0
#define AEM_ENERGY_ELEMENT 1
#define AEM_CLOCK_ELEMENT 4
#define AEM_POWER_CAP_ELEMENT 7
#define AEM_EXHAUST_ELEMENT 9
#define AEM_POWER_ELEMENT 10
#define AEM_MODULE_TYPE_ID 0x0001
#define AEM2_NUM_ENERGY_REGS 2
#define AEM2_NUM_PCAP_REGS 6
#define AEM2_NUM_TEMP_REGS 2
#define AEM2_NUM_SENSORS 14
#define AEM1_NUM_ENERGY_REGS 1
#define AEM1_NUM_SENSORS 3
/* AEM 2.x has more energy registers */
#define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
/* AEM 2.x needs more sensor files */
#define AEM_NUM_SENSORS AEM2_NUM_SENSORS
#define POWER_CAP 0
#define POWER_CAP_MAX_HOTPLUG 1
#define POWER_CAP_MAX 2
#define POWER_CAP_MIN_WARNING 3
#define POWER_CAP_MIN 4
#define POWER_AUX 5
#define AEM_DEFAULT_POWER_INTERVAL 1000
#define AEM_MIN_POWER_INTERVAL 200
#define UJ_PER_MJ 1000L
static DEFINE_IDR(aem_idr);
static DEFINE_SPINLOCK(aem_idr_lock);
static struct platform_driver aem_driver = {
.driver = {
.name = DRVNAME,
.bus = &platform_bus_type,
}
};
struct aem_ipmi_data {
struct completion read_complete;
struct ipmi_addr address;
ipmi_user_t user;
int interface;
struct kernel_ipmi_msg tx_message;
long tx_msgid;
void *rx_msg_data;
unsigned short rx_msg_len;
unsigned char rx_result;
int rx_recv_type;
struct device *bmc_device;
};
struct aem_ro_sensor_template {
char *label;
ssize_t (*show)(struct device *dev,
struct device_attribute *devattr,
char *buf);
int index;
};
struct aem_rw_sensor_template {
char *label;
ssize_t (*show)(struct device *dev,
struct device_attribute *devattr,
char *buf);
ssize_t (*set)(struct device *dev,
struct device_attribute *devattr,
const char *buf, size_t count);
int index;
};
struct aem_data {
struct list_head list;
struct device *hwmon_dev;
struct platform_device *pdev;
struct mutex lock;
char valid;
unsigned long last_updated; /* In jiffies */
u8 ver_major;
u8 ver_minor;
u8 module_handle;
int id;
struct aem_ipmi_data ipmi;
/* Function to update sensors */
void (*update)(struct aem_data *data);
/*
* AEM 1.x sensors:
* Available sensors:
* Energy meter
* Power meter
*
* AEM 2.x sensors:
* Two energy meters
* Two power meters
* Two temperature sensors
* Six power cap registers
*/
/* sysfs attrs */
struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
/* energy use in mJ */
u64 energy[AEM_NUM_ENERGY_REGS];
/* power sampling interval in ms */
unsigned long power_period[AEM_NUM_ENERGY_REGS];
/* Everything past here is for AEM2 only */
/* power caps in dW */
u16 pcap[AEM2_NUM_PCAP_REGS];
/* exhaust temperature in C */
u8 temp[AEM2_NUM_TEMP_REGS];
};
/* Data structures returned by the AEM firmware */
struct aem_iana_id {
u8 bytes[3];
};
static struct aem_iana_id system_x_id = {
.bytes = {0x4D, 0x4F, 0x00}
};
/* These are used to find AEM1 instances */
struct aem_find_firmware_req {
struct aem_iana_id id;
u8 rsvd;
__be16 index;
__be16 module_type_id;