blob: ef6df3d6437e3102224adef7033a3b26f058ef7e [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:
Jacob Kellerb89aae72014-02-22 01:23:50 +000023 Linux NICS <linux.nics@intel.com>
Don Skidmore3ca8bc62012-04-12 00:33:31 +000024 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27*******************************************************************************/
28
29#include "ixgbe.h"
30#include "ixgbe_common.h"
31#include "ixgbe_type.h"
32
33#include <linux/module.h>
34#include <linux/types.h>
35#include <linux/sysfs.h>
36#include <linux/kobject.h>
37#include <linux/device.h>
38#include <linux/netdevice.h>
39#include <linux/hwmon.h>
40
Don Skidmore3ca8bc62012-04-12 00:33:31 +000041/* hwmon callback functions */
42static ssize_t ixgbe_hwmon_show_location(struct device *dev,
43 struct device_attribute *attr,
44 char *buf)
45{
46 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
47 dev_attr);
48 return sprintf(buf, "loc%u\n",
49 ixgbe_attr->sensor->location);
50}
51
52static ssize_t ixgbe_hwmon_show_temp(struct device *dev,
53 struct device_attribute *attr,
54 char *buf)
55{
56 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
57 dev_attr);
58 unsigned int value;
59
60 /* reset the temp field */
61 ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw);
62
63 value = ixgbe_attr->sensor->temp;
64
65 /* display millidegree */
66 value *= 1000;
67
68 return sprintf(buf, "%u\n", value);
69}
70
71static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev,
72 struct device_attribute *attr,
73 char *buf)
74{
75 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
76 dev_attr);
77 unsigned int value = ixgbe_attr->sensor->caution_thresh;
78
79 /* display millidegree */
80 value *= 1000;
81
82 return sprintf(buf, "%u\n", value);
83}
84
85static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev,
86 struct device_attribute *attr,
87 char *buf)
88{
89 struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr,
90 dev_attr);
91 unsigned int value = ixgbe_attr->sensor->max_op_thresh;
92
93 /* display millidegree */
94 value *= 1000;
95
96 return sprintf(buf, "%u\n", value);
97}
98
Ben Hutchings49ce9c22012-07-10 10:56:00 +000099/**
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000100 * ixgbe_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000101 * @adapter: pointer to the adapter structure
102 * @offset: offset in the eeprom sensor data table
103 * @type: type of sensor data to display
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000104 *
105 * For each file we want in hwmon's sysfs interface we need a device_attribute
106 * This is included in our hwmon_attr struct that contains the references to
107 * the data structures we need to get the data to display.
108 */
109static int ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter,
110 unsigned int offset, int type) {
111 int rc;
112 unsigned int n_attr;
113 struct hwmon_attr *ixgbe_attr;
114
Guenter Roeck03b77d82013-11-26 07:15:28 +0000115 n_attr = adapter->ixgbe_hwmon_buff->n_hwmon;
116 ixgbe_attr = &adapter->ixgbe_hwmon_buff->hwmon_list[n_attr];
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000117
118 switch (type) {
119 case IXGBE_HWMON_TYPE_LOC:
120 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location;
121 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000122 "temp%u_label", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000123 break;
124 case IXGBE_HWMON_TYPE_TEMP:
125 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp;
126 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000127 "temp%u_input", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000128 break;
129 case IXGBE_HWMON_TYPE_CAUTION:
130 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh;
131 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000132 "temp%u_max", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000133 break;
134 case IXGBE_HWMON_TYPE_MAX:
135 ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh;
136 snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name),
Guenter Roeck22dd81c2013-11-26 07:15:39 +0000137 "temp%u_crit", offset + 1);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000138 break;
139 default:
140 rc = -EPERM;
141 return rc;
142 }
143
144 /* These always the same regardless of type */
145 ixgbe_attr->sensor =
146 &adapter->hw.mac.thermal_sensor_data.sensor[offset];
147 ixgbe_attr->hw = &adapter->hw;
148 ixgbe_attr->dev_attr.store = NULL;
149 ixgbe_attr->dev_attr.attr.mode = S_IRUGO;
150 ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name;
Guenter Roeck03b77d82013-11-26 07:15:28 +0000151 sysfs_attr_init(&ixgbe_attr->dev_attr.attr);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000152
Guenter Roeck03b77d82013-11-26 07:15:28 +0000153 adapter->ixgbe_hwmon_buff->attrs[n_attr] = &ixgbe_attr->dev_attr.attr;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000154
Guenter Roeck03b77d82013-11-26 07:15:28 +0000155 ++adapter->ixgbe_hwmon_buff->n_hwmon;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000156
Guenter Roeck03b77d82013-11-26 07:15:28 +0000157 return 0;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000158}
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000159
160static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter)
161{
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000162}
163
164/* called from ixgbe_main.c */
165void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter)
166{
167 ixgbe_sysfs_del_adapter(adapter);
168}
169
170/* called from ixgbe_main.c */
171int ixgbe_sysfs_init(struct ixgbe_adapter *adapter)
172{
Guenter Roeck03b77d82013-11-26 07:15:28 +0000173 struct hwmon_buff *ixgbe_hwmon;
174 struct device *hwmon_dev;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000175 unsigned int i;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000176 int rc = 0;
177
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000178 /* If this method isn't defined we don't support thermals */
179 if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) {
Don Skidmore12109822012-05-04 06:07:08 +0000180 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000181 }
182
183 /* Don't create thermal hwmon interface if no sensors present */
Don Skidmore12109822012-05-04 06:07:08 +0000184 if (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw))
185 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000186
Guenter Roeck03b77d82013-11-26 07:15:28 +0000187 ixgbe_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*ixgbe_hwmon),
188 GFP_KERNEL);
189 if (ixgbe_hwmon == NULL) {
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000190 rc = -ENOMEM;
Guenter Roeck03b77d82013-11-26 07:15:28 +0000191 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000192 }
Guenter Roeck03b77d82013-11-26 07:15:28 +0000193 adapter->ixgbe_hwmon_buff = ixgbe_hwmon;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000194
195 for (i = 0; i < IXGBE_MAX_SENSORS; i++) {
196 /*
197 * Only create hwmon sysfs entries for sensors that have
198 * meaningful data for.
199 */
200 if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
201 continue;
202
203 /* Bail if any hwmon attr struct fails to initialize */
204 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000205 if (rc)
Guenter Roeck03b77d82013-11-26 07:15:28 +0000206 goto exit;
207 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC);
208 if (rc)
209 goto exit;
210 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP);
211 if (rc)
212 goto exit;
213 rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX);
214 if (rc)
215 goto exit;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000216 }
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000217
Guenter Roeck03b77d82013-11-26 07:15:28 +0000218 ixgbe_hwmon->groups[0] = &ixgbe_hwmon->group;
219 ixgbe_hwmon->group.attrs = ixgbe_hwmon->attrs;
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000220
Guenter Roeck03b77d82013-11-26 07:15:28 +0000221 hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
222 "ixgbe",
223 ixgbe_hwmon,
224 ixgbe_hwmon->groups);
225 if (IS_ERR(hwmon_dev))
226 rc = PTR_ERR(hwmon_dev);
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000227exit:
228 return rc;
229}
Don Skidmore3ca8bc62012-04-12 00:33:31 +0000230