82a9a27a9812cecb5d0f7ba2c566dfbc443c08ff
[cascardo/linux.git] / drivers / net / benet / be_ethtool.c
1 /*
2  * Copyright (C) 2005 - 2010 ServerEngines
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@serverengines.com
12  *
13  * ServerEngines
14  * 209 N. Fair Oaks Ave
15  * Sunnyvale, CA 94085
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 {NETSTAT, PORTSTAT, MISCSTAT, DRVSTAT_TX, DRVSTAT_RX, ERXSTAT,
30                         PMEMSTAT};
31 #define FIELDINFO(_struct, field) FIELD_SIZEOF(_struct, field), \
32                                         offsetof(_struct, field)
33 #define NETSTAT_INFO(field)     #field, NETSTAT,\
34                                         FIELDINFO(struct net_device_stats,\
35                                                 field)
36 #define DRVSTAT_TX_INFO(field)  #field, DRVSTAT_TX,\
37                                         FIELDINFO(struct be_tx_stats, field)
38 #define DRVSTAT_RX_INFO(field)  #field, DRVSTAT_RX,\
39                                         FIELDINFO(struct be_rx_stats, field)
40 #define MISCSTAT_INFO(field)    #field, MISCSTAT,\
41                                         FIELDINFO(struct be_rxf_stats, field)
42 #define PORTSTAT_INFO(field)    #field, PORTSTAT,\
43                                         FIELDINFO(struct be_port_rxf_stats, \
44                                                 field)
45 #define ERXSTAT_INFO(field)     #field, ERXSTAT,\
46                                         FIELDINFO(struct be_erx_stats, field)
47 #define PMEMSTAT_INFO(field)    #field, PMEMSTAT,\
48                                         FIELDINFO(struct be_pmem_stats, field)
49
50 static const struct be_ethtool_stat et_stats[] = {
51         {NETSTAT_INFO(rx_packets)},
52         {NETSTAT_INFO(tx_packets)},
53         {NETSTAT_INFO(rx_bytes)},
54         {NETSTAT_INFO(tx_bytes)},
55         {NETSTAT_INFO(rx_errors)},
56         {NETSTAT_INFO(tx_errors)},
57         {NETSTAT_INFO(rx_dropped)},
58         {NETSTAT_INFO(tx_dropped)},
59         {DRVSTAT_TX_INFO(be_tx_rate)},
60         {DRVSTAT_TX_INFO(be_tx_reqs)},
61         {DRVSTAT_TX_INFO(be_tx_wrbs)},
62         {DRVSTAT_TX_INFO(be_tx_stops)},
63         {DRVSTAT_TX_INFO(be_tx_events)},
64         {DRVSTAT_TX_INFO(be_tx_compl)},
65         {PORTSTAT_INFO(rx_unicast_frames)},
66         {PORTSTAT_INFO(rx_multicast_frames)},
67         {PORTSTAT_INFO(rx_broadcast_frames)},
68         {PORTSTAT_INFO(rx_crc_errors)},
69         {PORTSTAT_INFO(rx_alignment_symbol_errors)},
70         {PORTSTAT_INFO(rx_pause_frames)},
71         {PORTSTAT_INFO(rx_control_frames)},
72         {PORTSTAT_INFO(rx_in_range_errors)},
73         {PORTSTAT_INFO(rx_out_range_errors)},
74         {PORTSTAT_INFO(rx_frame_too_long)},
75         {PORTSTAT_INFO(rx_address_match_errors)},
76         {PORTSTAT_INFO(rx_vlan_mismatch)},
77         {PORTSTAT_INFO(rx_dropped_too_small)},
78         {PORTSTAT_INFO(rx_dropped_too_short)},
79         {PORTSTAT_INFO(rx_dropped_header_too_small)},
80         {PORTSTAT_INFO(rx_dropped_tcp_length)},
81         {PORTSTAT_INFO(rx_dropped_runt)},
82         {PORTSTAT_INFO(rx_fifo_overflow)},
83         {PORTSTAT_INFO(rx_input_fifo_overflow)},
84         {PORTSTAT_INFO(rx_ip_checksum_errs)},
85         {PORTSTAT_INFO(rx_tcp_checksum_errs)},
86         {PORTSTAT_INFO(rx_udp_checksum_errs)},
87         {PORTSTAT_INFO(rx_non_rss_packets)},
88         {PORTSTAT_INFO(rx_ipv4_packets)},
89         {PORTSTAT_INFO(rx_ipv6_packets)},
90         {PORTSTAT_INFO(rx_switched_unicast_packets)},
91         {PORTSTAT_INFO(rx_switched_multicast_packets)},
92         {PORTSTAT_INFO(rx_switched_broadcast_packets)},
93         {PORTSTAT_INFO(tx_unicastframes)},
94         {PORTSTAT_INFO(tx_multicastframes)},
95         {PORTSTAT_INFO(tx_broadcastframes)},
96         {PORTSTAT_INFO(tx_pauseframes)},
97         {PORTSTAT_INFO(tx_controlframes)},
98         {MISCSTAT_INFO(rx_drops_no_pbuf)},
99         {MISCSTAT_INFO(rx_drops_no_txpb)},
100         {MISCSTAT_INFO(rx_drops_no_erx_descr)},
101         {MISCSTAT_INFO(rx_drops_no_tpre_descr)},
102         {MISCSTAT_INFO(rx_drops_too_many_frags)},
103         {MISCSTAT_INFO(rx_drops_invalid_ring)},
104         {MISCSTAT_INFO(forwarded_packets)},
105         {MISCSTAT_INFO(rx_drops_mtu)},
106         {MISCSTAT_INFO(port0_jabber_events)},
107         {MISCSTAT_INFO(port1_jabber_events)},
108         {PMEMSTAT_INFO(eth_red_drops)}
109 };
110 #define ETHTOOL_STATS_NUM ARRAY_SIZE(et_stats)
111
112 /* Stats related to multi RX queues */
113 static const struct be_ethtool_stat et_rx_stats[] = {
114         {DRVSTAT_RX_INFO(rx_bytes)},
115         {DRVSTAT_RX_INFO(rx_pkts)},
116         {DRVSTAT_RX_INFO(rx_rate)},
117         {DRVSTAT_RX_INFO(rx_polls)},
118         {DRVSTAT_RX_INFO(rx_events)},
119         {DRVSTAT_RX_INFO(rx_compl)},
120         {DRVSTAT_RX_INFO(rx_mcast_pkts)},
121         {DRVSTAT_RX_INFO(rx_post_fail)},
122         {ERXSTAT_INFO(rx_drops_no_fragments)}
123 };
124 #define ETHTOOL_RXSTATS_NUM (ARRAY_SIZE(et_rx_stats))
125
126 static const char et_self_tests[][ETH_GSTRING_LEN] = {
127         "MAC Loopback test",
128         "PHY Loopback test",
129         "External Loopback test",
130         "DDR DMA test"
131         "Link test"
132 };
133
134 #define ETHTOOL_TESTS_NUM ARRAY_SIZE(et_self_tests)
135 #define BE_MAC_LOOPBACK 0x0
136 #define BE_PHY_LOOPBACK 0x1
137 #define BE_ONE_PORT_EXT_LOOPBACK 0x2
138 #define BE_NO_LOOPBACK 0xff
139
140 static void
141 be_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
142 {
143         struct be_adapter *adapter = netdev_priv(netdev);
144
145         strcpy(drvinfo->driver, DRV_NAME);
146         strcpy(drvinfo->version, DRV_VER);
147         strncpy(drvinfo->fw_version, adapter->fw_ver, FW_VER_LEN);
148         strcpy(drvinfo->bus_info, pci_name(adapter->pdev));
149         drvinfo->testinfo_len = 0;
150         drvinfo->regdump_len = 0;
151         drvinfo->eedump_len = 0;
152 }
153
154 static int
155 be_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
156 {
157         struct be_adapter *adapter = netdev_priv(netdev);
158         struct be_eq_obj *rx_eq = &adapter->rx_obj[0].rx_eq;
159         struct be_eq_obj *tx_eq = &adapter->tx_eq;
160
161         coalesce->rx_coalesce_usecs = rx_eq->cur_eqd;
162         coalesce->rx_coalesce_usecs_high = rx_eq->max_eqd;
163         coalesce->rx_coalesce_usecs_low = rx_eq->min_eqd;
164
165         coalesce->tx_coalesce_usecs = tx_eq->cur_eqd;
166         coalesce->tx_coalesce_usecs_high = tx_eq->max_eqd;
167         coalesce->tx_coalesce_usecs_low = tx_eq->min_eqd;
168
169         coalesce->use_adaptive_rx_coalesce = rx_eq->enable_aic;
170         coalesce->use_adaptive_tx_coalesce = tx_eq->enable_aic;
171
172         return 0;
173 }
174
175 /*
176  * This routine is used to set interrup coalescing delay
177  */
178 static int
179 be_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coalesce)
180 {
181         struct be_adapter *adapter = netdev_priv(netdev);
182         struct be_rx_obj *rxo;
183         struct be_eq_obj *rx_eq;
184         struct be_eq_obj *tx_eq = &adapter->tx_eq;
185         u32 tx_max, tx_min, tx_cur;
186         u32 rx_max, rx_min, rx_cur;
187         int status = 0, i;
188
189         if (coalesce->use_adaptive_tx_coalesce == 1)
190                 return -EINVAL;
191
192         for_all_rx_queues(adapter, rxo, i) {
193                 rx_eq = &rxo->rx_eq;
194
195                 if (!rx_eq->enable_aic && coalesce->use_adaptive_rx_coalesce)
196                         rx_eq->cur_eqd = 0;
197                 rx_eq->enable_aic = coalesce->use_adaptive_rx_coalesce;
198
199                 rx_max = coalesce->rx_coalesce_usecs_high;
200                 rx_min = coalesce->rx_coalesce_usecs_low;
201                 rx_cur = coalesce->rx_coalesce_usecs;
202
203                 if (rx_eq->enable_aic) {
204                         if (rx_max > BE_MAX_EQD)
205                                 rx_max = BE_MAX_EQD;
206                         if (rx_min > rx_max)
207                                 rx_min = rx_max;
208                         rx_eq->max_eqd = rx_max;
209                         rx_eq->min_eqd = rx_min;
210                         if (rx_eq->cur_eqd > rx_max)
211                                 rx_eq->cur_eqd = rx_max;
212                         if (rx_eq->cur_eqd < rx_min)
213                                 rx_eq->cur_eqd = rx_min;
214                 } else {
215                         if (rx_cur > BE_MAX_EQD)
216                                 rx_cur = BE_MAX_EQD;
217                         if (rx_eq->cur_eqd != rx_cur) {
218                                 status = be_cmd_modify_eqd(adapter, rx_eq->q.id,
219                                                 rx_cur);
220                                 if (!status)
221                                         rx_eq->cur_eqd = rx_cur;
222                         }
223                 }
224         }
225
226         tx_max = coalesce->tx_coalesce_usecs_high;
227         tx_min = coalesce->tx_coalesce_usecs_low;
228         tx_cur = coalesce->tx_coalesce_usecs;
229
230         if (tx_cur > BE_MAX_EQD)
231                 tx_cur = BE_MAX_EQD;
232         if (tx_eq->cur_eqd != tx_cur) {
233                 status = be_cmd_modify_eqd(adapter, tx_eq->q.id, tx_cur);
234                 if (!status)
235                         tx_eq->cur_eqd = tx_cur;
236         }
237
238         return 0;
239 }
240
241 static u32 be_get_rx_csum(struct net_device *netdev)
242 {
243         struct be_adapter *adapter = netdev_priv(netdev);
244
245         return adapter->rx_csum;
246 }
247
248 static int be_set_rx_csum(struct net_device *netdev, uint32_t data)
249 {
250         struct be_adapter *adapter = netdev_priv(netdev);
251
252         if (data)
253                 adapter->rx_csum = true;
254         else
255                 adapter->rx_csum = false;
256
257         return 0;
258 }
259
260 static void
261 be_get_ethtool_stats(struct net_device *netdev,
262                 struct ethtool_stats *stats, uint64_t *data)
263 {
264         struct be_adapter *adapter = netdev_priv(netdev);
265         struct be_hw_stats *hw_stats = hw_stats_from_cmd(adapter->stats_cmd.va);
266         struct be_erx_stats *erx_stats = &hw_stats->erx;
267         struct be_rx_obj *rxo;
268         void *p = NULL;
269         int i, j;
270
271         for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
272                 switch (et_stats[i].type) {
273                 case NETSTAT:
274                         p = &netdev->stats;
275                         break;
276                 case DRVSTAT_TX:
277                         p = &adapter->tx_stats;
278                         break;
279                 case PORTSTAT:
280                         p = &hw_stats->rxf.port[adapter->port_num];
281                         break;
282                 case MISCSTAT:
283                         p = &hw_stats->rxf;
284                         break;
285                 case PMEMSTAT:
286                         p = &hw_stats->pmem;
287                         break;
288                 }
289
290                 p = (u8 *)p + et_stats[i].offset;
291                 data[i] = (et_stats[i].size == sizeof(u64)) ?
292                                 *(u64 *)p: *(u32 *)p;
293         }
294
295         for_all_rx_queues(adapter, rxo, j) {
296                 for (i = 0; i < ETHTOOL_RXSTATS_NUM; i++) {
297                         switch (et_rx_stats[i].type) {
298                         case DRVSTAT_RX:
299                                 p = (u8 *)&rxo->stats + et_rx_stats[i].offset;
300                                 break;
301                         case ERXSTAT:
302                                 p = (u32 *)erx_stats + rxo->q.id;
303                                 break;
304                         }
305                         data[ETHTOOL_STATS_NUM + j * ETHTOOL_RXSTATS_NUM + i] =
306                                 (et_rx_stats[i].size == sizeof(u64)) ?
307                                         *(u64 *)p: *(u32 *)p;
308                 }
309         }
310 }
311
312 static void
313 be_get_stat_strings(struct net_device *netdev, uint32_t stringset,
314                 uint8_t *data)
315 {
316         struct be_adapter *adapter = netdev_priv(netdev);
317         int i, j;
318
319         switch (stringset) {
320         case ETH_SS_STATS:
321                 for (i = 0; i < ETHTOOL_STATS_NUM; i++) {
322                         memcpy(data, et_stats[i].desc, ETH_GSTRING_LEN);
323                         data += ETH_GSTRING_LEN;
324                 }
325                 for (i = 0; i < adapter->num_rx_qs; i++) {
326                         for (j = 0; j < ETHTOOL_RXSTATS_NUM; j++) {
327                                 sprintf(data, "rxq%d: %s", i,
328                                         et_rx_stats[j].desc);
329                                 data += ETH_GSTRING_LEN;
330                         }
331                 }
332                 break;
333         case ETH_SS_TEST:
334                 for (i = 0; i < ETHTOOL_TESTS_NUM; i++) {
335                         memcpy(data, et_self_tests[i], ETH_GSTRING_LEN);
336                         data += ETH_GSTRING_LEN;
337                 }
338                 break;
339         }
340 }
341
342 static int be_get_sset_count(struct net_device *netdev, int stringset)
343 {
344         struct be_adapter *adapter = netdev_priv(netdev);
345
346         switch (stringset) {
347         case ETH_SS_TEST:
348                 return ETHTOOL_TESTS_NUM;
349         case ETH_SS_STATS:
350                 return ETHTOOL_STATS_NUM +
351                         adapter->num_rx_qs * ETHTOOL_RXSTATS_NUM;
352         default:
353                 return -EINVAL;
354         }
355 }
356
357 static int be_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
358 {
359         struct be_adapter *adapter = netdev_priv(netdev);
360         struct be_dma_mem phy_cmd;
361         struct be_cmd_resp_get_phy_info *resp;
362         u8 mac_speed = 0;
363         u16 link_speed = 0;
364         bool link_up = false;
365         int status;
366         u16 intf_type;
367
368         if ((adapter->link_speed < 0) || (!(netdev->flags & IFF_UP))) {
369                 status = be_cmd_link_status_query(adapter, &link_up,
370                                                 &mac_speed, &link_speed);
371
372                 be_link_status_update(adapter, link_up);
373                 /* link_speed is in units of 10 Mbps */
374                 if (link_speed) {
375                         ecmd->speed = link_speed*10;
376                 } else {
377                         switch (mac_speed) {
378                         case PHY_LINK_SPEED_1GBPS:
379                                 ecmd->speed = SPEED_1000;
380                                 break;
381                         case PHY_LINK_SPEED_10GBPS:
382                                 ecmd->speed = SPEED_10000;
383                                 break;
384                         }
385                 }
386
387                 phy_cmd.size = sizeof(struct be_cmd_req_get_phy_info);
388                 phy_cmd.va = dma_alloc_coherent(&adapter->pdev->dev,
389                                                 phy_cmd.size, &phy_cmd.dma,
390                                                 GFP_KERNEL);
391                 if (!phy_cmd.va) {
392                         dev_err(&adapter->pdev->dev, "Memory alloc failure\n");
393                         return -ENOMEM;
394                 }
395                 status = be_cmd_get_phy_info(adapter, &phy_cmd);
396                 if (!status) {
397                         resp = (struct be_cmd_resp_get_phy_info *) phy_cmd.va;
398                         intf_type = le16_to_cpu(resp->interface_type);
399
400                         switch (intf_type) {
401                         case PHY_TYPE_XFP_10GB:
402                         case PHY_TYPE_SFP_1GB:
403                         case PHY_TYPE_SFP_PLUS_10GB:
404                                 ecmd->port = PORT_FIBRE;
405                                 break;
406                         default:
407                                 ecmd->port = PORT_TP;
408                                 break;
409                         }
410
411                         switch (intf_type) {
412                         case PHY_TYPE_KR_10GB:
413                         case PHY_TYPE_KX4_10GB:
414                                 ecmd->autoneg = AUTONEG_ENABLE;
415                         ecmd->transceiver = XCVR_INTERNAL;
416                                 break;
417                         default:
418                                 ecmd->autoneg = AUTONEG_DISABLE;
419                                 ecmd->transceiver = XCVR_EXTERNAL;
420                                 break;
421                         }
422                 }
423
424                 /* Save for future use */
425                 adapter->link_speed = ecmd->speed;
426                 adapter->port_type = ecmd->port;
427                 adapter->transceiver = ecmd->transceiver;
428                 adapter->autoneg = ecmd->autoneg;
429                 dma_free_coherent(&adapter->pdev->dev, phy_cmd.size, phy_cmd.va,
430                                   phy_cmd.dma);
431         } else {
432                 ecmd->speed = adapter->link_speed;
433                 ecmd->port = adapter->port_type;
434                 ecmd->transceiver = adapter->transceiver;
435                 ecmd->autoneg = adapter->autoneg;
436         }
437
438         ecmd->duplex = DUPLEX_FULL;
439         ecmd->phy_address = adapter->port_num;
440         switch (ecmd->port) {
441         case PORT_FIBRE:
442                 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
443                 break;
444         case PORT_TP:
445                 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_TP);
446                 break;
447         case PORT_AUI:
448                 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_AUI);
449                 break;
450         }
451
452         if (ecmd->autoneg) {
453                 ecmd->supported |= SUPPORTED_1000baseT_Full;
454                 ecmd->supported |= SUPPORTED_Autoneg;
455                 ecmd->advertising |= (ADVERTISED_10000baseT_Full |
456                                 ADVERTISED_1000baseT_Full);
457         }
458
459         return 0;
460 }
461
462 static void
463 be_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring)
464 {
465         struct be_adapter *adapter = netdev_priv(netdev);
466
467         ring->rx_max_pending = adapter->rx_obj[0].q.len;
468         ring->tx_max_pending = adapter->tx_obj.q.len;
469
470         ring->rx_pending = atomic_read(&adapter->rx_obj[0].q.used);
471         ring->tx_pending = atomic_read(&adapter->tx_obj.q.used);
472 }
473
474 static void
475 be_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
476 {
477         struct be_adapter *adapter = netdev_priv(netdev);
478
479         be_cmd_get_flow_control(adapter, &ecmd->tx_pause, &ecmd->rx_pause);
480         ecmd->autoneg = 0;
481 }
482
483 static int
484 be_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *ecmd)
485 {
486         struct be_adapter *adapter = netdev_priv(netdev);
487         int status;
488
489         if (ecmd->autoneg != 0)
490                 return -EINVAL;
491         adapter->tx_fc = ecmd->tx_pause;
492         adapter->rx_fc = ecmd->rx_pause;
493
494         status = be_cmd_set_flow_control(adapter,
495                                         adapter->tx_fc, adapter->rx_fc);
496         if (status)
497                 dev_warn(&adapter->pdev->dev, "Pause param set failed.\n");
498
499         return status;
500 }
501
502 static int
503 be_phys_id(struct net_device *netdev, u32 data)
504 {
505         struct be_adapter *adapter = netdev_priv(netdev);
506         int status;
507         u32 cur;
508
509         be_cmd_get_beacon_state(adapter, adapter->port_num, &cur);
510
511         if (cur == BEACON_STATE_ENABLED)
512                 return 0;
513
514         if (data < 2)
515                 data = 2;
516
517         status = be_cmd_set_beacon_state(adapter, adapter->port_num, 0, 0,
518                         BEACON_STATE_ENABLED);
519         set_current_state(TASK_INTERRUPTIBLE);
520         schedule_timeout(data*HZ);
521
522         status = be_cmd_set_beacon_state(adapter, adapter->port_num, 0, 0,
523                         BEACON_STATE_DISABLED);
524
525         return status;
526 }
527
528 static bool
529 be_is_wol_supported(struct be_adapter *adapter)
530 {
531         if (!be_physfn(adapter))
532                 return false;
533         else
534                 return true;
535 }
536
537 static void
538 be_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
539 {
540         struct be_adapter *adapter = netdev_priv(netdev);
541
542         if (be_is_wol_supported(adapter))
543                 wol->supported = WAKE_MAGIC;
544
545         if (adapter->wol)
546                 wol->wolopts = WAKE_MAGIC;
547         else
548                 wol->wolopts = 0;
549         memset(&wol->sopass, 0, sizeof(wol->sopass));
550 }
551
552 static int
553 be_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
554 {
555         struct be_adapter *adapter = netdev_priv(netdev);
556
557         if (wol->wolopts & ~WAKE_MAGIC)
558                 return -EINVAL;
559
560         if ((wol->wolopts & WAKE_MAGIC) && be_is_wol_supported(adapter))
561                 adapter->wol = true;
562         else
563                 adapter->wol = false;
564
565         return 0;
566 }
567
568 static int
569 be_test_ddr_dma(struct be_adapter *adapter)
570 {
571         int ret, i;
572         struct be_dma_mem ddrdma_cmd;
573         static const u64 pattern[2] = {
574                 0x5a5a5a5a5a5a5a5aULL, 0xa5a5a5a5a5a5a5a5ULL
575         };
576
577         ddrdma_cmd.size = sizeof(struct be_cmd_req_ddrdma_test);
578         ddrdma_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, ddrdma_cmd.size,
579                                            &ddrdma_cmd.dma, GFP_KERNEL);
580         if (!ddrdma_cmd.va) {
581                 dev_err(&adapter->pdev->dev, "Memory allocation failure\n");
582                 return -ENOMEM;
583         }
584
585         for (i = 0; i < 2; i++) {
586                 ret = be_cmd_ddr_dma_test(adapter, pattern[i],
587                                         4096, &ddrdma_cmd);
588                 if (ret != 0)
589                         goto err;
590         }
591
592 err:
593         dma_free_coherent(&adapter->pdev->dev, ddrdma_cmd.size, ddrdma_cmd.va,
594                           ddrdma_cmd.dma);
595         return ret;
596 }
597
598 static u64 be_loopback_test(struct be_adapter *adapter, u8 loopback_type,
599                                 u64 *status)
600 {
601         be_cmd_set_loopback(adapter, adapter->port_num,
602                                 loopback_type, 1);
603         *status = be_cmd_loopback_test(adapter, adapter->port_num,
604                                 loopback_type, 1500,
605                                 2, 0xabc);
606         be_cmd_set_loopback(adapter, adapter->port_num,
607                                 BE_NO_LOOPBACK, 1);
608         return *status;
609 }
610
611 static void
612 be_self_test(struct net_device *netdev, struct ethtool_test *test, u64 *data)
613 {
614         struct be_adapter *adapter = netdev_priv(netdev);
615         bool link_up;
616         u8 mac_speed = 0;
617         u16 qos_link_speed = 0;
618
619         memset(data, 0, sizeof(u64) * ETHTOOL_TESTS_NUM);
620
621         if (test->flags & ETH_TEST_FL_OFFLINE) {
622                 if (be_loopback_test(adapter, BE_MAC_LOOPBACK,
623                                                 &data[0]) != 0) {
624                         test->flags |= ETH_TEST_FL_FAILED;
625                 }
626                 if (be_loopback_test(adapter, BE_PHY_LOOPBACK,
627                                                 &data[1]) != 0) {
628                         test->flags |= ETH_TEST_FL_FAILED;
629                 }
630                 if (be_loopback_test(adapter, BE_ONE_PORT_EXT_LOOPBACK,
631                                                 &data[2]) != 0) {
632                         test->flags |= ETH_TEST_FL_FAILED;
633                 }
634         }
635
636         if (be_test_ddr_dma(adapter) != 0) {
637                 data[3] = 1;
638                 test->flags |= ETH_TEST_FL_FAILED;
639         }
640
641         if (be_cmd_link_status_query(adapter, &link_up, &mac_speed,
642                                 &qos_link_speed) != 0) {
643                 test->flags |= ETH_TEST_FL_FAILED;
644                 data[4] = -1;
645         } else if (mac_speed) {
646                 data[4] = 1;
647         }
648 }
649
650 static int
651 be_do_flash(struct net_device *netdev, struct ethtool_flash *efl)
652 {
653         struct be_adapter *adapter = netdev_priv(netdev);
654         char file_name[ETHTOOL_FLASH_MAX_FILENAME];
655         u32 region;
656
657         file_name[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
658         strcpy(file_name, efl->data);
659         region = efl->region;
660
661         return be_load_fw(adapter, file_name);
662 }
663
664 static int
665 be_get_eeprom_len(struct net_device *netdev)
666 {
667         return BE_READ_SEEPROM_LEN;
668 }
669
670 static int
671 be_read_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom,
672                         uint8_t *data)
673 {
674         struct be_adapter *adapter = netdev_priv(netdev);
675         struct be_dma_mem eeprom_cmd;
676         struct be_cmd_resp_seeprom_read *resp;
677         int status;
678
679         if (!eeprom->len)
680                 return -EINVAL;
681
682         eeprom->magic = BE_VENDOR_ID | (adapter->pdev->device<<16);
683
684         memset(&eeprom_cmd, 0, sizeof(struct be_dma_mem));
685         eeprom_cmd.size = sizeof(struct be_cmd_req_seeprom_read);
686         eeprom_cmd.va = dma_alloc_coherent(&adapter->pdev->dev, eeprom_cmd.size,
687                                            &eeprom_cmd.dma, GFP_KERNEL);
688
689         if (!eeprom_cmd.va) {
690                 dev_err(&adapter->pdev->dev,
691                         "Memory allocation failure. Could not read eeprom\n");
692                 return -ENOMEM;
693         }
694
695         status = be_cmd_get_seeprom_data(adapter, &eeprom_cmd);
696
697         if (!status) {
698                 resp = (struct be_cmd_resp_seeprom_read *) eeprom_cmd.va;
699                 memcpy(data, resp->seeprom_data + eeprom->offset, eeprom->len);
700         }
701         dma_free_coherent(&adapter->pdev->dev, eeprom_cmd.size, eeprom_cmd.va,
702                           eeprom_cmd.dma);
703
704         return status;
705 }
706
707 const struct ethtool_ops be_ethtool_ops = {
708         .get_settings = be_get_settings,
709         .get_drvinfo = be_get_drvinfo,
710         .get_wol = be_get_wol,
711         .set_wol = be_set_wol,
712         .get_link = ethtool_op_get_link,
713         .get_eeprom_len = be_get_eeprom_len,
714         .get_eeprom = be_read_eeprom,
715         .get_coalesce = be_get_coalesce,
716         .set_coalesce = be_set_coalesce,
717         .get_ringparam = be_get_ringparam,
718         .get_pauseparam = be_get_pauseparam,
719         .set_pauseparam = be_set_pauseparam,
720         .get_rx_csum = be_get_rx_csum,
721         .set_rx_csum = be_set_rx_csum,
722         .get_tx_csum = ethtool_op_get_tx_csum,
723         .set_tx_csum = ethtool_op_set_tx_hw_csum,
724         .get_sg = ethtool_op_get_sg,
725         .set_sg = ethtool_op_set_sg,
726         .get_tso = ethtool_op_get_tso,
727         .set_tso = ethtool_op_set_tso,
728         .get_strings = be_get_stat_strings,
729         .phys_id = be_phys_id,
730         .get_sset_count = be_get_sset_count,
731         .get_ethtool_stats = be_get_ethtool_stats,
732         .flash_device = be_do_flash,
733         .self_test = be_self_test,
734 };