qlcnic: additional driver statistics
[linux-3.10.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         {"lrobytes",
63                 QLC_SIZEOF(stats.lrobytes), QLC_OFF(stats.lrobytes)},
64         {"lso_frames",
65                 QLC_SIZEOF(stats.lso_frames), QLC_OFF(stats.lso_frames)},
66         {"xmit_on",
67                 QLC_SIZEOF(stats.xmit_on), QLC_OFF(stats.xmit_on)},
68         {"xmit_off",
69                 QLC_SIZEOF(stats.xmit_off), QLC_OFF(stats.xmit_off)},
70         {"skb_alloc_failure", QLC_SIZEOF(stats.skb_alloc_failure),
71                 QLC_OFF(stats.skb_alloc_failure)},
72         {"null skb",
73                 QLC_SIZEOF(stats.null_skb), QLC_OFF(stats.null_skb)},
74         {"null rxbuf",
75                 QLC_SIZEOF(stats.null_rxbuf), QLC_OFF(stats.null_rxbuf)},
76         {"rx dma map error", QLC_SIZEOF(stats.rx_dma_map_error),
77                                          QLC_OFF(stats.rx_dma_map_error)},
78         {"tx dma map error", QLC_SIZEOF(stats.tx_dma_map_error),
79                                          QLC_OFF(stats.tx_dma_map_error)},
80
81 };
82
83 #define QLCNIC_STATS_LEN        ARRAY_SIZE(qlcnic_gstrings_stats)
84
85 static const char qlcnic_gstrings_test[][ETH_GSTRING_LEN] = {
86         "Register_Test_on_offline",
87         "Link_Test_on_offline",
88         "Interrupt_Test_offline",
89         "Loopback_Test_offline"
90 };
91
92 #define QLCNIC_TEST_LEN ARRAY_SIZE(qlcnic_gstrings_test)
93
94 #define QLCNIC_RING_REGS_COUNT  20
95 #define QLCNIC_RING_REGS_LEN    (QLCNIC_RING_REGS_COUNT * sizeof(u32))
96 #define QLCNIC_MAX_EEPROM_LEN   1024
97
98 static const u32 diag_registers[] = {
99         CRB_CMDPEG_STATE,
100         CRB_RCVPEG_STATE,
101         CRB_XG_STATE_P3,
102         CRB_FW_CAPABILITIES_1,
103         ISR_INT_STATE_REG,
104         QLCNIC_CRB_DEV_REF_COUNT,
105         QLCNIC_CRB_DEV_STATE,
106         QLCNIC_CRB_DRV_STATE,
107         QLCNIC_CRB_DRV_SCRATCH,
108         QLCNIC_CRB_DEV_PARTITION_INFO,
109         QLCNIC_CRB_DRV_IDC_VER,
110         QLCNIC_PEG_ALIVE_COUNTER,
111         QLCNIC_PEG_HALT_STATUS1,
112         QLCNIC_PEG_HALT_STATUS2,
113         QLCNIC_CRB_PEG_NET_0+0x3c,
114         QLCNIC_CRB_PEG_NET_1+0x3c,
115         QLCNIC_CRB_PEG_NET_2+0x3c,
116         QLCNIC_CRB_PEG_NET_4+0x3c,
117         -1
118 };
119
120 static int qlcnic_get_regs_len(struct net_device *dev)
121 {
122         return sizeof(diag_registers) + QLCNIC_RING_REGS_LEN;
123 }
124
125 static int qlcnic_get_eeprom_len(struct net_device *dev)
126 {
127         return QLCNIC_FLASH_TOTAL_SIZE;
128 }
129
130 static void
131 qlcnic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
132 {
133         struct qlcnic_adapter *adapter = netdev_priv(dev);
134         u32 fw_major, fw_minor, fw_build;
135
136         fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
137         fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
138         fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
139         sprintf(drvinfo->fw_version, "%d.%d.%d", fw_major, fw_minor, fw_build);
140
141         strlcpy(drvinfo->bus_info, pci_name(adapter->pdev), 32);
142         strlcpy(drvinfo->driver, qlcnic_driver_name, 32);
143         strlcpy(drvinfo->version, QLCNIC_LINUX_VERSIONID, 32);
144 }
145
146 static int
147 qlcnic_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
148 {
149         struct qlcnic_adapter *adapter = netdev_priv(dev);
150         int check_sfp_module = 0;
151         u16 pcifn = adapter->ahw.pci_func;
152
153         /* read which mode */
154         if (adapter->ahw.port_type == QLCNIC_GBE) {
155                 ecmd->supported = (SUPPORTED_10baseT_Half |
156                                    SUPPORTED_10baseT_Full |
157                                    SUPPORTED_100baseT_Half |
158                                    SUPPORTED_100baseT_Full |
159                                    SUPPORTED_1000baseT_Half |
160                                    SUPPORTED_1000baseT_Full);
161
162                 ecmd->advertising = (ADVERTISED_100baseT_Half |
163                                      ADVERTISED_100baseT_Full |
164                                      ADVERTISED_1000baseT_Half |
165                                      ADVERTISED_1000baseT_Full);
166
167                 ecmd->speed = adapter->link_speed;
168                 ecmd->duplex = adapter->link_duplex;
169                 ecmd->autoneg = adapter->link_autoneg;
170
171         } else if (adapter->ahw.port_type == QLCNIC_XGBE) {
172                 u32 val;
173
174                 val = QLCRD32(adapter, QLCNIC_PORT_MODE_ADDR);
175                 if (val == QLCNIC_PORT_MODE_802_3_AP) {
176                         ecmd->supported = SUPPORTED_1000baseT_Full;
177                         ecmd->advertising = ADVERTISED_1000baseT_Full;
178                 } else {
179                         ecmd->supported = SUPPORTED_10000baseT_Full;
180                         ecmd->advertising = ADVERTISED_10000baseT_Full;
181                 }
182
183                 if (netif_running(dev) && adapter->has_link_events) {
184                         ecmd->speed = adapter->link_speed;
185                         ecmd->autoneg = adapter->link_autoneg;
186                         ecmd->duplex = adapter->link_duplex;
187                         goto skip;
188                 }
189
190                 val = QLCRD32(adapter, P3_LINK_SPEED_REG(pcifn));
191                 ecmd->speed = P3_LINK_SPEED_MHZ *
192                         P3_LINK_SPEED_VAL(pcifn, val);
193                 ecmd->duplex = DUPLEX_FULL;
194                 ecmd->autoneg = AUTONEG_DISABLE;
195         } else
196                 return -EIO;
197
198 skip:
199         ecmd->phy_address = adapter->physical_port;
200         ecmd->transceiver = XCVR_EXTERNAL;
201
202         switch (adapter->ahw.board_type) {
203         case QLCNIC_BRDTYPE_P3_REF_QG:
204         case QLCNIC_BRDTYPE_P3_4_GB:
205         case QLCNIC_BRDTYPE_P3_4_GB_MM:
206
207                 ecmd->supported |= SUPPORTED_Autoneg;
208                 ecmd->advertising |= ADVERTISED_Autoneg;
209         case QLCNIC_BRDTYPE_P3_10G_CX4:
210         case QLCNIC_BRDTYPE_P3_10G_CX4_LP:
211         case QLCNIC_BRDTYPE_P3_10000_BASE_T:
212                 ecmd->supported |= SUPPORTED_TP;
213                 ecmd->advertising |= ADVERTISED_TP;
214                 ecmd->port = PORT_TP;
215                 ecmd->autoneg =  adapter->link_autoneg;
216                 break;
217         case QLCNIC_BRDTYPE_P3_IMEZ:
218         case QLCNIC_BRDTYPE_P3_XG_LOM:
219         case QLCNIC_BRDTYPE_P3_HMEZ:
220                 ecmd->supported |= SUPPORTED_MII;
221                 ecmd->advertising |= ADVERTISED_MII;
222                 ecmd->port = PORT_MII;
223                 ecmd->autoneg = AUTONEG_DISABLE;
224                 break;
225         case QLCNIC_BRDTYPE_P3_10G_SFP_PLUS:
226         case QLCNIC_BRDTYPE_P3_10G_SFP_CT:
227         case QLCNIC_BRDTYPE_P3_10G_SFP_QT:
228                 ecmd->advertising |= ADVERTISED_TP;
229                 ecmd->supported |= SUPPORTED_TP;
230                 check_sfp_module = netif_running(dev) &&
231                         adapter->has_link_events;
232         case QLCNIC_BRDTYPE_P3_10G_XFP:
233                 ecmd->supported |= SUPPORTED_FIBRE;
234                 ecmd->advertising |= ADVERTISED_FIBRE;
235                 ecmd->port = PORT_FIBRE;
236                 ecmd->autoneg = AUTONEG_DISABLE;
237                 break;
238         case QLCNIC_BRDTYPE_P3_10G_TP:
239                 if (adapter->ahw.port_type == QLCNIC_XGBE) {
240                         ecmd->autoneg = AUTONEG_DISABLE;
241                         ecmd->supported |= (SUPPORTED_FIBRE | SUPPORTED_TP);
242                         ecmd->advertising |=
243                                 (ADVERTISED_FIBRE | ADVERTISED_TP);
244                         ecmd->port = PORT_FIBRE;
245                         check_sfp_module = netif_running(dev) &&
246                                 adapter->has_link_events;
247                 } else {
248                         ecmd->autoneg = AUTONEG_ENABLE;
249                         ecmd->supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
250                         ecmd->advertising |=
251                                 (ADVERTISED_TP | ADVERTISED_Autoneg);
252                         ecmd->port = PORT_TP;
253                 }
254                 break;
255         default:
256                 dev_err(&adapter->pdev->dev, "Unsupported board model %d\n",
257                         adapter->ahw.board_type);
258                 return -EIO;
259         }
260
261         if (check_sfp_module) {
262                 switch (adapter->module_type) {
263                 case LINKEVENT_MODULE_OPTICAL_UNKNOWN:
264                 case LINKEVENT_MODULE_OPTICAL_SRLR:
265                 case LINKEVENT_MODULE_OPTICAL_LRM:
266                 case LINKEVENT_MODULE_OPTICAL_SFP_1G:
267                         ecmd->port = PORT_FIBRE;
268                         break;
269                 case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLE:
270                 case LINKEVENT_MODULE_TWINAX_UNSUPPORTED_CABLELEN:
271                 case LINKEVENT_MODULE_TWINAX:
272                         ecmd->port = PORT_TP;
273                         break;
274                 default:
275                         ecmd->port = PORT_OTHER;
276                 }
277         }
278
279         return 0;
280 }
281
282 static int
283 qlcnic_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
284 {
285         struct qlcnic_adapter *adapter = netdev_priv(dev);
286         __u32 status;
287
288         /* read which mode */
289         if (adapter->ahw.port_type == QLCNIC_GBE) {
290                 /* autonegotiation */
291                 if (qlcnic_fw_cmd_set_phy(adapter,
292                                QLCNIC_NIU_GB_MII_MGMT_ADDR_AUTONEG,
293                                ecmd->autoneg) != 0)
294                         return -EIO;
295                 else
296                         adapter->link_autoneg = ecmd->autoneg;
297
298                 if (qlcnic_fw_cmd_query_phy(adapter,
299                               QLCNIC_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
300                               &status) != 0)
301                         return -EIO;
302
303                 switch (ecmd->speed) {
304                 case SPEED_10:
305                         qlcnic_set_phy_speed(status, 0);
306                         break;
307                 case SPEED_100:
308                         qlcnic_set_phy_speed(status, 1);
309                         break;
310                 case SPEED_1000:
311                         qlcnic_set_phy_speed(status, 2);
312                         break;
313                 }
314
315                 if (ecmd->duplex == DUPLEX_HALF)
316                         qlcnic_clear_phy_duplex(status);
317                 if (ecmd->duplex == DUPLEX_FULL)
318                         qlcnic_set_phy_duplex(status);
319                 if (qlcnic_fw_cmd_set_phy(adapter,
320                                QLCNIC_NIU_GB_MII_MGMT_ADDR_PHY_STATUS,
321                                *((int *)&status)) != 0)
322                         return -EIO;
323                 else {
324                         adapter->link_speed = ecmd->speed;
325                         adapter->link_duplex = ecmd->duplex;
326                 }
327         } else
328                 return -EOPNOTSUPP;
329
330         if (!netif_running(dev))
331                 return 0;
332
333         dev->netdev_ops->ndo_stop(dev);
334         return dev->netdev_ops->ndo_open(dev);
335 }
336
337 static void
338 qlcnic_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *p)
339 {
340         struct qlcnic_adapter *adapter = netdev_priv(dev);
341         struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
342         struct qlcnic_host_sds_ring *sds_ring;
343         u32 *regs_buff = p;
344         int ring, i = 0;
345
346         memset(p, 0, qlcnic_get_regs_len(dev));
347         regs->version = (1 << 24) | (adapter->ahw.revision_id << 16) |
348             (adapter->pdev)->device;
349
350         for (i = 0; diag_registers[i] != -1; i++)
351                 regs_buff[i] = QLCRD32(adapter, diag_registers[i]);
352
353         if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
354                 return;
355
356         regs_buff[i++] = 0xFFEFCDAB; /* Marker btw regs and ring count*/
357
358         regs_buff[i++] = 1; /* No. of tx ring */
359         regs_buff[i++] = le32_to_cpu(*(adapter->tx_ring->hw_consumer));
360         regs_buff[i++] = readl(adapter->tx_ring->crb_cmd_producer);
361
362         regs_buff[i++] = 2; /* No. of rx ring */
363         regs_buff[i++] = readl(recv_ctx->rds_rings[0].crb_rcv_producer);
364         regs_buff[i++] = readl(recv_ctx->rds_rings[1].crb_rcv_producer);
365
366         regs_buff[i++] = adapter->max_sds_rings;
367
368         for (ring = 0; ring < adapter->max_sds_rings; ring++) {
369                 sds_ring = &(recv_ctx->sds_rings[ring]);
370                 regs_buff[i++] = readl(sds_ring->crb_sts_consumer);
371         }
372 }
373
374 static u32 qlcnic_test_link(struct net_device *dev)
375 {
376         struct qlcnic_adapter *adapter = netdev_priv(dev);
377         u32 val;
378
379         val = QLCRD32(adapter, CRB_XG_STATE_P3);
380         val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
381         return (val == XG_LINK_UP_P3) ? 0 : 1;
382 }
383
384 static int
385 qlcnic_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
386                       u8 *bytes)
387 {
388         struct qlcnic_adapter *adapter = netdev_priv(dev);
389         int offset;
390         int ret;
391
392         if (eeprom->len == 0)
393                 return -EINVAL;
394
395         eeprom->magic = (adapter->pdev)->vendor |
396                         ((adapter->pdev)->device << 16);
397         offset = eeprom->offset;
398
399         ret = qlcnic_rom_fast_read_words(adapter, offset, bytes,
400                                                 eeprom->len);
401         if (ret < 0)
402                 return ret;
403
404         return 0;
405 }
406
407 static void
408 qlcnic_get_ringparam(struct net_device *dev,
409                 struct ethtool_ringparam *ring)
410 {
411         struct qlcnic_adapter *adapter = netdev_priv(dev);
412
413         ring->rx_pending = adapter->num_rxd;
414         ring->rx_jumbo_pending = adapter->num_jumbo_rxd;
415         ring->rx_jumbo_pending += adapter->num_lro_rxd;
416         ring->tx_pending = adapter->num_txd;
417
418         if (adapter->ahw.port_type == QLCNIC_GBE) {
419                 ring->rx_max_pending = MAX_RCV_DESCRIPTORS_1G;
420                 ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_1G;
421         } else {
422                 ring->rx_max_pending = MAX_RCV_DESCRIPTORS_10G;
423                 ring->rx_jumbo_max_pending = MAX_JUMBO_RCV_DESCRIPTORS_10G;
424         }
425
426         ring->tx_max_pending = MAX_CMD_DESCRIPTORS;
427
428         ring->rx_mini_max_pending = 0;
429         ring->rx_mini_pending = 0;
430 }
431
432 static u32
433 qlcnic_validate_ringparam(u32 val, u32 min, u32 max, char *r_name)
434 {
435         u32 num_desc;
436         num_desc = max(val, min);
437         num_desc = min(num_desc, max);
438         num_desc = roundup_pow_of_two(num_desc);
439
440         if (val != num_desc) {
441                 printk(KERN_INFO "%s: setting %s ring size %d instead of %d\n",
442                        qlcnic_driver_name, r_name, num_desc, val);
443         }
444
445         return num_desc;
446 }
447
448 static int
449 qlcnic_set_ringparam(struct net_device *dev,
450                 struct ethtool_ringparam *ring)
451 {
452         struct qlcnic_adapter *adapter = netdev_priv(dev);
453         u16 max_rcv_desc = MAX_RCV_DESCRIPTORS_10G;
454         u16 max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
455         u16 num_rxd, num_jumbo_rxd, num_txd;
456
457
458         if (ring->rx_mini_pending)
459                 return -EOPNOTSUPP;
460
461         if (adapter->ahw.port_type == QLCNIC_GBE) {
462                 max_rcv_desc = MAX_RCV_DESCRIPTORS_1G;
463                 max_jumbo_desc = MAX_JUMBO_RCV_DESCRIPTORS_10G;
464         }
465
466         num_rxd = qlcnic_validate_ringparam(ring->rx_pending,
467                         MIN_RCV_DESCRIPTORS, max_rcv_desc, "rx");
468
469         num_jumbo_rxd = qlcnic_validate_ringparam(ring->rx_jumbo_pending,
470                         MIN_JUMBO_DESCRIPTORS, max_jumbo_desc, "rx jumbo");
471
472         num_txd = qlcnic_validate_ringparam(ring->tx_pending,
473                         MIN_CMD_DESCRIPTORS, MAX_CMD_DESCRIPTORS, "tx");
474
475         if (num_rxd == adapter->num_rxd && num_txd == adapter->num_txd &&
476                         num_jumbo_rxd == adapter->num_jumbo_rxd)
477                 return 0;
478
479         adapter->num_rxd = num_rxd;
480         adapter->num_jumbo_rxd = num_jumbo_rxd;
481         adapter->num_txd = num_txd;
482
483         return qlcnic_reset_context(adapter);
484 }
485
486 static void
487 qlcnic_get_pauseparam(struct net_device *netdev,
488                           struct ethtool_pauseparam *pause)
489 {
490         struct qlcnic_adapter *adapter = netdev_priv(netdev);
491         int port = adapter->physical_port;
492         __u32 val;
493
494         if (adapter->ahw.port_type == QLCNIC_GBE) {
495                 if ((port < 0) || (port > QLCNIC_NIU_MAX_GBE_PORTS))
496                         return;
497                 /* get flow control settings */
498                 val = QLCRD32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port));
499                 pause->rx_pause = qlcnic_gb_get_rx_flowctl(val);
500                 val = QLCRD32(adapter, QLCNIC_NIU_GB_PAUSE_CTL);
501                 switch (port) {
502                 case 0:
503                         pause->tx_pause = !(qlcnic_gb_get_gb0_mask(val));
504                         break;
505                 case 1:
506                         pause->tx_pause = !(qlcnic_gb_get_gb1_mask(val));
507                         break;
508                 case 2:
509                         pause->tx_pause = !(qlcnic_gb_get_gb2_mask(val));
510                         break;
511                 case 3:
512                 default:
513                         pause->tx_pause = !(qlcnic_gb_get_gb3_mask(val));
514                         break;
515                 }
516         } else if (adapter->ahw.port_type == QLCNIC_XGBE) {
517                 if ((port < 0) || (port > QLCNIC_NIU_MAX_XG_PORTS))
518                         return;
519                 pause->rx_pause = 1;
520                 val = QLCRD32(adapter, QLCNIC_NIU_XG_PAUSE_CTL);
521                 if (port == 0)
522                         pause->tx_pause = !(qlcnic_xg_get_xg0_mask(val));
523                 else
524                         pause->tx_pause = !(qlcnic_xg_get_xg1_mask(val));
525         } else {
526                 dev_err(&netdev->dev, "Unknown board type: %x\n",
527                                         adapter->ahw.port_type);
528         }
529 }
530
531 static int
532 qlcnic_set_pauseparam(struct net_device *netdev,
533                           struct ethtool_pauseparam *pause)
534 {
535         struct qlcnic_adapter *adapter = netdev_priv(netdev);
536         int port = adapter->physical_port;
537         __u32 val;
538
539         /* read mode */
540         if (adapter->ahw.port_type == QLCNIC_GBE) {
541                 if ((port < 0) || (port > QLCNIC_NIU_MAX_GBE_PORTS))
542                         return -EIO;
543                 /* set flow control */
544                 val = QLCRD32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port));
545
546                 if (pause->rx_pause)
547                         qlcnic_gb_rx_flowctl(val);
548                 else
549                         qlcnic_gb_unset_rx_flowctl(val);
550
551                 QLCWR32(adapter, QLCNIC_NIU_GB_MAC_CONFIG_0(port),
552                                 val);
553                 /* set autoneg */
554                 val = QLCRD32(adapter, QLCNIC_NIU_GB_PAUSE_CTL);
555                 switch (port) {
556                 case 0:
557                         if (pause->tx_pause)
558                                 qlcnic_gb_unset_gb0_mask(val);
559                         else
560                                 qlcnic_gb_set_gb0_mask(val);
561                         break;
562                 case 1:
563                         if (pause->tx_pause)
564                                 qlcnic_gb_unset_gb1_mask(val);
565                         else
566                                 qlcnic_gb_set_gb1_mask(val);
567                         break;
568                 case 2:
569                         if (pause->tx_pause)
570                                 qlcnic_gb_unset_gb2_mask(val);
571                         else
572                                 qlcnic_gb_set_gb2_mask(val);
573                         break;
574                 case 3:
575                 default:
576                         if (pause->tx_pause)
577                                 qlcnic_gb_unset_gb3_mask(val);
578                         else
579                                 qlcnic_gb_set_gb3_mask(val);
580                         break;
581                 }
582                 QLCWR32(adapter, QLCNIC_NIU_GB_PAUSE_CTL, val);
583         } else if (adapter->ahw.port_type == QLCNIC_XGBE) {
584                 if ((port < 0) || (port > QLCNIC_NIU_MAX_XG_PORTS))
585                         return -EIO;
586                 val = QLCRD32(adapter, QLCNIC_NIU_XG_PAUSE_CTL);
587                 if (port == 0) {
588                         if (pause->tx_pause)
589                                 qlcnic_xg_unset_xg0_mask(val);
590                         else
591                                 qlcnic_xg_set_xg0_mask(val);
592                 } else {
593                         if (pause->tx_pause)
594                                 qlcnic_xg_unset_xg1_mask(val);
595                         else
596                                 qlcnic_xg_set_xg1_mask(val);
597                 }
598                 QLCWR32(adapter, QLCNIC_NIU_XG_PAUSE_CTL, val);
599         } else {
600                 dev_err(&netdev->dev, "Unknown board type: %x\n",
601                                 adapter->ahw.port_type);
602         }
603         return 0;
604 }
605
606 static int qlcnic_reg_test(struct net_device *dev)
607 {
608         struct qlcnic_adapter *adapter = netdev_priv(dev);
609         u32 data_read, data_written;
610
611         data_read = QLCRD32(adapter, QLCNIC_PCIX_PH_REG(0));
612         if ((data_read & 0xffff) != adapter->pdev->vendor)
613                 return 1;
614
615         data_written = (u32)0xa5a5a5a5;
616
617         QLCWR32(adapter, CRB_SCRATCHPAD_TEST, data_written);
618         data_read = QLCRD32(adapter, CRB_SCRATCHPAD_TEST);
619         if (data_written != data_read)
620                 return 1;
621
622         return 0;
623 }
624
625 static int qlcnic_get_sset_count(struct net_device *dev, int sset)
626 {
627         switch (sset) {
628         case ETH_SS_TEST:
629                 return QLCNIC_TEST_LEN;
630         case ETH_SS_STATS:
631                 return QLCNIC_STATS_LEN;
632         default:
633                 return -EOPNOTSUPP;
634         }
635 }
636
637 #define QLC_ILB_PKT_SIZE 64
638
639 static void qlcnic_create_loopback_buff(unsigned char *data)
640 {
641         unsigned char random_data[] = {0xa8, 0x06, 0x45, 0x00};
642         memset(data, 0x4e, QLC_ILB_PKT_SIZE);
643         memset(data, 0xff, 12);
644         memcpy(data + 12, random_data, sizeof(random_data));
645 }
646
647 int qlcnic_check_loopback_buff(unsigned char *data)
648 {
649         unsigned char buff[QLC_ILB_PKT_SIZE];
650         qlcnic_create_loopback_buff(buff);
651         return memcmp(data, buff, QLC_ILB_PKT_SIZE);
652 }
653
654 static int qlcnic_do_ilb_test(struct qlcnic_adapter *adapter)
655 {
656         struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
657         struct qlcnic_host_sds_ring *sds_ring = &recv_ctx->sds_rings[0];
658         struct sk_buff *skb;
659         int i;
660
661         for (i = 0; i < 16; i++) {
662                 skb = dev_alloc_skb(QLC_ILB_PKT_SIZE);
663                 qlcnic_create_loopback_buff(skb->data);
664                 skb_put(skb, QLC_ILB_PKT_SIZE);
665
666                 adapter->diag_cnt = 0;
667
668                 qlcnic_xmit_frame(skb, adapter->netdev);
669
670                 msleep(5);
671
672                 qlcnic_process_rcv_ring_diag(sds_ring);
673
674                 dev_kfree_skb_any(skb);
675                 if (!adapter->diag_cnt)
676                         return -1;
677         }
678         return 0;
679 }
680
681 static int qlcnic_loopback_test(struct net_device *netdev)
682 {
683         struct qlcnic_adapter *adapter = netdev_priv(netdev);
684         int max_sds_rings = adapter->max_sds_rings;
685         int ret;
686
687         if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
688                 return -EIO;
689
690         ret = qlcnic_diag_alloc_res(netdev, QLCNIC_LOOPBACK_TEST);
691         if (ret)
692                 goto clear_it;
693
694         ret = qlcnic_set_ilb_mode(adapter);
695         if (ret)
696                 goto done;
697
698         ret = qlcnic_do_ilb_test(adapter);
699
700         qlcnic_clear_ilb_mode(adapter);
701
702 done:
703         qlcnic_diag_free_res(netdev, max_sds_rings);
704
705 clear_it:
706         adapter->max_sds_rings = max_sds_rings;
707         clear_bit(__QLCNIC_RESETTING, &adapter->state);
708         return ret;
709 }
710
711 static int qlcnic_irq_test(struct net_device *netdev)
712 {
713         struct qlcnic_adapter *adapter = netdev_priv(netdev);
714         int max_sds_rings = adapter->max_sds_rings;
715         int ret;
716
717         if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
718                 return -EIO;
719
720         ret = qlcnic_diag_alloc_res(netdev, QLCNIC_INTERRUPT_TEST);
721         if (ret)
722                 goto clear_it;
723
724         adapter->diag_cnt = 0;
725         ret = qlcnic_issue_cmd(adapter, adapter->ahw.pci_func,
726                         QLCHAL_VERSION, adapter->portnum, 0, 0, 0x00000011);
727         if (ret)
728                 goto done;
729
730         msleep(10);
731
732         ret = !adapter->diag_cnt;
733
734 done:
735         qlcnic_diag_free_res(netdev, max_sds_rings);
736
737 clear_it:
738         adapter->max_sds_rings = max_sds_rings;
739         clear_bit(__QLCNIC_RESETTING, &adapter->state);
740         return ret;
741 }
742
743 static void
744 qlcnic_diag_test(struct net_device *dev, struct ethtool_test *eth_test,
745                      u64 *data)
746 {
747         memset(data, 0, sizeof(u64) * QLCNIC_TEST_LEN);
748
749         if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
750                 data[2] = qlcnic_irq_test(dev);
751                 if (data[2])
752                         eth_test->flags |= ETH_TEST_FL_FAILED;
753
754                 data[3] = qlcnic_loopback_test(dev);
755                 if (data[3])
756                         eth_test->flags |= ETH_TEST_FL_FAILED;
757
758         }
759
760         data[0] = qlcnic_reg_test(dev);
761         if (data[0])
762                 eth_test->flags |= ETH_TEST_FL_FAILED;
763
764         /* link test */
765         data[1] = (u64) qlcnic_test_link(dev);
766         if (data[1])
767                 eth_test->flags |= ETH_TEST_FL_FAILED;
768 }
769
770 static void
771 qlcnic_get_strings(struct net_device *dev, u32 stringset, u8 * data)
772 {
773         int index;
774
775         switch (stringset) {
776         case ETH_SS_TEST:
777                 memcpy(data, *qlcnic_gstrings_test,
778                        QLCNIC_TEST_LEN * ETH_GSTRING_LEN);
779                 break;
780         case ETH_SS_STATS:
781                 for (index = 0; index < QLCNIC_STATS_LEN; index++) {
782                         memcpy(data + index * ETH_GSTRING_LEN,
783                                qlcnic_gstrings_stats[index].stat_string,
784                                ETH_GSTRING_LEN);
785                 }
786                 break;
787         }
788 }
789
790 static void
791 qlcnic_get_ethtool_stats(struct net_device *dev,
792                              struct ethtool_stats *stats, u64 * data)
793 {
794         struct qlcnic_adapter *adapter = netdev_priv(dev);
795         int index;
796
797         for (index = 0; index < QLCNIC_STATS_LEN; index++) {
798                 char *p =
799                     (char *)adapter +
800                     qlcnic_gstrings_stats[index].stat_offset;
801                 data[index] =
802                     (qlcnic_gstrings_stats[index].sizeof_stat ==
803                      sizeof(u64)) ? *(u64 *)p:(*(u32 *)p);
804         }
805 }
806
807 static u32 qlcnic_get_tx_csum(struct net_device *dev)
808 {
809         return dev->features & NETIF_F_IP_CSUM;
810 }
811
812 static u32 qlcnic_get_rx_csum(struct net_device *dev)
813 {
814         struct qlcnic_adapter *adapter = netdev_priv(dev);
815         return adapter->rx_csum;
816 }
817
818 static int qlcnic_set_rx_csum(struct net_device *dev, u32 data)
819 {
820         struct qlcnic_adapter *adapter = netdev_priv(dev);
821         adapter->rx_csum = !!data;
822         return 0;
823 }
824
825 static u32 qlcnic_get_tso(struct net_device *dev)
826 {
827         return (dev->features & (NETIF_F_TSO | NETIF_F_TSO6)) != 0;
828 }
829
830 static int qlcnic_set_tso(struct net_device *dev, u32 data)
831 {
832         if (data)
833                 dev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
834         else
835                 dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
836
837         return 0;
838 }
839
840 static int qlcnic_blink_led(struct net_device *dev, u32 val)
841 {
842         struct qlcnic_adapter *adapter = netdev_priv(dev);
843         int ret;
844
845         ret = qlcnic_config_led(adapter, 1, 0xf);
846         if (ret) {
847                 dev_err(&adapter->pdev->dev,
848                         "Failed to set LED blink state.\n");
849                 return ret;
850         }
851
852         msleep_interruptible(val * 1000);
853
854         ret = qlcnic_config_led(adapter, 0, 0xf);
855         if (ret) {
856                 dev_err(&adapter->pdev->dev,
857                         "Failed to reset LED blink state.\n");
858                 return ret;
859         }
860
861         return 0;
862 }
863
864 static void
865 qlcnic_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
866 {
867         struct qlcnic_adapter *adapter = netdev_priv(dev);
868         u32 wol_cfg;
869
870         wol->supported = 0;
871         wol->wolopts = 0;
872
873         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
874         if (wol_cfg & (1UL << adapter->portnum))
875                 wol->supported |= WAKE_MAGIC;
876
877         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
878         if (wol_cfg & (1UL << adapter->portnum))
879                 wol->wolopts |= WAKE_MAGIC;
880 }
881
882 static int
883 qlcnic_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
884 {
885         struct qlcnic_adapter *adapter = netdev_priv(dev);
886         u32 wol_cfg;
887
888         if (wol->wolopts & ~WAKE_MAGIC)
889                 return -EOPNOTSUPP;
890
891         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
892         if (!(wol_cfg & (1 << adapter->portnum)))
893                 return -EOPNOTSUPP;
894
895         wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
896         if (wol->wolopts & WAKE_MAGIC)
897                 wol_cfg |= 1UL << adapter->portnum;
898         else
899                 wol_cfg &= ~(1UL << adapter->portnum);
900
901         QLCWR32(adapter, QLCNIC_WOL_CONFIG, wol_cfg);
902
903         return 0;
904 }
905
906 /*
907  * Set the coalescing parameters. Currently only normal is supported.
908  * If rx_coalesce_usecs == 0 or rx_max_coalesced_frames == 0 then set the
909  * firmware coalescing to default.
910  */
911 static int qlcnic_set_intr_coalesce(struct net_device *netdev,
912                         struct ethtool_coalesce *ethcoal)
913 {
914         struct qlcnic_adapter *adapter = netdev_priv(netdev);
915
916         if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
917                 return -EINVAL;
918
919         /*
920         * Return Error if unsupported values or
921         * unsupported parameters are set.
922         */
923         if (ethcoal->rx_coalesce_usecs > 0xffff ||
924                 ethcoal->rx_max_coalesced_frames > 0xffff ||
925                 ethcoal->tx_coalesce_usecs > 0xffff ||
926                 ethcoal->tx_max_coalesced_frames > 0xffff ||
927                 ethcoal->rx_coalesce_usecs_irq ||
928                 ethcoal->rx_max_coalesced_frames_irq ||
929                 ethcoal->tx_coalesce_usecs_irq ||
930                 ethcoal->tx_max_coalesced_frames_irq ||
931                 ethcoal->stats_block_coalesce_usecs ||
932                 ethcoal->use_adaptive_rx_coalesce ||
933                 ethcoal->use_adaptive_tx_coalesce ||
934                 ethcoal->pkt_rate_low ||
935                 ethcoal->rx_coalesce_usecs_low ||
936                 ethcoal->rx_max_coalesced_frames_low ||
937                 ethcoal->tx_coalesce_usecs_low ||
938                 ethcoal->tx_max_coalesced_frames_low ||
939                 ethcoal->pkt_rate_high ||
940                 ethcoal->rx_coalesce_usecs_high ||
941                 ethcoal->rx_max_coalesced_frames_high ||
942                 ethcoal->tx_coalesce_usecs_high ||
943                 ethcoal->tx_max_coalesced_frames_high)
944                 return -EINVAL;
945
946         if (!ethcoal->rx_coalesce_usecs ||
947                 !ethcoal->rx_max_coalesced_frames) {
948                 adapter->coal.flags = QLCNIC_INTR_DEFAULT;
949                 adapter->coal.normal.data.rx_time_us =
950                         QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
951                 adapter->coal.normal.data.rx_packets =
952                         QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
953         } else {
954                 adapter->coal.flags = 0;
955                 adapter->coal.normal.data.rx_time_us =
956                 ethcoal->rx_coalesce_usecs;
957                 adapter->coal.normal.data.rx_packets =
958                 ethcoal->rx_max_coalesced_frames;
959         }
960         adapter->coal.normal.data.tx_time_us = ethcoal->tx_coalesce_usecs;
961         adapter->coal.normal.data.tx_packets =
962         ethcoal->tx_max_coalesced_frames;
963
964         qlcnic_config_intr_coalesce(adapter);
965
966         return 0;
967 }
968
969 static int qlcnic_get_intr_coalesce(struct net_device *netdev,
970                         struct ethtool_coalesce *ethcoal)
971 {
972         struct qlcnic_adapter *adapter = netdev_priv(netdev);
973
974         if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
975                 return -EINVAL;
976
977         ethcoal->rx_coalesce_usecs = adapter->coal.normal.data.rx_time_us;
978         ethcoal->tx_coalesce_usecs = adapter->coal.normal.data.tx_time_us;
979         ethcoal->rx_max_coalesced_frames =
980                 adapter->coal.normal.data.rx_packets;
981         ethcoal->tx_max_coalesced_frames =
982                 adapter->coal.normal.data.tx_packets;
983
984         return 0;
985 }
986
987 static int qlcnic_set_flags(struct net_device *netdev, u32 data)
988 {
989         struct qlcnic_adapter *adapter = netdev_priv(netdev);
990         int hw_lro;
991
992         if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO))
993                 return -EINVAL;
994
995         ethtool_op_set_flags(netdev, data);
996
997         hw_lro = (data & ETH_FLAG_LRO) ? QLCNIC_LRO_ENABLED : 0;
998
999         if (qlcnic_config_hw_lro(adapter, hw_lro))
1000                 return -EIO;
1001
1002         if ((hw_lro == 0) && qlcnic_send_lro_cleanup(adapter))
1003                 return -EIO;
1004
1005
1006         return 0;
1007 }
1008
1009 static u32 qlcnic_get_msglevel(struct net_device *netdev)
1010 {
1011         struct qlcnic_adapter *adapter = netdev_priv(netdev);
1012
1013         return adapter->msg_enable;
1014 }
1015
1016 static void qlcnic_set_msglevel(struct net_device *netdev, u32 msglvl)
1017 {
1018         struct qlcnic_adapter *adapter = netdev_priv(netdev);
1019
1020         adapter->msg_enable = msglvl;
1021 }
1022
1023 const struct ethtool_ops qlcnic_ethtool_ops = {
1024         .get_settings = qlcnic_get_settings,
1025         .set_settings = qlcnic_set_settings,
1026         .get_drvinfo = qlcnic_get_drvinfo,
1027         .get_regs_len = qlcnic_get_regs_len,
1028         .get_regs = qlcnic_get_regs,
1029         .get_link = ethtool_op_get_link,
1030         .get_eeprom_len = qlcnic_get_eeprom_len,
1031         .get_eeprom = qlcnic_get_eeprom,
1032         .get_ringparam = qlcnic_get_ringparam,
1033         .set_ringparam = qlcnic_set_ringparam,
1034         .get_pauseparam = qlcnic_get_pauseparam,
1035         .set_pauseparam = qlcnic_set_pauseparam,
1036         .get_tx_csum = qlcnic_get_tx_csum,
1037         .set_tx_csum = ethtool_op_set_tx_csum,
1038         .set_sg = ethtool_op_set_sg,
1039         .get_tso = qlcnic_get_tso,
1040         .set_tso = qlcnic_set_tso,
1041         .get_wol = qlcnic_get_wol,
1042         .set_wol = qlcnic_set_wol,
1043         .self_test = qlcnic_diag_test,
1044         .get_strings = qlcnic_get_strings,
1045         .get_ethtool_stats = qlcnic_get_ethtool_stats,
1046         .get_sset_count = qlcnic_get_sset_count,
1047         .get_rx_csum = qlcnic_get_rx_csum,
1048         .set_rx_csum = qlcnic_set_rx_csum,
1049         .get_coalesce = qlcnic_get_intr_coalesce,
1050         .set_coalesce = qlcnic_set_intr_coalesce,
1051         .get_flags = ethtool_op_get_flags,
1052         .set_flags = qlcnic_set_flags,
1053         .phys_id = qlcnic_blink_led,
1054         .set_msglevel = qlcnic_set_msglevel,
1055         .get_msglevel = qlcnic_get_msglevel,
1056 };