Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / linuxapp / kni / ethtool / igb / igb_hwmon.c
1 /*******************************************************************************
2
3   Intel(R) Gigabit Ethernet Linux driver
4   Copyright(c) 2007-2013 Intel Corporation.
5
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 #ifdef IGB_HWMON
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 HAVE_I2C_SUPPORT
42 static struct i2c_board_info i350_sensor_info = {
43         I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
44 };
45 #endif /* HAVE_I2C_SUPPORT */
46
47 /* hwmon callback functions */
48 static ssize_t igb_hwmon_show_location(struct device *dev,
49                                          struct device_attribute *attr,
50                                          char *buf)
51 {
52         struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
53                                                      dev_attr);
54         return sprintf(buf, "loc%u\n",
55                        igb_attr->sensor->location);
56 }
57
58 static ssize_t igb_hwmon_show_temp(struct device *dev,
59                                      struct device_attribute *attr,
60                                      char *buf)
61 {
62         struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
63                                                      dev_attr);
64         unsigned int value;
65
66         /* reset the temp field */
67         igb_attr->hw->mac.ops.get_thermal_sensor_data(igb_attr->hw);
68
69         value = igb_attr->sensor->temp;
70
71         /* display millidegree */
72         value *= 1000;
73
74         return sprintf(buf, "%u\n", value);
75 }
76
77 static ssize_t igb_hwmon_show_cautionthresh(struct device *dev,
78                                      struct device_attribute *attr,
79                                      char *buf)
80 {
81         struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
82                                                      dev_attr);
83         unsigned int value = igb_attr->sensor->caution_thresh;
84
85         /* display millidegree */
86         value *= 1000;
87
88         return sprintf(buf, "%u\n", value);
89 }
90
91 static ssize_t igb_hwmon_show_maxopthresh(struct device *dev,
92                                      struct device_attribute *attr,
93                                      char *buf)
94 {
95         struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
96                                                      dev_attr);
97         unsigned int value = igb_attr->sensor->max_op_thresh;
98
99         /* display millidegree */
100         value *= 1000;
101
102         return sprintf(buf, "%u\n", value);
103 }
104
105 /* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
106  * @ adapter: pointer to the adapter structure
107  * @ offset: offset in the eeprom sensor data table
108  * @ type: type of sensor data to display
109  *
110  * For each file we want in hwmon's sysfs interface we need a device_attribute
111  * This is included in our hwmon_attr struct that contains the references to
112  * the data structures we need to get the data to display.
113  */
114 static int igb_add_hwmon_attr(struct igb_adapter *adapter,
115                                 unsigned int offset, int type) {
116         int rc;
117         unsigned int n_attr;
118         struct hwmon_attr *igb_attr;
119
120         n_attr = adapter->igb_hwmon_buff.n_hwmon;
121         igb_attr = &adapter->igb_hwmon_buff.hwmon_list[n_attr];
122
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),
127                          "temp%u_label", offset);
128                 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),
132                          "temp%u_input", offset);
133                 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),
137                          "temp%u_max", offset);
138                 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),
142                          "temp%u_crit", offset);
143                 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);
157         rc = device_create_file(&adapter->pdev->dev,
158                                 &igb_attr->dev_attr);
159         if (rc == 0)
160                 ++adapter->igb_hwmon_buff.n_hwmon;
161
162         return rc;
163 }
164
165 static void igb_sysfs_del_adapter(struct igb_adapter *adapter)
166 {
167         int i;
168
169         if (adapter == NULL)
170                 return;
171
172         for (i = 0; i < adapter->igb_hwmon_buff.n_hwmon; i++) {
173                 device_remove_file(&adapter->pdev->dev,
174                            &adapter->igb_hwmon_buff.hwmon_list[i].dev_attr);
175         }
176
177         kfree(adapter->igb_hwmon_buff.hwmon_list);
178
179         if (adapter->igb_hwmon_buff.device)
180                 hwmon_device_unregister(adapter->igb_hwmon_buff.device);
181 }
182
183 /* called from igb_main.c */
184 void igb_sysfs_exit(struct igb_adapter *adapter)
185 {
186         igb_sysfs_del_adapter(adapter);
187 }
188
189 /* called from igb_main.c */
190 int igb_sysfs_init(struct igb_adapter *adapter)
191 {
192         struct hwmon_buff *igb_hwmon = &adapter->igb_hwmon_buff;
193         unsigned int i;
194         int n_attrs;
195         int rc = 0;
196 #ifdef HAVE_I2C_SUPPORT
197         struct i2c_client *client = NULL;
198 #endif /* HAVE_I2C_SUPPORT */
199
200         /* If this method isn't defined we don't support thermals */
201         if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL)
202                 goto exit;
203
204         /* Don't create thermal hwmon interface if no sensors present */
205         rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw));
206                 if (rc)
207                         goto exit;
208 #ifdef HAVE_I2C_SUPPORT
209         /* init i2c_client */
210         client = i2c_new_device(&adapter->i2c_adap, &i350_sensor_info);
211         if (client == NULL) {
212                 dev_info(&adapter->pdev->dev,
213                         "Failed to create new i2c device..\n");
214                 goto exit;
215         }
216         adapter->i2c_client = client;
217 #endif /* HAVE_I2C_SUPPORT */
218
219         /* Allocation space for max attributes
220          * max num sensors * values (loc, temp, max, caution)
221          */
222         n_attrs = E1000_MAX_SENSORS * 4;
223         igb_hwmon->hwmon_list = kcalloc(n_attrs, sizeof(struct hwmon_attr),
224                                           GFP_KERNEL);
225         if (!igb_hwmon->hwmon_list) {
226                 rc = -ENOMEM;
227                 goto err;
228         }
229
230         igb_hwmon->device = hwmon_device_register(&adapter->pdev->dev);
231         if (IS_ERR(igb_hwmon->device)) {
232                 rc = PTR_ERR(igb_hwmon->device);
233                 goto err;
234         }
235
236         for (i = 0; i < E1000_MAX_SENSORS; i++) {
237
238                 /* Only create hwmon sysfs entries for sensors that have
239                  * meaningful data.
240                  */
241                 if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
242                         continue;
243
244                 /* Bail if any hwmon attr struct fails to initialize */
245                 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION);
246                 rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC);
247                 rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP);
248                 rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX);
249                 if (rc)
250                         goto err;
251         }
252
253         goto exit;
254
255 err:
256         igb_sysfs_del_adapter(adapter);
257 exit:
258         return rc;
259 }
260 #endif /* IGB_HWMON */