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)
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
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
87 #define AEM_DEFAULT_POWER_INTERVAL 1000
88 #define AEM_MIN_POWER_INTERVAL 200
89 #define UJ_PER_MJ 1000L
91 static DEFINE_IDA(aem_ida
);
93 static struct platform_driver aem_driver
= {
96 .bus
= &platform_bus_type
,
100 struct aem_ipmi_data
{
101 struct completion read_complete
;
102 struct ipmi_addr address
;
106 struct kernel_ipmi_msg tx_message
;
110 unsigned short rx_msg_len
;
111 unsigned char rx_result
;
114 struct device
*bmc_device
;
117 struct aem_ro_sensor_template
{
119 ssize_t (*show
)(struct device
*dev
,
120 struct device_attribute
*devattr
,
125 struct aem_rw_sensor_template
{
127 ssize_t (*show
)(struct device
*dev
,
128 struct device_attribute
*devattr
,
130 ssize_t (*set
)(struct device
*dev
,
131 struct device_attribute
*devattr
,
132 const char *buf
, size_t count
);
137 struct list_head list
;
139 struct device
*hwmon_dev
;
140 struct platform_device
*pdev
;
143 unsigned long last_updated
; /* In jiffies */
148 struct aem_ipmi_data ipmi
;
150 /* Function and buffer to update sensors */
151 void (*update
)(struct aem_data
*data
);
152 struct aem_read_sensor_resp
*rs_resp
;
163 * Two temperature sensors
164 * Six power cap registers
168 struct sensor_device_attribute sensors
[AEM_NUM_SENSORS
];
170 /* energy use in mJ */
171 u64 energy
[AEM_NUM_ENERGY_REGS
];
173 /* power sampling interval in ms */
174 unsigned long power_period
[AEM_NUM_ENERGY_REGS
];
176 /* Everything past here is for AEM2 only */
178 /* power caps in dW */
179 u16 pcap
[AEM2_NUM_PCAP_REGS
];
181 /* exhaust temperature in C */
182 u8 temp
[AEM2_NUM_TEMP_REGS
];
185 /* Data structures returned by the AEM firmware */
189 static struct aem_iana_id system_x_id
= {
190 .bytes
= {0x4D, 0x4F, 0x00}
193 /* These are used to find AEM1 instances */
194 struct aem_find_firmware_req
{
195 struct aem_iana_id id
;
198 __be16 module_type_id
;
201 struct aem_find_firmware_resp
{
202 struct aem_iana_id id
;
206 /* These are used to find AEM2 instances */
207 struct aem_find_instance_req
{
208 struct aem_iana_id id
;
210 __be16 module_type_id
;
213 struct aem_find_instance_resp
{
214 struct aem_iana_id id
;
222 /* These are used to query sensors */
223 struct aem_read_sensor_req
{
224 struct aem_iana_id id
;
232 struct aem_read_sensor_resp
{
233 struct aem_iana_id id
;
237 /* Data structures to talk to the IPMI layer */
238 struct aem_driver_data
{
239 struct list_head aem_devices
;
240 struct ipmi_smi_watcher bmc_events
;
241 struct ipmi_user_hndl ipmi_hndlrs
;
244 static void aem_register_bmc(int iface
, struct device
*dev
);
245 static void aem_bmc_gone(int iface
);
246 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
248 static void aem_remove_sensors(struct aem_data
*data
);
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
);
357 /* Sensor support functions */
359 /* Read a sensor value; must be called with data->lock held */
360 static int aem_read_sensor(struct aem_data
*data
, u8 elt
, u8 reg
,
361 void *buf
, size_t size
)
364 struct aem_read_sensor_req rs_req
;
365 /* Use preallocated rx buffer */
366 struct aem_read_sensor_resp
*rs_resp
= data
->rs_resp
;
367 struct aem_ipmi_data
*ipmi
= &data
->ipmi
;
369 /* AEM registers are 1, 2, 4 or 8 bytes */
380 rs_req
.id
= system_x_id
;
381 rs_req
.module_handle
= data
->module_handle
;
382 rs_req
.element
= elt
;
383 rs_req
.subcommand
= AEM_READ_REGISTER
;
385 rs_req
.rx_buf_size
= size
;
387 ipmi
->tx_message
.cmd
= AEM_ELEMENT_CMD
;
388 ipmi
->tx_message
.data
= (char *)&rs_req
;
389 ipmi
->tx_message
.data_len
= sizeof(rs_req
);
391 rs_size
= sizeof(*rs_resp
) + size
;
392 ipmi
->rx_msg_data
= rs_resp
;
393 ipmi
->rx_msg_len
= rs_size
;
395 aem_send_message(ipmi
);
397 res
= wait_for_completion_timeout(&ipmi
->read_complete
, IPMI_TIMEOUT
);
403 if (ipmi
->rx_result
|| ipmi
->rx_msg_len
!= rs_size
||
404 memcmp(&rs_resp
->id
, &system_x_id
, sizeof(system_x_id
))) {
412 *x
= rs_resp
->bytes
[0];
417 *x
= be16_to_cpup((__be16
*)rs_resp
->bytes
);
422 *x
= be32_to_cpup((__be32
*)rs_resp
->bytes
);
427 *x
= be64_to_cpup((__be64
*)rs_resp
->bytes
);
437 /* Update AEM energy registers */
438 static void update_aem_energy_one(struct aem_data
*data
, int which
)
440 aem_read_sensor(data
, AEM_ENERGY_ELEMENT
, which
,
441 &data
->energy
[which
], 8);
444 static void update_aem_energy(struct aem_data
*data
)
446 update_aem_energy_one(data
, 0);
447 if (data
->ver_major
< 2)
449 update_aem_energy_one(data
, 1);
452 /* Update all AEM1 sensors */
453 static void update_aem1_sensors(struct aem_data
*data
)
455 mutex_lock(&data
->lock
);
456 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
460 update_aem_energy(data
);
462 mutex_unlock(&data
->lock
);
465 /* Update all AEM2 sensors */
466 static void update_aem2_sensors(struct aem_data
*data
)
470 mutex_lock(&data
->lock
);
471 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
475 update_aem_energy(data
);
476 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 0, &data
->temp
[0], 1);
477 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 1, &data
->temp
[1], 1);
479 for (i
= POWER_CAP
; i
<= POWER_AUX
; i
++)
480 aem_read_sensor(data
, AEM_POWER_CAP_ELEMENT
, i
,
483 mutex_unlock(&data
->lock
);
486 /* Delete an AEM instance */
487 static void aem_delete(struct aem_data
*data
)
489 list_del(&data
->list
);
490 aem_remove_sensors(data
);
491 kfree(data
->rs_resp
);
492 hwmon_device_unregister(data
->hwmon_dev
);
493 ipmi_destroy_user(data
->ipmi
.user
);
494 platform_set_drvdata(data
->pdev
, NULL
);
495 platform_device_unregister(data
->pdev
);
496 ida_simple_remove(&aem_ida
, data
->id
);
500 /* Probe functions for AEM1 devices */
502 /* Retrieve version and module handle for an AEM1 instance */
503 static int aem_find_aem1_count(struct aem_ipmi_data
*data
)
506 struct aem_find_firmware_req ff_req
;
507 struct aem_find_firmware_resp ff_resp
;
509 ff_req
.id
= system_x_id
;
511 ff_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
513 data
->tx_message
.cmd
= AEM_FIND_FW_CMD
;
514 data
->tx_message
.data
= (char *)&ff_req
;
515 data
->tx_message
.data_len
= sizeof(ff_req
);
517 data
->rx_msg_data
= &ff_resp
;
518 data
->rx_msg_len
= sizeof(ff_resp
);
520 aem_send_message(data
);
522 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
526 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(ff_resp
) ||
527 memcmp(&ff_resp
.id
, &system_x_id
, sizeof(system_x_id
)))
530 return ff_resp
.num_instances
;
533 /* Find and initialize one AEM1 instance */
534 static int aem_init_aem1_inst(struct aem_ipmi_data
*probe
, u8 module_handle
)
536 struct aem_data
*data
;
540 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
543 mutex_init(&data
->lock
);
545 /* Copy instance data */
548 data
->module_handle
= module_handle
;
549 for (i
= 0; i
< AEM1_NUM_ENERGY_REGS
; i
++)
550 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
552 /* Create sub-device for this fw instance */
553 data
->id
= ida_simple_get(&aem_ida
, 0, 0, GFP_KERNEL
);
557 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
560 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
562 res
= platform_device_add(data
->pdev
);
566 platform_set_drvdata(data
->pdev
, data
);
568 /* Set up IPMI interface */
569 res
= aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
574 /* Register with hwmon */
575 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
576 if (IS_ERR(data
->hwmon_dev
)) {
577 dev_err(&data
->pdev
->dev
, "Unable to register hwmon "
578 "device for IPMI interface %d\n",
580 res
= PTR_ERR(data
->hwmon_dev
);
584 data
->update
= update_aem1_sensors
;
585 data
->rs_resp
= kzalloc(sizeof(*(data
->rs_resp
)) + 8, GFP_KERNEL
);
586 if (!data
->rs_resp
) {
592 res
= aem1_find_sensors(data
);
596 /* Add to our list of AEM devices */
597 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
599 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
600 data
->ver_major
, data
->ver_minor
,
601 data
->module_handle
);
605 kfree(data
->rs_resp
);
607 hwmon_device_unregister(data
->hwmon_dev
);
609 ipmi_destroy_user(data
->ipmi
.user
);
611 platform_set_drvdata(data
->pdev
, NULL
);
612 platform_device_unregister(data
->pdev
);
614 ida_simple_remove(&aem_ida
, data
->id
);
621 /* Find and initialize all AEM1 instances */
622 static void aem_init_aem1(struct aem_ipmi_data
*probe
)
626 num
= aem_find_aem1_count(probe
);
627 for (i
= 0; i
< num
; i
++) {
628 err
= aem_init_aem1_inst(probe
, i
);
630 dev_err(probe
->bmc_device
,
631 "Error %d initializing AEM1 0x%X\n",
637 /* Probe functions for AEM2 devices */
639 /* Retrieve version and module handle for an AEM2 instance */
640 static int aem_find_aem2(struct aem_ipmi_data
*data
,
641 struct aem_find_instance_resp
*fi_resp
,
645 struct aem_find_instance_req fi_req
;
647 fi_req
.id
= system_x_id
;
648 fi_req
.instance_number
= instance_num
;
649 fi_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
651 data
->tx_message
.cmd
= AEM_FW_INSTANCE_CMD
;
652 data
->tx_message
.data
= (char *)&fi_req
;
653 data
->tx_message
.data_len
= sizeof(fi_req
);
655 data
->rx_msg_data
= fi_resp
;
656 data
->rx_msg_len
= sizeof(*fi_resp
);
658 aem_send_message(data
);
660 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
664 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(*fi_resp
) ||
665 memcmp(&fi_resp
->id
, &system_x_id
, sizeof(system_x_id
)) ||
666 fi_resp
->num_instances
<= instance_num
)
672 /* Find and initialize one AEM2 instance */
673 static int aem_init_aem2_inst(struct aem_ipmi_data
*probe
,
674 struct aem_find_instance_resp
*fi_resp
)
676 struct aem_data
*data
;
680 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
683 mutex_init(&data
->lock
);
685 /* Copy instance data */
686 data
->ver_major
= fi_resp
->major
;
687 data
->ver_minor
= fi_resp
->minor
;
688 data
->module_handle
= fi_resp
->module_handle
;
689 for (i
= 0; i
< AEM2_NUM_ENERGY_REGS
; i
++)
690 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
692 /* Create sub-device for this fw instance */
693 data
->id
= ida_simple_get(&aem_ida
, 0, 0, GFP_KERNEL
);
697 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
700 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
702 res
= platform_device_add(data
->pdev
);
706 platform_set_drvdata(data
->pdev
, data
);
708 /* Set up IPMI interface */
709 res
= aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
714 /* Register with hwmon */
715 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
716 if (IS_ERR(data
->hwmon_dev
)) {
717 dev_err(&data
->pdev
->dev
, "Unable to register hwmon "
718 "device for IPMI interface %d\n",
720 res
= PTR_ERR(data
->hwmon_dev
);
724 data
->update
= update_aem2_sensors
;
725 data
->rs_resp
= kzalloc(sizeof(*(data
->rs_resp
)) + 8, GFP_KERNEL
);
726 if (!data
->rs_resp
) {
732 res
= aem2_find_sensors(data
);
736 /* Add to our list of AEM devices */
737 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
739 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
740 data
->ver_major
, data
->ver_minor
,
741 data
->module_handle
);
745 kfree(data
->rs_resp
);
747 hwmon_device_unregister(data
->hwmon_dev
);
749 ipmi_destroy_user(data
->ipmi
.user
);
751 platform_set_drvdata(data
->pdev
, NULL
);
752 platform_device_unregister(data
->pdev
);
754 ida_simple_remove(&aem_ida
, data
->id
);
761 /* Find and initialize all AEM2 instances */
762 static void aem_init_aem2(struct aem_ipmi_data
*probe
)
764 struct aem_find_instance_resp fi_resp
;
768 while (!aem_find_aem2(probe
, &fi_resp
, i
)) {
769 if (fi_resp
.major
!= 2) {
770 dev_err(probe
->bmc_device
, "Unknown AEM v%d; please "
771 "report this to the maintainer.\n",
776 err
= aem_init_aem2_inst(probe
, &fi_resp
);
778 dev_err(probe
->bmc_device
,
779 "Error %d initializing AEM2 0x%X\n",
780 err
, fi_resp
.module_handle
);
786 /* Probe a BMC for AEM firmware instances */
787 static void aem_register_bmc(int iface
, struct device
*dev
)
789 struct aem_ipmi_data probe
;
791 if (aem_init_ipmi_data(&probe
, iface
, dev
))
794 /* Ignore probe errors; they won't cause problems */
795 aem_init_aem1(&probe
);
796 aem_init_aem2(&probe
);
798 ipmi_destroy_user(probe
.user
);
801 /* Handle BMC deletion */
802 static void aem_bmc_gone(int iface
)
804 struct aem_data
*p1
, *next1
;
806 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
807 if (p1
->ipmi
.interface
== iface
)
811 /* sysfs support functions */
813 /* AEM device name */
814 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*devattr
,
817 struct aem_data
*data
= dev_get_drvdata(dev
);
819 return sprintf(buf
, "%s%d\n", DRVNAME
, data
->ver_major
);
821 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, 0);
823 /* AEM device version */
824 static ssize_t
show_version(struct device
*dev
,
825 struct device_attribute
*devattr
,
828 struct aem_data
*data
= dev_get_drvdata(dev
);
830 return sprintf(buf
, "%d.%d\n", data
->ver_major
, data
->ver_minor
);
832 static SENSOR_DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
, 0);
834 /* Display power use */
835 static ssize_t
aem_show_power(struct device
*dev
,
836 struct device_attribute
*devattr
,
839 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
840 struct aem_data
*data
= dev_get_drvdata(dev
);
841 u64 before
, after
, delta
, time
;
842 signed long leftover
;
843 struct timespec b
, a
;
845 mutex_lock(&data
->lock
);
846 update_aem_energy_one(data
, attr
->index
);
848 before
= data
->energy
[attr
->index
];
850 leftover
= schedule_timeout_interruptible(
851 msecs_to_jiffies(data
->power_period
[attr
->index
])
854 mutex_unlock(&data
->lock
);
858 update_aem_energy_one(data
, attr
->index
);
860 after
= data
->energy
[attr
->index
];
861 mutex_unlock(&data
->lock
);
863 time
= timespec_to_ns(&a
) - timespec_to_ns(&b
);
864 delta
= (after
- before
) * UJ_PER_MJ
;
866 return sprintf(buf
, "%llu\n",
867 (unsigned long long)div64_u64(delta
* NSEC_PER_SEC
, time
));
870 /* Display energy use */
871 static ssize_t
aem_show_energy(struct device
*dev
,
872 struct device_attribute
*devattr
,
875 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
876 struct aem_data
*a
= dev_get_drvdata(dev
);
877 mutex_lock(&a
->lock
);
878 update_aem_energy_one(a
, attr
->index
);
879 mutex_unlock(&a
->lock
);
881 return sprintf(buf
, "%llu\n",
882 (unsigned long long)a
->energy
[attr
->index
] * 1000);
885 /* Display power interval registers */
886 static ssize_t
aem_show_power_period(struct device
*dev
,
887 struct device_attribute
*devattr
,
890 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
891 struct aem_data
*a
= dev_get_drvdata(dev
);
894 return sprintf(buf
, "%lu\n", a
->power_period
[attr
->index
]);
897 /* Set power interval registers */
898 static ssize_t
aem_set_power_period(struct device
*dev
,
899 struct device_attribute
*devattr
,
900 const char *buf
, size_t count
)
902 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
903 struct aem_data
*a
= dev_get_drvdata(dev
);
907 res
= kstrtoul(buf
, 10, &temp
);
911 if (temp
< AEM_MIN_POWER_INTERVAL
)
914 mutex_lock(&a
->lock
);
915 a
->power_period
[attr
->index
] = temp
;
916 mutex_unlock(&a
->lock
);
921 /* Discover sensors on an AEM device */
922 static int aem_register_sensors(struct aem_data
*data
,
923 struct aem_ro_sensor_template
*ro
,
924 struct aem_rw_sensor_template
*rw
)
926 struct device
*dev
= &data
->pdev
->dev
;
927 struct sensor_device_attribute
*sensors
= data
->sensors
;
930 /* Set up read-only sensors */
932 sysfs_attr_init(&sensors
->dev_attr
.attr
);
933 sensors
->dev_attr
.attr
.name
= ro
->label
;
934 sensors
->dev_attr
.attr
.mode
= S_IRUGO
;
935 sensors
->dev_attr
.show
= ro
->show
;
936 sensors
->index
= ro
->index
;
938 err
= device_create_file(dev
, &sensors
->dev_attr
);
940 sensors
->dev_attr
.attr
.name
= NULL
;
947 /* Set up read-write sensors */
949 sysfs_attr_init(&sensors
->dev_attr
.attr
);
950 sensors
->dev_attr
.attr
.name
= rw
->label
;
951 sensors
->dev_attr
.attr
.mode
= S_IRUGO
| S_IWUSR
;
952 sensors
->dev_attr
.show
= rw
->show
;
953 sensors
->dev_attr
.store
= rw
->set
;
954 sensors
->index
= rw
->index
;
956 err
= device_create_file(dev
, &sensors
->dev_attr
);
958 sensors
->dev_attr
.attr
.name
= NULL
;
965 err
= device_create_file(dev
, &sensor_dev_attr_name
.dev_attr
);
968 err
= device_create_file(dev
, &sensor_dev_attr_version
.dev_attr
);
972 aem_remove_sensors(data
);
976 /* sysfs support functions for AEM2 sensors */
978 /* Display temperature use */
979 static ssize_t
aem2_show_temp(struct device
*dev
,
980 struct device_attribute
*devattr
,
983 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
984 struct aem_data
*a
= dev_get_drvdata(dev
);
987 return sprintf(buf
, "%u\n", a
->temp
[attr
->index
] * 1000);
990 /* Display power-capping registers */
991 static ssize_t
aem2_show_pcap_value(struct device
*dev
,
992 struct device_attribute
*devattr
,
995 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
996 struct aem_data
*a
= dev_get_drvdata(dev
);
999 return sprintf(buf
, "%u\n", a
->pcap
[attr
->index
] * 100000);
1002 /* Remove sensors attached to an AEM device */
1003 static void aem_remove_sensors(struct aem_data
*data
)
1007 for (i
= 0; i
< AEM_NUM_SENSORS
; i
++) {
1008 if (!data
->sensors
[i
].dev_attr
.attr
.name
)
1010 device_remove_file(&data
->pdev
->dev
,
1011 &data
->sensors
[i
].dev_attr
);
1014 device_remove_file(&data
->pdev
->dev
,
1015 &sensor_dev_attr_name
.dev_attr
);
1016 device_remove_file(&data
->pdev
->dev
,
1017 &sensor_dev_attr_version
.dev_attr
);
1020 /* Sensor probe functions */
1022 /* Description of AEM1 sensors */
1023 static struct aem_ro_sensor_template aem1_ro_sensors
[] = {
1024 {"energy1_input", aem_show_energy
, 0},
1025 {"power1_average", aem_show_power
, 0},
1029 static struct aem_rw_sensor_template aem1_rw_sensors
[] = {
1030 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1031 {NULL
, NULL
, NULL
, 0},
1034 /* Description of AEM2 sensors */
1035 static struct aem_ro_sensor_template aem2_ro_sensors
[] = {
1036 {"energy1_input", aem_show_energy
, 0},
1037 {"energy2_input", aem_show_energy
, 1},
1038 {"power1_average", aem_show_power
, 0},
1039 {"power2_average", aem_show_power
, 1},
1040 {"temp1_input", aem2_show_temp
, 0},
1041 {"temp2_input", aem2_show_temp
, 1},
1043 {"power4_average", aem2_show_pcap_value
, POWER_CAP_MAX_HOTPLUG
},
1044 {"power5_average", aem2_show_pcap_value
, POWER_CAP_MAX
},
1045 {"power6_average", aem2_show_pcap_value
, POWER_CAP_MIN_WARNING
},
1046 {"power7_average", aem2_show_pcap_value
, POWER_CAP_MIN
},
1048 {"power3_average", aem2_show_pcap_value
, POWER_AUX
},
1049 {"power_cap", aem2_show_pcap_value
, POWER_CAP
},
1053 static struct aem_rw_sensor_template aem2_rw_sensors
[] = {
1054 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1055 {"power2_average_interval", aem_show_power_period
, aem_set_power_period
, 1},
1056 {NULL
, NULL
, NULL
, 0},
1059 /* Set up AEM1 sensor attrs */
1060 static int aem1_find_sensors(struct aem_data
*data
)
1062 return aem_register_sensors(data
, aem1_ro_sensors
, aem1_rw_sensors
);
1065 /* Set up AEM2 sensor attrs */
1066 static int aem2_find_sensors(struct aem_data
*data
)
1068 return aem_register_sensors(data
, aem2_ro_sensors
, aem2_rw_sensors
);
1071 /* Module init/exit routines */
1073 static int __init
aem_init(void)
1077 res
= driver_register(&aem_driver
.driver
);
1079 pr_err("Can't register aem driver\n");
1083 res
= ipmi_smi_watcher_register(&driver_data
.bmc_events
);
1089 driver_unregister(&aem_driver
.driver
);
1094 static void __exit
aem_exit(void)
1096 struct aem_data
*p1
, *next1
;
1098 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
1099 driver_unregister(&aem_driver
.driver
);
1100 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
1104 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1105 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1106 MODULE_LICENSE("GPL");
1108 module_init(aem_init
);
1109 module_exit(aem_exit
);
1111 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1112 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1113 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1114 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1115 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1116 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1117 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");