clocksource: make CLOCKSOURCE_OF_DECLARE type safe
[cascardo/linux.git] / drivers / net / ethernet / intel / 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
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 CONFIG_IGB_HWMON
42 /* hwmon callback functions */
43 static 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
53 static 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
72 static 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
86 static 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  */
109 static int igb_add_hwmon_attr(struct igb_adapter *adapter,
110                                 unsigned int offset, int type) {
111         int rc;
112         unsigned int n_attr;
113         struct hwmon_attr *igb_attr;
114
115         n_attr = adapter->igb_hwmon_buff.n_hwmon;
116         igb_attr = &adapter->igb_hwmon_buff.hwmon_list[n_attr];
117
118         switch (type) {
119         case IGB_HWMON_TYPE_LOC:
120                 igb_attr->dev_attr.show = igb_hwmon_show_location;
121                 snprintf(igb_attr->name, sizeof(igb_attr->name),
122                          "temp%u_label", offset);
123                 break;
124         case IGB_HWMON_TYPE_TEMP:
125                 igb_attr->dev_attr.show = igb_hwmon_show_temp;
126                 snprintf(igb_attr->name, sizeof(igb_attr->name),
127                          "temp%u_input", offset);
128                 break;
129         case IGB_HWMON_TYPE_CAUTION:
130                 igb_attr->dev_attr.show = igb_hwmon_show_cautionthresh;
131                 snprintf(igb_attr->name, sizeof(igb_attr->name),
132                          "temp%u_max", offset);
133                 break;
134         case IGB_HWMON_TYPE_MAX:
135                 igb_attr->dev_attr.show = igb_hwmon_show_maxopthresh;
136                 snprintf(igb_attr->name, sizeof(igb_attr->name),
137                          "temp%u_crit", offset);
138                 break;
139         default:
140                 rc = -EPERM;
141                 return rc;
142         }
143
144         /* These always the same regardless of type */
145         igb_attr->sensor =
146                 &adapter->hw.mac.thermal_sensor_data.sensor[offset];
147         igb_attr->hw = &adapter->hw;
148         igb_attr->dev_attr.store = NULL;
149         igb_attr->dev_attr.attr.mode = S_IRUGO;
150         igb_attr->dev_attr.attr.name = igb_attr->name;
151         sysfs_attr_init(&igb_attr->dev_attr.attr);
152         rc = device_create_file(&adapter->pdev->dev,
153                                 &igb_attr->dev_attr);
154         if (rc == 0)
155                 ++adapter->igb_hwmon_buff.n_hwmon;
156
157         return rc;
158 }
159
160 static void igb_sysfs_del_adapter(struct igb_adapter *adapter)
161 {
162         int i;
163
164         if (adapter == NULL)
165                 return;
166
167         for (i = 0; i < adapter->igb_hwmon_buff.n_hwmon; i++) {
168                 device_remove_file(&adapter->pdev->dev,
169                            &adapter->igb_hwmon_buff.hwmon_list[i].dev_attr);
170         }
171
172         kfree(adapter->igb_hwmon_buff.hwmon_list);
173
174         if (adapter->igb_hwmon_buff.device)
175                 hwmon_device_unregister(adapter->igb_hwmon_buff.device);
176 }
177
178 /* called from igb_main.c */
179 void igb_sysfs_exit(struct igb_adapter *adapter)
180 {
181         igb_sysfs_del_adapter(adapter);
182 }
183
184 /* called from igb_main.c */
185 int igb_sysfs_init(struct igb_adapter *adapter)
186 {
187         struct hwmon_buff *igb_hwmon = &adapter->igb_hwmon_buff;
188         unsigned int i;
189         int n_attrs;
190         int rc = 0;
191
192         /* If this method isn't defined we don't support thermals */
193         if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL)
194                 goto exit;
195
196         /* Don't create thermal hwmon interface if no sensors present */
197         rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw));
198                 if (rc)
199                         goto exit;
200
201         /* Allocation space for max attributes
202          * max num sensors * values (loc, temp, max, caution)
203          */
204         n_attrs = E1000_MAX_SENSORS * 4;
205         igb_hwmon->hwmon_list = kcalloc(n_attrs, sizeof(struct hwmon_attr),
206                                           GFP_KERNEL);
207         if (!igb_hwmon->hwmon_list) {
208                 rc = -ENOMEM;
209                 goto err;
210         }
211
212         igb_hwmon->device = hwmon_device_register(&adapter->pdev->dev);
213         if (IS_ERR(igb_hwmon->device)) {
214                 rc = PTR_ERR(igb_hwmon->device);
215                 goto err;
216         }
217
218         for (i = 0; i < E1000_MAX_SENSORS; i++) {
219
220                 /* Only create hwmon sysfs entries for sensors that have
221                  * meaningful data.
222                  */
223                 if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
224                         continue;
225
226                 /* Bail if any hwmon attr struct fails to initialize */
227                 rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION);
228                 rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC);
229                 rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP);
230                 rc |= igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX);
231                 if (rc)
232                         goto err;
233         }
234
235         goto exit;
236
237 err:
238         igb_sysfs_del_adapter(adapter);
239 exit:
240         return rc;
241 }
242 #endif