Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1 | /* |
Darrick J. Wong | eb93b7d | 2008-08-15 00:40:40 -0700 | [diff] [blame] | 2 | * A hwmon driver for the IBM System Director Active Energy Manager (AEM) |
| 3 | * temperature/power/energy sensors and capping functionality. |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 4 | * Copyright (C) 2008 IBM |
| 5 | * |
| 6 | * Author: Darrick J. Wong <djwong@us.ibm.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
Joe Perches | 55d705c | 2010-10-20 06:51:38 +0000 | [diff] [blame] | 23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 24 | |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 25 | #include <linux/ipmi.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/jiffies.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <linux/kdev_t.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/platform_device.h> |
| 37 | #include <linux/math64.h> |
| 38 | #include <linux/time.h> |
Jean Delvare | fa84574 | 2012-10-10 15:25:56 +0200 | [diff] [blame] | 39 | #include <linux/err.h> |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 40 | |
| 41 | #define REFRESH_INTERVAL (HZ) |
| 42 | #define IPMI_TIMEOUT (30 * HZ) |
| 43 | #define DRVNAME "aem" |
| 44 | |
| 45 | #define AEM_NETFN 0x2E |
| 46 | |
| 47 | #define AEM_FIND_FW_CMD 0x80 |
| 48 | #define AEM_ELEMENT_CMD 0x81 |
| 49 | #define AEM_FW_INSTANCE_CMD 0x82 |
| 50 | |
| 51 | #define AEM_READ_ELEMENT_CFG 0x80 |
| 52 | #define AEM_READ_BUFFER 0x81 |
| 53 | #define AEM_READ_REGISTER 0x82 |
| 54 | #define AEM_WRITE_REGISTER 0x83 |
| 55 | #define AEM_SET_REG_MASK 0x84 |
| 56 | #define AEM_CLEAR_REG_MASK 0x85 |
| 57 | #define AEM_READ_ELEMENT_CFG2 0x86 |
| 58 | |
| 59 | #define AEM_CONTROL_ELEMENT 0 |
| 60 | #define AEM_ENERGY_ELEMENT 1 |
| 61 | #define AEM_CLOCK_ELEMENT 4 |
| 62 | #define AEM_POWER_CAP_ELEMENT 7 |
| 63 | #define AEM_EXHAUST_ELEMENT 9 |
| 64 | #define AEM_POWER_ELEMENT 10 |
| 65 | |
| 66 | #define AEM_MODULE_TYPE_ID 0x0001 |
| 67 | |
| 68 | #define AEM2_NUM_ENERGY_REGS 2 |
| 69 | #define AEM2_NUM_PCAP_REGS 6 |
| 70 | #define AEM2_NUM_TEMP_REGS 2 |
| 71 | #define AEM2_NUM_SENSORS 14 |
| 72 | |
| 73 | #define AEM1_NUM_ENERGY_REGS 1 |
| 74 | #define AEM1_NUM_SENSORS 3 |
| 75 | |
| 76 | /* AEM 2.x has more energy registers */ |
| 77 | #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS |
| 78 | /* AEM 2.x needs more sensor files */ |
| 79 | #define AEM_NUM_SENSORS AEM2_NUM_SENSORS |
| 80 | |
| 81 | #define POWER_CAP 0 |
| 82 | #define POWER_CAP_MAX_HOTPLUG 1 |
| 83 | #define POWER_CAP_MAX 2 |
| 84 | #define POWER_CAP_MIN_WARNING 3 |
| 85 | #define POWER_CAP_MIN 4 |
| 86 | #define POWER_AUX 5 |
| 87 | |
| 88 | #define AEM_DEFAULT_POWER_INTERVAL 1000 |
| 89 | #define AEM_MIN_POWER_INTERVAL 200 |
| 90 | #define UJ_PER_MJ 1000L |
| 91 | |
Jonathan Cameron | 6580704 | 2011-10-31 17:10:27 -0700 | [diff] [blame] | 92 | static DEFINE_IDA(aem_ida); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 93 | |
Darrick J. Wong | fe2d5ff | 2008-11-12 13:25:00 -0800 | [diff] [blame] | 94 | static struct platform_driver aem_driver = { |
| 95 | .driver = { |
| 96 | .name = DRVNAME, |
| 97 | .bus = &platform_bus_type, |
| 98 | } |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct aem_ipmi_data { |
| 102 | struct completion read_complete; |
| 103 | struct ipmi_addr address; |
| 104 | ipmi_user_t user; |
| 105 | int interface; |
| 106 | |
| 107 | struct kernel_ipmi_msg tx_message; |
| 108 | long tx_msgid; |
| 109 | |
| 110 | void *rx_msg_data; |
| 111 | unsigned short rx_msg_len; |
| 112 | unsigned char rx_result; |
| 113 | int rx_recv_type; |
| 114 | |
| 115 | struct device *bmc_device; |
| 116 | }; |
| 117 | |
| 118 | struct aem_ro_sensor_template { |
| 119 | char *label; |
| 120 | ssize_t (*show)(struct device *dev, |
| 121 | struct device_attribute *devattr, |
| 122 | char *buf); |
| 123 | int index; |
| 124 | }; |
| 125 | |
| 126 | struct aem_rw_sensor_template { |
| 127 | char *label; |
| 128 | ssize_t (*show)(struct device *dev, |
| 129 | struct device_attribute *devattr, |
| 130 | char *buf); |
| 131 | ssize_t (*set)(struct device *dev, |
| 132 | struct device_attribute *devattr, |
| 133 | const char *buf, size_t count); |
| 134 | int index; |
| 135 | }; |
| 136 | |
| 137 | struct aem_data { |
| 138 | struct list_head list; |
| 139 | |
| 140 | struct device *hwmon_dev; |
| 141 | struct platform_device *pdev; |
| 142 | struct mutex lock; |
| 143 | char valid; |
| 144 | unsigned long last_updated; /* In jiffies */ |
| 145 | u8 ver_major; |
| 146 | u8 ver_minor; |
| 147 | u8 module_handle; |
| 148 | int id; |
| 149 | struct aem_ipmi_data ipmi; |
| 150 | |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 151 | /* Function and buffer to update sensors */ |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 152 | void (*update)(struct aem_data *data); |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 153 | struct aem_read_sensor_resp *rs_resp; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * AEM 1.x sensors: |
| 157 | * Available sensors: |
| 158 | * Energy meter |
| 159 | * Power meter |
| 160 | * |
| 161 | * AEM 2.x sensors: |
| 162 | * Two energy meters |
| 163 | * Two power meters |
| 164 | * Two temperature sensors |
| 165 | * Six power cap registers |
| 166 | */ |
| 167 | |
| 168 | /* sysfs attrs */ |
| 169 | struct sensor_device_attribute sensors[AEM_NUM_SENSORS]; |
| 170 | |
| 171 | /* energy use in mJ */ |
| 172 | u64 energy[AEM_NUM_ENERGY_REGS]; |
| 173 | |
| 174 | /* power sampling interval in ms */ |
| 175 | unsigned long power_period[AEM_NUM_ENERGY_REGS]; |
| 176 | |
| 177 | /* Everything past here is for AEM2 only */ |
| 178 | |
| 179 | /* power caps in dW */ |
| 180 | u16 pcap[AEM2_NUM_PCAP_REGS]; |
| 181 | |
| 182 | /* exhaust temperature in C */ |
| 183 | u8 temp[AEM2_NUM_TEMP_REGS]; |
| 184 | }; |
| 185 | |
| 186 | /* Data structures returned by the AEM firmware */ |
| 187 | struct aem_iana_id { |
| 188 | u8 bytes[3]; |
| 189 | }; |
| 190 | static struct aem_iana_id system_x_id = { |
| 191 | .bytes = {0x4D, 0x4F, 0x00} |
| 192 | }; |
| 193 | |
| 194 | /* These are used to find AEM1 instances */ |
| 195 | struct aem_find_firmware_req { |
| 196 | struct aem_iana_id id; |
| 197 | u8 rsvd; |
Al Viro | a064d5b | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 198 | __be16 index; |
| 199 | __be16 module_type_id; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 200 | } __packed; |
| 201 | |
| 202 | struct aem_find_firmware_resp { |
| 203 | struct aem_iana_id id; |
| 204 | u8 num_instances; |
| 205 | } __packed; |
| 206 | |
| 207 | /* These are used to find AEM2 instances */ |
| 208 | struct aem_find_instance_req { |
| 209 | struct aem_iana_id id; |
| 210 | u8 instance_number; |
Al Viro | a064d5b | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 211 | __be16 module_type_id; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 212 | } __packed; |
| 213 | |
| 214 | struct aem_find_instance_resp { |
| 215 | struct aem_iana_id id; |
| 216 | u8 num_instances; |
| 217 | u8 major; |
| 218 | u8 minor; |
| 219 | u8 module_handle; |
| 220 | u16 record_id; |
| 221 | } __packed; |
| 222 | |
| 223 | /* These are used to query sensors */ |
| 224 | struct aem_read_sensor_req { |
| 225 | struct aem_iana_id id; |
| 226 | u8 module_handle; |
| 227 | u8 element; |
| 228 | u8 subcommand; |
| 229 | u8 reg; |
| 230 | u8 rx_buf_size; |
| 231 | } __packed; |
| 232 | |
| 233 | struct aem_read_sensor_resp { |
| 234 | struct aem_iana_id id; |
| 235 | u8 bytes[0]; |
| 236 | } __packed; |
| 237 | |
| 238 | /* Data structures to talk to the IPMI layer */ |
| 239 | struct aem_driver_data { |
| 240 | struct list_head aem_devices; |
| 241 | struct ipmi_smi_watcher bmc_events; |
| 242 | struct ipmi_user_hndl ipmi_hndlrs; |
| 243 | }; |
| 244 | |
| 245 | static void aem_register_bmc(int iface, struct device *dev); |
| 246 | static void aem_bmc_gone(int iface); |
| 247 | static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data); |
| 248 | |
| 249 | static void aem_remove_sensors(struct aem_data *data); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 250 | static int aem1_find_sensors(struct aem_data *data); |
| 251 | static int aem2_find_sensors(struct aem_data *data); |
| 252 | static void update_aem1_sensors(struct aem_data *data); |
| 253 | static void update_aem2_sensors(struct aem_data *data); |
| 254 | |
| 255 | static struct aem_driver_data driver_data = { |
| 256 | .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices), |
| 257 | .bmc_events = { |
| 258 | .owner = THIS_MODULE, |
| 259 | .new_smi = aem_register_bmc, |
| 260 | .smi_gone = aem_bmc_gone, |
| 261 | }, |
| 262 | .ipmi_hndlrs = { |
| 263 | .ipmi_recv_hndl = aem_msg_handler, |
| 264 | }, |
| 265 | }; |
| 266 | |
| 267 | /* Functions to talk to the IPMI layer */ |
| 268 | |
| 269 | /* Initialize IPMI address, message buffers and user data */ |
| 270 | static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface, |
| 271 | struct device *bmc) |
| 272 | { |
| 273 | int err; |
| 274 | |
| 275 | init_completion(&data->read_complete); |
| 276 | data->bmc_device = bmc; |
| 277 | |
| 278 | /* Initialize IPMI address */ |
| 279 | data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; |
| 280 | data->address.channel = IPMI_BMC_CHANNEL; |
| 281 | data->address.data[0] = 0; |
| 282 | data->interface = iface; |
| 283 | |
| 284 | /* Initialize message buffers */ |
| 285 | data->tx_msgid = 0; |
| 286 | data->tx_message.netfn = AEM_NETFN; |
| 287 | |
| 288 | /* Create IPMI messaging interface user */ |
| 289 | err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs, |
| 290 | data, &data->user); |
| 291 | if (err < 0) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 292 | dev_err(bmc, |
| 293 | "Unable to register user with IPMI interface %d\n", |
| 294 | data->interface); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 295 | return -EACCES; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /* Send an IPMI command */ |
| 302 | static int aem_send_message(struct aem_ipmi_data *data) |
| 303 | { |
| 304 | int err; |
| 305 | |
| 306 | err = ipmi_validate_addr(&data->address, sizeof(data->address)); |
| 307 | if (err) |
| 308 | goto out; |
| 309 | |
| 310 | data->tx_msgid++; |
| 311 | err = ipmi_request_settime(data->user, &data->address, data->tx_msgid, |
| 312 | &data->tx_message, data, 0, 0, 0); |
| 313 | if (err) |
| 314 | goto out1; |
| 315 | |
| 316 | return 0; |
| 317 | out1: |
| 318 | dev_err(data->bmc_device, "request_settime=%x\n", err); |
| 319 | return err; |
| 320 | out: |
| 321 | dev_err(data->bmc_device, "validate_addr=%x\n", err); |
| 322 | return err; |
| 323 | } |
| 324 | |
| 325 | /* Dispatch IPMI messages to callers */ |
| 326 | static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data) |
| 327 | { |
| 328 | unsigned short rx_len; |
| 329 | struct aem_ipmi_data *data = user_msg_data; |
| 330 | |
| 331 | if (msg->msgid != data->tx_msgid) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 332 | dev_err(data->bmc_device, |
| 333 | "Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n", |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 334 | (int)msg->msgid, |
| 335 | (int)data->tx_msgid); |
| 336 | ipmi_free_recv_msg(msg); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | data->rx_recv_type = msg->recv_type; |
| 341 | if (msg->msg.data_len > 0) |
| 342 | data->rx_result = msg->msg.data[0]; |
| 343 | else |
| 344 | data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE; |
| 345 | |
| 346 | if (msg->msg.data_len > 1) { |
| 347 | rx_len = msg->msg.data_len - 1; |
| 348 | if (data->rx_msg_len < rx_len) |
| 349 | rx_len = data->rx_msg_len; |
| 350 | data->rx_msg_len = rx_len; |
| 351 | memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len); |
| 352 | } else |
| 353 | data->rx_msg_len = 0; |
| 354 | |
| 355 | ipmi_free_recv_msg(msg); |
| 356 | complete(&data->read_complete); |
| 357 | } |
| 358 | |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 359 | /* Sensor support functions */ |
| 360 | |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 361 | /* Read a sensor value; must be called with data->lock held */ |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 362 | static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg, |
| 363 | void *buf, size_t size) |
| 364 | { |
| 365 | int rs_size, res; |
| 366 | struct aem_read_sensor_req rs_req; |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 367 | /* Use preallocated rx buffer */ |
| 368 | struct aem_read_sensor_resp *rs_resp = data->rs_resp; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 369 | struct aem_ipmi_data *ipmi = &data->ipmi; |
| 370 | |
| 371 | /* AEM registers are 1, 2, 4 or 8 bytes */ |
| 372 | switch (size) { |
| 373 | case 1: |
| 374 | case 2: |
| 375 | case 4: |
| 376 | case 8: |
| 377 | break; |
| 378 | default: |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | rs_req.id = system_x_id; |
| 383 | rs_req.module_handle = data->module_handle; |
| 384 | rs_req.element = elt; |
| 385 | rs_req.subcommand = AEM_READ_REGISTER; |
| 386 | rs_req.reg = reg; |
| 387 | rs_req.rx_buf_size = size; |
| 388 | |
| 389 | ipmi->tx_message.cmd = AEM_ELEMENT_CMD; |
| 390 | ipmi->tx_message.data = (char *)&rs_req; |
| 391 | ipmi->tx_message.data_len = sizeof(rs_req); |
| 392 | |
| 393 | rs_size = sizeof(*rs_resp) + size; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 394 | ipmi->rx_msg_data = rs_resp; |
| 395 | ipmi->rx_msg_len = rs_size; |
| 396 | |
| 397 | aem_send_message(ipmi); |
| 398 | |
| 399 | res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT); |
Julia Lawall | 66a89b2 | 2011-08-09 11:10:56 -0400 | [diff] [blame] | 400 | if (!res) { |
| 401 | res = -ETIMEDOUT; |
| 402 | goto out; |
| 403 | } |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 404 | |
| 405 | if (ipmi->rx_result || ipmi->rx_msg_len != rs_size || |
| 406 | memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) { |
Julia Lawall | 66a89b2 | 2011-08-09 11:10:56 -0400 | [diff] [blame] | 407 | res = -ENOENT; |
| 408 | goto out; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | switch (size) { |
| 412 | case 1: { |
| 413 | u8 *x = buf; |
| 414 | *x = rs_resp->bytes[0]; |
| 415 | break; |
| 416 | } |
| 417 | case 2: { |
| 418 | u16 *x = buf; |
Al Viro | a064d5b | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 419 | *x = be16_to_cpup((__be16 *)rs_resp->bytes); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 420 | break; |
| 421 | } |
| 422 | case 4: { |
| 423 | u32 *x = buf; |
Al Viro | a064d5b | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 424 | *x = be32_to_cpup((__be32 *)rs_resp->bytes); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | case 8: { |
| 428 | u64 *x = buf; |
Al Viro | a064d5b | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 429 | *x = be64_to_cpup((__be64 *)rs_resp->bytes); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 430 | break; |
| 431 | } |
| 432 | } |
Julia Lawall | 66a89b2 | 2011-08-09 11:10:56 -0400 | [diff] [blame] | 433 | res = 0; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 434 | |
Julia Lawall | 66a89b2 | 2011-08-09 11:10:56 -0400 | [diff] [blame] | 435 | out: |
Julia Lawall | 66a89b2 | 2011-08-09 11:10:56 -0400 | [diff] [blame] | 436 | return res; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* Update AEM energy registers */ |
Darrick J. Wong | bb15e7f | 2008-08-15 00:40:41 -0700 | [diff] [blame] | 440 | static void update_aem_energy_one(struct aem_data *data, int which) |
| 441 | { |
| 442 | aem_read_sensor(data, AEM_ENERGY_ELEMENT, which, |
| 443 | &data->energy[which], 8); |
| 444 | } |
| 445 | |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 446 | static void update_aem_energy(struct aem_data *data) |
| 447 | { |
Darrick J. Wong | bb15e7f | 2008-08-15 00:40:41 -0700 | [diff] [blame] | 448 | update_aem_energy_one(data, 0); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 449 | if (data->ver_major < 2) |
| 450 | return; |
Darrick J. Wong | bb15e7f | 2008-08-15 00:40:41 -0700 | [diff] [blame] | 451 | update_aem_energy_one(data, 1); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* Update all AEM1 sensors */ |
| 455 | static void update_aem1_sensors(struct aem_data *data) |
| 456 | { |
| 457 | mutex_lock(&data->lock); |
| 458 | if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) && |
| 459 | data->valid) |
| 460 | goto out; |
| 461 | |
| 462 | update_aem_energy(data); |
| 463 | out: |
| 464 | mutex_unlock(&data->lock); |
| 465 | } |
| 466 | |
| 467 | /* Update all AEM2 sensors */ |
| 468 | static void update_aem2_sensors(struct aem_data *data) |
| 469 | { |
| 470 | int i; |
| 471 | |
| 472 | mutex_lock(&data->lock); |
| 473 | if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) && |
| 474 | data->valid) |
| 475 | goto out; |
| 476 | |
| 477 | update_aem_energy(data); |
| 478 | aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1); |
| 479 | aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1); |
| 480 | |
| 481 | for (i = POWER_CAP; i <= POWER_AUX; i++) |
| 482 | aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i, |
| 483 | &data->pcap[i], 2); |
| 484 | out: |
| 485 | mutex_unlock(&data->lock); |
| 486 | } |
| 487 | |
| 488 | /* Delete an AEM instance */ |
| 489 | static void aem_delete(struct aem_data *data) |
| 490 | { |
| 491 | list_del(&data->list); |
| 492 | aem_remove_sensors(data); |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 493 | kfree(data->rs_resp); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 494 | hwmon_device_unregister(data->hwmon_dev); |
| 495 | ipmi_destroy_user(data->ipmi.user); |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 496 | platform_set_drvdata(data->pdev, NULL); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 497 | platform_device_unregister(data->pdev); |
Jonathan Cameron | 6580704 | 2011-10-31 17:10:27 -0700 | [diff] [blame] | 498 | ida_simple_remove(&aem_ida, data->id); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 499 | kfree(data); |
| 500 | } |
| 501 | |
| 502 | /* Probe functions for AEM1 devices */ |
| 503 | |
| 504 | /* Retrieve version and module handle for an AEM1 instance */ |
| 505 | static int aem_find_aem1_count(struct aem_ipmi_data *data) |
| 506 | { |
| 507 | int res; |
| 508 | struct aem_find_firmware_req ff_req; |
| 509 | struct aem_find_firmware_resp ff_resp; |
| 510 | |
| 511 | ff_req.id = system_x_id; |
| 512 | ff_req.index = 0; |
| 513 | ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID); |
| 514 | |
| 515 | data->tx_message.cmd = AEM_FIND_FW_CMD; |
| 516 | data->tx_message.data = (char *)&ff_req; |
| 517 | data->tx_message.data_len = sizeof(ff_req); |
| 518 | |
| 519 | data->rx_msg_data = &ff_resp; |
| 520 | data->rx_msg_len = sizeof(ff_resp); |
| 521 | |
| 522 | aem_send_message(data); |
| 523 | |
| 524 | res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT); |
| 525 | if (!res) |
| 526 | return -ETIMEDOUT; |
| 527 | |
| 528 | if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) || |
| 529 | memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id))) |
| 530 | return -ENOENT; |
| 531 | |
| 532 | return ff_resp.num_instances; |
| 533 | } |
| 534 | |
| 535 | /* Find and initialize one AEM1 instance */ |
| 536 | static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle) |
| 537 | { |
| 538 | struct aem_data *data; |
| 539 | int i; |
| 540 | int res = -ENOMEM; |
| 541 | |
| 542 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 543 | if (!data) |
| 544 | return res; |
| 545 | mutex_init(&data->lock); |
| 546 | |
| 547 | /* Copy instance data */ |
| 548 | data->ver_major = 1; |
| 549 | data->ver_minor = 0; |
| 550 | data->module_handle = module_handle; |
| 551 | for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++) |
| 552 | data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL; |
| 553 | |
| 554 | /* Create sub-device for this fw instance */ |
Jonathan Cameron | 6580704 | 2011-10-31 17:10:27 -0700 | [diff] [blame] | 555 | data->id = ida_simple_get(&aem_ida, 0, 0, GFP_KERNEL); |
| 556 | if (data->id < 0) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 557 | goto id_err; |
| 558 | |
| 559 | data->pdev = platform_device_alloc(DRVNAME, data->id); |
| 560 | if (!data->pdev) |
| 561 | goto dev_err; |
Darrick J. Wong | fe2d5ff | 2008-11-12 13:25:00 -0800 | [diff] [blame] | 562 | data->pdev->dev.driver = &aem_driver.driver; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 563 | |
| 564 | res = platform_device_add(data->pdev); |
| 565 | if (res) |
| 566 | goto ipmi_err; |
| 567 | |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 568 | platform_set_drvdata(data->pdev, data); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 569 | |
| 570 | /* Set up IPMI interface */ |
Jean Delvare | 547a1c9 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 571 | res = aem_init_ipmi_data(&data->ipmi, probe->interface, |
| 572 | probe->bmc_device); |
| 573 | if (res) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 574 | goto ipmi_err; |
| 575 | |
| 576 | /* Register with hwmon */ |
| 577 | data->hwmon_dev = hwmon_device_register(&data->pdev->dev); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 578 | if (IS_ERR(data->hwmon_dev)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 579 | dev_err(&data->pdev->dev, |
| 580 | "Unable to register hwmon device for IPMI interface %d\n", |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 581 | probe->interface); |
Jean Delvare | 547a1c9 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 582 | res = PTR_ERR(data->hwmon_dev); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 583 | goto hwmon_reg_err; |
| 584 | } |
| 585 | |
| 586 | data->update = update_aem1_sensors; |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 587 | data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL); |
| 588 | if (!data->rs_resp) { |
| 589 | res = -ENOMEM; |
| 590 | goto alloc_resp_err; |
| 591 | } |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 592 | |
| 593 | /* Find sensors */ |
Jean Delvare | 547a1c9 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 594 | res = aem1_find_sensors(data); |
| 595 | if (res) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 596 | goto sensor_err; |
| 597 | |
| 598 | /* Add to our list of AEM devices */ |
| 599 | list_add_tail(&data->list, &driver_data.aem_devices); |
| 600 | |
| 601 | dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n", |
| 602 | data->ver_major, data->ver_minor, |
| 603 | data->module_handle); |
| 604 | return 0; |
| 605 | |
| 606 | sensor_err: |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 607 | kfree(data->rs_resp); |
| 608 | alloc_resp_err: |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 609 | hwmon_device_unregister(data->hwmon_dev); |
| 610 | hwmon_reg_err: |
| 611 | ipmi_destroy_user(data->ipmi.user); |
| 612 | ipmi_err: |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 613 | platform_set_drvdata(data->pdev, NULL); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 614 | platform_device_unregister(data->pdev); |
| 615 | dev_err: |
Jonathan Cameron | 6580704 | 2011-10-31 17:10:27 -0700 | [diff] [blame] | 616 | ida_simple_remove(&aem_ida, data->id); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 617 | id_err: |
| 618 | kfree(data); |
| 619 | |
| 620 | return res; |
| 621 | } |
| 622 | |
| 623 | /* Find and initialize all AEM1 instances */ |
Jean Delvare | 9d84c9e | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 624 | static void aem_init_aem1(struct aem_ipmi_data *probe) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 625 | { |
| 626 | int num, i, err; |
| 627 | |
| 628 | num = aem_find_aem1_count(probe); |
| 629 | for (i = 0; i < num; i++) { |
| 630 | err = aem_init_aem1_inst(probe, i); |
| 631 | if (err) { |
| 632 | dev_err(probe->bmc_device, |
| 633 | "Error %d initializing AEM1 0x%X\n", |
| 634 | err, i); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /* Probe functions for AEM2 devices */ |
| 640 | |
| 641 | /* Retrieve version and module handle for an AEM2 instance */ |
| 642 | static int aem_find_aem2(struct aem_ipmi_data *data, |
| 643 | struct aem_find_instance_resp *fi_resp, |
| 644 | int instance_num) |
| 645 | { |
| 646 | int res; |
| 647 | struct aem_find_instance_req fi_req; |
| 648 | |
| 649 | fi_req.id = system_x_id; |
| 650 | fi_req.instance_number = instance_num; |
| 651 | fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID); |
| 652 | |
| 653 | data->tx_message.cmd = AEM_FW_INSTANCE_CMD; |
| 654 | data->tx_message.data = (char *)&fi_req; |
| 655 | data->tx_message.data_len = sizeof(fi_req); |
| 656 | |
| 657 | data->rx_msg_data = fi_resp; |
| 658 | data->rx_msg_len = sizeof(*fi_resp); |
| 659 | |
| 660 | aem_send_message(data); |
| 661 | |
| 662 | res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT); |
| 663 | if (!res) |
| 664 | return -ETIMEDOUT; |
| 665 | |
| 666 | if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) || |
Darrick J. Wong | 9c5413e | 2008-08-15 00:40:40 -0700 | [diff] [blame] | 667 | memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) || |
| 668 | fi_resp->num_instances <= instance_num) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 669 | return -ENOENT; |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | /* Find and initialize one AEM2 instance */ |
| 675 | static int aem_init_aem2_inst(struct aem_ipmi_data *probe, |
| 676 | struct aem_find_instance_resp *fi_resp) |
| 677 | { |
| 678 | struct aem_data *data; |
| 679 | int i; |
| 680 | int res = -ENOMEM; |
| 681 | |
| 682 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 683 | if (!data) |
| 684 | return res; |
| 685 | mutex_init(&data->lock); |
| 686 | |
| 687 | /* Copy instance data */ |
| 688 | data->ver_major = fi_resp->major; |
| 689 | data->ver_minor = fi_resp->minor; |
| 690 | data->module_handle = fi_resp->module_handle; |
| 691 | for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++) |
| 692 | data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL; |
| 693 | |
| 694 | /* Create sub-device for this fw instance */ |
Jonathan Cameron | 6580704 | 2011-10-31 17:10:27 -0700 | [diff] [blame] | 695 | data->id = ida_simple_get(&aem_ida, 0, 0, GFP_KERNEL); |
| 696 | if (data->id < 0) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 697 | goto id_err; |
| 698 | |
| 699 | data->pdev = platform_device_alloc(DRVNAME, data->id); |
| 700 | if (!data->pdev) |
| 701 | goto dev_err; |
Darrick J. Wong | fe2d5ff | 2008-11-12 13:25:00 -0800 | [diff] [blame] | 702 | data->pdev->dev.driver = &aem_driver.driver; |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 703 | |
| 704 | res = platform_device_add(data->pdev); |
| 705 | if (res) |
| 706 | goto ipmi_err; |
| 707 | |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 708 | platform_set_drvdata(data->pdev, data); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 709 | |
| 710 | /* Set up IPMI interface */ |
Jean Delvare | 547a1c9 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 711 | res = aem_init_ipmi_data(&data->ipmi, probe->interface, |
| 712 | probe->bmc_device); |
| 713 | if (res) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 714 | goto ipmi_err; |
| 715 | |
| 716 | /* Register with hwmon */ |
| 717 | data->hwmon_dev = hwmon_device_register(&data->pdev->dev); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 718 | if (IS_ERR(data->hwmon_dev)) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 719 | dev_err(&data->pdev->dev, |
| 720 | "Unable to register hwmon device for IPMI interface %d\n", |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 721 | probe->interface); |
Jean Delvare | 547a1c9 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 722 | res = PTR_ERR(data->hwmon_dev); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 723 | goto hwmon_reg_err; |
| 724 | } |
| 725 | |
| 726 | data->update = update_aem2_sensors; |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 727 | data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL); |
| 728 | if (!data->rs_resp) { |
| 729 | res = -ENOMEM; |
| 730 | goto alloc_resp_err; |
| 731 | } |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 732 | |
| 733 | /* Find sensors */ |
Jean Delvare | 547a1c9 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 734 | res = aem2_find_sensors(data); |
| 735 | if (res) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 736 | goto sensor_err; |
| 737 | |
| 738 | /* Add to our list of AEM devices */ |
| 739 | list_add_tail(&data->list, &driver_data.aem_devices); |
| 740 | |
| 741 | dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n", |
| 742 | data->ver_major, data->ver_minor, |
| 743 | data->module_handle); |
| 744 | return 0; |
| 745 | |
| 746 | sensor_err: |
Jean Delvare | da8ebe4 | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 747 | kfree(data->rs_resp); |
| 748 | alloc_resp_err: |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 749 | hwmon_device_unregister(data->hwmon_dev); |
| 750 | hwmon_reg_err: |
| 751 | ipmi_destroy_user(data->ipmi.user); |
| 752 | ipmi_err: |
Jean Delvare | 95de3b2 | 2011-05-25 20:43:31 +0200 | [diff] [blame] | 753 | platform_set_drvdata(data->pdev, NULL); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 754 | platform_device_unregister(data->pdev); |
| 755 | dev_err: |
Jonathan Cameron | 6580704 | 2011-10-31 17:10:27 -0700 | [diff] [blame] | 756 | ida_simple_remove(&aem_ida, data->id); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 757 | id_err: |
| 758 | kfree(data); |
| 759 | |
| 760 | return res; |
| 761 | } |
| 762 | |
| 763 | /* Find and initialize all AEM2 instances */ |
Jean Delvare | 9d84c9e | 2011-11-04 12:00:46 +0100 | [diff] [blame] | 764 | static void aem_init_aem2(struct aem_ipmi_data *probe) |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 765 | { |
| 766 | struct aem_find_instance_resp fi_resp; |
| 767 | int err; |
| 768 | int i = 0; |
| 769 | |
| 770 | while (!aem_find_aem2(probe, &fi_resp, i)) { |
| 771 | if (fi_resp.major != 2) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 772 | dev_err(probe->bmc_device, |
| 773 | "Unknown AEM v%d; please report this to the maintainer.\n", |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 774 | fi_resp.major); |
| 775 | i++; |
| 776 | continue; |
| 777 | } |
| 778 | err = aem_init_aem2_inst(probe, &fi_resp); |
| 779 | if (err) { |
| 780 | dev_err(probe->bmc_device, |
| 781 | "Error %d initializing AEM2 0x%X\n", |
| 782 | err, fi_resp.module_handle); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 783 | } |
| 784 | i++; |
| 785 | } |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* Probe a BMC for AEM firmware instances */ |
| 789 | static void aem_register_bmc(int iface, struct device *dev) |
| 790 | { |
| 791 | struct aem_ipmi_data probe; |
| 792 | |
| 793 | if (aem_init_ipmi_data(&probe, iface, dev)) |
| 794 | return; |
| 795 | |
| 796 | /* Ignore probe errors; they won't cause problems */ |
| 797 | aem_init_aem1(&probe); |
| 798 | aem_init_aem2(&probe); |
| 799 | |
| 800 | ipmi_destroy_user(probe.user); |
| 801 | } |
| 802 | |
| 803 | /* Handle BMC deletion */ |
| 804 | static void aem_bmc_gone(int iface) |
| 805 | { |
| 806 | struct aem_data *p1, *next1; |
| 807 | |
| 808 | list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list) |
| 809 | if (p1->ipmi.interface == iface) |
| 810 | aem_delete(p1); |
| 811 | } |
| 812 | |
| 813 | /* sysfs support functions */ |
| 814 | |
| 815 | /* AEM device name */ |
| 816 | static ssize_t show_name(struct device *dev, struct device_attribute *devattr, |
| 817 | char *buf) |
| 818 | { |
| 819 | struct aem_data *data = dev_get_drvdata(dev); |
| 820 | |
| 821 | return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major); |
| 822 | } |
| 823 | static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0); |
| 824 | |
| 825 | /* AEM device version */ |
| 826 | static ssize_t show_version(struct device *dev, |
| 827 | struct device_attribute *devattr, |
| 828 | char *buf) |
| 829 | { |
| 830 | struct aem_data *data = dev_get_drvdata(dev); |
| 831 | |
| 832 | return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor); |
| 833 | } |
| 834 | static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0); |
| 835 | |
| 836 | /* Display power use */ |
| 837 | static ssize_t aem_show_power(struct device *dev, |
| 838 | struct device_attribute *devattr, |
| 839 | char *buf) |
| 840 | { |
| 841 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 842 | struct aem_data *data = dev_get_drvdata(dev); |
| 843 | u64 before, after, delta, time; |
| 844 | signed long leftover; |
| 845 | struct timespec b, a; |
| 846 | |
| 847 | mutex_lock(&data->lock); |
Darrick J. Wong | bb15e7f | 2008-08-15 00:40:41 -0700 | [diff] [blame] | 848 | update_aem_energy_one(data, attr->index); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 849 | getnstimeofday(&b); |
| 850 | before = data->energy[attr->index]; |
| 851 | |
| 852 | leftover = schedule_timeout_interruptible( |
| 853 | msecs_to_jiffies(data->power_period[attr->index]) |
| 854 | ); |
| 855 | if (leftover) { |
| 856 | mutex_unlock(&data->lock); |
| 857 | return 0; |
| 858 | } |
| 859 | |
Darrick J. Wong | bb15e7f | 2008-08-15 00:40:41 -0700 | [diff] [blame] | 860 | update_aem_energy_one(data, attr->index); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 861 | getnstimeofday(&a); |
| 862 | after = data->energy[attr->index]; |
| 863 | mutex_unlock(&data->lock); |
| 864 | |
| 865 | time = timespec_to_ns(&a) - timespec_to_ns(&b); |
| 866 | delta = (after - before) * UJ_PER_MJ; |
| 867 | |
| 868 | return sprintf(buf, "%llu\n", |
| 869 | (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time)); |
| 870 | } |
| 871 | |
| 872 | /* Display energy use */ |
| 873 | static ssize_t aem_show_energy(struct device *dev, |
| 874 | struct device_attribute *devattr, |
| 875 | char *buf) |
| 876 | { |
| 877 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 878 | struct aem_data *a = dev_get_drvdata(dev); |
Darrick J. Wong | bb15e7f | 2008-08-15 00:40:41 -0700 | [diff] [blame] | 879 | mutex_lock(&a->lock); |
| 880 | update_aem_energy_one(a, attr->index); |
| 881 | mutex_unlock(&a->lock); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 882 | |
| 883 | return sprintf(buf, "%llu\n", |
| 884 | (unsigned long long)a->energy[attr->index] * 1000); |
| 885 | } |
| 886 | |
| 887 | /* Display power interval registers */ |
| 888 | static ssize_t aem_show_power_period(struct device *dev, |
| 889 | struct device_attribute *devattr, |
| 890 | char *buf) |
| 891 | { |
| 892 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 893 | struct aem_data *a = dev_get_drvdata(dev); |
| 894 | a->update(a); |
| 895 | |
| 896 | return sprintf(buf, "%lu\n", a->power_period[attr->index]); |
| 897 | } |
| 898 | |
| 899 | /* Set power interval registers */ |
| 900 | static ssize_t aem_set_power_period(struct device *dev, |
| 901 | struct device_attribute *devattr, |
| 902 | const char *buf, size_t count) |
| 903 | { |
| 904 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 905 | struct aem_data *a = dev_get_drvdata(dev); |
| 906 | unsigned long temp; |
| 907 | int res; |
| 908 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 909 | res = kstrtoul(buf, 10, &temp); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 910 | if (res) |
| 911 | return res; |
| 912 | |
| 913 | if (temp < AEM_MIN_POWER_INTERVAL) |
| 914 | return -EINVAL; |
| 915 | |
| 916 | mutex_lock(&a->lock); |
| 917 | a->power_period[attr->index] = temp; |
| 918 | mutex_unlock(&a->lock); |
| 919 | |
| 920 | return count; |
| 921 | } |
| 922 | |
| 923 | /* Discover sensors on an AEM device */ |
| 924 | static int aem_register_sensors(struct aem_data *data, |
| 925 | struct aem_ro_sensor_template *ro, |
| 926 | struct aem_rw_sensor_template *rw) |
| 927 | { |
| 928 | struct device *dev = &data->pdev->dev; |
| 929 | struct sensor_device_attribute *sensors = data->sensors; |
| 930 | int err; |
| 931 | |
| 932 | /* Set up read-only sensors */ |
| 933 | while (ro->label) { |
Guenter Roeck | 3cdb205 | 2011-05-24 12:33:26 -0700 | [diff] [blame] | 934 | sysfs_attr_init(&sensors->dev_attr.attr); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 935 | sensors->dev_attr.attr.name = ro->label; |
| 936 | sensors->dev_attr.attr.mode = S_IRUGO; |
| 937 | sensors->dev_attr.show = ro->show; |
| 938 | sensors->index = ro->index; |
| 939 | |
| 940 | err = device_create_file(dev, &sensors->dev_attr); |
| 941 | if (err) { |
| 942 | sensors->dev_attr.attr.name = NULL; |
| 943 | goto error; |
| 944 | } |
| 945 | sensors++; |
| 946 | ro++; |
| 947 | } |
| 948 | |
| 949 | /* Set up read-write sensors */ |
| 950 | while (rw->label) { |
Guenter Roeck | 3cdb205 | 2011-05-24 12:33:26 -0700 | [diff] [blame] | 951 | sysfs_attr_init(&sensors->dev_attr.attr); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 952 | sensors->dev_attr.attr.name = rw->label; |
| 953 | sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 954 | sensors->dev_attr.show = rw->show; |
| 955 | sensors->dev_attr.store = rw->set; |
| 956 | sensors->index = rw->index; |
| 957 | |
| 958 | err = device_create_file(dev, &sensors->dev_attr); |
| 959 | if (err) { |
| 960 | sensors->dev_attr.attr.name = NULL; |
| 961 | goto error; |
| 962 | } |
| 963 | sensors++; |
| 964 | rw++; |
| 965 | } |
| 966 | |
| 967 | err = device_create_file(dev, &sensor_dev_attr_name.dev_attr); |
| 968 | if (err) |
| 969 | goto error; |
| 970 | err = device_create_file(dev, &sensor_dev_attr_version.dev_attr); |
| 971 | return err; |
| 972 | |
| 973 | error: |
| 974 | aem_remove_sensors(data); |
| 975 | return err; |
| 976 | } |
| 977 | |
| 978 | /* sysfs support functions for AEM2 sensors */ |
| 979 | |
| 980 | /* Display temperature use */ |
| 981 | static ssize_t aem2_show_temp(struct device *dev, |
| 982 | struct device_attribute *devattr, |
| 983 | char *buf) |
| 984 | { |
| 985 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 986 | struct aem_data *a = dev_get_drvdata(dev); |
| 987 | a->update(a); |
| 988 | |
| 989 | return sprintf(buf, "%u\n", a->temp[attr->index] * 1000); |
| 990 | } |
| 991 | |
| 992 | /* Display power-capping registers */ |
| 993 | static ssize_t aem2_show_pcap_value(struct device *dev, |
| 994 | struct device_attribute *devattr, |
| 995 | char *buf) |
| 996 | { |
| 997 | struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); |
| 998 | struct aem_data *a = dev_get_drvdata(dev); |
| 999 | a->update(a); |
| 1000 | |
| 1001 | return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000); |
| 1002 | } |
| 1003 | |
| 1004 | /* Remove sensors attached to an AEM device */ |
| 1005 | static void aem_remove_sensors(struct aem_data *data) |
| 1006 | { |
| 1007 | int i; |
| 1008 | |
| 1009 | for (i = 0; i < AEM_NUM_SENSORS; i++) { |
| 1010 | if (!data->sensors[i].dev_attr.attr.name) |
| 1011 | continue; |
| 1012 | device_remove_file(&data->pdev->dev, |
| 1013 | &data->sensors[i].dev_attr); |
| 1014 | } |
| 1015 | |
| 1016 | device_remove_file(&data->pdev->dev, |
| 1017 | &sensor_dev_attr_name.dev_attr); |
| 1018 | device_remove_file(&data->pdev->dev, |
| 1019 | &sensor_dev_attr_version.dev_attr); |
| 1020 | } |
| 1021 | |
| 1022 | /* Sensor probe functions */ |
| 1023 | |
| 1024 | /* Description of AEM1 sensors */ |
| 1025 | static struct aem_ro_sensor_template aem1_ro_sensors[] = { |
| 1026 | {"energy1_input", aem_show_energy, 0}, |
| 1027 | {"power1_average", aem_show_power, 0}, |
| 1028 | {NULL, NULL, 0}, |
| 1029 | }; |
| 1030 | |
| 1031 | static struct aem_rw_sensor_template aem1_rw_sensors[] = { |
| 1032 | {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0}, |
| 1033 | {NULL, NULL, NULL, 0}, |
| 1034 | }; |
| 1035 | |
| 1036 | /* Description of AEM2 sensors */ |
| 1037 | static struct aem_ro_sensor_template aem2_ro_sensors[] = { |
| 1038 | {"energy1_input", aem_show_energy, 0}, |
| 1039 | {"energy2_input", aem_show_energy, 1}, |
| 1040 | {"power1_average", aem_show_power, 0}, |
| 1041 | {"power2_average", aem_show_power, 1}, |
| 1042 | {"temp1_input", aem2_show_temp, 0}, |
| 1043 | {"temp2_input", aem2_show_temp, 1}, |
| 1044 | |
| 1045 | {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG}, |
| 1046 | {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX}, |
| 1047 | {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING}, |
| 1048 | {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN}, |
| 1049 | |
Frans Meulenbroeks | 0910b28 | 2012-01-08 19:34:05 +0100 | [diff] [blame] | 1050 | {"power3_average", aem2_show_pcap_value, POWER_AUX}, |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1051 | {"power_cap", aem2_show_pcap_value, POWER_CAP}, |
| 1052 | {NULL, NULL, 0}, |
| 1053 | }; |
| 1054 | |
| 1055 | static struct aem_rw_sensor_template aem2_rw_sensors[] = { |
| 1056 | {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0}, |
| 1057 | {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1}, |
| 1058 | {NULL, NULL, NULL, 0}, |
| 1059 | }; |
| 1060 | |
| 1061 | /* Set up AEM1 sensor attrs */ |
| 1062 | static int aem1_find_sensors(struct aem_data *data) |
| 1063 | { |
| 1064 | return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors); |
| 1065 | } |
| 1066 | |
| 1067 | /* Set up AEM2 sensor attrs */ |
| 1068 | static int aem2_find_sensors(struct aem_data *data) |
| 1069 | { |
| 1070 | return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors); |
| 1071 | } |
| 1072 | |
| 1073 | /* Module init/exit routines */ |
| 1074 | |
| 1075 | static int __init aem_init(void) |
| 1076 | { |
| 1077 | int res; |
| 1078 | |
Darrick J. Wong | fe2d5ff | 2008-11-12 13:25:00 -0800 | [diff] [blame] | 1079 | res = driver_register(&aem_driver.driver); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1080 | if (res) { |
Joe Perches | 55d705c | 2010-10-20 06:51:38 +0000 | [diff] [blame] | 1081 | pr_err("Can't register aem driver\n"); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1082 | return res; |
| 1083 | } |
| 1084 | |
| 1085 | res = ipmi_smi_watcher_register(&driver_data.bmc_events); |
| 1086 | if (res) |
| 1087 | goto ipmi_reg_err; |
| 1088 | return 0; |
| 1089 | |
| 1090 | ipmi_reg_err: |
Darrick J. Wong | fe2d5ff | 2008-11-12 13:25:00 -0800 | [diff] [blame] | 1091 | driver_unregister(&aem_driver.driver); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1092 | return res; |
| 1093 | |
| 1094 | } |
| 1095 | |
| 1096 | static void __exit aem_exit(void) |
| 1097 | { |
| 1098 | struct aem_data *p1, *next1; |
| 1099 | |
| 1100 | ipmi_smi_watcher_unregister(&driver_data.bmc_events); |
Darrick J. Wong | fe2d5ff | 2008-11-12 13:25:00 -0800 | [diff] [blame] | 1101 | driver_unregister(&aem_driver.driver); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1102 | list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list) |
| 1103 | aem_delete(p1); |
| 1104 | } |
| 1105 | |
| 1106 | MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>"); |
Darrick J. Wong | eb93b7d | 2008-08-15 00:40:40 -0700 | [diff] [blame] | 1107 | MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver"); |
Darrick J. Wong | 8808a79 | 2008-05-23 13:04:25 -0700 | [diff] [blame] | 1108 | MODULE_LICENSE("GPL"); |
| 1109 | |
| 1110 | module_init(aem_init); |
| 1111 | module_exit(aem_exit); |
Darrick J. Wong | 21d93c1 | 2008-10-17 17:51:19 +0200 | [diff] [blame] | 1112 | |
| 1113 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*"); |
| 1114 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*"); |
| 1115 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*"); |
| 1116 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*"); |
| 1117 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*"); |
| 1118 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*"); |
Darrick J. Wong | 8070408 | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 1119 | MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*"); |