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/slab.h>
33 #include <linux/sched.h>
34 #include <linux/platform_device.h>
35 #include <linux/math64.h>
36 #include <linux/time.h>
38 #define REFRESH_INTERVAL (HZ)
39 #define IPMI_TIMEOUT (30 * HZ)
42 #define AEM_NETFN 0x2E
44 #define AEM_FIND_FW_CMD 0x80
45 #define AEM_ELEMENT_CMD 0x81
46 #define AEM_FW_INSTANCE_CMD 0x82
48 #define AEM_READ_ELEMENT_CFG 0x80
49 #define AEM_READ_BUFFER 0x81
50 #define AEM_READ_REGISTER 0x82
51 #define AEM_WRITE_REGISTER 0x83
52 #define AEM_SET_REG_MASK 0x84
53 #define AEM_CLEAR_REG_MASK 0x85
54 #define AEM_READ_ELEMENT_CFG2 0x86
56 #define AEM_CONTROL_ELEMENT 0
57 #define AEM_ENERGY_ELEMENT 1
58 #define AEM_CLOCK_ELEMENT 4
59 #define AEM_POWER_CAP_ELEMENT 7
60 #define AEM_EXHAUST_ELEMENT 9
61 #define AEM_POWER_ELEMENT 10
63 #define AEM_MODULE_TYPE_ID 0x0001
65 #define AEM2_NUM_ENERGY_REGS 2
66 #define AEM2_NUM_PCAP_REGS 6
67 #define AEM2_NUM_TEMP_REGS 2
68 #define AEM2_NUM_SENSORS 14
70 #define AEM1_NUM_ENERGY_REGS 1
71 #define AEM1_NUM_SENSORS 3
73 /* AEM 2.x has more energy registers */
74 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
75 /* AEM 2.x needs more sensor files */
76 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
79 #define POWER_CAP_MAX_HOTPLUG 1
80 #define POWER_CAP_MAX 2
81 #define POWER_CAP_MIN_WARNING 3
82 #define POWER_CAP_MIN 4
85 #define AEM_DEFAULT_POWER_INTERVAL 1000
86 #define AEM_MIN_POWER_INTERVAL 200
87 #define UJ_PER_MJ 1000L
89 static DEFINE_IDR(aem_idr
);
90 static DEFINE_SPINLOCK(aem_idr_lock
);
92 static struct platform_driver aem_driver
= {
95 .bus
= &platform_bus_type
,
99 struct aem_ipmi_data
{
100 struct completion read_complete
;
101 struct ipmi_addr address
;
105 struct kernel_ipmi_msg tx_message
;
109 unsigned short rx_msg_len
;
110 unsigned char rx_result
;
113 struct device
*bmc_device
;
116 struct aem_ro_sensor_template
{
118 ssize_t (*show
)(struct device
*dev
,
119 struct device_attribute
*devattr
,
124 struct aem_rw_sensor_template
{
126 ssize_t (*show
)(struct device
*dev
,
127 struct device_attribute
*devattr
,
129 ssize_t (*set
)(struct device
*dev
,
130 struct device_attribute
*devattr
,
131 const char *buf
, size_t count
);
136 struct list_head list
;
138 struct device
*hwmon_dev
;
139 struct platform_device
*pdev
;
142 unsigned long last_updated
; /* In jiffies */
147 struct aem_ipmi_data ipmi
;
149 /* Function to update sensors */
150 void (*update
)(struct aem_data
*data
);
161 * Two temperature sensors
162 * Six power cap registers
166 struct sensor_device_attribute sensors
[AEM_NUM_SENSORS
];
168 /* energy use in mJ */
169 u64 energy
[AEM_NUM_ENERGY_REGS
];
171 /* power sampling interval in ms */
172 unsigned long power_period
[AEM_NUM_ENERGY_REGS
];
174 /* Everything past here is for AEM2 only */
176 /* power caps in dW */
177 u16 pcap
[AEM2_NUM_PCAP_REGS
];
179 /* exhaust temperature in C */
180 u8 temp
[AEM2_NUM_TEMP_REGS
];
183 /* Data structures returned by the AEM firmware */
187 static struct aem_iana_id system_x_id
= {
188 .bytes
= {0x4D, 0x4F, 0x00}
191 /* These are used to find AEM1 instances */
192 struct aem_find_firmware_req
{
193 struct aem_iana_id id
;
196 __be16 module_type_id
;
199 struct aem_find_firmware_resp
{
200 struct aem_iana_id id
;
204 /* These are used to find AEM2 instances */
205 struct aem_find_instance_req
{
206 struct aem_iana_id id
;
208 __be16 module_type_id
;
211 struct aem_find_instance_resp
{
212 struct aem_iana_id id
;
220 /* These are used to query sensors */
221 struct aem_read_sensor_req
{
222 struct aem_iana_id id
;
230 struct aem_read_sensor_resp
{
231 struct aem_iana_id id
;
235 /* Data structures to talk to the IPMI layer */
236 struct aem_driver_data
{
237 struct list_head aem_devices
;
238 struct ipmi_smi_watcher bmc_events
;
239 struct ipmi_user_hndl ipmi_hndlrs
;
242 static void aem_register_bmc(int iface
, struct device
*dev
);
243 static void aem_bmc_gone(int iface
);
244 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
246 static void aem_remove_sensors(struct aem_data
*data
);
247 static int aem_init_aem1(struct aem_ipmi_data
*probe
);
248 static int aem_init_aem2(struct aem_ipmi_data
*probe
);
249 static int aem1_find_sensors(struct aem_data
*data
);
250 static int aem2_find_sensors(struct aem_data
*data
);
251 static void update_aem1_sensors(struct aem_data
*data
);
252 static void update_aem2_sensors(struct aem_data
*data
);
254 static struct aem_driver_data driver_data
= {
255 .aem_devices
= LIST_HEAD_INIT(driver_data
.aem_devices
),
257 .owner
= THIS_MODULE
,
258 .new_smi
= aem_register_bmc
,
259 .smi_gone
= aem_bmc_gone
,
262 .ipmi_recv_hndl
= aem_msg_handler
,
266 /* Functions to talk to the IPMI layer */
268 /* Initialize IPMI address, message buffers and user data */
269 static int aem_init_ipmi_data(struct aem_ipmi_data
*data
, int iface
,
274 init_completion(&data
->read_complete
);
275 data
->bmc_device
= bmc
;
277 /* Initialize IPMI address */
278 data
->address
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
279 data
->address
.channel
= IPMI_BMC_CHANNEL
;
280 data
->address
.data
[0] = 0;
281 data
->interface
= iface
;
283 /* Initialize message buffers */
285 data
->tx_message
.netfn
= AEM_NETFN
;
287 /* Create IPMI messaging interface user */
288 err
= ipmi_create_user(data
->interface
, &driver_data
.ipmi_hndlrs
,
291 dev_err(bmc
, "Unable to register user with IPMI "
292 "interface %d\n", data
->interface
);
299 /* Send an IPMI command */
300 static int aem_send_message(struct aem_ipmi_data
*data
)
304 err
= ipmi_validate_addr(&data
->address
, sizeof(data
->address
));
309 err
= ipmi_request_settime(data
->user
, &data
->address
, data
->tx_msgid
,
310 &data
->tx_message
, data
, 0, 0, 0);
316 dev_err(data
->bmc_device
, "request_settime=%x\n", err
);
319 dev_err(data
->bmc_device
, "validate_addr=%x\n", err
);
323 /* Dispatch IPMI messages to callers */
324 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
)
326 unsigned short rx_len
;
327 struct aem_ipmi_data
*data
= user_msg_data
;
329 if (msg
->msgid
!= data
->tx_msgid
) {
330 dev_err(data
->bmc_device
, "Mismatch between received msgid "
331 "(%02x) and transmitted msgid (%02x)!\n",
333 (int)data
->tx_msgid
);
334 ipmi_free_recv_msg(msg
);
338 data
->rx_recv_type
= msg
->recv_type
;
339 if (msg
->msg
.data_len
> 0)
340 data
->rx_result
= msg
->msg
.data
[0];
342 data
->rx_result
= IPMI_UNKNOWN_ERR_COMPLETION_CODE
;
344 if (msg
->msg
.data_len
> 1) {
345 rx_len
= msg
->msg
.data_len
- 1;
346 if (data
->rx_msg_len
< rx_len
)
347 rx_len
= data
->rx_msg_len
;
348 data
->rx_msg_len
= rx_len
;
349 memcpy(data
->rx_msg_data
, msg
->msg
.data
+ 1, data
->rx_msg_len
);
351 data
->rx_msg_len
= 0;
353 ipmi_free_recv_msg(msg
);
354 complete(&data
->read_complete
);
360 static int aem_idr_get(int *id
)
365 if (unlikely(!idr_pre_get(&aem_idr
, GFP_KERNEL
)))
368 spin_lock(&aem_idr_lock
);
369 err
= idr_get_new(&aem_idr
, NULL
, &i
);
370 spin_unlock(&aem_idr_lock
);
372 if (unlikely(err
== -EAGAIN
))
374 else if (unlikely(err
))
377 *id
= i
& MAX_ID_MASK
;
381 /* Release an object ID */
382 static void aem_idr_put(int id
)
384 spin_lock(&aem_idr_lock
);
385 idr_remove(&aem_idr
, id
);
386 spin_unlock(&aem_idr_lock
);
389 /* Sensor support functions */
391 /* Read a sensor value */
392 static int aem_read_sensor(struct aem_data
*data
, u8 elt
, u8 reg
,
393 void *buf
, size_t size
)
396 struct aem_read_sensor_req rs_req
;
397 struct aem_read_sensor_resp
*rs_resp
;
398 struct aem_ipmi_data
*ipmi
= &data
->ipmi
;
400 /* AEM registers are 1, 2, 4 or 8 bytes */
411 rs_req
.id
= system_x_id
;
412 rs_req
.module_handle
= data
->module_handle
;
413 rs_req
.element
= elt
;
414 rs_req
.subcommand
= AEM_READ_REGISTER
;
416 rs_req
.rx_buf_size
= size
;
418 ipmi
->tx_message
.cmd
= AEM_ELEMENT_CMD
;
419 ipmi
->tx_message
.data
= (char *)&rs_req
;
420 ipmi
->tx_message
.data_len
= sizeof(rs_req
);
422 rs_size
= sizeof(*rs_resp
) + size
;
423 rs_resp
= kzalloc(rs_size
, GFP_KERNEL
);
427 ipmi
->rx_msg_data
= rs_resp
;
428 ipmi
->rx_msg_len
= rs_size
;
430 aem_send_message(ipmi
);
432 res
= wait_for_completion_timeout(&ipmi
->read_complete
, IPMI_TIMEOUT
);
436 if (ipmi
->rx_result
|| ipmi
->rx_msg_len
!= rs_size
||
437 memcmp(&rs_resp
->id
, &system_x_id
, sizeof(system_x_id
))) {
445 *x
= rs_resp
->bytes
[0];
450 *x
= be16_to_cpup((__be16
*)rs_resp
->bytes
);
455 *x
= be32_to_cpup((__be32
*)rs_resp
->bytes
);
460 *x
= be64_to_cpup((__be64
*)rs_resp
->bytes
);
468 /* Update AEM energy registers */
469 static void update_aem_energy_one(struct aem_data
*data
, int which
)
471 aem_read_sensor(data
, AEM_ENERGY_ELEMENT
, which
,
472 &data
->energy
[which
], 8);
475 static void update_aem_energy(struct aem_data
*data
)
477 update_aem_energy_one(data
, 0);
478 if (data
->ver_major
< 2)
480 update_aem_energy_one(data
, 1);
483 /* Update all AEM1 sensors */
484 static void update_aem1_sensors(struct aem_data
*data
)
486 mutex_lock(&data
->lock
);
487 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
491 update_aem_energy(data
);
493 mutex_unlock(&data
->lock
);
496 /* Update all AEM2 sensors */
497 static void update_aem2_sensors(struct aem_data
*data
)
501 mutex_lock(&data
->lock
);
502 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
506 update_aem_energy(data
);
507 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 0, &data
->temp
[0], 1);
508 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 1, &data
->temp
[1], 1);
510 for (i
= POWER_CAP
; i
<= POWER_AUX
; i
++)
511 aem_read_sensor(data
, AEM_POWER_CAP_ELEMENT
, i
,
514 mutex_unlock(&data
->lock
);
517 /* Delete an AEM instance */
518 static void aem_delete(struct aem_data
*data
)
520 list_del(&data
->list
);
521 aem_remove_sensors(data
);
522 hwmon_device_unregister(data
->hwmon_dev
);
523 ipmi_destroy_user(data
->ipmi
.user
);
524 dev_set_drvdata(&data
->pdev
->dev
, NULL
);
525 platform_device_unregister(data
->pdev
);
526 aem_idr_put(data
->id
);
530 /* Probe functions for AEM1 devices */
532 /* Retrieve version and module handle for an AEM1 instance */
533 static int aem_find_aem1_count(struct aem_ipmi_data
*data
)
536 struct aem_find_firmware_req ff_req
;
537 struct aem_find_firmware_resp ff_resp
;
539 ff_req
.id
= system_x_id
;
541 ff_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
543 data
->tx_message
.cmd
= AEM_FIND_FW_CMD
;
544 data
->tx_message
.data
= (char *)&ff_req
;
545 data
->tx_message
.data_len
= sizeof(ff_req
);
547 data
->rx_msg_data
= &ff_resp
;
548 data
->rx_msg_len
= sizeof(ff_resp
);
550 aem_send_message(data
);
552 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
556 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(ff_resp
) ||
557 memcmp(&ff_resp
.id
, &system_x_id
, sizeof(system_x_id
)))
560 return ff_resp
.num_instances
;
563 /* Find and initialize one AEM1 instance */
564 static int aem_init_aem1_inst(struct aem_ipmi_data
*probe
, u8 module_handle
)
566 struct aem_data
*data
;
570 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
573 mutex_init(&data
->lock
);
575 /* Copy instance data */
578 data
->module_handle
= module_handle
;
579 for (i
= 0; i
< AEM1_NUM_ENERGY_REGS
; i
++)
580 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
582 /* Create sub-device for this fw instance */
583 if (aem_idr_get(&data
->id
))
586 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
589 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
591 res
= platform_device_add(data
->pdev
);
595 dev_set_drvdata(&data
->pdev
->dev
, data
);
597 /* Set up IPMI interface */
598 if (aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
602 /* Register with hwmon */
603 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
605 if (IS_ERR(data
->hwmon_dev
)) {
606 dev_err(&data
->pdev
->dev
, "Unable to register hwmon "
607 "device for IPMI interface %d\n",
612 data
->update
= update_aem1_sensors
;
615 if (aem1_find_sensors(data
))
618 /* Add to our list of AEM devices */
619 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
621 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
622 data
->ver_major
, data
->ver_minor
,
623 data
->module_handle
);
627 hwmon_device_unregister(data
->hwmon_dev
);
629 ipmi_destroy_user(data
->ipmi
.user
);
631 dev_set_drvdata(&data
->pdev
->dev
, NULL
);
632 platform_device_unregister(data
->pdev
);
634 aem_idr_put(data
->id
);
641 /* Find and initialize all AEM1 instances */
642 static int aem_init_aem1(struct aem_ipmi_data
*probe
)
646 num
= aem_find_aem1_count(probe
);
647 for (i
= 0; i
< num
; i
++) {
648 err
= aem_init_aem1_inst(probe
, i
);
650 dev_err(probe
->bmc_device
,
651 "Error %d initializing AEM1 0x%X\n",
660 /* Probe functions for AEM2 devices */
662 /* Retrieve version and module handle for an AEM2 instance */
663 static int aem_find_aem2(struct aem_ipmi_data
*data
,
664 struct aem_find_instance_resp
*fi_resp
,
668 struct aem_find_instance_req fi_req
;
670 fi_req
.id
= system_x_id
;
671 fi_req
.instance_number
= instance_num
;
672 fi_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
674 data
->tx_message
.cmd
= AEM_FW_INSTANCE_CMD
;
675 data
->tx_message
.data
= (char *)&fi_req
;
676 data
->tx_message
.data_len
= sizeof(fi_req
);
678 data
->rx_msg_data
= fi_resp
;
679 data
->rx_msg_len
= sizeof(*fi_resp
);
681 aem_send_message(data
);
683 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
687 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(*fi_resp
) ||
688 memcmp(&fi_resp
->id
, &system_x_id
, sizeof(system_x_id
)) ||
689 fi_resp
->num_instances
<= instance_num
)
695 /* Find and initialize one AEM2 instance */
696 static int aem_init_aem2_inst(struct aem_ipmi_data
*probe
,
697 struct aem_find_instance_resp
*fi_resp
)
699 struct aem_data
*data
;
703 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
706 mutex_init(&data
->lock
);
708 /* Copy instance data */
709 data
->ver_major
= fi_resp
->major
;
710 data
->ver_minor
= fi_resp
->minor
;
711 data
->module_handle
= fi_resp
->module_handle
;
712 for (i
= 0; i
< AEM2_NUM_ENERGY_REGS
; i
++)
713 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
715 /* Create sub-device for this fw instance */
716 if (aem_idr_get(&data
->id
))
719 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
722 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
724 res
= platform_device_add(data
->pdev
);
728 dev_set_drvdata(&data
->pdev
->dev
, data
);
730 /* Set up IPMI interface */
731 if (aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
735 /* Register with hwmon */
736 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
738 if (IS_ERR(data
->hwmon_dev
)) {
739 dev_err(&data
->pdev
->dev
, "Unable to register hwmon "
740 "device for IPMI interface %d\n",
745 data
->update
= update_aem2_sensors
;
748 if (aem2_find_sensors(data
))
751 /* Add to our list of AEM devices */
752 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
754 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
755 data
->ver_major
, data
->ver_minor
,
756 data
->module_handle
);
760 hwmon_device_unregister(data
->hwmon_dev
);
762 ipmi_destroy_user(data
->ipmi
.user
);
764 dev_set_drvdata(&data
->pdev
->dev
, NULL
);
765 platform_device_unregister(data
->pdev
);
767 aem_idr_put(data
->id
);
774 /* Find and initialize all AEM2 instances */
775 static int aem_init_aem2(struct aem_ipmi_data
*probe
)
777 struct aem_find_instance_resp fi_resp
;
781 while (!aem_find_aem2(probe
, &fi_resp
, i
)) {
782 if (fi_resp
.major
!= 2) {
783 dev_err(probe
->bmc_device
, "Unknown AEM v%d; please "
784 "report this to the maintainer.\n",
789 err
= aem_init_aem2_inst(probe
, &fi_resp
);
791 dev_err(probe
->bmc_device
,
792 "Error %d initializing AEM2 0x%X\n",
793 err
, fi_resp
.module_handle
);
802 /* Probe a BMC for AEM firmware instances */
803 static void aem_register_bmc(int iface
, struct device
*dev
)
805 struct aem_ipmi_data probe
;
807 if (aem_init_ipmi_data(&probe
, iface
, dev
))
810 /* Ignore probe errors; they won't cause problems */
811 aem_init_aem1(&probe
);
812 aem_init_aem2(&probe
);
814 ipmi_destroy_user(probe
.user
);
817 /* Handle BMC deletion */
818 static void aem_bmc_gone(int iface
)
820 struct aem_data
*p1
, *next1
;
822 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
823 if (p1
->ipmi
.interface
== iface
)
827 /* sysfs support functions */
829 /* AEM device name */
830 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*devattr
,
833 struct aem_data
*data
= dev_get_drvdata(dev
);
835 return sprintf(buf
, "%s%d\n", DRVNAME
, data
->ver_major
);
837 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, 0);
839 /* AEM device version */
840 static ssize_t
show_version(struct device
*dev
,
841 struct device_attribute
*devattr
,
844 struct aem_data
*data
= dev_get_drvdata(dev
);
846 return sprintf(buf
, "%d.%d\n", data
->ver_major
, data
->ver_minor
);
848 static SENSOR_DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
, 0);
850 /* Display power use */
851 static ssize_t
aem_show_power(struct device
*dev
,
852 struct device_attribute
*devattr
,
855 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
856 struct aem_data
*data
= dev_get_drvdata(dev
);
857 u64 before
, after
, delta
, time
;
858 signed long leftover
;
859 struct timespec b
, a
;
861 mutex_lock(&data
->lock
);
862 update_aem_energy_one(data
, attr
->index
);
864 before
= data
->energy
[attr
->index
];
866 leftover
= schedule_timeout_interruptible(
867 msecs_to_jiffies(data
->power_period
[attr
->index
])
870 mutex_unlock(&data
->lock
);
874 update_aem_energy_one(data
, attr
->index
);
876 after
= data
->energy
[attr
->index
];
877 mutex_unlock(&data
->lock
);
879 time
= timespec_to_ns(&a
) - timespec_to_ns(&b
);
880 delta
= (after
- before
) * UJ_PER_MJ
;
882 return sprintf(buf
, "%llu\n",
883 (unsigned long long)div64_u64(delta
* NSEC_PER_SEC
, time
));
886 /* Display energy use */
887 static ssize_t
aem_show_energy(struct device
*dev
,
888 struct device_attribute
*devattr
,
891 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
892 struct aem_data
*a
= dev_get_drvdata(dev
);
893 mutex_lock(&a
->lock
);
894 update_aem_energy_one(a
, attr
->index
);
895 mutex_unlock(&a
->lock
);
897 return sprintf(buf
, "%llu\n",
898 (unsigned long long)a
->energy
[attr
->index
] * 1000);
901 /* Display power interval registers */
902 static ssize_t
aem_show_power_period(struct device
*dev
,
903 struct device_attribute
*devattr
,
906 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
907 struct aem_data
*a
= dev_get_drvdata(dev
);
910 return sprintf(buf
, "%lu\n", a
->power_period
[attr
->index
]);
913 /* Set power interval registers */
914 static ssize_t
aem_set_power_period(struct device
*dev
,
915 struct device_attribute
*devattr
,
916 const char *buf
, size_t count
)
918 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
919 struct aem_data
*a
= dev_get_drvdata(dev
);
923 res
= strict_strtoul(buf
, 10, &temp
);
927 if (temp
< AEM_MIN_POWER_INTERVAL
)
930 mutex_lock(&a
->lock
);
931 a
->power_period
[attr
->index
] = temp
;
932 mutex_unlock(&a
->lock
);
937 /* Discover sensors on an AEM device */
938 static int aem_register_sensors(struct aem_data
*data
,
939 struct aem_ro_sensor_template
*ro
,
940 struct aem_rw_sensor_template
*rw
)
942 struct device
*dev
= &data
->pdev
->dev
;
943 struct sensor_device_attribute
*sensors
= data
->sensors
;
946 /* Set up read-only sensors */
948 sensors
->dev_attr
.attr
.name
= ro
->label
;
949 sensors
->dev_attr
.attr
.mode
= S_IRUGO
;
950 sensors
->dev_attr
.show
= ro
->show
;
951 sensors
->index
= ro
->index
;
953 err
= device_create_file(dev
, &sensors
->dev_attr
);
955 sensors
->dev_attr
.attr
.name
= NULL
;
962 /* Set up read-write sensors */
964 sensors
->dev_attr
.attr
.name
= rw
->label
;
965 sensors
->dev_attr
.attr
.mode
= S_IRUGO
| S_IWUSR
;
966 sensors
->dev_attr
.show
= rw
->show
;
967 sensors
->dev_attr
.store
= rw
->set
;
968 sensors
->index
= rw
->index
;
970 err
= device_create_file(dev
, &sensors
->dev_attr
);
972 sensors
->dev_attr
.attr
.name
= NULL
;
979 err
= device_create_file(dev
, &sensor_dev_attr_name
.dev_attr
);
982 err
= device_create_file(dev
, &sensor_dev_attr_version
.dev_attr
);
986 aem_remove_sensors(data
);
990 /* sysfs support functions for AEM2 sensors */
992 /* Display temperature use */
993 static ssize_t
aem2_show_temp(struct device
*dev
,
994 struct device_attribute
*devattr
,
997 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
998 struct aem_data
*a
= dev_get_drvdata(dev
);
1001 return sprintf(buf
, "%u\n", a
->temp
[attr
->index
] * 1000);
1004 /* Display power-capping registers */
1005 static ssize_t
aem2_show_pcap_value(struct device
*dev
,
1006 struct device_attribute
*devattr
,
1009 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
1010 struct aem_data
*a
= dev_get_drvdata(dev
);
1013 return sprintf(buf
, "%u\n", a
->pcap
[attr
->index
] * 100000);
1016 /* Remove sensors attached to an AEM device */
1017 static void aem_remove_sensors(struct aem_data
*data
)
1021 for (i
= 0; i
< AEM_NUM_SENSORS
; i
++) {
1022 if (!data
->sensors
[i
].dev_attr
.attr
.name
)
1024 device_remove_file(&data
->pdev
->dev
,
1025 &data
->sensors
[i
].dev_attr
);
1028 device_remove_file(&data
->pdev
->dev
,
1029 &sensor_dev_attr_name
.dev_attr
);
1030 device_remove_file(&data
->pdev
->dev
,
1031 &sensor_dev_attr_version
.dev_attr
);
1034 /* Sensor probe functions */
1036 /* Description of AEM1 sensors */
1037 static struct aem_ro_sensor_template aem1_ro_sensors
[] = {
1038 {"energy1_input", aem_show_energy
, 0},
1039 {"power1_average", aem_show_power
, 0},
1043 static struct aem_rw_sensor_template aem1_rw_sensors
[] = {
1044 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1045 {NULL
, NULL
, NULL
, 0},
1048 /* Description of AEM2 sensors */
1049 static struct aem_ro_sensor_template aem2_ro_sensors
[] = {
1050 {"energy1_input", aem_show_energy
, 0},
1051 {"energy2_input", aem_show_energy
, 1},
1052 {"power1_average", aem_show_power
, 0},
1053 {"power2_average", aem_show_power
, 1},
1054 {"temp1_input", aem2_show_temp
, 0},
1055 {"temp2_input", aem2_show_temp
, 1},
1057 {"power4_average", aem2_show_pcap_value
, POWER_CAP_MAX_HOTPLUG
},
1058 {"power5_average", aem2_show_pcap_value
, POWER_CAP_MAX
},
1059 {"power6_average", aem2_show_pcap_value
, POWER_CAP_MIN_WARNING
},
1060 {"power7_average", aem2_show_pcap_value
, POWER_CAP_MIN
},
1062 {"power3_average", aem2_show_pcap_value
, POWER_AUX
},
1063 {"power_cap", aem2_show_pcap_value
, POWER_CAP
},
1067 static struct aem_rw_sensor_template aem2_rw_sensors
[] = {
1068 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1069 {"power2_average_interval", aem_show_power_period
, aem_set_power_period
, 1},
1070 {NULL
, NULL
, NULL
, 0},
1073 /* Set up AEM1 sensor attrs */
1074 static int aem1_find_sensors(struct aem_data
*data
)
1076 return aem_register_sensors(data
, aem1_ro_sensors
, aem1_rw_sensors
);
1079 /* Set up AEM2 sensor attrs */
1080 static int aem2_find_sensors(struct aem_data
*data
)
1082 return aem_register_sensors(data
, aem2_ro_sensors
, aem2_rw_sensors
);
1085 /* Module init/exit routines */
1087 static int __init
aem_init(void)
1091 res
= driver_register(&aem_driver
.driver
);
1093 printk(KERN_ERR
"Can't register aem driver\n");
1097 res
= ipmi_smi_watcher_register(&driver_data
.bmc_events
);
1103 driver_unregister(&aem_driver
.driver
);
1108 static void __exit
aem_exit(void)
1110 struct aem_data
*p1
, *next1
;
1112 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
1113 driver_unregister(&aem_driver
.driver
);
1114 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
1118 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1119 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1120 MODULE_LICENSE("GPL");
1122 module_init(aem_init
);
1123 module_exit(aem_exit
);
1125 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1126 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1127 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1128 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1129 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1130 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1131 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");