Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.git] / drivers / net / qlcnic / qlcnic_ethtool.c
1 /*
2  * Copyright (C) 2009 - QLogic Corporation.
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 "COPYING".
22  *
23  */
24
25 #include <linux/types.h>
26 #include <linux/delay.h>
27 #include <linux/pci.h>
28 #include <linux/io.h>
29 #include <linux/netdevice.h>
30 #include <linux/ethtool.h>
31
32 #include "qlcnic.h"
33
34 struct qlcnic_stats {
35         char stat_string[ETH_GSTRING_LEN];
36         int sizeof_stat;
37         int stat_offset;
38 };
39
40 #define QLC_SIZEOF(m) FIELD_SIZEOF(struct qlcnic_adapter, m)
41 #define QLC_OFF(m) offsetof(struct qlcnic_adapter, m)
42
43 static const struct qlcnic_stats qlcnic_gstrings_stats[] = {
44         {"xmit_called",
45                 QLC_SIZEOF(stats.xmitcalled), QLC_OFF(stats.xmitcalled)},
46         {"xmit_finished",
47                 QLC_SIZEOF(stats.xmitfinished), QLC_OFF(stats.xmitfinished)},
48         {"rx_dropped",
49                 QLC_SIZEOF(stats.rxdropped), QLC_OFF(stats.rxdropped)},
50         {"tx_dropped",
51                 QLC_SIZEOF(stats.txdropped), QLC_OFF(stats.txdropped)},
52         {"csummed",
53                 QLC_SIZEOF(stats.csummed), QLC_OFF(stats.csummed)},
54         {"rx_pkts",
55                 QLC_SIZEOF(stats.rx_pkts), QLC_OFF(stats.rx_pkts)},
56         {"lro_pkts",
57                 QLC_SIZEOF(stats.lro_pkts), QLC_OFF(stats.lro_pkts)},
58         {"rx_bytes",
59                 QLC_SIZEOF(stats.rxbytes), QLC_OFF(stats.rxbytes)},
60         {"tx_bytes",
61                 QLC_SIZEOF(stats.txbytes), QLC_OFF(stats.txbytes)},
62 };
63
64 #define QLCNIC_STATS_LEN        ARRAY_SIZE(qlcnic_gstrings_stats)
65
66 static const char qlcnic_gstrings_test[][ETH_GSTRING_LEN] = {
67         "Register_Test_on_offline",
68         "Link_Test_on_offline"
69 };
70
71 #define QLCNIC_TEST_LEN ARRAY_SIZE(qlcnic_gstrings_test)
72
73 #define QLCNIC_RING_REGS_COUNT  20
74 #define QLCNIC_RING_REGS_LEN    (QLCNIC_RING_REGS_COUNT * sizeof(u32))
75 #define QLCNIC_MAX_EEPROM_LEN   1024
76
77 static const u32 diag_registers[] = {
78         CRB_CMDPEG_STATE,
79         CRB_RCVPEG_STATE,
80         CRB_XG_STATE_P3,
81         CRB_FW_CAPABILITIES_1,
82         ISR_INT_STATE_REG,
83         QLCNIC_CRB_DEV_REF_COUNT,
84         QLCNIC_CRB_DEV_STATE,
85         QLCNIC_CRB_DRV_STATE,
86         QLCNIC_CRB_DRV_SCRATCH,
87         QLCNIC_CRB_DEV_PARTITION_INFO,
88         QLCNIC_CRB_DRV_IDC_VER,
89         QLCNIC_PEG_ALIVE_COUNTER,
90         QLCNIC_PEG_HALT_STATUS1,
91         QLCNIC_PEG_HALT_STATUS2,
92         QLCNIC_CRB_PEG_NET_0+0x3c,
93         QLCNIC_CRB_PEG_NET_1+0x3c,
94         QLCNIC_CRB_PEG_NET_2+0x3c,
95         QLCNIC_CRB_PEG_NET_4+0x3c,
96         -1
97 };
98
99 static int qlcnic_get_regs_len(struct net_device *dev)
100 {
101         return sizeof(diag_registers) + QLCNIC_RING_REGS_LEN;
102 }
103
104 static int qlcnic_get_eeprom_len(struct net_device *dev)
105 {
106         return QLCNIC_FLASH_TOTAL_SIZE;
107 }
108
109 static void
110 qlcnic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
111 {
112         struct qlcnic_adapter *adapter = netdev_priv(dev);
113         u32 fw_major, fw_minor, fw_build;
114
115         fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
116         fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
117         fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
118         sprintf(drvinfo->fw_version, "%d.%d.%d", fw_major, fw_minor, fw_build);
119
120         strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
121         strlcpy(drvinfo->driver, qlcnic_driver_name, 32);
122         strlcpy(drvinfo->version, QLCNIC_LINUX_VERSIONID, 32);
123 }
124
125 static int
126 qlcnic_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
127 {
128         struct qlcnic_adapter *adapter = netdev_priv(dev);
129         int check_sfp_module = 0;
130         u16 pcifn = adapter->ahw.pci_func;
131
132         /* read which mode */
133         if (adapter->ahw.port_type == QLCNIC_GBE) {
134                 ecmd->supported = (SUPPORTED_10baseT_Half |
135                                    SUPPORTED_10baseT_Full |
136                                    SUPPORTED_100baseT_Half |
137                                    SUPPORTED_100baseT_Full |
138                                    SUPPORTED_1000baseT_Half |
139                                    SUPPORTED_1000baseT_Full);
140
141                 ecmd->advertising = (ADVERTISED_100baseT_Half |
142                                      ADVERTISED_100baseT_Full |
143                                      ADVERTISED_1000baseT_Half |
144                                      ADVERTISED_1000baseT_Full);
145
146                 ecmd->speed = adapter->link_speed;
147                 ecmd->duplex = adapter->link_duplex;
148                 ecmd->autoneg = adapter->link_autoneg;
149
150         } else if (adapter->ahw.port_type == QLCNIC_XGBE) {
151                 u32 val;
152
153                 val = QLCRD32(adapter, QLCNIC_PORT_MODE_ADDR);
154                 if (val == QLCNIC_PORT_MODE_802_3_AP) {
155                         ecmd->supported = SUPPORTED_1000baseT_Full;
156                         ecmd->advertising = ADVERTISED_1000baseT_Full;
157                 } else {
158                         ecmd->supported = SUPPORTED_10000baseT_Full;
159                         ecmd->advertising = ADVERTISED_10000baseT_Full;
160                 }
161
162                 if (netif_running(dev) && adapter->has_link_events) {
163                         ecmd->speed = adapter->link_speed;
164                         ecmd->autoneg = adapter->link_autoneg;
165                         ecmd->duplex = adapter->link_duplex;
166                         goto skip;
167                 }
168
169                 val = QLCRD32(adapter, P3_LINK_SPEED_REG(pcifn));
170                 ecmd->speed = P3_LINK_SPEED_MHZ *
171                         P3_LINK_SPEED_VAL(pcifn, val);
172                 ecmd->duplex = DUPLEX_FULL;
173                 ecmd->autoneg = AUTONEG_DISABLE;
174         } else
175                 return -EIO;
176
177 skip:
178         ecmd->phy_address = adapter->physical_port;
179         ecmd->transceiver = XCVR_EXTERNAL;
180
181         switch (adapter->ahw.board_type) {
182         case QLCNIC_BRDTYPE_P3_REF_QG:
183         case QLCNIC_BRDTYPE_P3_4_GB:
184         case QLCNIC_BRDTYPE_P3_4_GB_MM:
185
186                 ecmd->supported |= SUPPORTED_Autoneg;
187                 ecmd->advertising |= ADVERTISED_Autoneg;
188         case QLCNIC_BRDTYPE_P3_10G_CX4:
189         case QLCNIC_BRDTYPE_P3_10G_CX4_LP:
190         case QLCNIC_BRDTYPE_P3_10000_BASE_T:
191                 ecmd->supported |= SUPPORTED_TP;
192                 ecmd->advertising |= ADVERTISED_TP;
193                 ecmd->port = PORT_TP;
194                 ecmd->autoneg =  adapter->link_autoneg;
195                 break;
196         case QLCNIC_BRDTYPE_P3_IMEZ:
197         case QLCNIC_BRDTYPE_P3_XG_LOM:
198         case QLCNIC_BRDTYPE_P3_HMEZ:
199                 ecmd->supported |= SUPPORTED_MII;
200                 ecmd->advertising |= ADVERTISED_MII;
201                 ecmd->port = PORT_MII;
202                 ecmd->autoneg = AUTONEG_DISABLE;
203                 break;
204         case QLCNIC_BRDTYPE_P3_10G_SFP_PLUS:
205         case QLCNIC_BRDTYPE_P3_10G_SFP_CT:
206         case QLCNIC_BRDTYPE_P3_10G_SFP_QT:
207                 ecmd->advertising |= ADVERTISED_TP;
208                 ecmd->supported |= SUPPORTED_TP;
209                 check_sfp_module = netif_running(dev) &&
210                         adapter->has_link_events;
211         case QLCNIC_BRDTYPE_P3_10G_XFP:
212                 ecmd->supported |= SUPPORTED_FIBRE;
213                 ecmd->advertising |= ADVERTISED_FIBRE;
214                 ecmd->port = PORT_FIBRE;
215                 ecmd->autoneg = AUTONEG_DISABLE;
216                 break;
217         case QLCNIC_BRDTYPE_P3_10G_TP:
218                 if (adapter->ahw.port_type == QLCNIC_XGBE) {
219                         ecmd->autoneg = AUTONEG_DISABLE;
220                         ecmd->supported |= (SUPPORTED_FIBRE | SUPPORTED_TP);
221                         ecmd->advertising |=
222                                 (ADVERTISED_FIBRE | ADVERTISED_TP);
223                         ecmd->port = PORT_FIBRE;
224                         check_sfp_module = netif_running(dev) &&
225                                 adapter->has_link_events;
226                 } else {
227                         ecmd->autoneg = AUTONEG_ENABLE;
228                         ecmd->supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
229                         ecmd->advertising |=
230                                 (ADVERTISED_TP | ADVERTISED_Autoneg);
231                         ecmd->port = PORT_TP;
232                 }
233                 break;
234         default:
235                 dev_err(&adapter->pdev->dev, "Unsupported board model %d\n",
236                         adapter->ahw.board_type);
237                 return -EIO;
238         }
239
240         if (check_sfp_module) {
241                 switch (adapter->module_type) {
242                 case LINKEVENT_MODULE_OPTICAL_UNKNOWN:
243                 case LINKEVENT_MODULE_OPTICAL_SRLR:
244                 case LINKEVENT_MODULE_OPTICAL_LRM:
245                 case LINKEVENT_MODULE_OPTICAL_SFP_1G:
246                         ecmd->port = PORT_FIBRE;
247                         break;
248                 case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE:
249                 case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN:
250                 case LINKEVENT_MODULE_TWINAX:
251                         ecmd->port = PORT_TP;
252                         break;
253                 default:
254                         ecmd->port = PORT_OTHER;
255                 }
256         }
257
258         return 0;
259 }
260
261 static int
262 qlcnic_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
263 {
264         struct qlcnic_adapter *adapter = netdev_priv(dev);
265         __u32 status;
266
267         /* read which mode */
268         if (adapter->ahw.port_type == QLCNIC_GBE) {
269                 /* autonegotiation */
270                 if (qlcnic_fw_cmd_set_phy(adapter,
271                                QLCNIC_NIU_GB_MII_MGMT_ADDR_AUTONEG,
272                                ecmd->autoneg) != 0)
273                         return -EIO;
274                 else
275                         adapter->link_autoneg = ecmd->autoneg;
276
277                 if (qlcnic_fw_cmd_query_phy(adapter,
278                               QLCNIC_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
279                               &status) != 0)
280                         return -EIO;
281
282                 switch (ecmd->speed) {
283                 case SPEED_10:
284                         qlcnic_set_phy_speed(status, 0);
285                         break;
286                 case SPEED_100:
287                         qlcnic_set_phy_speed(status, 1);
288                         break;
289                 case SPEED_1000:
290                         qlcnic_set_phy_speed(status, 2);
291                         break;
292                 }
293
294                 if (ecmd->duplex == DUPLEX_HALF)
295                         qlcnic_clear_phy_duplex(status);
296                 if (ecmd->duplex == DUPLEX_FULL)
297                         qlcnic_set_phy_duplex(status);
298                 if (qlcnic_fw_cmd_set_phy(adapter,
299                                QLCNIC_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
300                                *((int *)&status)) != 0)
301                         return -EIO;
302                 else {
303                         adapter->link_speed = ecmd->speed;
304                         adapter->link_duplex = ecmd->duplex;
305                 }
306         } else
307                 return -EOPNOTSUPP;
308
309         if (!netif_running(dev))
310                 return 0;
311
312         dev->netdev_ops->ndo_stop(dev);
313         return dev->netdev_ops->ndo_open(dev);
314 }
315
316 static void
317 qlcnic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
318 {
319         struct qlcnic_adapter *adapter = netdev_priv(dev);
320         struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
321         struct qlcnic_host_sds_ring *sds_ring;
322         u32 *regs_buff = p;
323         int ring, i = 0;
324
325         memset(p, 0, qlcnic_get_regs_len(dev));
326         regs->version = (1 << 24) | (adapter->ahw.revision_id << 16) |
327             (adapter->pdev)->device;
328
329         if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
330                 return;
331
332         for (i = 0; diag_registers[i] != -1; i++)
333                 regs_buff[i] = QLCRD32(adapter, diag_registers[i]);
334
335         regs_buff[i++] = 0xFFEFCDAB; /* Marker btw regs and ring count*/
336
337         regs_buff[i++] = 1; /* No. of tx ring */
338         regs_buff[i++] = le32_to_cpu(*(adapter->tx_ring->hw_consumer));
339         regs_buff[i++] = readl(adapter->tx_ring->crb_cmd_producer);
340
341         regs_buff[i++] = 2; /* No. of rx ring */
342         regs_buff[i++] = readl(recv_ctx->rds_rings[0].crb_rcv_producer);
343         regs_buff[i++] = readl(recv_ctx->rds_rings[1].crb_rcv_producer);
344
345         regs_buff[i++] = adapter->max_sds_rings;
346
347         for (ring = 0; ring < adapter->max_sds_rings; ring++) {
348                 sds_ring = &(recv_ctx->sds_rings[ring]);
349                 regs_buff[i++] = readl(sds_ring->crb_sts_consumer);
350         }
351 }
352
353 static u32 qlcnic_test_link(struct net_device *dev)
354 {
355         struct qlcnic_adapter *adapter = netdev_priv(dev);
356         u32 val;
357
358         val = QLCRD32(adapter, CRB_XG_STATE_P3);
359         val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
360         return (val == XG_LINK_UP_P3) ? 0 : 1;
361 }
362
363 static int
364 qlcnic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
365                       u8 *bytes)
366 {
367         struct qlcnic_adapter *adapter = netdev_priv(dev);
368         int offset;
369         int ret;
370
371         if (eeprom->len == 0)
372                 return -EINVAL;
373
374         eeprom->magic = (adapter->pdev)->vendor |
375                         ((adapter->pdev)->device << 16);
376         offset = eeprom->offset;
377
378         ret = qlcnic_rom_fast_read_words(adapter, offset, bytes,
379                                                 eeprom->len);
380         if (ret < 0)
381                 return ret;
382
383         return 0;
384 }
385
386 static void
387 qlcnic_get_ringparam(struct net_device *dev,
388                 struct ethtool_ringparam *ring)
389 {
390         struct qlcnic_adapter *adapter = netdev_priv(dev);
391
392         ring->rx_pending = adapter->num_rxd;
393         ring->rx_jumbo_pending = adapter->num_jumbo_rxd;
394         ring->rx_jumbo_pending += adapter->num_lro_rxd;
395         ring->tx_pending = adapter->num_txd;
396
397         if (adapter->ahw.port_type == QLCNIC_GBE) {
398                 ring->rx_max_pending = MAX_RCV_DESCRIPTORS_1G;
399                 ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_1G;
400         } else {
401                 ring->rx_max_pending = MAX_RCV_DESCRIPTORS_10G;
402                 ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_10G;
403         }
404
405         ring->tx_max_pending = MAX_CMD_DESCRIPTORS;
406
407         ring->rx_mini_max_pending = 0;
408         ring->rx_mini_pending = 0;
409 }
410
411 static u32
412 qlcnic_validate_ringparam(u32 val, u32 min, u32 max, char *r_name)
413 {
414         u32 num_desc;
415         num_desc = max(val, min);
416         num_desc = min(num_desc, max);
417         num_desc = roundup_pow_of_two(num_desc);
418
419         if (val != num_desc) {
420                 printk(KERN_INFO "%s: setting %s ring size %d instead of %d\n",
421                        qlcnic_driver_name, r_name, num_desc, val);
422         }
423
424         return num_desc;
425 }
426
427 static int
428 qlcnic_set_ringparam(struct net_device *dev,
429                 struct ethtool_ringparam *ring)
430 {
431         struct qlcnic_adapter *adapter = netdev_priv(dev);
432         u16 max_rcv_desc = MAX_RCV_DESCRIPTORS_10G;
433         u16 max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
434         u16 num_rxd, num_jumbo_rxd, num_txd;
435
436
437         if (ring->rx_mini_pending)
438                 return -EOPNOTSUPP;
439
440         if (adapter->ahw.port_type == QLCNIC_GBE) {
441                 max_rcv_desc = MAX_RCV_DESCRIPTORS_1G;
442                 max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
443         }
444
445         num_rxd = qlcnic_validate_ringparam(ring->rx_pending,
446                         MIN_RCV_DESCRIPTORS, max_rcv_desc, "rx");
447
448         num_jumbo_rxd = qlcnic_validate_ringparam(ring->rx_jumbo_pending,
449                         MIN_JUMBO_DESCRIPTORS, max_jumbo_desc, "rx jumbo");
450
451         num_txd = qlcnic_validate_ringparam(ring->tx_pending,
452                         MIN_CMD_DESCRIPTORS, MAX_CMD_DESCRIPTORS, "tx");
453
454         if (num_rxd == adapter->num_rxd && num_txd == adapter->num_txd &&
455                         num_jumbo_rxd == adapter->num_jumbo_rxd)
456                 return 0;
457
458         adapter->num_rxd = num_rxd;
459         adapter->num_jumbo_rxd = num_jumbo_rxd;
460         adapter->num_txd = num_txd;
461
462         return qlcnic_reset_context(adapter);
463 }
464
465 static void
466 qlcnic_get_pauseparam(struct net_device *netdev,
467                           struct ethtool_pauseparam *pause)
468 {
469         struct qlcnic_adapter *adapter = netdev_priv(netdev);
470         int port = adapter->physical_port;
471         __u32 val;
472
473         if (adapter->ahw.port_type == QLCNIC_GBE) {
474                 if ((port < 0) || (port > QLCNIC_NIU_MAX_GBE_PORTS))
475                         return;
476                 /* get flow control settings */
477                 val = QLCRD32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port));
478                 pause->rx_pause = qlcnic_gb_get_rx_flowctl(val);
479                 val = QLCRD32(adapter, QLCNIC_NIU_GB_PAUSE_CTL);
480                 switch (port) {
481                 case 0:
482                         pause->tx_pause = !(qlcnic_gb_get_gb0_mask(val));
483                         break;
484                 case 1:
485                         pause->tx_pause = !(qlcnic_gb_get_gb1_mask(val));
486                         break;
487                 case 2:
488                         pause->tx_pause = !(qlcnic_gb_get_gb2_mask(val));
489                         break;
490                 case 3:
491                 default:
492                         pause->tx_pause = !(qlcnic_gb_get_gb3_mask(val));
493                         break;
494                 }
495         } else if (adapter->ahw.port_type == QLCNIC_XGBE) {
496                 if ((port < 0) || (port > QLCNIC_NIU_MAX_XG_PORTS))
497                         return;
498                 pause->rx_pause = 1;
499                 val = QLCRD32(adapter, QLCNIC_NIU_XG_PAUSE_CTL);
500                 if (port == 0)
501                         pause->tx_pause = !(qlcnic_xg_get_xg0_mask(val));
502                 else
503                         pause->tx_pause = !(qlcnic_xg_get_xg1_mask(val));
504         } else {
505                 dev_err(&netdev->dev, "Unknown board type: %x\n",
506                                         adapter->ahw.port_type);
507         }
508 }
509
510 static int
511 qlcnic_set_pauseparam(struct net_device *netdev,
512                           struct ethtool_pauseparam *pause)
513 {
514         struct qlcnic_adapter *adapter = netdev_priv(netdev);
515         int port = adapter->physical_port;
516         __u32 val;
517
518         /* read mode */
519         if (adapter->ahw.port_type == QLCNIC_GBE) {
520                 if ((port < 0) || (port > QLCNIC_NIU_MAX_GBE_PORTS))
521                         return -EIO;
522                 /* set flow control */
523                 val = QLCRD32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port));
524
525                 if (pause->rx_pause)
526                         qlcnic_gb_rx_flowctl(val);
527                 else
528                         qlcnic_gb_unset_rx_flowctl(val);
529
530                 QLCWR32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port),
531                                 val);
532                 /* set autoneg */
533                 val = QLCRD32(adapter, QLCNIC_NIU_GB_PAUSE_CTL);
534                 switch (port) {
535                 case 0:
536                         if (pause->tx_pause)
537                                 qlcnic_gb_unset_gb0_mask(val);
538                         else
539                                 qlcnic_gb_set_gb0_mask(val);
540                         break;
541                 case 1:
542                         if (pause->tx_pause)
543                                 qlcnic_gb_unset_gb1_mask(val);
544                         else
545                                 qlcnic_gb_set_gb1_mask(val);
546                         break;
547                 case 2:
548                         if (pause->tx_pause)
549                                 qlcnic_gb_unset_gb2_mask(val);
550                         else
551                                 qlcnic_gb_set_gb2_mask(val);
552                         break;
553                 case 3:
554                 default:
555                         if (pause->tx_pause)
556                                 qlcnic_gb_unset_gb3_mask(val);
557                         else
558                                 qlcnic_gb_set_gb3_mask(val);
559                         break;
560                 }
561                 QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, val);
562         } else if (adapter->ahw.port_type == QLCNIC_XGBE) {
563                 if ((port < 0) || (port > QLCNIC_NIU_MAX_XG_PORTS))
564                         return -EIO;
565                 val = QLCRD32(adapter, QLCNIC_NIU_XG_PAUSE_CTL);
566                 if (port == 0) {
567                         if (pause->tx_pause)
568                                 qlcnic_xg_unset_xg0_mask(val);
569                         else
570                                 qlcnic_xg_set_xg0_mask(val);
571                 } else {
572                         if (pause->tx_pause)
573                                 qlcnic_xg_unset_xg1_mask(val);
574                         else
575                                 qlcnic_xg_set_xg1_mask(val);
576                 }
577                 QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, val);
578         } else {
579                 dev_err(&netdev->dev, "Unknown board type: %x\n",
580                                 adapter->ahw.port_type);
581         }
582         return 0;
583 }
584
585 static int qlcnic_reg_test(struct net_device *dev)
586 {
587         struct qlcnic_adapter *adapter = netdev_priv(dev);
588         u32 data_read, data_written;
589
590         data_read = QLCRD32(adapter, QLCNIC_PCIX_PH_REG(0));
591         if ((data_read & 0xffff) != adapter->pdev->vendor)
592                 return 1;
593
594         data_written = (u32)0xa5a5a5a5;
595
596         QLCWR32(adapter, CRB_SCRATCHPAD_TEST, data_written);
597         data_read = QLCRD32(adapter, CRB_SCRATCHPAD_TEST);
598         if (data_written != data_read)
599                 return 1;
600
601         return 0;
602 }
603
604 static int qlcnic_get_sset_count(struct net_device *dev, int sset)
605 {
606         switch (sset) {
607         case ETH_SS_TEST:
608                 return QLCNIC_TEST_LEN;
609         case ETH_SS_STATS:
610                 return QLCNIC_STATS_LEN;
611         default:
612                 return -EOPNOTSUPP;
613         }
614 }
615
616 static void
617 qlcnic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
618                      u64 *data)
619 {
620         memset(data, 0, sizeof(u64) * QLCNIC_TEST_LEN);
621         data[0] = qlcnic_reg_test(dev);
622         if (data[0])
623                 eth_test->flags |= ETH_TEST_FL_FAILED;
624
625         /* link test */
626         data[1] = (u64) qlcnic_test_link(dev);
627         if (data[1])
628                 eth_test->flags |= ETH_TEST_FL_FAILED;
629 }
630
631 static void
632 qlcnic_get_strings(struct net_device *dev, u32 stringset, u8 * data)
633 {
634         int index;
635
636         switch (stringset) {
637         case ETH_SS_TEST:
638                 memcpy(data, *qlcnic_gstrings_test,
639                        QLCNIC_TEST_LEN * ETH_GSTRING_LEN);
640                 break;
641         case ETH_SS_STATS:
642                 for (index = 0; index < QLCNIC_STATS_LEN; index++) {
643                         memcpy(data + index * ETH_GSTRING_LEN,
644                                qlcnic_gstrings_stats[index].stat_string,
645                                ETH_GSTRING_LEN);
646                 }
647                 break;
648         }
649 }
650
651 static void
652 qlcnic_get_ethtool_stats(struct net_device *dev,
653                              struct ethtool_stats *stats, u64 * data)
654 {
655         struct qlcnic_adapter *adapter = netdev_priv(dev);
656         int index;
657
658         for (index = 0; index < QLCNIC_STATS_LEN; index++) {
659                 char *p =
660                     (char *)adapter +
661                     qlcnic_gstrings_stats[index].stat_offset;
662                 data[index] =
663                     (qlcnic_gstrings_stats[index].sizeof_stat ==
664                      sizeof(u64)) ? *(u64 *)p:(*(u32 *)p);
665         }
666 }
667
668 static u32 qlcnic_get_rx_csum(struct net_device *dev)
669 {
670         struct qlcnic_adapter *adapter = netdev_priv(dev);
671         return adapter->rx_csum;
672 }
673
674 static int qlcnic_set_rx_csum(struct net_device *dev, u32 data)
675 {
676         struct qlcnic_adapter *adapter = netdev_priv(dev);
677         adapter->rx_csum = !!data;
678         return 0;
679 }
680
681 static u32 qlcnic_get_tso(struct net_device *dev)
682 {
683         return (dev->features & (NETIF_F_TSO | NETIF_F_TSO6)) != 0;
684 }
685
686 static int qlcnic_set_tso(struct net_device *dev, u32 data)
687 {
688         if (data)
689                 dev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
690         else
691                 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
692
693         return 0;
694 }
695
696 static void
697 qlcnic_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
698 {
699         struct qlcnic_adapter *adapter = netdev_priv(dev);
700         u32 wol_cfg;
701
702         wol->supported = 0;
703         wol->wolopts = 0;
704
705         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
706         if (wol_cfg & (1UL << adapter->portnum))
707                 wol->supported |= WAKE_MAGIC;
708
709         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
710         if (wol_cfg & (1UL << adapter->portnum))
711                 wol->wolopts |= WAKE_MAGIC;
712 }
713
714 static int
715 qlcnic_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
716 {
717         struct qlcnic_adapter *adapter = netdev_priv(dev);
718         u32 wol_cfg;
719
720         if (wol->wolopts & ~WAKE_MAGIC)
721                 return -EOPNOTSUPP;
722
723         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
724         if (!(wol_cfg & (1 << adapter->portnum)))
725                 return -EOPNOTSUPP;
726
727         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
728         if (wol->wolopts & WAKE_MAGIC)
729                 wol_cfg |= 1UL << adapter->portnum;
730         else
731                 wol_cfg &= ~(1UL << adapter->portnum);
732
733         QLCWR32(adapter, QLCNIC_WOL_CONFIG, wol_cfg);
734
735         return 0;
736 }
737
738 /*
739  * Set the coalescing parameters. Currently only normal is supported.
740  * If rx_coalesce_usecs == 0 or rx_max_coalesced_frames == 0 then set the
741  * firmware coalescing to default.
742  */
743 static int qlcnic_set_intr_coalesce(struct net_device *netdev,
744                         struct ethtool_coalesce *ethcoal)
745 {
746         struct qlcnic_adapter *adapter = netdev_priv(netdev);
747
748         if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
749                 return -EINVAL;
750
751         /*
752         * Return Error if unsupported values or
753         * unsupported parameters are set.
754         */
755         if (ethcoal->rx_coalesce_usecs > 0xffff ||
756                 ethcoal->rx_max_coalesced_frames > 0xffff ||
757                 ethcoal->tx_coalesce_usecs > 0xffff ||
758                 ethcoal->tx_max_coalesced_frames > 0xffff ||
759                 ethcoal->rx_coalesce_usecs_irq ||
760                 ethcoal->rx_max_coalesced_frames_irq ||
761                 ethcoal->tx_coalesce_usecs_irq ||
762                 ethcoal->tx_max_coalesced_frames_irq ||
763                 ethcoal->stats_block_coalesce_usecs ||
764                 ethcoal->use_adaptive_rx_coalesce ||
765                 ethcoal->use_adaptive_tx_coalesce ||
766                 ethcoal->pkt_rate_low ||
767                 ethcoal->rx_coalesce_usecs_low ||
768                 ethcoal->rx_max_coalesced_frames_low ||
769                 ethcoal->tx_coalesce_usecs_low ||
770                 ethcoal->tx_max_coalesced_frames_low ||
771                 ethcoal->pkt_rate_high ||
772                 ethcoal->rx_coalesce_usecs_high ||
773                 ethcoal->rx_max_coalesced_frames_high ||
774                 ethcoal->tx_coalesce_usecs_high ||
775                 ethcoal->tx_max_coalesced_frames_high)
776                 return -EINVAL;
777
778         if (!ethcoal->rx_coalesce_usecs ||
779                 !ethcoal->rx_max_coalesced_frames) {
780                 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
781                 adapter->coal.normal.data.rx_time_us =
782                         QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
783                 adapter->coal.normal.data.rx_packets =
784                         QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
785         } else {
786                 adapter->coal.flags = 0;
787                 adapter->coal.normal.data.rx_time_us =
788                 ethcoal->rx_coalesce_usecs;
789                 adapter->coal.normal.data.rx_packets =
790                 ethcoal->rx_max_coalesced_frames;
791         }
792         adapter->coal.normal.data.tx_time_us = ethcoal->tx_coalesce_usecs;
793         adapter->coal.normal.data.tx_packets =
794         ethcoal->tx_max_coalesced_frames;
795
796         qlcnic_config_intr_coalesce(adapter);
797
798         return 0;
799 }
800
801 static int qlcnic_get_intr_coalesce(struct net_device *netdev,
802                         struct ethtool_coalesce *ethcoal)
803 {
804         struct qlcnic_adapter *adapter = netdev_priv(netdev);
805
806         if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
807                 return -EINVAL;
808
809         ethcoal->rx_coalesce_usecs = adapter->coal.normal.data.rx_time_us;
810         ethcoal->tx_coalesce_usecs = adapter->coal.normal.data.tx_time_us;
811         ethcoal->rx_max_coalesced_frames =
812                 adapter->coal.normal.data.rx_packets;
813         ethcoal->tx_max_coalesced_frames =
814                 adapter->coal.normal.data.tx_packets;
815
816         return 0;
817 }
818
819 static int qlcnic_set_flags(struct net_device *netdev, u32 data)
820 {
821         struct qlcnic_adapter *adapter = netdev_priv(netdev);
822         int hw_lro;
823
824         if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO))
825                 return -EINVAL;
826
827         ethtool_op_set_flags(netdev, data);
828
829         hw_lro = (data & ETH_FLAG_LRO) ? QLCNIC_LRO_ENABLED : 0;
830
831         if (qlcnic_config_hw_lro(adapter, hw_lro))
832                 return -EIO;
833
834         if ((hw_lro == 0) && qlcnic_send_lro_cleanup(adapter))
835                 return -EIO;
836
837
838         return 0;
839 }
840
841 const struct ethtool_ops qlcnic_ethtool_ops = {
842         .get_settings = qlcnic_get_settings,
843         .set_settings = qlcnic_set_settings,
844         .get_drvinfo = qlcnic_get_drvinfo,
845         .get_regs_len = qlcnic_get_regs_len,
846         .get_regs = qlcnic_get_regs,
847         .get_link = ethtool_op_get_link,
848         .get_eeprom_len = qlcnic_get_eeprom_len,
849         .get_eeprom = qlcnic_get_eeprom,
850         .get_ringparam = qlcnic_get_ringparam,
851         .set_ringparam = qlcnic_set_ringparam,
852         .get_pauseparam = qlcnic_get_pauseparam,
853         .set_pauseparam = qlcnic_set_pauseparam,
854         .set_tx_csum = ethtool_op_set_tx_csum,
855         .set_sg = ethtool_op_set_sg,
856         .get_tso = qlcnic_get_tso,
857         .set_tso = qlcnic_set_tso,
858         .get_wol = qlcnic_get_wol,
859         .set_wol = qlcnic_set_wol,
860         .self_test = qlcnic_diag_test,
861         .get_strings = qlcnic_get_strings,
862         .get_ethtool_stats = qlcnic_get_ethtool_stats,
863         .get_sset_count = qlcnic_get_sset_count,
864         .get_rx_csum = qlcnic_get_rx_csum,
865         .set_rx_csum = qlcnic_set_rx_csum,
866         .get_coalesce = qlcnic_get_intr_coalesce,
867         .set_coalesce = qlcnic_set_intr_coalesce,
868         .get_flags = ethtool_op_get_flags,
869         .set_flags = qlcnic_set_flags,
870 };