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