mac80211: fix virtual interfaces vs. injection
[linux/fpc-iii.git] / drivers / hwmon / ibmaem.c
blob0f70dc204105370ce203a9725bdfd272fb6c3a24
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/sched.h>
33 #include <linux/platform_device.h>
34 #include <linux/math64.h>
35 #include <linux/time.h>
37 #define REFRESH_INTERVAL (HZ)
38 #define IPMI_TIMEOUT (30 * HZ)
39 #define DRVNAME "aem"
41 #define AEM_NETFN 0x2E
43 #define AEM_FIND_FW_CMD 0x80
44 #define AEM_ELEMENT_CMD 0x81
45 #define AEM_FW_INSTANCE_CMD 0x82
47 #define AEM_READ_ELEMENT_CFG 0x80
48 #define AEM_READ_BUFFER 0x81
49 #define AEM_READ_REGISTER 0x82
50 #define AEM_WRITE_REGISTER 0x83
51 #define AEM_SET_REG_MASK 0x84
52 #define AEM_CLEAR_REG_MASK 0x85
53 #define AEM_READ_ELEMENT_CFG2 0x86
55 #define AEM_CONTROL_ELEMENT 0
56 #define AEM_ENERGY_ELEMENT 1
57 #define AEM_CLOCK_ELEMENT 4
58 #define AEM_POWER_CAP_ELEMENT 7
59 #define AEM_EXHAUST_ELEMENT 9
60 #define AEM_POWER_ELEMENT 10
62 #define AEM_MODULE_TYPE_ID 0x0001
64 #define AEM2_NUM_ENERGY_REGS 2
65 #define AEM2_NUM_PCAP_REGS 6
66 #define AEM2_NUM_TEMP_REGS 2
67 #define AEM2_NUM_SENSORS 14
69 #define AEM1_NUM_ENERGY_REGS 1
70 #define AEM1_NUM_SENSORS 3
72 /* AEM 2.x has more energy registers */
73 #define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
74 /* AEM 2.x needs more sensor files */
75 #define AEM_NUM_SENSORS AEM2_NUM_SENSORS
77 #define POWER_CAP 0
78 #define POWER_CAP_MAX_HOTPLUG 1
79 #define POWER_CAP_MAX 2
80 #define POWER_CAP_MIN_WARNING 3
81 #define POWER_CAP_MIN 4
82 #define POWER_AUX 5
84 #define AEM_DEFAULT_POWER_INTERVAL 1000
85 #define AEM_MIN_POWER_INTERVAL 200
86 #define UJ_PER_MJ 1000L
88 static DEFINE_IDR(aem_idr);
89 static DEFINE_SPINLOCK(aem_idr_lock);
91 static struct device_driver aem_driver = {
92 .name = DRVNAME,
93 .bus = &platform_bus_type,
96 struct aem_ipmi_data {
97 struct completion read_complete;
98 struct ipmi_addr address;
99 ipmi_user_t user;
100 int interface;
102 struct kernel_ipmi_msg tx_message;
103 long tx_msgid;
105 void *rx_msg_data;
106 unsigned short rx_msg_len;
107 unsigned char rx_result;
108 int rx_recv_type;
110 struct device *bmc_device;
113 struct aem_ro_sensor_template {
114 char *label;
115 ssize_t (*show)(struct device *dev,
116 struct device_attribute *devattr,
117 char *buf);
118 int index;
121 struct aem_rw_sensor_template {
122 char *label;
123 ssize_t (*show)(struct device *dev,
124 struct device_attribute *devattr,
125 char *buf);
126 ssize_t (*set)(struct device *dev,
127 struct device_attribute *devattr,
128 const char *buf, size_t count);
129 int index;
132 struct aem_data {
133 struct list_head list;
135 struct device *hwmon_dev;
136 struct platform_device *pdev;
137 struct mutex lock;
138 char valid;
139 unsigned long last_updated; /* In jiffies */
140 u8 ver_major;
141 u8 ver_minor;
142 u8 module_handle;
143 int id;
144 struct aem_ipmi_data ipmi;
146 /* Function to update sensors */
147 void (*update)(struct aem_data *data);
150 * AEM 1.x sensors:
151 * Available sensors:
152 * Energy meter
153 * Power meter
155 * AEM 2.x sensors:
156 * Two energy meters
157 * Two power meters
158 * Two temperature sensors
159 * Six power cap registers
162 /* sysfs attrs */
163 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
165 /* energy use in mJ */
166 u64 energy[AEM_NUM_ENERGY_REGS];
168 /* power sampling interval in ms */
169 unsigned long power_period[AEM_NUM_ENERGY_REGS];
171 /* Everything past here is for AEM2 only */
173 /* power caps in dW */
174 u16 pcap[AEM2_NUM_PCAP_REGS];
176 /* exhaust temperature in C */
177 u8 temp[AEM2_NUM_TEMP_REGS];
180 /* Data structures returned by the AEM firmware */
181 struct aem_iana_id {
182 u8 bytes[3];
184 static struct aem_iana_id system_x_id = {
185 .bytes = {0x4D, 0x4F, 0x00}
188 /* These are used to find AEM1 instances */
189 struct aem_find_firmware_req {
190 struct aem_iana_id id;
191 u8 rsvd;
192 __be16 index;
193 __be16 module_type_id;
194 } __packed;
196 struct aem_find_firmware_resp {
197 struct aem_iana_id id;
198 u8 num_instances;
199 } __packed;
201 /* These are used to find AEM2 instances */
202 struct aem_find_instance_req {
203 struct aem_iana_id id;
204 u8 instance_number;
205 __be16 module_type_id;
206 } __packed;
208 struct aem_find_instance_resp {
209 struct aem_iana_id id;
210 u8 num_instances;
211 u8 major;
212 u8 minor;
213 u8 module_handle;
214 u16 record_id;
215 } __packed;
217 /* These are used to query sensors */
218 struct aem_read_sensor_req {
219 struct aem_iana_id id;
220 u8 module_handle;
221 u8 element;
222 u8 subcommand;
223 u8 reg;
224 u8 rx_buf_size;
225 } __packed;
227 struct aem_read_sensor_resp {
228 struct aem_iana_id id;
229 u8 bytes[0];
230 } __packed;
232 /* Data structures to talk to the IPMI layer */
233 struct aem_driver_data {
234 struct list_head aem_devices;
235 struct ipmi_smi_watcher bmc_events;
236 struct ipmi_user_hndl ipmi_hndlrs;
239 static void aem_register_bmc(int iface, struct device *dev);
240 static void aem_bmc_gone(int iface);
241 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
243 static void aem_remove_sensors(struct aem_data *data);
244 static int aem_init_aem1(struct aem_ipmi_data *probe);
245 static int aem_init_aem2(struct aem_ipmi_data *probe);
246 static int aem1_find_sensors(struct aem_data *data);
247 static int aem2_find_sensors(struct aem_data *data);
248 static void update_aem1_sensors(struct aem_data *data);
249 static void update_aem2_sensors(struct aem_data *data);
251 static struct aem_driver_data driver_data = {
252 .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
253 .bmc_events = {
254 .owner = THIS_MODULE,
255 .new_smi = aem_register_bmc,
256 .smi_gone = aem_bmc_gone,
258 .ipmi_hndlrs = {
259 .ipmi_recv_hndl = aem_msg_handler,
263 /* Functions to talk to the IPMI layer */
265 /* Initialize IPMI address, message buffers and user data */
266 static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
267 struct device *bmc)
269 int err;
271 init_completion(&data->read_complete);
272 data->bmc_device = bmc;
274 /* Initialize IPMI address */
275 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
276 data->address.channel = IPMI_BMC_CHANNEL;
277 data->address.data[0] = 0;
278 data->interface = iface;
280 /* Initialize message buffers */
281 data->tx_msgid = 0;
282 data->tx_message.netfn = AEM_NETFN;
284 /* Create IPMI messaging interface user */
285 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
286 data, &data->user);
287 if (err < 0) {
288 dev_err(bmc, "Unable to register user with IPMI "
289 "interface %d\n", data->interface);
290 return -EACCES;
293 return 0;
296 /* Send an IPMI command */
297 static int aem_send_message(struct aem_ipmi_data *data)
299 int err;
301 err = ipmi_validate_addr(&data->address, sizeof(data->address));
302 if (err)
303 goto out;
305 data->tx_msgid++;
306 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
307 &data->tx_message, data, 0, 0, 0);
308 if (err)
309 goto out1;
311 return 0;
312 out1:
313 dev_err(data->bmc_device, "request_settime=%x\n", err);
314 return err;
315 out:
316 dev_err(data->bmc_device, "validate_addr=%x\n", err);
317 return err;
320 /* Dispatch IPMI messages to callers */
321 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
323 unsigned short rx_len;
324 struct aem_ipmi_data *data = user_msg_data;
326 if (msg->msgid != data->tx_msgid) {
327 dev_err(data->bmc_device, "Mismatch between received msgid "
328 "(%02x) and transmitted msgid (%02x)!\n",
329 (int)msg->msgid,
330 (int)data->tx_msgid);
331 ipmi_free_recv_msg(msg);
332 return;
335 data->rx_recv_type = msg->recv_type;
336 if (msg->msg.data_len > 0)
337 data->rx_result = msg->msg.data[0];
338 else
339 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
341 if (msg->msg.data_len > 1) {
342 rx_len = msg->msg.data_len - 1;
343 if (data->rx_msg_len < rx_len)
344 rx_len = data->rx_msg_len;
345 data->rx_msg_len = rx_len;
346 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
347 } else
348 data->rx_msg_len = 0;
350 ipmi_free_recv_msg(msg);
351 complete(&data->read_complete);
354 /* ID functions */
356 /* Obtain an id */
357 static int aem_idr_get(int *id)
359 int i, err;
361 again:
362 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
363 return -ENOMEM;
365 spin_lock(&aem_idr_lock);
366 err = idr_get_new(&aem_idr, NULL, &i);
367 spin_unlock(&aem_idr_lock);
369 if (unlikely(err == -EAGAIN))
370 goto again;
371 else if (unlikely(err))
372 return err;
374 *id = i & MAX_ID_MASK;
375 return 0;
378 /* Release an object ID */
379 static void aem_idr_put(int id)
381 spin_lock(&aem_idr_lock);
382 idr_remove(&aem_idr, id);
383 spin_unlock(&aem_idr_lock);
386 /* Sensor support functions */
388 /* Read a sensor value */
389 static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
390 void *buf, size_t size)
392 int rs_size, res;
393 struct aem_read_sensor_req rs_req;
394 struct aem_read_sensor_resp *rs_resp;
395 struct aem_ipmi_data *ipmi = &data->ipmi;
397 /* AEM registers are 1, 2, 4 or 8 bytes */
398 switch (size) {
399 case 1:
400 case 2:
401 case 4:
402 case 8:
403 break;
404 default:
405 return -EINVAL;
408 rs_req.id = system_x_id;
409 rs_req.module_handle = data->module_handle;
410 rs_req.element = elt;
411 rs_req.subcommand = AEM_READ_REGISTER;
412 rs_req.reg = reg;
413 rs_req.rx_buf_size = size;
415 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
416 ipmi->tx_message.data = (char *)&rs_req;
417 ipmi->tx_message.data_len = sizeof(rs_req);
419 rs_size = sizeof(*rs_resp) + size;
420 rs_resp = kzalloc(rs_size, GFP_KERNEL);
421 if (!rs_resp)
422 return -ENOMEM;
424 ipmi->rx_msg_data = rs_resp;
425 ipmi->rx_msg_len = rs_size;
427 aem_send_message(ipmi);
429 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
430 if (!res)
431 return -ETIMEDOUT;
433 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
434 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
435 kfree(rs_resp);
436 return -ENOENT;
439 switch (size) {
440 case 1: {
441 u8 *x = buf;
442 *x = rs_resp->bytes[0];
443 break;
445 case 2: {
446 u16 *x = buf;
447 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
448 break;
450 case 4: {
451 u32 *x = buf;
452 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
453 break;
455 case 8: {
456 u64 *x = buf;
457 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
458 break;
462 return 0;
465 /* Update AEM energy registers */
466 static void update_aem_energy_one(struct aem_data *data, int which)
468 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
469 &data->energy[which], 8);
472 static void update_aem_energy(struct aem_data *data)
474 update_aem_energy_one(data, 0);
475 if (data->ver_major < 2)
476 return;
477 update_aem_energy_one(data, 1);
480 /* Update all AEM1 sensors */
481 static void update_aem1_sensors(struct aem_data *data)
483 mutex_lock(&data->lock);
484 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
485 data->valid)
486 goto out;
488 update_aem_energy(data);
489 out:
490 mutex_unlock(&data->lock);
493 /* Update all AEM2 sensors */
494 static void update_aem2_sensors(struct aem_data *data)
496 int i;
498 mutex_lock(&data->lock);
499 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
500 data->valid)
501 goto out;
503 update_aem_energy(data);
504 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
505 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
507 for (i = POWER_CAP; i <= POWER_AUX; i++)
508 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
509 &data->pcap[i], 2);
510 out:
511 mutex_unlock(&data->lock);
514 /* Delete an AEM instance */
515 static void aem_delete(struct aem_data *data)
517 list_del(&data->list);
518 aem_remove_sensors(data);
519 hwmon_device_unregister(data->hwmon_dev);
520 ipmi_destroy_user(data->ipmi.user);
521 dev_set_drvdata(&data->pdev->dev, NULL);
522 platform_device_unregister(data->pdev);
523 aem_idr_put(data->id);
524 kfree(data);
527 /* Probe functions for AEM1 devices */
529 /* Retrieve version and module handle for an AEM1 instance */
530 static int aem_find_aem1_count(struct aem_ipmi_data *data)
532 int res;
533 struct aem_find_firmware_req ff_req;
534 struct aem_find_firmware_resp ff_resp;
536 ff_req.id = system_x_id;
537 ff_req.index = 0;
538 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
540 data->tx_message.cmd = AEM_FIND_FW_CMD;
541 data->tx_message.data = (char *)&ff_req;
542 data->tx_message.data_len = sizeof(ff_req);
544 data->rx_msg_data = &ff_resp;
545 data->rx_msg_len = sizeof(ff_resp);
547 aem_send_message(data);
549 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
550 if (!res)
551 return -ETIMEDOUT;
553 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
554 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
555 return -ENOENT;
557 return ff_resp.num_instances;
560 /* Find and initialize one AEM1 instance */
561 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
563 struct aem_data *data;
564 int i;
565 int res = -ENOMEM;
567 data = kzalloc(sizeof(*data), GFP_KERNEL);
568 if (!data)
569 return res;
570 mutex_init(&data->lock);
572 /* Copy instance data */
573 data->ver_major = 1;
574 data->ver_minor = 0;
575 data->module_handle = module_handle;
576 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
577 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
579 /* Create sub-device for this fw instance */
580 if (aem_idr_get(&data->id))
581 goto id_err;
583 data->pdev = platform_device_alloc(DRVNAME, data->id);
584 if (!data->pdev)
585 goto dev_err;
586 data->pdev->dev.driver = &aem_driver;
588 res = platform_device_add(data->pdev);
589 if (res)
590 goto ipmi_err;
592 dev_set_drvdata(&data->pdev->dev, data);
594 /* Set up IPMI interface */
595 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
596 probe->bmc_device))
597 goto ipmi_err;
599 /* Register with hwmon */
600 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
602 if (IS_ERR(data->hwmon_dev)) {
603 dev_err(&data->pdev->dev, "Unable to register hwmon "
604 "device for IPMI interface %d\n",
605 probe->interface);
606 goto hwmon_reg_err;
609 data->update = update_aem1_sensors;
611 /* Find sensors */
612 if (aem1_find_sensors(data))
613 goto sensor_err;
615 /* Add to our list of AEM devices */
616 list_add_tail(&data->list, &driver_data.aem_devices);
618 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
619 data->ver_major, data->ver_minor,
620 data->module_handle);
621 return 0;
623 sensor_err:
624 hwmon_device_unregister(data->hwmon_dev);
625 hwmon_reg_err:
626 ipmi_destroy_user(data->ipmi.user);
627 ipmi_err:
628 dev_set_drvdata(&data->pdev->dev, NULL);
629 platform_device_unregister(data->pdev);
630 dev_err:
631 aem_idr_put(data->id);
632 id_err:
633 kfree(data);
635 return res;
638 /* Find and initialize all AEM1 instances */
639 static int aem_init_aem1(struct aem_ipmi_data *probe)
641 int num, i, err;
643 num = aem_find_aem1_count(probe);
644 for (i = 0; i < num; i++) {
645 err = aem_init_aem1_inst(probe, i);
646 if (err) {
647 dev_err(probe->bmc_device,
648 "Error %d initializing AEM1 0x%X\n",
649 err, i);
650 return err;
654 return 0;
657 /* Probe functions for AEM2 devices */
659 /* Retrieve version and module handle for an AEM2 instance */
660 static int aem_find_aem2(struct aem_ipmi_data *data,
661 struct aem_find_instance_resp *fi_resp,
662 int instance_num)
664 int res;
665 struct aem_find_instance_req fi_req;
667 fi_req.id = system_x_id;
668 fi_req.instance_number = instance_num;
669 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
671 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
672 data->tx_message.data = (char *)&fi_req;
673 data->tx_message.data_len = sizeof(fi_req);
675 data->rx_msg_data = fi_resp;
676 data->rx_msg_len = sizeof(*fi_resp);
678 aem_send_message(data);
680 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
681 if (!res)
682 return -ETIMEDOUT;
684 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
685 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
686 fi_resp->num_instances <= instance_num)
687 return -ENOENT;
689 return 0;
692 /* Find and initialize one AEM2 instance */
693 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
694 struct aem_find_instance_resp *fi_resp)
696 struct aem_data *data;
697 int i;
698 int res = -ENOMEM;
700 data = kzalloc(sizeof(*data), GFP_KERNEL);
701 if (!data)
702 return res;
703 mutex_init(&data->lock);
705 /* Copy instance data */
706 data->ver_major = fi_resp->major;
707 data->ver_minor = fi_resp->minor;
708 data->module_handle = fi_resp->module_handle;
709 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
710 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
712 /* Create sub-device for this fw instance */
713 if (aem_idr_get(&data->id))
714 goto id_err;
716 data->pdev = platform_device_alloc(DRVNAME, data->id);
717 if (!data->pdev)
718 goto dev_err;
719 data->pdev->dev.driver = &aem_driver;
721 res = platform_device_add(data->pdev);
722 if (res)
723 goto ipmi_err;
725 dev_set_drvdata(&data->pdev->dev, data);
727 /* Set up IPMI interface */
728 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
729 probe->bmc_device))
730 goto ipmi_err;
732 /* Register with hwmon */
733 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
735 if (IS_ERR(data->hwmon_dev)) {
736 dev_err(&data->pdev->dev, "Unable to register hwmon "
737 "device for IPMI interface %d\n",
738 probe->interface);
739 goto hwmon_reg_err;
742 data->update = update_aem2_sensors;
744 /* Find sensors */
745 if (aem2_find_sensors(data))
746 goto sensor_err;
748 /* Add to our list of AEM devices */
749 list_add_tail(&data->list, &driver_data.aem_devices);
751 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
752 data->ver_major, data->ver_minor,
753 data->module_handle);
754 return 0;
756 sensor_err:
757 hwmon_device_unregister(data->hwmon_dev);
758 hwmon_reg_err:
759 ipmi_destroy_user(data->ipmi.user);
760 ipmi_err:
761 dev_set_drvdata(&data->pdev->dev, NULL);
762 platform_device_unregister(data->pdev);
763 dev_err:
764 aem_idr_put(data->id);
765 id_err:
766 kfree(data);
768 return res;
771 /* Find and initialize all AEM2 instances */
772 static int aem_init_aem2(struct aem_ipmi_data *probe)
774 struct aem_find_instance_resp fi_resp;
775 int err;
776 int i = 0;
778 while (!aem_find_aem2(probe, &fi_resp, i)) {
779 if (fi_resp.major != 2) {
780 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
781 "report this to the maintainer.\n",
782 fi_resp.major);
783 i++;
784 continue;
786 err = aem_init_aem2_inst(probe, &fi_resp);
787 if (err) {
788 dev_err(probe->bmc_device,
789 "Error %d initializing AEM2 0x%X\n",
790 err, fi_resp.module_handle);
791 return err;
793 i++;
796 return 0;
799 /* Probe a BMC for AEM firmware instances */
800 static void aem_register_bmc(int iface, struct device *dev)
802 struct aem_ipmi_data probe;
804 if (aem_init_ipmi_data(&probe, iface, dev))
805 return;
807 /* Ignore probe errors; they won't cause problems */
808 aem_init_aem1(&probe);
809 aem_init_aem2(&probe);
811 ipmi_destroy_user(probe.user);
814 /* Handle BMC deletion */
815 static void aem_bmc_gone(int iface)
817 struct aem_data *p1, *next1;
819 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
820 if (p1->ipmi.interface == iface)
821 aem_delete(p1);
824 /* sysfs support functions */
826 /* AEM device name */
827 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
828 char *buf)
830 struct aem_data *data = dev_get_drvdata(dev);
832 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
834 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
836 /* AEM device version */
837 static ssize_t show_version(struct device *dev,
838 struct device_attribute *devattr,
839 char *buf)
841 struct aem_data *data = dev_get_drvdata(dev);
843 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
845 static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
847 /* Display power use */
848 static ssize_t aem_show_power(struct device *dev,
849 struct device_attribute *devattr,
850 char *buf)
852 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
853 struct aem_data *data = dev_get_drvdata(dev);
854 u64 before, after, delta, time;
855 signed long leftover;
856 struct timespec b, a;
858 mutex_lock(&data->lock);
859 update_aem_energy_one(data, attr->index);
860 getnstimeofday(&b);
861 before = data->energy[attr->index];
863 leftover = schedule_timeout_interruptible(
864 msecs_to_jiffies(data->power_period[attr->index])
866 if (leftover) {
867 mutex_unlock(&data->lock);
868 return 0;
871 update_aem_energy_one(data, attr->index);
872 getnstimeofday(&a);
873 after = data->energy[attr->index];
874 mutex_unlock(&data->lock);
876 time = timespec_to_ns(&a) - timespec_to_ns(&b);
877 delta = (after - before) * UJ_PER_MJ;
879 return sprintf(buf, "%llu\n",
880 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
883 /* Display energy use */
884 static ssize_t aem_show_energy(struct device *dev,
885 struct device_attribute *devattr,
886 char *buf)
888 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
889 struct aem_data *a = dev_get_drvdata(dev);
890 mutex_lock(&a->lock);
891 update_aem_energy_one(a, attr->index);
892 mutex_unlock(&a->lock);
894 return sprintf(buf, "%llu\n",
895 (unsigned long long)a->energy[attr->index] * 1000);
898 /* Display power interval registers */
899 static ssize_t aem_show_power_period(struct device *dev,
900 struct device_attribute *devattr,
901 char *buf)
903 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
904 struct aem_data *a = dev_get_drvdata(dev);
905 a->update(a);
907 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
910 /* Set power interval registers */
911 static ssize_t aem_set_power_period(struct device *dev,
912 struct device_attribute *devattr,
913 const char *buf, size_t count)
915 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
916 struct aem_data *a = dev_get_drvdata(dev);
917 unsigned long temp;
918 int res;
920 res = strict_strtoul(buf, 10, &temp);
921 if (res)
922 return res;
924 if (temp < AEM_MIN_POWER_INTERVAL)
925 return -EINVAL;
927 mutex_lock(&a->lock);
928 a->power_period[attr->index] = temp;
929 mutex_unlock(&a->lock);
931 return count;
934 /* Discover sensors on an AEM device */
935 static int aem_register_sensors(struct aem_data *data,
936 struct aem_ro_sensor_template *ro,
937 struct aem_rw_sensor_template *rw)
939 struct device *dev = &data->pdev->dev;
940 struct sensor_device_attribute *sensors = data->sensors;
941 int err;
943 /* Set up read-only sensors */
944 while (ro->label) {
945 sensors->dev_attr.attr.name = ro->label;
946 sensors->dev_attr.attr.mode = S_IRUGO;
947 sensors->dev_attr.show = ro->show;
948 sensors->index = ro->index;
950 err = device_create_file(dev, &sensors->dev_attr);
951 if (err) {
952 sensors->dev_attr.attr.name = NULL;
953 goto error;
955 sensors++;
956 ro++;
959 /* Set up read-write sensors */
960 while (rw->label) {
961 sensors->dev_attr.attr.name = rw->label;
962 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
963 sensors->dev_attr.show = rw->show;
964 sensors->dev_attr.store = rw->set;
965 sensors->index = rw->index;
967 err = device_create_file(dev, &sensors->dev_attr);
968 if (err) {
969 sensors->dev_attr.attr.name = NULL;
970 goto error;
972 sensors++;
973 rw++;
976 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
977 if (err)
978 goto error;
979 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
980 return err;
982 error:
983 aem_remove_sensors(data);
984 return err;
987 /* sysfs support functions for AEM2 sensors */
989 /* Display temperature use */
990 static ssize_t aem2_show_temp(struct device *dev,
991 struct device_attribute *devattr,
992 char *buf)
994 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
995 struct aem_data *a = dev_get_drvdata(dev);
996 a->update(a);
998 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1001 /* Display power-capping registers */
1002 static ssize_t aem2_show_pcap_value(struct device *dev,
1003 struct device_attribute *devattr,
1004 char *buf)
1006 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1007 struct aem_data *a = dev_get_drvdata(dev);
1008 a->update(a);
1010 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1013 /* Remove sensors attached to an AEM device */
1014 static void aem_remove_sensors(struct aem_data *data)
1016 int i;
1018 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1019 if (!data->sensors[i].dev_attr.attr.name)
1020 continue;
1021 device_remove_file(&data->pdev->dev,
1022 &data->sensors[i].dev_attr);
1025 device_remove_file(&data->pdev->dev,
1026 &sensor_dev_attr_name.dev_attr);
1027 device_remove_file(&data->pdev->dev,
1028 &sensor_dev_attr_version.dev_attr);
1031 /* Sensor probe functions */
1033 /* Description of AEM1 sensors */
1034 static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1035 {"energy1_input", aem_show_energy, 0},
1036 {"power1_average", aem_show_power, 0},
1037 {NULL, NULL, 0},
1040 static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1041 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1042 {NULL, NULL, NULL, 0},
1045 /* Description of AEM2 sensors */
1046 static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1047 {"energy1_input", aem_show_energy, 0},
1048 {"energy2_input", aem_show_energy, 1},
1049 {"power1_average", aem_show_power, 0},
1050 {"power2_average", aem_show_power, 1},
1051 {"temp1_input", aem2_show_temp, 0},
1052 {"temp2_input", aem2_show_temp, 1},
1054 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1055 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1056 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1057 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1059 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1060 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1061 {NULL, NULL, 0},
1064 static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1065 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1066 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1067 {NULL, NULL, NULL, 0},
1070 /* Set up AEM1 sensor attrs */
1071 static int aem1_find_sensors(struct aem_data *data)
1073 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1076 /* Set up AEM2 sensor attrs */
1077 static int aem2_find_sensors(struct aem_data *data)
1079 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1082 /* Module init/exit routines */
1084 static int __init aem_init(void)
1086 int res;
1088 res = driver_register(&aem_driver);
1089 if (res) {
1090 printk(KERN_ERR "Can't register aem driver\n");
1091 return res;
1094 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1095 if (res)
1096 goto ipmi_reg_err;
1097 return 0;
1099 ipmi_reg_err:
1100 driver_unregister(&aem_driver);
1101 return res;
1105 static void __exit aem_exit(void)
1107 struct aem_data *p1, *next1;
1109 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1110 driver_unregister(&aem_driver);
1111 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1112 aem_delete(p1);
1115 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1116 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1117 MODULE_LICENSE("GPL");
1119 module_init(aem_init);
1120 module_exit(aem_exit);