Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / hwmon / ibmaem.c
blobc6d59b1788d7d835b901297a93fdf8dcb737504f
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
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
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.
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.
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
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
40 #define REFRESH_INTERVAL (HZ)
41 #define IPMI_TIMEOUT (30 * HZ)
42 #define DRVNAME "aem"
44 #define AEM_NETFN 0x2E
46 #define AEM_FIND_FW_CMD 0x80
47 #define AEM_ELEMENT_CMD 0x81
48 #define AEM_FW_INSTANCE_CMD 0x82
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
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
65 #define AEM_MODULE_TYPE_ID 0x0001
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
72 #define AEM1_NUM_ENERGY_REGS 1
73 #define AEM1_NUM_SENSORS 3
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
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
87 #define AEM_DEFAULT_POWER_INTERVAL 1000
88 #define AEM_MIN_POWER_INTERVAL 200
89 #define UJ_PER_MJ 1000L
91 static DEFINE_IDR(aem_idr);
92 static DEFINE_SPINLOCK(aem_idr_lock);
94 static struct platform_driver aem_driver = {
95 .driver = {
96 .name = DRVNAME,
97 .bus = &platform_bus_type,
101 struct aem_ipmi_data {
102 struct completion read_complete;
103 struct ipmi_addr address;
104 ipmi_user_t user;
105 int interface;
107 struct kernel_ipmi_msg tx_message;
108 long tx_msgid;
110 void *rx_msg_data;
111 unsigned short rx_msg_len;
112 unsigned char rx_result;
113 int rx_recv_type;
115 struct device *bmc_device;
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;
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;
137 struct aem_data {
138 struct list_head list;
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;
151 /* Function and buffer to update sensors */
152 void (*update)(struct aem_data *data);
153 struct aem_read_sensor_resp *rs_resp;
156 * AEM 1.x sensors:
157 * Available sensors:
158 * Energy meter
159 * Power meter
161 * AEM 2.x sensors:
162 * Two energy meters
163 * Two power meters
164 * Two temperature sensors
165 * Six power cap registers
168 /* sysfs attrs */
169 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
171 /* energy use in mJ */
172 u64 energy[AEM_NUM_ENERGY_REGS];
174 /* power sampling interval in ms */
175 unsigned long power_period[AEM_NUM_ENERGY_REGS];
177 /* Everything past here is for AEM2 only */
179 /* power caps in dW */
180 u16 pcap[AEM2_NUM_PCAP_REGS];
182 /* exhaust temperature in C */
183 u8 temp[AEM2_NUM_TEMP_REGS];
186 /* Data structures returned by the AEM firmware */
187 struct aem_iana_id {
188 u8 bytes[3];
190 static struct aem_iana_id system_x_id = {
191 .bytes = {0x4D, 0x4F, 0x00}
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;
202 struct aem_find_firmware_resp {
203 struct aem_iana_id id;
204 u8 num_instances;
205 } __packed;
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;
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;
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;
233 struct aem_read_sensor_resp {
234 struct aem_iana_id id;
235 u8 bytes[0];
236 } __packed;
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;
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);
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);
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,
262 .ipmi_hndlrs = {
263 .ipmi_recv_hndl = aem_msg_handler,
267 /* Functions to talk to the IPMI layer */
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)
273 int err;
275 init_completion(&data->read_complete);
276 data->bmc_device = bmc;
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;
284 /* Initialize message buffers */
285 data->tx_msgid = 0;
286 data->tx_message.netfn = AEM_NETFN;
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;
297 return 0;
300 /* Send an IPMI command */
301 static int aem_send_message(struct aem_ipmi_data *data)
303 int err;
305 err = ipmi_validate_addr(&data->address, sizeof(data->address));
306 if (err)
307 goto out;
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;
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;
324 /* Dispatch IPMI messages to callers */
325 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
327 unsigned short rx_len;
328 struct aem_ipmi_data *data = user_msg_data;
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;
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;
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;
354 ipmi_free_recv_msg(msg);
355 complete(&data->read_complete);
358 /* ID functions */
360 /* Obtain an id */
361 static int aem_idr_get(int *id)
363 int i, err;
365 again:
366 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
367 return -ENOMEM;
369 spin_lock(&aem_idr_lock);
370 err = idr_get_new(&aem_idr, NULL, &i);
371 spin_unlock(&aem_idr_lock);
373 if (unlikely(err == -EAGAIN))
374 goto again;
375 else if (unlikely(err))
376 return err;
378 *id = i & MAX_ID_MASK;
379 return 0;
382 /* Release an object ID */
383 static void aem_idr_put(int id)
385 spin_lock(&aem_idr_lock);
386 idr_remove(&aem_idr, id);
387 spin_unlock(&aem_idr_lock);
390 /* Sensor support functions */
392 /* Read a sensor value; must be called with data->lock held */
393 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
394 void *buf, size_t size)
396 int rs_size, res;
397 struct aem_read_sensor_req rs_req;
398 /* Use preallocated rx buffer */
399 struct aem_read_sensor_resp *rs_resp = data->rs_resp;
400 struct aem_ipmi_data *ipmi = &data->ipmi;
402 /* AEM registers are 1, 2, 4 or 8 bytes */
403 switch (size) {
404 case 1:
405 case 2:
406 case 4:
407 case 8:
408 break;
409 default:
410 return -EINVAL;
413 rs_req.id = system_x_id;
414 rs_req.module_handle = data->module_handle;
415 rs_req.element = elt;
416 rs_req.subcommand = AEM_READ_REGISTER;
417 rs_req.reg = reg;
418 rs_req.rx_buf_size = size;
420 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
421 ipmi->tx_message.data = (char *)&rs_req;
422 ipmi->tx_message.data_len = sizeof(rs_req);
424 rs_size = sizeof(*rs_resp) + size;
425 ipmi->rx_msg_data = rs_resp;
426 ipmi->rx_msg_len = rs_size;
428 aem_send_message(ipmi);
430 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
431 if (!res) {
432 res = -ETIMEDOUT;
433 goto out;
436 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
437 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
438 res = -ENOENT;
439 goto out;
442 switch (size) {
443 case 1: {
444 u8 *x = buf;
445 *x = rs_resp->bytes[0];
446 break;
448 case 2: {
449 u16 *x = buf;
450 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
451 break;
453 case 4: {
454 u32 *x = buf;
455 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
456 break;
458 case 8: {
459 u64 *x = buf;
460 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
461 break;
464 res = 0;
466 out:
467 return res;
470 /* Update AEM energy registers */
471 static void update_aem_energy_one(struct aem_data *data, int which)
473 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
474 &data->energy[which], 8);
477 static void update_aem_energy(struct aem_data *data)
479 update_aem_energy_one(data, 0);
480 if (data->ver_major < 2)
481 return;
482 update_aem_energy_one(data, 1);
485 /* Update all AEM1 sensors */
486 static void update_aem1_sensors(struct aem_data *data)
488 mutex_lock(&data->lock);
489 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
490 data->valid)
491 goto out;
493 update_aem_energy(data);
494 out:
495 mutex_unlock(&data->lock);
498 /* Update all AEM2 sensors */
499 static void update_aem2_sensors(struct aem_data *data)
501 int i;
503 mutex_lock(&data->lock);
504 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
505 data->valid)
506 goto out;
508 update_aem_energy(data);
509 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
510 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
512 for (i = POWER_CAP; i <= POWER_AUX; i++)
513 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
514 &data->pcap[i], 2);
515 out:
516 mutex_unlock(&data->lock);
519 /* Delete an AEM instance */
520 static void aem_delete(struct aem_data *data)
522 list_del(&data->list);
523 aem_remove_sensors(data);
524 kfree(data->rs_resp);
525 hwmon_device_unregister(data->hwmon_dev);
526 ipmi_destroy_user(data->ipmi.user);
527 platform_set_drvdata(data->pdev, NULL);
528 platform_device_unregister(data->pdev);
529 aem_idr_put(data->id);
530 kfree(data);
533 /* Probe functions for AEM1 devices */
535 /* Retrieve version and module handle for an AEM1 instance */
536 static int aem_find_aem1_count(struct aem_ipmi_data *data)
538 int res;
539 struct aem_find_firmware_req ff_req;
540 struct aem_find_firmware_resp ff_resp;
542 ff_req.id = system_x_id;
543 ff_req.index = 0;
544 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
546 data->tx_message.cmd = AEM_FIND_FW_CMD;
547 data->tx_message.data = (char *)&ff_req;
548 data->tx_message.data_len = sizeof(ff_req);
550 data->rx_msg_data = &ff_resp;
551 data->rx_msg_len = sizeof(ff_resp);
553 aem_send_message(data);
555 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
556 if (!res)
557 return -ETIMEDOUT;
559 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
560 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
561 return -ENOENT;
563 return ff_resp.num_instances;
566 /* Find and initialize one AEM1 instance */
567 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
569 struct aem_data *data;
570 int i;
571 int res = -ENOMEM;
573 data = kzalloc(sizeof(*data), GFP_KERNEL);
574 if (!data)
575 return res;
576 mutex_init(&data->lock);
578 /* Copy instance data */
579 data->ver_major = 1;
580 data->ver_minor = 0;
581 data->module_handle = module_handle;
582 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
583 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
585 /* Create sub-device for this fw instance */
586 if (aem_idr_get(&data->id))
587 goto id_err;
589 data->pdev = platform_device_alloc(DRVNAME, data->id);
590 if (!data->pdev)
591 goto dev_err;
592 data->pdev->dev.driver = &aem_driver.driver;
594 res = platform_device_add(data->pdev);
595 if (res)
596 goto ipmi_err;
598 platform_set_drvdata(data->pdev, data);
600 /* Set up IPMI interface */
601 res = aem_init_ipmi_data(&data->ipmi, probe->interface,
602 probe->bmc_device);
603 if (res)
604 goto ipmi_err;
606 /* Register with hwmon */
607 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
608 if (IS_ERR(data->hwmon_dev)) {
609 dev_err(&data->pdev->dev, "Unable to register hwmon "
610 "device for IPMI interface %d\n",
611 probe->interface);
612 res = PTR_ERR(data->hwmon_dev);
613 goto hwmon_reg_err;
616 data->update = update_aem1_sensors;
617 data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL);
618 if (!data->rs_resp) {
619 res = -ENOMEM;
620 goto alloc_resp_err;
623 /* Find sensors */
624 res = aem1_find_sensors(data);
625 if (res)
626 goto sensor_err;
628 /* Add to our list of AEM devices */
629 list_add_tail(&data->list, &driver_data.aem_devices);
631 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
632 data->ver_major, data->ver_minor,
633 data->module_handle);
634 return 0;
636 sensor_err:
637 kfree(data->rs_resp);
638 alloc_resp_err:
639 hwmon_device_unregister(data->hwmon_dev);
640 hwmon_reg_err:
641 ipmi_destroy_user(data->ipmi.user);
642 ipmi_err:
643 platform_set_drvdata(data->pdev, NULL);
644 platform_device_unregister(data->pdev);
645 dev_err:
646 aem_idr_put(data->id);
647 id_err:
648 kfree(data);
650 return res;
653 /* Find and initialize all AEM1 instances */
654 static void aem_init_aem1(struct aem_ipmi_data *probe)
656 int num, i, err;
658 num = aem_find_aem1_count(probe);
659 for (i = 0; i < num; i++) {
660 err = aem_init_aem1_inst(probe, i);
661 if (err) {
662 dev_err(probe->bmc_device,
663 "Error %d initializing AEM1 0x%X\n",
664 err, i);
669 /* Probe functions for AEM2 devices */
671 /* Retrieve version and module handle for an AEM2 instance */
672 static int aem_find_aem2(struct aem_ipmi_data *data,
673 struct aem_find_instance_resp *fi_resp,
674 int instance_num)
676 int res;
677 struct aem_find_instance_req fi_req;
679 fi_req.id = system_x_id;
680 fi_req.instance_number = instance_num;
681 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
683 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
684 data->tx_message.data = (char *)&fi_req;
685 data->tx_message.data_len = sizeof(fi_req);
687 data->rx_msg_data = fi_resp;
688 data->rx_msg_len = sizeof(*fi_resp);
690 aem_send_message(data);
692 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
693 if (!res)
694 return -ETIMEDOUT;
696 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
697 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
698 fi_resp->num_instances <= instance_num)
699 return -ENOENT;
701 return 0;
704 /* Find and initialize one AEM2 instance */
705 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
706 struct aem_find_instance_resp *fi_resp)
708 struct aem_data *data;
709 int i;
710 int res = -ENOMEM;
712 data = kzalloc(sizeof(*data), GFP_KERNEL);
713 if (!data)
714 return res;
715 mutex_init(&data->lock);
717 /* Copy instance data */
718 data->ver_major = fi_resp->major;
719 data->ver_minor = fi_resp->minor;
720 data->module_handle = fi_resp->module_handle;
721 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
722 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
724 /* Create sub-device for this fw instance */
725 if (aem_idr_get(&data->id))
726 goto id_err;
728 data->pdev = platform_device_alloc(DRVNAME, data->id);
729 if (!data->pdev)
730 goto dev_err;
731 data->pdev->dev.driver = &aem_driver.driver;
733 res = platform_device_add(data->pdev);
734 if (res)
735 goto ipmi_err;
737 platform_set_drvdata(data->pdev, data);
739 /* Set up IPMI interface */
740 res = aem_init_ipmi_data(&data->ipmi, probe->interface,
741 probe->bmc_device);
742 if (res)
743 goto ipmi_err;
745 /* Register with hwmon */
746 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
747 if (IS_ERR(data->hwmon_dev)) {
748 dev_err(&data->pdev->dev, "Unable to register hwmon "
749 "device for IPMI interface %d\n",
750 probe->interface);
751 res = PTR_ERR(data->hwmon_dev);
752 goto hwmon_reg_err;
755 data->update = update_aem2_sensors;
756 data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL);
757 if (!data->rs_resp) {
758 res = -ENOMEM;
759 goto alloc_resp_err;
762 /* Find sensors */
763 res = aem2_find_sensors(data);
764 if (res)
765 goto sensor_err;
767 /* Add to our list of AEM devices */
768 list_add_tail(&data->list, &driver_data.aem_devices);
770 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
771 data->ver_major, data->ver_minor,
772 data->module_handle);
773 return 0;
775 sensor_err:
776 kfree(data->rs_resp);
777 alloc_resp_err:
778 hwmon_device_unregister(data->hwmon_dev);
779 hwmon_reg_err:
780 ipmi_destroy_user(data->ipmi.user);
781 ipmi_err:
782 platform_set_drvdata(data->pdev, NULL);
783 platform_device_unregister(data->pdev);
784 dev_err:
785 aem_idr_put(data->id);
786 id_err:
787 kfree(data);
789 return res;
792 /* Find and initialize all AEM2 instances */
793 static void aem_init_aem2(struct aem_ipmi_data *probe)
795 struct aem_find_instance_resp fi_resp;
796 int err;
797 int i = 0;
799 while (!aem_find_aem2(probe, &fi_resp, i)) {
800 if (fi_resp.major != 2) {
801 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
802 "report this to the maintainer.\n",
803 fi_resp.major);
804 i++;
805 continue;
807 err = aem_init_aem2_inst(probe, &fi_resp);
808 if (err) {
809 dev_err(probe->bmc_device,
810 "Error %d initializing AEM2 0x%X\n",
811 err, fi_resp.module_handle);
813 i++;
817 /* Probe a BMC for AEM firmware instances */
818 static void aem_register_bmc(int iface, struct device *dev)
820 struct aem_ipmi_data probe;
822 if (aem_init_ipmi_data(&probe, iface, dev))
823 return;
825 /* Ignore probe errors; they won't cause problems */
826 aem_init_aem1(&probe);
827 aem_init_aem2(&probe);
829 ipmi_destroy_user(probe.user);
832 /* Handle BMC deletion */
833 static void aem_bmc_gone(int iface)
835 struct aem_data *p1, *next1;
837 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
838 if (p1->ipmi.interface == iface)
839 aem_delete(p1);
842 /* sysfs support functions */
844 /* AEM device name */
845 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
846 char *buf)
848 struct aem_data *data = dev_get_drvdata(dev);
850 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
852 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
854 /* AEM device version */
855 static ssize_t show_version(struct device *dev,
856 struct device_attribute *devattr,
857 char *buf)
859 struct aem_data *data = dev_get_drvdata(dev);
861 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
863 static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
865 /* Display power use */
866 static ssize_t aem_show_power(struct device *dev,
867 struct device_attribute *devattr,
868 char *buf)
870 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
871 struct aem_data *data = dev_get_drvdata(dev);
872 u64 before, after, delta, time;
873 signed long leftover;
874 struct timespec b, a;
876 mutex_lock(&data->lock);
877 update_aem_energy_one(data, attr->index);
878 getnstimeofday(&b);
879 before = data->energy[attr->index];
881 leftover = schedule_timeout_interruptible(
882 msecs_to_jiffies(data->power_period[attr->index])
884 if (leftover) {
885 mutex_unlock(&data->lock);
886 return 0;
889 update_aem_energy_one(data, attr->index);
890 getnstimeofday(&a);
891 after = data->energy[attr->index];
892 mutex_unlock(&data->lock);
894 time = timespec_to_ns(&a) - timespec_to_ns(&b);
895 delta = (after - before) * UJ_PER_MJ;
897 return sprintf(buf, "%llu\n",
898 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
901 /* Display energy use */
902 static ssize_t aem_show_energy(struct device *dev,
903 struct device_attribute *devattr,
904 char *buf)
906 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 struct aem_data *a = dev_get_drvdata(dev);
908 mutex_lock(&a->lock);
909 update_aem_energy_one(a, attr->index);
910 mutex_unlock(&a->lock);
912 return sprintf(buf, "%llu\n",
913 (unsigned long long)a->energy[attr->index] * 1000);
916 /* Display power interval registers */
917 static ssize_t aem_show_power_period(struct device *dev,
918 struct device_attribute *devattr,
919 char *buf)
921 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
922 struct aem_data *a = dev_get_drvdata(dev);
923 a->update(a);
925 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
928 /* Set power interval registers */
929 static ssize_t aem_set_power_period(struct device *dev,
930 struct device_attribute *devattr,
931 const char *buf, size_t count)
933 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
934 struct aem_data *a = dev_get_drvdata(dev);
935 unsigned long temp;
936 int res;
938 res = strict_strtoul(buf, 10, &temp);
939 if (res)
940 return res;
942 if (temp < AEM_MIN_POWER_INTERVAL)
943 return -EINVAL;
945 mutex_lock(&a->lock);
946 a->power_period[attr->index] = temp;
947 mutex_unlock(&a->lock);
949 return count;
952 /* Discover sensors on an AEM device */
953 static int aem_register_sensors(struct aem_data *data,
954 struct aem_ro_sensor_template *ro,
955 struct aem_rw_sensor_template *rw)
957 struct device *dev = &data->pdev->dev;
958 struct sensor_device_attribute *sensors = data->sensors;
959 int err;
961 /* Set up read-only sensors */
962 while (ro->label) {
963 sysfs_attr_init(&sensors->dev_attr.attr);
964 sensors->dev_attr.attr.name = ro->label;
965 sensors->dev_attr.attr.mode = S_IRUGO;
966 sensors->dev_attr.show = ro->show;
967 sensors->index = ro->index;
969 err = device_create_file(dev, &sensors->dev_attr);
970 if (err) {
971 sensors->dev_attr.attr.name = NULL;
972 goto error;
974 sensors++;
975 ro++;
978 /* Set up read-write sensors */
979 while (rw->label) {
980 sysfs_attr_init(&sensors->dev_attr.attr);
981 sensors->dev_attr.attr.name = rw->label;
982 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
983 sensors->dev_attr.show = rw->show;
984 sensors->dev_attr.store = rw->set;
985 sensors->index = rw->index;
987 err = device_create_file(dev, &sensors->dev_attr);
988 if (err) {
989 sensors->dev_attr.attr.name = NULL;
990 goto error;
992 sensors++;
993 rw++;
996 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
997 if (err)
998 goto error;
999 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
1000 return err;
1002 error:
1003 aem_remove_sensors(data);
1004 return err;
1007 /* sysfs support functions for AEM2 sensors */
1009 /* Display temperature use */
1010 static ssize_t aem2_show_temp(struct device *dev,
1011 struct device_attribute *devattr,
1012 char *buf)
1014 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1015 struct aem_data *a = dev_get_drvdata(dev);
1016 a->update(a);
1018 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1021 /* Display power-capping registers */
1022 static ssize_t aem2_show_pcap_value(struct device *dev,
1023 struct device_attribute *devattr,
1024 char *buf)
1026 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1027 struct aem_data *a = dev_get_drvdata(dev);
1028 a->update(a);
1030 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1033 /* Remove sensors attached to an AEM device */
1034 static void aem_remove_sensors(struct aem_data *data)
1036 int i;
1038 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1039 if (!data->sensors[i].dev_attr.attr.name)
1040 continue;
1041 device_remove_file(&data->pdev->dev,
1042 &data->sensors[i].dev_attr);
1045 device_remove_file(&data->pdev->dev,
1046 &sensor_dev_attr_name.dev_attr);
1047 device_remove_file(&data->pdev->dev,
1048 &sensor_dev_attr_version.dev_attr);
1051 /* Sensor probe functions */
1053 /* Description of AEM1 sensors */
1054 static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1055 {"energy1_input", aem_show_energy, 0},
1056 {"power1_average", aem_show_power, 0},
1057 {NULL, NULL, 0},
1060 static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1061 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1062 {NULL, NULL, NULL, 0},
1065 /* Description of AEM2 sensors */
1066 static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1067 {"energy1_input", aem_show_energy, 0},
1068 {"energy2_input", aem_show_energy, 1},
1069 {"power1_average", aem_show_power, 0},
1070 {"power2_average", aem_show_power, 1},
1071 {"temp1_input", aem2_show_temp, 0},
1072 {"temp2_input", aem2_show_temp, 1},
1074 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1075 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1076 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1077 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1079 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1080 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1081 {NULL, NULL, 0},
1084 static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1085 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1086 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1087 {NULL, NULL, NULL, 0},
1090 /* Set up AEM1 sensor attrs */
1091 static int aem1_find_sensors(struct aem_data *data)
1093 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1096 /* Set up AEM2 sensor attrs */
1097 static int aem2_find_sensors(struct aem_data *data)
1099 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1102 /* Module init/exit routines */
1104 static int __init aem_init(void)
1106 int res;
1108 res = driver_register(&aem_driver.driver);
1109 if (res) {
1110 pr_err("Can't register aem driver\n");
1111 return res;
1114 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1115 if (res)
1116 goto ipmi_reg_err;
1117 return 0;
1119 ipmi_reg_err:
1120 driver_unregister(&aem_driver.driver);
1121 return res;
1125 static void __exit aem_exit(void)
1127 struct aem_data *p1, *next1;
1129 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1130 driver_unregister(&aem_driver.driver);
1131 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1132 aem_delete(p1);
1135 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1136 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1137 MODULE_LICENSE("GPL");
1139 module_init(aem_init);
1140 module_exit(aem_exit);
1142 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1143 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1144 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1145 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1146 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1147 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1148 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");