]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/emulex/benet/be_ethtool.c
be2net: add a counter for pkts dropped in xmit path
[karo-tx-linux.git] / drivers / net / ethernet / emulex / benet / be_ethtool.c
1 /*
2  * Copyright (C) 2005 - 2013 Emulex
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 version 2
7  * as published by the Free Software Foundation.  The full GNU General
8  * Public License is included in this distribution in the file called COPYING.
9  *
10  * Contact Information:
11  * linux-drivers@emulex.com
12  *
13  * Emulex
14  * 3333 Susan Street
15  * Costa Mesa, CA 92626
16  */
17
18 #include "be.h"
19 #include "be_cmds.h"
20 #include <linux/ethtool.h>
21
22 struct be_ethtool_stat {
23         char desc[ETH_GSTRING_LEN];
24         int type;
25         int size;
26         int offset;
27 };
28
29 enum {DRVSTAT_TX, DRVSTAT_RX, DRVSTAT};
30 #define FIELDINFO(_struct, field) FIELD_SIZEOF(_struct, field), \
31                                         offsetof(_struct, field)
32 #define DRVSTAT_TX_INFO(field)  #field, DRVSTAT_TX,\
33                                         FIELDINFO(struct be_tx_stats, field)
34 #define DRVSTAT_RX_INFO(field)  #field, DRVSTAT_RX,\
35                                         FIELDINFO(struct be_rx_stats, field)
36 #define DRVSTAT_INFO(field)     #field, DRVSTAT,\
37                                         FIELDINFO(struct be_drv_stats, field)
38
39 static const struct be_ethtool_stat et_stats[] = {
40         {DRVSTAT_INFO(rx_crc_errors)},
41         {DRVSTAT_INFO(rx_alignment_symbol_errors)},
42         {DRVSTAT_INFO(rx_pause_frames)},
43         {DRVSTAT_INFO(rx_control_frames)},
44         /* Received packets dropped when the Ethernet length field
45          * is not equal to the actual Ethernet data length.
46          */
47         {DRVSTAT_INFO(rx_in_range_errors)},
48         /* Received packets dropped when their length field is >= 1501 bytes
49          * and <= 1535 bytes.
50          */
51         {DRVSTAT_INFO(rx_out_range_errors)},
52         /* Received packets dropped when they are longer than 9216 bytes */
53         {DRVSTAT_INFO(rx_frame_too_long)},
54         /* Received packets dropped when they don't pass the unicast or
55          * multicast address filtering.
56          */
57         {DRVSTAT_INFO(rx_address_filtered)},
58         /* Received packets dropped when IP packet length field is less than
59          * the IP header length field.
60          */
61         {DRVSTAT_INFO(rx_dropped_too_small)},
62         /* Received packets dropped when IP length field is greater than
63          * the actual packet length.
64          */
65         {DRVSTAT_INFO(rx_dropped_too_short)},
66         /* Received packets dropped when the IP header length field is less
67          * than 5.
68          */
69         {DRVSTAT_INFO(rx_dropped_header_too_small)},
70         /* Received packets dropped when the TCP header length field is less
71          * than 5 or the TCP header length + IP header length is more
72          * than IP packet length.
73          */
74         {DRVSTAT_INFO(rx_dropped_tcp_length)},
75         {DRVSTAT_INFO(rx_dropped_runt)},
76         /* Number of received packets dropped when a fifo for descriptors going
77          * into the packet demux block overflows. In normal operation, this
78          * fifo must never overflow.
79          */
80         {DRVSTAT_INFO(rxpp_fifo_overflow_drop)},
81         {DRVSTAT_INFO(rx_input_fifo_overflow_drop)},
82         {DRVSTAT_INFO(rx_ip_checksum_errs)},
83         {DRVSTAT_INFO(rx_tcp_checksum_errs)},
84         {DRVSTAT_INFO(rx_udp_checksum_errs)},
85         {DRVSTAT_INFO(tx_pauseframes)},
86         {DRVSTAT_INFO(tx_controlframes)},
87         {DRVSTAT_INFO(rx_priority_pause_frames)},
88         {DRVSTAT_INFO(tx_priority_pauseframes)},
89         /* Received packets dropped when an internal fifo going into
90          * main packet buffer tank (PMEM) overflows.
91          */
92         {DRVSTAT_INFO(pmem_fifo_overflow_drop)},
93         {DRVSTAT_INFO(jabber_events)},
94         /* Received packets dropped due to lack of available HW packet buffers
95          * used to temporarily hold the received packets.
96          */
97         {DRVSTAT_INFO(rx_drops_no_pbuf)},
98         /* Received packets dropped due to input receive buffer
99          * descriptor fifo overflowing.
100          */
101         {DRVSTAT_INFO(rx_drops_no_erx_descr)},
102         /* Packets dropped because the internal FIFO to the offloaded TCP
103          * receive processing block is full. This could happen only for
104          * offloaded iSCSI or FCoE trarffic.
105          */
106         {DRVSTAT_INFO(rx_drops_no_tpre_descr)},
107         /* Received packets dropped when they need more than 8
108          * receive buffers. This cannot happen as the driver configures
109          * 2048 byte receive buffers.
110          */
111         {DRVSTAT_INFO(rx_drops_too_many_frags)},
112         {DRVSTAT_INFO(forwarded_packets)},
113         /* Received packets dropped when the frame length
114          * is more than 9018 bytes
115          */
116         {DRVSTAT_INFO(rx_drops_mtu)},
117         /* Number of packets dropped due to random early drop function */
118         {DRVSTAT_INFO(eth_red_drops)},
119         {DRVSTAT_INFO(be_on_die_temperature)}
120 };
121 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats)
122
123 /* Stats related to multi RX queues: get_stats routine assumes bytes, pkts
124  * are first and second members respectively.
125  */
126 static const struct be_ethtool_stat et_rx_stats[] = {
127         {DRVSTAT_RX_INFO(rx_bytes)},/* If moving this member see above note */
128         {DRVSTAT_RX_INFO(rx_pkts)}, /* If moving this member see above note */
129         {DRVSTAT_RX_INFO(rx_compl)},
130         {DRVSTAT_RX_INFO(rx_mcast_pkts)},
131         /* Number of page allocation failures while posting receive buffers
132          * to HW.
133          */
134         {DRVSTAT_RX_INFO(rx_post_fail)},
135         /* Recevied packets dropped due to skb allocation failure */
136         {DRVSTAT_RX_INFO(rx_drops_no_skbs)},
137         /* Received packets dropped due to lack of available fetched buffers
138          * posted by the driver.
139          */
140         {DRVSTAT_RX_INFO(rx_drops_no_frags)}
141 };
142 #define ETHTOOL_RXSTATS_NUM (ARRAY_SIZE(et_rx_stats))
143
144 /* Stats related to multi TX queues: get_stats routine assumes compl is the
145  * first member
146  */
147 static const struct be_ethtool_stat et_tx_stats[] = {
148         {DRVSTAT_TX_INFO(tx_compl)}, /* If moving this member see above note */
149         {DRVSTAT_TX_INFO(tx_bytes)},
150         {DRVSTAT_TX_INFO(tx_pkts)},
151         /* Number of skbs queued for trasmission by the driver */
152         {DRVSTAT_TX_INFO(tx_reqs)},
153         /* Number of TX work request blocks DMAed to HW */
154         {DRVSTAT_TX_INFO(tx_wrbs)},
155         /* Number of times the TX queue was stopped due to lack
156          * of spaces in the TXQ.
157          */
158         {DRVSTAT_TX_INFO(tx_stops)},
159         /* Pkts dropped in the driver's transmit path */
160         {DRVSTAT_TX_INFO(tx_drv_drops)}
161 };
162 #define ETHTOOL_TXSTATS_NUM (ARRAY_SIZE(et_tx_stats))
163
164 static const char et_self_tests[][ETH_GSTRING_LEN] = {
165         "MAC Loopback test",
166         "PHY Loopback test",
167         "External Loopback test",
168         "DDR DMA test",
169         "Link test"
170 };
171
172 #define ETHTOOL_TESTS_NUM ARRAY_SIZE(et_self_tests)
173 #define BE_MAC_LOOPBACK 0x0
174 #define BE_PHY_LOOPBACK 0x1
175 #define BE_ONE_PORT_EXT_LOOPBACK 0x2
176 #define BE_NO_LOOPBACK 0xff
177
178 static void be_get_drvinfo(struct net_device *netdev,
179                                 struct ethtool_drvinfo *drvinfo)
180 {
181         struct be_adapter *adapter = netdev_priv(netdev);
182
183         strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
184         strlcpy(drvinfo->version, DRV_VER, sizeof(drvinfo->version));
185         if (!memcmp(adapter->fw_ver, adapter->fw_on_flash, FW_VER_LEN))
186                 strlcpy(drvinfo->fw_version, adapter->fw_ver,
187                         sizeof(drvinfo->fw_version));
188         else
189                 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
190                          "%s [%s]", adapter->fw_ver, adapter->fw_on_flash);
191
192         strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
193                 sizeof(drvinfo->bus_info));
194         drvinfo->testinfo_len = 0;
195         drvinfo->regdump_len = 0;
196         drvinfo->eedump_len = 0;
197 }
198
199 static u32
200 lancer_cmd_get_file_len(struct be_adapter *adapter, u8 *file_name)
201 {
202         u32 data_read = 0, eof;
203         u8 addn_status;
204         struct be_dma_mem data_len_cmd;
205         int status;
206
207         memset(&data_len_cmd, 0, sizeof(data_len_cmd));
208         /* data_offset and data_size should be 0 to get reg len */
209         status = lancer_cmd_read_object(adapter, &data_len_cmd, 0, 0,
210                                 file_name, &data_read, &eof, &addn_status);
211
212         return data_read;
213 }
214
215 static int
216 lancer_cmd_read_file(struct be_adapter *adapter, u8 *file_name,
217                 u32 buf_len, void *buf)
218 {
219         struct be_dma_mem read_cmd;
220         u32 read_len = 0, total_read_len = 0, chunk_size;
221         u32 eof = 0;
222         u8 addn_status;
223         int status = 0;
224
225         read_cmd.size = LANCER_READ_FILE_CHUNK;
226         read_cmd.va = pci_alloc_consistent(adapter->pdev, read_cmd.size,
227                         &read_cmd.dma);
228
229         if (!read_cmd.va) {
230                 dev_err(&adapter->pdev->dev,
231                                 "Memory allocation failure while reading dump\n");
232                 return -ENOMEM;
233         }
234
235         while ((total_read_len < buf_len) && !eof) {
236                 chunk_size = min_t(u32, (buf_len - total_read_len),
237                                 LANCER_READ_FILE_CHUNK);
238                 chunk_size = ALIGN(chunk_size, 4);
239                 status = lancer_cmd_read_object(adapter, &read_cmd, chunk_size,
240                                 total_read_len, file_name, &read_len,
241                                 &eof, &addn_status);
242                 if (!status) {
243                         memcpy(buf + total_read_len, read_cmd.va, read_len);
244                         total_read_len += read_len;
245                         eof &= LANCER_READ_FILE_EOF_MASK;
246                 } else {
247                         status = -EIO;
248                         break;
249                 }
250         }
251         pci_free_consistent(adapter->pdev, read_cmd.size, read_cmd.va,
252                         read_cmd.dma);
253
254         return status;
255 }
256
257 static int
258 be_get_reg_len(struct net_device *netdev)
259 {
260         struct be_adapter *adapter = netdev_priv(netdev);
261         u32 log_size = 0;
262
263         if (!check_privilege(adapter, MAX_PRIVILEGES))
264                 return 0;
265
266         if (be_physfn(adapter)) {
267                 if (lancer_chip(adapter))
268                         log_size = lancer_cmd_get_file_len(adapter,
269                                         LANCER_FW_DUMP_FILE);
270                 else
271                         be_cmd_get_reg_len(adapter, &log_size);
272         }
273         return log_size;
274 }
275
276 static void
277 be_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *buf)
278 {
279         struct be_adapter *adapter = netdev_priv(netdev);
280
281         if (be_physfn(adapter)) {
282                 memset(buf, 0, regs->len);
283                 if (lancer_chip(adapter))
284                         lancer_cmd_read_file(adapter, LANCER_FW_DUMP_FILE,
285                                         regs->len, buf);
286                 else
287                         be_cmd_get_regs(adapter, regs->len, buf);
288         }
289 }
290
291 static int be_get_coalesce(struct net_device *netdev,
292                            struct ethtool_coalesce *et)
293 {
294         struct be_adapter *adapter = netdev_priv(netdev);
295         struct be_aic_obj *aic = &adapter->aic_obj[0];
296
297
298         et->rx_coalesce_usecs = aic->prev_eqd;
299         et->rx_coalesce_usecs_high = aic->max_eqd;
300         et->rx_coalesce_usecs_low = aic->min_eqd;
301
302         et->tx_coalesce_usecs = aic->prev_eqd;
303         et->tx_coalesce_usecs_high = aic->max_eqd;
304         et->tx_coalesce_usecs_low = aic->min_eqd;
305
306         et->use_adaptive_rx_coalesce = aic->enable;
307         et->use_adaptive_tx_coalesce = aic->enable;
308
309         return 0;
310 }
311
312 /* TX attributes are ignored. Only RX attributes are considered
313  * eqd cmd is issued in the worker thread.
314  */
315 static int be_set_coalesce(struct net_device *netdev,
316                            struct ethtool_coalesce *et)
317 {
318         struct be_adapter *adapter = netdev_priv(netdev);
319         struct be_aic_obj *aic = &adapter->aic_obj[0];
320         struct be_eq_obj *eqo;
321         int i;
322
323         for_all_evt_queues(adapter, eqo, i) {
324                 aic->enable = et->use_adaptive_rx_coalesce;
325                 aic->max_eqd = min(et->rx_coalesce_usecs_high, BE_MAX_EQD);
326                 aic->min_eqd = min(et->rx_coalesce_usecs_low, aic->max_eqd);
327                 aic->et_eqd = min(et->rx_coalesce_usecs, aic->max_eqd);
328                 aic->et_eqd = max(aic->et_eqd, aic->min_eqd);
329                 aic++;
330         }
331
332         return 0;
333 }
334
335 static void
336 be_get_ethtool_stats(struct net_device *netdev,
337                 struct ethtool_stats *stats, uint64_t *data)
338 {
339         struct be_adapter *adapter = netdev_priv(netdev);
340         struct be_rx_obj *rxo;
341         struct be_tx_obj *txo;
342         void *p;
343         unsigned int i, j, base = 0, start;
344
345         for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
346                 p = (u8 *)&adapter->drv_stats + et_stats[i].offset;
347                 data[i] = *(u32 *)p;
348         }
349         base += ETHTOOL_STATS_NUM;
350
351         for_all_rx_queues(adapter, rxo, j) {
352                 struct be_rx_stats *stats = rx_stats(rxo);
353
354                 do {
355                         start = u64_stats_fetch_begin_bh(&stats->sync);
356                         data[base] = stats->rx_bytes;
357                         data[base + 1] = stats->rx_pkts;
358                 } while (u64_stats_fetch_retry_bh(&stats->sync, start));
359
360                 for (i = 2; i < ETHTOOL_RXSTATS_NUM; i++) {
361                         p = (u8 *)stats + et_rx_stats[i].offset;
362                         data[base + i] = *(u32 *)p;
363                 }
364                 base += ETHTOOL_RXSTATS_NUM;
365         }
366
367         for_all_tx_queues(adapter, txo, j) {
368                 struct be_tx_stats *stats = tx_stats(txo);
369
370                 do {
371                         start = u64_stats_fetch_begin_bh(&stats->sync_compl);
372                         data[base] = stats->tx_compl;
373                 } while (u64_stats_fetch_retry_bh(&stats->sync_compl, start));
374
375                 do {
376                         start = u64_stats_fetch_begin_bh(&stats->sync);
377                         for (i = 1; i < ETHTOOL_TXSTATS_NUM; i++) {
378                                 p = (u8 *)stats + et_tx_stats[i].offset;
379                                 data[base + i] =
380                                         (et_tx_stats[i].size == sizeof(u64)) ?
381                                                 *(u64 *)p : *(u32 *)p;
382                         }
383                 } while (u64_stats_fetch_retry_bh(&stats->sync, start));
384                 base += ETHTOOL_TXSTATS_NUM;
385         }
386 }
387
388 static void
389 be_get_stat_strings(struct net_device *netdev, uint32_t stringset,
390                 uint8_t *data)
391 {
392         struct be_adapter *adapter = netdev_priv(netdev);
393         int i, j;
394
395         switch (stringset) {
396         case ETH_SS_STATS:
397                 for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
398                         memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN);
399                         data += ETH_GSTRING_LEN;
400                 }
401                 for (i = 0; i < adapter->num_rx_qs; i++) {
402                         for (j = 0; j < ETHTOOL_RXSTATS_NUM; j++) {
403                                 sprintf(data, "rxq%d: %s", i,
404                                         et_rx_stats[j].desc);
405                                 data += ETH_GSTRING_LEN;
406                         }
407                 }
408                 for (i = 0; i < adapter->num_tx_qs; i++) {
409                         for (j = 0; j < ETHTOOL_TXSTATS_NUM; j++) {
410                                 sprintf(data, "txq%d: %s", i,
411                                         et_tx_stats[j].desc);
412                                 data += ETH_GSTRING_LEN;
413                         }
414                 }
415                 break;
416         case ETH_SS_TEST:
417                 for (i = 0; i < ETHTOOL_TESTS_NUM; i++) {
418                         memcpy(data, et_self_tests[i], ETH_GSTRING_LEN);
419                         data += ETH_GSTRING_LEN;
420                 }
421                 break;
422         }
423 }
424
425 static int be_get_sset_count(struct net_device *netdev, int stringset)
426 {
427         struct be_adapter *adapter = netdev_priv(netdev);
428
429         switch (stringset) {
430         case ETH_SS_TEST:
431                 return ETHTOOL_TESTS_NUM;
432         case ETH_SS_STATS:
433                 return ETHTOOL_STATS_NUM +
434                         adapter->num_rx_qs * ETHTOOL_RXSTATS_NUM +
435                         adapter->num_tx_qs * ETHTOOL_TXSTATS_NUM;
436         default:
437                 return -EINVAL;
438         }
439 }
440
441 static u32 be_get_port_type(u32 phy_type, u32 dac_cable_len)
442 {
443         u32 port;
444
445         switch (phy_type) {
446         case PHY_TYPE_BASET_1GB:
447         case PHY_TYPE_BASEX_1GB:
448         case PHY_TYPE_SGMII:
449                 port = PORT_TP;
450                 break;
451         case PHY_TYPE_SFP_PLUS_10GB:
452                 port = dac_cable_len ? PORT_DA : PORT_FIBRE;
453                 break;
454         case PHY_TYPE_XFP_10GB:
455         case PHY_TYPE_SFP_1GB:
456                 port = PORT_FIBRE;
457                 break;
458         case PHY_TYPE_BASET_10GB:
459                 port = PORT_TP;
460                 break;
461         default:
462                 port = PORT_OTHER;
463         }
464
465         return port;
466 }
467
468 static u32 convert_to_et_setting(u32 if_type, u32 if_speeds)
469 {
470         u32 val = 0;
471
472         switch (if_type) {
473         case PHY_TYPE_BASET_1GB:
474         case PHY_TYPE_BASEX_1GB:
475         case PHY_TYPE_SGMII:
476                 val |= SUPPORTED_TP;
477                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
478                         val |= SUPPORTED_1000baseT_Full;
479                 if (if_speeds & BE_SUPPORTED_SPEED_100MBPS)
480                         val |= SUPPORTED_100baseT_Full;
481                 if (if_speeds & BE_SUPPORTED_SPEED_10MBPS)
482                         val |= SUPPORTED_10baseT_Full;
483                 break;
484         case PHY_TYPE_KX4_10GB:
485                 val |= SUPPORTED_Backplane;
486                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
487                         val |= SUPPORTED_1000baseKX_Full;
488                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
489                         val |= SUPPORTED_10000baseKX4_Full;
490                 break;
491         case PHY_TYPE_KR_10GB:
492                 val |= SUPPORTED_Backplane |
493                                 SUPPORTED_10000baseKR_Full;
494                 break;
495         case PHY_TYPE_SFP_PLUS_10GB:
496         case PHY_TYPE_XFP_10GB:
497         case PHY_TYPE_SFP_1GB:
498                 val |= SUPPORTED_FIBRE;
499                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
500                         val |= SUPPORTED_10000baseT_Full;
501                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
502                         val |= SUPPORTED_1000baseT_Full;
503                 break;
504         case PHY_TYPE_BASET_10GB:
505                 val |= SUPPORTED_TP;
506                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
507                         val |= SUPPORTED_10000baseT_Full;
508                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
509                         val |= SUPPORTED_1000baseT_Full;
510                 if (if_speeds & BE_SUPPORTED_SPEED_100MBPS)
511                         val |= SUPPORTED_100baseT_Full;
512                 break;
513         default:
514                 val |= SUPPORTED_TP;
515         }
516
517         return val;
518 }
519
520 bool be_pause_supported(struct be_adapter *adapter)
521 {
522         return (adapter->phy.interface_type == PHY_TYPE_SFP_PLUS_10GB ||
523                 adapter->phy.interface_type == PHY_TYPE_XFP_10GB) ?
524                 false : true;
525 }
526
527 static int be_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
528 {
529         struct be_adapter *adapter = netdev_priv(netdev);
530         u8 link_status;
531         u16 link_speed = 0;
532         int status;
533         u32 auto_speeds;
534         u32 fixed_speeds;
535         u32 dac_cable_len;
536         u16 interface_type;
537
538         if (adapter->phy.link_speed < 0) {
539                 status = be_cmd_link_status_query(adapter, &link_speed,
540                                                   &link_status, 0);
541                 if (!status)
542                         be_link_status_update(adapter, link_status);
543                 ethtool_cmd_speed_set(ecmd, link_speed);
544
545                 status = be_cmd_get_phy_info(adapter);
546                 if (!status) {
547                         interface_type = adapter->phy.interface_type;
548                         auto_speeds = adapter->phy.auto_speeds_supported;
549                         fixed_speeds = adapter->phy.fixed_speeds_supported;
550                         dac_cable_len = adapter->phy.dac_cable_len;
551
552                         ecmd->supported =
553                                 convert_to_et_setting(interface_type,
554                                                       auto_speeds |
555                                                       fixed_speeds);
556                         ecmd->advertising =
557                                 convert_to_et_setting(interface_type,
558                                                       auto_speeds);
559
560                         ecmd->port = be_get_port_type(interface_type,
561                                                       dac_cable_len);
562
563                         if (adapter->phy.auto_speeds_supported) {
564                                 ecmd->supported |= SUPPORTED_Autoneg;
565                                 ecmd->autoneg = AUTONEG_ENABLE;
566                                 ecmd->advertising |= ADVERTISED_Autoneg;
567                         }
568
569                         ecmd->supported |= SUPPORTED_Pause;
570                         if (be_pause_supported(adapter))
571                                 ecmd->advertising |= ADVERTISED_Pause;
572
573                         switch (adapter->phy.interface_type) {
574                         case PHY_TYPE_KR_10GB:
575                         case PHY_TYPE_KX4_10GB:
576                                 ecmd->transceiver = XCVR_INTERNAL;
577                                 break;
578                         default:
579                                 ecmd->transceiver = XCVR_EXTERNAL;
580                                 break;
581                         }
582                 } else {
583                         ecmd->port = PORT_OTHER;
584                         ecmd->autoneg = AUTONEG_DISABLE;
585                         ecmd->transceiver = XCVR_DUMMY1;
586                 }
587
588                 /* Save for future use */
589                 adapter->phy.link_speed = ethtool_cmd_speed(ecmd);
590                 adapter->phy.port_type = ecmd->port;
591                 adapter->phy.transceiver = ecmd->transceiver;
592                 adapter->phy.autoneg = ecmd->autoneg;
593                 adapter->phy.advertising = ecmd->advertising;
594                 adapter->phy.supported = ecmd->supported;
595         } else {
596                 ethtool_cmd_speed_set(ecmd, adapter->phy.link_speed);
597                 ecmd->port = adapter->phy.port_type;
598                 ecmd->transceiver = adapter->phy.transceiver;
599                 ecmd->autoneg = adapter->phy.autoneg;
600                 ecmd->advertising = adapter->phy.advertising;
601                 ecmd->supported = adapter->phy.supported;
602         }
603
604         ecmd->duplex = netif_carrier_ok(netdev) ? DUPLEX_FULL : DUPLEX_UNKNOWN;
605         ecmd->phy_address = adapter->port_num;
606
607         return 0;
608 }
609
610 static void be_get_ringparam(struct net_device *netdev,
611                              struct ethtool_ringparam *ring)
612 {
613         struct be_adapter *adapter = netdev_priv(netdev);
614
615         ring->rx_max_pending = ring->rx_pending = adapter->rx_obj[0].q.len;
616         ring->tx_max_pending = ring->tx_pending = adapter->tx_obj[0].q.len;
617 }
618
619 static void
620 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
621 {
622         struct be_adapter *adapter = netdev_priv(netdev);
623
624         be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause);
625         ecmd->autoneg = adapter->phy.fc_autoneg;
626 }
627
628 static int
629 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
630 {
631         struct be_adapter *adapter = netdev_priv(netdev);
632         int status;
633
634         if (ecmd->autoneg != adapter->phy.fc_autoneg)
635                 return -EINVAL;
636         adapter->tx_fc = ecmd->tx_pause;
637         adapter->rx_fc = ecmd->rx_pause;
638
639         status = be_cmd_set_flow_control(adapter,
640                                         adapter->tx_fc, adapter->rx_fc);
641         if (status)
642                 dev_warn(&adapter->pdev->dev, "Pause param set failed.\n");
643
644         return status;
645 }
646
647 static int
648 be_set_phys_id(struct net_device *netdev,
649                enum ethtool_phys_id_state state)
650 {
651         struct be_adapter *adapter = netdev_priv(netdev);
652
653         switch (state) {
654         case ETHTOOL_ID_ACTIVE:
655                 be_cmd_get_beacon_state(adapter, adapter->hba_port_num,
656                                         &adapter->beacon_state);
657                 return 1;       /* cycle on/off once per second */
658
659         case ETHTOOL_ID_ON:
660                 be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0,
661                                         BEACON_STATE_ENABLED);
662                 break;
663
664         case ETHTOOL_ID_OFF:
665                 be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0,
666                                         BEACON_STATE_DISABLED);
667                 break;
668
669         case ETHTOOL_ID_INACTIVE:
670                 be_cmd_set_beacon_state(adapter, adapter->hba_port_num, 0, 0,
671                                         adapter->beacon_state);
672         }
673
674         return 0;
675 }
676
677 static int be_set_dump(struct net_device *netdev, struct ethtool_dump *dump)
678 {
679         struct be_adapter *adapter = netdev_priv(netdev);
680         struct device *dev = &adapter->pdev->dev;
681         int status;
682
683         if (!lancer_chip(adapter)) {
684                 dev_err(dev, "FW dump not supported\n");
685                 return -EOPNOTSUPP;
686         }
687
688         if (dump_present(adapter)) {
689                 dev_err(dev, "Previous dump not cleared, not forcing dump\n");
690                 return 0;
691         }
692
693         switch (dump->flag) {
694         case LANCER_INITIATE_FW_DUMP:
695                 status = lancer_initiate_dump(adapter);
696                 if (!status)
697                         dev_info(dev, "F/w dump initiated successfully\n");
698                 break;
699         default:
700                 dev_err(dev, "Invalid dump level: 0x%x\n", dump->flag);
701                 return -EINVAL;
702         }
703         return status;
704 }
705
706 static void
707 be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
708 {
709         struct be_adapter *adapter = netdev_priv(netdev);
710
711         if (be_is_wol_supported(adapter)) {
712                 wol->supported |= WAKE_MAGIC;
713                 if (adapter->wol)
714                         wol->wolopts |= WAKE_MAGIC;
715         } else
716                 wol->wolopts = 0;
717         memset(&wol->sopass, 0, sizeof(wol->sopass));
718 }
719
720 static int
721 be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
722 {
723         struct be_adapter *adapter = netdev_priv(netdev);
724
725         if (wol->wolopts & ~WAKE_MAGIC)
726                 return -EOPNOTSUPP;
727
728         if (!be_is_wol_supported(adapter)) {
729                 dev_warn(&adapter->pdev->dev, "WOL not supported\n");
730                 return -EOPNOTSUPP;
731         }
732
733         if (wol->wolopts & WAKE_MAGIC)
734                 adapter->wol = true;
735         else
736                 adapter->wol = false;
737
738         return 0;
739 }
740
741 static int
742 be_test_ddr_dma(struct be_adapter *adapter)
743 {
744         int ret, i;
745         struct be_dma_mem ddrdma_cmd;
746         static const u64 pattern[2] = {
747                 0x5a5a5a5a5a5a5a5aULL, 0xa5a5a5a5a5a5a5a5ULL
748         };
749
750         ddrdma_cmd.size = sizeof(struct be_cmd_req_ddrdma_test);
751         ddrdma_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, ddrdma_cmd.size,
752                                            &ddrdma_cmd.dma, GFP_KERNEL);
753         if (!ddrdma_cmd.va)
754                 return -ENOMEM;
755
756         for (i = 0; i < 2; i++) {
757                 ret = be_cmd_ddr_dma_test(adapter, pattern[i],
758                                         4096, &ddrdma_cmd);
759                 if (ret != 0)
760                         goto err;
761         }
762
763 err:
764         dma_free_coherent(&adapter->pdev->dev, ddrdma_cmd.size, ddrdma_cmd.va,
765                           ddrdma_cmd.dma);
766         return ret;
767 }
768
769 static u64 be_loopback_test(struct be_adapter *adapter, u8 loopback_type,
770                                 u64 *status)
771 {
772         be_cmd_set_loopback(adapter, adapter->hba_port_num,
773                                 loopback_type, 1);
774         *status = be_cmd_loopback_test(adapter, adapter->hba_port_num,
775                                 loopback_type, 1500,
776                                 2, 0xabc);
777         be_cmd_set_loopback(adapter, adapter->hba_port_num,
778                                 BE_NO_LOOPBACK, 1);
779         return *status;
780 }
781
782 static void
783 be_self_test(struct net_device *netdev, struct ethtool_test *test, u64 *data)
784 {
785         struct be_adapter *adapter = netdev_priv(netdev);
786         int status;
787         u8 link_status = 0;
788
789         if (adapter->function_caps & BE_FUNCTION_CAPS_SUPER_NIC) {
790                 dev_err(&adapter->pdev->dev, "Self test not supported\n");
791                 test->flags |= ETH_TEST_FL_FAILED;
792                 return;
793         }
794
795         memset(data, 0, sizeof(u64) * ETHTOOL_TESTS_NUM);
796
797         if (test->flags & ETH_TEST_FL_OFFLINE) {
798                 if (be_loopback_test(adapter, BE_MAC_LOOPBACK,
799                                                 &data[0]) != 0) {
800                         test->flags |= ETH_TEST_FL_FAILED;
801                 }
802                 if (be_loopback_test(adapter, BE_PHY_LOOPBACK,
803                                                 &data[1]) != 0) {
804                         test->flags |= ETH_TEST_FL_FAILED;
805                 }
806                 if (be_loopback_test(adapter, BE_ONE_PORT_EXT_LOOPBACK,
807                                                 &data[2]) != 0) {
808                         test->flags |= ETH_TEST_FL_FAILED;
809                 }
810         }
811
812         if (!lancer_chip(adapter) && be_test_ddr_dma(adapter) != 0) {
813                 data[3] = 1;
814                 test->flags |= ETH_TEST_FL_FAILED;
815         }
816
817         status = be_cmd_link_status_query(adapter, NULL, &link_status, 0);
818         if (status) {
819                 test->flags |= ETH_TEST_FL_FAILED;
820                 data[4] = -1;
821         } else if (!link_status) {
822                 test->flags |= ETH_TEST_FL_FAILED;
823                 data[4] = 1;
824         }
825 }
826
827 static int
828 be_do_flash(struct net_device *netdev, struct ethtool_flash *efl)
829 {
830         struct be_adapter *adapter = netdev_priv(netdev);
831
832         return be_load_fw(adapter, efl->data);
833 }
834
835 static int
836 be_get_eeprom_len(struct net_device *netdev)
837 {
838         struct be_adapter *adapter = netdev_priv(netdev);
839
840         if (!check_privilege(adapter, MAX_PRIVILEGES))
841                 return 0;
842
843         if (lancer_chip(adapter)) {
844                 if (be_physfn(adapter))
845                         return lancer_cmd_get_file_len(adapter,
846                                         LANCER_VPD_PF_FILE);
847                 else
848                         return lancer_cmd_get_file_len(adapter,
849                                         LANCER_VPD_VF_FILE);
850         } else {
851                 return BE_READ_SEEPROM_LEN;
852         }
853 }
854
855 static int
856 be_read_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
857                         uint8_t *data)
858 {
859         struct be_adapter *adapter = netdev_priv(netdev);
860         struct be_dma_mem eeprom_cmd;
861         struct be_cmd_resp_seeprom_read *resp;
862         int status;
863
864         if (!eeprom->len)
865                 return -EINVAL;
866
867         if (lancer_chip(adapter)) {
868                 if (be_physfn(adapter))
869                         return lancer_cmd_read_file(adapter, LANCER_VPD_PF_FILE,
870                                         eeprom->len, data);
871                 else
872                         return lancer_cmd_read_file(adapter, LANCER_VPD_VF_FILE,
873                                         eeprom->len, data);
874         }
875
876         eeprom->magic = BE_VENDOR_ID | (adapter->pdev->device<<16);
877
878         memset(&eeprom_cmd, 0, sizeof(struct be_dma_mem));
879         eeprom_cmd.size = sizeof(struct be_cmd_req_seeprom_read);
880         eeprom_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, eeprom_cmd.size,
881                                            &eeprom_cmd.dma, GFP_KERNEL);
882
883         if (!eeprom_cmd.va)
884                 return -ENOMEM;
885
886         status = be_cmd_get_seeprom_data(adapter, &eeprom_cmd);
887
888         if (!status) {
889                 resp = eeprom_cmd.va;
890                 memcpy(data, resp->seeprom_data + eeprom->offset, eeprom->len);
891         }
892         dma_free_coherent(&adapter->pdev->dev, eeprom_cmd.size, eeprom_cmd.va,
893                           eeprom_cmd.dma);
894
895         return status;
896 }
897
898 static u32 be_get_msg_level(struct net_device *netdev)
899 {
900         struct be_adapter *adapter = netdev_priv(netdev);
901
902         if (lancer_chip(adapter)) {
903                 dev_err(&adapter->pdev->dev, "Operation not supported\n");
904                 return -EOPNOTSUPP;
905         }
906
907         return adapter->msg_enable;
908 }
909
910 static void be_set_fw_log_level(struct be_adapter *adapter, u32 level)
911 {
912         struct be_dma_mem extfat_cmd;
913         struct be_fat_conf_params *cfgs;
914         int status;
915         int i, j;
916
917         memset(&extfat_cmd, 0, sizeof(struct be_dma_mem));
918         extfat_cmd.size = sizeof(struct be_cmd_resp_get_ext_fat_caps);
919         extfat_cmd.va = pci_alloc_consistent(adapter->pdev, extfat_cmd.size,
920                                              &extfat_cmd.dma);
921         if (!extfat_cmd.va) {
922                 dev_err(&adapter->pdev->dev, "%s: Memory allocation failure\n",
923                         __func__);
924                 goto err;
925         }
926         status = be_cmd_get_ext_fat_capabilites(adapter, &extfat_cmd);
927         if (!status) {
928                 cfgs = (struct be_fat_conf_params *)(extfat_cmd.va +
929                                         sizeof(struct be_cmd_resp_hdr));
930                 for (i = 0; i < le32_to_cpu(cfgs->num_modules); i++) {
931                         u32 num_modes = le32_to_cpu(cfgs->module[i].num_modes);
932                         for (j = 0; j < num_modes; j++) {
933                                 if (cfgs->module[i].trace_lvl[j].mode ==
934                                                                 MODE_UART)
935                                         cfgs->module[i].trace_lvl[j].dbg_lvl =
936                                                         cpu_to_le32(level);
937                         }
938                 }
939                 status = be_cmd_set_ext_fat_capabilites(adapter, &extfat_cmd,
940                                                         cfgs);
941                 if (status)
942                         dev_err(&adapter->pdev->dev,
943                                 "Message level set failed\n");
944         } else {
945                 dev_err(&adapter->pdev->dev, "Message level get failed\n");
946         }
947
948         pci_free_consistent(adapter->pdev, extfat_cmd.size, extfat_cmd.va,
949                             extfat_cmd.dma);
950 err:
951         return;
952 }
953
954 static void be_set_msg_level(struct net_device *netdev, u32 level)
955 {
956         struct be_adapter *adapter = netdev_priv(netdev);
957
958         if (lancer_chip(adapter)) {
959                 dev_err(&adapter->pdev->dev, "Operation not supported\n");
960                 return;
961         }
962
963         if (adapter->msg_enable == level)
964                 return;
965
966         if ((level & NETIF_MSG_HW) != (adapter->msg_enable & NETIF_MSG_HW))
967                 be_set_fw_log_level(adapter, level & NETIF_MSG_HW ?
968                                     FW_LOG_LEVEL_DEFAULT : FW_LOG_LEVEL_FATAL);
969         adapter->msg_enable = level;
970
971         return;
972 }
973
974 static u64 be_get_rss_hash_opts(struct be_adapter *adapter, u64 flow_type)
975 {
976         u64 data = 0;
977
978         switch (flow_type) {
979         case TCP_V4_FLOW:
980                 if (adapter->rss_flags & RSS_ENABLE_IPV4)
981                         data |= RXH_IP_DST | RXH_IP_SRC;
982                 if (adapter->rss_flags & RSS_ENABLE_TCP_IPV4)
983                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
984                 break;
985         case UDP_V4_FLOW:
986                 if (adapter->rss_flags & RSS_ENABLE_IPV4)
987                         data |= RXH_IP_DST | RXH_IP_SRC;
988                 if (adapter->rss_flags & RSS_ENABLE_UDP_IPV4)
989                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
990                 break;
991         case TCP_V6_FLOW:
992                 if (adapter->rss_flags & RSS_ENABLE_IPV6)
993                         data |= RXH_IP_DST | RXH_IP_SRC;
994                 if (adapter->rss_flags & RSS_ENABLE_TCP_IPV6)
995                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
996                 break;
997         case UDP_V6_FLOW:
998                 if (adapter->rss_flags & RSS_ENABLE_IPV6)
999                         data |= RXH_IP_DST | RXH_IP_SRC;
1000                 if (adapter->rss_flags & RSS_ENABLE_UDP_IPV6)
1001                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1002                 break;
1003         }
1004
1005         return data;
1006 }
1007
1008 static int be_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1009                       u32 *rule_locs)
1010 {
1011         struct be_adapter *adapter = netdev_priv(netdev);
1012
1013         if (!be_multi_rxq(adapter)) {
1014                 dev_info(&adapter->pdev->dev,
1015                          "ethtool::get_rxnfc: RX flow hashing is disabled\n");
1016                 return -EINVAL;
1017         }
1018
1019         switch (cmd->cmd) {
1020         case ETHTOOL_GRXFH:
1021                 cmd->data = be_get_rss_hash_opts(adapter, cmd->flow_type);
1022                 break;
1023         case ETHTOOL_GRXRINGS:
1024                 cmd->data = adapter->num_rx_qs - 1;
1025                 break;
1026         default:
1027                 return -EINVAL;
1028         }
1029
1030         return 0;
1031 }
1032
1033 static int be_set_rss_hash_opts(struct be_adapter *adapter,
1034                                 struct ethtool_rxnfc *cmd)
1035 {
1036         struct be_rx_obj *rxo;
1037         int status = 0, i, j;
1038         u8 rsstable[128];
1039         u32 rss_flags = adapter->rss_flags;
1040
1041         if (cmd->data != L3_RSS_FLAGS &&
1042             cmd->data != (L3_RSS_FLAGS | L4_RSS_FLAGS))
1043                 return -EINVAL;
1044
1045         switch (cmd->flow_type) {
1046         case TCP_V4_FLOW:
1047                 if (cmd->data == L3_RSS_FLAGS)
1048                         rss_flags &= ~RSS_ENABLE_TCP_IPV4;
1049                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1050                         rss_flags |= RSS_ENABLE_IPV4 |
1051                                         RSS_ENABLE_TCP_IPV4;
1052                 break;
1053         case TCP_V6_FLOW:
1054                 if (cmd->data == L3_RSS_FLAGS)
1055                         rss_flags &= ~RSS_ENABLE_TCP_IPV6;
1056                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1057                         rss_flags |= RSS_ENABLE_IPV6 |
1058                                         RSS_ENABLE_TCP_IPV6;
1059                 break;
1060         case UDP_V4_FLOW:
1061                 if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) &&
1062                     BEx_chip(adapter))
1063                         return -EINVAL;
1064
1065                 if (cmd->data == L3_RSS_FLAGS)
1066                         rss_flags &= ~RSS_ENABLE_UDP_IPV4;
1067                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1068                         rss_flags |= RSS_ENABLE_IPV4 |
1069                                         RSS_ENABLE_UDP_IPV4;
1070                 break;
1071         case UDP_V6_FLOW:
1072                 if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) &&
1073                     BEx_chip(adapter))
1074                         return -EINVAL;
1075
1076                 if (cmd->data == L3_RSS_FLAGS)
1077                         rss_flags &= ~RSS_ENABLE_UDP_IPV6;
1078                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1079                         rss_flags |= RSS_ENABLE_IPV6 |
1080                                         RSS_ENABLE_UDP_IPV6;
1081                 break;
1082         default:
1083                 return -EINVAL;
1084         }
1085
1086         if (rss_flags == adapter->rss_flags)
1087                 return status;
1088
1089         if (be_multi_rxq(adapter)) {
1090                 for (j = 0; j < 128; j += adapter->num_rx_qs - 1) {
1091                         for_all_rss_queues(adapter, rxo, i) {
1092                                 if ((j + i) >= 128)
1093                                         break;
1094                                 rsstable[j + i] = rxo->rss_id;
1095                         }
1096                 }
1097         }
1098         status = be_cmd_rss_config(adapter, rsstable, rss_flags, 128);
1099         if (!status)
1100                 adapter->rss_flags = rss_flags;
1101
1102         return status;
1103 }
1104
1105 static int be_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1106 {
1107         struct be_adapter *adapter = netdev_priv(netdev);
1108         int status = 0;
1109
1110         if (!be_multi_rxq(adapter)) {
1111                 dev_err(&adapter->pdev->dev,
1112                         "ethtool::set_rxnfc: RX flow hashing is disabled\n");
1113                 return -EINVAL;
1114         }
1115
1116         switch (cmd->cmd) {
1117         case ETHTOOL_SRXFH:
1118                 status = be_set_rss_hash_opts(adapter, cmd);
1119                 break;
1120         default:
1121                 return -EINVAL;
1122         }
1123
1124         return status;
1125 }
1126
1127 static void be_get_channels(struct net_device *netdev,
1128                             struct ethtool_channels *ch)
1129 {
1130         struct be_adapter *adapter = netdev_priv(netdev);
1131
1132         ch->combined_count = adapter->num_evt_qs;
1133         ch->max_combined = be_max_qs(adapter);
1134 }
1135
1136 static int be_set_channels(struct net_device  *netdev,
1137                            struct ethtool_channels *ch)
1138 {
1139         struct be_adapter *adapter = netdev_priv(netdev);
1140
1141         if (ch->rx_count || ch->tx_count || ch->other_count ||
1142             !ch->combined_count || ch->combined_count > be_max_qs(adapter))
1143                 return -EINVAL;
1144
1145         adapter->cfg_num_qs = ch->combined_count;
1146
1147         return be_update_queues(adapter);
1148 }
1149
1150 const struct ethtool_ops be_ethtool_ops = {
1151         .get_settings = be_get_settings,
1152         .get_drvinfo = be_get_drvinfo,
1153         .get_wol = be_get_wol,
1154         .set_wol = be_set_wol,
1155         .get_link = ethtool_op_get_link,
1156         .get_eeprom_len = be_get_eeprom_len,
1157         .get_eeprom = be_read_eeprom,
1158         .get_coalesce = be_get_coalesce,
1159         .set_coalesce = be_set_coalesce,
1160         .get_ringparam = be_get_ringparam,
1161         .get_pauseparam = be_get_pauseparam,
1162         .set_pauseparam = be_set_pauseparam,
1163         .get_strings = be_get_stat_strings,
1164         .set_phys_id = be_set_phys_id,
1165         .set_dump = be_set_dump,
1166         .get_msglevel = be_get_msg_level,
1167         .set_msglevel = be_set_msg_level,
1168         .get_sset_count = be_get_sset_count,
1169         .get_ethtool_stats = be_get_ethtool_stats,
1170         .get_regs_len = be_get_reg_len,
1171         .get_regs = be_get_regs,
1172         .flash_device = be_do_flash,
1173         .self_test = be_self_test,
1174         .get_rxnfc = be_get_rxnfc,
1175         .set_rxnfc = be_set_rxnfc,
1176         .get_channels = be_get_channels,
1177         .set_channels = be_set_channels
1178 };