blob: 8333f67acf96b3a6e83746c1cedd1f0eff7636ec [file] [log] [blame]
Carolyn Wybornye4288932012-12-07 03:01:42 +00001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Carolyn Wyborny74cfb2e2014-02-25 17:58:57 -08004 Copyright(c) 2007-2014 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
Carolyn Wyborny74cfb2e2014-02-25 17:58:57 -080016 this program; if not, see <http://www.gnu.org/licenses/>.
Carolyn Wybornye4288932012-12-07 03:01:42 +000017
18 The full GNU General Public License is included in this distribution in
19 the file called "COPYING".
20
21 Contact Information:
22 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25*******************************************************************************/
26
27#include "igb.h"
28#include "e1000_82575.h"
29#include "e1000_hw.h"
30
31#include <linux/module.h>
32#include <linux/types.h>
33#include <linux/sysfs.h>
34#include <linux/kobject.h>
35#include <linux/device.h>
36#include <linux/netdevice.h>
37#include <linux/hwmon.h>
38#include <linux/pci.h>
39
40#ifdef CONFIG_IGB_HWMON
Stephen Hemminger05ec29e2013-03-20 09:06:29 +000041static struct i2c_board_info i350_sensor_info = {
Carolyn Wyborny603e86f2013-02-20 07:40:55 +000042 I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
43};
44
Carolyn Wybornye4288932012-12-07 03:01:42 +000045/* hwmon callback functions */
46static ssize_t igb_hwmon_show_location(struct device *dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000047 struct device_attribute *attr,
48 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000049{
50 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000051 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000052 return sprintf(buf, "loc%u\n",
53 igb_attr->sensor->location);
54}
55
56static ssize_t igb_hwmon_show_temp(struct device *dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000057 struct device_attribute *attr,
58 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000059{
60 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000061 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000062 unsigned int value;
63
64 /* reset the temp field */
65 igb_attr->hw->mac.ops.get_thermal_sensor_data(igb_attr->hw);
66
67 value = igb_attr->sensor->temp;
68
69 /* display millidegree */
70 value *= 1000;
71
72 return sprintf(buf, "%u\n", value);
73}
74
75static ssize_t igb_hwmon_show_cautionthresh(struct device *dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000076 struct device_attribute *attr,
77 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000078{
79 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000080 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000081 unsigned int value = igb_attr->sensor->caution_thresh;
82
83 /* display millidegree */
84 value *= 1000;
85
86 return sprintf(buf, "%u\n", value);
87}
88
89static ssize_t igb_hwmon_show_maxopthresh(struct device *dev,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000090 struct device_attribute *attr,
91 char *buf)
Carolyn Wybornye4288932012-12-07 03:01:42 +000092{
93 struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
Jeff Kirsherb980ac12013-02-23 07:29:56 +000094 dev_attr);
Carolyn Wybornye4288932012-12-07 03:01:42 +000095 unsigned int value = igb_attr->sensor->max_op_thresh;
96
97 /* display millidegree */
98 value *= 1000;
99
100 return sprintf(buf, "%u\n", value);
101}
102
103/* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
104 * @ adapter: pointer to the adapter structure
105 * @ offset: offset in the eeprom sensor data table
106 * @ type: type of sensor data to display
107 *
108 * For each file we want in hwmon's sysfs interface we need a device_attribute
109 * This is included in our hwmon_attr struct that contains the references to
110 * the data structures we need to get the data to display.
111 */
112static int igb_add_hwmon_attr(struct igb_adapter *adapter,
Jeff Kirsherb980ac12013-02-23 07:29:56 +0000113 unsigned int offset, int type)
114{
Carolyn Wybornye4288932012-12-07 03:01:42 +0000115 int rc;
116 unsigned int n_attr;
117 struct hwmon_attr *igb_attr;
118
Guenter Roecke3670b82013-11-26 07:15:23 +0000119 n_attr = adapter->igb_hwmon_buff->n_hwmon;
120 igb_attr = &adapter->igb_hwmon_buff->hwmon_list[n_attr];
Carolyn Wybornye4288932012-12-07 03:01:42 +0000121
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),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000126 "temp%u_label", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000127 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),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000131 "temp%u_input", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000132 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),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000136 "temp%u_max", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000137 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),
Guenter Roecke6e25bb2013-11-26 07:15:34 +0000141 "temp%u_crit", offset + 1);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000142 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);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000156
Guenter Roecke3670b82013-11-26 07:15:23 +0000157 adapter->igb_hwmon_buff->attrs[n_attr] = &igb_attr->dev_attr.attr;
158
159 ++adapter->igb_hwmon_buff->n_hwmon;
160
161 return 0;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000162}
163
164static void igb_sysfs_del_adapter(struct igb_adapter *adapter)
165{
Carolyn Wybornye4288932012-12-07 03:01:42 +0000166}
167
168/* called from igb_main.c */
169void igb_sysfs_exit(struct igb_adapter *adapter)
170{
171 igb_sysfs_del_adapter(adapter);
172}
173
174/* called from igb_main.c */
175int igb_sysfs_init(struct igb_adapter *adapter)
176{
Guenter Roecke3670b82013-11-26 07:15:23 +0000177 struct hwmon_buff *igb_hwmon;
178 struct i2c_client *client;
179 struct device *hwmon_dev;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000180 unsigned int i;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000181 int rc = 0;
182
183 /* If this method isn't defined we don't support thermals */
184 if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL)
185 goto exit;
186
187 /* Don't create thermal hwmon interface if no sensors present */
188 rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw));
Guenter Roecke3670b82013-11-26 07:15:23 +0000189 if (rc)
190 goto exit;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000191
Guenter Roecke3670b82013-11-26 07:15:23 +0000192 igb_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*igb_hwmon),
193 GFP_KERNEL);
194 if (!igb_hwmon) {
195 rc = -ENOMEM;
Carolyn Wyborny603e86f2013-02-20 07:40:55 +0000196 goto exit;
197 }
Guenter Roecke3670b82013-11-26 07:15:23 +0000198 adapter->igb_hwmon_buff = igb_hwmon;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000199
200 for (i = 0; i < E1000_MAX_SENSORS; i++) {
201
202 /* Only create hwmon sysfs entries for sensors that have
203 * meaningful data.
204 */
205 if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
206 continue;
207
208 /* Bail if any hwmon attr struct fails to initialize */
209 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION);
Carolyn Wybornye4288932012-12-07 03:01:42 +0000210 if (rc)
Guenter Roecke3670b82013-11-26 07:15:23 +0000211 goto exit;
212 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC);
213 if (rc)
214 goto exit;
215 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP);
216 if (rc)
217 goto exit;
218 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX);
219 if (rc)
220 goto exit;
221 }
222
223 /* init i2c_client */
224 client = i2c_new_device(&adapter->i2c_adap, &i350_sensor_info);
225 if (client == NULL) {
226 dev_info(&adapter->pdev->dev,
227 "Failed to create new i2c device.\n");
228 rc = -ENODEV;
229 goto exit;
230 }
231 adapter->i2c_client = client;
232
233 igb_hwmon->groups[0] = &igb_hwmon->group;
234 igb_hwmon->group.attrs = igb_hwmon->attrs;
235
236 hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
237 client->name,
238 igb_hwmon,
239 igb_hwmon->groups);
240 if (IS_ERR(hwmon_dev)) {
241 rc = PTR_ERR(hwmon_dev);
242 goto err;
Carolyn Wybornye4288932012-12-07 03:01:42 +0000243 }
244
245 goto exit;
246
247err:
248 igb_sysfs_del_adapter(adapter);
249exit:
250 return rc;
251}
252#endif