Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / drivers / net / netxen / netxen_nic_ethtool.c
1 /*
2  * Copyright (C) 2003 - 2009 NetXen, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18  * MA  02111-1307, USA.
19  *
20  * The full GNU General Public License is included in this distribution
21  * in the file called LICENSE.
22  *
23  * Contact Information:
24  *    info@netxen.com
25  * NetXen Inc,
26  * 18922 Forge Drive
27  * Cupertino, CA 95014-0701
28  *
29  */
30
31 #include <linux/types.h>
32 #include <linux/delay.h>
33 #include <asm/uaccess.h>
34 #include <linux/pci.h>
35 #include <asm/io.h>
36 #include <linux/netdevice.h>
37 #include <linux/ethtool.h>
38
39 #include "netxen_nic.h"
40 #include "netxen_nic_hw.h"
41 #include "netxen_nic_phan_reg.h"
42
43 struct netxen_nic_stats {
44         char stat_string[ETH_GSTRING_LEN];
45         int sizeof_stat;
46         int stat_offset;
47 };
48
49 #define NETXEN_NIC_STAT(m) sizeof(((struct netxen_adapter *)0)->m), \
50                         offsetof(struct netxen_adapter, m)
51
52 #define NETXEN_NIC_PORT_WINDOW 0x10000
53 #define NETXEN_NIC_INVALID_DATA 0xDEADBEEF
54
55 static const struct netxen_nic_stats netxen_nic_gstrings_stats[] = {
56         {"rcvd_bad_skb", NETXEN_NIC_STAT(stats.rcvdbadskb)},
57         {"xmit_called", NETXEN_NIC_STAT(stats.xmitcalled)},
58         {"xmited_frames", NETXEN_NIC_STAT(stats.xmitedframes)},
59         {"xmit_finished", NETXEN_NIC_STAT(stats.xmitfinished)},
60         {"bad_skb_len", NETXEN_NIC_STAT(stats.badskblen)},
61         {"no_cmd_desc", NETXEN_NIC_STAT(stats.nocmddescriptor)},
62         {"polled", NETXEN_NIC_STAT(stats.polled)},
63         {"tx_dropped", NETXEN_NIC_STAT(stats.txdropped)},
64         {"csummed", NETXEN_NIC_STAT(stats.csummed)},
65         {"no_rcv", NETXEN_NIC_STAT(stats.no_rcv)},
66         {"rx_bytes", NETXEN_NIC_STAT(stats.rxbytes)},
67         {"tx_bytes", NETXEN_NIC_STAT(stats.txbytes)},
68 };
69
70 #define NETXEN_NIC_STATS_LEN    ARRAY_SIZE(netxen_nic_gstrings_stats)
71
72 static const char netxen_nic_gstrings_test[][ETH_GSTRING_LEN] = {
73         "Register_Test_on_offline",
74         "Link_Test_on_offline"
75 };
76
77 #define NETXEN_NIC_TEST_LEN     ARRAY_SIZE(netxen_nic_gstrings_test)
78
79 #define NETXEN_NIC_REGS_COUNT 42
80 #define NETXEN_NIC_REGS_LEN (NETXEN_NIC_REGS_COUNT * sizeof(__le32))
81 #define NETXEN_MAX_EEPROM_LEN   1024
82
83 static int netxen_nic_get_eeprom_len(struct net_device *dev)
84 {
85         return NETXEN_FLASH_TOTAL_SIZE;
86 }
87
88 static void
89 netxen_nic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
90 {
91         struct netxen_adapter *adapter = netdev_priv(dev);
92         unsigned long flags;
93         u32 fw_major = 0;
94         u32 fw_minor = 0;
95         u32 fw_build = 0;
96
97         strncpy(drvinfo->driver, netxen_nic_driver_name, 32);
98         strncpy(drvinfo->version, NETXEN_NIC_LINUX_VERSIONID, 32);
99         write_lock_irqsave(&adapter->adapter_lock, flags);
100         fw_major = adapter->pci_read_normalize(adapter,
101                                         NETXEN_FW_VERSION_MAJOR);
102         fw_minor = adapter->pci_read_normalize(adapter,
103                                         NETXEN_FW_VERSION_MINOR);
104         fw_build = adapter->pci_read_normalize(adapter,
105                                         NETXEN_FW_VERSION_SUB);
106         write_unlock_irqrestore(&adapter->adapter_lock, flags);
107         sprintf(drvinfo->fw_version, "%d.%d.%d", fw_major, fw_minor, fw_build);
108
109         strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
110         drvinfo->regdump_len = NETXEN_NIC_REGS_LEN;
111         drvinfo->eedump_len = netxen_nic_get_eeprom_len(dev);
112 }
113
114 static int
115 netxen_nic_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
116 {
117         struct netxen_adapter *adapter = netdev_priv(dev);
118
119         /* read which mode */
120         if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
121                 ecmd->supported = (SUPPORTED_10baseT_Half |
122                                    SUPPORTED_10baseT_Full |
123                                    SUPPORTED_100baseT_Half |
124                                    SUPPORTED_100baseT_Full |
125                                    SUPPORTED_1000baseT_Half |
126                                    SUPPORTED_1000baseT_Full);
127
128                 ecmd->advertising = (ADVERTISED_100baseT_Half |
129                                      ADVERTISED_100baseT_Full |
130                                      ADVERTISED_1000baseT_Half |
131                                      ADVERTISED_1000baseT_Full);
132
133                 ecmd->port = PORT_TP;
134
135                 ecmd->speed = adapter->link_speed;
136                 ecmd->duplex = adapter->link_duplex;
137                 ecmd->autoneg = adapter->link_autoneg;
138
139         } else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
140                 u32 val;
141
142                 adapter->hw_read_wx(adapter, NETXEN_PORT_MODE_ADDR, &val, 4);
143                 if (val == NETXEN_PORT_MODE_802_3_AP) {
144                         ecmd->supported = SUPPORTED_1000baseT_Full;
145                         ecmd->advertising = ADVERTISED_1000baseT_Full;
146                 } else {
147                         ecmd->supported = SUPPORTED_10000baseT_Full;
148                         ecmd->advertising = ADVERTISED_10000baseT_Full;
149                 }
150
151                 ecmd->port = PORT_TP;
152
153                 if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
154                         u16 pcifn = adapter->ahw.pci_func;
155
156                         adapter->hw_read_wx(adapter,
157                                 P3_LINK_SPEED_REG(pcifn), &val, 4);
158                         ecmd->speed = P3_LINK_SPEED_MHZ *
159                                         P3_LINK_SPEED_VAL(pcifn, val);
160                 } else
161                         ecmd->speed = SPEED_10000;
162
163                 ecmd->duplex = DUPLEX_FULL;
164                 ecmd->autoneg = AUTONEG_DISABLE;
165         } else
166                 return -EIO;
167
168         ecmd->phy_address = adapter->physical_port;
169         ecmd->transceiver = XCVR_EXTERNAL;
170
171         switch ((netxen_brdtype_t)adapter->ahw.board_type) {
172         case NETXEN_BRDTYPE_P2_SB35_4G:
173         case NETXEN_BRDTYPE_P2_SB31_2G:
174         case NETXEN_BRDTYPE_P3_REF_QG:
175         case NETXEN_BRDTYPE_P3_4_GB:
176         case NETXEN_BRDTYPE_P3_4_GB_MM:
177
178                 ecmd->supported |= SUPPORTED_Autoneg;
179                 ecmd->advertising |= ADVERTISED_Autoneg;
180         case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
181         case NETXEN_BRDTYPE_P3_10G_CX4:
182         case NETXEN_BRDTYPE_P3_10G_CX4_LP:
183         case NETXEN_BRDTYPE_P3_10000_BASE_T:
184                 ecmd->supported |= SUPPORTED_TP;
185                 ecmd->advertising |= ADVERTISED_TP;
186                 ecmd->port = PORT_TP;
187                 ecmd->autoneg = (adapter->ahw.board_type ==
188                                  NETXEN_BRDTYPE_P2_SB31_10G_CX4) ?
189                     (AUTONEG_DISABLE) : (adapter->link_autoneg);
190                 break;
191         case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
192         case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
193         case NETXEN_BRDTYPE_P3_IMEZ:
194         case NETXEN_BRDTYPE_P3_XG_LOM:
195         case NETXEN_BRDTYPE_P3_HMEZ:
196                 ecmd->supported |= SUPPORTED_MII;
197                 ecmd->advertising |= ADVERTISED_MII;
198                 ecmd->port = PORT_FIBRE;
199                 ecmd->autoneg = AUTONEG_DISABLE;
200                 break;
201         case NETXEN_BRDTYPE_P3_10G_SFP_PLUS:
202         case NETXEN_BRDTYPE_P3_10G_SFP_CT:
203         case NETXEN_BRDTYPE_P3_10G_SFP_QT:
204                 ecmd->advertising |= ADVERTISED_TP;
205                 ecmd->supported |= SUPPORTED_TP;
206         case NETXEN_BRDTYPE_P2_SB31_10G:
207         case NETXEN_BRDTYPE_P3_10G_XFP:
208                 ecmd->supported |= SUPPORTED_FIBRE;
209                 ecmd->advertising |= ADVERTISED_FIBRE;
210                 ecmd->port = PORT_FIBRE;
211                 ecmd->autoneg = AUTONEG_DISABLE;
212                 break;
213         case NETXEN_BRDTYPE_P3_10G_TP:
214                 if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
215                         ecmd->autoneg = AUTONEG_DISABLE;
216                         ecmd->supported |= (SUPPORTED_FIBRE | SUPPORTED_TP);
217                         ecmd->advertising |=
218                                 (ADVERTISED_FIBRE | ADVERTISED_TP);
219                         ecmd->port = PORT_FIBRE;
220                 } else {
221                         ecmd->autoneg = AUTONEG_ENABLE;
222                         ecmd->supported |= (SUPPORTED_TP |SUPPORTED_Autoneg);
223                         ecmd->advertising |=
224                                 (ADVERTISED_TP | ADVERTISED_Autoneg);
225                         ecmd->port = PORT_TP;
226                 }
227                 break;
228         default:
229                 printk(KERN_ERR "netxen-nic: Unsupported board model %d\n",
230                        (netxen_brdtype_t)adapter->ahw.board_type);
231                 return -EIO;
232         }
233
234         return 0;
235 }
236
237 static int
238 netxen_nic_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
239 {
240         struct netxen_adapter *adapter = netdev_priv(dev);
241         __u32 status;
242
243         /* read which mode */
244         if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
245                 /* autonegotiation */
246                 if (adapter->phy_write
247                     && adapter->phy_write(adapter,
248                                           NETXEN_NIU_GB_MII_MGMT_ADDR_AUTONEG,
249                                           ecmd->autoneg) != 0)
250                         return -EIO;
251                 else
252                         adapter->link_autoneg = ecmd->autoneg;
253
254                 if (adapter->phy_read
255                     && adapter->phy_read(adapter,
256                                          NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
257                                          &status) != 0)
258                         return -EIO;
259
260                 /* speed */
261                 switch (ecmd->speed) {
262                 case SPEED_10:
263                         netxen_set_phy_speed(status, 0);
264                         break;
265                 case SPEED_100:
266                         netxen_set_phy_speed(status, 1);
267                         break;
268                 case SPEED_1000:
269                         netxen_set_phy_speed(status, 2);
270                         break;
271                 }
272                 /* set duplex mode */
273                 if (ecmd->duplex == DUPLEX_HALF)
274                         netxen_clear_phy_duplex(status);
275                 if (ecmd->duplex == DUPLEX_FULL)
276                         netxen_set_phy_duplex(status);
277                 if (adapter->phy_write
278                     && adapter->phy_write(adapter,
279                                           NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
280                                           *((int *)&status)) != 0)
281                         return -EIO;
282                 else {
283                         adapter->link_speed = ecmd->speed;
284                         adapter->link_duplex = ecmd->duplex;
285                 }
286         } else
287                 return -EOPNOTSUPP;
288
289         if (!netif_running(dev))
290                 return 0;
291
292         dev->netdev_ops->ndo_stop(dev);
293         return dev->netdev_ops->ndo_open(dev);
294 }
295
296 static int netxen_nic_get_regs_len(struct net_device *dev)
297 {
298         return NETXEN_NIC_REGS_LEN;
299 }
300
301 struct netxen_niu_regs {
302         __u32 reg[NETXEN_NIC_REGS_COUNT];
303 };
304
305 static struct netxen_niu_regs niu_registers[] = {
306         {
307          /* GB Mode */
308          {
309           NETXEN_NIU_GB_SERDES_RESET,
310           NETXEN_NIU_GB0_MII_MODE,
311           NETXEN_NIU_GB1_MII_MODE,
312           NETXEN_NIU_GB2_MII_MODE,
313           NETXEN_NIU_GB3_MII_MODE,
314           NETXEN_NIU_GB0_GMII_MODE,
315           NETXEN_NIU_GB1_GMII_MODE,
316           NETXEN_NIU_GB2_GMII_MODE,
317           NETXEN_NIU_GB3_GMII_MODE,
318           NETXEN_NIU_REMOTE_LOOPBACK,
319           NETXEN_NIU_GB0_HALF_DUPLEX,
320           NETXEN_NIU_GB1_HALF_DUPLEX,
321           NETXEN_NIU_RESET_SYS_FIFOS,
322           NETXEN_NIU_GB_CRC_DROP,
323           NETXEN_NIU_GB_DROP_WRONGADDR,
324           NETXEN_NIU_TEST_MUX_CTL,
325
326           NETXEN_NIU_GB_MAC_CONFIG_0(0),
327           NETXEN_NIU_GB_MAC_CONFIG_1(0),
328           NETXEN_NIU_GB_HALF_DUPLEX_CTRL(0),
329           NETXEN_NIU_GB_MAX_FRAME_SIZE(0),
330           NETXEN_NIU_GB_TEST_REG(0),
331           NETXEN_NIU_GB_MII_MGMT_CONFIG(0),
332           NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
333           NETXEN_NIU_GB_MII_MGMT_ADDR(0),
334           NETXEN_NIU_GB_MII_MGMT_CTRL(0),
335           NETXEN_NIU_GB_MII_MGMT_STATUS(0),
336           NETXEN_NIU_GB_MII_MGMT_INDICATE(0),
337           NETXEN_NIU_GB_INTERFACE_CTRL(0),
338           NETXEN_NIU_GB_INTERFACE_STATUS(0),
339           NETXEN_NIU_GB_STATION_ADDR_0(0),
340           NETXEN_NIU_GB_STATION_ADDR_1(0),
341           -1,
342           }
343          },
344         {
345          /* XG Mode */
346          {
347           NETXEN_NIU_XG_SINGLE_TERM,
348           NETXEN_NIU_XG_DRIVE_HI,
349           NETXEN_NIU_XG_DRIVE_LO,
350           NETXEN_NIU_XG_DTX,
351           NETXEN_NIU_XG_DEQ,
352           NETXEN_NIU_XG_WORD_ALIGN,
353           NETXEN_NIU_XG_RESET,
354           NETXEN_NIU_XG_POWER_DOWN,
355           NETXEN_NIU_XG_RESET_PLL,
356           NETXEN_NIU_XG_SERDES_LOOPBACK,
357           NETXEN_NIU_XG_DO_BYTE_ALIGN,
358           NETXEN_NIU_XG_TX_ENABLE,
359           NETXEN_NIU_XG_RX_ENABLE,
360           NETXEN_NIU_XG_STATUS,
361           NETXEN_NIU_XG_PAUSE_THRESHOLD,
362           NETXEN_NIU_XGE_CONFIG_0,
363           NETXEN_NIU_XGE_CONFIG_1,
364           NETXEN_NIU_XGE_IPG,
365           NETXEN_NIU_XGE_STATION_ADDR_0_HI,
366           NETXEN_NIU_XGE_STATION_ADDR_0_1,
367           NETXEN_NIU_XGE_STATION_ADDR_1_LO,
368           NETXEN_NIU_XGE_STATUS,
369           NETXEN_NIU_XGE_MAX_FRAME_SIZE,
370           NETXEN_NIU_XGE_PAUSE_FRAME_VALUE,
371           NETXEN_NIU_XGE_TX_BYTE_CNT,
372           NETXEN_NIU_XGE_TX_FRAME_CNT,
373           NETXEN_NIU_XGE_RX_BYTE_CNT,
374           NETXEN_NIU_XGE_RX_FRAME_CNT,
375           NETXEN_NIU_XGE_AGGR_ERROR_CNT,
376           NETXEN_NIU_XGE_MULTICAST_FRAME_CNT,
377           NETXEN_NIU_XGE_UNICAST_FRAME_CNT,
378           NETXEN_NIU_XGE_CRC_ERROR_CNT,
379           NETXEN_NIU_XGE_OVERSIZE_FRAME_ERR,
380           NETXEN_NIU_XGE_UNDERSIZE_FRAME_ERR,
381           NETXEN_NIU_XGE_LOCAL_ERROR_CNT,
382           NETXEN_NIU_XGE_REMOTE_ERROR_CNT,
383           NETXEN_NIU_XGE_CONTROL_CHAR_CNT,
384           NETXEN_NIU_XGE_PAUSE_FRAME_CNT,
385           -1,
386           }
387          }
388 };
389
390 static void
391 netxen_nic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
392 {
393         struct netxen_adapter *adapter = netdev_priv(dev);
394         __u32 mode, *regs_buff = p;
395         int i, window;
396
397         memset(p, 0, NETXEN_NIC_REGS_LEN);
398         regs->version = (1 << 24) | (adapter->ahw.revision_id << 16) |
399             (adapter->pdev)->device;
400         /* which mode */
401         adapter->hw_read_wx(adapter, NETXEN_NIU_MODE, &regs_buff[0], 4);
402         mode = regs_buff[0];
403
404         /* Common registers to all the modes */
405         adapter->hw_read_wx(adapter,
406                         NETXEN_NIU_STRAP_VALUE_SAVE_HIGHER, &regs_buff[2], 4);
407         /* GB/XGB Mode */
408         mode = (mode / 2) - 1;
409         window = 0;
410         if (mode <= 1) {
411                 for (i = 3; niu_registers[mode].reg[i - 3] != -1; i++) {
412                         /* GB: port specific registers */
413                         if (mode == 0 && i >= 19)
414                                 window = adapter->physical_port *
415                                         NETXEN_NIC_PORT_WINDOW;
416
417                         adapter->hw_read_wx(adapter,
418                                 niu_registers[mode].reg[i - 3] + window,
419                                 &regs_buff[i], 4);
420                 }
421
422         }
423 }
424
425 static u32 netxen_nic_test_link(struct net_device *dev)
426 {
427         struct netxen_adapter *adapter = netdev_priv(dev);
428         __u32 status;
429         int val;
430
431         /* read which mode */
432         if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
433                 if (adapter->phy_read
434                     && adapter->phy_read(adapter,
435                                          NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
436                                          &status) != 0)
437                         return -EIO;
438                 else {
439                         val = netxen_get_phy_link(status);
440                         return !val;
441                 }
442         } else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
443                 val = adapter->pci_read_normalize(adapter, CRB_XG_STATE);
444                 return (val == XG_LINK_UP) ? 0 : 1;
445         }
446         return -EIO;
447 }
448
449 static int
450 netxen_nic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
451                       u8 * bytes)
452 {
453         struct netxen_adapter *adapter = netdev_priv(dev);
454         int offset;
455         int ret;
456
457         if (eeprom->len == 0)
458                 return -EINVAL;
459
460         eeprom->magic = (adapter->pdev)->vendor |
461                         ((adapter->pdev)->device << 16);
462         offset = eeprom->offset;
463
464         ret = netxen_rom_fast_read_words(adapter, offset, bytes,
465                                                 eeprom->len);
466         if (ret < 0)
467                 return ret;
468
469         return 0;
470 }
471
472 static void
473 netxen_nic_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ring)
474 {
475         struct netxen_adapter *adapter = netdev_priv(dev);
476
477         ring->rx_pending = 0;
478         ring->rx_jumbo_pending = 0;
479         ring->rx_pending += adapter->recv_ctx.
480                 rds_rings[RCV_RING_NORMAL].num_desc;
481         ring->rx_jumbo_pending += adapter->recv_ctx.
482                 rds_rings[RCV_RING_JUMBO].num_desc;
483         ring->tx_pending = adapter->num_txd;
484
485         if (adapter->ahw.port_type == NETXEN_NIC_GBE)
486                 ring->rx_max_pending = MAX_RCV_DESCRIPTORS_1G;
487         else
488                 ring->rx_max_pending = MAX_RCV_DESCRIPTORS_10G;
489         ring->tx_max_pending = MAX_CMD_DESCRIPTORS_HOST;
490         ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS;
491         ring->rx_mini_max_pending = 0;
492         ring->rx_mini_pending = 0;
493 }
494
495 static void
496 netxen_nic_get_pauseparam(struct net_device *dev,
497                           struct ethtool_pauseparam *pause)
498 {
499         struct netxen_adapter *adapter = netdev_priv(dev);
500         __u32 val;
501         int port = adapter->physical_port;
502
503         if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
504                 if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
505                         return;
506                 /* get flow control settings */
507                 netxen_nic_read_w0(adapter,NETXEN_NIU_GB_MAC_CONFIG_0(port),
508                                 &val);
509                 pause->rx_pause = netxen_gb_get_rx_flowctl(val);
510                 netxen_nic_read_w0(adapter, NETXEN_NIU_GB_PAUSE_CTL, &val);
511                 switch (port) {
512                         case 0:
513                                 pause->tx_pause = !(netxen_gb_get_gb0_mask(val));
514                                 break;
515                         case 1:
516                                 pause->tx_pause = !(netxen_gb_get_gb1_mask(val));
517                                 break;
518                         case 2:
519                                 pause->tx_pause = !(netxen_gb_get_gb2_mask(val));
520                                 break;
521                         case 3:
522                         default:
523                                 pause->tx_pause = !(netxen_gb_get_gb3_mask(val));
524                                 break;
525                 }
526         } else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
527                 if ((port < 0) || (port > NETXEN_NIU_MAX_XG_PORTS))
528                         return;
529                 pause->rx_pause = 1;
530                 netxen_nic_read_w0(adapter, NETXEN_NIU_XG_PAUSE_CTL, &val);
531                 if (port == 0)
532                         pause->tx_pause = !(netxen_xg_get_xg0_mask(val));
533                 else
534                         pause->tx_pause = !(netxen_xg_get_xg1_mask(val));
535         } else {
536                 printk(KERN_ERR"%s: Unknown board type: %x\n",
537                                 netxen_nic_driver_name, adapter->ahw.port_type);
538         }
539 }
540
541 static int
542 netxen_nic_set_pauseparam(struct net_device *dev,
543                           struct ethtool_pauseparam *pause)
544 {
545         struct netxen_adapter *adapter = netdev_priv(dev);
546         __u32 val;
547         int port = adapter->physical_port;
548         /* read mode */
549         if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
550                 if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
551                         return -EIO;
552                 /* set flow control */
553                 netxen_nic_read_w0(adapter,
554                                         NETXEN_NIU_GB_MAC_CONFIG_0(port), &val);
555
556                 if (pause->rx_pause)
557                         netxen_gb_rx_flowctl(val);
558                 else
559                         netxen_gb_unset_rx_flowctl(val);
560
561                 netxen_nic_write_w0(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
562                                 val);
563                 /* set autoneg */
564                 netxen_nic_read_w0(adapter, NETXEN_NIU_GB_PAUSE_CTL, &val);
565                 switch (port) {
566                         case 0:
567                                 if (pause->tx_pause)
568                                         netxen_gb_unset_gb0_mask(val);
569                                 else
570                                         netxen_gb_set_gb0_mask(val);
571                                 break;
572                         case 1:
573                                 if (pause->tx_pause)
574                                         netxen_gb_unset_gb1_mask(val);
575                                 else
576                                         netxen_gb_set_gb1_mask(val);
577                                 break;
578                         case 2:
579                                 if (pause->tx_pause)
580                                         netxen_gb_unset_gb2_mask(val);
581                                 else
582                                         netxen_gb_set_gb2_mask(val);
583                                 break;
584                         case 3:
585                         default:
586                                 if (pause->tx_pause)
587                                         netxen_gb_unset_gb3_mask(val);
588                                 else
589                                         netxen_gb_set_gb3_mask(val);
590                                 break;
591                 }
592                 netxen_nic_write_w0(adapter, NETXEN_NIU_GB_PAUSE_CTL, val);
593         } else if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
594                 if ((port < 0) || (port > NETXEN_NIU_MAX_XG_PORTS))
595                         return -EIO;
596                 netxen_nic_read_w0(adapter, NETXEN_NIU_XG_PAUSE_CTL, &val);
597                 if (port == 0) {
598                         if (pause->tx_pause)
599                                 netxen_xg_unset_xg0_mask(val);
600                         else
601                                 netxen_xg_set_xg0_mask(val);
602                 } else {
603                         if (pause->tx_pause)
604                                 netxen_xg_unset_xg1_mask(val);
605                         else
606                                 netxen_xg_set_xg1_mask(val);
607                 }
608                 netxen_nic_write_w0(adapter, NETXEN_NIU_XG_PAUSE_CTL, val);
609         } else {
610                 printk(KERN_ERR "%s: Unknown board type: %x\n",
611                                 netxen_nic_driver_name,
612                                 adapter->ahw.port_type);
613         }
614         return 0;
615 }
616
617 static int netxen_nic_reg_test(struct net_device *dev)
618 {
619         struct netxen_adapter *adapter = netdev_priv(dev);
620         u32 data_read, data_written;
621
622         netxen_nic_read_w0(adapter, NETXEN_PCIX_PH_REG(0), &data_read);
623         if ((data_read & 0xffff) != PHAN_VENDOR_ID)
624         return 1;
625
626         data_written = (u32)0xa5a5a5a5;
627
628         netxen_nic_reg_write(adapter, CRB_SCRATCHPAD_TEST, data_written);
629         data_read = adapter->pci_read_normalize(adapter, CRB_SCRATCHPAD_TEST);
630         if (data_written != data_read)
631                 return 1;
632
633         return 0;
634 }
635
636 static int netxen_get_sset_count(struct net_device *dev, int sset)
637 {
638         switch (sset) {
639         case ETH_SS_TEST:
640                 return NETXEN_NIC_TEST_LEN;
641         case ETH_SS_STATS:
642                 return NETXEN_NIC_STATS_LEN;
643         default:
644                 return -EOPNOTSUPP;
645         }
646 }
647
648 static void
649 netxen_nic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
650                      u64 * data)
651 {
652         memset(data, 0, sizeof(uint64_t) * NETXEN_NIC_TEST_LEN);
653         if ((data[0] = netxen_nic_reg_test(dev)))
654                 eth_test->flags |= ETH_TEST_FL_FAILED;
655         /* link test */
656         if ((data[1] = (u64) netxen_nic_test_link(dev)))
657                 eth_test->flags |= ETH_TEST_FL_FAILED;
658 }
659
660 static void
661 netxen_nic_get_strings(struct net_device *dev, u32 stringset, u8 * data)
662 {
663         int index;
664
665         switch (stringset) {
666         case ETH_SS_TEST:
667                 memcpy(data, *netxen_nic_gstrings_test,
668                        NETXEN_NIC_TEST_LEN * ETH_GSTRING_LEN);
669                 break;
670         case ETH_SS_STATS:
671                 for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
672                         memcpy(data + index * ETH_GSTRING_LEN,
673                                netxen_nic_gstrings_stats[index].stat_string,
674                                ETH_GSTRING_LEN);
675                 }
676                 break;
677         }
678 }
679
680 static void
681 netxen_nic_get_ethtool_stats(struct net_device *dev,
682                              struct ethtool_stats *stats, u64 * data)
683 {
684         struct netxen_adapter *adapter = netdev_priv(dev);
685         int index;
686
687         for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
688                 char *p =
689                     (char *)adapter +
690                     netxen_nic_gstrings_stats[index].stat_offset;
691                 data[index] =
692                     (netxen_nic_gstrings_stats[index].sizeof_stat ==
693                      sizeof(u64)) ? *(u64 *) p : *(u32 *) p;
694         }
695 }
696
697 static u32 netxen_nic_get_rx_csum(struct net_device *dev)
698 {
699         struct netxen_adapter *adapter = netdev_priv(dev);
700         return adapter->rx_csum;
701 }
702
703 static int netxen_nic_set_rx_csum(struct net_device *dev, u32 data)
704 {
705         struct netxen_adapter *adapter = netdev_priv(dev);
706         adapter->rx_csum = !!data;
707         return 0;
708 }
709
710 static u32 netxen_nic_get_tso(struct net_device *dev)
711 {
712         struct netxen_adapter *adapter = netdev_priv(dev);
713
714         if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
715                 return (dev->features & (NETIF_F_TSO | NETIF_F_TSO6)) != 0;
716
717         return (dev->features & NETIF_F_TSO) != 0;
718 }
719
720 static int netxen_nic_set_tso(struct net_device *dev, u32 data)
721 {
722         if (data) {
723                 struct netxen_adapter *adapter = netdev_priv(dev);
724
725                 dev->features |= NETIF_F_TSO;
726                 if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
727                         dev->features |= NETIF_F_TSO6;
728         } else
729                 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
730
731         return 0;
732 }
733
734 static void
735 netxen_nic_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
736 {
737         struct netxen_adapter *adapter = netdev_priv(dev);
738         u32 wol_cfg = 0;
739
740         wol->supported = 0;
741         wol->wolopts = 0;
742
743         if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
744                 return;
745
746         wol_cfg = netxen_nic_reg_read(adapter, NETXEN_WOL_CONFIG_NV);
747         if (wol_cfg & (1UL << adapter->portnum))
748                 wol->supported |= WAKE_MAGIC;
749
750         wol_cfg = netxen_nic_reg_read(adapter, NETXEN_WOL_CONFIG);
751         if (wol_cfg & (1UL << adapter->portnum))
752                 wol->wolopts |= WAKE_MAGIC;
753 }
754
755 static int
756 netxen_nic_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
757 {
758         struct netxen_adapter *adapter = netdev_priv(dev);
759         u32 wol_cfg = 0;
760
761         if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
762                 return -EOPNOTSUPP;
763
764         if (wol->wolopts & ~WAKE_MAGIC)
765                 return -EOPNOTSUPP;
766
767         wol_cfg = netxen_nic_reg_read(adapter, NETXEN_WOL_CONFIG_NV);
768         if (!(wol_cfg & (1 << adapter->portnum)))
769                 return -EOPNOTSUPP;
770
771         wol_cfg = netxen_nic_reg_read(adapter, NETXEN_WOL_CONFIG);
772         if (wol->wolopts & WAKE_MAGIC)
773                 wol_cfg |= 1UL << adapter->portnum;
774         else
775                 wol_cfg &= ~(1UL << adapter->portnum);
776         netxen_nic_reg_write(adapter, NETXEN_WOL_CONFIG, wol_cfg);
777
778         return 0;
779 }
780
781 /*
782  * Set the coalescing parameters. Currently only normal is supported.
783  * If rx_coalesce_usecs == 0 or rx_max_coalesced_frames == 0 then set the
784  * firmware coalescing to default.
785  */
786 static int netxen_set_intr_coalesce(struct net_device *netdev,
787                         struct ethtool_coalesce *ethcoal)
788 {
789         struct netxen_adapter *adapter = netdev_priv(netdev);
790
791         if (!NX_IS_REVISION_P3(adapter->ahw.revision_id))
792                 return -EINVAL;
793
794         if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
795                 return -EINVAL;
796
797         /*
798         * Return Error if unsupported values or
799         * unsupported parameters are set.
800         */
801         if (ethcoal->rx_coalesce_usecs > 0xffff ||
802                 ethcoal->rx_max_coalesced_frames > 0xffff ||
803                 ethcoal->tx_coalesce_usecs > 0xffff ||
804                 ethcoal->tx_max_coalesced_frames > 0xffff ||
805                 ethcoal->rx_coalesce_usecs_irq ||
806                 ethcoal->rx_max_coalesced_frames_irq ||
807                 ethcoal->tx_coalesce_usecs_irq ||
808                 ethcoal->tx_max_coalesced_frames_irq ||
809                 ethcoal->stats_block_coalesce_usecs ||
810                 ethcoal->use_adaptive_rx_coalesce ||
811                 ethcoal->use_adaptive_tx_coalesce ||
812                 ethcoal->pkt_rate_low ||
813                 ethcoal->rx_coalesce_usecs_low ||
814                 ethcoal->rx_max_coalesced_frames_low ||
815                 ethcoal->tx_coalesce_usecs_low ||
816                 ethcoal->tx_max_coalesced_frames_low ||
817                 ethcoal->pkt_rate_high ||
818                 ethcoal->rx_coalesce_usecs_high ||
819                 ethcoal->rx_max_coalesced_frames_high ||
820                 ethcoal->tx_coalesce_usecs_high ||
821                 ethcoal->tx_max_coalesced_frames_high)
822                 return -EINVAL;
823
824         if (!ethcoal->rx_coalesce_usecs ||
825                 !ethcoal->rx_max_coalesced_frames) {
826                 adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
827                 adapter->coal.normal.data.rx_time_us =
828                         NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
829                 adapter->coal.normal.data.rx_packets =
830                         NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
831         } else {
832                 adapter->coal.flags = 0;
833                 adapter->coal.normal.data.rx_time_us =
834                 ethcoal->rx_coalesce_usecs;
835                 adapter->coal.normal.data.rx_packets =
836                 ethcoal->rx_max_coalesced_frames;
837         }
838         adapter->coal.normal.data.tx_time_us = ethcoal->tx_coalesce_usecs;
839         adapter->coal.normal.data.tx_packets =
840         ethcoal->tx_max_coalesced_frames;
841
842         netxen_config_intr_coalesce(adapter);
843
844         return 0;
845 }
846
847 static int netxen_get_intr_coalesce(struct net_device *netdev,
848                         struct ethtool_coalesce *ethcoal)
849 {
850         struct netxen_adapter *adapter = netdev_priv(netdev);
851
852         if (!NX_IS_REVISION_P3(adapter->ahw.revision_id))
853                 return -EINVAL;
854
855         if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
856                 return -EINVAL;
857
858         ethcoal->rx_coalesce_usecs = adapter->coal.normal.data.rx_time_us;
859         ethcoal->tx_coalesce_usecs = adapter->coal.normal.data.tx_time_us;
860         ethcoal->rx_max_coalesced_frames =
861                 adapter->coal.normal.data.rx_packets;
862         ethcoal->tx_max_coalesced_frames =
863                 adapter->coal.normal.data.tx_packets;
864
865         return 0;
866 }
867
868 struct ethtool_ops netxen_nic_ethtool_ops = {
869         .get_settings = netxen_nic_get_settings,
870         .set_settings = netxen_nic_set_settings,
871         .get_drvinfo = netxen_nic_get_drvinfo,
872         .get_regs_len = netxen_nic_get_regs_len,
873         .get_regs = netxen_nic_get_regs,
874         .get_link = ethtool_op_get_link,
875         .get_eeprom_len = netxen_nic_get_eeprom_len,
876         .get_eeprom = netxen_nic_get_eeprom,
877         .get_ringparam = netxen_nic_get_ringparam,
878         .get_pauseparam = netxen_nic_get_pauseparam,
879         .set_pauseparam = netxen_nic_set_pauseparam,
880         .set_tx_csum = ethtool_op_set_tx_csum,
881         .set_sg = ethtool_op_set_sg,
882         .get_tso = netxen_nic_get_tso,
883         .set_tso = netxen_nic_set_tso,
884         .get_wol = netxen_nic_get_wol,
885         .set_wol = netxen_nic_set_wol,
886         .self_test = netxen_nic_diag_test,
887         .get_strings = netxen_nic_get_strings,
888         .get_ethtool_stats = netxen_nic_get_ethtool_stats,
889         .get_sset_count = netxen_get_sset_count,
890         .get_rx_csum = netxen_nic_get_rx_csum,
891         .set_rx_csum = netxen_nic_set_rx_csum,
892         .get_coalesce = netxen_get_intr_coalesce,
893         .set_coalesce = netxen_set_intr_coalesce,
894 };