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>
30 #define REFRESH_INTERVAL (2 * HZ)
31 #define DRVNAME "ibmpex"
33 #define PEX_GET_VERSION 1
34 #define PEX_GET_SENSOR_COUNT 2
35 #define PEX_GET_SENSOR_NAME 3
36 #define PEX_RESET_HIGH_LOW 4
37 #define PEX_GET_SENSOR_DATA 6
39 #define PEX_NET_FUNCTION 0x3A
40 #define PEX_COMMAND 0x3C
42 static inline u16
extract_value(const char *data
, int offset
)
44 return be16_to_cpup((__be16
*)&data
[offset
]);
48 #define POWER_SENSOR 2
50 #define PEX_SENSOR_TYPE_LEN 3
51 static u8
const power_sensor_sig
[] = {0x70, 0x77, 0x72};
52 static u8
const temp_sensor_sig
[] = {0x74, 0x65, 0x6D};
54 #define PEX_MULT_LEN 2
55 static u8
const watt_sensor_sig
[] = {0x41, 0x43};
57 #define PEX_NUM_SENSOR_FUNCS 3
58 static char const * const power_sensor_name_templates
[] = {
60 "%s%d_average_lowest",
61 "%s%d_average_highest"
63 static char const * const temp_sensor_name_templates
[] = {
69 static void ibmpex_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
70 static void ibmpex_register_bmc(int iface
, struct device
*dev
);
71 static void ibmpex_bmc_gone(int iface
);
73 struct ibmpex_sensor_data
{
75 s16 values
[PEX_NUM_SENSOR_FUNCS
];
78 struct sensor_device_attribute_2 attr
[PEX_NUM_SENSOR_FUNCS
];
81 struct ibmpex_bmc_data
{
82 struct list_head list
;
83 struct device
*hwmon_dev
;
84 struct device
*bmc_device
;
87 unsigned long last_updated
; /* In jiffies */
89 struct ipmi_addr address
;
90 struct completion read_complete
;
94 struct kernel_ipmi_msg tx_message
;
95 unsigned char tx_msg_data
[IPMI_MAX_MSG_LENGTH
];
98 unsigned char rx_msg_data
[IPMI_MAX_MSG_LENGTH
];
99 unsigned long rx_msg_len
;
100 unsigned char rx_result
;
103 unsigned char sensor_major
;
104 unsigned char sensor_minor
;
106 unsigned char num_sensors
;
107 struct ibmpex_sensor_data
*sensors
;
110 struct ibmpex_driver_data
{
111 struct list_head bmc_data
;
112 struct ipmi_smi_watcher bmc_events
;
113 struct ipmi_user_hndl ipmi_hndlrs
;
116 static struct ibmpex_driver_data driver_data
= {
117 .bmc_data
= LIST_HEAD_INIT(driver_data
.bmc_data
),
119 .owner
= THIS_MODULE
,
120 .new_smi
= ibmpex_register_bmc
,
121 .smi_gone
= ibmpex_bmc_gone
,
124 .ipmi_recv_hndl
= ibmpex_msg_handler
,
128 static int ibmpex_send_message(struct ibmpex_bmc_data
*data
)
132 err
= ipmi_validate_addr(&data
->address
, sizeof(data
->address
));
137 err
= ipmi_request_settime(data
->user
, &data
->address
, data
->tx_msgid
,
138 &data
->tx_message
, data
, 0, 0, 0);
144 dev_err(data
->bmc_device
, "request_settime=%x\n", err
);
147 dev_err(data
->bmc_device
, "validate_addr=%x\n", err
);
151 static int ibmpex_ver_check(struct ibmpex_bmc_data
*data
)
153 data
->tx_msg_data
[0] = PEX_GET_VERSION
;
154 data
->tx_message
.data_len
= 1;
155 ibmpex_send_message(data
);
157 wait_for_completion(&data
->read_complete
);
159 if (data
->rx_result
|| data
->rx_msg_len
!= 6)
162 data
->sensor_major
= data
->rx_msg_data
[0];
163 data
->sensor_minor
= data
->rx_msg_data
[1];
165 dev_info(data
->bmc_device
, "Found BMC with sensor interface "
166 "v%d.%d %d-%02d-%02d on interface %d\n",
169 extract_value(data
->rx_msg_data
, 2),
170 data
->rx_msg_data
[4],
171 data
->rx_msg_data
[5],
177 static int ibmpex_query_sensor_count(struct ibmpex_bmc_data
*data
)
179 data
->tx_msg_data
[0] = PEX_GET_SENSOR_COUNT
;
180 data
->tx_message
.data_len
= 1;
181 ibmpex_send_message(data
);
183 wait_for_completion(&data
->read_complete
);
185 if (data
->rx_result
|| data
->rx_msg_len
!= 1)
188 return data
->rx_msg_data
[0];
191 static int ibmpex_query_sensor_name(struct ibmpex_bmc_data
*data
, int sensor
)
193 data
->tx_msg_data
[0] = PEX_GET_SENSOR_NAME
;
194 data
->tx_msg_data
[1] = sensor
;
195 data
->tx_message
.data_len
= 2;
196 ibmpex_send_message(data
);
198 wait_for_completion(&data
->read_complete
);
200 if (data
->rx_result
|| data
->rx_msg_len
< 1)
206 static int ibmpex_query_sensor_data(struct ibmpex_bmc_data
*data
, int sensor
)
208 data
->tx_msg_data
[0] = PEX_GET_SENSOR_DATA
;
209 data
->tx_msg_data
[1] = sensor
;
210 data
->tx_message
.data_len
= 2;
211 ibmpex_send_message(data
);
213 wait_for_completion(&data
->read_complete
);
215 if (data
->rx_result
|| data
->rx_msg_len
< 26) {
216 dev_err(data
->bmc_device
, "Error reading sensor %d.\n",
224 static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data
*data
)
226 data
->tx_msg_data
[0] = PEX_RESET_HIGH_LOW
;
227 data
->tx_message
.data_len
= 1;
228 ibmpex_send_message(data
);
230 wait_for_completion(&data
->read_complete
);
235 static void ibmpex_update_device(struct ibmpex_bmc_data
*data
)
239 mutex_lock(&data
->lock
);
240 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
244 for (i
= 0; i
< data
->num_sensors
; i
++) {
245 if (!data
->sensors
[i
].in_use
)
247 err
= ibmpex_query_sensor_data(data
, i
);
250 data
->sensors
[i
].values
[0] =
251 extract_value(data
->rx_msg_data
, 16);
252 data
->sensors
[i
].values
[1] =
253 extract_value(data
->rx_msg_data
, 18);
254 data
->sensors
[i
].values
[2] =
255 extract_value(data
->rx_msg_data
, 20);
258 data
->last_updated
= jiffies
;
262 mutex_unlock(&data
->lock
);
265 static struct ibmpex_bmc_data
*get_bmc_data(int iface
)
267 struct ibmpex_bmc_data
*p
, *next
;
269 list_for_each_entry_safe(p
, next
, &driver_data
.bmc_data
, list
)
270 if (p
->interface
== iface
)
276 static ssize_t
show_name(struct device
*dev
, struct device_attribute
*devattr
,
279 return sprintf(buf
, "%s\n", DRVNAME
);
281 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, 0);
283 static ssize_t
ibmpex_show_sensor(struct device
*dev
,
284 struct device_attribute
*devattr
,
287 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
288 struct ibmpex_bmc_data
*data
= dev_get_drvdata(dev
);
289 int mult
= data
->sensors
[attr
->index
].multiplier
;
290 ibmpex_update_device(data
);
292 return sprintf(buf
, "%d\n",
293 data
->sensors
[attr
->index
].values
[attr
->nr
] * mult
);
296 static ssize_t
ibmpex_reset_high_low(struct device
*dev
,
297 struct device_attribute
*devattr
,
301 struct ibmpex_bmc_data
*data
= dev_get_drvdata(dev
);
303 ibmpex_reset_high_low_data(data
);
308 static SENSOR_DEVICE_ATTR(reset_high_low
, S_IWUSR
, NULL
,
309 ibmpex_reset_high_low
, 0);
311 static int is_power_sensor(const char *sensor_id
, int len
)
313 if (len
< PEX_SENSOR_TYPE_LEN
)
316 if (!memcmp(sensor_id
, power_sensor_sig
, PEX_SENSOR_TYPE_LEN
))
321 static int is_temp_sensor(const char *sensor_id
, int len
)
323 if (len
< PEX_SENSOR_TYPE_LEN
)
326 if (!memcmp(sensor_id
, temp_sensor_sig
, PEX_SENSOR_TYPE_LEN
))
331 static int power_sensor_multiplier(struct ibmpex_bmc_data
*data
,
332 const char *sensor_id
, int len
)
336 if (data
->sensor_major
== 2)
339 for (i
= PEX_SENSOR_TYPE_LEN
; i
< len
- 1; i
++)
340 if (!memcmp(&sensor_id
[i
], watt_sensor_sig
, PEX_MULT_LEN
))
346 static int create_sensor(struct ibmpex_bmc_data
*data
, int type
,
347 int counter
, int sensor
, int func
)
352 n
= kmalloc(32, GFP_KERNEL
);
356 if (type
== TEMP_SENSOR
)
357 sprintf(n
, temp_sensor_name_templates
[func
], "temp", counter
);
358 else if (type
== POWER_SENSOR
)
359 sprintf(n
, power_sensor_name_templates
[func
], "power", counter
);
361 sysfs_attr_init(&data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
);
362 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.name
= n
;
363 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.mode
= S_IRUGO
;
364 data
->sensors
[sensor
].attr
[func
].dev_attr
.show
= ibmpex_show_sensor
;
365 data
->sensors
[sensor
].attr
[func
].index
= sensor
;
366 data
->sensors
[sensor
].attr
[func
].nr
= func
;
368 err
= device_create_file(data
->bmc_device
,
369 &data
->sensors
[sensor
].attr
[func
].dev_attr
);
371 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.name
= NULL
;
379 static int ibmpex_find_sensors(struct ibmpex_bmc_data
*data
)
387 err
= ibmpex_query_sensor_count(data
);
390 data
->num_sensors
= err
;
392 data
->sensors
= kzalloc(data
->num_sensors
* sizeof(*data
->sensors
),
397 for (i
= 0; i
< data
->num_sensors
; i
++) {
398 err
= ibmpex_query_sensor_name(data
, i
);
402 if (is_power_sensor(data
->rx_msg_data
, data
->rx_msg_len
)) {
403 sensor_type
= POWER_SENSOR
;
405 sensor_counter
= num_power
;
406 data
->sensors
[i
].multiplier
=
407 power_sensor_multiplier(data
,
410 } else if (is_temp_sensor(data
->rx_msg_data
,
412 sensor_type
= TEMP_SENSOR
;
414 sensor_counter
= num_temp
;
415 data
->sensors
[i
].multiplier
= 1000;
419 data
->sensors
[i
].in_use
= 1;
421 /* Create attributes */
422 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
423 err
= create_sensor(data
, sensor_type
, sensor_counter
,
430 err
= device_create_file(data
->bmc_device
,
431 &sensor_dev_attr_reset_high_low
.dev_attr
);
435 err
= device_create_file(data
->bmc_device
,
436 &sensor_dev_attr_name
.dev_attr
);
443 device_remove_file(data
->bmc_device
,
444 &sensor_dev_attr_reset_high_low
.dev_attr
);
445 device_remove_file(data
->bmc_device
, &sensor_dev_attr_name
.dev_attr
);
446 for (i
= 0; i
< data
->num_sensors
; i
++)
447 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
448 if (!data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
)
450 device_remove_file(data
->bmc_device
,
451 &data
->sensors
[i
].attr
[j
].dev_attr
);
452 kfree(data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
);
455 kfree(data
->sensors
);
459 static void ibmpex_register_bmc(int iface
, struct device
*dev
)
461 struct ibmpex_bmc_data
*data
;
464 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
466 dev_err(dev
, "Insufficient memory for BMC interface.\n");
470 data
->address
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
471 data
->address
.channel
= IPMI_BMC_CHANNEL
;
472 data
->address
.data
[0] = 0;
473 data
->interface
= iface
;
474 data
->bmc_device
= dev
;
476 /* Create IPMI messaging interface user */
477 err
= ipmi_create_user(data
->interface
, &driver_data
.ipmi_hndlrs
,
480 dev_err(dev
, "Unable to register user with IPMI "
481 "interface %d\n", data
->interface
);
485 mutex_init(&data
->lock
);
487 /* Initialize message */
489 init_completion(&data
->read_complete
);
490 data
->tx_message
.netfn
= PEX_NET_FUNCTION
;
491 data
->tx_message
.cmd
= PEX_COMMAND
;
492 data
->tx_message
.data
= data
->tx_msg_data
;
494 /* Does this BMC support PowerExecutive? */
495 err
= ibmpex_ver_check(data
);
499 /* Register the BMC as a HWMON class device */
500 data
->hwmon_dev
= hwmon_device_register(data
->bmc_device
);
502 if (IS_ERR(data
->hwmon_dev
)) {
503 dev_err(data
->bmc_device
, "Unable to register hwmon "
504 "device for IPMI interface %d\n",
509 /* finally add the new bmc data to the bmc data list */
510 dev_set_drvdata(dev
, data
);
511 list_add_tail(&data
->list
, &driver_data
.bmc_data
);
513 /* Now go find all the sensors */
514 err
= ibmpex_find_sensors(data
);
516 dev_err(data
->bmc_device
, "Error %d finding sensors\n", err
);
523 hwmon_device_unregister(data
->hwmon_dev
);
525 ipmi_destroy_user(data
->user
);
530 static void ibmpex_bmc_delete(struct ibmpex_bmc_data
*data
)
534 device_remove_file(data
->bmc_device
,
535 &sensor_dev_attr_reset_high_low
.dev_attr
);
536 device_remove_file(data
->bmc_device
, &sensor_dev_attr_name
.dev_attr
);
537 for (i
= 0; i
< data
->num_sensors
; i
++)
538 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
539 if (!data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
)
541 device_remove_file(data
->bmc_device
,
542 &data
->sensors
[i
].attr
[j
].dev_attr
);
543 kfree(data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
);
546 list_del(&data
->list
);
547 dev_set_drvdata(data
->bmc_device
, NULL
);
548 hwmon_device_unregister(data
->hwmon_dev
);
549 ipmi_destroy_user(data
->user
);
550 kfree(data
->sensors
);
554 static void ibmpex_bmc_gone(int iface
)
556 struct ibmpex_bmc_data
*data
= get_bmc_data(iface
);
561 ibmpex_bmc_delete(data
);
564 static void ibmpex_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
)
566 struct ibmpex_bmc_data
*data
= (struct ibmpex_bmc_data
*)user_msg_data
;
568 if (msg
->msgid
!= data
->tx_msgid
) {
569 dev_err(data
->bmc_device
, "Mismatch between received msgid "
570 "(%02x) and transmitted msgid (%02x)!\n",
572 (int)data
->tx_msgid
);
573 ipmi_free_recv_msg(msg
);
577 data
->rx_recv_type
= msg
->recv_type
;
578 if (msg
->msg
.data_len
> 0)
579 data
->rx_result
= msg
->msg
.data
[0];
581 data
->rx_result
= IPMI_UNKNOWN_ERR_COMPLETION_CODE
;
583 if (msg
->msg
.data_len
> 1) {
584 data
->rx_msg_len
= msg
->msg
.data_len
- 1;
585 memcpy(data
->rx_msg_data
, msg
->msg
.data
+ 1, data
->rx_msg_len
);
587 data
->rx_msg_len
= 0;
589 ipmi_free_recv_msg(msg
);
590 complete(&data
->read_complete
);
593 static int __init
ibmpex_init(void)
595 return ipmi_smi_watcher_register(&driver_data
.bmc_events
);
598 static void __exit
ibmpex_exit(void)
600 struct ibmpex_bmc_data
*p
, *next
;
602 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
603 list_for_each_entry_safe(p
, next
, &driver_data
.bmc_data
, list
)
604 ibmpex_bmc_delete(p
);
607 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
608 MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
609 MODULE_LICENSE("GPL");
611 module_init(ibmpex_init
);
612 module_exit(ibmpex_exit
);
614 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
615 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
616 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
617 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
618 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");