blob: e0af5bc616139bc6540577952175345053399dd1 [file] [log] [blame]
Carolyn Wybornye4288932012-12-07 03:01:42 +00001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Akeem G. Abodunrin4b9ea462013-01-08 18:31:12 +00004 Copyright(c) 2007-2013 Intel Corporation.
Carolyn Wybornye4288932012-12-07 03:01:42 +00005
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
Stephen Hemminger05ec29e2013-03-20 09:06:29 +000042static struct i2c_board_info i350_sensor_info = {
Carolyn Wyborny603e86f2013-02-20 07:40:55 +000043 I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
44};
45
Carolyn Wybornye4288932012-12-07 03:01:42 +000046/* hwmon callback functions */
47static ssize_t igb_hwmon_show_location(struct device *dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000048 struct device_attribute *attr,
49 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000050{
51 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000052 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000053 return sprintf(buf, "loc%u\n",
54 igb_attr->sensor->location);
55}
56
57static ssize_t igb_hwmon_show_temp(struct device *dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000058 struct device_attribute *attr,
59 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000060{
61 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000062 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000063 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,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000077 struct device_attribute *attr,
78 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000079{
80 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000081 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000082 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,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000091 struct device_attribute *attr,
92 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000093{
94 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000095 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000096 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,
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000114 unsigned int offset, int type)
115{
Carolyn Wybornye4288932012-12-07 03:01:42 +0000116 int rc;
117 unsigned int n_attr;
118 struct hwmon_attr *igb_attr;
119
Guenter Roecke3670b82013-11-26 07:15:23 +0000120 n_attr = adapter->igb_hwmon_buff->n_hwmon;
121 igb_attr = &adapter->igb_hwmon_buff->hwmon_list[n_attr];
Carolyn Wybornye4288932012-12-07 03:01:42 +0000122
123 switch (type) {
124 case IGB_HWMON_TYPE_LOC:
125 igb_attr->dev_attr.show = igb_hwmon_show_location;
126 snprintf(igb_attr->name, sizeof(igb_attr->name),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000127 "temp%u_label", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000128 break;
129 case IGB_HWMON_TYPE_TEMP:
130 igb_attr->dev_attr.show = igb_hwmon_show_temp;
131 snprintf(igb_attr->name, sizeof(igb_attr->name),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000132 "temp%u_input", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000133 break;
134 case IGB_HWMON_TYPE_CAUTION:
135 igb_attr->dev_attr.show = igb_hwmon_show_cautionthresh;
136 snprintf(igb_attr->name, sizeof(igb_attr->name),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000137 "temp%u_max", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000138 break;
139 case IGB_HWMON_TYPE_MAX:
140 igb_attr->dev_attr.show = igb_hwmon_show_maxopthresh;
141 snprintf(igb_attr->name, sizeof(igb_attr->name),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000142 "temp%u_crit", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000143 break;
144 default:
145 rc = -EPERM;
146 return rc;
147 }
148
149 /* These always the same regardless of type */
150 igb_attr->sensor =
151 &adapter->hw.mac.thermal_sensor_data.sensor[offset];
152 igb_attr->hw = &adapter->hw;
153 igb_attr->dev_attr.store = NULL;
154 igb_attr->dev_attr.attr.mode = S_IRUGO;
155 igb_attr->dev_attr.attr.name = igb_attr->name;
156 sysfs_attr_init(&igb_attr->dev_attr.attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000157
Guenter Roecke3670b82013-11-26 07:15:23 +0000158 adapter->igb_hwmon_buff->attrs[n_attr] = &igb_attr->dev_attr.attr;
159
160 ++adapter->igb_hwmon_buff->n_hwmon;
161
162 return 0;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000163}
164
165static void igb_sysfs_del_adapter(struct igb_adapter *adapter)
166{
Carolyn Wybornye4288932012-12-07 03:01:42 +0000167}
168
169/* called from igb_main.c */
170void igb_sysfs_exit(struct igb_adapter *adapter)
171{
172 igb_sysfs_del_adapter(adapter);
173}
174
175/* called from igb_main.c */
176int igb_sysfs_init(struct igb_adapter *adapter)
177{
Guenter Roecke3670b82013-11-26 07:15:23 +0000178 struct hwmon_buff *igb_hwmon;
179 struct i2c_client *client;
180 struct device *hwmon_dev;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000181 unsigned int i;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000182 int rc = 0;
183
184 /* If this method isn't defined we don't support thermals */
185 if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL)
186 goto exit;
187
188 /* Don't create thermal hwmon interface if no sensors present */
189 rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw));
Guenter Roecke3670b82013-11-26 07:15:23 +0000190 if (rc)
191 goto exit;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000192
Guenter Roecke3670b82013-11-26 07:15:23 +0000193 igb_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*igb_hwmon),
194 GFP_KERNEL);
195 if (!igb_hwmon) {
196 rc = -ENOMEM;
Carolyn Wyborny603e86f2013-02-20 07:40:55 +0000197 goto exit;
198 }
Guenter Roecke3670b82013-11-26 07:15:23 +0000199 adapter->igb_hwmon_buff = igb_hwmon;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000200
201 for (i = 0; i < E1000_MAX_SENSORS; i++) {
202
203 /* Only create hwmon sysfs entries for sensors that have
204 * meaningful data.
205 */
206 if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
207 continue;
208
209 /* Bail if any hwmon attr struct fails to initialize */
210 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000211 if (rc)
Guenter Roecke3670b82013-11-26 07:15:23 +0000212 goto exit;
213 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC);
214 if (rc)
215 goto exit;
216 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP);
217 if (rc)
218 goto exit;
219 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX);
220 if (rc)
221 goto exit;
222 }
223
224 /* init i2c_client */
225 client = i2c_new_device(&adapter->i2c_adap, &i350_sensor_info);
226 if (client == NULL) {
227 dev_info(&adapter->pdev->dev,
228 "Failed to create new i2c device.\n");
229 rc = -ENODEV;
230 goto exit;
231 }
232 adapter->i2c_client = client;
233
234 igb_hwmon->groups[0] = &igb_hwmon->group;
235 igb_hwmon->group.attrs = igb_hwmon->attrs;
236
237 hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
238 client->name,
239 igb_hwmon,
240 igb_hwmon->groups);
241 if (IS_ERR(hwmon_dev)) {
242 rc = PTR_ERR(hwmon_dev);
243 goto err;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000244 }
245
246 goto exit;
247
248err:
249 igb_sysfs_del_adapter(adapter);
250exit:
251 return rc;
252}
253#endif