netxen: remove low level tx lock
[pandora-kernel.git] / drivers / net / netxen / netxen_nic_ethtool.c
1 /*
2  * Copyright (C) 2003 - 2006 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,
26  * 3965 Freedom Circle, Fourth floor,
27  * Santa Clara, CA 95054
28  *
29  *
30  * ethtool support for netxen nic
31  *
32  */
33
34 #include <linux/types.h>
35 #include <linux/delay.h>
36 #include <asm/uaccess.h>
37 #include <linux/pci.h>
38 #include <asm/io.h>
39 #include <linux/netdevice.h>
40 #include <linux/ethtool.h>
41 #include <linux/version.h>
42
43 #include "netxen_nic.h"
44 #include "netxen_nic_hw.h"
45 #include "netxen_nic_phan_reg.h"
46
47 struct netxen_nic_stats {
48         char stat_string[ETH_GSTRING_LEN];
49         int sizeof_stat;
50         int stat_offset;
51 };
52
53 #define NETXEN_NIC_STAT(m) sizeof(((struct netxen_adapter *)0)->m), \
54                         offsetof(struct netxen_adapter, m)
55
56 #define NETXEN_NIC_PORT_WINDOW 0x10000
57 #define NETXEN_NIC_INVALID_DATA 0xDEADBEEF
58
59 static const struct netxen_nic_stats netxen_nic_gstrings_stats[] = {
60         {"rcvd_bad_skb", NETXEN_NIC_STAT(stats.rcvdbadskb)},
61         {"xmit_called", NETXEN_NIC_STAT(stats.xmitcalled)},
62         {"xmited_frames", NETXEN_NIC_STAT(stats.xmitedframes)},
63         {"xmit_finished", NETXEN_NIC_STAT(stats.xmitfinished)},
64         {"bad_skb_len", NETXEN_NIC_STAT(stats.badskblen)},
65         {"no_cmd_desc", NETXEN_NIC_STAT(stats.nocmddescriptor)},
66         {"polled", NETXEN_NIC_STAT(stats.polled)},
67         {"uphappy", NETXEN_NIC_STAT(stats.uphappy)},
68         {"updropped", NETXEN_NIC_STAT(stats.updropped)},
69         {"uplcong", NETXEN_NIC_STAT(stats.uplcong)},
70         {"uphcong", NETXEN_NIC_STAT(stats.uphcong)},
71         {"upmcong", NETXEN_NIC_STAT(stats.upmcong)},
72         {"updunno", NETXEN_NIC_STAT(stats.updunno)},
73         {"tx_dropped", NETXEN_NIC_STAT(stats.txdropped)},
74         {"csummed", NETXEN_NIC_STAT(stats.csummed)},
75         {"no_rcv", NETXEN_NIC_STAT(stats.no_rcv)},
76         {"rx_bytes", NETXEN_NIC_STAT(stats.rxbytes)},
77         {"tx_bytes", NETXEN_NIC_STAT(stats.txbytes)},
78 };
79
80 #define NETXEN_NIC_STATS_LEN    ARRAY_SIZE(netxen_nic_gstrings_stats)
81
82 static const char netxen_nic_gstrings_test[][ETH_GSTRING_LEN] = {
83         "Register_Test_on_offline",
84         "Link_Test_on_offline"
85 };
86
87 #define NETXEN_NIC_TEST_LEN     ARRAY_SIZE(netxen_nic_gstrings_test)
88
89 #define NETXEN_NIC_REGS_COUNT 42
90 #define NETXEN_NIC_REGS_LEN (NETXEN_NIC_REGS_COUNT * sizeof(__le32))
91 #define NETXEN_MAX_EEPROM_LEN   1024
92
93 static int netxen_nic_get_eeprom_len(struct net_device *dev)
94 {
95         return NETXEN_FLASH_TOTAL_SIZE;
96 }
97
98 static void
99 netxen_nic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
100 {
101         struct netxen_adapter *adapter = netdev_priv(dev);
102         u32 fw_major = 0;
103         u32 fw_minor = 0;
104         u32 fw_build = 0;
105
106         strncpy(drvinfo->driver, netxen_nic_driver_name, 32);
107         strncpy(drvinfo->version, NETXEN_NIC_LINUX_VERSIONID, 32);
108         fw_major = readl(NETXEN_CRB_NORMALIZE(adapter,
109                                               NETXEN_FW_VERSION_MAJOR));
110         fw_minor = readl(NETXEN_CRB_NORMALIZE(adapter,
111                                               NETXEN_FW_VERSION_MINOR));
112         fw_build = readl(NETXEN_CRB_NORMALIZE(adapter, NETXEN_FW_VERSION_SUB));
113         sprintf(drvinfo->fw_version, "%d.%d.%d", fw_major, fw_minor, fw_build);
114
115         strncpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
116         drvinfo->regdump_len = NETXEN_NIC_REGS_LEN;
117         drvinfo->eedump_len = netxen_nic_get_eeprom_len(dev);
118 }
119
120 static int
121 netxen_nic_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
122 {
123         struct netxen_adapter *adapter = netdev_priv(dev);
124         struct netxen_board_info *boardinfo = &adapter->ahw.boardcfg;
125
126         /* read which mode */
127         if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
128                 ecmd->supported = (SUPPORTED_10baseT_Half |
129                                    SUPPORTED_10baseT_Full |
130                                    SUPPORTED_100baseT_Half |
131                                    SUPPORTED_100baseT_Full |
132                                    SUPPORTED_1000baseT_Half |
133                                    SUPPORTED_1000baseT_Full);
134
135                 ecmd->advertising = (ADVERTISED_100baseT_Half |
136                                      ADVERTISED_100baseT_Full |
137                                      ADVERTISED_1000baseT_Half |
138                                      ADVERTISED_1000baseT_Full);
139
140                 ecmd->port = PORT_TP;
141
142                 if (netif_running(dev)) {
143                         ecmd->speed = adapter->link_speed;
144                         ecmd->duplex = adapter->link_duplex;
145                 } else
146                         return -EIO;    /* link absent */
147         } else if (adapter->ahw.board_type == NETXEN_NIC_XGBE) {
148                 ecmd->supported = (SUPPORTED_TP |
149                                    SUPPORTED_1000baseT_Full |
150                                    SUPPORTED_10000baseT_Full);
151                 ecmd->advertising = (ADVERTISED_TP |
152                                      ADVERTISED_1000baseT_Full |
153                                      ADVERTISED_10000baseT_Full);
154                 ecmd->port = PORT_TP;
155
156                 ecmd->speed = SPEED_10000;
157                 ecmd->duplex = DUPLEX_FULL;
158                 ecmd->autoneg = AUTONEG_DISABLE;
159         } else
160                 return -EIO;
161
162         ecmd->phy_address = adapter->portnum;
163         ecmd->transceiver = XCVR_EXTERNAL;
164
165         switch ((netxen_brdtype_t) boardinfo->board_type) {
166         case NETXEN_BRDTYPE_P2_SB35_4G:
167         case NETXEN_BRDTYPE_P2_SB31_2G:
168                 ecmd->supported |= SUPPORTED_Autoneg;
169                 ecmd->advertising |= ADVERTISED_Autoneg;
170         case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
171                 ecmd->supported |= SUPPORTED_TP;
172                 ecmd->advertising |= ADVERTISED_TP;
173                 ecmd->port = PORT_TP;
174                 ecmd->autoneg = (boardinfo->board_type ==
175                                  NETXEN_BRDTYPE_P2_SB31_10G_CX4) ?
176                     (AUTONEG_DISABLE) : (adapter->link_autoneg);
177                 break;
178         case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
179         case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
180                 ecmd->supported |= SUPPORTED_MII;
181                 ecmd->advertising |= ADVERTISED_MII;
182                 ecmd->port = PORT_FIBRE;
183                 ecmd->autoneg = AUTONEG_DISABLE;
184                 break;
185         case NETXEN_BRDTYPE_P2_SB31_10G:
186                 ecmd->supported |= SUPPORTED_FIBRE;
187                 ecmd->advertising |= ADVERTISED_FIBRE;
188                 ecmd->port = PORT_FIBRE;
189                 ecmd->autoneg = AUTONEG_DISABLE;
190                 break;
191         default:
192                 printk(KERN_ERR "netxen-nic: Unsupported board model %d\n",
193                        (netxen_brdtype_t) boardinfo->board_type);
194                 return -EIO;
195         }
196
197         return 0;
198 }
199
200 static int
201 netxen_nic_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
202 {
203         struct netxen_adapter *adapter = netdev_priv(dev);
204         __u32 status;
205
206         /* read which mode */
207         if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
208                 /* autonegotiation */
209                 if (adapter->phy_write
210                     && adapter->phy_write(adapter,
211                                           NETXEN_NIU_GB_MII_MGMT_ADDR_AUTONEG,
212                                           ecmd->autoneg) != 0)
213                         return -EIO;
214                 else
215                         adapter->link_autoneg = ecmd->autoneg;
216
217                 if (adapter->phy_read
218                     && adapter->phy_read(adapter,
219                                          NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
220                                          &status) != 0)
221                         return -EIO;
222
223                 /* speed */
224                 switch (ecmd->speed) {
225                 case SPEED_10:
226                         netxen_set_phy_speed(status, 0);
227                         break;
228                 case SPEED_100:
229                         netxen_set_phy_speed(status, 1);
230                         break;
231                 case SPEED_1000:
232                         netxen_set_phy_speed(status, 2);
233                         break;
234                 }
235                 /* set duplex mode */
236                 if (ecmd->duplex == DUPLEX_HALF)
237                         netxen_clear_phy_duplex(status);
238                 if (ecmd->duplex == DUPLEX_FULL)
239                         netxen_set_phy_duplex(status);
240                 if (adapter->phy_write
241                     && adapter->phy_write(adapter,
242                                           NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
243                                           *((int *)&status)) != 0)
244                         return -EIO;
245                 else {
246                         adapter->link_speed = ecmd->speed;
247                         adapter->link_duplex = ecmd->duplex;
248                 }
249         } else
250                 return -EOPNOTSUPP;
251
252         if (netif_running(dev)) {
253                 dev->stop(dev);
254                 dev->open(dev);
255         }
256         return 0;
257 }
258
259 static int netxen_nic_get_regs_len(struct net_device *dev)
260 {
261         return NETXEN_NIC_REGS_LEN;
262 }
263
264 struct netxen_niu_regs {
265         __u32 reg[NETXEN_NIC_REGS_COUNT];
266 };
267
268 static struct netxen_niu_regs niu_registers[] = {
269         {
270          /* GB Mode */
271          {
272           NETXEN_NIU_GB_SERDES_RESET,
273           NETXEN_NIU_GB0_MII_MODE,
274           NETXEN_NIU_GB1_MII_MODE,
275           NETXEN_NIU_GB2_MII_MODE,
276           NETXEN_NIU_GB3_MII_MODE,
277           NETXEN_NIU_GB0_GMII_MODE,
278           NETXEN_NIU_GB1_GMII_MODE,
279           NETXEN_NIU_GB2_GMII_MODE,
280           NETXEN_NIU_GB3_GMII_MODE,
281           NETXEN_NIU_REMOTE_LOOPBACK,
282           NETXEN_NIU_GB0_HALF_DUPLEX,
283           NETXEN_NIU_GB1_HALF_DUPLEX,
284           NETXEN_NIU_RESET_SYS_FIFOS,
285           NETXEN_NIU_GB_CRC_DROP,
286           NETXEN_NIU_GB_DROP_WRONGADDR,
287           NETXEN_NIU_TEST_MUX_CTL,
288
289           NETXEN_NIU_GB_MAC_CONFIG_0(0),
290           NETXEN_NIU_GB_MAC_CONFIG_1(0),
291           NETXEN_NIU_GB_HALF_DUPLEX_CTRL(0),
292           NETXEN_NIU_GB_MAX_FRAME_SIZE(0),
293           NETXEN_NIU_GB_TEST_REG(0),
294           NETXEN_NIU_GB_MII_MGMT_CONFIG(0),
295           NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
296           NETXEN_NIU_GB_MII_MGMT_ADDR(0),
297           NETXEN_NIU_GB_MII_MGMT_CTRL(0),
298           NETXEN_NIU_GB_MII_MGMT_STATUS(0),
299           NETXEN_NIU_GB_MII_MGMT_INDICATE(0),
300           NETXEN_NIU_GB_INTERFACE_CTRL(0),
301           NETXEN_NIU_GB_INTERFACE_STATUS(0),
302           NETXEN_NIU_GB_STATION_ADDR_0(0),
303           NETXEN_NIU_GB_STATION_ADDR_1(0),
304           -1,
305           }
306          },
307         {
308          /* XG Mode */
309          {
310           NETXEN_NIU_XG_SINGLE_TERM,
311           NETXEN_NIU_XG_DRIVE_HI,
312           NETXEN_NIU_XG_DRIVE_LO,
313           NETXEN_NIU_XG_DTX,
314           NETXEN_NIU_XG_DEQ,
315           NETXEN_NIU_XG_WORD_ALIGN,
316           NETXEN_NIU_XG_RESET,
317           NETXEN_NIU_XG_POWER_DOWN,
318           NETXEN_NIU_XG_RESET_PLL,
319           NETXEN_NIU_XG_SERDES_LOOPBACK,
320           NETXEN_NIU_XG_DO_BYTE_ALIGN,
321           NETXEN_NIU_XG_TX_ENABLE,
322           NETXEN_NIU_XG_RX_ENABLE,
323           NETXEN_NIU_XG_STATUS,
324           NETXEN_NIU_XG_PAUSE_THRESHOLD,
325           NETXEN_NIU_XGE_CONFIG_0,
326           NETXEN_NIU_XGE_CONFIG_1,
327           NETXEN_NIU_XGE_IPG,
328           NETXEN_NIU_XGE_STATION_ADDR_0_HI,
329           NETXEN_NIU_XGE_STATION_ADDR_0_1,
330           NETXEN_NIU_XGE_STATION_ADDR_1_LO,
331           NETXEN_NIU_XGE_STATUS,
332           NETXEN_NIU_XGE_MAX_FRAME_SIZE,
333           NETXEN_NIU_XGE_PAUSE_FRAME_VALUE,
334           NETXEN_NIU_XGE_TX_BYTE_CNT,
335           NETXEN_NIU_XGE_TX_FRAME_CNT,
336           NETXEN_NIU_XGE_RX_BYTE_CNT,
337           NETXEN_NIU_XGE_RX_FRAME_CNT,
338           NETXEN_NIU_XGE_AGGR_ERROR_CNT,
339           NETXEN_NIU_XGE_MULTICAST_FRAME_CNT,
340           NETXEN_NIU_XGE_UNICAST_FRAME_CNT,
341           NETXEN_NIU_XGE_CRC_ERROR_CNT,
342           NETXEN_NIU_XGE_OVERSIZE_FRAME_ERR,
343           NETXEN_NIU_XGE_UNDERSIZE_FRAME_ERR,
344           NETXEN_NIU_XGE_LOCAL_ERROR_CNT,
345           NETXEN_NIU_XGE_REMOTE_ERROR_CNT,
346           NETXEN_NIU_XGE_CONTROL_CHAR_CNT,
347           NETXEN_NIU_XGE_PAUSE_FRAME_CNT,
348           -1,
349           }
350          }
351 };
352
353 static void
354 netxen_nic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
355 {
356         struct netxen_adapter *adapter = netdev_priv(dev);
357         __u32 mode, *regs_buff = p;
358         void __iomem *addr;
359         int i, window;
360
361         memset(p, 0, NETXEN_NIC_REGS_LEN);
362         regs->version = (1 << 24) | (adapter->ahw.revision_id << 16) |
363             (adapter->pdev)->device;
364         /* which mode */
365         NETXEN_NIC_LOCKED_READ_REG(NETXEN_NIU_MODE, &regs_buff[0]);
366         mode = regs_buff[0];
367
368         /* Common registers to all the modes */
369         NETXEN_NIC_LOCKED_READ_REG(NETXEN_NIU_STRAP_VALUE_SAVE_HIGHER,
370                                    &regs_buff[2]);
371         /* GB/XGB Mode */
372         mode = (mode / 2) - 1;
373         window = 0;
374         if (mode <= 1) {
375                 for (i = 3; niu_registers[mode].reg[i - 3] != -1; i++) {
376                         /* GB: port specific registers */
377                         if (mode == 0 && i >= 19)
378                                 window = physical_port[adapter->portnum] *
379                                         NETXEN_NIC_PORT_WINDOW;
380
381                         NETXEN_NIC_LOCKED_READ_REG(niu_registers[mode].
382                                                    reg[i - 3] + window,
383                                                    &regs_buff[i]);
384                 }
385
386         }
387 }
388
389 static u32 netxen_nic_test_link(struct net_device *dev)
390 {
391         struct netxen_adapter *adapter = netdev_priv(dev);
392         __u32 status;
393         int val;
394
395         /* read which mode */
396         if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
397                 if (adapter->phy_read
398                     && adapter->phy_read(adapter,
399                                          NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
400                                          &status) != 0)
401                         return -EIO;
402                 else {
403                         val = netxen_get_phy_link(status);
404                         return !val;
405                 }
406         } else if (adapter->ahw.board_type == NETXEN_NIC_XGBE) {
407                 val = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_XG_STATE));
408                 return (val == XG_LINK_UP) ? 0 : 1;
409         }
410         return -EIO;
411 }
412
413 static int
414 netxen_nic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
415                       u8 * bytes)
416 {
417         struct netxen_adapter *adapter = netdev_priv(dev);
418         int offset;
419         int ret;
420
421         if (eeprom->len == 0)
422                 return -EINVAL;
423
424         eeprom->magic = (adapter->pdev)->vendor |
425                         ((adapter->pdev)->device << 16);
426         offset = eeprom->offset;
427
428         ret = netxen_rom_fast_read_words(adapter, offset, bytes,
429                                                 eeprom->len);
430         if (ret < 0)
431                 return ret;
432
433         return 0;
434 }
435
436 static int
437 netxen_nic_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
438                         u8 * bytes)
439 {
440         struct netxen_adapter *adapter = netdev_priv(dev);
441         int offset = eeprom->offset;
442         static int flash_start;
443         static int ready_to_flash;
444         int ret;
445
446         if (flash_start == 0) {
447                 netxen_halt_pegs(adapter);
448                 ret = netxen_flash_unlock(adapter);
449                 if (ret < 0) {
450                         printk(KERN_ERR "%s: Flash unlock failed.\n",
451                                 netxen_nic_driver_name);
452                         return ret;
453                 }
454                 printk(KERN_INFO "%s: flash unlocked. \n",
455                         netxen_nic_driver_name);
456                 last_schedule_time = jiffies;
457                 ret = netxen_flash_erase_secondary(adapter);
458                 if (ret != FLASH_SUCCESS) {
459                         printk(KERN_ERR "%s: Flash erase failed.\n",
460                                 netxen_nic_driver_name);
461                         return ret;
462                 }
463                 printk(KERN_INFO "%s: secondary flash erased successfully.\n",
464                         netxen_nic_driver_name);
465                 flash_start = 1;
466                 return 0;
467         }
468
469         if (offset == NETXEN_BOOTLD_START) {
470                 ret = netxen_flash_erase_primary(adapter);
471                 if (ret != FLASH_SUCCESS) {
472                         printk(KERN_ERR "%s: Flash erase failed.\n",
473                                 netxen_nic_driver_name);
474                         return ret;
475                 }
476
477                 ret = netxen_rom_se(adapter, NETXEN_USER_START);
478                 if (ret != FLASH_SUCCESS)
479                         return ret;
480                 ret = netxen_rom_se(adapter, NETXEN_FIXED_START);
481                 if (ret != FLASH_SUCCESS)
482                         return ret;
483
484                 printk(KERN_INFO "%s: primary flash erased successfully\n",
485                         netxen_nic_driver_name);
486
487                 ret = netxen_backup_crbinit(adapter);
488                 if (ret != FLASH_SUCCESS) {
489                         printk(KERN_ERR "%s: CRBinit backup failed.\n",
490                                 netxen_nic_driver_name);
491                         return ret;
492                 }
493                 printk(KERN_INFO "%s: CRBinit backup done.\n",
494                         netxen_nic_driver_name);
495                 ready_to_flash = 1;
496         }
497
498         if (!ready_to_flash) {
499                 printk(KERN_ERR "%s: Invalid write sequence, returning...\n",
500                         netxen_nic_driver_name);
501                 return -EINVAL;
502         }
503
504         return netxen_rom_fast_write_words(adapter, offset, bytes, eeprom->len);
505 }
506
507 static void
508 netxen_nic_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ring)
509 {
510         struct netxen_adapter *adapter = netdev_priv(dev);
511         int i;
512
513         ring->rx_pending = 0;
514         ring->rx_jumbo_pending = 0;
515         for (i = 0; i < MAX_RCV_CTX; ++i) {
516                 ring->rx_pending += adapter->recv_ctx[i].
517                     rcv_desc[RCV_DESC_NORMAL_CTXID].max_rx_desc_count;
518                 ring->rx_jumbo_pending += adapter->recv_ctx[i].
519                     rcv_desc[RCV_DESC_JUMBO_CTXID].max_rx_desc_count;
520         }
521         ring->tx_pending = adapter->max_tx_desc_count;
522
523         ring->rx_max_pending = MAX_RCV_DESCRIPTORS;
524         ring->tx_max_pending = MAX_CMD_DESCRIPTORS_HOST;
525         ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS;
526         ring->rx_mini_max_pending = 0;
527         ring->rx_mini_pending = 0;
528 }
529
530 static void
531 netxen_nic_get_pauseparam(struct net_device *dev,
532                           struct ethtool_pauseparam *pause)
533 {
534         struct netxen_adapter *adapter = netdev_priv(dev);
535         __u32 val;
536         int port = physical_port[adapter->portnum];
537
538         if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
539                 if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
540                         return;
541                 /* get flow control settings */
542                 netxen_nic_read_w0(adapter,NETXEN_NIU_GB_MAC_CONFIG_0(port),
543                                 &val);
544                 pause->rx_pause = netxen_gb_get_rx_flowctl(val);
545                 netxen_nic_read_w0(adapter, NETXEN_NIU_GB_PAUSE_CTL, &val);
546                 switch (port) {
547                         case 0:
548                                 pause->tx_pause = !(netxen_gb_get_gb0_mask(val));
549                                 break;
550                         case 1:
551                                 pause->tx_pause = !(netxen_gb_get_gb1_mask(val));
552                                 break;
553                         case 2:
554                                 pause->tx_pause = !(netxen_gb_get_gb2_mask(val));
555                                 break;
556                         case 3:
557                         default:
558                                 pause->tx_pause = !(netxen_gb_get_gb3_mask(val));
559                                 break;
560                 }
561         } else if (adapter->ahw.board_type == NETXEN_NIC_XGBE) {
562                 if ((port < 0) || (port > NETXEN_NIU_MAX_XG_PORTS))
563                         return;
564                 pause->rx_pause = 1;
565                 netxen_nic_read_w0(adapter, NETXEN_NIU_XG_PAUSE_CTL, &val);
566                 if (port == 0)
567                         pause->tx_pause = !(netxen_xg_get_xg0_mask(val));
568                 else
569                         pause->tx_pause = !(netxen_xg_get_xg1_mask(val));
570         } else {
571                 printk(KERN_ERR"%s: Unknown board type: %x\n",
572                                 netxen_nic_driver_name, adapter->ahw.board_type);
573         }
574 }
575
576 static int
577 netxen_nic_set_pauseparam(struct net_device *dev,
578                           struct ethtool_pauseparam *pause)
579 {
580         struct netxen_adapter *adapter = netdev_priv(dev);
581         __u32 val;
582         int port = physical_port[adapter->portnum];
583         /* read mode */
584         if (adapter->ahw.board_type == NETXEN_NIC_GBE) {
585                 if ((port < 0) || (port > NETXEN_NIU_MAX_GBE_PORTS))
586                         return -EIO;
587                 /* set flow control */
588                 netxen_nic_read_w0(adapter,
589                                         NETXEN_NIU_GB_MAC_CONFIG_0(port), &val);
590
591                 if (pause->rx_pause)
592                         netxen_gb_rx_flowctl(val);
593                 else
594                         netxen_gb_unset_rx_flowctl(val);
595
596                 netxen_nic_write_w0(adapter, NETXEN_NIU_GB_MAC_CONFIG_0(port),
597                                 val);
598                 /* set autoneg */
599                 netxen_nic_read_w0(adapter, NETXEN_NIU_GB_PAUSE_CTL, &val);
600                 switch (port) {
601                         case 0:
602                                 if (pause->tx_pause)
603                                         netxen_gb_unset_gb0_mask(val);
604                                 else
605                                         netxen_gb_set_gb0_mask(val);
606                                 break;
607                         case 1:
608                                 if (pause->tx_pause)
609                                         netxen_gb_unset_gb1_mask(val);
610                                 else
611                                         netxen_gb_set_gb1_mask(val);
612                                 break;
613                         case 2:
614                                 if (pause->tx_pause)
615                                         netxen_gb_unset_gb2_mask(val);
616                                 else
617                                         netxen_gb_set_gb2_mask(val);
618                                 break;
619                         case 3:
620                         default:
621                                 if (pause->tx_pause)
622                                         netxen_gb_unset_gb3_mask(val);
623                                 else
624                                         netxen_gb_set_gb3_mask(val);
625                                 break;
626                 }
627                 netxen_nic_write_w0(adapter, NETXEN_NIU_GB_PAUSE_CTL, val);
628         } else if (adapter->ahw.board_type == NETXEN_NIC_XGBE) {
629                 if ((port < 0) || (port > NETXEN_NIU_MAX_XG_PORTS))
630                         return -EIO;
631                 netxen_nic_read_w0(adapter, NETXEN_NIU_XG_PAUSE_CTL, &val);
632                 if (port == 0) {
633                         if (pause->tx_pause)
634                                 netxen_xg_unset_xg0_mask(val);
635                         else
636                                 netxen_xg_set_xg0_mask(val);
637                 } else {
638                         if (pause->tx_pause)
639                                 netxen_xg_unset_xg1_mask(val);
640                         else
641                                 netxen_xg_set_xg1_mask(val);
642                 }
643                 netxen_nic_write_w0(adapter, NETXEN_NIU_XG_PAUSE_CTL, val);
644         } else {
645                 printk(KERN_ERR "%s: Unknown board type: %x\n",
646                                 netxen_nic_driver_name,
647                                 adapter->ahw.board_type);
648         }
649         return 0;
650 }
651
652 static int netxen_nic_reg_test(struct net_device *dev)
653 {
654         struct netxen_adapter *adapter = netdev_priv(dev);
655         u32 data_read, data_written;
656
657         netxen_nic_read_w0(adapter, NETXEN_PCIX_PH_REG(0), &data_read);
658         if ((data_read & 0xffff) != PHAN_VENDOR_ID)
659         return 1;
660
661         data_written = (u32)0xa5a5a5a5;
662
663         netxen_nic_reg_write(adapter, CRB_SCRATCHPAD_TEST, data_written);
664         data_read = readl(NETXEN_CRB_NORMALIZE(adapter, CRB_SCRATCHPAD_TEST));
665         if (data_written != data_read)
666                 return 1;
667
668         return 0;
669 }
670
671 static int netxen_get_sset_count(struct net_device *dev, int sset)
672 {
673         switch (sset) {
674         case ETH_SS_TEST:
675                 return NETXEN_NIC_TEST_LEN;
676         case ETH_SS_STATS:
677                 return NETXEN_NIC_STATS_LEN;
678         default:
679                 return -EOPNOTSUPP;
680         }
681 }
682
683 static void
684 netxen_nic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
685                      u64 * data)
686 {
687         memset(data, 0, sizeof(uint64_t) * NETXEN_NIC_TEST_LEN);
688         if ((data[0] = netxen_nic_reg_test(dev)))
689                 eth_test->flags |= ETH_TEST_FL_FAILED;
690         /* link test */
691         if ((data[1] = (u64) netxen_nic_test_link(dev)))
692                 eth_test->flags |= ETH_TEST_FL_FAILED;
693 }
694
695 static void
696 netxen_nic_get_strings(struct net_device *dev, u32 stringset, u8 * data)
697 {
698         int index;
699
700         switch (stringset) {
701         case ETH_SS_TEST:
702                 memcpy(data, *netxen_nic_gstrings_test,
703                        NETXEN_NIC_TEST_LEN * ETH_GSTRING_LEN);
704                 break;
705         case ETH_SS_STATS:
706                 for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
707                         memcpy(data + index * ETH_GSTRING_LEN,
708                                netxen_nic_gstrings_stats[index].stat_string,
709                                ETH_GSTRING_LEN);
710                 }
711                 break;
712         }
713 }
714
715 static void
716 netxen_nic_get_ethtool_stats(struct net_device *dev,
717                              struct ethtool_stats *stats, u64 * data)
718 {
719         struct netxen_adapter *adapter = netdev_priv(dev);
720         int index;
721
722         for (index = 0; index < NETXEN_NIC_STATS_LEN; index++) {
723                 char *p =
724                     (char *)adapter +
725                     netxen_nic_gstrings_stats[index].stat_offset;
726                 data[index] =
727                     (netxen_nic_gstrings_stats[index].sizeof_stat ==
728                      sizeof(u64)) ? *(u64 *) p : *(u32 *) p;
729         }
730 }
731
732 static u32 netxen_nic_get_rx_csum(struct net_device *dev)
733 {
734         struct netxen_adapter *adapter = netdev_priv(dev);
735         return adapter->rx_csum;
736 }
737
738 static int netxen_nic_set_rx_csum(struct net_device *dev, u32 data)
739 {
740         struct netxen_adapter *adapter = netdev_priv(dev);
741         adapter->rx_csum = !!data;
742         return 0;
743 }
744
745 struct ethtool_ops netxen_nic_ethtool_ops = {
746         .get_settings = netxen_nic_get_settings,
747         .set_settings = netxen_nic_set_settings,
748         .get_drvinfo = netxen_nic_get_drvinfo,
749         .get_regs_len = netxen_nic_get_regs_len,
750         .get_regs = netxen_nic_get_regs,
751         .get_link = ethtool_op_get_link,
752         .get_eeprom_len = netxen_nic_get_eeprom_len,
753         .get_eeprom = netxen_nic_get_eeprom,
754         .set_eeprom = netxen_nic_set_eeprom,
755         .get_ringparam = netxen_nic_get_ringparam,
756         .get_pauseparam = netxen_nic_get_pauseparam,
757         .set_pauseparam = netxen_nic_set_pauseparam,
758         .set_tx_csum = ethtool_op_set_tx_csum,
759         .set_sg = ethtool_op_set_sg,
760         .set_tso = ethtool_op_set_tso,
761         .self_test = netxen_nic_diag_test,
762         .get_strings = netxen_nic_get_strings,
763         .get_ethtool_stats = netxen_nic_get_ethtool_stats,
764         .get_sset_count = netxen_get_sset_count,
765         .get_rx_csum = netxen_nic_get_rx_csum,
766         .set_rx_csum = netxen_nic_set_rx_csum,
767 };