Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
0004  * temperature/power/energy sensors and capping functionality.
0005  * Copyright (C) 2008 IBM
0006  *
0007  * Author: Darrick J. Wong <darrick.wong@oracle.com>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/ipmi.h>
0013 #include <linux/module.h>
0014 #include <linux/hwmon.h>
0015 #include <linux/hwmon-sysfs.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/mutex.h>
0018 #include <linux/kdev_t.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/idr.h>
0021 #include <linux/slab.h>
0022 #include <linux/sched.h>
0023 #include <linux/platform_device.h>
0024 #include <linux/math64.h>
0025 #include <linux/time.h>
0026 #include <linux/err.h>
0027 
0028 #define REFRESH_INTERVAL    (HZ)
0029 #define IPMI_TIMEOUT        (30 * HZ)
0030 #define DRVNAME         "aem"
0031 
0032 #define AEM_NETFN       0x2E
0033 
0034 #define AEM_FIND_FW_CMD     0x80
0035 #define AEM_ELEMENT_CMD     0x81
0036 #define AEM_FW_INSTANCE_CMD 0x82
0037 
0038 #define AEM_READ_ELEMENT_CFG    0x80
0039 #define AEM_READ_BUFFER     0x81
0040 #define AEM_READ_REGISTER   0x82
0041 #define AEM_WRITE_REGISTER  0x83
0042 #define AEM_SET_REG_MASK    0x84
0043 #define AEM_CLEAR_REG_MASK  0x85
0044 #define AEM_READ_ELEMENT_CFG2   0x86
0045 
0046 #define AEM_CONTROL_ELEMENT 0
0047 #define AEM_ENERGY_ELEMENT  1
0048 #define AEM_CLOCK_ELEMENT   4
0049 #define AEM_POWER_CAP_ELEMENT   7
0050 #define AEM_EXHAUST_ELEMENT 9
0051 #define AEM_POWER_ELEMENT   10
0052 
0053 #define AEM_MODULE_TYPE_ID  0x0001
0054 
0055 #define AEM2_NUM_ENERGY_REGS    2
0056 #define AEM2_NUM_PCAP_REGS  6
0057 #define AEM2_NUM_TEMP_REGS  2
0058 #define AEM2_NUM_SENSORS    14
0059 
0060 #define AEM1_NUM_ENERGY_REGS    1
0061 #define AEM1_NUM_SENSORS    3
0062 
0063 /* AEM 2.x has more energy registers */
0064 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
0065 /* AEM 2.x needs more sensor files */
0066 #define AEM_NUM_SENSORS     AEM2_NUM_SENSORS
0067 
0068 #define POWER_CAP       0
0069 #define POWER_CAP_MAX_HOTPLUG   1
0070 #define POWER_CAP_MAX       2
0071 #define POWER_CAP_MIN_WARNING   3
0072 #define POWER_CAP_MIN       4
0073 #define POWER_AUX       5
0074 
0075 #define AEM_DEFAULT_POWER_INTERVAL 1000
0076 #define AEM_MIN_POWER_INTERVAL  200
0077 #define UJ_PER_MJ       1000L
0078 
0079 static DEFINE_IDA(aem_ida);
0080 
0081 static struct platform_driver aem_driver = {
0082     .driver = {
0083         .name = DRVNAME,
0084         .bus = &platform_bus_type,
0085     }
0086 };
0087 
0088 struct aem_ipmi_data {
0089     struct completion   read_complete;
0090     struct ipmi_addr    address;
0091     struct ipmi_user    *user;
0092     int         interface;
0093 
0094     struct kernel_ipmi_msg  tx_message;
0095     long            tx_msgid;
0096 
0097     void            *rx_msg_data;
0098     unsigned short      rx_msg_len;
0099     unsigned char       rx_result;
0100     int         rx_recv_type;
0101 
0102     struct device       *bmc_device;
0103 };
0104 
0105 struct aem_ro_sensor_template {
0106     char *label;
0107     ssize_t (*show)(struct device *dev,
0108             struct device_attribute *devattr,
0109             char *buf);
0110     int index;
0111 };
0112 
0113 struct aem_rw_sensor_template {
0114     char *label;
0115     ssize_t (*show)(struct device *dev,
0116             struct device_attribute *devattr,
0117             char *buf);
0118     ssize_t (*set)(struct device *dev,
0119                struct device_attribute *devattr,
0120                const char *buf, size_t count);
0121     int index;
0122 };
0123 
0124 struct aem_data {
0125     struct list_head    list;
0126 
0127     struct device       *hwmon_dev;
0128     struct platform_device  *pdev;
0129     struct mutex        lock;
0130     bool            valid;
0131     unsigned long       last_updated;   /* In jiffies */
0132     u8          ver_major;
0133     u8          ver_minor;
0134     u8          module_handle;
0135     int         id;
0136     struct aem_ipmi_data    ipmi;
0137 
0138     /* Function and buffer to update sensors */
0139     void (*update)(struct aem_data *data);
0140     struct aem_read_sensor_resp *rs_resp;
0141 
0142     /*
0143      * AEM 1.x sensors:
0144      * Available sensors:
0145      * Energy meter
0146      * Power meter
0147      *
0148      * AEM 2.x sensors:
0149      * Two energy meters
0150      * Two power meters
0151      * Two temperature sensors
0152      * Six power cap registers
0153      */
0154 
0155     /* sysfs attrs */
0156     struct sensor_device_attribute  sensors[AEM_NUM_SENSORS];
0157 
0158     /* energy use in mJ */
0159     u64         energy[AEM_NUM_ENERGY_REGS];
0160 
0161     /* power sampling interval in ms */
0162     unsigned long       power_period[AEM_NUM_ENERGY_REGS];
0163 
0164     /* Everything past here is for AEM2 only */
0165 
0166     /* power caps in dW */
0167     u16         pcap[AEM2_NUM_PCAP_REGS];
0168 
0169     /* exhaust temperature in C */
0170     u8          temp[AEM2_NUM_TEMP_REGS];
0171 };
0172 
0173 /* Data structures returned by the AEM firmware */
0174 struct aem_iana_id {
0175     u8          bytes[3];
0176 };
0177 static struct aem_iana_id system_x_id = {
0178     .bytes = {0x4D, 0x4F, 0x00}
0179 };
0180 
0181 /* These are used to find AEM1 instances */
0182 struct aem_find_firmware_req {
0183     struct aem_iana_id  id;
0184     u8          rsvd;
0185     __be16          index;
0186     __be16          module_type_id;
0187 } __packed;
0188 
0189 struct aem_find_firmware_resp {
0190     struct aem_iana_id  id;
0191     u8          num_instances;
0192 } __packed;
0193 
0194 /* These are used to find AEM2 instances */
0195 struct aem_find_instance_req {
0196     struct aem_iana_id  id;
0197     u8          instance_number;
0198     __be16          module_type_id;
0199 } __packed;
0200 
0201 struct aem_find_instance_resp {
0202     struct aem_iana_id  id;
0203     u8          num_instances;
0204     u8          major;
0205     u8          minor;
0206     u8          module_handle;
0207     u16         record_id;
0208 } __packed;
0209 
0210 /* These are used to query sensors */
0211 struct aem_read_sensor_req {
0212     struct aem_iana_id  id;
0213     u8          module_handle;
0214     u8          element;
0215     u8          subcommand;
0216     u8          reg;
0217     u8          rx_buf_size;
0218 } __packed;
0219 
0220 struct aem_read_sensor_resp {
0221     struct aem_iana_id  id;
0222     u8          bytes[];
0223 } __packed;
0224 
0225 /* Data structures to talk to the IPMI layer */
0226 struct aem_driver_data {
0227     struct list_head    aem_devices;
0228     struct ipmi_smi_watcher bmc_events;
0229     struct ipmi_user_hndl   ipmi_hndlrs;
0230 };
0231 
0232 static void aem_register_bmc(int iface, struct device *dev);
0233 static void aem_bmc_gone(int iface);
0234 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
0235 
0236 static void aem_remove_sensors(struct aem_data *data);
0237 static int aem1_find_sensors(struct aem_data *data);
0238 static int aem2_find_sensors(struct aem_data *data);
0239 static void update_aem1_sensors(struct aem_data *data);
0240 static void update_aem2_sensors(struct aem_data *data);
0241 
0242 static struct aem_driver_data driver_data = {
0243     .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
0244     .bmc_events = {
0245         .owner = THIS_MODULE,
0246         .new_smi = aem_register_bmc,
0247         .smi_gone = aem_bmc_gone,
0248     },
0249     .ipmi_hndlrs = {
0250         .ipmi_recv_hndl = aem_msg_handler,
0251     },
0252 };
0253 
0254 /* Functions to talk to the IPMI layer */
0255 
0256 /* Initialize IPMI address, message buffers and user data */
0257 static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
0258                   struct device *bmc)
0259 {
0260     int err;
0261 
0262     init_completion(&data->read_complete);
0263     data->bmc_device = bmc;
0264 
0265     /* Initialize IPMI address */
0266     data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
0267     data->address.channel = IPMI_BMC_CHANNEL;
0268     data->address.data[0] = 0;
0269     data->interface = iface;
0270 
0271     /* Initialize message buffers */
0272     data->tx_msgid = 0;
0273     data->tx_message.netfn = AEM_NETFN;
0274 
0275     /* Create IPMI messaging interface user */
0276     err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
0277                    data, &data->user);
0278     if (err < 0) {
0279         dev_err(bmc,
0280             "Unable to register user with IPMI interface %d\n",
0281             data->interface);
0282         return err;
0283     }
0284 
0285     return 0;
0286 }
0287 
0288 /* Send an IPMI command */
0289 static int aem_send_message(struct aem_ipmi_data *data)
0290 {
0291     int err;
0292 
0293     err = ipmi_validate_addr(&data->address, sizeof(data->address));
0294     if (err)
0295         goto out;
0296 
0297     data->tx_msgid++;
0298     err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
0299                    &data->tx_message, data, 0, 0, 0);
0300     if (err)
0301         goto out1;
0302 
0303     return 0;
0304 out1:
0305     dev_err(data->bmc_device, "request_settime=%x\n", err);
0306     return err;
0307 out:
0308     dev_err(data->bmc_device, "validate_addr=%x\n", err);
0309     return err;
0310 }
0311 
0312 /* Dispatch IPMI messages to callers */
0313 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
0314 {
0315     unsigned short rx_len;
0316     struct aem_ipmi_data *data = user_msg_data;
0317 
0318     if (msg->msgid != data->tx_msgid) {
0319         dev_err(data->bmc_device,
0320             "Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
0321             (int)msg->msgid,
0322             (int)data->tx_msgid);
0323         ipmi_free_recv_msg(msg);
0324         return;
0325     }
0326 
0327     data->rx_recv_type = msg->recv_type;
0328     if (msg->msg.data_len > 0)
0329         data->rx_result = msg->msg.data[0];
0330     else
0331         data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
0332 
0333     if (msg->msg.data_len > 1) {
0334         rx_len = msg->msg.data_len - 1;
0335         if (data->rx_msg_len < rx_len)
0336             rx_len = data->rx_msg_len;
0337         data->rx_msg_len = rx_len;
0338         memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
0339     } else
0340         data->rx_msg_len = 0;
0341 
0342     ipmi_free_recv_msg(msg);
0343     complete(&data->read_complete);
0344 }
0345 
0346 /* Sensor support functions */
0347 
0348 /* Read a sensor value; must be called with data->lock held */
0349 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
0350                void *buf, size_t size)
0351 {
0352     int rs_size, res;
0353     struct aem_read_sensor_req rs_req;
0354     /* Use preallocated rx buffer */
0355     struct aem_read_sensor_resp *rs_resp = data->rs_resp;
0356     struct aem_ipmi_data *ipmi = &data->ipmi;
0357 
0358     /* AEM registers are 1, 2, 4 or 8 bytes */
0359     switch (size) {
0360     case 1:
0361     case 2:
0362     case 4:
0363     case 8:
0364         break;
0365     default:
0366         return -EINVAL;
0367     }
0368 
0369     rs_req.id = system_x_id;
0370     rs_req.module_handle = data->module_handle;
0371     rs_req.element = elt;
0372     rs_req.subcommand = AEM_READ_REGISTER;
0373     rs_req.reg = reg;
0374     rs_req.rx_buf_size = size;
0375 
0376     ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
0377     ipmi->tx_message.data = (char *)&rs_req;
0378     ipmi->tx_message.data_len = sizeof(rs_req);
0379 
0380     rs_size = sizeof(*rs_resp) + size;
0381     ipmi->rx_msg_data = rs_resp;
0382     ipmi->rx_msg_len = rs_size;
0383 
0384     aem_send_message(ipmi);
0385 
0386     res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
0387     if (!res) {
0388         res = -ETIMEDOUT;
0389         goto out;
0390     }
0391 
0392     if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
0393         memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
0394         res = -ENOENT;
0395         goto out;
0396     }
0397 
0398     switch (size) {
0399     case 1: {
0400         u8 *x = buf;
0401         *x = rs_resp->bytes[0];
0402         break;
0403     }
0404     case 2: {
0405         u16 *x = buf;
0406         *x = be16_to_cpup((__be16 *)rs_resp->bytes);
0407         break;
0408     }
0409     case 4: {
0410         u32 *x = buf;
0411         *x = be32_to_cpup((__be32 *)rs_resp->bytes);
0412         break;
0413     }
0414     case 8: {
0415         u64 *x = buf;
0416         *x = be64_to_cpup((__be64 *)rs_resp->bytes);
0417         break;
0418     }
0419     }
0420     res = 0;
0421 
0422 out:
0423     return res;
0424 }
0425 
0426 /* Update AEM energy registers */
0427 static void update_aem_energy_one(struct aem_data *data, int which)
0428 {
0429     aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
0430             &data->energy[which], 8);
0431 }
0432 
0433 static void update_aem_energy(struct aem_data *data)
0434 {
0435     update_aem_energy_one(data, 0);
0436     if (data->ver_major < 2)
0437         return;
0438     update_aem_energy_one(data, 1);
0439 }
0440 
0441 /* Update all AEM1 sensors */
0442 static void update_aem1_sensors(struct aem_data *data)
0443 {
0444     mutex_lock(&data->lock);
0445     if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
0446         data->valid)
0447         goto out;
0448 
0449     update_aem_energy(data);
0450 out:
0451     mutex_unlock(&data->lock);
0452 }
0453 
0454 /* Update all AEM2 sensors */
0455 static void update_aem2_sensors(struct aem_data *data)
0456 {
0457     int i;
0458 
0459     mutex_lock(&data->lock);
0460     if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
0461         data->valid)
0462         goto out;
0463 
0464     update_aem_energy(data);
0465     aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
0466     aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
0467 
0468     for (i = POWER_CAP; i <= POWER_AUX; i++)
0469         aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
0470                 &data->pcap[i], 2);
0471 out:
0472     mutex_unlock(&data->lock);
0473 }
0474 
0475 /* Delete an AEM instance */
0476 static void aem_delete(struct aem_data *data)
0477 {
0478     list_del(&data->list);
0479     aem_remove_sensors(data);
0480     kfree(data->rs_resp);
0481     hwmon_device_unregister(data->hwmon_dev);
0482     ipmi_destroy_user(data->ipmi.user);
0483     platform_set_drvdata(data->pdev, NULL);
0484     platform_device_unregister(data->pdev);
0485     ida_free(&aem_ida, data->id);
0486     kfree(data);
0487 }
0488 
0489 /* Probe functions for AEM1 devices */
0490 
0491 /* Retrieve version and module handle for an AEM1 instance */
0492 static int aem_find_aem1_count(struct aem_ipmi_data *data)
0493 {
0494     int res;
0495     struct aem_find_firmware_req    ff_req;
0496     struct aem_find_firmware_resp   ff_resp;
0497 
0498     ff_req.id = system_x_id;
0499     ff_req.index = 0;
0500     ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
0501 
0502     data->tx_message.cmd = AEM_FIND_FW_CMD;
0503     data->tx_message.data = (char *)&ff_req;
0504     data->tx_message.data_len = sizeof(ff_req);
0505 
0506     data->rx_msg_data = &ff_resp;
0507     data->rx_msg_len = sizeof(ff_resp);
0508 
0509     aem_send_message(data);
0510 
0511     res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
0512     if (!res)
0513         return -ETIMEDOUT;
0514 
0515     if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
0516         memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
0517         return -ENOENT;
0518 
0519     return ff_resp.num_instances;
0520 }
0521 
0522 /* Find and initialize one AEM1 instance */
0523 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
0524 {
0525     struct aem_data *data;
0526     int i;
0527     int res = -ENOMEM;
0528 
0529     data = kzalloc(sizeof(*data), GFP_KERNEL);
0530     if (!data)
0531         return res;
0532     mutex_init(&data->lock);
0533 
0534     /* Copy instance data */
0535     data->ver_major = 1;
0536     data->ver_minor = 0;
0537     data->module_handle = module_handle;
0538     for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
0539         data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
0540 
0541     /* Create sub-device for this fw instance */
0542     data->id = ida_alloc(&aem_ida, GFP_KERNEL);
0543     if (data->id < 0)
0544         goto id_err;
0545 
0546     data->pdev = platform_device_alloc(DRVNAME, data->id);
0547     if (!data->pdev)
0548         goto dev_err;
0549     data->pdev->dev.driver = &aem_driver.driver;
0550 
0551     res = platform_device_add(data->pdev);
0552     if (res)
0553         goto dev_add_err;
0554 
0555     platform_set_drvdata(data->pdev, data);
0556 
0557     /* Set up IPMI interface */
0558     res = aem_init_ipmi_data(&data->ipmi, probe->interface,
0559                  probe->bmc_device);
0560     if (res)
0561         goto ipmi_err;
0562 
0563     /* Register with hwmon */
0564     data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
0565     if (IS_ERR(data->hwmon_dev)) {
0566         dev_err(&data->pdev->dev,
0567             "Unable to register hwmon device for IPMI interface %d\n",
0568             probe->interface);
0569         res = PTR_ERR(data->hwmon_dev);
0570         goto hwmon_reg_err;
0571     }
0572 
0573     data->update = update_aem1_sensors;
0574     data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL);
0575     if (!data->rs_resp) {
0576         res = -ENOMEM;
0577         goto alloc_resp_err;
0578     }
0579 
0580     /* Find sensors */
0581     res = aem1_find_sensors(data);
0582     if (res)
0583         goto sensor_err;
0584 
0585     /* Add to our list of AEM devices */
0586     list_add_tail(&data->list, &driver_data.aem_devices);
0587 
0588     dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
0589          data->ver_major, data->ver_minor,
0590          data->module_handle);
0591     return 0;
0592 
0593 sensor_err:
0594     kfree(data->rs_resp);
0595 alloc_resp_err:
0596     hwmon_device_unregister(data->hwmon_dev);
0597 hwmon_reg_err:
0598     ipmi_destroy_user(data->ipmi.user);
0599 ipmi_err:
0600     platform_set_drvdata(data->pdev, NULL);
0601     platform_device_del(data->pdev);
0602 dev_add_err:
0603     platform_device_put(data->pdev);
0604 dev_err:
0605     ida_free(&aem_ida, data->id);
0606 id_err:
0607     kfree(data);
0608 
0609     return res;
0610 }
0611 
0612 /* Find and initialize all AEM1 instances */
0613 static void aem_init_aem1(struct aem_ipmi_data *probe)
0614 {
0615     int num, i, err;
0616 
0617     num = aem_find_aem1_count(probe);
0618     for (i = 0; i < num; i++) {
0619         err = aem_init_aem1_inst(probe, i);
0620         if (err) {
0621             dev_err(probe->bmc_device,
0622                 "Error %d initializing AEM1 0x%X\n",
0623                 err, i);
0624         }
0625     }
0626 }
0627 
0628 /* Probe functions for AEM2 devices */
0629 
0630 /* Retrieve version and module handle for an AEM2 instance */
0631 static int aem_find_aem2(struct aem_ipmi_data *data,
0632                 struct aem_find_instance_resp *fi_resp,
0633                 int instance_num)
0634 {
0635     int res;
0636     struct aem_find_instance_req fi_req;
0637 
0638     fi_req.id = system_x_id;
0639     fi_req.instance_number = instance_num;
0640     fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
0641 
0642     data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
0643     data->tx_message.data = (char *)&fi_req;
0644     data->tx_message.data_len = sizeof(fi_req);
0645 
0646     data->rx_msg_data = fi_resp;
0647     data->rx_msg_len = sizeof(*fi_resp);
0648 
0649     aem_send_message(data);
0650 
0651     res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
0652     if (!res)
0653         return -ETIMEDOUT;
0654 
0655     if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
0656         memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
0657         fi_resp->num_instances <= instance_num)
0658         return -ENOENT;
0659 
0660     return 0;
0661 }
0662 
0663 /* Find and initialize one AEM2 instance */
0664 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
0665                   struct aem_find_instance_resp *fi_resp)
0666 {
0667     struct aem_data *data;
0668     int i;
0669     int res = -ENOMEM;
0670 
0671     data = kzalloc(sizeof(*data), GFP_KERNEL);
0672     if (!data)
0673         return res;
0674     mutex_init(&data->lock);
0675 
0676     /* Copy instance data */
0677     data->ver_major = fi_resp->major;
0678     data->ver_minor = fi_resp->minor;
0679     data->module_handle = fi_resp->module_handle;
0680     for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
0681         data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
0682 
0683     /* Create sub-device for this fw instance */
0684     data->id = ida_alloc(&aem_ida, GFP_KERNEL);
0685     if (data->id < 0)
0686         goto id_err;
0687 
0688     data->pdev = platform_device_alloc(DRVNAME, data->id);
0689     if (!data->pdev)
0690         goto dev_err;
0691     data->pdev->dev.driver = &aem_driver.driver;
0692 
0693     res = platform_device_add(data->pdev);
0694     if (res)
0695         goto dev_add_err;
0696 
0697     platform_set_drvdata(data->pdev, data);
0698 
0699     /* Set up IPMI interface */
0700     res = aem_init_ipmi_data(&data->ipmi, probe->interface,
0701                  probe->bmc_device);
0702     if (res)
0703         goto ipmi_err;
0704 
0705     /* Register with hwmon */
0706     data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
0707     if (IS_ERR(data->hwmon_dev)) {
0708         dev_err(&data->pdev->dev,
0709             "Unable to register hwmon device for IPMI interface %d\n",
0710             probe->interface);
0711         res = PTR_ERR(data->hwmon_dev);
0712         goto hwmon_reg_err;
0713     }
0714 
0715     data->update = update_aem2_sensors;
0716     data->rs_resp = kzalloc(sizeof(*(data->rs_resp)) + 8, GFP_KERNEL);
0717     if (!data->rs_resp) {
0718         res = -ENOMEM;
0719         goto alloc_resp_err;
0720     }
0721 
0722     /* Find sensors */
0723     res = aem2_find_sensors(data);
0724     if (res)
0725         goto sensor_err;
0726 
0727     /* Add to our list of AEM devices */
0728     list_add_tail(&data->list, &driver_data.aem_devices);
0729 
0730     dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
0731          data->ver_major, data->ver_minor,
0732          data->module_handle);
0733     return 0;
0734 
0735 sensor_err:
0736     kfree(data->rs_resp);
0737 alloc_resp_err:
0738     hwmon_device_unregister(data->hwmon_dev);
0739 hwmon_reg_err:
0740     ipmi_destroy_user(data->ipmi.user);
0741 ipmi_err:
0742     platform_set_drvdata(data->pdev, NULL);
0743     platform_device_del(data->pdev);
0744 dev_add_err:
0745     platform_device_put(data->pdev);
0746 dev_err:
0747     ida_free(&aem_ida, data->id);
0748 id_err:
0749     kfree(data);
0750 
0751     return res;
0752 }
0753 
0754 /* Find and initialize all AEM2 instances */
0755 static void aem_init_aem2(struct aem_ipmi_data *probe)
0756 {
0757     struct aem_find_instance_resp fi_resp;
0758     int err;
0759     int i = 0;
0760 
0761     while (!aem_find_aem2(probe, &fi_resp, i)) {
0762         if (fi_resp.major != 2) {
0763             dev_err(probe->bmc_device,
0764                 "Unknown AEM v%d; please report this to the maintainer.\n",
0765                 fi_resp.major);
0766             i++;
0767             continue;
0768         }
0769         err = aem_init_aem2_inst(probe, &fi_resp);
0770         if (err) {
0771             dev_err(probe->bmc_device,
0772                 "Error %d initializing AEM2 0x%X\n",
0773                 err, fi_resp.module_handle);
0774         }
0775         i++;
0776     }
0777 }
0778 
0779 /* Probe a BMC for AEM firmware instances */
0780 static void aem_register_bmc(int iface, struct device *dev)
0781 {
0782     struct aem_ipmi_data probe;
0783 
0784     if (aem_init_ipmi_data(&probe, iface, dev))
0785         return;
0786 
0787     /* Ignore probe errors; they won't cause problems */
0788     aem_init_aem1(&probe);
0789     aem_init_aem2(&probe);
0790 
0791     ipmi_destroy_user(probe.user);
0792 }
0793 
0794 /* Handle BMC deletion */
0795 static void aem_bmc_gone(int iface)
0796 {
0797     struct aem_data *p1, *next1;
0798 
0799     list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
0800         if (p1->ipmi.interface == iface)
0801             aem_delete(p1);
0802 }
0803 
0804 /* sysfs support functions */
0805 
0806 /* AEM device name */
0807 static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
0808              char *buf)
0809 {
0810     struct aem_data *data = dev_get_drvdata(dev);
0811 
0812     return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
0813 }
0814 static SENSOR_DEVICE_ATTR_RO(name, name, 0);
0815 
0816 /* AEM device version */
0817 static ssize_t version_show(struct device *dev,
0818                 struct device_attribute *devattr, char *buf)
0819 {
0820     struct aem_data *data = dev_get_drvdata(dev);
0821 
0822     return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
0823 }
0824 static SENSOR_DEVICE_ATTR_RO(version, version, 0);
0825 
0826 /* Display power use */
0827 static ssize_t aem_show_power(struct device *dev,
0828                   struct device_attribute *devattr,
0829                   char *buf)
0830 {
0831     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0832     struct aem_data *data = dev_get_drvdata(dev);
0833     u64 before, after, delta, time;
0834     signed long leftover;
0835 
0836     mutex_lock(&data->lock);
0837     update_aem_energy_one(data, attr->index);
0838     time = ktime_get_ns();
0839     before = data->energy[attr->index];
0840 
0841     leftover = schedule_timeout_interruptible(
0842             msecs_to_jiffies(data->power_period[attr->index])
0843            );
0844     if (leftover) {
0845         mutex_unlock(&data->lock);
0846         return 0;
0847     }
0848 
0849     update_aem_energy_one(data, attr->index);
0850     time = ktime_get_ns() - time;
0851     after = data->energy[attr->index];
0852     mutex_unlock(&data->lock);
0853 
0854     delta = (after - before) * UJ_PER_MJ;
0855 
0856     return sprintf(buf, "%llu\n",
0857         (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
0858 }
0859 
0860 /* Display energy use */
0861 static ssize_t aem_show_energy(struct device *dev,
0862                    struct device_attribute *devattr,
0863                    char *buf)
0864 {
0865     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0866     struct aem_data *a = dev_get_drvdata(dev);
0867     mutex_lock(&a->lock);
0868     update_aem_energy_one(a, attr->index);
0869     mutex_unlock(&a->lock);
0870 
0871     return sprintf(buf, "%llu\n",
0872             (unsigned long long)a->energy[attr->index] * 1000);
0873 }
0874 
0875 /* Display power interval registers */
0876 static ssize_t aem_show_power_period(struct device *dev,
0877                      struct device_attribute *devattr,
0878                      char *buf)
0879 {
0880     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0881     struct aem_data *a = dev_get_drvdata(dev);
0882     a->update(a);
0883 
0884     return sprintf(buf, "%lu\n", a->power_period[attr->index]);
0885 }
0886 
0887 /* Set power interval registers */
0888 static ssize_t aem_set_power_period(struct device *dev,
0889                     struct device_attribute *devattr,
0890                     const char *buf, size_t count)
0891 {
0892     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0893     struct aem_data *a = dev_get_drvdata(dev);
0894     unsigned long temp;
0895     int res;
0896 
0897     res = kstrtoul(buf, 10, &temp);
0898     if (res)
0899         return res;
0900 
0901     if (temp < AEM_MIN_POWER_INTERVAL)
0902         return -EINVAL;
0903 
0904     mutex_lock(&a->lock);
0905     a->power_period[attr->index] = temp;
0906     mutex_unlock(&a->lock);
0907 
0908     return count;
0909 }
0910 
0911 /* Discover sensors on an AEM device */
0912 static int aem_register_sensors(struct aem_data *data,
0913                 const struct aem_ro_sensor_template *ro,
0914                 const struct aem_rw_sensor_template *rw)
0915 {
0916     struct device *dev = &data->pdev->dev;
0917     struct sensor_device_attribute *sensors = data->sensors;
0918     int err;
0919 
0920     /* Set up read-only sensors */
0921     while (ro->label) {
0922         sysfs_attr_init(&sensors->dev_attr.attr);
0923         sensors->dev_attr.attr.name = ro->label;
0924         sensors->dev_attr.attr.mode = 0444;
0925         sensors->dev_attr.show = ro->show;
0926         sensors->index = ro->index;
0927 
0928         err = device_create_file(dev, &sensors->dev_attr);
0929         if (err) {
0930             sensors->dev_attr.attr.name = NULL;
0931             goto error;
0932         }
0933         sensors++;
0934         ro++;
0935     }
0936 
0937     /* Set up read-write sensors */
0938     while (rw->label) {
0939         sysfs_attr_init(&sensors->dev_attr.attr);
0940         sensors->dev_attr.attr.name = rw->label;
0941         sensors->dev_attr.attr.mode = 0644;
0942         sensors->dev_attr.show = rw->show;
0943         sensors->dev_attr.store = rw->set;
0944         sensors->index = rw->index;
0945 
0946         err = device_create_file(dev, &sensors->dev_attr);
0947         if (err) {
0948             sensors->dev_attr.attr.name = NULL;
0949             goto error;
0950         }
0951         sensors++;
0952         rw++;
0953     }
0954 
0955     err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
0956     if (err)
0957         goto error;
0958     err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
0959     return err;
0960 
0961 error:
0962     aem_remove_sensors(data);
0963     return err;
0964 }
0965 
0966 /* sysfs support functions for AEM2 sensors */
0967 
0968 /* Display temperature use */
0969 static ssize_t aem2_show_temp(struct device *dev,
0970                   struct device_attribute *devattr,
0971                   char *buf)
0972 {
0973     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0974     struct aem_data *a = dev_get_drvdata(dev);
0975     a->update(a);
0976 
0977     return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
0978 }
0979 
0980 /* Display power-capping registers */
0981 static ssize_t aem2_show_pcap_value(struct device *dev,
0982                     struct device_attribute *devattr,
0983                     char *buf)
0984 {
0985     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0986     struct aem_data *a = dev_get_drvdata(dev);
0987     a->update(a);
0988 
0989     return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
0990 }
0991 
0992 /* Remove sensors attached to an AEM device */
0993 static void aem_remove_sensors(struct aem_data *data)
0994 {
0995     int i;
0996 
0997     for (i = 0; i < AEM_NUM_SENSORS; i++) {
0998         if (!data->sensors[i].dev_attr.attr.name)
0999             continue;
1000         device_remove_file(&data->pdev->dev,
1001                    &data->sensors[i].dev_attr);
1002     }
1003 
1004     device_remove_file(&data->pdev->dev,
1005                &sensor_dev_attr_name.dev_attr);
1006     device_remove_file(&data->pdev->dev,
1007                &sensor_dev_attr_version.dev_attr);
1008 }
1009 
1010 /* Sensor probe functions */
1011 
1012 /* Description of AEM1 sensors */
1013 static const struct aem_ro_sensor_template aem1_ro_sensors[] = {
1014 {"energy1_input",  aem_show_energy, 0},
1015 {"power1_average", aem_show_power,  0},
1016 {NULL,         NULL,        0},
1017 };
1018 
1019 static const struct aem_rw_sensor_template aem1_rw_sensors[] = {
1020 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1021 {NULL,              NULL,                  NULL,                 0},
1022 };
1023 
1024 /* Description of AEM2 sensors */
1025 static const struct aem_ro_sensor_template aem2_ro_sensors[] = {
1026 {"energy1_input",     aem_show_energy,  0},
1027 {"energy2_input",     aem_show_energy,  1},
1028 {"power1_average",    aem_show_power,   0},
1029 {"power2_average",    aem_show_power,   1},
1030 {"temp1_input",       aem2_show_temp,   0},
1031 {"temp2_input",       aem2_show_temp,   1},
1032 
1033 {"power4_average",    aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1034 {"power5_average",    aem2_show_pcap_value, POWER_CAP_MAX},
1035 {"power6_average",    aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1036 {"power7_average",    aem2_show_pcap_value, POWER_CAP_MIN},
1037 
1038 {"power3_average",    aem2_show_pcap_value, POWER_AUX},
1039 {"power_cap",         aem2_show_pcap_value, POWER_CAP},
1040 {NULL,                    NULL,                 0},
1041 };
1042 
1043 static const struct aem_rw_sensor_template aem2_rw_sensors[] = {
1044 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1045 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1046 {NULL,              NULL,                  NULL,                 0},
1047 };
1048 
1049 /* Set up AEM1 sensor attrs */
1050 static int aem1_find_sensors(struct aem_data *data)
1051 {
1052     return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1053 }
1054 
1055 /* Set up AEM2 sensor attrs */
1056 static int aem2_find_sensors(struct aem_data *data)
1057 {
1058     return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1059 }
1060 
1061 /* Module init/exit routines */
1062 
1063 static int __init aem_init(void)
1064 {
1065     int res;
1066 
1067     res = driver_register(&aem_driver.driver);
1068     if (res) {
1069         pr_err("Can't register aem driver\n");
1070         return res;
1071     }
1072 
1073     res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1074     if (res)
1075         goto ipmi_reg_err;
1076     return 0;
1077 
1078 ipmi_reg_err:
1079     driver_unregister(&aem_driver.driver);
1080     return res;
1081 
1082 }
1083 
1084 static void __exit aem_exit(void)
1085 {
1086     struct aem_data *p1, *next1;
1087 
1088     ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1089     driver_unregister(&aem_driver.driver);
1090     list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1091         aem_delete(p1);
1092 }
1093 
1094 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1095 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1096 MODULE_LICENSE("GPL");
1097 
1098 module_init(aem_init);
1099 module_exit(aem_exit);
1100 
1101 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1102 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1103 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1104 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1105 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1106 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1107 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");