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