GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / net / ethernet / emulex / benet / be_ethtool.c
1 /*
2  * Copyright (C) 2005 - 2016 Broadcom
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         /* Received packets dropped when the RX block runs out of space in
82          * one of its input FIFOs. This could happen due a long burst of
83          * minimum-sized (64b) frames in the receive path.
84          * This counter may also be erroneously incremented rarely.
85          */
86         {DRVSTAT_INFO(rx_input_fifo_overflow_drop)},
87         {DRVSTAT_INFO(rx_ip_checksum_errs)},
88         {DRVSTAT_INFO(rx_tcp_checksum_errs)},
89         {DRVSTAT_INFO(rx_udp_checksum_errs)},
90         {DRVSTAT_INFO(tx_pauseframes)},
91         {DRVSTAT_INFO(tx_controlframes)},
92         {DRVSTAT_INFO(rx_priority_pause_frames)},
93         {DRVSTAT_INFO(tx_priority_pauseframes)},
94         /* Received packets dropped when an internal fifo going into
95          * main packet buffer tank (PMEM) overflows.
96          */
97         {DRVSTAT_INFO(pmem_fifo_overflow_drop)},
98         {DRVSTAT_INFO(jabber_events)},
99         /* Received packets dropped due to lack of available HW packet buffers
100          * used to temporarily hold the received packets.
101          */
102         {DRVSTAT_INFO(rx_drops_no_pbuf)},
103         /* Received packets dropped due to input receive buffer
104          * descriptor fifo overflowing.
105          */
106         {DRVSTAT_INFO(rx_drops_no_erx_descr)},
107         /* Packets dropped because the internal FIFO to the offloaded TCP
108          * receive processing block is full. This could happen only for
109          * offloaded iSCSI or FCoE trarffic.
110          */
111         {DRVSTAT_INFO(rx_drops_no_tpre_descr)},
112         /* Received packets dropped when they need more than 8
113          * receive buffers. This cannot happen as the driver configures
114          * 2048 byte receive buffers.
115          */
116         {DRVSTAT_INFO(rx_drops_too_many_frags)},
117         {DRVSTAT_INFO(forwarded_packets)},
118         /* Received packets dropped when the frame length
119          * is more than 9018 bytes
120          */
121         {DRVSTAT_INFO(rx_drops_mtu)},
122         /* Number of dma mapping errors */
123         {DRVSTAT_INFO(dma_map_errors)},
124         /* Number of packets dropped due to random early drop function */
125         {DRVSTAT_INFO(eth_red_drops)},
126         {DRVSTAT_INFO(rx_roce_bytes_lsd)},
127         {DRVSTAT_INFO(rx_roce_bytes_msd)},
128         {DRVSTAT_INFO(rx_roce_frames)},
129         {DRVSTAT_INFO(roce_drops_payload_len)},
130         {DRVSTAT_INFO(roce_drops_crc)}
131 };
132
133 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats)
134
135 /* Stats related to multi RX queues: get_stats routine assumes bytes, pkts
136  * are first and second members respectively.
137  */
138 static const struct be_ethtool_stat et_rx_stats[] = {
139         {DRVSTAT_RX_INFO(rx_bytes)},/* If moving this member see above note */
140         {DRVSTAT_RX_INFO(rx_pkts)}, /* If moving this member see above note */
141         {DRVSTAT_RX_INFO(rx_vxlan_offload_pkts)},
142         {DRVSTAT_RX_INFO(rx_compl)},
143         {DRVSTAT_RX_INFO(rx_compl_err)},
144         {DRVSTAT_RX_INFO(rx_mcast_pkts)},
145         /* Number of page allocation failures while posting receive buffers
146          * to HW.
147          */
148         {DRVSTAT_RX_INFO(rx_post_fail)},
149         /* Recevied packets dropped due to skb allocation failure */
150         {DRVSTAT_RX_INFO(rx_drops_no_skbs)},
151         /* Received packets dropped due to lack of available fetched buffers
152          * posted by the driver.
153          */
154         {DRVSTAT_RX_INFO(rx_drops_no_frags)}
155 };
156
157 #define ETHTOOL_RXSTATS_NUM (ARRAY_SIZE(et_rx_stats))
158
159 /* Stats related to multi TX queues: get_stats routine assumes compl is the
160  * first member
161  */
162 static const struct be_ethtool_stat et_tx_stats[] = {
163         {DRVSTAT_TX_INFO(tx_compl)}, /* If moving this member see above note */
164         /* This counter is incremented when the HW encounters an error while
165          * parsing the packet header of an outgoing TX request. This counter is
166          * applicable only for BE2, BE3 and Skyhawk based adapters.
167          */
168         {DRVSTAT_TX_INFO(tx_hdr_parse_err)},
169         /* This counter is incremented when an error occurs in the DMA
170          * operation associated with the TX request from the host to the device.
171          */
172         {DRVSTAT_TX_INFO(tx_dma_err)},
173         /* This counter is incremented when MAC or VLAN spoof checking is
174          * enabled on the interface and the TX request fails the spoof check
175          * in HW.
176          */
177         {DRVSTAT_TX_INFO(tx_spoof_check_err)},
178         /* This counter is incremented when the HW encounters an error while
179          * performing TSO offload. This counter is applicable only for Lancer
180          * adapters.
181          */
182         {DRVSTAT_TX_INFO(tx_tso_err)},
183         /* This counter is incremented when the HW detects Q-in-Q style VLAN
184          * tagging in a packet and such tagging is not expected on the outgoing
185          * interface. This counter is applicable only for Lancer adapters.
186          */
187         {DRVSTAT_TX_INFO(tx_qinq_err)},
188         /* This counter is incremented when the HW detects parity errors in the
189          * packet data. This counter is applicable only for Lancer adapters.
190          */
191         {DRVSTAT_TX_INFO(tx_internal_parity_err)},
192         {DRVSTAT_TX_INFO(tx_bytes)},
193         {DRVSTAT_TX_INFO(tx_pkts)},
194         {DRVSTAT_TX_INFO(tx_vxlan_offload_pkts)},
195         /* Number of skbs queued for trasmission by the driver */
196         {DRVSTAT_TX_INFO(tx_reqs)},
197         /* Number of times the TX queue was stopped due to lack
198          * of spaces in the TXQ.
199          */
200         {DRVSTAT_TX_INFO(tx_stops)},
201         /* Pkts dropped in the driver's transmit path */
202         {DRVSTAT_TX_INFO(tx_drv_drops)}
203 };
204
205 #define ETHTOOL_TXSTATS_NUM (ARRAY_SIZE(et_tx_stats))
206
207 static const char et_self_tests[][ETH_GSTRING_LEN] = {
208         "MAC Loopback test",
209         "PHY Loopback test",
210         "External Loopback test",
211         "DDR DMA test",
212         "Link test"
213 };
214
215 #define ETHTOOL_TESTS_NUM ARRAY_SIZE(et_self_tests)
216 #define BE_MAC_LOOPBACK 0x0
217 #define BE_PHY_LOOPBACK 0x1
218 #define BE_ONE_PORT_EXT_LOOPBACK 0x2
219 #define BE_NO_LOOPBACK 0xff
220
221 static void be_get_drvinfo(struct net_device *netdev,
222                            struct ethtool_drvinfo *drvinfo)
223 {
224         struct be_adapter *adapter = netdev_priv(netdev);
225
226         strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
227         strlcpy(drvinfo->version, DRV_VER, sizeof(drvinfo->version));
228         if (!memcmp(adapter->fw_ver, adapter->fw_on_flash, FW_VER_LEN))
229                 strlcpy(drvinfo->fw_version, adapter->fw_ver,
230                         sizeof(drvinfo->fw_version));
231         else
232                 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
233                          "%s [%s]", adapter->fw_ver, adapter->fw_on_flash);
234
235         strlcpy(drvinfo->bus_info, pci_name(adapter->pdev),
236                 sizeof(drvinfo->bus_info));
237 }
238
239 static u32 lancer_cmd_get_file_len(struct be_adapter *adapter, u8 *file_name)
240 {
241         u32 data_read = 0, eof;
242         u8 addn_status;
243         struct be_dma_mem data_len_cmd;
244
245         memset(&data_len_cmd, 0, sizeof(data_len_cmd));
246         /* data_offset and data_size should be 0 to get reg len */
247         lancer_cmd_read_object(adapter, &data_len_cmd, 0, 0, file_name,
248                                &data_read, &eof, &addn_status);
249
250         return data_read;
251 }
252
253 static int be_get_dump_len(struct be_adapter *adapter)
254 {
255         u32 dump_size = 0;
256
257         if (lancer_chip(adapter))
258                 dump_size = lancer_cmd_get_file_len(adapter,
259                                                     LANCER_FW_DUMP_FILE);
260         else
261                 dump_size = adapter->fat_dump_len;
262
263         return dump_size;
264 }
265
266 static int lancer_cmd_read_file(struct be_adapter *adapter, u8 *file_name,
267                                 u32 buf_len, void *buf)
268 {
269         struct be_dma_mem read_cmd;
270         u32 read_len = 0, total_read_len = 0, chunk_size;
271         u32 eof = 0;
272         u8 addn_status;
273         int status = 0;
274
275         read_cmd.size = LANCER_READ_FILE_CHUNK;
276         read_cmd.va = dma_zalloc_coherent(&adapter->pdev->dev, read_cmd.size,
277                                           &read_cmd.dma, GFP_ATOMIC);
278
279         if (!read_cmd.va) {
280                 dev_err(&adapter->pdev->dev,
281                         "Memory allocation failure while reading dump\n");
282                 return -ENOMEM;
283         }
284
285         while ((total_read_len < buf_len) && !eof) {
286                 chunk_size = min_t(u32, (buf_len - total_read_len),
287                                    LANCER_READ_FILE_CHUNK);
288                 chunk_size = ALIGN(chunk_size, 4);
289                 status = lancer_cmd_read_object(adapter, &read_cmd, chunk_size,
290                                                 total_read_len, file_name,
291                                                 &read_len, &eof, &addn_status);
292                 if (!status) {
293                         memcpy(buf + total_read_len, read_cmd.va, read_len);
294                         total_read_len += read_len;
295                         eof &= LANCER_READ_FILE_EOF_MASK;
296                 } else {
297                         status = -EIO;
298                         break;
299                 }
300         }
301         dma_free_coherent(&adapter->pdev->dev, read_cmd.size, read_cmd.va,
302                           read_cmd.dma);
303
304         return status;
305 }
306
307 static int be_read_dump_data(struct be_adapter *adapter, u32 dump_len,
308                              void *buf)
309 {
310         int status = 0;
311
312         if (lancer_chip(adapter))
313                 status = lancer_cmd_read_file(adapter, LANCER_FW_DUMP_FILE,
314                                               dump_len, buf);
315         else
316                 status = be_cmd_get_fat_dump(adapter, dump_len, buf);
317
318         return status;
319 }
320
321 static int be_get_coalesce(struct net_device *netdev,
322                            struct ethtool_coalesce *et)
323 {
324         struct be_adapter *adapter = netdev_priv(netdev);
325         struct be_aic_obj *aic = &adapter->aic_obj[0];
326
327         et->rx_coalesce_usecs = aic->prev_eqd;
328         et->rx_coalesce_usecs_high = aic->max_eqd;
329         et->rx_coalesce_usecs_low = aic->min_eqd;
330
331         et->tx_coalesce_usecs = aic->prev_eqd;
332         et->tx_coalesce_usecs_high = aic->max_eqd;
333         et->tx_coalesce_usecs_low = aic->min_eqd;
334
335         et->use_adaptive_rx_coalesce = aic->enable;
336         et->use_adaptive_tx_coalesce = aic->enable;
337
338         return 0;
339 }
340
341 /* TX attributes are ignored. Only RX attributes are considered
342  * eqd cmd is issued in the worker thread.
343  */
344 static int be_set_coalesce(struct net_device *netdev,
345                            struct ethtool_coalesce *et)
346 {
347         struct be_adapter *adapter = netdev_priv(netdev);
348         struct be_aic_obj *aic = &adapter->aic_obj[0];
349         struct be_eq_obj *eqo;
350         int i;
351
352         for_all_evt_queues(adapter, eqo, i) {
353                 aic->enable = et->use_adaptive_rx_coalesce;
354                 aic->max_eqd = min(et->rx_coalesce_usecs_high, BE_MAX_EQD);
355                 aic->min_eqd = min(et->rx_coalesce_usecs_low, aic->max_eqd);
356                 aic->et_eqd = min(et->rx_coalesce_usecs, aic->max_eqd);
357                 aic->et_eqd = max(aic->et_eqd, aic->min_eqd);
358                 aic++;
359         }
360
361         /* For Skyhawk, the EQD setting happens via EQ_DB when AIC is enabled.
362          * When AIC is disabled, persistently force set EQD value via the
363          * FW cmd, so that we don't have to calculate the delay multiplier
364          * encode value each time EQ_DB is rung
365          */
366         if (!et->use_adaptive_rx_coalesce && skyhawk_chip(adapter))
367                 be_eqd_update(adapter, true);
368
369         return 0;
370 }
371
372 static void be_get_ethtool_stats(struct net_device *netdev,
373                                  struct ethtool_stats *stats, uint64_t *data)
374 {
375         struct be_adapter *adapter = netdev_priv(netdev);
376         struct be_rx_obj *rxo;
377         struct be_tx_obj *txo;
378         void *p;
379         unsigned int i, j, base = 0, start;
380
381         for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
382                 p = (u8 *)&adapter->drv_stats + et_stats[i].offset;
383                 data[i] = *(u32 *)p;
384         }
385         base += ETHTOOL_STATS_NUM;
386
387         for_all_rx_queues(adapter, rxo, j) {
388                 struct be_rx_stats *stats = rx_stats(rxo);
389
390                 do {
391                         start = u64_stats_fetch_begin_irq(&stats->sync);
392                         data[base] = stats->rx_bytes;
393                         data[base + 1] = stats->rx_pkts;
394                 } while (u64_stats_fetch_retry_irq(&stats->sync, start));
395
396                 for (i = 2; i < ETHTOOL_RXSTATS_NUM; i++) {
397                         p = (u8 *)stats + et_rx_stats[i].offset;
398                         data[base + i] = *(u32 *)p;
399                 }
400                 base += ETHTOOL_RXSTATS_NUM;
401         }
402
403         for_all_tx_queues(adapter, txo, j) {
404                 struct be_tx_stats *stats = tx_stats(txo);
405
406                 do {
407                         start = u64_stats_fetch_begin_irq(&stats->sync_compl);
408                         data[base] = stats->tx_compl;
409                 } while (u64_stats_fetch_retry_irq(&stats->sync_compl, start));
410
411                 do {
412                         start = u64_stats_fetch_begin_irq(&stats->sync);
413                         for (i = 1; i < ETHTOOL_TXSTATS_NUM; i++) {
414                                 p = (u8 *)stats + et_tx_stats[i].offset;
415                                 data[base + i] =
416                                         (et_tx_stats[i].size == sizeof(u64)) ?
417                                                 *(u64 *)p : *(u32 *)p;
418                         }
419                 } while (u64_stats_fetch_retry_irq(&stats->sync, start));
420                 base += ETHTOOL_TXSTATS_NUM;
421         }
422 }
423
424 static const char be_priv_flags[][ETH_GSTRING_LEN] = {
425         "disable-tpe-recovery"
426 };
427
428 static void be_get_stat_strings(struct net_device *netdev, uint32_t stringset,
429                                 uint8_t *data)
430 {
431         struct be_adapter *adapter = netdev_priv(netdev);
432         int i, j;
433
434         switch (stringset) {
435         case ETH_SS_STATS:
436                 for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
437                         memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN);
438                         data += ETH_GSTRING_LEN;
439                 }
440                 for (i = 0; i < adapter->num_rx_qs; i++) {
441                         for (j = 0; j < ETHTOOL_RXSTATS_NUM; j++) {
442                                 sprintf(data, "rxq%d: %s", i,
443                                         et_rx_stats[j].desc);
444                                 data += ETH_GSTRING_LEN;
445                         }
446                 }
447                 for (i = 0; i < adapter->num_tx_qs; i++) {
448                         for (j = 0; j < ETHTOOL_TXSTATS_NUM; j++) {
449                                 sprintf(data, "txq%d: %s", i,
450                                         et_tx_stats[j].desc);
451                                 data += ETH_GSTRING_LEN;
452                         }
453                 }
454                 break;
455         case ETH_SS_TEST:
456                 for (i = 0; i < ETHTOOL_TESTS_NUM; i++) {
457                         memcpy(data, et_self_tests[i], ETH_GSTRING_LEN);
458                         data += ETH_GSTRING_LEN;
459                 }
460                 break;
461         case ETH_SS_PRIV_FLAGS:
462                 for (i = 0; i < ARRAY_SIZE(be_priv_flags); i++)
463                         strcpy(data + i * ETH_GSTRING_LEN, be_priv_flags[i]);
464                 break;
465         }
466 }
467
468 static int be_get_sset_count(struct net_device *netdev, int stringset)
469 {
470         struct be_adapter *adapter = netdev_priv(netdev);
471
472         switch (stringset) {
473         case ETH_SS_TEST:
474                 return ETHTOOL_TESTS_NUM;
475         case ETH_SS_STATS:
476                 return ETHTOOL_STATS_NUM +
477                         adapter->num_rx_qs * ETHTOOL_RXSTATS_NUM +
478                         adapter->num_tx_qs * ETHTOOL_TXSTATS_NUM;
479         case ETH_SS_PRIV_FLAGS:
480                 return ARRAY_SIZE(be_priv_flags);
481         default:
482                 return -EINVAL;
483         }
484 }
485
486 static u32 be_get_port_type(struct be_adapter *adapter)
487 {
488         u32 port;
489
490         switch (adapter->phy.interface_type) {
491         case PHY_TYPE_BASET_1GB:
492         case PHY_TYPE_BASEX_1GB:
493         case PHY_TYPE_SGMII:
494                 port = PORT_TP;
495                 break;
496         case PHY_TYPE_SFP_PLUS_10GB:
497                 if (adapter->phy.cable_type & SFP_PLUS_COPPER_CABLE)
498                         port = PORT_DA;
499                 else
500                         port = PORT_FIBRE;
501                 break;
502         case PHY_TYPE_QSFP:
503                 if (adapter->phy.cable_type & QSFP_PLUS_CR4_CABLE)
504                         port = PORT_DA;
505                 else
506                         port = PORT_FIBRE;
507                 break;
508         case PHY_TYPE_XFP_10GB:
509         case PHY_TYPE_SFP_1GB:
510                 port = PORT_FIBRE;
511                 break;
512         case PHY_TYPE_BASET_10GB:
513                 port = PORT_TP;
514                 break;
515         default:
516                 port = PORT_OTHER;
517         }
518
519         return port;
520 }
521
522 static u32 convert_to_et_setting(struct be_adapter *adapter, u32 if_speeds)
523 {
524         u32 val = 0;
525
526         switch (adapter->phy.interface_type) {
527         case PHY_TYPE_BASET_1GB:
528         case PHY_TYPE_BASEX_1GB:
529         case PHY_TYPE_SGMII:
530                 val |= SUPPORTED_TP;
531                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
532                         val |= SUPPORTED_1000baseT_Full;
533                 if (if_speeds & BE_SUPPORTED_SPEED_100MBPS)
534                         val |= SUPPORTED_100baseT_Full;
535                 if (if_speeds & BE_SUPPORTED_SPEED_10MBPS)
536                         val |= SUPPORTED_10baseT_Full;
537                 break;
538         case PHY_TYPE_KX4_10GB:
539                 val |= SUPPORTED_Backplane;
540                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
541                         val |= SUPPORTED_1000baseKX_Full;
542                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
543                         val |= SUPPORTED_10000baseKX4_Full;
544                 break;
545         case PHY_TYPE_KR2_20GB:
546                 val |= SUPPORTED_Backplane;
547                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
548                         val |= SUPPORTED_10000baseKR_Full;
549                 if (if_speeds & BE_SUPPORTED_SPEED_20GBPS)
550                         val |= SUPPORTED_20000baseKR2_Full;
551                 break;
552         case PHY_TYPE_KR_10GB:
553                 val |= SUPPORTED_Backplane |
554                                 SUPPORTED_10000baseKR_Full;
555                 break;
556         case PHY_TYPE_KR4_40GB:
557                 val |= SUPPORTED_Backplane;
558                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
559                         val |= SUPPORTED_10000baseKR_Full;
560                 if (if_speeds & BE_SUPPORTED_SPEED_40GBPS)
561                         val |= SUPPORTED_40000baseKR4_Full;
562                 break;
563         case PHY_TYPE_QSFP:
564                 if (if_speeds & BE_SUPPORTED_SPEED_40GBPS) {
565                         switch (adapter->phy.cable_type) {
566                         case QSFP_PLUS_CR4_CABLE:
567                                 val |= SUPPORTED_40000baseCR4_Full;
568                                 break;
569                         case QSFP_PLUS_LR4_CABLE:
570                                 val |= SUPPORTED_40000baseLR4_Full;
571                                 break;
572                         default:
573                                 val |= SUPPORTED_40000baseSR4_Full;
574                                 break;
575                         }
576                 }
577         case PHY_TYPE_SFP_PLUS_10GB:
578         case PHY_TYPE_XFP_10GB:
579         case PHY_TYPE_SFP_1GB:
580                 val |= SUPPORTED_FIBRE;
581                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
582                         val |= SUPPORTED_10000baseT_Full;
583                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
584                         val |= SUPPORTED_1000baseT_Full;
585                 break;
586         case PHY_TYPE_BASET_10GB:
587                 val |= SUPPORTED_TP;
588                 if (if_speeds & BE_SUPPORTED_SPEED_10GBPS)
589                         val |= SUPPORTED_10000baseT_Full;
590                 if (if_speeds & BE_SUPPORTED_SPEED_1GBPS)
591                         val |= SUPPORTED_1000baseT_Full;
592                 if (if_speeds & BE_SUPPORTED_SPEED_100MBPS)
593                         val |= SUPPORTED_100baseT_Full;
594                 break;
595         default:
596                 val |= SUPPORTED_TP;
597         }
598
599         return val;
600 }
601
602 bool be_pause_supported(struct be_adapter *adapter)
603 {
604         return (adapter->phy.interface_type == PHY_TYPE_SFP_PLUS_10GB ||
605                 adapter->phy.interface_type == PHY_TYPE_XFP_10GB) ?
606                 false : true;
607 }
608
609 static int be_get_link_ksettings(struct net_device *netdev,
610                                  struct ethtool_link_ksettings *cmd)
611 {
612         struct be_adapter *adapter = netdev_priv(netdev);
613         u8 link_status;
614         u16 link_speed = 0;
615         int status;
616         u32 auto_speeds;
617         u32 fixed_speeds;
618         u32 supported = 0, advertising = 0;
619
620         if (adapter->phy.link_speed < 0) {
621                 status = be_cmd_link_status_query(adapter, &link_speed,
622                                                   &link_status, 0);
623                 if (!status)
624                         be_link_status_update(adapter, link_status);
625                 cmd->base.speed = link_speed;
626
627                 status = be_cmd_get_phy_info(adapter);
628                 if (!status) {
629                         auto_speeds = adapter->phy.auto_speeds_supported;
630                         fixed_speeds = adapter->phy.fixed_speeds_supported;
631
632                         be_cmd_query_cable_type(adapter);
633
634                         supported =
635                                 convert_to_et_setting(adapter,
636                                                       auto_speeds |
637                                                       fixed_speeds);
638                         advertising =
639                                 convert_to_et_setting(adapter, auto_speeds);
640
641                         cmd->base.port = be_get_port_type(adapter);
642
643                         if (adapter->phy.auto_speeds_supported) {
644                                 supported |= SUPPORTED_Autoneg;
645                                 cmd->base.autoneg = AUTONEG_ENABLE;
646                                 advertising |= ADVERTISED_Autoneg;
647                         }
648
649                         supported |= SUPPORTED_Pause;
650                         if (be_pause_supported(adapter))
651                                 advertising |= ADVERTISED_Pause;
652                 } else {
653                         cmd->base.port = PORT_OTHER;
654                         cmd->base.autoneg = AUTONEG_DISABLE;
655                 }
656
657                 /* Save for future use */
658                 adapter->phy.link_speed = cmd->base.speed;
659                 adapter->phy.port_type = cmd->base.port;
660                 adapter->phy.autoneg = cmd->base.autoneg;
661                 adapter->phy.advertising = advertising;
662                 adapter->phy.supported = supported;
663         } else {
664                 cmd->base.speed = adapter->phy.link_speed;
665                 cmd->base.port = adapter->phy.port_type;
666                 cmd->base.autoneg = adapter->phy.autoneg;
667                 advertising = adapter->phy.advertising;
668                 supported = adapter->phy.supported;
669         }
670
671         cmd->base.duplex = netif_carrier_ok(netdev) ?
672                 DUPLEX_FULL : DUPLEX_UNKNOWN;
673         cmd->base.phy_address = adapter->port_num;
674
675         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
676                                                 supported);
677         ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
678                                                 advertising);
679
680         return 0;
681 }
682
683 static void be_get_ringparam(struct net_device *netdev,
684                              struct ethtool_ringparam *ring)
685 {
686         struct be_adapter *adapter = netdev_priv(netdev);
687
688         ring->rx_max_pending = adapter->rx_obj[0].q.len;
689         ring->rx_pending = adapter->rx_obj[0].q.len;
690         ring->tx_max_pending = adapter->tx_obj[0].q.len;
691         ring->tx_pending = adapter->tx_obj[0].q.len;
692 }
693
694 static void
695 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
696 {
697         struct be_adapter *adapter = netdev_priv(netdev);
698
699         be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause);
700         ecmd->autoneg = adapter->phy.fc_autoneg;
701 }
702
703 static int
704 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
705 {
706         struct be_adapter *adapter = netdev_priv(netdev);
707         int status;
708
709         if (ecmd->autoneg != adapter->phy.fc_autoneg)
710                 return -EINVAL;
711
712         status = be_cmd_set_flow_control(adapter, ecmd->tx_pause,
713                                          ecmd->rx_pause);
714         if (status) {
715                 dev_warn(&adapter->pdev->dev, "Pause param set failed\n");
716                 return be_cmd_status(status);
717         }
718
719         adapter->tx_fc = ecmd->tx_pause;
720         adapter->rx_fc = ecmd->rx_pause;
721         return 0;
722 }
723
724 static int be_set_phys_id(struct net_device *netdev,
725                           enum ethtool_phys_id_state state)
726 {
727         struct be_adapter *adapter = netdev_priv(netdev);
728         int status = 0;
729
730         switch (state) {
731         case ETHTOOL_ID_ACTIVE:
732                 status = be_cmd_get_beacon_state(adapter, adapter->hba_port_num,
733                                                  &adapter->beacon_state);
734                 if (status)
735                         return be_cmd_status(status);
736                 return 1;       /* cycle on/off once per second */
737
738         case ETHTOOL_ID_ON:
739                 status = be_cmd_set_beacon_state(adapter, adapter->hba_port_num,
740                                                  0, 0, BEACON_STATE_ENABLED);
741                 break;
742
743         case ETHTOOL_ID_OFF:
744                 status = be_cmd_set_beacon_state(adapter, adapter->hba_port_num,
745                                                  0, 0, BEACON_STATE_DISABLED);
746                 break;
747
748         case ETHTOOL_ID_INACTIVE:
749                 status = be_cmd_set_beacon_state(adapter, adapter->hba_port_num,
750                                                  0, 0, adapter->beacon_state);
751         }
752
753         return be_cmd_status(status);
754 }
755
756 static int be_set_dump(struct net_device *netdev, struct ethtool_dump *dump)
757 {
758         struct be_adapter *adapter = netdev_priv(netdev);
759         struct device *dev = &adapter->pdev->dev;
760         int status;
761
762         if (!lancer_chip(adapter) ||
763             !check_privilege(adapter, MAX_PRIVILEGES))
764                 return -EOPNOTSUPP;
765
766         switch (dump->flag) {
767         case LANCER_INITIATE_FW_DUMP:
768                 status = lancer_initiate_dump(adapter);
769                 if (!status)
770                         dev_info(dev, "FW dump initiated successfully\n");
771                 break;
772         case LANCER_DELETE_FW_DUMP:
773                 status = lancer_delete_dump(adapter);
774                 if (!status)
775                         dev_info(dev, "FW dump deleted successfully\n");
776         break;
777         default:
778                 dev_err(dev, "Invalid dump level: 0x%x\n", dump->flag);
779                 return -EINVAL;
780         }
781         return status;
782 }
783
784 static void be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
785 {
786         struct be_adapter *adapter = netdev_priv(netdev);
787
788         if (adapter->wol_cap & BE_WOL_CAP) {
789                 wol->supported |= WAKE_MAGIC;
790                 if (adapter->wol_en)
791                         wol->wolopts |= WAKE_MAGIC;
792         } else {
793                 wol->wolopts = 0;
794         }
795         memset(&wol->sopass, 0, sizeof(wol->sopass));
796 }
797
798 static int be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
799 {
800         struct be_adapter *adapter = netdev_priv(netdev);
801         struct device *dev = &adapter->pdev->dev;
802         struct be_dma_mem cmd;
803         u8 mac[ETH_ALEN];
804         bool enable;
805         int status;
806
807         if (wol->wolopts & ~WAKE_MAGIC)
808                 return -EOPNOTSUPP;
809
810         if (!(adapter->wol_cap & BE_WOL_CAP)) {
811                 dev_warn(&adapter->pdev->dev, "WOL not supported\n");
812                 return -EOPNOTSUPP;
813         }
814
815         cmd.size = sizeof(struct be_cmd_req_acpi_wol_magic_config);
816         cmd.va = dma_zalloc_coherent(dev, cmd.size, &cmd.dma, GFP_KERNEL);
817         if (!cmd.va)
818                 return -ENOMEM;
819
820         eth_zero_addr(mac);
821
822         enable = wol->wolopts & WAKE_MAGIC;
823         if (enable)
824                 ether_addr_copy(mac, adapter->netdev->dev_addr);
825
826         status = be_cmd_enable_magic_wol(adapter, mac, &cmd);
827         if (status) {
828                 dev_err(dev, "Could not set Wake-on-lan mac address\n");
829                 status = be_cmd_status(status);
830                 goto err;
831         }
832
833         pci_enable_wake(adapter->pdev, PCI_D3hot, enable);
834         pci_enable_wake(adapter->pdev, PCI_D3cold, enable);
835
836         adapter->wol_en = enable ? true : false;
837
838 err:
839         dma_free_coherent(dev, cmd.size, cmd.va, cmd.dma);
840         return status;
841 }
842
843 static int be_test_ddr_dma(struct be_adapter *adapter)
844 {
845         int ret, i;
846         struct be_dma_mem ddrdma_cmd;
847         static const u64 pattern[2] = {
848                 0x5a5a5a5a5a5a5a5aULL, 0xa5a5a5a5a5a5a5a5ULL
849         };
850
851         ddrdma_cmd.size = sizeof(struct be_cmd_req_ddrdma_test);
852         ddrdma_cmd.va = dma_zalloc_coherent(&adapter->pdev->dev,
853                                             ddrdma_cmd.size, &ddrdma_cmd.dma,
854                                             GFP_KERNEL);
855         if (!ddrdma_cmd.va)
856                 return -ENOMEM;
857
858         for (i = 0; i < 2; i++) {
859                 ret = be_cmd_ddr_dma_test(adapter, pattern[i],
860                                           4096, &ddrdma_cmd);
861                 if (ret != 0)
862                         goto err;
863         }
864
865 err:
866         dma_free_coherent(&adapter->pdev->dev, ddrdma_cmd.size, ddrdma_cmd.va,
867                           ddrdma_cmd.dma);
868         return be_cmd_status(ret);
869 }
870
871 static u64 be_loopback_test(struct be_adapter *adapter, u8 loopback_type,
872                             u64 *status)
873 {
874         int ret;
875
876         ret = be_cmd_set_loopback(adapter, adapter->hba_port_num,
877                                   loopback_type, 1);
878         if (ret)
879                 return ret;
880
881         *status = be_cmd_loopback_test(adapter, adapter->hba_port_num,
882                                        loopback_type, 1500, 2, 0xabc);
883
884         ret = be_cmd_set_loopback(adapter, adapter->hba_port_num,
885                                   BE_NO_LOOPBACK, 1);
886         if (ret)
887                 return ret;
888
889         return *status;
890 }
891
892 static void be_self_test(struct net_device *netdev, struct ethtool_test *test,
893                          u64 *data)
894 {
895         struct be_adapter *adapter = netdev_priv(netdev);
896         int status, cnt;
897         u8 link_status = 0;
898
899         if (adapter->function_caps & BE_FUNCTION_CAPS_SUPER_NIC) {
900                 dev_err(&adapter->pdev->dev, "Self test not supported\n");
901                 test->flags |= ETH_TEST_FL_FAILED;
902                 return;
903         }
904
905         memset(data, 0, sizeof(u64) * ETHTOOL_TESTS_NUM);
906
907         /* check link status before offline tests */
908         link_status = netif_carrier_ok(netdev);
909
910         if (test->flags & ETH_TEST_FL_OFFLINE) {
911                 if (be_loopback_test(adapter, BE_MAC_LOOPBACK, &data[0]) != 0)
912                         test->flags |= ETH_TEST_FL_FAILED;
913
914                 if (be_loopback_test(adapter, BE_PHY_LOOPBACK, &data[1]) != 0)
915                         test->flags |= ETH_TEST_FL_FAILED;
916
917                 if (test->flags & ETH_TEST_FL_EXTERNAL_LB) {
918                         if (be_loopback_test(adapter, BE_ONE_PORT_EXT_LOOPBACK,
919                                              &data[2]) != 0)
920                                 test->flags |= ETH_TEST_FL_FAILED;
921                         test->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
922                 }
923         }
924
925         if (!lancer_chip(adapter) && be_test_ddr_dma(adapter) != 0) {
926                 data[3] = 1;
927                 test->flags |= ETH_TEST_FL_FAILED;
928         }
929
930         /* link status was down prior to test */
931         if (!link_status) {
932                 test->flags |= ETH_TEST_FL_FAILED;
933                 data[4] = 1;
934                 return;
935         }
936
937         for (cnt = 10; cnt; cnt--) {
938                 status = be_cmd_link_status_query(adapter, NULL, &link_status,
939                                                   0);
940                 if (status) {
941                         test->flags |= ETH_TEST_FL_FAILED;
942                         data[4] = -1;
943                         break;
944                 }
945
946                 if (link_status)
947                         break;
948
949                 msleep_interruptible(500);
950         }
951 }
952
953 static int be_do_flash(struct net_device *netdev, struct ethtool_flash *efl)
954 {
955         struct be_adapter *adapter = netdev_priv(netdev);
956
957         return be_load_fw(adapter, efl->data);
958 }
959
960 static int
961 be_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
962 {
963         struct be_adapter *adapter = netdev_priv(netdev);
964
965         if (!check_privilege(adapter, MAX_PRIVILEGES))
966                 return -EOPNOTSUPP;
967
968         dump->len = be_get_dump_len(adapter);
969         dump->version = 1;
970         dump->flag = 0x1;       /* FW dump is enabled */
971         return 0;
972 }
973
974 static int
975 be_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
976                  void *buf)
977 {
978         struct be_adapter *adapter = netdev_priv(netdev);
979         int status;
980
981         if (!check_privilege(adapter, MAX_PRIVILEGES))
982                 return -EOPNOTSUPP;
983
984         status = be_read_dump_data(adapter, dump->len, buf);
985         return be_cmd_status(status);
986 }
987
988 static int be_get_eeprom_len(struct net_device *netdev)
989 {
990         struct be_adapter *adapter = netdev_priv(netdev);
991
992         if (!check_privilege(adapter, MAX_PRIVILEGES))
993                 return 0;
994
995         if (lancer_chip(adapter)) {
996                 if (be_physfn(adapter))
997                         return lancer_cmd_get_file_len(adapter,
998                                                        LANCER_VPD_PF_FILE);
999                 else
1000                         return lancer_cmd_get_file_len(adapter,
1001                                                        LANCER_VPD_VF_FILE);
1002         } else {
1003                 return BE_READ_SEEPROM_LEN;
1004         }
1005 }
1006
1007 static int be_read_eeprom(struct net_device *netdev,
1008                           struct ethtool_eeprom *eeprom, uint8_t *data)
1009 {
1010         struct be_adapter *adapter = netdev_priv(netdev);
1011         struct be_dma_mem eeprom_cmd;
1012         struct be_cmd_resp_seeprom_read *resp;
1013         int status;
1014
1015         if (!eeprom->len)
1016                 return -EINVAL;
1017
1018         if (lancer_chip(adapter)) {
1019                 if (be_physfn(adapter))
1020                         return lancer_cmd_read_file(adapter, LANCER_VPD_PF_FILE,
1021                                                     eeprom->len, data);
1022                 else
1023                         return lancer_cmd_read_file(adapter, LANCER_VPD_VF_FILE,
1024                                                     eeprom->len, data);
1025         }
1026
1027         eeprom->magic = BE_VENDOR_ID | (adapter->pdev->device<<16);
1028
1029         memset(&eeprom_cmd, 0, sizeof(struct be_dma_mem));
1030         eeprom_cmd.size = sizeof(struct be_cmd_req_seeprom_read);
1031         eeprom_cmd.va = dma_zalloc_coherent(&adapter->pdev->dev,
1032                                             eeprom_cmd.size, &eeprom_cmd.dma,
1033                                             GFP_KERNEL);
1034
1035         if (!eeprom_cmd.va)
1036                 return -ENOMEM;
1037
1038         status = be_cmd_get_seeprom_data(adapter, &eeprom_cmd);
1039
1040         if (!status) {
1041                 resp = eeprom_cmd.va;
1042                 memcpy(data, resp->seeprom_data + eeprom->offset, eeprom->len);
1043         }
1044         dma_free_coherent(&adapter->pdev->dev, eeprom_cmd.size, eeprom_cmd.va,
1045                           eeprom_cmd.dma);
1046
1047         return be_cmd_status(status);
1048 }
1049
1050 static u32 be_get_msg_level(struct net_device *netdev)
1051 {
1052         struct be_adapter *adapter = netdev_priv(netdev);
1053
1054         return adapter->msg_enable;
1055 }
1056
1057 static void be_set_msg_level(struct net_device *netdev, u32 level)
1058 {
1059         struct be_adapter *adapter = netdev_priv(netdev);
1060
1061         if (adapter->msg_enable == level)
1062                 return;
1063
1064         if ((level & NETIF_MSG_HW) != (adapter->msg_enable & NETIF_MSG_HW))
1065                 if (BEx_chip(adapter))
1066                         be_cmd_set_fw_log_level(adapter, level & NETIF_MSG_HW ?
1067                                                 FW_LOG_LEVEL_DEFAULT :
1068                                                 FW_LOG_LEVEL_FATAL);
1069         adapter->msg_enable = level;
1070 }
1071
1072 static u64 be_get_rss_hash_opts(struct be_adapter *adapter, u64 flow_type)
1073 {
1074         u64 data = 0;
1075
1076         switch (flow_type) {
1077         case TCP_V4_FLOW:
1078                 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV4)
1079                         data |= RXH_IP_DST | RXH_IP_SRC;
1080                 if (adapter->rss_info.rss_flags & RSS_ENABLE_TCP_IPV4)
1081                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1082                 break;
1083         case UDP_V4_FLOW:
1084                 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV4)
1085                         data |= RXH_IP_DST | RXH_IP_SRC;
1086                 if (adapter->rss_info.rss_flags & RSS_ENABLE_UDP_IPV4)
1087                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1088                 break;
1089         case TCP_V6_FLOW:
1090                 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV6)
1091                         data |= RXH_IP_DST | RXH_IP_SRC;
1092                 if (adapter->rss_info.rss_flags & RSS_ENABLE_TCP_IPV6)
1093                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1094                 break;
1095         case UDP_V6_FLOW:
1096                 if (adapter->rss_info.rss_flags & RSS_ENABLE_IPV6)
1097                         data |= RXH_IP_DST | RXH_IP_SRC;
1098                 if (adapter->rss_info.rss_flags & RSS_ENABLE_UDP_IPV6)
1099                         data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1100                 break;
1101         }
1102
1103         return data;
1104 }
1105
1106 static int be_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
1107                         u32 *rule_locs)
1108 {
1109         struct be_adapter *adapter = netdev_priv(netdev);
1110
1111         if (!be_multi_rxq(adapter)) {
1112                 dev_info(&adapter->pdev->dev,
1113                          "ethtool::get_rxnfc: RX flow hashing is disabled\n");
1114                 return -EINVAL;
1115         }
1116
1117         switch (cmd->cmd) {
1118         case ETHTOOL_GRXFH:
1119                 cmd->data = be_get_rss_hash_opts(adapter, cmd->flow_type);
1120                 break;
1121         case ETHTOOL_GRXRINGS:
1122                 cmd->data = adapter->num_rx_qs;
1123                 break;
1124         default:
1125                 return -EINVAL;
1126         }
1127
1128         return 0;
1129 }
1130
1131 static int be_set_rss_hash_opts(struct be_adapter *adapter,
1132                                 struct ethtool_rxnfc *cmd)
1133 {
1134         int status;
1135         u32 rss_flags = adapter->rss_info.rss_flags;
1136
1137         if (cmd->data != L3_RSS_FLAGS &&
1138             cmd->data != (L3_RSS_FLAGS | L4_RSS_FLAGS))
1139                 return -EINVAL;
1140
1141         switch (cmd->flow_type) {
1142         case TCP_V4_FLOW:
1143                 if (cmd->data == L3_RSS_FLAGS)
1144                         rss_flags &= ~RSS_ENABLE_TCP_IPV4;
1145                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1146                         rss_flags |= RSS_ENABLE_IPV4 |
1147                                         RSS_ENABLE_TCP_IPV4;
1148                 break;
1149         case TCP_V6_FLOW:
1150                 if (cmd->data == L3_RSS_FLAGS)
1151                         rss_flags &= ~RSS_ENABLE_TCP_IPV6;
1152                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1153                         rss_flags |= RSS_ENABLE_IPV6 |
1154                                         RSS_ENABLE_TCP_IPV6;
1155                 break;
1156         case UDP_V4_FLOW:
1157                 if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) &&
1158                     BEx_chip(adapter))
1159                         return -EINVAL;
1160
1161                 if (cmd->data == L3_RSS_FLAGS)
1162                         rss_flags &= ~RSS_ENABLE_UDP_IPV4;
1163                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1164                         rss_flags |= RSS_ENABLE_IPV4 |
1165                                         RSS_ENABLE_UDP_IPV4;
1166                 break;
1167         case UDP_V6_FLOW:
1168                 if ((cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS)) &&
1169                     BEx_chip(adapter))
1170                         return -EINVAL;
1171
1172                 if (cmd->data == L3_RSS_FLAGS)
1173                         rss_flags &= ~RSS_ENABLE_UDP_IPV6;
1174                 else if (cmd->data == (L3_RSS_FLAGS | L4_RSS_FLAGS))
1175                         rss_flags |= RSS_ENABLE_IPV6 |
1176                                         RSS_ENABLE_UDP_IPV6;
1177                 break;
1178         default:
1179                 return -EINVAL;
1180         }
1181
1182         if (rss_flags == adapter->rss_info.rss_flags)
1183                 return 0;
1184
1185         status = be_cmd_rss_config(adapter, adapter->rss_info.rsstable,
1186                                    rss_flags, RSS_INDIR_TABLE_LEN,
1187                                    adapter->rss_info.rss_hkey);
1188         if (!status)
1189                 adapter->rss_info.rss_flags = rss_flags;
1190
1191         return be_cmd_status(status);
1192 }
1193
1194 static int be_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
1195 {
1196         struct be_adapter *adapter = netdev_priv(netdev);
1197         int status = 0;
1198
1199         if (!be_multi_rxq(adapter)) {
1200                 dev_err(&adapter->pdev->dev,
1201                         "ethtool::set_rxnfc: RX flow hashing is disabled\n");
1202                 return -EINVAL;
1203         }
1204
1205         switch (cmd->cmd) {
1206         case ETHTOOL_SRXFH:
1207                 status = be_set_rss_hash_opts(adapter, cmd);
1208                 break;
1209         default:
1210                 return -EINVAL;
1211         }
1212
1213         return status;
1214 }
1215
1216 static void be_get_channels(struct net_device *netdev,
1217                             struct ethtool_channels *ch)
1218 {
1219         struct be_adapter *adapter = netdev_priv(netdev);
1220         u16 num_rx_irqs = max_t(u16, adapter->num_rss_qs, 1);
1221
1222         /* num_tx_qs is always same as the number of irqs used for TX */
1223         ch->combined_count = min(adapter->num_tx_qs, num_rx_irqs);
1224         ch->rx_count = num_rx_irqs - ch->combined_count;
1225         ch->tx_count = adapter->num_tx_qs - ch->combined_count;
1226
1227         ch->max_combined = be_max_qp_irqs(adapter);
1228         /* The user must create atleast one combined channel */
1229         ch->max_rx = be_max_rx_irqs(adapter) - 1;
1230         ch->max_tx = be_max_tx_irqs(adapter) - 1;
1231 }
1232
1233 static int be_set_channels(struct net_device  *netdev,
1234                            struct ethtool_channels *ch)
1235 {
1236         struct be_adapter *adapter = netdev_priv(netdev);
1237         int status;
1238
1239         /* we support either only combined channels or a combination of
1240          * combined and either RX-only or TX-only channels.
1241          */
1242         if (ch->other_count || !ch->combined_count ||
1243             (ch->rx_count && ch->tx_count))
1244                 return -EINVAL;
1245
1246         if (ch->combined_count > be_max_qp_irqs(adapter) ||
1247             (ch->rx_count &&
1248              (ch->rx_count + ch->combined_count) > be_max_rx_irqs(adapter)) ||
1249             (ch->tx_count &&
1250              (ch->tx_count + ch->combined_count) > be_max_tx_irqs(adapter)))
1251                 return -EINVAL;
1252
1253         adapter->cfg_num_rx_irqs = ch->combined_count + ch->rx_count;
1254         adapter->cfg_num_tx_irqs = ch->combined_count + ch->tx_count;
1255
1256         status = be_update_queues(adapter);
1257         return be_cmd_status(status);
1258 }
1259
1260 static u32 be_get_rxfh_indir_size(struct net_device *netdev)
1261 {
1262         return RSS_INDIR_TABLE_LEN;
1263 }
1264
1265 static u32 be_get_rxfh_key_size(struct net_device *netdev)
1266 {
1267         return RSS_HASH_KEY_LEN;
1268 }
1269
1270 static int be_get_rxfh(struct net_device *netdev, u32 *indir, u8 *hkey,
1271                        u8 *hfunc)
1272 {
1273         struct be_adapter *adapter = netdev_priv(netdev);
1274         int i;
1275         struct rss_info *rss = &adapter->rss_info;
1276
1277         if (indir) {
1278                 for (i = 0; i < RSS_INDIR_TABLE_LEN; i++)
1279                         indir[i] = rss->rss_queue[i];
1280         }
1281
1282         if (hkey)
1283                 memcpy(hkey, rss->rss_hkey, RSS_HASH_KEY_LEN);
1284
1285         if (hfunc)
1286                 *hfunc = ETH_RSS_HASH_TOP;
1287
1288         return 0;
1289 }
1290
1291 static int be_set_rxfh(struct net_device *netdev, const u32 *indir,
1292                        const u8 *hkey, const u8 hfunc)
1293 {
1294         int rc = 0, i, j;
1295         struct be_adapter *adapter = netdev_priv(netdev);
1296         u8 rsstable[RSS_INDIR_TABLE_LEN];
1297
1298         /* We do not allow change in unsupported parameters */
1299         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1300                 return -EOPNOTSUPP;
1301
1302         if (indir) {
1303                 struct be_rx_obj *rxo;
1304
1305                 for (i = 0; i < RSS_INDIR_TABLE_LEN; i++) {
1306                         j = indir[i];
1307                         rxo = &adapter->rx_obj[j];
1308                         rsstable[i] = rxo->rss_id;
1309                         adapter->rss_info.rss_queue[i] = j;
1310                 }
1311         } else {
1312                 memcpy(rsstable, adapter->rss_info.rsstable,
1313                        RSS_INDIR_TABLE_LEN);
1314         }
1315
1316         if (!hkey)
1317                 hkey =  adapter->rss_info.rss_hkey;
1318
1319         rc = be_cmd_rss_config(adapter, rsstable,
1320                                adapter->rss_info.rss_flags,
1321                                RSS_INDIR_TABLE_LEN, hkey);
1322         if (rc) {
1323                 adapter->rss_info.rss_flags = RSS_ENABLE_NONE;
1324                 return -EIO;
1325         }
1326         memcpy(adapter->rss_info.rss_hkey, hkey, RSS_HASH_KEY_LEN);
1327         memcpy(adapter->rss_info.rsstable, rsstable,
1328                RSS_INDIR_TABLE_LEN);
1329         return 0;
1330 }
1331
1332 static int be_get_module_info(struct net_device *netdev,
1333                               struct ethtool_modinfo *modinfo)
1334 {
1335         struct be_adapter *adapter = netdev_priv(netdev);
1336         u8 page_data[PAGE_DATA_LEN];
1337         int status;
1338
1339         if (!check_privilege(adapter, MAX_PRIVILEGES))
1340                 return -EOPNOTSUPP;
1341
1342         status = be_cmd_read_port_transceiver_data(adapter, TR_PAGE_A0,
1343                                                    page_data);
1344         if (!status) {
1345                 if (!page_data[SFP_PLUS_SFF_8472_COMP]) {
1346                         modinfo->type = ETH_MODULE_SFF_8079;
1347                         modinfo->eeprom_len = PAGE_DATA_LEN;
1348                 } else {
1349                         modinfo->type = ETH_MODULE_SFF_8472;
1350                         modinfo->eeprom_len = 2 * PAGE_DATA_LEN;
1351                 }
1352         }
1353         return be_cmd_status(status);
1354 }
1355
1356 static int be_get_module_eeprom(struct net_device *netdev,
1357                                 struct ethtool_eeprom *eeprom, u8 *data)
1358 {
1359         struct be_adapter *adapter = netdev_priv(netdev);
1360         int status;
1361
1362         if (!check_privilege(adapter, MAX_PRIVILEGES))
1363                 return -EOPNOTSUPP;
1364
1365         status = be_cmd_read_port_transceiver_data(adapter, TR_PAGE_A0,
1366                                                    data);
1367         if (status)
1368                 goto err;
1369
1370         if (eeprom->offset + eeprom->len > PAGE_DATA_LEN) {
1371                 status = be_cmd_read_port_transceiver_data(adapter,
1372                                                            TR_PAGE_A2,
1373                                                            data +
1374                                                            PAGE_DATA_LEN);
1375                 if (status)
1376                         goto err;
1377         }
1378         if (eeprom->offset)
1379                 memcpy(data, data + eeprom->offset, eeprom->len);
1380 err:
1381         return be_cmd_status(status);
1382 }
1383
1384 static u32 be_get_priv_flags(struct net_device *netdev)
1385 {
1386         struct be_adapter *adapter = netdev_priv(netdev);
1387
1388         return adapter->priv_flags;
1389 }
1390
1391 static int be_set_priv_flags(struct net_device *netdev, u32 flags)
1392 {
1393         struct be_adapter *adapter = netdev_priv(netdev);
1394         bool tpe_old = !!(adapter->priv_flags & BE_DISABLE_TPE_RECOVERY);
1395         bool tpe_new = !!(flags & BE_DISABLE_TPE_RECOVERY);
1396
1397         if (tpe_old != tpe_new) {
1398                 if (tpe_new) {
1399                         adapter->priv_flags |= BE_DISABLE_TPE_RECOVERY;
1400                         dev_info(&adapter->pdev->dev,
1401                                  "HW error recovery is disabled\n");
1402                 } else {
1403                         adapter->priv_flags &= ~BE_DISABLE_TPE_RECOVERY;
1404                         dev_info(&adapter->pdev->dev,
1405                                  "HW error recovery is enabled\n");
1406                 }
1407         }
1408
1409         return 0;
1410 }
1411
1412 const struct ethtool_ops be_ethtool_ops = {
1413         .get_drvinfo = be_get_drvinfo,
1414         .get_wol = be_get_wol,
1415         .set_wol = be_set_wol,
1416         .get_link = ethtool_op_get_link,
1417         .get_eeprom_len = be_get_eeprom_len,
1418         .get_eeprom = be_read_eeprom,
1419         .get_coalesce = be_get_coalesce,
1420         .set_coalesce = be_set_coalesce,
1421         .get_ringparam = be_get_ringparam,
1422         .get_pauseparam = be_get_pauseparam,
1423         .set_pauseparam = be_set_pauseparam,
1424         .set_priv_flags = be_set_priv_flags,
1425         .get_priv_flags = be_get_priv_flags,
1426         .get_strings = be_get_stat_strings,
1427         .set_phys_id = be_set_phys_id,
1428         .set_dump = be_set_dump,
1429         .get_msglevel = be_get_msg_level,
1430         .set_msglevel = be_set_msg_level,
1431         .get_sset_count = be_get_sset_count,
1432         .get_ethtool_stats = be_get_ethtool_stats,
1433         .flash_device = be_do_flash,
1434         .self_test = be_self_test,
1435         .get_rxnfc = be_get_rxnfc,
1436         .set_rxnfc = be_set_rxnfc,
1437         .get_rxfh_indir_size = be_get_rxfh_indir_size,
1438         .get_rxfh_key_size = be_get_rxfh_key_size,
1439         .get_rxfh = be_get_rxfh,
1440         .set_rxfh = be_set_rxfh,
1441         .get_dump_flag = be_get_dump_flag,
1442         .get_dump_data = be_get_dump_data,
1443         .get_channels = be_get_channels,
1444         .set_channels = be_set_channels,
1445         .get_module_info = be_get_module_info,
1446         .get_module_eeprom = be_get_module_eeprom,
1447         .get_link_ksettings = be_get_link_ksettings,
1448 };