23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/ipmi.h>
26 #include <linux/module.h>
31 #include <linux/kdev_t.h>
34 #include <linux/slab.h>
35 #include <linux/sched.h>
38 #include <linux/time.h>
41 #define REFRESH_INTERVAL (HZ)
42 #define IPMI_TIMEOUT (30 * HZ)
45 #define AEM_NETFN 0x2E
47 #define AEM_FIND_FW_CMD 0x80
48 #define AEM_ELEMENT_CMD 0x81
49 #define AEM_FW_INSTANCE_CMD 0x82
51 #define AEM_READ_ELEMENT_CFG 0x80
52 #define AEM_READ_BUFFER 0x81
53 #define AEM_READ_REGISTER 0x82
54 #define AEM_WRITE_REGISTER 0x83
55 #define AEM_SET_REG_MASK 0x84
56 #define AEM_CLEAR_REG_MASK 0x85
57 #define AEM_READ_ELEMENT_CFG2 0x86
59 #define AEM_CONTROL_ELEMENT 0
60 #define AEM_ENERGY_ELEMENT 1
61 #define AEM_CLOCK_ELEMENT 4
62 #define AEM_POWER_CAP_ELEMENT 7
63 #define AEM_EXHAUST_ELEMENT 9
64 #define AEM_POWER_ELEMENT 10
66 #define AEM_MODULE_TYPE_ID 0x0001
68 #define AEM2_NUM_ENERGY_REGS 2
69 #define AEM2_NUM_PCAP_REGS 6
70 #define AEM2_NUM_TEMP_REGS 2
71 #define AEM2_NUM_SENSORS 14
73 #define AEM1_NUM_ENERGY_REGS 1
74 #define AEM1_NUM_SENSORS 3
77 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
79 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
82 #define POWER_CAP_MAX_HOTPLUG 1
83 #define POWER_CAP_MAX 2
84 #define POWER_CAP_MIN_WARNING 3
85 #define POWER_CAP_MIN 4
88 #define AEM_DEFAULT_POWER_INTERVAL 1000
89 #define AEM_MIN_POWER_INTERVAL 200
90 #define UJ_PER_MJ 1000L
191 .bytes = {0x4D, 0x4F, 0x00}
245 static void aem_register_bmc(
int iface,
struct device *
dev);
246 static void aem_bmc_gone(
int iface);
247 static void aem_msg_handler(
struct ipmi_recv_msg *
msg,
void *user_msg_data);
259 .new_smi = aem_register_bmc,
260 .smi_gone = aem_bmc_gone,
263 .ipmi_recv_hndl = aem_msg_handler,
292 dev_err(bmc,
"Unable to register user with IPMI "
327 unsigned short rx_len;
332 "(%02x) and transmitted msgid (%02x)!\n",
340 if (msg->
msg.data_len > 0)
345 if (msg->
msg.data_len > 1) {
346 rx_len = msg->
msg.data_len - 1;
381 rs_req.id = system_x_id;
383 rs_req.element = elt;
386 rs_req.rx_buf_size =
size;
392 rs_size =
sizeof(*rs_resp) +
size;
396 aem_send_message(ipmi);
405 memcmp(&rs_resp->
id, &system_x_id,
sizeof(system_x_id))) {
413 *x = rs_resp->
bytes[0];
439 static void update_aem_energy_one(
struct aem_data *data,
int which)
445 static void update_aem_energy(
struct aem_data *data)
447 update_aem_energy_one(data, 0);
450 update_aem_energy_one(data, 1);
454 static void update_aem1_sensors(
struct aem_data *data)
461 update_aem_energy(data);
467 static void update_aem2_sensors(
struct aem_data *data)
476 update_aem_energy(data);
488 static void aem_delete(
struct aem_data *data)
491 aem_remove_sensors(data);
495 platform_set_drvdata(data->
pdev,
NULL);
510 ff_req.
id = system_x_id;
521 aem_send_message(data);
528 memcmp(&ff_resp.id, &system_x_id,
sizeof(system_x_id)))
531 return ff_resp.num_instances;
535 static int aem_init_aem1_inst(
struct aem_ipmi_data *probe,
u8 module_handle)
567 platform_set_drvdata(data->
pdev, data);
578 dev_err(&data->
pdev->dev,
"Unable to register hwmon "
579 "device for IPMI interface %d\n",
585 data->
update = update_aem1_sensors;
593 res = aem1_find_sensors(data);
600 dev_info(data->
ipmi.bmc_device,
"Found AEM v%d.%d at 0x%X\n",
612 platform_set_drvdata(data->
pdev,
NULL);
627 num = aem_find_aem1_count(probe);
628 for (i = 0; i < num; i++) {
629 err = aem_init_aem1_inst(probe, i);
632 "Error %d initializing AEM1 0x%X\n",
648 fi_req.
id = system_x_id;
649 fi_req.instance_number = instance_num;
659 aem_send_message(data);
666 memcmp(&fi_resp->
id, &system_x_id,
sizeof(system_x_id)) ||
707 platform_set_drvdata(data->
pdev, data);
718 dev_err(&data->
pdev->dev,
"Unable to register hwmon "
719 "device for IPMI interface %d\n",
725 data->
update = update_aem2_sensors;
733 res = aem2_find_sensors(data);
740 dev_info(data->
ipmi.bmc_device,
"Found AEM v%d.%d at 0x%X\n",
752 platform_set_drvdata(data->
pdev,
NULL);
769 while (!aem_find_aem2(probe, &fi_resp, i)) {
770 if (fi_resp.
major != 2) {
772 "report this to the maintainer.\n",
777 err = aem_init_aem2_inst(probe, &fi_resp);
780 "Error %d initializing AEM2 0x%X\n",
788 static void aem_register_bmc(
int iface,
struct device *
dev)
792 if (aem_init_ipmi_data(&probe, iface, dev))
796 aem_init_aem1(&probe);
797 aem_init_aem2(&probe);
803 static void aem_bmc_gone(
int iface)
843 signed long leftover;
847 update_aem_energy_one(data, attr->
index);
859 update_aem_energy_one(data, attr->
index);
864 time = timespec_to_ns(&
a) - timespec_to_ns(&
b);
868 (
unsigned long long)div64_u64(delta *
NSEC_PER_SEC, time));
879 update_aem_energy_one(a, attr->
index);
883 (
unsigned long long)a->
energy[attr->
index] * 1000);
901 const char *buf,
size_t count)
908 res = kstrtoul(buf, 10, &temp);
923 static int aem_register_sensors(
struct aem_data *data,
973 aem_remove_sensors(data);
1004 static void aem_remove_sensors(
struct aem_data *data)
1009 if (!data->
sensors[i].dev_attr.attr.name)
1016 &sensor_dev_attr_name.dev_attr);
1018 &sensor_dev_attr_version.dev_attr);
1025 {
"energy1_input", aem_show_energy, 0},
1026 {
"power1_average", aem_show_power, 0},
1031 {
"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1037 {
"energy1_input", aem_show_energy, 0},
1038 {
"energy2_input", aem_show_energy, 1},
1039 {
"power1_average", aem_show_power, 0},
1040 {
"power2_average", aem_show_power, 1},
1041 {
"temp1_input", aem2_show_temp, 0},
1042 {
"temp2_input", aem2_show_temp, 1},
1049 {
"power3_average", aem2_show_pcap_value,
POWER_AUX},
1050 {
"power_cap", aem2_show_pcap_value,
POWER_CAP},
1055 {
"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1056 {
"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1061 static int aem1_find_sensors(
struct aem_data *data)
1063 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1067 static int aem2_find_sensors(
struct aem_data *data)
1069 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1074 static int __init aem_init(
void)
1080 pr_err(
"Can't register aem driver\n");
1095 static void __exit aem_exit(
void)