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