Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/vapier...
[linux-2.6/linux-2.6-openrd.git] / drivers / hwmon / ibmaem.c
blob405d3fb5d76f9e90aa9115b56eb4676c4e1b03bb
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 #include <linux/ipmi.h>
24 #include <linux/module.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/jiffies.h>
28 #include <linux/mutex.h>
29 #include <linux/kdev_t.h>
30 #include <linux/spinlock.h>
31 #include <linux/idr.h>
32 #include <linux/sched.h>
33 #include <linux/platform_device.h>
34 #include <linux/math64.h>
35 #include <linux/time.h>
37 #define REFRESH_INTERVAL (HZ)
38 #define IPMI_TIMEOUT (30 * HZ)
39 #define DRVNAME "aem"
41 #define AEM_NETFN 0x2E
43 #define AEM_FIND_FW_CMD 0x80
44 #define AEM_ELEMENT_CMD 0x81
45 #define AEM_FW_INSTANCE_CMD 0x82
47 #define AEM_READ_ELEMENT_CFG 0x80
48 #define AEM_READ_BUFFER 0x81
49 #define AEM_READ_REGISTER 0x82
50 #define AEM_WRITE_REGISTER 0x83
51 #define AEM_SET_REG_MASK 0x84
52 #define AEM_CLEAR_REG_MASK 0x85
53 #define AEM_READ_ELEMENT_CFG2 0x86
55 #define AEM_CONTROL_ELEMENT 0
56 #define AEM_ENERGY_ELEMENT 1
57 #define AEM_CLOCK_ELEMENT 4
58 #define AEM_POWER_CAP_ELEMENT 7
59 #define AEM_EXHAUST_ELEMENT 9
60 #define AEM_POWER_ELEMENT 10
62 #define AEM_MODULE_TYPE_ID 0x0001
64 #define AEM2_NUM_ENERGY_REGS 2
65 #define AEM2_NUM_PCAP_REGS 6
66 #define AEM2_NUM_TEMP_REGS 2
67 #define AEM2_NUM_SENSORS 14
69 #define AEM1_NUM_ENERGY_REGS 1
70 #define AEM1_NUM_SENSORS 3
72 /* AEM 2.x has more energy registers */
73 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
74 /* AEM 2.x needs more sensor files */
75 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
77 #define POWER_CAP 0
78 #define POWER_CAP_MAX_HOTPLUG 1
79 #define POWER_CAP_MAX 2
80 #define POWER_CAP_MIN_WARNING 3
81 #define POWER_CAP_MIN 4
82 #define POWER_AUX 5
84 #define AEM_DEFAULT_POWER_INTERVAL 1000
85 #define AEM_MIN_POWER_INTERVAL 200
86 #define UJ_PER_MJ 1000L
88 static DEFINE_IDR(aem_idr);
89 static DEFINE_SPINLOCK(aem_idr_lock);
91 static struct platform_driver aem_driver = {
92 .driver = {
93 .name = DRVNAME,
94 .bus = &platform_bus_type,
98 struct aem_ipmi_data {
99 struct completion read_complete;
100 struct ipmi_addr address;
101 ipmi_user_t user;
102 int interface;
104 struct kernel_ipmi_msg tx_message;
105 long tx_msgid;
107 void *rx_msg_data;
108 unsigned short rx_msg_len;
109 unsigned char rx_result;
110 int rx_recv_type;
112 struct device *bmc_device;
115 struct aem_ro_sensor_template {
116 char *label;
117 ssize_t (*show)(struct device *dev,
118 struct device_attribute *devattr,
119 char *buf);
120 int index;
123 struct aem_rw_sensor_template {
124 char *label;
125 ssize_t (*show)(struct device *dev,
126 struct device_attribute *devattr,
127 char *buf);
128 ssize_t (*set)(struct device *dev,
129 struct device_attribute *devattr,
130 const char *buf, size_t count);
131 int index;
134 struct aem_data {
135 struct list_head list;
137 struct device *hwmon_dev;
138 struct platform_device *pdev;
139 struct mutex lock;
140 char valid;
141 unsigned long last_updated; /* In jiffies */
142 u8 ver_major;
143 u8 ver_minor;
144 u8 module_handle;
145 int id;
146 struct aem_ipmi_data ipmi;
148 /* Function to update sensors */
149 void (*update)(struct aem_data *data);
152 * AEM 1.x sensors:
153 * Available sensors:
154 * Energy meter
155 * Power meter
157 * AEM 2.x sensors:
158 * Two energy meters
159 * Two power meters
160 * Two temperature sensors
161 * Six power cap registers
164 /* sysfs attrs */
165 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
167 /* energy use in mJ */
168 u64 energy[AEM_NUM_ENERGY_REGS];
170 /* power sampling interval in ms */
171 unsigned long power_period[AEM_NUM_ENERGY_REGS];
173 /* Everything past here is for AEM2 only */
175 /* power caps in dW */
176 u16 pcap[AEM2_NUM_PCAP_REGS];
178 /* exhaust temperature in C */
179 u8 temp[AEM2_NUM_TEMP_REGS];
182 /* Data structures returned by the AEM firmware */
183 struct aem_iana_id {
184 u8 bytes[3];
186 static struct aem_iana_id system_x_id = {
187 .bytes = {0x4D, 0x4F, 0x00}
190 /* These are used to find AEM1 instances */
191 struct aem_find_firmware_req {
192 struct aem_iana_id id;
193 u8 rsvd;
194 __be16 index;
195 __be16 module_type_id;
196 } __packed;
198 struct aem_find_firmware_resp {
199 struct aem_iana_id id;
200 u8 num_instances;
201 } __packed;
203 /* These are used to find AEM2 instances */
204 struct aem_find_instance_req {
205 struct aem_iana_id id;
206 u8 instance_number;
207 __be16 module_type_id;
208 } __packed;
210 struct aem_find_instance_resp {
211 struct aem_iana_id id;
212 u8 num_instances;
213 u8 major;
214 u8 minor;
215 u8 module_handle;
216 u16 record_id;
217 } __packed;
219 /* These are used to query sensors */
220 struct aem_read_sensor_req {
221 struct aem_iana_id id;
222 u8 module_handle;
223 u8 element;
224 u8 subcommand;
225 u8 reg;
226 u8 rx_buf_size;
227 } __packed;
229 struct aem_read_sensor_resp {
230 struct aem_iana_id id;
231 u8 bytes[0];
232 } __packed;
234 /* Data structures to talk to the IPMI layer */
235 struct aem_driver_data {
236 struct list_head aem_devices;
237 struct ipmi_smi_watcher bmc_events;
238 struct ipmi_user_hndl ipmi_hndlrs;
241 static void aem_register_bmc(int iface, struct device *dev);
242 static void aem_bmc_gone(int iface);
243 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
245 static void aem_remove_sensors(struct aem_data *data);
246 static int aem_init_aem1(struct aem_ipmi_data *probe);
247 static int aem_init_aem2(struct aem_ipmi_data *probe);
248 static int aem1_find_sensors(struct aem_data *data);
249 static int aem2_find_sensors(struct aem_data *data);
250 static void update_aem1_sensors(struct aem_data *data);
251 static void update_aem2_sensors(struct aem_data *data);
253 static 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,
260 .ipmi_hndlrs = {
261 .ipmi_recv_hndl = aem_msg_handler,
265 /* Functions to talk to the IPMI layer */
267 /* Initialize IPMI address, message buffers and user data */
268 static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
269 struct device *bmc)
271 int err;
273 init_completion(&data->read_complete);
274 data->bmc_device = bmc;
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;
282 /* Initialize message buffers */
283 data->tx_msgid = 0;
284 data->tx_message.netfn = AEM_NETFN;
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;
295 return 0;
298 /* Send an IPMI command */
299 static int aem_send_message(struct aem_ipmi_data *data)
301 int err;
303 err = ipmi_validate_addr(&data->address, sizeof(data->address));
304 if (err)
305 goto out;
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;
313 return 0;
314 out1:
315 dev_err(data->bmc_device, "request_settime=%x\n", err);
316 return err;
317 out:
318 dev_err(data->bmc_device, "validate_addr=%x\n", err);
319 return err;
322 /* Dispatch IPMI messages to callers */
323 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
325 unsigned short rx_len;
326 struct aem_ipmi_data *data = user_msg_data;
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;
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;
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;
352 ipmi_free_recv_msg(msg);
353 complete(&data->read_complete);
356 /* ID functions */
358 /* Obtain an id */
359 static int aem_idr_get(int *id)
361 int i, err;
363 again:
364 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
365 return -ENOMEM;
367 spin_lock(&aem_idr_lock);
368 err = idr_get_new(&aem_idr, NULL, &i);
369 spin_unlock(&aem_idr_lock);
371 if (unlikely(err == -EAGAIN))
372 goto again;
373 else if (unlikely(err))
374 return err;
376 *id = i & MAX_ID_MASK;
377 return 0;
380 /* Release an object ID */
381 static void aem_idr_put(int id)
383 spin_lock(&aem_idr_lock);
384 idr_remove(&aem_idr, id);
385 spin_unlock(&aem_idr_lock);
388 /* Sensor support functions */
390 /* Read a sensor value */
391 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
392 void *buf, size_t size)
394 int rs_size, res;
395 struct aem_read_sensor_req rs_req;
396 struct aem_read_sensor_resp *rs_resp;
397 struct aem_ipmi_data *ipmi = &data->ipmi;
399 /* AEM registers are 1, 2, 4 or 8 bytes */
400 switch (size) {
401 case 1:
402 case 2:
403 case 4:
404 case 8:
405 break;
406 default:
407 return -EINVAL;
410 rs_req.id = system_x_id;
411 rs_req.module_handle = data->module_handle;
412 rs_req.element = elt;
413 rs_req.subcommand = AEM_READ_REGISTER;
414 rs_req.reg = reg;
415 rs_req.rx_buf_size = size;
417 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
418 ipmi->tx_message.data = (char *)&rs_req;
419 ipmi->tx_message.data_len = sizeof(rs_req);
421 rs_size = sizeof(*rs_resp) + size;
422 rs_resp = kzalloc(rs_size, GFP_KERNEL);
423 if (!rs_resp)
424 return -ENOMEM;
426 ipmi->rx_msg_data = rs_resp;
427 ipmi->rx_msg_len = rs_size;
429 aem_send_message(ipmi);
431 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
432 if (!res)
433 return -ETIMEDOUT;
435 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
436 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
437 kfree(rs_resp);
438 return -ENOENT;
441 switch (size) {
442 case 1: {
443 u8 *x = buf;
444 *x = rs_resp->bytes[0];
445 break;
447 case 2: {
448 u16 *x = buf;
449 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
450 break;
452 case 4: {
453 u32 *x = buf;
454 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
455 break;
457 case 8: {
458 u64 *x = buf;
459 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
460 break;
464 return 0;
467 /* Update AEM energy registers */
468 static void update_aem_energy_one(struct aem_data *data, int which)
470 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
471 &data->energy[which], 8);
474 static void update_aem_energy(struct aem_data *data)
476 update_aem_energy_one(data, 0);
477 if (data->ver_major < 2)
478 return;
479 update_aem_energy_one(data, 1);
482 /* Update all AEM1 sensors */
483 static void update_aem1_sensors(struct aem_data *data)
485 mutex_lock(&data->lock);
486 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
487 data->valid)
488 goto out;
490 update_aem_energy(data);
491 out:
492 mutex_unlock(&data->lock);
495 /* Update all AEM2 sensors */
496 static void update_aem2_sensors(struct aem_data *data)
498 int i;
500 mutex_lock(&data->lock);
501 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
502 data->valid)
503 goto out;
505 update_aem_energy(data);
506 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
507 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
509 for (i = POWER_CAP; i <= POWER_AUX; i++)
510 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
511 &data->pcap[i], 2);
512 out:
513 mutex_unlock(&data->lock);
516 /* Delete an AEM instance */
517 static void aem_delete(struct aem_data *data)
519 list_del(&data->list);
520 aem_remove_sensors(data);
521 hwmon_device_unregister(data->hwmon_dev);
522 ipmi_destroy_user(data->ipmi.user);
523 dev_set_drvdata(&data->pdev->dev, NULL);
524 platform_device_unregister(data->pdev);
525 aem_idr_put(data->id);
526 kfree(data);
529 /* Probe functions for AEM1 devices */
531 /* Retrieve version and module handle for an AEM1 instance */
532 static int aem_find_aem1_count(struct aem_ipmi_data *data)
534 int res;
535 struct aem_find_firmware_req ff_req;
536 struct aem_find_firmware_resp ff_resp;
538 ff_req.id = system_x_id;
539 ff_req.index = 0;
540 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
542 data->tx_message.cmd = AEM_FIND_FW_CMD;
543 data->tx_message.data = (char *)&ff_req;
544 data->tx_message.data_len = sizeof(ff_req);
546 data->rx_msg_data = &ff_resp;
547 data->rx_msg_len = sizeof(ff_resp);
549 aem_send_message(data);
551 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
552 if (!res)
553 return -ETIMEDOUT;
555 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
556 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
557 return -ENOENT;
559 return ff_resp.num_instances;
562 /* Find and initialize one AEM1 instance */
563 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
565 struct aem_data *data;
566 int i;
567 int res = -ENOMEM;
569 data = kzalloc(sizeof(*data), GFP_KERNEL);
570 if (!data)
571 return res;
572 mutex_init(&data->lock);
574 /* Copy instance data */
575 data->ver_major = 1;
576 data->ver_minor = 0;
577 data->module_handle = module_handle;
578 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
579 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
581 /* Create sub-device for this fw instance */
582 if (aem_idr_get(&data->id))
583 goto id_err;
585 data->pdev = platform_device_alloc(DRVNAME, data->id);
586 if (!data->pdev)
587 goto dev_err;
588 data->pdev->dev.driver = &aem_driver.driver;
590 res = platform_device_add(data->pdev);
591 if (res)
592 goto ipmi_err;
594 dev_set_drvdata(&data->pdev->dev, data);
596 /* Set up IPMI interface */
597 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
598 probe->bmc_device))
599 goto ipmi_err;
601 /* Register with hwmon */
602 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
604 if (IS_ERR(data->hwmon_dev)) {
605 dev_err(&data->pdev->dev, "Unable to register hwmon "
606 "device for IPMI interface %d\n",
607 probe->interface);
608 goto hwmon_reg_err;
611 data->update = update_aem1_sensors;
613 /* Find sensors */
614 if (aem1_find_sensors(data))
615 goto sensor_err;
617 /* Add to our list of AEM devices */
618 list_add_tail(&data->list, &driver_data.aem_devices);
620 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
621 data->ver_major, data->ver_minor,
622 data->module_handle);
623 return 0;
625 sensor_err:
626 hwmon_device_unregister(data->hwmon_dev);
627 hwmon_reg_err:
628 ipmi_destroy_user(data->ipmi.user);
629 ipmi_err:
630 dev_set_drvdata(&data->pdev->dev, NULL);
631 platform_device_unregister(data->pdev);
632 dev_err:
633 aem_idr_put(data->id);
634 id_err:
635 kfree(data);
637 return res;
640 /* Find and initialize all AEM1 instances */
641 static int aem_init_aem1(struct aem_ipmi_data *probe)
643 int num, i, err;
645 num = aem_find_aem1_count(probe);
646 for (i = 0; i < num; i++) {
647 err = aem_init_aem1_inst(probe, i);
648 if (err) {
649 dev_err(probe->bmc_device,
650 "Error %d initializing AEM1 0x%X\n",
651 err, i);
652 return err;
656 return 0;
659 /* Probe functions for AEM2 devices */
661 /* Retrieve version and module handle for an AEM2 instance */
662 static int aem_find_aem2(struct aem_ipmi_data *data,
663 struct aem_find_instance_resp *fi_resp,
664 int instance_num)
666 int res;
667 struct aem_find_instance_req fi_req;
669 fi_req.id = system_x_id;
670 fi_req.instance_number = instance_num;
671 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
673 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
674 data->tx_message.data = (char *)&fi_req;
675 data->tx_message.data_len = sizeof(fi_req);
677 data->rx_msg_data = fi_resp;
678 data->rx_msg_len = sizeof(*fi_resp);
680 aem_send_message(data);
682 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
683 if (!res)
684 return -ETIMEDOUT;
686 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
687 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
688 fi_resp->num_instances <= instance_num)
689 return -ENOENT;
691 return 0;
694 /* Find and initialize one AEM2 instance */
695 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
696 struct aem_find_instance_resp *fi_resp)
698 struct aem_data *data;
699 int i;
700 int res = -ENOMEM;
702 data = kzalloc(sizeof(*data), GFP_KERNEL);
703 if (!data)
704 return res;
705 mutex_init(&data->lock);
707 /* Copy instance data */
708 data->ver_major = fi_resp->major;
709 data->ver_minor = fi_resp->minor;
710 data->module_handle = fi_resp->module_handle;
711 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
712 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
714 /* Create sub-device for this fw instance */
715 if (aem_idr_get(&data->id))
716 goto id_err;
718 data->pdev = platform_device_alloc(DRVNAME, data->id);
719 if (!data->pdev)
720 goto dev_err;
721 data->pdev->dev.driver = &aem_driver.driver;
723 res = platform_device_add(data->pdev);
724 if (res)
725 goto ipmi_err;
727 dev_set_drvdata(&data->pdev->dev, data);
729 /* Set up IPMI interface */
730 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
731 probe->bmc_device))
732 goto ipmi_err;
734 /* Register with hwmon */
735 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
737 if (IS_ERR(data->hwmon_dev)) {
738 dev_err(&data->pdev->dev, "Unable to register hwmon "
739 "device for IPMI interface %d\n",
740 probe->interface);
741 goto hwmon_reg_err;
744 data->update = update_aem2_sensors;
746 /* Find sensors */
747 if (aem2_find_sensors(data))
748 goto sensor_err;
750 /* Add to our list of AEM devices */
751 list_add_tail(&data->list, &driver_data.aem_devices);
753 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
754 data->ver_major, data->ver_minor,
755 data->module_handle);
756 return 0;
758 sensor_err:
759 hwmon_device_unregister(data->hwmon_dev);
760 hwmon_reg_err:
761 ipmi_destroy_user(data->ipmi.user);
762 ipmi_err:
763 dev_set_drvdata(&data->pdev->dev, NULL);
764 platform_device_unregister(data->pdev);
765 dev_err:
766 aem_idr_put(data->id);
767 id_err:
768 kfree(data);
770 return res;
773 /* Find and initialize all AEM2 instances */
774 static int aem_init_aem2(struct aem_ipmi_data *probe)
776 struct aem_find_instance_resp fi_resp;
777 int err;
778 int i = 0;
780 while (!aem_find_aem2(probe, &fi_resp, i)) {
781 if (fi_resp.major != 2) {
782 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
783 "report this to the maintainer.\n",
784 fi_resp.major);
785 i++;
786 continue;
788 err = aem_init_aem2_inst(probe, &fi_resp);
789 if (err) {
790 dev_err(probe->bmc_device,
791 "Error %d initializing AEM2 0x%X\n",
792 err, fi_resp.module_handle);
793 return err;
795 i++;
798 return 0;
801 /* Probe a BMC for AEM firmware instances */
802 static void aem_register_bmc(int iface, struct device *dev)
804 struct aem_ipmi_data probe;
806 if (aem_init_ipmi_data(&probe, iface, dev))
807 return;
809 /* Ignore probe errors; they won't cause problems */
810 aem_init_aem1(&probe);
811 aem_init_aem2(&probe);
813 ipmi_destroy_user(probe.user);
816 /* Handle BMC deletion */
817 static void aem_bmc_gone(int iface)
819 struct aem_data *p1, *next1;
821 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
822 if (p1->ipmi.interface == iface)
823 aem_delete(p1);
826 /* sysfs support functions */
828 /* AEM device name */
829 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
830 char *buf)
832 struct aem_data *data = dev_get_drvdata(dev);
834 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
836 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
838 /* AEM device version */
839 static ssize_t show_version(struct device *dev,
840 struct device_attribute *devattr,
841 char *buf)
843 struct aem_data *data = dev_get_drvdata(dev);
845 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
847 static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
849 /* Display power use */
850 static ssize_t aem_show_power(struct device *dev,
851 struct device_attribute *devattr,
852 char *buf)
854 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
855 struct aem_data *data = dev_get_drvdata(dev);
856 u64 before, after, delta, time;
857 signed long leftover;
858 struct timespec b, a;
860 mutex_lock(&data->lock);
861 update_aem_energy_one(data, attr->index);
862 getnstimeofday(&b);
863 before = data->energy[attr->index];
865 leftover = schedule_timeout_interruptible(
866 msecs_to_jiffies(data->power_period[attr->index])
868 if (leftover) {
869 mutex_unlock(&data->lock);
870 return 0;
873 update_aem_energy_one(data, attr->index);
874 getnstimeofday(&a);
875 after = data->energy[attr->index];
876 mutex_unlock(&data->lock);
878 time = timespec_to_ns(&a) - timespec_to_ns(&b);
879 delta = (after - before) * UJ_PER_MJ;
881 return sprintf(buf, "%llu\n",
882 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
885 /* Display energy use */
886 static ssize_t aem_show_energy(struct device *dev,
887 struct device_attribute *devattr,
888 char *buf)
890 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
891 struct aem_data *a = dev_get_drvdata(dev);
892 mutex_lock(&a->lock);
893 update_aem_energy_one(a, attr->index);
894 mutex_unlock(&a->lock);
896 return sprintf(buf, "%llu\n",
897 (unsigned long long)a->energy[attr->index] * 1000);
900 /* Display power interval registers */
901 static ssize_t aem_show_power_period(struct device *dev,
902 struct device_attribute *devattr,
903 char *buf)
905 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
906 struct aem_data *a = dev_get_drvdata(dev);
907 a->update(a);
909 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
912 /* Set power interval registers */
913 static ssize_t aem_set_power_period(struct device *dev,
914 struct device_attribute *devattr,
915 const char *buf, size_t count)
917 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
918 struct aem_data *a = dev_get_drvdata(dev);
919 unsigned long temp;
920 int res;
922 res = strict_strtoul(buf, 10, &temp);
923 if (res)
924 return res;
926 if (temp < AEM_MIN_POWER_INTERVAL)
927 return -EINVAL;
929 mutex_lock(&a->lock);
930 a->power_period[attr->index] = temp;
931 mutex_unlock(&a->lock);
933 return count;
936 /* Discover sensors on an AEM device */
937 static int aem_register_sensors(struct aem_data *data,
938 struct aem_ro_sensor_template *ro,
939 struct aem_rw_sensor_template *rw)
941 struct device *dev = &data->pdev->dev;
942 struct sensor_device_attribute *sensors = data->sensors;
943 int err;
945 /* Set up read-only sensors */
946 while (ro->label) {
947 sensors->dev_attr.attr.name = ro->label;
948 sensors->dev_attr.attr.mode = S_IRUGO;
949 sensors->dev_attr.show = ro->show;
950 sensors->index = ro->index;
952 err = device_create_file(dev, &sensors->dev_attr);
953 if (err) {
954 sensors->dev_attr.attr.name = NULL;
955 goto error;
957 sensors++;
958 ro++;
961 /* Set up read-write sensors */
962 while (rw->label) {
963 sensors->dev_attr.attr.name = rw->label;
964 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
965 sensors->dev_attr.show = rw->show;
966 sensors->dev_attr.store = rw->set;
967 sensors->index = rw->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 rw++;
978 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
979 if (err)
980 goto error;
981 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
982 return err;
984 error:
985 aem_remove_sensors(data);
986 return err;
989 /* sysfs support functions for AEM2 sensors */
991 /* Display temperature use */
992 static ssize_t aem2_show_temp(struct device *dev,
993 struct device_attribute *devattr,
994 char *buf)
996 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
997 struct aem_data *a = dev_get_drvdata(dev);
998 a->update(a);
1000 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1003 /* Display power-capping registers */
1004 static ssize_t aem2_show_pcap_value(struct device *dev,
1005 struct device_attribute *devattr,
1006 char *buf)
1008 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1009 struct aem_data *a = dev_get_drvdata(dev);
1010 a->update(a);
1012 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1015 /* Remove sensors attached to an AEM device */
1016 static void aem_remove_sensors(struct aem_data *data)
1018 int i;
1020 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1021 if (!data->sensors[i].dev_attr.attr.name)
1022 continue;
1023 device_remove_file(&data->pdev->dev,
1024 &data->sensors[i].dev_attr);
1027 device_remove_file(&data->pdev->dev,
1028 &sensor_dev_attr_name.dev_attr);
1029 device_remove_file(&data->pdev->dev,
1030 &sensor_dev_attr_version.dev_attr);
1033 /* Sensor probe functions */
1035 /* Description of AEM1 sensors */
1036 static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1037 {"energy1_input", aem_show_energy, 0},
1038 {"power1_average", aem_show_power, 0},
1039 {NULL, NULL, 0},
1042 static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1043 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1044 {NULL, NULL, NULL, 0},
1047 /* Description of AEM2 sensors */
1048 static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1049 {"energy1_input", aem_show_energy, 0},
1050 {"energy2_input", aem_show_energy, 1},
1051 {"power1_average", aem_show_power, 0},
1052 {"power2_average", aem_show_power, 1},
1053 {"temp1_input", aem2_show_temp, 0},
1054 {"temp2_input", aem2_show_temp, 1},
1056 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1057 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1058 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1059 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1061 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1062 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1063 {NULL, NULL, 0},
1066 static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1067 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1068 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1069 {NULL, NULL, NULL, 0},
1072 /* Set up AEM1 sensor attrs */
1073 static int aem1_find_sensors(struct aem_data *data)
1075 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1078 /* Set up AEM2 sensor attrs */
1079 static int aem2_find_sensors(struct aem_data *data)
1081 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1084 /* Module init/exit routines */
1086 static int __init aem_init(void)
1088 int res;
1090 res = driver_register(&aem_driver.driver);
1091 if (res) {
1092 printk(KERN_ERR "Can't register aem driver\n");
1093 return res;
1096 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1097 if (res)
1098 goto ipmi_reg_err;
1099 return 0;
1101 ipmi_reg_err:
1102 driver_unregister(&aem_driver.driver);
1103 return res;
1107 static void __exit aem_exit(void)
1109 struct aem_data *p1, *next1;
1111 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1112 driver_unregister(&aem_driver.driver);
1113 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1114 aem_delete(p1);
1117 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1118 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1119 MODULE_LICENSE("GPL");
1121 module_init(aem_init);
1122 module_exit(aem_exit);
1124 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1125 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1126 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1127 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1128 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1129 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1130 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");