zfcp: Report scatter gather limit for DIX protection information
[linux-2.6/next.git] / drivers / hwmon / ibmaem.c
blobeaee546af19a1c519758783ee66217a1d8bae46d
1 /*
2 * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
3 * temperature/power/energy sensors and capping functionality.
4 * Copyright (C) 2008 IBM
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/ipmi.h>
24 #include <linux/module.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/jiffies.h>
28 #include <linux/mutex.h>
29 #include <linux/kdev_t.h>
30 #include <linux/spinlock.h>
31 #include <linux/idr.h>
32 #include <linux/slab.h>
33 #include <linux/sched.h>
34 #include <linux/platform_device.h>
35 #include <linux/math64.h>
36 #include <linux/time.h>
38 #define REFRESH_INTERVAL (HZ)
39 #define IPMI_TIMEOUT (30 * HZ)
40 #define DRVNAME "aem"
42 #define AEM_NETFN 0x2E
44 #define AEM_FIND_FW_CMD 0x80
45 #define AEM_ELEMENT_CMD 0x81
46 #define AEM_FW_INSTANCE_CMD 0x82
48 #define AEM_READ_ELEMENT_CFG 0x80
49 #define AEM_READ_BUFFER 0x81
50 #define AEM_READ_REGISTER 0x82
51 #define AEM_WRITE_REGISTER 0x83
52 #define AEM_SET_REG_MASK 0x84
53 #define AEM_CLEAR_REG_MASK 0x85
54 #define AEM_READ_ELEMENT_CFG2 0x86
56 #define AEM_CONTROL_ELEMENT 0
57 #define AEM_ENERGY_ELEMENT 1
58 #define AEM_CLOCK_ELEMENT 4
59 #define AEM_POWER_CAP_ELEMENT 7
60 #define AEM_EXHAUST_ELEMENT 9
61 #define AEM_POWER_ELEMENT 10
63 #define AEM_MODULE_TYPE_ID 0x0001
65 #define AEM2_NUM_ENERGY_REGS 2
66 #define AEM2_NUM_PCAP_REGS 6
67 #define AEM2_NUM_TEMP_REGS 2
68 #define AEM2_NUM_SENSORS 14
70 #define AEM1_NUM_ENERGY_REGS 1
71 #define AEM1_NUM_SENSORS 3
73 /* AEM 2.x has more energy registers */
74 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
75 /* AEM 2.x needs more sensor files */
76 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
78 #define POWER_CAP 0
79 #define POWER_CAP_MAX_HOTPLUG 1
80 #define POWER_CAP_MAX 2
81 #define POWER_CAP_MIN_WARNING 3
82 #define POWER_CAP_MIN 4
83 #define POWER_AUX 5
85 #define AEM_DEFAULT_POWER_INTERVAL 1000
86 #define AEM_MIN_POWER_INTERVAL 200
87 #define UJ_PER_MJ 1000L
89 static DEFINE_IDR(aem_idr);
90 static DEFINE_SPINLOCK(aem_idr_lock);
92 static struct platform_driver aem_driver = {
93 .driver = {
94 .name = DRVNAME,
95 .bus = &platform_bus_type,
99 struct aem_ipmi_data {
100 struct completion read_complete;
101 struct ipmi_addr address;
102 ipmi_user_t user;
103 int interface;
105 struct kernel_ipmi_msg tx_message;
106 long tx_msgid;
108 void *rx_msg_data;
109 unsigned short rx_msg_len;
110 unsigned char rx_result;
111 int rx_recv_type;
113 struct device *bmc_device;
116 struct aem_ro_sensor_template {
117 char *label;
118 ssize_t (*show)(struct device *dev,
119 struct device_attribute *devattr,
120 char *buf);
121 int index;
124 struct aem_rw_sensor_template {
125 char *label;
126 ssize_t (*show)(struct device *dev,
127 struct device_attribute *devattr,
128 char *buf);
129 ssize_t (*set)(struct device *dev,
130 struct device_attribute *devattr,
131 const char *buf, size_t count);
132 int index;
135 struct aem_data {
136 struct list_head list;
138 struct device *hwmon_dev;
139 struct platform_device *pdev;
140 struct mutex lock;
141 char valid;
142 unsigned long last_updated; /* In jiffies */
143 u8 ver_major;
144 u8 ver_minor;
145 u8 module_handle;
146 int id;
147 struct aem_ipmi_data ipmi;
149 /* Function to update sensors */
150 void (*update)(struct aem_data *data);
153 * AEM 1.x sensors:
154 * Available sensors:
155 * Energy meter
156 * Power meter
158 * AEM 2.x sensors:
159 * Two energy meters
160 * Two power meters
161 * Two temperature sensors
162 * Six power cap registers
165 /* sysfs attrs */
166 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
168 /* energy use in mJ */
169 u64 energy[AEM_NUM_ENERGY_REGS];
171 /* power sampling interval in ms */
172 unsigned long power_period[AEM_NUM_ENERGY_REGS];
174 /* Everything past here is for AEM2 only */
176 /* power caps in dW */
177 u16 pcap[AEM2_NUM_PCAP_REGS];
179 /* exhaust temperature in C */
180 u8 temp[AEM2_NUM_TEMP_REGS];
183 /* Data structures returned by the AEM firmware */
184 struct aem_iana_id {
185 u8 bytes[3];
187 static struct aem_iana_id system_x_id = {
188 .bytes = {0x4D, 0x4F, 0x00}
191 /* These are used to find AEM1 instances */
192 struct aem_find_firmware_req {
193 struct aem_iana_id id;
194 u8 rsvd;
195 __be16 index;
196 __be16 module_type_id;
197 } __packed;
199 struct aem_find_firmware_resp {
200 struct aem_iana_id id;
201 u8 num_instances;
202 } __packed;
204 /* These are used to find AEM2 instances */
205 struct aem_find_instance_req {
206 struct aem_iana_id id;
207 u8 instance_number;
208 __be16 module_type_id;
209 } __packed;
211 struct aem_find_instance_resp {
212 struct aem_iana_id id;
213 u8 num_instances;
214 u8 major;
215 u8 minor;
216 u8 module_handle;
217 u16 record_id;
218 } __packed;
220 /* These are used to query sensors */
221 struct aem_read_sensor_req {
222 struct aem_iana_id id;
223 u8 module_handle;
224 u8 element;
225 u8 subcommand;
226 u8 reg;
227 u8 rx_buf_size;
228 } __packed;
230 struct aem_read_sensor_resp {
231 struct aem_iana_id id;
232 u8 bytes[0];
233 } __packed;
235 /* Data structures to talk to the IPMI layer */
236 struct aem_driver_data {
237 struct list_head aem_devices;
238 struct ipmi_smi_watcher bmc_events;
239 struct ipmi_user_hndl ipmi_hndlrs;
242 static void aem_register_bmc(int iface, struct device *dev);
243 static void aem_bmc_gone(int iface);
244 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
246 static void aem_remove_sensors(struct aem_data *data);
247 static int aem_init_aem1(struct aem_ipmi_data *probe);
248 static int aem_init_aem2(struct aem_ipmi_data *probe);
249 static int aem1_find_sensors(struct aem_data *data);
250 static int aem2_find_sensors(struct aem_data *data);
251 static void update_aem1_sensors(struct aem_data *data);
252 static void update_aem2_sensors(struct aem_data *data);
254 static struct aem_driver_data driver_data = {
255 .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
256 .bmc_events = {
257 .owner = THIS_MODULE,
258 .new_smi = aem_register_bmc,
259 .smi_gone = aem_bmc_gone,
261 .ipmi_hndlrs = {
262 .ipmi_recv_hndl = aem_msg_handler,
266 /* Functions to talk to the IPMI layer */
268 /* Initialize IPMI address, message buffers and user data */
269 static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
270 struct device *bmc)
272 int err;
274 init_completion(&data->read_complete);
275 data->bmc_device = bmc;
277 /* Initialize IPMI address */
278 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
279 data->address.channel = IPMI_BMC_CHANNEL;
280 data->address.data[0] = 0;
281 data->interface = iface;
283 /* Initialize message buffers */
284 data->tx_msgid = 0;
285 data->tx_message.netfn = AEM_NETFN;
287 /* Create IPMI messaging interface user */
288 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
289 data, &data->user);
290 if (err < 0) {
291 dev_err(bmc, "Unable to register user with IPMI "
292 "interface %d\n", data->interface);
293 return -EACCES;
296 return 0;
299 /* Send an IPMI command */
300 static int aem_send_message(struct aem_ipmi_data *data)
302 int err;
304 err = ipmi_validate_addr(&data->address, sizeof(data->address));
305 if (err)
306 goto out;
308 data->tx_msgid++;
309 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
310 &data->tx_message, data, 0, 0, 0);
311 if (err)
312 goto out1;
314 return 0;
315 out1:
316 dev_err(data->bmc_device, "request_settime=%x\n", err);
317 return err;
318 out:
319 dev_err(data->bmc_device, "validate_addr=%x\n", err);
320 return err;
323 /* Dispatch IPMI messages to callers */
324 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
326 unsigned short rx_len;
327 struct aem_ipmi_data *data = user_msg_data;
329 if (msg->msgid != data->tx_msgid) {
330 dev_err(data->bmc_device, "Mismatch between received msgid "
331 "(%02x) and transmitted msgid (%02x)!\n",
332 (int)msg->msgid,
333 (int)data->tx_msgid);
334 ipmi_free_recv_msg(msg);
335 return;
338 data->rx_recv_type = msg->recv_type;
339 if (msg->msg.data_len > 0)
340 data->rx_result = msg->msg.data[0];
341 else
342 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
344 if (msg->msg.data_len > 1) {
345 rx_len = msg->msg.data_len - 1;
346 if (data->rx_msg_len < rx_len)
347 rx_len = data->rx_msg_len;
348 data->rx_msg_len = rx_len;
349 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
350 } else
351 data->rx_msg_len = 0;
353 ipmi_free_recv_msg(msg);
354 complete(&data->read_complete);
357 /* ID functions */
359 /* Obtain an id */
360 static int aem_idr_get(int *id)
362 int i, err;
364 again:
365 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
366 return -ENOMEM;
368 spin_lock(&aem_idr_lock);
369 err = idr_get_new(&aem_idr, NULL, &i);
370 spin_unlock(&aem_idr_lock);
372 if (unlikely(err == -EAGAIN))
373 goto again;
374 else if (unlikely(err))
375 return err;
377 *id = i & MAX_ID_MASK;
378 return 0;
381 /* Release an object ID */
382 static void aem_idr_put(int id)
384 spin_lock(&aem_idr_lock);
385 idr_remove(&aem_idr, id);
386 spin_unlock(&aem_idr_lock);
389 /* Sensor support functions */
391 /* Read a sensor value */
392 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
393 void *buf, size_t size)
395 int rs_size, res;
396 struct aem_read_sensor_req rs_req;
397 struct aem_read_sensor_resp *rs_resp;
398 struct aem_ipmi_data *ipmi = &data->ipmi;
400 /* AEM registers are 1, 2, 4 or 8 bytes */
401 switch (size) {
402 case 1:
403 case 2:
404 case 4:
405 case 8:
406 break;
407 default:
408 return -EINVAL;
411 rs_req.id = system_x_id;
412 rs_req.module_handle = data->module_handle;
413 rs_req.element = elt;
414 rs_req.subcommand = AEM_READ_REGISTER;
415 rs_req.reg = reg;
416 rs_req.rx_buf_size = size;
418 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
419 ipmi->tx_message.data = (char *)&rs_req;
420 ipmi->tx_message.data_len = sizeof(rs_req);
422 rs_size = sizeof(*rs_resp) + size;
423 rs_resp = kzalloc(rs_size, GFP_KERNEL);
424 if (!rs_resp)
425 return -ENOMEM;
427 ipmi->rx_msg_data = rs_resp;
428 ipmi->rx_msg_len = rs_size;
430 aem_send_message(ipmi);
432 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
433 if (!res)
434 return -ETIMEDOUT;
436 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
437 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
438 kfree(rs_resp);
439 return -ENOENT;
442 switch (size) {
443 case 1: {
444 u8 *x = buf;
445 *x = rs_resp->bytes[0];
446 break;
448 case 2: {
449 u16 *x = buf;
450 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
451 break;
453 case 4: {
454 u32 *x = buf;
455 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
456 break;
458 case 8: {
459 u64 *x = buf;
460 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
461 break;
465 return 0;
468 /* Update AEM energy registers */
469 static void update_aem_energy_one(struct aem_data *data, int which)
471 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
472 &data->energy[which], 8);
475 static void update_aem_energy(struct aem_data *data)
477 update_aem_energy_one(data, 0);
478 if (data->ver_major < 2)
479 return;
480 update_aem_energy_one(data, 1);
483 /* Update all AEM1 sensors */
484 static void update_aem1_sensors(struct aem_data *data)
486 mutex_lock(&data->lock);
487 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
488 data->valid)
489 goto out;
491 update_aem_energy(data);
492 out:
493 mutex_unlock(&data->lock);
496 /* Update all AEM2 sensors */
497 static void update_aem2_sensors(struct aem_data *data)
499 int i;
501 mutex_lock(&data->lock);
502 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
503 data->valid)
504 goto out;
506 update_aem_energy(data);
507 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
508 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
510 for (i = POWER_CAP; i <= POWER_AUX; i++)
511 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
512 &data->pcap[i], 2);
513 out:
514 mutex_unlock(&data->lock);
517 /* Delete an AEM instance */
518 static void aem_delete(struct aem_data *data)
520 list_del(&data->list);
521 aem_remove_sensors(data);
522 hwmon_device_unregister(data->hwmon_dev);
523 ipmi_destroy_user(data->ipmi.user);
524 dev_set_drvdata(&data->pdev->dev, NULL);
525 platform_device_unregister(data->pdev);
526 aem_idr_put(data->id);
527 kfree(data);
530 /* Probe functions for AEM1 devices */
532 /* Retrieve version and module handle for an AEM1 instance */
533 static int aem_find_aem1_count(struct aem_ipmi_data *data)
535 int res;
536 struct aem_find_firmware_req ff_req;
537 struct aem_find_firmware_resp ff_resp;
539 ff_req.id = system_x_id;
540 ff_req.index = 0;
541 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
543 data->tx_message.cmd = AEM_FIND_FW_CMD;
544 data->tx_message.data = (char *)&ff_req;
545 data->tx_message.data_len = sizeof(ff_req);
547 data->rx_msg_data = &ff_resp;
548 data->rx_msg_len = sizeof(ff_resp);
550 aem_send_message(data);
552 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
553 if (!res)
554 return -ETIMEDOUT;
556 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
557 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
558 return -ENOENT;
560 return ff_resp.num_instances;
563 /* Find and initialize one AEM1 instance */
564 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
566 struct aem_data *data;
567 int i;
568 int res = -ENOMEM;
570 data = kzalloc(sizeof(*data), GFP_KERNEL);
571 if (!data)
572 return res;
573 mutex_init(&data->lock);
575 /* Copy instance data */
576 data->ver_major = 1;
577 data->ver_minor = 0;
578 data->module_handle = module_handle;
579 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
580 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
582 /* Create sub-device for this fw instance */
583 if (aem_idr_get(&data->id))
584 goto id_err;
586 data->pdev = platform_device_alloc(DRVNAME, data->id);
587 if (!data->pdev)
588 goto dev_err;
589 data->pdev->dev.driver = &aem_driver.driver;
591 res = platform_device_add(data->pdev);
592 if (res)
593 goto ipmi_err;
595 dev_set_drvdata(&data->pdev->dev, data);
597 /* Set up IPMI interface */
598 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
599 probe->bmc_device))
600 goto ipmi_err;
602 /* Register with hwmon */
603 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
605 if (IS_ERR(data->hwmon_dev)) {
606 dev_err(&data->pdev->dev, "Unable to register hwmon "
607 "device for IPMI interface %d\n",
608 probe->interface);
609 goto hwmon_reg_err;
612 data->update = update_aem1_sensors;
614 /* Find sensors */
615 if (aem1_find_sensors(data))
616 goto sensor_err;
618 /* Add to our list of AEM devices */
619 list_add_tail(&data->list, &driver_data.aem_devices);
621 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
622 data->ver_major, data->ver_minor,
623 data->module_handle);
624 return 0;
626 sensor_err:
627 hwmon_device_unregister(data->hwmon_dev);
628 hwmon_reg_err:
629 ipmi_destroy_user(data->ipmi.user);
630 ipmi_err:
631 dev_set_drvdata(&data->pdev->dev, NULL);
632 platform_device_unregister(data->pdev);
633 dev_err:
634 aem_idr_put(data->id);
635 id_err:
636 kfree(data);
638 return res;
641 /* Find and initialize all AEM1 instances */
642 static int aem_init_aem1(struct aem_ipmi_data *probe)
644 int num, i, err;
646 num = aem_find_aem1_count(probe);
647 for (i = 0; i < num; i++) {
648 err = aem_init_aem1_inst(probe, i);
649 if (err) {
650 dev_err(probe->bmc_device,
651 "Error %d initializing AEM1 0x%X\n",
652 err, i);
653 return err;
657 return 0;
660 /* Probe functions for AEM2 devices */
662 /* Retrieve version and module handle for an AEM2 instance */
663 static int aem_find_aem2(struct aem_ipmi_data *data,
664 struct aem_find_instance_resp *fi_resp,
665 int instance_num)
667 int res;
668 struct aem_find_instance_req fi_req;
670 fi_req.id = system_x_id;
671 fi_req.instance_number = instance_num;
672 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
674 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
675 data->tx_message.data = (char *)&fi_req;
676 data->tx_message.data_len = sizeof(fi_req);
678 data->rx_msg_data = fi_resp;
679 data->rx_msg_len = sizeof(*fi_resp);
681 aem_send_message(data);
683 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
684 if (!res)
685 return -ETIMEDOUT;
687 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
688 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
689 fi_resp->num_instances <= instance_num)
690 return -ENOENT;
692 return 0;
695 /* Find and initialize one AEM2 instance */
696 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
697 struct aem_find_instance_resp *fi_resp)
699 struct aem_data *data;
700 int i;
701 int res = -ENOMEM;
703 data = kzalloc(sizeof(*data), GFP_KERNEL);
704 if (!data)
705 return res;
706 mutex_init(&data->lock);
708 /* Copy instance data */
709 data->ver_major = fi_resp->major;
710 data->ver_minor = fi_resp->minor;
711 data->module_handle = fi_resp->module_handle;
712 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
713 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
715 /* Create sub-device for this fw instance */
716 if (aem_idr_get(&data->id))
717 goto id_err;
719 data->pdev = platform_device_alloc(DRVNAME, data->id);
720 if (!data->pdev)
721 goto dev_err;
722 data->pdev->dev.driver = &aem_driver.driver;
724 res = platform_device_add(data->pdev);
725 if (res)
726 goto ipmi_err;
728 dev_set_drvdata(&data->pdev->dev, data);
730 /* Set up IPMI interface */
731 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
732 probe->bmc_device))
733 goto ipmi_err;
735 /* Register with hwmon */
736 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
738 if (IS_ERR(data->hwmon_dev)) {
739 dev_err(&data->pdev->dev, "Unable to register hwmon "
740 "device for IPMI interface %d\n",
741 probe->interface);
742 goto hwmon_reg_err;
745 data->update = update_aem2_sensors;
747 /* Find sensors */
748 if (aem2_find_sensors(data))
749 goto sensor_err;
751 /* Add to our list of AEM devices */
752 list_add_tail(&data->list, &driver_data.aem_devices);
754 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
755 data->ver_major, data->ver_minor,
756 data->module_handle);
757 return 0;
759 sensor_err:
760 hwmon_device_unregister(data->hwmon_dev);
761 hwmon_reg_err:
762 ipmi_destroy_user(data->ipmi.user);
763 ipmi_err:
764 dev_set_drvdata(&data->pdev->dev, NULL);
765 platform_device_unregister(data->pdev);
766 dev_err:
767 aem_idr_put(data->id);
768 id_err:
769 kfree(data);
771 return res;
774 /* Find and initialize all AEM2 instances */
775 static int aem_init_aem2(struct aem_ipmi_data *probe)
777 struct aem_find_instance_resp fi_resp;
778 int err;
779 int i = 0;
781 while (!aem_find_aem2(probe, &fi_resp, i)) {
782 if (fi_resp.major != 2) {
783 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
784 "report this to the maintainer.\n",
785 fi_resp.major);
786 i++;
787 continue;
789 err = aem_init_aem2_inst(probe, &fi_resp);
790 if (err) {
791 dev_err(probe->bmc_device,
792 "Error %d initializing AEM2 0x%X\n",
793 err, fi_resp.module_handle);
794 return err;
796 i++;
799 return 0;
802 /* Probe a BMC for AEM firmware instances */
803 static void aem_register_bmc(int iface, struct device *dev)
805 struct aem_ipmi_data probe;
807 if (aem_init_ipmi_data(&probe, iface, dev))
808 return;
810 /* Ignore probe errors; they won't cause problems */
811 aem_init_aem1(&probe);
812 aem_init_aem2(&probe);
814 ipmi_destroy_user(probe.user);
817 /* Handle BMC deletion */
818 static void aem_bmc_gone(int iface)
820 struct aem_data *p1, *next1;
822 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
823 if (p1->ipmi.interface == iface)
824 aem_delete(p1);
827 /* sysfs support functions */
829 /* AEM device name */
830 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
831 char *buf)
833 struct aem_data *data = dev_get_drvdata(dev);
835 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
837 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
839 /* AEM device version */
840 static ssize_t show_version(struct device *dev,
841 struct device_attribute *devattr,
842 char *buf)
844 struct aem_data *data = dev_get_drvdata(dev);
846 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
848 static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
850 /* Display power use */
851 static ssize_t aem_show_power(struct device *dev,
852 struct device_attribute *devattr,
853 char *buf)
855 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
856 struct aem_data *data = dev_get_drvdata(dev);
857 u64 before, after, delta, time;
858 signed long leftover;
859 struct timespec b, a;
861 mutex_lock(&data->lock);
862 update_aem_energy_one(data, attr->index);
863 getnstimeofday(&b);
864 before = data->energy[attr->index];
866 leftover = schedule_timeout_interruptible(
867 msecs_to_jiffies(data->power_period[attr->index])
869 if (leftover) {
870 mutex_unlock(&data->lock);
871 return 0;
874 update_aem_energy_one(data, attr->index);
875 getnstimeofday(&a);
876 after = data->energy[attr->index];
877 mutex_unlock(&data->lock);
879 time = timespec_to_ns(&a) - timespec_to_ns(&b);
880 delta = (after - before) * UJ_PER_MJ;
882 return sprintf(buf, "%llu\n",
883 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
886 /* Display energy use */
887 static ssize_t aem_show_energy(struct device *dev,
888 struct device_attribute *devattr,
889 char *buf)
891 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
892 struct aem_data *a = dev_get_drvdata(dev);
893 mutex_lock(&a->lock);
894 update_aem_energy_one(a, attr->index);
895 mutex_unlock(&a->lock);
897 return sprintf(buf, "%llu\n",
898 (unsigned long long)a->energy[attr->index] * 1000);
901 /* Display power interval registers */
902 static ssize_t aem_show_power_period(struct device *dev,
903 struct device_attribute *devattr,
904 char *buf)
906 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 struct aem_data *a = dev_get_drvdata(dev);
908 a->update(a);
910 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
913 /* Set power interval registers */
914 static ssize_t aem_set_power_period(struct device *dev,
915 struct device_attribute *devattr,
916 const char *buf, size_t count)
918 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
919 struct aem_data *a = dev_get_drvdata(dev);
920 unsigned long temp;
921 int res;
923 res = strict_strtoul(buf, 10, &temp);
924 if (res)
925 return res;
927 if (temp < AEM_MIN_POWER_INTERVAL)
928 return -EINVAL;
930 mutex_lock(&a->lock);
931 a->power_period[attr->index] = temp;
932 mutex_unlock(&a->lock);
934 return count;
937 /* Discover sensors on an AEM device */
938 static int aem_register_sensors(struct aem_data *data,
939 struct aem_ro_sensor_template *ro,
940 struct aem_rw_sensor_template *rw)
942 struct device *dev = &data->pdev->dev;
943 struct sensor_device_attribute *sensors = data->sensors;
944 int err;
946 /* Set up read-only sensors */
947 while (ro->label) {
948 sensors->dev_attr.attr.name = ro->label;
949 sensors->dev_attr.attr.mode = S_IRUGO;
950 sensors->dev_attr.show = ro->show;
951 sensors->index = ro->index;
953 err = device_create_file(dev, &sensors->dev_attr);
954 if (err) {
955 sensors->dev_attr.attr.name = NULL;
956 goto error;
958 sensors++;
959 ro++;
962 /* Set up read-write sensors */
963 while (rw->label) {
964 sensors->dev_attr.attr.name = rw->label;
965 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
966 sensors->dev_attr.show = rw->show;
967 sensors->dev_attr.store = rw->set;
968 sensors->index = rw->index;
970 err = device_create_file(dev, &sensors->dev_attr);
971 if (err) {
972 sensors->dev_attr.attr.name = NULL;
973 goto error;
975 sensors++;
976 rw++;
979 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
980 if (err)
981 goto error;
982 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
983 return err;
985 error:
986 aem_remove_sensors(data);
987 return err;
990 /* sysfs support functions for AEM2 sensors */
992 /* Display temperature use */
993 static ssize_t aem2_show_temp(struct device *dev,
994 struct device_attribute *devattr,
995 char *buf)
997 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
998 struct aem_data *a = dev_get_drvdata(dev);
999 a->update(a);
1001 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1004 /* Display power-capping registers */
1005 static ssize_t aem2_show_pcap_value(struct device *dev,
1006 struct device_attribute *devattr,
1007 char *buf)
1009 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1010 struct aem_data *a = dev_get_drvdata(dev);
1011 a->update(a);
1013 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1016 /* Remove sensors attached to an AEM device */
1017 static void aem_remove_sensors(struct aem_data *data)
1019 int i;
1021 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1022 if (!data->sensors[i].dev_attr.attr.name)
1023 continue;
1024 device_remove_file(&data->pdev->dev,
1025 &data->sensors[i].dev_attr);
1028 device_remove_file(&data->pdev->dev,
1029 &sensor_dev_attr_name.dev_attr);
1030 device_remove_file(&data->pdev->dev,
1031 &sensor_dev_attr_version.dev_attr);
1034 /* Sensor probe functions */
1036 /* Description of AEM1 sensors */
1037 static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1038 {"energy1_input", aem_show_energy, 0},
1039 {"power1_average", aem_show_power, 0},
1040 {NULL, NULL, 0},
1043 static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1044 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1045 {NULL, NULL, NULL, 0},
1048 /* Description of AEM2 sensors */
1049 static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1050 {"energy1_input", aem_show_energy, 0},
1051 {"energy2_input", aem_show_energy, 1},
1052 {"power1_average", aem_show_power, 0},
1053 {"power2_average", aem_show_power, 1},
1054 {"temp1_input", aem2_show_temp, 0},
1055 {"temp2_input", aem2_show_temp, 1},
1057 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1058 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1059 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1060 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1062 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1063 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1064 {NULL, NULL, 0},
1067 static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1068 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1069 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1070 {NULL, NULL, NULL, 0},
1073 /* Set up AEM1 sensor attrs */
1074 static int aem1_find_sensors(struct aem_data *data)
1076 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1079 /* Set up AEM2 sensor attrs */
1080 static int aem2_find_sensors(struct aem_data *data)
1082 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1085 /* Module init/exit routines */
1087 static int __init aem_init(void)
1089 int res;
1091 res = driver_register(&aem_driver.driver);
1092 if (res) {
1093 printk(KERN_ERR "Can't register aem driver\n");
1094 return res;
1097 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1098 if (res)
1099 goto ipmi_reg_err;
1100 return 0;
1102 ipmi_reg_err:
1103 driver_unregister(&aem_driver.driver);
1104 return res;
1108 static void __exit aem_exit(void)
1110 struct aem_data *p1, *next1;
1112 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1113 driver_unregister(&aem_driver.driver);
1114 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1115 aem_delete(p1);
1118 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1119 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1120 MODULE_LICENSE("GPL");
1122 module_init(aem_init);
1123 module_exit(aem_exit);
1125 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1126 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1127 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1128 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1129 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1130 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1131 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");