Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * A hwmon driver for the IBM PowerExecutive temperature/power sensors
  3 * Copyright (C) 2007 IBM
  4 *
  5 * Author: Darrick J. Wong <darrick.wong@oracle.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation; either version 2 of the License, or
 10 * (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20 */
 21
 22#include <linux/ipmi.h>
 23#include <linux/module.h>
 24#include <linux/hwmon.h>
 25#include <linux/hwmon-sysfs.h>
 26#include <linux/jiffies.h>
 27#include <linux/mutex.h>
 28#include <linux/slab.h>
 29#include <linux/err.h>
 30
 31#define REFRESH_INTERVAL	(2 * HZ)
 32#define DRVNAME			"ibmpex"
 33
 34#define PEX_GET_VERSION		1
 35#define PEX_GET_SENSOR_COUNT	2
 36#define PEX_GET_SENSOR_NAME	3
 37#define PEX_RESET_HIGH_LOW	4
 38#define PEX_GET_SENSOR_DATA	6
 39
 40#define PEX_NET_FUNCTION	0x3A
 41#define PEX_COMMAND		0x3C
 42
 43static inline u16 extract_value(const char *data, int offset)
 44{
 45	return be16_to_cpup((__be16 *)&data[offset]);
 46}
 47
 48#define TEMP_SENSOR		1
 49#define POWER_SENSOR		2
 50
 51#define PEX_SENSOR_TYPE_LEN	3
 52static u8 const power_sensor_sig[] = {0x70, 0x77, 0x72};
 53static u8 const temp_sensor_sig[]  = {0x74, 0x65, 0x6D};
 54
 55#define PEX_MULT_LEN		2
 56static u8 const watt_sensor_sig[]  = {0x41, 0x43};
 57
 58#define PEX_NUM_SENSOR_FUNCS	3
 59static const char * const sensor_name_suffixes[] = {
 60	"",
 61	"_lowest",
 62	"_highest"
 
 
 
 
 
 63};
 64
 65static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
 66static void ibmpex_register_bmc(int iface, struct device *dev);
 67static void ibmpex_bmc_gone(int iface);
 68
 69struct ibmpex_sensor_data {
 70	int			in_use;
 71	s16			values[PEX_NUM_SENSOR_FUNCS];
 72	int			multiplier;
 73
 74	struct sensor_device_attribute_2	attr[PEX_NUM_SENSOR_FUNCS];
 75};
 76
 77struct ibmpex_bmc_data {
 78	struct list_head	list;
 79	struct device		*hwmon_dev;
 80	struct device		*bmc_device;
 81	struct mutex		lock;
 82	char			valid;
 83	unsigned long		last_updated;	/* In jiffies */
 84
 85	struct ipmi_addr	address;
 86	struct completion	read_complete;
 87	ipmi_user_t		user;
 88	int			interface;
 89
 90	struct kernel_ipmi_msg	tx_message;
 91	unsigned char		tx_msg_data[IPMI_MAX_MSG_LENGTH];
 92	long			tx_msgid;
 93
 94	unsigned char		rx_msg_data[IPMI_MAX_MSG_LENGTH];
 95	unsigned long		rx_msg_len;
 96	unsigned char		rx_result;
 97	int			rx_recv_type;
 98
 99	unsigned char		sensor_major;
100	unsigned char		sensor_minor;
101
102	unsigned char		num_sensors;
103	struct ibmpex_sensor_data	*sensors;
104};
105
106struct ibmpex_driver_data {
107	struct list_head	bmc_data;
108	struct ipmi_smi_watcher	bmc_events;
109	struct ipmi_user_hndl	ipmi_hndlrs;
110};
111
112static struct ibmpex_driver_data driver_data = {
113	.bmc_data = LIST_HEAD_INIT(driver_data.bmc_data),
114	.bmc_events = {
115		.owner = THIS_MODULE,
116		.new_smi = ibmpex_register_bmc,
117		.smi_gone = ibmpex_bmc_gone,
118	},
119	.ipmi_hndlrs = {
120		.ipmi_recv_hndl = ibmpex_msg_handler,
121	},
122};
123
124static int ibmpex_send_message(struct ibmpex_bmc_data *data)
125{
126	int err;
127
128	err = ipmi_validate_addr(&data->address, sizeof(data->address));
129	if (err)
130		goto out;
131
132	data->tx_msgid++;
133	err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
134				   &data->tx_message, data, 0, 0, 0);
135	if (err)
136		goto out1;
137
138	return 0;
139out1:
140	dev_err(data->bmc_device, "request_settime=%x\n", err);
141	return err;
142out:
143	dev_err(data->bmc_device, "validate_addr=%x\n", err);
144	return err;
145}
146
147static int ibmpex_ver_check(struct ibmpex_bmc_data *data)
148{
149	data->tx_msg_data[0] = PEX_GET_VERSION;
150	data->tx_message.data_len = 1;
151	ibmpex_send_message(data);
152
153	wait_for_completion(&data->read_complete);
154
155	if (data->rx_result || data->rx_msg_len != 6)
156		return -ENOENT;
157
158	data->sensor_major = data->rx_msg_data[0];
159	data->sensor_minor = data->rx_msg_data[1];
160
161	dev_info(data->bmc_device,
162		 "Found BMC with sensor interface v%d.%d %d-%02d-%02d on interface %d\n",
163		 data->sensor_major,
164		 data->sensor_minor,
165		 extract_value(data->rx_msg_data, 2),
166		 data->rx_msg_data[4],
167		 data->rx_msg_data[5],
168		 data->interface);
169
170	return 0;
171}
172
173static int ibmpex_query_sensor_count(struct ibmpex_bmc_data *data)
174{
175	data->tx_msg_data[0] = PEX_GET_SENSOR_COUNT;
176	data->tx_message.data_len = 1;
177	ibmpex_send_message(data);
178
179	wait_for_completion(&data->read_complete);
180
181	if (data->rx_result || data->rx_msg_len != 1)
182		return -ENOENT;
183
184	return data->rx_msg_data[0];
185}
186
187static int ibmpex_query_sensor_name(struct ibmpex_bmc_data *data, int sensor)
188{
189	data->tx_msg_data[0] = PEX_GET_SENSOR_NAME;
190	data->tx_msg_data[1] = sensor;
191	data->tx_message.data_len = 2;
192	ibmpex_send_message(data);
193
194	wait_for_completion(&data->read_complete);
195
196	if (data->rx_result || data->rx_msg_len < 1)
197		return -ENOENT;
198
199	return 0;
200}
201
202static int ibmpex_query_sensor_data(struct ibmpex_bmc_data *data, int sensor)
203{
204	data->tx_msg_data[0] = PEX_GET_SENSOR_DATA;
205	data->tx_msg_data[1] = sensor;
206	data->tx_message.data_len = 2;
207	ibmpex_send_message(data);
208
209	wait_for_completion(&data->read_complete);
210
211	if (data->rx_result || data->rx_msg_len < 26) {
212		dev_err(data->bmc_device, "Error reading sensor %d.\n",
213			sensor);
214		return -ENOENT;
215	}
216
217	return 0;
218}
219
220static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data *data)
221{
222	data->tx_msg_data[0] = PEX_RESET_HIGH_LOW;
223	data->tx_message.data_len = 1;
224	ibmpex_send_message(data);
225
226	wait_for_completion(&data->read_complete);
227
228	return 0;
229}
230
231static void ibmpex_update_device(struct ibmpex_bmc_data *data)
232{
233	int i, err;
234
235	mutex_lock(&data->lock);
236	if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
237	    data->valid)
238		goto out;
239
240	for (i = 0; i < data->num_sensors; i++) {
241		if (!data->sensors[i].in_use)
242			continue;
243		err = ibmpex_query_sensor_data(data, i);
244		if (err)
245			continue;
246		data->sensors[i].values[0] =
247			extract_value(data->rx_msg_data, 16);
248		data->sensors[i].values[1] =
249			extract_value(data->rx_msg_data, 18);
250		data->sensors[i].values[2] =
251			extract_value(data->rx_msg_data, 20);
252	}
253
254	data->last_updated = jiffies;
255	data->valid = 1;
256
257out:
258	mutex_unlock(&data->lock);
259}
260
261static struct ibmpex_bmc_data *get_bmc_data(int iface)
262{
263	struct ibmpex_bmc_data *p, *next;
264
265	list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
266		if (p->interface == iface)
267			return p;
268
269	return NULL;
270}
271
272static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
273			 char *buf)
274{
275	return sprintf(buf, "%s\n", DRVNAME);
276}
277static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
278
279static ssize_t ibmpex_show_sensor(struct device *dev,
280				  struct device_attribute *devattr,
281				  char *buf)
282{
283	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
284	struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
285	int mult = data->sensors[attr->index].multiplier;
286	ibmpex_update_device(data);
287
288	return sprintf(buf, "%d\n",
289		       data->sensors[attr->index].values[attr->nr] * mult);
290}
291
292static ssize_t ibmpex_reset_high_low(struct device *dev,
293				     struct device_attribute *devattr,
294				     const char *buf,
295				     size_t count)
296{
297	struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
298
299	ibmpex_reset_high_low_data(data);
300
301	return count;
302}
303
304static SENSOR_DEVICE_ATTR(reset_high_low, S_IWUSR, NULL,
305			  ibmpex_reset_high_low, 0);
306
307static int is_power_sensor(const char *sensor_id, int len)
308{
309	if (len < PEX_SENSOR_TYPE_LEN)
310		return 0;
311
312	if (!memcmp(sensor_id, power_sensor_sig, PEX_SENSOR_TYPE_LEN))
313		return 1;
314	return 0;
315}
316
317static int is_temp_sensor(const char *sensor_id, int len)
318{
319	if (len < PEX_SENSOR_TYPE_LEN)
320		return 0;
321
322	if (!memcmp(sensor_id, temp_sensor_sig, PEX_SENSOR_TYPE_LEN))
323		return 1;
324	return 0;
325}
326
327static int power_sensor_multiplier(struct ibmpex_bmc_data *data,
328				   const char *sensor_id, int len)
329{
330	int i;
331
332	if (data->sensor_major == 2)
333		return 1000000;
334
335	for (i = PEX_SENSOR_TYPE_LEN; i < len - 1; i++)
336		if (!memcmp(&sensor_id[i], watt_sensor_sig, PEX_MULT_LEN))
337			return 1000000;
338
339	return 100000;
340}
341
342static int create_sensor(struct ibmpex_bmc_data *data, int type,
343			 int counter, int sensor, int func)
344{
345	int err;
346	char *n;
347
348	n = kmalloc(32, GFP_KERNEL);
349	if (!n)
350		return -ENOMEM;
351
352	if (type == TEMP_SENSOR)
353		sprintf(n, "temp%d_input%s",
354			counter, sensor_name_suffixes[func]);
355	else if (type == POWER_SENSOR)
356		sprintf(n, "power%d_average%s",
357			counter, sensor_name_suffixes[func]);
358
359	sysfs_attr_init(&data->sensors[sensor].attr[func].dev_attr.attr);
360	data->sensors[sensor].attr[func].dev_attr.attr.name = n;
361	data->sensors[sensor].attr[func].dev_attr.attr.mode = S_IRUGO;
362	data->sensors[sensor].attr[func].dev_attr.show = ibmpex_show_sensor;
363	data->sensors[sensor].attr[func].index = sensor;
364	data->sensors[sensor].attr[func].nr = func;
365
366	err = device_create_file(data->bmc_device,
367				 &data->sensors[sensor].attr[func].dev_attr);
368	if (err) {
369		data->sensors[sensor].attr[func].dev_attr.attr.name = NULL;
370		kfree(n);
371		return err;
372	}
373
374	return 0;
375}
376
377static int ibmpex_find_sensors(struct ibmpex_bmc_data *data)
378{
379	int i, j, err;
380	int sensor_type;
381	int sensor_counter;
382	int num_power = 0;
383	int num_temp = 0;
384
385	err = ibmpex_query_sensor_count(data);
386	if (err <= 0)
387		return -ENOENT;
388	data->num_sensors = err;
389
390	data->sensors = kzalloc(data->num_sensors * sizeof(*data->sensors),
391				GFP_KERNEL);
392	if (!data->sensors)
393		return -ENOMEM;
394
395	for (i = 0; i < data->num_sensors; i++) {
396		err = ibmpex_query_sensor_name(data, i);
397		if (err)
398			continue;
399
400		if (is_power_sensor(data->rx_msg_data, data->rx_msg_len)) {
401			sensor_type = POWER_SENSOR;
402			num_power++;
403			sensor_counter = num_power;
404			data->sensors[i].multiplier =
405				power_sensor_multiplier(data,
406							data->rx_msg_data,
407							data->rx_msg_len);
408		} else if (is_temp_sensor(data->rx_msg_data,
409					  data->rx_msg_len)) {
410			sensor_type = TEMP_SENSOR;
411			num_temp++;
412			sensor_counter = num_temp;
413			data->sensors[i].multiplier = 1000;
414		} else
415			continue;
416
417		data->sensors[i].in_use = 1;
418
419		/* Create attributes */
420		for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
421			err = create_sensor(data, sensor_type, sensor_counter,
422					    i, j);
423			if (err)
424				goto exit_remove;
425		}
426	}
427
428	err = device_create_file(data->bmc_device,
429			&sensor_dev_attr_reset_high_low.dev_attr);
430	if (err)
431		goto exit_remove;
432
433	err = device_create_file(data->bmc_device,
434			&sensor_dev_attr_name.dev_attr);
435	if (err)
436		goto exit_remove;
437
438	return 0;
439
440exit_remove:
441	device_remove_file(data->bmc_device,
442			   &sensor_dev_attr_reset_high_low.dev_attr);
443	device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
444	for (i = 0; i < data->num_sensors; i++)
445		for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
446			if (!data->sensors[i].attr[j].dev_attr.attr.name)
447				continue;
448			device_remove_file(data->bmc_device,
449				&data->sensors[i].attr[j].dev_attr);
450			kfree(data->sensors[i].attr[j].dev_attr.attr.name);
451		}
452
453	kfree(data->sensors);
454	return err;
455}
456
457static void ibmpex_register_bmc(int iface, struct device *dev)
458{
459	struct ibmpex_bmc_data *data;
460	int err;
461
462	data = kzalloc(sizeof(*data), GFP_KERNEL);
463	if (!data)
 
464		return;
 
465
466	data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
467	data->address.channel = IPMI_BMC_CHANNEL;
468	data->address.data[0] = 0;
469	data->interface = iface;
470	data->bmc_device = dev;
471
472	/* Create IPMI messaging interface user */
473	err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
474			       data, &data->user);
475	if (err < 0) {
476		dev_err(dev,
477			"Unable to register user with IPMI interface %d\n",
478			data->interface);
479		goto out;
480	}
481
482	mutex_init(&data->lock);
483
484	/* Initialize message */
485	data->tx_msgid = 0;
486	init_completion(&data->read_complete);
487	data->tx_message.netfn = PEX_NET_FUNCTION;
488	data->tx_message.cmd = PEX_COMMAND;
489	data->tx_message.data = data->tx_msg_data;
490
491	/* Does this BMC support PowerExecutive? */
492	err = ibmpex_ver_check(data);
493	if (err)
494		goto out_user;
495
496	/* Register the BMC as a HWMON class device */
497	data->hwmon_dev = hwmon_device_register(data->bmc_device);
498
499	if (IS_ERR(data->hwmon_dev)) {
500		dev_err(data->bmc_device,
501			"Unable to register hwmon device for IPMI interface %d\n",
502			data->interface);
503		goto out_user;
504	}
505
506	/* finally add the new bmc data to the bmc data list */
507	dev_set_drvdata(dev, data);
508	list_add_tail(&data->list, &driver_data.bmc_data);
509
510	/* Now go find all the sensors */
511	err = ibmpex_find_sensors(data);
512	if (err) {
513		dev_err(data->bmc_device, "Error %d finding sensors\n", err);
514		goto out_register;
515	}
516
517	return;
518
519out_register:
520	hwmon_device_unregister(data->hwmon_dev);
521out_user:
522	ipmi_destroy_user(data->user);
523out:
524	kfree(data);
525}
526
527static void ibmpex_bmc_delete(struct ibmpex_bmc_data *data)
528{
529	int i, j;
530
531	device_remove_file(data->bmc_device,
532			   &sensor_dev_attr_reset_high_low.dev_attr);
533	device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
534	for (i = 0; i < data->num_sensors; i++)
535		for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
536			if (!data->sensors[i].attr[j].dev_attr.attr.name)
537				continue;
538			device_remove_file(data->bmc_device,
539				&data->sensors[i].attr[j].dev_attr);
540			kfree(data->sensors[i].attr[j].dev_attr.attr.name);
541		}
542
543	list_del(&data->list);
544	dev_set_drvdata(data->bmc_device, NULL);
545	hwmon_device_unregister(data->hwmon_dev);
546	ipmi_destroy_user(data->user);
547	kfree(data->sensors);
548	kfree(data);
549}
550
551static void ibmpex_bmc_gone(int iface)
552{
553	struct ibmpex_bmc_data *data = get_bmc_data(iface);
554
555	if (!data)
556		return;
557
558	ibmpex_bmc_delete(data);
559}
560
561static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
562{
563	struct ibmpex_bmc_data *data = (struct ibmpex_bmc_data *)user_msg_data;
564
565	if (msg->msgid != data->tx_msgid) {
566		dev_err(data->bmc_device,
567			"Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
568			(int)msg->msgid,
569			(int)data->tx_msgid);
570		ipmi_free_recv_msg(msg);
571		return;
572	}
573
574	data->rx_recv_type = msg->recv_type;
575	if (msg->msg.data_len > 0)
576		data->rx_result = msg->msg.data[0];
577	else
578		data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
579
580	if (msg->msg.data_len > 1) {
581		data->rx_msg_len = msg->msg.data_len - 1;
582		memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
583	} else
584		data->rx_msg_len = 0;
585
586	ipmi_free_recv_msg(msg);
587	complete(&data->read_complete);
588}
589
590static int __init ibmpex_init(void)
591{
592	return ipmi_smi_watcher_register(&driver_data.bmc_events);
593}
594
595static void __exit ibmpex_exit(void)
596{
597	struct ibmpex_bmc_data *p, *next;
598
599	ipmi_smi_watcher_unregister(&driver_data.bmc_events);
600	list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
601		ibmpex_bmc_delete(p);
602}
603
604MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
605MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
606MODULE_LICENSE("GPL");
607
608module_init(ibmpex_init);
609module_exit(ibmpex_exit);
610
611MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
612MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
613MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
614MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
615MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");
v3.15
  1/*
  2 * A hwmon driver for the IBM PowerExecutive temperature/power sensors
  3 * Copyright (C) 2007 IBM
  4 *
  5 * Author: Darrick J. Wong <darrick.wong@oracle.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License as published by
  9 * the Free Software Foundation; either version 2 of the License, or
 10 * (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20 */
 21
 22#include <linux/ipmi.h>
 23#include <linux/module.h>
 24#include <linux/hwmon.h>
 25#include <linux/hwmon-sysfs.h>
 26#include <linux/jiffies.h>
 27#include <linux/mutex.h>
 28#include <linux/slab.h>
 29#include <linux/err.h>
 30
 31#define REFRESH_INTERVAL	(2 * HZ)
 32#define DRVNAME			"ibmpex"
 33
 34#define PEX_GET_VERSION		1
 35#define PEX_GET_SENSOR_COUNT	2
 36#define PEX_GET_SENSOR_NAME	3
 37#define PEX_RESET_HIGH_LOW	4
 38#define PEX_GET_SENSOR_DATA	6
 39
 40#define PEX_NET_FUNCTION	0x3A
 41#define PEX_COMMAND		0x3C
 42
 43static inline u16 extract_value(const char *data, int offset)
 44{
 45	return be16_to_cpup((__be16 *)&data[offset]);
 46}
 47
 48#define TEMP_SENSOR		1
 49#define POWER_SENSOR		2
 50
 51#define PEX_SENSOR_TYPE_LEN	3
 52static u8 const power_sensor_sig[] = {0x70, 0x77, 0x72};
 53static u8 const temp_sensor_sig[]  = {0x74, 0x65, 0x6D};
 54
 55#define PEX_MULT_LEN		2
 56static u8 const watt_sensor_sig[]  = {0x41, 0x43};
 57
 58#define PEX_NUM_SENSOR_FUNCS	3
 59static char const * const power_sensor_name_templates[] = {
 60	"%s%d_average",
 61	"%s%d_average_lowest",
 62	"%s%d_average_highest"
 63};
 64static char const * const temp_sensor_name_templates[] = {
 65	"%s%d_input",
 66	"%s%d_input_lowest",
 67	"%s%d_input_highest"
 68};
 69
 70static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
 71static void ibmpex_register_bmc(int iface, struct device *dev);
 72static void ibmpex_bmc_gone(int iface);
 73
 74struct ibmpex_sensor_data {
 75	int			in_use;
 76	s16			values[PEX_NUM_SENSOR_FUNCS];
 77	int			multiplier;
 78
 79	struct sensor_device_attribute_2	attr[PEX_NUM_SENSOR_FUNCS];
 80};
 81
 82struct ibmpex_bmc_data {
 83	struct list_head	list;
 84	struct device		*hwmon_dev;
 85	struct device		*bmc_device;
 86	struct mutex		lock;
 87	char			valid;
 88	unsigned long		last_updated;	/* In jiffies */
 89
 90	struct ipmi_addr	address;
 91	struct completion	read_complete;
 92	ipmi_user_t		user;
 93	int			interface;
 94
 95	struct kernel_ipmi_msg	tx_message;
 96	unsigned char		tx_msg_data[IPMI_MAX_MSG_LENGTH];
 97	long			tx_msgid;
 98
 99	unsigned char		rx_msg_data[IPMI_MAX_MSG_LENGTH];
100	unsigned long		rx_msg_len;
101	unsigned char		rx_result;
102	int			rx_recv_type;
103
104	unsigned char		sensor_major;
105	unsigned char		sensor_minor;
106
107	unsigned char		num_sensors;
108	struct ibmpex_sensor_data	*sensors;
109};
110
111struct ibmpex_driver_data {
112	struct list_head	bmc_data;
113	struct ipmi_smi_watcher	bmc_events;
114	struct ipmi_user_hndl	ipmi_hndlrs;
115};
116
117static struct ibmpex_driver_data driver_data = {
118	.bmc_data = LIST_HEAD_INIT(driver_data.bmc_data),
119	.bmc_events = {
120		.owner = THIS_MODULE,
121		.new_smi = ibmpex_register_bmc,
122		.smi_gone = ibmpex_bmc_gone,
123	},
124	.ipmi_hndlrs = {
125		.ipmi_recv_hndl = ibmpex_msg_handler,
126	},
127};
128
129static int ibmpex_send_message(struct ibmpex_bmc_data *data)
130{
131	int err;
132
133	err = ipmi_validate_addr(&data->address, sizeof(data->address));
134	if (err)
135		goto out;
136
137	data->tx_msgid++;
138	err = ipmi_request_settime(data->user, &data->address, data->tx_msgid,
139				   &data->tx_message, data, 0, 0, 0);
140	if (err)
141		goto out1;
142
143	return 0;
144out1:
145	dev_err(data->bmc_device, "request_settime=%x\n", err);
146	return err;
147out:
148	dev_err(data->bmc_device, "validate_addr=%x\n", err);
149	return err;
150}
151
152static int ibmpex_ver_check(struct ibmpex_bmc_data *data)
153{
154	data->tx_msg_data[0] = PEX_GET_VERSION;
155	data->tx_message.data_len = 1;
156	ibmpex_send_message(data);
157
158	wait_for_completion(&data->read_complete);
159
160	if (data->rx_result || data->rx_msg_len != 6)
161		return -ENOENT;
162
163	data->sensor_major = data->rx_msg_data[0];
164	data->sensor_minor = data->rx_msg_data[1];
165
166	dev_info(data->bmc_device,
167		 "Found BMC with sensor interface v%d.%d %d-%02d-%02d on interface %d\n",
168		 data->sensor_major,
169		 data->sensor_minor,
170		 extract_value(data->rx_msg_data, 2),
171		 data->rx_msg_data[4],
172		 data->rx_msg_data[5],
173		 data->interface);
174
175	return 0;
176}
177
178static int ibmpex_query_sensor_count(struct ibmpex_bmc_data *data)
179{
180	data->tx_msg_data[0] = PEX_GET_SENSOR_COUNT;
181	data->tx_message.data_len = 1;
182	ibmpex_send_message(data);
183
184	wait_for_completion(&data->read_complete);
185
186	if (data->rx_result || data->rx_msg_len != 1)
187		return -ENOENT;
188
189	return data->rx_msg_data[0];
190}
191
192static int ibmpex_query_sensor_name(struct ibmpex_bmc_data *data, int sensor)
193{
194	data->tx_msg_data[0] = PEX_GET_SENSOR_NAME;
195	data->tx_msg_data[1] = sensor;
196	data->tx_message.data_len = 2;
197	ibmpex_send_message(data);
198
199	wait_for_completion(&data->read_complete);
200
201	if (data->rx_result || data->rx_msg_len < 1)
202		return -ENOENT;
203
204	return 0;
205}
206
207static int ibmpex_query_sensor_data(struct ibmpex_bmc_data *data, int sensor)
208{
209	data->tx_msg_data[0] = PEX_GET_SENSOR_DATA;
210	data->tx_msg_data[1] = sensor;
211	data->tx_message.data_len = 2;
212	ibmpex_send_message(data);
213
214	wait_for_completion(&data->read_complete);
215
216	if (data->rx_result || data->rx_msg_len < 26) {
217		dev_err(data->bmc_device, "Error reading sensor %d.\n",
218			sensor);
219		return -ENOENT;
220	}
221
222	return 0;
223}
224
225static int ibmpex_reset_high_low_data(struct ibmpex_bmc_data *data)
226{
227	data->tx_msg_data[0] = PEX_RESET_HIGH_LOW;
228	data->tx_message.data_len = 1;
229	ibmpex_send_message(data);
230
231	wait_for_completion(&data->read_complete);
232
233	return 0;
234}
235
236static void ibmpex_update_device(struct ibmpex_bmc_data *data)
237{
238	int i, err;
239
240	mutex_lock(&data->lock);
241	if (time_before(jiffies, data->last_updated + REFRESH_INTERVAL) &&
242	    data->valid)
243		goto out;
244
245	for (i = 0; i < data->num_sensors; i++) {
246		if (!data->sensors[i].in_use)
247			continue;
248		err = ibmpex_query_sensor_data(data, i);
249		if (err)
250			continue;
251		data->sensors[i].values[0] =
252			extract_value(data->rx_msg_data, 16);
253		data->sensors[i].values[1] =
254			extract_value(data->rx_msg_data, 18);
255		data->sensors[i].values[2] =
256			extract_value(data->rx_msg_data, 20);
257	}
258
259	data->last_updated = jiffies;
260	data->valid = 1;
261
262out:
263	mutex_unlock(&data->lock);
264}
265
266static struct ibmpex_bmc_data *get_bmc_data(int iface)
267{
268	struct ibmpex_bmc_data *p, *next;
269
270	list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
271		if (p->interface == iface)
272			return p;
273
274	return NULL;
275}
276
277static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
278			 char *buf)
279{
280	return sprintf(buf, "%s\n", DRVNAME);
281}
282static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
283
284static ssize_t ibmpex_show_sensor(struct device *dev,
285				  struct device_attribute *devattr,
286				  char *buf)
287{
288	struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
289	struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
290	int mult = data->sensors[attr->index].multiplier;
291	ibmpex_update_device(data);
292
293	return sprintf(buf, "%d\n",
294		       data->sensors[attr->index].values[attr->nr] * mult);
295}
296
297static ssize_t ibmpex_reset_high_low(struct device *dev,
298				     struct device_attribute *devattr,
299				     const char *buf,
300				     size_t count)
301{
302	struct ibmpex_bmc_data *data = dev_get_drvdata(dev);
303
304	ibmpex_reset_high_low_data(data);
305
306	return count;
307}
308
309static SENSOR_DEVICE_ATTR(reset_high_low, S_IWUSR, NULL,
310			  ibmpex_reset_high_low, 0);
311
312static int is_power_sensor(const char *sensor_id, int len)
313{
314	if (len < PEX_SENSOR_TYPE_LEN)
315		return 0;
316
317	if (!memcmp(sensor_id, power_sensor_sig, PEX_SENSOR_TYPE_LEN))
318		return 1;
319	return 0;
320}
321
322static int is_temp_sensor(const char *sensor_id, int len)
323{
324	if (len < PEX_SENSOR_TYPE_LEN)
325		return 0;
326
327	if (!memcmp(sensor_id, temp_sensor_sig, PEX_SENSOR_TYPE_LEN))
328		return 1;
329	return 0;
330}
331
332static int power_sensor_multiplier(struct ibmpex_bmc_data *data,
333				   const char *sensor_id, int len)
334{
335	int i;
336
337	if (data->sensor_major == 2)
338		return 1000000;
339
340	for (i = PEX_SENSOR_TYPE_LEN; i < len - 1; i++)
341		if (!memcmp(&sensor_id[i], watt_sensor_sig, PEX_MULT_LEN))
342			return 1000000;
343
344	return 100000;
345}
346
347static int create_sensor(struct ibmpex_bmc_data *data, int type,
348			 int counter, int sensor, int func)
349{
350	int err;
351	char *n;
352
353	n = kmalloc(32, GFP_KERNEL);
354	if (!n)
355		return -ENOMEM;
356
357	if (type == TEMP_SENSOR)
358		sprintf(n, temp_sensor_name_templates[func], "temp", counter);
 
359	else if (type == POWER_SENSOR)
360		sprintf(n, power_sensor_name_templates[func], "power", counter);
 
361
362	sysfs_attr_init(&data->sensors[sensor].attr[func].dev_attr.attr);
363	data->sensors[sensor].attr[func].dev_attr.attr.name = n;
364	data->sensors[sensor].attr[func].dev_attr.attr.mode = S_IRUGO;
365	data->sensors[sensor].attr[func].dev_attr.show = ibmpex_show_sensor;
366	data->sensors[sensor].attr[func].index = sensor;
367	data->sensors[sensor].attr[func].nr = func;
368
369	err = device_create_file(data->bmc_device,
370				 &data->sensors[sensor].attr[func].dev_attr);
371	if (err) {
372		data->sensors[sensor].attr[func].dev_attr.attr.name = NULL;
373		kfree(n);
374		return err;
375	}
376
377	return 0;
378}
379
380static int ibmpex_find_sensors(struct ibmpex_bmc_data *data)
381{
382	int i, j, err;
383	int sensor_type;
384	int sensor_counter;
385	int num_power = 0;
386	int num_temp = 0;
387
388	err = ibmpex_query_sensor_count(data);
389	if (err <= 0)
390		return -ENOENT;
391	data->num_sensors = err;
392
393	data->sensors = kzalloc(data->num_sensors * sizeof(*data->sensors),
394				GFP_KERNEL);
395	if (!data->sensors)
396		return -ENOMEM;
397
398	for (i = 0; i < data->num_sensors; i++) {
399		err = ibmpex_query_sensor_name(data, i);
400		if (err)
401			continue;
402
403		if (is_power_sensor(data->rx_msg_data, data->rx_msg_len)) {
404			sensor_type = POWER_SENSOR;
405			num_power++;
406			sensor_counter = num_power;
407			data->sensors[i].multiplier =
408				power_sensor_multiplier(data,
409							data->rx_msg_data,
410							data->rx_msg_len);
411		} else if (is_temp_sensor(data->rx_msg_data,
412					  data->rx_msg_len)) {
413			sensor_type = TEMP_SENSOR;
414			num_temp++;
415			sensor_counter = num_temp;
416			data->sensors[i].multiplier = 1000;
417		} else
418			continue;
419
420		data->sensors[i].in_use = 1;
421
422		/* Create attributes */
423		for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
424			err = create_sensor(data, sensor_type, sensor_counter,
425					    i, j);
426			if (err)
427				goto exit_remove;
428		}
429	}
430
431	err = device_create_file(data->bmc_device,
432			&sensor_dev_attr_reset_high_low.dev_attr);
433	if (err)
434		goto exit_remove;
435
436	err = device_create_file(data->bmc_device,
437			&sensor_dev_attr_name.dev_attr);
438	if (err)
439		goto exit_remove;
440
441	return 0;
442
443exit_remove:
444	device_remove_file(data->bmc_device,
445			   &sensor_dev_attr_reset_high_low.dev_attr);
446	device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
447	for (i = 0; i < data->num_sensors; i++)
448		for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
449			if (!data->sensors[i].attr[j].dev_attr.attr.name)
450				continue;
451			device_remove_file(data->bmc_device,
452				&data->sensors[i].attr[j].dev_attr);
453			kfree(data->sensors[i].attr[j].dev_attr.attr.name);
454		}
455
456	kfree(data->sensors);
457	return err;
458}
459
460static void ibmpex_register_bmc(int iface, struct device *dev)
461{
462	struct ibmpex_bmc_data *data;
463	int err;
464
465	data = kzalloc(sizeof(*data), GFP_KERNEL);
466	if (!data) {
467		dev_err(dev, "Insufficient memory for BMC interface.\n");
468		return;
469	}
470
471	data->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
472	data->address.channel = IPMI_BMC_CHANNEL;
473	data->address.data[0] = 0;
474	data->interface = iface;
475	data->bmc_device = dev;
476
477	/* Create IPMI messaging interface user */
478	err = ipmi_create_user(data->interface, &driver_data.ipmi_hndlrs,
479			       data, &data->user);
480	if (err < 0) {
481		dev_err(dev,
482			"Unable to register user with IPMI interface %d\n",
483			data->interface);
484		goto out;
485	}
486
487	mutex_init(&data->lock);
488
489	/* Initialize message */
490	data->tx_msgid = 0;
491	init_completion(&data->read_complete);
492	data->tx_message.netfn = PEX_NET_FUNCTION;
493	data->tx_message.cmd = PEX_COMMAND;
494	data->tx_message.data = data->tx_msg_data;
495
496	/* Does this BMC support PowerExecutive? */
497	err = ibmpex_ver_check(data);
498	if (err)
499		goto out_user;
500
501	/* Register the BMC as a HWMON class device */
502	data->hwmon_dev = hwmon_device_register(data->bmc_device);
503
504	if (IS_ERR(data->hwmon_dev)) {
505		dev_err(data->bmc_device,
506			"Unable to register hwmon device for IPMI interface %d\n",
507			data->interface);
508		goto out_user;
509	}
510
511	/* finally add the new bmc data to the bmc data list */
512	dev_set_drvdata(dev, data);
513	list_add_tail(&data->list, &driver_data.bmc_data);
514
515	/* Now go find all the sensors */
516	err = ibmpex_find_sensors(data);
517	if (err) {
518		dev_err(data->bmc_device, "Error %d finding sensors\n", err);
519		goto out_register;
520	}
521
522	return;
523
524out_register:
525	hwmon_device_unregister(data->hwmon_dev);
526out_user:
527	ipmi_destroy_user(data->user);
528out:
529	kfree(data);
530}
531
532static void ibmpex_bmc_delete(struct ibmpex_bmc_data *data)
533{
534	int i, j;
535
536	device_remove_file(data->bmc_device,
537			   &sensor_dev_attr_reset_high_low.dev_attr);
538	device_remove_file(data->bmc_device, &sensor_dev_attr_name.dev_attr);
539	for (i = 0; i < data->num_sensors; i++)
540		for (j = 0; j < PEX_NUM_SENSOR_FUNCS; j++) {
541			if (!data->sensors[i].attr[j].dev_attr.attr.name)
542				continue;
543			device_remove_file(data->bmc_device,
544				&data->sensors[i].attr[j].dev_attr);
545			kfree(data->sensors[i].attr[j].dev_attr.attr.name);
546		}
547
548	list_del(&data->list);
549	dev_set_drvdata(data->bmc_device, NULL);
550	hwmon_device_unregister(data->hwmon_dev);
551	ipmi_destroy_user(data->user);
552	kfree(data->sensors);
553	kfree(data);
554}
555
556static void ibmpex_bmc_gone(int iface)
557{
558	struct ibmpex_bmc_data *data = get_bmc_data(iface);
559
560	if (!data)
561		return;
562
563	ibmpex_bmc_delete(data);
564}
565
566static void ibmpex_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
567{
568	struct ibmpex_bmc_data *data = (struct ibmpex_bmc_data *)user_msg_data;
569
570	if (msg->msgid != data->tx_msgid) {
571		dev_err(data->bmc_device,
572			"Mismatch between received msgid (%02x) and transmitted msgid (%02x)!\n",
573			(int)msg->msgid,
574			(int)data->tx_msgid);
575		ipmi_free_recv_msg(msg);
576		return;
577	}
578
579	data->rx_recv_type = msg->recv_type;
580	if (msg->msg.data_len > 0)
581		data->rx_result = msg->msg.data[0];
582	else
583		data->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE;
584
585	if (msg->msg.data_len > 1) {
586		data->rx_msg_len = msg->msg.data_len - 1;
587		memcpy(data->rx_msg_data, msg->msg.data + 1, data->rx_msg_len);
588	} else
589		data->rx_msg_len = 0;
590
591	ipmi_free_recv_msg(msg);
592	complete(&data->read_complete);
593}
594
595static int __init ibmpex_init(void)
596{
597	return ipmi_smi_watcher_register(&driver_data.bmc_events);
598}
599
600static void __exit ibmpex_exit(void)
601{
602	struct ibmpex_bmc_data *p, *next;
603
604	ipmi_smi_watcher_unregister(&driver_data.bmc_events);
605	list_for_each_entry_safe(p, next, &driver_data.bmc_data, list)
606		ibmpex_bmc_delete(p);
607}
608
609MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
610MODULE_DESCRIPTION("IBM PowerExecutive power/temperature sensor driver");
611MODULE_LICENSE("GPL");
612
613module_init(ibmpex_init);
614module_exit(ibmpex_exit);
615
616MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3350-*");
617MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3550-*");
618MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3650-*");
619MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3655-*");
620MODULE_ALIAS("dmi:bvnIBM:*:pnIBMSystemx3755-*");