2 * A hwmon driver for the IBM PowerExecutive temperature/power sensors
3 * Copyright (C) 2007 IBM
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/ipmi.h>
23 #include <linux/module.h>
24 #include <linux/hwmon.h>
25 #include <linux/hwmon-sysfs.h>
26 #include <linux/jiffies.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/err.h>
31 #define REFRESH_INTERVAL (2 * HZ)
32 #define DRVNAME "ibmpex"
34 #define PEX_GET_VERSION 1
35 #define PEX_GET_SENSOR_COUNT 2
36 #define PEX_GET_SENSOR_NAME 3
37 #define PEX_RESET_HIGH_LOW 4
38 #define PEX_GET_SENSOR_DATA 6
40 #define PEX_NET_FUNCTION 0x3A
41 #define PEX_COMMAND 0x3C
43 static inline u16
extract_value(const char *data
, int offset
)
45 return be16_to_cpup((__be16
*)&data
[offset
]);
49 #define POWER_SENSOR 2
51 #define PEX_SENSOR_TYPE_LEN 3
52 static u8
const power_sensor_sig
[] = {0x70, 0x77, 0x72};
53 static u8
const temp_sensor_sig
[] = {0x74, 0x65, 0x6D};
55 #define PEX_MULT_LEN 2
56 static u8
const watt_sensor_sig
[] = {0x41, 0x43};
58 #define PEX_NUM_SENSOR_FUNCS 3
59 static char const * const power_sensor_name_templates
[] = {
61 "%s%d_average_lowest",
62 "%s%d_average_highest"
64 static char const * const temp_sensor_name_templates
[] = {
70 static void ibmpex_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
71 static void ibmpex_register_bmc(int iface
, struct device
*dev
);
72 static void ibmpex_bmc_gone(int iface
);
74 struct ibmpex_sensor_data
{
76 s16 values
[PEX_NUM_SENSOR_FUNCS
];
79 struct sensor_device_attribute_2 attr
[PEX_NUM_SENSOR_FUNCS
];
82 struct ibmpex_bmc_data
{
83 struct list_head list
;
84 struct device
*hwmon_dev
;
85 struct device
*bmc_device
;
88 unsigned long last_updated
; /* In jiffies */
90 struct ipmi_addr address
;
91 struct completion read_complete
;
95 struct kernel_ipmi_msg tx_message
;
96 unsigned char tx_msg_data
[IPMI_MAX_MSG_LENGTH
];
99 unsigned char rx_msg_data
[IPMI_MAX_MSG_LENGTH
];
100 unsigned long rx_msg_len
;
101 unsigned char rx_result
;
104 unsigned char sensor_major
;
105 unsigned char sensor_minor
;
107 unsigned char num_sensors
;
108 struct ibmpex_sensor_data
*sensors
;
111 struct ibmpex_driver_data
{
112 struct list_head bmc_data
;
113 struct ipmi_smi_watcher bmc_events
;
114 struct ipmi_user_hndl ipmi_hndlrs
;
117 static struct ibmpex_driver_data driver_data
= {
118 .bmc_data
= LIST_HEAD_INIT(driver_data
.bmc_data
),
120 .owner
= THIS_MODULE
,
121 .new_smi
= ibmpex_register_bmc
,
122 .smi_gone
= ibmpex_bmc_gone
,
125 .ipmi_recv_hndl
= ibmpex_msg_handler
,
129 static int ibmpex_send_message(struct ibmpex_bmc_data
*data
)
133 err
= ipmi_validate_addr(&data
->address
, sizeof(data
->address
));
138 err
= ipmi_request_settime(data
->user
, &data
->address
, data
->tx_msgid
,
139 &data
->tx_message
, data
, 0, 0, 0);
145 dev_err(data
->bmc_device
, "request_settime=%x\n", err
);
148 dev_err(data
->bmc_device
, "validate_addr=%x\n", err
);
152 static int ibmpex_ver_check(struct ibmpex_bmc_data
*data
)
154 data
->tx_msg_data
[0] = PEX_GET_VERSION
;
155 data
->tx_message
.data_len
= 1;
156 ibmpex_send_message(data
);
158 wait_for_completion(&data
->read_complete
);
160 if (data
->rx_result
|| data
->rx_msg_len
!= 6)
163 data
->sensor_major
= data
->rx_msg_data
[0];
164 data
->sensor_minor
= data
->rx_msg_data
[1];
166 dev_info(data
->bmc_device
, "Found BMC with sensor interface "
167 "v%d.%d %d-%02d-%02d on interface %d\n",
170 extract_value(data
->rx_msg_data
, 2),
171 data
->rx_msg_data
[4],
172 data
->rx_msg_data
[5],
178 static int ibmpex_query_sensor_count(struct ibmpex_bmc_data
*data
)
180 data
->tx_msg_data
[0] = PEX_GET_SENSOR_COUNT
;
181 data
->tx_message
.data_len
= 1;
182 ibmpex_send_message(data
);
184 wait_for_completion(&data
->read_complete
);
186 if (data
->rx_result
|| data
->rx_msg_len
!= 1)
189 return data
->rx_msg_data
[0];
192 static int ibmpex_query_sensor_name(struct ibmpex_bmc_data
*data
, int sensor
)
194 data
->tx_msg_data
[0] = PEX_GET_SENSOR_NAME
;
195 data
->tx_msg_data
[1] = sensor
;
196 data
->tx_message
.data_len
= 2;
197 ibmpex_send_message(data
);
199 wait_for_completion(&data
->read_complete
);
201 if (data
->rx_result
|| data
->rx_msg_len
< 1)
207 static int ibmpex_query_sensor_data(struct ibmpex_bmc_data
*data
, int sensor
)
209 data
->tx_msg_data
[0] = PEX_GET_SENSOR_DATA
;
210 data
->tx_msg_data
[1] = sensor
;
211 data
->tx_message
.data_len
= 2;
212 ibmpex_send_message(data
);
214 wait_for_completion(&data
->read_complete
);
216 if (data
->rx_result
|| data
->rx_msg_len
< 26) {
217 dev_err(data
->bmc_device
, "Error reading sensor %d.\n",
225 static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data
*data
)
227 data
->tx_msg_data
[0] = PEX_RESET_HIGH_LOW
;
228 data
->tx_message
.data_len
= 1;
229 ibmpex_send_message(data
);
231 wait_for_completion(&data
->read_complete
);
236 static void ibmpex_update_device(struct ibmpex_bmc_data
*data
)
240 mutex_lock(&data
->lock
);
241 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
245 for (i
= 0; i
< data
->num_sensors
; i
++) {
246 if (!data
->sensors
[i
].in_use
)
248 err
= ibmpex_query_sensor_data(data
, i
);
251 data
->sensors
[i
].values
[0] =
252 extract_value(data
->rx_msg_data
, 16);
253 data
->sensors
[i
].values
[1] =
254 extract_value(data
->rx_msg_data
, 18);
255 data
->sensors
[i
].values
[2] =
256 extract_value(data
->rx_msg_data
, 20);
259 data
->last_updated
= jiffies
;
263 mutex_unlock(&data
->lock
);
266 static struct ibmpex_bmc_data
*get_bmc_data(int iface
)
268 struct ibmpex_bmc_data
*p
, *next
;
270 list_for_each_entry_safe(p
, next
, &driver_data
.bmc_data
, list
)
271 if (p
->interface
== iface
)
277 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*devattr
,
280 return sprintf(buf
, "%s\n", DRVNAME
);
282 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, 0);
284 static ssize_t
ibmpex_show_sensor(struct device
*dev
,
285 struct device_attribute
*devattr
,
288 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
289 struct ibmpex_bmc_data
*data
= dev_get_drvdata(dev
);
290 int mult
= data
->sensors
[attr
->index
].multiplier
;
291 ibmpex_update_device(data
);
293 return sprintf(buf
, "%d\n",
294 data
->sensors
[attr
->index
].values
[attr
->nr
] * mult
);
297 static ssize_t
ibmpex_reset_high_low(struct device
*dev
,
298 struct device_attribute
*devattr
,
302 struct ibmpex_bmc_data
*data
= dev_get_drvdata(dev
);
304 ibmpex_reset_high_low_data(data
);
309 static SENSOR_DEVICE_ATTR(reset_high_low
, S_IWUSR
, NULL
,
310 ibmpex_reset_high_low
, 0);
312 static int is_power_sensor(const char *sensor_id
, int len
)
314 if (len
< PEX_SENSOR_TYPE_LEN
)
317 if (!memcmp(sensor_id
, power_sensor_sig
, PEX_SENSOR_TYPE_LEN
))
322 static int is_temp_sensor(const char *sensor_id
, int len
)
324 if (len
< PEX_SENSOR_TYPE_LEN
)
327 if (!memcmp(sensor_id
, temp_sensor_sig
, PEX_SENSOR_TYPE_LEN
))
332 static int power_sensor_multiplier(struct ibmpex_bmc_data
*data
,
333 const char *sensor_id
, int len
)
337 if (data
->sensor_major
== 2)
340 for (i
= PEX_SENSOR_TYPE_LEN
; i
< len
- 1; i
++)
341 if (!memcmp(&sensor_id
[i
], watt_sensor_sig
, PEX_MULT_LEN
))
347 static int create_sensor(struct ibmpex_bmc_data
*data
, int type
,
348 int counter
, int sensor
, int func
)
353 n
= kmalloc(32, GFP_KERNEL
);
357 if (type
== TEMP_SENSOR
)
358 sprintf(n
, temp_sensor_name_templates
[func
], "temp", counter
);
359 else if (type
== POWER_SENSOR
)
360 sprintf(n
, power_sensor_name_templates
[func
], "power", counter
);
362 sysfs_attr_init(&data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
);
363 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.name
= n
;
364 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.mode
= S_IRUGO
;
365 data
->sensors
[sensor
].attr
[func
].dev_attr
.show
= ibmpex_show_sensor
;
366 data
->sensors
[sensor
].attr
[func
].index
= sensor
;
367 data
->sensors
[sensor
].attr
[func
].nr
= func
;
369 err
= device_create_file(data
->bmc_device
,
370 &data
->sensors
[sensor
].attr
[func
].dev_attr
);
372 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.name
= NULL
;
380 static int ibmpex_find_sensors(struct ibmpex_bmc_data
*data
)
388 err
= ibmpex_query_sensor_count(data
);
391 data
->num_sensors
= err
;
393 data
->sensors
= kzalloc(data
->num_sensors
* sizeof(*data
->sensors
),
398 for (i
= 0; i
< data
->num_sensors
; i
++) {
399 err
= ibmpex_query_sensor_name(data
, i
);
403 if (is_power_sensor(data
->rx_msg_data
, data
->rx_msg_len
)) {
404 sensor_type
= POWER_SENSOR
;
406 sensor_counter
= num_power
;
407 data
->sensors
[i
].multiplier
=
408 power_sensor_multiplier(data
,
411 } else if (is_temp_sensor(data
->rx_msg_data
,
413 sensor_type
= TEMP_SENSOR
;
415 sensor_counter
= num_temp
;
416 data
->sensors
[i
].multiplier
= 1000;
420 data
->sensors
[i
].in_use
= 1;
422 /* Create attributes */
423 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
424 err
= create_sensor(data
, sensor_type
, sensor_counter
,
431 err
= device_create_file(data
->bmc_device
,
432 &sensor_dev_attr_reset_high_low
.dev_attr
);
436 err
= device_create_file(data
->bmc_device
,
437 &sensor_dev_attr_name
.dev_attr
);
444 device_remove_file(data
->bmc_device
,
445 &sensor_dev_attr_reset_high_low
.dev_attr
);
446 device_remove_file(data
->bmc_device
, &sensor_dev_attr_name
.dev_attr
);
447 for (i
= 0; i
< data
->num_sensors
; i
++)
448 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
449 if (!data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
)
451 device_remove_file(data
->bmc_device
,
452 &data
->sensors
[i
].attr
[j
].dev_attr
);
453 kfree(data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
);
456 kfree(data
->sensors
);
460 static void ibmpex_register_bmc(int iface
, struct device
*dev
)
462 struct ibmpex_bmc_data
*data
;
465 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
467 dev_err(dev
, "Insufficient memory for BMC interface.\n");
471 data
->address
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
472 data
->address
.channel
= IPMI_BMC_CHANNEL
;
473 data
->address
.data
[0] = 0;
474 data
->interface
= iface
;
475 data
->bmc_device
= dev
;
477 /* Create IPMI messaging interface user */
478 err
= ipmi_create_user(data
->interface
, &driver_data
.ipmi_hndlrs
,
481 dev_err(dev
, "Unable to register user with IPMI "
482 "interface %d\n", data
->interface
);
486 mutex_init(&data
->lock
);
488 /* Initialize message */
490 init_completion(&data
->read_complete
);
491 data
->tx_message
.netfn
= PEX_NET_FUNCTION
;
492 data
->tx_message
.cmd
= PEX_COMMAND
;
493 data
->tx_message
.data
= data
->tx_msg_data
;
495 /* Does this BMC support PowerExecutive? */
496 err
= ibmpex_ver_check(data
);
500 /* Register the BMC as a HWMON class device */
501 data
->hwmon_dev
= hwmon_device_register(data
->bmc_device
);
503 if (IS_ERR(data
->hwmon_dev
)) {
504 dev_err(data
->bmc_device
, "Unable to register hwmon "
505 "device for IPMI interface %d\n",
510 /* finally add the new bmc data to the bmc data list */
511 dev_set_drvdata(dev
, data
);
512 list_add_tail(&data
->list
, &driver_data
.bmc_data
);
514 /* Now go find all the sensors */
515 err
= ibmpex_find_sensors(data
);
517 dev_err(data
->bmc_device
, "Error %d finding sensors\n", err
);
524 hwmon_device_unregister(data
->hwmon_dev
);
526 ipmi_destroy_user(data
->user
);
531 static void ibmpex_bmc_delete(struct ibmpex_bmc_data
*data
)
535 device_remove_file(data
->bmc_device
,
536 &sensor_dev_attr_reset_high_low
.dev_attr
);
537 device_remove_file(data
->bmc_device
, &sensor_dev_attr_name
.dev_attr
);
538 for (i
= 0; i
< data
->num_sensors
; i
++)
539 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
540 if (!data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
)
542 device_remove_file(data
->bmc_device
,
543 &data
->sensors
[i
].attr
[j
].dev_attr
);
544 kfree(data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
);
547 list_del(&data
->list
);
548 dev_set_drvdata(data
->bmc_device
, NULL
);
549 hwmon_device_unregister(data
->hwmon_dev
);
550 ipmi_destroy_user(data
->user
);
551 kfree(data
->sensors
);
555 static void ibmpex_bmc_gone(int iface
)
557 struct ibmpex_bmc_data
*data
= get_bmc_data(iface
);
562 ibmpex_bmc_delete(data
);
565 static void ibmpex_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
)
567 struct ibmpex_bmc_data
*data
= (struct ibmpex_bmc_data
*)user_msg_data
;
569 if (msg
->msgid
!= data
->tx_msgid
) {
570 dev_err(data
->bmc_device
, "Mismatch between received msgid "
571 "(%02x) and transmitted msgid (%02x)!\n",
573 (int)data
->tx_msgid
);
574 ipmi_free_recv_msg(msg
);
578 data
->rx_recv_type
= msg
->recv_type
;
579 if (msg
->msg
.data_len
> 0)
580 data
->rx_result
= msg
->msg
.data
[0];
582 data
->rx_result
= IPMI_UNKNOWN_ERR_COMPLETION_CODE
;
584 if (msg
->msg
.data_len
> 1) {
585 data
->rx_msg_len
= msg
->msg
.data_len
- 1;
586 memcpy(data
->rx_msg_data
, msg
->msg
.data
+ 1, data
->rx_msg_len
);
588 data
->rx_msg_len
= 0;
590 ipmi_free_recv_msg(msg
);
591 complete(&data
->read_complete
);
594 static int __init
ibmpex_init(void)
596 return ipmi_smi_watcher_register(&driver_data
.bmc_events
);
599 static void __exit
ibmpex_exit(void)
601 struct ibmpex_bmc_data
*p
, *next
;
603 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
604 list_for_each_entry_safe(p
, next
, &driver_data
.bmc_data
, list
)
605 ibmpex_bmc_delete(p
);
608 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
609 MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
610 MODULE_LICENSE("GPL");
612 module_init(ibmpex_init
);
613 module_exit(ibmpex_exit
);
615 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
616 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
617 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
618 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
619 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");