1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * A hwmon driver for the IBM PowerExecutive temperature/power sensors
4 * Copyright (C) 2007 IBM
6 * Author: Darrick J. Wong <darrick.wong@oracle.com>
9 #include <linux/ipmi.h>
10 #include <linux/module.h>
11 #include <linux/hwmon.h>
12 #include <linux/hwmon-sysfs.h>
13 #include <linux/jiffies.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
18 #define REFRESH_INTERVAL (2 * HZ)
19 #define DRVNAME "ibmpex"
21 #define PEX_GET_VERSION 1
22 #define PEX_GET_SENSOR_COUNT 2
23 #define PEX_GET_SENSOR_NAME 3
24 #define PEX_RESET_HIGH_LOW 4
25 #define PEX_GET_SENSOR_DATA 6
27 #define PEX_NET_FUNCTION 0x3A
28 #define PEX_COMMAND 0x3C
30 static inline u16
extract_value(const char *data
, int offset
)
32 return be16_to_cpup((__be16
*)&data
[offset
]);
36 #define POWER_SENSOR 2
38 #define PEX_SENSOR_TYPE_LEN 3
39 static u8
const power_sensor_sig
[] = {0x70, 0x77, 0x72};
40 static u8
const temp_sensor_sig
[] = {0x74, 0x65, 0x6D};
42 #define PEX_MULT_LEN 2
43 static u8
const watt_sensor_sig
[] = {0x41, 0x43};
45 #define PEX_NUM_SENSOR_FUNCS 3
46 static const char * const sensor_name_suffixes
[] = {
52 static void ibmpex_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
);
53 static void ibmpex_register_bmc(int iface
, struct device
*dev
);
54 static void ibmpex_bmc_gone(int iface
);
56 struct ibmpex_sensor_data
{
58 s16 values
[PEX_NUM_SENSOR_FUNCS
];
61 struct sensor_device_attribute_2 attr
[PEX_NUM_SENSOR_FUNCS
];
64 struct ibmpex_bmc_data
{
65 struct list_head list
;
66 struct device
*hwmon_dev
;
67 struct device
*bmc_device
;
70 unsigned long last_updated
; /* In jiffies */
72 struct ipmi_addr address
;
73 struct completion read_complete
;
74 struct ipmi_user
*user
;
77 struct kernel_ipmi_msg tx_message
;
78 unsigned char tx_msg_data
[IPMI_MAX_MSG_LENGTH
];
81 unsigned char rx_msg_data
[IPMI_MAX_MSG_LENGTH
];
82 unsigned long rx_msg_len
;
83 unsigned char rx_result
;
86 unsigned char sensor_major
;
87 unsigned char sensor_minor
;
89 unsigned char num_sensors
;
90 struct ibmpex_sensor_data
*sensors
;
93 struct ibmpex_driver_data
{
94 struct list_head bmc_data
;
95 struct ipmi_smi_watcher bmc_events
;
96 struct ipmi_user_hndl ipmi_hndlrs
;
99 static struct ibmpex_driver_data driver_data
= {
100 .bmc_data
= LIST_HEAD_INIT(driver_data
.bmc_data
),
102 .owner
= THIS_MODULE
,
103 .new_smi
= ibmpex_register_bmc
,
104 .smi_gone
= ibmpex_bmc_gone
,
107 .ipmi_recv_hndl
= ibmpex_msg_handler
,
111 static int ibmpex_send_message(struct ibmpex_bmc_data
*data
)
115 err
= ipmi_validate_addr(&data
->address
, sizeof(data
->address
));
120 err
= ipmi_request_settime(data
->user
, &data
->address
, data
->tx_msgid
,
121 &data
->tx_message
, data
, 0, 0, 0);
127 dev_err(data
->bmc_device
, "request_settime=%x\n", err
);
130 dev_err(data
->bmc_device
, "validate_addr=%x\n", err
);
134 static int ibmpex_ver_check(struct ibmpex_bmc_data
*data
)
136 data
->tx_msg_data
[0] = PEX_GET_VERSION
;
137 data
->tx_message
.data_len
= 1;
138 ibmpex_send_message(data
);
140 wait_for_completion(&data
->read_complete
);
142 if (data
->rx_result
|| data
->rx_msg_len
!= 6)
145 data
->sensor_major
= data
->rx_msg_data
[0];
146 data
->sensor_minor
= data
->rx_msg_data
[1];
148 dev_info(data
->bmc_device
,
149 "Found BMC with sensor interface v%d.%d %d-%02d-%02d on interface %d\n",
152 extract_value(data
->rx_msg_data
, 2),
153 data
->rx_msg_data
[4],
154 data
->rx_msg_data
[5],
160 static int ibmpex_query_sensor_count(struct ibmpex_bmc_data
*data
)
162 data
->tx_msg_data
[0] = PEX_GET_SENSOR_COUNT
;
163 data
->tx_message
.data_len
= 1;
164 ibmpex_send_message(data
);
166 wait_for_completion(&data
->read_complete
);
168 if (data
->rx_result
|| data
->rx_msg_len
!= 1)
171 return data
->rx_msg_data
[0];
174 static int ibmpex_query_sensor_name(struct ibmpex_bmc_data
*data
, int sensor
)
176 data
->tx_msg_data
[0] = PEX_GET_SENSOR_NAME
;
177 data
->tx_msg_data
[1] = sensor
;
178 data
->tx_message
.data_len
= 2;
179 ibmpex_send_message(data
);
181 wait_for_completion(&data
->read_complete
);
183 if (data
->rx_result
|| data
->rx_msg_len
< 1)
189 static int ibmpex_query_sensor_data(struct ibmpex_bmc_data
*data
, int sensor
)
191 data
->tx_msg_data
[0] = PEX_GET_SENSOR_DATA
;
192 data
->tx_msg_data
[1] = sensor
;
193 data
->tx_message
.data_len
= 2;
194 ibmpex_send_message(data
);
196 wait_for_completion(&data
->read_complete
);
198 if (data
->rx_result
|| data
->rx_msg_len
< 26) {
199 dev_err(data
->bmc_device
, "Error reading sensor %d.\n",
207 static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data
*data
)
209 data
->tx_msg_data
[0] = PEX_RESET_HIGH_LOW
;
210 data
->tx_message
.data_len
= 1;
211 ibmpex_send_message(data
);
213 wait_for_completion(&data
->read_complete
);
218 static void ibmpex_update_device(struct ibmpex_bmc_data
*data
)
222 mutex_lock(&data
->lock
);
223 if (time_before(jiffies
, data
->last_updated
+ REFRESH_INTERVAL
) &&
227 for (i
= 0; i
< data
->num_sensors
; i
++) {
228 if (!data
->sensors
[i
].in_use
)
230 err
= ibmpex_query_sensor_data(data
, i
);
233 data
->sensors
[i
].values
[0] =
234 extract_value(data
->rx_msg_data
, 16);
235 data
->sensors
[i
].values
[1] =
236 extract_value(data
->rx_msg_data
, 18);
237 data
->sensors
[i
].values
[2] =
238 extract_value(data
->rx_msg_data
, 20);
241 data
->last_updated
= jiffies
;
245 mutex_unlock(&data
->lock
);
248 static struct ibmpex_bmc_data
*get_bmc_data(int iface
)
250 struct ibmpex_bmc_data
*p
, *next
;
252 list_for_each_entry_safe(p
, next
, &driver_data
.bmc_data
, list
)
253 if (p
->interface
== iface
)
259 static DEVICE_STRING_ATTR_RO(name
, 0444, DRVNAME
);
261 static ssize_t
ibmpex_show_sensor(struct device
*dev
,
262 struct device_attribute
*devattr
,
265 struct sensor_device_attribute_2
*attr
= to_sensor_dev_attr_2(devattr
);
266 struct ibmpex_bmc_data
*data
= dev_get_drvdata(dev
);
267 int mult
= data
->sensors
[attr
->index
].multiplier
;
268 ibmpex_update_device(data
);
270 return sprintf(buf
, "%d\n",
271 data
->sensors
[attr
->index
].values
[attr
->nr
] * mult
);
274 static ssize_t
ibmpex_high_low_store(struct device
*dev
,
275 struct device_attribute
*devattr
,
276 const char *buf
, size_t count
)
278 struct ibmpex_bmc_data
*data
= dev_get_drvdata(dev
);
280 ibmpex_reset_high_low_data(data
);
285 static SENSOR_DEVICE_ATTR_WO(reset_high_low
, ibmpex_high_low
, 0);
287 static int is_power_sensor(const char *sensor_id
, int len
)
289 if (len
< PEX_SENSOR_TYPE_LEN
)
292 if (!memcmp(sensor_id
, power_sensor_sig
, PEX_SENSOR_TYPE_LEN
))
297 static int is_temp_sensor(const char *sensor_id
, int len
)
299 if (len
< PEX_SENSOR_TYPE_LEN
)
302 if (!memcmp(sensor_id
, temp_sensor_sig
, PEX_SENSOR_TYPE_LEN
))
307 static int power_sensor_multiplier(struct ibmpex_bmc_data
*data
,
308 const char *sensor_id
, int len
)
312 if (data
->sensor_major
== 2)
315 for (i
= PEX_SENSOR_TYPE_LEN
; i
< len
- 1; i
++)
316 if (!memcmp(&sensor_id
[i
], watt_sensor_sig
, PEX_MULT_LEN
))
322 static int create_sensor(struct ibmpex_bmc_data
*data
, int type
,
323 int counter
, int sensor
, int func
)
328 n
= kmalloc(32, GFP_KERNEL
);
332 if (type
== TEMP_SENSOR
)
333 sprintf(n
, "temp%d_input%s",
334 counter
, sensor_name_suffixes
[func
]);
335 else if (type
== POWER_SENSOR
)
336 sprintf(n
, "power%d_average%s",
337 counter
, sensor_name_suffixes
[func
]);
339 sysfs_attr_init(&data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
);
340 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.name
= n
;
341 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.mode
= 0444;
342 data
->sensors
[sensor
].attr
[func
].dev_attr
.show
= ibmpex_show_sensor
;
343 data
->sensors
[sensor
].attr
[func
].index
= sensor
;
344 data
->sensors
[sensor
].attr
[func
].nr
= func
;
346 err
= device_create_file(data
->bmc_device
,
347 &data
->sensors
[sensor
].attr
[func
].dev_attr
);
349 data
->sensors
[sensor
].attr
[func
].dev_attr
.attr
.name
= NULL
;
357 static int ibmpex_find_sensors(struct ibmpex_bmc_data
*data
)
365 err
= ibmpex_query_sensor_count(data
);
368 data
->num_sensors
= err
;
370 data
->sensors
= kcalloc(data
->num_sensors
, sizeof(*data
->sensors
),
375 for (i
= 0; i
< data
->num_sensors
; i
++) {
376 err
= ibmpex_query_sensor_name(data
, i
);
380 if (is_power_sensor(data
->rx_msg_data
, data
->rx_msg_len
)) {
381 sensor_type
= POWER_SENSOR
;
383 sensor_counter
= num_power
;
384 data
->sensors
[i
].multiplier
=
385 power_sensor_multiplier(data
,
388 } else if (is_temp_sensor(data
->rx_msg_data
,
390 sensor_type
= TEMP_SENSOR
;
392 sensor_counter
= num_temp
;
393 data
->sensors
[i
].multiplier
= 1000;
397 data
->sensors
[i
].in_use
= 1;
399 /* Create attributes */
400 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
401 err
= create_sensor(data
, sensor_type
, sensor_counter
,
408 err
= device_create_file(data
->bmc_device
,
409 &sensor_dev_attr_reset_high_low
.dev_attr
);
413 err
= device_create_file(data
->bmc_device
, &dev_attr_name
.attr
);
420 device_remove_file(data
->bmc_device
,
421 &sensor_dev_attr_reset_high_low
.dev_attr
);
422 device_remove_file(data
->bmc_device
, &dev_attr_name
.attr
);
423 for (i
= 0; i
< data
->num_sensors
; i
++)
424 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
425 if (!data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
)
427 device_remove_file(data
->bmc_device
,
428 &data
->sensors
[i
].attr
[j
].dev_attr
);
429 kfree(data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
);
432 kfree(data
->sensors
);
436 static void ibmpex_register_bmc(int iface
, struct device
*dev
)
438 struct ibmpex_bmc_data
*data
;
441 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
445 data
->address
.addr_type
= IPMI_SYSTEM_INTERFACE_ADDR_TYPE
;
446 data
->address
.channel
= IPMI_BMC_CHANNEL
;
447 data
->address
.data
[0] = 0;
448 data
->interface
= iface
;
449 data
->bmc_device
= dev
;
451 /* Create IPMI messaging interface user */
452 err
= ipmi_create_user(data
->interface
, &driver_data
.ipmi_hndlrs
,
456 "Unable to register user with IPMI interface %d\n",
461 mutex_init(&data
->lock
);
463 /* Initialize message */
465 init_completion(&data
->read_complete
);
466 data
->tx_message
.netfn
= PEX_NET_FUNCTION
;
467 data
->tx_message
.cmd
= PEX_COMMAND
;
468 data
->tx_message
.data
= data
->tx_msg_data
;
470 /* Does this BMC support PowerExecutive? */
471 err
= ibmpex_ver_check(data
);
475 /* Register the BMC as a HWMON class device */
476 data
->hwmon_dev
= hwmon_device_register(data
->bmc_device
);
478 if (IS_ERR(data
->hwmon_dev
)) {
479 dev_err(data
->bmc_device
,
480 "Unable to register hwmon device for IPMI interface %d\n",
485 /* finally add the new bmc data to the bmc data list */
486 dev_set_drvdata(dev
, data
);
487 list_add_tail(&data
->list
, &driver_data
.bmc_data
);
489 /* Now go find all the sensors */
490 err
= ibmpex_find_sensors(data
);
492 dev_err(data
->bmc_device
, "Error %d finding sensors\n", err
);
499 list_del(&data
->list
);
500 hwmon_device_unregister(data
->hwmon_dev
);
502 ipmi_destroy_user(data
->user
);
507 static void ibmpex_bmc_delete(struct ibmpex_bmc_data
*data
)
511 device_remove_file(data
->bmc_device
,
512 &sensor_dev_attr_reset_high_low
.dev_attr
);
513 device_remove_file(data
->bmc_device
, &dev_attr_name
.attr
);
514 for (i
= 0; i
< data
->num_sensors
; i
++)
515 for (j
= 0; j
< PEX_NUM_SENSOR_FUNCS
; j
++) {
516 if (!data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
)
518 device_remove_file(data
->bmc_device
,
519 &data
->sensors
[i
].attr
[j
].dev_attr
);
520 kfree(data
->sensors
[i
].attr
[j
].dev_attr
.attr
.name
);
523 list_del(&data
->list
);
524 dev_set_drvdata(data
->bmc_device
, NULL
);
525 hwmon_device_unregister(data
->hwmon_dev
);
526 ipmi_destroy_user(data
->user
);
527 kfree(data
->sensors
);
531 static void ibmpex_bmc_gone(int iface
)
533 struct ibmpex_bmc_data
*data
= get_bmc_data(iface
);
538 ibmpex_bmc_delete(data
);
541 static void ibmpex_msg_handler(struct ipmi_recv_msg
*msg
, void *user_msg_data
)
543 struct ibmpex_bmc_data
*data
= user_msg_data
;
545 if (msg
->msgid
!= data
->tx_msgid
) {
546 dev_err(data
->bmc_device
,
547 "Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
549 (int)data
->tx_msgid
);
550 ipmi_free_recv_msg(msg
);
554 data
->rx_recv_type
= msg
->recv_type
;
555 if (msg
->msg
.data_len
> 0)
556 data
->rx_result
= msg
->msg
.data
[0];
558 data
->rx_result
= IPMI_UNKNOWN_ERR_COMPLETION_CODE
;
560 if (msg
->msg
.data_len
> 1) {
561 data
->rx_msg_len
= msg
->msg
.data_len
- 1;
562 memcpy(data
->rx_msg_data
, msg
->msg
.data
+ 1, data
->rx_msg_len
);
564 data
->rx_msg_len
= 0;
566 ipmi_free_recv_msg(msg
);
567 complete(&data
->read_complete
);
570 static int __init
ibmpex_init(void)
572 return ipmi_smi_watcher_register(&driver_data
.bmc_events
);
575 static void __exit
ibmpex_exit(void)
577 struct ibmpex_bmc_data
*p
, *next
;
579 ipmi_smi_watcher_unregister(&driver_data
.bmc_events
);
580 list_for_each_entry_safe(p
, next
, &driver_data
.bmc_data
, list
)
581 ibmpex_bmc_delete(p
);
584 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
585 MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
586 MODULE_LICENSE("GPL");
588 module_init(ibmpex_init
);
589 module_exit(ibmpex_exit
);
591 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
592 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
593 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
594 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
595 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");