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