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
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #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>
34 #include <linux/slab.h>
35 #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
91 static DEFINE_IDR(aem_idr);
92 static DEFINE_SPINLOCK(aem_idr_lock);
93
94 static struct platform_driver aem_driver = {
95 .driver = {
96 .name = DRVNAME,
97 .bus = &platform_bus_type,
98 }
99 };
100
101 struct 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
118 struct 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
126 struct 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
137 struct 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 */
186 struct aem_iana_id {
187 u8 bytes[3];
188 };
189 static struct aem_iana_id system_x_id = {
190 .bytes = {0x4D, 0x4F, 0x00}
191 };
192
193 /* These are used to find AEM1 instances */
194 struct aem_find_firmware_req {
195 struct aem_iana_id id;
196 u8 rsvd;
197 __be16 index;
198 __be16 module_type_id;
199 } __packed;
200
201 struct 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 */
207 struct aem_find_instance_req {
208 struct aem_iana_id id;
209 u8 instance_number;
210 __be16 module_type_id;
211 } __packed;
212
213 struct 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 */
223 struct 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
232 struct 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 */
238 struct 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
244 static void aem_register_bmc(int iface, struct device *dev);
245 static void aem_bmc_gone(int iface);
246 static void aem_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
247
248 static void aem_remove_sensors(struct aem_data *data);
249 static int aem_init_aem1(struct aem_ipmi_data *probe);
250 static int aem_init_aem2(struct aem_ipmi_data *probe);
251 static int aem1_find_sensors(struct aem_data *data);
252 static int aem2_find_sensors(struct aem_data *data);
253 static void update_aem1_sensors(struct aem_data *data);
254 static void update_aem2_sensors(struct aem_data *data);
255
256 static 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 */
aem_init_ipmi_data(struct aem_ipmi_data * data,int iface,struct device * bmc)271 static 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 */
aem_send_message(struct aem_ipmi_data * data)302 static 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;
317 out1:
318 dev_err(data->bmc_device, "request_settime=%x\n", err);
319 return err;
320 out:
321 dev_err(data->bmc_device, "validate_addr=%x\n", err);
322 return err;
323 }
324
325 /* Dispatch IPMI messages to callers */
aem_msg_handler(struct ipmi_recv_msg * msg,void * user_msg_data)326 static 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 */
aem_idr_get(int * id)362 static int aem_idr_get(int *id)
363 {
364 int i, err;
365
366 again:
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 */
aem_idr_put(int id)384 static 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 */
aem_read_sensor(struct aem_data * data,u8 elt,u8 reg,void * buf,size_t size)394 static 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);
435 if (!res)
436 return -ETIMEDOUT;
437
438 if (ipmi->rx_result || ipmi->rx_msg_len != rs_size ||
439 memcmp(&rs_resp->id, &system_x_id, sizeof(system_x_id))) {
440 kfree(rs_resp);
441 return -ENOENT;
442 }
443
444 switch (size) {
445 case 1: {
446 u8 *x = buf;
447 *x = rs_resp->bytes[0];
448 break;
449 }
450 case 2: {
451 u16 *x = buf;
452 *x = be16_to_cpup((__be16 *)rs_resp->bytes);
453 break;
454 }
455 case 4: {
456 u32 *x = buf;
457 *x = be32_to_cpup((__be32 *)rs_resp->bytes);
458 break;
459 }
460 case 8: {
461 u64 *x = buf;
462 *x = be64_to_cpup((__be64 *)rs_resp->bytes);
463 break;
464 }
465 }
466
467 return 0;
468 }
469
470 /* Update AEM energy registers */
update_aem_energy_one(struct aem_data * data,int which)471 static void update_aem_energy_one(struct aem_data *data, int which)
472 {
473 aem_read_sensor(data, AEM_ENERGY_ELEMENT, which,
474 &data->energy[which], 8);
475 }
476
update_aem_energy(struct aem_data * data)477 static void update_aem_energy(struct aem_data *data)
478 {
479 update_aem_energy_one(data, 0);
480 if (data->ver_major < 2)
481 return;
482 update_aem_energy_one(data, 1);
483 }
484
485 /* Update all AEM1 sensors */
update_aem1_sensors(struct aem_data * data)486 static void update_aem1_sensors(struct aem_data *data)
487 {
488 mutex_lock(&data->lock);
489 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
490 data->valid)
491 goto out;
492
493 update_aem_energy(data);
494 out:
495 mutex_unlock(&data->lock);
496 }
497
498 /* Update all AEM2 sensors */
update_aem2_sensors(struct aem_data * data)499 static void update_aem2_sensors(struct aem_data *data)
500 {
501 int i;
502
503 mutex_lock(&data->lock);
504 if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
505 data->valid)
506 goto out;
507
508 update_aem_energy(data);
509 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 0, &data->temp[0], 1);
510 aem_read_sensor(data, AEM_EXHAUST_ELEMENT, 1, &data->temp[1], 1);
511
512 for (i = POWER_CAP; i <= POWER_AUX; i++)
513 aem_read_sensor(data, AEM_POWER_CAP_ELEMENT, i,
514 &data->pcap[i], 2);
515 out:
516 mutex_unlock(&data->lock);
517 }
518
519 /* Delete an AEM instance */
aem_delete(struct aem_data * data)520 static void aem_delete(struct aem_data *data)
521 {
522 list_del(&data->list);
523 aem_remove_sensors(data);
524 hwmon_device_unregister(data->hwmon_dev);
525 ipmi_destroy_user(data->ipmi.user);
526 dev_set_drvdata(&data->pdev->dev, NULL);
527 platform_device_unregister(data->pdev);
528 aem_idr_put(data->id);
529 kfree(data);
530 }
531
532 /* Probe functions for AEM1 devices */
533
534 /* Retrieve version and module handle for an AEM1 instance */
aem_find_aem1_count(struct aem_ipmi_data * data)535 static int aem_find_aem1_count(struct aem_ipmi_data *data)
536 {
537 int res;
538 struct aem_find_firmware_req ff_req;
539 struct aem_find_firmware_resp ff_resp;
540
541 ff_req.id = system_x_id;
542 ff_req.index = 0;
543 ff_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
544
545 data->tx_message.cmd = AEM_FIND_FW_CMD;
546 data->tx_message.data = (char *)&ff_req;
547 data->tx_message.data_len = sizeof(ff_req);
548
549 data->rx_msg_data = &ff_resp;
550 data->rx_msg_len = sizeof(ff_resp);
551
552 aem_send_message(data);
553
554 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
555 if (!res)
556 return -ETIMEDOUT;
557
558 if (data->rx_result || data->rx_msg_len != sizeof(ff_resp) ||
559 memcmp(&ff_resp.id, &system_x_id, sizeof(system_x_id)))
560 return -ENOENT;
561
562 return ff_resp.num_instances;
563 }
564
565 /* Find and initialize one AEM1 instance */
aem_init_aem1_inst(struct aem_ipmi_data * probe,u8 module_handle)566 static int aem_init_aem1_inst(struct aem_ipmi_data *probe, u8 module_handle)
567 {
568 struct aem_data *data;
569 int i;
570 int res = -ENOMEM;
571
572 data = kzalloc(sizeof(*data), GFP_KERNEL);
573 if (!data)
574 return res;
575 mutex_init(&data->lock);
576
577 /* Copy instance data */
578 data->ver_major = 1;
579 data->ver_minor = 0;
580 data->module_handle = module_handle;
581 for (i = 0; i < AEM1_NUM_ENERGY_REGS; i++)
582 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
583
584 /* Create sub-device for this fw instance */
585 if (aem_idr_get(&data->id))
586 goto id_err;
587
588 data->pdev = platform_device_alloc(DRVNAME, data->id);
589 if (!data->pdev)
590 goto dev_err;
591 data->pdev->dev.driver = &aem_driver.driver;
592
593 res = platform_device_add(data->pdev);
594 if (res)
595 goto ipmi_err;
596
597 dev_set_drvdata(&data->pdev->dev, data);
598
599 /* Set up IPMI interface */
600 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
601 probe->bmc_device))
602 goto ipmi_err;
603
604 /* Register with hwmon */
605 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
606
607 if (IS_ERR(data->hwmon_dev)) {
608 dev_err(&data->pdev->dev, "Unable to register hwmon "
609 "device for IPMI interface %d\n",
610 probe->interface);
611 goto hwmon_reg_err;
612 }
613
614 data->update = update_aem1_sensors;
615
616 /* Find sensors */
617 if (aem1_find_sensors(data))
618 goto sensor_err;
619
620 /* Add to our list of AEM devices */
621 list_add_tail(&data->list, &driver_data.aem_devices);
622
623 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
624 data->ver_major, data->ver_minor,
625 data->module_handle);
626 return 0;
627
628 sensor_err:
629 hwmon_device_unregister(data->hwmon_dev);
630 hwmon_reg_err:
631 ipmi_destroy_user(data->ipmi.user);
632 ipmi_err:
633 dev_set_drvdata(&data->pdev->dev, NULL);
634 platform_device_unregister(data->pdev);
635 dev_err:
636 aem_idr_put(data->id);
637 id_err:
638 kfree(data);
639
640 return res;
641 }
642
643 /* Find and initialize all AEM1 instances */
aem_init_aem1(struct aem_ipmi_data * probe)644 static int aem_init_aem1(struct aem_ipmi_data *probe)
645 {
646 int num, i, err;
647
648 num = aem_find_aem1_count(probe);
649 for (i = 0; i < num; i++) {
650 err = aem_init_aem1_inst(probe, i);
651 if (err) {
652 dev_err(probe->bmc_device,
653 "Error %d initializing AEM1 0x%X\n",
654 err, i);
655 return err;
656 }
657 }
658
659 return 0;
660 }
661
662 /* Probe functions for AEM2 devices */
663
664 /* Retrieve version and module handle for an AEM2 instance */
aem_find_aem2(struct aem_ipmi_data * data,struct aem_find_instance_resp * fi_resp,int instance_num)665 static int aem_find_aem2(struct aem_ipmi_data *data,
666 struct aem_find_instance_resp *fi_resp,
667 int instance_num)
668 {
669 int res;
670 struct aem_find_instance_req fi_req;
671
672 fi_req.id = system_x_id;
673 fi_req.instance_number = instance_num;
674 fi_req.module_type_id = cpu_to_be16(AEM_MODULE_TYPE_ID);
675
676 data->tx_message.cmd = AEM_FW_INSTANCE_CMD;
677 data->tx_message.data = (char *)&fi_req;
678 data->tx_message.data_len = sizeof(fi_req);
679
680 data->rx_msg_data = fi_resp;
681 data->rx_msg_len = sizeof(*fi_resp);
682
683 aem_send_message(data);
684
685 res = wait_for_completion_timeout(&data->read_complete, IPMI_TIMEOUT);
686 if (!res)
687 return -ETIMEDOUT;
688
689 if (data->rx_result || data->rx_msg_len != sizeof(*fi_resp) ||
690 memcmp(&fi_resp->id, &system_x_id, sizeof(system_x_id)) ||
691 fi_resp->num_instances <= instance_num)
692 return -ENOENT;
693
694 return 0;
695 }
696
697 /* Find and initialize one AEM2 instance */
aem_init_aem2_inst(struct aem_ipmi_data * probe,struct aem_find_instance_resp * fi_resp)698 static int aem_init_aem2_inst(struct aem_ipmi_data *probe,
699 struct aem_find_instance_resp *fi_resp)
700 {
701 struct aem_data *data;
702 int i;
703 int res = -ENOMEM;
704
705 data = kzalloc(sizeof(*data), GFP_KERNEL);
706 if (!data)
707 return res;
708 mutex_init(&data->lock);
709
710 /* Copy instance data */
711 data->ver_major = fi_resp->major;
712 data->ver_minor = fi_resp->minor;
713 data->module_handle = fi_resp->module_handle;
714 for (i = 0; i < AEM2_NUM_ENERGY_REGS; i++)
715 data->power_period[i] = AEM_DEFAULT_POWER_INTERVAL;
716
717 /* Create sub-device for this fw instance */
718 if (aem_idr_get(&data->id))
719 goto id_err;
720
721 data->pdev = platform_device_alloc(DRVNAME, data->id);
722 if (!data->pdev)
723 goto dev_err;
724 data->pdev->dev.driver = &aem_driver.driver;
725
726 res = platform_device_add(data->pdev);
727 if (res)
728 goto ipmi_err;
729
730 dev_set_drvdata(&data->pdev->dev, data);
731
732 /* Set up IPMI interface */
733 if (aem_init_ipmi_data(&data->ipmi, probe->interface,
734 probe->bmc_device))
735 goto ipmi_err;
736
737 /* Register with hwmon */
738 data->hwmon_dev = hwmon_device_register(&data->pdev->dev);
739
740 if (IS_ERR(data->hwmon_dev)) {
741 dev_err(&data->pdev->dev, "Unable to register hwmon "
742 "device for IPMI interface %d\n",
743 probe->interface);
744 goto hwmon_reg_err;
745 }
746
747 data->update = update_aem2_sensors;
748
749 /* Find sensors */
750 if (aem2_find_sensors(data))
751 goto sensor_err;
752
753 /* Add to our list of AEM devices */
754 list_add_tail(&data->list, &driver_data.aem_devices);
755
756 dev_info(data->ipmi.bmc_device, "Found AEM v%d.%d at 0x%X\n",
757 data->ver_major, data->ver_minor,
758 data->module_handle);
759 return 0;
760
761 sensor_err:
762 hwmon_device_unregister(data->hwmon_dev);
763 hwmon_reg_err:
764 ipmi_destroy_user(data->ipmi.user);
765 ipmi_err:
766 dev_set_drvdata(&data->pdev->dev, NULL);
767 platform_device_unregister(data->pdev);
768 dev_err:
769 aem_idr_put(data->id);
770 id_err:
771 kfree(data);
772
773 return res;
774 }
775
776 /* Find and initialize all AEM2 instances */
aem_init_aem2(struct aem_ipmi_data * probe)777 static int aem_init_aem2(struct aem_ipmi_data *probe)
778 {
779 struct aem_find_instance_resp fi_resp;
780 int err;
781 int i = 0;
782
783 while (!aem_find_aem2(probe, &fi_resp, i)) {
784 if (fi_resp.major != 2) {
785 dev_err(probe->bmc_device, "Unknown AEM v%d; please "
786 "report this to the maintainer.\n",
787 fi_resp.major);
788 i++;
789 continue;
790 }
791 err = aem_init_aem2_inst(probe, &fi_resp);
792 if (err) {
793 dev_err(probe->bmc_device,
794 "Error %d initializing AEM2 0x%X\n",
795 err, fi_resp.module_handle);
796 return err;
797 }
798 i++;
799 }
800
801 return 0;
802 }
803
804 /* Probe a BMC for AEM firmware instances */
aem_register_bmc(int iface,struct device * dev)805 static void aem_register_bmc(int iface, struct device *dev)
806 {
807 struct aem_ipmi_data probe;
808
809 if (aem_init_ipmi_data(&probe, iface, dev))
810 return;
811
812 /* Ignore probe errors; they won't cause problems */
813 aem_init_aem1(&probe);
814 aem_init_aem2(&probe);
815
816 ipmi_destroy_user(probe.user);
817 }
818
819 /* Handle BMC deletion */
aem_bmc_gone(int iface)820 static void aem_bmc_gone(int iface)
821 {
822 struct aem_data *p1, *next1;
823
824 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
825 if (p1->ipmi.interface == iface)
826 aem_delete(p1);
827 }
828
829 /* sysfs support functions */
830
831 /* AEM device name */
show_name(struct device * dev,struct device_attribute * devattr,char * buf)832 static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
833 char *buf)
834 {
835 struct aem_data *data = dev_get_drvdata(dev);
836
837 return sprintf(buf, "%s%d\n", DRVNAME, data->ver_major);
838 }
839 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
840
841 /* AEM device version */
show_version(struct device * dev,struct device_attribute * devattr,char * buf)842 static ssize_t show_version(struct device *dev,
843 struct device_attribute *devattr,
844 char *buf)
845 {
846 struct aem_data *data = dev_get_drvdata(dev);
847
848 return sprintf(buf, "%d.%d\n", data->ver_major, data->ver_minor);
849 }
850 static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, 0);
851
852 /* Display power use */
aem_show_power(struct device * dev,struct device_attribute * devattr,char * buf)853 static ssize_t aem_show_power(struct device *dev,
854 struct device_attribute *devattr,
855 char *buf)
856 {
857 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
858 struct aem_data *data = dev_get_drvdata(dev);
859 u64 before, after, delta, time;
860 signed long leftover;
861 struct timespec b, a;
862
863 mutex_lock(&data->lock);
864 update_aem_energy_one(data, attr->index);
865 getnstimeofday(&b);
866 before = data->energy[attr->index];
867
868 leftover = schedule_timeout_interruptible(
869 msecs_to_jiffies(data->power_period[attr->index])
870 );
871 if (leftover) {
872 mutex_unlock(&data->lock);
873 return 0;
874 }
875
876 update_aem_energy_one(data, attr->index);
877 getnstimeofday(&a);
878 after = data->energy[attr->index];
879 mutex_unlock(&data->lock);
880
881 time = timespec_to_ns(&a) - timespec_to_ns(&b);
882 delta = (after - before) * UJ_PER_MJ;
883
884 return sprintf(buf, "%llu\n",
885 (unsigned long long)div64_u64(delta * NSEC_PER_SEC, time));
886 }
887
888 /* Display energy use */
aem_show_energy(struct device * dev,struct device_attribute * devattr,char * buf)889 static ssize_t aem_show_energy(struct device *dev,
890 struct device_attribute *devattr,
891 char *buf)
892 {
893 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
894 struct aem_data *a = dev_get_drvdata(dev);
895 mutex_lock(&a->lock);
896 update_aem_energy_one(a, attr->index);
897 mutex_unlock(&a->lock);
898
899 return sprintf(buf, "%llu\n",
900 (unsigned long long)a->energy[attr->index] * 1000);
901 }
902
903 /* Display power interval registers */
aem_show_power_period(struct device * dev,struct device_attribute * devattr,char * buf)904 static ssize_t aem_show_power_period(struct device *dev,
905 struct device_attribute *devattr,
906 char *buf)
907 {
908 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
909 struct aem_data *a = dev_get_drvdata(dev);
910 a->update(a);
911
912 return sprintf(buf, "%lu\n", a->power_period[attr->index]);
913 }
914
915 /* Set power interval registers */
aem_set_power_period(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)916 static ssize_t aem_set_power_period(struct device *dev,
917 struct device_attribute *devattr,
918 const char *buf, size_t count)
919 {
920 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
921 struct aem_data *a = dev_get_drvdata(dev);
922 unsigned long temp;
923 int res;
924
925 res = strict_strtoul(buf, 10, &temp);
926 if (res)
927 return res;
928
929 if (temp < AEM_MIN_POWER_INTERVAL)
930 return -EINVAL;
931
932 mutex_lock(&a->lock);
933 a->power_period[attr->index] = temp;
934 mutex_unlock(&a->lock);
935
936 return count;
937 }
938
939 /* Discover sensors on an AEM device */
aem_register_sensors(struct aem_data * data,struct aem_ro_sensor_template * ro,struct aem_rw_sensor_template * rw)940 static int aem_register_sensors(struct aem_data *data,
941 struct aem_ro_sensor_template *ro,
942 struct aem_rw_sensor_template *rw)
943 {
944 struct device *dev = &data->pdev->dev;
945 struct sensor_device_attribute *sensors = data->sensors;
946 int err;
947
948 /* Set up read-only sensors */
949 while (ro->label) {
950 sensors->dev_attr.attr.name = ro->label;
951 sensors->dev_attr.attr.mode = S_IRUGO;
952 sensors->dev_attr.show = ro->show;
953 sensors->index = ro->index;
954
955 err = device_create_file(dev, &sensors->dev_attr);
956 if (err) {
957 sensors->dev_attr.attr.name = NULL;
958 goto error;
959 }
960 sensors++;
961 ro++;
962 }
963
964 /* Set up read-write sensors */
965 while (rw->label) {
966 sensors->dev_attr.attr.name = rw->label;
967 sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
968 sensors->dev_attr.show = rw->show;
969 sensors->dev_attr.store = rw->set;
970 sensors->index = rw->index;
971
972 err = device_create_file(dev, &sensors->dev_attr);
973 if (err) {
974 sensors->dev_attr.attr.name = NULL;
975 goto error;
976 }
977 sensors++;
978 rw++;
979 }
980
981 err = device_create_file(dev, &sensor_dev_attr_name.dev_attr);
982 if (err)
983 goto error;
984 err = device_create_file(dev, &sensor_dev_attr_version.dev_attr);
985 return err;
986
987 error:
988 aem_remove_sensors(data);
989 return err;
990 }
991
992 /* sysfs support functions for AEM2 sensors */
993
994 /* Display temperature use */
aem2_show_temp(struct device * dev,struct device_attribute * devattr,char * buf)995 static ssize_t aem2_show_temp(struct device *dev,
996 struct device_attribute *devattr,
997 char *buf)
998 {
999 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1000 struct aem_data *a = dev_get_drvdata(dev);
1001 a->update(a);
1002
1003 return sprintf(buf, "%u\n", a->temp[attr->index] * 1000);
1004 }
1005
1006 /* Display power-capping registers */
aem2_show_pcap_value(struct device * dev,struct device_attribute * devattr,char * buf)1007 static ssize_t aem2_show_pcap_value(struct device *dev,
1008 struct device_attribute *devattr,
1009 char *buf)
1010 {
1011 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1012 struct aem_data *a = dev_get_drvdata(dev);
1013 a->update(a);
1014
1015 return sprintf(buf, "%u\n", a->pcap[attr->index] * 100000);
1016 }
1017
1018 /* Remove sensors attached to an AEM device */
aem_remove_sensors(struct aem_data * data)1019 static void aem_remove_sensors(struct aem_data *data)
1020 {
1021 int i;
1022
1023 for (i = 0; i < AEM_NUM_SENSORS; i++) {
1024 if (!data->sensors[i].dev_attr.attr.name)
1025 continue;
1026 device_remove_file(&data->pdev->dev,
1027 &data->sensors[i].dev_attr);
1028 }
1029
1030 device_remove_file(&data->pdev->dev,
1031 &sensor_dev_attr_name.dev_attr);
1032 device_remove_file(&data->pdev->dev,
1033 &sensor_dev_attr_version.dev_attr);
1034 }
1035
1036 /* Sensor probe functions */
1037
1038 /* Description of AEM1 sensors */
1039 static struct aem_ro_sensor_template aem1_ro_sensors[] = {
1040 {"energy1_input", aem_show_energy, 0},
1041 {"power1_average", aem_show_power, 0},
1042 {NULL, NULL, 0},
1043 };
1044
1045 static struct aem_rw_sensor_template aem1_rw_sensors[] = {
1046 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1047 {NULL, NULL, NULL, 0},
1048 };
1049
1050 /* Description of AEM2 sensors */
1051 static struct aem_ro_sensor_template aem2_ro_sensors[] = {
1052 {"energy1_input", aem_show_energy, 0},
1053 {"energy2_input", aem_show_energy, 1},
1054 {"power1_average", aem_show_power, 0},
1055 {"power2_average", aem_show_power, 1},
1056 {"temp1_input", aem2_show_temp, 0},
1057 {"temp2_input", aem2_show_temp, 1},
1058
1059 {"power4_average", aem2_show_pcap_value, POWER_CAP_MAX_HOTPLUG},
1060 {"power5_average", aem2_show_pcap_value, POWER_CAP_MAX},
1061 {"power6_average", aem2_show_pcap_value, POWER_CAP_MIN_WARNING},
1062 {"power7_average", aem2_show_pcap_value, POWER_CAP_MIN},
1063
1064 {"power3_average", aem2_show_pcap_value, POWER_AUX},
1065 {"power_cap", aem2_show_pcap_value, POWER_CAP},
1066 {NULL, NULL, 0},
1067 };
1068
1069 static struct aem_rw_sensor_template aem2_rw_sensors[] = {
1070 {"power1_average_interval", aem_show_power_period, aem_set_power_period, 0},
1071 {"power2_average_interval", aem_show_power_period, aem_set_power_period, 1},
1072 {NULL, NULL, NULL, 0},
1073 };
1074
1075 /* Set up AEM1 sensor attrs */
aem1_find_sensors(struct aem_data * data)1076 static int aem1_find_sensors(struct aem_data *data)
1077 {
1078 return aem_register_sensors(data, aem1_ro_sensors, aem1_rw_sensors);
1079 }
1080
1081 /* Set up AEM2 sensor attrs */
aem2_find_sensors(struct aem_data * data)1082 static int aem2_find_sensors(struct aem_data *data)
1083 {
1084 return aem_register_sensors(data, aem2_ro_sensors, aem2_rw_sensors);
1085 }
1086
1087 /* Module init/exit routines */
1088
aem_init(void)1089 static int __init aem_init(void)
1090 {
1091 int res;
1092
1093 res = driver_register(&aem_driver.driver);
1094 if (res) {
1095 pr_err("Can't register aem driver\n");
1096 return res;
1097 }
1098
1099 res = ipmi_smi_watcher_register(&driver_data.bmc_events);
1100 if (res)
1101 goto ipmi_reg_err;
1102 return 0;
1103
1104 ipmi_reg_err:
1105 driver_unregister(&aem_driver.driver);
1106 return res;
1107
1108 }
1109
aem_exit(void)1110 static void __exit aem_exit(void)
1111 {
1112 struct aem_data *p1, *next1;
1113
1114 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
1115 driver_unregister(&aem_driver.driver);
1116 list_for_each_entry_safe(p1, next1, &driver_data.aem_devices, list)
1117 aem_delete(p1);
1118 }
1119
1120 MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1121 MODULE_DESCRIPTION("IBM AEM power/temp/energy sensor driver");
1122 MODULE_LICENSE("GPL");
1123
1124 module_init(aem_init);
1125 module_exit(aem_exit);
1126
1127 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
1128 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
1129 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
1130 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
1131 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
1132 MODULE_ALIAS("dmi:bvnIBM:*:pnIBM3850M2/x3950M2-*");
1133 MODULE_ALIAS("dmi:bvnIBM:*:pnIBMBladeHC10-*");
1134