Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / drivers / net / ixgb / ixgb_ethtool.c
1 /*******************************************************************************
2
3   Intel PRO/10GbE Linux driver
4   Copyright(c) 1999 - 2006 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   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 /* ethtool support for ixgb */
30
31 #include "ixgb.h"
32
33 #include <asm/uaccess.h>
34
35 extern int ixgb_up(struct ixgb_adapter *adapter);
36 extern void ixgb_down(struct ixgb_adapter *adapter, boolean_t kill_watchdog);
37 extern void ixgb_reset(struct ixgb_adapter *adapter);
38 extern int ixgb_setup_rx_resources(struct ixgb_adapter *adapter);
39 extern int ixgb_setup_tx_resources(struct ixgb_adapter *adapter);
40 extern void ixgb_free_rx_resources(struct ixgb_adapter *adapter);
41 extern void ixgb_free_tx_resources(struct ixgb_adapter *adapter);
42 extern void ixgb_update_stats(struct ixgb_adapter *adapter);
43
44 #define IXGB_ALL_RAR_ENTRIES 16
45
46 struct ixgb_stats {
47         char stat_string[ETH_GSTRING_LEN];
48         int sizeof_stat;
49         int stat_offset;
50 };
51
52 #define IXGB_STAT(m) FIELD_SIZEOF(struct ixgb_adapter, m), \
53                       offsetof(struct ixgb_adapter, m)
54 static struct ixgb_stats ixgb_gstrings_stats[] = {
55         {"rx_packets", IXGB_STAT(net_stats.rx_packets)},
56         {"tx_packets", IXGB_STAT(net_stats.tx_packets)},
57         {"rx_bytes", IXGB_STAT(net_stats.rx_bytes)},
58         {"tx_bytes", IXGB_STAT(net_stats.tx_bytes)},
59         {"rx_errors", IXGB_STAT(net_stats.rx_errors)},
60         {"tx_errors", IXGB_STAT(net_stats.tx_errors)},
61         {"rx_dropped", IXGB_STAT(net_stats.rx_dropped)},
62         {"tx_dropped", IXGB_STAT(net_stats.tx_dropped)},
63         {"multicast", IXGB_STAT(net_stats.multicast)},
64         {"collisions", IXGB_STAT(net_stats.collisions)},
65
66 /*      { "rx_length_errors", IXGB_STAT(net_stats.rx_length_errors) },  */
67         {"rx_over_errors", IXGB_STAT(net_stats.rx_over_errors)},
68         {"rx_crc_errors", IXGB_STAT(net_stats.rx_crc_errors)},
69         {"rx_frame_errors", IXGB_STAT(net_stats.rx_frame_errors)},
70         {"rx_no_buffer_count", IXGB_STAT(stats.rnbc)},
71         {"rx_fifo_errors", IXGB_STAT(net_stats.rx_fifo_errors)},
72         {"rx_missed_errors", IXGB_STAT(net_stats.rx_missed_errors)},
73         {"tx_aborted_errors", IXGB_STAT(net_stats.tx_aborted_errors)},
74         {"tx_carrier_errors", IXGB_STAT(net_stats.tx_carrier_errors)},
75         {"tx_fifo_errors", IXGB_STAT(net_stats.tx_fifo_errors)},
76         {"tx_heartbeat_errors", IXGB_STAT(net_stats.tx_heartbeat_errors)},
77         {"tx_window_errors", IXGB_STAT(net_stats.tx_window_errors)},
78         {"tx_deferred_ok", IXGB_STAT(stats.dc)},
79         {"tx_timeout_count", IXGB_STAT(tx_timeout_count) },
80         {"tx_restart_queue", IXGB_STAT(restart_queue) },
81         {"rx_long_length_errors", IXGB_STAT(stats.roc)},
82         {"rx_short_length_errors", IXGB_STAT(stats.ruc)},
83         {"tx_tcp_seg_good", IXGB_STAT(stats.tsctc)},
84         {"tx_tcp_seg_failed", IXGB_STAT(stats.tsctfc)},
85         {"rx_flow_control_xon", IXGB_STAT(stats.xonrxc)},
86         {"rx_flow_control_xoff", IXGB_STAT(stats.xoffrxc)},
87         {"tx_flow_control_xon", IXGB_STAT(stats.xontxc)},
88         {"tx_flow_control_xoff", IXGB_STAT(stats.xofftxc)},
89         {"rx_csum_offload_good", IXGB_STAT(hw_csum_rx_good)},
90         {"rx_csum_offload_errors", IXGB_STAT(hw_csum_rx_error)},
91         {"tx_csum_offload_good", IXGB_STAT(hw_csum_tx_good)},
92         {"tx_csum_offload_errors", IXGB_STAT(hw_csum_tx_error)}
93 };
94
95 #define IXGB_STATS_LEN  ARRAY_SIZE(ixgb_gstrings_stats)
96
97 static int
98 ixgb_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
99 {
100         struct ixgb_adapter *adapter = netdev_priv(netdev);
101
102         ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
103         ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
104         ecmd->port = PORT_FIBRE;
105         ecmd->transceiver = XCVR_EXTERNAL;
106
107         if(netif_carrier_ok(adapter->netdev)) {
108                 ecmd->speed = SPEED_10000;
109                 ecmd->duplex = DUPLEX_FULL;
110         } else {
111                 ecmd->speed = -1;
112                 ecmd->duplex = -1;
113         }
114
115         ecmd->autoneg = AUTONEG_DISABLE;
116         return 0;
117 }
118
119 static void ixgb_set_speed_duplex(struct net_device *netdev)
120 {
121         struct ixgb_adapter *adapter = netdev_priv(netdev);
122         /* be optimistic about our link, since we were up before */
123         adapter->link_speed = 10000;
124         adapter->link_duplex = FULL_DUPLEX;
125         netif_carrier_on(netdev);
126         netif_wake_queue(netdev);
127 }
128
129 static int
130 ixgb_set_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
131 {
132         struct ixgb_adapter *adapter = netdev_priv(netdev);
133
134         if(ecmd->autoneg == AUTONEG_ENABLE ||
135            ecmd->speed + ecmd->duplex != SPEED_10000 + DUPLEX_FULL)
136                 return -EINVAL;
137         
138         if(netif_running(adapter->netdev)) {
139                 ixgb_down(adapter, TRUE);
140                 ixgb_reset(adapter);
141                 ixgb_up(adapter);
142                 ixgb_set_speed_duplex(netdev);
143         } else
144                 ixgb_reset(adapter);
145
146         return 0;
147 }
148
149 static void
150 ixgb_get_pauseparam(struct net_device *netdev,
151                          struct ethtool_pauseparam *pause)
152 {
153         struct ixgb_adapter *adapter = netdev_priv(netdev);
154         struct ixgb_hw *hw = &adapter->hw;
155         
156         pause->autoneg = AUTONEG_DISABLE;
157                 
158         if(hw->fc.type == ixgb_fc_rx_pause)
159                 pause->rx_pause = 1;
160         else if(hw->fc.type == ixgb_fc_tx_pause)
161                 pause->tx_pause = 1;
162         else if(hw->fc.type == ixgb_fc_full) {
163                 pause->rx_pause = 1;
164                 pause->tx_pause = 1;
165         }
166 }
167
168 static int
169 ixgb_set_pauseparam(struct net_device *netdev,
170                          struct ethtool_pauseparam *pause)
171 {
172         struct ixgb_adapter *adapter = netdev_priv(netdev);
173         struct ixgb_hw *hw = &adapter->hw;
174         
175         if(pause->autoneg == AUTONEG_ENABLE)
176                 return -EINVAL;
177
178         if(pause->rx_pause && pause->tx_pause)
179                 hw->fc.type = ixgb_fc_full;
180         else if(pause->rx_pause && !pause->tx_pause)
181                 hw->fc.type = ixgb_fc_rx_pause;
182         else if(!pause->rx_pause && pause->tx_pause)
183                 hw->fc.type = ixgb_fc_tx_pause;
184         else if(!pause->rx_pause && !pause->tx_pause)
185                 hw->fc.type = ixgb_fc_none;
186
187         if(netif_running(adapter->netdev)) {
188                 ixgb_down(adapter, TRUE);
189                 ixgb_up(adapter);
190                 ixgb_set_speed_duplex(netdev);
191         } else
192                 ixgb_reset(adapter);
193                 
194         return 0;
195 }
196
197 static uint32_t
198 ixgb_get_rx_csum(struct net_device *netdev)
199 {
200         struct ixgb_adapter *adapter = netdev_priv(netdev);
201
202         return adapter->rx_csum;
203 }
204
205 static int
206 ixgb_set_rx_csum(struct net_device *netdev, uint32_t data)
207 {
208         struct ixgb_adapter *adapter = netdev_priv(netdev);
209
210         adapter->rx_csum = data;
211
212         if(netif_running(netdev)) {
213                 ixgb_down(adapter,TRUE);
214                 ixgb_up(adapter);
215                 ixgb_set_speed_duplex(netdev);
216         } else
217                 ixgb_reset(adapter);
218         return 0;
219 }
220         
221 static uint32_t
222 ixgb_get_tx_csum(struct net_device *netdev)
223 {
224         return (netdev->features & NETIF_F_HW_CSUM) != 0;
225 }
226
227 static int
228 ixgb_set_tx_csum(struct net_device *netdev, uint32_t data)
229 {
230         if (data)
231                 netdev->features |= NETIF_F_HW_CSUM;
232         else
233                 netdev->features &= ~NETIF_F_HW_CSUM;
234
235         return 0;
236 }
237
238 static int
239 ixgb_set_tso(struct net_device *netdev, uint32_t data)
240 {
241         if(data)
242                 netdev->features |= NETIF_F_TSO;
243         else
244                 netdev->features &= ~NETIF_F_TSO;
245         return 0;
246
247
248 static uint32_t
249 ixgb_get_msglevel(struct net_device *netdev)
250 {
251         struct ixgb_adapter *adapter = netdev_priv(netdev);
252         return adapter->msg_enable;
253 }
254
255 static void
256 ixgb_set_msglevel(struct net_device *netdev, uint32_t data)
257 {
258         struct ixgb_adapter *adapter = netdev_priv(netdev);
259         adapter->msg_enable = data;
260 }
261 #define IXGB_GET_STAT(_A_, _R_) _A_->stats._R_
262
263 static int 
264 ixgb_get_regs_len(struct net_device *netdev)
265 {
266 #define IXGB_REG_DUMP_LEN  136*sizeof(uint32_t)
267         return IXGB_REG_DUMP_LEN;
268 }
269
270 static void
271 ixgb_get_regs(struct net_device *netdev,
272                    struct ethtool_regs *regs, void *p)
273 {
274         struct ixgb_adapter *adapter = netdev_priv(netdev);
275         struct ixgb_hw *hw = &adapter->hw;
276         uint32_t *reg = p;
277         uint32_t *reg_start = reg;
278         uint8_t i;
279
280         /* the 1 (one) below indicates an attempt at versioning, if the
281          * interface in ethtool or the driver changes, this 1 should be
282          * incremented */
283         regs->version = (1<<24) | hw->revision_id << 16 | hw->device_id;
284
285         /* General Registers */
286         *reg++ = IXGB_READ_REG(hw, CTRL0);      /*   0 */
287         *reg++ = IXGB_READ_REG(hw, CTRL1);      /*   1 */
288         *reg++ = IXGB_READ_REG(hw, STATUS);     /*   2 */
289         *reg++ = IXGB_READ_REG(hw, EECD);       /*   3 */
290         *reg++ = IXGB_READ_REG(hw, MFS);        /*   4 */
291
292         /* Interrupt */
293         *reg++ = IXGB_READ_REG(hw, ICR);        /*   5 */
294         *reg++ = IXGB_READ_REG(hw, ICS);        /*   6 */
295         *reg++ = IXGB_READ_REG(hw, IMS);        /*   7 */
296         *reg++ = IXGB_READ_REG(hw, IMC);        /*   8 */
297
298         /* Receive */
299         *reg++ = IXGB_READ_REG(hw, RCTL);       /*   9 */
300         *reg++ = IXGB_READ_REG(hw, FCRTL);      /*  10 */
301         *reg++ = IXGB_READ_REG(hw, FCRTH);      /*  11 */
302         *reg++ = IXGB_READ_REG(hw, RDBAL);      /*  12 */
303         *reg++ = IXGB_READ_REG(hw, RDBAH);      /*  13 */
304         *reg++ = IXGB_READ_REG(hw, RDLEN);      /*  14 */
305         *reg++ = IXGB_READ_REG(hw, RDH);        /*  15 */
306         *reg++ = IXGB_READ_REG(hw, RDT);        /*  16 */
307         *reg++ = IXGB_READ_REG(hw, RDTR);       /*  17 */
308         *reg++ = IXGB_READ_REG(hw, RXDCTL);     /*  18 */
309         *reg++ = IXGB_READ_REG(hw, RAIDC);      /*  19 */
310         *reg++ = IXGB_READ_REG(hw, RXCSUM);     /*  20 */
311
312         /* there are 16 RAR entries in hardware, we only use 3 */
313         for(i = 0; i < IXGB_ALL_RAR_ENTRIES; i++) {
314                 *reg++ = IXGB_READ_REG_ARRAY(hw, RAL, (i << 1)); /*21,...,51 */
315                 *reg++ = IXGB_READ_REG_ARRAY(hw, RAH, (i << 1)); /*22,...,52 */
316         }
317
318         /* Transmit */
319         *reg++ = IXGB_READ_REG(hw, TCTL);       /*  53 */
320         *reg++ = IXGB_READ_REG(hw, TDBAL);      /*  54 */
321         *reg++ = IXGB_READ_REG(hw, TDBAH);      /*  55 */
322         *reg++ = IXGB_READ_REG(hw, TDLEN);      /*  56 */
323         *reg++ = IXGB_READ_REG(hw, TDH);        /*  57 */
324         *reg++ = IXGB_READ_REG(hw, TDT);        /*  58 */
325         *reg++ = IXGB_READ_REG(hw, TIDV);       /*  59 */
326         *reg++ = IXGB_READ_REG(hw, TXDCTL);     /*  60 */
327         *reg++ = IXGB_READ_REG(hw, TSPMT);      /*  61 */
328         *reg++ = IXGB_READ_REG(hw, PAP);        /*  62 */
329
330         /* Physical */
331         *reg++ = IXGB_READ_REG(hw, PCSC1);      /*  63 */
332         *reg++ = IXGB_READ_REG(hw, PCSC2);      /*  64 */
333         *reg++ = IXGB_READ_REG(hw, PCSS1);      /*  65 */
334         *reg++ = IXGB_READ_REG(hw, PCSS2);      /*  66 */
335         *reg++ = IXGB_READ_REG(hw, XPCSS);      /*  67 */
336         *reg++ = IXGB_READ_REG(hw, UCCR);       /*  68 */
337         *reg++ = IXGB_READ_REG(hw, XPCSTC);     /*  69 */
338         *reg++ = IXGB_READ_REG(hw, MACA);       /*  70 */
339         *reg++ = IXGB_READ_REG(hw, APAE);       /*  71 */
340         *reg++ = IXGB_READ_REG(hw, ARD);        /*  72 */
341         *reg++ = IXGB_READ_REG(hw, AIS);        /*  73 */
342         *reg++ = IXGB_READ_REG(hw, MSCA);       /*  74 */
343         *reg++ = IXGB_READ_REG(hw, MSRWD);      /*  75 */
344
345         /* Statistics */
346         *reg++ = IXGB_GET_STAT(adapter, tprl);  /*  76 */
347         *reg++ = IXGB_GET_STAT(adapter, tprh);  /*  77 */
348         *reg++ = IXGB_GET_STAT(adapter, gprcl); /*  78 */
349         *reg++ = IXGB_GET_STAT(adapter, gprch); /*  79 */
350         *reg++ = IXGB_GET_STAT(adapter, bprcl); /*  80 */
351         *reg++ = IXGB_GET_STAT(adapter, bprch); /*  81 */
352         *reg++ = IXGB_GET_STAT(adapter, mprcl); /*  82 */
353         *reg++ = IXGB_GET_STAT(adapter, mprch); /*  83 */
354         *reg++ = IXGB_GET_STAT(adapter, uprcl); /*  84 */
355         *reg++ = IXGB_GET_STAT(adapter, uprch); /*  85 */
356         *reg++ = IXGB_GET_STAT(adapter, vprcl); /*  86 */
357         *reg++ = IXGB_GET_STAT(adapter, vprch); /*  87 */
358         *reg++ = IXGB_GET_STAT(adapter, jprcl); /*  88 */
359         *reg++ = IXGB_GET_STAT(adapter, jprch); /*  89 */
360         *reg++ = IXGB_GET_STAT(adapter, gorcl); /*  90 */
361         *reg++ = IXGB_GET_STAT(adapter, gorch); /*  91 */
362         *reg++ = IXGB_GET_STAT(adapter, torl);  /*  92 */
363         *reg++ = IXGB_GET_STAT(adapter, torh);  /*  93 */
364         *reg++ = IXGB_GET_STAT(adapter, rnbc);  /*  94 */
365         *reg++ = IXGB_GET_STAT(adapter, ruc);   /*  95 */
366         *reg++ = IXGB_GET_STAT(adapter, roc);   /*  96 */
367         *reg++ = IXGB_GET_STAT(adapter, rlec);  /*  97 */
368         *reg++ = IXGB_GET_STAT(adapter, crcerrs);       /*  98 */
369         *reg++ = IXGB_GET_STAT(adapter, icbc);  /*  99 */
370         *reg++ = IXGB_GET_STAT(adapter, ecbc);  /* 100 */
371         *reg++ = IXGB_GET_STAT(adapter, mpc);   /* 101 */
372         *reg++ = IXGB_GET_STAT(adapter, tptl);  /* 102 */
373         *reg++ = IXGB_GET_STAT(adapter, tpth);  /* 103 */
374         *reg++ = IXGB_GET_STAT(adapter, gptcl); /* 104 */
375         *reg++ = IXGB_GET_STAT(adapter, gptch); /* 105 */
376         *reg++ = IXGB_GET_STAT(adapter, bptcl); /* 106 */
377         *reg++ = IXGB_GET_STAT(adapter, bptch); /* 107 */
378         *reg++ = IXGB_GET_STAT(adapter, mptcl); /* 108 */
379         *reg++ = IXGB_GET_STAT(adapter, mptch); /* 109 */
380         *reg++ = IXGB_GET_STAT(adapter, uptcl); /* 110 */
381         *reg++ = IXGB_GET_STAT(adapter, uptch); /* 111 */
382         *reg++ = IXGB_GET_STAT(adapter, vptcl); /* 112 */
383         *reg++ = IXGB_GET_STAT(adapter, vptch); /* 113 */
384         *reg++ = IXGB_GET_STAT(adapter, jptcl); /* 114 */
385         *reg++ = IXGB_GET_STAT(adapter, jptch); /* 115 */
386         *reg++ = IXGB_GET_STAT(adapter, gotcl); /* 116 */
387         *reg++ = IXGB_GET_STAT(adapter, gotch); /* 117 */
388         *reg++ = IXGB_GET_STAT(adapter, totl);  /* 118 */
389         *reg++ = IXGB_GET_STAT(adapter, toth);  /* 119 */
390         *reg++ = IXGB_GET_STAT(adapter, dc);    /* 120 */
391         *reg++ = IXGB_GET_STAT(adapter, plt64c);        /* 121 */
392         *reg++ = IXGB_GET_STAT(adapter, tsctc); /* 122 */
393         *reg++ = IXGB_GET_STAT(adapter, tsctfc);        /* 123 */
394         *reg++ = IXGB_GET_STAT(adapter, ibic);  /* 124 */
395         *reg++ = IXGB_GET_STAT(adapter, rfc);   /* 125 */
396         *reg++ = IXGB_GET_STAT(adapter, lfc);   /* 126 */
397         *reg++ = IXGB_GET_STAT(adapter, pfrc);  /* 127 */
398         *reg++ = IXGB_GET_STAT(adapter, pftc);  /* 128 */
399         *reg++ = IXGB_GET_STAT(adapter, mcfrc); /* 129 */
400         *reg++ = IXGB_GET_STAT(adapter, mcftc); /* 130 */
401         *reg++ = IXGB_GET_STAT(adapter, xonrxc);        /* 131 */
402         *reg++ = IXGB_GET_STAT(adapter, xontxc);        /* 132 */
403         *reg++ = IXGB_GET_STAT(adapter, xoffrxc);       /* 133 */
404         *reg++ = IXGB_GET_STAT(adapter, xofftxc);       /* 134 */
405         *reg++ = IXGB_GET_STAT(adapter, rjc);   /* 135 */
406
407         regs->len = (reg - reg_start) * sizeof(uint32_t);
408 }
409
410 static int
411 ixgb_get_eeprom_len(struct net_device *netdev)
412 {
413         /* return size in bytes */
414         return (IXGB_EEPROM_SIZE << 1);
415 }
416
417 static int
418 ixgb_get_eeprom(struct net_device *netdev,
419                   struct ethtool_eeprom *eeprom, uint8_t *bytes)
420 {
421         struct ixgb_adapter *adapter = netdev_priv(netdev);
422         struct ixgb_hw *hw = &adapter->hw;
423         __le16 *eeprom_buff;
424         int i, max_len, first_word, last_word;
425         int ret_val = 0;
426
427         if(eeprom->len == 0) {
428                 ret_val = -EINVAL;
429                 goto geeprom_error;
430         }
431
432         eeprom->magic = hw->vendor_id | (hw->device_id << 16);
433
434         max_len = ixgb_get_eeprom_len(netdev);
435
436         if(eeprom->offset > eeprom->offset + eeprom->len) {
437                 ret_val = -EINVAL;
438                 goto geeprom_error;
439         }
440
441         if((eeprom->offset + eeprom->len) > max_len)
442                 eeprom->len = (max_len - eeprom->offset);
443
444         first_word = eeprom->offset >> 1;
445         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
446
447         eeprom_buff = kmalloc(sizeof(__le16) *
448                         (last_word - first_word + 1), GFP_KERNEL);
449         if(!eeprom_buff)
450                 return -ENOMEM;
451
452         /* note the eeprom was good because the driver loaded */
453         for(i = 0; i <= (last_word - first_word); i++) {
454                 eeprom_buff[i] = ixgb_get_eeprom_word(hw, (first_word + i));
455         }
456
457         memcpy(bytes, (uint8_t *)eeprom_buff + (eeprom->offset & 1),
458                         eeprom->len);
459         kfree(eeprom_buff);
460
461 geeprom_error:
462         return ret_val;
463 }
464
465 static int
466 ixgb_set_eeprom(struct net_device *netdev,
467                   struct ethtool_eeprom *eeprom, uint8_t *bytes)
468 {
469         struct ixgb_adapter *adapter = netdev_priv(netdev);
470         struct ixgb_hw *hw = &adapter->hw;
471         uint16_t *eeprom_buff;
472         void *ptr;
473         int max_len, first_word, last_word;
474         uint16_t i;
475
476         if(eeprom->len == 0)
477                 return -EINVAL;
478
479         if(eeprom->magic != (hw->vendor_id | (hw->device_id << 16)))
480                 return -EFAULT;
481
482         max_len = ixgb_get_eeprom_len(netdev);
483
484         if(eeprom->offset > eeprom->offset + eeprom->len)
485                 return -EINVAL;
486
487         if((eeprom->offset + eeprom->len) > max_len)
488                 eeprom->len = (max_len - eeprom->offset);
489
490         first_word = eeprom->offset >> 1;
491         last_word = (eeprom->offset + eeprom->len - 1) >> 1;
492         eeprom_buff = kmalloc(max_len, GFP_KERNEL);
493         if(!eeprom_buff)
494                 return -ENOMEM;
495
496         ptr = (void *)eeprom_buff;
497
498         if(eeprom->offset & 1) {
499                 /* need read/modify/write of first changed EEPROM word */
500                 /* only the second byte of the word is being modified */
501                 eeprom_buff[0] = ixgb_read_eeprom(hw, first_word);
502                 ptr++;
503         }
504         if((eeprom->offset + eeprom->len) & 1) {
505                 /* need read/modify/write of last changed EEPROM word */
506                 /* only the first byte of the word is being modified */
507                 eeprom_buff[last_word - first_word] 
508                         = ixgb_read_eeprom(hw, last_word);
509         }
510
511         memcpy(ptr, bytes, eeprom->len);
512         for(i = 0; i <= (last_word - first_word); i++)
513                 ixgb_write_eeprom(hw, first_word + i, eeprom_buff[i]);
514
515         /* Update the checksum over the first part of the EEPROM if needed */
516         if(first_word <= EEPROM_CHECKSUM_REG)
517                 ixgb_update_eeprom_checksum(hw);
518
519         kfree(eeprom_buff);
520         return 0;
521 }
522
523 static void
524 ixgb_get_drvinfo(struct net_device *netdev,
525                    struct ethtool_drvinfo *drvinfo)
526 {
527         struct ixgb_adapter *adapter = netdev_priv(netdev);
528
529         strncpy(drvinfo->driver,  ixgb_driver_name, 32);
530         strncpy(drvinfo->version, ixgb_driver_version, 32);
531         strncpy(drvinfo->fw_version, "N/A", 32);
532         strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
533         drvinfo->n_stats = IXGB_STATS_LEN;
534         drvinfo->regdump_len = ixgb_get_regs_len(netdev);
535         drvinfo->eedump_len = ixgb_get_eeprom_len(netdev);
536 }
537
538 static void
539 ixgb_get_ringparam(struct net_device *netdev,
540                 struct ethtool_ringparam *ring)
541 {
542         struct ixgb_adapter *adapter = netdev_priv(netdev);
543         struct ixgb_desc_ring *txdr = &adapter->tx_ring;
544         struct ixgb_desc_ring *rxdr = &adapter->rx_ring;
545
546         ring->rx_max_pending = MAX_RXD; 
547         ring->tx_max_pending = MAX_TXD;
548         ring->rx_mini_max_pending = 0;
549         ring->rx_jumbo_max_pending = 0;
550         ring->rx_pending = rxdr->count;
551         ring->tx_pending = txdr->count;
552         ring->rx_mini_pending = 0;
553         ring->rx_jumbo_pending = 0;
554 }
555
556 static int 
557 ixgb_set_ringparam(struct net_device *netdev,
558                 struct ethtool_ringparam *ring)
559 {
560         struct ixgb_adapter *adapter = netdev_priv(netdev);
561         struct ixgb_desc_ring *txdr = &adapter->tx_ring;
562         struct ixgb_desc_ring *rxdr = &adapter->rx_ring;
563         struct ixgb_desc_ring tx_old, tx_new, rx_old, rx_new;
564         int err;
565
566         tx_old = adapter->tx_ring;
567         rx_old = adapter->rx_ring;
568
569         if((ring->rx_mini_pending) || (ring->rx_jumbo_pending)) 
570                 return -EINVAL;
571
572         if(netif_running(adapter->netdev))
573                 ixgb_down(adapter,TRUE);
574
575         rxdr->count = max(ring->rx_pending,(uint32_t)MIN_RXD);
576         rxdr->count = min(rxdr->count,(uint32_t)MAX_RXD);
577         rxdr->count = ALIGN(rxdr->count, IXGB_REQ_RX_DESCRIPTOR_MULTIPLE);
578
579         txdr->count = max(ring->tx_pending,(uint32_t)MIN_TXD);
580         txdr->count = min(txdr->count,(uint32_t)MAX_TXD);
581         txdr->count = ALIGN(txdr->count, IXGB_REQ_TX_DESCRIPTOR_MULTIPLE);
582
583         if(netif_running(adapter->netdev)) {
584                 /* Try to get new resources before deleting old */
585                 if((err = ixgb_setup_rx_resources(adapter)))
586                         goto err_setup_rx;
587                 if((err = ixgb_setup_tx_resources(adapter)))
588                         goto err_setup_tx;
589
590                 /* save the new, restore the old in order to free it,
591                  * then restore the new back again */
592
593                 rx_new = adapter->rx_ring;
594                 tx_new = adapter->tx_ring;
595                 adapter->rx_ring = rx_old;
596                 adapter->tx_ring = tx_old;
597                 ixgb_free_rx_resources(adapter);
598                 ixgb_free_tx_resources(adapter);
599                 adapter->rx_ring = rx_new;
600                 adapter->tx_ring = tx_new;
601                 if((err = ixgb_up(adapter)))
602                         return err;
603                 ixgb_set_speed_duplex(netdev);
604         }
605
606         return 0;
607 err_setup_tx:
608         ixgb_free_rx_resources(adapter);
609 err_setup_rx:
610         adapter->rx_ring = rx_old;
611         adapter->tx_ring = tx_old;
612         ixgb_up(adapter);
613         return err;
614 }
615
616 /* toggle LED 4 times per second = 2 "blinks" per second */
617 #define IXGB_ID_INTERVAL        (HZ/4)
618
619 /* bit defines for adapter->led_status */
620 #define IXGB_LED_ON             0
621
622 static void
623 ixgb_led_blink_callback(unsigned long data)
624 {
625         struct ixgb_adapter *adapter = (struct ixgb_adapter *)data;
626
627         if(test_and_change_bit(IXGB_LED_ON, &adapter->led_status))
628                 ixgb_led_off(&adapter->hw);
629         else
630                 ixgb_led_on(&adapter->hw);
631
632         mod_timer(&adapter->blink_timer, jiffies + IXGB_ID_INTERVAL);
633 }
634
635 static int
636 ixgb_phys_id(struct net_device *netdev, uint32_t data)
637 {
638         struct ixgb_adapter *adapter = netdev_priv(netdev);
639
640         if (!data)
641                 data = INT_MAX;
642
643         if(!adapter->blink_timer.function) {
644                 init_timer(&adapter->blink_timer);
645                 adapter->blink_timer.function = ixgb_led_blink_callback;
646                 adapter->blink_timer.data = (unsigned long)adapter;
647         }
648
649         mod_timer(&adapter->blink_timer, jiffies);
650
651         msleep_interruptible(data * 1000);
652         del_timer_sync(&adapter->blink_timer);
653         ixgb_led_off(&adapter->hw);
654         clear_bit(IXGB_LED_ON, &adapter->led_status);
655
656         return 0;
657 }
658
659 static int 
660 ixgb_get_sset_count(struct net_device *netdev, int sset)
661 {
662         switch (sset) {
663         case ETH_SS_STATS:
664                 return IXGB_STATS_LEN;
665         default:
666                 return -EOPNOTSUPP;
667         }
668 }
669
670 static void 
671 ixgb_get_ethtool_stats(struct net_device *netdev, 
672                 struct ethtool_stats *stats, uint64_t *data)
673 {
674         struct ixgb_adapter *adapter = netdev_priv(netdev);
675         int i;
676
677         ixgb_update_stats(adapter);
678         for(i = 0; i < IXGB_STATS_LEN; i++) {
679                 char *p = (char *)adapter+ixgb_gstrings_stats[i].stat_offset;   
680                 data[i] = (ixgb_gstrings_stats[i].sizeof_stat == 
681                         sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
682         }
683 }
684
685 static void 
686 ixgb_get_strings(struct net_device *netdev, uint32_t stringset, uint8_t *data)
687 {
688         int i;
689
690         switch(stringset) {
691         case ETH_SS_STATS:
692                 for(i=0; i < IXGB_STATS_LEN; i++) {
693                         memcpy(data + i * ETH_GSTRING_LEN, 
694                         ixgb_gstrings_stats[i].stat_string,
695                         ETH_GSTRING_LEN);
696                 }
697                 break;
698         }
699 }
700
701 static const struct ethtool_ops ixgb_ethtool_ops = {
702         .get_settings = ixgb_get_settings,
703         .set_settings = ixgb_set_settings,
704         .get_drvinfo = ixgb_get_drvinfo,
705         .get_regs_len = ixgb_get_regs_len,
706         .get_regs = ixgb_get_regs,
707         .get_link = ethtool_op_get_link,
708         .get_eeprom_len = ixgb_get_eeprom_len,
709         .get_eeprom = ixgb_get_eeprom,
710         .set_eeprom = ixgb_set_eeprom,
711         .get_ringparam = ixgb_get_ringparam,
712         .set_ringparam = ixgb_set_ringparam,
713         .get_pauseparam = ixgb_get_pauseparam,
714         .set_pauseparam = ixgb_set_pauseparam,
715         .get_rx_csum = ixgb_get_rx_csum,
716         .set_rx_csum = ixgb_set_rx_csum,
717         .get_tx_csum = ixgb_get_tx_csum,
718         .set_tx_csum = ixgb_set_tx_csum,
719         .set_sg = ethtool_op_set_sg,
720         .get_msglevel = ixgb_get_msglevel,
721         .set_msglevel = ixgb_set_msglevel,
722         .set_tso = ixgb_set_tso,
723         .get_strings = ixgb_get_strings,
724         .phys_id = ixgb_phys_id,
725         .get_sset_count = ixgb_get_sset_count,
726         .get_ethtool_stats = ixgb_get_ethtool_stats,
727 };
728
729 void ixgb_set_ethtool_ops(struct net_device *netdev)
730 {
731         SET_ETHTOOL_OPS(netdev, &ixgb_ethtool_ops);
732 }