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