1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
4 * temperature/power/energy sensors and capping functionality.
5 * Copyright (C) 2008 IBM
7 * Author: Darrick J. Wong <darrick.wong@oracle.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/ipmi.h>
13 #include <linux/module.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/jiffies.h>
17 #include <linux/mutex.h>
18 #include <linux/kdev_t.h>
19 #include <linux/spinlock.h>
20 #include <linux/idr.h>
21 #include <linux/slab.h>
22 #include <linux/sched.h>
23 #include <linux/platform_device.h>
24 #include <linux/math64.h>
25 #include <linux/time.h>
26 #include <linux/err.h>
28 #define REFRESH_INTERVAL (HZ)
29 #define IPMI_TIMEOUT (30 * HZ)
32 #define AEM_NETFN 0x2E
34 #define AEM_FIND_FW_CMD 0x80
35 #define AEM_ELEMENT_CMD 0x81
36 #define AEM_FW_INSTANCE_CMD 0x82
38 #define AEM_READ_ELEMENT_CFG 0x80
39 #define AEM_READ_BUFFER 0x81
40 #define AEM_READ_REGISTER 0x82
41 #define AEM_WRITE_REGISTER 0x83
42 #define AEM_SET_REG_MASK 0x84
43 #define AEM_CLEAR_REG_MASK 0x85
44 #define AEM_READ_ELEMENT_CFG2 0x86
46 #define AEM_CONTROL_ELEMENT 0
47 #define AEM_ENERGY_ELEMENT 1
48 #define AEM_CLOCK_ELEMENT 4
49 #define AEM_POWER_CAP_ELEMENT 7
50 #define AEM_EXHAUST_ELEMENT 9
51 #define AEM_POWER_ELEMENT 10
53 #define AEM_MODULE_TYPE_ID 0x0001
55 #define AEM2_NUM_ENERGY_REGS 2
56 #define AEM2_NUM_PCAP_REGS 6
57 #define AEM2_NUM_TEMP_REGS 2
58 #define AEM2_NUM_SENSORS 14
60 #define AEM1_NUM_ENERGY_REGS 1
61 #define AEM1_NUM_SENSORS 3
63 /* AEM 2.x has more energy registers */
64 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
65 /* AEM 2.x needs more sensor files */
66 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
69 #define POWER_CAP_MAX_HOTPLUG 1
70 #define POWER_CAP_MAX 2
71 #define POWER_CAP_MIN_WARNING 3
72 #define POWER_CAP_MIN 4
75 #define AEM_DEFAULT_POWER_INTERVAL 1000
76 #define AEM_MIN_POWER_INTERVAL 200
77 #define UJ_PER_MJ 1000L
79 static DEFINE_IDA(aem_ida
);
81 static struct platform_driver aem_driver
= {
84 .bus
= &platform_bus_type
,
88 struct aem_ipmi_data
{
89 struct completion read_complete
;
90 struct ipmi_addr address
;
91 struct ipmi_user
*user
;
94 struct kernel_ipmi_msg tx_message
;
98 unsigned short rx_msg_len
;
99 unsigned char rx_result
;
102 struct device
*bmc_device
;
105 struct aem_ro_sensor_template
{
107 ssize_t (*show
)(struct device
*dev
,
108 struct device_attribute
*devattr
,
113 struct aem_rw_sensor_template
{
115 ssize_t (*show
)(struct device
*dev
,
116 struct device_attribute
*devattr
,
118 ssize_t (*set
)(struct device
*dev
,
119 struct device_attribute
*devattr
,
120 const char *buf
, size_t count
);
125 struct list_head list
;
127 struct device
*hwmon_dev
;
128 struct platform_device
*pdev
;
131 unsigned long last_updated
; /* In jiffies */
136 struct aem_ipmi_data ipmi
;
138 /* Function and buffer to update sensors */
139 void (*update
)(struct aem_data
*data
);
140 struct aem_read_sensor_resp
*rs_resp
;
151 * Two temperature sensors
152 * Six power cap registers
156 struct sensor_device_attribute sensors
[AEM_NUM_SENSORS
];
158 /* energy use in mJ */
159 u64 energy
[AEM_NUM_ENERGY_REGS
];
161 /* power sampling interval in ms */
162 unsigned long power_period
[AEM_NUM_ENERGY_REGS
];
164 /* Everything past here is for AEM2 only */
166 /* power caps in dW */
167 u16 pcap
[AEM2_NUM_PCAP_REGS
];
169 /* exhaust temperature in C */
170 u8 temp
[AEM2_NUM_TEMP_REGS
];
173 /* Data structures returned by the AEM firmware */
177 static struct aem_iana_id system_x_id
= {
178 .bytes
= {0x4D, 0x4F, 0x00}
181 /* These are used to find AEM1 instances */
182 struct aem_find_firmware_req
{
183 struct aem_iana_id id
;
186 __be16 module_type_id
;
189 struct aem_find_firmware_resp
{
190 struct aem_iana_id id
;
194 /* These are used to find AEM2 instances */
195 struct aem_find_instance_req
{
196 struct aem_iana_id id
;
198 __be16 module_type_id
;
201 struct aem_find_instance_resp
{
202 struct aem_iana_id id
;
210 /* These are used to query sensors */
211 struct aem_read_sensor_req
{
212 struct aem_iana_id id
;
220 struct aem_read_sensor_resp
{
221 struct aem_iana_id id
;
225 /* Data structures to talk to the IPMI layer */
226 struct aem_driver_data
{
227 struct list_head aem_devices
;
228 struct ipmi_smi_watcher bmc_events
;
229 struct ipmi_user_hndl ipmi_hndlrs
;
232 static void aem_register_bmc(int iface
, struct device
*dev
);
233 static void aem_bmc_gone(int iface
);
234 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
236 static void aem_remove_sensors(struct aem_data
*data
);
237 static int aem1_find_sensors(struct aem_data
*data
);
238 static int aem2_find_sensors(struct aem_data
*data
);
239 static void update_aem1_sensors(struct aem_data
*data
);
240 static void update_aem2_sensors(struct aem_data
*data
);
242 static struct aem_driver_data driver_data
= {
243 .aem_devices
= LIST_HEAD_INIT(driver_data
.aem_devices
),
245 .owner
= THIS_MODULE
,
246 .new_smi
= aem_register_bmc
,
247 .smi_gone
= aem_bmc_gone
,
250 .ipmi_recv_hndl
= aem_msg_handler
,
254 /* Functions to talk to the IPMI layer */
256 /* Initialize IPMI address, message buffers and user data */
257 static int aem_init_ipmi_data(struct aem_ipmi_data
*data
, int iface
,
262 init_completion(&data
->read_complete
);
263 data
->bmc_device
= bmc
;
265 /* Initialize IPMI address */
266 data
->address
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
267 data
->address
.channel
= IPMI_BMC_CHANNEL
;
268 data
->address
.data
[0] = 0;
269 data
->interface
= iface
;
271 /* Initialize message buffers */
273 data
->tx_message
.netfn
= AEM_NETFN
;
275 /* Create IPMI messaging interface user */
276 err
= ipmi_create_user(data
->interface
, &driver_data
.ipmi_hndlrs
,
280 "Unable to register user with IPMI interface %d\n",
288 /* Send an IPMI command */
289 static int aem_send_message(struct aem_ipmi_data
*data
)
293 err
= ipmi_validate_addr(&data
->address
, sizeof(data
->address
));
298 err
= ipmi_request_settime(data
->user
, &data
->address
, data
->tx_msgid
,
299 &data
->tx_message
, data
, 0, 0, 0);
305 dev_err(data
->bmc_device
, "request_settime=%x\n", err
);
308 dev_err(data
->bmc_device
, "validate_addr=%x\n", err
);
312 /* Dispatch IPMI messages to callers */
313 static void aem_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
)
315 unsigned short rx_len
;
316 struct aem_ipmi_data
*data
= user_msg_data
;
318 if (msg
->msgid
!= data
->tx_msgid
) {
319 dev_err(data
->bmc_device
,
320 "Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
322 (int)data
->tx_msgid
);
323 ipmi_free_recv_msg(msg
);
327 data
->rx_recv_type
= msg
->recv_type
;
328 if (msg
->msg
.data_len
> 0)
329 data
->rx_result
= msg
->msg
.data
[0];
331 data
->rx_result
= IPMI_UNKNOWN_ERR_COMPLETION_CODE
;
333 if (msg
->msg
.data_len
> 1) {
334 rx_len
= msg
->msg
.data_len
- 1;
335 if (data
->rx_msg_len
< rx_len
)
336 rx_len
= data
->rx_msg_len
;
337 data
->rx_msg_len
= rx_len
;
338 memcpy(data
->rx_msg_data
, msg
->msg
.data
+ 1, data
->rx_msg_len
);
340 data
->rx_msg_len
= 0;
342 ipmi_free_recv_msg(msg
);
343 complete(&data
->read_complete
);
346 /* Sensor support functions */
348 /* Read a sensor value; must be called with data->lock held */
349 static int aem_read_sensor(struct aem_data
*data
, u8 elt
, u8 reg
,
350 void *buf
, size_t size
)
353 struct aem_read_sensor_req rs_req
;
354 /* Use preallocated rx buffer */
355 struct aem_read_sensor_resp
*rs_resp
= data
->rs_resp
;
356 struct aem_ipmi_data
*ipmi
= &data
->ipmi
;
358 /* AEM registers are 1, 2, 4 or 8 bytes */
369 rs_req
.id
= system_x_id
;
370 rs_req
.module_handle
= data
->module_handle
;
371 rs_req
.element
= elt
;
372 rs_req
.subcommand
= AEM_READ_REGISTER
;
374 rs_req
.rx_buf_size
= size
;
376 ipmi
->tx_message
.cmd
= AEM_ELEMENT_CMD
;
377 ipmi
->tx_message
.data
= (char *)&rs_req
;
378 ipmi
->tx_message
.data_len
= sizeof(rs_req
);
380 rs_size
= sizeof(*rs_resp
) + size
;
381 ipmi
->rx_msg_data
= rs_resp
;
382 ipmi
->rx_msg_len
= rs_size
;
384 aem_send_message(ipmi
);
386 res
= wait_for_completion_timeout(&ipmi
->read_complete
, IPMI_TIMEOUT
);
392 if (ipmi
->rx_result
|| ipmi
->rx_msg_len
!= rs_size
||
393 memcmp(&rs_resp
->id
, &system_x_id
, sizeof(system_x_id
))) {
401 *x
= rs_resp
->bytes
[0];
406 *x
= be16_to_cpup((__be16
*)rs_resp
->bytes
);
411 *x
= be32_to_cpup((__be32
*)rs_resp
->bytes
);
416 *x
= be64_to_cpup((__be64
*)rs_resp
->bytes
);
426 /* Update AEM energy registers */
427 static void update_aem_energy_one(struct aem_data
*data
, int which
)
429 aem_read_sensor(data
, AEM_ENERGY_ELEMENT
, which
,
430 &data
->energy
[which
], 8);
433 static void update_aem_energy(struct aem_data
*data
)
435 update_aem_energy_one(data
, 0);
436 if (data
->ver_major
< 2)
438 update_aem_energy_one(data
, 1);
441 /* Update all AEM1 sensors */
442 static void update_aem1_sensors(struct aem_data
*data
)
444 mutex_lock(&data
->lock
);
445 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
449 update_aem_energy(data
);
451 mutex_unlock(&data
->lock
);
454 /* Update all AEM2 sensors */
455 static void update_aem2_sensors(struct aem_data
*data
)
459 mutex_lock(&data
->lock
);
460 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
464 update_aem_energy(data
);
465 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 0, &data
->temp
[0], 1);
466 aem_read_sensor(data
, AEM_EXHAUST_ELEMENT
, 1, &data
->temp
[1], 1);
468 for (i
= POWER_CAP
; i
<= POWER_AUX
; i
++)
469 aem_read_sensor(data
, AEM_POWER_CAP_ELEMENT
, i
,
472 mutex_unlock(&data
->lock
);
475 /* Delete an AEM instance */
476 static void aem_delete(struct aem_data
*data
)
478 list_del(&data
->list
);
479 aem_remove_sensors(data
);
480 kfree(data
->rs_resp
);
481 hwmon_device_unregister(data
->hwmon_dev
);
482 ipmi_destroy_user(data
->ipmi
.user
);
483 platform_set_drvdata(data
->pdev
, NULL
);
484 platform_device_unregister(data
->pdev
);
485 ida_simple_remove(&aem_ida
, data
->id
);
489 /* Probe functions for AEM1 devices */
491 /* Retrieve version and module handle for an AEM1 instance */
492 static int aem_find_aem1_count(struct aem_ipmi_data
*data
)
495 struct aem_find_firmware_req ff_req
;
496 struct aem_find_firmware_resp ff_resp
;
498 ff_req
.id
= system_x_id
;
500 ff_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
502 data
->tx_message
.cmd
= AEM_FIND_FW_CMD
;
503 data
->tx_message
.data
= (char *)&ff_req
;
504 data
->tx_message
.data_len
= sizeof(ff_req
);
506 data
->rx_msg_data
= &ff_resp
;
507 data
->rx_msg_len
= sizeof(ff_resp
);
509 aem_send_message(data
);
511 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
515 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(ff_resp
) ||
516 memcmp(&ff_resp
.id
, &system_x_id
, sizeof(system_x_id
)))
519 return ff_resp
.num_instances
;
522 /* Find and initialize one AEM1 instance */
523 static int aem_init_aem1_inst(struct aem_ipmi_data
*probe
, u8 module_handle
)
525 struct aem_data
*data
;
529 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
532 mutex_init(&data
->lock
);
534 /* Copy instance data */
537 data
->module_handle
= module_handle
;
538 for (i
= 0; i
< AEM1_NUM_ENERGY_REGS
; i
++)
539 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
541 /* Create sub-device for this fw instance */
542 data
->id
= ida_simple_get(&aem_ida
, 0, 0, GFP_KERNEL
);
546 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
549 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
551 res
= platform_device_add(data
->pdev
);
555 platform_set_drvdata(data
->pdev
, data
);
557 /* Set up IPMI interface */
558 res
= aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
563 /* Register with hwmon */
564 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
565 if (IS_ERR(data
->hwmon_dev
)) {
566 dev_err(&data
->pdev
->dev
,
567 "Unable to register hwmon device for IPMI interface %d\n",
569 res
= PTR_ERR(data
->hwmon_dev
);
573 data
->update
= update_aem1_sensors
;
574 data
->rs_resp
= kzalloc(sizeof(*(data
->rs_resp
)) + 8, GFP_KERNEL
);
575 if (!data
->rs_resp
) {
581 res
= aem1_find_sensors(data
);
585 /* Add to our list of AEM devices */
586 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
588 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
589 data
->ver_major
, data
->ver_minor
,
590 data
->module_handle
);
594 kfree(data
->rs_resp
);
596 hwmon_device_unregister(data
->hwmon_dev
);
598 ipmi_destroy_user(data
->ipmi
.user
);
600 platform_set_drvdata(data
->pdev
, NULL
);
601 platform_device_unregister(data
->pdev
);
603 ida_simple_remove(&aem_ida
, data
->id
);
610 /* Find and initialize all AEM1 instances */
611 static void aem_init_aem1(struct aem_ipmi_data
*probe
)
615 num
= aem_find_aem1_count(probe
);
616 for (i
= 0; i
< num
; i
++) {
617 err
= aem_init_aem1_inst(probe
, i
);
619 dev_err(probe
->bmc_device
,
620 "Error %d initializing AEM1 0x%X\n",
626 /* Probe functions for AEM2 devices */
628 /* Retrieve version and module handle for an AEM2 instance */
629 static int aem_find_aem2(struct aem_ipmi_data
*data
,
630 struct aem_find_instance_resp
*fi_resp
,
634 struct aem_find_instance_req fi_req
;
636 fi_req
.id
= system_x_id
;
637 fi_req
.instance_number
= instance_num
;
638 fi_req
.module_type_id
= cpu_to_be16(AEM_MODULE_TYPE_ID
);
640 data
->tx_message
.cmd
= AEM_FW_INSTANCE_CMD
;
641 data
->tx_message
.data
= (char *)&fi_req
;
642 data
->tx_message
.data_len
= sizeof(fi_req
);
644 data
->rx_msg_data
= fi_resp
;
645 data
->rx_msg_len
= sizeof(*fi_resp
);
647 aem_send_message(data
);
649 res
= wait_for_completion_timeout(&data
->read_complete
, IPMI_TIMEOUT
);
653 if (data
->rx_result
|| data
->rx_msg_len
!= sizeof(*fi_resp
) ||
654 memcmp(&fi_resp
->id
, &system_x_id
, sizeof(system_x_id
)) ||
655 fi_resp
->num_instances
<= instance_num
)
661 /* Find and initialize one AEM2 instance */
662 static int aem_init_aem2_inst(struct aem_ipmi_data
*probe
,
663 struct aem_find_instance_resp
*fi_resp
)
665 struct aem_data
*data
;
669 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
672 mutex_init(&data
->lock
);
674 /* Copy instance data */
675 data
->ver_major
= fi_resp
->major
;
676 data
->ver_minor
= fi_resp
->minor
;
677 data
->module_handle
= fi_resp
->module_handle
;
678 for (i
= 0; i
< AEM2_NUM_ENERGY_REGS
; i
++)
679 data
->power_period
[i
] = AEM_DEFAULT_POWER_INTERVAL
;
681 /* Create sub-device for this fw instance */
682 data
->id
= ida_simple_get(&aem_ida
, 0, 0, GFP_KERNEL
);
686 data
->pdev
= platform_device_alloc(DRVNAME
, data
->id
);
689 data
->pdev
->dev
.driver
= &aem_driver
.driver
;
691 res
= platform_device_add(data
->pdev
);
695 platform_set_drvdata(data
->pdev
, data
);
697 /* Set up IPMI interface */
698 res
= aem_init_ipmi_data(&data
->ipmi
, probe
->interface
,
703 /* Register with hwmon */
704 data
->hwmon_dev
= hwmon_device_register(&data
->pdev
->dev
);
705 if (IS_ERR(data
->hwmon_dev
)) {
706 dev_err(&data
->pdev
->dev
,
707 "Unable to register hwmon device for IPMI interface %d\n",
709 res
= PTR_ERR(data
->hwmon_dev
);
713 data
->update
= update_aem2_sensors
;
714 data
->rs_resp
= kzalloc(sizeof(*(data
->rs_resp
)) + 8, GFP_KERNEL
);
715 if (!data
->rs_resp
) {
721 res
= aem2_find_sensors(data
);
725 /* Add to our list of AEM devices */
726 list_add_tail(&data
->list
, &driver_data
.aem_devices
);
728 dev_info(data
->ipmi
.bmc_device
, "Found AEM v%d.%d at 0x%X\n",
729 data
->ver_major
, data
->ver_minor
,
730 data
->module_handle
);
734 kfree(data
->rs_resp
);
736 hwmon_device_unregister(data
->hwmon_dev
);
738 ipmi_destroy_user(data
->ipmi
.user
);
740 platform_set_drvdata(data
->pdev
, NULL
);
741 platform_device_unregister(data
->pdev
);
743 ida_simple_remove(&aem_ida
, data
->id
);
750 /* Find and initialize all AEM2 instances */
751 static void aem_init_aem2(struct aem_ipmi_data
*probe
)
753 struct aem_find_instance_resp fi_resp
;
757 while (!aem_find_aem2(probe
, &fi_resp
, i
)) {
758 if (fi_resp
.major
!= 2) {
759 dev_err(probe
->bmc_device
,
760 "Unknown AEM v%d; please report this to the maintainer.\n",
765 err
= aem_init_aem2_inst(probe
, &fi_resp
);
767 dev_err(probe
->bmc_device
,
768 "Error %d initializing AEM2 0x%X\n",
769 err
, fi_resp
.module_handle
);
775 /* Probe a BMC for AEM firmware instances */
776 static void aem_register_bmc(int iface
, struct device
*dev
)
778 struct aem_ipmi_data probe
;
780 if (aem_init_ipmi_data(&probe
, iface
, dev
))
783 /* Ignore probe errors; they won't cause problems */
784 aem_init_aem1(&probe
);
785 aem_init_aem2(&probe
);
787 ipmi_destroy_user(probe
.user
);
790 /* Handle BMC deletion */
791 static void aem_bmc_gone(int iface
)
793 struct aem_data
*p1
, *next1
;
795 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
796 if (p1
->ipmi
.interface
== iface
)
800 /* sysfs support functions */
802 /* AEM device name */
803 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*devattr
,
806 struct aem_data
*data
= dev_get_drvdata(dev
);
808 return sprintf(buf
, "%s%d\n", DRVNAME
, data
->ver_major
);
810 static SENSOR_DEVICE_ATTR_RO(name
, name
, 0);
812 /* AEM device version */
813 static ssize_t
version_show(struct device
*dev
,
814 struct device_attribute
*devattr
, char *buf
)
816 struct aem_data
*data
= dev_get_drvdata(dev
);
818 return sprintf(buf
, "%d.%d\n", data
->ver_major
, data
->ver_minor
);
820 static SENSOR_DEVICE_ATTR_RO(version
, version
, 0);
822 /* Display power use */
823 static ssize_t
aem_show_power(struct device
*dev
,
824 struct device_attribute
*devattr
,
827 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
828 struct aem_data
*data
= dev_get_drvdata(dev
);
829 u64 before
, after
, delta
, time
;
830 signed long leftover
;
832 mutex_lock(&data
->lock
);
833 update_aem_energy_one(data
, attr
->index
);
834 time
= ktime_get_ns();
835 before
= data
->energy
[attr
->index
];
837 leftover
= schedule_timeout_interruptible(
838 msecs_to_jiffies(data
->power_period
[attr
->index
])
841 mutex_unlock(&data
->lock
);
845 update_aem_energy_one(data
, attr
->index
);
846 time
= ktime_get_ns() - time
;
847 after
= data
->energy
[attr
->index
];
848 mutex_unlock(&data
->lock
);
850 delta
= (after
- before
) * UJ_PER_MJ
;
852 return sprintf(buf
, "%llu\n",
853 (unsigned long long)div64_u64(delta
* NSEC_PER_SEC
, time
));
856 /* Display energy use */
857 static ssize_t
aem_show_energy(struct device
*dev
,
858 struct device_attribute
*devattr
,
861 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
862 struct aem_data
*a
= dev_get_drvdata(dev
);
863 mutex_lock(&a
->lock
);
864 update_aem_energy_one(a
, attr
->index
);
865 mutex_unlock(&a
->lock
);
867 return sprintf(buf
, "%llu\n",
868 (unsigned long long)a
->energy
[attr
->index
] * 1000);
871 /* Display power interval registers */
872 static ssize_t
aem_show_power_period(struct device
*dev
,
873 struct device_attribute
*devattr
,
876 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
877 struct aem_data
*a
= dev_get_drvdata(dev
);
880 return sprintf(buf
, "%lu\n", a
->power_period
[attr
->index
]);
883 /* Set power interval registers */
884 static ssize_t
aem_set_power_period(struct device
*dev
,
885 struct device_attribute
*devattr
,
886 const char *buf
, size_t count
)
888 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
889 struct aem_data
*a
= dev_get_drvdata(dev
);
893 res
= kstrtoul(buf
, 10, &temp
);
897 if (temp
< AEM_MIN_POWER_INTERVAL
)
900 mutex_lock(&a
->lock
);
901 a
->power_period
[attr
->index
] = temp
;
902 mutex_unlock(&a
->lock
);
907 /* Discover sensors on an AEM device */
908 static int aem_register_sensors(struct aem_data
*data
,
909 const struct aem_ro_sensor_template
*ro
,
910 const struct aem_rw_sensor_template
*rw
)
912 struct device
*dev
= &data
->pdev
->dev
;
913 struct sensor_device_attribute
*sensors
= data
->sensors
;
916 /* Set up read-only sensors */
918 sysfs_attr_init(&sensors
->dev_attr
.attr
);
919 sensors
->dev_attr
.attr
.name
= ro
->label
;
920 sensors
->dev_attr
.attr
.mode
= 0444;
921 sensors
->dev_attr
.show
= ro
->show
;
922 sensors
->index
= ro
->index
;
924 err
= device_create_file(dev
, &sensors
->dev_attr
);
926 sensors
->dev_attr
.attr
.name
= NULL
;
933 /* Set up read-write sensors */
935 sysfs_attr_init(&sensors
->dev_attr
.attr
);
936 sensors
->dev_attr
.attr
.name
= rw
->label
;
937 sensors
->dev_attr
.attr
.mode
= 0644;
938 sensors
->dev_attr
.show
= rw
->show
;
939 sensors
->dev_attr
.store
= rw
->set
;
940 sensors
->index
= rw
->index
;
942 err
= device_create_file(dev
, &sensors
->dev_attr
);
944 sensors
->dev_attr
.attr
.name
= NULL
;
951 err
= device_create_file(dev
, &sensor_dev_attr_name
.dev_attr
);
954 err
= device_create_file(dev
, &sensor_dev_attr_version
.dev_attr
);
958 aem_remove_sensors(data
);
962 /* sysfs support functions for AEM2 sensors */
964 /* Display temperature use */
965 static ssize_t
aem2_show_temp(struct device
*dev
,
966 struct device_attribute
*devattr
,
969 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
970 struct aem_data
*a
= dev_get_drvdata(dev
);
973 return sprintf(buf
, "%u\n", a
->temp
[attr
->index
] * 1000);
976 /* Display power-capping registers */
977 static ssize_t
aem2_show_pcap_value(struct device
*dev
,
978 struct device_attribute
*devattr
,
981 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
982 struct aem_data
*a
= dev_get_drvdata(dev
);
985 return sprintf(buf
, "%u\n", a
->pcap
[attr
->index
] * 100000);
988 /* Remove sensors attached to an AEM device */
989 static void aem_remove_sensors(struct aem_data
*data
)
993 for (i
= 0; i
< AEM_NUM_SENSORS
; i
++) {
994 if (!data
->sensors
[i
].dev_attr
.attr
.name
)
996 device_remove_file(&data
->pdev
->dev
,
997 &data
->sensors
[i
].dev_attr
);
1000 device_remove_file(&data
->pdev
->dev
,
1001 &sensor_dev_attr_name
.dev_attr
);
1002 device_remove_file(&data
->pdev
->dev
,
1003 &sensor_dev_attr_version
.dev_attr
);
1006 /* Sensor probe functions */
1008 /* Description of AEM1 sensors */
1009 static const struct aem_ro_sensor_template aem1_ro_sensors
[] = {
1010 {"energy1_input", aem_show_energy
, 0},
1011 {"power1_average", aem_show_power
, 0},
1015 static const struct aem_rw_sensor_template aem1_rw_sensors
[] = {
1016 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1017 {NULL
, NULL
, NULL
, 0},
1020 /* Description of AEM2 sensors */
1021 static const struct aem_ro_sensor_template aem2_ro_sensors
[] = {
1022 {"energy1_input", aem_show_energy
, 0},
1023 {"energy2_input", aem_show_energy
, 1},
1024 {"power1_average", aem_show_power
, 0},
1025 {"power2_average", aem_show_power
, 1},
1026 {"temp1_input", aem2_show_temp
, 0},
1027 {"temp2_input", aem2_show_temp
, 1},
1029 {"power4_average", aem2_show_pcap_value
, POWER_CAP_MAX_HOTPLUG
},
1030 {"power5_average", aem2_show_pcap_value
, POWER_CAP_MAX
},
1031 {"power6_average", aem2_show_pcap_value
, POWER_CAP_MIN_WARNING
},
1032 {"power7_average", aem2_show_pcap_value
, POWER_CAP_MIN
},
1034 {"power3_average", aem2_show_pcap_value
, POWER_AUX
},
1035 {"power_cap", aem2_show_pcap_value
, POWER_CAP
},
1039 static const struct aem_rw_sensor_template aem2_rw_sensors
[] = {
1040 {"power1_average_interval", aem_show_power_period
, aem_set_power_period
, 0},
1041 {"power2_average_interval", aem_show_power_period
, aem_set_power_period
, 1},
1042 {NULL
, NULL
, NULL
, 0},
1045 /* Set up AEM1 sensor attrs */
1046 static int aem1_find_sensors(struct aem_data
*data
)
1048 return aem_register_sensors(data
, aem1_ro_sensors
, aem1_rw_sensors
);
1051 /* Set up AEM2 sensor attrs */
1052 static int aem2_find_sensors(struct aem_data
*data
)
1054 return aem_register_sensors(data
, aem2_ro_sensors
, aem2_rw_sensors
);
1057 /* Module init/exit routines */
1059 static int __init
aem_init(void)
1063 res
= driver_register(&aem_driver
.driver
);
1065 pr_err("Can't register aem driver\n");
1069 res
= ipmi_smi_watcher_register(&driver_data
.bmc_events
);
1075 driver_unregister(&aem_driver
.driver
);
1080 static void __exit
aem_exit(void)
1082 struct aem_data
*p1
, *next1
;
1084 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
1085 driver_unregister(&aem_driver
.driver
);
1086 list_for_each_entry_safe(p1
, next1
, &driver_data
.aem_devices
, list
)
1090 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1091 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1092 MODULE_LICENSE("GPL");
1094 module_init(aem_init
);
1095 module_exit(aem_exit
);
1097 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1098 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1099 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1100 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1101 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1102 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1103 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");