blob: c316294c48b47ba848f5844270e0394d13e2be87 [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
Joe Perches55d705c2010-10-20 06:51:38 +000023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Darrick J. Wong8808a792008-05-23 13:04:25 -070025#include <linux/ipmi.h>
26#include <linux/module.h>
27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
29#include <linux/jiffies.h>
30#include <linux/mutex.h>
31#include <linux/kdev_t.h>
32#include <linux/spinlock.h>
33#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Darrick J. Wong8808a792008-05-23 13:04:25 -070035#include <linux/sched.h>
36#include <linux/platform_device.h>
37#include <linux/math64.h>
38#include <linux/time.h>
39
40#define REFRESH_INTERVAL (HZ)
41#define IPMI_TIMEOUT (30 * HZ)
42#define DRVNAME "aem"
43
44#define AEM_NETFN 0x2E
45
46#define AEM_FIND_FW_CMD 0x80
47#define AEM_ELEMENT_CMD 0x81
48#define AEM_FW_INSTANCE_CMD 0x82
49
50#define AEM_READ_ELEMENT_CFG 0x80
51#define AEM_READ_BUFFER 0x81
52#define AEM_READ_REGISTER 0x82
53#define AEM_WRITE_REGISTER 0x83
54#define AEM_SET_REG_MASK 0x84
55#define AEM_CLEAR_REG_MASK 0x85
56#define AEM_READ_ELEMENT_CFG2 0x86
57
58#define AEM_CONTROL_ELEMENT 0
59#define AEM_ENERGY_ELEMENT 1
60#define AEM_CLOCK_ELEMENT 4
61#define AEM_POWER_CAP_ELEMENT 7
62#define AEM_EXHAUST_ELEMENT 9
63#define AEM_POWER_ELEMENT 10
64
65#define AEM_MODULE_TYPE_ID 0x0001
66
67#define AEM2_NUM_ENERGY_REGS 2
68#define AEM2_NUM_PCAP_REGS 6
69#define AEM2_NUM_TEMP_REGS 2
70#define AEM2_NUM_SENSORS 14
71
72#define AEM1_NUM_ENERGY_REGS 1
73#define AEM1_NUM_SENSORS 3
74
75/* AEM 2.x has more energy registers */
76#define AEM_NUM_ENERGY_REGS AEM2_NUM_ENERGY_REGS
77/* AEM 2.x needs more sensor files */
78#define AEM_NUM_SENSORS AEM2_NUM_SENSORS
79
80#define POWER_CAP 0
81#define POWER_CAP_MAX_HOTPLUG 1
82#define POWER_CAP_MAX 2
83#define POWER_CAP_MIN_WARNING 3
84#define POWER_CAP_MIN 4
85#define POWER_AUX 5
86
87#define AEM_DEFAULT_POWER_INTERVAL 1000
88#define AEM_MIN_POWER_INTERVAL 200
89#define UJ_PER_MJ 1000L
90
91static DEFINE_IDR(aem_idr);
92static DEFINE_SPINLOCK(aem_idr_lock);
93
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -080094static struct platform_driver aem_driver = {
95 .driver = {
96 .name = DRVNAME,
97 .bus = &platform_bus_type,
98 }
Darrick J. Wong8808a792008-05-23 13:04:25 -070099};
100
101struct aem_ipmi_data {
102 struct completion read_complete;
103 struct ipmi_addr address;
104 ipmi_user_t user;
105 int interface;
106
107 struct kernel_ipmi_msg tx_message;
108 long tx_msgid;
109
110 void *rx_msg_data;
111 unsigned short rx_msg_len;
112 unsigned char rx_result;
113 int rx_recv_type;
114
115 struct device *bmc_device;
116};
117
118struct aem_ro_sensor_template {
119 char *label;
120 ssize_t (*show)(struct device *dev,
121 struct device_attribute *devattr,
122 char *buf);
123 int index;
124};
125
126struct aem_rw_sensor_template {
127 char *label;
128 ssize_t (*show)(struct device *dev,
129 struct device_attribute *devattr,
130 char *buf);
131 ssize_t (*set)(struct device *dev,
132 struct device_attribute *devattr,
133 const char *buf, size_t count);
134 int index;
135};
136
137struct aem_data {
138 struct list_head list;
139
140 struct device *hwmon_dev;
141 struct platform_device *pdev;
142 struct mutex lock;
143 char valid;
144 unsigned long last_updated; /* In jiffies */
145 u8 ver_major;
146 u8 ver_minor;
147 u8 module_handle;
148 int id;
149 struct aem_ipmi_data ipmi;
150
151 /* Function to update sensors */
152 void (*update)(struct aem_data *data);
153
154 /*
155 * AEM 1.x sensors:
156 * Available sensors:
157 * Energy meter
158 * Power meter
159 *
160 * AEM 2.x sensors:
161 * Two energy meters
162 * Two power meters
163 * Two temperature sensors
164 * Six power cap registers
165 */
166
167 /* sysfs attrs */
168 struct sensor_device_attribute sensors[AEM_NUM_SENSORS];
169
170 /* energy use in mJ */
171 u64 energy[AEM_NUM_ENERGY_REGS];
172
173 /* power sampling interval in ms */
174 unsigned long power_period[AEM_NUM_ENERGY_REGS];
175
176 /* Everything past here is for AEM2 only */
177
178 /* power caps in dW */
179 u16 pcap[AEM2_NUM_PCAP_REGS];
180
181 /* exhaust temperature in C */
182 u8 temp[AEM2_NUM_TEMP_REGS];
183};
184
185/* Data structures returned by the AEM firmware */
186struct aem_iana_id {
187 u8 bytes[3];
188};
189static struct aem_iana_id system_x_id = {
190 .bytes = {0x4D, 0x4F, 0x00}
191};
192
193/* These are used to find AEM1 instances */
194struct aem_find_firmware_req {
195 struct aem_iana_id id;
196 u8 rsvd;
Al Viroa064d5b2008-06-02 10:59:02 +0100197 __be16 index;
198 __be16 module_type_id;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700199} __packed;
200
201struct aem_find_firmware_resp {
202 struct aem_iana_id id;
203 u8 num_instances;
204} __packed;
205
206/* These are used to find AEM2 instances */
207struct aem_find_instance_req {
208 struct aem_iana_id id;
209 u8 instance_number;
Al Viroa064d5b2008-06-02 10:59:02 +0100210 __be16 module_type_id;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700211} __packed;
212
213struct aem_find_instance_resp {
214 struct aem_iana_id id;
215 u8 num_instances;
216 u8 major;
217 u8 minor;
218 u8 module_handle;
219 u16 record_id;
220} __packed;
221
222/* These are used to query sensors */
223struct aem_read_sensor_req {
224 struct aem_iana_id id;
225 u8 module_handle;
226 u8 element;
227 u8 subcommand;
228 u8 reg;
229 u8 rx_buf_size;
230} __packed;
231
232struct aem_read_sensor_resp {
233 struct aem_iana_id id;
234 u8 bytes[0];
235} __packed;
236
237/* Data structures to talk to the IPMI layer */
238struct aem_driver_data {
239 struct list_head aem_devices;
240 struct ipmi_smi_watcher bmc_events;
241 struct ipmi_user_hndl ipmi_hndlrs;
242};
243
244static void aem_register_bmc(int iface, struct device *dev);
245static void aem_bmc_gone(int iface);
246static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
247
248static void aem_remove_sensors(struct aem_data *data);
249static int aem_init_aem1(struct aem_ipmi_data *probe);
250static int aem_init_aem2(struct aem_ipmi_data *probe);
251static int aem1_find_sensors(struct aem_data *data);
252static int aem2_find_sensors(struct aem_data *data);
253static void update_aem1_sensors(struct aem_data *data);
254static void update_aem2_sensors(struct aem_data *data);
255
256static struct aem_driver_data driver_data = {
257 .aem_devices = LIST_HEAD_INIT(driver_data.aem_devices),
258 .bmc_events = {
259 .owner = THIS_MODULE,
260 .new_smi = aem_register_bmc,
261 .smi_gone = aem_bmc_gone,
262 },
263 .ipmi_hndlrs = {
264 .ipmi_recv_hndl = aem_msg_handler,
265 },
266};
267
268/* Functions to talk to the IPMI layer */
269
270/* Initialize IPMI address, message buffers and user data */
271static int aem_init_ipmi_data(struct aem_ipmi_data *data, int iface,
272 struct device *bmc)
273{
274 int err;
275
276 init_completion(&data->read_complete);
277 data->bmc_device = bmc;
278
279 /* Initialize IPMI address */
280 data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
281 data->address.channel = IPMI_BMC_CHANNEL;
282 data->address.data[0] = 0;
283 data->interface = iface;
284
285 /* Initialize message buffers */
286 data->tx_msgid = 0;
287 data->tx_message.netfn = AEM_NETFN;
288
289 /* Create IPMI messaging interface user */
290 err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
291 data, &data->user);
292 if (err < 0) {
293 dev_err(bmc, "Unable to register user with IPMI "
294 "interface %d\n", data->interface);
295 return -EACCES;
296 }
297
298 return 0;
299}
300
301/* Send an IPMI command */
302static int aem_send_message(struct aem_ipmi_data *data)
303{
304 int err;
305
306 err = ipmi_validate_addr(&data->address, sizeof(data->address));
307 if (err)
308 goto out;
309
310 data->tx_msgid++;
311 err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
312 &data->tx_message, data, 0, 0, 0);
313 if (err)
314 goto out1;
315
316 return 0;
317out1:
318 dev_err(data->bmc_device, "request_settime=%x\n", err);
319 return err;
320out:
321 dev_err(data->bmc_device, "validate_addr=%x\n", err);
322 return err;
323}
324
325/* Dispatch IPMI messages to callers */
326static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
327{
328 unsigned short rx_len;
329 struct aem_ipmi_data *data = user_msg_data;
330
331 if (msg->msgid != data->tx_msgid) {
332 dev_err(data->bmc_device, "Mismatch between received msgid "
333 "(%02x) and transmitted msgid (%02x)!\n",
334 (int)msg->msgid,
335 (int)data->tx_msgid);
336 ipmi_free_recv_msg(msg);
337 return;
338 }
339
340 data->rx_recv_type = msg->recv_type;
341 if (msg->msg.data_len > 0)
342 data->rx_result = msg->msg.data[0];
343 else
344 data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
345
346 if (msg->msg.data_len > 1) {
347 rx_len = msg->msg.data_len - 1;
348 if (data->rx_msg_len < rx_len)
349 rx_len = data->rx_msg_len;
350 data->rx_msg_len = rx_len;
351 memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
352 } else
353 data->rx_msg_len = 0;
354
355 ipmi_free_recv_msg(msg);
356 complete(&data->read_complete);
357}
358
359/* ID functions */
360
361/* Obtain an id */
362static int aem_idr_get(int *id)
363{
364 int i, err;
365
366again:
367 if (unlikely(!idr_pre_get(&aem_idr, GFP_KERNEL)))
368 return -ENOMEM;
369
370 spin_lock(&aem_idr_lock);
371 err = idr_get_new(&aem_idr, NULL, &i);
372 spin_unlock(&aem_idr_lock);
373
374 if (unlikely(err == -EAGAIN))
375 goto again;
376 else if (unlikely(err))
377 return err;
378
379 *id = i & MAX_ID_MASK;
380 return 0;
381}
382
383/* Release an object ID */
384static void aem_idr_put(int id)
385{
386 spin_lock(&aem_idr_lock);
387 idr_remove(&aem_idr, id);
388 spin_unlock(&aem_idr_lock);
389}
390
391/* Sensor support functions */
392
393/* Read a sensor value */
394static int aem_read_sensor(struct aem_data *data, u8 elt, u8 reg,
395 void *buf, size_t size)
396{
397 int rs_size, res;
398 struct aem_read_sensor_req rs_req;
399 struct aem_read_sensor_resp *rs_resp;
400 struct aem_ipmi_data *ipmi = &data->ipmi;
401
402 /* AEM registers are 1, 2, 4 or 8 bytes */
403 switch (size) {
404 case 1:
405 case 2:
406 case 4:
407 case 8:
408 break;
409 default:
410 return -EINVAL;
411 }
412
413 rs_req.id = system_x_id;
414 rs_req.module_handle = data->module_handle;
415 rs_req.element = elt;
416 rs_req.subcommand = AEM_READ_REGISTER;
417 rs_req.reg = reg;
418 rs_req.rx_buf_size = size;
419
420 ipmi->tx_message.cmd = AEM_ELEMENT_CMD;
421 ipmi->tx_message.data = (char *)&rs_req;
422 ipmi->tx_message.data_len = sizeof(rs_req);
423
424 rs_size = sizeof(*rs_resp) + size;
425 rs_resp = kzalloc(rs_size, GFP_KERNEL);
426 if (!rs_resp)
427 return -ENOMEM;
428
429 ipmi->rx_msg_data = rs_resp;
430 ipmi->rx_msg_len = rs_size;
431
432 aem_send_message(ipmi);
433
434 res = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT);
Julia Lawall66a89b22011-08-09 11:10:56 -0400435 if (!res) {
436 res = -ETIMEDOUT;
437 goto out;
438 }
Darrick J. Wong8808a792008-05-23 13:04:25 -0700439
440 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
441 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
Julia Lawall66a89b22011-08-09 11:10:56 -0400442 res = -ENOENT;
443 goto out;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700444 }
445
446 switch (size) {
447 case 1: {
448 u8 *x = buf;
449 *x = rs_resp->bytes[0];
450 break;
451 }
452 case 2: {
453 u16 *x = buf;
Al Viroa064d5b2008-06-02 10:59:02 +0100454 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700455 break;
456 }
457 case 4: {
458 u32 *x = buf;
Al Viroa064d5b2008-06-02 10:59:02 +0100459 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700460 break;
461 }
462 case 8: {
463 u64 *x = buf;
Al Viroa064d5b2008-06-02 10:59:02 +0100464 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700465 break;
466 }
467 }
Julia Lawall66a89b22011-08-09 11:10:56 -0400468 res = 0;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700469
Julia Lawall66a89b22011-08-09 11:10:56 -0400470out:
471 kfree(rs_resp);
472 return res;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700473}
474
475/* Update AEM energy registers */
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700476static void update_aem_energy_one(struct aem_data *data, int which)
477{
478 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
479 &data->energy[which], 8);
480}
481
Darrick J. Wong8808a792008-05-23 13:04:25 -0700482static void update_aem_energy(struct aem_data *data)
483{
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700484 update_aem_energy_one(data, 0);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700485 if (data->ver_major < 2)
486 return;
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700487 update_aem_energy_one(data, 1);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700488}
489
490/* Update all AEM1 sensors */
491static void update_aem1_sensors(struct aem_data *data)
492{
493 mutex_lock(&data->lock);
494 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
495 data->valid)
496 goto out;
497
498 update_aem_energy(data);
499out:
500 mutex_unlock(&data->lock);
501}
502
503/* Update all AEM2 sensors */
504static void update_aem2_sensors(struct aem_data *data)
505{
506 int i;
507
508 mutex_lock(&data->lock);
509 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
510 data->valid)
511 goto out;
512
513 update_aem_energy(data);
514 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
515 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
516
517 for (i = POWER_CAP; i <= POWER_AUX; i++)
518 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
519 &data->pcap[i], 2);
520out:
521 mutex_unlock(&data->lock);
522}
523
524/* Delete an AEM instance */
525static void aem_delete(struct aem_data *data)
526{
527 list_del(&data->list);
528 aem_remove_sensors(data);
529 hwmon_device_unregister(data->hwmon_dev);
530 ipmi_destroy_user(data->ipmi.user);
Jean Delvare95de3b22011-05-25 20:43:31 +0200531 platform_set_drvdata(data->pdev, NULL);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700532 platform_device_unregister(data->pdev);
533 aem_idr_put(data->id);
534 kfree(data);
535}
536
537/* Probe functions for AEM1 devices */
538
539/* Retrieve version and module handle for an AEM1 instance */
540static int aem_find_aem1_count(struct aem_ipmi_data *data)
541{
542 int res;
543 struct aem_find_firmware_req ff_req;
544 struct aem_find_firmware_resp ff_resp;
545
546 ff_req.id = system_x_id;
547 ff_req.index = 0;
548 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
549
550 data->tx_message.cmd = AEM_FIND_FW_CMD;
551 data->tx_message.data = (char *)&ff_req;
552 data->tx_message.data_len = sizeof(ff_req);
553
554 data->rx_msg_data = &ff_resp;
555 data->rx_msg_len = sizeof(ff_resp);
556
557 aem_send_message(data);
558
559 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
560 if (!res)
561 return -ETIMEDOUT;
562
563 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
564 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
565 return -ENOENT;
566
567 return ff_resp.num_instances;
568}
569
570/* Find and initialize one AEM1 instance */
571static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
572{
573 struct aem_data *data;
574 int i;
575 int res = -ENOMEM;
576
577 data = kzalloc(sizeof(*data), GFP_KERNEL);
578 if (!data)
579 return res;
580 mutex_init(&data->lock);
581
582 /* Copy instance data */
583 data->ver_major = 1;
584 data->ver_minor = 0;
585 data->module_handle = module_handle;
586 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
587 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
588
589 /* Create sub-device for this fw instance */
590 if (aem_idr_get(&data->id))
591 goto id_err;
592
593 data->pdev = platform_device_alloc(DRVNAME, data->id);
594 if (!data->pdev)
595 goto dev_err;
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800596 data->pdev->dev.driver = &aem_driver.driver;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700597
598 res = platform_device_add(data->pdev);
599 if (res)
600 goto ipmi_err;
601
Jean Delvare95de3b22011-05-25 20:43:31 +0200602 platform_set_drvdata(data->pdev, data);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700603
604 /* Set up IPMI interface */
605 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
606 probe->bmc_device))
607 goto ipmi_err;
608
609 /* Register with hwmon */
610 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
611
612 if (IS_ERR(data->hwmon_dev)) {
613 dev_err(&data->pdev->dev, "Unable to register hwmon "
614 "device for IPMI interface %d\n",
615 probe->interface);
616 goto hwmon_reg_err;
617 }
618
619 data->update = update_aem1_sensors;
620
621 /* Find sensors */
622 if (aem1_find_sensors(data))
623 goto sensor_err;
624
625 /* Add to our list of AEM devices */
626 list_add_tail(&data->list, &driver_data.aem_devices);
627
628 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
629 data->ver_major, data->ver_minor,
630 data->module_handle);
631 return 0;
632
633sensor_err:
634 hwmon_device_unregister(data->hwmon_dev);
635hwmon_reg_err:
636 ipmi_destroy_user(data->ipmi.user);
637ipmi_err:
Jean Delvare95de3b22011-05-25 20:43:31 +0200638 platform_set_drvdata(data->pdev, NULL);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700639 platform_device_unregister(data->pdev);
640dev_err:
641 aem_idr_put(data->id);
642id_err:
643 kfree(data);
644
645 return res;
646}
647
648/* Find and initialize all AEM1 instances */
649static int aem_init_aem1(struct aem_ipmi_data *probe)
650{
651 int num, i, err;
652
653 num = aem_find_aem1_count(probe);
654 for (i = 0; i < num; i++) {
655 err = aem_init_aem1_inst(probe, i);
656 if (err) {
657 dev_err(probe->bmc_device,
658 "Error %d initializing AEM1 0x%X\n",
659 err, i);
660 return err;
661 }
662 }
663
664 return 0;
665}
666
667/* Probe functions for AEM2 devices */
668
669/* Retrieve version and module handle for an AEM2 instance */
670static int aem_find_aem2(struct aem_ipmi_data *data,
671 struct aem_find_instance_resp *fi_resp,
672 int instance_num)
673{
674 int res;
675 struct aem_find_instance_req fi_req;
676
677 fi_req.id = system_x_id;
678 fi_req.instance_number = instance_num;
679 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
680
681 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
682 data->tx_message.data = (char *)&fi_req;
683 data->tx_message.data_len = sizeof(fi_req);
684
685 data->rx_msg_data = fi_resp;
686 data->rx_msg_len = sizeof(*fi_resp);
687
688 aem_send_message(data);
689
690 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
691 if (!res)
692 return -ETIMEDOUT;
693
694 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
Darrick J. Wong9c5413e2008-08-15 00:40:40 -0700695 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
696 fi_resp->num_instances <= instance_num)
Darrick J. Wong8808a792008-05-23 13:04:25 -0700697 return -ENOENT;
698
699 return 0;
700}
701
702/* Find and initialize one AEM2 instance */
703static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
704 struct aem_find_instance_resp *fi_resp)
705{
706 struct aem_data *data;
707 int i;
708 int res = -ENOMEM;
709
710 data = kzalloc(sizeof(*data), GFP_KERNEL);
711 if (!data)
712 return res;
713 mutex_init(&data->lock);
714
715 /* Copy instance data */
716 data->ver_major = fi_resp->major;
717 data->ver_minor = fi_resp->minor;
718 data->module_handle = fi_resp->module_handle;
719 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
720 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
721
722 /* Create sub-device for this fw instance */
723 if (aem_idr_get(&data->id))
724 goto id_err;
725
726 data->pdev = platform_device_alloc(DRVNAME, data->id);
727 if (!data->pdev)
728 goto dev_err;
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -0800729 data->pdev->dev.driver = &aem_driver.driver;
Darrick J. Wong8808a792008-05-23 13:04:25 -0700730
731 res = platform_device_add(data->pdev);
732 if (res)
733 goto ipmi_err;
734
Jean Delvare95de3b22011-05-25 20:43:31 +0200735 platform_set_drvdata(data->pdev, data);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700736
737 /* Set up IPMI interface */
738 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
739 probe->bmc_device))
740 goto ipmi_err;
741
742 /* Register with hwmon */
743 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
744
745 if (IS_ERR(data->hwmon_dev)) {
746 dev_err(&data->pdev->dev, "Unable to register hwmon "
747 "device for IPMI interface %d\n",
748 probe->interface);
749 goto hwmon_reg_err;
750 }
751
752 data->update = update_aem2_sensors;
753
754 /* Find sensors */
755 if (aem2_find_sensors(data))
756 goto sensor_err;
757
758 /* Add to our list of AEM devices */
759 list_add_tail(&data->list, &driver_data.aem_devices);
760
761 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
762 data->ver_major, data->ver_minor,
763 data->module_handle);
764 return 0;
765
766sensor_err:
767 hwmon_device_unregister(data->hwmon_dev);
768hwmon_reg_err:
769 ipmi_destroy_user(data->ipmi.user);
770ipmi_err:
Jean Delvare95de3b22011-05-25 20:43:31 +0200771 platform_set_drvdata(data->pdev, NULL);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700772 platform_device_unregister(data->pdev);
773dev_err:
774 aem_idr_put(data->id);
775id_err:
776 kfree(data);
777
778 return res;
779}
780
781/* Find and initialize all AEM2 instances */
782static int aem_init_aem2(struct aem_ipmi_data *probe)
783{
784 struct aem_find_instance_resp fi_resp;
785 int err;
786 int i = 0;
787
788 while (!aem_find_aem2(probe, &fi_resp, i)) {
789 if (fi_resp.major != 2) {
790 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
791 "report this to the maintainer.\n",
792 fi_resp.major);
793 i++;
794 continue;
795 }
796 err = aem_init_aem2_inst(probe, &fi_resp);
797 if (err) {
798 dev_err(probe->bmc_device,
799 "Error %d initializing AEM2 0x%X\n",
800 err, fi_resp.module_handle);
801 return err;
802 }
803 i++;
804 }
805
806 return 0;
807}
808
809/* Probe a BMC for AEM firmware instances */
810static void aem_register_bmc(int iface, struct device *dev)
811{
812 struct aem_ipmi_data probe;
813
814 if (aem_init_ipmi_data(&probe, iface, dev))
815 return;
816
817 /* Ignore probe errors; they won't cause problems */
818 aem_init_aem1(&probe);
819 aem_init_aem2(&probe);
820
821 ipmi_destroy_user(probe.user);
822}
823
824/* Handle BMC deletion */
825static void aem_bmc_gone(int iface)
826{
827 struct aem_data *p1, *next1;
828
829 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
830 if (p1->ipmi.interface == iface)
831 aem_delete(p1);
832}
833
834/* sysfs support functions */
835
836/* AEM device name */
837static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
838 char *buf)
839{
840 struct aem_data *data = dev_get_drvdata(dev);
841
842 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
843}
844static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
845
846/* AEM device version */
847static ssize_t show_version(struct device *dev,
848 struct device_attribute *devattr,
849 char *buf)
850{
851 struct aem_data *data = dev_get_drvdata(dev);
852
853 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
854}
855static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
856
857/* Display power use */
858static ssize_t aem_show_power(struct device *dev,
859 struct device_attribute *devattr,
860 char *buf)
861{
862 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
863 struct aem_data *data = dev_get_drvdata(dev);
864 u64 before, after, delta, time;
865 signed long leftover;
866 struct timespec b, a;
867
868 mutex_lock(&data->lock);
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700869 update_aem_energy_one(data, attr->index);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700870 getnstimeofday(&b);
871 before = data->energy[attr->index];
872
873 leftover = schedule_timeout_interruptible(
874 msecs_to_jiffies(data->power_period[attr->index])
875 );
876 if (leftover) {
877 mutex_unlock(&data->lock);
878 return 0;
879 }
880
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700881 update_aem_energy_one(data, attr->index);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700882 getnstimeofday(&a);
883 after = data->energy[attr->index];
884 mutex_unlock(&data->lock);
885
886 time = timespec_to_ns(&a) - timespec_to_ns(&b);
887 delta = (after - before) * UJ_PER_MJ;
888
889 return sprintf(buf, "%llu\n",
890 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
891}
892
893/* Display energy use */
894static ssize_t aem_show_energy(struct device *dev,
895 struct device_attribute *devattr,
896 char *buf)
897{
898 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
899 struct aem_data *a = dev_get_drvdata(dev);
Darrick J. Wongbb15e7f2008-08-15 00:40:41 -0700900 mutex_lock(&a->lock);
901 update_aem_energy_one(a, attr->index);
902 mutex_unlock(&a->lock);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700903
904 return sprintf(buf, "%llu\n",
905 (unsigned long long)a->energy[attr->index] * 1000);
906}
907
908/* Display power interval registers */
909static ssize_t aem_show_power_period(struct device *dev,
910 struct device_attribute *devattr,
911 char *buf)
912{
913 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
914 struct aem_data *a = dev_get_drvdata(dev);
915 a->update(a);
916
917 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
918}
919
920/* Set power interval registers */
921static ssize_t aem_set_power_period(struct device *dev,
922 struct device_attribute *devattr,
923 const char *buf, size_t count)
924{
925 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
926 struct aem_data *a = dev_get_drvdata(dev);
927 unsigned long temp;
928 int res;
929
930 res = strict_strtoul(buf, 10, &temp);
931 if (res)
932 return res;
933
934 if (temp < AEM_MIN_POWER_INTERVAL)
935 return -EINVAL;
936
937 mutex_lock(&a->lock);
938 a->power_period[attr->index] = temp;
939 mutex_unlock(&a->lock);
940
941 return count;
942}
943
944/* Discover sensors on an AEM device */
945static int aem_register_sensors(struct aem_data *data,
946 struct aem_ro_sensor_template *ro,
947 struct aem_rw_sensor_template *rw)
948{
949 struct device *dev = &data->pdev->dev;
950 struct sensor_device_attribute *sensors = data->sensors;
951 int err;
952
953 /* Set up read-only sensors */
954 while (ro->label) {
Guenter Roeck3cdb2052011-05-24 12:33:26 -0700955 sysfs_attr_init(&sensors->dev_attr.attr);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700956 sensors->dev_attr.attr.name = ro->label;
957 sensors->dev_attr.attr.mode = S_IRUGO;
958 sensors->dev_attr.show = ro->show;
959 sensors->index = ro->index;
960
961 err = device_create_file(dev, &sensors->dev_attr);
962 if (err) {
963 sensors->dev_attr.attr.name = NULL;
964 goto error;
965 }
966 sensors++;
967 ro++;
968 }
969
970 /* Set up read-write sensors */
971 while (rw->label) {
Guenter Roeck3cdb2052011-05-24 12:33:26 -0700972 sysfs_attr_init(&sensors->dev_attr.attr);
Darrick J. Wong8808a792008-05-23 13:04:25 -0700973 sensors->dev_attr.attr.name = rw->label;
974 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
975 sensors->dev_attr.show = rw->show;
976 sensors->dev_attr.store = rw->set;
977 sensors->index = rw->index;
978
979 err = device_create_file(dev, &sensors->dev_attr);
980 if (err) {
981 sensors->dev_attr.attr.name = NULL;
982 goto error;
983 }
984 sensors++;
985 rw++;
986 }
987
988 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
989 if (err)
990 goto error;
991 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
992 return err;
993
994error:
995 aem_remove_sensors(data);
996 return err;
997}
998
999/* sysfs support functions for AEM2 sensors */
1000
1001/* Display temperature use */
1002static ssize_t aem2_show_temp(struct device *dev,
1003 struct device_attribute *devattr,
1004 char *buf)
1005{
1006 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1007 struct aem_data *a = dev_get_drvdata(dev);
1008 a->update(a);
1009
1010 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1011}
1012
1013/* Display power-capping registers */
1014static ssize_t aem2_show_pcap_value(struct device *dev,
1015 struct device_attribute *devattr,
1016 char *buf)
1017{
1018 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1019 struct aem_data *a = dev_get_drvdata(dev);
1020 a->update(a);
1021
1022 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1023}
1024
1025/* Remove sensors attached to an AEM device */
1026static void aem_remove_sensors(struct aem_data *data)
1027{
1028 int i;
1029
1030 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1031 if (!data->sensors[i].dev_attr.attr.name)
1032 continue;
1033 device_remove_file(&data->pdev->dev,
1034 &data->sensors[i].dev_attr);
1035 }
1036
1037 device_remove_file(&data->pdev->dev,
1038 &sensor_dev_attr_name.dev_attr);
1039 device_remove_file(&data->pdev->dev,
1040 &sensor_dev_attr_version.dev_attr);
1041}
1042
1043/* Sensor probe functions */
1044
1045/* Description of AEM1 sensors */
1046static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1047{"energy1_input", aem_show_energy, 0},
1048{"power1_average", aem_show_power, 0},
1049{NULL, NULL, 0},
1050};
1051
1052static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1053{"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1054{NULL, NULL, NULL, 0},
1055};
1056
1057/* Description of AEM2 sensors */
1058static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1059{"energy1_input", aem_show_energy, 0},
1060{"energy2_input", aem_show_energy, 1},
1061{"power1_average", aem_show_power, 0},
1062{"power2_average", aem_show_power, 1},
1063{"temp1_input", aem2_show_temp, 0},
1064{"temp2_input", aem2_show_temp, 1},
1065
1066{"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1067{"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1068{"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1069{"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1070
1071{"power3_average", aem2_show_pcap_value, POWER_AUX},
1072{"power_cap", aem2_show_pcap_value, POWER_CAP},
1073{NULL, NULL, 0},
1074};
1075
1076static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1077{"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1078{"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1079{NULL, NULL, NULL, 0},
1080};
1081
1082/* Set up AEM1 sensor attrs */
1083static int aem1_find_sensors(struct aem_data *data)
1084{
1085 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1086}
1087
1088/* Set up AEM2 sensor attrs */
1089static int aem2_find_sensors(struct aem_data *data)
1090{
1091 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1092}
1093
1094/* Module init/exit routines */
1095
1096static int __init aem_init(void)
1097{
1098 int res;
1099
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08001100 res = driver_register(&aem_driver.driver);
Darrick J. Wong8808a792008-05-23 13:04:25 -07001101 if (res) {
Joe Perches55d705c2010-10-20 06:51:38 +00001102 pr_err("Can't register aem driver\n");
Darrick J. Wong8808a792008-05-23 13:04:25 -07001103 return res;
1104 }
1105
1106 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1107 if (res)
1108 goto ipmi_reg_err;
1109 return 0;
1110
1111ipmi_reg_err:
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08001112 driver_unregister(&aem_driver.driver);
Darrick J. Wong8808a792008-05-23 13:04:25 -07001113 return res;
1114
1115}
1116
1117static void __exit aem_exit(void)
1118{
1119 struct aem_data *p1, *next1;
1120
1121 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
Darrick J. Wongfe2d5ff2008-11-12 13:25:00 -08001122 driver_unregister(&aem_driver.driver);
Darrick J. Wong8808a792008-05-23 13:04:25 -07001123 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1124 aem_delete(p1);
1125}
1126
1127MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
Darrick J. Wongeb93b7d2008-08-15 00:40:40 -07001128MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
Darrick J. Wong8808a792008-05-23 13:04:25 -07001129MODULE_LICENSE("GPL");
1130
1131module_init(aem_init);
1132module_exit(aem_exit);
Darrick J. Wong21d93c12008-10-17 17:51:19 +02001133
1134MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1135MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1136MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1137MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1138MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1139MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
Darrick J. Wong80704082009-06-15 18:39:46 +02001140MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");