Linux Audio

Check our new training course

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