[go: nahoru, domu]

igb_hwmon.c revision 05ec29e8fa9b6ec8d4ad5d2f6d5fc5467c7970bc
1/*******************************************************************************
2
3  Intel(R) Gigabit Ethernet Linux driver
4  Copyright(c) 2007-2013 Intel Corporation.
5
6  This program is free software; you can redistribute it and/or modify it
7  under the terms and conditions of the GNU General Public License,
8  version 2, as published by the Free Software Foundation.
9
10  This program is distributed in the hope it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  more details.
14
15  You should have received a copy of the GNU General Public License along with
16  this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19  The full GNU General Public License is included in this distribution in
20  the file called "COPYING".
21
22  Contact Information:
23  e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24  Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
28#include "igb.h"
29#include "e1000_82575.h"
30#include "e1000_hw.h"
31
32#include <linux/module.h>
33#include <linux/types.h>
34#include <linux/sysfs.h>
35#include <linux/kobject.h>
36#include <linux/device.h>
37#include <linux/netdevice.h>
38#include <linux/hwmon.h>
39#include <linux/pci.h>
40
41#ifdef CONFIG_IGB_HWMON
42static struct i2c_board_info i350_sensor_info = {
43	I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
44};
45
46/* hwmon callback functions */
47static ssize_t igb_hwmon_show_location(struct device *dev,
48					 struct device_attribute *attr,
49					 char *buf)
50{
51	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
52						     dev_attr);
53	return sprintf(buf, "loc%u\n",
54		       igb_attr->sensor->location);
55}
56
57static ssize_t igb_hwmon_show_temp(struct device *dev,
58				     struct device_attribute *attr,
59				     char *buf)
60{
61	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
62						     dev_attr);
63	unsigned int value;
64
65	/* reset the temp field */
66	igb_attr->hw->mac.ops.get_thermal_sensor_data(igb_attr->hw);
67
68	value = igb_attr->sensor->temp;
69
70	/* display millidegree */
71	value *= 1000;
72
73	return sprintf(buf, "%u\n", value);
74}
75
76static ssize_t igb_hwmon_show_cautionthresh(struct device *dev,
77				     struct device_attribute *attr,
78				     char *buf)
79{
80	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
81						     dev_attr);
82	unsigned int value = igb_attr->sensor->caution_thresh;
83
84	/* display millidegree */
85	value *= 1000;
86
87	return sprintf(buf, "%u\n", value);
88}
89
90static ssize_t igb_hwmon_show_maxopthresh(struct device *dev,
91				     struct device_attribute *attr,
92				     char *buf)
93{
94	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
95						     dev_attr);
96	unsigned int value = igb_attr->sensor->max_op_thresh;
97
98	/* display millidegree */
99	value *= 1000;
100
101	return sprintf(buf, "%u\n", value);
102}
103
104/* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
105 * @ adapter: pointer to the adapter structure
106 * @ offset: offset in the eeprom sensor data table
107 * @ type: type of sensor data to display
108 *
109 * For each file we want in hwmon's sysfs interface we need a device_attribute
110 * This is included in our hwmon_attr struct that contains the references to
111 * the data structures we need to get the data to display.
112 */
113static int igb_add_hwmon_attr(struct igb_adapter *adapter,
114				unsigned int offset, int type) {
115	int rc;
116	unsigned int n_attr;
117	struct hwmon_attr *igb_attr;
118
119	n_attr = adapter->igb_hwmon_buff.n_hwmon;
120	igb_attr = &adapter->igb_hwmon_buff.hwmon_list[n_attr];
121
122	switch (type) {
123	case IGB_HWMON_TYPE_LOC:
124		igb_attr->dev_attr.show = igb_hwmon_show_location;
125		snprintf(igb_attr->name, sizeof(igb_attr->name),
126			 "temp%u_label", offset);
127		break;
128	case IGB_HWMON_TYPE_TEMP:
129		igb_attr->dev_attr.show = igb_hwmon_show_temp;
130		snprintf(igb_attr->name, sizeof(igb_attr->name),
131			 "temp%u_input", offset);
132		break;
133	case IGB_HWMON_TYPE_CAUTION:
134		igb_attr->dev_attr.show = igb_hwmon_show_cautionthresh;
135		snprintf(igb_attr->name, sizeof(igb_attr->name),
136			 "temp%u_max", offset);
137		break;
138	case IGB_HWMON_TYPE_MAX:
139		igb_attr->dev_attr.show = igb_hwmon_show_maxopthresh;
140		snprintf(igb_attr->name, sizeof(igb_attr->name),
141			 "temp%u_crit", offset);
142		break;
143	default:
144		rc = -EPERM;
145		return rc;
146	}
147
148	/* These always the same regardless of type */
149	igb_attr->sensor =
150		&adapter->hw.mac.thermal_sensor_data.sensor[offset];
151	igb_attr->hw = &adapter->hw;
152	igb_attr->dev_attr.store = NULL;
153	igb_attr->dev_attr.attr.mode = S_IRUGO;
154	igb_attr->dev_attr.attr.name = igb_attr->name;
155	sysfs_attr_init(&igb_attr->dev_attr.attr);
156	rc = device_create_file(&adapter->pdev->dev,
157				&igb_attr->dev_attr);
158	if (rc == 0)
159		++adapter->igb_hwmon_buff.n_hwmon;
160
161	return rc;
162}
163
164static void igb_sysfs_del_adapter(struct igb_adapter *adapter)
165{
166	int i;
167
168	if (adapter == NULL)
169		return;
170
171	for (i = 0; i < adapter->igb_hwmon_buff.n_hwmon; i++) {
172		device_remove_file(&adapter->pdev->dev,
173			   &adapter->igb_hwmon_buff.hwmon_list[i].dev_attr);
174	}
175
176	kfree(adapter->igb_hwmon_buff.hwmon_list);
177
178	if (adapter->igb_hwmon_buff.device)
179		hwmon_device_unregister(adapter->igb_hwmon_buff.device);
180}
181
182/* called from igb_main.c */
183void igb_sysfs_exit(struct igb_adapter *adapter)
184{
185	igb_sysfs_del_adapter(adapter);
186}
187
188/* called from igb_main.c */
189int igb_sysfs_init(struct igb_adapter *adapter)
190{
191	struct hwmon_buff *igb_hwmon = &adapter->igb_hwmon_buff;
192	unsigned int i;
193	int n_attrs;
194	int rc = 0;
195	struct i2c_client *client = NULL;
196
197	/* If this method isn't defined we don't support thermals */
198	if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL)
199		goto exit;
200
201	/* Don't create thermal hwmon interface if no sensors present */
202	rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw));
203		if (rc)
204			goto exit;
205
206	/* init i2c_client */
207	client = i2c_new_device(&adapter->i2c_adap, &i350_sensor_info);
208	if (client == NULL) {
209		dev_info(&adapter->pdev->dev,
210			"Failed to create new i2c device..\n");
211		goto exit;
212	}
213	adapter->i2c_client = client;
214
215	/* Allocation space for max attributes
216	 * max num sensors * values (loc, temp, max, caution)
217	 */
218	n_attrs = E1000_MAX_SENSORS * 4;
219	igb_hwmon->hwmon_list = kcalloc(n_attrs, sizeof(struct hwmon_attr),
220					  GFP_KERNEL);
221	if (!igb_hwmon->hwmon_list) {
222		rc = -ENOMEM;
223		goto err;
224	}
225
226	igb_hwmon->device = hwmon_device_register(&adapter->pdev->dev);
227	if (IS_ERR(igb_hwmon->device)) {
228		rc = PTR_ERR(igb_hwmon->device);
229		goto err;
230	}
231
232	for (i = 0; i < E1000_MAX_SENSORS; i++) {
233
234		/* Only create hwmon sysfs entries for sensors that have
235		 * meaningful data.
236		 */
237		if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
238			continue;
239
240		/* Bail if any hwmon attr struct fails to initialize */
241		rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION);
242		rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC);
243		rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP);
244		rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX);
245		if (rc)
246			goto err;
247	}
248
249	goto exit;
250
251err:
252	igb_sysfs_del_adapter(adapter);
253exit:
254	return rc;
255}
256#endif
257