Commit | Line | Data |
---|---|---|
3ca8bc6d DS |
1 | /******************************************************************************* |
2 | ||
3 | Intel 10 Gigabit PCI Express Linux driver | |
4 | Copyright(c) 1999 - 2012 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 "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 | ||
40 | /* | |
41 | * This file provides a sysfs interface to export information from the | |
42 | * driver. The information presented is READ-ONLY. | |
43 | */ | |
44 | #ifdef CONFIG_IXGBE_HWMON | |
45 | ||
46 | /* hwmon callback functions */ | |
47 | static ssize_t ixgbe_hwmon_show_location(struct device *dev, | |
48 | struct device_attribute *attr, | |
49 | char *buf) | |
50 | { | |
51 | struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr, | |
52 | dev_attr); | |
53 | return sprintf(buf, "loc%u\n", | |
54 | ixgbe_attr->sensor->location); | |
55 | } | |
56 | ||
57 | static ssize_t ixgbe_hwmon_show_temp(struct device *dev, | |
58 | struct device_attribute *attr, | |
59 | char *buf) | |
60 | { | |
61 | struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr, | |
62 | dev_attr); | |
63 | unsigned int value; | |
64 | ||
65 | /* reset the temp field */ | |
66 | ixgbe_attr->hw->mac.ops.get_thermal_sensor_data(ixgbe_attr->hw); | |
67 | ||
68 | value = ixgbe_attr->sensor->temp; | |
69 | ||
70 | /* display millidegree */ | |
71 | value *= 1000; | |
72 | ||
73 | return sprintf(buf, "%u\n", value); | |
74 | } | |
75 | ||
76 | static ssize_t ixgbe_hwmon_show_cautionthresh(struct device *dev, | |
77 | struct device_attribute *attr, | |
78 | char *buf) | |
79 | { | |
80 | struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr, | |
81 | dev_attr); | |
82 | unsigned int value = ixgbe_attr->sensor->caution_thresh; | |
83 | ||
84 | /* display millidegree */ | |
85 | value *= 1000; | |
86 | ||
87 | return sprintf(buf, "%u\n", value); | |
88 | } | |
89 | ||
90 | static ssize_t ixgbe_hwmon_show_maxopthresh(struct device *dev, | |
91 | struct device_attribute *attr, | |
92 | char *buf) | |
93 | { | |
94 | struct hwmon_attr *ixgbe_attr = container_of(attr, struct hwmon_attr, | |
95 | dev_attr); | |
96 | unsigned int value = ixgbe_attr->sensor->max_op_thresh; | |
97 | ||
98 | /* display millidegree */ | |
99 | value *= 1000; | |
100 | ||
101 | return sprintf(buf, "%u\n", value); | |
102 | } | |
103 | ||
104 | /* | |
105 | * ixgbe_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 ixgbe_add_hwmon_attr(struct ixgbe_adapter *adapter, | |
115 | unsigned int offset, int type) { | |
116 | int rc; | |
117 | unsigned int n_attr; | |
118 | struct hwmon_attr *ixgbe_attr; | |
119 | ||
120 | n_attr = adapter->ixgbe_hwmon_buff.n_hwmon; | |
121 | ixgbe_attr = &adapter->ixgbe_hwmon_buff.hwmon_list[n_attr]; | |
122 | ||
123 | switch (type) { | |
124 | case IXGBE_HWMON_TYPE_LOC: | |
125 | ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_location; | |
126 | snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name), | |
127 | "temp%u_label", offset); | |
128 | break; | |
129 | case IXGBE_HWMON_TYPE_TEMP: | |
130 | ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_temp; | |
131 | snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name), | |
132 | "temp%u_input", offset); | |
133 | break; | |
134 | case IXGBE_HWMON_TYPE_CAUTION: | |
135 | ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_cautionthresh; | |
136 | snprintf(ixgbe_attr->name, sizeof(ixgbe_attr->name), | |
137 | "temp%u_max", offset); | |
138 | break; | |
139 | case IXGBE_HWMON_TYPE_MAX: | |
140 | ixgbe_attr->dev_attr.show = ixgbe_hwmon_show_maxopthresh; | |
141 | snprintf(ixgbe_attr->name, sizeof(ixgbe_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 | ixgbe_attr->sensor = | |
151 | &adapter->hw.mac.thermal_sensor_data.sensor[offset]; | |
152 | ixgbe_attr->hw = &adapter->hw; | |
153 | ixgbe_attr->dev_attr.store = NULL; | |
154 | ixgbe_attr->dev_attr.attr.mode = S_IRUGO; | |
155 | ixgbe_attr->dev_attr.attr.name = ixgbe_attr->name; | |
156 | ||
157 | rc = device_create_file(&adapter->pdev->dev, | |
158 | &ixgbe_attr->dev_attr); | |
159 | ||
160 | if (rc == 0) | |
161 | ++adapter->ixgbe_hwmon_buff.n_hwmon; | |
162 | ||
163 | return rc; | |
164 | } | |
165 | #endif /* CONFIG_IXGBE_HWMON */ | |
166 | ||
167 | static void ixgbe_sysfs_del_adapter(struct ixgbe_adapter *adapter) | |
168 | { | |
169 | #ifdef CONFIG_IXGBE_HWMON | |
170 | int i; | |
171 | #endif /* CONFIG_IXGBE_HWMON */ | |
172 | ||
173 | if (adapter == NULL) | |
174 | return; | |
175 | #ifdef CONFIG_IXGBE_HWMON | |
176 | ||
177 | for (i = 0; i < adapter->ixgbe_hwmon_buff.n_hwmon; i++) { | |
178 | device_remove_file(&adapter->pdev->dev, | |
179 | &adapter->ixgbe_hwmon_buff.hwmon_list[i].dev_attr); | |
180 | } | |
181 | ||
182 | kfree(adapter->ixgbe_hwmon_buff.hwmon_list); | |
183 | ||
184 | if (adapter->ixgbe_hwmon_buff.device) | |
185 | hwmon_device_unregister(adapter->ixgbe_hwmon_buff.device); | |
186 | #endif /* CONFIG_IXGBE_HWMON */ | |
187 | ||
af94bf6d | 188 | if (adapter->info_kobj != NULL) { |
3ca8bc6d | 189 | kobject_put(adapter->info_kobj); |
af94bf6d AD |
190 | adapter->info_kobj = NULL; |
191 | } | |
3ca8bc6d DS |
192 | } |
193 | ||
194 | /* called from ixgbe_main.c */ | |
195 | void ixgbe_sysfs_exit(struct ixgbe_adapter *adapter) | |
196 | { | |
197 | ixgbe_sysfs_del_adapter(adapter); | |
198 | } | |
199 | ||
200 | /* called from ixgbe_main.c */ | |
201 | int ixgbe_sysfs_init(struct ixgbe_adapter *adapter) | |
202 | { | |
203 | #ifdef CONFIG_IXGBE_HWMON | |
204 | struct hwmon_buff *ixgbe_hwmon = &adapter->ixgbe_hwmon_buff; | |
205 | unsigned int i; | |
206 | int n_attrs; | |
207 | #endif /* CONFIG_IXGBE_HWMON */ | |
208 | struct net_device *netdev = adapter->netdev; | |
209 | int rc = 0; | |
210 | ||
211 | /* create info kobj and attribute listings in kobj */ | |
212 | adapter->info_kobj = kobject_create_and_add("info", &netdev->dev.kobj); | |
213 | if (adapter->info_kobj == NULL) { | |
214 | rc = -ENOMEM; | |
215 | goto err; | |
216 | } | |
217 | ||
218 | #ifdef CONFIG_IXGBE_HWMON | |
219 | /* If this method isn't defined we don't support thermals */ | |
220 | if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL) { | |
221 | rc = -EPERM; | |
222 | goto err; | |
223 | } | |
224 | ||
225 | /* Don't create thermal hwmon interface if no sensors present */ | |
226 | rc = adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw); | |
227 | if (rc) | |
228 | goto err; | |
229 | ||
230 | /* | |
231 | * Allocation space for max attributs | |
232 | * max num sensors * values (loc, temp, max, caution) | |
233 | */ | |
234 | n_attrs = IXGBE_MAX_SENSORS * 4; | |
235 | ixgbe_hwmon->hwmon_list = kcalloc(n_attrs, sizeof(struct hwmon_attr), | |
236 | GFP_KERNEL); | |
237 | if (!ixgbe_hwmon->hwmon_list) { | |
238 | rc = -ENOMEM; | |
239 | goto err; | |
240 | } | |
241 | ||
242 | ixgbe_hwmon->device = hwmon_device_register(&adapter->pdev->dev); | |
243 | if (IS_ERR(ixgbe_hwmon->device)) { | |
244 | rc = PTR_ERR(ixgbe_hwmon->device); | |
245 | goto err; | |
246 | } | |
247 | ||
248 | for (i = 0; i < IXGBE_MAX_SENSORS; i++) { | |
249 | /* | |
250 | * Only create hwmon sysfs entries for sensors that have | |
251 | * meaningful data for. | |
252 | */ | |
253 | if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0) | |
254 | continue; | |
255 | ||
256 | /* Bail if any hwmon attr struct fails to initialize */ | |
257 | rc = ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_CAUTION); | |
258 | rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_LOC); | |
259 | rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_TEMP); | |
260 | rc |= ixgbe_add_hwmon_attr(adapter, i, IXGBE_HWMON_TYPE_MAX); | |
261 | if (rc) | |
262 | goto err; | |
263 | } | |
264 | #endif /* CONFIG_IXGBE_HWMON */ | |
265 | ||
266 | goto exit; | |
267 | ||
268 | err: | |
269 | ixgbe_sysfs_del_adapter(adapter); | |
270 | exit: | |
271 | return rc; | |
272 | } | |
273 |