Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_eal / linuxapp / kni / ethtool / igb / igb_procfs.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 #ifdef IGB_PROCFS
33 #ifndef IGB_HWMON
34
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/proc_fs.h>
38 #include <linux/device.h>
39 #include <linux/netdevice.h>
40
41 static struct proc_dir_entry *igb_top_dir = NULL;
42
43
44 bool igb_thermal_present(struct igb_adapter *adapter)
45 {
46         s32 status;
47         struct e1000_hw *hw;
48
49         if (adapter == NULL)
50                 return false;
51         hw = &adapter->hw;
52
53         /*
54          * Only set I2C bit-bang mode if an external thermal sensor is
55          * supported on this device.
56          */
57         if (adapter->ets) {
58                 status = e1000_set_i2c_bb(hw);
59                 if (status != E1000_SUCCESS)
60                         return false;
61         }
62
63         status = hw->mac.ops.init_thermal_sensor_thresh(hw);
64         if (status != E1000_SUCCESS)
65                 return false;
66
67         return true;
68 }
69
70
71 static int igb_macburn(char *page, char **start, off_t off, int count,
72                         int *eof, void *data)
73 {
74         struct e1000_hw *hw;
75         struct igb_adapter *adapter = (struct igb_adapter *)data;
76         if (adapter == NULL)
77                 return snprintf(page, count, "error: no adapter\n");
78
79         hw = &adapter->hw;
80         if (hw == NULL)
81                 return snprintf(page, count, "error: no hw data\n");
82
83         return snprintf(page, count, "0x%02X%02X%02X%02X%02X%02X\n",
84                        (unsigned int)hw->mac.perm_addr[0],
85                        (unsigned int)hw->mac.perm_addr[1],
86                        (unsigned int)hw->mac.perm_addr[2],
87                        (unsigned int)hw->mac.perm_addr[3],
88                        (unsigned int)hw->mac.perm_addr[4],
89                        (unsigned int)hw->mac.perm_addr[5]);
90 }
91
92 static int igb_macadmn(char *page, char **start, off_t off,
93                        int count, int *eof, void *data)
94 {
95         struct e1000_hw *hw;
96         struct igb_adapter *adapter = (struct igb_adapter *)data;
97         if (adapter == NULL)
98                 return snprintf(page, count, "error: no adapter\n");
99
100         hw = &adapter->hw;
101         if (hw == NULL)
102                 return snprintf(page, count, "error: no hw data\n");
103
104         return snprintf(page, count, "0x%02X%02X%02X%02X%02X%02X\n",
105                        (unsigned int)hw->mac.addr[0],
106                        (unsigned int)hw->mac.addr[1],
107                        (unsigned int)hw->mac.addr[2],
108                        (unsigned int)hw->mac.addr[3],
109                        (unsigned int)hw->mac.addr[4],
110                        (unsigned int)hw->mac.addr[5]);
111 }
112
113 static int igb_numeports(char *page, char **start, off_t off, int count,
114                          int *eof, void *data)
115 {
116         struct e1000_hw *hw;
117         int ports;
118         struct igb_adapter *adapter = (struct igb_adapter *)data;
119         if (adapter == NULL)
120                 return snprintf(page, count, "error: no adapter\n");
121
122         hw = &adapter->hw;
123         if (hw == NULL)
124                 return snprintf(page, count, "error: no hw data\n");
125
126         ports = 4;
127
128         return snprintf(page, count, "%d\n", ports);
129 }
130
131 static int igb_porttype(char *page, char **start, off_t off, int count,
132                         int *eof, void *data)
133 {
134         struct igb_adapter *adapter = (struct igb_adapter *)data;
135         if (adapter == NULL)
136                 return snprintf(page, count, "error: no adapter\n");
137
138         return snprintf(page, count, "%d\n",
139                         test_bit(__IGB_DOWN, &adapter->state));
140 }
141
142 static int igb_therm_location(char *page, char **start, off_t off,
143                                      int count, int *eof, void *data)
144 {
145         struct igb_therm_proc_data *therm_data =
146                 (struct igb_therm_proc_data *)data;
147
148         if (therm_data == NULL)
149                 return snprintf(page, count, "error: no therm_data\n");
150
151         return snprintf(page, count, "%d\n", therm_data->sensor_data->location);
152 }
153
154 static int igb_therm_maxopthresh(char *page, char **start, off_t off,
155                                     int count, int *eof, void *data)
156 {
157         struct igb_therm_proc_data *therm_data =
158                 (struct igb_therm_proc_data *)data;
159
160         if (therm_data == NULL)
161                 return snprintf(page, count, "error: no therm_data\n");
162
163         return snprintf(page, count, "%d\n",
164                         therm_data->sensor_data->max_op_thresh);
165 }
166
167 static int igb_therm_cautionthresh(char *page, char **start, off_t off,
168                                       int count, int *eof, void *data)
169 {
170         struct igb_therm_proc_data *therm_data =
171                 (struct igb_therm_proc_data *)data;
172
173         if (therm_data == NULL)
174                 return snprintf(page, count, "error: no therm_data\n");
175
176         return snprintf(page, count, "%d\n",
177                         therm_data->sensor_data->caution_thresh);
178 }
179
180 static int igb_therm_temp(char *page, char **start, off_t off,
181                              int count, int *eof, void *data)
182 {
183         s32 status;
184         struct igb_therm_proc_data *therm_data =
185                 (struct igb_therm_proc_data *)data;
186
187         if (therm_data == NULL)
188                 return snprintf(page, count, "error: no therm_data\n");
189
190         status = e1000_get_thermal_sensor_data(therm_data->hw);
191         if (status != E1000_SUCCESS)
192                 snprintf(page, count, "error: status %d returned\n", status);
193
194         return snprintf(page, count, "%d\n", therm_data->sensor_data->temp);
195 }
196
197 struct igb_proc_type{
198         char name[32];
199         int (*read)(char*, char**, off_t, int, int*, void*);
200 };
201
202 struct igb_proc_type igb_proc_entries[] = {
203         {"numeports", &igb_numeports},
204         {"porttype", &igb_porttype},
205         {"macburn", &igb_macburn},
206         {"macadmn", &igb_macadmn},
207         {"", NULL}
208 };
209
210 struct igb_proc_type igb_internal_entries[] = {
211         {"location", &igb_therm_location},
212         {"temp", &igb_therm_temp},
213         {"cautionthresh", &igb_therm_cautionthresh},
214         {"maxopthresh", &igb_therm_maxopthresh},
215         {"", NULL}
216 };
217
218 void igb_del_proc_entries(struct igb_adapter *adapter)
219 {
220         int index, i;
221         char buf[16];   /* much larger than the sensor number will ever be */
222
223         if (igb_top_dir == NULL)
224                 return;
225
226         for (i = 0; i < E1000_MAX_SENSORS; i++) {
227                 if (adapter->therm_dir[i] == NULL)
228                         continue;
229
230                 for (index = 0; ; index++) {
231                         if (igb_internal_entries[index].read == NULL)
232                                 break;
233
234                          remove_proc_entry(igb_internal_entries[index].name,
235                                            adapter->therm_dir[i]);
236                 }
237                 snprintf(buf, sizeof(buf), "sensor_%d", i);
238                 remove_proc_entry(buf, adapter->info_dir);
239         }
240
241         if (adapter->info_dir != NULL) {
242                 for (index = 0; ; index++) {
243                         if (igb_proc_entries[index].read == NULL)
244                                 break;
245                         remove_proc_entry(igb_proc_entries[index].name,
246                                           adapter->info_dir);
247                 }
248                 remove_proc_entry("info", adapter->eth_dir);
249         }
250
251         if (adapter->eth_dir != NULL)
252                 remove_proc_entry(pci_name(adapter->pdev), igb_top_dir);
253 }
254
255 /* called from igb_main.c */
256 void igb_procfs_exit(struct igb_adapter *adapter)
257 {
258         igb_del_proc_entries(adapter);
259 }
260
261 int igb_procfs_topdir_init(void)
262 {
263         igb_top_dir = proc_mkdir("driver/igb", NULL);
264         if (igb_top_dir == NULL)
265                 return -ENOMEM;
266
267         return 0;
268 }
269
270 void igb_procfs_topdir_exit(void)
271 {
272         remove_proc_entry("driver/igb", NULL);
273 }
274
275 /* called from igb_main.c */
276 int igb_procfs_init(struct igb_adapter *adapter)
277 {
278         int rc = 0;
279         int i;
280         int index;
281         char buf[16];   /* much larger than the sensor number will ever be */
282
283         adapter->eth_dir = NULL;
284         adapter->info_dir = NULL;
285         for (i = 0; i < E1000_MAX_SENSORS; i++)
286                 adapter->therm_dir[i] = NULL;
287
288         if ( igb_top_dir == NULL ) {
289                 rc = -ENOMEM;
290                 goto fail;
291         }
292
293         adapter->eth_dir = proc_mkdir(pci_name(adapter->pdev), igb_top_dir);
294         if (adapter->eth_dir == NULL) {
295                 rc = -ENOMEM;
296                 goto fail;
297         }
298
299         adapter->info_dir = proc_mkdir("info", adapter->eth_dir);
300         if (adapter->info_dir == NULL) {
301                 rc = -ENOMEM;
302                 goto fail;
303         }
304         for (index = 0; ; index++) {
305                 if (igb_proc_entries[index].read == NULL) {
306                         break;
307                 }
308                 if (!(create_proc_read_entry(igb_proc_entries[index].name,
309                                            0444,
310                                            adapter->info_dir,
311                                            igb_proc_entries[index].read,
312                                            adapter))) {
313
314                         rc = -ENOMEM;
315                         goto fail;
316                 }
317         }
318         if (igb_thermal_present(adapter) == false)
319                 goto exit;
320
321         for (i = 0; i < E1000_MAX_SENSORS; i++) {
322
323                  if (adapter->hw.mac.thermal_sensor_data.sensor[i].location== 0)
324                         continue;
325
326                 snprintf(buf, sizeof(buf), "sensor_%d", i);
327                 adapter->therm_dir[i] = proc_mkdir(buf, adapter->info_dir);
328                 if (adapter->therm_dir[i] == NULL) {
329                         rc = -ENOMEM;
330                         goto fail;
331                 }
332                 for (index = 0; ; index++) {
333                         if (igb_internal_entries[index].read == NULL)
334                                 break;
335                         /*
336                          * therm_data struct contains pointer the read func
337                          * will be needing
338                          */
339                         adapter->therm_data[i].hw = &adapter->hw;
340                         adapter->therm_data[i].sensor_data =
341                                 &adapter->hw.mac.thermal_sensor_data.sensor[i];
342
343                         if (!(create_proc_read_entry(
344                                            igb_internal_entries[index].name,
345                                            0444,
346                                            adapter->therm_dir[i],
347                                            igb_internal_entries[index].read,
348                                            &adapter->therm_data[i]))) {
349                                 rc = -ENOMEM;
350                                 goto fail;
351                         }
352                 }
353         }
354         goto exit;
355
356 fail:
357         igb_del_proc_entries(adapter);
358 exit:
359         return rc;
360 }
361
362 #endif /* !IGB_HWMON */
363 #endif /* IGB_PROCFS */