2 * A hwmon driver for the IBM PowerExecutive temperature/power sensors
3 * Copyright (C) 2007 IBM
5 * Author: Darrick J. Wong <darrick.wong@oracle.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 const char * const sensor_name_suffixes[] = {
65 static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
66 static void ibmpex_register_bmc(int iface, struct device *dev);
67 static void ibmpex_bmc_gone(int iface);
69 struct ibmpex_sensor_data {
71 s16 values[PEX_NUM_SENSOR_FUNCS];
74 struct sensor_device_attribute_2 attr[PEX_NUM_SENSOR_FUNCS];
77 struct ibmpex_bmc_data {
78 struct list_head list;
79 struct device *hwmon_dev;
80 struct device *bmc_device;
83 unsigned long last_updated; /* In jiffies */
85 struct ipmi_addr address;
86 struct completion read_complete;
90 struct kernel_ipmi_msg tx_message;
91 unsigned char tx_msg_data[IPMI_MAX_MSG_LENGTH];
94 unsigned char rx_msg_data[IPMI_MAX_MSG_LENGTH];
95 unsigned long rx_msg_len;
96 unsigned char rx_result;
99 unsigned char sensor_major;
100 unsigned char sensor_minor;
102 unsigned char num_sensors;
103 struct ibmpex_sensor_data *sensors;
106 struct ibmpex_driver_data {
107 struct list_head bmc_data;
108 struct ipmi_smi_watcher bmc_events;
109 struct ipmi_user_hndl ipmi_hndlrs;
112 static struct ibmpex_driver_data driver_data = {
113 .bmc_data = LIST_HEAD_INIT(driver_data.bmc_data),
115 .owner = THIS_MODULE,
116 .new_smi = ibmpex_register_bmc,
117 .smi_gone = ibmpex_bmc_gone,
120 .ipmi_recv_hndl = ibmpex_msg_handler,
124 static int ibmpex_send_message(struct ibmpex_bmc_data *data)
128 err = ipmi_validate_addr(&data->address, sizeof(data->address));
133 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
134 &data->tx_message, data, 0, 0, 0);
140 dev_err(data->bmc_device, "request_settime=%x\n", err);
143 dev_err(data->bmc_device, "validate_addr=%x\n", err);
147 static int ibmpex_ver_check(struct ibmpex_bmc_data *data)
149 data->tx_msg_data[0] = PEX_GET_VERSION;
150 data->tx_message.data_len = 1;
151 ibmpex_send_message(data);
153 wait_for_completion(&data->read_complete);
155 if (data->rx_result || data->rx_msg_len != 6)
158 data->sensor_major = data->rx_msg_data[0];
159 data->sensor_minor = data->rx_msg_data[1];
161 dev_info(data->bmc_device,
162 "Found BMC with sensor interface v%d.%d %d-%02d-%02d on interface %d\n",
165 extract_value(data->rx_msg_data, 2),
166 data->rx_msg_data[4],
167 data->rx_msg_data[5],
173 static int ibmpex_query_sensor_count(struct ibmpex_bmc_data *data)
175 data->tx_msg_data[0] = PEX_GET_SENSOR_COUNT;
176 data->tx_message.data_len = 1;
177 ibmpex_send_message(data);
179 wait_for_completion(&data->read_complete);
181 if (data->rx_result || data->rx_msg_len != 1)
184 return data->rx_msg_data[0];
187 static int ibmpex_query_sensor_name(struct ibmpex_bmc_data *data, int sensor)
189 data->tx_msg_data[0] = PEX_GET_SENSOR_NAME;
190 data->tx_msg_data[1] = sensor;
191 data->tx_message.data_len = 2;
192 ibmpex_send_message(data);
194 wait_for_completion(&data->read_complete);
196 if (data->rx_result || data->rx_msg_len < 1)
202 static int ibmpex_query_sensor_data(struct ibmpex_bmc_data *data, int sensor)
204 data->tx_msg_data[0] = PEX_GET_SENSOR_DATA;
205 data->tx_msg_data[1] = sensor;
206 data->tx_message.data_len = 2;
207 ibmpex_send_message(data);
209 wait_for_completion(&data->read_complete);
211 if (data->rx_result || data->rx_msg_len < 26) {
212 dev_err(data->bmc_device, "Error reading sensor %d.\n",
220 static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data *data)
222 data->tx_msg_data[0] = PEX_RESET_HIGH_LOW;
223 data->tx_message.data_len = 1;
224 ibmpex_send_message(data);
226 wait_for_completion(&data->read_complete);
231 static void ibmpex_update_device(struct ibmpex_bmc_data *data)
235 mutex_lock(&data->lock);
236 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
240 for (i = 0; i < data->num_sensors; i++) {
241 if (!data->sensors[i].in_use)
243 err = ibmpex_query_sensor_data(data, i);
246 data->sensors[i].values[0] =
247 extract_value(data->rx_msg_data, 16);
248 data->sensors[i].values[1] =
249 extract_value(data->rx_msg_data, 18);
250 data->sensors[i].values[2] =
251 extract_value(data->rx_msg_data, 20);
254 data->last_updated = jiffies;
258 mutex_unlock(&data->lock);
261 static struct ibmpex_bmc_data *get_bmc_data(int iface)
263 struct ibmpex_bmc_data *p, *next;
265 list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
266 if (p->interface == iface)
272 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
275 return sprintf(buf, "%s\n", DRVNAME);
277 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
279 static ssize_t ibmpex_show_sensor(struct device *dev,
280 struct device_attribute *devattr,
283 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
284 struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
285 int mult = data->sensors[attr->index].multiplier;
286 ibmpex_update_device(data);
288 return sprintf(buf, "%d\n",
289 data->sensors[attr->index].values[attr->nr] * mult);
292 static ssize_t ibmpex_reset_high_low(struct device *dev,
293 struct device_attribute *devattr,
297 struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
299 ibmpex_reset_high_low_data(data);
304 static SENSOR_DEVICE_ATTR(reset_high_low, S_IWUSR, NULL,
305 ibmpex_reset_high_low, 0);
307 static int is_power_sensor(const char *sensor_id, int len)
309 if (len < PEX_SENSOR_TYPE_LEN)
312 if (!memcmp(sensor_id, power_sensor_sig, PEX_SENSOR_TYPE_LEN))
317 static int is_temp_sensor(const char *sensor_id, int len)
319 if (len < PEX_SENSOR_TYPE_LEN)
322 if (!memcmp(sensor_id, temp_sensor_sig, PEX_SENSOR_TYPE_LEN))
327 static int power_sensor_multiplier(struct ibmpex_bmc_data *data,
328 const char *sensor_id, int len)
332 if (data->sensor_major == 2)
335 for (i = PEX_SENSOR_TYPE_LEN; i < len - 1; i++)
336 if (!memcmp(&sensor_id[i], watt_sensor_sig, PEX_MULT_LEN))
342 static int create_sensor(struct ibmpex_bmc_data *data, int type,
343 int counter, int sensor, int func)
348 n = kmalloc(32, GFP_KERNEL);
352 if (type == TEMP_SENSOR)
353 sprintf(n, "temp%d_input%s",
354 counter, sensor_name_suffixes[func]);
355 else if (type == POWER_SENSOR)
356 sprintf(n, "power%d_average%s",
357 counter, sensor_name_suffixes[func]);
359 sysfs_attr_init(&data->sensors[sensor].attr[func].dev_attr.attr);
360 data->sensors[sensor].attr[func].dev_attr.attr.name = n;
361 data->sensors[sensor].attr[func].dev_attr.attr.mode = S_IRUGO;
362 data->sensors[sensor].attr[func].dev_attr.show = ibmpex_show_sensor;
363 data->sensors[sensor].attr[func].index = sensor;
364 data->sensors[sensor].attr[func].nr = func;
366 err = device_create_file(data->bmc_device,
367 &data->sensors[sensor].attr[func].dev_attr);
369 data->sensors[sensor].attr[func].dev_attr.attr.name = NULL;
377 static int ibmpex_find_sensors(struct ibmpex_bmc_data *data)
385 err = ibmpex_query_sensor_count(data);
388 data->num_sensors = err;
390 data->sensors = kzalloc(data->num_sensors * sizeof(*data->sensors),
395 for (i = 0; i < data->num_sensors; i++) {
396 err = ibmpex_query_sensor_name(data, i);
400 if (is_power_sensor(data->rx_msg_data, data->rx_msg_len)) {
401 sensor_type = POWER_SENSOR;
403 sensor_counter = num_power;
404 data->sensors[i].multiplier =
405 power_sensor_multiplier(data,
408 } else if (is_temp_sensor(data->rx_msg_data,
410 sensor_type = TEMP_SENSOR;
412 sensor_counter = num_temp;
413 data->sensors[i].multiplier = 1000;
417 data->sensors[i].in_use = 1;
419 /* Create attributes */
420 for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
421 err = create_sensor(data, sensor_type, sensor_counter,
428 err = device_create_file(data->bmc_device,
429 &sensor_dev_attr_reset_high_low.dev_attr);
433 err = device_create_file(data->bmc_device,
434 &sensor_dev_attr_name.dev_attr);
441 device_remove_file(data->bmc_device,
442 &sensor_dev_attr_reset_high_low.dev_attr);
443 device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
444 for (i = 0; i < data->num_sensors; i++)
445 for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
446 if (!data->sensors[i].attr[j].dev_attr.attr.name)
448 device_remove_file(data->bmc_device,
449 &data->sensors[i].attr[j].dev_attr);
450 kfree(data->sensors[i].attr[j].dev_attr.attr.name);
453 kfree(data->sensors);
457 static void ibmpex_register_bmc(int iface, struct device *dev)
459 struct ibmpex_bmc_data *data;
462 data = kzalloc(sizeof(*data), GFP_KERNEL);
466 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
467 data->address.channel = IPMI_BMC_CHANNEL;
468 data->address.data[0] = 0;
469 data->interface = iface;
470 data->bmc_device = dev;
472 /* Create IPMI messaging interface user */
473 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
477 "Unable to register user with IPMI interface %d\n",
482 mutex_init(&data->lock);
484 /* Initialize message */
486 init_completion(&data->read_complete);
487 data->tx_message.netfn = PEX_NET_FUNCTION;
488 data->tx_message.cmd = PEX_COMMAND;
489 data->tx_message.data = data->tx_msg_data;
491 /* Does this BMC support PowerExecutive? */
492 err = ibmpex_ver_check(data);
496 /* Register the BMC as a HWMON class device */
497 data->hwmon_dev = hwmon_device_register(data->bmc_device);
499 if (IS_ERR(data->hwmon_dev)) {
500 dev_err(data->bmc_device,
501 "Unable to register hwmon device for IPMI interface %d\n",
506 /* finally add the new bmc data to the bmc data list */
507 dev_set_drvdata(dev, data);
508 list_add_tail(&data->list, &driver_data.bmc_data);
510 /* Now go find all the sensors */
511 err = ibmpex_find_sensors(data);
513 dev_err(data->bmc_device, "Error %d finding sensors\n", err);
520 hwmon_device_unregister(data->hwmon_dev);
522 ipmi_destroy_user(data->user);
527 static void ibmpex_bmc_delete(struct ibmpex_bmc_data *data)
531 device_remove_file(data->bmc_device,
532 &sensor_dev_attr_reset_high_low.dev_attr);
533 device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
534 for (i = 0; i < data->num_sensors; i++)
535 for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
536 if (!data->sensors[i].attr[j].dev_attr.attr.name)
538 device_remove_file(data->bmc_device,
539 &data->sensors[i].attr[j].dev_attr);
540 kfree(data->sensors[i].attr[j].dev_attr.attr.name);
543 list_del(&data->list);
544 dev_set_drvdata(data->bmc_device, NULL);
545 hwmon_device_unregister(data->hwmon_dev);
546 ipmi_destroy_user(data->user);
547 kfree(data->sensors);
551 static void ibmpex_bmc_gone(int iface)
553 struct ibmpex_bmc_data *data = get_bmc_data(iface);
558 ibmpex_bmc_delete(data);
561 static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
563 struct ibmpex_bmc_data *data = (struct ibmpex_bmc_data *)user_msg_data;
565 if (msg->msgid != data->tx_msgid) {
566 dev_err(data->bmc_device,
567 "Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
569 (int)data->tx_msgid);
570 ipmi_free_recv_msg(msg);
574 data->rx_recv_type = msg->recv_type;
575 if (msg->msg.data_len > 0)
576 data->rx_result = msg->msg.data[0];
578 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
580 if (msg->msg.data_len > 1) {
581 data->rx_msg_len = msg->msg.data_len - 1;
582 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
584 data->rx_msg_len = 0;
586 ipmi_free_recv_msg(msg);
587 complete(&data->read_complete);
590 static int __init ibmpex_init(void)
592 return ipmi_smi_watcher_register(&driver_data.bmc_events);
595 static void __exit ibmpex_exit(void)
597 struct ibmpex_bmc_data *p, *next;
599 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
600 list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
601 ibmpex_bmc_delete(p);
604 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
605 MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
606 MODULE_LICENSE("GPL");
608 module_init(ibmpex_init);
609 module_exit(ibmpex_exit);
611 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
612 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
613 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
614 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
615 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");