blob: 06d4eafcf76b231fdc74e0418341e496387b78d9 [file] [log] [blame]
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -07001/*
2 * A hwmon driver for the IBM PowerExecutive temperature/power sensors
3 * Copyright (C) 2007 IBM
4 *
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -070029
30#define REFRESH_INTERVAL (2 * HZ)
31#define DRVNAME "ibmpex"
32
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
38
39#define PEX_NET_FUNCTION 0x3A
40#define PEX_COMMAND 0x3C
41
42static inline u16 extract_value(const char *data, int offset)
43{
Harvey Harrison29041b42009-01-06 14:41:37 -080044 return be16_to_cpup((__be16 *)&data[offset]);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -070045}
46
47#define TEMP_SENSOR 1
48#define POWER_SENSOR 2
49
50#define PEX_SENSOR_TYPE_LEN 3
51static u8 const power_sensor_sig[] = {0x70, 0x77, 0x72};
52static u8 const temp_sensor_sig[] = {0x74, 0x65, 0x6D};
53
54#define PEX_MULT_LEN 2
55static u8 const watt_sensor_sig[] = {0x41, 0x43};
56
57#define PEX_NUM_SENSOR_FUNCS 3
58static char const * const power_sensor_name_templates[] = {
59 "%s%d_average",
60 "%s%d_average_lowest",
61 "%s%d_average_highest"
62};
63static char const * const temp_sensor_name_templates[] = {
64 "%s%d_input",
65 "%s%d_input_lowest",
66 "%s%d_input_highest"
67};
68
69static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
70static void ibmpex_register_bmc(int iface, struct device *dev);
71static void ibmpex_bmc_gone(int iface);
72
73struct ibmpex_sensor_data {
74 int in_use;
75 s16 values[PEX_NUM_SENSOR_FUNCS];
76 int multiplier;
77
78 struct sensor_device_attribute_2 attr[PEX_NUM_SENSOR_FUNCS];
79};
80
81struct ibmpex_bmc_data {
82 struct list_head list;
83 struct device *hwmon_dev;
84 struct device *bmc_device;
85 struct mutex lock;
86 char valid;
87 unsigned long last_updated; /* In jiffies */
88
89 struct ipmi_addr address;
90 struct completion read_complete;
91 ipmi_user_t user;
92 int interface;
93
94 struct kernel_ipmi_msg tx_message;
95 unsigned char tx_msg_data[IPMI_MAX_MSG_LENGTH];
96 long tx_msgid;
97
98 unsigned char rx_msg_data[IPMI_MAX_MSG_LENGTH];
99 unsigned long rx_msg_len;
100 unsigned char rx_result;
101 int rx_recv_type;
102
103 unsigned char sensor_major;
104 unsigned char sensor_minor;
105
106 unsigned char num_sensors;
107 struct ibmpex_sensor_data *sensors;
108};
109
110struct ibmpex_driver_data {
111 struct list_head bmc_data;
112 struct ipmi_smi_watcher bmc_events;
113 struct ipmi_user_hndl ipmi_hndlrs;
114};
115
116static struct ibmpex_driver_data driver_data = {
117 .bmc_data = LIST_HEAD_INIT(driver_data.bmc_data),
118 .bmc_events = {
119 .owner = THIS_MODULE,
120 .new_smi = ibmpex_register_bmc,
121 .smi_gone = ibmpex_bmc_gone,
122 },
123 .ipmi_hndlrs = {
124 .ipmi_recv_hndl = ibmpex_msg_handler,
125 },
126};
127
128static int ibmpex_send_message(struct ibmpex_bmc_data *data)
129{
130 int err;
131
132 err = ipmi_validate_addr(&data->address, sizeof(data->address));
133 if (err)
134 goto out;
135
136 data->tx_msgid++;
137 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
138 &data->tx_message, data, 0, 0, 0);
139 if (err)
140 goto out1;
141
142 return 0;
143out1:
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700144 dev_err(data->bmc_device, "request_settime=%x\n", err);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700145 return err;
146out:
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700147 dev_err(data->bmc_device, "validate_addr=%x\n", err);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700148 return err;
149}
150
151static int ibmpex_ver_check(struct ibmpex_bmc_data *data)
152{
153 data->tx_msg_data[0] = PEX_GET_VERSION;
154 data->tx_message.data_len = 1;
155 ibmpex_send_message(data);
156
157 wait_for_completion(&data->read_complete);
158
159 if (data->rx_result || data->rx_msg_len != 6)
160 return -ENOENT;
161
162 data->sensor_major = data->rx_msg_data[0];
163 data->sensor_minor = data->rx_msg_data[1];
164
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700165 dev_info(data->bmc_device, "Found BMC with sensor interface "
166 "v%d.%d %d-%02d-%02d on interface %d\n",
167 data->sensor_major,
168 data->sensor_minor,
169 extract_value(data->rx_msg_data, 2),
170 data->rx_msg_data[4],
171 data->rx_msg_data[5],
172 data->interface);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700173
174 return 0;
175}
176
177static int ibmpex_query_sensor_count(struct ibmpex_bmc_data *data)
178{
179 data->tx_msg_data[0] = PEX_GET_SENSOR_COUNT;
180 data->tx_message.data_len = 1;
181 ibmpex_send_message(data);
182
183 wait_for_completion(&data->read_complete);
184
185 if (data->rx_result || data->rx_msg_len != 1)
186 return -ENOENT;
187
188 return data->rx_msg_data[0];
189}
190
191static int ibmpex_query_sensor_name(struct ibmpex_bmc_data *data, int sensor)
192{
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);
197
198 wait_for_completion(&data->read_complete);
199
200 if (data->rx_result || data->rx_msg_len < 1)
201 return -ENOENT;
202
203 return 0;
204}
205
206static int ibmpex_query_sensor_data(struct ibmpex_bmc_data *data, int sensor)
207{
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);
212
213 wait_for_completion(&data->read_complete);
214
215 if (data->rx_result || data->rx_msg_len < 26) {
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700216 dev_err(data->bmc_device, "Error reading sensor %d.\n",
217 sensor);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700218 return -ENOENT;
219 }
220
221 return 0;
222}
223
224static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data *data)
225{
226 data->tx_msg_data[0] = PEX_RESET_HIGH_LOW;
227 data->tx_message.data_len = 1;
228 ibmpex_send_message(data);
229
230 wait_for_completion(&data->read_complete);
231
232 return 0;
233}
234
235static void ibmpex_update_device(struct ibmpex_bmc_data *data)
236{
237 int i, err;
238
239 mutex_lock(&data->lock);
240 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
241 data->valid)
242 goto out;
243
244 for (i = 0; i < data->num_sensors; i++) {
245 if (!data->sensors[i].in_use)
246 continue;
247 err = ibmpex_query_sensor_data(data, i);
248 if (err)
249 continue;
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);
256 }
257
258 data->last_updated = jiffies;
259 data->valid = 1;
260
261out:
262 mutex_unlock(&data->lock);
263}
264
265static struct ibmpex_bmc_data *get_bmc_data(int iface)
266{
267 struct ibmpex_bmc_data *p, *next;
268
269 list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
270 if (p->interface == iface)
271 return p;
272
273 return NULL;
274}
275
276static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
277 char *buf)
278{
279 return sprintf(buf, "%s\n", DRVNAME);
280}
281static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
282
283static ssize_t ibmpex_show_sensor(struct device *dev,
284 struct device_attribute *devattr,
285 char *buf)
286{
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);
291
292 return sprintf(buf, "%d\n",
293 data->sensors[attr->index].values[attr->nr] * mult);
294}
295
296static ssize_t ibmpex_reset_high_low(struct device *dev,
297 struct device_attribute *devattr,
298 const char *buf,
299 size_t count)
300{
301 struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
302
303 ibmpex_reset_high_low_data(data);
304
305 return count;
306}
307
308static SENSOR_DEVICE_ATTR(reset_high_low, S_IWUSR, NULL,
309 ibmpex_reset_high_low, 0);
310
311static int is_power_sensor(const char *sensor_id, int len)
312{
313 if (len < PEX_SENSOR_TYPE_LEN)
314 return 0;
315
316 if (!memcmp(sensor_id, power_sensor_sig, PEX_SENSOR_TYPE_LEN))
317 return 1;
318 return 0;
319}
320
321static int is_temp_sensor(const char *sensor_id, int len)
322{
323 if (len < PEX_SENSOR_TYPE_LEN)
324 return 0;
325
326 if (!memcmp(sensor_id, temp_sensor_sig, PEX_SENSOR_TYPE_LEN))
327 return 1;
328 return 0;
329}
330
Darrick J. Wongdf9cb032008-03-19 17:00:47 -0700331static int power_sensor_multiplier(struct ibmpex_bmc_data *data,
332 const char *sensor_id, int len)
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700333{
334 int i;
335
Darrick J. Wongdf9cb032008-03-19 17:00:47 -0700336 if (data->sensor_major == 2)
337 return 1000000;
338
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700339 for (i = PEX_SENSOR_TYPE_LEN; i < len - 1; i++)
340 if (!memcmp(&sensor_id[i], watt_sensor_sig, PEX_MULT_LEN))
341 return 1000000;
342
343 return 100000;
344}
345
346static int create_sensor(struct ibmpex_bmc_data *data, int type,
347 int counter, int sensor, int func)
348{
349 int err;
350 char *n;
351
352 n = kmalloc(32, GFP_KERNEL);
353 if (!n)
354 return -ENOMEM;
355
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);
360
361 data->sensors[sensor].attr[func].dev_attr.attr.name = n;
362 data->sensors[sensor].attr[func].dev_attr.attr.mode = S_IRUGO;
363 data->sensors[sensor].attr[func].dev_attr.show = ibmpex_show_sensor;
364 data->sensors[sensor].attr[func].index = sensor;
365 data->sensors[sensor].attr[func].nr = func;
366
367 err = device_create_file(data->bmc_device,
368 &data->sensors[sensor].attr[func].dev_attr);
369 if (err) {
370 data->sensors[sensor].attr[func].dev_attr.attr.name = NULL;
371 kfree(n);
372 return err;
373 }
374
375 return 0;
376}
377
378static int ibmpex_find_sensors(struct ibmpex_bmc_data *data)
379{
380 int i, j, err;
381 int sensor_type;
382 int sensor_counter;
383 int num_power = 0;
384 int num_temp = 0;
385
386 err = ibmpex_query_sensor_count(data);
387 if (err <= 0)
388 return -ENOENT;
389 data->num_sensors = err;
390
391 data->sensors = kzalloc(data->num_sensors * sizeof(*data->sensors),
392 GFP_KERNEL);
393 if (!data->sensors)
394 return -ENOMEM;
395
396 for (i = 0; i < data->num_sensors; i++) {
397 err = ibmpex_query_sensor_name(data, i);
398 if (err)
399 continue;
400
401 if (is_power_sensor(data->rx_msg_data, data->rx_msg_len)) {
402 sensor_type = POWER_SENSOR;
403 num_power++;
404 sensor_counter = num_power;
405 data->sensors[i].multiplier =
Darrick J. Wongdf9cb032008-03-19 17:00:47 -0700406 power_sensor_multiplier(data,
407 data->rx_msg_data,
408 data->rx_msg_len);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700409 } else if (is_temp_sensor(data->rx_msg_data,
410 data->rx_msg_len)) {
411 sensor_type = TEMP_SENSOR;
412 num_temp++;
413 sensor_counter = num_temp;
Darrick J. Wongffda6852008-03-19 17:00:48 -0700414 data->sensors[i].multiplier = 1000;
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700415 } else
416 continue;
417
418 data->sensors[i].in_use = 1;
419
420 /* Create attributes */
421 for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
422 err = create_sensor(data, sensor_type, sensor_counter,
423 i, j);
424 if (err)
425 goto exit_remove;
426 }
427 }
428
429 err = device_create_file(data->bmc_device,
430 &sensor_dev_attr_reset_high_low.dev_attr);
431 if (err)
432 goto exit_remove;
433
434 err = device_create_file(data->bmc_device,
435 &sensor_dev_attr_name.dev_attr);
436 if (err)
437 goto exit_remove;
438
439 return 0;
440
441exit_remove:
442 device_remove_file(data->bmc_device,
443 &sensor_dev_attr_reset_high_low.dev_attr);
444 device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
445 for (i = 0; i < data->num_sensors; i++)
446 for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
447 if (!data->sensors[i].attr[j].dev_attr.attr.name)
448 continue;
449 device_remove_file(data->bmc_device,
450 &data->sensors[i].attr[j].dev_attr);
451 kfree(data->sensors[i].attr[j].dev_attr.attr.name);
452 }
453
454 kfree(data->sensors);
455 return err;
456}
457
458static void ibmpex_register_bmc(int iface, struct device *dev)
459{
460 struct ibmpex_bmc_data *data;
461 int err;
462
463 data = kzalloc(sizeof(*data), GFP_KERNEL);
464 if (!data) {
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700465 dev_err(dev, "Insufficient memory for BMC interface.\n");
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700466 return;
467 }
468
469 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
470 data->address.channel = IPMI_BMC_CHANNEL;
471 data->address.data[0] = 0;
472 data->interface = iface;
473 data->bmc_device = dev;
474
475 /* Create IPMI messaging interface user */
476 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
477 data, &data->user);
478 if (err < 0) {
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700479 dev_err(dev, "Unable to register user with IPMI "
480 "interface %d\n", data->interface);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700481 goto out;
482 }
483
484 mutex_init(&data->lock);
485
486 /* Initialize message */
487 data->tx_msgid = 0;
488 init_completion(&data->read_complete);
489 data->tx_message.netfn = PEX_NET_FUNCTION;
490 data->tx_message.cmd = PEX_COMMAND;
491 data->tx_message.data = data->tx_msg_data;
492
493 /* Does this BMC support PowerExecutive? */
494 err = ibmpex_ver_check(data);
495 if (err)
496 goto out_user;
497
498 /* Register the BMC as a HWMON class device */
499 data->hwmon_dev = hwmon_device_register(data->bmc_device);
500
501 if (IS_ERR(data->hwmon_dev)) {
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700502 dev_err(data->bmc_device, "Unable to register hwmon "
503 "device for IPMI interface %d\n",
504 data->interface);
Darrick J. Wong4cfdbe72007-10-09 15:08:24 -0700505 goto out_user;
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700506 }
507
508 /* finally add the new bmc data to the bmc data list */
509 dev_set_drvdata(dev, data);
510 list_add_tail(&data->list, &driver_data.bmc_data);
511
512 /* Now go find all the sensors */
513 err = ibmpex_find_sensors(data);
514 if (err) {
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700515 dev_err(data->bmc_device, "Error %d finding sensors\n", err);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700516 goto out_register;
517 }
518
519 return;
520
521out_register:
522 hwmon_device_unregister(data->hwmon_dev);
523out_user:
524 ipmi_destroy_user(data->user);
525out:
526 kfree(data);
527}
528
529static void ibmpex_bmc_delete(struct ibmpex_bmc_data *data)
530{
531 int i, j;
532
533 device_remove_file(data->bmc_device,
534 &sensor_dev_attr_reset_high_low.dev_attr);
535 device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
536 for (i = 0; i < data->num_sensors; i++)
537 for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
538 if (!data->sensors[i].attr[j].dev_attr.attr.name)
539 continue;
540 device_remove_file(data->bmc_device,
541 &data->sensors[i].attr[j].dev_attr);
542 kfree(data->sensors[i].attr[j].dev_attr.attr.name);
543 }
544
545 list_del(&data->list);
546 dev_set_drvdata(data->bmc_device, NULL);
547 hwmon_device_unregister(data->hwmon_dev);
548 ipmi_destroy_user(data->user);
549 kfree(data->sensors);
550 kfree(data);
551}
552
553static void ibmpex_bmc_gone(int iface)
554{
555 struct ibmpex_bmc_data *data = get_bmc_data(iface);
556
557 if (!data)
558 return;
559
560 ibmpex_bmc_delete(data);
561}
562
563static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
564{
565 struct ibmpex_bmc_data *data = (struct ibmpex_bmc_data *)user_msg_data;
566
567 if (msg->msgid != data->tx_msgid) {
Darrick J. Wong2ecb0442007-10-19 16:35:07 -0700568 dev_err(data->bmc_device, "Mismatch between received msgid "
569 "(%02x) and transmitted msgid (%02x)!\n",
570 (int)msg->msgid,
571 (int)data->tx_msgid);
Darrick J. Wong57c7c3a2007-09-14 12:33:46 -0700572 ipmi_free_recv_msg(msg);
573 return;
574 }
575
576 data->rx_recv_type = msg->recv_type;
577 if (msg->msg.data_len > 0)
578 data->rx_result = msg->msg.data[0];
579 else
580 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
581
582 if (msg->msg.data_len > 1) {
583 data->rx_msg_len = msg->msg.data_len - 1;
584 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
585 } else
586 data->rx_msg_len = 0;
587
588 ipmi_free_recv_msg(msg);
589 complete(&data->read_complete);
590}
591
592static int __init ibmpex_init(void)
593{
594 return ipmi_smi_watcher_register(&driver_data.bmc_events);
595}
596
597static void __exit ibmpex_exit(void)
598{
599 struct ibmpex_bmc_data *p, *next;
600
601 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
602 list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
603 ibmpex_bmc_delete(p);
604}
605
606MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
607MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
608MODULE_LICENSE("GPL");
609
610module_init(ibmpex_init);
611module_exit(ibmpex_exit);
Darrick J. Wongff921362008-10-17 17:51:19 +0200612
613MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
614MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
615MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
616MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
617MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");