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