ibmaem.c revision 9d84c9e8b5b0386ee1d7769de0ff8a2546a2d054
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 aem1_find_sensors(struct aem_data *data); 249static int aem2_find_sensors(struct aem_data *data); 250static void update_aem1_sensors(struct aem_data *data); 251static void update_aem2_sensors(struct aem_data *data); 252 253static struct aem_driver_data driver_data = { 254 .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices), 255 .bmc_events = { 256 .owner = THIS_MODULE, 257 .new_smi = aem_register_bmc, 258 .smi_gone = aem_bmc_gone, 259 }, 260 .ipmi_hndlrs = { 261 .ipmi_recv_hndl = aem_msg_handler, 262 }, 263}; 264 265/* Functions to talk to the IPMI layer */ 266 267/* Initialize IPMI address, message buffers and user data */ 268static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface, 269 struct device *bmc) 270{ 271 int err; 272 273 init_completion(&data->read_complete); 274 data->bmc_device = bmc; 275 276 /* Initialize IPMI address */ 277 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; 278 data->address.channel = IPMI_BMC_CHANNEL; 279 data->address.data[0] = 0; 280 data->interface = iface; 281 282 /* Initialize message buffers */ 283 data->tx_msgid = 0; 284 data->tx_message.netfn = AEM_NETFN; 285 286 /* Create IPMI messaging interface user */ 287 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs, 288 data, &data->user); 289 if (err < 0) { 290 dev_err(bmc, "Unable to register user with IPMI " 291 "interface %d\n", data->interface); 292 return -EACCES; 293 } 294 295 return 0; 296} 297 298/* Send an IPMI command */ 299static int aem_send_message(struct aem_ipmi_data *data) 300{ 301 int err; 302 303 err = ipmi_validate_addr(&data->address, sizeof(data->address)); 304 if (err) 305 goto out; 306 307 data->tx_msgid++; 308 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid, 309 &data->tx_message, data, 0, 0, 0); 310 if (err) 311 goto out1; 312 313 return 0; 314out1: 315 dev_err(data->bmc_device, "request_settime=%x\n", err); 316 return err; 317out: 318 dev_err(data->bmc_device, "validate_addr=%x\n", err); 319 return err; 320} 321 322/* Dispatch IPMI messages to callers */ 323static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data) 324{ 325 unsigned short rx_len; 326 struct aem_ipmi_data *data = user_msg_data; 327 328 if (msg->msgid != data->tx_msgid) { 329 dev_err(data->bmc_device, "Mismatch between received msgid " 330 "(%02x) and transmitted msgid (%02x)!\n", 331 (int)msg->msgid, 332 (int)data->tx_msgid); 333 ipmi_free_recv_msg(msg); 334 return; 335 } 336 337 data->rx_recv_type = msg->recv_type; 338 if (msg->msg.data_len > 0) 339 data->rx_result = msg->msg.data[0]; 340 else 341 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE; 342 343 if (msg->msg.data_len > 1) { 344 rx_len = msg->msg.data_len - 1; 345 if (data->rx_msg_len < rx_len) 346 rx_len = data->rx_msg_len; 347 data->rx_msg_len = rx_len; 348 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len); 349 } else 350 data->rx_msg_len = 0; 351 352 ipmi_free_recv_msg(msg); 353 complete(&data->read_complete); 354} 355 356/* Sensor support functions */ 357 358/* Read a sensor value */ 359static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg, 360 void *buf, size_t size) 361{ 362 int rs_size, res; 363 struct aem_read_sensor_req rs_req; 364 struct aem_read_sensor_resp *rs_resp; 365 struct aem_ipmi_data *ipmi = &data->ipmi; 366 367 /* AEM registers are 1, 2, 4 or 8 bytes */ 368 switch (size) { 369 case 1: 370 case 2: 371 case 4: 372 case 8: 373 break; 374 default: 375 return -EINVAL; 376 } 377 378 rs_req.id = system_x_id; 379 rs_req.module_handle = data->module_handle; 380 rs_req.element = elt; 381 rs_req.subcommand = AEM_READ_REGISTER; 382 rs_req.reg = reg; 383 rs_req.rx_buf_size = size; 384 385 ipmi->tx_message.cmd = AEM_ELEMENT_CMD; 386 ipmi->tx_message.data = (char *)&rs_req; 387 ipmi->tx_message.data_len = sizeof(rs_req); 388 389 rs_size = sizeof(*rs_resp) + size; 390 rs_resp = kzalloc(rs_size, GFP_KERNEL); 391 if (!rs_resp) 392 return -ENOMEM; 393 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); 400 if (!res) { 401 res = -ETIMEDOUT; 402 goto out; 403 } 404 405 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size || 406 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) { 407 res = -ENOENT; 408 goto out; 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; 419 *x = be16_to_cpup((__be16 *)rs_resp->bytes); 420 break; 421 } 422 case 4: { 423 u32 *x = buf; 424 *x = be32_to_cpup((__be32 *)rs_resp->bytes); 425 break; 426 } 427 case 8: { 428 u64 *x = buf; 429 *x = be64_to_cpup((__be64 *)rs_resp->bytes); 430 break; 431 } 432 } 433 res = 0; 434 435out: 436 kfree(rs_resp); 437 return res; 438} 439 440/* Update AEM energy registers */ 441static void update_aem_energy_one(struct aem_data *data, int which) 442{ 443 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which, 444 &data->energy[which], 8); 445} 446 447static void update_aem_energy(struct aem_data *data) 448{ 449 update_aem_energy_one(data, 0); 450 if (data->ver_major < 2) 451 return; 452 update_aem_energy_one(data, 1); 453} 454 455/* Update all AEM1 sensors */ 456static void update_aem1_sensors(struct aem_data *data) 457{ 458 mutex_lock(&data->lock); 459 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) && 460 data->valid) 461 goto out; 462 463 update_aem_energy(data); 464out: 465 mutex_unlock(&data->lock); 466} 467 468/* Update all AEM2 sensors */ 469static void update_aem2_sensors(struct aem_data *data) 470{ 471 int i; 472 473 mutex_lock(&data->lock); 474 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) && 475 data->valid) 476 goto out; 477 478 update_aem_energy(data); 479 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1); 480 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1); 481 482 for (i = POWER_CAP; i <= POWER_AUX; i++) 483 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i, 484 &data->pcap[i], 2); 485out: 486 mutex_unlock(&data->lock); 487} 488 489/* Delete an AEM instance */ 490static void aem_delete(struct aem_data *data) 491{ 492 list_del(&data->list); 493 aem_remove_sensors(data); 494 hwmon_device_unregister(data->hwmon_dev); 495 ipmi_destroy_user(data->ipmi.user); 496 platform_set_drvdata(data->pdev, NULL); 497 platform_device_unregister(data->pdev); 498 ida_simple_remove(&aem_ida, data->id); 499 kfree(data); 500} 501 502/* Probe functions for AEM1 devices */ 503 504/* Retrieve version and module handle for an AEM1 instance */ 505static 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 */ 536static 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 */ 555 data->id = ida_simple_get(&aem_ida, 0, 0, GFP_KERNEL); 556 if (data->id < 0) 557 goto id_err; 558 559 data->pdev = platform_device_alloc(DRVNAME, data->id); 560 if (!data->pdev) 561 goto dev_err; 562 data->pdev->dev.driver = &aem_driver.driver; 563 564 res = platform_device_add(data->pdev); 565 if (res) 566 goto ipmi_err; 567 568 platform_set_drvdata(data->pdev, data); 569 570 /* Set up IPMI interface */ 571 res = aem_init_ipmi_data(&data->ipmi, probe->interface, 572 probe->bmc_device); 573 if (res) 574 goto ipmi_err; 575 576 /* Register with hwmon */ 577 data->hwmon_dev = hwmon_device_register(&data->pdev->dev); 578 if (IS_ERR(data->hwmon_dev)) { 579 dev_err(&data->pdev->dev, "Unable to register hwmon " 580 "device for IPMI interface %d\n", 581 probe->interface); 582 res = PTR_ERR(data->hwmon_dev); 583 goto hwmon_reg_err; 584 } 585 586 data->update = update_aem1_sensors; 587 588 /* Find sensors */ 589 res = aem1_find_sensors(data); 590 if (res) 591 goto sensor_err; 592 593 /* Add to our list of AEM devices */ 594 list_add_tail(&data->list, &driver_data.aem_devices); 595 596 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n", 597 data->ver_major, data->ver_minor, 598 data->module_handle); 599 return 0; 600 601sensor_err: 602 hwmon_device_unregister(data->hwmon_dev); 603hwmon_reg_err: 604 ipmi_destroy_user(data->ipmi.user); 605ipmi_err: 606 platform_set_drvdata(data->pdev, NULL); 607 platform_device_unregister(data->pdev); 608dev_err: 609 ida_simple_remove(&aem_ida, data->id); 610id_err: 611 kfree(data); 612 613 return res; 614} 615 616/* Find and initialize all AEM1 instances */ 617static void aem_init_aem1(struct aem_ipmi_data *probe) 618{ 619 int num, i, err; 620 621 num = aem_find_aem1_count(probe); 622 for (i = 0; i < num; i++) { 623 err = aem_init_aem1_inst(probe, i); 624 if (err) { 625 dev_err(probe->bmc_device, 626 "Error %d initializing AEM1 0x%X\n", 627 err, i); 628 } 629 } 630} 631 632/* Probe functions for AEM2 devices */ 633 634/* Retrieve version and module handle for an AEM2 instance */ 635static int aem_find_aem2(struct aem_ipmi_data *data, 636 struct aem_find_instance_resp *fi_resp, 637 int instance_num) 638{ 639 int res; 640 struct aem_find_instance_req fi_req; 641 642 fi_req.id = system_x_id; 643 fi_req.instance_number = instance_num; 644 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID); 645 646 data->tx_message.cmd = AEM_FW_INSTANCE_CMD; 647 data->tx_message.data = (char *)&fi_req; 648 data->tx_message.data_len = sizeof(fi_req); 649 650 data->rx_msg_data = fi_resp; 651 data->rx_msg_len = sizeof(*fi_resp); 652 653 aem_send_message(data); 654 655 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT); 656 if (!res) 657 return -ETIMEDOUT; 658 659 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) || 660 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) || 661 fi_resp->num_instances <= instance_num) 662 return -ENOENT; 663 664 return 0; 665} 666 667/* Find and initialize one AEM2 instance */ 668static int aem_init_aem2_inst(struct aem_ipmi_data *probe, 669 struct aem_find_instance_resp *fi_resp) 670{ 671 struct aem_data *data; 672 int i; 673 int res = -ENOMEM; 674 675 data = kzalloc(sizeof(*data), GFP_KERNEL); 676 if (!data) 677 return res; 678 mutex_init(&data->lock); 679 680 /* Copy instance data */ 681 data->ver_major = fi_resp->major; 682 data->ver_minor = fi_resp->minor; 683 data->module_handle = fi_resp->module_handle; 684 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++) 685 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL; 686 687 /* Create sub-device for this fw instance */ 688 data->id = ida_simple_get(&aem_ida, 0, 0, GFP_KERNEL); 689 if (data->id < 0) 690 goto id_err; 691 692 data->pdev = platform_device_alloc(DRVNAME, data->id); 693 if (!data->pdev) 694 goto dev_err; 695 data->pdev->dev.driver = &aem_driver.driver; 696 697 res = platform_device_add(data->pdev); 698 if (res) 699 goto ipmi_err; 700 701 platform_set_drvdata(data->pdev, data); 702 703 /* Set up IPMI interface */ 704 res = aem_init_ipmi_data(&data->ipmi, probe->interface, 705 probe->bmc_device); 706 if (res) 707 goto ipmi_err; 708 709 /* Register with hwmon */ 710 data->hwmon_dev = hwmon_device_register(&data->pdev->dev); 711 if (IS_ERR(data->hwmon_dev)) { 712 dev_err(&data->pdev->dev, "Unable to register hwmon " 713 "device for IPMI interface %d\n", 714 probe->interface); 715 res = PTR_ERR(data->hwmon_dev); 716 goto hwmon_reg_err; 717 } 718 719 data->update = update_aem2_sensors; 720 721 /* Find sensors */ 722 res = aem2_find_sensors(data); 723 if (res) 724 goto sensor_err; 725 726 /* Add to our list of AEM devices */ 727 list_add_tail(&data->list, &driver_data.aem_devices); 728 729 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n", 730 data->ver_major, data->ver_minor, 731 data->module_handle); 732 return 0; 733 734sensor_err: 735 hwmon_device_unregister(data->hwmon_dev); 736hwmon_reg_err: 737 ipmi_destroy_user(data->ipmi.user); 738ipmi_err: 739 platform_set_drvdata(data->pdev, NULL); 740 platform_device_unregister(data->pdev); 741dev_err: 742 ida_simple_remove(&aem_ida, data->id); 743id_err: 744 kfree(data); 745 746 return res; 747} 748 749/* Find and initialize all AEM2 instances */ 750static void aem_init_aem2(struct aem_ipmi_data *probe) 751{ 752 struct aem_find_instance_resp fi_resp; 753 int err; 754 int i = 0; 755 756 while (!aem_find_aem2(probe, &fi_resp, i)) { 757 if (fi_resp.major != 2) { 758 dev_err(probe->bmc_device, "Unknown AEM v%d; please " 759 "report this to the maintainer.\n", 760 fi_resp.major); 761 i++; 762 continue; 763 } 764 err = aem_init_aem2_inst(probe, &fi_resp); 765 if (err) { 766 dev_err(probe->bmc_device, 767 "Error %d initializing AEM2 0x%X\n", 768 err, fi_resp.module_handle); 769 } 770 i++; 771 } 772} 773 774/* Probe a BMC for AEM firmware instances */ 775static void aem_register_bmc(int iface, struct device *dev) 776{ 777 struct aem_ipmi_data probe; 778 779 if (aem_init_ipmi_data(&probe, iface, dev)) 780 return; 781 782 /* Ignore probe errors; they won't cause problems */ 783 aem_init_aem1(&probe); 784 aem_init_aem2(&probe); 785 786 ipmi_destroy_user(probe.user); 787} 788 789/* Handle BMC deletion */ 790static void aem_bmc_gone(int iface) 791{ 792 struct aem_data *p1, *next1; 793 794 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list) 795 if (p1->ipmi.interface == iface) 796 aem_delete(p1); 797} 798 799/* sysfs support functions */ 800 801/* AEM device name */ 802static ssize_t show_name(struct device *dev, struct device_attribute *devattr, 803 char *buf) 804{ 805 struct aem_data *data = dev_get_drvdata(dev); 806 807 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major); 808} 809static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0); 810 811/* AEM device version */ 812static ssize_t show_version(struct device *dev, 813 struct device_attribute *devattr, 814 char *buf) 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} 820static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0); 821 822/* Display power use */ 823static 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; 831 struct timespec b, a; 832 833 mutex_lock(&data->lock); 834 update_aem_energy_one(data, attr->index); 835 getnstimeofday(&b); 836 before = data->energy[attr->index]; 837 838 leftover = schedule_timeout_interruptible( 839 msecs_to_jiffies(data->power_period[attr->index]) 840 ); 841 if (leftover) { 842 mutex_unlock(&data->lock); 843 return 0; 844 } 845 846 update_aem_energy_one(data, attr->index); 847 getnstimeofday(&a); 848 after = data->energy[attr->index]; 849 mutex_unlock(&data->lock); 850 851 time = timespec_to_ns(&a) - timespec_to_ns(&b); 852 delta = (after - before) * UJ_PER_MJ; 853 854 return sprintf(buf, "%llu\n", 855 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time)); 856} 857 858/* Display energy use */ 859static ssize_t aem_show_energy(struct device *dev, 860 struct device_attribute *devattr, 861 char *buf) 862{ 863 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 864 struct aem_data *a = dev_get_drvdata(dev); 865 mutex_lock(&a->lock); 866 update_aem_energy_one(a, attr->index); 867 mutex_unlock(&a->lock); 868 869 return sprintf(buf, "%llu\n", 870 (unsigned long long)a->energy[attr->index] * 1000); 871} 872 873/* Display power interval registers */ 874static ssize_t aem_show_power_period(struct device *dev, 875 struct device_attribute *devattr, 876 char *buf) 877{ 878 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 879 struct aem_data *a = dev_get_drvdata(dev); 880 a->update(a); 881 882 return sprintf(buf, "%lu\n", a->power_period[attr->index]); 883} 884 885/* Set power interval registers */ 886static ssize_t aem_set_power_period(struct device *dev, 887 struct device_attribute *devattr, 888 const char *buf, size_t count) 889{ 890 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 891 struct aem_data *a = dev_get_drvdata(dev); 892 unsigned long temp; 893 int res; 894 895 res = strict_strtoul(buf, 10, &temp); 896 if (res) 897 return res; 898 899 if (temp < AEM_MIN_POWER_INTERVAL) 900 return -EINVAL; 901 902 mutex_lock(&a->lock); 903 a->power_period[attr->index] = temp; 904 mutex_unlock(&a->lock); 905 906 return count; 907} 908 909/* Discover sensors on an AEM device */ 910static int aem_register_sensors(struct aem_data *data, 911 struct aem_ro_sensor_template *ro, 912 struct aem_rw_sensor_template *rw) 913{ 914 struct device *dev = &data->pdev->dev; 915 struct sensor_device_attribute *sensors = data->sensors; 916 int err; 917 918 /* Set up read-only sensors */ 919 while (ro->label) { 920 sysfs_attr_init(&sensors->dev_attr.attr); 921 sensors->dev_attr.attr.name = ro->label; 922 sensors->dev_attr.attr.mode = S_IRUGO; 923 sensors->dev_attr.show = ro->show; 924 sensors->index = ro->index; 925 926 err = device_create_file(dev, &sensors->dev_attr); 927 if (err) { 928 sensors->dev_attr.attr.name = NULL; 929 goto error; 930 } 931 sensors++; 932 ro++; 933 } 934 935 /* Set up read-write sensors */ 936 while (rw->label) { 937 sysfs_attr_init(&sensors->dev_attr.attr); 938 sensors->dev_attr.attr.name = rw->label; 939 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR; 940 sensors->dev_attr.show = rw->show; 941 sensors->dev_attr.store = rw->set; 942 sensors->index = rw->index; 943 944 err = device_create_file(dev, &sensors->dev_attr); 945 if (err) { 946 sensors->dev_attr.attr.name = NULL; 947 goto error; 948 } 949 sensors++; 950 rw++; 951 } 952 953 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr); 954 if (err) 955 goto error; 956 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr); 957 return err; 958 959error: 960 aem_remove_sensors(data); 961 return err; 962} 963 964/* sysfs support functions for AEM2 sensors */ 965 966/* Display temperature use */ 967static ssize_t aem2_show_temp(struct device *dev, 968 struct device_attribute *devattr, 969 char *buf) 970{ 971 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 972 struct aem_data *a = dev_get_drvdata(dev); 973 a->update(a); 974 975 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000); 976} 977 978/* Display power-capping registers */ 979static ssize_t aem2_show_pcap_value(struct device *dev, 980 struct device_attribute *devattr, 981 char *buf) 982{ 983 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 984 struct aem_data *a = dev_get_drvdata(dev); 985 a->update(a); 986 987 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000); 988} 989 990/* Remove sensors attached to an AEM device */ 991static void aem_remove_sensors(struct aem_data *data) 992{ 993 int i; 994 995 for (i = 0; i < AEM_NUM_SENSORS; i++) { 996 if (!data->sensors[i].dev_attr.attr.name) 997 continue; 998 device_remove_file(&data->pdev->dev, 999 &data->sensors[i].dev_attr); 1000 } 1001 1002 device_remove_file(&data->pdev->dev, 1003 &sensor_dev_attr_name.dev_attr); 1004 device_remove_file(&data->pdev->dev, 1005 &sensor_dev_attr_version.dev_attr); 1006} 1007 1008/* Sensor probe functions */ 1009 1010/* Description of AEM1 sensors */ 1011static struct aem_ro_sensor_template aem1_ro_sensors[] = { 1012{"energy1_input", aem_show_energy, 0}, 1013{"power1_average", aem_show_power, 0}, 1014{NULL, NULL, 0}, 1015}; 1016 1017static struct aem_rw_sensor_template aem1_rw_sensors[] = { 1018{"power1_average_interval", aem_show_power_period, aem_set_power_period, 0}, 1019{NULL, NULL, NULL, 0}, 1020}; 1021 1022/* Description of AEM2 sensors */ 1023static struct aem_ro_sensor_template aem2_ro_sensors[] = { 1024{"energy1_input", aem_show_energy, 0}, 1025{"energy2_input", aem_show_energy, 1}, 1026{"power1_average", aem_show_power, 0}, 1027{"power2_average", aem_show_power, 1}, 1028{"temp1_input", aem2_show_temp, 0}, 1029{"temp2_input", aem2_show_temp, 1}, 1030 1031{"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG}, 1032{"power5_average", aem2_show_pcap_value, POWER_CAP_MAX}, 1033{"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING}, 1034{"power7_average", aem2_show_pcap_value, POWER_CAP_MIN}, 1035 1036{"power3_average", aem2_show_pcap_value, POWER_AUX}, 1037{"power_cap", aem2_show_pcap_value, POWER_CAP}, 1038{NULL, NULL, 0}, 1039}; 1040 1041static struct aem_rw_sensor_template aem2_rw_sensors[] = { 1042{"power1_average_interval", aem_show_power_period, aem_set_power_period, 0}, 1043{"power2_average_interval", aem_show_power_period, aem_set_power_period, 1}, 1044{NULL, NULL, NULL, 0}, 1045}; 1046 1047/* Set up AEM1 sensor attrs */ 1048static int aem1_find_sensors(struct aem_data *data) 1049{ 1050 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors); 1051} 1052 1053/* Set up AEM2 sensor attrs */ 1054static int aem2_find_sensors(struct aem_data *data) 1055{ 1056 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors); 1057} 1058 1059/* Module init/exit routines */ 1060 1061static int __init aem_init(void) 1062{ 1063 int res; 1064 1065 res = driver_register(&aem_driver.driver); 1066 if (res) { 1067 pr_err("Can't register aem driver\n"); 1068 return res; 1069 } 1070 1071 res = ipmi_smi_watcher_register(&driver_data.bmc_events); 1072 if (res) 1073 goto ipmi_reg_err; 1074 return 0; 1075 1076ipmi_reg_err: 1077 driver_unregister(&aem_driver.driver); 1078 return res; 1079 1080} 1081 1082static void __exit aem_exit(void) 1083{ 1084 struct aem_data *p1, *next1; 1085 1086 ipmi_smi_watcher_unregister(&driver_data.bmc_events); 1087 driver_unregister(&aem_driver.driver); 1088 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list) 1089 aem_delete(p1); 1090} 1091 1092MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>"); 1093MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver"); 1094MODULE_LICENSE("GPL"); 1095 1096module_init(aem_init); 1097module_exit(aem_exit); 1098 1099MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*"); 1100MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*"); 1101MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*"); 1102MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*"); 1103MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*"); 1104MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*"); 1105MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*"); 1106