blob: e74ae3682733df328b8b9f85631da133778bc9ee [file] [log] [blame]
Don Skidmore3ca8bc62012-04-12 00:33:31 +00001/*******************************************************************************
2
3 Intel 10 Gigabit PCI Express Linux driver
Don Skidmore434c5e32013-01-08 05:02:28 +00004 Copyright(c) 1999 - 2013 Intel Corporation.
Don Skidmore3ca8bc62012-04-12 00:33:31 +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 "ixgbe.h"
29#include "ixgbe_common.h"
30#include "ixgbe_type.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
Don Skidmore3ca8bc62012-04-12 00:33:31 +000040/* hwmon callback functions */
41static ssize_t ixgbe_hwmon_show_location(struct device *dev,
42 struct device_attribute *attr,
43 char *buf)
44{
45 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
46 dev_attr);
47 return sprintf(buf, "loc%u\n",
48 ixgbe_attr->sensor->location);
49}
50
51static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
52 struct device_attribute *attr,
53 char *buf)
54{
55 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
56 dev_attr);
57 unsigned int value;
58
59 /* reset the temp field */
60 ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
61
62 value = ixgbe_attr->sensor->temp;
63
64 /* display millidegree */
65 value *= 1000;
66
67 return sprintf(buf, "%u\n", value);
68}
69
70static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
71 struct device_attribute *attr,
72 char *buf)
73{
74 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
75 dev_attr);
76 unsigned int value = ixgbe_attr->sensor->caution_thresh;
77
78 /* display millidegree */
79 value *= 1000;
80
81 return sprintf(buf, "%u\n", value);
82}
83
84static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
85 struct device_attribute *attr,
86 char *buf)
87{
88 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
89 dev_attr);
90 unsigned int value = ixgbe_attr->sensor->max_op_thresh;
91
92 /* display millidegree */
93 value *= 1000;
94
95 return sprintf(buf, "%u\n", value);
96}
97
Ben Hutchings49ce9c22012-07-10 10:56:00 +000098/**
Don Skidmore3ca8bc62012-04-12 00:33:31 +000099 * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000100 * @adapter: pointer to the adapter structure
101 * @offset: offset in the eeprom sensor data table
102 * @type: type of sensor data to display
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000103 *
104 * For each file we want in hwmon's sysfs interface we need a device_attribute
105 * This is included in our hwmon_attr struct that contains the references to
106 * the data structures we need to get the data to display.
107 */
108static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
109 unsigned int offset, int type) {
110 int rc;
111 unsigned int n_attr;
112 struct hwmon_attr *ixgbe_attr;
113
Guenter Roeck03b77d82013-11-26 07:15:28 +0000114 n_attr = adapter->ixgbe_hwmon_buff->n_hwmon;
115 ixgbe_attr = &adapter->ixgbe_hwmon_buff->hwmon_list[n_attr];
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000116
117 switch (type) {
118 case IXGBE_HWMON_TYPE_LOC:
119 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
120 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000121 "temp%u_label", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000122 break;
123 case IXGBE_HWMON_TYPE_TEMP:
124 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
125 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000126 "temp%u_input", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000127 break;
128 case IXGBE_HWMON_TYPE_CAUTION:
129 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
130 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000131 "temp%u_max", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000132 break;
133 case IXGBE_HWMON_TYPE_MAX:
134 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
135 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000136 "temp%u_crit", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000137 break;
138 default:
139 rc = -EPERM;
140 return rc;
141 }
142
143 /* These always the same regardless of type */
144 ixgbe_attr->sensor =
145 &adapter->hw.mac.thermal_sensor_data.sensor[offset];
146 ixgbe_attr->hw = &adapter->hw;
147 ixgbe_attr->dev_attr.store = NULL;
148 ixgbe_attr->dev_attr.attr.mode = S_IRUGO;
149 ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
Guenter Roeck03b77d82013-11-26 07:15:28 +0000150 sysfs_attr_init(&ixgbe_attr->dev_attr.attr);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000151
Guenter Roeck03b77d82013-11-26 07:15:28 +0000152 adapter->ixgbe_hwmon_buff->attrs[n_attr] = &ixgbe_attr->dev_attr.attr;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000153
Guenter Roeck03b77d82013-11-26 07:15:28 +0000154 ++adapter->ixgbe_hwmon_buff->n_hwmon;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000155
Guenter Roeck03b77d82013-11-26 07:15:28 +0000156 return 0;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000157}
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000158
159static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
160{
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000161}
162
163/* called from ixgbe_main.c */
164void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
165{
166 ixgbe_sysfs_del_adapter(adapter);
167}
168
169/* called from ixgbe_main.c */
170int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
171{
Guenter Roeck03b77d82013-11-26 07:15:28 +0000172 struct hwmon_buff *ixgbe_hwmon;
173 struct device *hwmon_dev;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000174 unsigned int i;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000175 int rc = 0;
176
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000177 /* If this method isn't defined we don't support thermals */
178 if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
Don Skidmore12109822012-05-04 06:07:08 +0000179 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000180 }
181
182 /* Don't create thermal hwmon interface if no sensors present */
Don Skidmore12109822012-05-04 06:07:08 +0000183 if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
184 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000185
Guenter Roeck03b77d82013-11-26 07:15:28 +0000186 ixgbe_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*ixgbe_hwmon),
187 GFP_KERNEL);
188 if (ixgbe_hwmon == NULL) {
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000189 rc = -ENOMEM;
Guenter Roeck03b77d82013-11-26 07:15:28 +0000190 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000191 }
Guenter Roeck03b77d82013-11-26 07:15:28 +0000192 adapter->ixgbe_hwmon_buff = ixgbe_hwmon;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000193
194 for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
195 /*
196 * Only create hwmon sysfs entries for sensors that have
197 * meaningful data for.
198 */
199 if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
200 continue;
201
202 /* Bail if any hwmon attr struct fails to initialize */
203 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000204 if (rc)
Guenter Roeck03b77d82013-11-26 07:15:28 +0000205 goto exit;
206 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
207 if (rc)
208 goto exit;
209 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
210 if (rc)
211 goto exit;
212 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
213 if (rc)
214 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000215 }
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000216
Guenter Roeck03b77d82013-11-26 07:15:28 +0000217 ixgbe_hwmon->groups[0] = &ixgbe_hwmon->group;
218 ixgbe_hwmon->group.attrs = ixgbe_hwmon->attrs;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000219
Guenter Roeck03b77d82013-11-26 07:15:28 +0000220 hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
221 "ixgbe",
222 ixgbe_hwmon,
223 ixgbe_hwmon->groups);
224 if (IS_ERR(hwmon_dev))
225 rc = PTR_ERR(hwmon_dev);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000226exit:
227 return rc;
228}
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000229