blob: eaee546af19a1c519758783ee66217a1d8bae46d [file] [log] [blame]
Darrick J. Wong8808a792008-05-23 13:04:25 -07001/*
Darrick J. Wongeb93b7d2008-08-15 00:40:40 -07002 * A hwmon driver for the IBM System Director Active Energy Manager (AEM)
3 * temperature/power/energy sensors and capping functionality.
Darrick J. Wong8808a792008-05-23 13:04:25 -07004 * Copyright (C) 2008 IBM
5 *
6 * Author: Darrick J. Wong <djwong@us.ibm.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Darrick J. Wong8808a792008-05-23 13:04:25 -070033#include <linux/sched.h>
34#include <linux/platform_device.h>
35#include <linux/math64.h>
36#include <linux/time.h>
37
38#define REFRESH_INTERVAL (HZ)
39#define IPMI_TIMEOUT (30 * HZ)
40#define DRVNAME "aem"
41
42#define AEM_NETFN 0x2E
43
44#define AEM_FIND_FW_CMD 0x80
45#define AEM_ELEMENT_CMD 0x81
46#define AEM_FW_INSTANCE_CMD 0x82
47
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
55
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
62
63#define AEM_MODULE_TYPE_ID 0x0001
64
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
69
70#define AEM1_NUM_ENERGY_REGS 1
71#define AEM1_NUM_SENSORS 3
72
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
77
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
84
85#define AEM_DEFAULT_POWER_INTERVAL 1000
86#define AEM_MIN_POWER_INTERVAL 200
87#define UJ_PER_MJ 1000L
88
89static DEFINE_IDR(aem_idr);
90static DEFINE_SPINLOCK(aem_idr_lock);
91
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -080092static struct platform_driver aem_driver = {
93 .driver = {
94 .name = DRVNAME,
95 .bus = &platform_bus_type,
96 }
Darrick J. Wong8808a792008-05-23 13:04:25 -070097};
98
99struct aem_ipmi_data {
100 struct completion read_complete;
101 struct ipmi_addr address;
102 ipmi_user_t user;
103 int interface;
104
105 struct kernel_ipmi_msg tx_message;
106 long tx_msgid;
107
108 void *rx_msg_data;
109 unsigned short rx_msg_len;
110 unsigned char rx_result;
111 int rx_recv_type;
112
113 struct device *bmc_device;
114};
115
116struct 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;
122};
123
124struct 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;
133};
134
135struct aem_data {
136 struct list_head list;
137
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;
148
149 /* Function to update sensors */
150 void (*update)(struct aem_data *data);
151
152 /*
153 * AEM 1.x sensors:
154 * Available sensors:
155 * Energy meter
156 * Power meter
157 *
158 * AEM 2.x sensors:
159 * Two energy meters
160 * Two power meters
161 * Two temperature sensors
162 * Six power cap registers
163 */
164
165 /* sysfs attrs */
166 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
167
168 /* energy use in mJ */
169 u64 energy[AEM_NUM_ENERGY_REGS];
170
171 /* power sampling interval in ms */
172 unsigned long power_period[AEM_NUM_ENERGY_REGS];
173
174 /* Everything past here is for AEM2 only */
175
176 /* power caps in dW */
177 u16 pcap[AEM2_NUM_PCAP_REGS];
178
179 /* exhaust temperature in C */
180 u8 temp[AEM2_NUM_TEMP_REGS];
181};
182
183/* Data structures returned by the AEM firmware */
184struct aem_iana_id {
185 u8 bytes[3];
186};
187static struct aem_iana_id system_x_id = {
188 .bytes = {0x4D, 0x4F, 0x00}
189};
190
191/* These are used to find AEM1 instances */
192struct aem_find_firmware_req {
193 struct aem_iana_id id;
194 u8 rsvd;
Al Viroa064d5b2008-06-02 10:59:02 +0100195 __be16 index;
196 __be16 module_type_id;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700197} __packed;
198
199struct aem_find_firmware_resp {
200 struct aem_iana_id id;
201 u8 num_instances;
202} __packed;
203
204/* These are used to find AEM2 instances */
205struct aem_find_instance_req {
206 struct aem_iana_id id;
207 u8 instance_number;
Al Viroa064d5b2008-06-02 10:59:02 +0100208 __be16 module_type_id;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700209} __packed;
210
211struct 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;
219
220/* These are used to query sensors */
221struct 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;
229
230struct aem_read_sensor_resp {
231 struct aem_iana_id id;
232 u8 bytes[0];
233} __packed;
234
235/* Data structures to talk to the IPMI layer */
236struct aem_driver_data {
237 struct list_head aem_devices;
238 struct ipmi_smi_watcher bmc_events;
239 struct ipmi_user_hndl ipmi_hndlrs;
240};
241
242static void aem_register_bmc(int iface, struct device *dev);
243static void aem_bmc_gone(int iface);
244static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
245
246static void aem_remove_sensors(struct aem_data *data);
247static int aem_init_aem1(struct aem_ipmi_data *probe);
248static int aem_init_aem2(struct aem_ipmi_data *probe);
249static int aem1_find_sensors(struct aem_data *data);
250static int aem2_find_sensors(struct aem_data *data);
251static void update_aem1_sensors(struct aem_data *data);
252static void update_aem2_sensors(struct aem_data *data);
253
254static 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,
260 },
261 .ipmi_hndlrs = {
262 .ipmi_recv_hndl = aem_msg_handler,
263 },
264};
265
266/* Functions to talk to the IPMI layer */
267
268/* Initialize IPMI address, message buffers and user data */
269static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
270 struct device *bmc)
271{
272 int err;
273
274 init_completion(&data->read_complete);
275 data->bmc_device = bmc;
276
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;
282
283 /* Initialize message buffers */
284 data->tx_msgid = 0;
285 data->tx_message.netfn = AEM_NETFN;
286
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;
294 }
295
296 return 0;
297}
298
299/* Send an IPMI command */
300static int aem_send_message(struct aem_ipmi_data *data)
301{
302 int err;
303
304 err = ipmi_validate_addr(&data->address, sizeof(data->address));
305 if (err)
306 goto out;
307
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;
313
314 return 0;
315out1:
316 dev_err(data->bmc_device, "request_settime=%x\n", err);
317 return err;
318out:
319 dev_err(data->bmc_device, "validate_addr=%x\n", err);
320 return err;
321}
322
323/* Dispatch IPMI messages to callers */
324static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
325{
326 unsigned short rx_len;
327 struct aem_ipmi_data *data = user_msg_data;
328
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;
336 }
337
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;
343
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;
352
353 ipmi_free_recv_msg(msg);
354 complete(&data->read_complete);
355}
356
357/* ID functions */
358
359/* Obtain an id */
360static int aem_idr_get(int *id)
361{
362 int i, err;
363
364again:
365 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
366 return -ENOMEM;
367
368 spin_lock(&aem_idr_lock);
369 err = idr_get_new(&aem_idr, NULL, &i);
370 spin_unlock(&aem_idr_lock);
371
372 if (unlikely(err == -EAGAIN))
373 goto again;
374 else if (unlikely(err))
375 return err;
376
377 *id = i & MAX_ID_MASK;
378 return 0;
379}
380
381/* Release an object ID */
382static void aem_idr_put(int id)
383{
384 spin_lock(&aem_idr_lock);
385 idr_remove(&aem_idr, id);
386 spin_unlock(&aem_idr_lock);
387}
388
389/* Sensor support functions */
390
391/* Read a sensor value */
392static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
393 void *buf, size_t size)
394{
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;
399
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;
409 }
410
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;
417
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);
421
422 rs_size = sizeof(*rs_resp) + size;
423 rs_resp = kzalloc(rs_size, GFP_KERNEL);
424 if (!rs_resp)
425 return -ENOMEM;
426
427 ipmi->rx_msg_data = rs_resp;
428 ipmi->rx_msg_len = rs_size;
429
430 aem_send_message(ipmi);
431
432 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
433 if (!res)
434 return -ETIMEDOUT;
435
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;
440 }
441
442 switch (size) {
443 case 1: {
444 u8 *x = buf;
445 *x = rs_resp->bytes[0];
446 break;
447 }
448 case 2: {
449 u16 *x = buf;
Al Viroa064d5b2008-06-02 10:59:02 +0100450 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700451 break;
452 }
453 case 4: {
454 u32 *x = buf;
Al Viroa064d5b2008-06-02 10:59:02 +0100455 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700456 break;
457 }
458 case 8: {
459 u64 *x = buf;
Al Viroa064d5b2008-06-02 10:59:02 +0100460 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700461 break;
462 }
463 }
464
465 return 0;
466}
467
468/* Update AEM energy registers */
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700469static void update_aem_energy_one(struct aem_data *data, int which)
470{
471 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
472 &data->energy[which], 8);
473}
474
Darrick J. Wong8808a792008-05-23 13:04:25 -0700475static void update_aem_energy(struct aem_data *data)
476{
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700477 update_aem_energy_one(data, 0);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700478 if (data->ver_major < 2)
479 return;
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700480 update_aem_energy_one(data, 1);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700481}
482
483/* Update all AEM1 sensors */
484static void update_aem1_sensors(struct aem_data *data)
485{
486 mutex_lock(&data->lock);
487 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
488 data->valid)
489 goto out;
490
491 update_aem_energy(data);
492out:
493 mutex_unlock(&data->lock);
494}
495
496/* Update all AEM2 sensors */
497static void update_aem2_sensors(struct aem_data *data)
498{
499 int i;
500
501 mutex_lock(&data->lock);
502 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
503 data->valid)
504 goto out;
505
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);
509
510 for (i = POWER_CAP; i <= POWER_AUX; i++)
511 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
512 &data->pcap[i], 2);
513out:
514 mutex_unlock(&data->lock);
515}
516
517/* Delete an AEM instance */
518static void aem_delete(struct aem_data *data)
519{
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);
528}
529
530/* Probe functions for AEM1 devices */
531
532/* Retrieve version and module handle for an AEM1 instance */
533static int aem_find_aem1_count(struct aem_ipmi_data *data)
534{
535 int res;
536 struct aem_find_firmware_req ff_req;
537 struct aem_find_firmware_resp ff_resp;
538
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);
542
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);
546
547 data->rx_msg_data = &ff_resp;
548 data->rx_msg_len = sizeof(ff_resp);
549
550 aem_send_message(data);
551
552 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
553 if (!res)
554 return -ETIMEDOUT;
555
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;
559
560 return ff_resp.num_instances;
561}
562
563/* Find and initialize one AEM1 instance */
564static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
565{
566 struct aem_data *data;
567 int i;
568 int res = -ENOMEM;
569
570 data = kzalloc(sizeof(*data), GFP_KERNEL);
571 if (!data)
572 return res;
573 mutex_init(&data->lock);
574
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;
581
582 /* Create sub-device for this fw instance */
583 if (aem_idr_get(&data->id))
584 goto id_err;
585
586 data->pdev = platform_device_alloc(DRVNAME, data->id);
587 if (!data->pdev)
588 goto dev_err;
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800589 data->pdev->dev.driver = &aem_driver.driver;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700590
591 res = platform_device_add(data->pdev);
592 if (res)
593 goto ipmi_err;
594
595 dev_set_drvdata(&data->pdev->dev, data);
596
597 /* Set up IPMI interface */
598 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
599 probe->bmc_device))
600 goto ipmi_err;
601
602 /* Register with hwmon */
603 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
604
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;
610 }
611
612 data->update = update_aem1_sensors;
613
614 /* Find sensors */
615 if (aem1_find_sensors(data))
616 goto sensor_err;
617
618 /* Add to our list of AEM devices */
619 list_add_tail(&data->list, &driver_data.aem_devices);
620
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;
625
626sensor_err:
627 hwmon_device_unregister(data->hwmon_dev);
628hwmon_reg_err:
629 ipmi_destroy_user(data->ipmi.user);
630ipmi_err:
631 dev_set_drvdata(&data->pdev->dev, NULL);
632 platform_device_unregister(data->pdev);
633dev_err:
634 aem_idr_put(data->id);
635id_err:
636 kfree(data);
637
638 return res;
639}
640
641/* Find and initialize all AEM1 instances */
642static int aem_init_aem1(struct aem_ipmi_data *probe)
643{
644 int num, i, err;
645
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;
654 }
655 }
656
657 return 0;
658}
659
660/* Probe functions for AEM2 devices */
661
662/* Retrieve version and module handle for an AEM2 instance */
663static int aem_find_aem2(struct aem_ipmi_data *data,
664 struct aem_find_instance_resp *fi_resp,
665 int instance_num)
666{
667 int res;
668 struct aem_find_instance_req fi_req;
669
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);
673
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);
677
678 data->rx_msg_data = fi_resp;
679 data->rx_msg_len = sizeof(*fi_resp);
680
681 aem_send_message(data);
682
683 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
684 if (!res)
685 return -ETIMEDOUT;
686
687 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
Darrick J. Wong9c5413e2008-08-15 00:40:40 -0700688 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
689 fi_resp->num_instances <= instance_num)
Darrick J. Wong8808a792008-05-23 13:04:25 -0700690 return -ENOENT;
691
692 return 0;
693}
694
695/* Find and initialize one AEM2 instance */
696static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
697 struct aem_find_instance_resp *fi_resp)
698{
699 struct aem_data *data;
700 int i;
701 int res = -ENOMEM;
702
703 data = kzalloc(sizeof(*data), GFP_KERNEL);
704 if (!data)
705 return res;
706 mutex_init(&data->lock);
707
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;
714
715 /* Create sub-device for this fw instance */
716 if (aem_idr_get(&data->id))
717 goto id_err;
718
719 data->pdev = platform_device_alloc(DRVNAME, data->id);
720 if (!data->pdev)
721 goto dev_err;
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800722 data->pdev->dev.driver = &aem_driver.driver;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700723
724 res = platform_device_add(data->pdev);
725 if (res)
726 goto ipmi_err;
727
728 dev_set_drvdata(&data->pdev->dev, data);
729
730 /* Set up IPMI interface */
731 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
732 probe->bmc_device))
733 goto ipmi_err;
734
735 /* Register with hwmon */
736 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
737
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;
743 }
744
745 data->update = update_aem2_sensors;
746
747 /* Find sensors */
748 if (aem2_find_sensors(data))
749 goto sensor_err;
750
751 /* Add to our list of AEM devices */
752 list_add_tail(&data->list, &driver_data.aem_devices);
753
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;
758
759sensor_err:
760 hwmon_device_unregister(data->hwmon_dev);
761hwmon_reg_err:
762 ipmi_destroy_user(data->ipmi.user);
763ipmi_err:
764 dev_set_drvdata(&data->pdev->dev, NULL);
765 platform_device_unregister(data->pdev);
766dev_err:
767 aem_idr_put(data->id);
768id_err:
769 kfree(data);
770
771 return res;
772}
773
774/* Find and initialize all AEM2 instances */
775static int aem_init_aem2(struct aem_ipmi_data *probe)
776{
777 struct aem_find_instance_resp fi_resp;
778 int err;
779 int i = 0;
780
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;
788 }
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;
795 }
796 i++;
797 }
798
799 return 0;
800}
801
802/* Probe a BMC for AEM firmware instances */
803static void aem_register_bmc(int iface, struct device *dev)
804{
805 struct aem_ipmi_data probe;
806
807 if (aem_init_ipmi_data(&probe, iface, dev))
808 return;
809
810 /* Ignore probe errors; they won't cause problems */
811 aem_init_aem1(&probe);
812 aem_init_aem2(&probe);
813
814 ipmi_destroy_user(probe.user);
815}
816
817/* Handle BMC deletion */
818static void aem_bmc_gone(int iface)
819{
820 struct aem_data *p1, *next1;
821
822 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
823 if (p1->ipmi.interface == iface)
824 aem_delete(p1);
825}
826
827/* sysfs support functions */
828
829/* AEM device name */
830static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
831 char *buf)
832{
833 struct aem_data *data = dev_get_drvdata(dev);
834
835 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
836}
837static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
838
839/* AEM device version */
840static ssize_t show_version(struct device *dev,
841 struct device_attribute *devattr,
842 char *buf)
843{
844 struct aem_data *data = dev_get_drvdata(dev);
845
846 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
847}
848static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
849
850/* Display power use */
851static ssize_t aem_show_power(struct device *dev,
852 struct device_attribute *devattr,
853 char *buf)
854{
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;
860
861 mutex_lock(&data->lock);
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700862 update_aem_energy_one(data, attr->index);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700863 getnstimeofday(&b);
864 before = data->energy[attr->index];
865
866 leftover = schedule_timeout_interruptible(
867 msecs_to_jiffies(data->power_period[attr->index])
868 );
869 if (leftover) {
870 mutex_unlock(&data->lock);
871 return 0;
872 }
873
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700874 update_aem_energy_one(data, attr->index);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700875 getnstimeofday(&a);
876 after = data->energy[attr->index];
877 mutex_unlock(&data->lock);
878
879 time = timespec_to_ns(&a) - timespec_to_ns(&b);
880 delta = (after - before) * UJ_PER_MJ;
881
882 return sprintf(buf, "%llu\n",
883 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
884}
885
886/* Display energy use */
887static ssize_t aem_show_energy(struct device *dev,
888 struct device_attribute *devattr,
889 char *buf)
890{
891 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
892 struct aem_data *a = dev_get_drvdata(dev);
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700893 mutex_lock(&a->lock);
894 update_aem_energy_one(a, attr->index);
895 mutex_unlock(&a->lock);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700896
897 return sprintf(buf, "%llu\n",
898 (unsigned long long)a->energy[attr->index] * 1000);
899}
900
901/* Display power interval registers */
902static ssize_t aem_show_power_period(struct device *dev,
903 struct device_attribute *devattr,
904 char *buf)
905{
906 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 struct aem_data *a = dev_get_drvdata(dev);
908 a->update(a);
909
910 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
911}
912
913/* Set power interval registers */
914static ssize_t aem_set_power_period(struct device *dev,
915 struct device_attribute *devattr,
916 const char *buf, size_t count)
917{
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;
922
923 res = strict_strtoul(buf, 10, &temp);
924 if (res)
925 return res;
926
927 if (temp < AEM_MIN_POWER_INTERVAL)
928 return -EINVAL;
929
930 mutex_lock(&a->lock);
931 a->power_period[attr->index] = temp;
932 mutex_unlock(&a->lock);
933
934 return count;
935}
936
937/* Discover sensors on an AEM device */
938static int aem_register_sensors(struct aem_data *data,
939 struct aem_ro_sensor_template *ro,
940 struct aem_rw_sensor_template *rw)
941{
942 struct device *dev = &data->pdev->dev;
943 struct sensor_device_attribute *sensors = data->sensors;
944 int err;
945
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;
952
953 err = device_create_file(dev, &sensors->dev_attr);
954 if (err) {
955 sensors->dev_attr.attr.name = NULL;
956 goto error;
957 }
958 sensors++;
959 ro++;
960 }
961
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;
969
970 err = device_create_file(dev, &sensors->dev_attr);
971 if (err) {
972 sensors->dev_attr.attr.name = NULL;
973 goto error;
974 }
975 sensors++;
976 rw++;
977 }
978
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;
984
985error:
986 aem_remove_sensors(data);
987 return err;
988}
989
990/* sysfs support functions for AEM2 sensors */
991
992/* Display temperature use */
993static ssize_t aem2_show_temp(struct device *dev,
994 struct device_attribute *devattr,
995 char *buf)
996{
997 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
998 struct aem_data *a = dev_get_drvdata(dev);
999 a->update(a);
1000
1001 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1002}
1003
1004/* Display power-capping registers */
1005static ssize_t aem2_show_pcap_value(struct device *dev,
1006 struct device_attribute *devattr,
1007 char *buf)
1008{
1009 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1010 struct aem_data *a = dev_get_drvdata(dev);
1011 a->update(a);
1012
1013 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1014}
1015
1016/* Remove sensors attached to an AEM device */
1017static void aem_remove_sensors(struct aem_data *data)
1018{
1019 int i;
1020
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);
1026 }
1027
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);
1032}
1033
1034/* Sensor probe functions */
1035
1036/* Description of AEM1 sensors */
1037static 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},
1041};
1042
1043static 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},
1046};
1047
1048/* Description of AEM2 sensors */
1049static 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},
1056
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},
1061
1062{"power3_average", aem2_show_pcap_value, POWER_AUX},
1063{"power_cap", aem2_show_pcap_value, POWER_CAP},
1064{NULL, NULL, 0},
1065};
1066
1067static 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},
1071};
1072
1073/* Set up AEM1 sensor attrs */
1074static int aem1_find_sensors(struct aem_data *data)
1075{
1076 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1077}
1078
1079/* Set up AEM2 sensor attrs */
1080static int aem2_find_sensors(struct aem_data *data)
1081{
1082 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1083}
1084
1085/* Module init/exit routines */
1086
1087static int __init aem_init(void)
1088{
1089 int res;
1090
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08001091 res = driver_register(&aem_driver.driver);
Darrick J. Wong8808a792008-05-23 13:04:25 -07001092 if (res) {
1093 printk(KERN_ERR "Can't register aem driver\n");
1094 return res;
1095 }
1096
1097 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1098 if (res)
1099 goto ipmi_reg_err;
1100 return 0;
1101
1102ipmi_reg_err:
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08001103 driver_unregister(&aem_driver.driver);
Darrick J. Wong8808a792008-05-23 13:04:25 -07001104 return res;
1105
1106}
1107
1108static void __exit aem_exit(void)
1109{
1110 struct aem_data *p1, *next1;
1111
1112 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08001113 driver_unregister(&aem_driver.driver);
Darrick J. Wong8808a792008-05-23 13:04:25 -07001114 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1115 aem_delete(p1);
1116}
1117
1118MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
Darrick J. Wongeb93b7d2008-08-15 00:40:40 -07001119MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
Darrick J. Wong8808a792008-05-23 13:04:25 -07001120MODULE_LICENSE("GPL");
1121
1122module_init(aem_init);
1123module_exit(aem_exit);
Darrick J. Wong21d93c12008-10-17 17:51:19 +02001124
1125MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1126MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1127MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1128MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1129MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1130MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
Darrick J. Wong80704082009-06-15 18:39:46 +02001131MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");